api.c 2.6 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412614136141461415614166141761418614196142061421614226142361424614256142661427614286142961430614316143261433614346143561436614376143861439614406144161442614436144461445614466144761448614496145061451614526145361454614556145661457614586145961460614616146261463614646146561466614676146861469614706147161472614736147461475614766147761478614796148061481614826148361484614856148661487614886148961490614916149261493614946149561496614976149861499615006150161502615036150461505615066150761508615096151061511615126151361514615156151661517615186151961520615216152261523615246152561526615276152861529615306153161532615336153461535615366153761538615396154061541615426154361544615456154661547615486154961550615516155261553615546155561556615576155861559615606156161562615636156461565615666156761568615696157061571615726157361574615756157661577615786157961580615816158261583615846158561586615876158861589615906159161592615936159461595615966159761598615996160061601616026160361604616056160661607616086160961610616116161261613616146161561616616176161861619616206162161622616236162461625616266162761628616296163061631616326163361634616356163661637616386163961640616416164261643616446164561646616476164861649616506165161652616536165461655616566165761658616596166061661616626166361664616656166661667616686166961670616716167261673616746167561676616776167861679616806168161682616836168461685616866168761688616896169061691616926169361694616956169661697616986169961700617016170261703617046170561706617076170861709617106171161712617136171461715617166171761718617196172061721617226172361724617256172661727617286172961730617316173261733617346173561736617376173861739617406174161742617436174461745617466174761748617496175061751617526175361754617556175661757617586175961760617616176261763617646176561766617676176861769617706177161772617736177461775617766177761778617796178061781617826178361784617856178661787617886178961790617916179261793617946179561796617976179861799618006180161802618036180461805618066180761808618096181061811618126181361814618156181661817618186181961820618216182261823618246182561826618276182861829618306183161832618336183461835618366183761838618396184061841618426184361844618456184661847618486184961850618516185261853618546185561856618576185861859618606186161862618636186461865618666186761868618696187061871618726187361874618756187661877618786187961880618816188261883618846188561886618876188861889618906189161892618936189461895618966189761898618996190061901619026190361904619056190661907619086190961910619116191261913619146191561916619176191861919619206192161922619236192461925619266192761928619296193061931619326193361934619356193661937619386193961940619416194261943619446194561946619476194861949619506195161952619536195461955619566195761958619596196061961619626196361964619656196661967619686196961970619716197261973619746197561976619776197861979619806198161982619836198461985619866198761988619896199061991619926199361994619956199661997619986199962000620016200262003620046200562006620076200862009620106201162012620136201462015620166201762018620196202062021620226202362024620256202662027620286202962030620316203262033620346203562036620376203862039620406204162042620436204462045620466204762048620496205062051620526205362054620556205662057620586205962060620616206262063620646206562066620676206862069620706207162072620736207462075620766207762078620796208062081620826208362084620856208662087620886208962090620916209262093620946209562096620976209862099621006210162102621036210462105621066210762108621096211062111621126211362114621156211662117621186211962120621216212262123621246212562126621276212862129621306213162132621336213462135621366213762138621396214062141621426214362144621456214662147621486214962150621516215262153621546215562156621576215862159621606216162162621636216462165621666216762168621696217062171621726217362174621756217662177621786217962180621816218262183621846218562186621876218862189621906219162192621936219462195621966219762198621996220062201622026220362204622056220662207622086220962210622116221262213622146221562216622176221862219622206222162222622236222462225622266222762228622296223062231622326223362234622356223662237622386223962240622416224262243622446224562246622476224862249622506225162252622536225462255622566225762258622596226062261622626226362264622656226662267622686226962270622716227262273622746227562276622776227862279622806228162282622836228462285622866228762288622896229062291622926229362294622956229662297622986229962300623016230262303623046230562306623076230862309623106231162312623136231462315623166231762318623196232062321623226232362324623256232662327623286232962330623316233262333623346233562336623376233862339623406234162342623436234462345623466234762348623496235062351623526235362354623556235662357623586235962360623616236262363623646236562366623676236862369623706237162372623736237462375623766237762378623796238062381623826238362384623856238662387623886238962390623916239262393623946239562396623976239862399624006240162402624036240462405624066240762408624096241062411624126241362414624156241662417624186241962420624216242262423624246242562426624276242862429624306243162432624336243462435624366243762438624396244062441624426244362444624456244662447624486244962450624516245262453624546245562456624576245862459624606246162462624636246462465624666246762468624696247062471624726247362474624756247662477624786247962480624816248262483624846248562486624876248862489624906249162492624936249462495624966249762498624996250062501625026250362504625056250662507625086250962510625116251262513625146251562516625176251862519625206252162522625236252462525625266252762528625296253062531625326253362534625356253662537625386253962540625416254262543625446254562546625476254862549625506255162552625536255462555625566255762558625596256062561625626256362564625656256662567625686256962570625716257262573625746257562576625776257862579625806258162582625836258462585625866258762588625896259062591625926259362594625956259662597625986259962600626016260262603626046260562606626076260862609626106261162612626136261462615626166261762618626196262062621626226262362624626256262662627626286262962630626316263262633626346263562636626376263862639626406264162642626436264462645626466264762648626496265062651626526265362654626556265662657626586265962660626616266262663626646266562666626676266862669626706267162672626736267462675626766267762678626796268062681626826268362684626856268662687626886268962690626916269262693626946269562696626976269862699627006270162702627036270462705627066270762708627096271062711627126271362714627156271662717627186271962720627216272262723627246272562726627276272862729627306273162732627336273462735627366273762738627396274062741627426274362744627456274662747627486274962750627516275262753627546275562756627576275862759627606276162762627636276462765627666276762768627696277062771627726277362774627756277662777627786277962780627816278262783627846278562786627876278862789627906279162792627936279462795627966279762798627996280062801628026280362804628056280662807628086280962810628116281262813628146281562816628176281862819628206282162822628236282462825628266282762828628296283062831628326283362834628356283662837628386283962840628416284262843628446284562846628476284862849628506285162852628536285462855628566285762858628596286062861628626286362864628656286662867628686286962870628716287262873628746287562876628776287862879628806288162882628836288462885628866288762888628896289062891628926289362894628956289662897628986289962900629016290262903629046290562906629076290862909629106291162912629136291462915629166291762918629196292062921629226292362924629256292662927629286292962930629316293262933629346293562936629376293862939629406294162942629436294462945629466294762948629496295062951629526295362954629556295662957629586295962960629616296262963629646296562966629676296862969629706297162972629736297462975629766297762978629796298062981629826298362984629856298662987629886298962990629916299262993629946299562996629976299862999630006300163002630036300463005630066300763008630096301063011630126301363014630156301663017630186301963020630216302263023630246302563026630276302863029630306303163032630336303463035630366303763038630396304063041630426304363044630456304663047630486304963050630516305263053630546305563056630576305863059630606306163062630636306463065630666306763068630696307063071630726307363074630756307663077630786307963080630816308263083630846308563086630876308863089630906309163092630936309463095630966309763098630996310063101631026310363104631056310663107631086310963110631116311263113631146311563116631176311863119631206312163122631236312463125631266312763128631296313063131631326313363134631356313663137631386313963140631416314263143631446314563146631476314863149631506315163152631536315463155631566315763158631596316063161631626316363164631656316663167631686316963170631716317263173631746317563176631776317863179631806318163182631836318463185631866318763188631896319063191631926319363194631956319663197631986319963200632016320263203632046320563206632076320863209632106321163212632136321463215632166321763218632196322063221632226322363224632256322663227632286322963230632316323263233632346323563236632376323863239632406324163242632436324463245632466324763248632496325063251632526325363254632556325663257632586325963260632616326263263632646326563266632676326863269632706327163272632736327463275632766327763278632796328063281632826328363284632856328663287632886328963290632916329263293632946329563296632976329863299633006330163302633036330463305633066330763308633096331063311633126331363314633156331663317633186331963320633216332263323633246332563326633276332863329633306333163332633336333463335633366333763338633396334063341633426334363344633456334663347633486334963350633516335263353633546335563356633576335863359633606336163362633636336463365633666336763368633696337063371633726337363374633756337663377633786337963380633816338263383633846338563386633876338863389633906339163392633936339463395633966339763398633996340063401634026340363404634056340663407634086340963410634116341263413634146341563416634176341863419634206342163422634236342463425634266342763428634296343063431634326343363434634356343663437634386343963440634416344263443634446344563446634476344863449634506345163452634536345463455634566345763458634596346063461634626346363464634656346663467634686346963470634716347263473634746347563476634776347863479634806348163482634836348463485634866348763488634896349063491634926349363494634956349663497634986349963500635016350263503635046350563506635076350863509635106351163512635136351463515635166351763518635196352063521635226352363524635256352663527635286352963530635316353263533635346353563536635376353863539635406354163542635436354463545635466354763548635496355063551635526355363554635556355663557635586355963560635616356263563635646356563566635676356863569635706357163572635736357463575635766357763578635796358063581635826358363584635856358663587635886358963590635916359263593635946359563596635976359863599636006360163602636036360463605636066360763608636096361063611636126361363614636156361663617636186361963620636216362263623636246362563626636276362863629636306363163632636336363463635636366363763638636396364063641636426364363644636456364663647636486364963650636516365263653636546365563656636576365863659636606366163662636636366463665636666366763668636696367063671636726367363674636756367663677636786367963680636816368263683636846368563686636876368863689636906369163692636936369463695636966369763698636996370063701637026370363704637056370663707637086370963710637116371263713637146371563716637176371863719637206372163722637236372463725637266372763728637296373063731637326373363734637356373663737637386373963740637416374263743637446374563746637476374863749637506375163752637536375463755637566375763758637596376063761637626376363764637656376663767637686376963770637716377263773637746377563776637776377863779637806378163782637836378463785637866378763788637896379063791637926379363794637956379663797637986379963800638016380263803638046380563806638076380863809638106381163812638136381463815638166381763818638196382063821638226382363824638256382663827638286382963830638316383263833638346383563836638376383863839638406384163842638436384463845638466384763848638496385063851638526385363854638556385663857638586385963860638616386263863638646386563866638676386863869638706387163872638736387463875638766387763878638796388063881638826388363884638856388663887638886388963890638916389263893638946389563896638976389863899639006390163902639036390463905639066390763908639096391063911639126391363914639156391663917639186391963920639216392263923639246392563926639276392863929639306393163932639336393463935639366393763938639396394063941639426394363944639456394663947639486394963950639516395263953639546395563956639576395863959639606396163962639636396463965639666396763968639696397063971639726397363974639756397663977639786397963980639816398263983639846398563986639876398863989639906399163992639936399463995639966399763998639996400064001640026400364004640056400664007640086400964010640116401264013640146401564016640176401864019640206402164022640236402464025640266402764028640296403064031640326403364034640356403664037640386403964040640416404264043640446404564046640476404864049640506405164052640536405464055640566405764058640596406064061640626406364064640656406664067640686406964070640716407264073640746407564076640776407864079640806408164082640836408464085640866408764088640896409064091640926409364094640956409664097640986409964100641016410264103641046410564106641076410864109641106411164112641136411464115641166411764118641196412064121641226412364124641256412664127641286412964130641316413264133641346413564136641376413864139641406414164142641436414464145641466414764148641496415064151641526415364154641556415664157641586415964160641616416264163641646416564166641676416864169641706417164172641736417464175641766417764178641796418064181641826418364184641856418664187641886418964190641916419264193641946419564196641976419864199642006420164202642036420464205642066420764208642096421064211642126421364214642156421664217642186421964220642216422264223642246422564226642276422864229642306423164232642336423464235642366423764238642396424064241642426424364244642456424664247642486424964250642516425264253642546425564256642576425864259642606426164262642636426464265642666426764268642696427064271642726427364274642756427664277642786427964280642816428264283642846428564286642876428864289642906429164292642936429464295642966429764298642996430064301643026430364304643056430664307643086430964310643116431264313643146431564316643176431864319643206432164322643236432464325643266432764328643296433064331643326433364334643356433664337643386433964340643416434264343643446434564346643476434864349643506435164352643536435464355643566435764358643596436064361643626436364364643656436664367643686436964370643716437264373643746437564376643776437864379643806438164382643836438464385643866438764388643896439064391643926439364394643956439664397643986439964400644016440264403644046440564406644076440864409644106441164412644136441464415644166441764418644196442064421644226442364424644256442664427644286442964430644316443264433644346443564436644376443864439644406444164442644436444464445644466444764448644496445064451644526445364454644556445664457644586445964460644616446264463644646446564466644676446864469644706447164472644736447464475644766447764478644796448064481644826448364484644856448664487644886448964490644916449264493644946449564496644976449864499645006450164502645036450464505645066450764508645096451064511645126451364514645156451664517645186451964520645216452264523645246452564526645276452864529645306453164532645336453464535645366453764538645396454064541645426454364544645456454664547645486454964550645516455264553645546455564556645576455864559645606456164562645636456464565645666456764568645696457064571645726457364574645756457664577645786457964580645816458264583645846458564586645876458864589645906459164592645936459464595645966459764598645996460064601646026460364604646056460664607646086460964610646116461264613646146461564616646176461864619646206462164622646236462464625646266462764628646296463064631646326463364634646356463664637646386463964640646416464264643646446464564646646476464864649646506465164652646536465464655646566465764658646596466064661646626466364664646656466664667646686466964670646716467264673646746467564676646776467864679646806468164682646836468464685646866468764688646896469064691646926469364694646956469664697646986469964700647016470264703647046470564706647076470864709647106471164712647136471464715647166471764718647196472064721647226472364724647256472664727647286472964730647316473264733647346473564736647376473864739647406474164742647436474464745647466474764748647496475064751647526475364754647556475664757647586475964760647616476264763647646476564766647676476864769647706477164772647736477464775647766477764778647796478064781647826478364784647856478664787647886478964790647916479264793647946479564796647976479864799648006480164802648036480464805648066480764808648096481064811648126481364814648156481664817648186481964820648216482264823648246482564826648276482864829648306483164832648336483464835648366483764838648396484064841648426484364844648456484664847648486484964850648516485264853648546485564856648576485864859648606486164862648636486464865648666486764868648696487064871648726487364874648756487664877648786487964880648816488264883648846488564886648876488864889648906489164892648936489464895648966489764898648996490064901649026490364904649056490664907649086490964910649116491264913649146491564916649176491864919649206492164922649236492464925649266492764928649296493064931649326493364934649356493664937649386493964940649416494264943649446494564946649476494864949649506495164952649536495464955649566495764958649596496064961649626496364964649656496664967649686496964970649716497264973649746497564976649776497864979649806498164982649836498464985649866498764988649896499064991649926499364994649956499664997649986499965000650016500265003650046500565006650076500865009650106501165012650136501465015650166501765018650196502065021650226502365024650256502665027650286502965030650316503265033650346503565036650376503865039650406504165042650436504465045650466504765048650496505065051650526505365054650556505665057650586505965060650616506265063650646506565066650676506865069650706507165072650736507465075650766507765078650796508065081650826508365084650856508665087650886508965090650916509265093650946509565096650976509865099651006510165102651036510465105651066510765108651096511065111651126511365114651156511665117651186511965120651216512265123651246512565126651276512865129651306513165132651336513465135651366513765138651396514065141651426514365144651456514665147651486514965150651516515265153651546515565156651576515865159651606516165162651636516465165651666516765168651696517065171651726517365174651756517665177651786517965180651816518265183651846518565186651876518865189651906519165192651936519465195651966519765198651996520065201652026520365204652056520665207652086520965210652116521265213652146521565216652176521865219652206522165222652236522465225652266522765228652296523065231652326523365234652356523665237652386523965240652416524265243652446524565246652476524865249652506525165252652536525465255652566525765258652596526065261652626526365264652656526665267652686526965270652716527265273652746527565276652776527865279652806528165282652836528465285652866528765288652896529065291652926529365294652956529665297652986529965300653016530265303653046530565306653076530865309653106531165312653136531465315653166531765318653196532065321653226532365324653256532665327653286532965330653316533265333653346533565336653376533865339653406534165342653436534465345653466534765348653496535065351653526535365354653556535665357653586535965360653616536265363653646536565366653676536865369653706537165372653736537465375653766537765378653796538065381653826538365384653856538665387653886538965390653916539265393653946539565396653976539865399654006540165402654036540465405654066540765408654096541065411654126541365414654156541665417654186541965420654216542265423654246542565426654276542865429654306543165432654336543465435654366543765438654396544065441654426544365444654456544665447654486544965450654516545265453654546545565456654576545865459654606546165462654636546465465654666546765468654696547065471654726547365474654756547665477654786547965480654816548265483654846548565486654876548865489654906549165492654936549465495654966549765498654996550065501655026550365504655056550665507655086550965510655116551265513655146551565516655176551865519655206552165522655236552465525655266552765528655296553065531655326553365534655356553665537655386553965540655416554265543655446554565546655476554865549655506555165552655536555465555655566555765558655596556065561655626556365564655656556665567655686556965570655716557265573655746557565576655776557865579655806558165582655836558465585655866558765588655896559065591655926559365594655956559665597655986559965600656016560265603656046560565606656076560865609656106561165612656136561465615656166561765618656196562065621656226562365624656256562665627656286562965630656316563265633656346563565636656376563865639656406564165642656436564465645656466564765648656496565065651656526565365654656556565665657656586565965660656616566265663656646566565666656676566865669656706567165672656736567465675656766567765678656796568065681656826568365684656856568665687656886568965690656916569265693656946569565696656976569865699657006570165702657036570465705657066570765708657096571065711657126571365714657156571665717657186571965720657216572265723657246572565726657276572865729657306573165732657336573465735657366573765738657396574065741657426574365744657456574665747657486574965750657516575265753657546575565756657576575865759657606576165762657636576465765657666576765768657696577065771657726577365774657756577665777657786577965780657816578265783657846578565786657876578865789657906579165792657936579465795657966579765798657996580065801658026580365804658056580665807658086580965810658116581265813658146581565816658176581865819658206582165822658236582465825658266582765828658296583065831658326583365834658356583665837658386583965840658416584265843658446584565846658476584865849658506585165852658536585465855658566585765858658596586065861658626586365864658656586665867658686586965870658716587265873658746587565876658776587865879658806588165882658836588465885658866588765888658896589065891658926589365894658956589665897658986589965900659016590265903659046590565906659076590865909659106591165912659136591465915659166591765918659196592065921659226592365924659256592665927659286592965930659316593265933659346593565936659376593865939659406594165942659436594465945659466594765948659496595065951659526595365954659556595665957659586595965960659616596265963659646596565966659676596865969659706597165972659736597465975659766597765978659796598065981659826598365984659856598665987659886598965990659916599265993659946599565996659976599865999660006600166002660036600466005660066600766008660096601066011660126601366014660156601666017660186601966020660216602266023660246602566026660276602866029660306603166032660336603466035660366603766038660396604066041660426604366044660456604666047660486604966050660516605266053660546605566056660576605866059660606606166062660636606466065660666606766068660696607066071660726607366074660756607666077660786607966080660816608266083660846608566086660876608866089660906609166092660936609466095660966609766098660996610066101661026610366104661056610666107661086610966110661116611266113661146611566116661176611866119661206612166122661236612466125661266612766128661296613066131661326613366134661356613666137661386613966140661416614266143661446614566146661476614866149661506615166152661536615466155661566615766158661596616066161661626616366164661656616666167661686616966170661716617266173661746617566176661776617866179661806618166182661836618466185661866618766188661896619066191661926619366194661956619666197661986619966200662016620266203662046620566206662076620866209662106621166212662136621466215662166621766218662196622066221662226622366224662256622666227662286622966230662316623266233662346623566236662376623866239662406624166242662436624466245662466624766248662496625066251662526625366254662556625666257662586625966260662616626266263662646626566266662676626866269662706627166272662736627466275662766627766278662796628066281662826628366284662856628666287662886628966290662916629266293662946629566296662976629866299663006630166302663036630466305663066630766308663096631066311663126631366314663156631666317663186631966320663216632266323663246632566326663276632866329663306633166332663336633466335663366633766338663396634066341663426634366344663456634666347663486634966350663516635266353663546635566356663576635866359663606636166362663636636466365663666636766368663696637066371663726637366374663756637666377663786637966380663816638266383663846638566386663876638866389663906639166392663936639466395663966639766398663996640066401664026640366404664056640666407664086640966410664116641266413664146641566416664176641866419664206642166422664236642466425664266642766428664296643066431664326643366434664356643666437664386643966440664416644266443664446644566446664476644866449664506645166452664536645466455664566645766458664596646066461664626646366464664656646666467664686646966470664716647266473664746647566476664776647866479664806648166482664836648466485664866648766488664896649066491664926649366494664956649666497664986649966500665016650266503665046650566506665076650866509665106651166512665136651466515665166651766518665196652066521665226652366524665256652666527665286652966530665316653266533665346653566536665376653866539665406654166542665436654466545665466654766548665496655066551665526655366554665556655666557665586655966560665616656266563665646656566566665676656866569665706657166572665736657466575665766657766578665796658066581665826658366584665856658666587665886658966590665916659266593665946659566596665976659866599666006660166602666036660466605666066660766608666096661066611666126661366614666156661666617666186661966620666216662266623666246662566626666276662866629666306663166632666336663466635666366663766638666396664066641666426664366644666456664666647666486664966650666516665266653666546665566656666576665866659666606666166662666636666466665666666666766668666696667066671666726667366674666756667666677666786667966680666816668266683666846668566686666876668866689666906669166692666936669466695666966669766698666996670066701667026670366704667056670666707667086670966710667116671266713667146671566716667176671866719667206672166722667236672466725667266672766728667296673066731667326673366734667356673666737667386673966740667416674266743667446674566746667476674866749667506675166752667536675466755667566675766758667596676066761667626676366764667656676666767667686676966770667716677266773667746677566776667776677866779667806678166782667836678466785667866678766788667896679066791667926679366794667956679666797667986679966800668016680266803668046680566806668076680866809668106681166812668136681466815668166681766818668196682066821668226682366824668256682666827668286682966830668316683266833668346683566836668376683866839668406684166842668436684466845668466684766848668496685066851668526685366854668556685666857668586685966860668616686266863668646686566866668676686866869668706687166872668736687466875668766687766878668796688066881668826688366884668856688666887668886688966890668916689266893668946689566896668976689866899669006690166902669036690466905669066690766908669096691066911669126691366914669156691666917669186691966920669216692266923669246692566926669276692866929669306693166932669336693466935669366693766938669396694066941669426694366944669456694666947669486694966950669516695266953669546695566956669576695866959669606696166962669636696466965669666696766968669696697066971669726697366974669756697666977669786697966980669816698266983669846698566986669876698866989669906699166992669936699466995669966699766998669996700067001670026700367004670056700667007670086700967010670116701267013670146701567016670176701867019670206702167022670236702467025670266702767028670296703067031670326703367034670356703667037670386703967040670416704267043670446704567046670476704867049670506705167052670536705467055670566705767058670596706067061670626706367064670656706667067670686706967070670716707267073670746707567076670776707867079670806708167082670836708467085670866708767088670896709067091670926709367094670956709667097670986709967100671016710267103671046710567106671076710867109671106711167112671136711467115671166711767118671196712067121671226712367124671256712667127671286712967130671316713267133671346713567136671376713867139671406714167142671436714467145671466714767148671496715067151671526715367154671556715667157671586715967160671616716267163671646716567166671676716867169671706717167172671736717467175671766717767178671796718067181671826718367184671856718667187671886718967190671916719267193671946719567196671976719867199672006720167202672036720467205672066720767208672096721067211672126721367214672156721667217672186721967220672216722267223672246722567226672276722867229672306723167232672336723467235672366723767238672396724067241672426724367244672456724667247672486724967250672516725267253672546725567256672576725867259672606726167262672636726467265672666726767268672696727067271672726727367274672756727667277672786727967280672816728267283672846728567286672876728867289672906729167292672936729467295672966729767298672996730067301673026730367304673056730667307673086730967310673116731267313673146731567316673176731867319673206732167322673236732467325673266732767328673296733067331673326733367334673356733667337673386733967340673416734267343673446734567346673476734867349673506735167352673536735467355673566735767358673596736067361673626736367364673656736667367673686736967370673716737267373673746737567376673776737867379673806738167382673836738467385673866738767388673896739067391673926739367394673956739667397673986739967400674016740267403674046740567406674076740867409674106741167412674136741467415674166741767418674196742067421674226742367424674256742667427674286742967430674316743267433674346743567436674376743867439674406744167442674436744467445674466744767448674496745067451674526745367454674556745667457674586745967460674616746267463674646746567466674676746867469674706747167472674736747467475674766747767478674796748067481674826748367484674856748667487674886748967490674916749267493674946749567496674976749867499675006750167502675036750467505675066750767508675096751067511675126751367514675156751667517675186751967520675216752267523675246752567526675276752867529675306753167532675336753467535675366753767538675396754067541675426754367544675456754667547675486754967550675516755267553675546755567556675576755867559675606756167562675636756467565675666756767568675696757067571675726757367574675756757667577675786757967580675816758267583675846758567586675876758867589675906759167592675936759467595675966759767598675996760067601676026760367604676056760667607676086760967610676116761267613676146761567616676176761867619676206762167622676236762467625676266762767628676296763067631676326763367634676356763667637676386763967640676416764267643676446764567646676476764867649676506765167652676536765467655676566765767658676596766067661676626766367664676656766667667676686766967670676716767267673676746767567676676776767867679676806768167682676836768467685676866768767688676896769067691676926769367694676956769667697676986769967700677016770267703677046770567706677076770867709677106771167712677136771467715677166771767718677196772067721677226772367724677256772667727677286772967730677316773267733677346773567736677376773867739677406774167742677436774467745677466774767748677496775067751677526775367754677556775667757677586775967760677616776267763677646776567766677676776867769677706777167772677736777467775677766777767778677796778067781677826778367784677856778667787677886778967790677916779267793677946779567796677976779867799678006780167802678036780467805678066780767808678096781067811678126781367814678156781667817678186781967820678216782267823678246782567826678276782867829678306783167832678336783467835678366783767838678396784067841678426784367844678456784667847678486784967850678516785267853678546785567856678576785867859678606786167862678636786467865678666786767868678696787067871678726787367874678756787667877678786787967880678816788267883678846788567886678876788867889678906789167892678936789467895678966789767898678996790067901679026790367904679056790667907679086790967910679116791267913679146791567916679176791867919679206792167922679236792467925679266792767928679296793067931679326793367934679356793667937679386793967940679416794267943679446794567946679476794867949679506795167952679536795467955679566795767958679596796067961679626796367964679656796667967679686796967970679716797267973679746797567976679776797867979679806798167982679836798467985679866798767988679896799067991679926799367994679956799667997679986799968000680016800268003680046800568006680076800868009680106801168012680136801468015680166801768018680196802068021680226802368024680256802668027680286802968030680316803268033680346803568036680376803868039680406804168042680436804468045680466804768048680496805068051680526805368054680556805668057680586805968060680616806268063680646806568066680676806868069680706807168072680736807468075680766807768078680796808068081680826808368084680856808668087680886808968090680916809268093680946809568096680976809868099681006810168102681036810468105681066810768108681096811068111681126811368114681156811668117681186811968120681216812268123681246812568126681276812868129681306813168132681336813468135681366813768138681396814068141681426814368144681456814668147681486814968150681516815268153681546815568156681576815868159681606816168162681636816468165681666816768168681696817068171681726817368174681756817668177681786817968180681816818268183681846818568186681876818868189681906819168192681936819468195681966819768198681996820068201682026820368204682056820668207682086820968210682116821268213682146821568216682176821868219682206822168222682236822468225682266822768228682296823068231682326823368234682356823668237682386823968240682416824268243682446824568246682476824868249682506825168252682536825468255682566825768258682596826068261682626826368264682656826668267682686826968270682716827268273682746827568276682776827868279682806828168282682836828468285682866828768288682896829068291682926829368294682956829668297682986829968300683016830268303683046830568306683076830868309683106831168312683136831468315683166831768318683196832068321683226832368324683256832668327683286832968330683316833268333683346833568336683376833868339683406834168342683436834468345683466834768348683496835068351683526835368354683556835668357683586835968360683616836268363683646836568366683676836868369683706837168372683736837468375683766837768378683796838068381683826838368384683856838668387683886838968390683916839268393683946839568396683976839868399684006840168402684036840468405684066840768408684096841068411684126841368414684156841668417684186841968420684216842268423684246842568426684276842868429684306843168432684336843468435684366843768438684396844068441684426844368444684456844668447684486844968450684516845268453684546845568456684576845868459684606846168462684636846468465684666846768468684696847068471684726847368474684756847668477684786847968480684816848268483684846848568486684876848868489684906849168492684936849468495684966849768498684996850068501685026850368504685056850668507685086850968510685116851268513685146851568516685176851868519685206852168522685236852468525685266852768528685296853068531685326853368534685356853668537685386853968540685416854268543685446854568546685476854868549685506855168552685536855468555685566855768558685596856068561685626856368564685656856668567685686856968570685716857268573685746857568576685776857868579685806858168582685836858468585685866858768588685896859068591685926859368594685956859668597685986859968600686016860268603686046860568606686076860868609686106861168612686136861468615686166861768618686196862068621686226862368624686256862668627686286862968630686316863268633686346863568636686376863868639686406864168642686436864468645686466864768648686496865068651686526865368654686556865668657686586865968660686616866268663686646866568666686676866868669686706867168672686736867468675686766867768678686796868068681686826868368684686856868668687686886868968690686916869268693686946869568696686976869868699687006870168702687036870468705687066870768708687096871068711687126871368714687156871668717687186871968720687216872268723687246872568726687276872868729687306873168732687336873468735687366873768738687396874068741687426874368744687456874668747687486874968750687516875268753687546875568756687576875868759687606876168762687636876468765687666876768768687696877068771687726877368774687756877668777687786877968780687816878268783687846878568786687876878868789687906879168792687936879468795687966879768798687996880068801688026880368804688056880668807688086880968810688116881268813688146881568816688176881868819688206882168822688236882468825688266882768828688296883068831688326883368834688356883668837688386883968840688416884268843688446884568846688476884868849688506885168852688536885468855688566885768858688596886068861688626886368864688656886668867688686886968870688716887268873688746887568876688776887868879688806888168882688836888468885688866888768888688896889068891688926889368894688956889668897688986889968900689016890268903689046890568906689076890868909689106891168912689136891468915689166891768918689196892068921689226892368924689256892668927689286892968930689316893268933689346893568936689376893868939689406894168942689436894468945689466894768948689496895068951689526895368954689556895668957689586895968960689616896268963689646896568966689676896868969689706897168972689736897468975689766897768978689796898068981689826898368984689856898668987689886898968990689916899268993689946899568996689976899868999690006900169002690036900469005690066900769008690096901069011690126901369014690156901669017690186901969020690216902269023690246902569026690276902869029690306903169032690336903469035690366903769038690396904069041690426904369044690456904669047690486904969050690516905269053690546905569056690576905869059690606906169062690636906469065690666906769068690696907069071690726907369074690756907669077690786907969080690816908269083690846908569086690876908869089690906909169092690936909469095690966909769098690996910069101691026910369104691056910669107691086910969110691116911269113691146911569116691176911869119691206912169122691236912469125691266912769128691296913069131691326913369134691356913669137691386913969140691416914269143691446914569146691476914869149691506915169152691536915469155691566915769158691596916069161691626916369164691656916669167691686916969170691716917269173691746917569176691776917869179691806918169182691836918469185691866918769188691896919069191691926919369194691956919669197691986919969200692016920269203692046920569206692076920869209692106921169212692136921469215692166921769218692196922069221692226922369224692256922669227692286922969230692316923269233692346923569236692376923869239692406924169242692436924469245692466924769248692496925069251692526925369254692556925669257692586925969260692616926269263692646926569266692676926869269692706927169272692736927469275692766927769278692796928069281692826928369284692856928669287692886928969290692916929269293692946929569296692976929869299693006930169302693036930469305693066930769308693096931069311693126931369314693156931669317693186931969320693216932269323693246932569326693276932869329693306933169332693336933469335693366933769338693396934069341693426934369344693456934669347693486934969350693516935269353693546935569356693576935869359693606936169362693636936469365693666936769368693696937069371693726937369374693756937669377693786937969380693816938269383693846938569386693876938869389693906939169392693936939469395693966939769398693996940069401694026940369404694056940669407694086940969410694116941269413694146941569416694176941869419694206942169422694236942469425694266942769428694296943069431694326943369434694356943669437694386943969440694416944269443694446944569446694476944869449694506945169452694536945469455694566945769458694596946069461694626946369464694656946669467694686946969470694716947269473694746947569476694776947869479694806948169482694836948469485694866948769488694896949069491694926949369494694956949669497694986949969500695016950269503695046950569506695076950869509695106951169512695136951469515695166951769518695196952069521695226952369524695256952669527695286952969530695316953269533695346953569536695376953869539695406954169542695436954469545695466954769548695496955069551695526955369554695556955669557695586955969560695616956269563695646956569566695676956869569695706957169572695736957469575695766957769578695796958069581695826958369584695856958669587695886958969590695916959269593695946959569596695976959869599696006960169602696036960469605696066960769608696096961069611696126961369614696156961669617696186961969620696216962269623696246962569626696276962869629696306963169632696336963469635696366963769638696396964069641696426964369644696456964669647696486964969650696516965269653696546965569656696576965869659696606966169662696636966469665696666966769668696696967069671696726967369674696756967669677696786967969680696816968269683696846968569686696876968869689696906969169692696936969469695696966969769698696996970069701697026970369704697056970669707697086970969710697116971269713697146971569716697176971869719697206972169722697236972469725697266972769728697296973069731697326973369734697356973669737697386973969740697416974269743697446974569746697476974869749697506975169752697536975469755697566975769758697596976069761697626976369764697656976669767697686976969770697716977269773697746977569776697776977869779697806978169782697836978469785697866978769788697896979069791697926979369794697956979669797697986979969800698016980269803698046980569806698076980869809698106981169812698136981469815698166981769818698196982069821698226982369824698256982669827698286982969830698316983269833698346983569836698376983869839698406984169842698436984469845698466984769848698496985069851698526985369854698556985669857698586985969860698616986269863698646986569866698676986869869698706987169872698736987469875698766987769878698796988069881698826988369884698856988669887698886988969890698916989269893698946989569896698976989869899699006990169902699036990469905699066990769908699096991069911699126991369914699156991669917699186991969920699216992269923699246992569926699276992869929699306993169932699336993469935699366993769938699396994069941699426994369944699456994669947699486994969950699516995269953699546995569956699576995869959699606996169962699636996469965699666996769968699696997069971699726997369974699756997669977699786997969980699816998269983699846998569986699876998869989699906999169992699936999469995699966999769998699997000070001700027000370004700057000670007700087000970010700117001270013700147001570016700177001870019700207002170022700237002470025700267002770028700297003070031700327003370034700357003670037700387003970040700417004270043700447004570046700477004870049700507005170052700537005470055700567005770058700597006070061700627006370064700657006670067700687006970070700717007270073700747007570076700777007870079700807008170082700837008470085700867008770088700897009070091700927009370094700957009670097700987009970100701017010270103701047010570106701077010870109701107011170112701137011470115701167011770118701197012070121701227012370124701257012670127701287012970130701317013270133701347013570136701377013870139701407014170142701437014470145701467014770148701497015070151701527015370154701557015670157701587015970160701617016270163701647016570166701677016870169701707017170172701737017470175701767017770178701797018070181701827018370184701857018670187701887018970190701917019270193701947019570196701977019870199702007020170202702037020470205702067020770208702097021070211702127021370214702157021670217702187021970220702217022270223702247022570226702277022870229702307023170232702337023470235702367023770238702397024070241702427024370244702457024670247702487024970250702517025270253702547025570256702577025870259702607026170262702637026470265702667026770268702697027070271702727027370274702757027670277702787027970280702817028270283702847028570286702877028870289702907029170292702937029470295702967029770298702997030070301703027030370304703057030670307703087030970310703117031270313703147031570316703177031870319703207032170322703237032470325703267032770328703297033070331703327033370334703357033670337703387033970340703417034270343703447034570346703477034870349703507035170352703537035470355703567035770358703597036070361703627036370364703657036670367703687036970370703717037270373703747037570376703777037870379703807038170382703837038470385703867038770388703897039070391703927039370394703957039670397703987039970400704017040270403704047040570406704077040870409704107041170412704137041470415704167041770418704197042070421704227042370424704257042670427704287042970430704317043270433704347043570436704377043870439704407044170442704437044470445704467044770448704497045070451704527045370454704557045670457704587045970460704617046270463704647046570466704677046870469704707047170472704737047470475704767047770478704797048070481704827048370484704857048670487704887048970490704917049270493704947049570496704977049870499705007050170502705037050470505705067050770508705097051070511705127051370514705157051670517705187051970520705217052270523705247052570526705277052870529705307053170532705337053470535705367053770538705397054070541705427054370544705457054670547705487054970550705517055270553705547055570556705577055870559705607056170562705637056470565705667056770568705697057070571705727057370574705757057670577705787057970580705817058270583705847058570586705877058870589705907059170592705937059470595705967059770598705997060070601706027060370604706057060670607706087060970610706117061270613706147061570616706177061870619706207062170622706237062470625706267062770628706297063070631706327063370634706357063670637706387063970640706417064270643706447064570646706477064870649706507065170652706537065470655706567065770658706597066070661706627066370664706657066670667706687066970670706717067270673706747067570676706777067870679706807068170682706837068470685706867068770688706897069070691706927069370694706957069670697706987069970700707017070270703707047070570706707077070870709707107071170712707137071470715707167071770718707197072070721707227072370724707257072670727707287072970730707317073270733707347073570736707377073870739707407074170742707437074470745707467074770748707497075070751707527075370754707557075670757707587075970760707617076270763707647076570766707677076870769707707077170772707737077470775707767077770778707797078070781707827078370784707857078670787707887078970790707917079270793707947079570796707977079870799708007080170802708037080470805708067080770808708097081070811708127081370814708157081670817708187081970820708217082270823708247082570826708277082870829708307083170832708337083470835708367083770838708397084070841708427084370844708457084670847708487084970850708517085270853708547085570856708577085870859708607086170862708637086470865708667086770868708697087070871708727087370874708757087670877708787087970880708817088270883708847088570886708877088870889708907089170892708937089470895708967089770898708997090070901709027090370904709057090670907709087090970910709117091270913709147091570916709177091870919709207092170922709237092470925709267092770928709297093070931709327093370934709357093670937709387093970940709417094270943709447094570946709477094870949709507095170952709537095470955709567095770958709597096070961709627096370964709657096670967709687096970970709717097270973709747097570976709777097870979709807098170982709837098470985709867098770988709897099070991709927099370994709957099670997709987099971000710017100271003710047100571006710077100871009710107101171012710137101471015710167101771018710197102071021710227102371024710257102671027710287102971030710317103271033710347103571036710377103871039710407104171042710437104471045710467104771048710497105071051710527105371054710557105671057710587105971060710617106271063710647106571066710677106871069710707107171072710737107471075710767107771078710797108071081710827108371084710857108671087710887108971090710917109271093710947109571096710977109871099711007110171102711037110471105711067110771108711097111071111711127111371114711157111671117711187111971120711217112271123711247112571126711277112871129711307113171132711337113471135711367113771138711397114071141711427114371144711457114671147711487114971150711517115271153711547115571156711577115871159711607116171162711637116471165711667116771168711697117071171711727117371174711757117671177711787117971180711817118271183711847118571186711877118871189711907119171192711937119471195711967119771198711997120071201712027120371204712057120671207712087120971210712117121271213712147121571216712177121871219712207122171222712237122471225712267122771228712297123071231712327123371234712357123671237712387123971240712417124271243712447124571246712477124871249712507125171252712537125471255712567125771258712597126071261712627126371264712657126671267712687126971270712717127271273712747127571276712777127871279712807128171282712837128471285712867128771288712897129071291712927129371294712957129671297712987129971300713017130271303713047130571306713077130871309713107131171312713137131471315713167131771318713197132071321713227132371324713257132671327713287132971330713317133271333713347133571336713377133871339713407134171342713437134471345713467134771348713497135071351713527135371354713557135671357713587135971360713617136271363713647136571366713677136871369713707137171372713737137471375713767137771378713797138071381713827138371384713857138671387713887138971390713917139271393713947139571396713977139871399714007140171402714037140471405714067140771408714097141071411714127141371414714157141671417714187141971420714217142271423714247142571426714277142871429714307143171432714337143471435714367143771438714397144071441714427144371444714457144671447714487144971450714517145271453714547145571456714577145871459714607146171462714637146471465714667146771468714697147071471714727147371474714757147671477714787147971480714817148271483714847148571486714877148871489714907149171492714937149471495714967149771498714997150071501715027150371504715057150671507715087150971510715117151271513715147151571516715177151871519715207152171522715237152471525715267152771528715297153071531715327153371534715357153671537715387153971540715417154271543715447154571546715477154871549715507155171552715537155471555715567155771558715597156071561715627156371564715657156671567715687156971570715717157271573715747157571576715777157871579715807158171582715837158471585715867158771588715897159071591715927159371594715957159671597715987159971600716017160271603716047160571606716077160871609716107161171612716137161471615716167161771618716197162071621716227162371624716257162671627716287162971630716317163271633716347163571636716377163871639716407164171642716437164471645716467164771648716497165071651716527165371654716557165671657716587165971660716617166271663716647166571666716677166871669716707167171672716737167471675716767167771678716797168071681716827168371684716857168671687716887168971690716917169271693716947169571696716977169871699717007170171702717037170471705717067170771708717097171071711717127171371714717157171671717717187171971720717217172271723717247172571726717277172871729717307173171732717337173471735717367173771738717397174071741717427174371744717457174671747717487174971750717517175271753717547175571756717577175871759717607176171762717637176471765717667176771768717697177071771717727177371774717757177671777717787177971780717817178271783717847178571786717877178871789717907179171792717937179471795717967179771798717997180071801718027180371804718057180671807718087180971810718117181271813718147181571816718177181871819718207182171822718237182471825718267182771828718297183071831718327183371834718357183671837718387183971840718417184271843718447184571846718477184871849718507185171852718537185471855718567185771858718597186071861718627186371864718657186671867718687186971870718717187271873718747187571876718777187871879718807188171882718837188471885718867188771888718897189071891718927189371894718957189671897718987189971900719017190271903719047190571906719077190871909719107191171912719137191471915719167191771918719197192071921719227192371924719257192671927719287192971930719317193271933719347193571936719377193871939719407194171942719437194471945719467194771948719497195071951719527195371954719557195671957719587195971960719617196271963719647196571966719677196871969719707197171972719737197471975719767197771978719797198071981719827198371984719857198671987719887198971990719917199271993719947199571996719977199871999720007200172002720037200472005720067200772008720097201072011720127201372014720157201672017720187201972020720217202272023720247202572026720277202872029720307203172032720337203472035720367203772038720397204072041720427204372044720457204672047720487204972050720517205272053720547205572056720577205872059720607206172062720637206472065720667206772068720697207072071720727207372074720757207672077720787207972080720817208272083720847208572086720877208872089720907209172092720937209472095720967209772098720997210072101721027210372104721057210672107721087210972110721117211272113721147211572116721177211872119721207212172122721237212472125721267212772128721297213072131721327213372134721357213672137721387213972140721417214272143721447214572146721477214872149721507215172152721537215472155721567215772158721597216072161721627216372164721657216672167721687216972170721717217272173721747217572176721777217872179721807218172182721837218472185721867218772188721897219072191721927219372194721957219672197721987219972200722017220272203722047220572206722077220872209722107221172212722137221472215722167221772218722197222072221722227222372224722257222672227722287222972230722317223272233722347223572236722377223872239722407224172242722437224472245722467224772248722497225072251722527225372254722557225672257722587225972260722617226272263722647226572266722677226872269722707227172272722737227472275722767227772278722797228072281722827228372284722857228672287722887228972290722917229272293722947229572296722977229872299723007230172302723037230472305723067230772308723097231072311723127231372314723157231672317723187231972320723217232272323723247232572326723277232872329723307233172332723337233472335723367233772338723397234072341723427234372344723457234672347723487234972350723517235272353723547235572356723577235872359723607236172362723637236472365723667236772368723697237072371723727237372374723757237672377723787237972380723817238272383723847238572386723877238872389723907239172392723937239472395723967239772398723997240072401724027240372404724057240672407724087240972410724117241272413724147241572416724177241872419724207242172422724237242472425724267242772428724297243072431724327243372434724357243672437724387243972440724417244272443724447244572446724477244872449724507245172452724537245472455724567245772458724597246072461724627246372464724657246672467724687246972470724717247272473724747247572476724777247872479724807248172482724837248472485724867248772488724897249072491724927249372494724957249672497724987249972500725017250272503725047250572506725077250872509725107251172512725137251472515725167251772518725197252072521725227252372524725257252672527725287252972530725317253272533725347253572536725377253872539725407254172542725437254472545725467254772548725497255072551725527255372554725557255672557725587255972560725617256272563725647256572566725677256872569725707257172572725737257472575725767257772578725797258072581725827258372584725857258672587725887258972590725917259272593725947259572596725977259872599726007260172602726037260472605726067260772608726097261072611726127261372614726157261672617726187261972620726217262272623726247262572626726277262872629726307263172632726337263472635726367263772638726397264072641726427264372644726457264672647726487264972650726517265272653726547265572656726577265872659726607266172662726637266472665726667266772668726697267072671726727267372674726757267672677726787267972680726817268272683726847268572686726877268872689726907269172692726937269472695726967269772698726997270072701727027270372704727057270672707727087270972710727117271272713727147271572716727177271872719727207272172722727237272472725727267272772728727297273072731727327273372734727357273672737727387273972740727417274272743727447274572746727477274872749727507275172752727537275472755727567275772758727597276072761727627276372764727657276672767727687276972770727717277272773727747277572776727777277872779727807278172782727837278472785727867278772788727897279072791727927279372794727957279672797727987279972800728017280272803728047280572806728077280872809728107281172812728137281472815728167281772818728197282072821728227282372824728257282672827728287282972830728317283272833728347283572836728377283872839728407284172842728437284472845728467284772848728497285072851728527285372854728557285672857728587285972860728617286272863728647286572866728677286872869728707287172872728737287472875728767287772878728797288072881728827288372884728857288672887728887288972890728917289272893728947289572896728977289872899729007290172902729037290472905729067290772908729097291072911729127291372914729157291672917729187291972920729217292272923729247292572926729277292872929729307293172932729337293472935729367293772938729397294072941729427294372944729457294672947729487294972950729517295272953729547295572956729577295872959729607296172962729637296472965729667296772968729697297072971729727297372974729757297672977729787297972980729817298272983729847298572986729877298872989729907299172992729937299472995729967299772998729997300073001730027300373004730057300673007730087300973010730117301273013730147301573016730177301873019730207302173022730237302473025730267302773028730297303073031730327303373034730357303673037730387303973040730417304273043730447304573046730477304873049730507305173052730537305473055730567305773058730597306073061730627306373064730657306673067730687306973070730717307273073730747307573076730777307873079730807308173082730837308473085730867308773088730897309073091730927309373094730957309673097730987309973100731017310273103731047310573106731077310873109731107311173112731137311473115731167311773118731197312073121731227312373124731257312673127731287312973130731317313273133731347313573136731377313873139731407314173142731437314473145731467314773148731497315073151731527315373154731557315673157731587315973160731617316273163731647316573166731677316873169731707317173172731737317473175731767317773178731797318073181731827318373184731857318673187731887318973190731917319273193731947319573196731977319873199732007320173202732037320473205732067320773208732097321073211732127321373214732157321673217732187321973220732217322273223732247322573226732277322873229732307323173232732337323473235732367323773238732397324073241732427324373244732457324673247732487324973250732517325273253732547325573256732577325873259732607326173262732637326473265732667326773268732697327073271732727327373274732757327673277732787327973280732817328273283732847328573286732877328873289732907329173292732937329473295732967329773298732997330073301733027330373304733057330673307733087330973310733117331273313733147331573316733177331873319733207332173322733237332473325733267332773328733297333073331733327333373334733357333673337733387333973340733417334273343733447334573346733477334873349733507335173352733537335473355733567335773358733597336073361733627336373364733657336673367733687336973370733717337273373733747337573376733777337873379733807338173382733837338473385733867338773388733897339073391733927339373394733957339673397733987339973400734017340273403734047340573406734077340873409734107341173412734137341473415734167341773418734197342073421734227342373424734257342673427734287342973430734317343273433734347343573436734377343873439734407344173442734437344473445734467344773448734497345073451734527345373454734557345673457734587345973460734617346273463734647346573466734677346873469734707347173472734737347473475734767347773478734797348073481734827348373484734857348673487734887348973490734917349273493734947349573496734977349873499735007350173502735037350473505735067350773508735097351073511735127351373514735157351673517735187351973520735217352273523735247352573526735277352873529735307353173532735337353473535735367353773538735397354073541735427354373544735457354673547735487354973550735517355273553735547355573556735577355873559735607356173562735637356473565735667356773568735697357073571735727357373574735757357673577735787357973580735817358273583735847358573586735877358873589735907359173592735937359473595735967359773598735997360073601736027360373604736057360673607736087360973610736117361273613736147361573616736177361873619736207362173622736237362473625736267362773628736297363073631736327363373634736357363673637736387363973640736417364273643736447364573646736477364873649736507365173652736537365473655736567365773658736597366073661736627366373664736657366673667736687366973670736717367273673736747367573676736777367873679736807368173682736837368473685736867368773688736897369073691736927369373694736957369673697736987369973700737017370273703737047370573706737077370873709737107371173712737137371473715737167371773718737197372073721737227372373724737257372673727737287372973730737317373273733737347373573736737377373873739737407374173742737437374473745737467374773748737497375073751737527375373754737557375673757737587375973760737617376273763737647376573766737677376873769737707377173772737737377473775737767377773778737797378073781737827378373784737857378673787737887378973790737917379273793737947379573796737977379873799738007380173802738037380473805738067380773808738097381073811738127381373814738157381673817738187381973820738217382273823738247382573826738277382873829738307383173832738337383473835738367383773838738397384073841738427384373844738457384673847738487384973850738517385273853738547385573856738577385873859738607386173862738637386473865738667386773868738697387073871738727387373874738757387673877738787387973880738817388273883738847388573886738877388873889738907389173892738937389473895738967389773898738997390073901739027390373904739057390673907739087390973910739117391273913739147391573916739177391873919739207392173922739237392473925739267392773928739297393073931739327393373934739357393673937739387393973940739417394273943739447394573946739477394873949739507395173952739537395473955739567395773958739597396073961739627396373964739657396673967739687396973970739717397273973739747397573976739777397873979739807398173982739837398473985739867398773988739897399073991739927399373994739957399673997739987399974000740017400274003740047400574006740077400874009740107401174012740137401474015740167401774018740197402074021740227402374024740257402674027740287402974030740317403274033740347403574036740377403874039740407404174042740437404474045740467404774048740497405074051740527405374054740557405674057740587405974060740617406274063740647406574066740677406874069740707407174072740737407474075740767407774078740797408074081740827408374084740857408674087740887408974090740917409274093740947409574096740977409874099741007410174102741037410474105741067410774108741097411074111741127411374114741157411674117741187411974120741217412274123741247412574126741277412874129741307413174132741337413474135741367413774138741397414074141741427414374144741457414674147741487414974150741517415274153741547415574156741577415874159741607416174162741637416474165741667416774168741697417074171741727417374174741757417674177741787417974180741817418274183741847418574186741877418874189741907419174192741937419474195741967419774198741997420074201742027420374204742057420674207742087420974210742117421274213742147421574216742177421874219742207422174222742237422474225742267422774228742297423074231742327423374234742357423674237742387423974240742417424274243742447424574246742477424874249742507425174252742537425474255742567425774258742597426074261742627426374264742657426674267742687426974270742717427274273742747427574276742777427874279742807428174282742837428474285742867428774288742897429074291742927429374294742957429674297742987429974300743017430274303743047430574306743077430874309743107431174312743137431474315743167431774318743197432074321743227432374324743257432674327743287432974330743317433274333743347433574336743377433874339743407434174342743437434474345743467434774348743497435074351743527435374354743557435674357743587435974360743617436274363743647436574366743677436874369743707437174372743737437474375743767437774378743797438074381743827438374384743857438674387743887438974390743917439274393743947439574396743977439874399744007440174402744037440474405744067440774408744097441074411744127441374414744157441674417744187441974420744217442274423744247442574426744277442874429744307443174432744337443474435744367443774438744397444074441744427444374444744457444674447744487444974450744517445274453744547445574456744577445874459744607446174462744637446474465744667446774468744697447074471744727447374474744757447674477744787447974480744817448274483744847448574486744877448874489744907449174492744937449474495744967449774498744997450074501745027450374504745057450674507745087450974510745117451274513745147451574516745177451874519745207452174522745237452474525745267452774528745297453074531745327453374534745357453674537745387453974540745417454274543745447454574546745477454874549745507455174552745537455474555745567455774558745597456074561745627456374564745657456674567745687456974570745717457274573745747457574576745777457874579745807458174582745837458474585745867458774588745897459074591745927459374594745957459674597745987459974600746017460274603746047460574606746077460874609746107461174612746137461474615746167461774618746197462074621746227462374624746257462674627746287462974630746317463274633746347463574636746377463874639746407464174642746437464474645746467464774648746497465074651746527465374654746557465674657746587465974660746617466274663746647466574666746677466874669746707467174672746737467474675746767467774678746797468074681746827468374684746857468674687746887468974690746917469274693746947469574696746977469874699747007470174702747037470474705747067470774708747097471074711747127471374714747157471674717747187471974720747217472274723747247472574726747277472874729747307473174732747337473474735747367473774738747397474074741747427474374744747457474674747747487474974750747517475274753747547475574756747577475874759747607476174762747637476474765747667476774768747697477074771747727477374774747757477674777747787477974780747817478274783747847478574786747877478874789747907479174792747937479474795747967479774798747997480074801748027480374804748057480674807748087480974810748117481274813748147481574816748177481874819748207482174822748237482474825748267482774828748297483074831748327483374834748357483674837748387483974840748417484274843748447484574846748477484874849748507485174852748537485474855748567485774858748597486074861748627486374864748657486674867748687486974870748717487274873748747487574876748777487874879748807488174882748837488474885748867488774888748897489074891748927489374894748957489674897748987489974900749017490274903749047490574906749077490874909749107491174912749137491474915749167491774918749197492074921749227492374924749257492674927749287492974930749317493274933749347493574936749377493874939749407494174942749437494474945749467494774948749497495074951749527495374954749557495674957749587495974960749617496274963749647496574966749677496874969749707497174972749737497474975749767497774978749797498074981749827498374984749857498674987749887498974990749917499274993749947499574996749977499874999750007500175002750037500475005750067500775008750097501075011750127501375014750157501675017750187501975020750217502275023750247502575026750277502875029750307503175032750337503475035750367503775038750397504075041750427504375044750457504675047750487504975050750517505275053750547505575056750577505875059750607506175062750637506475065750667506775068750697507075071750727507375074750757507675077750787507975080750817508275083750847508575086750877508875089750907509175092750937509475095750967509775098750997510075101751027510375104751057510675107751087510975110751117511275113751147511575116751177511875119751207512175122751237512475125751267512775128751297513075131751327513375134751357513675137751387513975140751417514275143751447514575146751477514875149751507515175152751537515475155751567515775158751597516075161751627516375164751657516675167751687516975170751717517275173751747517575176751777517875179751807518175182751837518475185751867518775188751897519075191751927519375194751957519675197751987519975200752017520275203752047520575206752077520875209752107521175212752137521475215752167521775218752197522075221752227522375224752257522675227752287522975230752317523275233752347523575236752377523875239752407524175242752437524475245752467524775248752497525075251752527525375254752557525675257752587525975260752617526275263752647526575266752677526875269752707527175272752737527475275752767527775278752797528075281752827528375284752857528675287752887528975290752917529275293752947529575296752977529875299753007530175302753037530475305753067530775308753097531075311753127531375314753157531675317753187531975320753217532275323753247532575326753277532875329753307533175332753337533475335753367533775338753397534075341753427534375344753457534675347753487534975350753517535275353753547535575356753577535875359753607536175362753637536475365753667536775368753697537075371753727537375374753757537675377753787537975380753817538275383753847538575386753877538875389753907539175392753937539475395753967539775398753997540075401754027540375404754057540675407754087540975410754117541275413754147541575416754177541875419754207542175422754237542475425754267542775428754297543075431754327543375434754357543675437754387543975440754417544275443754447544575446754477544875449754507545175452754537545475455754567545775458754597546075461754627546375464754657546675467754687546975470754717547275473754747547575476754777547875479754807548175482754837548475485754867548775488754897549075491754927549375494754957549675497754987549975500755017550275503755047550575506755077550875509755107551175512755137551475515755167551775518755197552075521755227552375524755257552675527755287552975530755317553275533755347553575536755377553875539755407554175542755437554475545755467554775548755497555075551755527555375554755557555675557755587555975560755617556275563755647556575566755677556875569755707557175572755737557475575755767557775578755797558075581755827558375584755857558675587755887558975590755917559275593755947559575596755977559875599756007560175602756037560475605756067560775608756097561075611756127561375614756157561675617756187561975620756217562275623756247562575626756277562875629756307563175632756337563475635756367563775638756397564075641756427564375644756457564675647756487564975650756517565275653756547565575656756577565875659756607566175662756637566475665756667566775668756697567075671756727567375674756757567675677756787567975680756817568275683756847568575686756877568875689756907569175692756937569475695756967569775698756997570075701757027570375704757057570675707757087570975710757117571275713757147571575716757177571875719757207572175722757237572475725757267572775728757297573075731757327573375734757357573675737757387573975740757417574275743757447574575746757477574875749757507575175752757537575475755757567575775758757597576075761757627576375764757657576675767757687576975770757717577275773757747577575776757777577875779757807578175782757837578475785757867578775788757897579075791757927579375794757957579675797757987579975800758017580275803758047580575806758077580875809758107581175812758137581475815758167581775818758197582075821758227582375824758257582675827758287582975830758317583275833758347583575836758377583875839758407584175842758437584475845758467584775848758497585075851758527585375854758557585675857758587585975860758617586275863758647586575866758677586875869758707587175872758737587475875758767587775878758797588075881758827588375884758857588675887758887588975890758917589275893758947589575896758977589875899759007590175902759037590475905759067590775908759097591075911759127591375914759157591675917759187591975920759217592275923759247592575926759277592875929759307593175932759337593475935759367593775938759397594075941759427594375944759457594675947759487594975950759517595275953759547595575956759577595875959759607596175962759637596475965759667596775968759697597075971759727597375974759757597675977759787597975980759817598275983759847598575986759877598875989759907599175992759937599475995759967599775998759997600076001760027600376004760057600676007760087600976010760117601276013760147601576016760177601876019760207602176022760237602476025760267602776028
  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_DILITHIUM
  265. #include <wolfssl/wolfcrypt/dilithium.h>
  266. #endif
  267. #ifdef HAVE_PKCS12
  268. #include <wolfssl/wolfcrypt/pkcs12.h>
  269. #endif
  270. #include <wolfssl/wolfcrypt/logging.h>
  271. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  272. #include <wolfssl/openssl/ssl.h>
  273. #ifndef NO_ASN
  274. /* for ASN_COMMON_NAME DN_tags enum */
  275. #include <wolfssl/wolfcrypt/asn.h>
  276. #endif
  277. #ifdef HAVE_OCSP
  278. #include <wolfssl/openssl/ocsp.h>
  279. #endif
  280. #endif
  281. #ifdef OPENSSL_EXTRA
  282. #include <wolfssl/openssl/cmac.h>
  283. #include <wolfssl/openssl/x509v3.h>
  284. #include <wolfssl/openssl/asn1.h>
  285. #include <wolfssl/openssl/crypto.h>
  286. #include <wolfssl/openssl/pkcs12.h>
  287. #include <wolfssl/openssl/evp.h>
  288. #include <wolfssl/openssl/dh.h>
  289. #include <wolfssl/openssl/bn.h>
  290. #include <wolfssl/openssl/buffer.h>
  291. #include <wolfssl/openssl/pem.h>
  292. #include <wolfssl/openssl/ec.h>
  293. #include <wolfssl/openssl/ecdh.h>
  294. #include <wolfssl/openssl/engine.h>
  295. #include <wolfssl/openssl/hmac.h>
  296. #include <wolfssl/openssl/objects.h>
  297. #include <wolfssl/openssl/rand.h>
  298. #include <wolfssl/openssl/modes.h>
  299. #include <wolfssl/openssl/fips_rand.h>
  300. #include <wolfssl/openssl/kdf.h>
  301. #ifdef OPENSSL_ALL
  302. #include <wolfssl/openssl/txt_db.h>
  303. #include <wolfssl/openssl/lhash.h>
  304. #endif
  305. #ifndef NO_AES
  306. #include <wolfssl/openssl/aes.h>
  307. #endif
  308. #ifndef NO_DES3
  309. #include <wolfssl/openssl/des.h>
  310. #endif
  311. #ifndef NO_RC4
  312. #include <wolfssl/openssl/rc4.h>
  313. #endif
  314. #ifdef HAVE_ECC
  315. #include <wolfssl/openssl/ecdsa.h>
  316. #endif
  317. #ifdef HAVE_PKCS7
  318. #include <wolfssl/openssl/pkcs7.h>
  319. #endif
  320. #ifdef HAVE_CURVE25519
  321. #include <wolfssl/openssl/ec25519.h>
  322. #endif
  323. #ifdef HAVE_ED25519
  324. #include <wolfssl/openssl/ed25519.h>
  325. #endif
  326. #ifdef HAVE_CURVE448
  327. #include <wolfssl/openssl/ec448.h>
  328. #endif
  329. #ifdef HAVE_ED448
  330. #include <wolfssl/openssl/ed448.h>
  331. #endif
  332. #endif /* OPENSSL_EXTRA */
  333. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  334. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  335. #include <wolfssl/wolfcrypt/srp.h>
  336. #endif
  337. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  338. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  339. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  340. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  341. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  342. || !defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13)
  343. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  344. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  345. * number tracking */
  346. #include "wolfssl/internal.h"
  347. #endif
  348. /* force enable test buffers */
  349. #ifndef USE_CERT_BUFFERS_2048
  350. #define USE_CERT_BUFFERS_2048
  351. #endif
  352. #ifndef USE_CERT_BUFFERS_256
  353. #define USE_CERT_BUFFERS_256
  354. #endif
  355. #include <wolfssl/certs_test.h>
  356. #include "tests/utils.h"
  357. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  358. * have default (hidden) visibility, and in the absence of visibility, it's
  359. * benign to mask out the library implementation.
  360. */
  361. #define WOLFSSL_MISC_INCLUDED
  362. #include <wolfcrypt/src/misc.c>
  363. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  364. /* FIPS build has replaced ecc.h. */
  365. #define wc_ecc_key_get_priv(key) (&((key)->k))
  366. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  367. #endif
  368. typedef struct testVector {
  369. const char* input;
  370. const char* output;
  371. size_t inLen;
  372. size_t outLen;
  373. } testVector;
  374. #if defined(HAVE_PKCS7)
  375. typedef struct {
  376. const byte* content;
  377. word32 contentSz;
  378. int contentOID;
  379. int encryptOID;
  380. int keyWrapOID;
  381. int keyAgreeOID;
  382. byte* cert;
  383. size_t certSz;
  384. byte* privateKey;
  385. word32 privateKeySz;
  386. } pkcs7EnvelopedVector;
  387. #ifndef NO_PKCS7_ENCRYPTED_DATA
  388. typedef struct {
  389. const byte* content;
  390. word32 contentSz;
  391. int contentOID;
  392. int encryptOID;
  393. byte* encryptionKey;
  394. word32 encryptionKeySz;
  395. } pkcs7EncryptedVector;
  396. #endif
  397. #endif /* HAVE_PKCS7 */
  398. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  399. typedef int (*ssl_cb)(WOLFSSL* ssl);
  400. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  401. typedef int (*hs_cb)(WOLFSSL_CTX **ctx, WOLFSSL **ssl);
  402. typedef struct test_ssl_cbf {
  403. method_provider method;
  404. ctx_cb ctx_ready;
  405. ssl_cb ssl_ready;
  406. ssl_cb on_result;
  407. ctx_cb on_ctx_cleanup;
  408. ssl_cb on_cleanup;
  409. hs_cb on_handshake;
  410. WOLFSSL_CTX* ctx;
  411. const char* caPemFile;
  412. const char* certPemFile;
  413. const char* keyPemFile;
  414. const char* crlPemFile;
  415. #ifdef WOLFSSL_STATIC_MEMORY
  416. byte* mem;
  417. word32 memSz;
  418. wolfSSL_method_func method_ex;
  419. #endif
  420. int devId;
  421. int return_code;
  422. int last_err;
  423. unsigned char isSharedCtx:1;
  424. unsigned char loadToSSL:1;
  425. unsigned char ticNoInit:1;
  426. unsigned char doUdp:1;
  427. } test_ssl_cbf;
  428. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  429. typedef struct test_ssl_memio_ctx {
  430. WOLFSSL_CTX* s_ctx;
  431. WOLFSSL_CTX* c_ctx;
  432. WOLFSSL* s_ssl;
  433. WOLFSSL* c_ssl;
  434. const char* c_ciphers;
  435. const char* s_ciphers;
  436. char* c_msg;
  437. int c_msglen;
  438. char* s_msg;
  439. int s_msglen;
  440. test_ssl_cbf s_cb;
  441. test_ssl_cbf c_cb;
  442. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  443. int c_len;
  444. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  445. int s_len;
  446. } test_ssl_memio_ctx;
  447. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  448. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  449. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  450. const char* currentTestName;
  451. char tmpDirName[16];
  452. int tmpDirNameSet = 0;
  453. #endif
  454. /*----------------------------------------------------------------------------*
  455. | Constants
  456. *----------------------------------------------------------------------------*/
  457. /* Test result constants and macros. */
  458. /* Test succeeded. */
  459. #define TEST_SUCCESS (1)
  460. /* Test failed. */
  461. #define TEST_FAIL (0)
  462. /* Test skipped - not run. */
  463. #define TEST_SKIPPED (-7777)
  464. /* Returns the result based on whether check is true.
  465. *
  466. * @param [in] check Condition for success.
  467. * @return When condition is true: TEST_SUCCESS.
  468. * @return When condition is false: TEST_FAIL.
  469. */
  470. #ifdef DEBUG_WOLFSSL_VERBOSE
  471. #define XSTRINGIFY(s) STRINGIFY(s)
  472. #define STRINGIFY(s) #s
  473. #define TEST_RES_CHECK(check) ({ \
  474. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  475. if (_ret == TEST_FAIL) { \
  476. fprintf(stderr, " check \"%s\" at %d ", \
  477. XSTRINGIFY(check), __LINE__); \
  478. } \
  479. _ret; })
  480. #else
  481. #define TEST_RES_CHECK(check) \
  482. ((check) ? TEST_SUCCESS : TEST_FAIL)
  483. #endif /* DEBUG_WOLFSSL_VERBOSE */
  484. #define TEST_STRING "Everyone gets Friday off."
  485. #define TEST_STRING_SZ 25
  486. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  487. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  488. #define TEST_RSA_BITS 1024
  489. #else
  490. #define TEST_RSA_BITS 2048
  491. #endif
  492. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  493. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  494. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  495. static const char* bogusFile =
  496. #ifdef _WIN32
  497. "NUL"
  498. #else
  499. "/dev/null"
  500. #endif
  501. ;
  502. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  503. enum {
  504. TESTING_RSA = 1,
  505. TESTING_ECC = 2
  506. };
  507. #ifdef WOLFSSL_QNX_CAAM
  508. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  509. static int testDevId = WOLFSSL_CAAM_DEVID;
  510. #else
  511. static int testDevId = INVALID_DEVID;
  512. #endif
  513. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  514. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  515. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  516. #define HAVE_IO_TESTS_DEPENDENCIES
  517. #endif
  518. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  519. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  520. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  521. #endif
  522. /*----------------------------------------------------------------------------*
  523. | BIO with fixed read/write size
  524. *----------------------------------------------------------------------------*/
  525. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  526. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  527. int len)
  528. {
  529. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  530. len = 0;
  531. }
  532. else {
  533. if (bio->wrSz - bio->wrIdx < len) {
  534. len = bio->wrSz - bio->wrIdx;
  535. }
  536. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  537. bio->wrIdx += len;
  538. }
  539. return len;
  540. }
  541. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  542. {
  543. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  544. len = 0;
  545. }
  546. else {
  547. if (bio->wrSz - bio->rdIdx < len) {
  548. len = bio->wrSz - bio->rdIdx;
  549. }
  550. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  551. bio->rdIdx += len;
  552. }
  553. return len;
  554. }
  555. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  556. {
  557. static WOLFSSL_BIO_METHOD meth;
  558. meth.type = WOLFSSL_BIO_BIO;
  559. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  560. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  561. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  562. return &meth;
  563. }
  564. #endif
  565. /*----------------------------------------------------------------------------*
  566. | Setup
  567. *----------------------------------------------------------------------------*/
  568. static int test_wolfSSL_Init(void)
  569. {
  570. EXPECT_DECLS;
  571. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  572. return EXPECT_RESULT();
  573. }
  574. static int test_wolfSSL_Cleanup(void)
  575. {
  576. EXPECT_DECLS;
  577. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  578. return EXPECT_RESULT();
  579. }
  580. /* Initialize the wolfCrypt state.
  581. * POST: 0 success.
  582. */
  583. static int test_wolfCrypt_Init(void)
  584. {
  585. EXPECT_DECLS;
  586. ExpectIntEQ(wolfCrypt_Init(), 0);
  587. return EXPECT_RESULT();
  588. } /* END test_wolfCrypt_Init */
  589. static int test_wolfCrypt_Cleanup(void)
  590. {
  591. EXPECT_DECLS;
  592. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  593. return EXPECT_RESULT();
  594. }
  595. #ifdef WOLFSSL_STATIC_MEMORY
  596. #define TEST_LSM_STATIC_SIZE 440000
  597. /* Create new bucket list, using the default list, adding
  598. * one dang large buffer size. */
  599. #define TEST_LSM_DEF_BUCKETS (WOLFMEM_DEF_BUCKETS+1)
  600. #define TEST_LSM_BUCKETS WOLFMEM_BUCKETS,(LARGEST_MEM_BUCKET*2)
  601. #define TEST_LSM_DIST WOLFMEM_DIST,1
  602. #endif
  603. static int test_wc_LoadStaticMemory_ex(void)
  604. {
  605. EXPECT_DECLS;
  606. #ifdef WOLFSSL_STATIC_MEMORY
  607. byte staticMemory[TEST_LSM_STATIC_SIZE];
  608. word32 sizeList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_BUCKETS };
  609. word32 distList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_DIST };
  610. WOLFSSL_HEAP_HINT* heap;
  611. /* For this test, the size and dist lists will be the ones configured
  612. * for the build, or default. The value of WOLFMEM_DEF_BUCKETS is 9,
  613. * so these lists are 10 long. For most tests, the value of
  614. * WOLFMEM_DEF_BUCKETS is used. There's a test case where one is added
  615. * to that, to make sure the list size is larger than
  616. * WOLFMEM_MAX_BUCKETS. */
  617. /* Pass in zero everything. */
  618. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL, 0, NULL, NULL, NULL, 0, 0, 0),
  619. BAD_FUNC_ARG);
  620. /* Set the heap pointer to NULL. */
  621. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL,
  622. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  623. staticMemory, (word32)sizeof(staticMemory),
  624. 0, 1),
  625. BAD_FUNC_ARG);
  626. /* Set other pointer values to NULL one at a time. */
  627. heap = NULL;
  628. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  629. WOLFMEM_DEF_BUCKETS, NULL, distList,
  630. staticMemory, (word32)sizeof(staticMemory),
  631. 0, 1),
  632. BAD_FUNC_ARG);
  633. heap = NULL;
  634. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  635. WOLFMEM_DEF_BUCKETS, sizeList, NULL,
  636. staticMemory, (word32)sizeof(staticMemory),
  637. 0, 1),
  638. BAD_FUNC_ARG);
  639. heap = NULL;
  640. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  641. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  642. NULL, (word32)sizeof(staticMemory),
  643. 0, 1),
  644. BAD_FUNC_ARG);
  645. /* Set the size of the static buffer to 0. */
  646. heap = NULL;
  647. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  648. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  649. staticMemory, 0,
  650. 0, 1),
  651. BUFFER_E);
  652. /* Set the size of the static buffer to one less than minimum allowed. */
  653. heap = NULL;
  654. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  655. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  656. staticMemory,
  657. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) - 1,
  658. 0, 1),
  659. BUFFER_E);
  660. /* Set the size of the static buffer to exactly the minimum size. */
  661. heap = NULL;
  662. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  663. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  664. staticMemory,
  665. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)),
  666. 0, 1),
  667. 0);
  668. wc_UnloadStaticMemory(heap);
  669. /* Use more buckets than able. Success case. */
  670. heap = NULL;
  671. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  672. WOLFMEM_DEF_BUCKETS*2, sizeList, distList,
  673. staticMemory, (word32)sizeof(staticMemory),
  674. 0, 1),
  675. 0);
  676. wc_UnloadStaticMemory(heap);
  677. /* Success case. */
  678. heap = NULL;
  679. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  680. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  681. staticMemory, (word32)sizeof(staticMemory),
  682. 0, 1),
  683. 0);
  684. wc_UnloadStaticMemory(heap);
  685. #endif /* WOLFSSL_STATIC_MEMORY */
  686. return EXPECT_RESULT();
  687. }
  688. /*----------------------------------------------------------------------------*
  689. | Platform dependent function test
  690. *----------------------------------------------------------------------------*/
  691. static int test_fileAccess(void)
  692. {
  693. EXPECT_DECLS;
  694. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  695. const char *fname[] = {
  696. svrCertFile, svrKeyFile, caCertFile,
  697. eccCertFile, eccKeyFile, eccRsaCertFile,
  698. cliCertFile, cliCertDerFile, cliKeyFile,
  699. dhParamFile,
  700. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  701. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  702. NULL
  703. };
  704. const char derfile[] = "./certs/server-cert.der";
  705. XFILE f = XBADFILE;
  706. size_t sz;
  707. byte *buff = NULL;
  708. int i;
  709. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  710. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  711. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  712. XFCLOSE(f);
  713. }
  714. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  715. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  716. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  717. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  718. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  719. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  720. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  721. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  722. XFCLOSE(f);
  723. #endif
  724. return EXPECT_RESULT();
  725. }
  726. /*----------------------------------------------------------------------------*
  727. | Method Allocators
  728. *----------------------------------------------------------------------------*/
  729. static int test_wolfSSL_Method_Allocators(void)
  730. {
  731. EXPECT_DECLS;
  732. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  733. do { \
  734. WOLFSSL_METHOD *method = NULL; \
  735. condition(method = allocator()); \
  736. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  737. } while (0)
  738. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  739. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  740. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  741. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  742. #ifndef NO_OLD_TLS
  743. #ifdef WOLFSSL_ALLOW_SSLV3
  744. #ifndef NO_WOLFSSL_SERVER
  745. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  746. #endif
  747. #ifndef NO_WOLFSSL_CLIENT
  748. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  749. #endif
  750. #endif
  751. #ifdef WOLFSSL_ALLOW_TLSV10
  752. #ifndef NO_WOLFSSL_SERVER
  753. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  754. #endif
  755. #ifndef NO_WOLFSSL_CLIENT
  756. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  757. #endif
  758. #endif
  759. #ifndef NO_WOLFSSL_SERVER
  760. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  761. #endif
  762. #ifndef NO_WOLFSSL_CLIENT
  763. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  764. #endif
  765. #endif /* !NO_OLD_TLS */
  766. #ifndef WOLFSSL_NO_TLS12
  767. #ifndef NO_WOLFSSL_SERVER
  768. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  769. #endif
  770. #ifndef NO_WOLFSSL_CLIENT
  771. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  772. #endif
  773. #endif /* !WOLFSSL_NO_TLS12 */
  774. #ifdef WOLFSSL_TLS13
  775. #ifndef NO_WOLFSSL_SERVER
  776. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  777. #endif
  778. #ifndef NO_WOLFSSL_CLIENT
  779. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  780. #endif
  781. #endif /* WOLFSSL_TLS13 */
  782. #ifndef NO_WOLFSSL_SERVER
  783. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  784. #endif
  785. #ifndef NO_WOLFSSL_CLIENT
  786. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  787. #endif
  788. #ifdef WOLFSSL_DTLS
  789. #ifndef NO_OLD_TLS
  790. #ifndef NO_WOLFSSL_SERVER
  791. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  792. #endif
  793. #ifndef NO_WOLFSSL_CLIENT
  794. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  795. #endif
  796. #endif
  797. #ifndef WOLFSSL_NO_TLS12
  798. #ifndef NO_WOLFSSL_SERVER
  799. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  800. #endif
  801. #ifndef NO_WOLFSSL_CLIENT
  802. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  803. #endif
  804. #endif
  805. #endif /* WOLFSSL_DTLS */
  806. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  807. /* Stubs */
  808. #ifndef NO_WOLFSSL_SERVER
  809. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  810. #endif
  811. #ifndef NO_WOLFSSL_CLIENT
  812. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  813. #endif
  814. #endif
  815. /* Test Either Method (client or server) */
  816. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  817. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  818. #ifndef NO_OLD_TLS
  819. #ifdef WOLFSSL_ALLOW_TLSV10
  820. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  821. #endif
  822. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  823. #endif /* !NO_OLD_TLS */
  824. #ifndef WOLFSSL_NO_TLS12
  825. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  826. #endif /* !WOLFSSL_NO_TLS12 */
  827. #ifdef WOLFSSL_TLS13
  828. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  829. #endif /* WOLFSSL_TLS13 */
  830. #ifdef WOLFSSL_DTLS
  831. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  832. #ifndef NO_OLD_TLS
  833. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  834. #endif /* !NO_OLD_TLS */
  835. #ifndef WOLFSSL_NO_TLS12
  836. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  837. #endif /* !WOLFSSL_NO_TLS12 */
  838. #endif /* WOLFSSL_DTLS */
  839. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  840. return EXPECT_RESULT();
  841. }
  842. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  843. /*----------------------------------------------------------------------------*
  844. | Dual algorithm Certificate Tests
  845. *----------------------------------------------------------------------------*/
  846. #define LARGE_TEMP_SZ 4096
  847. /* To better understand this, please see the X9.146 example in wolfssl-examples
  848. * repo. */
  849. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  850. char *sapkiFile, char *altPrivFile)
  851. {
  852. EXPECT_DECLS;
  853. FILE* file = NULL;
  854. Cert newCert;
  855. DecodedCert preTBS;
  856. byte caKeyBuf[LARGE_TEMP_SZ];
  857. word32 caKeySz = LARGE_TEMP_SZ;
  858. byte sapkiBuf[LARGE_TEMP_SZ];
  859. word32 sapkiSz = LARGE_TEMP_SZ;
  860. byte altPrivBuf[LARGE_TEMP_SZ];
  861. word32 altPrivSz = LARGE_TEMP_SZ;
  862. byte altSigAlgBuf[LARGE_TEMP_SZ];
  863. word32 altSigAlgSz = LARGE_TEMP_SZ;
  864. byte scratchBuf[LARGE_TEMP_SZ];
  865. word32 scratchSz = LARGE_TEMP_SZ;
  866. byte preTbsBuf[LARGE_TEMP_SZ];
  867. word32 preTbsSz = LARGE_TEMP_SZ;
  868. byte altSigValBuf[LARGE_TEMP_SZ];
  869. word32 altSigValSz = LARGE_TEMP_SZ;
  870. byte *outBuf = NULL;
  871. word32 outSz = LARGE_TEMP_SZ;
  872. WC_RNG rng;
  873. RsaKey caKey;
  874. ecc_key altCaKey;
  875. word32 idx = 0;
  876. XMEMSET(&rng, 0, sizeof(WC_RNG));
  877. XMEMSET(&caKey, 0, sizeof(RsaKey));
  878. XMEMSET(&altCaKey, 0, sizeof(ecc_key));
  879. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  880. DYNAMIC_TYPE_TMP_BUFFER));
  881. ExpectIntEQ(wc_InitRng(&rng), 0);
  882. XMEMSET(caKeyBuf, 0, caKeySz);
  883. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  884. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  885. if (file) {
  886. fclose(file);
  887. file = NULL;
  888. }
  889. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  890. idx = 0;
  891. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  892. 0);
  893. XMEMSET(sapkiBuf, 0, sapkiSz);
  894. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  895. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  896. if (file) {
  897. fclose(file);
  898. file = NULL;
  899. }
  900. XMEMSET(altPrivBuf, 0, altPrivSz);
  901. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  902. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  903. if (file) {
  904. fclose(file);
  905. file = NULL;
  906. }
  907. wc_ecc_init(&altCaKey);
  908. idx = 0;
  909. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  910. (word32)altPrivSz), 0);
  911. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  912. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  913. oidSigType, 0), 0);
  914. wc_InitCert(&newCert);
  915. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  916. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  917. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  918. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  919. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  920. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  921. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  922. newCert.sigType = CTC_SHA256wRSA;
  923. newCert.isCA = 1;
  924. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  925. (const byte *)"This is NOT a critical extension", 32), 0);
  926. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  927. sapkiSz), 0);
  928. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  929. altSigAlgSz), 0);
  930. XMEMSET(scratchBuf, 0, scratchSz);
  931. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  932. &caKey, &rng), 0);
  933. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  934. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  935. XMEMSET(preTbsBuf, 0, preTbsSz);
  936. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  937. XMEMSET(altSigValBuf, 0, altSigValSz);
  938. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  939. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  940. &rng), 0);
  941. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  942. altSigValSz), 0);
  943. /* Finally, generate the new certificate. */
  944. if (outBuf != NULL) {
  945. XMEMSET(outBuf, 0, outSz);
  946. }
  947. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  948. 0);
  949. *out = outBuf;
  950. wc_FreeRsaKey(&caKey);
  951. wc_FreeRng(&rng);
  952. wc_FreeDecodedCert(&preTBS);
  953. return outSz;
  954. }
  955. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  956. char *sapkiFile, char *altPrivFile,
  957. char *serverKeyFile,
  958. byte *caCertBuf, int caCertSz)
  959. {
  960. EXPECT_DECLS;
  961. FILE* file = NULL;
  962. Cert newCert;
  963. DecodedCert preTBS;
  964. byte serverKeyBuf[LARGE_TEMP_SZ];
  965. word32 serverKeySz = LARGE_TEMP_SZ;
  966. byte caKeyBuf[LARGE_TEMP_SZ];
  967. word32 caKeySz = LARGE_TEMP_SZ;
  968. byte sapkiBuf[LARGE_TEMP_SZ];
  969. word32 sapkiSz = LARGE_TEMP_SZ;
  970. byte altPrivBuf[LARGE_TEMP_SZ];
  971. word32 altPrivSz = LARGE_TEMP_SZ;
  972. byte altSigAlgBuf[LARGE_TEMP_SZ];
  973. word32 altSigAlgSz = LARGE_TEMP_SZ;
  974. byte scratchBuf[LARGE_TEMP_SZ];
  975. word32 scratchSz = LARGE_TEMP_SZ;
  976. byte preTbsBuf[LARGE_TEMP_SZ];
  977. word32 preTbsSz = LARGE_TEMP_SZ;
  978. byte altSigValBuf[LARGE_TEMP_SZ];
  979. word32 altSigValSz = LARGE_TEMP_SZ;
  980. byte *outBuf = NULL;
  981. word32 outSz = LARGE_TEMP_SZ;
  982. WC_RNG rng;
  983. RsaKey caKey;
  984. RsaKey serverKey;
  985. ecc_key altCaKey;
  986. word32 idx = 0;
  987. XMEMSET(&rng, 0, sizeof(WC_RNG));
  988. XMEMSET(&caKey, 0, sizeof(RsaKey));
  989. XMEMSET(&serverKey, 0, sizeof(RsaKey));
  990. XMEMSET(&altCaKey, 0, sizeof(ecc_key));
  991. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  992. DYNAMIC_TYPE_TMP_BUFFER));
  993. ExpectIntEQ(wc_InitRng(&rng), 0);
  994. XMEMSET(serverKeyBuf, 0, serverKeySz);
  995. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  996. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  997. 0);
  998. if (file) {
  999. fclose(file);
  1000. file = NULL;
  1001. }
  1002. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  1003. idx = 0;
  1004. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  1005. (word32)serverKeySz), 0);
  1006. XMEMSET(caKeyBuf, 0, caKeySz);
  1007. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  1008. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  1009. if (file) {
  1010. fclose(file);
  1011. file = NULL;
  1012. }
  1013. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  1014. idx = 0;
  1015. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  1016. (word32)caKeySz), 0);
  1017. XMEMSET(sapkiBuf, 0, sapkiSz);
  1018. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  1019. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  1020. if (file) {
  1021. fclose(file);
  1022. file = NULL;
  1023. }
  1024. XMEMSET(altPrivBuf, 0, altPrivSz);
  1025. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  1026. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  1027. if (file) {
  1028. fclose(file);
  1029. file = NULL;
  1030. }
  1031. wc_ecc_init(&altCaKey);
  1032. idx = 0;
  1033. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  1034. (word32)altPrivSz), 0);
  1035. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  1036. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  1037. oidSigType, 0), 0);
  1038. wc_InitCert(&newCert);
  1039. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  1040. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  1041. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  1042. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  1043. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  1044. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  1045. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  1046. newCert.sigType = CTC_SHA256wRSA;
  1047. newCert.isCA = 0;
  1048. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  1049. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  1050. (const byte *)"This is NOT a critical extension", 32), 0);
  1051. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  1052. sapkiSz), 0);
  1053. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  1054. altSigAlgSz), 0);
  1055. XMEMSET(scratchBuf, 0, scratchSz);
  1056. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  1057. &rng), 0);
  1058. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  1059. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  1060. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  1061. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  1062. XMEMSET(preTbsBuf, 0, preTbsSz);
  1063. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  1064. XMEMSET(altSigValBuf, 0, altSigValSz);
  1065. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  1066. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  1067. &rng), 0);
  1068. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  1069. altSigValBuf, altSigValSz), 0);
  1070. /* Finally, generate the new certificate. */
  1071. if (outBuf != NULL) {
  1072. XMEMSET(outBuf, 0, outSz);
  1073. }
  1074. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  1075. 0);
  1076. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  1077. outSz, &caKey, NULL, &rng), 0);
  1078. *out = outBuf;
  1079. wc_FreeRsaKey(&caKey);
  1080. wc_FreeRsaKey(&serverKey);
  1081. wc_FreeRng(&rng);
  1082. wc_FreeDecodedCert(&preTBS);
  1083. return outSz;
  1084. }
  1085. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  1086. byte *serverCert, word32 serverCertSz,
  1087. byte *serverKey, word32 serverKeySz,
  1088. int negative_test)
  1089. {
  1090. EXPECT_DECLS;
  1091. WOLFSSL_CTX *ctx_c = NULL;
  1092. WOLFSSL_CTX *ctx_s = NULL;
  1093. WOLFSSL *ssl_c = NULL;
  1094. WOLFSSL *ssl_s = NULL;
  1095. struct test_memio_ctx test_ctx;
  1096. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  1097. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  1098. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  1099. caCert, caCertSz, serverCert, serverCertSz,
  1100. serverKey, serverKeySz), 0);
  1101. if (negative_test) {
  1102. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  1103. }
  1104. else {
  1105. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  1106. }
  1107. wolfSSL_free(ssl_c);
  1108. wolfSSL_free(ssl_s);
  1109. wolfSSL_CTX_free(ctx_c);
  1110. wolfSSL_CTX_free(ctx_s);
  1111. return EXPECT_RESULT();
  1112. }
  1113. static int extCount = 0;
  1114. static int myUnknownExtCallback(const word16* oid, word32 oidSz, int crit,
  1115. const unsigned char* der, word32 derSz)
  1116. {
  1117. (void) oid;
  1118. (void) oidSz;
  1119. (void) crit;
  1120. (void) der;
  1121. (void) derSz;
  1122. extCount ++;
  1123. /* Accept all extensions. This is only a test. Normally we would be much more
  1124. * careful about critical extensions. */
  1125. return 1;
  1126. }
  1127. static int test_dual_alg_support(void)
  1128. {
  1129. EXPECT_DECLS;
  1130. /* Root CA and server keys will be the same. This is only appropriate for
  1131. * testing. */
  1132. char keyFile[] = "./certs/ca-key.der";
  1133. char sapkiFile[] = "./certs/ecc-keyPub.der";
  1134. char altPrivFile[] = "./certs/ecc-key.der";
  1135. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1136. byte *serverKey = NULL;
  1137. size_t serverKeySz = 0;
  1138. byte *root = NULL;
  1139. int rootSz = 0;
  1140. byte *server = NULL;
  1141. int serverSz = 0;
  1142. WOLFSSL_CERT_MANAGER* cm = NULL;
  1143. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1144. /* Base normal case. */
  1145. if (EXPECT_SUCCESS()) {
  1146. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile,
  1147. altPrivFile);
  1148. }
  1149. ExpectNotNull(root);
  1150. ExpectIntGT(rootSz, 0);
  1151. if (EXPECT_SUCCESS()) {
  1152. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1153. altPrivFile, keyFile, root, rootSz);
  1154. }
  1155. ExpectNotNull(server);
  1156. ExpectIntGT(serverSz, 0);
  1157. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1158. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1159. TEST_SUCCESS);
  1160. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1161. root = NULL;
  1162. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1163. server = NULL;
  1164. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1165. * the alternative signature and then set negative_test to true for the
  1166. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1167. * because the signature won't verify. The exception is if
  1168. * WOLFSSL_TRUST_PEER_CERT is defined. In that case, no verfication happens
  1169. * and this is no longer a negative test. */
  1170. if (EXPECT_SUCCESS()) {
  1171. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile,
  1172. wrongPrivFile);
  1173. }
  1174. ExpectNotNull(root);
  1175. ExpectIntGT(rootSz, 0);
  1176. if (EXPECT_SUCCESS()) {
  1177. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1178. wrongPrivFile, keyFile, root, rootSz);
  1179. }
  1180. ExpectNotNull(server);
  1181. ExpectIntGT(serverSz, 0);
  1182. #ifdef WOLFSSL_TRUST_PEER_CERT
  1183. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1184. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1185. TEST_SUCCESS);
  1186. #else
  1187. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1188. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1189. TEST_SUCCESS);
  1190. #endif
  1191. /* Lets see if CertManager can find the new extensions */
  1192. extCount = 0;
  1193. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1194. wolfSSL_CertManagerSetUnknownExtCallback(cm, myUnknownExtCallback);
  1195. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, root, rootSz,
  1196. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1197. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server, serverSz,
  1198. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1199. /* There is only 1 unknown extension (1.2.3.4.5). The other ones are known
  1200. * because they are for the dual alg extensions. */
  1201. ExpectIntEQ(extCount, 1);
  1202. wolfSSL_CertManagerFree(cm);
  1203. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1204. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1205. free(serverKey);
  1206. return EXPECT_RESULT();
  1207. }
  1208. #else
  1209. static int test_dual_alg_support(void)
  1210. {
  1211. return TEST_SKIPPED;
  1212. }
  1213. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1214. /*----------------------------------------------------------------------------*
  1215. | Context
  1216. *----------------------------------------------------------------------------*/
  1217. #ifndef NO_WOLFSSL_SERVER
  1218. static int test_wolfSSL_CTX_new(void)
  1219. {
  1220. EXPECT_DECLS;
  1221. WOLFSSL_CTX *ctx;
  1222. WOLFSSL_METHOD* method;
  1223. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1224. ExpectNotNull(method = wolfSSLv23_server_method());
  1225. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1226. wolfSSL_CTX_free(ctx);
  1227. return EXPECT_RESULT();
  1228. }
  1229. #endif
  1230. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1231. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1232. static int test_for_double_Free(void)
  1233. {
  1234. EXPECT_DECLS;
  1235. WOLFSSL_CTX* ctx = NULL;
  1236. WOLFSSL* ssl = NULL;
  1237. int skipTest = 0;
  1238. const char* testCertFile;
  1239. const char* testKeyFile;
  1240. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1241. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1242. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1243. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1244. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1245. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1246. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1247. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1248. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1249. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1250. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1251. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1252. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1253. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1254. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1255. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1256. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1257. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1258. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1259. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1260. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1261. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1262. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1263. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1264. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1265. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1266. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1267. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1268. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1269. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1270. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1271. #ifdef OPENSSL_EXTRA
  1272. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1273. "!SRP:!kRSA:!aNULL:!eNULL";
  1274. #endif
  1275. #ifndef NO_RSA
  1276. testCertFile = svrCertFile;
  1277. testKeyFile = svrKeyFile;
  1278. #elif defined(HAVE_ECC)
  1279. testCertFile = eccCertFile;
  1280. testKeyFile = eccKeyFile;
  1281. #else
  1282. skipTest = 1;
  1283. #endif
  1284. if (skipTest != 1) {
  1285. #ifndef NO_WOLFSSL_SERVER
  1286. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1287. #else
  1288. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1289. #endif
  1290. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1291. WOLFSSL_FILETYPE_PEM));
  1292. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1293. WOLFSSL_FILETYPE_PEM));
  1294. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1295. /* First test freeing SSL, then CTX */
  1296. wolfSSL_free(ssl);
  1297. ssl = NULL;
  1298. wolfSSL_CTX_free(ctx);
  1299. ctx = NULL;
  1300. #ifndef NO_WOLFSSL_CLIENT
  1301. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1302. #else
  1303. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1304. #endif
  1305. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1306. WOLFSSL_FILETYPE_PEM));
  1307. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1308. WOLFSSL_FILETYPE_PEM));
  1309. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1310. /* Next test freeing CTX then SSL */
  1311. wolfSSL_CTX_free(ctx);
  1312. ctx = NULL;
  1313. wolfSSL_free(ssl);
  1314. ssl = NULL;
  1315. #ifndef NO_WOLFSSL_SERVER
  1316. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1317. #else
  1318. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1319. #endif
  1320. /* Test setting ciphers at ctx level */
  1321. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1322. WOLFSSL_FILETYPE_PEM));
  1323. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1324. WOLFSSL_FILETYPE_PEM));
  1325. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1326. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1327. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1328. /* only update TLSv13 suites */
  1329. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1330. #endif
  1331. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1332. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1333. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1334. /* only update pre-TLSv13 suites */
  1335. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1336. "ECDHE-RSA-AES128-GCM-SHA256"));
  1337. #endif
  1338. #ifdef OPENSSL_EXTRA
  1339. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1340. #endif
  1341. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1342. wolfSSL_CTX_free(ctx);
  1343. ctx = NULL;
  1344. wolfSSL_free(ssl);
  1345. ssl = NULL;
  1346. #ifndef NO_WOLFSSL_CLIENT
  1347. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1348. #else
  1349. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1350. #endif
  1351. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1352. WOLFSSL_FILETYPE_PEM));
  1353. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1354. WOLFSSL_FILETYPE_PEM));
  1355. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1356. /* test setting ciphers at SSL level */
  1357. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1358. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1359. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1360. /* only update TLSv13 suites */
  1361. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1362. #endif
  1363. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1364. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1365. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1366. /* only update pre-TLSv13 suites */
  1367. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1368. #endif
  1369. wolfSSL_CTX_free(ctx);
  1370. ctx = NULL;
  1371. wolfSSL_free(ssl);
  1372. ssl = NULL;
  1373. }
  1374. return EXPECT_RESULT();
  1375. }
  1376. #endif
  1377. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1378. {
  1379. EXPECT_DECLS;
  1380. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1381. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1382. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1383. const char* testCertFile;
  1384. const char* testKeyFile;
  1385. WOLFSSL_CTX* ctx = NULL;
  1386. WOLFSSL* ssl = NULL;
  1387. const byte cipherList[] =
  1388. {
  1389. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  1390. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  1391. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  1392. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  1393. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  1394. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  1395. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  1396. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  1397. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  1398. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  1399. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  1400. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  1401. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  1402. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  1403. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  1404. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  1405. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  1406. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  1407. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1408. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1409. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1410. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1411. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1412. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1413. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1414. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1415. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1416. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1417. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1418. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1419. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1420. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1421. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1422. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1423. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1424. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1425. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1426. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1427. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1428. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1429. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1430. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1431. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1432. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1433. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1434. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1435. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1436. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1437. /* SHA256 */
  1438. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1439. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1440. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1441. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1442. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1443. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1444. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1445. /* SHA384 */
  1446. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1447. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1448. /* AES-GCM */
  1449. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1450. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1451. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1452. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1453. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1454. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1455. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1456. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1457. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1458. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1459. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1460. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1461. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1462. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1463. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1464. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1465. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1466. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1467. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1468. * also, in some of the other AES-CCM suites
  1469. * there will be second byte number conflicts
  1470. * with non-ECC AES-GCM */
  1471. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1472. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1473. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1474. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1475. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1476. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1477. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1478. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1479. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1480. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1481. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1482. /* Camellia */
  1483. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1484. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1485. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1486. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1487. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1488. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1489. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1490. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1491. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1492. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1493. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1494. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1495. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1496. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1497. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1498. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1499. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1500. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1501. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1502. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1503. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1504. /* TLS v1.3 cipher suites */
  1505. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1506. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1507. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1508. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1509. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1510. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1511. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1512. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1513. };
  1514. #ifndef NO_RSA
  1515. testCertFile = svrCertFile;
  1516. testKeyFile = svrKeyFile;
  1517. #elif defined(HAVE_ECC)
  1518. testCertFile = eccCertFile;
  1519. testKeyFile = eccKeyFile;
  1520. #endif
  1521. #ifndef NO_WOLFSSL_SERVER
  1522. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1523. #else
  1524. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1525. #endif
  1526. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1527. sizeof(cipherList)));
  1528. wolfSSL_CTX_free(ctx);
  1529. ctx = NULL;
  1530. #ifndef NO_WOLFSSL_SERVER
  1531. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1532. #else
  1533. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1534. #endif
  1535. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1536. WOLFSSL_FILETYPE_PEM));
  1537. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1538. WOLFSSL_FILETYPE_PEM));
  1539. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1540. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1541. sizeof(cipherList)));
  1542. wolfSSL_free(ssl);
  1543. wolfSSL_CTX_free(ctx);
  1544. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1545. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1546. return EXPECT_RESULT();
  1547. }
  1548. static int test_wolfSSL_CTX_use_certificate_file(void)
  1549. {
  1550. EXPECT_DECLS;
  1551. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1552. WOLFSSL_CTX *ctx = NULL;
  1553. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1554. /* invalid context */
  1555. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1556. WOLFSSL_FILETYPE_PEM));
  1557. /* invalid cert file */
  1558. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1559. WOLFSSL_FILETYPE_PEM));
  1560. /* invalid cert type */
  1561. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1562. #ifdef NO_RSA
  1563. /* rsa needed */
  1564. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1565. WOLFSSL_FILETYPE_PEM));
  1566. #else
  1567. /* success */
  1568. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1569. WOLFSSL_FILETYPE_PEM));
  1570. #endif
  1571. wolfSSL_CTX_free(ctx);
  1572. #endif
  1573. return EXPECT_RESULT();
  1574. }
  1575. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1576. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1577. {
  1578. EXPECT_DECLS;
  1579. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1580. WOLFSSL_CTX* ctx = NULL;
  1581. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1582. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1583. server_cert_der_2048), WOLFSSL_SUCCESS);
  1584. wolfSSL_CTX_free(ctx);
  1585. #endif
  1586. return EXPECT_RESULT();
  1587. }
  1588. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1589. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1590. * context using buffer.
  1591. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1592. * --enable-testcert flag.
  1593. */
  1594. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1595. {
  1596. EXPECT_DECLS;
  1597. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1598. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1599. WOLFSSL_CTX* ctx = NULL;
  1600. int ret;
  1601. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1602. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1603. server_cert_der_2048, sizeof_server_cert_der_2048,
  1604. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1605. wolfSSL_CTX_free(ctx);
  1606. #endif
  1607. return EXPECT_RESULT();
  1608. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1609. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1610. {
  1611. EXPECT_DECLS;
  1612. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1613. WOLFSSL_CTX *ctx = NULL;
  1614. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1615. /* invalid context */
  1616. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1617. WOLFSSL_FILETYPE_PEM));
  1618. /* invalid key file */
  1619. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1620. WOLFSSL_FILETYPE_PEM));
  1621. /* invalid key type */
  1622. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1623. /* success */
  1624. #ifdef NO_RSA
  1625. /* rsa needed */
  1626. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1627. WOLFSSL_FILETYPE_PEM));
  1628. #else
  1629. /* success */
  1630. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1631. WOLFSSL_FILETYPE_PEM));
  1632. #endif
  1633. wolfSSL_CTX_free(ctx);
  1634. #endif
  1635. return EXPECT_RESULT();
  1636. }
  1637. /* test both file and buffer versions along with unloading trusted peer certs */
  1638. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1639. {
  1640. EXPECT_DECLS;
  1641. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1642. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1643. WOLFSSL_CTX *ctx = NULL;
  1644. WOLFSSL* ssl = NULL;
  1645. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1646. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1647. #if !defined(NO_FILESYSTEM)
  1648. /* invalid file */
  1649. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1650. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1651. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1652. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1653. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1654. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1655. /* success */
  1656. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1657. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1658. /* unload cert */
  1659. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1660. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1661. /* invalid file */
  1662. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1663. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1664. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1665. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1666. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1667. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1668. /* success */
  1669. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1670. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1671. #ifdef WOLFSSL_LOCAL_X509_STORE
  1672. /* unload cert */
  1673. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1674. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1675. #endif
  1676. #endif
  1677. /* Test of loading certs from buffers */
  1678. /* invalid buffer */
  1679. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1680. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1681. /* success */
  1682. #ifdef USE_CERT_BUFFERS_1024
  1683. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1684. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1685. #endif
  1686. #ifdef USE_CERT_BUFFERS_2048
  1687. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1688. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1689. #endif
  1690. /* unload cert */
  1691. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1692. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1693. wolfSSL_free(ssl);
  1694. wolfSSL_CTX_free(ctx);
  1695. #endif
  1696. return EXPECT_RESULT();
  1697. }
  1698. static int test_wolfSSL_CTX_load_verify_locations(void)
  1699. {
  1700. EXPECT_DECLS;
  1701. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1702. WOLFSSL_CTX *ctx = NULL;
  1703. #ifndef NO_RSA
  1704. WOLFSSL_CERT_MANAGER* cm = NULL;
  1705. #ifdef PERSIST_CERT_CACHE
  1706. int cacheSz = 0;
  1707. unsigned char* cache = NULL;
  1708. int used = 0;
  1709. #ifndef NO_FILESYSTEM
  1710. const char* cacheFile = "./tests/cert_cache.tmp";
  1711. #endif
  1712. int i;
  1713. int t;
  1714. int* p;
  1715. #endif
  1716. #endif
  1717. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1718. const char* load_certs_path = "./certs/external";
  1719. const char* load_no_certs_path = "./examples";
  1720. const char* load_expired_path = "./certs/test/expired";
  1721. #endif
  1722. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1723. /* invalid arguments */
  1724. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1725. WOLFSSL_FAILURE);
  1726. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1727. WOLFSSL_FAILURE);
  1728. /* invalid ca file */
  1729. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1730. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1731. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1732. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1733. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1734. /* invalid path */
  1735. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1736. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1737. #endif
  1738. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1739. /* test ignoring the invalid path */
  1740. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1741. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1742. #endif
  1743. /* load ca cert */
  1744. #ifdef NO_RSA
  1745. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1746. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1747. #else /* Skip the following test without RSA certs. */
  1748. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1749. WOLFSSL_SUCCESS);
  1750. #ifdef PERSIST_CERT_CACHE
  1751. /* Get cert cache size */
  1752. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1753. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1754. DYNAMIC_TYPE_TMP_BUFFER));
  1755. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1756. BAD_FUNC_ARG);
  1757. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1758. BAD_FUNC_ARG);
  1759. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1760. BAD_FUNC_ARG);
  1761. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1762. BAD_FUNC_ARG);
  1763. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1764. BAD_FUNC_ARG);
  1765. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1766. BAD_FUNC_ARG);
  1767. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1768. BAD_FUNC_ARG);
  1769. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1770. BAD_FUNC_ARG);
  1771. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1772. BAD_FUNC_ARG);
  1773. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1774. BUFFER_E);
  1775. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1776. ExpectIntEQ(cacheSz, used);
  1777. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1778. BAD_FUNC_ARG);
  1779. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1780. BAD_FUNC_ARG);
  1781. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1782. BAD_FUNC_ARG);
  1783. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1784. BAD_FUNC_ARG);
  1785. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1786. BAD_FUNC_ARG);
  1787. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1788. BAD_FUNC_ARG);
  1789. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1790. BAD_FUNC_ARG);
  1791. /* Smaller than header. */
  1792. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1793. for (i = 1; i < cacheSz; i++) {
  1794. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1795. BUFFER_E);
  1796. }
  1797. if (EXPECT_SUCCESS()) {
  1798. /* Modify header for bad results! */
  1799. p = (int*)cache;
  1800. /* version */
  1801. t = p[0]; p[0] = 0xff;
  1802. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1803. CACHE_MATCH_ERROR);
  1804. p[0] = t; p++;
  1805. /* rows */
  1806. t = p[0]; p[0] = 0xff;
  1807. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1808. CACHE_MATCH_ERROR);
  1809. p[0] = t; p++;
  1810. /* columns[0] */
  1811. t = p[0]; p[0] = -1;
  1812. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1813. PARSE_ERROR);
  1814. p[0] = t; p += CA_TABLE_SIZE;
  1815. /* signerSz*/
  1816. t = p[0]; p[0] = 0xff;
  1817. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1818. CACHE_MATCH_ERROR);
  1819. p[0] = t;
  1820. }
  1821. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1822. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1823. #ifndef NO_FILESYSTEM
  1824. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1825. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1826. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1827. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1828. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1829. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1830. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1831. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1832. WOLFSSL_BAD_FILE);
  1833. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1834. /* File contents is not a cache. */
  1835. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1836. CACHE_MATCH_ERROR);
  1837. #endif
  1838. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1839. #endif
  1840. /* Test unloading CA's */
  1841. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1842. #ifdef PERSIST_CERT_CACHE
  1843. /* Verify no certs (result is less than cacheSz) */
  1844. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1845. #endif
  1846. /* load ca cert again */
  1847. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1848. WOLFSSL_SUCCESS);
  1849. /* Test getting CERT_MANAGER */
  1850. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1851. /* Test unloading CA's using CM */
  1852. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1853. #ifdef PERSIST_CERT_CACHE
  1854. /* Verify no certs (result is less than cacheSz) */
  1855. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1856. #endif
  1857. #endif
  1858. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1859. /* Test loading CA certificates using a path */
  1860. #ifdef NO_RSA
  1861. /* failure here okay since certs in external directory are RSA */
  1862. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1863. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1864. #else
  1865. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1866. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1867. #endif
  1868. /* Test loading path with no files */
  1869. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1870. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1871. /* Test loading expired CA certificates */
  1872. #ifdef NO_RSA
  1873. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1874. load_expired_path,
  1875. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1876. WOLFSSL_SUCCESS);
  1877. #else
  1878. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1879. load_expired_path,
  1880. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1881. WOLFSSL_SUCCESS);
  1882. #endif
  1883. /* Test loading CA certificates and ignoring all errors */
  1884. #ifdef NO_RSA
  1885. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1886. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1887. #else
  1888. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1889. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1890. #endif
  1891. #endif
  1892. wolfSSL_CTX_free(ctx);
  1893. #endif
  1894. return EXPECT_RESULT();
  1895. }
  1896. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1897. {
  1898. int res = TEST_SKIPPED;
  1899. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1900. (!defined(NO_RSA) || defined(HAVE_ECC))
  1901. WOLFSSL_CTX* ctx;
  1902. byte dirValid = 0;
  1903. int ret = 0;
  1904. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1905. if (ctx == NULL) {
  1906. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1907. ret = -1;
  1908. }
  1909. if (ret == 0) {
  1910. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1911. dirValid = 1;
  1912. #else
  1913. word32 numDirs;
  1914. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1915. if (caDirs == NULL || numDirs == 0) {
  1916. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1917. ret = -1;
  1918. }
  1919. else {
  1920. ReadDirCtx dirCtx;
  1921. word32 i;
  1922. for (i = 0; i < numDirs; ++i) {
  1923. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1924. /* Directory isn't empty. */
  1925. dirValid = 1;
  1926. wc_ReadDirClose(&dirCtx);
  1927. break;
  1928. }
  1929. }
  1930. }
  1931. #endif
  1932. }
  1933. /*
  1934. * If the directory isn't empty, we should be able to load CA
  1935. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1936. * usable.
  1937. */
  1938. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1939. WOLFSSL_SUCCESS) {
  1940. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1941. ret = -1;
  1942. }
  1943. #ifdef OPENSSL_EXTRA
  1944. if (ret == 0 &&
  1945. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1946. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1947. ret = -1;
  1948. }
  1949. #endif /* OPENSSL_EXTRA */
  1950. wolfSSL_CTX_free(ctx);
  1951. res = TEST_RES_CHECK(ret == 0);
  1952. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1953. return res;
  1954. }
  1955. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1956. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1957. int file_type)
  1958. {
  1959. int ret;
  1960. WOLFSSL_CERT_MANAGER* cm;
  1961. cm = wolfSSL_CertManagerNew();
  1962. if (cm == NULL) {
  1963. fprintf(stderr, "test_cm_load_ca failed\n");
  1964. return -1;
  1965. }
  1966. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1967. wolfSSL_CertManagerFree(cm);
  1968. return ret;
  1969. }
  1970. static int test_cm_load_ca_file(const char* ca_cert_file)
  1971. {
  1972. int ret = 0;
  1973. byte* cert_buf = NULL;
  1974. size_t cert_sz = 0;
  1975. #if defined(WOLFSSL_PEM_TO_DER)
  1976. DerBuffer* pDer = NULL;
  1977. #endif
  1978. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1979. if (ret == 0) {
  1980. /* normal test */
  1981. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1982. if (ret == WOLFSSL_SUCCESS) {
  1983. /* test including null terminator in length */
  1984. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1985. if (tmp == NULL) {
  1986. ret = MEMORY_E;
  1987. }
  1988. else {
  1989. cert_buf = tmp;
  1990. cert_buf[cert_sz] = '\0';
  1991. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1992. WOLFSSL_FILETYPE_PEM);
  1993. }
  1994. }
  1995. #if defined(WOLFSSL_PEM_TO_DER)
  1996. if (ret == WOLFSSL_SUCCESS) {
  1997. /* test loading DER */
  1998. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1999. if (ret == 0 && pDer != NULL) {
  2000. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  2001. WOLFSSL_FILETYPE_ASN1);
  2002. wc_FreeDer(&pDer);
  2003. }
  2004. }
  2005. #endif
  2006. }
  2007. free(cert_buf);
  2008. return ret;
  2009. }
  2010. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  2011. int file_type, word32 flags)
  2012. {
  2013. int ret;
  2014. WOLFSSL_CERT_MANAGER* cm;
  2015. cm = wolfSSL_CertManagerNew();
  2016. if (cm == NULL) {
  2017. fprintf(stderr, "test_cm_load_ca failed\n");
  2018. return -1;
  2019. }
  2020. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  2021. 0, flags);
  2022. wolfSSL_CertManagerFree(cm);
  2023. return ret;
  2024. }
  2025. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  2026. {
  2027. int ret = 0;
  2028. byte* cert_buf = NULL;
  2029. size_t cert_sz = 0;
  2030. #if defined(WOLFSSL_PEM_TO_DER)
  2031. DerBuffer* pDer = NULL;
  2032. #endif
  2033. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  2034. if (ret == 0) {
  2035. /* normal test */
  2036. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  2037. WOLFSSL_FILETYPE_PEM, flags);
  2038. if (ret == WOLFSSL_SUCCESS) {
  2039. /* test including null terminator in length */
  2040. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  2041. if (tmp == NULL) {
  2042. ret = MEMORY_E;
  2043. }
  2044. else {
  2045. cert_buf = tmp;
  2046. cert_buf[cert_sz] = '\0';
  2047. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  2048. WOLFSSL_FILETYPE_PEM, flags);
  2049. }
  2050. }
  2051. #if defined(WOLFSSL_PEM_TO_DER)
  2052. if (ret == WOLFSSL_SUCCESS) {
  2053. /* test loading DER */
  2054. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  2055. if (ret == 0 && pDer != NULL) {
  2056. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  2057. WOLFSSL_FILETYPE_ASN1, flags);
  2058. wc_FreeDer(&pDer);
  2059. }
  2060. }
  2061. #endif
  2062. }
  2063. free(cert_buf);
  2064. return ret;
  2065. }
  2066. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  2067. static int test_wolfSSL_CertManagerAPI(void)
  2068. {
  2069. EXPECT_DECLS;
  2070. #ifndef NO_CERTS
  2071. WOLFSSL_CERT_MANAGER* cm = NULL;
  2072. unsigned char c;
  2073. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2074. wolfSSL_CertManagerFree(NULL);
  2075. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  2076. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  2077. #ifdef WOLFSSL_TRUST_PEER_CERT
  2078. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  2079. #endif
  2080. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  2081. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  2082. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2083. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  2084. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2085. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  2086. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2087. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  2088. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2089. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  2090. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2091. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  2092. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2093. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  2094. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2095. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  2096. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2097. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  2098. WOLFSSL_BAD_FILETYPE);
  2099. #endif
  2100. #if !defined(NO_FILESYSTEM)
  2101. {
  2102. const char* ca_cert = "./certs/ca-cert.pem";
  2103. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2104. const char* ca_cert_der = "./certs/ca-cert.der";
  2105. #endif
  2106. const char* ca_path = "./certs";
  2107. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2108. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  2109. BAD_FUNC_ARG);
  2110. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2111. BAD_FUNC_ARG);
  2112. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  2113. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  2114. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  2115. WOLFSSL_BAD_FILETYPE);
  2116. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  2117. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  2118. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  2119. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  2120. #endif
  2121. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  2122. WOLFSSL_FATAL_ERROR);
  2123. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  2124. WOLFSSL_FATAL_ERROR);
  2125. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  2126. WOLFSSL_FATAL_ERROR);
  2127. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  2128. WOLFSSL_FATAL_ERROR);
  2129. }
  2130. #endif
  2131. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2132. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  2133. #elif !defined(HAVE_CRL)
  2134. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  2135. #endif
  2136. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  2137. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  2138. #ifdef HAVE_CRL
  2139. /* Test APIs when CRL is disabled. */
  2140. #ifdef HAVE_CRL_IO
  2141. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2142. #endif
  2143. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2144. sizeof_server_cert_der_2048), 1);
  2145. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2146. #endif
  2147. /* OCSP */
  2148. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  2149. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  2150. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  2151. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  2152. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2153. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2154. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  2155. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2156. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2157. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2158. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2159. #endif
  2160. #ifdef HAVE_OCSP
  2161. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2162. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2163. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2164. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2165. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2166. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2167. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2168. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2169. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2170. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2171. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2172. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2173. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2174. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2175. BAD_FUNC_ARG);
  2176. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2177. BAD_FUNC_ARG);
  2178. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2179. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2180. BAD_FUNC_ARG);
  2181. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2182. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2183. /* Test APIs when OCSP is disabled. */
  2184. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2185. NULL, NULL, NULL, NULL), 1);
  2186. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2187. #endif
  2188. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2189. if (EXPECT_SUCCESS()) {
  2190. wolfSSL_CertManagerFree(cm);
  2191. }
  2192. wolfSSL_CertManagerFree(cm);
  2193. cm = NULL;
  2194. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2195. #ifdef HAVE_OCSP
  2196. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2197. WOLFSSL_OCSP_CHECKALL), 1);
  2198. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2199. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2200. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2201. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2202. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2203. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2204. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2205. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2206. #endif
  2207. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2208. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2209. #endif
  2210. #ifdef WOLFSSL_TRUST_PEER_CERT
  2211. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2212. #endif
  2213. wolfSSL_CertManagerFree(cm);
  2214. #endif
  2215. return EXPECT_RESULT();
  2216. }
  2217. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2218. {
  2219. EXPECT_DECLS;
  2220. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2221. const char* ca_cert = "./certs/ca-cert.pem";
  2222. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2223. int ret;
  2224. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2225. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2226. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2227. #elif defined(NO_RSA)
  2228. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2229. #else
  2230. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2231. #endif
  2232. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2233. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2234. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2235. #elif defined(NO_RSA)
  2236. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2237. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2238. !defined(NO_ASN_TIME)
  2239. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2240. #else
  2241. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2242. #endif
  2243. #endif
  2244. return EXPECT_RESULT();
  2245. }
  2246. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2247. {
  2248. EXPECT_DECLS;
  2249. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2250. const char* ca_cert = "./certs/ca-cert.pem";
  2251. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2252. int ret;
  2253. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2254. 1);
  2255. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2256. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2257. #elif defined(NO_RSA)
  2258. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2259. #else
  2260. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2261. #endif
  2262. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2263. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2264. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2265. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2266. #elif defined(NO_RSA)
  2267. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2268. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2269. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2270. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2271. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2272. #else
  2273. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2274. #endif
  2275. #endif
  2276. return EXPECT_RESULT();
  2277. }
  2278. static int test_wolfSSL_CertManagerGetCerts(void)
  2279. {
  2280. EXPECT_DECLS;
  2281. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2282. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2283. defined(WOLFSSL_SIGNER_DER_CERT)
  2284. WOLFSSL_CERT_MANAGER* cm = NULL;
  2285. WOLFSSL_STACK* sk = NULL;
  2286. X509* x509 = NULL;
  2287. X509* cert1 = NULL;
  2288. FILE* file1 = NULL;
  2289. #ifdef DEBUG_WOLFSSL_VERBOSE
  2290. WOLFSSL_BIO* bio = NULL;
  2291. #endif
  2292. int i = 0;
  2293. int ret = 0;
  2294. const byte* der = NULL;
  2295. int derSz = 0;
  2296. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2297. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2298. if (file1 != NULL) {
  2299. fclose(file1);
  2300. }
  2301. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2302. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2303. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2304. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2305. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2306. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2307. * and full OpenSSL compatibility */
  2308. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2309. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2310. #else
  2311. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2312. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2313. #endif
  2314. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2315. "./certs/ca-cert.pem", NULL));
  2316. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2317. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2318. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2319. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2320. #ifdef DEBUG_WOLFSSL_VERBOSE
  2321. bio = BIO_new(wolfSSL_BIO_s_file());
  2322. if (bio != NULL) {
  2323. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2324. X509_print(bio, x509);
  2325. BIO_free(bio);
  2326. }
  2327. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2328. }
  2329. wolfSSL_X509_free(cert1);
  2330. sk_X509_pop_free(sk, NULL);
  2331. wolfSSL_CertManagerFree(cm);
  2332. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2333. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2334. defined(WOLFSSL_SIGNER_DER_CERT) */
  2335. return EXPECT_RESULT();
  2336. }
  2337. static int test_wolfSSL_CertManagerSetVerify(void)
  2338. {
  2339. EXPECT_DECLS;
  2340. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2341. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2342. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2343. WOLFSSL_CERT_MANAGER* cm = NULL;
  2344. int tmp = myVerifyAction;
  2345. const char* ca_cert = "./certs/ca-cert.pem";
  2346. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2347. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2348. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2349. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2350. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2351. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2352. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2353. #else
  2354. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2355. WOLFSSL_SUCCESS);
  2356. #endif
  2357. /* Use the test CB that always accepts certs */
  2358. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2359. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2360. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2361. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2362. {
  2363. const char* verifyCert = "./certs/server-cert.der";
  2364. /* Use the test CB that always fails certs */
  2365. myVerifyAction = VERIFY_FORCE_FAIL;
  2366. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2367. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2368. }
  2369. #endif
  2370. wolfSSL_CertManagerFree(cm);
  2371. myVerifyAction = tmp;
  2372. #endif
  2373. return EXPECT_RESULT();
  2374. }
  2375. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2376. defined(DEBUG_UNIT_TEST_CERTS)
  2377. /* Used when debugging name constraint tests. Not static to allow use in
  2378. * multiple locations with complex define guards. */
  2379. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2380. {
  2381. BIO* out = BIO_new_file(fileName, "wb");
  2382. if (out != NULL) {
  2383. PEM_write_bio_X509(out, x509);
  2384. BIO_free(out);
  2385. }
  2386. }
  2387. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2388. {
  2389. BIO* out = BIO_new_file(fileName, "wb");
  2390. if (out != NULL) {
  2391. BIO_write(out, der, derSz);
  2392. BIO_free(out);
  2393. }
  2394. }
  2395. #else
  2396. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2397. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2398. #endif
  2399. static int test_wolfSSL_CertManagerNameConstraint(void)
  2400. {
  2401. EXPECT_DECLS;
  2402. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2403. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2404. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2405. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2406. !defined(NO_SHA256)
  2407. WOLFSSL_CERT_MANAGER* cm = NULL;
  2408. WOLFSSL_EVP_PKEY *priv = NULL;
  2409. WOLFSSL_X509_NAME* name = NULL;
  2410. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2411. const char* server_cert = "./certs/test/server-goodcn.pem";
  2412. int i = 0;
  2413. static const byte extNameConsOid[] = {85, 29, 30};
  2414. RsaKey key;
  2415. WC_RNG rng;
  2416. byte *der = NULL;
  2417. int derSz = 0;
  2418. word32 idx = 0;
  2419. byte *pt;
  2420. WOLFSSL_X509 *x509 = NULL;
  2421. WOLFSSL_X509 *ca = NULL;
  2422. wc_InitRng(&rng);
  2423. /* load in CA private key for signing */
  2424. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2425. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2426. sizeof_server_key_der_2048), 0);
  2427. /* get ca certificate then alter it */
  2428. ExpectNotNull(der =
  2429. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2430. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2431. WOLFSSL_FILETYPE_ASN1));
  2432. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2433. if (EXPECT_SUCCESS() && (der != NULL)) {
  2434. XMEMCPY(der, pt, derSz);
  2435. /* find the name constraint extension and alter it */
  2436. pt = der;
  2437. for (i = 0; i < derSz - 3; i++) {
  2438. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2439. pt += 3;
  2440. break;
  2441. }
  2442. pt++;
  2443. }
  2444. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2445. /* go to the length value and set it to 0 */
  2446. while (i < derSz && *pt != 0x81) {
  2447. pt++;
  2448. i++;
  2449. }
  2450. ExpectIntNE(i, derSz); /* did not place to alter */
  2451. pt++;
  2452. *pt = 0x00;
  2453. }
  2454. /* resign the altered certificate */
  2455. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2456. FOURK_BUF, &key, NULL, &rng)), 0);
  2457. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2458. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2459. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2460. wolfSSL_CertManagerFree(cm);
  2461. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2462. wolfSSL_X509_free(x509);
  2463. wc_FreeRsaKey(&key);
  2464. wc_FreeRng(&rng);
  2465. /* add email alt name to satisfy constraint */
  2466. pt = (byte*)server_key_der_2048;
  2467. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2468. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2469. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2470. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2471. WOLFSSL_FILETYPE_ASN1));
  2472. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2473. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2474. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2475. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2476. /* Good cert test with proper alt email name */
  2477. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2478. WOLFSSL_FILETYPE_PEM));
  2479. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2480. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2481. name = NULL;
  2482. ExpectNotNull(name = X509_NAME_new());
  2483. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2484. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2485. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2486. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2487. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2488. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2489. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2490. X509_NAME_free(name);
  2491. name = NULL;
  2492. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2493. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2494. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2495. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2496. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2497. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2498. wolfSSL_X509_free(x509);
  2499. x509 = NULL;
  2500. /* Cert with bad alt name list */
  2501. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2502. WOLFSSL_FILETYPE_PEM));
  2503. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2504. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2505. name = NULL;
  2506. ExpectNotNull(name = X509_NAME_new());
  2507. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2508. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2509. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2510. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2511. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2512. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2513. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2514. X509_NAME_free(name);
  2515. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2516. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2517. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2518. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2519. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2520. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2521. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2522. wolfSSL_CertManagerFree(cm);
  2523. wolfSSL_X509_free(x509);
  2524. wolfSSL_X509_free(ca);
  2525. wolfSSL_EVP_PKEY_free(priv);
  2526. #endif
  2527. return EXPECT_RESULT();
  2528. }
  2529. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2530. {
  2531. EXPECT_DECLS;
  2532. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2533. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2534. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2535. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2536. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2537. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2538. const char* server_cert = "./certs/server-cert.pem";
  2539. WOLFSSL_CERT_MANAGER* cm = NULL;
  2540. WOLFSSL_X509 *x509 = NULL;
  2541. WOLFSSL_X509 *ca = NULL;
  2542. const unsigned char *der = NULL;
  2543. const unsigned char *pt;
  2544. WOLFSSL_EVP_PKEY *priv = NULL;
  2545. WOLFSSL_X509_NAME* name = NULL;
  2546. int derSz = 0;
  2547. /* C=US*/
  2548. char altName[] = {
  2549. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2550. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2551. };
  2552. /* C=ID */
  2553. char altNameFail[] = {
  2554. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2555. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2556. };
  2557. /* C=US ST=California*/
  2558. char altNameExc[] = {
  2559. 0x30, 0x22,
  2560. 0x31, 0x0B,
  2561. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2562. 0x31, 0x13,
  2563. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2564. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2565. };
  2566. /* load in CA private key for signing */
  2567. pt = ca_key_der_2048;
  2568. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2569. sizeof_ca_key_der_2048));
  2570. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2571. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2572. WOLFSSL_FILETYPE_ASN1));
  2573. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2574. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2575. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2576. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2577. WOLFSSL_FILETYPE_PEM));
  2578. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2579. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2580. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2581. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2582. #else
  2583. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2584. #endif
  2585. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2586. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2587. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2588. /* add in matching DIR alt name and resign */
  2589. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2590. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2591. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2592. #else
  2593. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2594. #endif
  2595. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2596. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2597. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2598. wolfSSL_X509_free(x509);
  2599. x509 = NULL;
  2600. /* check verify fail */
  2601. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2602. WOLFSSL_FILETYPE_PEM));
  2603. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2604. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2605. /* add in miss matching DIR alt name and resign */
  2606. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2607. ASN_DIR_TYPE);
  2608. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2609. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2610. #else
  2611. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2612. #endif
  2613. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2614. #ifndef WOLFSSL_NO_ASN_STRICT
  2615. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2616. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2617. #else
  2618. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2619. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2620. #endif
  2621. /* check that it still fails if one bad altname and one good altname is in
  2622. * the certificate */
  2623. wolfSSL_X509_free(x509);
  2624. x509 = NULL;
  2625. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2626. WOLFSSL_FILETYPE_PEM));
  2627. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2628. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2629. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2630. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2631. ASN_DIR_TYPE);
  2632. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2633. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2634. #else
  2635. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2636. #endif
  2637. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2638. #ifndef WOLFSSL_NO_ASN_STRICT
  2639. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2640. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2641. #else
  2642. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2643. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2644. #endif
  2645. /* check it fails with switching position of bad altname */
  2646. wolfSSL_X509_free(x509);
  2647. x509 = NULL;
  2648. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2649. WOLFSSL_FILETYPE_PEM));
  2650. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2651. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2652. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2653. ASN_DIR_TYPE);
  2654. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2655. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2656. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2657. #else
  2658. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2659. #endif
  2660. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2661. #ifndef WOLFSSL_NO_ASN_STRICT
  2662. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2663. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2664. #else
  2665. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2666. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2667. #endif
  2668. wolfSSL_CertManagerFree(cm);
  2669. wolfSSL_X509_free(x509);
  2670. x509 = NULL;
  2671. wolfSSL_X509_free(ca);
  2672. ca = NULL;
  2673. /* now test with excluded name constraint */
  2674. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2675. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2676. WOLFSSL_FILETYPE_ASN1));
  2677. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2678. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2679. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2680. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2681. WOLFSSL_FILETYPE_PEM));
  2682. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2683. ASN_DIR_TYPE);
  2684. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2685. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2686. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2687. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2688. #else
  2689. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2690. #endif
  2691. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2692. #ifndef WOLFSSL_NO_ASN_STRICT
  2693. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2694. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2695. #else
  2696. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2697. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2698. #endif
  2699. wolfSSL_CertManagerFree(cm);
  2700. wolfSSL_X509_free(x509);
  2701. wolfSSL_X509_free(ca);
  2702. wolfSSL_EVP_PKEY_free(priv);
  2703. #endif
  2704. return EXPECT_RESULT();
  2705. }
  2706. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2707. {
  2708. EXPECT_DECLS;
  2709. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2710. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2711. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2712. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2713. !defined(NO_SHA256)
  2714. WOLFSSL_CERT_MANAGER* cm = NULL;
  2715. WOLFSSL_EVP_PKEY *priv = NULL;
  2716. WOLFSSL_X509_NAME* name = NULL;
  2717. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2718. const char* server_cert = "./certs/test/server-goodcn.pem";
  2719. byte *der = NULL;
  2720. int derSz = 0;
  2721. byte *pt;
  2722. WOLFSSL_X509 *x509 = NULL;
  2723. WOLFSSL_X509 *ca = NULL;
  2724. pt = (byte*)server_key_der_2048;
  2725. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2726. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2727. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2728. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2729. WOLFSSL_FILETYPE_ASN1));
  2730. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2731. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2732. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2733. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2734. /* check satisfying .wolfssl.com constraint passes */
  2735. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2736. WOLFSSL_FILETYPE_PEM));
  2737. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2738. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2739. name = NULL;
  2740. ExpectNotNull(name = X509_NAME_new());
  2741. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2742. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2743. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2744. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2745. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2746. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2747. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2748. X509_NAME_free(name);
  2749. name = NULL;
  2750. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2751. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2752. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2753. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2754. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2755. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2756. wolfSSL_X509_free(x509);
  2757. x509 = NULL;
  2758. /* check satisfying .random.com constraint passes */
  2759. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2760. WOLFSSL_FILETYPE_PEM));
  2761. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2762. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2763. name = NULL;
  2764. ExpectNotNull(name = X509_NAME_new());
  2765. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2766. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2767. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2768. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2769. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2770. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2771. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2772. X509_NAME_free(name);
  2773. name = NULL;
  2774. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2775. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2776. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2777. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2778. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2779. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2780. wolfSSL_X509_free(x509);
  2781. x509 = NULL;
  2782. /* check fail case when neither constraint is matched */
  2783. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2784. WOLFSSL_FILETYPE_PEM));
  2785. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2786. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2787. name = NULL;
  2788. ExpectNotNull(name = X509_NAME_new());
  2789. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2790. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2791. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2792. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2793. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2794. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2795. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2796. X509_NAME_free(name);
  2797. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2798. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2799. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2800. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2801. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2802. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2803. wolfSSL_CertManagerFree(cm);
  2804. wolfSSL_X509_free(x509);
  2805. wolfSSL_X509_free(ca);
  2806. wolfSSL_EVP_PKEY_free(priv);
  2807. #endif
  2808. return EXPECT_RESULT();
  2809. }
  2810. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2811. {
  2812. EXPECT_DECLS;
  2813. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2814. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2815. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2816. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2817. !defined(NO_SHA256)
  2818. WOLFSSL_CERT_MANAGER* cm = NULL;
  2819. WOLFSSL_EVP_PKEY *priv = NULL;
  2820. WOLFSSL_X509_NAME* name = NULL;
  2821. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2822. const char* server_cert = "./certs/test/server-goodcn.pem";
  2823. byte *der = NULL;
  2824. int derSz;
  2825. byte *pt;
  2826. WOLFSSL_X509 *x509 = NULL;
  2827. WOLFSSL_X509 *ca = NULL;
  2828. pt = (byte*)server_key_der_2048;
  2829. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2830. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2831. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2832. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2833. WOLFSSL_FILETYPE_ASN1));
  2834. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2835. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2836. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2837. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2838. /* check satisfying wolfssl.com constraint passes */
  2839. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2840. WOLFSSL_FILETYPE_PEM));
  2841. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2842. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2843. name = NULL;
  2844. ExpectNotNull(name = X509_NAME_new());
  2845. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2846. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2847. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2848. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2849. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2850. X509_NAME_free(name);
  2851. name = NULL;
  2852. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2853. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2854. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2855. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2856. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2857. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2858. wolfSSL_X509_free(x509);
  2859. x509 = NULL;
  2860. /* check satisfying example.com constraint passes */
  2861. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2862. WOLFSSL_FILETYPE_PEM));
  2863. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2864. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2865. name = NULL;
  2866. ExpectNotNull(name = X509_NAME_new());
  2867. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2868. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2869. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2870. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2871. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2872. X509_NAME_free(name);
  2873. name = NULL;
  2874. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2875. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2876. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2877. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2878. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2879. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2880. wolfSSL_X509_free(x509);
  2881. x509 = NULL;
  2882. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2883. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2884. WOLFSSL_FILETYPE_PEM));
  2885. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2886. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2887. name = NULL;
  2888. ExpectNotNull(name = X509_NAME_new());
  2889. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2890. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2891. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2892. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2893. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2894. X509_NAME_free(name);
  2895. name = NULL;
  2896. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2897. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2898. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2899. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2900. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2901. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2902. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2903. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2904. wolfSSL_X509_free(x509);
  2905. x509 = NULL;
  2906. /* check fail when one DNS in the list is bad */
  2907. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2908. WOLFSSL_FILETYPE_PEM));
  2909. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2910. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2911. name = NULL;
  2912. ExpectNotNull(name = X509_NAME_new());
  2913. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2914. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2915. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2916. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2917. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2918. X509_NAME_free(name);
  2919. name = NULL;
  2920. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2921. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2922. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2923. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2924. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2925. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2926. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2927. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2928. wolfSSL_X509_free(x509);
  2929. x509 = NULL;
  2930. /* check fail case when neither constraint is matched */
  2931. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2932. WOLFSSL_FILETYPE_PEM));
  2933. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2934. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2935. name = NULL;
  2936. ExpectNotNull(name = X509_NAME_new());
  2937. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2938. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2939. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2940. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2941. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2942. X509_NAME_free(name);
  2943. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2944. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2945. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2946. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2947. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2948. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2949. wolfSSL_CertManagerFree(cm);
  2950. wolfSSL_X509_free(x509);
  2951. wolfSSL_X509_free(ca);
  2952. wolfSSL_EVP_PKEY_free(priv);
  2953. #endif
  2954. return EXPECT_RESULT();
  2955. }
  2956. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2957. {
  2958. EXPECT_DECLS;
  2959. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2960. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2961. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2962. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2963. !defined(NO_SHA256)
  2964. WOLFSSL_CERT_MANAGER* cm = NULL;
  2965. WOLFSSL_EVP_PKEY *priv = NULL;
  2966. WOLFSSL_X509_NAME* name = NULL;
  2967. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2968. const char* server_cert = "./certs/test/server-goodcn.pem";
  2969. byte *der = NULL;
  2970. int derSz;
  2971. byte *pt;
  2972. WOLFSSL_X509 *x509 = NULL;
  2973. WOLFSSL_X509 *ca = NULL;
  2974. pt = (byte*)server_key_der_2048;
  2975. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2976. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2977. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2978. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2979. WOLFSSL_FILETYPE_ASN1));
  2980. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2981. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2982. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2983. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2984. /* check satisfying wolfssl.com constraint passes */
  2985. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2986. WOLFSSL_FILETYPE_PEM));
  2987. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2988. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2989. name = NULL;
  2990. ExpectNotNull(name = X509_NAME_new());
  2991. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2992. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2993. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2994. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2995. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2996. X509_NAME_free(name);
  2997. name = NULL;
  2998. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2999. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  3000. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3001. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  3002. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3003. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3004. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3005. wolfSSL_X509_free(x509);
  3006. x509 = NULL;
  3007. /* fail with DNS check because of common name */
  3008. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3009. WOLFSSL_FILETYPE_PEM));
  3010. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3011. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3012. name = NULL;
  3013. ExpectNotNull(name = X509_NAME_new());
  3014. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3015. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3016. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  3017. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  3018. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3019. X509_NAME_free(name);
  3020. name = NULL;
  3021. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3022. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  3023. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3024. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  3025. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3026. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3027. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3028. wolfSSL_X509_free(x509);
  3029. x509 = NULL;
  3030. /* fail on permitted DNS name constraint */
  3031. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3032. WOLFSSL_FILETYPE_PEM));
  3033. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3034. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3035. name = NULL;
  3036. ExpectNotNull(name = X509_NAME_new());
  3037. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3038. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3039. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3040. X509_NAME_free(name);
  3041. name = NULL;
  3042. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  3043. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  3044. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3045. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3046. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  3047. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3048. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3049. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3050. wolfSSL_X509_free(x509);
  3051. x509 = NULL;
  3052. /* fail on permitted email name constraint */
  3053. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3054. WOLFSSL_FILETYPE_PEM));
  3055. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3056. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3057. name = NULL;
  3058. ExpectNotNull(name = X509_NAME_new());
  3059. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3060. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3061. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3062. X509_NAME_free(name);
  3063. name = NULL;
  3064. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3065. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3066. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  3067. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3068. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  3069. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3070. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3071. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3072. wolfSSL_X509_free(x509);
  3073. x509 = NULL;
  3074. /* success with empty email name */
  3075. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3076. WOLFSSL_FILETYPE_PEM));
  3077. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3078. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3079. name = NULL;
  3080. ExpectNotNull(name = X509_NAME_new());
  3081. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3082. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3083. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3084. X509_NAME_free(name);
  3085. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3086. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3087. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  3088. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3089. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3090. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3091. wolfSSL_X509_free(x509);
  3092. wolfSSL_CertManagerFree(cm);
  3093. wolfSSL_X509_free(ca);
  3094. wolfSSL_EVP_PKEY_free(priv);
  3095. #endif
  3096. return EXPECT_RESULT();
  3097. }
  3098. static int test_wolfSSL_CertManagerCRL(void)
  3099. {
  3100. EXPECT_DECLS;
  3101. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  3102. !defined(NO_RSA)
  3103. const char* ca_cert = "./certs/ca-cert.pem";
  3104. const char* crl1 = "./certs/crl/crl.pem";
  3105. const char* crl2 = "./certs/crl/crl2.pem";
  3106. #ifdef WC_RSA_PSS
  3107. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  3108. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  3109. #endif
  3110. const unsigned char crl_buff[] = {
  3111. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  3112. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  3113. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  3114. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  3115. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  3116. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  3117. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  3118. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  3119. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  3120. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  3121. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  3122. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  3123. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  3124. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  3125. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  3126. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  3127. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  3128. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  3129. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  3130. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  3131. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  3132. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  3133. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  3134. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  3135. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  3136. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  3137. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  3138. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  3139. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  3140. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  3141. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  3142. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  3143. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  3144. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  3145. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  3146. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  3147. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  3148. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  3149. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  3150. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  3151. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  3152. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  3153. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  3154. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  3155. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  3156. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  3157. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3158. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3159. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3160. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3161. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3162. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3163. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3164. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3165. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3166. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3167. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3168. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3169. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3170. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3171. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3172. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3173. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3174. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3175. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3176. };
  3177. WOLFSSL_CERT_MANAGER* cm = NULL;
  3178. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3179. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3180. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3181. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3182. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3183. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3184. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3185. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3186. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3187. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3188. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3189. BAD_FUNC_ARG);
  3190. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3191. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3192. BAD_FUNC_ARG);
  3193. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3194. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3195. BAD_FUNC_ARG);
  3196. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3197. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3198. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3199. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3200. #ifdef HAVE_CRL_IO
  3201. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3202. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3203. #endif
  3204. #ifndef NO_FILESYSTEM
  3205. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3206. 0), BAD_FUNC_ARG);
  3207. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3208. 0), BAD_FUNC_ARG);
  3209. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3210. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3211. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3212. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3213. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3214. BAD_FUNC_ARG);
  3215. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3216. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3217. ASN_PARSE_E);
  3218. #endif
  3219. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3220. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3221. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3222. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3223. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3224. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3225. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3226. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3227. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3228. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3229. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3230. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3231. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3232. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3233. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3234. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3235. ExpectIntEQ(WOLFSSL_SUCCESS,
  3236. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3237. ExpectIntEQ(WOLFSSL_SUCCESS,
  3238. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3239. ExpectIntEQ(WOLFSSL_SUCCESS,
  3240. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3241. wolfSSL_CertManagerFreeCRL(cm);
  3242. ExpectIntEQ(WOLFSSL_SUCCESS,
  3243. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3244. ExpectIntEQ(WOLFSSL_SUCCESS,
  3245. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3246. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3247. sizeof_server_cert_der_2048), CRL_MISSING);
  3248. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3249. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3250. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3251. WOLFSSL_FILETYPE_ASN1), 1);
  3252. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3253. /* loading should fail without the CA set */
  3254. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3255. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3256. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3257. ExpectIntEQ(WOLFSSL_SUCCESS,
  3258. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3259. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3260. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3261. #endif
  3262. wolfSSL_CertManagerFree(cm);
  3263. #endif
  3264. return EXPECT_RESULT();
  3265. }
  3266. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3267. {
  3268. EXPECT_DECLS;
  3269. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3270. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3271. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3272. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3273. defined(HAVE_LIGHTY)
  3274. WOLFSSL_CERT_MANAGER* cm = NULL;
  3275. /* Raw OCSP response bytes captured using the following setup:
  3276. * - Run responder with
  3277. * openssl ocsp -port 9999 -ndays 9999
  3278. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3279. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3280. * -rkey certs/ocsp/ocsp-responder-key.pem
  3281. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3282. * - Run client with
  3283. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3284. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3285. * -cert certs/ocsp/server1-cert.pem
  3286. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3287. * - Select the response packet in Wireshark, and export it using
  3288. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3289. * packets only". After importing into the editor, remove the initial
  3290. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3291. */
  3292. static const byte response[] = {
  3293. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3294. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3295. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3296. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3297. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3298. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3299. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3300. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3301. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3302. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3303. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3304. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3305. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3306. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3307. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3308. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3309. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3310. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3311. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3312. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3313. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3314. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3315. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3316. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3317. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3318. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3319. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3320. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3321. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3322. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3323. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3324. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3325. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3326. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3327. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3328. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3329. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3330. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3331. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3332. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3333. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3334. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3335. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3336. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3337. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3338. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3339. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3340. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3341. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3342. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3343. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3344. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3345. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3346. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3347. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3348. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3349. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3350. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3351. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3352. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3353. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3354. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3355. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3356. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3357. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3358. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3359. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3360. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3361. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3362. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3363. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3364. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3365. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3366. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3367. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3368. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3369. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3370. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3371. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3372. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3373. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3374. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3375. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3376. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3377. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3378. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3379. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3380. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3381. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3382. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3383. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3384. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3385. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3386. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3387. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3388. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3389. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3390. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3391. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3392. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3393. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3394. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3395. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3396. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3397. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3398. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3399. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3400. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3401. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3402. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3403. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3404. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3405. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3406. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3407. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3408. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3409. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3410. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3411. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3412. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3413. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3414. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3415. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3416. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3417. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3418. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3419. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3420. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3421. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3422. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3423. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3424. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3425. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3426. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3427. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3428. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3429. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3430. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3431. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3432. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3433. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3434. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3435. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3436. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3437. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3438. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3439. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3440. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3441. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3442. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3443. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3444. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3445. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3446. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3447. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3448. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3449. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3450. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3451. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3452. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3453. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3454. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3455. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3456. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3457. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3458. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3459. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3460. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3461. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3462. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3463. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3464. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3465. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3466. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3467. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3468. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3469. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3470. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3471. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3472. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3473. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3474. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3475. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3476. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3477. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3478. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3479. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3480. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3481. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3482. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3483. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3484. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3485. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3486. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3487. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3488. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3489. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3490. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3491. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3492. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3493. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3494. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3495. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3496. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3497. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3498. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3499. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3500. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3501. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3502. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3503. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3504. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3505. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3506. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3507. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3508. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3509. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3510. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3511. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3512. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3513. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3514. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3515. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3516. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3517. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3518. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3519. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3520. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3521. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3522. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3523. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3524. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3525. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3526. };
  3527. OcspEntry entry[1];
  3528. CertStatus status[1];
  3529. OcspRequest* request = NULL;
  3530. #ifndef NO_FILESYSTEM
  3531. const char* ca_cert = "./certs/ca-cert.pem";
  3532. #endif
  3533. byte serial[] = {0x05};
  3534. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3535. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3536. XMEMSET(entry, 0, sizeof(OcspEntry));
  3537. XMEMSET(status, 0, sizeof(CertStatus));
  3538. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3539. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3540. DYNAMIC_TYPE_OCSP_REQUEST));
  3541. if ((request != NULL) && (request->serial != NULL)) {
  3542. request->serialSz = sizeof(serial);
  3543. XMEMCPY(request->serial, serial, sizeof(serial));
  3544. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3545. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3546. }
  3547. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3548. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3549. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3550. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3551. /* Response should be valid. */
  3552. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3553. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3554. /* Flip a byte in the request serial number, response should be invalid
  3555. * now. */
  3556. if ((request != NULL) && (request->serial != NULL))
  3557. request->serial[0] ^= request->serial[0];
  3558. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3559. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3560. #ifndef NO_FILESYSTEM
  3561. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3562. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3563. ExpectIntEQ(WOLFSSL_SUCCESS,
  3564. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3565. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3566. sizeof(server_cert_der_2048)), 1);
  3567. #endif
  3568. wolfSSL_OCSP_REQUEST_free(request);
  3569. wolfSSL_CertManagerFree(cm);
  3570. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3571. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3572. #endif /* HAVE_OCSP */
  3573. return EXPECT_RESULT();
  3574. }
  3575. static int test_wolfSSL_CheckOCSPResponse(void)
  3576. {
  3577. EXPECT_DECLS;
  3578. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3579. defined(OPENSSL_ALL)
  3580. const char* responseFile = "./certs/ocsp/test-response.der";
  3581. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3582. const char* responseNoInternFile =
  3583. "./certs/ocsp/test-response-nointern.der";
  3584. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3585. OcspResponse* res = NULL;
  3586. byte data[4096];
  3587. const unsigned char* pt;
  3588. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3589. * gcc sanitizer with --enable-heapmath.
  3590. */
  3591. XFILE f = XBADFILE;
  3592. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3593. WOLFSSL_X509_STORE* st = NULL;
  3594. WOLFSSL_X509* issuer = NULL;
  3595. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3596. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3597. if (f != XBADFILE) {
  3598. XFCLOSE(f);
  3599. f = XBADFILE;
  3600. }
  3601. pt = data;
  3602. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3603. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3604. SSL_FILETYPE_PEM));
  3605. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3606. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3607. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3608. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3609. wolfSSL_OCSP_BASICRESP_free(bs);
  3610. bs = NULL;
  3611. wolfSSL_OCSP_RESPONSE_free(res);
  3612. res = NULL;
  3613. wolfSSL_X509_STORE_free(st);
  3614. st = NULL;
  3615. wolfSSL_X509_free(issuer);
  3616. issuer = NULL;
  3617. /* check loading a response with optional certs */
  3618. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3619. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3620. if (f != XBADFILE)
  3621. XFCLOSE(f);
  3622. f = XBADFILE;
  3623. pt = data;
  3624. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3625. wolfSSL_OCSP_RESPONSE_free(res);
  3626. res = NULL;
  3627. /* check loading a response with multiple certs */
  3628. {
  3629. WOLFSSL_CERT_MANAGER* cm = NULL;
  3630. OcspEntry *entry = NULL;
  3631. CertStatus* status = NULL;
  3632. OcspRequest* request = NULL;
  3633. byte serial1[] = {0x01};
  3634. byte serial[] = {0x02};
  3635. byte issuerHash[] = {
  3636. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3637. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3638. 0x37, 0x15, 0x8A, 0x88
  3639. };
  3640. byte issuerKeyHash[] = {
  3641. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3642. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3643. 0x7E, 0x72, 0x15, 0x21
  3644. };
  3645. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3646. DYNAMIC_TYPE_OPENSSL));
  3647. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3648. DYNAMIC_TYPE_OPENSSL));
  3649. if (entry != NULL)
  3650. XMEMSET(entry, 0, sizeof(OcspEntry));
  3651. if (status != NULL)
  3652. XMEMSET(status, 0, sizeof(CertStatus));
  3653. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3654. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3655. DYNAMIC_TYPE_OCSP_REQUEST));
  3656. if (request != NULL && request->serial != NULL) {
  3657. request->serialSz = sizeof(serial);
  3658. XMEMCPY(request->serial, serial, sizeof(serial));
  3659. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3660. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3661. sizeof(issuerKeyHash));
  3662. }
  3663. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3664. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3665. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3666. WOLFSSL_SUCCESS);
  3667. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3668. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3669. if (f != XBADFILE)
  3670. XFCLOSE(f);
  3671. f = XBADFILE;
  3672. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3673. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3674. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3675. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3676. ExpectNotNull(entry->status);
  3677. if (request != NULL && request->serial != NULL)
  3678. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3679. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3680. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3681. /* store both status's in the entry to check that "next" is not
  3682. * overwritten */
  3683. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3684. status->next = entry->status;
  3685. entry->status = status;
  3686. }
  3687. if (request != NULL && request->serial != NULL)
  3688. XMEMCPY(request->serial, serial, sizeof(serial));
  3689. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3690. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3691. ExpectNotNull(entry->status->next);
  3692. /* compare the status found */
  3693. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3694. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3695. status->serialSz), 0);
  3696. if (status != NULL && entry != NULL && entry->status != status) {
  3697. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3698. }
  3699. wolfSSL_OCSP_CERTID_free(entry);
  3700. wolfSSL_OCSP_REQUEST_free(request);
  3701. wolfSSL_CertManagerFree(cm);
  3702. }
  3703. #if defined(WC_RSA_PSS)
  3704. {
  3705. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3706. /* check loading a response with RSA-PSS signature */
  3707. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3708. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3709. if (f != XBADFILE)
  3710. XFCLOSE(f);
  3711. pt = data;
  3712. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3713. /* try to verify the response */
  3714. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3715. SSL_FILETYPE_PEM));
  3716. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3717. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3718. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3719. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3720. WOLFSSL_SUCCESS);
  3721. wolfSSL_OCSP_BASICRESP_free(bs);
  3722. wolfSSL_OCSP_RESPONSE_free(res);
  3723. wolfSSL_X509_STORE_free(st);
  3724. wolfSSL_X509_free(issuer);
  3725. }
  3726. #endif
  3727. #endif /* HAVE_OCSP */
  3728. return EXPECT_RESULT();
  3729. }
  3730. static int test_wolfSSL_FPKI(void)
  3731. {
  3732. EXPECT_DECLS;
  3733. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3734. XFILE f = XBADFILE;
  3735. const char* fpkiCert = "./certs/fpki-cert.der";
  3736. DecodedCert cert;
  3737. byte buf[4096];
  3738. byte* uuid = NULL;
  3739. byte* fascn = NULL;
  3740. word32 fascnSz;
  3741. word32 uuidSz;
  3742. int bytes = 0;
  3743. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3744. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3745. if (f != XBADFILE)
  3746. XFCLOSE(f);
  3747. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3748. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3749. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3750. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3751. DYNAMIC_TYPE_TMP_BUFFER));
  3752. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3753. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3754. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3755. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3756. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3757. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3758. wc_FreeDecodedCert(&cert);
  3759. #endif
  3760. return EXPECT_RESULT();
  3761. }
  3762. /* use RID in confuncture with other names to test parsing of unknown other
  3763. * names */
  3764. static int test_wolfSSL_OtherName(void)
  3765. {
  3766. EXPECT_DECLS;
  3767. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3768. XFILE f = XBADFILE;
  3769. const char* ridCert = "./certs/rid-cert.der";
  3770. DecodedCert cert;
  3771. byte buf[4096];
  3772. int bytes = 0;
  3773. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3774. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3775. if (f != XBADFILE)
  3776. XFCLOSE(f);
  3777. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3778. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3779. wc_FreeDecodedCert(&cert);
  3780. #endif
  3781. return EXPECT_RESULT();
  3782. }
  3783. static int test_wolfSSL_CertRsaPss(void)
  3784. {
  3785. EXPECT_DECLS;
  3786. /* FIPS v2 and below don't support long salts. */
  3787. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3788. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3789. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3790. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3791. XFILE f = XBADFILE;
  3792. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3793. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3794. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3795. RSA_MAX_SIZE >= 3072
  3796. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3797. #endif
  3798. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3799. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3800. #endif
  3801. DecodedCert cert;
  3802. byte buf[4096];
  3803. int bytes = 0;
  3804. WOLFSSL_CERT_MANAGER* cm = NULL;
  3805. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3806. ExpectIntEQ(WOLFSSL_SUCCESS,
  3807. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3808. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3809. ExpectIntEQ(WOLFSSL_SUCCESS,
  3810. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3811. #endif
  3812. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3813. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3814. if (f != XBADFILE) {
  3815. XFCLOSE(f);
  3816. f = XBADFILE;
  3817. }
  3818. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3819. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3820. wc_FreeDecodedCert(&cert);
  3821. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3822. RSA_MAX_SIZE >= 3072
  3823. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3824. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3825. if (f != XBADFILE)
  3826. XFCLOSE(f);
  3827. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3828. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3829. wc_FreeDecodedCert(&cert);
  3830. #endif
  3831. wolfSSL_CertManagerFree(cm);
  3832. #endif
  3833. return EXPECT_RESULT();
  3834. }
  3835. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3836. {
  3837. EXPECT_DECLS;
  3838. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3839. !defined(NO_WOLFSSL_CLIENT)
  3840. WOLFSSL_CTX* ctx = NULL;
  3841. const char* ca_cert = "./certs/ca-cert.pem";
  3842. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3843. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3844. /* test good CA */
  3845. ExpectTrue(WOLFSSL_SUCCESS ==
  3846. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3847. WOLFSSL_LOAD_FLAG_NONE));
  3848. /* test expired CA */
  3849. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3850. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3851. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3852. #else
  3853. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3854. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3855. #endif
  3856. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3857. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3858. wolfSSL_CTX_free(ctx);
  3859. #endif
  3860. return EXPECT_RESULT();
  3861. }
  3862. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3863. {
  3864. EXPECT_DECLS;
  3865. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3866. defined(USE_CERT_BUFFERS_2048)
  3867. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3868. WOLFSSL_CTX* ctx;
  3869. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3870. byte ca_expired_cert[TWOK_BUF];
  3871. word32 sizeof_ca_expired_cert = 0;
  3872. XFILE fp = XBADFILE;
  3873. #ifndef NO_WOLFSSL_CLIENT
  3874. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3875. #else
  3876. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3877. #endif
  3878. ExpectNotNull(ctx);
  3879. /* test good CA */
  3880. ExpectTrue(WOLFSSL_SUCCESS ==
  3881. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3882. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3883. WOLFSSL_LOAD_FLAG_NONE));
  3884. /* load expired CA */
  3885. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3886. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3887. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3888. sizeof(ca_expired_cert), fp), 0);
  3889. if (fp != XBADFILE)
  3890. XFCLOSE(fp);
  3891. /* test expired CA failure */
  3892. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3893. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3894. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3895. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3896. #else
  3897. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3898. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3899. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3900. #endif
  3901. /* test expired CA success */
  3902. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3903. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3904. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3905. wolfSSL_CTX_free(ctx);
  3906. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3907. #endif
  3908. return EXPECT_RESULT();
  3909. }
  3910. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3911. {
  3912. EXPECT_DECLS;
  3913. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3914. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3915. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3916. WOLFSSL_CTX* ctx = NULL;
  3917. #ifndef NO_WOLFSSL_CLIENT
  3918. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3919. #else
  3920. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3921. #endif
  3922. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3923. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3924. WOLFSSL_FILETYPE_ASN1));
  3925. wolfSSL_CTX_free(ctx);
  3926. #endif
  3927. return EXPECT_RESULT();
  3928. }
  3929. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3930. {
  3931. EXPECT_DECLS;
  3932. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3933. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3934. WOLFSSL_CTX* ctx;
  3935. WOLFSSL* ssl = NULL;
  3936. const char *certChain[] = {
  3937. "./certs/intermediate/client-int-cert.pem",
  3938. "./certs/intermediate/ca-int2-cert.pem",
  3939. "./certs/intermediate/ca-int-cert.pem",
  3940. "./certs/ca-cert.pem",
  3941. NULL
  3942. };
  3943. const char** cert;
  3944. WOLFSSL_X509* x509 = NULL;
  3945. WOLF_STACK_OF(X509)* chain = NULL;
  3946. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3947. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3948. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3949. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3950. WOLFSSL_FILETYPE_PEM));
  3951. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3952. X509_free(x509);
  3953. x509 = NULL;
  3954. }
  3955. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3956. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3957. WOLFSSL_FILETYPE_PEM));
  3958. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3959. X509_free(x509);
  3960. x509 = NULL;
  3961. }
  3962. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3963. ExpectIntEQ(sk_X509_num(chain), 3);
  3964. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3965. ExpectIntEQ(sk_X509_num(chain), 3);
  3966. SSL_free(ssl);
  3967. SSL_CTX_free(ctx);
  3968. #endif
  3969. return EXPECT_RESULT();
  3970. }
  3971. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3972. {
  3973. EXPECT_DECLS;
  3974. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3975. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3976. const char* server_chain_der = "./certs/server-cert-chain.der";
  3977. const char* client_single_pem = "./certs/client-cert.pem";
  3978. WOLFSSL_CTX* ctx;
  3979. (void)server_chain_der;
  3980. (void)client_single_pem;
  3981. (void)ctx;
  3982. #ifndef NO_WOLFSSL_CLIENT
  3983. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3984. #else
  3985. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3986. #endif
  3987. ExpectNotNull(ctx);
  3988. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3989. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3990. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3991. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3992. wolfSSL_CTX_free(ctx);
  3993. #endif
  3994. return EXPECT_RESULT();
  3995. }
  3996. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3997. {
  3998. EXPECT_DECLS;
  3999. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4000. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4001. WOLFSSL_CTX *ctx = NULL;
  4002. (void)ctx;
  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_file(NULL,
  4010. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4011. /* invalid dhParamFile file */
  4012. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  4013. NULL, WOLFSSL_FILETYPE_PEM));
  4014. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  4015. bogusFile, WOLFSSL_FILETYPE_PEM));
  4016. /* success */
  4017. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  4018. WOLFSSL_FILETYPE_PEM));
  4019. wolfSSL_CTX_free(ctx);
  4020. #endif
  4021. return EXPECT_RESULT();
  4022. }
  4023. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  4024. {
  4025. EXPECT_DECLS;
  4026. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4027. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4028. WOLFSSL_CTX *ctx = NULL;
  4029. #ifndef NO_WOLFSSL_CLIENT
  4030. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4031. #else
  4032. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4033. #endif
  4034. /* invalid context */
  4035. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  4036. dh_key_der_2048, sizeof_dh_key_der_2048,
  4037. WOLFSSL_FILETYPE_ASN1));
  4038. /* invalid dhParamFile file */
  4039. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  4040. 0, WOLFSSL_FILETYPE_ASN1));
  4041. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4042. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  4043. WOLFSSL_FILETYPE_ASN1));
  4044. /* success */
  4045. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4046. dh_key_der_2048, sizeof_dh_key_der_2048,
  4047. WOLFSSL_FILETYPE_ASN1));
  4048. wolfSSL_CTX_free(ctx);
  4049. #endif
  4050. return EXPECT_RESULT();
  4051. }
  4052. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  4053. {
  4054. EXPECT_DECLS;
  4055. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4056. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4057. WOLFSSL_CTX *ctx;
  4058. (void)ctx;
  4059. #ifndef NO_WOLFSSL_CLIENT
  4060. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4061. #else
  4062. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  4063. #endif
  4064. ExpectNotNull(ctx);
  4065. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4066. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  4067. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4068. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  4069. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4070. dh_key_der_2048, sizeof_dh_key_der_2048,
  4071. WOLFSSL_FILETYPE_ASN1));
  4072. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4073. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4074. dh_key_der_2048, sizeof_dh_key_der_2048,
  4075. WOLFSSL_FILETYPE_ASN1));
  4076. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  4077. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4078. dh_key_der_2048, sizeof_dh_key_der_2048,
  4079. WOLFSSL_FILETYPE_ASN1));
  4080. wolfSSL_CTX_free(ctx);
  4081. #endif
  4082. return EXPECT_RESULT();
  4083. }
  4084. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  4085. {
  4086. EXPECT_DECLS;
  4087. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  4088. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4089. WOLFSSL_CTX* ctx = NULL;
  4090. const char* derCert = "./certs/server-cert.der";
  4091. const char* nullPath = NULL;
  4092. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  4093. const char* emptyPath = "";
  4094. /* der load Case 1 ctx NULL */
  4095. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4096. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4097. #ifndef NO_WOLFSSL_CLIENT
  4098. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4099. #else
  4100. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4101. #endif
  4102. /* Case 2 filePath NULL */
  4103. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  4104. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4105. /* Case 3 invalid format */
  4106. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4107. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  4108. /* Case 4 filePath not valid */
  4109. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  4110. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4111. /* Case 5 filePath empty */
  4112. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  4113. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4114. #ifndef NO_RSA
  4115. /* Case 6 success case */
  4116. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4117. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4118. #endif
  4119. wolfSSL_CTX_free(ctx);
  4120. #endif
  4121. return EXPECT_RESULT();
  4122. }
  4123. static int test_wolfSSL_CTX_enable_disable(void)
  4124. {
  4125. EXPECT_DECLS;
  4126. #ifndef NO_CERTS
  4127. WOLFSSL_CTX* ctx = NULL;
  4128. #ifdef HAVE_CRL
  4129. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  4130. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  4131. #endif
  4132. #ifdef HAVE_OCSP
  4133. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  4134. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  4135. #endif
  4136. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4137. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4138. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  4139. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  4140. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4141. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4142. #endif
  4143. #ifndef NO_WOLFSSL_CLIENT
  4144. #ifdef HAVE_EXTENDED_MASTER
  4145. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  4146. #endif
  4147. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4148. #ifdef HAVE_EXTENDED_MASTER
  4149. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  4150. #endif
  4151. #elif !defined(NO_WOLFSSL_SERVER)
  4152. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4153. #endif
  4154. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4155. #ifdef HAVE_CRL
  4156. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  4157. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4158. #endif
  4159. #ifdef HAVE_OCSP
  4160. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4161. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4162. WOLFSSL_SUCCESS);
  4163. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4164. WOLFSSL_SUCCESS);
  4165. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4166. WOLFSSL_SUCCESS);
  4167. #endif
  4168. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4169. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4170. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4171. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4172. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4173. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4174. #endif
  4175. wolfSSL_CTX_free(ctx);
  4176. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4177. #endif /* NO_CERTS */
  4178. return EXPECT_RESULT();
  4179. }
  4180. static int test_wolfSSL_CTX_ticket_API(void)
  4181. {
  4182. EXPECT_DECLS;
  4183. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4184. WOLFSSL_CTX* ctx = NULL;
  4185. void *userCtx = (void*)"this is my ctx";
  4186. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4187. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4188. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4189. wolfSSL_CTX_free(ctx);
  4190. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4191. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4192. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4193. return EXPECT_RESULT();
  4194. }
  4195. static int test_wolfSSL_set_minmax_proto_version(void)
  4196. {
  4197. EXPECT_DECLS;
  4198. #ifdef OPENSSL_EXTRA
  4199. WOLFSSL_CTX *ctx = NULL;
  4200. WOLFSSL *ssl = NULL;
  4201. (void)ssl;
  4202. #ifndef NO_WOLFSSL_CLIENT
  4203. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4204. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4205. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4206. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4207. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4208. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4209. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4210. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4211. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4212. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4213. wolfSSL_free(ssl);
  4214. wolfSSL_CTX_free(ctx);
  4215. ctx = NULL;
  4216. #endif
  4217. #ifndef NO_WOLFSSL_SERVER
  4218. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4219. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4220. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4221. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4222. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4223. wolfSSL_CTX_free(ctx);
  4224. #endif
  4225. #endif
  4226. return EXPECT_RESULT();
  4227. }
  4228. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4229. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4230. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4231. {
  4232. EXPECT_DECLS;
  4233. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4234. return EXPECT_RESULT();
  4235. }
  4236. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4237. {
  4238. EXPECT_DECLS;
  4239. /* Set TLS 1.2 */
  4240. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4241. WOLFSSL_SUCCESS);
  4242. return EXPECT_RESULT();
  4243. }
  4244. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4245. * what was set at ctx creation. */
  4246. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4247. {
  4248. EXPECT_DECLS;
  4249. test_ssl_cbf client_cbs;
  4250. test_ssl_cbf server_cbs;
  4251. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4252. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4253. client_cbs.method = wolfTLS_client_method;
  4254. server_cbs.method = wolfTLS_server_method;
  4255. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4256. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4257. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4258. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4259. &server_cbs, NULL), TEST_SUCCESS);
  4260. return EXPECT_RESULT();
  4261. }
  4262. #else
  4263. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4264. {
  4265. return TEST_SKIPPED;
  4266. }
  4267. #endif
  4268. /*----------------------------------------------------------------------------*
  4269. | SSL
  4270. *----------------------------------------------------------------------------*/
  4271. static int test_server_wolfSSL_new(void)
  4272. {
  4273. EXPECT_DECLS;
  4274. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4275. !defined(NO_WOLFSSL_SERVER)
  4276. WOLFSSL_CTX *ctx = NULL;
  4277. WOLFSSL_CTX *ctx_nocert = NULL;
  4278. WOLFSSL *ssl = NULL;
  4279. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4280. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4281. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4282. WOLFSSL_FILETYPE_PEM));
  4283. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4284. WOLFSSL_FILETYPE_PEM));
  4285. /* invalid context */
  4286. ExpectNull(ssl = wolfSSL_new(NULL));
  4287. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4288. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4289. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4290. #endif
  4291. /* success */
  4292. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4293. wolfSSL_free(ssl);
  4294. wolfSSL_CTX_free(ctx);
  4295. wolfSSL_CTX_free(ctx_nocert);
  4296. #endif
  4297. return EXPECT_RESULT();
  4298. }
  4299. static int test_client_wolfSSL_new(void)
  4300. {
  4301. EXPECT_DECLS;
  4302. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4303. !defined(NO_WOLFSSL_CLIENT)
  4304. WOLFSSL_CTX *ctx = NULL;
  4305. WOLFSSL_CTX *ctx_nocert = NULL;
  4306. WOLFSSL *ssl = NULL;
  4307. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4308. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4309. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4310. /* invalid context */
  4311. ExpectNull(ssl = wolfSSL_new(NULL));
  4312. /* success */
  4313. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4314. wolfSSL_free(ssl);
  4315. ssl = NULL;
  4316. /* success */
  4317. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4318. wolfSSL_free(ssl);
  4319. wolfSSL_CTX_free(ctx);
  4320. wolfSSL_CTX_free(ctx_nocert);
  4321. #endif
  4322. return EXPECT_RESULT();
  4323. }
  4324. static int test_wolfSSL_SetTmpDH_file(void)
  4325. {
  4326. EXPECT_DECLS;
  4327. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4328. !defined(NO_WOLFSSL_SERVER)
  4329. WOLFSSL_CTX *ctx = NULL;
  4330. WOLFSSL *ssl = NULL;
  4331. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4332. #ifndef NO_RSA
  4333. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4334. WOLFSSL_FILETYPE_PEM));
  4335. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4336. WOLFSSL_FILETYPE_PEM));
  4337. #elif defined(HAVE_ECC)
  4338. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4339. WOLFSSL_FILETYPE_PEM));
  4340. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4341. WOLFSSL_FILETYPE_PEM));
  4342. #elif defined(HAVE_ED25519)
  4343. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4344. WOLFSSL_FILETYPE_PEM));
  4345. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4346. WOLFSSL_FILETYPE_PEM));
  4347. #elif defined(HAVE_ED448)
  4348. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4349. WOLFSSL_FILETYPE_PEM));
  4350. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4351. WOLFSSL_FILETYPE_PEM));
  4352. #endif
  4353. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4354. /* invalid ssl */
  4355. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4356. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4357. /* invalid dhParamFile file */
  4358. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4359. NULL, WOLFSSL_FILETYPE_PEM));
  4360. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4361. bogusFile, WOLFSSL_FILETYPE_PEM));
  4362. /* success */
  4363. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4364. WOLFSSL_FILETYPE_PEM));
  4365. wolfSSL_free(ssl);
  4366. wolfSSL_CTX_free(ctx);
  4367. #endif
  4368. return EXPECT_RESULT();
  4369. }
  4370. static int test_wolfSSL_SetTmpDH_buffer(void)
  4371. {
  4372. EXPECT_DECLS;
  4373. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4374. WOLFSSL_CTX *ctx = NULL;
  4375. WOLFSSL *ssl = NULL;
  4376. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4377. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4378. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4379. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4380. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4381. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4382. /* invalid ssl */
  4383. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4384. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4385. /* invalid dhParamFile file */
  4386. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4387. 0, WOLFSSL_FILETYPE_ASN1));
  4388. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4389. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4390. /* success */
  4391. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4392. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4393. wolfSSL_free(ssl);
  4394. wolfSSL_CTX_free(ctx);
  4395. #endif
  4396. return EXPECT_RESULT();
  4397. }
  4398. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4399. {
  4400. EXPECT_DECLS;
  4401. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4402. WOLFSSL_CTX *ctx = NULL;
  4403. WOLFSSL_CTX *ctx2 = NULL;
  4404. WOLFSSL *ssl = NULL;
  4405. WOLFSSL *ssl2 = NULL;
  4406. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4407. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4408. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4409. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4410. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4411. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4412. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4413. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4414. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4415. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4416. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4417. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4418. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4419. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4420. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4421. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4422. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4423. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4424. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4425. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4426. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4427. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4428. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4429. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4430. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4431. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4432. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4433. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4434. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4435. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4436. wolfSSL_free(ssl2);
  4437. wolfSSL_CTX_free(ctx2);
  4438. wolfSSL_free(ssl);
  4439. wolfSSL_CTX_free(ctx);
  4440. #endif
  4441. return EXPECT_RESULT();
  4442. }
  4443. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4444. * allowed.
  4445. * POST: return 1 on success.
  4446. */
  4447. static int test_wolfSSL_SetMinVersion(void)
  4448. {
  4449. int res = TEST_SKIPPED;
  4450. #ifndef NO_WOLFSSL_CLIENT
  4451. int failFlag = WOLFSSL_SUCCESS;
  4452. WOLFSSL_CTX* ctx = NULL;
  4453. WOLFSSL* ssl = NULL;
  4454. int itr;
  4455. #ifndef NO_OLD_TLS
  4456. const int versions[] = {
  4457. #ifdef WOLFSSL_ALLOW_TLSV10
  4458. WOLFSSL_TLSV1,
  4459. #endif
  4460. WOLFSSL_TLSV1_1,
  4461. WOLFSSL_TLSV1_2};
  4462. #elif !defined(WOLFSSL_NO_TLS12)
  4463. const int versions[] = { WOLFSSL_TLSV1_2 };
  4464. #else
  4465. const int versions[] = { WOLFSSL_TLSV1_3 };
  4466. #endif
  4467. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4468. ssl = wolfSSL_new(ctx);
  4469. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4470. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4471. failFlag = WOLFSSL_FAILURE;
  4472. }
  4473. }
  4474. wolfSSL_free(ssl);
  4475. wolfSSL_CTX_free(ctx);
  4476. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4477. #endif
  4478. return res;
  4479. } /* END test_wolfSSL_SetMinVersion */
  4480. #ifdef OPENSSL_EXTRA
  4481. static int test_EC25519(void)
  4482. {
  4483. EXPECT_DECLS;
  4484. #if defined(HAVE_CURVE25519) && defined(WOLFSSL_KEY_GEN)
  4485. byte priv[CURVE25519_KEYSIZE];
  4486. unsigned int privSz = CURVE25519_KEYSIZE;
  4487. byte pub[CURVE25519_KEYSIZE];
  4488. unsigned int pubSz = CURVE25519_KEYSIZE;
  4489. byte priv2[CURVE25519_KEYSIZE];
  4490. unsigned int priv2Sz = CURVE25519_KEYSIZE;
  4491. byte pub2[CURVE25519_KEYSIZE];
  4492. unsigned int pub2Sz = CURVE25519_KEYSIZE;
  4493. byte shared[CURVE25519_KEYSIZE];
  4494. unsigned int sharedSz = CURVE25519_KEYSIZE;
  4495. byte shared2[CURVE25519_KEYSIZE];
  4496. unsigned int shared2Sz = CURVE25519_KEYSIZE;
  4497. /* Bad parameter testing of key generation. */
  4498. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4499. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4500. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4501. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4502. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4503. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, NULL), 0);
  4504. /* Bad length */
  4505. privSz = 1;
  4506. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4507. privSz = CURVE25519_KEYSIZE;
  4508. pubSz = 1;
  4509. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4510. pubSz = CURVE25519_KEYSIZE;
  4511. /* Good case of generating key. */
  4512. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 1);
  4513. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv2, &priv2Sz, pub2, &pub2Sz),
  4514. 1);
  4515. ExpectIntEQ(privSz, CURVE25519_KEYSIZE);
  4516. ExpectIntEQ(pubSz, CURVE25519_KEYSIZE);
  4517. /* Bad parameter testing of shared key. */
  4518. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, NULL, NULL, privSz,
  4519. NULL, pubSz), 0);
  4520. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, NULL, privSz,
  4521. NULL, pubSz), 0);
  4522. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4523. pub, pubSz), 0);
  4524. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4525. pub, pubSz), 0);
  4526. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4527. NULL, pubSz), 0);
  4528. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4529. pub, pubSz), 0);
  4530. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, NULL, priv, privSz,
  4531. pub, pubSz), 0);
  4532. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4533. pub, pubSz), 0);
  4534. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4535. NULL, pubSz), 0);
  4536. /* Bad length. */
  4537. sharedSz = 1;
  4538. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4539. pub, pubSz), 0);
  4540. sharedSz = CURVE25519_KEYSIZE;
  4541. privSz = 1;
  4542. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4543. pub, pubSz), 0);
  4544. privSz = CURVE25519_KEYSIZE;
  4545. pubSz = 1;
  4546. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4547. pub, pubSz), 0);
  4548. pubSz = CURVE25519_KEYSIZE;
  4549. /* Good case of shared key. */
  4550. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4551. pub2, pub2Sz), 1);
  4552. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4553. pub, pubSz), 1);
  4554. ExpectIntEQ(sharedSz, CURVE25519_KEYSIZE);
  4555. ExpectIntEQ(shared2Sz, CURVE25519_KEYSIZE);
  4556. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4557. #endif /* HAVE_CURVE25519 && WOLFSSL_KEY_GEN */
  4558. return EXPECT_RESULT();
  4559. }
  4560. static int test_ED25519(void)
  4561. {
  4562. EXPECT_DECLS;
  4563. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4564. defined(WOLFSSL_KEY_GEN)
  4565. byte priv[ED25519_PRV_KEY_SIZE];
  4566. unsigned int privSz = (unsigned int)sizeof(priv);
  4567. byte pub[ED25519_PUB_KEY_SIZE];
  4568. unsigned int pubSz = (unsigned int)sizeof(pub);
  4569. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4570. const char* msg = TEST_STRING;
  4571. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4572. byte sig[ED25519_SIG_SIZE];
  4573. unsigned int sigSz = (unsigned int)sizeof(sig);
  4574. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4575. /* Bad parameter testing of key generation. */
  4576. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4577. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, NULL, NULL), 0);
  4578. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, NULL, NULL), 0);
  4579. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, pub, NULL), 0);
  4580. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4581. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4582. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4583. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4584. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, NULL), 0);
  4585. /* Bad length. */
  4586. privSz = 1;
  4587. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4588. privSz = ED25519_PRV_KEY_SIZE;
  4589. pubSz = 1;
  4590. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4591. pubSz = ED25519_PUB_KEY_SIZE;
  4592. /* Good case of generating key. */
  4593. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4594. 1);
  4595. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4596. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4597. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4598. /* Bad parameter testing of signing. */
  4599. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4600. NULL), 0);
  4601. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4602. NULL), 0);
  4603. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, NULL,
  4604. NULL), 0);
  4605. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, sig,
  4606. NULL), 0);
  4607. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4608. &sigSz), 0);
  4609. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, sig,
  4610. &sigSz), 0);
  4611. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, sig,
  4612. &sigSz), 0);
  4613. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, NULL,
  4614. &sigSz), 0);
  4615. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4616. NULL), 0);
  4617. /* Bad length. */
  4618. privSz = 1;
  4619. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4620. &sigSz), 0);
  4621. privSz = ED25519_PRV_KEY_SIZE;
  4622. sigSz = 1;
  4623. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4624. &sigSz), 0);
  4625. sigSz = ED25519_SIG_SIZE;
  4626. /* Good case of signing. */
  4627. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4628. &sigSz), 1);
  4629. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4630. #ifdef HAVE_ED25519_VERIFY
  4631. /* Bad parameter testing of verification. */
  4632. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, NULL,
  4633. sigSz), 0);
  4634. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4635. sigSz), 0);
  4636. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, NULL,
  4637. sigSz), 0);
  4638. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, sig,
  4639. sigSz), 0);
  4640. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, sig,
  4641. sigSz), 0);
  4642. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4643. sigSz), 0);
  4644. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4645. sigSz), 0);
  4646. /* Bad length. */
  4647. pubSz = 1;
  4648. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4649. sigSz), 0);
  4650. pubSz = ED25519_PUB_KEY_SIZE;
  4651. sigSz = 1;
  4652. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4653. sigSz), 0);
  4654. sigSz = ED25519_SIG_SIZE;
  4655. /* Good case of verification. */
  4656. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4657. sigSz), 1);
  4658. /* Bad signature. */
  4659. if (EXPECT_SUCCESS()) {
  4660. sig[1] ^= 0x80;
  4661. }
  4662. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4663. sigSz), 0);
  4664. #endif /* HAVE_ED25519_VERIFY */
  4665. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4666. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4667. return EXPECT_RESULT();
  4668. }
  4669. static int test_EC448(void)
  4670. {
  4671. EXPECT_DECLS;
  4672. #if defined(HAVE_CURVE448) && defined(WOLFSSL_KEY_GEN)
  4673. byte priv[CURVE448_KEY_SIZE];
  4674. unsigned int privSz = CURVE448_KEY_SIZE;
  4675. byte pub[CURVE448_KEY_SIZE];
  4676. unsigned int pubSz = CURVE448_KEY_SIZE;
  4677. byte priv2[CURVE448_KEY_SIZE];
  4678. unsigned int priv2Sz = CURVE448_KEY_SIZE;
  4679. byte pub2[CURVE448_KEY_SIZE];
  4680. unsigned int pub2Sz = CURVE448_KEY_SIZE;
  4681. byte shared[CURVE448_KEY_SIZE];
  4682. unsigned int sharedSz = CURVE448_KEY_SIZE;
  4683. byte shared2[CURVE448_KEY_SIZE];
  4684. unsigned int shared2Sz = CURVE448_KEY_SIZE;
  4685. /* Bad parameter testing of key generation. */
  4686. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, NULL, NULL, NULL), 0);
  4687. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4688. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4689. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, NULL, pub, &pubSz), 0);
  4690. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4691. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, NULL), 0);
  4692. /* Bad length. */
  4693. privSz = 1;
  4694. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4695. privSz = CURVE448_KEY_SIZE;
  4696. pubSz = 1;
  4697. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4698. pubSz = CURVE448_KEY_SIZE;
  4699. /* Good case of generating key. */
  4700. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4701. ExpectIntEQ(wolfSSL_EC448_generate_key(priv2, &priv2Sz, pub2, &pub2Sz), 1);
  4702. ExpectIntEQ(privSz, CURVE448_KEY_SIZE);
  4703. ExpectIntEQ(pubSz, CURVE448_KEY_SIZE);
  4704. /* Bad parameter testing of shared key. */
  4705. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, NULL, NULL, privSz,
  4706. NULL, pubSz), 0);
  4707. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, NULL, privSz,
  4708. NULL, pubSz), 0);
  4709. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4710. pub, pubSz), 0);
  4711. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4712. pub, pubSz), 0);
  4713. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4714. NULL, pubSz), 0);
  4715. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4716. pub, pubSz), 0);
  4717. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, NULL, priv, privSz,
  4718. pub, pubSz), 0);
  4719. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4720. pub, pubSz), 0);
  4721. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4722. NULL, pubSz), 0);
  4723. /* Bad length. */
  4724. sharedSz = 1;
  4725. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4726. pub, pubSz), 0);
  4727. sharedSz = CURVE448_KEY_SIZE;
  4728. privSz = 1;
  4729. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4730. pub, pubSz), 0);
  4731. privSz = CURVE448_KEY_SIZE;
  4732. pubSz = 1;
  4733. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4734. pub, pubSz), 0);
  4735. pubSz = CURVE448_KEY_SIZE;
  4736. /* Good case of shared key. */
  4737. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4738. pub2, pub2Sz), 1);
  4739. ExpectIntEQ(wolfSSL_EC448_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4740. pub, pubSz), 1);
  4741. ExpectIntEQ(sharedSz, CURVE448_KEY_SIZE);
  4742. ExpectIntEQ(shared2Sz, CURVE448_KEY_SIZE);
  4743. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4744. #endif /* HAVE_CURVE448 && WOLFSSL_KEY_GEN */
  4745. return EXPECT_RESULT();
  4746. }
  4747. static int test_ED448(void)
  4748. {
  4749. EXPECT_DECLS;
  4750. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4751. defined(WOLFSSL_KEY_GEN)
  4752. byte priv[ED448_PRV_KEY_SIZE];
  4753. unsigned int privSz = (unsigned int)sizeof(priv);
  4754. byte pub[ED448_PUB_KEY_SIZE];
  4755. unsigned int pubSz = (unsigned int)sizeof(pub);
  4756. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4757. const char* msg = TEST_STRING;
  4758. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4759. byte sig[ED448_SIG_SIZE];
  4760. unsigned int sigSz = (unsigned int)sizeof(sig);
  4761. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4762. /* Bad parameter testing of key generation. */
  4763. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, NULL), 0);
  4764. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, NULL, NULL), 0);
  4765. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, NULL, NULL), 0);
  4766. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, pub, NULL), 0);
  4767. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4768. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4769. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, pub, &pubSz), 0);
  4770. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4771. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, NULL), 0);
  4772. /* Bad length. */
  4773. privSz = 1;
  4774. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4775. privSz = ED448_PRV_KEY_SIZE;
  4776. pubSz = 1;
  4777. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4778. pubSz = ED448_PUB_KEY_SIZE;
  4779. /* Good case of generating key. */
  4780. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4781. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4782. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4783. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4784. /* Bad parameter testing of signing. */
  4785. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4786. NULL), 0);
  4787. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4788. NULL), 0);
  4789. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, NULL,
  4790. NULL), 0);
  4791. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, sig,
  4792. NULL), 0);
  4793. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4794. &sigSz), 0);
  4795. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, sig,
  4796. &sigSz), 0);
  4797. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, sig,
  4798. &sigSz), 0);
  4799. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, NULL,
  4800. &sigSz), 0);
  4801. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4802. NULL), 0);
  4803. /* Bad length. */
  4804. privSz = 1;
  4805. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4806. &sigSz), 0);
  4807. privSz = ED448_PRV_KEY_SIZE;
  4808. sigSz = 1;
  4809. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4810. &sigSz), 0);
  4811. sigSz = ED448_SIG_SIZE;
  4812. /* Good case of signing. */
  4813. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4814. &sigSz), 1);
  4815. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4816. #ifdef HAVE_ED448_VERIFY
  4817. /* Bad parameter testing of verification. */
  4818. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, NULL,
  4819. sigSz), 0);
  4820. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4821. sigSz), 0);
  4822. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, NULL,
  4823. sigSz), 0);
  4824. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, sig,
  4825. sigSz), 0);
  4826. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, sig,
  4827. sigSz), 0);
  4828. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4829. sigSz), 0);
  4830. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4831. sigSz), 0);
  4832. /* Bad length. */
  4833. pubSz = 1;
  4834. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4835. sigSz), 0);
  4836. pubSz = ED448_PUB_KEY_SIZE;
  4837. sigSz = 1;
  4838. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4839. sigSz), 0);
  4840. sigSz = ED448_SIG_SIZE;
  4841. /* Good case of verification. */
  4842. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4843. sigSz), 1);
  4844. /* Bad signature. */
  4845. if (EXPECT_SUCCESS()) {
  4846. sig[1] ^= 0x80;
  4847. }
  4848. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4849. sigSz), 0);
  4850. #endif /* HAVE_ED448_VERIFY */
  4851. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4852. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4853. return EXPECT_RESULT();
  4854. }
  4855. #endif /* OPENSSL_EXTRA */
  4856. #include <wolfssl/openssl/pem.h>
  4857. /*----------------------------------------------------------------------------*
  4858. | EVP
  4859. *----------------------------------------------------------------------------*/
  4860. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4861. {
  4862. EXPECT_DECLS;
  4863. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4864. WOLFSSL_BIO* rbio = NULL;
  4865. WOLFSSL_BIO* wbio = NULL;
  4866. WOLFSSL_EVP_PKEY* pkey = NULL;
  4867. char line[256] = { 0 };
  4868. char line1[256] = { 0 };
  4869. int i = 0;
  4870. /* test error cases */
  4871. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4872. /*
  4873. * test RSA public key print
  4874. * in this test, pass '3' for indent
  4875. */
  4876. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4877. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4878. sizeof_client_keypub_der_1024));
  4879. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4880. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4881. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4882. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4883. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4884. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4885. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4886. strcpy(line1, " Modulus:\n");
  4887. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4888. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4889. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4890. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4891. /* skip to the end of modulus element*/
  4892. for (i = 0; i < 8 ;i++) {
  4893. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4894. }
  4895. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4896. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4897. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4898. /* should reach EOF */
  4899. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4900. EVP_PKEY_free(pkey);
  4901. pkey = NULL;
  4902. BIO_free(rbio);
  4903. BIO_free(wbio);
  4904. rbio = NULL;
  4905. wbio = NULL;
  4906. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4907. /*
  4908. * test DSA public key print
  4909. */
  4910. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4911. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4912. sizeof_dsa_pub_key_der_2048));
  4913. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4914. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4915. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4916. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4917. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4918. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4919. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4920. strcpy(line1, "pub:\n");
  4921. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4922. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4923. strcpy(line1,
  4924. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4925. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4926. /* skip to the end of pub element*/
  4927. for (i = 0; i < 17 ;i++) {
  4928. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4929. }
  4930. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4931. strcpy(line1, "P:\n");
  4932. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4933. /* skip to the end of P element*/
  4934. for (i = 0; i < 18 ;i++) {
  4935. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4936. }
  4937. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4938. strcpy(line1, "Q:\n");
  4939. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4940. /* skip to the end of Q element*/
  4941. for (i = 0; i < 3 ;i++) {
  4942. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4943. }
  4944. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4945. strcpy(line1, "G:\n");
  4946. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4947. /* skip to the end of G element*/
  4948. for (i = 0; i < 18 ;i++) {
  4949. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4950. }
  4951. /* should reach EOF */
  4952. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4953. EVP_PKEY_free(pkey);
  4954. pkey = NULL;
  4955. BIO_free(rbio);
  4956. BIO_free(wbio);
  4957. rbio = NULL;
  4958. wbio = NULL;
  4959. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4960. /*
  4961. * test ECC public key print
  4962. */
  4963. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4964. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4965. sizeof_ecc_clikeypub_der_256));
  4966. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4967. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4968. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4969. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4970. strcpy(line1, "Public-Key: (256 bit)\n");
  4971. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4972. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4973. strcpy(line1, "pub:\n");
  4974. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4975. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4976. strcpy(line1,
  4977. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4978. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4979. /* skip to the end of pub element*/
  4980. for (i = 0; i < 4 ;i++) {
  4981. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4982. }
  4983. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4984. strcpy(line1, "ASN1 OID: prime256v1\n");
  4985. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4986. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4987. strcpy(line1, "NIST CURVE: P-256\n");
  4988. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4989. /* should reach EOF */
  4990. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4991. EVP_PKEY_free(pkey);
  4992. pkey = NULL;
  4993. BIO_free(rbio);
  4994. BIO_free(wbio);
  4995. rbio = NULL;
  4996. wbio = NULL;
  4997. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4998. /*
  4999. * test DH public key print
  5000. */
  5001. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  5002. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  5003. sizeof_dh_pub_key_der_2048));
  5004. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  5005. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  5006. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  5007. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5008. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  5009. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5010. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5011. strcpy(line1, "public-key:\n");
  5012. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5013. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5014. strcpy(line1,
  5015. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  5016. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5017. /* skip to the end of public-key element*/
  5018. for (i = 0; i < 17 ;i++) {
  5019. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5020. }
  5021. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5022. strcpy(line1, "prime:\n");
  5023. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5024. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5025. strcpy(line1,
  5026. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  5027. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5028. /* skip to the end of prime element*/
  5029. for (i = 0; i < 17 ;i++) {
  5030. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5031. }
  5032. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5033. strcpy(line1, "generator: 2 (0x02)\n");
  5034. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5035. /* should reach EOF */
  5036. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  5037. EVP_PKEY_free(pkey);
  5038. pkey = NULL;
  5039. BIO_free(rbio);
  5040. BIO_free(wbio);
  5041. rbio = NULL;
  5042. wbio = NULL;
  5043. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  5044. /* to prevent "unused variable" warning */
  5045. (void)pkey;
  5046. (void)wbio;
  5047. (void)rbio;
  5048. (void)line;
  5049. (void)line1;
  5050. (void)i;
  5051. #endif /* OPENSSL_EXTRA */
  5052. return EXPECT_RESULT();
  5053. }
  5054. /* Test functions for base64 encode/decode */
  5055. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  5056. {
  5057. EXPECT_DECLS;
  5058. #if defined(OPENSSL_EXTRA) && \
  5059. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5060. EVP_ENCODE_CTX* ctx = NULL;
  5061. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5062. ExpectIntEQ(ctx->remaining,0);
  5063. ExpectIntEQ(ctx->data[0],0);
  5064. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  5065. EVP_ENCODE_CTX_free(ctx);
  5066. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5067. return EXPECT_RESULT();
  5068. }
  5069. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  5070. {
  5071. EXPECT_DECLS;
  5072. #if defined(OPENSSL_EXTRA) && \
  5073. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5074. EVP_ENCODE_CTX* ctx = NULL;
  5075. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5076. EVP_ENCODE_CTX_free(ctx);
  5077. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5078. return EXPECT_RESULT();
  5079. }
  5080. static int test_wolfSSL_EVP_EncodeInit(void)
  5081. {
  5082. EXPECT_DECLS;
  5083. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5084. EVP_ENCODE_CTX* ctx = NULL;
  5085. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5086. ExpectIntEQ(ctx->remaining, 0);
  5087. ExpectIntEQ(ctx->data[0], 0);
  5088. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5089. if (ctx != NULL) {
  5090. /* make ctx dirty */
  5091. ctx->remaining = 10;
  5092. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  5093. }
  5094. EVP_EncodeInit(ctx);
  5095. ExpectIntEQ(ctx->remaining, 0);
  5096. ExpectIntEQ(ctx->data[0], 0);
  5097. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5098. EVP_ENCODE_CTX_free(ctx);
  5099. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5100. return EXPECT_RESULT();
  5101. }
  5102. static int test_wolfSSL_EVP_EncodeUpdate(void)
  5103. {
  5104. EXPECT_DECLS;
  5105. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5106. int outl;
  5107. int total;
  5108. const unsigned char plain0[] = {"Th"};
  5109. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  5110. const unsigned char plain2[] = {"This is additional data."};
  5111. const unsigned char encBlock0[] = {"VGg="};
  5112. const unsigned char enc0[] = {"VGg=\n"};
  5113. /* expected encoded result for the first output 64 chars plus trailing LF*/
  5114. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  5115. const unsigned char enc2[] =
  5116. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  5117. unsigned char encOutBuff[300];
  5118. EVP_ENCODE_CTX* ctx = NULL;
  5119. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5120. EVP_EncodeInit(ctx);
  5121. /* illegal parameter test */
  5122. ExpectIntEQ(
  5123. EVP_EncodeUpdate(
  5124. NULL, /* pass NULL as ctx */
  5125. encOutBuff,
  5126. &outl,
  5127. plain1,
  5128. sizeof(plain1)-1),
  5129. 0 /* expected result code 0: fail */
  5130. );
  5131. ExpectIntEQ(
  5132. EVP_EncodeUpdate(
  5133. ctx,
  5134. NULL, /* pass NULL as out buff */
  5135. &outl,
  5136. plain1,
  5137. sizeof(plain1)-1),
  5138. 0 /* expected result code 0: fail */
  5139. );
  5140. ExpectIntEQ(
  5141. EVP_EncodeUpdate(
  5142. ctx,
  5143. encOutBuff,
  5144. NULL, /* pass NULL as outl */
  5145. plain1,
  5146. sizeof(plain1)-1),
  5147. 0 /* expected result code 0: fail */
  5148. );
  5149. ExpectIntEQ(
  5150. EVP_EncodeUpdate(
  5151. ctx,
  5152. encOutBuff,
  5153. &outl,
  5154. NULL, /* pass NULL as in */
  5155. sizeof(plain1)-1),
  5156. 0 /* expected result code 0: fail */
  5157. );
  5158. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  5159. /* meaningless parameter test */
  5160. ExpectIntEQ(
  5161. EVP_EncodeUpdate(
  5162. ctx,
  5163. encOutBuff,
  5164. &outl,
  5165. plain1,
  5166. 0), /* pass zero input */
  5167. 1 /* expected result code 1: success */
  5168. );
  5169. /* very small data encoding test */
  5170. EVP_EncodeInit(ctx);
  5171. ExpectIntEQ(
  5172. EVP_EncodeUpdate(
  5173. ctx,
  5174. encOutBuff,
  5175. &outl,
  5176. plain0,
  5177. sizeof(plain0)-1),
  5178. 1 /* expected result code 1: success */
  5179. );
  5180. ExpectIntEQ(outl,0);
  5181. if (EXPECT_SUCCESS()) {
  5182. EVP_EncodeFinal(
  5183. ctx,
  5184. encOutBuff + outl,
  5185. &outl);
  5186. }
  5187. ExpectIntEQ( outl, sizeof(enc0)-1);
  5188. ExpectIntEQ(
  5189. XSTRNCMP(
  5190. (const char*)encOutBuff,
  5191. (const char*)enc0,sizeof(enc0) ),
  5192. 0);
  5193. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5194. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  5195. sizeof(encBlock0)-1);
  5196. ExpectStrEQ(encOutBuff, encBlock0);
  5197. /* pass small size( < 48bytes ) input, then make sure they are not
  5198. * encoded and just stored in ctx
  5199. */
  5200. EVP_EncodeInit(ctx);
  5201. total = 0;
  5202. outl = 0;
  5203. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5204. ExpectIntEQ(
  5205. EVP_EncodeUpdate(
  5206. ctx,
  5207. encOutBuff, /* buffer for output */
  5208. &outl, /* size of output */
  5209. plain1, /* input */
  5210. sizeof(plain1)-1), /* size of input */
  5211. 1); /* expected result code 1:success */
  5212. total += outl;
  5213. ExpectIntEQ(outl, 0); /* no output expected */
  5214. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  5215. ExpectTrue(
  5216. XSTRNCMP((const char*)(ctx->data),
  5217. (const char*)plain1,
  5218. ctx->remaining) ==0 );
  5219. ExpectTrue(encOutBuff[0] == 0);
  5220. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  5221. * the stored data and the new input together
  5222. */
  5223. ExpectIntEQ(
  5224. EVP_EncodeUpdate(
  5225. ctx,
  5226. encOutBuff + outl, /* buffer for output */
  5227. &outl, /* size of output */
  5228. plain2, /* additional input */
  5229. sizeof(plain2) -1), /* size of additional input */
  5230. 1); /* expected result code 1:success */
  5231. total += outl;
  5232. ExpectIntNE(outl, 0); /* some output is expected this time*/
  5233. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  5234. ExpectIntEQ(
  5235. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  5236. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  5237. EVP_EncodeFinal(
  5238. ctx,
  5239. encOutBuff + outl,
  5240. &outl);
  5241. total += outl;
  5242. ExpectIntNE(total,0);
  5243. ExpectIntNE(outl,0);
  5244. ExpectIntEQ(XSTRNCMP(
  5245. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  5246. /* test with illeagal parameters */
  5247. outl = 1;
  5248. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  5249. ExpectIntEQ(outl, 0);
  5250. outl = 1;
  5251. EVP_EncodeFinal(ctx, NULL, &outl);
  5252. ExpectIntEQ(outl, 0);
  5253. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  5254. EVP_EncodeFinal(NULL, NULL, NULL);
  5255. EVP_ENCODE_CTX_free(ctx);
  5256. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5257. return EXPECT_RESULT();
  5258. }
  5259. static int test_wolfSSL_EVP_EncodeFinal(void)
  5260. {
  5261. int res = TEST_SKIPPED;
  5262. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5263. /* tests for wolfSSL_EVP_EncodeFinal are included in
  5264. * test_wolfSSL_EVP_EncodeUpdate
  5265. */
  5266. res = TEST_SUCCESS;
  5267. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5268. return res;
  5269. }
  5270. static int test_wolfSSL_EVP_DecodeInit(void)
  5271. {
  5272. EXPECT_DECLS;
  5273. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5274. EVP_ENCODE_CTX* ctx = NULL;
  5275. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  5276. ExpectIntEQ( ctx->remaining,0);
  5277. ExpectIntEQ( ctx->data[0],0);
  5278. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5279. if (ctx != NULL) {
  5280. /* make ctx dirty */
  5281. ctx->remaining = 10;
  5282. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  5283. }
  5284. EVP_DecodeInit(ctx);
  5285. ExpectIntEQ( ctx->remaining,0);
  5286. ExpectIntEQ( ctx->data[0],0);
  5287. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5288. EVP_ENCODE_CTX_free(ctx);
  5289. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5290. return EXPECT_RESULT();
  5291. }
  5292. static int test_wolfSSL_EVP_DecodeUpdate(void)
  5293. {
  5294. EXPECT_DECLS;
  5295. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5296. int outl;
  5297. unsigned char decOutBuff[300];
  5298. EVP_ENCODE_CTX* ctx = NULL;
  5299. static const unsigned char enc1[] =
  5300. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5301. /* const unsigned char plain1[] =
  5302. {"This is a base64 decoding test."} */
  5303. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5304. EVP_DecodeInit(ctx);
  5305. /* illegal parameter tests */
  5306. /* pass NULL as ctx */
  5307. ExpectIntEQ(
  5308. EVP_DecodeUpdate(
  5309. NULL, /* pass NULL as ctx */
  5310. decOutBuff,
  5311. &outl,
  5312. enc1,
  5313. sizeof(enc1)-1),
  5314. -1 /* expected result code -1: fail */
  5315. );
  5316. ExpectIntEQ( outl, 0);
  5317. /* pass NULL as output */
  5318. ExpectIntEQ(
  5319. EVP_DecodeUpdate(
  5320. ctx,
  5321. NULL, /* pass NULL as out buff */
  5322. &outl,
  5323. enc1,
  5324. sizeof(enc1)-1),
  5325. -1 /* expected result code -1: fail */
  5326. );
  5327. ExpectIntEQ( outl, 0);
  5328. /* pass NULL as outl */
  5329. ExpectIntEQ(
  5330. EVP_DecodeUpdate(
  5331. ctx,
  5332. decOutBuff,
  5333. NULL, /* pass NULL as outl */
  5334. enc1,
  5335. sizeof(enc1)-1),
  5336. -1 /* expected result code -1: fail */
  5337. );
  5338. /* pass NULL as input */
  5339. ExpectIntEQ(
  5340. EVP_DecodeUpdate(
  5341. ctx,
  5342. decOutBuff,
  5343. &outl,
  5344. NULL, /* pass NULL as in */
  5345. sizeof(enc1)-1),
  5346. -1 /* expected result code -1: fail */
  5347. );
  5348. ExpectIntEQ( outl, 0);
  5349. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  5350. /* pass zero length input */
  5351. ExpectIntEQ(
  5352. EVP_DecodeUpdate(
  5353. ctx,
  5354. decOutBuff,
  5355. &outl,
  5356. enc1,
  5357. 0), /* pass zero as input len */
  5358. 1 /* expected result code 1: success */
  5359. );
  5360. /* decode correct base64 string */
  5361. {
  5362. static const unsigned char enc2[] =
  5363. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5364. static const unsigned char plain2[] =
  5365. {"This is a base64 decoding test."};
  5366. EVP_EncodeInit(ctx);
  5367. ExpectIntEQ(
  5368. EVP_DecodeUpdate(
  5369. ctx,
  5370. decOutBuff,
  5371. &outl,
  5372. enc2,
  5373. sizeof(enc2)-1),
  5374. 0 /* expected result code 0: success */
  5375. );
  5376. ExpectIntEQ(outl,sizeof(plain2) -1);
  5377. ExpectIntEQ(
  5378. EVP_DecodeFinal(
  5379. ctx,
  5380. decOutBuff + outl,
  5381. &outl),
  5382. 1 /* expected result code 1: success */
  5383. );
  5384. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  5385. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5386. sizeof(plain2) -1 ),0);
  5387. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  5388. sizeof(plain2)-1);
  5389. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5390. sizeof(plain2) -1 ),0);
  5391. }
  5392. /* decode correct base64 string which does not have '\n' in its last*/
  5393. {
  5394. static const unsigned char enc3[] =
  5395. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  5396. static const unsigned char plain3[] =
  5397. {"This is a base64 decoding test."}; /* 31 chars */
  5398. EVP_EncodeInit(ctx);
  5399. ExpectIntEQ(
  5400. EVP_DecodeUpdate(
  5401. ctx,
  5402. decOutBuff,
  5403. &outl,
  5404. enc3,
  5405. sizeof(enc3)-1),
  5406. 0 /* expected result code 0: success */
  5407. );
  5408. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  5409. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5410. sizeof(plain3) -1 ),0);
  5411. ExpectIntEQ(
  5412. EVP_DecodeFinal(
  5413. ctx,
  5414. decOutBuff + outl,
  5415. &outl),
  5416. 1 /* expected result code 1: success */
  5417. );
  5418. ExpectIntEQ(outl,0 );
  5419. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  5420. sizeof(plain3)-1);
  5421. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5422. sizeof(plain3) -1 ),0);
  5423. }
  5424. /* decode string which has a padding char ('=') in the illegal position*/
  5425. {
  5426. static const unsigned char enc4[] =
  5427. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  5428. EVP_EncodeInit(ctx);
  5429. ExpectIntEQ(
  5430. EVP_DecodeUpdate(
  5431. ctx,
  5432. decOutBuff,
  5433. &outl,
  5434. enc4,
  5435. sizeof(enc4)-1),
  5436. -1 /* expected result code -1: error */
  5437. );
  5438. ExpectIntEQ(outl,0);
  5439. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  5440. }
  5441. /* small data decode test */
  5442. {
  5443. static const unsigned char enc00[] = {"VG"};
  5444. static const unsigned char enc01[] = {"g=\n"};
  5445. static const unsigned char plain4[] = {"Th"};
  5446. EVP_EncodeInit(ctx);
  5447. ExpectIntEQ(
  5448. EVP_DecodeUpdate(
  5449. ctx,
  5450. decOutBuff,
  5451. &outl,
  5452. enc00,
  5453. sizeof(enc00)-1),
  5454. 1 /* expected result code 1: success */
  5455. );
  5456. ExpectIntEQ(outl,0);
  5457. ExpectIntEQ(
  5458. EVP_DecodeUpdate(
  5459. ctx,
  5460. decOutBuff + outl,
  5461. &outl,
  5462. enc01,
  5463. sizeof(enc01)-1),
  5464. 0 /* expected result code 0: success */
  5465. );
  5466. ExpectIntEQ(outl,sizeof(plain4)-1);
  5467. /* test with illegal parameters */
  5468. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  5469. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5470. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5471. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5472. if (EXPECT_SUCCESS()) {
  5473. EVP_DecodeFinal(
  5474. ctx,
  5475. decOutBuff + outl,
  5476. &outl);
  5477. }
  5478. ExpectIntEQ( outl, 0);
  5479. ExpectIntEQ(
  5480. XSTRNCMP(
  5481. (const char*)decOutBuff,
  5482. (const char*)plain4,sizeof(plain4)-1 ),
  5483. 0);
  5484. }
  5485. EVP_ENCODE_CTX_free(ctx);
  5486. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5487. return EXPECT_RESULT();
  5488. }
  5489. static int test_wolfSSL_EVP_DecodeFinal(void)
  5490. {
  5491. int res = TEST_SKIPPED;
  5492. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5493. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5494. * test_wolfSSL_EVP_DecodeUpdate
  5495. */
  5496. res = TEST_SUCCESS;
  5497. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5498. return res;
  5499. }
  5500. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5501. */
  5502. #ifdef OPENSSL_EXTRA
  5503. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5504. {
  5505. EXPECT_DECLS;
  5506. #ifndef NO_AES
  5507. const WOLFSSL_EVP_CIPHER* c;
  5508. c = wolfSSL_EVP_get_cipherbynid(419);
  5509. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5510. defined(WOLFSSL_AES_128)
  5511. ExpectNotNull(c);
  5512. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5513. #else
  5514. ExpectNull(c);
  5515. #endif
  5516. c = wolfSSL_EVP_get_cipherbynid(423);
  5517. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5518. defined(WOLFSSL_AES_192)
  5519. ExpectNotNull(c);
  5520. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5521. #else
  5522. ExpectNull(c);
  5523. #endif
  5524. c = wolfSSL_EVP_get_cipherbynid(427);
  5525. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5526. defined(WOLFSSL_AES_256)
  5527. ExpectNotNull(c);
  5528. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5529. #else
  5530. ExpectNull(c);
  5531. #endif
  5532. c = wolfSSL_EVP_get_cipherbynid(904);
  5533. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5534. ExpectNotNull(c);
  5535. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5536. #else
  5537. ExpectNull(c);
  5538. #endif
  5539. c = wolfSSL_EVP_get_cipherbynid(905);
  5540. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5541. ExpectNotNull(c);
  5542. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5543. #else
  5544. ExpectNull(c);
  5545. #endif
  5546. c = wolfSSL_EVP_get_cipherbynid(906);
  5547. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5548. ExpectNotNull(c);
  5549. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5550. #else
  5551. ExpectNull(c);
  5552. #endif
  5553. c = wolfSSL_EVP_get_cipherbynid(418);
  5554. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5555. ExpectNotNull(c);
  5556. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5557. #else
  5558. ExpectNull(c);
  5559. #endif
  5560. c = wolfSSL_EVP_get_cipherbynid(422);
  5561. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5562. ExpectNotNull(c);
  5563. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5564. #else
  5565. ExpectNull(c);
  5566. #endif
  5567. c = wolfSSL_EVP_get_cipherbynid(426);
  5568. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5569. ExpectNotNull(c);
  5570. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5571. #else
  5572. ExpectNull(c);
  5573. #endif
  5574. #endif /* !NO_AES */
  5575. #ifndef NO_DES3
  5576. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5577. #ifdef WOLFSSL_DES_ECB
  5578. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5579. #endif
  5580. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5581. #ifdef WOLFSSL_DES_ECB
  5582. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5583. #endif
  5584. #endif /* !NO_DES3 */
  5585. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5586. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5587. #endif
  5588. /* test for nid is out of range */
  5589. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5590. return EXPECT_RESULT();
  5591. }
  5592. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5593. {
  5594. EXPECT_DECLS;
  5595. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5596. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5597. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5598. const EVP_CIPHER *test;
  5599. byte key[AES_BLOCK_SIZE] = {0};
  5600. byte iv[AES_BLOCK_SIZE] = {0};
  5601. ExpectNotNull(ctx);
  5602. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5603. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5604. test = EVP_CIPHER_CTX_cipher(ctx);
  5605. ExpectTrue(init == test);
  5606. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5607. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5608. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5609. EVP_CIPHER_CTX_free(ctx);
  5610. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5611. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5612. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5613. return EXPECT_RESULT();
  5614. }
  5615. #endif /* OPENSSL_EXTRA */
  5616. /*----------------------------------------------------------------------------*
  5617. | IO
  5618. *----------------------------------------------------------------------------*/
  5619. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5620. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5621. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5622. #ifdef WC_SHA512_DIGEST_SIZE
  5623. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5624. #else
  5625. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5626. #endif
  5627. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5628. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5629. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5630. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5631. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5632. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5633. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5634. defined(HAVE_AES_CBC)
  5635. typedef struct openssl_key_ctx {
  5636. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5637. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5638. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5639. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5640. } openssl_key_ctx;
  5641. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5642. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5643. static WC_INLINE int OpenSSLTicketInit(void)
  5644. {
  5645. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5646. if (ret != 0) return ret;
  5647. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5648. sizeof(myOpenSSLKey_ctx.name));
  5649. if (ret != 0) return ret;
  5650. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5651. sizeof(myOpenSSLKey_ctx.key));
  5652. if (ret != 0) return ret;
  5653. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5654. sizeof(myOpenSSLKey_ctx.hmacKey));
  5655. if (ret != 0) return ret;
  5656. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5657. sizeof(myOpenSSLKey_ctx.iv));
  5658. if (ret != 0) return ret;
  5659. return 0;
  5660. }
  5661. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  5662. byte name[WOLFSSL_TICKET_NAME_SZ],
  5663. byte iv[WOLFSSL_TICKET_IV_SZ],
  5664. WOLFSSL_EVP_CIPHER_CTX *ectx,
  5665. WOLFSSL_HMAC_CTX *hctx, int enc) {
  5666. (void)ssl;
  5667. if (enc) {
  5668. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  5669. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  5670. }
  5671. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  5672. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  5673. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  5674. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  5675. return 0;
  5676. }
  5677. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  5678. if (enc)
  5679. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5680. else
  5681. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5682. return 1;
  5683. }
  5684. static WC_INLINE void OpenSSLTicketCleanup(void)
  5685. {
  5686. wc_FreeRng(&myOpenSSLKey_rng);
  5687. }
  5688. #endif
  5689. #endif
  5690. /* helper functions */
  5691. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  5692. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  5693. void *ctx)
  5694. {
  5695. struct test_ssl_memio_ctx *test_ctx;
  5696. byte *buf;
  5697. int *len;
  5698. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5699. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5700. buf = test_ctx->c_buff;
  5701. len = &test_ctx->c_len;
  5702. }
  5703. else {
  5704. buf = test_ctx->s_buff;
  5705. len = &test_ctx->s_len;
  5706. }
  5707. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  5708. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  5709. XMEMCPY(buf + *len, data, sz);
  5710. *len += sz;
  5711. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  5712. {
  5713. /* This can be imported into Wireshark by transforming the file with
  5714. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  5715. * And then loading test_output.dump.hex into Wireshark using the
  5716. * "Import from Hex Dump..." option ion and selecting the TCP
  5717. * encapsulation option. */
  5718. char dump_file_name[64];
  5719. WOLFSSL_BIO *dump_file;
  5720. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  5721. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  5722. if (dump_file != NULL) {
  5723. (void)wolfSSL_BIO_write(dump_file, data, sz);
  5724. wolfSSL_BIO_free(dump_file);
  5725. }
  5726. }
  5727. #endif
  5728. return sz;
  5729. }
  5730. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  5731. void *ctx)
  5732. {
  5733. struct test_ssl_memio_ctx *test_ctx;
  5734. int read_sz;
  5735. byte *buf;
  5736. int *len;
  5737. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5738. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5739. buf = test_ctx->s_buff;
  5740. len = &test_ctx->s_len;
  5741. }
  5742. else {
  5743. buf = test_ctx->c_buff;
  5744. len = &test_ctx->c_len;
  5745. }
  5746. if (*len == 0)
  5747. return WOLFSSL_CBIO_ERR_WANT_READ;
  5748. read_sz = sz < *len ? sz : *len;
  5749. XMEMCPY(data, buf, read_sz);
  5750. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  5751. *len -= read_sz;
  5752. return read_sz;
  5753. }
  5754. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  5755. {
  5756. EXPECT_DECLS;
  5757. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5758. int c_sharedCtx = 0;
  5759. int s_sharedCtx = 0;
  5760. #endif
  5761. const char* clientCertFile = cliCertFile;
  5762. const char* clientKeyFile = cliKeyFile;
  5763. const char* serverCertFile = svrCertFile;
  5764. const char* serverKeyFile = svrKeyFile;
  5765. /********************************
  5766. * Create WOLFSSL_CTX for client.
  5767. ********************************/
  5768. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5769. if (ctx->c_ctx != NULL) {
  5770. c_sharedCtx = ctx->c_cb.isSharedCtx;
  5771. }
  5772. else
  5773. #endif
  5774. {
  5775. WOLFSSL_METHOD* method = NULL;
  5776. if (ctx->c_cb.method != NULL) {
  5777. method = ctx->c_cb.method();
  5778. }
  5779. else {
  5780. method = wolfSSLv23_client_method();
  5781. }
  5782. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  5783. }
  5784. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  5785. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  5786. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5787. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  5788. #endif
  5789. if (ctx->c_cb.caPemFile != NULL)
  5790. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5791. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5792. else
  5793. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5794. caCertFile, 0), WOLFSSL_SUCCESS);
  5795. if (ctx->c_cb.certPemFile != NULL) {
  5796. clientCertFile = ctx->c_cb.certPemFile;
  5797. }
  5798. if (ctx->c_cb.keyPemFile != NULL) {
  5799. clientKeyFile = ctx->c_cb.keyPemFile;
  5800. }
  5801. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5802. if (!c_sharedCtx)
  5803. #endif
  5804. {
  5805. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  5806. clientCertFile), WOLFSSL_SUCCESS);
  5807. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  5808. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5809. }
  5810. #ifdef HAVE_CRL
  5811. if (ctx->c_cb.crlPemFile != NULL) {
  5812. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5813. WOLFSSL_SUCCESS);
  5814. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5815. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5816. }
  5817. #endif
  5818. if (ctx->c_ciphers != NULL) {
  5819. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5820. WOLFSSL_SUCCESS);
  5821. }
  5822. if (ctx->c_cb.ctx_ready != NULL) {
  5823. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5824. }
  5825. /********************************
  5826. * Create WOLFSSL_CTX for server.
  5827. ********************************/
  5828. if (ctx->s_ctx != NULL) {
  5829. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5830. s_sharedCtx = 1;
  5831. #endif
  5832. ctx->s_cb.isSharedCtx = 1;
  5833. }
  5834. else
  5835. {
  5836. WOLFSSL_METHOD* method = NULL;
  5837. if (ctx->s_cb.method != NULL) {
  5838. method = ctx->s_cb.method();
  5839. }
  5840. else {
  5841. method = wolfSSLv23_server_method();
  5842. }
  5843. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5844. ctx->s_cb.isSharedCtx = 0;
  5845. }
  5846. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5847. #if defined(HAVE_SESSION_TICKET) && \
  5848. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5849. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5850. OpenSSLTicketInit();
  5851. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5852. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5853. TicketInit();
  5854. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5855. #endif
  5856. #endif
  5857. }
  5858. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5859. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5860. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5861. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5862. if (ctx->s_cb.caPemFile != NULL)
  5863. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5864. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5865. else
  5866. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5867. cliCertFile, 0), WOLFSSL_SUCCESS);
  5868. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5869. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5870. #endif
  5871. if (ctx->s_cb.certPemFile != NULL) {
  5872. serverCertFile = ctx->s_cb.certPemFile;
  5873. }
  5874. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5875. if (!s_sharedCtx)
  5876. #endif
  5877. {
  5878. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5879. serverCertFile), WOLFSSL_SUCCESS);
  5880. }
  5881. if (ctx->s_cb.keyPemFile != NULL) {
  5882. serverKeyFile = ctx->s_cb.keyPemFile;
  5883. }
  5884. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5885. if (!s_sharedCtx)
  5886. #endif
  5887. {
  5888. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  5889. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5890. }
  5891. if (ctx->s_ciphers != NULL) {
  5892. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5893. WOLFSSL_SUCCESS);
  5894. }
  5895. if (ctx->s_cb.ctx_ready != NULL) {
  5896. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5897. }
  5898. /****************************
  5899. * Create WOLFSSL for client.
  5900. ****************************/
  5901. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5902. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5903. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5904. if (0
  5905. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5906. || c_sharedCtx
  5907. #endif
  5908. )
  5909. {
  5910. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  5911. clientCertFile), WOLFSSL_SUCCESS);
  5912. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  5913. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5914. }
  5915. if (ctx->c_cb.ssl_ready != NULL) {
  5916. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5917. }
  5918. /****************************
  5919. * Create WOLFSSL for server.
  5920. ****************************/
  5921. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5922. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5923. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5924. if (0
  5925. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5926. || s_sharedCtx
  5927. #endif
  5928. )
  5929. {
  5930. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  5931. serverCertFile), WOLFSSL_SUCCESS);
  5932. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  5933. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5934. }
  5935. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5936. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5937. #elif !defined(NO_DH)
  5938. /* will repick suites with DHE, higher priority than PSK */
  5939. SetDH(ctx->s_ssl);
  5940. #endif
  5941. if (ctx->s_cb.ssl_ready != NULL) {
  5942. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5943. }
  5944. return EXPECT_RESULT();
  5945. }
  5946. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5947. int* rounds)
  5948. {
  5949. int handshake_complete = 0;
  5950. int hs_c = 0;
  5951. int hs_s = 0;
  5952. int failing_s = 0;
  5953. int failing_c = 0;
  5954. int ret;
  5955. int err;
  5956. if (rounds != NULL) {
  5957. *rounds = 0;
  5958. }
  5959. while ((!handshake_complete) && (max_rounds > 0)) {
  5960. if (!hs_c) {
  5961. wolfSSL_SetLoggingPrefix("client");
  5962. ret = wolfSSL_connect(ctx->c_ssl);
  5963. wolfSSL_SetLoggingPrefix(NULL);
  5964. if (ret == WOLFSSL_SUCCESS) {
  5965. hs_c = 1;
  5966. }
  5967. else {
  5968. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5969. if (err != WOLFSSL_ERROR_WANT_READ &&
  5970. err != WOLFSSL_ERROR_WANT_WRITE) {
  5971. char buff[WOLFSSL_MAX_ERROR_SZ];
  5972. fprintf(stderr, "error = %d, %s\n", err,
  5973. wolfSSL_ERR_error_string(err, buff));
  5974. failing_c = 1;
  5975. hs_c = 1;
  5976. if (failing_c && failing_s) {
  5977. break;
  5978. }
  5979. }
  5980. }
  5981. }
  5982. if (!hs_s) {
  5983. wolfSSL_SetLoggingPrefix("server");
  5984. ret = wolfSSL_accept(ctx->s_ssl);
  5985. wolfSSL_SetLoggingPrefix(NULL);
  5986. if (ret == WOLFSSL_SUCCESS) {
  5987. hs_s = 1;
  5988. }
  5989. else {
  5990. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5991. if (err != WOLFSSL_ERROR_WANT_READ &&
  5992. err != WOLFSSL_ERROR_WANT_WRITE) {
  5993. char buff[WOLFSSL_MAX_ERROR_SZ];
  5994. fprintf(stderr, "error = %d, %s\n", err,
  5995. wolfSSL_ERR_error_string(err, buff));
  5996. failing_s = 1;
  5997. hs_s = 1;
  5998. if (failing_c && failing_s) {
  5999. break;
  6000. }
  6001. }
  6002. }
  6003. }
  6004. handshake_complete = hs_c && hs_s;
  6005. max_rounds--;
  6006. if (rounds != NULL) {
  6007. *rounds += 1;
  6008. }
  6009. }
  6010. if (!handshake_complete || failing_c || failing_s) {
  6011. return TEST_FAIL;
  6012. }
  6013. return TEST_SUCCESS;
  6014. }
  6015. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  6016. {
  6017. EXPECT_DECLS;
  6018. char input[1024];
  6019. int idx = 0;
  6020. const char* msg_c = "hello wolfssl!";
  6021. int msglen_c = (int)XSTRLEN(msg_c);
  6022. const char* msg_s = "I hear you fa shizzle!";
  6023. int msglen_s = (int)XSTRLEN(msg_s);
  6024. if (ctx->c_msg != NULL) {
  6025. msg_c = ctx->c_msg;
  6026. msglen_c = ctx->c_msglen;
  6027. }
  6028. if (ctx->s_msg != NULL) {
  6029. msg_s = ctx->s_msg;
  6030. msglen_s = ctx->s_msglen;
  6031. }
  6032. wolfSSL_SetLoggingPrefix("client");
  6033. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  6034. wolfSSL_SetLoggingPrefix("server");
  6035. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  6036. if (idx >= 0) {
  6037. input[idx] = '\0';
  6038. }
  6039. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  6040. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  6041. ctx->s_cb.return_code = EXPECT_RESULT();
  6042. wolfSSL_SetLoggingPrefix("client");
  6043. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  6044. wolfSSL_SetLoggingPrefix(NULL);
  6045. if (idx >= 0) {
  6046. input[idx] = '\0';
  6047. }
  6048. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  6049. ctx->c_cb.return_code = EXPECT_RESULT();
  6050. if (ctx->c_cb.on_result != NULL) {
  6051. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  6052. }
  6053. if (ctx->s_cb.on_result != NULL) {
  6054. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  6055. }
  6056. return EXPECT_RESULT();
  6057. }
  6058. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  6059. {
  6060. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  6061. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  6062. if (ctx->c_cb.on_cleanup != NULL) {
  6063. ctx->c_cb.on_cleanup(ctx->c_ssl);
  6064. }
  6065. if (ctx->s_cb.on_cleanup != NULL) {
  6066. ctx->s_cb.on_cleanup(ctx->s_ssl);
  6067. }
  6068. wolfSSL_shutdown(ctx->s_ssl);
  6069. wolfSSL_shutdown(ctx->c_ssl);
  6070. wolfSSL_free(ctx->s_ssl);
  6071. wolfSSL_free(ctx->c_ssl);
  6072. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  6073. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  6074. }
  6075. if (!ctx->c_cb.isSharedCtx) {
  6076. wolfSSL_CTX_free(ctx->c_ctx);
  6077. ctx->c_ctx = NULL;
  6078. }
  6079. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  6080. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  6081. }
  6082. if (!ctx->s_cb.isSharedCtx) {
  6083. wolfSSL_CTX_free(ctx->s_ctx);
  6084. ctx->s_ctx = NULL;
  6085. }
  6086. if (!ctx->s_cb.ticNoInit) {
  6087. #if defined(HAVE_SESSION_TICKET) && \
  6088. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6089. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6090. OpenSSLTicketCleanup();
  6091. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6092. TicketCleanup();
  6093. #endif
  6094. #endif
  6095. }
  6096. }
  6097. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  6098. test_ssl_cbf* server_cb, cbType client_on_handshake)
  6099. {
  6100. EXPECT_DECLS;
  6101. struct test_ssl_memio_ctx test_ctx;
  6102. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6103. size_t msg_len;
  6104. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6105. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  6106. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  6107. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  6108. test_ctx.c_ctx = client_cb->ctx;
  6109. test_ctx.s_ctx = server_cb->ctx;
  6110. test_ctx.c_cb.return_code = TEST_FAIL;
  6111. test_ctx.s_cb.return_code = TEST_FAIL;
  6112. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  6113. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  6114. if (client_on_handshake != NULL) {
  6115. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  6116. TEST_SUCCESS);
  6117. }
  6118. if (client_cb->on_handshake != NULL) {
  6119. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  6120. TEST_SUCCESS);
  6121. }
  6122. if (server_cb->on_handshake != NULL) {
  6123. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  6124. TEST_SUCCESS);
  6125. }
  6126. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6127. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6128. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  6129. MD_MAX_SIZE);
  6130. ExpectIntGE(msg_len, 0);
  6131. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6132. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  6133. MD_MAX_SIZE);
  6134. ExpectIntGE(msg_len, 0);
  6135. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6136. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  6137. test_ssl_memio_cleanup(&test_ctx);
  6138. client_cb->return_code = test_ctx.c_cb.return_code;
  6139. client_cb->last_err = test_ctx.c_cb.last_err;
  6140. server_cb->return_code = test_ctx.s_cb.return_code;
  6141. server_cb->last_err = test_ctx.s_cb.last_err;
  6142. return EXPECT_RESULT();
  6143. }
  6144. #endif
  6145. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  6146. #ifdef WOLFSSL_SESSION_EXPORT
  6147. #ifdef WOLFSSL_DTLS
  6148. /* set up function for sending session information */
  6149. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  6150. {
  6151. WOLFSSL_CTX* ctx = NULL;
  6152. WOLFSSL* ssl = NULL;
  6153. AssertNotNull(inSsl);
  6154. AssertNotNull(buf);
  6155. AssertIntNE(0, sz);
  6156. /* Set ctx to DTLS 1.2 */
  6157. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  6158. AssertNotNull(ctx);
  6159. ssl = wolfSSL_new(ctx);
  6160. AssertNotNull(ssl);
  6161. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  6162. wolfSSL_free(ssl);
  6163. wolfSSL_CTX_free(ctx);
  6164. (void)userCtx;
  6165. return 0;
  6166. }
  6167. #endif
  6168. /* returns negative value on fail and positive (including 0) on success */
  6169. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  6170. {
  6171. int ret, err, loop_count, count, timeout = 10;
  6172. char msg[] = "I hear you fa shizzle!";
  6173. char input[1024];
  6174. loop_count = ((func_args*)args)->argc;
  6175. #ifdef WOLFSSL_ASYNC_CRYPT
  6176. err = 0; /* Reset error */
  6177. #endif
  6178. do {
  6179. #ifdef WOLFSSL_ASYNC_CRYPT
  6180. if (err == WC_PENDING_E) {
  6181. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6182. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6183. }
  6184. #endif
  6185. ret = wolfSSL_accept(ssl);
  6186. err = wolfSSL_get_error(ssl, 0);
  6187. if (err == WOLFSSL_ERROR_WANT_READ ||
  6188. err == WOLFSSL_ERROR_WANT_WRITE) {
  6189. int select_ret;
  6190. err = WC_PENDING_E;
  6191. select_ret = tcp_select(*sockfd, timeout);
  6192. if (select_ret == TEST_TIMEOUT) {
  6193. return WOLFSSL_FATAL_ERROR;
  6194. }
  6195. }
  6196. } while (err == WC_PENDING_E);
  6197. if (ret != WOLFSSL_SUCCESS) {
  6198. char buff[WOLFSSL_MAX_ERROR_SZ];
  6199. fprintf(stderr, "error = %d, %s\n", err,
  6200. wolfSSL_ERR_error_string(err, buff));
  6201. return ret;
  6202. }
  6203. for (count = 0; count < loop_count; count++) {
  6204. int select_ret;
  6205. select_ret = tcp_select(*sockfd, timeout);
  6206. if (select_ret == TEST_TIMEOUT) {
  6207. ret = WOLFSSL_FATAL_ERROR;
  6208. break;
  6209. }
  6210. do {
  6211. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6212. if (ret > 0) {
  6213. input[ret] = '\0';
  6214. fprintf(stderr, "Client message: %s\n", input);
  6215. }
  6216. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6217. do {
  6218. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  6219. return WOLFSSL_FATAL_ERROR;
  6220. }
  6221. err = wolfSSL_get_error(ssl, ret);
  6222. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6223. }
  6224. return ret;
  6225. }
  6226. #endif /* WOLFSSL_SESSION_EXPORT */
  6227. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  6228. {
  6229. SOCKET_T sockfd = 0;
  6230. SOCKET_T clientfd = 0;
  6231. word16 port;
  6232. callback_functions* cbf;
  6233. WOLFSSL_CTX* ctx = NULL;
  6234. WOLFSSL* ssl = NULL;
  6235. func_args* opts = (func_args*)args;
  6236. char msg[] = "I hear you fa shizzle!";
  6237. char input[1024];
  6238. int idx;
  6239. int ret, err = 0;
  6240. int sharedCtx = 0;
  6241. int doUdp = 0;
  6242. SOCKADDR_IN_T cliAddr;
  6243. socklen_t cliLen;
  6244. const char* certFile = svrCertFile;
  6245. const char* keyFile = svrKeyFile;
  6246. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6247. size_t msg_len = 0;
  6248. #endif
  6249. wolfSSL_SetLoggingPrefix("server");
  6250. #ifdef WOLFSSL_TIRTOS
  6251. fdOpenSession(Task_self());
  6252. #endif
  6253. opts->return_code = TEST_FAIL;
  6254. cbf = opts->callbacks;
  6255. if (cbf != NULL && cbf->ctx) {
  6256. ctx = cbf->ctx;
  6257. sharedCtx = 1;
  6258. }
  6259. else
  6260. {
  6261. WOLFSSL_METHOD* method = NULL;
  6262. if (cbf != NULL && cbf->method != NULL) {
  6263. method = cbf->method();
  6264. }
  6265. else {
  6266. method = wolfSSLv23_server_method();
  6267. }
  6268. ctx = wolfSSL_CTX_new(method);
  6269. }
  6270. if (ctx == NULL) {
  6271. /* Release the wait for TCP ready. */
  6272. signal_ready(opts->signal);
  6273. goto done;
  6274. }
  6275. if (cbf == NULL || !cbf->ticNoInit) {
  6276. #if defined(HAVE_SESSION_TICKET) && \
  6277. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6278. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6279. OpenSSLTicketInit();
  6280. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  6281. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6282. TicketInit();
  6283. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  6284. #endif
  6285. #endif
  6286. }
  6287. #if defined(USE_WINDOWS_API)
  6288. port = opts->signal->port;
  6289. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6290. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6291. /* Let tcp_listen assign port */
  6292. port = 0;
  6293. #else
  6294. /* Use default port */
  6295. port = wolfSSLPort;
  6296. #endif
  6297. if (cbf != NULL)
  6298. doUdp = cbf->doUdp;
  6299. /* do it here to detect failure */
  6300. tcp_accept(
  6301. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  6302. if (doUdp) {
  6303. cliLen = sizeof(cliAddr);
  6304. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  6305. (struct sockaddr*)&cliAddr, &cliLen);
  6306. AssertIntGT(idx, 0);
  6307. }
  6308. else {
  6309. CloseSocket(sockfd);
  6310. }
  6311. wolfSSL_CTX_set_verify(ctx,
  6312. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6313. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6314. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6315. #endif
  6316. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6317. != WOLFSSL_SUCCESS) {
  6318. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6319. goto done;
  6320. }
  6321. if (cbf != NULL && cbf->certPemFile != NULL)
  6322. certFile = cbf->certPemFile;
  6323. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6324. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6325. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6326. #else
  6327. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6328. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6329. #endif
  6330. /*err_sys("can't load server cert chain file, "
  6331. "Please run from wolfSSL home dir");*/
  6332. goto done;
  6333. }
  6334. if (cbf != NULL && cbf->keyPemFile != NULL)
  6335. keyFile = cbf->keyPemFile;
  6336. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6337. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6338. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6339. #else
  6340. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6341. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6342. #endif
  6343. /*err_sys("can't load server key file, "
  6344. "Please run from wolfSSL home dir");*/
  6345. goto done;
  6346. }
  6347. #ifdef HAVE_CRL
  6348. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6349. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6350. goto done;
  6351. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6352. != WOLFSSL_SUCCESS)
  6353. goto done;
  6354. }
  6355. #endif
  6356. /* call ctx setup callback */
  6357. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6358. cbf->ctx_ready(ctx);
  6359. }
  6360. ssl = wolfSSL_new(ctx);
  6361. if (ssl == NULL) {
  6362. goto done;
  6363. }
  6364. if (doUdp) {
  6365. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6366. if (err != WOLFSSL_SUCCESS)
  6367. goto done;
  6368. }
  6369. #ifdef WOLFSSL_SESSION_EXPORT
  6370. /* only add in more complex nonblocking case with session export tests */
  6371. if (args && opts->argc > 0) {
  6372. /* set as nonblock and time out for waiting on read/write */
  6373. tcp_set_nonblocking(&clientfd);
  6374. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  6375. }
  6376. #endif
  6377. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6378. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  6379. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6380. #else
  6381. if (wolfSSL_use_certificate_file(ssl, certFile,
  6382. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6383. #endif
  6384. /*err_sys("can't load server cert chain file, "
  6385. "Please run from wolfSSL home dir");*/
  6386. goto done;
  6387. }
  6388. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6389. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6390. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6391. #else
  6392. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6393. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6394. #endif
  6395. /*err_sys("can't load server key file, "
  6396. "Please run from wolfSSL home dir");*/
  6397. goto done;
  6398. }
  6399. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6400. /*err_sys("SSL_set_fd failed");*/
  6401. goto done;
  6402. }
  6403. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6404. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6405. #elif !defined(NO_DH)
  6406. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6407. #endif
  6408. /* call ssl setup callback */
  6409. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6410. cbf->ssl_ready(ssl);
  6411. }
  6412. #ifdef WOLFSSL_SESSION_EXPORT
  6413. /* only add in more complex nonblocking case with session export tests */
  6414. if (opts->argc > 0) {
  6415. ret = nonblocking_accept_read(args, ssl, &clientfd);
  6416. if (ret >= 0) {
  6417. opts->return_code = TEST_SUCCESS;
  6418. }
  6419. #ifdef WOLFSSL_TIRTOS
  6420. Task_yield();
  6421. #endif
  6422. goto done;
  6423. }
  6424. #endif
  6425. #ifdef WOLFSSL_ASYNC_CRYPT
  6426. err = 0; /* Reset error */
  6427. #endif
  6428. do {
  6429. #ifdef WOLFSSL_ASYNC_CRYPT
  6430. if (err == WC_PENDING_E) {
  6431. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6432. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6433. }
  6434. #endif
  6435. ret = wolfSSL_negotiate(ssl);
  6436. err = wolfSSL_get_error(ssl, 0);
  6437. } while (err == WC_PENDING_E);
  6438. if (ret != WOLFSSL_SUCCESS) {
  6439. char buff[WOLFSSL_MAX_ERROR_SZ];
  6440. fprintf(stderr, "error = %d, %s\n", err,
  6441. wolfSSL_ERR_error_string(err, buff));
  6442. /*err_sys("SSL_accept failed");*/
  6443. goto done;
  6444. }
  6445. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6446. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6447. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  6448. AssertIntGE(msg_len, 0);
  6449. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6450. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  6451. AssertIntGE(msg_len, 0);
  6452. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6453. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6454. if (idx > 0) {
  6455. input[idx] = '\0';
  6456. fprintf(stderr, "Client message: %s\n", input);
  6457. }
  6458. else if (idx < 0) {
  6459. goto done;
  6460. }
  6461. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6462. /*err_sys("SSL_write failed");*/
  6463. goto done;
  6464. }
  6465. if (cbf != NULL && cbf->on_result != NULL)
  6466. cbf->on_result(ssl);
  6467. #ifdef WOLFSSL_TIRTOS
  6468. Task_yield();
  6469. #endif
  6470. opts->return_code = TEST_SUCCESS;
  6471. done:
  6472. if (cbf != NULL)
  6473. cbf->last_err = err;
  6474. if (cbf != NULL && cbf->on_cleanup != NULL)
  6475. cbf->on_cleanup(ssl);
  6476. wolfSSL_shutdown(ssl);
  6477. wolfSSL_free(ssl);
  6478. if (!sharedCtx)
  6479. wolfSSL_CTX_free(ctx);
  6480. CloseSocket(clientfd);
  6481. #ifdef WOLFSSL_TIRTOS
  6482. fdCloseSession(Task_self());
  6483. #endif
  6484. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6485. && defined(HAVE_THREAD_LS)
  6486. wc_ecc_fp_free(); /* free per thread cache */
  6487. #endif
  6488. if (cbf == NULL || !cbf->ticNoInit) {
  6489. #if defined(HAVE_SESSION_TICKET) && \
  6490. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6491. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6492. OpenSSLTicketCleanup();
  6493. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6494. TicketCleanup();
  6495. #endif
  6496. #endif
  6497. }
  6498. wolfSSL_SetLoggingPrefix(NULL);
  6499. WOLFSSL_RETURN_FROM_THREAD(0);
  6500. }
  6501. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6502. !defined(WOLFSSL_NO_TLS12)
  6503. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6504. {
  6505. SOCKET_T sockfd;
  6506. SOCKET_T clientfd = -1;
  6507. word16 port;
  6508. callback_functions* cbf;
  6509. WOLFSSL_CTX* ctx = 0;
  6510. WOLFSSL* ssl = 0;
  6511. char msg[] = "I hear you fa shizzle!";
  6512. char input[1024];
  6513. int idx;
  6514. int ret, err = 0;
  6515. int sharedCtx = 0;
  6516. func_args* opts = (func_args*)args;
  6517. int loop_count = opts->argc;
  6518. int count = 0;
  6519. #ifdef WOLFSSL_TIRTOS
  6520. fdOpenSession(Task_self());
  6521. #endif
  6522. opts->return_code = TEST_FAIL;
  6523. cbf = opts->callbacks;
  6524. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6525. if (cbf != NULL && cbf->ctx) {
  6526. ctx = cbf->ctx;
  6527. sharedCtx = 1;
  6528. }
  6529. else
  6530. #endif
  6531. {
  6532. WOLFSSL_METHOD* method = NULL;
  6533. if (cbf != NULL && cbf->method != NULL) {
  6534. method = cbf->method();
  6535. }
  6536. else {
  6537. method = wolfSSLv23_server_method();
  6538. }
  6539. ctx = wolfSSL_CTX_new(method);
  6540. }
  6541. #if defined(USE_WINDOWS_API)
  6542. port = opts->signal->port;
  6543. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6544. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6545. /* Let tcp_listen assign port */
  6546. port = 0;
  6547. #else
  6548. /* Use default port */
  6549. port = wolfSSLPort;
  6550. #endif
  6551. wolfSSL_CTX_set_verify(ctx,
  6552. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6553. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6554. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6555. #endif
  6556. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6557. != WOLFSSL_SUCCESS) {
  6558. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6559. /* Release the wait for TCP ready. */
  6560. signal_ready(opts->signal);
  6561. goto done;
  6562. }
  6563. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, 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_CTX_use_PrivateKey_file(ctx, 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. /* call ctx setup callback */
  6580. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6581. cbf->ctx_ready(ctx);
  6582. }
  6583. while (count != loop_count) {
  6584. ssl = wolfSSL_new(ctx);
  6585. if (ssl == NULL) {
  6586. signal_ready(opts->signal);
  6587. goto done;
  6588. }
  6589. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6590. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6591. /*err_sys("can't load server cert chain file, "
  6592. "Please run from wolfSSL home dir");*/
  6593. /* Release the wait for TCP ready. */
  6594. signal_ready(opts->signal);
  6595. goto done;
  6596. }
  6597. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6598. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6599. /*err_sys("can't load server key file, "
  6600. "Please run from wolfSSL home dir");*/
  6601. /* Release the wait for TCP ready. */
  6602. signal_ready(opts->signal);
  6603. goto done;
  6604. }
  6605. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6606. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6607. #elif !defined(NO_DH)
  6608. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6609. #endif
  6610. /* call ssl setup callback */
  6611. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6612. cbf->ssl_ready(ssl);
  6613. }
  6614. /* do it here to detect failure */
  6615. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6616. 0);
  6617. CloseSocket(sockfd);
  6618. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6619. /*err_sys("SSL_set_fd failed");*/
  6620. goto done;
  6621. }
  6622. #ifdef WOLFSSL_ASYNC_CRYPT
  6623. err = 0; /* Reset error */
  6624. #endif
  6625. do {
  6626. #ifdef WOLFSSL_ASYNC_CRYPT
  6627. if (err == WC_PENDING_E) {
  6628. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6629. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6630. }
  6631. #endif
  6632. ret = wolfSSL_accept(ssl);
  6633. err = wolfSSL_get_error(ssl, 0);
  6634. } while (err == WC_PENDING_E);
  6635. if (ret != WOLFSSL_SUCCESS) {
  6636. char buff[WOLFSSL_MAX_ERROR_SZ];
  6637. fprintf(stderr, "error = %d, %s\n", err,
  6638. wolfSSL_ERR_error_string(err, buff));
  6639. /*err_sys("SSL_accept failed");*/
  6640. goto done;
  6641. }
  6642. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6643. if (idx > 0) {
  6644. input[idx] = '\0';
  6645. fprintf(stderr, "Client message: %s\n", input);
  6646. }
  6647. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6648. /*err_sys("SSL_write failed");*/
  6649. goto done;
  6650. }
  6651. /* free ssl for this connection */
  6652. wolfSSL_shutdown(ssl);
  6653. wolfSSL_free(ssl); ssl = NULL;
  6654. CloseSocket(clientfd);
  6655. clientfd = -1;
  6656. count++;
  6657. }
  6658. #ifdef WOLFSSL_TIRTOS
  6659. Task_yield();
  6660. #endif
  6661. opts->return_code = TEST_SUCCESS;
  6662. done:
  6663. if (ssl != NULL) {
  6664. wolfSSL_shutdown(ssl);
  6665. wolfSSL_free(ssl);
  6666. }
  6667. if (!sharedCtx)
  6668. wolfSSL_CTX_free(ctx);
  6669. if (clientfd >= 0)
  6670. CloseSocket(clientfd);
  6671. #ifdef WOLFSSL_TIRTOS
  6672. fdCloseSession(Task_self());
  6673. #endif
  6674. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6675. && defined(HAVE_THREAD_LS)
  6676. wc_ecc_fp_free(); /* free per thread cache */
  6677. #endif
  6678. WOLFSSL_RETURN_FROM_THREAD(0);
  6679. }
  6680. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6681. static int test_client_nofail(void* args, cbType cb)
  6682. {
  6683. #if !defined(NO_WOLFSSL_CLIENT)
  6684. SOCKET_T sockfd = 0;
  6685. callback_functions* cbf;
  6686. WOLFSSL_CTX* ctx = 0;
  6687. WOLFSSL* ssl = 0;
  6688. WOLFSSL_CIPHER* cipher;
  6689. char msg[64] = "hello wolfssl!";
  6690. char reply[1024];
  6691. int input;
  6692. int msgSz = (int)XSTRLEN(msg);
  6693. int ret, err = 0;
  6694. int cipherSuite;
  6695. int sharedCtx = 0;
  6696. int doUdp = 0;
  6697. const char* cipherName1, *cipherName2;
  6698. wolfSSL_SetLoggingPrefix("client");
  6699. #ifdef WOLFSSL_TIRTOS
  6700. fdOpenSession(Task_self());
  6701. #endif
  6702. ((func_args*)args)->return_code = TEST_FAIL;
  6703. cbf = ((func_args*)args)->callbacks;
  6704. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6705. if (cbf != NULL && cbf->ctx) {
  6706. ctx = cbf->ctx;
  6707. sharedCtx = cbf->isSharedCtx;
  6708. }
  6709. else
  6710. #endif
  6711. {
  6712. WOLFSSL_METHOD* method = NULL;
  6713. if (cbf != NULL && cbf->method != NULL) {
  6714. method = cbf->method();
  6715. }
  6716. else {
  6717. method = wolfSSLv23_client_method();
  6718. }
  6719. ctx = wolfSSL_CTX_new(method);
  6720. }
  6721. if (cbf != NULL)
  6722. doUdp = cbf->doUdp;
  6723. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6724. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6725. #endif
  6726. /* Do connect here so server detects failures */
  6727. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6728. doUdp, 0, NULL);
  6729. /* Connect the socket so that we don't have to set the peer later on */
  6730. if (doUdp)
  6731. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  6732. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6733. {
  6734. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6735. goto done;
  6736. }
  6737. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6738. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6739. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6740. #else
  6741. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6742. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6743. #endif
  6744. /*err_sys("can't load client cert file, "
  6745. "Please run from wolfSSL home dir");*/
  6746. goto done;
  6747. }
  6748. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6749. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6750. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6751. #else
  6752. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6753. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6754. #endif
  6755. /*err_sys("can't load client key file, "
  6756. "Please run from wolfSSL home dir");*/
  6757. goto done;
  6758. }
  6759. #ifdef HAVE_CRL
  6760. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6761. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6762. goto done;
  6763. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6764. != WOLFSSL_SUCCESS)
  6765. goto done;
  6766. }
  6767. #endif
  6768. /* call ctx setup callback */
  6769. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6770. cbf->ctx_ready(ctx);
  6771. }
  6772. ssl = wolfSSL_new(ctx);
  6773. if (ssl == NULL) {
  6774. goto done;
  6775. }
  6776. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6777. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6778. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6779. #else
  6780. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  6781. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6782. #endif
  6783. /*err_sys("can't load client cert file, "
  6784. "Please run from wolfSSL home dir");*/
  6785. goto done;
  6786. }
  6787. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6788. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6789. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6790. #else
  6791. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6792. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6793. #endif
  6794. /*err_sys("can't load client key file, "
  6795. "Please run from wolfSSL home dir");*/
  6796. goto done;
  6797. }
  6798. if (!doUdp) {
  6799. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6800. /*err_sys("SSL_set_fd failed");*/
  6801. goto done;
  6802. }
  6803. }
  6804. else {
  6805. #ifdef WOLFSSL_DTLS
  6806. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6807. /*err_sys("SSL_set_fd failed");*/
  6808. goto done;
  6809. }
  6810. #else
  6811. goto done;
  6812. #endif
  6813. }
  6814. /* call ssl setup callback */
  6815. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6816. cbf->ssl_ready(ssl);
  6817. }
  6818. #ifdef WOLFSSL_ASYNC_CRYPT
  6819. err = 0; /* Reset error */
  6820. #endif
  6821. do {
  6822. #ifdef WOLFSSL_ASYNC_CRYPT
  6823. if (err == WC_PENDING_E) {
  6824. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6825. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6826. }
  6827. #endif
  6828. ret = wolfSSL_negotiate(ssl);
  6829. err = wolfSSL_get_error(ssl, 0);
  6830. } while (err == WC_PENDING_E);
  6831. if (ret != WOLFSSL_SUCCESS) {
  6832. char buff[WOLFSSL_MAX_ERROR_SZ];
  6833. fprintf(stderr, "error = %d, %s\n", err,
  6834. wolfSSL_ERR_error_string(err, buff));
  6835. /*err_sys("SSL_connect failed");*/
  6836. goto done;
  6837. }
  6838. /* test the various get cipher methods */
  6839. /* Internal cipher suite names */
  6840. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6841. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6842. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6843. (cipherSuite >> 8), cipherSuite & 0xFF);
  6844. AssertStrEQ(cipherName1, cipherName2);
  6845. /* IANA Cipher Suites Names */
  6846. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6847. then it's the internal cipher suite name */
  6848. cipher = wolfSSL_get_current_cipher(ssl);
  6849. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6850. cipherName2 = wolfSSL_get_cipher(ssl);
  6851. AssertStrEQ(cipherName1, cipherName2);
  6852. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6853. !defined(WOLFSSL_QT)
  6854. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6855. (cipherSuite >> 8), cipherSuite & 0xFF);
  6856. AssertStrEQ(cipherName1, cipherName2);
  6857. #endif
  6858. if (cb != NULL)
  6859. (cb)(ctx, ssl);
  6860. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6861. /*err_sys("SSL_write failed");*/
  6862. goto done;
  6863. }
  6864. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6865. if (input > 0) {
  6866. reply[input] = '\0';
  6867. fprintf(stderr, "Server response: %s\n", reply);
  6868. }
  6869. if (cbf != NULL && cbf->on_result != NULL)
  6870. cbf->on_result(ssl);
  6871. ((func_args*)args)->return_code = TEST_SUCCESS;
  6872. done:
  6873. if (cbf != NULL)
  6874. cbf->last_err = err;
  6875. if (cbf != NULL && cbf->on_cleanup != NULL)
  6876. cbf->on_cleanup(ssl);
  6877. wolfSSL_free(ssl);
  6878. if (!sharedCtx)
  6879. wolfSSL_CTX_free(ctx);
  6880. CloseSocket(sockfd);
  6881. #ifdef WOLFSSL_TIRTOS
  6882. fdCloseSession(Task_self());
  6883. #endif
  6884. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6885. && defined(HAVE_THREAD_LS)
  6886. wc_ecc_fp_free(); /* free per thread cache */
  6887. #endif
  6888. #else
  6889. (void)args;
  6890. (void)cb;
  6891. #endif /* !NO_WOLFSSL_CLIENT */
  6892. wolfSSL_SetLoggingPrefix(NULL);
  6893. return 0;
  6894. }
  6895. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6896. callback_functions* server_cb, cbType client_on_handshake)
  6897. {
  6898. func_args client_args;
  6899. func_args server_args;
  6900. tcp_ready ready;
  6901. THREAD_TYPE serverThread;
  6902. XMEMSET(&client_args, 0, sizeof(func_args));
  6903. XMEMSET(&server_args, 0, sizeof(func_args));
  6904. #ifdef WOLFSSL_TIRTOS
  6905. fdOpenSession(Task_self());
  6906. #endif
  6907. StartTCP();
  6908. InitTcpReady(&ready);
  6909. #if defined(USE_WINDOWS_API)
  6910. /* use RNG to get random port if using windows */
  6911. ready.port = GetRandomPort();
  6912. #endif
  6913. server_args.signal = &ready;
  6914. server_args.callbacks = server_cb;
  6915. client_args.signal = &ready;
  6916. client_args.callbacks = client_cb;
  6917. start_thread(test_server_nofail, &server_args, &serverThread);
  6918. wait_tcp_ready(&server_args);
  6919. test_client_nofail(&client_args, client_on_handshake);
  6920. join_thread(serverThread);
  6921. client_cb->return_code = client_args.return_code;
  6922. server_cb->return_code = server_args.return_code;
  6923. FreeTcpReady(&ready);
  6924. #ifdef WOLFSSL_TIRTOS
  6925. fdOpenSession(Task_self());
  6926. #endif
  6927. }
  6928. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6929. callback_functions* server_cb)
  6930. {
  6931. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6932. }
  6933. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6934. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6935. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6936. void* server_args)
  6937. {
  6938. SOCKET_T sockfd = 0;
  6939. callback_functions* cbf;
  6940. WOLFSSL_CTX* ctx = 0;
  6941. WOLFSSL* ssl = 0;
  6942. WOLFSSL_SESSION* session = NULL;
  6943. char msg[64] = "hello wolfssl!";
  6944. char reply[1024];
  6945. int input;
  6946. int msgSz = (int)XSTRLEN(msg);
  6947. int ret, err = 0;
  6948. int sharedCtx = 0;
  6949. #ifdef WOLFSSL_TIRTOS
  6950. fdOpenSession(Task_self());
  6951. #endif
  6952. ((func_args*)args)->return_code = TEST_FAIL;
  6953. cbf = ((func_args*)args)->callbacks;
  6954. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6955. if (cbf != NULL && cbf->ctx) {
  6956. ctx = cbf->ctx;
  6957. sharedCtx = 1;
  6958. }
  6959. else
  6960. #endif
  6961. {
  6962. WOLFSSL_METHOD* method = NULL;
  6963. if (cbf != NULL && cbf->method != NULL) {
  6964. method = cbf->method();
  6965. }
  6966. else {
  6967. method = wolfSSLv23_client_method();
  6968. }
  6969. ctx = wolfSSL_CTX_new(method);
  6970. }
  6971. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6972. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6973. #endif
  6974. /* Do connect here so server detects failures */
  6975. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6976. 0, 0, NULL);
  6977. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6978. WOLFSSL_SUCCESS) {
  6979. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6980. goto done;
  6981. }
  6982. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, 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_CTX_use_PrivateKey_file(ctx, 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. /* call ctx setup callback */
  6995. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6996. cbf->ctx_ready(ctx);
  6997. }
  6998. ssl = wolfSSL_new(ctx);
  6999. if (ssl == NULL) {
  7000. goto done;
  7001. }
  7002. /* keep handshake resources for re-using WOLFSSL obj */
  7003. wolfSSL_KeepArrays(ssl);
  7004. if (wolfSSL_KeepHandshakeResources(ssl)) {
  7005. /* err_sys("SSL_KeepHandshakeResources failed"); */
  7006. goto done;
  7007. }
  7008. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  7009. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7010. /*err_sys("can't load client cert file, "
  7011. "Please run from wolfSSL home dir");*/
  7012. goto done;
  7013. }
  7014. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  7015. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7016. /*err_sys("can't load client key file, "
  7017. "Please run from wolfSSL home dir");*/
  7018. goto done;
  7019. }
  7020. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7021. /*err_sys("SSL_set_fd failed");*/
  7022. goto done;
  7023. }
  7024. /* call ssl setup callback */
  7025. if (cbf != NULL && cbf->ssl_ready != NULL) {
  7026. cbf->ssl_ready(ssl);
  7027. }
  7028. #ifdef WOLFSSL_ASYNC_CRYPT
  7029. err = 0; /* Reset error */
  7030. #endif
  7031. do {
  7032. #ifdef WOLFSSL_ASYNC_CRYPT
  7033. if (err == WC_PENDING_E) {
  7034. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7035. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7036. }
  7037. #endif
  7038. ret = wolfSSL_connect(ssl);
  7039. err = wolfSSL_get_error(ssl, 0);
  7040. } while (err == WC_PENDING_E);
  7041. if (ret != WOLFSSL_SUCCESS) {
  7042. char buff[WOLFSSL_MAX_ERROR_SZ];
  7043. fprintf(stderr, "error = %d, %s\n", err,
  7044. wolfSSL_ERR_error_string(err, buff));
  7045. /*err_sys("SSL_connect failed");*/
  7046. goto done;
  7047. }
  7048. /* Build first session */
  7049. if (cb != NULL)
  7050. cb(ctx, ssl);
  7051. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7052. /*err_sys("SSL_write failed");*/
  7053. goto done;
  7054. }
  7055. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7056. if (input > 0) {
  7057. reply[input] = '\0';
  7058. fprintf(stderr, "Server response: %s\n", reply);
  7059. }
  7060. /* Session Resumption by re-using WOLFSSL object */
  7061. wolfSSL_set_quiet_shutdown(ssl, 1);
  7062. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  7063. /* err_sys ("SSL shutdown failed"); */
  7064. goto done;
  7065. }
  7066. session = wolfSSL_get1_session(ssl);
  7067. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  7068. wolfSSL_SESSION_free(session);
  7069. /* err_sys ("SSL_clear failed"); */
  7070. goto done;
  7071. }
  7072. wolfSSL_set_session(ssl, session);
  7073. wolfSSL_SESSION_free(session);
  7074. session = NULL;
  7075. /* close socket once */
  7076. CloseSocket(sockfd);
  7077. sockfd = 0;
  7078. /* wait until server ready */
  7079. wait_tcp_ready((func_args*)server_args);
  7080. fprintf(stderr, "session resumption\n");
  7081. /* Do re-connect */
  7082. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7083. 0, 0, NULL);
  7084. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7085. /*err_sys("SSL_set_fd failed");*/
  7086. goto done;
  7087. }
  7088. #ifdef WOLFSSL_ASYNC_CRYPT
  7089. err = 0; /* Reset error */
  7090. #endif
  7091. do {
  7092. #ifdef WOLFSSL_ASYNC_CRYPT
  7093. if (err == WC_PENDING_E) {
  7094. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7095. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7096. }
  7097. #endif
  7098. ret = wolfSSL_connect(ssl);
  7099. err = wolfSSL_get_error(ssl, 0);
  7100. } while (err == WC_PENDING_E);
  7101. if (ret != WOLFSSL_SUCCESS) {
  7102. char buff[WOLFSSL_MAX_ERROR_SZ];
  7103. fprintf(stderr, "error = %d, %s\n", err,
  7104. wolfSSL_ERR_error_string(err, buff));
  7105. /*err_sys("SSL_connect failed");*/
  7106. goto done;
  7107. }
  7108. /* Build first session */
  7109. if (cb != NULL)
  7110. cb(ctx, ssl);
  7111. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7112. /*err_sys("SSL_write failed");*/
  7113. goto done;
  7114. }
  7115. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7116. if (input > 0) {
  7117. reply[input] = '\0';
  7118. fprintf(stderr, "Server response: %s\n", reply);
  7119. }
  7120. ((func_args*)args)->return_code = TEST_SUCCESS;
  7121. done:
  7122. wolfSSL_free(ssl);
  7123. if (!sharedCtx)
  7124. wolfSSL_CTX_free(ctx);
  7125. CloseSocket(sockfd);
  7126. #ifdef WOLFSSL_TIRTOS
  7127. fdCloseSession(Task_self());
  7128. #endif
  7129. return;
  7130. }
  7131. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7132. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  7133. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  7134. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  7135. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  7136. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  7137. #define HAVE_ALPN_PROTOS_SUPPORT
  7138. #endif
  7139. /* Generic TLS client / server with callbacks for API unit tests
  7140. * Used by SNI / ALPN / crypto callback helper functions */
  7141. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  7142. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  7143. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  7144. #define ENABLE_TLS_CALLBACK_TEST
  7145. #endif
  7146. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  7147. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  7148. /* TLS server for API unit testing - generic */
  7149. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  7150. {
  7151. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7152. WOLFSSL_CTX* ctx = NULL;
  7153. WOLFSSL* ssl = NULL;
  7154. SOCKET_T sfd = 0;
  7155. SOCKET_T cfd = 0;
  7156. word16 port;
  7157. char msg[] = "I hear you fa shizzle!";
  7158. int len = (int) XSTRLEN(msg);
  7159. char input[1024];
  7160. int idx;
  7161. int ret, err = 0;
  7162. ((func_args*)args)->return_code = TEST_FAIL;
  7163. #if defined(USE_WINDOWS_API)
  7164. port = ((func_args*)args)->signal->port;
  7165. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7166. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7167. /* Let tcp_listen assign port */
  7168. port = 0;
  7169. #else
  7170. /* Use default port */
  7171. port = wolfSSLPort;
  7172. #endif
  7173. #ifdef WOLFSSL_DTLS
  7174. if (callbacks->method == wolfDTLS_server_method
  7175. #ifdef WOLFSSL_STATIC_MEMORY
  7176. || callbacks->method_ex == wolfDTLS_server_method_ex
  7177. #endif
  7178. #ifndef NO_OLD_TLS
  7179. || callbacks->method == wolfDTLSv1_server_method
  7180. #ifdef WOLFSSL_STATIC_MEMORY
  7181. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  7182. #endif
  7183. #endif
  7184. #ifndef WOLFSSL_NO_TLS12
  7185. || callbacks->method == wolfDTLSv1_2_server_method
  7186. #ifdef WOLFSSL_STATIC_MEMORY
  7187. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  7188. #endif
  7189. #endif
  7190. #ifdef WOLFSSL_DTLS13
  7191. || callbacks->method == wolfDTLSv1_3_server_method
  7192. #ifdef WOLFSSL_STATIC_MEMORY
  7193. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  7194. #endif
  7195. #endif
  7196. ) {
  7197. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  7198. }
  7199. else
  7200. #endif
  7201. {
  7202. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7203. }
  7204. #ifdef WOLFSSL_STATIC_MEMORY
  7205. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7206. callbacks->memSz > 0) {
  7207. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7208. callbacks->mem, callbacks->memSz, 0, 1);
  7209. if (ret != WOLFSSL_SUCCESS) {
  7210. fprintf(stderr, "CTX static new failed %d\n", ret);
  7211. goto cleanup;
  7212. }
  7213. }
  7214. #else
  7215. ctx = wolfSSL_CTX_new(callbacks->method());
  7216. #endif
  7217. if (ctx == NULL) {
  7218. fprintf(stderr, "CTX new failed\n");
  7219. goto cleanup;
  7220. }
  7221. /* set defaults */
  7222. if (callbacks->caPemFile == NULL)
  7223. callbacks->caPemFile = cliCertFile;
  7224. if (callbacks->certPemFile == NULL)
  7225. callbacks->certPemFile = svrCertFile;
  7226. if (callbacks->keyPemFile == NULL)
  7227. callbacks->keyPemFile = svrKeyFile;
  7228. #ifdef WOLFSSL_TIRTOS
  7229. fdOpenSession(Task_self());
  7230. #endif
  7231. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7232. wolfSSL_CTX_set_verify(ctx,
  7233. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  7234. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7235. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7236. #endif
  7237. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  7238. if (callbacks->method == wolfDTLSv1_2_server_method) {
  7239. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  7240. goto cleanup;
  7241. }
  7242. #endif
  7243. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7244. WOLFSSL_SUCCESS) {
  7245. goto cleanup;
  7246. }
  7247. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7248. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7249. goto cleanup;
  7250. }
  7251. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7252. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7253. goto cleanup;
  7254. }
  7255. #ifdef HAVE_CRL
  7256. if (callbacks->crlPemFile != NULL) {
  7257. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7258. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7259. goto cleanup;
  7260. }
  7261. }
  7262. #endif
  7263. if (callbacks->ctx_ready)
  7264. callbacks->ctx_ready(ctx);
  7265. ssl = wolfSSL_new(ctx);
  7266. if (ssl == NULL) {
  7267. fprintf(stderr, "SSL new failed\n");
  7268. goto cleanup;
  7269. }
  7270. if (wolfSSL_dtls(ssl)) {
  7271. SOCKADDR_IN_T cliAddr;
  7272. socklen_t cliLen;
  7273. cliLen = sizeof(cliAddr);
  7274. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  7275. (struct sockaddr*)&cliAddr, &cliLen);
  7276. if (idx <= 0) {
  7277. goto cleanup;
  7278. }
  7279. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  7280. }
  7281. else {
  7282. CloseSocket(sfd);
  7283. }
  7284. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  7285. goto cleanup;
  7286. }
  7287. if (callbacks->loadToSSL) {
  7288. wolfSSL_SetDevId(ssl, callbacks->devId);
  7289. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7290. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7291. goto cleanup;
  7292. }
  7293. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7294. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7295. goto cleanup;
  7296. }
  7297. }
  7298. #ifdef NO_PSK
  7299. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  7300. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  7301. #elif !defined(NO_DH)
  7302. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  7303. #endif
  7304. #endif
  7305. if (callbacks->ssl_ready)
  7306. callbacks->ssl_ready(ssl);
  7307. #ifdef WOLFSSL_ASYNC_CRYPT
  7308. err = 0; /* Reset error */
  7309. #endif
  7310. do {
  7311. #ifdef WOLFSSL_ASYNC_CRYPT
  7312. if (err == WC_PENDING_E) {
  7313. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7314. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7315. }
  7316. #endif
  7317. ret = wolfSSL_accept(ssl);
  7318. err = wolfSSL_get_error(ssl, ret);
  7319. } while (err == WC_PENDING_E);
  7320. if (ret != WOLFSSL_SUCCESS) {
  7321. char buff[WOLFSSL_MAX_ERROR_SZ];
  7322. fprintf(stderr, "accept error = %d, %s\n", err,
  7323. wolfSSL_ERR_error_string(err, buff));
  7324. /*err_sys("SSL_accept failed");*/
  7325. }
  7326. else {
  7327. #ifdef WOLFSSL_ASYNC_CRYPT
  7328. err = 0; /* Reset error */
  7329. #endif
  7330. do {
  7331. #ifdef WOLFSSL_ASYNC_CRYPT
  7332. if (err == WC_PENDING_E) {
  7333. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7334. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7335. }
  7336. #endif
  7337. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7338. err = wolfSSL_get_error(ssl, idx);
  7339. } while (err == WC_PENDING_E);
  7340. if (idx > 0) {
  7341. input[idx] = 0;
  7342. fprintf(stderr, "Client message: %s\n", input);
  7343. }
  7344. #ifdef WOLFSSL_ASYNC_CRYPT
  7345. err = 0; /* Reset error */
  7346. #endif
  7347. do {
  7348. #ifdef WOLFSSL_ASYNC_CRYPT
  7349. if (err == WC_PENDING_E) {
  7350. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7351. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7352. }
  7353. #endif
  7354. ret = wolfSSL_write(ssl, msg, len);
  7355. err = wolfSSL_get_error(ssl, ret);
  7356. } while (err == WC_PENDING_E);
  7357. if (len != ret) {
  7358. goto cleanup;
  7359. }
  7360. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  7361. defined(WOLFSSL_DTLS)
  7362. if (wolfSSL_dtls(ssl)) {
  7363. byte* import;
  7364. word32 sz;
  7365. wolfSSL_dtls_export(ssl, NULL, &sz);
  7366. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7367. if (import == NULL) {
  7368. goto cleanup;
  7369. }
  7370. idx = wolfSSL_dtls_export(ssl, import, &sz);
  7371. if (idx < 0) {
  7372. goto cleanup;
  7373. }
  7374. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  7375. goto cleanup;
  7376. }
  7377. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7378. }
  7379. #endif
  7380. #ifdef WOLFSSL_TIRTOS
  7381. Task_yield();
  7382. #endif
  7383. ((func_args*)args)->return_code = TEST_SUCCESS;
  7384. }
  7385. if (callbacks->on_result)
  7386. callbacks->on_result(ssl);
  7387. wolfSSL_shutdown(ssl);
  7388. cleanup:
  7389. wolfSSL_free(ssl);
  7390. wolfSSL_CTX_free(ctx);
  7391. CloseSocket(cfd);
  7392. #ifdef WOLFSSL_TIRTOS
  7393. fdCloseSession(Task_self());
  7394. #endif
  7395. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7396. && defined(HAVE_THREAD_LS)
  7397. wc_ecc_fp_free(); /* free per thread cache */
  7398. #endif
  7399. WOLFSSL_RETURN_FROM_THREAD(0);
  7400. }
  7401. /* TLS Client for API unit testing - generic */
  7402. static void run_wolfssl_client(void* args)
  7403. {
  7404. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7405. WOLFSSL_CTX* ctx = NULL;
  7406. WOLFSSL* ssl = NULL;
  7407. SOCKET_T sfd = 0;
  7408. char msg[] = "hello wolfssl server!";
  7409. int len = (int) XSTRLEN(msg);
  7410. char input[1024];
  7411. int ret, err = 0;
  7412. ((func_args*)args)->return_code = TEST_FAIL;
  7413. /* set defaults */
  7414. if (callbacks->caPemFile == NULL)
  7415. callbacks->caPemFile = caCertFile;
  7416. if (callbacks->certPemFile == NULL)
  7417. callbacks->certPemFile = cliCertFile;
  7418. if (callbacks->keyPemFile == NULL)
  7419. callbacks->keyPemFile = cliKeyFile;
  7420. #ifdef WOLFSSL_STATIC_MEMORY
  7421. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7422. callbacks->memSz > 0) {
  7423. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7424. callbacks->mem, callbacks->memSz, 0, 1);
  7425. if (ret != WOLFSSL_SUCCESS) {
  7426. fprintf(stderr, "CTX static new failed %d\n", ret);
  7427. goto cleanup;
  7428. }
  7429. }
  7430. #else
  7431. ctx = wolfSSL_CTX_new(callbacks->method());
  7432. #endif
  7433. if (ctx == NULL) {
  7434. fprintf(stderr, "CTX new failed\n");
  7435. goto cleanup;
  7436. }
  7437. #ifdef WOLFSSL_TIRTOS
  7438. fdOpenSession(Task_self());
  7439. #endif
  7440. if (!callbacks->loadToSSL) {
  7441. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7442. }
  7443. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7444. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7445. #endif
  7446. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7447. WOLFSSL_SUCCESS) {
  7448. goto cleanup;
  7449. }
  7450. if (!callbacks->loadToSSL) {
  7451. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7452. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7453. goto cleanup;
  7454. }
  7455. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7456. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7457. goto cleanup;
  7458. }
  7459. }
  7460. #ifdef HAVE_CRL
  7461. if (callbacks->crlPemFile != NULL) {
  7462. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7463. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7464. goto cleanup;
  7465. }
  7466. }
  7467. #endif
  7468. if (callbacks->ctx_ready)
  7469. callbacks->ctx_ready(ctx);
  7470. ssl = wolfSSL_new(ctx);
  7471. if (wolfSSL_dtls(ssl)) {
  7472. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7473. 1, 0, ssl);
  7474. }
  7475. else {
  7476. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7477. 0, 0, ssl);
  7478. }
  7479. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7480. goto cleanup;
  7481. }
  7482. if (callbacks->loadToSSL) {
  7483. wolfSSL_SetDevId(ssl, callbacks->devId);
  7484. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7485. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7486. goto cleanup;
  7487. }
  7488. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7489. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7490. goto cleanup;
  7491. }
  7492. }
  7493. if (callbacks->ssl_ready)
  7494. callbacks->ssl_ready(ssl);
  7495. #ifdef WOLFSSL_ASYNC_CRYPT
  7496. err = 0; /* Reset error */
  7497. #endif
  7498. do {
  7499. #ifdef WOLFSSL_ASYNC_CRYPT
  7500. if (err == WC_PENDING_E) {
  7501. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7502. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7503. }
  7504. #endif
  7505. ret = wolfSSL_connect(ssl);
  7506. err = wolfSSL_get_error(ssl, ret);
  7507. } while (err == WC_PENDING_E);
  7508. if (ret != WOLFSSL_SUCCESS) {
  7509. char buff[WOLFSSL_MAX_ERROR_SZ];
  7510. fprintf(stderr, "error = %d, %s\n", err,
  7511. wolfSSL_ERR_error_string(err, buff));
  7512. /*err_sys("SSL_connect failed");*/
  7513. }
  7514. else {
  7515. #ifdef WOLFSSL_ASYNC_CRYPT
  7516. err = 0; /* Reset error */
  7517. #endif
  7518. do {
  7519. #ifdef WOLFSSL_ASYNC_CRYPT
  7520. if (err == WC_PENDING_E) {
  7521. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7522. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7523. }
  7524. #endif
  7525. ret = wolfSSL_write(ssl, msg, len);
  7526. err = wolfSSL_get_error(ssl, ret);
  7527. } while (err == WC_PENDING_E);
  7528. if (len != ret)
  7529. goto cleanup;
  7530. #ifdef WOLFSSL_ASYNC_CRYPT
  7531. err = 0; /* Reset error */
  7532. #endif
  7533. do {
  7534. #ifdef WOLFSSL_ASYNC_CRYPT
  7535. if (err == WC_PENDING_E) {
  7536. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7537. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7538. }
  7539. #endif
  7540. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7541. err = wolfSSL_get_error(ssl, ret);
  7542. } while (err == WC_PENDING_E);
  7543. if (ret > 0) {
  7544. input[ret] = '\0'; /* null term */
  7545. fprintf(stderr, "Server response: %s\n", input);
  7546. }
  7547. ((func_args*)args)->return_code = TEST_SUCCESS;
  7548. }
  7549. if (callbacks->on_result)
  7550. callbacks->on_result(ssl);
  7551. cleanup:
  7552. wolfSSL_free(ssl);
  7553. wolfSSL_CTX_free(ctx);
  7554. CloseSocket(sfd);
  7555. #ifdef WOLFSSL_TIRTOS
  7556. fdCloseSession(Task_self());
  7557. #endif
  7558. }
  7559. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7560. static int test_wolfSSL_read_write(void)
  7561. {
  7562. /* The unit testing for read and write shall happen simultaneously, since
  7563. * one can't do anything with one without the other. (Except for a failure
  7564. * test case.) This function will call all the others that will set up,
  7565. * execute, and report their test findings.
  7566. *
  7567. * Set up the success case first. This function will become the template
  7568. * for the other tests. This should eventually be renamed
  7569. *
  7570. * The success case isn't interesting, how can this fail?
  7571. * - Do not give the client context a CA certificate. The connect should
  7572. * fail. Do not need server for this?
  7573. * - Using NULL for the ssl object on server. Do not need client for this.
  7574. * - Using NULL for the ssl object on client. Do not need server for this.
  7575. * - Good ssl objects for client and server. Client write() without server
  7576. * read().
  7577. * - Good ssl objects for client and server. Server write() without client
  7578. * read().
  7579. * - Forgetting the password callback?
  7580. */
  7581. tcp_ready ready;
  7582. func_args client_args;
  7583. func_args server_args;
  7584. THREAD_TYPE serverThread;
  7585. EXPECT_DECLS;
  7586. XMEMSET(&client_args, 0, sizeof(func_args));
  7587. XMEMSET(&server_args, 0, sizeof(func_args));
  7588. #ifdef WOLFSSL_TIRTOS
  7589. fdOpenSession(Task_self());
  7590. #endif
  7591. StartTCP();
  7592. InitTcpReady(&ready);
  7593. #if defined(USE_WINDOWS_API)
  7594. /* use RNG to get random port if using windows */
  7595. ready.port = GetRandomPort();
  7596. #endif
  7597. server_args.signal = &ready;
  7598. client_args.signal = &ready;
  7599. start_thread(test_server_nofail, &server_args, &serverThread);
  7600. wait_tcp_ready(&server_args);
  7601. test_client_nofail(&client_args, NULL);
  7602. join_thread(serverThread);
  7603. ExpectTrue(client_args.return_code);
  7604. ExpectTrue(server_args.return_code);
  7605. FreeTcpReady(&ready);
  7606. #ifdef WOLFSSL_TIRTOS
  7607. fdOpenSession(Task_self());
  7608. #endif
  7609. return EXPECT_RESULT();
  7610. }
  7611. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7612. {
  7613. EXPECT_DECLS;
  7614. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7615. !defined(WOLFSSL_NO_TLS12)
  7616. /* The unit test for session resumption by re-using WOLFSSL object.
  7617. * WOLFSSL object is not cleared after first session. It reuse the object
  7618. * for second connection.
  7619. */
  7620. tcp_ready ready;
  7621. func_args client_args;
  7622. func_args server_args;
  7623. THREAD_TYPE serverThread;
  7624. callback_functions client_cbf;
  7625. callback_functions server_cbf;
  7626. XMEMSET(&client_args, 0, sizeof(func_args));
  7627. XMEMSET(&server_args, 0, sizeof(func_args));
  7628. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7629. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7630. #ifdef WOLFSSL_TIRTOS
  7631. fdOpenSession(Task_self());
  7632. #endif
  7633. StartTCP();
  7634. InitTcpReady(&ready);
  7635. #if defined(USE_WINDOWS_API)
  7636. /* use RNG to get random port if using windows */
  7637. ready.port = GetRandomPort();
  7638. #endif
  7639. client_cbf.method = wolfTLSv1_2_client_method;
  7640. server_cbf.method = wolfTLSv1_2_server_method;
  7641. client_args.callbacks = &client_cbf;
  7642. server_args.callbacks = &server_cbf;
  7643. server_args.signal = &ready;
  7644. client_args.signal = &ready;
  7645. /* the var is used for loop number */
  7646. server_args.argc = 2;
  7647. start_thread(test_server_loop, &server_args, &serverThread);
  7648. wait_tcp_ready(&server_args);
  7649. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7650. join_thread(serverThread);
  7651. ExpectTrue(client_args.return_code);
  7652. ExpectTrue(server_args.return_code);
  7653. FreeTcpReady(&ready);
  7654. #ifdef WOLFSSL_TIRTOS
  7655. fdOpenSession(Task_self());
  7656. #endif
  7657. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7658. * !defined(WOLFSSL_TLS13) */
  7659. return EXPECT_RESULT();
  7660. }
  7661. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7662. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7663. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  7664. WOLFSSL_CTX* ctx)
  7665. {
  7666. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7667. myVerifyAction = VERIFY_USE_PREVERFIY;
  7668. wolfSSL_CTX_set_verify_depth(ctx, 2);
  7669. return TEST_SUCCESS;
  7670. }
  7671. #endif
  7672. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  7673. {
  7674. EXPECT_DECLS;
  7675. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7676. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7677. test_ssl_cbf client_cbf;
  7678. test_ssl_cbf server_cbf;
  7679. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7680. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7681. #ifdef WOLFSSL_TLS13
  7682. client_cbf.method = wolfTLSv1_3_client_method;
  7683. #endif /* WOLFSSL_TLS13 */
  7684. client_cbf.ctx_ready =
  7685. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  7686. /* test case 1 verify depth is equal to peer chain */
  7687. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7688. &server_cbf, NULL), TEST_SUCCESS);
  7689. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7690. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7691. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7692. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7693. return EXPECT_RESULT();
  7694. }
  7695. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7696. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7697. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  7698. WOLFSSL_CTX* ctx)
  7699. {
  7700. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7701. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  7702. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7703. return TEST_SUCCESS;
  7704. }
  7705. #endif
  7706. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  7707. {
  7708. EXPECT_DECLS;
  7709. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7710. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7711. test_ssl_cbf client_cbf;
  7712. test_ssl_cbf server_cbf;
  7713. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7714. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7715. #ifdef WOLFSSL_TLS13
  7716. client_cbf.method = wolfTLSv1_3_client_method;
  7717. #endif /* WOLFSSL_TLS13 */
  7718. client_cbf.ctx_ready =
  7719. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  7720. /* test case 2
  7721. * verify depth is zero, number of peer's chain is 2.
  7722. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  7723. * callback.
  7724. */
  7725. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7726. &server_cbf, NULL), TEST_SUCCESS);
  7727. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7728. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7729. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7730. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7731. return EXPECT_RESULT();
  7732. }
  7733. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7734. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7735. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  7736. WOLFSSL_CTX* ctx)
  7737. {
  7738. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7739. myVerifyAction = VERIFY_USE_PREVERFIY;
  7740. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7741. return TEST_SUCCESS;
  7742. }
  7743. #endif
  7744. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  7745. {
  7746. EXPECT_DECLS;
  7747. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7748. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7749. test_ssl_cbf client_cbf;
  7750. test_ssl_cbf server_cbf;
  7751. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7752. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7753. #ifdef WOLFSSL_TLS13
  7754. client_cbf.method = wolfTLSv1_3_client_method;
  7755. #endif /* WOLFSSL_TLS13 */
  7756. client_cbf.ctx_ready =
  7757. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  7758. /* test case 3
  7759. * verify depth is zero, number of peer's chain is 2
  7760. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  7761. * therefore, handshake becomes failure.
  7762. */
  7763. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7764. &server_cbf, NULL), TEST_FAIL);
  7765. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  7766. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  7767. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  7768. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  7769. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7770. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7771. return EXPECT_RESULT();
  7772. }
  7773. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7774. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7775. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7776. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  7777. {
  7778. EXPECT_DECLS;
  7779. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  7780. return EXPECT_RESULT();
  7781. }
  7782. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  7783. {
  7784. EXPECT_DECLS;
  7785. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  7786. return EXPECT_RESULT();
  7787. }
  7788. #endif
  7789. static int test_wolfSSL_CTX_set_cipher_list(void)
  7790. {
  7791. EXPECT_DECLS;
  7792. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7793. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7794. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7795. WOLFSSL_CTX* ctxClient = NULL;
  7796. WOLFSSL* sslClient = NULL;
  7797. test_ssl_cbf client_cbf;
  7798. test_ssl_cbf server_cbf;
  7799. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7800. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7801. server_cbf.method = wolfTLSv1_2_server_method;
  7802. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  7803. client_cbf.method = wolfTLSv1_2_client_method;
  7804. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  7805. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7806. &server_cbf, NULL), TEST_SUCCESS);
  7807. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7808. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7809. /* check with cipher string that has '+' */
  7810. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  7811. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  7812. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  7813. /* check for the existence of an ECDHE ECDSA cipher suite */
  7814. if (EXPECT_SUCCESS()) {
  7815. int i = 0;
  7816. int found = 0;
  7817. const char* suite;
  7818. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  7819. WOLFSSL_CIPHER* current;
  7820. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  7821. do {
  7822. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7823. if (current) {
  7824. suite = wolfSSL_CIPHER_get_name(current);
  7825. if (suite && XSTRSTR(suite, "ECDSA")) {
  7826. found = 1;
  7827. break;
  7828. }
  7829. }
  7830. } while (current);
  7831. ExpectIntEQ(found, 1);
  7832. }
  7833. wolfSSL_free(sslClient);
  7834. wolfSSL_CTX_free(ctxClient);
  7835. #endif
  7836. return EXPECT_RESULT();
  7837. }
  7838. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7839. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7840. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7841. WOLFSSL* ssl)
  7842. {
  7843. EXPECT_DECLS;
  7844. size_t msg_len;
  7845. (void)ctx;
  7846. /* get_finished test */
  7847. /* 1. get own sent message */
  7848. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7849. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7850. ExpectIntGE(msg_len, 0);
  7851. /* 2. get peer message */
  7852. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7853. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7854. ExpectIntGE(msg_len, 0);
  7855. return EXPECT_RESULT();
  7856. }
  7857. #endif
  7858. static int test_wolfSSL_get_finished(void)
  7859. {
  7860. EXPECT_DECLS;
  7861. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7862. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7863. test_ssl_cbf client_cbf;
  7864. test_ssl_cbf server_cbf;
  7865. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7866. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7867. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7868. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7869. TEST_SUCCESS);
  7870. /* test received msg vs sent msg */
  7871. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7872. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7873. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7874. return EXPECT_RESULT();
  7875. }
  7876. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7877. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7878. !defined(NO_SESSION_CACHE)
  7879. /* Sessions to restore/store */
  7880. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7881. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7882. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7883. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7884. {
  7885. /* Don't store sessions. Lookup is still enabled. */
  7886. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7887. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7888. #ifdef OPENSSL_EXTRA
  7889. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7890. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7891. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7892. #endif
  7893. /* Require both peers to provide certs */
  7894. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7895. }
  7896. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7897. {
  7898. WOLFSSL_SESSION** sess;
  7899. if (wolfSSL_is_server(ssl))
  7900. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7901. else
  7902. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7903. if (*sess == NULL) {
  7904. #ifdef NO_SESSION_CACHE_REF
  7905. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7906. #else
  7907. /* Test for backwards compatibility */
  7908. if (wolfSSL_is_server(ssl)) {
  7909. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7910. }
  7911. else {
  7912. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7913. }
  7914. #endif
  7915. /* Now save the session in the internal store to make it available
  7916. * for lookup. For TLS 1.3, we can't save the session without
  7917. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7918. * session from cache. */
  7919. if (wolfSSL_is_server(ssl)
  7920. #ifndef WOLFSSL_TICKET_HAVE_ID
  7921. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7922. #endif
  7923. )
  7924. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7925. *sess), WOLFSSL_SUCCESS);
  7926. }
  7927. else {
  7928. /* If we have a session retrieved then remaining connections should be
  7929. * resuming on that session */
  7930. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7931. }
  7932. /* Save CTX to be able to decrypt tickets */
  7933. if (wolfSSL_is_server(ssl) &&
  7934. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7935. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7936. = wolfSSL_get_SSL_CTX(ssl));
  7937. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7938. WOLFSSL_SUCCESS);
  7939. }
  7940. #ifdef SESSION_CERTS
  7941. #ifndef WOLFSSL_TICKET_HAVE_ID
  7942. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7943. wolfSSL_session_reused(ssl))
  7944. #endif
  7945. {
  7946. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7947. * for all connections. TLS 1.3 only has tickets so if we don't
  7948. * include the session id in the ticket then the certificates
  7949. * will not be available on resumption. */
  7950. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7951. AssertNotNull(peer);
  7952. wolfSSL_X509_free(peer);
  7953. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7954. #ifdef OPENSSL_EXTRA
  7955. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7956. #endif
  7957. }
  7958. #endif /* SESSION_CERTS */
  7959. }
  7960. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7961. {
  7962. /* Set the session to reuse for the client */
  7963. AssertIntEQ(wolfSSL_set_session(ssl,
  7964. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7965. }
  7966. #endif
  7967. static int test_wolfSSL_CTX_add_session(void)
  7968. {
  7969. EXPECT_DECLS;
  7970. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7971. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7972. !defined(NO_SESSION_CACHE)
  7973. tcp_ready ready;
  7974. func_args client_args;
  7975. func_args server_args;
  7976. THREAD_TYPE serverThread;
  7977. callback_functions client_cb;
  7978. callback_functions server_cb;
  7979. method_provider methods[][2] = {
  7980. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7981. !defined(NO_DES3))
  7982. /* Without AES there are almost no ciphersuites available. This leads
  7983. * to no ciphersuites being available and an error. */
  7984. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7985. #endif
  7986. #ifndef WOLFSSL_NO_TLS12
  7987. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7988. #endif
  7989. /* Needs the default ticket callback since it is tied to the
  7990. * connection context and this makes it easy to carry over the ticket
  7991. * crypto context between connections */
  7992. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7993. defined(HAVE_SESSION_TICKET)
  7994. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7995. #endif
  7996. };
  7997. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7998. size_t i, j;
  7999. for (i = 0; i < methodsLen; i++) {
  8000. /* First run creates a connection while the second+ run will attempt
  8001. * to resume the connection. The trick is that the internal cache
  8002. * is turned off. wolfSSL_CTX_add_session should put the session in
  8003. * the cache anyway. */
  8004. test_wolfSSL_CTX_add_session_client_sess = NULL;
  8005. test_wolfSSL_CTX_add_session_server_sess = NULL;
  8006. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  8007. #ifdef NO_SESSION_CACHE_REF
  8008. for (j = 0; j < 4; j++) {
  8009. #else
  8010. /* The session may be overwritten in this case. Do only one resumption
  8011. * to stop this test from failing intermittently. */
  8012. for (j = 0; j < 2; j++) {
  8013. #endif
  8014. #ifdef WOLFSSL_TIRTOS
  8015. fdOpenSession(Task_self());
  8016. #endif
  8017. StartTCP();
  8018. InitTcpReady(&ready);
  8019. XMEMSET(&client_args, 0, sizeof(func_args));
  8020. XMEMSET(&server_args, 0, sizeof(func_args));
  8021. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8022. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8023. client_cb.method = methods[i][0];
  8024. server_cb.method = methods[i][1];
  8025. server_args.signal = &ready;
  8026. server_args.callbacks = &server_cb;
  8027. client_args.signal = &ready;
  8028. client_args.callbacks = &client_cb;
  8029. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  8030. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  8031. server_cb.isSharedCtx = 1;
  8032. }
  8033. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8034. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8035. if (j != 0)
  8036. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  8037. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8038. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8039. server_cb.ticNoInit = 1; /* Use default builtin */
  8040. start_thread(test_server_nofail, &server_args, &serverThread);
  8041. wait_tcp_ready(&server_args);
  8042. test_client_nofail(&client_args, NULL);
  8043. join_thread(serverThread);
  8044. ExpectTrue(client_args.return_code);
  8045. ExpectTrue(server_args.return_code);
  8046. FreeTcpReady(&ready);
  8047. if (EXPECT_FAIL())
  8048. break;
  8049. }
  8050. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  8051. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  8052. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  8053. if (EXPECT_FAIL())
  8054. break;
  8055. }
  8056. #endif
  8057. return EXPECT_RESULT();
  8058. }
  8059. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8060. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8061. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8062. defined(HAVE_SESSION_TICKET) && \
  8063. !defined(TITAN_SESSION_CACHE) && \
  8064. !defined(HUGE_SESSION_CACHE) && \
  8065. !defined(BIG_SESSION_CACHE) && \
  8066. !defined(MEDIUM_SESSION_CACHE)
  8067. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  8068. /* Sessions to restore/store */
  8069. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  8070. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  8071. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  8072. static int twcase_new_session_called = 0;
  8073. static int twcase_remove_session_called = 0;
  8074. static int twcase_get_session_called = 0;
  8075. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  8076. #define SESSION_CACHE_SIZE 33
  8077. typedef struct {
  8078. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  8079. WOLFSSL_SESSION* value;
  8080. } hashTable_entry;
  8081. typedef struct {
  8082. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  8083. size_t capacity; /* size of entries */
  8084. size_t length; /* number of items in the hash table */
  8085. wolfSSL_Mutex htLock; /* lock */
  8086. }hashTable;
  8087. static hashTable server_sessionCache;
  8088. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  8089. {
  8090. int i;
  8091. unsigned int len;
  8092. (void)ssl;
  8093. /*
  8094. * This example uses a hash table.
  8095. * Steps you should take for a non-demo code:
  8096. * - acquire a lock for the file named according to the session id
  8097. * - open the file
  8098. * - encrypt and write the SSL_SESSION object to the file
  8099. * - release the lock
  8100. *
  8101. * Return:
  8102. * 0: The callback does not wish to hold a reference of the sess
  8103. * 1: The callback wants to hold a reference of the sess. The callback is
  8104. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  8105. */
  8106. if (sess == NULL)
  8107. return 0;
  8108. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8109. return 0;
  8110. }
  8111. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8112. if (server_sessionCache.entries[i].value == NULL) {
  8113. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  8114. server_sessionCache.entries[i].value = sess;
  8115. server_sessionCache.length++;
  8116. break;
  8117. }
  8118. }
  8119. ++twcase_new_session_called;
  8120. wc_UnLockMutex(&server_sessionCache.htLock);
  8121. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  8122. twcase_new_session_called);
  8123. return 1;
  8124. }
  8125. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  8126. {
  8127. int i;
  8128. (void)ctx;
  8129. (void)sess;
  8130. if (sess == NULL)
  8131. return;
  8132. /*
  8133. * This example uses a hash table.
  8134. * Steps you should take for a non-demo code:
  8135. * - acquire a lock for the file named according to the session id
  8136. * - remove the file
  8137. * - release the lock
  8138. */
  8139. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8140. return;
  8141. }
  8142. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8143. if (server_sessionCache.entries[i].key != NULL &&
  8144. XMEMCMP(server_sessionCache.entries[i].key,
  8145. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8146. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8147. server_sessionCache.entries[i].value = NULL;
  8148. server_sessionCache.entries[i].key = NULL;
  8149. server_sessionCache.length--;
  8150. break;
  8151. }
  8152. }
  8153. ++twcase_remove_session_called;
  8154. wc_UnLockMutex(&server_sessionCache.htLock);
  8155. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  8156. twcase_remove_session_called);
  8157. }
  8158. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  8159. const unsigned char *id, int len, int *ref)
  8160. {
  8161. int i;
  8162. (void)ssl;
  8163. (void)id;
  8164. (void)len;
  8165. /*
  8166. * This example uses a hash table.
  8167. * Steps you should take for a non-demo code:
  8168. * - acquire a lock for the file named according to the session id in the
  8169. * 2nd arg
  8170. * - read and decrypt contents of file and create a new SSL_SESSION
  8171. * - object release the lock
  8172. * - return the new session object
  8173. */
  8174. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  8175. ++twcase_get_session_called);
  8176. /* This callback want to retain a copy of the object. If we want wolfSSL to
  8177. * be responsible for the pointer then set to 0. */
  8178. *ref = 1;
  8179. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8180. if (server_sessionCache.entries[i].key != NULL &&
  8181. XMEMCMP(server_sessionCache.entries[i].key, id,
  8182. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8183. return server_sessionCache.entries[i].value;
  8184. }
  8185. }
  8186. return NULL;
  8187. }
  8188. static int twcase_get_sessionCb_cleanup(void)
  8189. {
  8190. int i;
  8191. int cnt = 0;
  8192. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  8193. * for freeing sessions */
  8194. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8195. if (server_sessionCache.entries[i].value != NULL) {
  8196. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8197. cnt++;
  8198. }
  8199. }
  8200. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  8201. cnt);
  8202. return TEST_SUCCESS;
  8203. }
  8204. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  8205. {
  8206. EXPECT_DECLS;
  8207. /* off - Disable internal cache */
  8208. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8209. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8210. #ifdef OPENSSL_EXTRA
  8211. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8212. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8213. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8214. #endif
  8215. /* off - Do not setup external cache */
  8216. /* Require both peers to provide certs */
  8217. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8218. return EXPECT_RESULT();
  8219. }
  8220. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  8221. {
  8222. /* on - internal cache is on by default */
  8223. /* off - Do not setup external cache */
  8224. /* Require both peers to provide certs */
  8225. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8226. return TEST_SUCCESS;
  8227. }
  8228. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  8229. {
  8230. EXPECT_DECLS;
  8231. /* off - Disable internal cache */
  8232. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8233. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8234. #ifdef OPENSSL_EXTRA
  8235. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8236. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8237. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8238. #endif
  8239. /* on - Enable external cache */
  8240. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8241. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8242. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8243. /* Require both peers to provide certs */
  8244. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8245. return EXPECT_RESULT();
  8246. }
  8247. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  8248. {
  8249. /* on - internal cache is on by default */
  8250. /* on - Enable external cache */
  8251. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8252. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8253. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8254. /* Require both peers to provide certs */
  8255. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8256. return TEST_SUCCESS;
  8257. }
  8258. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  8259. {
  8260. /* on - internal cache is on by default */
  8261. /* on - Enable external cache */
  8262. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8263. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8264. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8265. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  8266. /* Require both peers to provide certs */
  8267. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8268. return TEST_SUCCESS;
  8269. }
  8270. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8271. {
  8272. EXPECT_DECLS;
  8273. WOLFSSL_SESSION** sess;
  8274. if (wolfSSL_is_server(ssl))
  8275. sess = &twcase_server_first_session_ptr;
  8276. else
  8277. return TEST_SUCCESS;
  8278. if (*sess == NULL) {
  8279. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8280. /* Now save the session in the internal store to make it available
  8281. * for lookup. For TLS 1.3, we can't save the session without
  8282. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  8283. * session from cache. */
  8284. if (wolfSSL_is_server(ssl)
  8285. #ifndef WOLFSSL_TICKET_HAVE_ID
  8286. && wolfSSL_version(ssl) != TLS1_3_VERSION
  8287. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  8288. #endif
  8289. ) {
  8290. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  8291. *sess), WOLFSSL_SUCCESS);
  8292. }
  8293. }
  8294. /* Save CTX to be able to decrypt tickets */
  8295. if (twcase_server_current_ctx_ptr == NULL) {
  8296. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  8297. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  8298. WOLFSSL_SUCCESS);
  8299. }
  8300. #ifdef SESSION_CERTS
  8301. #ifndef WOLFSSL_TICKET_HAVE_ID
  8302. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8303. wolfSSL_session_reused(ssl))
  8304. #endif
  8305. {
  8306. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  8307. * for all connections. TLS 1.3 only has tickets so if we don't
  8308. * include the session id in the ticket then the certificates
  8309. * will not be available on resumption. */
  8310. WOLFSSL_X509* peer = NULL;
  8311. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  8312. wolfSSL_X509_free(peer);
  8313. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8314. }
  8315. #endif
  8316. return EXPECT_RESULT();
  8317. }
  8318. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8319. {
  8320. EXPECT_DECLS;
  8321. WOLFSSL_SESSION** sess;
  8322. sess = &twcase_client_first_session_ptr;
  8323. if (*sess == NULL) {
  8324. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8325. }
  8326. else {
  8327. /* If we have a session retrieved then remaining connections should be
  8328. * resuming on that session */
  8329. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  8330. }
  8331. #ifdef SESSION_CERTS
  8332. #ifndef WOLFSSL_TICKET_HAVE_ID
  8333. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8334. wolfSSL_session_reused(ssl))
  8335. #endif
  8336. {
  8337. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  8338. ExpectNotNull(peer);
  8339. wolfSSL_X509_free(peer);
  8340. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8341. #ifdef OPENSSL_EXTRA
  8342. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  8343. #endif
  8344. }
  8345. #endif
  8346. return EXPECT_RESULT();
  8347. }
  8348. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  8349. {
  8350. EXPECT_DECLS;
  8351. /* Set the session to reuse for the client */
  8352. ExpectNotNull(ssl);
  8353. ExpectNotNull(twcase_client_first_session_ptr);
  8354. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  8355. WOLFSSL_SUCCESS);
  8356. return EXPECT_RESULT();
  8357. }
  8358. struct test_add_session_ext_params {
  8359. method_provider client_meth;
  8360. method_provider server_meth;
  8361. const char* tls_version;
  8362. };
  8363. static int test_wolfSSL_CTX_add_session_ext(
  8364. struct test_add_session_ext_params* param)
  8365. {
  8366. EXPECT_DECLS;
  8367. /* Test the default 33 sessions */
  8368. int j;
  8369. /* Clear cache before starting */
  8370. wolfSSL_CTX_flush_sessions(NULL, -1);
  8371. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  8372. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  8373. return BAD_MUTEX_E;
  8374. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  8375. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  8376. for (j = 0; j < 5; j++) {
  8377. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  8378. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  8379. test_ssl_cbf client_cb;
  8380. test_ssl_cbf server_cb;
  8381. (void)dtls;
  8382. /* Test five cache configurations */
  8383. twcase_client_first_session_ptr = NULL;
  8384. twcase_server_first_session_ptr = NULL;
  8385. twcase_server_current_ctx_ptr = NULL;
  8386. twcase_new_session_called = 0;
  8387. twcase_remove_session_called = 0;
  8388. twcase_get_session_called = 0;
  8389. /* connection 1 - first connection */
  8390. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  8391. XMEMSET(&client_cb, 0, sizeof(client_cb));
  8392. XMEMSET(&server_cb, 0, sizeof(server_cb));
  8393. client_cb.method = param->client_meth;
  8394. server_cb.method = param->server_meth;
  8395. if (dtls)
  8396. client_cb.doUdp = server_cb.doUdp = 1;
  8397. /* Setup internal and external cache */
  8398. switch (j) {
  8399. case 0:
  8400. /* SSL_OP_NO_TICKET stateful ticket case */
  8401. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  8402. break;
  8403. case 1:
  8404. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  8405. break;
  8406. case 2:
  8407. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  8408. break;
  8409. case 3:
  8410. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  8411. break;
  8412. case 4:
  8413. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  8414. break;
  8415. }
  8416. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  8417. /* Add session to internal cache and save SSL session for testing */
  8418. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  8419. /* Save client SSL session for testing */
  8420. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  8421. server_cb.ticNoInit = 1; /* Use default builtin */
  8422. /* Don't free/release ctx */
  8423. server_cb.ctx = twcase_server_current_ctx_ptr;
  8424. server_cb.isSharedCtx = 1;
  8425. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8426. &server_cb, NULL), TEST_SUCCESS);
  8427. ExpectIntEQ(twcase_get_session_called, 0);
  8428. if (EXPECT_FAIL()) {
  8429. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8430. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8431. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8432. break;
  8433. }
  8434. switch (j) {
  8435. case 0:
  8436. case 1:
  8437. case 2:
  8438. /* cache cannot be searched with out a connection */
  8439. /* Add a new session */
  8440. ExpectIntEQ(twcase_new_session_called, 1);
  8441. /* In twcase_server_sess_ctx_pre_shutdown
  8442. * wolfSSL_CTX_add_session which evicts the existing session
  8443. * in cache and adds it back in */
  8444. ExpectIntLE(twcase_remove_session_called, 1);
  8445. break;
  8446. case 3:
  8447. case 4:
  8448. /* no external cache */
  8449. ExpectIntEQ(twcase_new_session_called, 0);
  8450. ExpectIntEQ(twcase_remove_session_called, 0);
  8451. break;
  8452. }
  8453. /* connection 2 - session resume */
  8454. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  8455. twcase_new_session_called = 0;
  8456. twcase_remove_session_called = 0;
  8457. twcase_get_session_called = 0;
  8458. server_cb.on_result = 0;
  8459. client_cb.on_result = 0;
  8460. server_cb.ticNoInit = 1; /* Use default builtin */
  8461. server_cb.ctx = twcase_server_current_ctx_ptr;
  8462. /* try session resumption */
  8463. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  8464. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8465. &server_cb, NULL), TEST_SUCCESS);
  8466. /* Clear cache before checking */
  8467. wolfSSL_CTX_flush_sessions(NULL, -1);
  8468. switch (j) {
  8469. case 0:
  8470. if (tls13) {
  8471. /* (D)TLSv1.3 stateful case */
  8472. /* cache hit */
  8473. /* DTLS accesses cache once for stateless parsing and
  8474. * once for stateful parsing */
  8475. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8476. /* (D)TLSv1.3 creates a new ticket,
  8477. * updates both internal and external cache */
  8478. ExpectIntEQ(twcase_new_session_called, 1);
  8479. /* A new session ID is created for a new ticket */
  8480. ExpectIntEQ(twcase_remove_session_called, 2);
  8481. }
  8482. else {
  8483. /* non (D)TLSv1.3 case, no update */
  8484. /* DTLS accesses cache once for stateless parsing and
  8485. * once for stateful parsing */
  8486. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8487. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8488. #else
  8489. ExpectIntEQ(twcase_get_session_called, 1);
  8490. #endif
  8491. ExpectIntEQ(twcase_new_session_called, 0);
  8492. /* Called on session added in
  8493. * twcase_server_sess_ctx_pre_shutdown */
  8494. ExpectIntEQ(twcase_remove_session_called, 1);
  8495. }
  8496. break;
  8497. case 1:
  8498. if (tls13) {
  8499. /* (D)TLSv1.3 case */
  8500. /* cache hit */
  8501. ExpectIntEQ(twcase_get_session_called, 1);
  8502. /* (D)TLSv1.3 creates a new ticket,
  8503. * updates both internal and external cache */
  8504. ExpectIntEQ(twcase_new_session_called, 1);
  8505. /* Called on session added in
  8506. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8507. ExpectIntEQ(twcase_remove_session_called, 1);
  8508. }
  8509. else {
  8510. /* non (D)TLSv1.3 case */
  8511. /* cache hit */
  8512. /* DTLS accesses cache once for stateless parsing and
  8513. * once for stateful parsing */
  8514. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8515. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8516. #else
  8517. ExpectIntEQ(twcase_get_session_called, 1);
  8518. #endif
  8519. ExpectIntEQ(twcase_new_session_called, 0);
  8520. /* Called on session added in
  8521. * twcase_server_sess_ctx_pre_shutdown */
  8522. ExpectIntEQ(twcase_remove_session_called, 1);
  8523. }
  8524. break;
  8525. case 2:
  8526. if (tls13) {
  8527. /* (D)TLSv1.3 case */
  8528. /* cache hit */
  8529. ExpectIntEQ(twcase_get_session_called, 1);
  8530. /* (D)TLSv1.3 creates a new ticket,
  8531. * updates both internal and external cache */
  8532. ExpectIntEQ(twcase_new_session_called, 1);
  8533. /* Called on session added in
  8534. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8535. ExpectIntEQ(twcase_remove_session_called, 1);
  8536. }
  8537. else {
  8538. /* non (D)TLSv1.3 case */
  8539. /* cache hit */
  8540. /* DTLS accesses cache once for stateless parsing and
  8541. * once for stateful parsing */
  8542. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8543. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8544. #else
  8545. ExpectIntEQ(twcase_get_session_called, 1);
  8546. #endif
  8547. ExpectIntEQ(twcase_new_session_called, 0);
  8548. /* Called on session added in
  8549. * twcase_server_sess_ctx_pre_shutdown */
  8550. ExpectIntEQ(twcase_remove_session_called, 1);
  8551. }
  8552. break;
  8553. case 3:
  8554. case 4:
  8555. /* no external cache */
  8556. ExpectIntEQ(twcase_get_session_called, 0);
  8557. ExpectIntEQ(twcase_new_session_called, 0);
  8558. ExpectIntEQ(twcase_remove_session_called, 0);
  8559. break;
  8560. }
  8561. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8562. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8563. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8564. if (EXPECT_FAIL())
  8565. break;
  8566. }
  8567. twcase_get_sessionCb_cleanup();
  8568. XMEMSET(&server_sessionCache.entries, 0,
  8569. sizeof(server_sessionCache.entries));
  8570. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8571. wc_FreeMutex(&server_sessionCache.htLock);
  8572. return EXPECT_RESULT();
  8573. }
  8574. #endif
  8575. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8576. {
  8577. EXPECT_DECLS;
  8578. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8579. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8580. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8581. defined(HAVE_SESSION_TICKET) && \
  8582. !defined(TITAN_SESSION_CACHE) && \
  8583. !defined(HUGE_SESSION_CACHE) && \
  8584. !defined(BIG_SESSION_CACHE) && \
  8585. !defined(MEDIUM_SESSION_CACHE)
  8586. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8587. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8588. struct test_add_session_ext_params param[1] = {
  8589. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8590. };
  8591. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8592. #endif
  8593. #endif
  8594. return EXPECT_RESULT();
  8595. }
  8596. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8597. {
  8598. EXPECT_DECLS;
  8599. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8600. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8601. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8602. defined(HAVE_SESSION_TICKET) && \
  8603. !defined(TITAN_SESSION_CACHE) && \
  8604. !defined(HUGE_SESSION_CACHE) && \
  8605. !defined(BIG_SESSION_CACHE) && \
  8606. !defined(MEDIUM_SESSION_CACHE)
  8607. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8608. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8609. #ifdef WOLFSSL_DTLS13
  8610. struct test_add_session_ext_params param[1] = {
  8611. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8612. };
  8613. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8614. #endif
  8615. #endif
  8616. #endif
  8617. return EXPECT_RESULT();
  8618. }
  8619. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8620. {
  8621. EXPECT_DECLS;
  8622. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8623. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8624. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8625. defined(HAVE_SESSION_TICKET) && \
  8626. !defined(TITAN_SESSION_CACHE) && \
  8627. !defined(HUGE_SESSION_CACHE) && \
  8628. !defined(BIG_SESSION_CACHE) && \
  8629. !defined(MEDIUM_SESSION_CACHE)
  8630. #ifndef WOLFSSL_NO_TLS12
  8631. struct test_add_session_ext_params param[1] = {
  8632. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8633. };
  8634. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8635. #endif
  8636. #endif
  8637. return EXPECT_RESULT();
  8638. }
  8639. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8640. {
  8641. EXPECT_DECLS;
  8642. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8643. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8644. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8645. defined(HAVE_SESSION_TICKET) && \
  8646. !defined(TITAN_SESSION_CACHE) && \
  8647. !defined(HUGE_SESSION_CACHE) && \
  8648. !defined(BIG_SESSION_CACHE) && \
  8649. !defined(MEDIUM_SESSION_CACHE)
  8650. #ifndef WOLFSSL_NO_TLS12
  8651. #ifdef WOLFSSL_DTLS
  8652. struct test_add_session_ext_params param[1] = {
  8653. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8654. };
  8655. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8656. #endif
  8657. #endif
  8658. #endif
  8659. return EXPECT_RESULT();
  8660. }
  8661. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8662. {
  8663. EXPECT_DECLS;
  8664. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8665. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8666. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8667. defined(HAVE_SESSION_TICKET) && \
  8668. !defined(TITAN_SESSION_CACHE) && \
  8669. !defined(HUGE_SESSION_CACHE) && \
  8670. !defined(BIG_SESSION_CACHE) && \
  8671. !defined(MEDIUM_SESSION_CACHE)
  8672. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8673. !defined(NO_DES3))
  8674. struct test_add_session_ext_params param[1] = {
  8675. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  8676. };
  8677. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8678. #endif
  8679. #endif
  8680. return EXPECT_RESULT();
  8681. }
  8682. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  8683. {
  8684. EXPECT_DECLS;
  8685. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8686. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8687. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8688. defined(HAVE_SESSION_TICKET) && \
  8689. !defined(TITAN_SESSION_CACHE) && \
  8690. !defined(HUGE_SESSION_CACHE) && \
  8691. !defined(BIG_SESSION_CACHE) && \
  8692. !defined(MEDIUM_SESSION_CACHE)
  8693. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8694. !defined(NO_DES3))
  8695. #ifdef WOLFSSL_DTLS
  8696. struct test_add_session_ext_params param[1] = {
  8697. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  8698. };
  8699. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8700. #endif
  8701. #endif
  8702. #endif
  8703. return EXPECT_RESULT();
  8704. }
  8705. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8706. /* canned export of a session using older version 3 */
  8707. static unsigned char version_3[] = {
  8708. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  8709. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  8710. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8711. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  8712. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8713. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8714. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  8715. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  8716. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  8717. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8718. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8719. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  8720. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8721. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  8722. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8723. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  8724. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  8725. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8726. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  8727. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  8728. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8729. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8730. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8731. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8732. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8733. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8734. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8735. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8736. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8737. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8738. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8739. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  8740. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  8741. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  8742. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  8743. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  8744. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  8745. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  8746. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  8747. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  8748. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  8749. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8750. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  8751. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  8752. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8753. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  8754. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  8755. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  8756. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  8757. 0x2E, 0x31, 0xED, 0x4F
  8758. };
  8759. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  8760. static int test_wolfSSL_dtls_export(void)
  8761. {
  8762. EXPECT_DECLS;
  8763. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8764. tcp_ready ready;
  8765. func_args client_args;
  8766. func_args server_args;
  8767. THREAD_TYPE serverThread;
  8768. callback_functions server_cbf;
  8769. callback_functions client_cbf;
  8770. #ifdef WOLFSSL_TIRTOS
  8771. fdOpenSession(Task_self());
  8772. #endif
  8773. InitTcpReady(&ready);
  8774. #if defined(USE_WINDOWS_API)
  8775. /* use RNG to get random port if using windows */
  8776. ready.port = GetRandomPort();
  8777. #endif
  8778. /* set using dtls */
  8779. XMEMSET(&client_args, 0, sizeof(func_args));
  8780. XMEMSET(&server_args, 0, sizeof(func_args));
  8781. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8782. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  8783. server_cbf.method = wolfDTLSv1_2_server_method;
  8784. client_cbf.method = wolfDTLSv1_2_client_method;
  8785. server_args.callbacks = &server_cbf;
  8786. client_args.callbacks = &client_cbf;
  8787. server_args.signal = &ready;
  8788. client_args.signal = &ready;
  8789. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8790. wait_tcp_ready(&server_args);
  8791. run_wolfssl_client(&client_args);
  8792. join_thread(serverThread);
  8793. ExpectTrue(client_args.return_code);
  8794. ExpectTrue(server_args.return_code);
  8795. FreeTcpReady(&ready);
  8796. #ifdef WOLFSSL_TIRTOS
  8797. fdOpenSession(Task_self());
  8798. #endif
  8799. if (EXPECT_SUCCESS()) {
  8800. SOCKET_T sockfd = 0;
  8801. WOLFSSL_CTX* ctx = NULL;
  8802. WOLFSSL* ssl = NULL;
  8803. char msg[64] = "hello wolfssl!";
  8804. char reply[1024];
  8805. int msgSz = (int)XSTRLEN(msg);
  8806. byte *session, *window;
  8807. unsigned int sessionSz = 0;
  8808. unsigned int windowSz = 0;
  8809. #ifndef TEST_IPV6
  8810. struct sockaddr_in peerAddr;
  8811. #else
  8812. struct sockaddr_in6 peerAddr;
  8813. #endif /* TEST_IPV6 */
  8814. int i;
  8815. /* Set ctx to DTLS 1.2 */
  8816. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  8817. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8818. /* test importing version 3 */
  8819. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8820. /* test importing bad length and bad version */
  8821. version_3[2] += 1;
  8822. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8823. version_3[2] -= 1; version_3[1] = 0XA0;
  8824. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8825. wolfSSL_free(ssl);
  8826. wolfSSL_CTX_free(ctx);
  8827. /* check storing client state after connection and storing window only */
  8828. #ifdef WOLFSSL_TIRTOS
  8829. fdOpenSession(Task_self());
  8830. #endif
  8831. InitTcpReady(&ready);
  8832. #if defined(USE_WINDOWS_API)
  8833. /* use RNG to get random port if using windows */
  8834. ready.port = GetRandomPort();
  8835. #endif
  8836. /* set using dtls */
  8837. XMEMSET(&server_args, 0, sizeof(func_args));
  8838. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8839. server_cbf.method = wolfDTLSv1_2_server_method;
  8840. server_cbf.doUdp = 1;
  8841. server_args.callbacks = &server_cbf;
  8842. server_args.argc = 3; /* set loop_count to 3 */
  8843. server_args.signal = &ready;
  8844. start_thread(test_server_nofail, &server_args, &serverThread);
  8845. wait_tcp_ready(&server_args);
  8846. /* create and connect with client */
  8847. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8848. ExpectIntEQ(WOLFSSL_SUCCESS,
  8849. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8850. ExpectIntEQ(WOLFSSL_SUCCESS,
  8851. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8852. ExpectIntEQ(WOLFSSL_SUCCESS,
  8853. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8854. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8855. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8856. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8857. /* store server information connected too */
  8858. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8859. #ifndef TEST_IPV6
  8860. peerAddr.sin_family = AF_INET;
  8861. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8862. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8863. #else
  8864. peerAddr.sin6_family = AF_INET6;
  8865. ExpectIntEQ(
  8866. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8867. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8868. #endif
  8869. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8870. WOLFSSL_SUCCESS);
  8871. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8872. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8873. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8874. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8875. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8876. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8877. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8878. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8879. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8880. wolfSSL_free(ssl);
  8881. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8882. /* restore state */
  8883. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8884. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8885. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8886. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8887. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8888. WOLFSSL_SUCCESS);
  8889. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8890. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8891. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8892. wolfSSL_free(ssl);
  8893. }
  8894. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8895. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8896. wolfSSL_CTX_free(ctx);
  8897. fprintf(stderr, "done and waiting for server\n");
  8898. join_thread(serverThread);
  8899. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8900. FreeTcpReady(&ready);
  8901. #ifdef WOLFSSL_TIRTOS
  8902. fdOpenSession(Task_self());
  8903. #endif
  8904. }
  8905. #endif
  8906. return EXPECT_RESULT();
  8907. }
  8908. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8909. #ifdef WOLFSSL_TLS13
  8910. static const byte canned_client_tls13_session[] = {
  8911. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8912. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8913. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8914. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8915. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8916. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8917. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8918. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8919. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8920. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8921. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8922. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8923. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8924. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8925. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8926. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8927. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8928. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8929. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8930. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8931. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8932. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8933. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8934. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8935. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8936. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8937. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8938. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8939. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8940. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8941. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8942. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8943. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8944. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8945. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8946. 0x00, 0x03
  8947. };
  8948. static const byte canned_server_tls13_session[] = {
  8949. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8950. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8951. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8952. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8953. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8954. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8955. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8956. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8957. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8958. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8959. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8960. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8961. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8962. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8963. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8964. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8965. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8966. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8967. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8968. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8969. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8970. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8971. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8972. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8973. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8974. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8975. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8976. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8977. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8978. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8979. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8980. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8981. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8982. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8983. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8984. 0x00, 0x04
  8985. };
  8986. #endif /* WOLFSSL_TLS13 */
  8987. static const byte canned_client_session[] = {
  8988. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8989. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8990. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8991. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8992. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8993. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8994. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8995. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8996. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8997. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8998. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8999. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  9000. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  9001. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  9002. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  9003. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  9004. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  9005. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  9006. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  9007. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  9008. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  9009. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  9010. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  9011. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  9012. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  9013. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  9014. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  9015. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  9016. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  9017. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9018. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9019. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9020. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9021. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  9022. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  9023. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  9024. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  9025. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9026. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9027. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9028. 0x00, 0x03
  9029. };
  9030. static const byte canned_server_session[] = {
  9031. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  9032. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  9033. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  9034. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  9035. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  9036. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9037. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  9038. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  9039. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  9040. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9041. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9042. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  9043. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  9044. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  9045. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  9046. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  9047. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  9048. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  9049. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  9050. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  9051. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  9052. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  9053. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  9054. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  9055. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  9056. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  9057. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  9058. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  9059. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  9060. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9061. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9062. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9063. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9064. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  9065. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  9066. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9067. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9068. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  9069. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  9070. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9071. 0x00, 0x04
  9072. };
  9073. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  9074. {
  9075. SOCKET_T sockfd = 0;
  9076. SOCKET_T clientfd = 0;
  9077. word16 port;
  9078. callback_functions* cbf;
  9079. WOLFSSL_CTX* ctx = 0;
  9080. WOLFSSL* ssl = 0;
  9081. char msg[] = "I hear you fa shizzle!";
  9082. char input[1024];
  9083. int idx;
  9084. #ifdef WOLFSSL_TIRTOS
  9085. fdOpenSession(Task_self());
  9086. #endif
  9087. ((func_args*)args)->return_code = TEST_FAIL;
  9088. cbf = ((func_args*)args)->callbacks;
  9089. #if defined(USE_WINDOWS_API)
  9090. port = ((func_args*)args)->signal->port;
  9091. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  9092. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  9093. /* Let tcp_listen assign port */
  9094. port = 0;
  9095. #else
  9096. /* Use default port */
  9097. port = wolfSSLPort;
  9098. #endif
  9099. /* do it here to detect failure */
  9100. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  9101. CloseSocket(sockfd);
  9102. {
  9103. WOLFSSL_METHOD* method = NULL;
  9104. if (cbf != NULL && cbf->method != NULL) {
  9105. method = cbf->method();
  9106. }
  9107. else {
  9108. method = wolfTLSv1_2_server_method();
  9109. }
  9110. ctx = wolfSSL_CTX_new(method);
  9111. }
  9112. if (ctx == NULL) {
  9113. goto done;
  9114. }
  9115. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9116. /* call ctx setup callback */
  9117. if (cbf != NULL && cbf->ctx_ready != NULL) {
  9118. cbf->ctx_ready(ctx);
  9119. }
  9120. ssl = wolfSSL_new(ctx);
  9121. if (ssl == NULL) {
  9122. goto done;
  9123. }
  9124. wolfSSL_set_fd(ssl, clientfd);
  9125. /* call ssl setup callback */
  9126. if (cbf != NULL && cbf->ssl_ready != NULL) {
  9127. cbf->ssl_ready(ssl);
  9128. }
  9129. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  9130. if (idx > 0) {
  9131. input[idx] = '\0';
  9132. fprintf(stderr, "Client message export/import: %s\n", input);
  9133. }
  9134. else {
  9135. fprintf(stderr, "ret = %d error = %d\n", idx,
  9136. wolfSSL_get_error(ssl, idx));
  9137. goto done;
  9138. }
  9139. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  9140. /*err_sys("SSL_write failed");*/
  9141. WOLFSSL_RETURN_FROM_THREAD(0);
  9142. }
  9143. #ifdef WOLFSSL_TIRTOS
  9144. Task_yield();
  9145. #endif
  9146. ((func_args*)args)->return_code = TEST_SUCCESS;
  9147. done:
  9148. wolfSSL_shutdown(ssl);
  9149. wolfSSL_free(ssl);
  9150. wolfSSL_CTX_free(ctx);
  9151. CloseSocket(clientfd);
  9152. #ifdef WOLFSSL_TIRTOS
  9153. fdCloseSession(Task_self());
  9154. #endif
  9155. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9156. && defined(HAVE_THREAD_LS)
  9157. wc_ecc_fp_free(); /* free per thread cache */
  9158. #endif
  9159. #if defined(HAVE_SESSION_TICKET) && \
  9160. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  9161. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  9162. OpenSSLTicketCleanup();
  9163. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  9164. TicketCleanup();
  9165. #endif
  9166. #endif
  9167. WOLFSSL_RETURN_FROM_THREAD(0);
  9168. }
  9169. static void load_tls12_canned_server(WOLFSSL* ssl)
  9170. {
  9171. int clientfd = wolfSSL_get_fd(ssl);
  9172. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  9173. sizeof(canned_server_session)), sizeof(canned_server_session));
  9174. wolfSSL_set_fd(ssl, clientfd);
  9175. }
  9176. #ifdef WOLFSSL_TLS13
  9177. static void load_tls13_canned_server(WOLFSSL* ssl)
  9178. {
  9179. int clientfd = wolfSSL_get_fd(ssl);
  9180. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  9181. sizeof(canned_server_tls13_session)),
  9182. sizeof(canned_server_tls13_session));
  9183. wolfSSL_set_fd(ssl, clientfd);
  9184. }
  9185. #endif
  9186. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  9187. static int test_wolfSSL_tls_export_run(int v)
  9188. {
  9189. EXPECT_DECLS;
  9190. SOCKET_T sockfd = 0;
  9191. WOLFSSL_CTX* ctx = 0;
  9192. WOLFSSL* ssl = 0;
  9193. char msg[64] = "hello wolfssl!";
  9194. char reply[1024];
  9195. word32 replySz;
  9196. int msgSz = (int)XSTRLEN(msg);
  9197. const byte* clientSession = NULL;
  9198. int clientSessionSz = 0;
  9199. tcp_ready ready;
  9200. func_args server_args;
  9201. THREAD_TYPE serverThread;
  9202. callback_functions server_cbf;
  9203. #ifdef WOLFSSL_TIRTOS
  9204. fdOpenSession(Task_self());
  9205. #endif
  9206. InitTcpReady(&ready);
  9207. #if defined(USE_WINDOWS_API)
  9208. /* use RNG to get random port if using windows */
  9209. ready.port = GetRandomPort();
  9210. #endif
  9211. XMEMSET(&server_args, 0, sizeof(func_args));
  9212. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9213. switch (v) {
  9214. case WOLFSSL_TLSV1_2:
  9215. server_cbf.method = wolfTLSv1_2_server_method;
  9216. server_cbf.ssl_ready = load_tls12_canned_server;
  9217. /* setup the client side */
  9218. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9219. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9220. clientSession = canned_client_session;
  9221. clientSessionSz = sizeof(canned_client_session);
  9222. break;
  9223. #ifdef WOLFSSL_TLS13
  9224. case WOLFSSL_TLSV1_3:
  9225. server_cbf.method = wolfTLSv1_3_server_method;
  9226. server_cbf.ssl_ready = load_tls13_canned_server;
  9227. /* setup the client side */
  9228. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9229. clientSession = canned_client_tls13_session;
  9230. clientSessionSz = sizeof(canned_client_tls13_session);
  9231. break;
  9232. #endif
  9233. }
  9234. server_args.callbacks = &server_cbf;
  9235. server_args.signal = &ready;
  9236. start_thread(tls_export_server, &server_args, &serverThread);
  9237. wait_tcp_ready(&server_args);
  9238. #ifdef WOLFSSL_TIRTOS
  9239. fdOpenSession(Task_self());
  9240. #endif
  9241. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9242. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  9243. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  9244. clientSessionSz);
  9245. replySz = sizeof(reply);
  9246. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  9247. #if !defined(NO_PSK) && defined(HAVE_ANON)
  9248. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  9249. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  9250. #endif
  9251. wolfSSL_set_fd(ssl, sockfd);
  9252. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9253. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  9254. wolfSSL_free(ssl);
  9255. wolfSSL_CTX_free(ctx);
  9256. CloseSocket(sockfd);
  9257. #ifdef WOLFSSL_TIRTOS
  9258. fdCloseSession(Task_self());
  9259. #endif
  9260. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9261. && defined(HAVE_THREAD_LS)
  9262. wc_ecc_fp_free(); /* free per thread cache */
  9263. #endif
  9264. join_thread(serverThread);
  9265. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  9266. FreeTcpReady(&ready);
  9267. #ifdef WOLFSSL_TIRTOS
  9268. fdOpenSession(Task_self());
  9269. #endif
  9270. return EXPECT_RESULT();
  9271. }
  9272. #endif
  9273. static int test_wolfSSL_tls_export(void)
  9274. {
  9275. int res = TEST_SKIPPED;
  9276. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  9277. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  9278. #ifdef WOLFSSL_TLS13
  9279. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  9280. #endif
  9281. res = TEST_RES_CHECK(1);
  9282. #endif
  9283. return res;
  9284. }
  9285. /*----------------------------------------------------------------------------*
  9286. | TLS extensions tests
  9287. *----------------------------------------------------------------------------*/
  9288. #ifdef ENABLE_TLS_CALLBACK_TEST
  9289. /* Connection test runner - generic */
  9290. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  9291. callback_functions* server_callbacks)
  9292. {
  9293. tcp_ready ready;
  9294. func_args client_args;
  9295. func_args server_args;
  9296. THREAD_TYPE serverThread;
  9297. XMEMSET(&client_args, 0, sizeof(func_args));
  9298. XMEMSET(&server_args, 0, sizeof(func_args));
  9299. StartTCP();
  9300. client_args.callbacks = client_callbacks;
  9301. server_args.callbacks = server_callbacks;
  9302. #ifdef WOLFSSL_TIRTOS
  9303. fdOpenSession(Task_self());
  9304. #endif
  9305. /* RUN Server side */
  9306. InitTcpReady(&ready);
  9307. #if defined(USE_WINDOWS_API)
  9308. /* use RNG to get random port if using windows */
  9309. ready.port = GetRandomPort();
  9310. #endif
  9311. server_args.signal = &ready;
  9312. client_args.signal = &ready;
  9313. start_thread(run_wolfssl_server, &server_args, &serverThread);
  9314. wait_tcp_ready(&server_args);
  9315. /* RUN Client side */
  9316. run_wolfssl_client(&client_args);
  9317. join_thread(serverThread);
  9318. FreeTcpReady(&ready);
  9319. #ifdef WOLFSSL_TIRTOS
  9320. fdCloseSession(Task_self());
  9321. #endif
  9322. client_callbacks->return_code = client_args.return_code;
  9323. server_callbacks->return_code = server_args.return_code;
  9324. }
  9325. #endif /* ENABLE_TLS_CALLBACK_TEST */
  9326. #ifdef HAVE_SNI
  9327. static int test_wolfSSL_UseSNI_params(void)
  9328. {
  9329. EXPECT_DECLS;
  9330. #if !defined(NO_WOLFSSL_CLIENT)
  9331. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9332. WOLFSSL *ssl = wolfSSL_new(ctx);
  9333. ExpectNotNull(ctx);
  9334. ExpectNotNull(ssl);
  9335. /* invalid [ctx|ssl] */
  9336. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  9337. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  9338. /* invalid type */
  9339. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  9340. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  9341. /* invalid data */
  9342. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  9343. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  9344. /* success case */
  9345. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  9346. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  9347. wolfSSL_free(ssl);
  9348. wolfSSL_CTX_free(ctx);
  9349. #endif /* !NO_WOLFSSL_CLIENT */
  9350. return EXPECT_RESULT();
  9351. }
  9352. /* BEGIN of connection tests callbacks */
  9353. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9354. {
  9355. AssertIntEQ(WOLFSSL_SUCCESS,
  9356. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9357. }
  9358. static void use_SNI_at_ssl(WOLFSSL* ssl)
  9359. {
  9360. AssertIntEQ(WOLFSSL_SUCCESS,
  9361. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9362. }
  9363. static void different_SNI_at_ssl(WOLFSSL* ssl)
  9364. {
  9365. AssertIntEQ(WOLFSSL_SUCCESS,
  9366. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  9367. }
  9368. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  9369. {
  9370. use_SNI_at_ssl(ssl);
  9371. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9372. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  9373. }
  9374. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  9375. {
  9376. use_SNI_at_ssl(ssl);
  9377. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9378. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  9379. }
  9380. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9381. {
  9382. use_SNI_at_ctx(ctx);
  9383. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9384. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9385. }
  9386. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  9387. {
  9388. use_SNI_at_ssl(ssl);
  9389. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9390. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9391. }
  9392. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9393. {
  9394. use_SNI_at_ctx(ctx);
  9395. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9396. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9397. }
  9398. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  9399. {
  9400. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  9401. }
  9402. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  9403. {
  9404. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  9405. }
  9406. static void verify_SNI_no_matching(WOLFSSL* ssl)
  9407. {
  9408. byte type = WOLFSSL_SNI_HOST_NAME;
  9409. void* request = (void*) &type; /* to be overwritten */
  9410. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  9411. AssertNotNull(request);
  9412. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9413. AssertNull(request);
  9414. }
  9415. static void verify_SNI_real_matching(WOLFSSL* ssl)
  9416. {
  9417. byte type = WOLFSSL_SNI_HOST_NAME;
  9418. void* request = NULL;
  9419. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  9420. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9421. AssertNotNull(request);
  9422. AssertStrEQ("www.wolfssl.com", (char*)request);
  9423. }
  9424. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  9425. {
  9426. byte type = WOLFSSL_SNI_HOST_NAME;
  9427. void* request = NULL;
  9428. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  9429. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9430. AssertNotNull(request);
  9431. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  9432. }
  9433. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9434. {
  9435. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  9436. }
  9437. /* END of connection tests callbacks */
  9438. static int test_wolfSSL_UseSNI_connection(void)
  9439. {
  9440. int res = TEST_SKIPPED;
  9441. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9442. callback_functions client_cb;
  9443. callback_functions server_cb;
  9444. size_t i;
  9445. #ifdef WOLFSSL_STATIC_MEMORY
  9446. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  9447. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  9448. #endif
  9449. struct {
  9450. method_provider client_meth;
  9451. method_provider server_meth;
  9452. #ifdef WOLFSSL_STATIC_MEMORY
  9453. wolfSSL_method_func client_meth_ex;
  9454. wolfSSL_method_func server_meth_ex;
  9455. #endif
  9456. } methods[] = {
  9457. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  9458. {wolfSSLv23_client_method, wolfSSLv23_server_method
  9459. #ifdef WOLFSSL_STATIC_MEMORY
  9460. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  9461. #endif
  9462. },
  9463. #endif
  9464. #ifndef WOLFSSL_NO_TLS12
  9465. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  9466. #ifdef WOLFSSL_STATIC_MEMORY
  9467. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  9468. #endif
  9469. },
  9470. #endif
  9471. #ifdef WOLFSSL_TLS13
  9472. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9473. #ifdef WOLFSSL_STATIC_MEMORY
  9474. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9475. #endif
  9476. },
  9477. #endif
  9478. };
  9479. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9480. for (i = 0; i < methodsSz; i++) {
  9481. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9482. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9483. client_cb.method = methods[i].client_meth;
  9484. server_cb.method = methods[i].server_meth;
  9485. client_cb.devId = testDevId;
  9486. server_cb.devId = testDevId;
  9487. #ifdef WOLFSSL_STATIC_MEMORY
  9488. client_cb.method_ex = methods[i].client_meth_ex;
  9489. server_cb.method_ex = methods[i].server_meth_ex;
  9490. client_cb.mem = cliMem;
  9491. client_cb.memSz = (word32)sizeof(cliMem);
  9492. server_cb.mem = svrMem;
  9493. server_cb.memSz = (word32)sizeof(svrMem);;
  9494. #endif
  9495. /* success case at ctx */
  9496. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9497. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9498. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9499. test_wolfSSL_client_server(&client_cb, &server_cb);
  9500. /* success case at ssl */
  9501. fprintf(stderr, "\tsuccess case at ssl\n");
  9502. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9503. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9504. test_wolfSSL_client_server(&client_cb, &server_cb);
  9505. /* default mismatch behavior */
  9506. fprintf(stderr, "\tdefault mismatch behavior\n");
  9507. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9508. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9509. test_wolfSSL_client_server(&client_cb, &server_cb);
  9510. /* continue on mismatch */
  9511. fprintf(stderr, "\tcontinue on mismatch\n");
  9512. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9513. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9514. test_wolfSSL_client_server(&client_cb, &server_cb);
  9515. /* fake answer on mismatch */
  9516. fprintf(stderr, "\tfake answer on mismatch\n");
  9517. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9518. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9519. test_wolfSSL_client_server(&client_cb, &server_cb);
  9520. /* sni abort - success */
  9521. fprintf(stderr, "\tsni abort - success\n");
  9522. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9523. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9524. test_wolfSSL_client_server(&client_cb, &server_cb);
  9525. /* sni abort - abort when absent (ctx) */
  9526. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9527. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9528. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9529. test_wolfSSL_client_server(&client_cb, &server_cb);
  9530. /* sni abort - abort when absent (ssl) */
  9531. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9532. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9533. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9534. test_wolfSSL_client_server(&client_cb, &server_cb);
  9535. /* sni abort - success when overwritten */
  9536. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9537. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9538. 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;
  9539. test_wolfSSL_client_server(&client_cb, &server_cb);
  9540. /* sni abort - success when allowing mismatches */
  9541. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9542. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9543. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9544. test_wolfSSL_client_server(&client_cb, &server_cb);
  9545. }
  9546. res = TEST_RES_CHECK(1);
  9547. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9548. return res;
  9549. }
  9550. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9551. {
  9552. EXPECT_DECLS;
  9553. byte buff[] = { /* www.paypal.com */
  9554. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9555. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9556. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9557. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9558. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9559. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9560. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9561. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9562. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9563. };
  9564. byte buff2[] = { /* api.textmate.org */
  9565. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9566. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9567. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9568. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9569. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9570. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9571. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9572. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9573. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9574. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9575. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9576. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9577. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9578. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9579. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9580. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9581. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9582. };
  9583. byte buff3[] = { /* no sni extension */
  9584. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9585. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9586. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9587. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9588. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9589. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9590. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9591. };
  9592. byte buff4[] = { /* last extension has zero size */
  9593. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9594. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9595. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9596. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9597. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9598. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9599. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9600. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9601. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9602. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9603. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9604. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9605. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9606. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9607. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9608. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9609. 0x12, 0x00, 0x00
  9610. };
  9611. byte buff5[] = { /* SSL v2.0 client hello */
  9612. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9613. /* dummy bytes below, just to pass size check */
  9614. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9615. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9616. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9617. };
  9618. byte result[32] = {0};
  9619. word32 length = 32;
  9620. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9621. 0, result, &length));
  9622. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9623. 0, result, &length));
  9624. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9625. 1, result, &length));
  9626. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9627. 0, result, &length));
  9628. buff[0] = 0x16;
  9629. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9630. 0, result, &length));
  9631. buff[1] = 0x03;
  9632. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9633. sizeof(buff), 0, result, &length));
  9634. buff[2] = 0x03;
  9635. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9636. sizeof(buff), 0, result, &length));
  9637. buff[4] = 0x64;
  9638. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9639. 0, result, &length));
  9640. if (EXPECT_SUCCESS())
  9641. result[length] = 0;
  9642. ExpectStrEQ("www.paypal.com", (const char*) result);
  9643. length = 32;
  9644. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9645. 0, result, &length));
  9646. if (EXPECT_SUCCESS())
  9647. result[length] = 0;
  9648. ExpectStrEQ("api.textmate.org", (const char*) result);
  9649. /* SSL v2.0 tests */
  9650. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9651. sizeof(buff5), 0, result, &length));
  9652. buff5[2] = 0x02;
  9653. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9654. sizeof(buff5), 0, result, &length));
  9655. buff5[2] = 0x01; buff5[6] = 0x08;
  9656. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9657. sizeof(buff5), 0, result, &length));
  9658. buff5[6] = 0x09; buff5[8] = 0x01;
  9659. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9660. sizeof(buff5), 0, result, &length));
  9661. return EXPECT_RESULT();
  9662. }
  9663. #endif /* HAVE_SNI */
  9664. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9665. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9666. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9667. /* Dummy peer functions to satisfy the exporter/importer */
  9668. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  9669. int* ipSz, unsigned short* port, int* fam)
  9670. {
  9671. (void)ssl;
  9672. ip[0] = -1;
  9673. *ipSz = 1;
  9674. *port = 1;
  9675. *fam = 2;
  9676. return 1;
  9677. }
  9678. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  9679. int ipSz, unsigned short port, int fam)
  9680. {
  9681. (void)ssl;
  9682. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  9683. return 0;
  9684. return 1;
  9685. }
  9686. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  9687. WOLFSSL **ssl)
  9688. {
  9689. EXPECT_DECLS;
  9690. unsigned char* sessionBuf = NULL;
  9691. unsigned int sessionSz = 0;
  9692. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  9693. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  9694. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  9695. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  9696. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  9697. ExpectNotNull(sessionBuf =
  9698. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  9699. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  9700. wolfSSL_free(*ssl);
  9701. *ssl = NULL;
  9702. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  9703. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  9704. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  9705. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  9706. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9707. return EXPECT_RESULT();
  9708. }
  9709. #endif
  9710. static int test_wolfSSL_dtls_export_peers(void)
  9711. {
  9712. EXPECT_DECLS;
  9713. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9714. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9715. test_ssl_cbf client_cbf;
  9716. test_ssl_cbf server_cbf;
  9717. size_t i, j;
  9718. struct test_params {
  9719. method_provider client_meth;
  9720. method_provider server_meth;
  9721. const char* dtls_version;
  9722. } params[] = {
  9723. #ifndef NO_OLD_TLS
  9724. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  9725. #endif
  9726. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  9727. /* TODO DTLS 1.3 exporting not supported
  9728. #ifdef WOLFSSL_DTLS13
  9729. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  9730. #endif
  9731. */
  9732. };
  9733. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  9734. for (j = 0; j <= 0b11; j++) {
  9735. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  9736. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  9737. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  9738. client_cbf.method = params[i].client_meth;
  9739. server_cbf.method = params[i].server_meth;
  9740. if (j & 0b01) {
  9741. client_cbf.on_handshake =
  9742. test_wolfSSL_dtls_export_peers_on_handshake;
  9743. printf(" With client export;");
  9744. }
  9745. if (j & 0b10) {
  9746. server_cbf.on_handshake =
  9747. test_wolfSSL_dtls_export_peers_on_handshake;
  9748. printf(" With server export;");
  9749. }
  9750. printf("\n");
  9751. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  9752. &server_cbf, NULL), TEST_SUCCESS);
  9753. if (!EXPECT_SUCCESS())
  9754. break;
  9755. }
  9756. }
  9757. #endif
  9758. return EXPECT_RESULT();
  9759. }
  9760. static int test_wolfSSL_UseTrustedCA(void)
  9761. {
  9762. EXPECT_DECLS;
  9763. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  9764. && !defined(NO_RSA)
  9765. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9766. WOLFSSL_CTX *ctx = NULL;
  9767. WOLFSSL *ssl = NULL;
  9768. byte id[20];
  9769. #ifndef NO_WOLFSSL_SERVER
  9770. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  9771. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9772. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9773. #else
  9774. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  9775. #endif
  9776. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  9777. XMEMSET(id, 0, sizeof(id));
  9778. /* error cases */
  9779. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  9780. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9781. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  9782. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9783. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  9784. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9785. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  9786. #ifdef NO_SHA
  9787. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9788. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9789. #endif
  9790. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9791. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  9792. /* success cases */
  9793. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9794. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  9795. #ifndef NO_SHA
  9796. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9797. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9798. #endif
  9799. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9800. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  9801. wolfSSL_free(ssl);
  9802. wolfSSL_CTX_free(ctx);
  9803. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9804. #endif /* HAVE_TRUSTED_CA */
  9805. return EXPECT_RESULT();
  9806. }
  9807. static int test_wolfSSL_UseMaxFragment(void)
  9808. {
  9809. EXPECT_DECLS;
  9810. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  9811. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9812. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9813. #ifndef NO_WOLFSSL_SERVER
  9814. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9815. #else
  9816. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9817. #endif
  9818. WOLFSSL *ssl = NULL;
  9819. #ifdef OPENSSL_EXTRA
  9820. int (*UseMaxFragment)(SSL *s, unsigned char mode);
  9821. int (*CTX_UseMaxFragment)(SSL_CTX *c, unsigned char mode);
  9822. #else
  9823. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9824. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9825. #endif
  9826. #ifndef NO_WOLFSSL_SERVER
  9827. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9828. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9829. #endif
  9830. ExpectNotNull(ctx);
  9831. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9832. #ifdef OPENSSL_EXTRA
  9833. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9834. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9835. #else
  9836. UseMaxFragment = wolfSSL_UseMaxFragment;
  9837. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9838. #endif
  9839. /* error cases */
  9840. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9841. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9842. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9843. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9844. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9845. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9846. /* success case */
  9847. #ifdef OPENSSL_EXTRA
  9848. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9849. #else
  9850. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9851. #endif
  9852. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9853. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9854. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9855. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9856. #ifdef OPENSSL_EXTRA
  9857. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9858. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9859. #else
  9860. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9861. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9862. #endif
  9863. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9864. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9865. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9866. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9867. #ifdef OPENSSL_EXTRA
  9868. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9869. #else
  9870. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9871. #endif
  9872. wolfSSL_free(ssl);
  9873. wolfSSL_CTX_free(ctx);
  9874. #if defined(OPENSSL_EXTRA) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  9875. /* check negotiated max fragment size */
  9876. {
  9877. WOLFSSL *ssl_c = NULL;
  9878. WOLFSSL *ssl_s = NULL;
  9879. struct test_memio_ctx test_ctx;
  9880. WOLFSSL_CTX *ctx_c = NULL;
  9881. WOLFSSL_CTX *ctx_s = NULL;
  9882. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  9883. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9884. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9885. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c, WOLFSSL_MFL_2_8),
  9886. WOLFSSL_SUCCESS);
  9887. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9888. ExpectIntEQ(SSL_SESSION_get_max_fragment_length(
  9889. wolfSSL_get_session(ssl_c)), WOLFSSL_MFL_2_8);
  9890. wolfSSL_free(ssl_c);
  9891. wolfSSL_free(ssl_s);
  9892. wolfSSL_CTX_free(ctx_c);
  9893. wolfSSL_CTX_free(ctx_s);
  9894. }
  9895. #endif
  9896. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9897. #endif
  9898. return EXPECT_RESULT();
  9899. }
  9900. static int test_wolfSSL_UseTruncatedHMAC(void)
  9901. {
  9902. EXPECT_DECLS;
  9903. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9904. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9905. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9906. #ifndef NO_WOLFSSL_SERVER
  9907. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9908. #else
  9909. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9910. #endif
  9911. WOLFSSL *ssl = NULL;
  9912. ExpectNotNull(ctx);
  9913. #ifndef NO_WOLFSSL_SERVER
  9914. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9915. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9916. #endif
  9917. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9918. /* error cases */
  9919. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9920. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9921. /* success case */
  9922. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9923. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9924. wolfSSL_free(ssl);
  9925. wolfSSL_CTX_free(ctx);
  9926. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9927. #endif
  9928. return EXPECT_RESULT();
  9929. }
  9930. static int test_wolfSSL_UseSupportedCurve(void)
  9931. {
  9932. EXPECT_DECLS;
  9933. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9934. !defined(NO_TLS)
  9935. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9936. WOLFSSL *ssl = wolfSSL_new(ctx);
  9937. ExpectNotNull(ctx);
  9938. ExpectNotNull(ssl);
  9939. /* error cases */
  9940. ExpectIntNE(WOLFSSL_SUCCESS,
  9941. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9942. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9943. ExpectIntNE(WOLFSSL_SUCCESS,
  9944. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9945. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9946. /* success case */
  9947. ExpectIntEQ(WOLFSSL_SUCCESS,
  9948. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9949. ExpectIntEQ(WOLFSSL_SUCCESS,
  9950. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9951. wolfSSL_free(ssl);
  9952. wolfSSL_CTX_free(ctx);
  9953. #endif
  9954. return EXPECT_RESULT();
  9955. }
  9956. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9957. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9958. {
  9959. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9960. }
  9961. static void use_ALPN_all(WOLFSSL* ssl)
  9962. {
  9963. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9964. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9965. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9966. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9967. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9968. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9969. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9970. }
  9971. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9972. {
  9973. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9974. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9975. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9976. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9977. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9978. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9979. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9980. }
  9981. static void use_ALPN_one(WOLFSSL* ssl)
  9982. {
  9983. /* spdy/2 */
  9984. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9985. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9986. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9987. }
  9988. static void use_ALPN_unknown(WOLFSSL* ssl)
  9989. {
  9990. /* http/2.0 */
  9991. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9992. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9993. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9994. }
  9995. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9996. {
  9997. /* http/2.0 */
  9998. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9999. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  10000. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10001. }
  10002. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  10003. {
  10004. /* spdy/3 */
  10005. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10006. char *proto = NULL;
  10007. word16 protoSz = 0;
  10008. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  10009. /* check value */
  10010. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  10011. if (proto) {
  10012. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  10013. }
  10014. }
  10015. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  10016. {
  10017. char *proto = NULL;
  10018. word16 protoSz = 0;
  10019. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  10020. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  10021. /* check value */
  10022. AssertIntEQ(1, (0 == protoSz));
  10023. AssertIntEQ(1, (NULL == proto));
  10024. }
  10025. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  10026. {
  10027. /* http/1.1 */
  10028. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10029. char *proto;
  10030. word16 protoSz = 0;
  10031. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  10032. /* check value */
  10033. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10034. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10035. }
  10036. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  10037. {
  10038. /* spdy/2 */
  10039. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10040. char *proto;
  10041. word16 protoSz = 0;
  10042. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  10043. /* check value */
  10044. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10045. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10046. }
  10047. static void verify_ALPN_client_list(WOLFSSL* ssl)
  10048. {
  10049. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  10050. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  10051. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  10052. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  10053. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10054. char *clist = NULL;
  10055. word16 clistSz = 0;
  10056. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  10057. &clistSz));
  10058. /* check value */
  10059. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  10060. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  10061. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  10062. }
  10063. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10064. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10065. /* ALPN select callback, success with spdy/2 */
  10066. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  10067. unsigned char *outlen, const unsigned char *in,
  10068. unsigned int inlen, void *arg)
  10069. {
  10070. /* spdy/2 */
  10071. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10072. (void)ssl;
  10073. (void)arg;
  10074. /* adding +1 since LEN byte comes first */
  10075. if (inlen < sizeof(proto) + 1) {
  10076. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10077. }
  10078. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  10079. *out = in + 1;
  10080. *outlen = (unsigned char)sizeof(proto);
  10081. return SSL_TLSEXT_ERR_OK;
  10082. }
  10083. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10084. }
  10085. /* ALPN select callback, force failure */
  10086. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  10087. unsigned char *outlen, const unsigned char *in,
  10088. unsigned int inlen, void *arg)
  10089. {
  10090. (void)ssl;
  10091. (void)out;
  10092. (void)outlen;
  10093. (void)in;
  10094. (void)inlen;
  10095. (void)arg;
  10096. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10097. }
  10098. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  10099. {
  10100. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  10101. }
  10102. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  10103. {
  10104. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  10105. }
  10106. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  10107. static int test_wolfSSL_UseALPN_connection(void)
  10108. {
  10109. int res = TEST_SKIPPED;
  10110. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10111. callback_functions client_cb;
  10112. callback_functions server_cb;
  10113. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10114. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10115. client_cb.method = wolfSSLv23_client_method;
  10116. server_cb.method = wolfSSLv23_server_method;
  10117. client_cb.devId = testDevId;
  10118. server_cb.devId = testDevId;
  10119. /* success case same list */
  10120. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10121. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  10122. test_wolfSSL_client_server(&client_cb, &server_cb);
  10123. /* success case only one for server */
  10124. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10125. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  10126. test_wolfSSL_client_server(&client_cb, &server_cb);
  10127. /* success case only one for client */
  10128. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  10129. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  10130. test_wolfSSL_client_server(&client_cb, &server_cb);
  10131. /* success case none for client */
  10132. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10133. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  10134. test_wolfSSL_client_server(&client_cb, &server_cb);
  10135. /* success case mismatch behavior but option 'continue' set */
  10136. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  10137. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  10138. test_wolfSSL_client_server(&client_cb, &server_cb);
  10139. /* success case read protocol send by client */
  10140. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10141. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  10142. test_wolfSSL_client_server(&client_cb, &server_cb);
  10143. /* mismatch behavior with same list
  10144. * the first and only this one must be taken */
  10145. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10146. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  10147. test_wolfSSL_client_server(&client_cb, &server_cb);
  10148. /* default mismatch behavior */
  10149. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10150. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10151. test_wolfSSL_client_server(&client_cb, &server_cb);
  10152. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10153. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10154. /* WOLFSSL-level ALPN select callback tests */
  10155. /* Callback: success (one protocol, spdy/2) */
  10156. client_cb.ctx_ready = NULL;
  10157. client_cb.ssl_ready = use_ALPN_one;
  10158. client_cb.on_result = verify_ALPN_matching_spdy2;
  10159. server_cb.ctx_ready = NULL;
  10160. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  10161. server_cb.on_result = verify_ALPN_matching_spdy2;
  10162. test_wolfSSL_client_server(&client_cb, &server_cb);
  10163. /* Callback: failure (one client protocol, spdy/2) */
  10164. client_cb.ctx_ready = NULL;
  10165. client_cb.ssl_ready = use_ALPN_one;
  10166. client_cb.on_result = NULL;
  10167. server_cb.ctx_ready = NULL;
  10168. server_cb.ssl_ready = use_ALPN_failure_callback;
  10169. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10170. test_wolfSSL_client_server(&client_cb, &server_cb);
  10171. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  10172. res = TEST_RES_CHECK(1);
  10173. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10174. return res;
  10175. }
  10176. static int test_wolfSSL_UseALPN_params(void)
  10177. {
  10178. EXPECT_DECLS;
  10179. #ifndef NO_WOLFSSL_CLIENT
  10180. /* "http/1.1" */
  10181. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10182. /* "spdy/1" */
  10183. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  10184. /* "spdy/2" */
  10185. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10186. /* "spdy/3" */
  10187. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10188. char buff[256];
  10189. word32 idx;
  10190. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10191. WOLFSSL *ssl = wolfSSL_new(ctx);
  10192. ExpectNotNull(ctx);
  10193. ExpectNotNull(ssl);
  10194. /* error cases */
  10195. ExpectIntNE(WOLFSSL_SUCCESS,
  10196. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  10197. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10198. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  10199. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10200. /* success case */
  10201. /* http1 only */
  10202. ExpectIntEQ(WOLFSSL_SUCCESS,
  10203. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  10204. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10205. /* http1, spdy1 */
  10206. XMEMCPY(buff, http1, sizeof(http1));
  10207. idx = sizeof(http1);
  10208. buff[idx++] = ',';
  10209. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10210. idx += sizeof(spdy1);
  10211. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10212. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10213. /* http1, spdy2, spdy1 */
  10214. XMEMCPY(buff, http1, sizeof(http1));
  10215. idx = sizeof(http1);
  10216. buff[idx++] = ',';
  10217. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10218. idx += sizeof(spdy2);
  10219. buff[idx++] = ',';
  10220. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10221. idx += sizeof(spdy1);
  10222. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10223. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10224. /* spdy3, http1, spdy2, spdy1 */
  10225. XMEMCPY(buff, spdy3, sizeof(spdy3));
  10226. idx = sizeof(spdy3);
  10227. buff[idx++] = ',';
  10228. XMEMCPY(buff+idx, http1, sizeof(http1));
  10229. idx += sizeof(http1);
  10230. buff[idx++] = ',';
  10231. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10232. idx += sizeof(spdy2);
  10233. buff[idx++] = ',';
  10234. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10235. idx += sizeof(spdy1);
  10236. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10237. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10238. wolfSSL_free(ssl);
  10239. wolfSSL_CTX_free(ctx);
  10240. #endif
  10241. return EXPECT_RESULT();
  10242. }
  10243. #endif /* HAVE_ALPN */
  10244. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  10245. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  10246. {
  10247. unsigned char p[] = {
  10248. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10249. 6, 's', 'p', 'd', 'y', '/', '2',
  10250. 6, 's', 'p', 'd', 'y', '/', '1',
  10251. };
  10252. unsigned char p_len = sizeof(p);
  10253. int ret;
  10254. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  10255. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10256. AssertIntEQ(ret, 0);
  10257. #else
  10258. AssertIntEQ(ret, SSL_SUCCESS);
  10259. #endif
  10260. }
  10261. static void set_alpn_protos(SSL* ssl)
  10262. {
  10263. unsigned char p[] = {
  10264. 6, 's', 'p', 'd', 'y', '/', '3',
  10265. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10266. 6, 's', 'p', 'd', 'y', '/', '2',
  10267. 6, 's', 'p', 'd', 'y', '/', '1',
  10268. };
  10269. unsigned char p_len = sizeof(p);
  10270. int ret;
  10271. ret = SSL_set_alpn_protos(ssl, p, p_len);
  10272. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10273. AssertIntEQ(ret, 0);
  10274. #else
  10275. AssertIntEQ(ret, SSL_SUCCESS);
  10276. #endif
  10277. }
  10278. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  10279. {
  10280. /* "spdy/3" */
  10281. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10282. const unsigned char *proto;
  10283. unsigned int protoSz = 0;
  10284. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10285. /* check value */
  10286. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10287. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10288. }
  10289. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  10290. {
  10291. /* "http/1.1" */
  10292. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10293. const unsigned char *proto;
  10294. unsigned int protoSz = 0;
  10295. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10296. /* check value */
  10297. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10298. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10299. }
  10300. static int test_wolfSSL_set_alpn_protos(void)
  10301. {
  10302. int res = TEST_SKIPPED;
  10303. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10304. callback_functions client_cb;
  10305. callback_functions server_cb;
  10306. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10307. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10308. client_cb.method = wolfSSLv23_client_method;
  10309. server_cb.method = wolfSSLv23_server_method;
  10310. client_cb.devId = testDevId;
  10311. server_cb.devId = testDevId;
  10312. /* use CTX_alpn_protos */
  10313. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10314. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  10315. test_wolfSSL_client_server(&client_cb, &server_cb);
  10316. /* use set_alpn_protos */
  10317. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  10318. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  10319. test_wolfSSL_client_server(&client_cb, &server_cb);
  10320. res = TEST_SUCCESS;
  10321. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10322. return res;
  10323. }
  10324. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  10325. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  10326. {
  10327. EXPECT_DECLS;
  10328. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  10329. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10330. WOLFSSL *ssl = wolfSSL_new(ctx);
  10331. ExpectNotNull(ctx);
  10332. ExpectNotNull(ssl);
  10333. /* error cases */
  10334. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  10335. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  10336. /* success cases */
  10337. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  10338. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  10339. wolfSSL_free(ssl);
  10340. wolfSSL_CTX_free(ctx);
  10341. #endif
  10342. return EXPECT_RESULT();
  10343. }
  10344. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  10345. {
  10346. EXPECT_DECLS;
  10347. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  10348. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10349. WOLFSSL *ssl = wolfSSL_new(ctx);
  10350. ExpectNotNull(ctx);
  10351. ExpectNotNull(ssl);
  10352. /* error cases */
  10353. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  10354. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  10355. /* success cases */
  10356. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  10357. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  10358. wolfSSL_free(ssl);
  10359. wolfSSL_CTX_free(ctx);
  10360. #endif
  10361. return EXPECT_RESULT();
  10362. }
  10363. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  10364. static int test_wolfSSL_SCR_Reconnect(void)
  10365. {
  10366. EXPECT_DECLS;
  10367. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  10368. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  10369. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  10370. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  10371. struct test_memio_ctx test_ctx;
  10372. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  10373. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  10374. byte data;
  10375. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  10376. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  10377. test_ctx.s_ciphers =
  10378. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  10379. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10380. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10381. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  10382. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  10383. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  10384. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  10385. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10386. /* WOLFSSL_FATAL_ERROR since it will block */
  10387. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  10388. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10389. WOLFSSL_ERROR_WANT_READ);
  10390. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  10391. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10392. WOLFSSL_ERROR_WANT_READ);
  10393. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10394. wolfSSL_free(ssl_c);
  10395. ssl_c = NULL;
  10396. wolfSSL_free(ssl_s);
  10397. ssl_s = NULL;
  10398. wolfSSL_CTX_free(ctx_c);
  10399. ctx_c = NULL;
  10400. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  10401. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10402. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10403. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10404. wolfSSL_free(ssl_s);
  10405. wolfSSL_free(ssl_c);
  10406. wolfSSL_CTX_free(ctx_s);
  10407. wolfSSL_CTX_free(ctx_c);
  10408. #endif
  10409. return EXPECT_RESULT();
  10410. }
  10411. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  10412. (!defined(NO_RSA) || defined(HAVE_ECC))
  10413. /* Called when writing. */
  10414. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10415. {
  10416. (void)ssl;
  10417. (void)buf;
  10418. (void)sz;
  10419. (void)ctx;
  10420. /* Force error return from wolfSSL_accept_TLSv13(). */
  10421. return WANT_WRITE;
  10422. }
  10423. /* Called when reading. */
  10424. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10425. {
  10426. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  10427. int len = (int)msg->length;
  10428. (void)ssl;
  10429. (void)sz;
  10430. /* Pass back as much of message as will fit in buffer. */
  10431. if (len > sz)
  10432. len = sz;
  10433. XMEMCPY(buf, msg->buffer, len);
  10434. /* Move over returned data. */
  10435. msg->buffer += len;
  10436. msg->length -= len;
  10437. /* Amount actually copied. */
  10438. return len;
  10439. }
  10440. #endif
  10441. /* Test the detection of duplicate known TLS extensions.
  10442. * Specifically in a ClientHello.
  10443. */
  10444. static int test_tls_ext_duplicate(void)
  10445. {
  10446. EXPECT_DECLS;
  10447. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  10448. !defined(NO_FILESYSTEM)
  10449. const unsigned char clientHelloDupTlsExt[] = {
  10450. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  10451. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  10452. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  10453. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  10454. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  10455. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  10456. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  10457. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  10458. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10459. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  10460. 0x00, 0x9e, 0x01, 0x00,
  10461. /* Extensions - duplicate signature algorithms. */
  10462. 0x00, 0x19, 0x00, 0x0d,
  10463. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  10464. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  10465. /* Supported Versions extension for TLS 1.3. */
  10466. 0x00, 0x2b,
  10467. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  10468. };
  10469. WOLFSSL_BUFFER_INFO msg;
  10470. const char* testCertFile;
  10471. const char* testKeyFile;
  10472. WOLFSSL_CTX *ctx = NULL;
  10473. WOLFSSL *ssl = NULL;
  10474. #ifndef NO_RSA
  10475. testCertFile = svrCertFile;
  10476. testKeyFile = svrKeyFile;
  10477. #elif defined(HAVE_ECC)
  10478. testCertFile = eccCertFile;
  10479. testKeyFile = eccKeyFile;
  10480. #endif
  10481. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10482. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  10483. WOLFSSL_FILETYPE_PEM));
  10484. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  10485. WOLFSSL_FILETYPE_PEM));
  10486. /* Read from 'msg'. */
  10487. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  10488. /* No where to send to - dummy sender. */
  10489. wolfSSL_SetIOSend(ctx, DummySend);
  10490. ssl = wolfSSL_new(ctx);
  10491. ExpectNotNull(ssl);
  10492. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  10493. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10494. wolfSSL_SetIOReadCtx(ssl, &msg);
  10495. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10496. /* can return duplicate ext error or socket error if the peer closed down
  10497. * while sending alert */
  10498. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10499. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10500. }
  10501. wolfSSL_free(ssl);
  10502. wolfSSL_CTX_free(ctx);
  10503. #endif
  10504. return EXPECT_RESULT();
  10505. }
  10506. /*----------------------------------------------------------------------------*
  10507. | X509 Tests
  10508. *----------------------------------------------------------------------------*/
  10509. static int test_wolfSSL_X509_NAME_get_entry(void)
  10510. {
  10511. EXPECT_DECLS;
  10512. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10513. #if defined(OPENSSL_ALL) || \
  10514. (defined(OPENSSL_EXTRA) && \
  10515. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10516. /* use openssl like name to test mapping */
  10517. X509_NAME_ENTRY* ne = NULL;
  10518. X509_NAME* name = NULL;
  10519. X509* x509 = NULL;
  10520. #ifndef NO_FILESYSTEM
  10521. ASN1_STRING* asn = NULL;
  10522. char* subCN = NULL;
  10523. #endif
  10524. int idx = 0;
  10525. ASN1_OBJECT *object = NULL;
  10526. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10527. defined(WOLFSSL_NGINX)
  10528. #ifndef NO_BIO
  10529. BIO* bio = NULL;
  10530. #endif
  10531. #endif
  10532. #ifndef NO_FILESYSTEM
  10533. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10534. WOLFSSL_FILETYPE_PEM));
  10535. ExpectNotNull(name = X509_get_subject_name(x509));
  10536. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10537. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10538. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10539. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10540. wolfSSL_FreeX509(x509);
  10541. x509 = NULL;
  10542. #endif
  10543. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10544. WOLFSSL_FILETYPE_PEM));
  10545. ExpectNotNull(name = X509_get_subject_name(x509));
  10546. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10547. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10548. defined(WOLFSSL_NGINX)
  10549. #ifndef NO_BIO
  10550. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10551. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10552. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10553. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10554. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10555. BIO_free(bio);
  10556. #endif
  10557. #endif
  10558. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10559. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10560. wolfSSL_FreeX509(x509);
  10561. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10562. #endif /* !NO_CERTS && !NO_RSA */
  10563. return EXPECT_RESULT();
  10564. }
  10565. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10566. static int test_wolfSSL_PKCS12(void)
  10567. {
  10568. EXPECT_DECLS;
  10569. /* .p12 file is encrypted with DES3 */
  10570. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10571. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10572. * Password Key
  10573. */
  10574. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10575. !defined(NO_STDIO_FILESYSTEM) && \
  10576. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10577. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10578. byte buf[6000];
  10579. char file[] = "./certs/test-servercert.p12";
  10580. char order[] = "./certs/ecc-rsa-server.p12";
  10581. #ifdef WC_RC2
  10582. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10583. #endif
  10584. char pass[] = "a password";
  10585. const char goodPsw[] = "wolfSSL test";
  10586. const char badPsw[] = "bad";
  10587. #ifdef HAVE_ECC
  10588. WOLFSSL_X509_NAME *subject = NULL;
  10589. WOLFSSL_X509 *x509 = NULL;
  10590. #endif
  10591. XFILE f = XBADFILE;
  10592. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10593. WOLFSSL_BIO *bio = NULL;
  10594. WOLFSSL_EVP_PKEY *pkey = NULL;
  10595. WC_PKCS12 *pkcs12 = NULL;
  10596. WC_PKCS12 *pkcs12_2 = NULL;
  10597. WOLFSSL_X509 *cert = NULL;
  10598. WOLFSSL_X509 *tmp = NULL;
  10599. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10600. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10601. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10602. WOLFSSL_CTX *ctx = NULL;
  10603. WOLFSSL *ssl = NULL;
  10604. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10605. #endif
  10606. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10607. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10608. if (f != XBADFILE) {
  10609. XFCLOSE(f);
  10610. f = XBADFILE;
  10611. }
  10612. goodPswLen = (int)XSTRLEN(goodPsw);
  10613. badPswLen = (int)XSTRLEN(badPsw);
  10614. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10615. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10616. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10617. ExpectNotNull(pkcs12);
  10618. BIO_free(bio);
  10619. bio = NULL;
  10620. /* check verify MAC directly */
  10621. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10622. /* check verify MAC fail case directly */
  10623. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10624. /* check verify MAC fail case */
  10625. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10626. ExpectNull(pkey);
  10627. ExpectNull(cert);
  10628. /* check parse with no extra certs kept */
  10629. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10630. 1);
  10631. ExpectNotNull(pkey);
  10632. ExpectNotNull(cert);
  10633. wolfSSL_EVP_PKEY_free(pkey);
  10634. pkey = NULL;
  10635. wolfSSL_X509_free(cert);
  10636. cert = NULL;
  10637. /* check parse with extra certs kept */
  10638. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10639. 1);
  10640. ExpectNotNull(pkey);
  10641. ExpectNotNull(cert);
  10642. ExpectNotNull(ca);
  10643. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10644. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10645. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10646. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10647. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10648. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10649. #else
  10650. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10651. #endif
  10652. /* Copy stack structure */
  10653. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10654. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10655. /* CTX now owns the tmp_ca stack structure */
  10656. tmp_ca = NULL;
  10657. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10658. ExpectNotNull(tmp_ca);
  10659. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10660. /* Check that the main cert is also set */
  10661. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10662. ExpectNotNull(ssl = SSL_new(ctx));
  10663. ExpectNotNull(SSL_get_certificate(ssl));
  10664. SSL_free(ssl);
  10665. SSL_CTX_free(ctx);
  10666. ctx = NULL;
  10667. #endif
  10668. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  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. EVP_PKEY_free(pkey);
  10676. pkey = NULL;
  10677. X509_free(cert);
  10678. cert = NULL;
  10679. sk_X509_pop_free(ca, X509_free);
  10680. ca = NULL;
  10681. /* check PKCS12_create */
  10682. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  10683. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10684. SSL_SUCCESS);
  10685. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10686. -1, -1, 100, -1, 0)));
  10687. EVP_PKEY_free(pkey);
  10688. pkey = NULL;
  10689. X509_free(cert);
  10690. cert = NULL;
  10691. sk_X509_pop_free(ca, NULL);
  10692. ca = NULL;
  10693. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10694. SSL_SUCCESS);
  10695. PKCS12_free(pkcs12_2);
  10696. pkcs12_2 = NULL;
  10697. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10698. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10699. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10700. 2000, 1, 0)));
  10701. EVP_PKEY_free(pkey);
  10702. pkey = NULL;
  10703. X509_free(cert);
  10704. cert = NULL;
  10705. sk_X509_pop_free(ca, NULL);
  10706. ca = NULL;
  10707. /* convert to DER then back and parse */
  10708. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10709. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  10710. PKCS12_free(pkcs12_2);
  10711. pkcs12_2 = NULL;
  10712. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  10713. BIO_free(bio);
  10714. bio = NULL;
  10715. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10716. SSL_SUCCESS);
  10717. /* should be 2 other certs on stack */
  10718. ExpectNotNull(tmp = sk_X509_pop(ca));
  10719. X509_free(tmp);
  10720. ExpectNotNull(tmp = sk_X509_pop(ca));
  10721. X509_free(tmp);
  10722. ExpectNull(sk_X509_pop(ca));
  10723. #ifndef NO_RC4
  10724. PKCS12_free(pkcs12_2);
  10725. pkcs12_2 = NULL;
  10726. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  10727. NID_pbe_WithSHA1And128BitRC4,
  10728. NID_pbe_WithSHA1And128BitRC4,
  10729. 2000, 1, 0)));
  10730. EVP_PKEY_free(pkey);
  10731. pkey = NULL;
  10732. X509_free(cert);
  10733. cert = NULL;
  10734. sk_X509_pop_free(ca, NULL);
  10735. ca = NULL;
  10736. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10737. SSL_SUCCESS);
  10738. #endif /* NO_RC4 */
  10739. EVP_PKEY_free(pkey);
  10740. pkey = NULL;
  10741. X509_free(cert);
  10742. cert = NULL;
  10743. PKCS12_free(pkcs12);
  10744. pkcs12 = NULL;
  10745. PKCS12_free(pkcs12_2);
  10746. pkcs12_2 = NULL;
  10747. sk_X509_pop_free(ca, NULL);
  10748. ca = NULL;
  10749. #ifdef HAVE_ECC
  10750. /* test order of parsing */
  10751. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  10752. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10753. if (f != XBADFILE) {
  10754. XFCLOSE(f);
  10755. f = XBADFILE;
  10756. }
  10757. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10758. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10759. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  10760. WOLFSSL_SUCCESS);
  10761. /* check use of pkey after parse */
  10762. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10763. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10764. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10765. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10766. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10767. #else
  10768. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10769. #endif
  10770. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  10771. SSL_CTX_free(ctx);
  10772. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10773. #endif
  10774. ExpectNotNull(pkey);
  10775. ExpectNotNull(cert);
  10776. ExpectNotNull(ca);
  10777. /* compare subject lines of certificates */
  10778. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10779. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  10780. SSL_FILETYPE_PEM));
  10781. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10782. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10783. X509_free(x509);
  10784. x509 = NULL;
  10785. /* test expected fail case */
  10786. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10787. SSL_FILETYPE_PEM));
  10788. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10789. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10790. X509_free(x509);
  10791. x509 = NULL;
  10792. X509_free(cert);
  10793. cert = NULL;
  10794. /* get subject line from ca stack */
  10795. ExpectNotNull(cert = sk_X509_pop(ca));
  10796. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10797. /* compare subject from certificate in ca to expected */
  10798. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10799. SSL_FILETYPE_PEM));
  10800. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10801. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10802. /* modify case and compare subject from certificate in ca to expected.
  10803. * The first bit of the name is:
  10804. * /C=US/ST=Washington
  10805. * So we'll change subject->name[1] to 'c' (lower case) */
  10806. if (subject != NULL) {
  10807. subject->name[1] = 'c';
  10808. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10809. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10810. }
  10811. EVP_PKEY_free(pkey);
  10812. pkey = NULL;
  10813. X509_free(x509);
  10814. x509 = NULL;
  10815. X509_free(cert);
  10816. cert = NULL;
  10817. BIO_free(bio);
  10818. bio = NULL;
  10819. PKCS12_free(pkcs12);
  10820. pkcs12 = NULL;
  10821. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10822. ca = NULL;
  10823. /* test order of parsing */
  10824. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10825. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10826. if (f != XBADFILE) {
  10827. XFCLOSE(f);
  10828. f = XBADFILE;
  10829. }
  10830. /* check verify MAC fail case */
  10831. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10832. ExpectNull(pkey);
  10833. ExpectNull(cert);
  10834. /* check parse with no extra certs kept */
  10835. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10836. 1);
  10837. ExpectNotNull(pkey);
  10838. ExpectNotNull(cert);
  10839. wolfSSL_EVP_PKEY_free(pkey);
  10840. pkey = NULL;
  10841. wolfSSL_X509_free(cert);
  10842. cert = NULL;
  10843. /* check parse with extra certs kept */
  10844. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10845. 1);
  10846. ExpectNotNull(pkey);
  10847. ExpectNotNull(cert);
  10848. ExpectNotNull(ca);
  10849. wolfSSL_EVP_PKEY_free(pkey);
  10850. pkey = NULL;
  10851. wolfSSL_X509_free(cert);
  10852. cert = NULL;
  10853. sk_X509_pop_free(ca, NULL);
  10854. ca = NULL;
  10855. PKCS12_free(pkcs12);
  10856. pkcs12 = NULL;
  10857. #endif /* HAVE_ECC */
  10858. #ifdef WC_RC2
  10859. /* test PKCS#12 with RC2 encryption */
  10860. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10861. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10862. if (f != XBADFILE) {
  10863. XFCLOSE(f);
  10864. f = XBADFILE;
  10865. }
  10866. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10867. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10868. /* check verify MAC fail case */
  10869. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10870. ExpectNull(pkey);
  10871. ExpectNull(cert);
  10872. /* check parse with not extra certs kept */
  10873. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10874. WOLFSSL_SUCCESS);
  10875. ExpectNotNull(pkey);
  10876. ExpectNotNull(cert);
  10877. wolfSSL_EVP_PKEY_free(pkey);
  10878. pkey = NULL;
  10879. wolfSSL_X509_free(cert);
  10880. cert = NULL;
  10881. /* check parse with extra certs kept */
  10882. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10883. WOLFSSL_SUCCESS);
  10884. ExpectNotNull(pkey);
  10885. ExpectNotNull(cert);
  10886. ExpectNotNull(ca);
  10887. wolfSSL_EVP_PKEY_free(pkey);
  10888. wolfSSL_X509_free(cert);
  10889. sk_X509_pop_free(ca, NULL);
  10890. BIO_free(bio);
  10891. bio = NULL;
  10892. PKCS12_free(pkcs12);
  10893. pkcs12 = NULL;
  10894. #endif /* WC_RC2 */
  10895. /* Test i2d_PKCS12_bio */
  10896. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10897. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10898. if (f != XBADFILE)
  10899. XFCLOSE(f);
  10900. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10901. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10902. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10903. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10904. PKCS12_free(pkcs12);
  10905. BIO_free(bio);
  10906. (void)order;
  10907. #endif /* OPENSSL_EXTRA */
  10908. #endif /* HAVE_FIPS */
  10909. return EXPECT_RESULT();
  10910. }
  10911. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10912. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10913. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10914. #define TEST_PKCS8_ENC
  10915. #endif
  10916. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10917. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10918. /* used to keep track if FailTestCallback was called */
  10919. static int failTestCallbackCalled = 0;
  10920. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10921. {
  10922. (void)passwd;
  10923. (void)sz;
  10924. (void)rw;
  10925. (void)userdata;
  10926. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10927. failTestCallbackCalled = 1;
  10928. return -1;
  10929. }
  10930. #endif
  10931. static int test_wolfSSL_no_password_cb(void)
  10932. {
  10933. EXPECT_DECLS;
  10934. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10935. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10936. WOLFSSL_CTX* ctx = NULL;
  10937. byte buff[FOURK_BUF];
  10938. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10939. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10940. XFILE f = XBADFILE;
  10941. int bytes = 0;
  10942. #ifndef NO_WOLFSSL_CLIENT
  10943. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10944. #else
  10945. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10946. #endif
  10947. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10948. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10949. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10950. if (f != XBADFILE) {
  10951. XFCLOSE(f);
  10952. f = XBADFILE;
  10953. }
  10954. ExpectIntLE(bytes, sizeof(buff));
  10955. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10956. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10957. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10958. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10959. if (f != XBADFILE)
  10960. XFCLOSE(f);
  10961. ExpectIntLE(bytes, sizeof(buff));
  10962. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10963. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10964. wolfSSL_CTX_free(ctx);
  10965. /* Password callback should not be called by default */
  10966. ExpectIntEQ(failTestCallbackCalled, 0);
  10967. #endif
  10968. return EXPECT_RESULT();
  10969. }
  10970. #ifdef TEST_PKCS8_ENC
  10971. /* for PKCS8 test case */
  10972. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10973. {
  10974. int flag = 0;
  10975. (void)rw;
  10976. if (userdata != NULL) {
  10977. flag = *((int*)userdata); /* user set data */
  10978. }
  10979. switch (flag) {
  10980. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10981. * can be anything the user wishes to be passed to the callback
  10982. * associated with the WOLFSSL_CTX */
  10983. XSTRNCPY(passwd, "yassl123", sz);
  10984. return 8;
  10985. default:
  10986. return BAD_FUNC_ARG;
  10987. }
  10988. }
  10989. #endif /* TEST_PKCS8_ENC */
  10990. /* Testing functions dealing with PKCS8 */
  10991. static int test_wolfSSL_PKCS8(void)
  10992. {
  10993. EXPECT_DECLS;
  10994. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10995. !defined(WOLFCRYPT_ONLY)
  10996. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10997. byte buff[FOURK_BUF];
  10998. byte der[FOURK_BUF];
  10999. #ifndef NO_RSA
  11000. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  11001. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  11002. #endif
  11003. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  11004. #ifdef HAVE_ECC
  11005. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  11006. #endif
  11007. XFILE f = XBADFILE;
  11008. int bytes = 0;
  11009. WOLFSSL_CTX* ctx = NULL;
  11010. #if defined(HAVE_ECC) && !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  11011. int ret;
  11012. ecc_key key;
  11013. word32 x = 0;
  11014. #endif
  11015. #ifdef TEST_PKCS8_ENC
  11016. #if !defined(NO_RSA) && !defined(NO_SHA)
  11017. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  11018. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  11019. #endif
  11020. #if defined(HAVE_ECC) && !defined(NO_SHA)
  11021. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  11022. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  11023. #endif
  11024. int flag;
  11025. #endif
  11026. (void)der;
  11027. #ifndef NO_WOLFSSL_CLIENT
  11028. #ifndef WOLFSSL_NO_TLS12
  11029. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11030. #else
  11031. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11032. #endif
  11033. #else
  11034. #ifndef WOLFSSL_NO_TLS12
  11035. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11036. #else
  11037. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11038. #endif
  11039. #endif
  11040. #ifdef TEST_PKCS8_ENC
  11041. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  11042. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  11043. flag = 1; /* used by password callback as return code */
  11044. #if !defined(NO_RSA) && !defined(NO_SHA)
  11045. /* test loading PEM PKCS8 encrypted file */
  11046. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  11047. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11048. if (f != XBADFILE) {
  11049. XFCLOSE(f);
  11050. f = XBADFILE;
  11051. }
  11052. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11053. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11054. /* this next case should fail because of password callback return code */
  11055. flag = 0; /* used by password callback as return code */
  11056. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11057. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11058. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11059. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11060. "yassl123"), 0);
  11061. /* test that error value is returned with a bad password */
  11062. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11063. "bad"), 0);
  11064. /* test loading PEM PKCS8 encrypted file */
  11065. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  11066. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11067. if (f != XBADFILE) {
  11068. XFCLOSE(f);
  11069. f = XBADFILE;
  11070. }
  11071. flag = 1; /* used by password callback as return code */
  11072. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11073. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11074. /* this next case should fail because of password callback return code */
  11075. flag = 0; /* used by password callback as return code */
  11076. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11077. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11078. #endif /* !NO_RSA && !NO_SHA */
  11079. #if defined(HAVE_ECC) && !defined(NO_SHA)
  11080. /* test loading PEM PKCS8 encrypted ECC Key file */
  11081. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "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. flag = 1; /* used by password callback as return code */
  11088. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11089. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11090. /* this next case should fail because of password callback return code */
  11091. flag = 0; /* used by password callback as return code */
  11092. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11093. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11094. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11095. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11096. "yassl123"), 0);
  11097. /* test that error value is returned with a bad password */
  11098. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11099. "bad"), 0);
  11100. /* test loading DER PKCS8 encrypted ECC Key file */
  11101. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  11102. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11103. if (f != XBADFILE) {
  11104. XFCLOSE(f);
  11105. f = XBADFILE;
  11106. }
  11107. flag = 1; /* used by password callback as return code */
  11108. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11109. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11110. /* this next case should fail because of password callback return code */
  11111. flag = 0; /* used by password callback as return code */
  11112. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11113. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11114. /* leave flag as "okay" */
  11115. flag = 1;
  11116. #endif /* HAVE_ECC && !NO_SHA */
  11117. #endif /* TEST_PKCS8_ENC */
  11118. #ifndef NO_RSA
  11119. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  11120. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  11121. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11122. if (f != XBADFILE) {
  11123. XFCLOSE(f);
  11124. f = XBADFILE;
  11125. }
  11126. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11127. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11128. /* test loading PEM PKCS8 private key file (not encrypted) */
  11129. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  11130. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11131. if (f != XBADFILE) {
  11132. XFCLOSE(f);
  11133. f = XBADFILE;
  11134. }
  11135. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11136. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11137. #endif /* !NO_RSA */
  11138. /* Test PKCS8 PEM ECC key no crypt */
  11139. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  11140. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11141. if (f != XBADFILE) {
  11142. XFCLOSE(f);
  11143. f = XBADFILE;
  11144. }
  11145. #ifdef HAVE_ECC
  11146. /* Test PKCS8 PEM ECC key no crypt */
  11147. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11148. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11149. #if !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  11150. /* decrypt PKCS8 PEM to key in DER format */
  11151. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  11152. (word32)sizeof(der), NULL)), 0);
  11153. ret = wc_ecc_init(&key);
  11154. if (ret == 0) {
  11155. ret = wc_EccPrivateKeyDecode(der, &x, &key, (word32)bytes);
  11156. wc_ecc_free(&key);
  11157. }
  11158. ExpectIntEQ(ret, 0);
  11159. #endif
  11160. /* Test PKCS8 DER ECC key no crypt */
  11161. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11162. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11163. if (f != XBADFILE)
  11164. XFCLOSE(f);
  11165. /* Test using a PKCS8 ECC PEM */
  11166. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11167. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11168. #else
  11169. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  11170. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  11171. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  11172. #endif /* HAVE_ECC */
  11173. wolfSSL_CTX_free(ctx);
  11174. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11175. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  11176. return EXPECT_RESULT();
  11177. }
  11178. static int test_wolfSSL_PKCS8_ED25519(void)
  11179. {
  11180. EXPECT_DECLS;
  11181. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11182. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  11183. defined(HAVE_ED25519_KEY_IMPORT)
  11184. const byte encPrivKey[] = \
  11185. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11186. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  11187. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  11188. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  11189. "2L9W4v7swXkV+X+a1ww=\n"
  11190. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11191. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11192. byte der[FOURK_BUF];
  11193. WOLFSSL_CTX* ctx = NULL;
  11194. int bytes;
  11195. XMEMSET(der, 0, sizeof(der));
  11196. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11197. (word32)sizeof(der), password)), 0);
  11198. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11199. #ifndef NO_WOLFSSL_SERVER
  11200. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11201. #else
  11202. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11203. #endif
  11204. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11205. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11206. wolfSSL_CTX_free(ctx);
  11207. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11208. #endif
  11209. return EXPECT_RESULT();
  11210. }
  11211. static int test_wolfSSL_PKCS8_ED448(void)
  11212. {
  11213. EXPECT_DECLS;
  11214. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11215. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  11216. defined(HAVE_ED448_KEY_IMPORT)
  11217. const byte encPrivKey[] = \
  11218. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11219. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  11220. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  11221. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  11222. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  11223. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11224. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11225. byte der[FOURK_BUF];
  11226. WOLFSSL_CTX* ctx = NULL;
  11227. int bytes;
  11228. XMEMSET(der, 0, sizeof(der));
  11229. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11230. (word32)sizeof(der), password)), 0);
  11231. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11232. #ifndef NO_WOLFSSL_SERVER
  11233. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11234. #else
  11235. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11236. #endif
  11237. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11238. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11239. wolfSSL_CTX_free(ctx);
  11240. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11241. #endif
  11242. return EXPECT_RESULT();
  11243. }
  11244. /* Testing functions dealing with PKCS5 */
  11245. static int test_wolfSSL_PKCS5(void)
  11246. {
  11247. EXPECT_DECLS;
  11248. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  11249. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  11250. const char* passwd = "myfipsPa$$W0rd";
  11251. #else
  11252. const char *passwd = "pass1234";
  11253. #endif
  11254. const unsigned char *salt = (unsigned char *)"salt1234";
  11255. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  11256. DYNAMIC_TYPE_TMP_BUFFER);
  11257. int ret = 0;
  11258. ExpectNotNull(out);
  11259. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  11260. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  11261. WOLFSSL_SUCCESS);
  11262. #ifdef WOLFSSL_SHA512
  11263. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  11264. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  11265. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  11266. #endif
  11267. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11268. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  11269. return EXPECT_RESULT();
  11270. }
  11271. /* test parsing URI from certificate */
  11272. static int test_wolfSSL_URI(void)
  11273. {
  11274. EXPECT_DECLS;
  11275. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11276. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  11277. defined(OPENSSL_EXTRA))
  11278. WOLFSSL_X509* x509 = NULL;
  11279. const char uri[] = "./certs/client-uri-cert.pem";
  11280. const char urn[] = "./certs/client-absolute-urn.pem";
  11281. const char badUri[] = "./certs/client-relative-uri.pem";
  11282. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  11283. WOLFSSL_FILETYPE_PEM));
  11284. wolfSSL_FreeX509(x509);
  11285. x509 = NULL;
  11286. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  11287. WOLFSSL_FILETYPE_PEM));
  11288. wolfSSL_FreeX509(x509);
  11289. x509 = NULL;
  11290. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  11291. && !defined(WOLFSSL_FPKI)
  11292. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11293. WOLFSSL_FILETYPE_PEM));
  11294. #else
  11295. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11296. WOLFSSL_FILETYPE_PEM));
  11297. #endif
  11298. wolfSSL_FreeX509(x509);
  11299. #endif
  11300. return EXPECT_RESULT();
  11301. }
  11302. static int test_wolfSSL_TBS(void)
  11303. {
  11304. EXPECT_DECLS;
  11305. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11306. && defined(OPENSSL_EXTRA)
  11307. WOLFSSL_X509* x509 = NULL;
  11308. const unsigned char* tbs;
  11309. int tbsSz;
  11310. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  11311. WOLFSSL_FILETYPE_PEM));
  11312. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  11313. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  11314. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  11315. ExpectIntEQ(tbsSz, 1003);
  11316. wolfSSL_FreeX509(x509);
  11317. #endif
  11318. return EXPECT_RESULT();
  11319. }
  11320. static int test_wolfSSL_X509_verify(void)
  11321. {
  11322. EXPECT_DECLS;
  11323. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  11324. defined(OPENSSL_EXTRA)
  11325. WOLFSSL_X509* ca = NULL;
  11326. WOLFSSL_X509* serv = NULL;
  11327. WOLFSSL_EVP_PKEY* pkey = NULL;
  11328. unsigned char buf[2048];
  11329. const unsigned char* pt = NULL;
  11330. int bufSz;
  11331. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  11332. WOLFSSL_FILETYPE_PEM));
  11333. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  11334. WOLFSSL_SUCCESS);
  11335. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  11336. WOLFSSL_SUCCESS);
  11337. ExpectIntEQ(bufSz, 294);
  11338. bufSz = 2048;
  11339. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  11340. WOLFSSL_SUCCESS);
  11341. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  11342. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  11343. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  11344. WOLFSSL_FILETYPE_PEM));
  11345. /* success case */
  11346. pt = buf;
  11347. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11348. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  11349. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  11350. wolfSSL_EVP_PKEY_free(pkey);
  11351. pkey = NULL;
  11352. /* fail case */
  11353. bufSz = 2048;
  11354. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  11355. WOLFSSL_SUCCESS);
  11356. pt = buf;
  11357. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11358. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  11359. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  11360. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  11361. wolfSSL_EVP_PKEY_free(pkey);
  11362. wolfSSL_FreeX509(ca);
  11363. wolfSSL_FreeX509(serv);
  11364. #endif
  11365. return EXPECT_RESULT();
  11366. }
  11367. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11368. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11369. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11370. /* create certificate with version 2 */
  11371. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  11372. {
  11373. EXPECT_DECLS;
  11374. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  11375. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  11376. unsigned char *der = NULL, *key = NULL, *pt;
  11377. char *header = NULL, *name = NULL;
  11378. int derSz;
  11379. long keySz;
  11380. XFILE fp = XBADFILE;
  11381. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  11382. time_t t;
  11383. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  11384. WOLFSSL_FILETYPE_PEM));
  11385. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  11386. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  11387. WOLFSSL_SUCCESS);
  11388. if (fp != XBADFILE)
  11389. XFCLOSE(fp);
  11390. pt = key;
  11391. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  11392. (const unsigned char**)&pt, keySz));
  11393. /* create the version 2 certificate */
  11394. ExpectNotNull(x509v2 = X509_new());
  11395. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  11396. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  11397. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  11398. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  11399. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  11400. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  11401. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  11402. t = time(NULL);
  11403. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  11404. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  11405. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  11406. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  11407. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  11408. derSz = wolfSSL_i2d_X509(x509v2, &der);
  11409. ExpectIntGT(derSz, 0);
  11410. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  11411. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11412. /* TODO: Replace with API call */
  11413. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  11414. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11415. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11416. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11417. wolfSSL_X509_free(x509);
  11418. wolfSSL_X509_free(x509v2);
  11419. wolfSSL_EVP_PKEY_free(priv);
  11420. wolfSSL_EVP_PKEY_free(pub);
  11421. wolfSSL_ASN1_TIME_free(notBefore);
  11422. wolfSSL_ASN1_TIME_free(notAfter);
  11423. return EXPECT_RESULT();
  11424. }
  11425. /* override certificate version error */
  11426. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  11427. {
  11428. EXPECT_DECLS;
  11429. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11430. ExpectIntEQ(store->error, ASN_VERSION_E);
  11431. #else
  11432. ExpectIntEQ(store->error, 0);
  11433. #endif
  11434. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  11435. (void)preverify;
  11436. return EXPECT_RESULT() == TEST_SUCCESS;
  11437. }
  11438. /* set verify callback that will override bad certificate version */
  11439. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  11440. {
  11441. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  11442. return TEST_SUCCESS;
  11443. }
  11444. #endif
  11445. static int test_wolfSSL_X509_TLS_version_test_1(void)
  11446. {
  11447. EXPECT_DECLS;
  11448. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11449. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11450. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11451. test_ssl_cbf func_cb_client;
  11452. test_ssl_cbf func_cb_server;
  11453. /* test server rejects a client certificate that is not version 3 */
  11454. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11455. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11456. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11457. #ifndef WOLFSSL_NO_TLS12
  11458. func_cb_client.method = wolfTLSv1_2_client_method;
  11459. #else
  11460. func_cb_client.method = wolfTLSv1_3_client_method;
  11461. #endif
  11462. #ifndef WOLFSSL_NO_TLS12
  11463. func_cb_server.method = wolfTLSv1_2_server_method;
  11464. #else
  11465. func_cb_server.method = wolfTLSv1_3_server_method;
  11466. #endif
  11467. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11468. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11469. &func_cb_server, NULL), TEST_FAIL);
  11470. #else
  11471. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11472. &func_cb_server, NULL), TEST_SUCCESS);
  11473. #endif
  11474. #endif
  11475. return EXPECT_RESULT();
  11476. }
  11477. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11478. {
  11479. EXPECT_DECLS;
  11480. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11481. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11482. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11483. test_ssl_cbf func_cb_client;
  11484. test_ssl_cbf func_cb_server;
  11485. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11486. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11487. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11488. func_cb_server.ctx_ready = &test_set_override_x509;
  11489. #ifndef WOLFSSL_NO_TLS12
  11490. func_cb_client.method = wolfTLSv1_2_client_method;
  11491. #else
  11492. func_cb_client.method = wolfTLSv1_3_client_method;
  11493. #endif
  11494. #ifndef WOLFSSL_NO_TLS12
  11495. func_cb_server.method = wolfTLSv1_2_server_method;
  11496. #else
  11497. func_cb_server.method = wolfTLSv1_3_server_method;
  11498. #endif
  11499. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11500. &func_cb_server, NULL), TEST_SUCCESS);
  11501. #endif
  11502. return EXPECT_RESULT();
  11503. }
  11504. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11505. * version allowed.
  11506. * POST: 1 on success.
  11507. */
  11508. static int test_wolfSSL_CTX_SetMinVersion(void)
  11509. {
  11510. int res = TEST_SKIPPED;
  11511. #ifndef NO_WOLFSSL_CLIENT
  11512. int failFlag = WOLFSSL_SUCCESS;
  11513. WOLFSSL_CTX* ctx;
  11514. int itr;
  11515. #ifndef NO_OLD_TLS
  11516. const int versions[] = {
  11517. #ifdef WOLFSSL_ALLOW_TLSV10
  11518. WOLFSSL_TLSV1,
  11519. #endif
  11520. WOLFSSL_TLSV1_1,
  11521. WOLFSSL_TLSV1_2 };
  11522. #elif !defined(WOLFSSL_NO_TLS12)
  11523. const int versions[] = { WOLFSSL_TLSV1_2 };
  11524. #elif defined(WOLFSSL_TLS13)
  11525. const int versions[] = { WOLFSSL_TLSV1_3 };
  11526. #else
  11527. const int versions[0];
  11528. #endif
  11529. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11530. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11531. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11532. != WOLFSSL_SUCCESS) {
  11533. failFlag = WOLFSSL_FAILURE;
  11534. }
  11535. }
  11536. wolfSSL_CTX_free(ctx);
  11537. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11538. #endif
  11539. return res;
  11540. } /* END test_wolfSSL_CTX_SetMinVersion */
  11541. /*----------------------------------------------------------------------------*
  11542. | OCSP Stapling
  11543. *----------------------------------------------------------------------------*/
  11544. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11545. * need to contact the CA, lowering the cost of cert revocation checking.
  11546. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11547. * POST: 1 returned for success.
  11548. */
  11549. static int test_wolfSSL_UseOCSPStapling(void)
  11550. {
  11551. EXPECT_DECLS;
  11552. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11553. !defined(NO_WOLFSSL_CLIENT)
  11554. WOLFSSL_CTX* ctx = NULL;
  11555. WOLFSSL* ssl = NULL;
  11556. #ifndef NO_WOLFSSL_CLIENT
  11557. #ifndef WOLFSSL_NO_TLS12
  11558. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11559. #else
  11560. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11561. #endif
  11562. #else
  11563. #ifndef WOLFSSL_NO_TLS12
  11564. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11565. #else
  11566. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11567. #endif
  11568. #endif
  11569. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11570. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11571. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11572. #ifndef NO_WOLFSSL_CLIENT
  11573. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11574. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11575. #else
  11576. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11577. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11578. #endif
  11579. wolfSSL_free(ssl);
  11580. wolfSSL_CTX_free(ctx);
  11581. #endif
  11582. return EXPECT_RESULT();
  11583. } /* END test_wolfSSL_UseOCSPStapling */
  11584. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11585. * stapling eliminates the need to contact the CA and lowers cert revocation
  11586. * check.
  11587. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11588. */
  11589. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11590. {
  11591. EXPECT_DECLS;
  11592. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11593. !defined(NO_WOLFSSL_CLIENT)
  11594. WOLFSSL_CTX* ctx = NULL;
  11595. WOLFSSL* ssl = NULL;
  11596. #ifndef NO_WOLFSSL_CLIENT
  11597. #ifndef WOLFSSL_NO_TLS12
  11598. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11599. #else
  11600. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11601. #endif
  11602. #else
  11603. #ifndef WOLFSSL_NO_TLS12
  11604. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11605. #else
  11606. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11607. #endif
  11608. #endif
  11609. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11610. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11611. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11612. #ifndef NO_WOLFSSL_CLIENT
  11613. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11614. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11615. #else
  11616. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11617. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11618. #endif
  11619. wolfSSL_free(ssl);
  11620. wolfSSL_CTX_free(ctx);
  11621. #endif
  11622. return EXPECT_RESULT();
  11623. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11624. /*----------------------------------------------------------------------------*
  11625. | Multicast Tests
  11626. *----------------------------------------------------------------------------*/
  11627. static int test_wolfSSL_mcast(void)
  11628. {
  11629. EXPECT_DECLS;
  11630. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11631. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11632. WOLFSSL_CTX* ctx = NULL;
  11633. WOLFSSL* ssl = NULL;
  11634. byte preMasterSecret[512];
  11635. byte clientRandom[32];
  11636. byte serverRandom[32];
  11637. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11638. byte buf[256];
  11639. word16 newId;
  11640. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11641. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11642. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11643. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11644. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11645. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11646. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11647. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11648. WOLFSSL_SUCCESS);
  11649. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11650. ExpectIntLE(newId, 100);
  11651. wolfSSL_free(ssl);
  11652. wolfSSL_CTX_free(ctx);
  11653. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11654. * WOLFSSL_SNIFFER) */
  11655. return EXPECT_RESULT();
  11656. }
  11657. /*----------------------------------------------------------------------------*
  11658. | Wolfcrypt
  11659. *----------------------------------------------------------------------------*/
  11660. /*
  11661. * Unit test for the wc_InitBlake2b()
  11662. */
  11663. static int test_wc_InitBlake2b(void)
  11664. {
  11665. EXPECT_DECLS;
  11666. #ifdef HAVE_BLAKE2
  11667. Blake2b blake;
  11668. /* Test good arg. */
  11669. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11670. /* Test bad arg. */
  11671. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11672. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11673. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11674. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11675. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11676. #endif
  11677. return EXPECT_RESULT();
  11678. } /* END test_wc_InitBlake2b*/
  11679. /*
  11680. * Unit test for the wc_InitBlake2b_WithKey()
  11681. */
  11682. static int test_wc_InitBlake2b_WithKey(void)
  11683. {
  11684. EXPECT_DECLS;
  11685. #ifdef HAVE_BLAKE2
  11686. Blake2b blake;
  11687. word32 digestSz = BLAKE2B_KEYBYTES;
  11688. byte key[BLAKE2B_KEYBYTES];
  11689. word32 keylen = BLAKE2B_KEYBYTES;
  11690. XMEMSET(key, 0, sizeof(key));
  11691. /* Test good arg. */
  11692. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11693. /* Test bad args. */
  11694. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11695. BAD_FUNC_ARG);
  11696. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11697. BAD_FUNC_ARG);
  11698. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11699. #endif
  11700. return EXPECT_RESULT();
  11701. } /* END wc_InitBlake2b_WithKey*/
  11702. /*
  11703. * Unit test for the wc_InitBlake2s_WithKey()
  11704. */
  11705. static int test_wc_InitBlake2s_WithKey(void)
  11706. {
  11707. EXPECT_DECLS;
  11708. #ifdef HAVE_BLAKE2S
  11709. Blake2s blake;
  11710. word32 digestSz = BLAKE2S_KEYBYTES;
  11711. byte *key = (byte*)"01234567890123456789012345678901";
  11712. word32 keylen = BLAKE2S_KEYBYTES;
  11713. /* Test good arg. */
  11714. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11715. /* Test bad args. */
  11716. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11717. BAD_FUNC_ARG);
  11718. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11719. BAD_FUNC_ARG);
  11720. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11721. #endif
  11722. return EXPECT_RESULT();
  11723. } /* END wc_InitBlake2s_WithKey*/
  11724. /*
  11725. * Unit test for the wc_InitMd5()
  11726. */
  11727. static int test_wc_InitMd5(void)
  11728. {
  11729. EXPECT_DECLS;
  11730. #ifndef NO_MD5
  11731. wc_Md5 md5;
  11732. /* Test good arg. */
  11733. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11734. /* Test bad arg. */
  11735. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11736. wc_Md5Free(&md5);
  11737. #endif
  11738. return EXPECT_RESULT();
  11739. } /* END test_wc_InitMd5 */
  11740. /*
  11741. * Testing wc_UpdateMd5()
  11742. */
  11743. static int test_wc_Md5Update(void)
  11744. {
  11745. EXPECT_DECLS;
  11746. #ifndef NO_MD5
  11747. wc_Md5 md5;
  11748. byte hash[WC_MD5_DIGEST_SIZE];
  11749. testVector a, b, c;
  11750. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11751. /* Input */
  11752. a.input = "a";
  11753. a.inLen = XSTRLEN(a.input);
  11754. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11755. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11756. /* Update input. */
  11757. a.input = "abc";
  11758. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11759. "\x72";
  11760. a.inLen = XSTRLEN(a.input);
  11761. a.outLen = XSTRLEN(a.output);
  11762. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11763. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11764. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11765. /* Pass in bad values. */
  11766. b.input = NULL;
  11767. b.inLen = 0;
  11768. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11769. c.input = NULL;
  11770. c.inLen = WC_MD5_DIGEST_SIZE;
  11771. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11772. BAD_FUNC_ARG);
  11773. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11774. BAD_FUNC_ARG);
  11775. wc_Md5Free(&md5);
  11776. #endif
  11777. return EXPECT_RESULT();
  11778. } /* END test_wc_Md5Update() */
  11779. /*
  11780. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11781. */
  11782. static int test_wc_Md5Final(void)
  11783. {
  11784. EXPECT_DECLS;
  11785. #ifndef NO_MD5
  11786. /* Instantiate */
  11787. wc_Md5 md5;
  11788. byte* hash_test[3];
  11789. byte hash1[WC_MD5_DIGEST_SIZE];
  11790. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11791. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11792. int times, i;
  11793. /* Initialize */
  11794. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11795. hash_test[0] = hash1;
  11796. hash_test[1] = hash2;
  11797. hash_test[2] = hash3;
  11798. times = sizeof(hash_test)/sizeof(byte*);
  11799. for (i = 0; i < times; i++) {
  11800. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11801. }
  11802. /* Test bad args. */
  11803. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11804. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11805. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11806. wc_Md5Free(&md5);
  11807. #endif
  11808. return EXPECT_RESULT();
  11809. }
  11810. /*
  11811. * Unit test for the wc_InitSha()
  11812. */
  11813. static int test_wc_InitSha(void)
  11814. {
  11815. EXPECT_DECLS;
  11816. #ifndef NO_SHA
  11817. wc_Sha sha;
  11818. /* Test good arg. */
  11819. ExpectIntEQ(wc_InitSha(&sha), 0);
  11820. /* Test bad arg. */
  11821. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11822. wc_ShaFree(&sha);
  11823. #endif
  11824. return EXPECT_RESULT();
  11825. } /* END test_wc_InitSha */
  11826. /*
  11827. * Tesing wc_ShaUpdate()
  11828. */
  11829. static int test_wc_ShaUpdate(void)
  11830. {
  11831. EXPECT_DECLS;
  11832. #ifndef NO_SHA
  11833. wc_Sha sha;
  11834. byte hash[WC_SHA_DIGEST_SIZE];
  11835. testVector a, b, c;
  11836. ExpectIntEQ(wc_InitSha(&sha), 0);
  11837. /* Input. */
  11838. a.input = "a";
  11839. a.inLen = XSTRLEN(a.input);
  11840. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11841. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11842. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11843. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11844. /* Update input. */
  11845. a.input = "abc";
  11846. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11847. "\x6C\x9C\xD0\xD8\x9D";
  11848. a.inLen = XSTRLEN(a.input);
  11849. a.outLen = XSTRLEN(a.output);
  11850. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11851. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11852. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11853. /* Try passing in bad values. */
  11854. b.input = NULL;
  11855. b.inLen = 0;
  11856. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11857. c.input = NULL;
  11858. c.inLen = WC_SHA_DIGEST_SIZE;
  11859. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11860. BAD_FUNC_ARG);
  11861. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11862. BAD_FUNC_ARG);
  11863. wc_ShaFree(&sha);
  11864. #endif
  11865. return EXPECT_RESULT();
  11866. } /* END test_wc_ShaUpdate() */
  11867. /*
  11868. * Unit test on wc_ShaFinal
  11869. */
  11870. static int test_wc_ShaFinal(void)
  11871. {
  11872. EXPECT_DECLS;
  11873. #ifndef NO_SHA
  11874. wc_Sha sha;
  11875. byte* hash_test[3];
  11876. byte hash1[WC_SHA_DIGEST_SIZE];
  11877. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11878. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11879. int times, i;
  11880. /* Initialize*/
  11881. ExpectIntEQ(wc_InitSha(&sha), 0);
  11882. hash_test[0] = hash1;
  11883. hash_test[1] = hash2;
  11884. hash_test[2] = hash3;
  11885. times = sizeof(hash_test)/sizeof(byte*);
  11886. for (i = 0; i < times; i++) {
  11887. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11888. }
  11889. /* Test bad args. */
  11890. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11891. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11892. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11893. wc_ShaFree(&sha);
  11894. #endif
  11895. return EXPECT_RESULT();
  11896. } /* END test_wc_ShaFinal */
  11897. /*
  11898. * Unit test for wc_InitSha256()
  11899. */
  11900. static int test_wc_InitSha256(void)
  11901. {
  11902. EXPECT_DECLS;
  11903. #ifndef NO_SHA256
  11904. wc_Sha256 sha256;
  11905. /* Test good arg. */
  11906. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11907. /* Test bad arg. */
  11908. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11909. wc_Sha256Free(&sha256);
  11910. #endif
  11911. return EXPECT_RESULT();
  11912. } /* END test_wc_InitSha256 */
  11913. /*
  11914. * Unit test for wc_Sha256Update()
  11915. */
  11916. static int test_wc_Sha256Update(void)
  11917. {
  11918. EXPECT_DECLS;
  11919. #ifndef NO_SHA256
  11920. wc_Sha256 sha256;
  11921. byte hash[WC_SHA256_DIGEST_SIZE];
  11922. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11923. testVector a, b, c;
  11924. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11925. /* Input. */
  11926. a.input = "a";
  11927. a.inLen = XSTRLEN(a.input);
  11928. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11929. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11930. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11931. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11932. /* Update input. */
  11933. a.input = "abc";
  11934. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11935. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11936. "\x15\xAD";
  11937. a.inLen = XSTRLEN(a.input);
  11938. a.outLen = XSTRLEN(a.output);
  11939. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11940. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11941. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11942. /* Unaligned check. */
  11943. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11944. 0);
  11945. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11946. /* Try passing in bad values */
  11947. b.input = NULL;
  11948. b.inLen = 0;
  11949. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11950. c.input = NULL;
  11951. c.inLen = WC_SHA256_DIGEST_SIZE;
  11952. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11953. BAD_FUNC_ARG);
  11954. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11955. BAD_FUNC_ARG);
  11956. wc_Sha256Free(&sha256);
  11957. #endif
  11958. return EXPECT_RESULT();
  11959. } /* END test_wc_Sha256Update */
  11960. /*
  11961. * Unit test function for wc_Sha256Final()
  11962. */
  11963. static int test_wc_Sha256Final(void)
  11964. {
  11965. EXPECT_DECLS;
  11966. #ifndef NO_SHA256
  11967. wc_Sha256 sha256;
  11968. byte* hash_test[3];
  11969. byte hash1[WC_SHA256_DIGEST_SIZE];
  11970. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11971. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11972. int times, i;
  11973. /* Initialize */
  11974. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11975. hash_test[0] = hash1;
  11976. hash_test[1] = hash2;
  11977. hash_test[2] = hash3;
  11978. times = sizeof(hash_test) / sizeof(byte*);
  11979. for (i = 0; i < times; i++) {
  11980. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11981. }
  11982. /* Test bad args. */
  11983. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11984. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11985. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11986. wc_Sha256Free(&sha256);
  11987. #endif
  11988. return EXPECT_RESULT();
  11989. } /* END test_wc_Sha256Final */
  11990. /*
  11991. * Unit test function for wc_Sha256FinalRaw()
  11992. */
  11993. static int test_wc_Sha256FinalRaw(void)
  11994. {
  11995. EXPECT_DECLS;
  11996. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11997. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11998. !defined(WOLFSSL_NO_HASH_RAW)
  11999. wc_Sha256 sha256;
  12000. byte* hash_test[3];
  12001. byte hash1[WC_SHA256_DIGEST_SIZE];
  12002. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  12003. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  12004. int times, i;
  12005. /* Initialize */
  12006. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12007. hash_test[0] = hash1;
  12008. hash_test[1] = hash2;
  12009. hash_test[2] = hash3;
  12010. times = sizeof(hash_test) / sizeof(byte*);
  12011. for (i = 0; i < times; i++) {
  12012. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  12013. }
  12014. /* Test bad args. */
  12015. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12016. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12017. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  12018. wc_Sha256Free(&sha256);
  12019. #endif
  12020. return EXPECT_RESULT();
  12021. } /* END test_wc_Sha256FinalRaw */
  12022. /*
  12023. * Unit test function for wc_Sha256GetFlags()
  12024. */
  12025. static int test_wc_Sha256GetFlags(void)
  12026. {
  12027. EXPECT_DECLS;
  12028. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  12029. wc_Sha256 sha256;
  12030. word32 flags = 0;
  12031. /* Initialize */
  12032. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12033. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  12034. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12035. wc_Sha256Free(&sha256);
  12036. #endif
  12037. return EXPECT_RESULT();
  12038. } /* END test_wc_Sha256GetFlags */
  12039. /*
  12040. * Unit test function for wc_Sha256Free()
  12041. */
  12042. static int test_wc_Sha256Free(void)
  12043. {
  12044. EXPECT_DECLS;
  12045. #ifndef NO_SHA256
  12046. wc_Sha256Free(NULL);
  12047. /* Set result to SUCCESS. */
  12048. ExpectTrue(1);
  12049. #endif
  12050. return EXPECT_RESULT();
  12051. } /* END test_wc_Sha256Free */
  12052. /*
  12053. * Unit test function for wc_Sha256GetHash()
  12054. */
  12055. static int test_wc_Sha256GetHash(void)
  12056. {
  12057. EXPECT_DECLS;
  12058. #ifndef NO_SHA256
  12059. wc_Sha256 sha256;
  12060. byte hash1[WC_SHA256_DIGEST_SIZE];
  12061. /* Initialize */
  12062. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12063. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  12064. /* test bad arguments*/
  12065. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  12066. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  12067. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  12068. wc_Sha256Free(&sha256);
  12069. #endif
  12070. return EXPECT_RESULT();
  12071. } /* END test_wc_Sha256GetHash */
  12072. /*
  12073. * Unit test function for wc_Sha256Copy()
  12074. */
  12075. static int test_wc_Sha256Copy(void)
  12076. {
  12077. EXPECT_DECLS;
  12078. #ifndef NO_SHA256
  12079. wc_Sha256 sha256;
  12080. wc_Sha256 temp;
  12081. XMEMSET(&sha256, 0, sizeof(sha256));
  12082. XMEMSET(&temp, 0, sizeof(temp));
  12083. /* Initialize */
  12084. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12085. ExpectIntEQ(wc_InitSha256(&temp), 0);
  12086. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  12087. /* test bad arguments*/
  12088. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  12089. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  12090. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  12091. wc_Sha256Free(&sha256);
  12092. wc_Sha256Free(&temp);
  12093. #endif
  12094. return EXPECT_RESULT();
  12095. } /* END test_wc_Sha256Copy */
  12096. /*
  12097. * Testing wc_InitSha512()
  12098. */
  12099. static int test_wc_InitSha512(void)
  12100. {
  12101. EXPECT_DECLS;
  12102. #ifdef WOLFSSL_SHA512
  12103. wc_Sha512 sha512;
  12104. /* Test good arg. */
  12105. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12106. /* Test bad arg. */
  12107. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  12108. wc_Sha512Free(&sha512);
  12109. #endif
  12110. return EXPECT_RESULT();
  12111. } /* END test_wc_InitSha512 */
  12112. /*
  12113. * wc_Sha512Update() test.
  12114. */
  12115. static int test_wc_Sha512Update(void)
  12116. {
  12117. EXPECT_DECLS;
  12118. #ifdef WOLFSSL_SHA512
  12119. wc_Sha512 sha512;
  12120. byte hash[WC_SHA512_DIGEST_SIZE];
  12121. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  12122. testVector a, b, c;
  12123. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12124. /* Input. */
  12125. a.input = "a";
  12126. a.inLen = XSTRLEN(a.input);
  12127. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  12128. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  12129. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  12130. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12131. /* Update input. */
  12132. a.input = "abc";
  12133. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  12134. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  12135. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  12136. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  12137. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  12138. a.inLen = XSTRLEN(a.input);
  12139. a.outLen = XSTRLEN(a.output);
  12140. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  12141. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12142. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  12143. /* Unaligned check. */
  12144. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  12145. 0);
  12146. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  12147. /* Try passing in bad values */
  12148. b.input = NULL;
  12149. b.inLen = 0;
  12150. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  12151. c.input = NULL;
  12152. c.inLen = WC_SHA512_DIGEST_SIZE;
  12153. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12154. BAD_FUNC_ARG);
  12155. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  12156. BAD_FUNC_ARG);
  12157. wc_Sha512Free(&sha512);
  12158. #endif
  12159. return EXPECT_RESULT();
  12160. } /* END test_wc_Sha512Update */
  12161. #ifdef WOLFSSL_SHA512
  12162. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12163. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12164. /* Performs test for
  12165. * - wc_Sha512Final/wc_Sha512FinalRaw
  12166. * - wc_Sha512_224Final/wc_Sha512_224Final
  12167. * - wc_Sha512_256Final/wc_Sha512_256Final
  12168. * parameter:
  12169. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  12170. * WC_HASH_TYPE_SHA512_256
  12171. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  12172. *return 0 on success
  12173. */
  12174. static int test_Sha512_Family_Final(int type, int isRaw)
  12175. {
  12176. EXPECT_DECLS;
  12177. wc_Sha512 sha512;
  12178. byte* hash_test[3];
  12179. byte hash1[WC_SHA512_DIGEST_SIZE];
  12180. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12181. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12182. int times, i;
  12183. int(*initFp)(wc_Sha512*);
  12184. int(*finalFp)(wc_Sha512*, byte*);
  12185. void(*freeFp)(wc_Sha512*);
  12186. if (type == WC_HASH_TYPE_SHA512) {
  12187. initFp = wc_InitSha512;
  12188. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12189. !defined(WOLFSSL_NO_HASH_RAW)
  12190. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  12191. #else
  12192. finalFp = (isRaw)? NULL : wc_Sha512Final;
  12193. #endif
  12194. freeFp = wc_Sha512Free;
  12195. }
  12196. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12197. #if !defined(WOLFSSL_NOSHA512_224)
  12198. else if (type == WC_HASH_TYPE_SHA512_224) {
  12199. initFp = wc_InitSha512_224;
  12200. #if !defined(WOLFSSL_NO_HASH_RAW)
  12201. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  12202. #else
  12203. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  12204. #endif
  12205. freeFp = wc_Sha512_224Free;
  12206. }
  12207. #endif
  12208. #if !defined(WOLFSSL_NOSHA512_256)
  12209. else if (type == WC_HASH_TYPE_SHA512_256) {
  12210. initFp = wc_InitSha512_256;
  12211. #if !defined(WOLFSSL_NO_HASH_RAW)
  12212. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  12213. #else
  12214. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  12215. #endif
  12216. freeFp = wc_Sha512_256Free;
  12217. }
  12218. #endif
  12219. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12220. else
  12221. return TEST_FAIL;
  12222. /* Initialize */
  12223. ExpectIntEQ(initFp(&sha512), 0);
  12224. hash_test[0] = hash1;
  12225. hash_test[1] = hash2;
  12226. hash_test[2] = hash3;
  12227. times = sizeof(hash_test) / sizeof(byte *);
  12228. /* Good test args. */
  12229. for (i = 0; i < times; i++) {
  12230. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  12231. }
  12232. /* Test bad args. */
  12233. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  12234. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  12235. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  12236. freeFp(&sha512);
  12237. return EXPECT_RESULT();
  12238. }
  12239. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12240. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12241. #endif /* WOLFSSL_SHA512 */
  12242. /*
  12243. * Unit test function for wc_Sha512Final()
  12244. */
  12245. static int test_wc_Sha512Final(void)
  12246. {
  12247. EXPECT_DECLS;
  12248. #ifdef WOLFSSL_SHA512
  12249. wc_Sha512 sha512;
  12250. byte* hash_test[3];
  12251. byte hash1[WC_SHA512_DIGEST_SIZE];
  12252. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12253. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12254. int times, i;
  12255. /* Initialize */
  12256. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12257. hash_test[0] = hash1;
  12258. hash_test[1] = hash2;
  12259. hash_test[2] = hash3;
  12260. times = sizeof(hash_test) / sizeof(byte *);
  12261. for (i = 0; i < times; i++) {
  12262. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  12263. }
  12264. /* Test bad args. */
  12265. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  12266. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  12267. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  12268. wc_Sha512Free(&sha512);
  12269. #endif
  12270. return EXPECT_RESULT();
  12271. } /* END test_wc_Sha512Final */
  12272. /*
  12273. * Unit test function for wc_Sha512GetFlags()
  12274. */
  12275. static int test_wc_Sha512GetFlags(void)
  12276. {
  12277. EXPECT_DECLS;
  12278. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  12279. wc_Sha512 sha512;
  12280. word32 flags = 0;
  12281. /* Initialize */
  12282. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12283. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  12284. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  12285. wc_Sha512Free(&sha512);
  12286. #endif
  12287. return EXPECT_RESULT();
  12288. } /* END test_wc_Sha512GetFlags */
  12289. /*
  12290. * Unit test function for wc_Sha512FinalRaw()
  12291. */
  12292. static int test_wc_Sha512FinalRaw(void)
  12293. {
  12294. EXPECT_DECLS;
  12295. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12296. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12297. !defined(WOLFSSL_NO_HASH_RAW)
  12298. wc_Sha512 sha512;
  12299. byte* hash_test[3];
  12300. byte hash1[WC_SHA512_DIGEST_SIZE];
  12301. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12302. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12303. int times, i;
  12304. /* Initialize */
  12305. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12306. hash_test[0] = hash1;
  12307. hash_test[1] = hash2;
  12308. hash_test[2] = hash3;
  12309. times = sizeof(hash_test) / sizeof(byte*);
  12310. /* Good test args. */
  12311. for (i = 0; i < times; i++) {
  12312. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  12313. }
  12314. /* Test bad args. */
  12315. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12316. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12317. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  12318. wc_Sha512Free(&sha512);
  12319. #endif
  12320. return EXPECT_RESULT();
  12321. } /* END test_wc_Sha512FinalRaw */
  12322. /*
  12323. * Unit test function for wc_Sha512Free()
  12324. */
  12325. static int test_wc_Sha512Free(void)
  12326. {
  12327. EXPECT_DECLS;
  12328. #ifdef WOLFSSL_SHA512
  12329. wc_Sha512Free(NULL);
  12330. /* Set result to SUCCESS. */
  12331. ExpectTrue(1);
  12332. #endif
  12333. return EXPECT_RESULT();
  12334. } /* END test_wc_Sha512Free */
  12335. #ifdef WOLFSSL_SHA512
  12336. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12337. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12338. static int test_Sha512_Family_GetHash(int type )
  12339. {
  12340. EXPECT_DECLS;
  12341. int(*initFp)(wc_Sha512*);
  12342. int(*ghashFp)(wc_Sha512*, byte*);
  12343. wc_Sha512 sha512;
  12344. byte hash1[WC_SHA512_DIGEST_SIZE];
  12345. if (type == WC_HASH_TYPE_SHA512) {
  12346. initFp = wc_InitSha512;
  12347. ghashFp = wc_Sha512GetHash;
  12348. }
  12349. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12350. #if !defined(WOLFSSL_NOSHA512_224)
  12351. else if (type == WC_HASH_TYPE_SHA512_224) {
  12352. initFp = wc_InitSha512_224;
  12353. ghashFp = wc_Sha512_224GetHash;
  12354. }
  12355. #endif
  12356. #if !defined(WOLFSSL_NOSHA512_256)
  12357. else if (type == WC_HASH_TYPE_SHA512_256) {
  12358. initFp = wc_InitSha512_256;
  12359. ghashFp = wc_Sha512_256GetHash;
  12360. }
  12361. #endif
  12362. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12363. else {
  12364. initFp = NULL;
  12365. ghashFp = NULL;
  12366. }
  12367. if (initFp == NULL || ghashFp == NULL)
  12368. return TEST_FAIL;
  12369. ExpectIntEQ(initFp(&sha512), 0);
  12370. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  12371. /* test bad arguments*/
  12372. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  12373. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  12374. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  12375. wc_Sha512Free(&sha512);
  12376. return EXPECT_RESULT();
  12377. }
  12378. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12379. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12380. #endif /* WOLFSSL_SHA512 */
  12381. /*
  12382. * Unit test function for wc_Sha512GetHash()
  12383. */
  12384. static int test_wc_Sha512GetHash(void)
  12385. {
  12386. EXPECT_DECLS;
  12387. #ifdef WOLFSSL_SHA512
  12388. wc_Sha512 sha512;
  12389. byte hash1[WC_SHA512_DIGEST_SIZE];
  12390. /* Initialize */
  12391. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12392. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  12393. /* test bad arguments*/
  12394. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  12395. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  12396. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  12397. wc_Sha512Free(&sha512);
  12398. #endif
  12399. return EXPECT_RESULT();
  12400. } /* END test_wc_Sha512GetHash */
  12401. /*
  12402. * Unit test function for wc_Sha512Copy()
  12403. */
  12404. static int test_wc_Sha512Copy(void)
  12405. {
  12406. EXPECT_DECLS;
  12407. #ifdef WOLFSSL_SHA512
  12408. wc_Sha512 sha512;
  12409. wc_Sha512 temp;
  12410. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12411. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12412. /* Initialize */
  12413. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12414. ExpectIntEQ(wc_InitSha512(&temp), 0);
  12415. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  12416. /* test bad arguments*/
  12417. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  12418. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  12419. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  12420. wc_Sha512Free(&sha512);
  12421. wc_Sha512Free(&temp);
  12422. #endif
  12423. return EXPECT_RESULT();
  12424. } /* END test_wc_Sha512Copy */
  12425. static int test_wc_InitSha512_224(void)
  12426. {
  12427. EXPECT_DECLS;
  12428. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12429. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12430. wc_Sha512 sha512;
  12431. /* Test good arg. */
  12432. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12433. /* Test bad arg. */
  12434. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  12435. wc_Sha512_224Free(&sha512);
  12436. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12437. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12438. return EXPECT_RESULT();
  12439. }
  12440. static int test_wc_Sha512_224Update(void)
  12441. {
  12442. EXPECT_DECLS;
  12443. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12444. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12445. wc_Sha512 sha512;
  12446. byte hash[WC_SHA512_DIGEST_SIZE];
  12447. testVector a, c;
  12448. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12449. /* Input. */
  12450. a.input = "a";
  12451. a.inLen = XSTRLEN(a.input);
  12452. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  12453. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  12454. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12455. 0);
  12456. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12457. /* Update input. */
  12458. a.input = "abc";
  12459. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  12460. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  12461. a.inLen = XSTRLEN(a.input);
  12462. a.outLen = XSTRLEN(a.output);
  12463. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12464. 0);
  12465. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12466. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  12467. c.input = NULL;
  12468. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  12469. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12470. BAD_FUNC_ARG);
  12471. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12472. BAD_FUNC_ARG);
  12473. wc_Sha512_224Free(&sha512);
  12474. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12475. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12476. return EXPECT_RESULT();
  12477. }
  12478. static int test_wc_Sha512_224Final(void)
  12479. {
  12480. EXPECT_DECLS;
  12481. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12482. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12483. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12484. TEST_SUCCESS);
  12485. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12486. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12487. return EXPECT_RESULT();
  12488. }
  12489. static int test_wc_Sha512_224GetFlags(void)
  12490. {
  12491. EXPECT_DECLS;
  12492. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12493. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12494. wc_Sha512 sha512;
  12495. wc_Sha512 copy;
  12496. word32 flags = 0;
  12497. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12498. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12499. /* Initialize */
  12500. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12501. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12502. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12503. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12504. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12505. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12506. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12507. wc_Sha512_224Free(&copy);
  12508. wc_Sha512_224Free(&sha512);
  12509. #endif
  12510. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12511. return EXPECT_RESULT();
  12512. }
  12513. static int test_wc_Sha512_224FinalRaw(void)
  12514. {
  12515. EXPECT_DECLS;
  12516. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12517. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12518. !defined(WOLFSSL_NO_HASH_RAW)
  12519. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12520. TEST_SUCCESS);
  12521. #endif
  12522. return EXPECT_RESULT();
  12523. }
  12524. static int test_wc_Sha512_224Free(void)
  12525. {
  12526. EXPECT_DECLS;
  12527. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12528. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12529. wc_Sha512_224Free(NULL);
  12530. /* Set result to SUCCESS. */
  12531. ExpectTrue(1);
  12532. #endif
  12533. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12534. return EXPECT_RESULT();
  12535. }
  12536. static int test_wc_Sha512_224GetHash(void)
  12537. {
  12538. EXPECT_DECLS;
  12539. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12540. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12541. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12542. TEST_SUCCESS);
  12543. #endif
  12544. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12545. return EXPECT_RESULT();
  12546. }
  12547. static int test_wc_Sha512_224Copy(void)
  12548. {
  12549. EXPECT_DECLS;
  12550. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12551. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12552. wc_Sha512 sha512;
  12553. wc_Sha512 temp;
  12554. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12555. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12556. /* Initialize */
  12557. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12558. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12559. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12560. /* test bad arguments*/
  12561. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12562. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12563. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12564. wc_Sha512_224Free(&sha512);
  12565. wc_Sha512_224Free(&temp);
  12566. #endif
  12567. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12568. return EXPECT_RESULT();
  12569. }
  12570. static int test_wc_InitSha512_256(void)
  12571. {
  12572. EXPECT_DECLS;
  12573. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12574. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12575. wc_Sha512 sha512;
  12576. /* Test good arg. */
  12577. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12578. /* Test bad arg. */
  12579. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12580. wc_Sha512_256Free(&sha512);
  12581. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12582. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12583. return EXPECT_RESULT();
  12584. }
  12585. static int test_wc_Sha512_256Update(void)
  12586. {
  12587. EXPECT_DECLS;
  12588. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12589. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12590. wc_Sha512 sha512;
  12591. byte hash[WC_SHA512_DIGEST_SIZE];
  12592. testVector a, c;
  12593. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12594. /* Input. */
  12595. a.input = "a";
  12596. a.inLen = XSTRLEN(a.input);
  12597. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12598. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12599. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12600. 0);
  12601. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12602. /* Update input. */
  12603. a.input = "abc";
  12604. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12605. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12606. "\x07\xe7\xaf\x23";
  12607. a.inLen = XSTRLEN(a.input);
  12608. a.outLen = XSTRLEN(a.output);
  12609. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12610. 0);
  12611. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12612. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12613. c.input = NULL;
  12614. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12615. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12616. BAD_FUNC_ARG);
  12617. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12618. BAD_FUNC_ARG);
  12619. wc_Sha512_256Free(&sha512);
  12620. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12621. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12622. return EXPECT_RESULT();
  12623. }
  12624. static int test_wc_Sha512_256Final(void)
  12625. {
  12626. EXPECT_DECLS;
  12627. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12628. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12629. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12630. TEST_SUCCESS);
  12631. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12632. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12633. return EXPECT_RESULT();
  12634. }
  12635. static int test_wc_Sha512_256GetFlags(void)
  12636. {
  12637. EXPECT_DECLS;
  12638. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12639. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12640. wc_Sha512 sha512, copy;
  12641. word32 flags = 0;
  12642. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12643. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12644. /* Initialize */
  12645. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12646. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12647. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12648. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12649. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12650. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12651. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12652. wc_Sha512_256Free(&sha512);
  12653. #endif
  12654. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12655. return EXPECT_RESULT();
  12656. }
  12657. static int test_wc_Sha512_256FinalRaw(void)
  12658. {
  12659. EXPECT_DECLS;
  12660. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12661. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12662. !defined(WOLFSSL_NO_HASH_RAW)
  12663. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12664. TEST_SUCCESS);
  12665. #endif
  12666. return EXPECT_RESULT();
  12667. }
  12668. static int test_wc_Sha512_256Free(void)
  12669. {
  12670. EXPECT_DECLS;
  12671. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12672. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12673. wc_Sha512_256Free(NULL);
  12674. /* Set result to SUCCESS. */
  12675. ExpectTrue(1);
  12676. #endif
  12677. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12678. return EXPECT_RESULT();
  12679. }
  12680. static int test_wc_Sha512_256GetHash(void)
  12681. {
  12682. EXPECT_DECLS;
  12683. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12684. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12685. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12686. TEST_SUCCESS);
  12687. #endif
  12688. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12689. return EXPECT_RESULT();
  12690. }
  12691. static int test_wc_Sha512_256Copy(void)
  12692. {
  12693. EXPECT_DECLS;
  12694. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12695. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12696. wc_Sha512 sha512;
  12697. wc_Sha512 temp;
  12698. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12699. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12700. /* Initialize */
  12701. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12702. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12703. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12704. /* test bad arguments*/
  12705. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12706. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12707. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12708. wc_Sha512_256Free(&sha512);
  12709. wc_Sha512_256Free(&temp);
  12710. #endif
  12711. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12712. return EXPECT_RESULT();
  12713. }
  12714. /*
  12715. * Testing wc_InitSha384()
  12716. */
  12717. static int test_wc_InitSha384(void)
  12718. {
  12719. EXPECT_DECLS;
  12720. #ifdef WOLFSSL_SHA384
  12721. wc_Sha384 sha384;
  12722. /* Test good arg. */
  12723. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12724. /* Test bad arg. */
  12725. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12726. wc_Sha384Free(&sha384);
  12727. #endif
  12728. return EXPECT_RESULT();
  12729. } /* END test_wc_InitSha384 */
  12730. /*
  12731. * test wc_Sha384Update()
  12732. */
  12733. static int test_wc_Sha384Update(void)
  12734. {
  12735. EXPECT_DECLS;
  12736. #ifdef WOLFSSL_SHA384
  12737. wc_Sha384 sha384;
  12738. byte hash[WC_SHA384_DIGEST_SIZE];
  12739. testVector a, b, c;
  12740. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12741. /* Input */
  12742. a.input = "a";
  12743. a.inLen = XSTRLEN(a.input);
  12744. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12745. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12746. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12747. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12748. /* Update input. */
  12749. a.input = "abc";
  12750. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12751. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12752. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12753. "\xc8\x25\xa7";
  12754. a.inLen = XSTRLEN(a.input);
  12755. a.outLen = XSTRLEN(a.output);
  12756. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12757. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12758. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12759. /* Pass in bad values. */
  12760. b.input = NULL;
  12761. b.inLen = 0;
  12762. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12763. c.input = NULL;
  12764. c.inLen = WC_SHA384_DIGEST_SIZE;
  12765. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12766. BAD_FUNC_ARG);
  12767. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12768. BAD_FUNC_ARG);
  12769. wc_Sha384Free(&sha384);
  12770. #endif
  12771. return EXPECT_RESULT();
  12772. } /* END test_wc_Sha384Update */
  12773. /*
  12774. * Unit test function for wc_Sha384Final();
  12775. */
  12776. static int test_wc_Sha384Final(void)
  12777. {
  12778. EXPECT_DECLS;
  12779. #ifdef WOLFSSL_SHA384
  12780. wc_Sha384 sha384;
  12781. byte* hash_test[3];
  12782. byte hash1[WC_SHA384_DIGEST_SIZE];
  12783. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12784. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12785. int times, i;
  12786. /* Initialize */
  12787. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12788. hash_test[0] = hash1;
  12789. hash_test[1] = hash2;
  12790. hash_test[2] = hash3;
  12791. times = sizeof(hash_test) / sizeof(byte*);
  12792. /* Good test args. */
  12793. for (i = 0; i < times; i++) {
  12794. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12795. }
  12796. /* Test bad args. */
  12797. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12798. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12799. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12800. wc_Sha384Free(&sha384);
  12801. #endif
  12802. return EXPECT_RESULT();
  12803. } /* END test_wc_Sha384Final */
  12804. /*
  12805. * Unit test function for wc_Sha384GetFlags()
  12806. */
  12807. static int test_wc_Sha384GetFlags(void)
  12808. {
  12809. EXPECT_DECLS;
  12810. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12811. wc_Sha384 sha384;
  12812. word32 flags = 0;
  12813. /* Initialize */
  12814. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12815. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12816. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12817. wc_Sha384Free(&sha384);
  12818. #endif
  12819. return EXPECT_RESULT();
  12820. } /* END test_wc_Sha384GetFlags */
  12821. /*
  12822. * Unit test function for wc_Sha384FinalRaw()
  12823. */
  12824. static int test_wc_Sha384FinalRaw(void)
  12825. {
  12826. EXPECT_DECLS;
  12827. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12828. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12829. !defined(WOLFSSL_NO_HASH_RAW)
  12830. wc_Sha384 sha384;
  12831. byte* hash_test[3];
  12832. byte hash1[WC_SHA384_DIGEST_SIZE];
  12833. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12834. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12835. int times, i;
  12836. /* Initialize */
  12837. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12838. hash_test[0] = hash1;
  12839. hash_test[1] = hash2;
  12840. hash_test[2] = hash3;
  12841. times = sizeof(hash_test) / sizeof(byte*);
  12842. /* Good test args. */
  12843. for (i = 0; i < times; i++) {
  12844. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12845. }
  12846. /* Test bad args. */
  12847. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12848. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12849. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12850. wc_Sha384Free(&sha384);
  12851. #endif
  12852. return EXPECT_RESULT();
  12853. } /* END test_wc_Sha384FinalRaw */
  12854. /*
  12855. * Unit test function for wc_Sha384Free()
  12856. */
  12857. static int test_wc_Sha384Free(void)
  12858. {
  12859. EXPECT_DECLS;
  12860. #ifdef WOLFSSL_SHA384
  12861. wc_Sha384Free(NULL);
  12862. /* Set result to SUCCESS. */
  12863. ExpectTrue(1);
  12864. #endif
  12865. return EXPECT_RESULT();
  12866. } /* END test_wc_Sha384Free */
  12867. /*
  12868. * Unit test function for wc_Sha384GetHash()
  12869. */
  12870. static int test_wc_Sha384GetHash(void)
  12871. {
  12872. EXPECT_DECLS;
  12873. #ifdef WOLFSSL_SHA384
  12874. wc_Sha384 sha384;
  12875. byte hash1[WC_SHA384_DIGEST_SIZE];
  12876. /* Initialize */
  12877. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12878. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12879. /* test bad arguments*/
  12880. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12881. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12882. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12883. wc_Sha384Free(&sha384);
  12884. #endif
  12885. return EXPECT_RESULT();
  12886. } /* END test_wc_Sha384GetHash */
  12887. /*
  12888. * Unit test function for wc_Sha384Copy()
  12889. */
  12890. static int test_wc_Sha384Copy(void)
  12891. {
  12892. EXPECT_DECLS;
  12893. #ifdef WOLFSSL_SHA384
  12894. wc_Sha384 sha384;
  12895. wc_Sha384 temp;
  12896. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12897. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12898. /* Initialize */
  12899. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12900. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12901. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12902. /* test bad arguments*/
  12903. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12904. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12905. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12906. wc_Sha384Free(&sha384);
  12907. wc_Sha384Free(&temp);
  12908. #endif
  12909. return EXPECT_RESULT();
  12910. } /* END test_wc_Sha384Copy */
  12911. /*
  12912. * Testing wc_InitSha224();
  12913. */
  12914. static int test_wc_InitSha224(void)
  12915. {
  12916. EXPECT_DECLS;
  12917. #ifdef WOLFSSL_SHA224
  12918. wc_Sha224 sha224;
  12919. /* Test good arg. */
  12920. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12921. /* Test bad arg. */
  12922. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12923. wc_Sha224Free(&sha224);
  12924. #endif
  12925. return EXPECT_RESULT();
  12926. } /* END test_wc_InitSha224 */
  12927. /*
  12928. * Unit test on wc_Sha224Update
  12929. */
  12930. static int test_wc_Sha224Update(void)
  12931. {
  12932. EXPECT_DECLS;
  12933. #ifdef WOLFSSL_SHA224
  12934. wc_Sha224 sha224;
  12935. byte hash[WC_SHA224_DIGEST_SIZE];
  12936. testVector a, b, c;
  12937. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12938. /* Input. */
  12939. a.input = "a";
  12940. a.inLen = XSTRLEN(a.input);
  12941. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12942. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12943. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12944. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12945. /* Update input. */
  12946. a.input = "abc";
  12947. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12948. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12949. a.inLen = XSTRLEN(a.input);
  12950. a.outLen = XSTRLEN(a.output);
  12951. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12952. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12953. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12954. /* Pass in bad values. */
  12955. b.input = NULL;
  12956. b.inLen = 0;
  12957. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12958. c.input = NULL;
  12959. c.inLen = WC_SHA224_DIGEST_SIZE;
  12960. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12961. BAD_FUNC_ARG);
  12962. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12963. BAD_FUNC_ARG);
  12964. wc_Sha224Free(&sha224);
  12965. #endif
  12966. return EXPECT_RESULT();
  12967. } /* END test_wc_Sha224Update */
  12968. /*
  12969. * Unit test for wc_Sha224Final();
  12970. */
  12971. static int test_wc_Sha224Final(void)
  12972. {
  12973. EXPECT_DECLS;
  12974. #ifdef WOLFSSL_SHA224
  12975. wc_Sha224 sha224;
  12976. byte* hash_test[3];
  12977. byte hash1[WC_SHA224_DIGEST_SIZE];
  12978. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12979. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12980. int times, i;
  12981. /* Initialize */
  12982. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12983. hash_test[0] = hash1;
  12984. hash_test[1] = hash2;
  12985. hash_test[2] = hash3;
  12986. times = sizeof(hash_test) / sizeof(byte*);
  12987. /* Good test args. */
  12988. /* Testing oversized buffers. */
  12989. for (i = 0; i < times; i++) {
  12990. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12991. }
  12992. /* Test bad args. */
  12993. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12994. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12995. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12996. wc_Sha224Free(&sha224);
  12997. #endif
  12998. return EXPECT_RESULT();
  12999. } /* END test_wc_Sha224Final */
  13000. /*
  13001. * Unit test function for wc_Sha224SetFlags()
  13002. */
  13003. static int test_wc_Sha224SetFlags(void)
  13004. {
  13005. EXPECT_DECLS;
  13006. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  13007. wc_Sha224 sha224;
  13008. word32 flags = WC_HASH_FLAG_WILLCOPY;
  13009. /* Initialize */
  13010. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13011. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  13012. flags = 0;
  13013. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  13014. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  13015. wc_Sha224Free(&sha224);
  13016. #endif
  13017. return EXPECT_RESULT();
  13018. } /* END test_wc_Sha224SetFlags */
  13019. /*
  13020. * Unit test function for wc_Sha224GetFlags()
  13021. */
  13022. static int test_wc_Sha224GetFlags(void)
  13023. {
  13024. EXPECT_DECLS;
  13025. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  13026. wc_Sha224 sha224;
  13027. word32 flags = 0;
  13028. /* Initialize */
  13029. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13030. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  13031. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13032. wc_Sha224Free(&sha224);
  13033. #endif
  13034. return EXPECT_RESULT();
  13035. } /* END test_wc_Sha224GetFlags */
  13036. /*
  13037. * Unit test function for wc_Sha224Free()
  13038. */
  13039. static int test_wc_Sha224Free(void)
  13040. {
  13041. EXPECT_DECLS;
  13042. #ifdef WOLFSSL_SHA224
  13043. wc_Sha224Free(NULL);
  13044. /* Set result to SUCCESS. */
  13045. ExpectTrue(1);
  13046. #endif
  13047. return EXPECT_RESULT();
  13048. } /* END test_wc_Sha224Free */
  13049. /*
  13050. * Unit test function for wc_Sha224GetHash()
  13051. */
  13052. static int test_wc_Sha224GetHash(void)
  13053. {
  13054. EXPECT_DECLS;
  13055. #ifdef WOLFSSL_SHA224
  13056. wc_Sha224 sha224;
  13057. byte hash1[WC_SHA224_DIGEST_SIZE];
  13058. /* Initialize */
  13059. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13060. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  13061. /* test bad arguments*/
  13062. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  13063. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  13064. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  13065. wc_Sha224Free(&sha224);
  13066. #endif
  13067. return EXPECT_RESULT();
  13068. } /* END test_wc_Sha224GetHash */
  13069. /*
  13070. * Unit test function for wc_Sha224Copy()
  13071. */
  13072. static int test_wc_Sha224Copy(void)
  13073. {
  13074. EXPECT_DECLS;
  13075. #ifdef WOLFSSL_SHA224
  13076. wc_Sha224 sha224;
  13077. wc_Sha224 temp;
  13078. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  13079. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  13080. /* Initialize */
  13081. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13082. ExpectIntEQ(wc_InitSha224(&temp), 0);
  13083. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  13084. /* test bad arguments*/
  13085. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  13086. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  13087. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  13088. wc_Sha224Free(&sha224);
  13089. wc_Sha224Free(&temp);
  13090. #endif
  13091. return EXPECT_RESULT();
  13092. } /* END test_wc_Sha224Copy */
  13093. /*
  13094. * Testing wc_InitRipeMd()
  13095. */
  13096. static int test_wc_InitRipeMd(void)
  13097. {
  13098. EXPECT_DECLS;
  13099. #ifdef WOLFSSL_RIPEMD
  13100. RipeMd ripemd;
  13101. /* Test good arg. */
  13102. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13103. /* Test bad arg. */
  13104. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  13105. #endif
  13106. return EXPECT_RESULT();
  13107. } /* END test_wc_InitRipeMd */
  13108. /*
  13109. * Testing wc_RipeMdUpdate()
  13110. */
  13111. static int test_wc_RipeMdUpdate(void)
  13112. {
  13113. EXPECT_DECLS;
  13114. #ifdef WOLFSSL_RIPEMD
  13115. RipeMd ripemd;
  13116. byte hash[RIPEMD_DIGEST_SIZE];
  13117. testVector a, b, c;
  13118. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13119. /* Input */
  13120. a.input = "a";
  13121. a.inLen = XSTRLEN(a.input);
  13122. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13123. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13124. /* Update input. */
  13125. a.input = "abc";
  13126. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  13127. "\xb0\x87\xf1\x5a\x0b\xfc";
  13128. a.inLen = XSTRLEN(a.input);
  13129. a.outLen = XSTRLEN(a.output);
  13130. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13131. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13132. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  13133. /* Pass in bad values. */
  13134. b.input = NULL;
  13135. b.inLen = 0;
  13136. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  13137. c.input = NULL;
  13138. c.inLen = RIPEMD_DIGEST_SIZE;
  13139. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  13140. BAD_FUNC_ARG);
  13141. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13142. BAD_FUNC_ARG);
  13143. #endif
  13144. return EXPECT_RESULT();
  13145. } /* END test_wc_RipeMdUdpate */
  13146. /*
  13147. * Unit test function for wc_RipeMdFinal()
  13148. */
  13149. static int test_wc_RipeMdFinal(void)
  13150. {
  13151. EXPECT_DECLS;
  13152. #ifdef WOLFSSL_RIPEMD
  13153. RipeMd ripemd;
  13154. byte* hash_test[3];
  13155. byte hash1[RIPEMD_DIGEST_SIZE];
  13156. byte hash2[2*RIPEMD_DIGEST_SIZE];
  13157. byte hash3[5*RIPEMD_DIGEST_SIZE];
  13158. int times, i;
  13159. /* Initialize */
  13160. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13161. hash_test[0] = hash1;
  13162. hash_test[1] = hash2;
  13163. hash_test[2] = hash3;
  13164. times = sizeof(hash_test) / sizeof(byte*);
  13165. /* Testing oversized buffers. */
  13166. for (i = 0; i < times; i++) {
  13167. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  13168. }
  13169. /* Test bad args. */
  13170. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  13171. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  13172. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  13173. #endif
  13174. return EXPECT_RESULT();
  13175. } /* END test_wc_RipeMdFinal */
  13176. /*
  13177. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  13178. * wc_InitSha3_512
  13179. */
  13180. static int test_wc_InitSha3(void)
  13181. {
  13182. EXPECT_DECLS;
  13183. #if defined(WOLFSSL_SHA3)
  13184. wc_Sha3 sha3;
  13185. (void)sha3;
  13186. #if !defined(WOLFSSL_NOSHA3_224)
  13187. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13188. /* Test bad args. */
  13189. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13190. wc_Sha3_224_Free(&sha3);
  13191. #endif /* NOSHA3_224 */
  13192. #if !defined(WOLFSSL_NOSHA3_256)
  13193. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13194. /* Test bad args. */
  13195. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13196. wc_Sha3_256_Free(&sha3);
  13197. #endif /* NOSHA3_256 */
  13198. #if !defined(WOLFSSL_NOSHA3_384)
  13199. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13200. /* Test bad args. */
  13201. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13202. wc_Sha3_384_Free(&sha3);
  13203. #endif /* NOSHA3_384 */
  13204. #if !defined(WOLFSSL_NOSHA3_512)
  13205. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13206. /* Test bad args. */
  13207. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13208. wc_Sha3_512_Free(&sha3);
  13209. #endif /* NOSHA3_512 */
  13210. #endif
  13211. return EXPECT_RESULT();
  13212. } /* END test_wc_InitSha3 */
  13213. /*
  13214. * Testing wc_Sha3_Update()
  13215. */
  13216. static int testing_wc_Sha3_Update(void)
  13217. {
  13218. EXPECT_DECLS;
  13219. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  13220. !defined(WOLFSSL_AFALG_XILINX)
  13221. wc_Sha3 sha3;
  13222. byte msg[] = "Everybody's working for the weekend.";
  13223. byte msg2[] = "Everybody gets Friday off.";
  13224. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13225. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13226. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13227. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13228. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13229. word32 msglen = sizeof(msg) - 1;
  13230. word32 msg2len = sizeof(msg2);
  13231. word32 msgCmplen = sizeof(msgCmp);
  13232. #if !defined(WOLFSSL_NOSHA3_224)
  13233. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13234. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  13235. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13236. ExpectTrue(sha3.i == msglen);
  13237. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13238. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13239. /* Pass bad args. */
  13240. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13241. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13242. wc_Sha3_224_Free(&sha3);
  13243. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13244. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  13245. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13246. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13247. wc_Sha3_224_Free(&sha3);
  13248. #endif /* SHA3_224 */
  13249. #if !defined(WOLFSSL_NOSHA3_256)
  13250. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13251. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  13252. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13253. ExpectTrue(sha3.i == msglen);
  13254. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13255. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13256. /* Pass bad args. */
  13257. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13258. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13259. wc_Sha3_256_Free(&sha3);
  13260. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13261. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  13262. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13263. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13264. wc_Sha3_256_Free(&sha3);
  13265. #endif /* SHA3_256 */
  13266. #if !defined(WOLFSSL_NOSHA3_384)
  13267. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13268. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  13269. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13270. ExpectTrue(sha3.i == msglen);
  13271. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13272. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13273. /* Pass bad args. */
  13274. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13275. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13276. wc_Sha3_384_Free(&sha3);
  13277. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13278. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  13279. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13280. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13281. wc_Sha3_384_Free(&sha3);
  13282. #endif /* SHA3_384 */
  13283. #if !defined(WOLFSSL_NOSHA3_512)
  13284. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13285. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  13286. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13287. ExpectTrue(sha3.i == msglen);
  13288. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13289. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13290. /* Pass bad args. */
  13291. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13292. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13293. wc_Sha3_512_Free(&sha3);
  13294. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13295. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  13296. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13297. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13298. wc_Sha3_512_Free(&sha3);
  13299. #endif /* SHA3_512 */
  13300. #endif /* WOLFSSL_SHA3 */
  13301. return EXPECT_RESULT();
  13302. } /* END testing_wc_Sha3_Update */
  13303. /*
  13304. * Testing wc_Sha3_224_Final()
  13305. */
  13306. static int test_wc_Sha3_224_Final(void)
  13307. {
  13308. EXPECT_DECLS;
  13309. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13310. wc_Sha3 sha3;
  13311. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13312. "nopnopq";
  13313. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13314. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13315. "\x64\xea\xd0\xfc\xce\x33";
  13316. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13317. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13318. /* Init stack variables. */
  13319. XMEMSET(hash, 0, sizeof(hash));
  13320. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13321. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13322. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13323. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  13324. /* Test bad args. */
  13325. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  13326. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  13327. wc_Sha3_224_Free(&sha3);
  13328. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13329. /* Init stack variables. */
  13330. XMEMSET(hash, 0, sizeof(hash));
  13331. XMEMSET(hashRet, 0, sizeof(hashRet));
  13332. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13333. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  13334. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13335. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  13336. /* Test bad args. */
  13337. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13338. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13339. wc_Sha3_224_Free(&sha3);
  13340. #endif
  13341. return EXPECT_RESULT();
  13342. } /* END test_wc_Sha3_224_Final */
  13343. /*
  13344. * Testing wc_Sha3_256_Final()
  13345. */
  13346. static int test_wc_Sha3_256_Final(void)
  13347. {
  13348. EXPECT_DECLS;
  13349. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13350. wc_Sha3 sha3;
  13351. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13352. "nopnopq";
  13353. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13354. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13355. "\xdd\x97\x49\x6d\x33\x76";
  13356. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13357. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13358. /* Init stack variables. */
  13359. XMEMSET(hash, 0, sizeof(hash));
  13360. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13361. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13362. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13363. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  13364. /* Test bad args. */
  13365. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  13366. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  13367. wc_Sha3_256_Free(&sha3);
  13368. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13369. /* Init stack variables. */
  13370. XMEMSET(hash, 0, sizeof(hash));
  13371. XMEMSET(hashRet, 0, sizeof(hashRet));
  13372. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13373. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  13374. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13375. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  13376. /* Test bad args. */
  13377. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13378. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13379. wc_Sha3_256_Free(&sha3);
  13380. #endif
  13381. return EXPECT_RESULT();
  13382. } /* END test_wc_Sha3_256_Final */
  13383. /*
  13384. * Testing wc_Sha3_384_Final()
  13385. */
  13386. static int test_wc_Sha3_384_Final(void)
  13387. {
  13388. EXPECT_DECLS;
  13389. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13390. wc_Sha3 sha3;
  13391. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13392. "nopnopq";
  13393. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13394. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13395. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13396. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13397. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13398. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13399. /* Init stack variables. */
  13400. XMEMSET(hash, 0, sizeof(hash));
  13401. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13402. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13403. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13404. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  13405. /* Test bad args. */
  13406. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  13407. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  13408. wc_Sha3_384_Free(&sha3);
  13409. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13410. /* Init stack variables. */
  13411. XMEMSET(hash, 0, sizeof(hash));
  13412. XMEMSET(hashRet, 0, sizeof(hashRet));
  13413. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13414. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  13415. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13416. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  13417. /* Test bad args. */
  13418. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13419. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13420. wc_Sha3_384_Free(&sha3);
  13421. #endif
  13422. return EXPECT_RESULT();
  13423. } /* END test_wc_Sha3_384_Final */
  13424. /*
  13425. * Testing wc_Sha3_512_Final()
  13426. */
  13427. static int test_wc_Sha3_512_Final(void)
  13428. {
  13429. EXPECT_DECLS;
  13430. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13431. !defined(WOLFSSL_NOSHA3_384)
  13432. wc_Sha3 sha3;
  13433. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13434. "nopnopq";
  13435. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13436. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13437. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13438. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13439. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13440. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13441. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13442. /* Init stack variables. */
  13443. XMEMSET(hash, 0, sizeof(hash));
  13444. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13445. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13446. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13447. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  13448. /* Test bad args. */
  13449. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  13450. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  13451. wc_Sha3_512_Free(&sha3);
  13452. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13453. /* Init stack variables. */
  13454. XMEMSET(hash, 0, sizeof(hash));
  13455. XMEMSET(hashRet, 0, sizeof(hashRet));
  13456. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13457. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  13458. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13459. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  13460. /* Test bad args. */
  13461. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13462. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13463. wc_Sha3_512_Free(&sha3);
  13464. #endif
  13465. return EXPECT_RESULT();
  13466. } /* END test_wc_Sha3_512_Final */
  13467. /*
  13468. * Testing wc_Sha3_224_Copy()
  13469. */
  13470. static int test_wc_Sha3_224_Copy(void)
  13471. {
  13472. EXPECT_DECLS;
  13473. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13474. wc_Sha3 sha3, sha3Cpy;
  13475. const char* msg = TEST_STRING;
  13476. word32 msglen = (word32)TEST_STRING_SZ;
  13477. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13478. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13479. XMEMSET(hash, 0, sizeof(hash));
  13480. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13481. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13482. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13483. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13484. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13485. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13486. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13487. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13488. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13489. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13490. /* Test bad args. */
  13491. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13492. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13493. wc_Sha3_224_Free(&sha3);
  13494. wc_Sha3_224_Free(&sha3Cpy);
  13495. #endif
  13496. return EXPECT_RESULT();
  13497. } /* END test_wc_Sha3_224_Copy */
  13498. /*
  13499. * Testing wc_Sha3_256_Copy()
  13500. */
  13501. static int test_wc_Sha3_256_Copy(void)
  13502. {
  13503. EXPECT_DECLS;
  13504. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13505. wc_Sha3 sha3, sha3Cpy;
  13506. const char* msg = TEST_STRING;
  13507. word32 msglen = (word32)TEST_STRING_SZ;
  13508. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13509. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13510. XMEMSET(hash, 0, sizeof(hash));
  13511. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13512. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13513. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13514. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13515. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13516. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13517. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13518. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13519. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13520. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13521. /* Test bad args. */
  13522. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13523. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13524. wc_Sha3_256_Free(&sha3);
  13525. wc_Sha3_256_Free(&sha3Cpy);
  13526. #endif
  13527. return EXPECT_RESULT();
  13528. } /* END test_wc_Sha3_256_Copy */
  13529. /*
  13530. * Testing wc_Sha3_384_Copy()
  13531. */
  13532. static int test_wc_Sha3_384_Copy(void)
  13533. {
  13534. EXPECT_DECLS;
  13535. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13536. wc_Sha3 sha3, sha3Cpy;
  13537. const char* msg = TEST_STRING;
  13538. word32 msglen = (word32)TEST_STRING_SZ;
  13539. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13540. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13541. XMEMSET(hash, 0, sizeof(hash));
  13542. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13543. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13544. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13545. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13546. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13547. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13548. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13549. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13550. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13551. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13552. /* Test bad args. */
  13553. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13554. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13555. wc_Sha3_384_Free(&sha3);
  13556. wc_Sha3_384_Free(&sha3Cpy);
  13557. #endif
  13558. return EXPECT_RESULT();
  13559. } /* END test_wc_Sha3_384_Copy */
  13560. /*
  13561. * Testing wc_Sha3_512_Copy()
  13562. */
  13563. static int test_wc_Sha3_512_Copy(void)
  13564. {
  13565. EXPECT_DECLS;
  13566. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13567. wc_Sha3 sha3, sha3Cpy;
  13568. const char* msg = TEST_STRING;
  13569. word32 msglen = (word32)TEST_STRING_SZ;
  13570. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13571. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13572. XMEMSET(hash, 0, sizeof(hash));
  13573. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13574. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13575. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13576. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13577. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13578. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13579. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13580. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13581. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13582. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13583. /* Test bad args. */
  13584. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13585. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13586. wc_Sha3_512_Free(&sha3);
  13587. wc_Sha3_512_Free(&sha3Cpy);
  13588. #endif
  13589. return EXPECT_RESULT();
  13590. } /* END test_wc_Sha3_512_Copy */
  13591. /*
  13592. * Unit test function for wc_Sha3_GetFlags()
  13593. */
  13594. static int test_wc_Sha3_GetFlags(void)
  13595. {
  13596. EXPECT_DECLS;
  13597. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13598. wc_Sha3 sha3;
  13599. word32 flags = 0;
  13600. /* Initialize */
  13601. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13602. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13603. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13604. wc_Sha3_224_Free(&sha3);
  13605. #endif
  13606. return EXPECT_RESULT();
  13607. } /* END test_wc_Sha3_GetFlags */
  13608. static int test_wc_InitShake256(void)
  13609. {
  13610. EXPECT_DECLS;
  13611. #ifdef WOLFSSL_SHAKE256
  13612. wc_Shake shake;
  13613. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13614. /* Test bad args. */
  13615. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13616. wc_Shake256_Free(&shake);
  13617. #endif
  13618. return EXPECT_RESULT();
  13619. }
  13620. static int testing_wc_Shake256_Update(void)
  13621. {
  13622. EXPECT_DECLS;
  13623. #ifdef WOLFSSL_SHAKE256
  13624. wc_Shake shake;
  13625. byte msg[] = "Everybody's working for the weekend.";
  13626. byte msg2[] = "Everybody gets Friday off.";
  13627. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13628. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13629. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13630. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13631. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13632. word32 msglen = sizeof(msg) - 1;
  13633. word32 msg2len = sizeof(msg2);
  13634. word32 msgCmplen = sizeof(msgCmp);
  13635. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13636. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13637. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13638. ExpectTrue(shake.i == msglen);
  13639. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13640. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13641. /* Pass bad args. */
  13642. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13643. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13644. wc_Shake256_Free(&shake);
  13645. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13646. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13647. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13648. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13649. wc_Shake256_Free(&shake);
  13650. #endif /* WOLFSSL_SHAKE256 */
  13651. return EXPECT_RESULT();
  13652. }
  13653. static int test_wc_Shake256_Final(void)
  13654. {
  13655. EXPECT_DECLS;
  13656. #ifdef WOLFSSL_SHAKE256
  13657. wc_Shake shake;
  13658. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13659. "nopnopq";
  13660. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13661. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13662. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13663. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13664. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13665. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13666. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13667. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13668. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13669. byte hash[114];
  13670. /* Init stack variables. */
  13671. XMEMSET(hash, 0, sizeof(hash));
  13672. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13673. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13674. 0);
  13675. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13676. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13677. /* Test bad args. */
  13678. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13679. BAD_FUNC_ARG);
  13680. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13681. BAD_FUNC_ARG);
  13682. wc_Shake256_Free(&shake);
  13683. #endif
  13684. return EXPECT_RESULT();
  13685. }
  13686. /*
  13687. * Testing wc_Shake256_Copy()
  13688. */
  13689. static int test_wc_Shake256_Copy(void)
  13690. {
  13691. EXPECT_DECLS;
  13692. #ifdef WOLFSSL_SHAKE256
  13693. wc_Shake shake, shakeCpy;
  13694. const char* msg = TEST_STRING;
  13695. word32 msglen = (word32)TEST_STRING_SZ;
  13696. byte hash[144];
  13697. byte hashCpy[144];
  13698. word32 hashLen = sizeof(hash);
  13699. word32 hashLenCpy = sizeof(hashCpy);
  13700. XMEMSET(hash, 0, sizeof(hash));
  13701. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13702. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13703. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13704. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13705. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13706. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13707. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13708. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13709. /* Test bad args. */
  13710. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13711. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13712. wc_Shake256_Free(&shake);
  13713. wc_Shake256_Free(&shakeCpy);
  13714. #endif
  13715. return EXPECT_RESULT();
  13716. } /* END test_wc_Shake256_Copy */
  13717. /*
  13718. * Unit test function for wc_Shake256Hash()
  13719. */
  13720. static int test_wc_Shake256Hash(void)
  13721. {
  13722. EXPECT_DECLS;
  13723. #ifdef WOLFSSL_SHAKE256
  13724. const byte data[] = { /* Hello World */
  13725. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13726. 0x72,0x6c,0x64
  13727. };
  13728. word32 len = sizeof(data);
  13729. byte hash[144];
  13730. word32 hashLen = sizeof(hash);
  13731. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13732. #endif
  13733. return EXPECT_RESULT();
  13734. } /* END test_wc_Shake256Hash */
  13735. /*
  13736. * Testing wc_InitSm3(), wc_Sm3Free()
  13737. */
  13738. static int test_wc_InitSm3Free(void)
  13739. {
  13740. EXPECT_DECLS;
  13741. #ifdef WOLFSSL_SM3
  13742. wc_Sm3 sm3;
  13743. /* Invalid Parameters */
  13744. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13745. /* Valid Parameters */
  13746. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13747. wc_Sm3Free(NULL);
  13748. wc_Sm3Free(&sm3);
  13749. #endif
  13750. return EXPECT_RESULT();
  13751. } /* END test_wc_InitSm3 */
  13752. /*
  13753. * Testing wc_Sm3Update(), wc_Sm3Final()
  13754. */
  13755. static int test_wc_Sm3UpdateFinal(void)
  13756. {
  13757. EXPECT_DECLS;
  13758. #ifdef WOLFSSL_SM3
  13759. wc_Sm3 sm3;
  13760. byte data[WC_SM3_BLOCK_SIZE * 4];
  13761. byte hash[WC_SM3_DIGEST_SIZE];
  13762. byte calcHash[WC_SM3_DIGEST_SIZE];
  13763. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13764. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13765. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13766. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13767. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13768. };
  13769. word32 chunk;
  13770. word32 i;
  13771. XMEMSET(data, 0, sizeof(data));
  13772. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13773. /* Invalid Parameters */
  13774. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13775. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13776. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13777. /* Valid Parameters */
  13778. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13779. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13780. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13781. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13782. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13783. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13784. /* Ensure too many bytes for lengths. */
  13785. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13786. /* Invalid Parameters */
  13787. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13788. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13789. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13790. /* Valid Parameters */
  13791. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13792. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13793. /* Chunk tests. */
  13794. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13795. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13796. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13797. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13798. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13799. }
  13800. if (i < (word32)sizeof(data)) {
  13801. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13802. 0);
  13803. }
  13804. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13805. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13806. }
  13807. /* Not testing when the low 32-bit length overflows. */
  13808. wc_Sm3Free(&sm3);
  13809. #endif
  13810. return EXPECT_RESULT();
  13811. } /* END test_wc_Sm3Update */
  13812. /*
  13813. * Testing wc_Sm3GetHash()
  13814. */
  13815. static int test_wc_Sm3GetHash(void)
  13816. {
  13817. EXPECT_DECLS;
  13818. #ifdef WOLFSSL_SM3
  13819. wc_Sm3 sm3;
  13820. byte hash[WC_SM3_DIGEST_SIZE];
  13821. byte calcHash[WC_SM3_DIGEST_SIZE];
  13822. byte data[WC_SM3_BLOCK_SIZE];
  13823. XMEMSET(data, 0, sizeof(data));
  13824. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13825. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13826. /* Invalid Parameters */
  13827. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13828. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13829. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13830. /* Valid Parameters */
  13831. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13832. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13833. /* With update. */
  13834. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13835. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13836. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13837. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13838. wc_Sm3Free(&sm3);
  13839. #endif
  13840. return EXPECT_RESULT();
  13841. } /* END test_wc_Sm3Update */
  13842. /*
  13843. * Testing wc_Sm3Copy()
  13844. */
  13845. static int test_wc_Sm3Copy(void)
  13846. {
  13847. EXPECT_DECLS;
  13848. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13849. wc_Sm3 sm3;
  13850. wc_Sm3 sm3Copy;
  13851. byte hash[WC_SM3_DIGEST_SIZE];
  13852. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13853. byte data[WC_SM3_BLOCK_SIZE + 1];
  13854. int i;
  13855. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13856. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13857. /* Invalid Parameters */
  13858. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13859. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13860. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13861. /* Valid Parameters */
  13862. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13863. /* Ensure all parts of data updated during hashing are copied. */
  13864. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13865. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13866. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13867. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13868. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13869. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13870. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13871. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13872. }
  13873. wc_Sm3Free(&sm3Copy);
  13874. wc_Sm3Free(&sm3);
  13875. #endif
  13876. return EXPECT_RESULT();
  13877. } /* END test_wc_Sm3Copy */
  13878. /*
  13879. * Testing wc_Sm3FinalRaw()
  13880. */
  13881. static int test_wc_Sm3FinalRaw(void)
  13882. {
  13883. EXPECT_DECLS;
  13884. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13885. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13886. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13887. !defined(WOLFSSL_NO_HASH_RAW)
  13888. wc_Sm3 sm3;
  13889. byte hash1[WC_SM3_DIGEST_SIZE];
  13890. byte hash2[WC_SM3_DIGEST_SIZE];
  13891. byte hash3[WC_SM3_DIGEST_SIZE];
  13892. byte* hash_test[3] = { hash1, hash2, hash3 };
  13893. int times;
  13894. int i;
  13895. XMEMSET(&sm3, 0, sizeof(sm3));
  13896. /* Initialize */
  13897. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13898. /* Invalid Parameters */
  13899. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13900. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13901. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13902. times = sizeof(hash_test) / sizeof(byte*);
  13903. for (i = 0; i < times; i++) {
  13904. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13905. }
  13906. wc_Sm3Free(&sm3);
  13907. #endif
  13908. return EXPECT_RESULT();
  13909. } /* END test_wc_Sm3FinalRaw */
  13910. /*
  13911. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13912. */
  13913. static int test_wc_Sm3GetSetFlags(void)
  13914. {
  13915. EXPECT_DECLS;
  13916. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13917. wc_Sm3 sm3;
  13918. wc_Sm3 sm3Copy;
  13919. word32 flags = 0;
  13920. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13921. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13922. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13923. ExpectIntEQ(flags, 0);
  13924. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13925. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13926. ExpectIntEQ(flags, 0);
  13927. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13928. ExpectIntEQ(flags, 0);
  13929. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13930. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13931. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13932. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13933. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13934. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13935. wc_Sm3Free(&sm3Copy);
  13936. wc_Sm3Free(&sm3);
  13937. #endif
  13938. return EXPECT_RESULT();
  13939. } /* END test_wc_Sm3Update */
  13940. /*
  13941. * Testing wc_Sm3Hash()
  13942. */
  13943. static int test_wc_Sm3Hash(void)
  13944. {
  13945. EXPECT_DECLS;
  13946. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13947. byte data[WC_SM3_BLOCK_SIZE];
  13948. byte hash[WC_SM3_DIGEST_SIZE];
  13949. /* Invalid parameters. */
  13950. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13951. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13952. /* Valid parameters. */
  13953. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13954. #endif
  13955. return EXPECT_RESULT();
  13956. } /* END test_wc_Sm3Hash */
  13957. /*
  13958. * Test function for wc_HmacSetKey
  13959. */
  13960. static int test_wc_Md5HmacSetKey(void)
  13961. {
  13962. EXPECT_DECLS;
  13963. #if !defined(NO_HMAC) && !defined(NO_MD5)
  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",
  13969. #ifndef HAVE_FIPS
  13970. "Jefe", /* smaller than minimum FIPS key size */
  13971. #endif
  13972. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13973. };
  13974. times = sizeof(keys) / sizeof(char*);
  13975. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13976. for (itr = 0; itr < times; itr++) {
  13977. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13978. (word32)XSTRLEN(keys[itr]));
  13979. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13980. wc_HmacFree(&hmac);
  13981. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13982. #else
  13983. ExpectIntEQ(ret, 0);
  13984. #endif
  13985. }
  13986. /* Bad args. */
  13987. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13988. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13989. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13990. BAD_FUNC_ARG);
  13991. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13992. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13993. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13994. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13995. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13996. #elif defined(HAVE_FIPS)
  13997. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13998. #else
  13999. ExpectIntEQ(ret, 0);
  14000. #endif
  14001. wc_HmacFree(&hmac);
  14002. #endif
  14003. return EXPECT_RESULT();
  14004. } /* END test_wc_Md5HmacSetKey */
  14005. /*
  14006. * testing wc_HmacSetKey() on wc_Sha hash.
  14007. */
  14008. static int test_wc_ShaHmacSetKey(void)
  14009. {
  14010. EXPECT_DECLS;
  14011. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14012. Hmac hmac;
  14013. int ret, times, itr;
  14014. const char* keys[]=
  14015. {
  14016. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14017. "\x0b\x0b\x0b",
  14018. #ifndef HAVE_FIPS
  14019. "Jefe", /* smaller than minimum FIPS key size */
  14020. #endif
  14021. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14022. "\xAA\xAA\xAA"
  14023. };
  14024. times = sizeof(keys) / sizeof(char*);
  14025. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14026. for (itr = 0; itr < times; itr++) {
  14027. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  14028. (word32)XSTRLEN(keys[itr])), 0);
  14029. }
  14030. /* Bad args. */
  14031. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  14032. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14033. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  14034. BAD_FUNC_ARG);
  14035. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14036. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14037. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  14038. #ifdef HAVE_FIPS
  14039. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14040. #else
  14041. ExpectIntEQ(ret, 0);
  14042. #endif
  14043. wc_HmacFree(&hmac);
  14044. #endif
  14045. return EXPECT_RESULT();
  14046. } /* END test_wc_ShaHmacSetKey() */
  14047. /*
  14048. * testing wc_HmacSetKey() on Sha224 hash.
  14049. */
  14050. static int test_wc_Sha224HmacSetKey(void)
  14051. {
  14052. EXPECT_DECLS;
  14053. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14054. Hmac hmac;
  14055. int ret, times, itr;
  14056. const char* keys[]=
  14057. {
  14058. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14059. "\x0b\x0b\x0b",
  14060. #ifndef HAVE_FIPS
  14061. "Jefe", /* smaller than minimum FIPS key size */
  14062. #endif
  14063. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14064. "\xAA\xAA\xAA"
  14065. };
  14066. times = sizeof(keys) / sizeof(char*);
  14067. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14068. for (itr = 0; itr < times; itr++) {
  14069. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  14070. (word32)XSTRLEN(keys[itr])), 0);
  14071. }
  14072. /* Bad args. */
  14073. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  14074. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14075. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  14076. BAD_FUNC_ARG);
  14077. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14078. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14079. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  14080. #ifdef HAVE_FIPS
  14081. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14082. #else
  14083. ExpectIntEQ(ret, 0);
  14084. #endif
  14085. wc_HmacFree(&hmac);
  14086. #endif
  14087. return EXPECT_RESULT();
  14088. } /* END test_wc_Sha224HmacSetKey() */
  14089. /*
  14090. * testing wc_HmacSetKey() on Sha256 hash
  14091. */
  14092. static int test_wc_Sha256HmacSetKey(void)
  14093. {
  14094. EXPECT_DECLS;
  14095. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14096. Hmac hmac;
  14097. int ret, times, itr;
  14098. const char* keys[]=
  14099. {
  14100. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14101. "\x0b\x0b\x0b",
  14102. #ifndef HAVE_FIPS
  14103. "Jefe", /* smaller than minimum FIPS key size */
  14104. #endif
  14105. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14106. "\xAA\xAA\xAA"
  14107. };
  14108. times = sizeof(keys) / sizeof(char*);
  14109. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14110. for (itr = 0; itr < times; itr++) {
  14111. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  14112. (word32)XSTRLEN(keys[itr])), 0);
  14113. }
  14114. /* Bad args. */
  14115. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  14116. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14117. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  14118. BAD_FUNC_ARG);
  14119. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14120. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14121. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  14122. #ifdef HAVE_FIPS
  14123. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14124. #else
  14125. ExpectIntEQ(ret, 0);
  14126. #endif
  14127. wc_HmacFree(&hmac);
  14128. #endif
  14129. return EXPECT_RESULT();
  14130. } /* END test_wc_Sha256HmacSetKey() */
  14131. /*
  14132. * testing wc_HmacSetKey on Sha384 hash.
  14133. */
  14134. static int test_wc_Sha384HmacSetKey(void)
  14135. {
  14136. EXPECT_DECLS;
  14137. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14138. Hmac hmac;
  14139. int ret, times, itr;
  14140. const char* keys[]=
  14141. {
  14142. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14143. "\x0b\x0b\x0b",
  14144. #ifndef HAVE_FIPS
  14145. "Jefe", /* smaller than minimum FIPS key size */
  14146. #endif
  14147. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14148. "\xAA\xAA\xAA"
  14149. };
  14150. times = sizeof(keys) / sizeof(char*);
  14151. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14152. for (itr = 0; itr < times; itr++) {
  14153. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14154. (word32)XSTRLEN(keys[itr])), 0);
  14155. }
  14156. /* Bad args. */
  14157. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14158. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14159. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  14160. BAD_FUNC_ARG);
  14161. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14162. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14163. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14164. #ifdef HAVE_FIPS
  14165. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14166. #else
  14167. ExpectIntEQ(ret, 0);
  14168. #endif
  14169. wc_HmacFree(&hmac);
  14170. #endif
  14171. return EXPECT_RESULT();
  14172. } /* END test_wc_Sha384HmacSetKey() */
  14173. /*
  14174. * testing wc_HmacUpdate on wc_Md5 hash.
  14175. */
  14176. static int test_wc_Md5HmacUpdate(void)
  14177. {
  14178. EXPECT_DECLS;
  14179. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14180. Hmac hmac;
  14181. testVector a, b;
  14182. #ifdef HAVE_FIPS
  14183. const char* keys =
  14184. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14185. #else
  14186. const char* keys = "Jefe";
  14187. #endif
  14188. a.input = "what do ya want for nothing?";
  14189. a.inLen = XSTRLEN(a.input);
  14190. b.input = "Hi There";
  14191. b.inLen = XSTRLEN(b.input);
  14192. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14193. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  14194. (word32)XSTRLEN(keys)), 0);
  14195. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14196. /* Update Hmac. */
  14197. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14198. /* Test bad args. */
  14199. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14200. BAD_FUNC_ARG);
  14201. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14202. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14203. wc_HmacFree(&hmac);
  14204. #endif
  14205. return EXPECT_RESULT();
  14206. } /* END test_wc_Md5HmacUpdate */
  14207. /*
  14208. * testing wc_HmacUpdate on SHA hash.
  14209. */
  14210. static int test_wc_ShaHmacUpdate(void)
  14211. {
  14212. EXPECT_DECLS;
  14213. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14214. Hmac hmac;
  14215. testVector a, b;
  14216. #ifdef HAVE_FIPS
  14217. const char* keys =
  14218. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14219. #else
  14220. const char* keys = "Jefe";
  14221. #endif
  14222. a.input = "what do ya want for nothing?";
  14223. a.inLen = XSTRLEN(a.input);
  14224. b.input = "Hi There";
  14225. b.inLen = XSTRLEN(b.input);
  14226. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14227. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  14228. (word32)XSTRLEN(keys)), 0);
  14229. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14230. /* Update Hmac. */
  14231. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14232. /* Test bad args. */
  14233. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14234. BAD_FUNC_ARG);
  14235. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14236. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14237. wc_HmacFree(&hmac);
  14238. #endif
  14239. return EXPECT_RESULT();
  14240. } /* END test_wc_ShaHmacUpdate */
  14241. /*
  14242. * testing wc_HmacUpdate on SHA224 hash.
  14243. */
  14244. static int test_wc_Sha224HmacUpdate(void)
  14245. {
  14246. EXPECT_DECLS;
  14247. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14248. Hmac hmac;
  14249. testVector a, b;
  14250. #ifdef HAVE_FIPS
  14251. const char* keys =
  14252. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14253. #else
  14254. const char* keys = "Jefe";
  14255. #endif
  14256. a.input = "what do ya want for nothing?";
  14257. a.inLen = XSTRLEN(a.input);
  14258. b.input = "Hi There";
  14259. b.inLen = XSTRLEN(b.input);
  14260. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14261. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  14262. (word32)XSTRLEN(keys)), 0);
  14263. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14264. /* Update Hmac. */
  14265. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14266. /* Test bad args. */
  14267. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14268. BAD_FUNC_ARG);
  14269. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14270. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14271. wc_HmacFree(&hmac);
  14272. #endif
  14273. return EXPECT_RESULT();
  14274. } /* END test_wc_Sha224HmacUpdate */
  14275. /*
  14276. * testing wc_HmacUpdate on SHA256 hash.
  14277. */
  14278. static int test_wc_Sha256HmacUpdate(void)
  14279. {
  14280. EXPECT_DECLS;
  14281. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14282. Hmac hmac;
  14283. testVector a, b;
  14284. #ifdef HAVE_FIPS
  14285. const char* keys =
  14286. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14287. #else
  14288. const char* keys = "Jefe";
  14289. #endif
  14290. a.input = "what do ya want for nothing?";
  14291. a.inLen = XSTRLEN(a.input);
  14292. b.input = "Hi There";
  14293. b.inLen = XSTRLEN(b.input);
  14294. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14295. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  14296. (word32)XSTRLEN(keys)), 0);
  14297. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14298. /* Update Hmac. */
  14299. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14300. /* Test bad args. */
  14301. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14302. BAD_FUNC_ARG);
  14303. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14304. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14305. wc_HmacFree(&hmac);
  14306. #endif
  14307. return EXPECT_RESULT();
  14308. } /* END test_wc_Sha256HmacUpdate */
  14309. /*
  14310. * testing wc_HmacUpdate on SHA384 hash.
  14311. */
  14312. static int test_wc_Sha384HmacUpdate(void)
  14313. {
  14314. EXPECT_DECLS;
  14315. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14316. Hmac hmac;
  14317. testVector a, b;
  14318. #ifdef HAVE_FIPS
  14319. const char* keys =
  14320. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14321. #else
  14322. const char* keys = "Jefe";
  14323. #endif
  14324. a.input = "what do ya want for nothing?";
  14325. a.inLen = XSTRLEN(a.input);
  14326. b.input = "Hi There";
  14327. b.inLen = XSTRLEN(b.input);
  14328. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14329. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  14330. (word32)XSTRLEN(keys)), 0);
  14331. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14332. /* Update Hmac. */
  14333. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14334. /* Test bad args. */
  14335. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14336. BAD_FUNC_ARG);
  14337. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14338. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14339. wc_HmacFree(&hmac);
  14340. #endif
  14341. return EXPECT_RESULT();
  14342. } /* END test_wc_Sha384HmacUpdate */
  14343. /*
  14344. * Testing wc_HmacFinal() with MD5
  14345. */
  14346. static int test_wc_Md5HmacFinal(void)
  14347. {
  14348. EXPECT_DECLS;
  14349. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14350. Hmac hmac;
  14351. byte hash[WC_MD5_DIGEST_SIZE];
  14352. testVector a;
  14353. const char* key;
  14354. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14355. a.input = "Hi There";
  14356. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14357. "\x9d";
  14358. a.inLen = XSTRLEN(a.input);
  14359. a.outLen = XSTRLEN(a.output);
  14360. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14361. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  14362. 0);
  14363. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14364. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14365. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  14366. /* Try bad parameters. */
  14367. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14368. #ifndef HAVE_FIPS
  14369. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14370. #endif
  14371. wc_HmacFree(&hmac);
  14372. #endif
  14373. return EXPECT_RESULT();
  14374. } /* END test_wc_Md5HmacFinal */
  14375. /*
  14376. * Testing wc_HmacFinal() with SHA
  14377. */
  14378. static int test_wc_ShaHmacFinal(void)
  14379. {
  14380. EXPECT_DECLS;
  14381. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14382. Hmac hmac;
  14383. byte hash[WC_SHA_DIGEST_SIZE];
  14384. testVector a;
  14385. const char* key;
  14386. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14387. "\x0b\x0b\x0b";
  14388. a.input = "Hi There";
  14389. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14390. "\x8e\xf1\x46\xbe\x00";
  14391. a.inLen = XSTRLEN(a.input);
  14392. a.outLen = XSTRLEN(a.output);
  14393. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14394. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  14395. 0);
  14396. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14397. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14398. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  14399. /* Try bad parameters. */
  14400. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14401. #ifndef HAVE_FIPS
  14402. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14403. #endif
  14404. wc_HmacFree(&hmac);
  14405. #endif
  14406. return EXPECT_RESULT();
  14407. } /* END test_wc_ShaHmacFinal */
  14408. /*
  14409. * Testing wc_HmacFinal() with SHA224
  14410. */
  14411. static int test_wc_Sha224HmacFinal(void)
  14412. {
  14413. EXPECT_DECLS;
  14414. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14415. Hmac hmac;
  14416. byte hash[WC_SHA224_DIGEST_SIZE];
  14417. testVector a;
  14418. const char* key;
  14419. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14420. "\x0b\x0b\x0b";
  14421. a.input = "Hi There";
  14422. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14423. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14424. a.inLen = XSTRLEN(a.input);
  14425. a.outLen = XSTRLEN(a.output);
  14426. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14427. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  14428. (word32)XSTRLEN(key)), 0);
  14429. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14430. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14431. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  14432. /* Try bad parameters. */
  14433. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14434. #ifndef HAVE_FIPS
  14435. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14436. #endif
  14437. wc_HmacFree(&hmac);
  14438. #endif
  14439. return EXPECT_RESULT();
  14440. } /* END test_wc_Sha224HmacFinal */
  14441. /*
  14442. * Testing wc_HmacFinal() with SHA256
  14443. */
  14444. static int test_wc_Sha256HmacFinal(void)
  14445. {
  14446. EXPECT_DECLS;
  14447. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14448. Hmac hmac;
  14449. byte hash[WC_SHA256_DIGEST_SIZE];
  14450. testVector a;
  14451. const char* key;
  14452. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14453. "\x0b\x0b\x0b";
  14454. a.input = "Hi There";
  14455. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14456. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14457. "\xcf\xf7";
  14458. a.inLen = XSTRLEN(a.input);
  14459. a.outLen = XSTRLEN(a.output);
  14460. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14461. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  14462. (word32)XSTRLEN(key)), 0);
  14463. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14464. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14465. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  14466. /* Try bad parameters. */
  14467. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14468. #ifndef HAVE_FIPS
  14469. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14470. #endif
  14471. wc_HmacFree(&hmac);
  14472. #endif
  14473. return EXPECT_RESULT();
  14474. } /* END test_wc_Sha256HmacFinal */
  14475. /*
  14476. * Testing wc_HmacFinal() with SHA384
  14477. */
  14478. static int test_wc_Sha384HmacFinal(void)
  14479. {
  14480. EXPECT_DECLS;
  14481. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14482. Hmac hmac;
  14483. byte hash[WC_SHA384_DIGEST_SIZE];
  14484. testVector a;
  14485. const char* key;
  14486. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14487. "\x0b\x0b\x0b";
  14488. a.input = "Hi There";
  14489. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14490. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14491. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14492. "\xfa\x9c\xb6";
  14493. a.inLen = XSTRLEN(a.input);
  14494. a.outLen = XSTRLEN(a.output);
  14495. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14496. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14497. (word32)XSTRLEN(key)), 0);
  14498. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14499. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14500. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14501. /* Try bad parameters. */
  14502. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14503. #ifndef HAVE_FIPS
  14504. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14505. #endif
  14506. wc_HmacFree(&hmac);
  14507. #endif
  14508. return EXPECT_RESULT();
  14509. } /* END test_wc_Sha384HmacFinal */
  14510. /*
  14511. * Testing wc_InitCmac()
  14512. */
  14513. static int test_wc_InitCmac(void)
  14514. {
  14515. EXPECT_DECLS;
  14516. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14517. Cmac cmac1;
  14518. Cmac cmac2;
  14519. Cmac cmac3;
  14520. /* AES 128 key. */
  14521. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14522. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14523. /* AES 192 key. */
  14524. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14525. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14526. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14527. /* AES 256 key. */
  14528. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14529. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14530. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14531. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14532. word32 key1Sz = (word32)sizeof(key1) - 1;
  14533. word32 key2Sz = (word32)sizeof(key2) - 1;
  14534. word32 key3Sz = (word32)sizeof(key3) - 1;
  14535. int type = WC_CMAC_AES;
  14536. (void)key1;
  14537. (void)key1Sz;
  14538. (void)key2;
  14539. (void)key2Sz;
  14540. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14541. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14542. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14543. #ifdef WOLFSSL_AES_128
  14544. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14545. #endif
  14546. #ifdef WOLFSSL_AES_192
  14547. wc_AesFree(&cmac1.aes);
  14548. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14549. #endif
  14550. #ifdef WOLFSSL_AES_256
  14551. wc_AesFree(&cmac2.aes);
  14552. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14553. #endif
  14554. wc_AesFree(&cmac3.aes);
  14555. /* Test bad args. */
  14556. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14557. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14558. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14559. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14560. #endif
  14561. return EXPECT_RESULT();
  14562. } /* END test_wc_InitCmac */
  14563. /*
  14564. * Testing wc_CmacUpdate()
  14565. */
  14566. static int test_wc_CmacUpdate(void)
  14567. {
  14568. EXPECT_DECLS;
  14569. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14570. Cmac cmac;
  14571. byte key[] = {
  14572. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14573. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14574. };
  14575. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14576. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14577. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14578. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14579. "\xf4";
  14580. word32 inSz = (word32)sizeof(in) - 1;
  14581. word32 keySz = (word32)sizeof(key);
  14582. int type = WC_CMAC_AES;
  14583. XMEMSET(&cmac, 0, sizeof(Cmac));
  14584. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14585. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14586. /* Test bad args. */
  14587. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14588. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14589. wc_AesFree(&cmac.aes);
  14590. #endif
  14591. return EXPECT_RESULT();
  14592. } /* END test_wc_CmacUpdate */
  14593. /*
  14594. * Testing wc_CmacFinal()
  14595. */
  14596. static int test_wc_CmacFinal(void)
  14597. {
  14598. EXPECT_DECLS;
  14599. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14600. Cmac cmac;
  14601. byte key[] = {
  14602. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14603. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14604. };
  14605. byte msg[] = {
  14606. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14607. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14608. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14609. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14610. 0xf4
  14611. };
  14612. /* Test vectors from CMACGenAES128.rsp from
  14613. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14614. * Per RFC4493 truncation of lsb is possible.
  14615. */
  14616. byte expMac[] = {
  14617. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14618. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14619. };
  14620. byte mac[AES_BLOCK_SIZE];
  14621. word32 msgSz = (word32)sizeof(msg);
  14622. word32 keySz = (word32)sizeof(key);
  14623. word32 macSz = sizeof(mac);
  14624. word32 badMacSz = 17;
  14625. int expMacSz = sizeof(expMac);
  14626. int type = WC_CMAC_AES;
  14627. XMEMSET(&cmac, 0, sizeof(Cmac));
  14628. XMEMSET(mac, 0, macSz);
  14629. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14630. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14631. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14632. /* Pass in bad args. */
  14633. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14634. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14635. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14636. /* For the last call, use the API with implicit wc_CmacFree(). */
  14637. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14638. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14639. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14640. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14641. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14642. /* Pass in bad args. */
  14643. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14644. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14645. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14646. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14647. #endif
  14648. return EXPECT_RESULT();
  14649. } /* END test_wc_CmacFinal */
  14650. /*
  14651. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14652. */
  14653. static int test_wc_AesCmacGenerate(void)
  14654. {
  14655. EXPECT_DECLS;
  14656. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14657. byte key[] = {
  14658. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14659. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14660. };
  14661. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14662. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14663. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14664. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14665. byte mac[AES_BLOCK_SIZE];
  14666. word32 keySz = sizeof(key);
  14667. word32 macSz = sizeof(mac);
  14668. word32 msgSz = sizeof(msg) - 1;
  14669. word32 expMacSz = sizeof(expMac) - 1;
  14670. XMEMSET(mac, 0, macSz);
  14671. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14672. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14673. /* Pass in bad args. */
  14674. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14675. BAD_FUNC_ARG);
  14676. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14677. BAD_FUNC_ARG);
  14678. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14679. BAD_FUNC_ARG);
  14680. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14681. BAD_FUNC_ARG);
  14682. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14683. /* Test bad args. */
  14684. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14685. BAD_FUNC_ARG);
  14686. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14687. BAD_FUNC_ARG);
  14688. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14689. BAD_FUNC_ARG);
  14690. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14691. BAD_FUNC_ARG);
  14692. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14693. BAD_FUNC_ARG);
  14694. #endif
  14695. return EXPECT_RESULT();
  14696. } /* END test_wc_AesCmacGenerate */
  14697. /*
  14698. * Testing streaming AES-GCM API.
  14699. */
  14700. static int test_wc_AesGcmStream(void)
  14701. {
  14702. EXPECT_DECLS;
  14703. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14704. defined(WOLFSSL_AESGCM_STREAM)
  14705. int i;
  14706. WC_RNG rng[1];
  14707. Aes aesEnc[1];
  14708. Aes aesDec[1];
  14709. byte tag[AES_BLOCK_SIZE];
  14710. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14711. byte out[AES_BLOCK_SIZE * 3 + 2];
  14712. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14713. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14714. byte key[AES_128_KEY_SIZE] = { 0, };
  14715. byte iv[AES_IV_SIZE] = { 1, };
  14716. byte ivOut[AES_IV_SIZE];
  14717. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14718. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14719. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14720. };
  14721. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14722. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14723. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14724. };
  14725. static const byte expTag[AES_BLOCK_SIZE] = {
  14726. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14727. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14728. };
  14729. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14730. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14731. XMEMSET(&aesDec, 0, sizeof(Aes));
  14732. /* Create a random for generating IV/nonce. */
  14733. ExpectIntEQ(wc_InitRng(rng), 0);
  14734. /* Initialize data structures. */
  14735. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14736. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14737. /* BadParameters to streaming init. */
  14738. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14739. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14740. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14741. BAD_FUNC_ARG);
  14742. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14743. BAD_FUNC_ARG);
  14744. /* Bad parameters to encrypt update. */
  14745. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14746. BAD_FUNC_ARG);
  14747. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14748. BAD_FUNC_ARG);
  14749. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14750. BAD_FUNC_ARG);
  14751. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14752. BAD_FUNC_ARG);
  14753. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14754. BAD_FUNC_ARG);
  14755. /* Bad parameters to decrypt update. */
  14756. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14757. BAD_FUNC_ARG);
  14758. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14759. BAD_FUNC_ARG);
  14760. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14761. BAD_FUNC_ARG);
  14762. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14763. BAD_FUNC_ARG);
  14764. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14765. BAD_FUNC_ARG);
  14766. /* Bad parameters to encrypt final. */
  14767. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14768. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14769. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14770. BAD_FUNC_ARG);
  14771. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14772. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14773. BAD_FUNC_ARG);
  14774. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14775. BAD_FUNC_ARG);
  14776. /* Bad parameters to decrypt final. */
  14777. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14778. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14779. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14780. BAD_FUNC_ARG);
  14781. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14782. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14783. BAD_FUNC_ARG);
  14784. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14785. BAD_FUNC_ARG);
  14786. /* Check calling final before setting key fails. */
  14787. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14788. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14789. /* Check calling update before setting key else fails. */
  14790. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14791. MISSING_KEY);
  14792. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14793. MISSING_KEY);
  14794. /* Set key but not IV. */
  14795. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14796. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14797. /* Check calling final before setting IV fails. */
  14798. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14799. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14800. /* Check calling update before setting IV else fails. */
  14801. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14802. MISSING_IV);
  14803. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14804. MISSING_IV);
  14805. /* Set IV using fixed part IV and external IV APIs. */
  14806. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14807. rng), 0);
  14808. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14809. GCM_NONCE_MID_SZ), 0);
  14810. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14811. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14812. /* Encrypt and decrypt data. */
  14813. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14814. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14815. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14816. /* Finalize and check tag matches. */
  14817. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14818. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14819. /* Set key and IV through streaming init API. */
  14820. wc_AesFree(aesEnc);
  14821. wc_AesFree(aesDec);
  14822. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14823. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14824. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14825. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14826. /* Encrypt/decrypt one block and AAD of one block. */
  14827. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14828. AES_BLOCK_SIZE), 0);
  14829. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14830. AES_BLOCK_SIZE), 0);
  14831. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14832. /* Finalize and check tag matches. */
  14833. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14834. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14835. /* Set key and IV through streaming init API. */
  14836. wc_AesFree(aesEnc);
  14837. wc_AesFree(aesDec);
  14838. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14839. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14840. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14841. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14842. /* No data to encrypt/decrypt one byte of AAD. */
  14843. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14844. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14845. /* Finalize and check tag matches. */
  14846. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14847. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14848. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14849. /* Set key and IV through streaming init API. */
  14850. wc_AesFree(aesEnc);
  14851. wc_AesFree(aesDec);
  14852. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14853. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14854. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14855. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14856. /* Encrypt/decrypt one byte and no AAD. */
  14857. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14858. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14859. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14860. /* Finalize and check tag matches. */
  14861. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14862. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14863. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14864. /* Set key and IV through streaming init API. */
  14865. wc_AesFree(aesEnc);
  14866. wc_AesFree(aesDec);
  14867. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14868. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14869. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14870. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14871. /* Encryption AES is one byte at a time */
  14872. for (i = 0; i < (int)sizeof(aad); i++) {
  14873. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14874. 0);
  14875. }
  14876. for (i = 0; i < (int)sizeof(in); i++) {
  14877. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14878. 0);
  14879. }
  14880. /* Decryption AES is two bytes at a time */
  14881. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14882. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14883. 0);
  14884. }
  14885. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14886. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14887. 0), 0);
  14888. }
  14889. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14890. /* Finalize and check tag matches. */
  14891. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14892. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14893. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14894. /* Check streaming encryption can be decrypted with one shot. */
  14895. wc_AesFree(aesDec);
  14896. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14897. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14898. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14899. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14900. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14901. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14902. wc_AesFree(aesEnc);
  14903. wc_AesFree(aesDec);
  14904. wc_FreeRng(rng);
  14905. #endif
  14906. return EXPECT_RESULT();
  14907. } /* END test_wc_AesGcmStream */
  14908. /*
  14909. * Testing streaming SM4 API.
  14910. */
  14911. static int test_wc_Sm4(void)
  14912. {
  14913. int res = TEST_SKIPPED;
  14914. #ifdef WOLFSSL_SM4
  14915. EXPECT_DECLS;
  14916. wc_Sm4 sm4;
  14917. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14918. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14919. unsigned char key[SM4_KEY_SIZE];
  14920. #endif
  14921. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14922. unsigned char iv[SM4_IV_SIZE];
  14923. #endif
  14924. /* Invalid parameters - wc_Sm4Init */
  14925. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14926. /* Valid cases - wc_Sm4Init */
  14927. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14928. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14929. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14930. XMEMSET(key, 0, sizeof(key));
  14931. /* Invalid parameters - wc_Sm4SetKey. */
  14932. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14933. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14934. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14935. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14936. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14937. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14938. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14939. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14940. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14941. /* Valid cases - wc_Sm4SetKey. */
  14942. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14943. #endif
  14944. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14945. XMEMSET(iv, 0, sizeof(iv));
  14946. /* Invalid parameters - wc_Sm4SetIV. */
  14947. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14948. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14949. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14950. /* Valid cases - wc_Sm4SetIV. */
  14951. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14952. #endif
  14953. /* Valid cases - wc_Sm4Free */
  14954. wc_Sm4Free(NULL);
  14955. wc_Sm4Free(&sm4);
  14956. res = EXPECT_RESULT();
  14957. #endif
  14958. return res;
  14959. } /* END test_wc_Sm4 */
  14960. /*
  14961. * Testing block based SM4-ECB API.
  14962. */
  14963. static int test_wc_Sm4Ecb(void)
  14964. {
  14965. int res = TEST_SKIPPED;
  14966. #ifdef WOLFSSL_SM4_ECB
  14967. EXPECT_DECLS;
  14968. wc_Sm4 sm4;
  14969. unsigned char key[SM4_KEY_SIZE];
  14970. unsigned char in[SM4_BLOCK_SIZE * 2];
  14971. unsigned char out[SM4_BLOCK_SIZE * 2];
  14972. unsigned char out2[SM4_BLOCK_SIZE];
  14973. XMEMSET(key, 0, sizeof(key));
  14974. XMEMSET(in, 0, sizeof(in));
  14975. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14976. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14977. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14978. /* Tested in test_wc_Sm4. */
  14979. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14980. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14981. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14982. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14983. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14984. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14985. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14986. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14987. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14988. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14989. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14990. /* Valid cases - wc_Sm4EcbEncrypt. */
  14991. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14992. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14993. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14994. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14995. /* In and out are same pointer. */
  14996. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14997. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14998. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14999. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15000. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15001. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15002. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15003. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15004. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15005. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15006. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15007. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15008. /* Valid cases - wc_Sm4EcbDecrypt. */
  15009. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  15010. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15011. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15012. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15013. /* In and out are same pointer. */
  15014. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15015. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15016. wc_Sm4Free(&sm4);
  15017. res = EXPECT_RESULT();
  15018. #endif
  15019. return res;
  15020. } /* END test_wc_Sm4Ecb */
  15021. /*
  15022. * Testing block based SM4-CBC API.
  15023. */
  15024. static int test_wc_Sm4Cbc(void)
  15025. {
  15026. int res = TEST_SKIPPED;
  15027. #ifdef WOLFSSL_SM4_CBC
  15028. EXPECT_DECLS;
  15029. wc_Sm4 sm4;
  15030. unsigned char key[SM4_KEY_SIZE];
  15031. unsigned char iv[SM4_IV_SIZE];
  15032. unsigned char in[SM4_BLOCK_SIZE * 2];
  15033. unsigned char out[SM4_BLOCK_SIZE * 2];
  15034. unsigned char out2[SM4_BLOCK_SIZE];
  15035. XMEMSET(key, 0, sizeof(key));
  15036. XMEMSET(iv, 0, sizeof(iv));
  15037. XMEMSET(in, 0, sizeof(in));
  15038. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15039. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15040. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  15041. /* Tested in test_wc_Sm4. */
  15042. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15043. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  15044. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  15045. /* Tested in test_wc_Sm4. */
  15046. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15047. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  15048. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15049. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15050. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15051. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15052. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15053. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15054. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15055. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15056. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15057. /* Valid cases - wc_Sm4CbcEncrypt. */
  15058. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  15059. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15060. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15061. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15062. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15063. /* In and out are same pointer. */
  15064. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15065. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15066. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15067. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  15068. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15069. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15070. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15071. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15072. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15073. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15074. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15075. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15076. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15077. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15078. /* Valid cases - wc_Sm4CbcDecrypt. */
  15079. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  15080. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15081. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15082. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15083. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15084. /* In and out are same pointer. */
  15085. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15086. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15087. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15088. wc_Sm4Free(&sm4);
  15089. res = EXPECT_RESULT();
  15090. #endif
  15091. return res;
  15092. } /* END test_wc_Sm4Cbc */
  15093. /*
  15094. * Testing streaming SM4-CTR API.
  15095. */
  15096. static int test_wc_Sm4Ctr(void)
  15097. {
  15098. int res = TEST_SKIPPED;
  15099. #ifdef WOLFSSL_SM4_CTR
  15100. EXPECT_DECLS;
  15101. wc_Sm4 sm4;
  15102. unsigned char key[SM4_KEY_SIZE];
  15103. unsigned char iv[SM4_IV_SIZE];
  15104. unsigned char in[SM4_BLOCK_SIZE * 4];
  15105. unsigned char out[SM4_BLOCK_SIZE * 4];
  15106. unsigned char out2[SM4_BLOCK_SIZE * 4];
  15107. word32 chunk;
  15108. word32 i;
  15109. XMEMSET(key, 0, sizeof(key));
  15110. XMEMSET(iv, 0, sizeof(iv));
  15111. XMEMSET(in, 0, sizeof(in));
  15112. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15113. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15114. /* Tested in test_wc_Sm4. */
  15115. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15116. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  15117. /* Tested in test_wc_Sm4. */
  15118. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15119. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  15120. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15121. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  15122. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  15123. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  15124. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15125. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15126. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15127. /* Valid cases - wc_Sm4CtrEncrypt. */
  15128. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  15129. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  15130. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15131. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  15132. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  15133. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15134. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15135. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  15136. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15137. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15138. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15139. /* In and out are same pointer. Also check encrypt of cipher text produces
  15140. * plaintext.
  15141. */
  15142. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15143. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  15144. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15145. /* Chunking tests. */
  15146. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15147. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  15148. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  15149. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15150. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  15151. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  15152. }
  15153. if (i < (word32)sizeof(in)) {
  15154. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  15155. (word32)sizeof(in) - i), 0);
  15156. }
  15157. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  15158. }
  15159. for (i = 0; i < (word32)sizeof(iv); i++) {
  15160. iv[i] = 0xff;
  15161. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15162. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15163. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15164. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  15165. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  15166. }
  15167. wc_Sm4Free(&sm4);
  15168. res = EXPECT_RESULT();
  15169. #endif
  15170. return res;
  15171. } /* END test_wc_Sm4Ctr */
  15172. /*
  15173. * Testing stream SM4-GCM API.
  15174. */
  15175. static int test_wc_Sm4Gcm(void)
  15176. {
  15177. int res = TEST_SKIPPED;
  15178. #ifdef WOLFSSL_SM4_GCM
  15179. EXPECT_DECLS;
  15180. wc_Sm4 sm4;
  15181. unsigned char key[SM4_KEY_SIZE];
  15182. unsigned char nonce[GCM_NONCE_MAX_SZ];
  15183. unsigned char in[SM4_BLOCK_SIZE * 2];
  15184. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15185. unsigned char out[SM4_BLOCK_SIZE * 2];
  15186. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15187. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15188. unsigned char tag[SM4_BLOCK_SIZE];
  15189. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15190. word32 i;
  15191. XMEMSET(key, 0, sizeof(key));
  15192. XMEMSET(nonce, 0, sizeof(nonce));
  15193. XMEMSET(in, 0, sizeof(in));
  15194. XMEMSET(in2, 0, sizeof(in2));
  15195. XMEMSET(aad, 0, sizeof(aad));
  15196. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15197. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15198. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15199. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15200. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15201. /* Invalid parameters - wc_Sm4GcmSetKey. */
  15202. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15203. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15204. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  15205. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15206. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15207. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15208. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15209. /* Valid parameters - wc_Sm4GcmSetKey. */
  15210. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15211. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  15212. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15213. 0), BAD_FUNC_ARG);
  15214. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15215. 0), BAD_FUNC_ARG);
  15216. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15217. 0), BAD_FUNC_ARG);
  15218. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15219. 0), BAD_FUNC_ARG);
  15220. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15221. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15222. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15223. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15224. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15225. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15226. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15227. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15228. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15229. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15230. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15231. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15232. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15233. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15234. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15235. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15236. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15237. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15238. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15239. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15240. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  15241. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15242. 0), BAD_FUNC_ARG);
  15243. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15244. 0), BAD_FUNC_ARG);
  15245. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15246. 0), BAD_FUNC_ARG);
  15247. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15248. 0), BAD_FUNC_ARG);
  15249. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15250. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15251. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15252. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15253. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15254. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15255. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15256. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15257. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15258. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15259. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15260. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15261. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15262. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15263. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15264. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15265. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15266. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15267. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15268. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15269. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15270. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15271. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15272. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15273. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15274. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15275. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15276. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15277. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15278. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15279. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15280. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15281. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15282. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15283. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15284. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15285. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15286. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15287. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15288. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15289. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15290. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15291. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15292. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15293. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15294. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15295. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15296. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15297. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15298. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15299. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15300. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15301. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15302. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15303. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15304. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15305. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15306. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15307. SM4_GCM_AUTH_E);
  15308. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15309. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  15310. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15311. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15312. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15313. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15314. }
  15315. /* Check different in/out sizes. */
  15316. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  15317. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15318. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  15319. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15320. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  15321. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15322. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15323. XMEMCPY(out2, out, i - 1);
  15324. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  15325. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15326. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15327. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  15328. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15329. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15330. }
  15331. /* Force the counter to roll over in first byte. */
  15332. {
  15333. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15334. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15335. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15336. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15337. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15338. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15339. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15340. }
  15341. wc_Sm4Free(&sm4);
  15342. res = EXPECT_RESULT();
  15343. #endif
  15344. return res;
  15345. } /* END test_wc_Sm4Gcm */
  15346. /*
  15347. * Testing stream SM4-CCM API.
  15348. */
  15349. static int test_wc_Sm4Ccm(void)
  15350. {
  15351. int res = TEST_SKIPPED;
  15352. #ifdef WOLFSSL_SM4_CCM
  15353. EXPECT_DECLS;
  15354. wc_Sm4 sm4;
  15355. unsigned char key[SM4_KEY_SIZE];
  15356. unsigned char nonce[CCM_NONCE_MAX_SZ];
  15357. unsigned char in[SM4_BLOCK_SIZE * 2];
  15358. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15359. unsigned char out[SM4_BLOCK_SIZE * 2];
  15360. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15361. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15362. unsigned char tag[SM4_BLOCK_SIZE];
  15363. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15364. word32 i;
  15365. XMEMSET(key, 0, sizeof(key));
  15366. XMEMSET(nonce, 0, sizeof(nonce));
  15367. XMEMSET(in, 0, sizeof(in));
  15368. XMEMSET(in2, 0, sizeof(in2));
  15369. XMEMSET(aad, 0, sizeof(aad));
  15370. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15371. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15372. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15373. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15374. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15375. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15376. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  15377. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15378. 0), BAD_FUNC_ARG);
  15379. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15380. 0), BAD_FUNC_ARG);
  15381. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15382. 0), BAD_FUNC_ARG);
  15383. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15384. 0), BAD_FUNC_ARG);
  15385. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15386. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15387. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15388. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15389. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15390. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15391. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15392. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15393. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15394. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15395. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15396. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15397. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15398. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15399. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15400. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15401. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15402. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15403. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15404. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15405. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  15406. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15407. 0), BAD_FUNC_ARG);
  15408. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15409. 0), BAD_FUNC_ARG);
  15410. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15411. 0), BAD_FUNC_ARG);
  15412. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15413. 0), BAD_FUNC_ARG);
  15414. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15415. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15416. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15417. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15418. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15419. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15420. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15421. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15422. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15423. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15424. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15425. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15426. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15427. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15428. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15429. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15430. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15431. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15432. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15433. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15434. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15435. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15436. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15437. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15438. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15439. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15440. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15441. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15442. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15443. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15444. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15445. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15446. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15447. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15448. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15449. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15450. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15451. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15452. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15453. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15454. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15455. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15456. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15457. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15458. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15459. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15460. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15461. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15462. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  15463. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15464. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15465. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15466. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15467. }
  15468. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15469. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15470. SM4_CCM_AUTH_E);
  15471. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15472. for (i = 0; i < 4; i++) {
  15473. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15474. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15475. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15476. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15477. }
  15478. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  15479. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15480. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15481. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15482. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15483. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15484. }
  15485. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15486. * Even values in range 4..SM4_BLOCK_SIZE.
  15487. */
  15488. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15489. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15490. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15491. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15492. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15493. }
  15494. /* Check different in/out sizes. */
  15495. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15496. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15497. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15498. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15499. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15500. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15501. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15502. XMEMCPY(out2, out, i - 1);
  15503. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15504. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15505. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15506. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15507. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15508. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15509. }
  15510. /* Force the counter to roll over in first byte. */
  15511. {
  15512. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15513. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15514. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15515. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15516. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15517. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15518. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15519. }
  15520. wc_Sm4Free(&sm4);
  15521. res = EXPECT_RESULT();
  15522. #endif
  15523. return res;
  15524. } /* END test_wc_Sm4Ccm */
  15525. /*
  15526. * unit test for wc_Des3_SetIV()
  15527. */
  15528. static int test_wc_Des3_SetIV(void)
  15529. {
  15530. EXPECT_DECLS;
  15531. #ifndef NO_DES3
  15532. Des3 des;
  15533. const byte key[] = {
  15534. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15535. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15536. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15537. };
  15538. const byte iv[] = {
  15539. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15540. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15541. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15542. };
  15543. XMEMSET(&des, 0, sizeof(Des3));
  15544. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15545. /* DES_ENCRYPTION or DES_DECRYPTION */
  15546. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15547. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15548. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15549. /* Test explicitly wc_Des3_SetIV() */
  15550. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15551. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15552. #endif
  15553. wc_Des3Free(&des);
  15554. #endif
  15555. return EXPECT_RESULT();
  15556. } /* END test_wc_Des3_SetIV */
  15557. /*
  15558. * unit test for wc_Des3_SetKey()
  15559. */
  15560. static int test_wc_Des3_SetKey(void)
  15561. {
  15562. EXPECT_DECLS;
  15563. #ifndef NO_DES3
  15564. Des3 des;
  15565. const byte key[] = {
  15566. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15567. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15568. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15569. };
  15570. const byte iv[] = {
  15571. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15572. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15573. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15574. };
  15575. XMEMSET(&des, 0, sizeof(Des3));
  15576. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15577. /* DES_ENCRYPTION or DES_DECRYPTION */
  15578. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15579. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15580. /* Test bad args. */
  15581. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15582. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15583. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15584. /* Default case. Should return 0. */
  15585. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15586. wc_Des3Free(&des);
  15587. #endif
  15588. return EXPECT_RESULT();
  15589. } /* END test_wc_Des3_SetKey */
  15590. /*
  15591. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15592. */
  15593. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15594. {
  15595. EXPECT_DECLS;
  15596. #ifndef NO_DES3
  15597. Des3 des;
  15598. byte cipher[24];
  15599. byte plain[24];
  15600. const byte key[] = {
  15601. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15602. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15603. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15604. };
  15605. const byte iv[] = {
  15606. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15607. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15608. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15609. };
  15610. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15611. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15612. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15613. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15614. };
  15615. XMEMSET(&des, 0, sizeof(Des3));
  15616. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15617. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15618. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15619. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15620. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15621. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15622. /* Pass in bad args. */
  15623. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15624. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15625. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15626. BAD_FUNC_ARG);
  15627. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15628. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15629. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15630. wc_Des3Free(&des);
  15631. #endif
  15632. return EXPECT_RESULT();
  15633. } /* END wc_Des3_CbcEncrypt */
  15634. /*
  15635. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15636. */
  15637. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15638. {
  15639. EXPECT_DECLS;
  15640. #ifndef NO_DES3
  15641. word32 vectorSz, cipherSz;
  15642. byte cipher[24];
  15643. byte plain[24];
  15644. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15645. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15646. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15647. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15648. };
  15649. byte key[] = {
  15650. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15651. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15652. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15653. };
  15654. byte iv[] = {
  15655. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15656. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15657. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15658. };
  15659. vectorSz = sizeof(byte) * 24;
  15660. cipherSz = sizeof(byte) * 24;
  15661. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15662. 0);
  15663. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15664. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15665. /* pass in bad args. */
  15666. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15667. BAD_FUNC_ARG);
  15668. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15669. BAD_FUNC_ARG);
  15670. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15671. BAD_FUNC_ARG);
  15672. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15673. 0);
  15674. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15675. BAD_FUNC_ARG);
  15676. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15677. BAD_FUNC_ARG);
  15678. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15679. BAD_FUNC_ARG);
  15680. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15681. 0);
  15682. #endif
  15683. return EXPECT_RESULT();
  15684. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15685. /*
  15686. * Unit test for wc_Des3_EcbEncrypt
  15687. */
  15688. static int test_wc_Des3_EcbEncrypt(void)
  15689. {
  15690. EXPECT_DECLS;
  15691. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15692. Des3 des;
  15693. byte cipher[24];
  15694. word32 cipherSz = sizeof(cipher);
  15695. const byte key[] = {
  15696. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15697. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15698. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15699. };
  15700. const byte iv[] = {
  15701. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15702. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15703. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15704. };
  15705. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15706. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15707. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15708. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15709. };
  15710. XMEMSET(&des, 0, sizeof(Des3));
  15711. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15712. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15713. /* Bad Cases */
  15714. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15715. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15716. BAD_FUNC_ARG);
  15717. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15718. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15719. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15720. /* Good Cases */
  15721. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15722. wc_Des3Free(&des);
  15723. #endif
  15724. return EXPECT_RESULT();
  15725. } /* END test_wc_Des3_EcbEncrypt */
  15726. /*
  15727. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15728. */
  15729. static int test_wc_Chacha_SetKey(void)
  15730. {
  15731. EXPECT_DECLS;
  15732. #ifdef HAVE_CHACHA
  15733. ChaCha ctx;
  15734. const byte key[] = {
  15735. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15736. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15737. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15738. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15739. };
  15740. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15741. byte cipher[128];
  15742. XMEMSET(cipher, 0, sizeof(cipher));
  15743. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15744. /* Test bad args. */
  15745. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15746. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15747. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15748. /* Test bad args. */
  15749. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15750. #endif
  15751. return EXPECT_RESULT();
  15752. } /* END test_wc_Chacha_SetKey */
  15753. /*
  15754. * unit test for wc_Poly1305SetKey()
  15755. */
  15756. static int test_wc_Poly1305SetKey(void)
  15757. {
  15758. EXPECT_DECLS;
  15759. #ifdef HAVE_POLY1305
  15760. Poly1305 ctx;
  15761. const byte key[] =
  15762. {
  15763. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15764. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15765. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15766. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15767. };
  15768. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15769. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15770. /* Test bad args. */
  15771. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15772. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15773. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15774. #endif
  15775. return EXPECT_RESULT();
  15776. } /* END test_wc_Poly1305_SetKey() */
  15777. /*
  15778. * Testing wc_Chacha_Process()
  15779. */
  15780. static int test_wc_Chacha_Process(void)
  15781. {
  15782. EXPECT_DECLS;
  15783. #ifdef HAVE_CHACHA
  15784. ChaCha enc, dec;
  15785. byte cipher[128];
  15786. byte plain[128];
  15787. const byte key[] =
  15788. {
  15789. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15790. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15791. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15792. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15793. };
  15794. const char* input = "Everybody gets Friday off.";
  15795. word32 keySz = sizeof(key)/sizeof(byte);
  15796. unsigned long int inlen = XSTRLEN(input);
  15797. /* Initialize stack variables. */
  15798. XMEMSET(cipher, 0, 128);
  15799. XMEMSET(plain, 0, 128);
  15800. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15801. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15802. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15803. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15804. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15805. 0);
  15806. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15807. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15808. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15809. /* test checking and using leftovers, currently just in C code */
  15810. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15811. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15812. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15813. (word32)inlen - 2), 0);
  15814. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15815. (byte*)input + (inlen - 2), 2), 0);
  15816. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15817. (word32)inlen - 2), 0);
  15818. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15819. (byte*)input + (inlen - 2), 2), 0);
  15820. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15821. /* check edge cases with counter increment */
  15822. {
  15823. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15824. const byte expected[] = {
  15825. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15826. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15827. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15828. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15829. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15830. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15831. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15832. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15833. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15834. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15835. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15836. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15837. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15838. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15839. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15840. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15841. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15842. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15843. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15844. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15845. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15846. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15847. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15848. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15849. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15850. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15851. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15852. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15853. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15854. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15855. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15856. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15857. };
  15858. const byte iv2[] = {
  15859. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15860. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15861. };
  15862. byte input2[256];
  15863. int i;
  15864. for (i = 0; i < 256; i++)
  15865. input2[i] = i;
  15866. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15867. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15868. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15869. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15870. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15871. /* partial */
  15872. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15873. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15874. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15875. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15876. }
  15877. #endif
  15878. /* Test bad args. */
  15879. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15880. BAD_FUNC_ARG);
  15881. #endif
  15882. return EXPECT_RESULT();
  15883. } /* END test_wc_Chacha_Process */
  15884. /*
  15885. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15886. */
  15887. static int test_wc_ChaCha20Poly1305_aead(void)
  15888. {
  15889. EXPECT_DECLS;
  15890. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15891. const byte key[] = {
  15892. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15893. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15894. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15895. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15896. };
  15897. const byte plaintext[] = {
  15898. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15899. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15900. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15901. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15902. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15903. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15904. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15905. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15906. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15907. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15908. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15909. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15910. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15911. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15912. 0x74, 0x2e
  15913. };
  15914. const byte iv[] = {
  15915. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15916. 0x44, 0x45, 0x46, 0x47
  15917. };
  15918. const byte aad[] = { /* additional data */
  15919. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15920. 0xc4, 0xc5, 0xc6, 0xc7
  15921. };
  15922. const byte cipher[] = { /* expected output from operation */
  15923. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15924. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15925. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15926. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15927. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15928. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15929. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15930. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15931. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15932. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15933. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15934. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15935. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15936. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15937. 0x61, 0x16
  15938. };
  15939. const byte authTag[] = { /* expected output from operation */
  15940. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15941. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15942. };
  15943. byte generatedCiphertext[272];
  15944. byte generatedPlaintext[272];
  15945. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15946. /* Initialize stack variables. */
  15947. XMEMSET(generatedCiphertext, 0, 272);
  15948. XMEMSET(generatedPlaintext, 0, 272);
  15949. /* Test Encrypt */
  15950. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15951. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15952. 0);
  15953. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15954. sizeof(cipher)/sizeof(byte)), 0);
  15955. /* Test bad args. */
  15956. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15957. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15958. BAD_FUNC_ARG);
  15959. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15960. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15961. BAD_FUNC_ARG);
  15962. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15963. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15964. BAD_FUNC_ARG);
  15965. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15966. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15967. BAD_FUNC_ARG);
  15968. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15969. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15970. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15971. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15972. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15973. sizeof(cipher), authTag, generatedPlaintext), 0);
  15974. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15975. sizeof(plaintext)/sizeof(byte)), 0);
  15976. /* Test bad args. */
  15977. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15978. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15979. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15980. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15981. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15982. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15983. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15984. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15985. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15986. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15987. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15988. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15989. #endif
  15990. return EXPECT_RESULT();
  15991. } /* END test_wc_ChaCha20Poly1305_aead */
  15992. /*
  15993. * Testing function for wc_Rc2SetKey().
  15994. */
  15995. static int test_wc_Rc2SetKey(void)
  15996. {
  15997. EXPECT_DECLS;
  15998. #ifdef WC_RC2
  15999. Rc2 rc2;
  16000. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  16001. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  16002. /* valid key and IV */
  16003. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  16004. iv, 40), 0);
  16005. /* valid key, no IV */
  16006. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  16007. NULL, 40), 0);
  16008. /* bad arguments */
  16009. /* null Rc2 struct */
  16010. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  16011. iv, 40), BAD_FUNC_ARG);
  16012. /* null key */
  16013. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  16014. iv, 40), BAD_FUNC_ARG);
  16015. /* key size == 0 */
  16016. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  16017. /* key size > 128 */
  16018. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  16019. /* effective bits == 0 */
  16020. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  16021. iv, 0), WC_KEY_SIZE_E);
  16022. /* effective bits > 1024 */
  16023. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  16024. iv, 1025), WC_KEY_SIZE_E);
  16025. #endif
  16026. return EXPECT_RESULT();
  16027. } /* END test_wc_Rc2SetKey */
  16028. /*
  16029. * Testing function for wc_Rc2SetIV().
  16030. */
  16031. static int test_wc_Rc2SetIV(void)
  16032. {
  16033. EXPECT_DECLS;
  16034. #ifdef WC_RC2
  16035. Rc2 rc2;
  16036. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  16037. /* valid IV */
  16038. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16039. /* valid NULL IV */
  16040. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  16041. /* bad arguments */
  16042. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  16043. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  16044. #endif
  16045. return EXPECT_RESULT();
  16046. } /* END test_wc_Rc2SetIV */
  16047. /*
  16048. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  16049. */
  16050. static int test_wc_Rc2EcbEncryptDecrypt(void)
  16051. {
  16052. EXPECT_DECLS;
  16053. #ifdef WC_RC2
  16054. Rc2 rc2;
  16055. int effectiveKeyBits = 63;
  16056. byte cipher[RC2_BLOCK_SIZE];
  16057. byte plain[RC2_BLOCK_SIZE];
  16058. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16059. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16060. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  16061. XMEMSET(cipher, 0, sizeof(cipher));
  16062. XMEMSET(plain, 0, sizeof(plain));
  16063. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16064. NULL, effectiveKeyBits), 0);
  16065. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  16066. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  16067. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  16068. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  16069. /* Rc2EcbEncrypt bad arguments */
  16070. /* null Rc2 struct */
  16071. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  16072. BAD_FUNC_ARG);
  16073. /* null out buffer */
  16074. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  16075. BAD_FUNC_ARG);
  16076. /* null input buffer */
  16077. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  16078. BAD_FUNC_ARG);
  16079. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16080. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  16081. /* Rc2EcbDecrypt bad arguments */
  16082. /* null Rc2 struct */
  16083. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  16084. BAD_FUNC_ARG);
  16085. /* null out buffer */
  16086. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  16087. BAD_FUNC_ARG);
  16088. /* null input buffer */
  16089. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  16090. BAD_FUNC_ARG);
  16091. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16092. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  16093. #endif
  16094. return EXPECT_RESULT();
  16095. } /* END test_wc_Rc2EcbEncryptDecrypt */
  16096. /*
  16097. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  16098. */
  16099. static int test_wc_Rc2CbcEncryptDecrypt(void)
  16100. {
  16101. EXPECT_DECLS;
  16102. #ifdef WC_RC2
  16103. Rc2 rc2;
  16104. int effectiveKeyBits = 63;
  16105. byte cipher[RC2_BLOCK_SIZE*2];
  16106. byte plain[RC2_BLOCK_SIZE*2];
  16107. /* vector taken from test.c */
  16108. byte key[] = {
  16109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16110. };
  16111. byte iv[] = {
  16112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16113. };
  16114. byte input[] = {
  16115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16117. };
  16118. byte output[] = {
  16119. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  16120. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  16121. };
  16122. XMEMSET(cipher, 0, sizeof(cipher));
  16123. XMEMSET(plain, 0, sizeof(plain));
  16124. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16125. iv, effectiveKeyBits), 0);
  16126. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  16127. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  16128. /* reset IV for decrypt */
  16129. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16130. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  16131. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  16132. /* Rc2CbcEncrypt bad arguments */
  16133. /* null Rc2 struct */
  16134. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  16135. BAD_FUNC_ARG);
  16136. /* null out buffer */
  16137. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  16138. BAD_FUNC_ARG);
  16139. /* null input buffer */
  16140. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  16141. BAD_FUNC_ARG);
  16142. /* Rc2CbcDecrypt bad arguments */
  16143. /* in size is 0 */
  16144. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  16145. /* null Rc2 struct */
  16146. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  16147. BAD_FUNC_ARG);
  16148. /* null out buffer */
  16149. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  16150. BAD_FUNC_ARG);
  16151. /* null input buffer */
  16152. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  16153. BAD_FUNC_ARG);
  16154. #endif
  16155. return EXPECT_RESULT();
  16156. } /* END test_wc_Rc2CbcEncryptDecrypt */
  16157. /*
  16158. * Testing function for wc_AesSetIV
  16159. */
  16160. static int test_wc_AesSetIV(void)
  16161. {
  16162. int res = TEST_SKIPPED;
  16163. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16164. Aes aes;
  16165. int ret = 0;
  16166. byte key16[] =
  16167. {
  16168. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16169. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16170. };
  16171. byte iv1[] = "1234567890abcdef";
  16172. byte iv2[] = "0987654321fedcba";
  16173. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16174. if (ret != 0)
  16175. return ret;
  16176. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16177. iv1, AES_ENCRYPTION);
  16178. if (ret == 0) {
  16179. ret = wc_AesSetIV(&aes, iv2);
  16180. }
  16181. /* Test bad args. */
  16182. if (ret == 0) {
  16183. ret = wc_AesSetIV(NULL, iv1);
  16184. if (ret == BAD_FUNC_ARG) {
  16185. /* NULL iv should return 0. */
  16186. ret = wc_AesSetIV(&aes, NULL);
  16187. }
  16188. else {
  16189. ret = WOLFSSL_FATAL_ERROR;
  16190. }
  16191. }
  16192. wc_AesFree(&aes);
  16193. res = TEST_RES_CHECK(ret == 0);
  16194. #endif
  16195. return res;
  16196. } /* test_wc_AesSetIV */
  16197. /*
  16198. * Testing function for wc_AesSetKey().
  16199. */
  16200. static int test_wc_AesSetKey(void)
  16201. {
  16202. EXPECT_DECLS;
  16203. #ifndef NO_AES
  16204. Aes aes;
  16205. byte key16[] = {
  16206. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16207. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16208. };
  16209. #ifdef WOLFSSL_AES_192
  16210. byte key24[] = {
  16211. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16212. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16213. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16214. };
  16215. #endif
  16216. #ifdef WOLFSSL_AES_256
  16217. byte key32[] = {
  16218. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16219. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16220. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16221. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16222. };
  16223. #endif
  16224. byte badKey16[] = {
  16225. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16226. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16227. };
  16228. byte iv[] = "1234567890abcdef";
  16229. XMEMSET(&aes, 0, sizeof(Aes));
  16230. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16231. #ifdef WOLFSSL_AES_128
  16232. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  16233. iv, AES_ENCRYPTION), 0);
  16234. #endif
  16235. #ifdef WOLFSSL_AES_192
  16236. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  16237. iv, AES_ENCRYPTION), 0);
  16238. #endif
  16239. #ifdef WOLFSSL_AES_256
  16240. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  16241. iv, AES_ENCRYPTION), 0);
  16242. #endif
  16243. /* Pass in bad args. */
  16244. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  16245. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  16246. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  16247. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  16248. BAD_FUNC_ARG);
  16249. wc_AesFree(&aes);
  16250. #endif
  16251. return EXPECT_RESULT();
  16252. } /* END test_wc_AesSetKey */
  16253. /*
  16254. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16255. * and wc_AesCbcDecryptWithKey()
  16256. */
  16257. static int test_wc_AesCbcEncryptDecrypt(void)
  16258. {
  16259. EXPECT_DECLS;
  16260. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16261. defined(WOLFSSL_AES_256)
  16262. Aes aes;
  16263. byte key32[] = {
  16264. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16265. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16266. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16267. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16268. };
  16269. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  16270. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  16271. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  16272. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  16273. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  16274. };
  16275. byte iv[] = "1234567890abcdef";
  16276. byte enc[sizeof(vector)];
  16277. byte dec[sizeof(vector)];
  16278. byte dec2[sizeof(vector)];
  16279. /* Init stack variables. */
  16280. XMEMSET(&aes, 0, sizeof(Aes));
  16281. XMEMSET(enc, 0, sizeof(enc));
  16282. XMEMSET(dec, 0, sizeof(vector));
  16283. XMEMSET(dec2, 0, sizeof(vector));
  16284. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16285. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16286. AES_ENCRYPTION), 0);
  16287. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  16288. /* Re init for decrypt and set flag. */
  16289. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16290. AES_DECRYPTION), 0);
  16291. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  16292. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16293. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  16294. sizeof(key32)/sizeof(byte), iv), 0);
  16295. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  16296. /* Pass in bad args */
  16297. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  16298. BAD_FUNC_ARG);
  16299. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  16300. BAD_FUNC_ARG);
  16301. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  16302. BAD_FUNC_ARG);
  16303. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16304. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  16305. BAD_LENGTH_E);
  16306. #endif
  16307. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16308. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16309. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16310. "mode (v2), skip test");
  16311. #else
  16312. /* Test passing in size of 0 */
  16313. XMEMSET(enc, 0, sizeof(enc));
  16314. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  16315. /* Check enc was not modified */
  16316. {
  16317. int i;
  16318. for (i = 0; i < (int)sizeof(enc); i++)
  16319. ExpectIntEQ(enc[i], 0);
  16320. }
  16321. #endif
  16322. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  16323. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  16324. BAD_FUNC_ARG);
  16325. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  16326. BAD_FUNC_ARG);
  16327. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16328. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16329. BAD_LENGTH_E);
  16330. #else
  16331. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16332. BAD_FUNC_ARG);
  16333. #endif
  16334. /* Test passing in size of 0 */
  16335. XMEMSET(dec, 0, sizeof(dec));
  16336. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  16337. /* Check dec was not modified */
  16338. {
  16339. int i;
  16340. for (i = 0; i < (int)sizeof(dec); i++)
  16341. ExpectIntEQ(dec[i], 0);
  16342. }
  16343. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16344. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16345. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16346. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16347. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16348. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16349. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16350. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  16351. wc_AesFree(&aes);
  16352. #endif
  16353. return EXPECT_RESULT();
  16354. } /* END test_wc_AesCbcEncryptDecrypt */
  16355. /*
  16356. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16357. */
  16358. static int test_wc_AesCtrEncryptDecrypt(void)
  16359. {
  16360. EXPECT_DECLS;
  16361. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16362. Aes aesEnc;
  16363. Aes aesDec;
  16364. byte key32[] = {
  16365. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16366. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16367. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16368. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16369. };
  16370. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16371. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16372. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16373. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16374. };
  16375. byte iv[] = "1234567890abcdef";
  16376. byte enc[AES_BLOCK_SIZE * 2];
  16377. byte dec[AES_BLOCK_SIZE * 2];
  16378. /* Init stack variables. */
  16379. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16380. XMEMSET(&aesDec, 0, sizeof(Aes));
  16381. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16382. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16383. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16384. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16385. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  16386. AES_ENCRYPTION), 0);
  16387. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16388. sizeof(vector)/sizeof(byte)), 0);
  16389. /* Decrypt with wc_AesCtrEncrypt() */
  16390. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  16391. AES_ENCRYPTION), 0);
  16392. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  16393. 0);
  16394. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16395. /* Test bad args. */
  16396. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  16397. BAD_FUNC_ARG);
  16398. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  16399. BAD_FUNC_ARG);
  16400. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  16401. BAD_FUNC_ARG);
  16402. wc_AesFree(&aesEnc);
  16403. wc_AesFree(&aesDec);
  16404. #endif
  16405. return EXPECT_RESULT();
  16406. } /* END test_wc_AesCtrEncryptDecrypt */
  16407. /*
  16408. * test function for wc_AesGcmSetKey()
  16409. */
  16410. static int test_wc_AesGcmSetKey(void)
  16411. {
  16412. EXPECT_DECLS;
  16413. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16414. Aes aes;
  16415. #ifdef WOLFSSL_AES_128
  16416. byte key16[] = {
  16417. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16418. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16419. };
  16420. #endif
  16421. #ifdef WOLFSSL_AES_192
  16422. byte key24[] = {
  16423. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16424. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16425. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16426. };
  16427. #endif
  16428. #ifdef WOLFSSL_AES_256
  16429. byte key32[] = {
  16430. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16431. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16432. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16433. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16434. };
  16435. #endif
  16436. byte badKey16[] = {
  16437. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16438. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16439. };
  16440. byte badKey24[] = {
  16441. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16442. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16443. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16444. };
  16445. byte badKey32[] = {
  16446. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16447. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16448. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16449. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16450. };
  16451. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16452. #ifdef WOLFSSL_AES_128
  16453. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  16454. #endif
  16455. #ifdef WOLFSSL_AES_192
  16456. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  16457. #endif
  16458. #ifdef WOLFSSL_AES_256
  16459. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16460. #endif
  16461. /* Pass in bad args. */
  16462. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  16463. BAD_FUNC_ARG);
  16464. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  16465. BAD_FUNC_ARG);
  16466. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  16467. BAD_FUNC_ARG);
  16468. wc_AesFree(&aes);
  16469. #endif
  16470. return EXPECT_RESULT();
  16471. } /* END test_wc_AesGcmSetKey */
  16472. /*
  16473. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16474. */
  16475. static int test_wc_AesGcmEncryptDecrypt(void)
  16476. {
  16477. EXPECT_DECLS;
  16478. /* WOLFSSL_AFALG requires 12 byte IV */
  16479. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16480. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16481. Aes aes;
  16482. byte key32[] = {
  16483. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16484. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16485. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16486. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16487. };
  16488. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16489. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16490. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16491. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16492. };
  16493. const byte a[] = {
  16494. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16495. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16496. 0xab, 0xad, 0xda, 0xd2
  16497. };
  16498. byte iv[] = "1234567890a";
  16499. byte longIV[] = "1234567890abcdefghij";
  16500. byte enc[sizeof(vector)];
  16501. byte resultT[AES_BLOCK_SIZE];
  16502. byte dec[sizeof(vector)];
  16503. /* Init stack variables. */
  16504. XMEMSET(&aes, 0, sizeof(Aes));
  16505. XMEMSET(enc, 0, sizeof(vector));
  16506. XMEMSET(dec, 0, sizeof(vector));
  16507. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16508. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16509. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16510. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16511. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16512. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16513. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16514. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16515. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16516. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16517. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16518. BAD_FUNC_ARG);
  16519. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16520. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16521. BAD_FUNC_ARG);
  16522. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16523. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16524. BAD_FUNC_ARG);
  16525. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16526. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16527. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16528. /* FIPS does not check the lower bound of ivSz */
  16529. #else
  16530. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16531. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16532. #endif
  16533. /* This case is now considered good. Long IVs are now allowed.
  16534. * Except for the original FIPS release, it still has an upper
  16535. * bound on the IV length. */
  16536. #if (!defined(HAVE_FIPS) || \
  16537. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16538. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16539. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16540. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16541. 0);
  16542. #else
  16543. (void)longIV;
  16544. #endif /* Old FIPS */
  16545. /* END wc_AesGcmEncrypt */
  16546. #ifdef HAVE_AES_DECRYPT
  16547. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16548. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16549. BAD_FUNC_ARG);
  16550. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16551. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16552. BAD_FUNC_ARG);
  16553. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16554. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16555. BAD_FUNC_ARG);
  16556. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16557. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16558. BAD_FUNC_ARG);
  16559. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16560. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16561. BAD_FUNC_ARG);
  16562. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16563. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16564. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16565. AES_GCM_AUTH_E);
  16566. #else
  16567. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16568. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16569. BAD_FUNC_ARG);
  16570. #endif
  16571. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16572. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16573. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16574. /* FIPS does not check the lower bound of ivSz */
  16575. #else
  16576. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16577. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16578. #endif
  16579. #endif /* HAVE_AES_DECRYPT */
  16580. wc_AesFree(&aes);
  16581. #endif
  16582. return EXPECT_RESULT();
  16583. } /* END test_wc_AesGcmEncryptDecrypt */
  16584. /*
  16585. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16586. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16587. */
  16588. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16589. {
  16590. EXPECT_DECLS;
  16591. #if (!defined(HAVE_FIPS) || \
  16592. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16593. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16594. const byte key[] = {
  16595. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16596. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16597. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16598. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16599. };
  16600. const byte in[] = {
  16601. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16602. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16603. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16604. };
  16605. const byte aad[] = {
  16606. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16607. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16608. 0xab, 0xad, 0xda, 0xd2
  16609. };
  16610. Aes aesEnc;
  16611. Aes aesDec;
  16612. byte iv[] = "1234567890abcdefghij";
  16613. byte out[sizeof(in)];
  16614. byte plain[sizeof(in)];
  16615. byte tag[AES_BLOCK_SIZE];
  16616. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16617. XMEMSET(&aesDec, 0, sizeof(Aes));
  16618. XMEMSET(out, 0, sizeof(out));
  16619. XMEMSET(plain, 0, sizeof(plain));
  16620. XMEMSET(tag, 0, sizeof(tag));
  16621. /* Perform one-shot encryption using long IV */
  16622. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16623. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16624. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16625. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16626. /* Perform streaming decryption using long IV */
  16627. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16628. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16629. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16630. sizeof(aad)), 0);
  16631. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16632. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16633. /* Free resources */
  16634. wc_AesFree(&aesEnc);
  16635. wc_AesFree(&aesDec);
  16636. #endif
  16637. return EXPECT_RESULT();
  16638. } /* END wc_AesGcmMixedEncDecLongIV */
  16639. /*
  16640. * unit test for wc_GmacSetKey()
  16641. */
  16642. static int test_wc_GmacSetKey(void)
  16643. {
  16644. EXPECT_DECLS;
  16645. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16646. Gmac gmac;
  16647. byte key16[] = {
  16648. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16649. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16650. };
  16651. #ifdef WOLFSSL_AES_192
  16652. byte key24[] = {
  16653. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16654. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16655. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16656. };
  16657. #endif
  16658. #ifdef WOLFSSL_AES_256
  16659. byte key32[] = {
  16660. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16661. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16662. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16663. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16664. };
  16665. #endif
  16666. byte badKey16[] = {
  16667. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16668. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16669. };
  16670. byte badKey24[] = {
  16671. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16672. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16673. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16674. };
  16675. byte badKey32[] = {
  16676. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16677. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16678. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16679. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16680. };
  16681. XMEMSET(&gmac, 0, sizeof(Gmac));
  16682. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16683. #ifdef WOLFSSL_AES_128
  16684. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16685. #endif
  16686. #ifdef WOLFSSL_AES_192
  16687. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16688. #endif
  16689. #ifdef WOLFSSL_AES_256
  16690. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16691. #endif
  16692. /* Pass in bad args. */
  16693. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16694. BAD_FUNC_ARG);
  16695. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16696. BAD_FUNC_ARG);
  16697. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16698. BAD_FUNC_ARG);
  16699. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16700. BAD_FUNC_ARG);
  16701. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16702. BAD_FUNC_ARG);
  16703. wc_AesFree(&gmac.aes);
  16704. #endif
  16705. return EXPECT_RESULT();
  16706. } /* END test_wc_GmacSetKey */
  16707. /*
  16708. * unit test for wc_GmacUpdate
  16709. */
  16710. static int test_wc_GmacUpdate(void)
  16711. {
  16712. EXPECT_DECLS;
  16713. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16714. Gmac gmac;
  16715. #ifdef WOLFSSL_AES_128
  16716. const byte key16[] = {
  16717. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16718. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16719. };
  16720. #endif
  16721. #ifdef WOLFSSL_AES_192
  16722. byte key24[] = {
  16723. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16724. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16725. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16726. };
  16727. #endif
  16728. #ifdef WOLFSSL_AES_256
  16729. byte key32[] = {
  16730. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16731. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16732. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16733. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16734. };
  16735. #endif
  16736. #ifdef WOLFSSL_AES_128
  16737. const byte authIn[] = {
  16738. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16739. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16740. };
  16741. #endif
  16742. #ifdef WOLFSSL_AES_192
  16743. const byte authIn2[] = {
  16744. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16745. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16746. };
  16747. #endif
  16748. const byte authIn3[] = {
  16749. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16750. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16751. };
  16752. #ifdef WOLFSSL_AES_128
  16753. const byte tag1[] = { /* Known. */
  16754. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16755. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16756. };
  16757. #endif
  16758. #ifdef WOLFSSL_AES_192
  16759. const byte tag2[] = { /* Known */
  16760. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16761. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16762. };
  16763. #endif
  16764. const byte tag3[] = { /* Known */
  16765. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16766. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16767. };
  16768. #ifdef WOLFSSL_AES_128
  16769. const byte iv[] = {
  16770. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16771. 0xe2, 0x8c, 0x8f, 0x16
  16772. };
  16773. #endif
  16774. #ifdef WOLFSSL_AES_192
  16775. const byte iv2[] = {
  16776. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16777. 0x7e, 0x1a, 0x6f, 0xbc
  16778. };
  16779. #endif
  16780. const byte iv3[] = {
  16781. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16782. 0xc3, 0xfb, 0x6c, 0x8a
  16783. };
  16784. byte tagOut[16];
  16785. byte tagOut2[24];
  16786. byte tagOut3[32];
  16787. /* Init stack variables. */
  16788. XMEMSET(&gmac, 0, sizeof(Gmac));
  16789. XMEMSET(tagOut, 0, sizeof(tagOut));
  16790. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16791. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16792. #ifdef WOLFSSL_AES_128
  16793. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16794. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16795. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16796. tagOut, sizeof(tag1)), 0);
  16797. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16798. wc_AesFree(&gmac.aes);
  16799. #endif
  16800. #ifdef WOLFSSL_AES_192
  16801. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16802. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16803. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16804. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16805. tagOut2, sizeof(tag2)), 0);
  16806. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16807. wc_AesFree(&gmac.aes);
  16808. #endif
  16809. #ifdef WOLFSSL_AES_256
  16810. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16811. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16812. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16813. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16814. tagOut3, sizeof(tag3)), 0);
  16815. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16816. wc_AesFree(&gmac.aes);
  16817. #endif
  16818. /* Pass bad args. */
  16819. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16820. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16821. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16822. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16823. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16824. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16825. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16826. wc_AesFree(&gmac.aes);
  16827. #endif
  16828. return EXPECT_RESULT();
  16829. } /* END test_wc_GmacUpdate */
  16830. /*
  16831. * testing wc_CamelliaSetKey
  16832. */
  16833. static int test_wc_CamelliaSetKey(void)
  16834. {
  16835. EXPECT_DECLS;
  16836. #ifdef HAVE_CAMELLIA
  16837. Camellia camellia;
  16838. /*128-bit key*/
  16839. static const byte key16[] = {
  16840. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16841. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16842. };
  16843. /* 192-bit key */
  16844. static const byte key24[] = {
  16845. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16846. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16847. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16848. };
  16849. /* 256-bit key */
  16850. static const byte key32[] = {
  16851. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16852. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16853. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16854. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16855. };
  16856. static const byte iv[] = {
  16857. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16858. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16859. };
  16860. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16861. 0);
  16862. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16863. NULL), 0);
  16864. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16865. 0);
  16866. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16867. NULL), 0);
  16868. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16869. 0);
  16870. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16871. NULL), 0);
  16872. /* Bad args. */
  16873. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16874. BAD_FUNC_ARG);
  16875. #endif
  16876. return EXPECT_RESULT();
  16877. } /* END test_wc_CammeliaSetKey */
  16878. /*
  16879. * Testing wc_CamelliaSetIV()
  16880. */
  16881. static int test_wc_CamelliaSetIV(void)
  16882. {
  16883. EXPECT_DECLS;
  16884. #ifdef HAVE_CAMELLIA
  16885. Camellia camellia;
  16886. static const byte iv[] = {
  16887. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16888. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16889. };
  16890. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16891. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16892. /* Bad args. */
  16893. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16894. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16895. #endif
  16896. return EXPECT_RESULT();
  16897. } /* END test_wc_CamelliaSetIV*/
  16898. /*
  16899. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16900. */
  16901. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16902. {
  16903. EXPECT_DECLS;
  16904. #ifdef HAVE_CAMELLIA
  16905. Camellia camellia;
  16906. static const byte key24[] = {
  16907. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16908. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16909. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16910. };
  16911. static const byte iv[] = {
  16912. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16913. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16914. };
  16915. static const byte plainT[] = {
  16916. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16917. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16918. };
  16919. byte enc[sizeof(plainT)];
  16920. byte dec[sizeof(enc)];
  16921. /* Init stack variables.*/
  16922. XMEMSET(enc, 0, 16);
  16923. XMEMSET(enc, 0, 16);
  16924. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16925. 0);
  16926. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16927. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16928. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16929. /* Pass bad args. */
  16930. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16931. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16932. BAD_FUNC_ARG);
  16933. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16934. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16935. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16936. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16937. #endif
  16938. return EXPECT_RESULT();
  16939. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16940. /*
  16941. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16942. */
  16943. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16944. {
  16945. EXPECT_DECLS;
  16946. #ifdef HAVE_CAMELLIA
  16947. Camellia camellia;
  16948. static const byte key24[] = {
  16949. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16950. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16951. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16952. };
  16953. static const byte plainT[] = {
  16954. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16955. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16956. };
  16957. byte enc[CAMELLIA_BLOCK_SIZE];
  16958. byte dec[CAMELLIA_BLOCK_SIZE];
  16959. /* Init stack variables. */
  16960. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16961. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16962. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16963. NULL), 0);
  16964. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16965. CAMELLIA_BLOCK_SIZE), 0);
  16966. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16967. NULL), 0);
  16968. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16969. 0);
  16970. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16971. /* Pass in bad args. */
  16972. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16973. BAD_FUNC_ARG);
  16974. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16975. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16976. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16977. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16978. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16979. BAD_FUNC_ARG);
  16980. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16981. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16982. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16983. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16984. #endif
  16985. return EXPECT_RESULT();
  16986. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16987. /*
  16988. * Testing wc_Arc4SetKey()
  16989. */
  16990. static int test_wc_Arc4SetKey(void)
  16991. {
  16992. EXPECT_DECLS;
  16993. #ifndef NO_RC4
  16994. Arc4 arc;
  16995. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16996. int keyLen = 8;
  16997. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, (word32)keyLen), 0);
  16998. /* Test bad args. */
  16999. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, (word32)keyLen), BAD_FUNC_ARG);
  17000. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , (word32)keyLen), BAD_FUNC_ARG);
  17001. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  17002. #endif
  17003. return EXPECT_RESULT();
  17004. } /* END test_wc_Arc4SetKey */
  17005. /*
  17006. * Testing wc_Arc4Process for ENC/DEC.
  17007. */
  17008. static int test_wc_Arc4Process(void)
  17009. {
  17010. EXPECT_DECLS;
  17011. #ifndef NO_RC4
  17012. Arc4 enc;
  17013. Arc4 dec;
  17014. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  17015. int keyLen = 8;
  17016. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  17017. byte cipher[8];
  17018. byte plain[8];
  17019. /* Init stack variables */
  17020. XMEMSET(&enc, 0, sizeof(Arc4));
  17021. XMEMSET(&dec, 0, sizeof(Arc4));
  17022. XMEMSET(cipher, 0, sizeof(cipher));
  17023. XMEMSET(plain, 0, sizeof(plain));
  17024. /* Use for async. */
  17025. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  17026. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  17027. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, (word32)keyLen), 0);
  17028. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, (word32)keyLen), 0);
  17029. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, (word32)keyLen), 0);
  17030. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, (word32)keyLen), 0);
  17031. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  17032. /* Bad args. */
  17033. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, (word32)keyLen), BAD_FUNC_ARG);
  17034. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, (word32)keyLen), BAD_FUNC_ARG);
  17035. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, (word32)keyLen), BAD_FUNC_ARG);
  17036. wc_Arc4Free(&enc);
  17037. wc_Arc4Free(&dec);
  17038. #endif
  17039. return EXPECT_RESULT();
  17040. } /* END test_wc_Arc4Process */
  17041. /*
  17042. * Testing wc_Init RsaKey()
  17043. */
  17044. static int test_wc_InitRsaKey(void)
  17045. {
  17046. EXPECT_DECLS;
  17047. #ifndef NO_RSA
  17048. RsaKey key;
  17049. XMEMSET(&key, 0, sizeof(RsaKey));
  17050. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17051. /* Test bad args. */
  17052. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  17053. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17054. #endif
  17055. return EXPECT_RESULT();
  17056. } /* END test_wc_InitRsaKey */
  17057. /*
  17058. * Testing wc_RsaPrivateKeyDecode()
  17059. */
  17060. static int test_wc_RsaPrivateKeyDecode(void)
  17061. {
  17062. EXPECT_DECLS;
  17063. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17064. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17065. RsaKey key;
  17066. byte* tmp = NULL;
  17067. word32 idx = 0;
  17068. int bytes = 0;
  17069. XMEMSET(&key, 0, sizeof(RsaKey));
  17070. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  17071. DYNAMIC_TYPE_TMP_BUFFER));
  17072. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17073. if (tmp != NULL) {
  17074. #ifdef USE_CERT_BUFFERS_1024
  17075. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17076. bytes = sizeof_client_key_der_1024;
  17077. #else
  17078. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17079. bytes = sizeof_client_key_der_2048;
  17080. #endif /* Use cert buffers. */
  17081. }
  17082. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  17083. /* Test bad args. */
  17084. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  17085. BAD_FUNC_ARG);
  17086. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  17087. BAD_FUNC_ARG);
  17088. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17089. BAD_FUNC_ARG);
  17090. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17091. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17092. #endif
  17093. return EXPECT_RESULT();
  17094. } /* END test_wc_RsaPrivateKeyDecode */
  17095. /*
  17096. * Testing wc_RsaPublicKeyDecode()
  17097. */
  17098. static int test_wc_RsaPublicKeyDecode(void)
  17099. {
  17100. EXPECT_DECLS;
  17101. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17102. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17103. RsaKey keyPub;
  17104. byte* tmp = NULL;
  17105. word32 idx = 0;
  17106. int bytes = 0;
  17107. word32 keySz = 0;
  17108. word32 tstKeySz = 0;
  17109. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17110. XFILE f = XBADFILE;
  17111. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17112. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17113. byte buf[4096];
  17114. #endif
  17115. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  17116. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17117. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  17118. if (tmp != NULL) {
  17119. #ifdef USE_CERT_BUFFERS_1024
  17120. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17121. bytes = sizeof_client_keypub_der_1024;
  17122. keySz = 1024;
  17123. #else
  17124. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17125. bytes = sizeof_client_keypub_der_2048;
  17126. keySz = 2048;
  17127. #endif
  17128. }
  17129. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  17130. /* Pass in bad args. */
  17131. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  17132. BAD_FUNC_ARG);
  17133. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  17134. BAD_FUNC_ARG);
  17135. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17136. BAD_FUNC_ARG);
  17137. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  17138. /* Test for getting modulus key size */
  17139. idx = 0;
  17140. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17141. &tstKeySz, NULL, NULL), 0);
  17142. ExpectIntEQ(tstKeySz, keySz/8);
  17143. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17144. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  17145. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17146. if (f != XBADFILE) {
  17147. XFCLOSE(f);
  17148. f = XBADFILE;
  17149. }
  17150. idx = 0;
  17151. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17152. NULL), 0);
  17153. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  17154. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17155. if (f != XBADFILE)
  17156. XFCLOSE(f);
  17157. idx = 0;
  17158. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17159. NULL), 0);
  17160. #endif
  17161. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17162. #endif
  17163. return EXPECT_RESULT();
  17164. } /* END test_wc_RsaPublicKeyDecode */
  17165. /*
  17166. * Testing wc_RsaPublicKeyDecodeRaw()
  17167. */
  17168. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17169. {
  17170. EXPECT_DECLS;
  17171. #if !defined(NO_RSA)
  17172. RsaKey key;
  17173. const byte n = 0x23;
  17174. const byte e = 0x03;
  17175. int nSz = sizeof(n);
  17176. int eSz = sizeof(e);
  17177. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17178. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  17179. /* Pass in bad args. */
  17180. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  17181. BAD_FUNC_ARG);
  17182. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  17183. BAD_FUNC_ARG);
  17184. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  17185. BAD_FUNC_ARG);
  17186. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17187. #endif
  17188. return EXPECT_RESULT();
  17189. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17190. /*
  17191. * Testing wc_RsaPrivateKeyDecodeRaw()
  17192. */
  17193. static int test_wc_RsaPrivateKeyDecodeRaw(void)
  17194. {
  17195. EXPECT_DECLS;
  17196. #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) \
  17197. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  17198. RsaKey key;
  17199. const byte n = 33;
  17200. const byte e = 3;
  17201. const byte d = 7;
  17202. const byte u = 2;
  17203. const byte p = 3;
  17204. const byte q = 11;
  17205. const byte dp = 1;
  17206. const byte dq = 7;
  17207. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17208. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17209. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17210. &p, sizeof(p), &q, sizeof(q), NULL, 0,
  17211. NULL, 0, &key), 0);
  17212. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17213. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17214. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17215. NULL, 0, &key), 0);
  17216. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17217. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17218. &p, sizeof(p), &q, sizeof(q), NULL, 0,
  17219. &dq, sizeof(dq), &key), 0);
  17220. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17221. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17222. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17223. &dq, sizeof(dq), &key), 0);
  17224. /* Pass in bad args. */
  17225. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(NULL, sizeof(n),
  17226. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17227. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17228. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17229. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, 0,
  17230. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17231. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17232. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17233. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17234. NULL, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17235. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17236. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17237. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17238. &e, 0, &d, sizeof(d), &u, sizeof(u),
  17239. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17240. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17241. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17242. &e, sizeof(e), NULL, sizeof(d), &u, sizeof(u),
  17243. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17244. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17245. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17246. &e, sizeof(e), &d, 0, &u, sizeof(u),
  17247. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17248. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17249. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17250. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17251. NULL, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17252. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17253. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17254. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17255. &p, 0, &q, sizeof(q), &dp, sizeof(dp),
  17256. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17257. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17258. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17259. &p, sizeof(p), NULL, sizeof(q), &dp, sizeof(dp),
  17260. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17261. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17262. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17263. &p, sizeof(p), &q, 0, &dp, sizeof(dp),
  17264. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17265. #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
  17266. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17267. &e, sizeof(e), &d, sizeof(d), &u, 0,
  17268. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17269. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17270. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17271. &e, sizeof(e), &d, sizeof(d), NULL, sizeof(u),
  17272. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17273. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17274. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17275. &e, sizeof(e), &d, sizeof(d), &u, 0,
  17276. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17277. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17278. #endif
  17279. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17280. #endif
  17281. return EXPECT_RESULT();
  17282. } /* END test_wc_RsaPrivateKeyDecodeRaw */
  17283. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17284. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17285. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17286. * trying until it gets a probable prime. */
  17287. #ifdef HAVE_FIPS
  17288. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17289. {
  17290. int ret;
  17291. for (;;) {
  17292. ret = wc_MakeRsaKey(key, size, e, rng);
  17293. if (ret != PRIME_GEN_E) break;
  17294. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17295. "trying again.\n");
  17296. }
  17297. return ret;
  17298. }
  17299. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17300. #else
  17301. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17302. #endif
  17303. #endif
  17304. /*
  17305. * Testing wc_MakeRsaKey()
  17306. */
  17307. static int test_wc_MakeRsaKey(void)
  17308. {
  17309. EXPECT_DECLS;
  17310. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17311. RsaKey genKey;
  17312. WC_RNG rng;
  17313. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17314. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17315. int bits = 1024;
  17316. #else
  17317. int bits = 2048;
  17318. #endif
  17319. XMEMSET(&genKey, 0, sizeof(RsaKey));
  17320. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17321. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17322. ExpectIntEQ(wc_InitRng(&rng), 0);
  17323. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17324. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17325. /* Test bad args. */
  17326. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  17327. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  17328. BAD_FUNC_ARG);
  17329. /* e < 3 */
  17330. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  17331. /* e & 1 == 0 */
  17332. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  17333. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17334. #endif
  17335. return EXPECT_RESULT();
  17336. } /* END test_wc_MakeRsaKey */
  17337. /*
  17338. * Test the bounds checking on the cipher text versus the key modulus.
  17339. * 1. Make a new RSA key.
  17340. * 2. Set c to 1.
  17341. * 3. Decrypt c into k. (error)
  17342. * 4. Copy the key modulus to c and sub 1 from the copy.
  17343. * 5. Decrypt c into k. (error)
  17344. * Valid bounds test cases are covered by all the other RSA tests.
  17345. */
  17346. static int test_RsaDecryptBoundsCheck(void)
  17347. {
  17348. EXPECT_DECLS;
  17349. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17350. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17351. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17352. WC_RNG rng;
  17353. RsaKey key;
  17354. byte flatC[256];
  17355. word32 flatCSz;
  17356. byte out[256];
  17357. word32 outSz = sizeof(out);
  17358. XMEMSET(&key, 0, sizeof(RsaKey));
  17359. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17360. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17361. ExpectIntEQ(wc_InitRng(&rng), 0);
  17362. if (EXPECT_SUCCESS()) {
  17363. const byte* derKey;
  17364. word32 derKeySz;
  17365. word32 idx = 0;
  17366. #ifdef USE_CERT_BUFFERS_1024
  17367. derKey = server_key_der_1024;
  17368. derKeySz = (word32)sizeof_server_key_der_1024;
  17369. flatCSz = 128;
  17370. #else
  17371. derKey = server_key_der_2048;
  17372. derKeySz = (word32)sizeof_server_key_der_2048;
  17373. flatCSz = 256;
  17374. #endif
  17375. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  17376. }
  17377. if (EXPECT_SUCCESS()) {
  17378. XMEMSET(flatC, 0, flatCSz);
  17379. flatC[flatCSz-1] = 1;
  17380. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17381. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  17382. if (EXPECT_SUCCESS()) {
  17383. mp_int c;
  17384. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  17385. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  17386. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  17387. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17388. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  17389. mp_clear(&c);
  17390. }
  17391. }
  17392. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17393. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17394. #endif
  17395. return EXPECT_RESULT();
  17396. } /* END test_wc_RsaDecryptBoundsCheck */
  17397. /*
  17398. * Testing wc_SetKeyUsage()
  17399. */
  17400. static int test_wc_SetKeyUsage(void)
  17401. {
  17402. EXPECT_DECLS;
  17403. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17404. Cert myCert;
  17405. ExpectIntEQ(wc_InitCert(&myCert), 0);
  17406. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  17407. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  17408. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  17409. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  17410. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  17411. /* Test bad args. */
  17412. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  17413. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  17414. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  17415. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  17416. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  17417. KEYUSAGE_E);
  17418. #endif
  17419. return EXPECT_RESULT();
  17420. } /* END test_wc_SetKeyUsage */
  17421. /*
  17422. * Testing wc_CheckProbablePrime()
  17423. */
  17424. static int test_wc_CheckProbablePrime(void)
  17425. {
  17426. EXPECT_DECLS;
  17427. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17428. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17429. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17430. RsaKey key;
  17431. WC_RNG rng;
  17432. byte e[3];
  17433. word32 eSz = (word32)sizeof(e);
  17434. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17435. word32 nSz = (word32)sizeof(n);
  17436. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17437. word32 dSz = (word32)sizeof(d);
  17438. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17439. word32 pSz = (word32)sizeof(p);
  17440. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17441. word32 qSz = (word32)sizeof(q);
  17442. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17443. int* isPrime;
  17444. int test[5];
  17445. isPrime = test;
  17446. XMEMSET(&key, 0, sizeof(RsaKey));
  17447. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17448. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17449. ExpectIntEQ(wc_InitRng(&rng), 0);
  17450. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17451. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  17452. WC_RSA_EXPONENT, &rng), 0);
  17453. PRIVATE_KEY_UNLOCK();
  17454. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  17455. &qSz), 0);
  17456. PRIVATE_KEY_LOCK();
  17457. /* Bad cases */
  17458. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  17459. BAD_FUNC_ARG);
  17460. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  17461. BAD_FUNC_ARG);
  17462. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  17463. BAD_FUNC_ARG);
  17464. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  17465. BAD_FUNC_ARG);
  17466. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  17467. BAD_FUNC_ARG);
  17468. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  17469. BAD_FUNC_ARG);
  17470. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  17471. BAD_FUNC_ARG);
  17472. /* Good case */
  17473. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  17474. 0);
  17475. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17476. wc_FreeRng(&rng);
  17477. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17478. #endif
  17479. return EXPECT_RESULT();
  17480. } /* END test_wc_CheckProbablePrime */
  17481. /*
  17482. * Testing wc_RsaPSS_Verify()
  17483. */
  17484. static int test_wc_RsaPSS_Verify(void)
  17485. {
  17486. EXPECT_DECLS;
  17487. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17488. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17489. RsaKey key;
  17490. WC_RNG rng;
  17491. int sz = 256;
  17492. const char* szMessage = "This is the string to be signed";
  17493. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17494. unsigned char pDecrypted[2048/8];
  17495. byte* pt = pDecrypted;
  17496. word32 outLen = sizeof(pDecrypted);
  17497. XMEMSET(&key, 0, sizeof(RsaKey));
  17498. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17499. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17500. ExpectIntEQ(wc_InitRng(&rng), 0);
  17501. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17502. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17503. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  17504. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  17505. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17506. /* Bad cases */
  17507. ExpectIntEQ(wc_RsaPSS_Verify(NULL, (word32)sz, pt, outLen,
  17508. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17509. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17510. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17511. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, (word32)sz, NULL, outLen,
  17512. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17513. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17514. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17515. /* Good case */
  17516. ExpectIntGT(wc_RsaPSS_Verify(pSignature, (word32)sz, pt, outLen,
  17517. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17518. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17519. wc_FreeRng(&rng);
  17520. #endif
  17521. return EXPECT_RESULT();
  17522. } /* END test_wc_RsaPSS_Verify */
  17523. /*
  17524. * Testing wc_RsaPSS_VerifyCheck()
  17525. */
  17526. static int test_wc_RsaPSS_VerifyCheck(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; /* 2048/8 */
  17534. byte digest[32];
  17535. word32 digestSz = sizeof(digest);
  17536. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17537. word32 pSignatureSz = sizeof(pSignature);
  17538. unsigned char pDecrypted[2048/8];
  17539. byte* pt = pDecrypted;
  17540. word32 outLen = sizeof(pDecrypted);
  17541. XMEMSET(&key, 0, sizeof(RsaKey));
  17542. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17543. XMEMSET(digest, 0, sizeof(digest));
  17544. XMEMSET(pSignature, 0, sizeof(pSignature));
  17545. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17546. ExpectIntEQ(wc_InitRng(&rng), 0);
  17547. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17548. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17549. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17550. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17551. 0);
  17552. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17553. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17554. /* Bad cases */
  17555. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, (word32)sz, pt, outLen, digest,
  17556. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17557. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  17558. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17559. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, NULL, outLen, digest,
  17560. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17561. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  17562. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17563. /* Good case */
  17564. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, pt, outLen, digest,
  17565. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17566. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17567. wc_FreeRng(&rng);
  17568. #endif
  17569. return EXPECT_RESULT();
  17570. } /* END test_wc_RsaPSS_VerifyCheck */
  17571. /*
  17572. * Testing wc_RsaPSS_VerifyCheckInline()
  17573. */
  17574. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17575. {
  17576. EXPECT_DECLS;
  17577. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17578. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17579. RsaKey key;
  17580. WC_RNG rng;
  17581. int sz = 256;
  17582. byte digest[32];
  17583. word32 digestSz = sizeof(digest);
  17584. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17585. unsigned char pDecrypted[2048/8];
  17586. byte* pt = pDecrypted;
  17587. XMEMSET(&key, 0, sizeof(RsaKey));
  17588. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17589. XMEMSET(digest, 0, sizeof(digest));
  17590. XMEMSET(pSignature, 0, sizeof(pSignature));
  17591. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17592. ExpectIntEQ(wc_InitRng(&rng), 0);
  17593. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17594. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17595. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17596. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17597. 0);
  17598. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17599. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17600. /* Bad Cases */
  17601. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, (word32)sz, &pt, digest,
  17602. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17603. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17604. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17605. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17606. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17607. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17608. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17609. /* Good case */
  17610. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17611. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17612. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17613. wc_FreeRng(&rng);
  17614. #endif
  17615. return EXPECT_RESULT();
  17616. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17617. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17618. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17619. {
  17620. (void)flag;
  17621. (void)type;
  17622. (void)file;
  17623. (void)line;
  17624. }
  17625. #endif
  17626. /*
  17627. * Testing wc_LockMutex_ex
  17628. */
  17629. static int test_wc_LockMutex_ex(void)
  17630. {
  17631. EXPECT_DECLS;
  17632. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17633. int flag = CRYPTO_LOCK;
  17634. int type = 0;
  17635. const char* file = "./test-LockMutex_ex.txt";
  17636. int line = 0;
  17637. /* without SetMutexCb */
  17638. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17639. /* with SetMutexCb */
  17640. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17641. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17642. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17643. #endif
  17644. return EXPECT_RESULT();
  17645. } /* End test_wc_LockMutex_ex*/
  17646. /*
  17647. * Testing wc_SetMutexCb
  17648. */
  17649. static int test_wc_SetMutexCb(void)
  17650. {
  17651. EXPECT_DECLS;
  17652. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17653. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17654. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17655. #endif
  17656. return EXPECT_RESULT();
  17657. } /* End test_wc_SetMutexCb*/
  17658. /*
  17659. * Testing wc_RsaKeyToDer()
  17660. */
  17661. static int test_wc_RsaKeyToDer(void)
  17662. {
  17663. EXPECT_DECLS;
  17664. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17665. RsaKey genKey;
  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 derSz = 611;
  17672. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17673. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17674. #else
  17675. int bits = 2048;
  17676. word32 derSz = 1196;
  17677. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17678. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17679. #endif
  17680. XMEMSET(&rng, 0, sizeof(rng));
  17681. XMEMSET(&genKey, 0, sizeof(genKey));
  17682. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17683. /* Init structures. */
  17684. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17685. ExpectIntEQ(wc_InitRng(&rng), 0);
  17686. /* Make key. */
  17687. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17688. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17689. /* Pass good/bad args. */
  17690. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17691. /* Get just the output length */
  17692. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17693. /* Try Public Key. */
  17694. genKey.type = 0;
  17695. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17696. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17697. /* Put back to Private Key */
  17698. genKey.type = 1;
  17699. #endif
  17700. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17701. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17702. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17703. #endif
  17704. return EXPECT_RESULT();
  17705. } /* END test_wc_RsaKeyToDer */
  17706. /*
  17707. * Testing wc_RsaKeyToPublicDer()
  17708. */
  17709. static int test_wc_RsaKeyToPublicDer(void)
  17710. {
  17711. EXPECT_DECLS;
  17712. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17713. RsaKey key;
  17714. WC_RNG rng;
  17715. byte* der = NULL;
  17716. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17717. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17718. int bits = 1024;
  17719. word32 derLen = 162;
  17720. #else
  17721. int bits = 2048;
  17722. word32 derLen = 294;
  17723. #endif
  17724. int ret;
  17725. XMEMSET(&rng, 0, sizeof(rng));
  17726. XMEMSET(&key, 0, sizeof(key));
  17727. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17728. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17729. ExpectIntEQ(wc_InitRng(&rng), 0);
  17730. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17731. /* test getting size only */
  17732. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17733. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17734. /* test getting size only */
  17735. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17736. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17737. /* Pass in bad args. */
  17738. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17739. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17740. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17741. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17742. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17743. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17744. #endif
  17745. return EXPECT_RESULT();
  17746. } /* END test_wc_RsaKeyToPublicDer */
  17747. /*
  17748. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17749. */
  17750. static int test_wc_RsaPublicEncryptDecrypt(void)
  17751. {
  17752. EXPECT_DECLS;
  17753. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17754. RsaKey key;
  17755. WC_RNG rng;
  17756. const char inStr[] = TEST_STRING;
  17757. const word32 plainLen = (word32)TEST_STRING_SZ;
  17758. const word32 inLen = (word32)TEST_STRING_SZ;
  17759. int bits = TEST_RSA_BITS;
  17760. const word32 cipherLen = TEST_RSA_BYTES;
  17761. word32 cipherLenResult = cipherLen;
  17762. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17763. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17764. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17765. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17766. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17767. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17768. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17769. ExpectNotNull(in);
  17770. ExpectNotNull(plain);
  17771. ExpectNotNull(cipher);
  17772. #endif
  17773. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17774. /* Initialize stack structures. */
  17775. XMEMSET(&key, 0, sizeof(RsaKey));
  17776. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17777. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17778. ExpectIntEQ(wc_InitRng(&rng), 0);
  17779. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17780. /* Encrypt. */
  17781. ExpectIntGT(cipherLenResult = (word32)wc_RsaPublicEncrypt(in, inLen, cipher,
  17782. cipherLen, &key, &rng), 0);
  17783. /* Pass bad args - tested in another testing function.*/
  17784. /* Decrypt */
  17785. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17786. /* Bind rng */
  17787. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17788. #endif
  17789. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17790. &key), 0);
  17791. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17792. /* Pass bad args - tested in another testing function.*/
  17793. WC_FREE_VAR(in, NULL);
  17794. WC_FREE_VAR(plain, NULL);
  17795. WC_FREE_VAR(cipher, NULL);
  17796. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17797. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17798. #endif
  17799. return EXPECT_RESULT();
  17800. } /* END test_wc_RsaPublicEncryptDecrypt */
  17801. /*
  17802. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17803. */
  17804. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17805. {
  17806. EXPECT_DECLS;
  17807. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17808. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17809. RsaKey key;
  17810. WC_RNG rng;
  17811. const char inStr[] = TEST_STRING;
  17812. const word32 inLen = (word32)TEST_STRING_SZ;
  17813. const word32 plainSz = (word32)TEST_STRING_SZ;
  17814. byte* res = NULL;
  17815. int idx = 0;
  17816. int bits = TEST_RSA_BITS;
  17817. const word32 cipherSz = TEST_RSA_BYTES;
  17818. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17819. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17820. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17821. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17822. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17823. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17824. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17825. ExpectNotNull(in);
  17826. ExpectNotNull(plain);
  17827. ExpectNotNull(cipher);
  17828. #endif
  17829. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17830. /* Initialize stack structures. */
  17831. XMEMSET(&key, 0, sizeof(RsaKey));
  17832. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17833. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17834. ExpectIntEQ(wc_InitRng(&rng), 0);
  17835. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17836. /* Encrypt */
  17837. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17838. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17839. /* Pass bad args - tested in another testing function.*/
  17840. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17841. /* Decrypt */
  17842. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17843. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17844. #endif
  17845. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17846. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17847. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17848. /* Pass bad args - tested in another testing function.*/
  17849. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17850. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17851. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17852. #endif
  17853. WC_FREE_VAR(in, NULL);
  17854. WC_FREE_VAR(plain, NULL);
  17855. WC_FREE_VAR(cipher, NULL);
  17856. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17857. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17858. #endif
  17859. return EXPECT_RESULT();
  17860. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17861. /*
  17862. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17863. */
  17864. static int test_wc_RsaSSL_SignVerify(void)
  17865. {
  17866. EXPECT_DECLS;
  17867. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17868. RsaKey key;
  17869. WC_RNG rng;
  17870. const char inStr[] = TEST_STRING;
  17871. const word32 plainSz = (word32)TEST_STRING_SZ;
  17872. const word32 inLen = (word32)TEST_STRING_SZ;
  17873. word32 idx = 0;
  17874. int bits = TEST_RSA_BITS;
  17875. const word32 outSz = TEST_RSA_BYTES;
  17876. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17877. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17878. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17879. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17880. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17881. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17882. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17883. ExpectNotNull(in);
  17884. ExpectNotNull(out);
  17885. ExpectNotNull(plain);
  17886. #endif
  17887. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17888. XMEMSET(&key, 0, sizeof(RsaKey));
  17889. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17890. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17891. ExpectIntEQ(wc_InitRng(&rng), 0);
  17892. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17893. /* Sign. */
  17894. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17895. idx = (int)outSz;
  17896. /* Test bad args. */
  17897. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17898. BAD_FUNC_ARG);
  17899. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17900. BAD_FUNC_ARG);
  17901. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17902. BAD_FUNC_ARG);
  17903. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17904. BAD_FUNC_ARG);
  17905. /* Verify. */
  17906. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17907. /* Pass bad args. */
  17908. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17909. BAD_FUNC_ARG);
  17910. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17911. BAD_FUNC_ARG);
  17912. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17913. BAD_FUNC_ARG);
  17914. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17915. BAD_FUNC_ARG);
  17916. WC_FREE_VAR(in, NULL);
  17917. WC_FREE_VAR(out, NULL);
  17918. WC_FREE_VAR(plain, NULL);
  17919. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17920. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17921. #endif
  17922. return EXPECT_RESULT();
  17923. } /* END test_wc_RsaSSL_SignVerify */
  17924. /*
  17925. * Testing wc_RsaEncryptSize()
  17926. */
  17927. static int test_wc_RsaEncryptSize(void)
  17928. {
  17929. EXPECT_DECLS;
  17930. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17931. RsaKey key;
  17932. WC_RNG rng;
  17933. XMEMSET(&key, 0, sizeof(RsaKey));
  17934. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17935. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17936. ExpectIntEQ(wc_InitRng(&rng), 0);
  17937. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17938. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17939. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17940. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17941. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17942. #endif
  17943. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17944. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17945. /* Pass in bad arg. */
  17946. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17947. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17948. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17949. #endif
  17950. return EXPECT_RESULT();
  17951. } /* END test_wc_RsaEncryptSize*/
  17952. /*
  17953. * Testing wc_RsaFlattenPublicKey()
  17954. */
  17955. static int test_wc_RsaFlattenPublicKey(void)
  17956. {
  17957. EXPECT_DECLS;
  17958. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17959. RsaKey key;
  17960. WC_RNG rng;
  17961. byte e[256];
  17962. byte n[256];
  17963. word32 eSz = sizeof(e);
  17964. word32 nSz = sizeof(n);
  17965. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17966. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17967. int bits = 1024;
  17968. #else
  17969. int bits = 2048;
  17970. #endif
  17971. XMEMSET(&key, 0, sizeof(RsaKey));
  17972. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17973. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17974. ExpectIntEQ(wc_InitRng(&rng), 0);
  17975. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17976. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17977. /* Pass bad args. */
  17978. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17979. BAD_FUNC_ARG);
  17980. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17981. BAD_FUNC_ARG);
  17982. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17983. BAD_FUNC_ARG);
  17984. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17985. BAD_FUNC_ARG);
  17986. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17987. BAD_FUNC_ARG);
  17988. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17989. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17990. #endif
  17991. return EXPECT_RESULT();
  17992. } /* END test_wc_RsaFlattenPublicKey */
  17993. /*
  17994. * unit test for wc_AesCcmSetKey
  17995. */
  17996. static int test_wc_AesCcmSetKey(void)
  17997. {
  17998. EXPECT_DECLS;
  17999. #ifdef HAVE_AESCCM
  18000. Aes aes;
  18001. const byte key16[] = {
  18002. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18003. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18004. };
  18005. const byte key24[] = {
  18006. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18007. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  18008. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  18009. };
  18010. const byte key32[] = {
  18011. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18012. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  18013. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18014. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  18015. };
  18016. XMEMSET(&aes, 0, sizeof(Aes));
  18017. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  18018. #ifdef WOLFSSL_AES_128
  18019. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  18020. #endif
  18021. #ifdef WOLFSSL_AES_192
  18022. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  18023. #endif
  18024. #ifdef WOLFSSL_AES_256
  18025. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  18026. #endif
  18027. /* Test bad args. */
  18028. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  18029. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  18030. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  18031. wc_AesFree(&aes);
  18032. #endif
  18033. return EXPECT_RESULT();
  18034. } /* END test_wc_AesCcmSetKey */
  18035. /*
  18036. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  18037. */
  18038. static int test_wc_AesCcmEncryptDecrypt(void)
  18039. {
  18040. EXPECT_DECLS;
  18041. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  18042. Aes aes;
  18043. const byte key16[] = {
  18044. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18045. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18046. };
  18047. /* plaintext */
  18048. const byte plainT[] = {
  18049. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  18050. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18051. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  18052. };
  18053. /* nonce */
  18054. const byte iv[] = {
  18055. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  18056. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  18057. };
  18058. const byte c[] = { /* cipher text. */
  18059. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  18060. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  18061. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  18062. };
  18063. const byte t[] = { /* Auth tag */
  18064. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  18065. };
  18066. const byte authIn[] = {
  18067. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  18068. };
  18069. byte cipherOut[sizeof(plainT)];
  18070. byte authTag[sizeof(t)];
  18071. #ifdef HAVE_AES_DECRYPT
  18072. byte plainOut[sizeof(cipherOut)];
  18073. #endif
  18074. XMEMSET(&aes, 0, sizeof(Aes));
  18075. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  18076. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  18077. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18078. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  18079. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  18080. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  18081. #ifdef HAVE_AES_DECRYPT
  18082. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18083. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  18084. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  18085. #endif
  18086. /* Pass in bad args. Encrypt*/
  18087. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  18088. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18089. BAD_FUNC_ARG);
  18090. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  18091. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18092. BAD_FUNC_ARG);
  18093. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  18094. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18095. BAD_FUNC_ARG);
  18096. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18097. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18098. BAD_FUNC_ARG);
  18099. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18100. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  18101. BAD_FUNC_ARG);
  18102. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18103. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18104. BAD_FUNC_ARG);
  18105. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18106. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18107. BAD_FUNC_ARG);
  18108. #ifdef HAVE_AES_DECRYPT
  18109. /* Pass in bad args. Decrypt*/
  18110. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  18111. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18112. BAD_FUNC_ARG);
  18113. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  18114. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18115. BAD_FUNC_ARG);
  18116. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  18117. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18118. BAD_FUNC_ARG);
  18119. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18120. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18121. BAD_FUNC_ARG);
  18122. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18123. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  18124. BAD_FUNC_ARG);
  18125. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18126. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18127. BAD_FUNC_ARG);
  18128. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18129. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18130. BAD_FUNC_ARG);
  18131. #endif
  18132. wc_AesFree(&aes);
  18133. #endif /* HAVE_AESCCM */
  18134. return EXPECT_RESULT();
  18135. } /* END test_wc_AesCcmEncryptDecrypt */
  18136. #if defined(WOLFSSL_AES_EAX) && \
  18137. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  18138. /*
  18139. * Testing test_wc_AesEaxVectors()
  18140. */
  18141. static int test_wc_AesEaxVectors(void)
  18142. {
  18143. EXPECT_DECLS;
  18144. typedef struct {
  18145. byte key[AES_256_KEY_SIZE];
  18146. int key_length;
  18147. byte iv[AES_BLOCK_SIZE];
  18148. int iv_length;
  18149. byte aad[AES_BLOCK_SIZE * 2];
  18150. int aad_length;
  18151. byte msg[AES_BLOCK_SIZE * 5];
  18152. int msg_length;
  18153. byte ct[AES_BLOCK_SIZE * 5];
  18154. int ct_length;
  18155. byte tag[AES_BLOCK_SIZE];
  18156. int tag_length;
  18157. int valid;
  18158. } AadVector;
  18159. /* Test vectors obtained from Google wycheproof project
  18160. * https://github.com/google/wycheproof
  18161. * from testvectors/aes_eax_test.json
  18162. */
  18163. const AadVector vectors[] = {
  18164. {
  18165. /* key, key length */
  18166. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  18167. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  18168. /* iv, iv length */
  18169. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  18170. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  18171. /* aad, aad length */
  18172. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  18173. /* msg, msg length */
  18174. {0x00}, 0,
  18175. /* ct, ct length */
  18176. {0x00}, 0,
  18177. /* tag, tag length */
  18178. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  18179. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  18180. /* valid */
  18181. 1,
  18182. },
  18183. {
  18184. /* key, key length */
  18185. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  18186. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  18187. /* iv, iv length */
  18188. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  18189. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  18190. /* aad, aad length */
  18191. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  18192. /* msg, msg length */
  18193. {0xf7, 0xfb}, 2,
  18194. /* ct, ct length */
  18195. {0x19, 0xdd}, 2,
  18196. /* tag, tag length */
  18197. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  18198. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  18199. /* valid */
  18200. 1,
  18201. },
  18202. {
  18203. /* key, key length */
  18204. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  18205. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  18206. /* iv, iv length */
  18207. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  18208. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  18209. /* aad, aad length */
  18210. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  18211. /* msg, msg length */
  18212. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  18213. /* ct, ct length */
  18214. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  18215. /* tag, tag length */
  18216. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  18217. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  18218. /* valid */
  18219. 1,
  18220. },
  18221. {
  18222. /* key, key length */
  18223. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  18224. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  18225. /* iv, iv length */
  18226. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  18227. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  18228. /* aad, aad length */
  18229. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  18230. /* msg, msg length */
  18231. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  18232. /* ct, ct length */
  18233. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  18234. /* tag, tag length */
  18235. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  18236. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  18237. /* valid */
  18238. 1,
  18239. },
  18240. {
  18241. /* key, key length */
  18242. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  18243. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  18244. /* iv, iv length */
  18245. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  18246. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  18247. /* aad, aad length */
  18248. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  18249. /* msg, msg length */
  18250. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  18251. /* ct, ct length */
  18252. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  18253. /* tag, tag length */
  18254. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  18255. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  18256. /* valid */
  18257. 1,
  18258. },
  18259. {
  18260. /* key, key length */
  18261. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  18262. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  18263. /* iv, iv length */
  18264. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  18265. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  18266. /* aad, aad length */
  18267. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  18268. /* msg, msg length */
  18269. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  18270. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  18271. /* ct, ct length */
  18272. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  18273. 0x0e, 0x72, 0x84, 0x14}, 12,
  18274. /* tag, tag length */
  18275. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  18276. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  18277. /* valid */
  18278. 1,
  18279. },
  18280. {
  18281. /* key, key length */
  18282. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  18283. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  18284. /* iv, iv length */
  18285. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  18286. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  18287. /* aad, aad length */
  18288. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  18289. /* msg, msg length */
  18290. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  18291. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  18292. 0x36}, 17,
  18293. /* ct, ct length */
  18294. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  18295. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  18296. 0x30}, 17,
  18297. /* tag, tag length */
  18298. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  18299. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  18300. /* valid */
  18301. 1,
  18302. },
  18303. {
  18304. /* key, key length */
  18305. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  18306. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  18307. /* iv, iv length */
  18308. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  18309. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  18310. /* aad, aad length */
  18311. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  18312. /* msg, msg length */
  18313. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  18314. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  18315. 0x2d, 0x56}, 18,
  18316. /* ct, ct length */
  18317. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  18318. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  18319. 0x8c, 0xc3}, 18,
  18320. /* tag, tag length */
  18321. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  18322. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  18323. /* valid */
  18324. 1,
  18325. },
  18326. {
  18327. /* key, key length */
  18328. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  18329. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  18330. /* iv, iv length */
  18331. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  18332. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  18333. /* aad, aad length */
  18334. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  18335. /* msg, msg length */
  18336. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  18337. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  18338. 0xef, 0x11}, 18,
  18339. /* ct, ct length */
  18340. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  18341. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  18342. 0xb1, 0xe8}, 18,
  18343. /* tag, tag length */
  18344. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  18345. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  18346. /* valid */
  18347. 1,
  18348. },
  18349. {
  18350. /* key, key length */
  18351. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  18352. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  18353. /* iv, iv length */
  18354. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  18355. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  18356. /* aad, aad length */
  18357. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  18358. /* msg, msg length */
  18359. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  18360. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  18361. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  18362. /* ct, ct length */
  18363. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  18364. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  18365. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  18366. /* tag, tag length */
  18367. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  18368. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  18369. /* valid */
  18370. 1,
  18371. },
  18372. {
  18373. /* key, key length */
  18374. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18375. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18376. /* iv, iv length */
  18377. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  18378. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  18379. /* aad, aad length */
  18380. {0x00}, 0,
  18381. /* msg, msg length */
  18382. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18383. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18384. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18385. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18386. /* ct, ct length */
  18387. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  18388. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  18389. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  18390. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  18391. /* tag, tag length */
  18392. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  18393. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  18394. /* valid */
  18395. 1,
  18396. },
  18397. {
  18398. /* key, key length */
  18399. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18400. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18401. /* iv, iv length */
  18402. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  18403. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  18404. /* aad, aad length */
  18405. {0x00}, 0,
  18406. /* msg, msg length */
  18407. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18408. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18409. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18410. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18411. /* ct, ct length */
  18412. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  18413. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  18414. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  18415. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  18416. /* tag, tag length */
  18417. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  18418. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  18419. /* valid */
  18420. 1,
  18421. },
  18422. {
  18423. /* key, key length */
  18424. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18425. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18426. /* iv, iv length */
  18427. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  18428. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  18429. /* aad, aad length */
  18430. {0x00}, 0,
  18431. /* msg, msg length */
  18432. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18433. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18434. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18435. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18436. /* ct, ct length */
  18437. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  18438. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  18439. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  18440. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  18441. /* tag, tag length */
  18442. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  18443. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  18444. /* valid */
  18445. 1,
  18446. },
  18447. {
  18448. /* key, key length */
  18449. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18450. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18451. /* iv, iv length */
  18452. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  18453. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  18454. /* aad, aad length */
  18455. {0x00}, 0,
  18456. /* msg, msg length */
  18457. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18458. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18459. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18460. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18461. /* ct, ct length */
  18462. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  18463. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  18464. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  18465. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  18466. /* tag, tag length */
  18467. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  18468. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  18469. /* valid */
  18470. 1,
  18471. },
  18472. {
  18473. /* key, key length */
  18474. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18475. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18476. /* iv, iv length */
  18477. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  18478. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  18479. /* aad, aad length */
  18480. {0x00}, 0,
  18481. /* msg, msg length */
  18482. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18483. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18484. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18485. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18486. /* ct, ct length */
  18487. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  18488. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  18489. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  18490. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  18491. /* tag, tag length */
  18492. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  18493. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  18494. /* valid */
  18495. 1,
  18496. },
  18497. {
  18498. /* key, key length */
  18499. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18500. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18501. /* iv, iv length */
  18502. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  18503. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  18504. /* aad, aad length */
  18505. {0x00}, 0,
  18506. /* msg, msg length */
  18507. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18508. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18509. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18510. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18511. /* ct, ct length */
  18512. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  18513. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  18514. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  18515. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  18516. /* tag, tag length */
  18517. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  18518. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  18519. /* valid */
  18520. 1,
  18521. },
  18522. {
  18523. /* key, key length */
  18524. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18525. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18526. /* iv, iv length */
  18527. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  18528. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  18529. /* aad, aad length */
  18530. {0x00}, 0,
  18531. /* msg, msg length */
  18532. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18533. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18534. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18535. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18536. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18537. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18538. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18539. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  18540. /* ct, ct length */
  18541. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  18542. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  18543. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  18544. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  18545. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  18546. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  18547. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  18548. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  18549. /* tag, tag length */
  18550. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  18551. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  18552. /* valid */
  18553. 1,
  18554. },
  18555. {
  18556. /* key, key length */
  18557. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18558. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18559. /* iv, iv length */
  18560. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  18561. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  18562. /* aad, aad length */
  18563. {0x00}, 0,
  18564. /* msg, msg length */
  18565. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18566. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18567. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18568. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18569. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18570. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18571. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18572. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18573. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18574. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18575. /* ct, ct length */
  18576. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18577. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18578. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18579. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18580. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18581. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18582. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18583. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18584. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18585. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18586. /* tag, tag length */
  18587. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18588. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18589. /* valid */
  18590. 1,
  18591. },
  18592. {
  18593. /* key, key length */
  18594. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18595. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18596. /* iv, iv length */
  18597. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18598. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18599. /* aad, aad length */
  18600. {0x00}, 0,
  18601. /* msg, msg length */
  18602. {0x00}, 0,
  18603. /* ct, ct length */
  18604. {0x00}, 0,
  18605. /* tag, tag length */
  18606. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18607. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18608. /* valid */
  18609. 1,
  18610. },
  18611. {
  18612. /* key, key length */
  18613. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18614. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18615. /* iv, iv length */
  18616. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18617. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18618. /* aad, aad length */
  18619. {0x00}, 0,
  18620. /* msg, msg length */
  18621. {0x01}, 1,
  18622. /* ct, ct length */
  18623. {0x1d}, 1,
  18624. /* tag, tag length */
  18625. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18626. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18627. /* valid */
  18628. 1,
  18629. },
  18630. {
  18631. /* key, key length */
  18632. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18633. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18634. /* iv, iv length */
  18635. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18636. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18637. /* aad, aad length */
  18638. {0x00}, 0,
  18639. /* msg, msg length */
  18640. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18641. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18642. /* ct, ct length */
  18643. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18644. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18645. /* tag, tag length */
  18646. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18647. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18648. /* valid */
  18649. 1,
  18650. },
  18651. {
  18652. /* key, key length */
  18653. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18654. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18655. /* iv, iv length */
  18656. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18657. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18658. /* aad, aad length */
  18659. {0x00}, 0,
  18660. /* msg, msg length */
  18661. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18662. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18663. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18664. /* ct, ct length */
  18665. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18666. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18667. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18668. /* tag, tag length */
  18669. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18670. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18671. /* valid */
  18672. 1,
  18673. },
  18674. {
  18675. /* key, key length */
  18676. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18677. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18678. /* iv, iv length */
  18679. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18680. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18681. /* aad, aad length */
  18682. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18683. /* msg, msg length */
  18684. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18685. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18686. /* ct, ct length */
  18687. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18688. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18689. /* tag, tag length */
  18690. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18691. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18692. /* valid */
  18693. 1,
  18694. },
  18695. {
  18696. /* key, key length */
  18697. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18698. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18699. /* iv, iv length */
  18700. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18701. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18702. /* aad, aad length */
  18703. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18704. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18705. /* msg, msg length */
  18706. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18707. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18708. /* ct, ct length */
  18709. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18710. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18711. /* tag, tag length */
  18712. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18713. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18714. /* valid */
  18715. 1,
  18716. },
  18717. {
  18718. /* key, key length */
  18719. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18720. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18721. /* iv, iv length */
  18722. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18723. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18724. /* aad, aad length */
  18725. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18726. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18727. /* msg, msg length */
  18728. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18729. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18730. /* ct, ct length */
  18731. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18732. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18733. /* tag, tag length */
  18734. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18735. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18736. /* valid */
  18737. 1,
  18738. },
  18739. {
  18740. /* key, key length */
  18741. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18742. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18743. /* iv, iv length */
  18744. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18745. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18746. /* aad, aad length */
  18747. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18748. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18749. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18750. /* msg, msg length */
  18751. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18752. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18753. /* ct, ct length */
  18754. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18755. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18756. /* tag, tag length */
  18757. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18758. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18759. /* valid */
  18760. 1,
  18761. },
  18762. {
  18763. /* key, key length */
  18764. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18765. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18766. /* iv, iv length */
  18767. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18768. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18769. /* aad, aad length */
  18770. {0x00}, 0,
  18771. /* msg, msg length */
  18772. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18773. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18774. /* ct, ct length */
  18775. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18776. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18777. /* tag, tag length */
  18778. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18779. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18780. /* valid */
  18781. 0,
  18782. },
  18783. {
  18784. /* key, key length */
  18785. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18786. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18787. /* iv, iv length */
  18788. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18789. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18790. /* aad, aad length */
  18791. {0x00}, 0,
  18792. /* msg, msg length */
  18793. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18794. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18795. /* ct, ct length */
  18796. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18797. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18798. /* tag, tag length */
  18799. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18800. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18801. /* valid */
  18802. 0,
  18803. },
  18804. {
  18805. /* key, key length */
  18806. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18807. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18808. /* iv, iv length */
  18809. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18810. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18811. /* aad, aad length */
  18812. {0x00}, 0,
  18813. /* msg, msg length */
  18814. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18815. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18816. /* ct, ct length */
  18817. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18818. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18819. /* tag, tag length */
  18820. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18821. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18822. /* valid */
  18823. 0,
  18824. },
  18825. {
  18826. /* key, key length */
  18827. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18828. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18829. /* iv, iv length */
  18830. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18831. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18832. /* aad, aad length */
  18833. {0x00}, 0,
  18834. /* msg, msg length */
  18835. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18836. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18837. /* ct, ct length */
  18838. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18839. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18840. /* tag, tag length */
  18841. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18842. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18843. /* valid */
  18844. 0,
  18845. },
  18846. {
  18847. /* key, key length */
  18848. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18849. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18850. /* iv, iv length */
  18851. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18852. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18853. /* aad, aad length */
  18854. {0x00}, 0,
  18855. /* msg, msg length */
  18856. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18857. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18858. /* ct, ct length */
  18859. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18860. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18861. /* tag, tag length */
  18862. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18863. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18864. /* valid */
  18865. 0,
  18866. },
  18867. {
  18868. /* key, key length */
  18869. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18870. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18871. /* iv, iv length */
  18872. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18873. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18874. /* aad, aad length */
  18875. {0x00}, 0,
  18876. /* msg, msg length */
  18877. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18878. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18879. /* ct, ct length */
  18880. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18881. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18882. /* tag, tag length */
  18883. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18884. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18885. /* valid */
  18886. 0,
  18887. },
  18888. {
  18889. /* key, key length */
  18890. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18891. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18892. /* iv, iv length */
  18893. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18894. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18895. /* aad, aad length */
  18896. {0x00}, 0,
  18897. /* msg, msg length */
  18898. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18899. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18900. /* ct, ct length */
  18901. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18902. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18903. /* tag, tag length */
  18904. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18905. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18906. /* valid */
  18907. 0,
  18908. },
  18909. {
  18910. /* key, key length */
  18911. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18912. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18913. /* iv, iv length */
  18914. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18915. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18916. /* aad, aad length */
  18917. {0x00}, 0,
  18918. /* msg, msg length */
  18919. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18920. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18921. /* ct, ct length */
  18922. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18923. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18924. /* tag, tag length */
  18925. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18926. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18927. /* valid */
  18928. 0,
  18929. },
  18930. {
  18931. /* key, key length */
  18932. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18933. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18934. /* iv, iv length */
  18935. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18936. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18937. /* aad, aad length */
  18938. {0x00}, 0,
  18939. /* msg, msg length */
  18940. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18941. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18942. /* ct, ct length */
  18943. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18944. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18945. /* tag, tag length */
  18946. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18947. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18948. /* valid */
  18949. 0,
  18950. },
  18951. {
  18952. /* key, key length */
  18953. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18954. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18955. /* iv, iv length */
  18956. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18957. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18958. /* aad, aad length */
  18959. {0x00}, 0,
  18960. /* msg, msg length */
  18961. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18962. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18963. /* ct, ct length */
  18964. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18965. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18966. /* tag, tag length */
  18967. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18968. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18969. /* valid */
  18970. 0,
  18971. },
  18972. {
  18973. /* key, key length */
  18974. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18975. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18976. /* iv, iv length */
  18977. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18978. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18979. /* aad, aad length */
  18980. {0x00}, 0,
  18981. /* msg, msg length */
  18982. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18983. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18984. /* ct, ct length */
  18985. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18986. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18987. /* tag, tag length */
  18988. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18989. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18990. /* valid */
  18991. 0,
  18992. },
  18993. {
  18994. /* key, key length */
  18995. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18996. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18997. /* iv, iv length */
  18998. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18999. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19000. /* aad, aad length */
  19001. {0x00}, 0,
  19002. /* msg, msg length */
  19003. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19004. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19005. /* ct, ct length */
  19006. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19007. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19008. /* tag, tag length */
  19009. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19010. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19011. /* valid */
  19012. 0,
  19013. },
  19014. {
  19015. /* key, key length */
  19016. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19017. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19018. /* iv, iv length */
  19019. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19020. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19021. /* aad, aad length */
  19022. {0x00}, 0,
  19023. /* msg, msg length */
  19024. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19025. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19026. /* ct, ct length */
  19027. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19028. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19029. /* tag, tag length */
  19030. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19031. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  19032. /* valid */
  19033. 0,
  19034. },
  19035. {
  19036. /* key, key length */
  19037. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19038. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19039. /* iv, iv length */
  19040. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19041. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19042. /* aad, aad length */
  19043. {0x00}, 0,
  19044. /* msg, msg length */
  19045. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19046. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19047. /* ct, ct length */
  19048. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19049. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19050. /* tag, tag length */
  19051. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19052. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  19053. /* valid */
  19054. 0,
  19055. },
  19056. {
  19057. /* key, key length */
  19058. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19059. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19060. /* iv, iv length */
  19061. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19062. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19063. /* aad, aad length */
  19064. {0x00}, 0,
  19065. /* msg, msg length */
  19066. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19067. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19068. /* ct, ct length */
  19069. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19070. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19071. /* tag, tag length */
  19072. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19073. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  19074. /* valid */
  19075. 0,
  19076. },
  19077. {
  19078. /* key, key length */
  19079. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19080. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19081. /* iv, iv length */
  19082. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19083. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19084. /* aad, aad length */
  19085. {0x00}, 0,
  19086. /* msg, msg length */
  19087. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19088. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19089. /* ct, ct length */
  19090. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19091. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19092. /* tag, tag length */
  19093. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19094. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  19095. /* valid */
  19096. 0,
  19097. },
  19098. {
  19099. /* key, key length */
  19100. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19101. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19102. /* iv, iv length */
  19103. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19104. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19105. /* aad, aad length */
  19106. {0x00}, 0,
  19107. /* msg, msg length */
  19108. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19109. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19110. /* ct, ct length */
  19111. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19112. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19113. /* tag, tag length */
  19114. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19115. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  19116. /* valid */
  19117. 0,
  19118. },
  19119. {
  19120. /* key, key length */
  19121. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19122. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19123. /* iv, iv length */
  19124. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19125. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19126. /* aad, aad length */
  19127. {0x00}, 0,
  19128. /* msg, msg length */
  19129. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19130. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19131. /* ct, ct length */
  19132. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19133. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19134. /* tag, tag length */
  19135. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19136. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  19137. /* valid */
  19138. 0,
  19139. },
  19140. {
  19141. /* key, key length */
  19142. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19143. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19144. /* iv, iv length */
  19145. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19146. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19147. /* aad, aad length */
  19148. {0x00}, 0,
  19149. /* msg, msg length */
  19150. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19151. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19152. /* ct, ct length */
  19153. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19154. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19155. /* tag, tag length */
  19156. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19157. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19158. /* valid */
  19159. 0,
  19160. },
  19161. {
  19162. /* key, key length */
  19163. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19164. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19165. /* iv, iv length */
  19166. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19167. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19168. /* aad, aad length */
  19169. {0x00}, 0,
  19170. /* msg, msg length */
  19171. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19172. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19173. /* ct, ct length */
  19174. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19175. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19176. /* tag, tag length */
  19177. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19178. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19179. /* valid */
  19180. 0,
  19181. },
  19182. {
  19183. /* key, key length */
  19184. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19185. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19186. /* iv, iv length */
  19187. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19188. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19189. /* aad, aad length */
  19190. {0x00}, 0,
  19191. /* msg, msg length */
  19192. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19193. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19194. /* ct, ct length */
  19195. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19196. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19197. /* tag, tag length */
  19198. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  19199. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19200. /* valid */
  19201. 0,
  19202. },
  19203. {
  19204. /* key, key length */
  19205. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19206. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19207. /* iv, iv length */
  19208. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19209. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19210. /* aad, aad length */
  19211. {0x00}, 0,
  19212. /* msg, msg length */
  19213. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19214. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19215. /* ct, ct length */
  19216. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19217. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19218. /* tag, tag length */
  19219. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  19220. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19221. /* valid */
  19222. 0,
  19223. },
  19224. {
  19225. /* key, key length */
  19226. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19227. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19228. /* iv, iv length */
  19229. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19230. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19231. /* aad, aad length */
  19232. {0x00}, 0,
  19233. /* msg, msg length */
  19234. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19235. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19236. /* ct, ct length */
  19237. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19238. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19239. /* tag, tag length */
  19240. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  19241. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  19242. /* valid */
  19243. 0,
  19244. },
  19245. {
  19246. /* key, key length */
  19247. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19248. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19249. /* iv, iv length */
  19250. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19251. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19252. /* aad, aad length */
  19253. {0x00}, 0,
  19254. /* msg, msg length */
  19255. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19256. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19257. /* ct, ct length */
  19258. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19259. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19260. /* tag, tag length */
  19261. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19262. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  19263. /* valid */
  19264. 0,
  19265. },
  19266. {
  19267. /* key, key length */
  19268. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19269. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19270. /* iv, iv length */
  19271. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19272. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19273. /* aad, aad length */
  19274. {0x00}, 0,
  19275. /* msg, msg length */
  19276. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19277. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19278. /* ct, ct length */
  19279. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19280. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19281. /* tag, tag length */
  19282. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19283. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  19284. /* valid */
  19285. 0,
  19286. },
  19287. {
  19288. /* key, key length */
  19289. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19290. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19291. /* iv, iv length */
  19292. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19293. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19294. /* aad, aad length */
  19295. {0x00}, 0,
  19296. /* msg, msg length */
  19297. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19298. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19299. /* ct, ct length */
  19300. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19301. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19302. /* tag, tag length */
  19303. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  19304. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  19305. /* valid */
  19306. 0,
  19307. },
  19308. {
  19309. /* key, key length */
  19310. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19311. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19312. /* iv, iv length */
  19313. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19314. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19315. /* aad, aad length */
  19316. {0x00}, 0,
  19317. /* msg, msg length */
  19318. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19319. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19320. /* ct, ct length */
  19321. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19322. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19323. /* tag, tag length */
  19324. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  19325. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  19326. /* valid */
  19327. 0,
  19328. },
  19329. };
  19330. byte ciphertext[sizeof(vectors[0].ct)];
  19331. byte authtag[sizeof(vectors[0].tag)];
  19332. int i;
  19333. int len;
  19334. int ret;
  19335. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  19336. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19337. len = sizeof(authtag);
  19338. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  19339. ciphertext,
  19340. vectors[i].msg, vectors[i].msg_length,
  19341. vectors[i].iv, vectors[i].iv_length,
  19342. authtag, len,
  19343. vectors[i].aad, vectors[i].aad_length),
  19344. 0);
  19345. /* check ciphertext matches vector */
  19346. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  19347. 0);
  19348. /* check that computed tag matches vector only for vectors marked as valid */
  19349. ret = XMEMCMP(authtag, vectors[i].tag, len);
  19350. if (vectors[i].valid) {
  19351. ExpectIntEQ(ret, 0);
  19352. }
  19353. else {
  19354. ExpectIntNE(ret, 0);
  19355. }
  19356. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19357. /* Decrypt, checking that the computed auth tags match */
  19358. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  19359. ciphertext,
  19360. vectors[i].ct, vectors[i].ct_length,
  19361. vectors[i].iv, vectors[i].iv_length,
  19362. authtag, len,
  19363. vectors[i].aad, vectors[i].aad_length),
  19364. 0);
  19365. /* check decrypted ciphertext matches vector plaintext */
  19366. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  19367. 0);
  19368. }
  19369. return EXPECT_RESULT();
  19370. } /* END test_wc_AesEaxVectors */
  19371. /*
  19372. * Testing test_wc_AesEaxEncryptAuth()
  19373. */
  19374. static int test_wc_AesEaxEncryptAuth(void)
  19375. {
  19376. EXPECT_DECLS;
  19377. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19378. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19379. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19380. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19381. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19382. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19383. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19384. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19385. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19386. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19387. byte ciphertext[sizeof(msg)];
  19388. byte authtag[AES_BLOCK_SIZE];
  19389. int i;
  19390. int len;
  19391. len = sizeof(authtag);
  19392. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19393. ciphertext,
  19394. msg, sizeof(msg),
  19395. iv, sizeof(iv),
  19396. authtag, (word32)len,
  19397. aad, sizeof(aad)),
  19398. 0);
  19399. /* Test null checking */
  19400. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  19401. ciphertext,
  19402. msg, sizeof(msg),
  19403. iv, sizeof(iv),
  19404. authtag, (word32)len,
  19405. aad, sizeof(aad)),
  19406. BAD_FUNC_ARG);
  19407. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19408. NULL,
  19409. msg, sizeof(msg),
  19410. iv, sizeof(iv),
  19411. authtag, (word32)len,
  19412. aad, sizeof(aad)),
  19413. BAD_FUNC_ARG);
  19414. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19415. ciphertext,
  19416. NULL, sizeof(msg),
  19417. iv, sizeof(iv),
  19418. authtag, (word32)len,
  19419. aad, sizeof(aad)),
  19420. BAD_FUNC_ARG);
  19421. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19422. ciphertext,
  19423. msg, sizeof(msg),
  19424. NULL, sizeof(iv),
  19425. authtag, (word32)len,
  19426. aad, sizeof(aad)),
  19427. BAD_FUNC_ARG);
  19428. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19429. ciphertext,
  19430. msg, sizeof(msg),
  19431. iv, sizeof(iv),
  19432. NULL, (word32)len,
  19433. aad, sizeof(aad)),
  19434. BAD_FUNC_ARG);
  19435. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19436. ciphertext,
  19437. msg, sizeof(msg),
  19438. iv, sizeof(iv),
  19439. authtag, (word32)len,
  19440. NULL, sizeof(aad)),
  19441. BAD_FUNC_ARG);
  19442. /* Test bad key lengths */
  19443. for (i = 0; i <= 32; i++) {
  19444. int exp_ret;
  19445. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19446. || i == AES_256_KEY_SIZE) {
  19447. exp_ret = 0;
  19448. }
  19449. else {
  19450. exp_ret = BAD_FUNC_ARG;
  19451. }
  19452. ExpectIntEQ(wc_AesEaxEncryptAuth(key, (word32)i,
  19453. ciphertext,
  19454. msg, sizeof(msg),
  19455. iv, sizeof(iv),
  19456. authtag, (word32)len,
  19457. aad, sizeof(aad)),
  19458. exp_ret);
  19459. }
  19460. /* Test auth tag size out of range */
  19461. len = AES_BLOCK_SIZE + 1;
  19462. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19463. ciphertext,
  19464. msg, sizeof(msg),
  19465. iv, sizeof(iv),
  19466. authtag, (word32)len,
  19467. aad, sizeof(aad)),
  19468. BAD_FUNC_ARG);
  19469. return EXPECT_RESULT();
  19470. } /* END test_wc_AesEaxEncryptAuth() */
  19471. /*
  19472. * Testing test_wc_AesEaxDecryptAuth()
  19473. */
  19474. static int test_wc_AesEaxDecryptAuth(void)
  19475. {
  19476. EXPECT_DECLS;
  19477. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19478. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19479. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19480. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19481. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19482. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19483. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19484. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19485. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19486. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19487. /* Garbage tag that should always fail for above aad */
  19488. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  19489. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  19490. byte plaintext[sizeof(ct)];
  19491. int i;
  19492. int len;
  19493. len = sizeof(tag);
  19494. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19495. plaintext,
  19496. ct, sizeof(ct),
  19497. iv, sizeof(iv),
  19498. tag, (word32)len,
  19499. aad, sizeof(aad)),
  19500. AES_EAX_AUTH_E);
  19501. /* Test null checking */
  19502. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  19503. plaintext,
  19504. ct, sizeof(ct),
  19505. iv, sizeof(iv),
  19506. tag, (word32)len,
  19507. aad, sizeof(aad)),
  19508. BAD_FUNC_ARG);
  19509. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19510. NULL,
  19511. ct, sizeof(ct),
  19512. iv, sizeof(iv),
  19513. tag, (word32)len,
  19514. aad, sizeof(aad)),
  19515. BAD_FUNC_ARG);
  19516. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19517. plaintext,
  19518. NULL, sizeof(ct),
  19519. iv, sizeof(iv),
  19520. tag, (word32)len,
  19521. aad, sizeof(aad)),
  19522. BAD_FUNC_ARG);
  19523. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19524. plaintext,
  19525. ct, sizeof(ct),
  19526. NULL, sizeof(iv),
  19527. tag, (word32)len,
  19528. aad, sizeof(aad)),
  19529. BAD_FUNC_ARG);
  19530. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19531. plaintext,
  19532. ct, sizeof(ct),
  19533. iv, sizeof(iv),
  19534. NULL, (word32)len,
  19535. aad, sizeof(aad)),
  19536. BAD_FUNC_ARG);
  19537. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19538. plaintext,
  19539. ct, sizeof(ct),
  19540. iv, sizeof(iv),
  19541. tag, (word32)len,
  19542. NULL, sizeof(aad)),
  19543. BAD_FUNC_ARG);
  19544. /* Test bad key lengths */
  19545. for (i = 0; i <= 32; i++) {
  19546. int exp_ret;
  19547. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19548. || i == AES_256_KEY_SIZE) {
  19549. exp_ret = AES_EAX_AUTH_E;
  19550. }
  19551. else {
  19552. exp_ret = BAD_FUNC_ARG;
  19553. }
  19554. ExpectIntEQ(wc_AesEaxDecryptAuth(key, (word32)i,
  19555. plaintext,
  19556. ct, sizeof(ct),
  19557. iv, sizeof(iv),
  19558. tag, (word32)len,
  19559. aad, sizeof(aad)),
  19560. exp_ret);
  19561. }
  19562. /* Test auth tag size out of range */
  19563. len = AES_BLOCK_SIZE + 1;
  19564. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19565. plaintext,
  19566. ct, sizeof(ct),
  19567. iv, sizeof(iv),
  19568. tag, (word32)len,
  19569. aad, sizeof(aad)),
  19570. BAD_FUNC_ARG);
  19571. return EXPECT_RESULT();
  19572. } /* END test_wc_AesEaxDecryptAuth() */
  19573. #endif /* WOLFSSL_AES_EAX &&
  19574. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19575. */
  19576. /*
  19577. * Testing wc_InitDsaKey()
  19578. */
  19579. static int test_wc_InitDsaKey(void)
  19580. {
  19581. EXPECT_DECLS;
  19582. #ifndef NO_DSA
  19583. DsaKey key;
  19584. XMEMSET(&key, 0, sizeof(DsaKey));
  19585. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19586. /* Pass in bad args. */
  19587. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19588. wc_FreeDsaKey(&key);
  19589. #endif
  19590. return EXPECT_RESULT();
  19591. } /* END test_wc_InitDsaKey */
  19592. /*
  19593. * Testing wc_DsaSign() and wc_DsaVerify()
  19594. */
  19595. static int test_wc_DsaSignVerify(void)
  19596. {
  19597. EXPECT_DECLS;
  19598. #if !defined(NO_DSA)
  19599. DsaKey key;
  19600. WC_RNG rng;
  19601. wc_Sha sha;
  19602. byte signature[DSA_SIG_SIZE];
  19603. byte hash[WC_SHA_DIGEST_SIZE];
  19604. word32 idx = 0;
  19605. word32 bytes;
  19606. int answer;
  19607. #ifdef USE_CERT_BUFFERS_1024
  19608. byte tmp[ONEK_BUF];
  19609. XMEMSET(tmp, 0, sizeof(tmp));
  19610. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19611. bytes = sizeof_dsa_key_der_1024;
  19612. #elif defined(USE_CERT_BUFFERS_2048)
  19613. byte tmp[TWOK_BUF];
  19614. XMEMSET(tmp, 0, sizeof(tmp));
  19615. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19616. bytes = sizeof_dsa_key_der_2048;
  19617. #else
  19618. byte tmp[TWOK_BUF];
  19619. XFILE fp = XBADFILE;
  19620. XMEMSET(tmp, 0, sizeof(tmp));
  19621. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19622. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19623. if (fp != XBADFILE)
  19624. XFCLOSE(fp);
  19625. #endif /* END USE_CERT_BUFFERS_1024 */
  19626. ExpectIntEQ(wc_InitSha(&sha), 0);
  19627. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19628. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19629. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19630. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19631. ExpectIntEQ(wc_InitRng(&rng), 0);
  19632. /* Sign. */
  19633. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19634. /* Test bad args. */
  19635. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19636. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19637. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19638. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19639. /* Verify. */
  19640. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19641. ExpectIntEQ(answer, 1);
  19642. /* Pass in bad args. */
  19643. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19644. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19645. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19646. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19647. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19648. /* hard set q to 0 and test fail case */
  19649. mp_free(&key.q);
  19650. mp_init(&key.q);
  19651. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19652. mp_set(&key.q, 1);
  19653. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19654. #endif
  19655. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19656. wc_FreeDsaKey(&key);
  19657. wc_ShaFree(&sha);
  19658. #endif
  19659. return EXPECT_RESULT();
  19660. } /* END test_wc_DsaSign */
  19661. /*
  19662. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19663. */
  19664. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19665. {
  19666. EXPECT_DECLS;
  19667. #if !defined(NO_DSA)
  19668. DsaKey key;
  19669. word32 bytes = 0;
  19670. word32 idx = 0;
  19671. int ret = 0;
  19672. #ifdef USE_CERT_BUFFERS_1024
  19673. byte tmp[ONEK_BUF];
  19674. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19675. bytes = sizeof_dsa_key_der_1024;
  19676. #elif defined(USE_CERT_BUFFERS_2048)
  19677. byte tmp[TWOK_BUF];
  19678. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19679. bytes = sizeof_dsa_key_der_2048;
  19680. #else
  19681. byte tmp[TWOK_BUF];
  19682. XFILE fp = XBADFILE;
  19683. XMEMSET(tmp, 0, sizeof(tmp));
  19684. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19685. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19686. if (fp != XBADFILE)
  19687. XFCLOSE(fp);
  19688. #endif /* END USE_CERT_BUFFERS_1024 */
  19689. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19690. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19691. /* Test bad args. */
  19692. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19693. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19694. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19695. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19696. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19697. wc_FreeDsaKey(&key);
  19698. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19699. idx = 0; /* Reset */
  19700. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19701. /* Test bad args. */
  19702. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19703. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19704. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19705. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19706. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19707. wc_FreeDsaKey(&key);
  19708. #endif /* !NO_DSA */
  19709. return EXPECT_RESULT();
  19710. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19711. /*
  19712. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19713. */
  19714. static int test_wc_MakeDsaKey(void)
  19715. {
  19716. EXPECT_DECLS;
  19717. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19718. DsaKey genKey;
  19719. WC_RNG rng;
  19720. XMEMSET(&genKey, 0, sizeof(genKey));
  19721. XMEMSET(&rng, 0, sizeof(rng));
  19722. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19723. ExpectIntEQ(wc_InitRng(&rng), 0);
  19724. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19725. /* Test bad args. */
  19726. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19727. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19728. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19729. BAD_FUNC_ARG);
  19730. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19731. /* Test bad args. */
  19732. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19733. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19734. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19735. wc_FreeDsaKey(&genKey);
  19736. #endif
  19737. return EXPECT_RESULT();
  19738. } /* END test_wc_MakeDsaKey */
  19739. /*
  19740. * Testing wc_DsaKeyToDer()
  19741. */
  19742. static int test_wc_DsaKeyToDer(void)
  19743. {
  19744. EXPECT_DECLS;
  19745. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19746. DsaKey key;
  19747. word32 bytes;
  19748. word32 idx = 0;
  19749. #ifdef USE_CERT_BUFFERS_1024
  19750. byte tmp[ONEK_BUF];
  19751. byte der[ONEK_BUF];
  19752. XMEMSET(tmp, 0, sizeof(tmp));
  19753. XMEMSET(der, 0, sizeof(der));
  19754. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19755. bytes = sizeof_dsa_key_der_1024;
  19756. #elif defined(USE_CERT_BUFFERS_2048)
  19757. byte tmp[TWOK_BUF];
  19758. byte der[TWOK_BUF];
  19759. XMEMSET(tmp, 0, sizeof(tmp));
  19760. XMEMSET(der, 0, sizeof(der));
  19761. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19762. bytes = sizeof_dsa_key_der_2048;
  19763. #else
  19764. byte tmp[TWOK_BUF];
  19765. byte der[TWOK_BUF];
  19766. XFILE fp = XBADFILE;
  19767. XMEMSET(tmp, 0, sizeof(tmp));
  19768. XMEMSET(der, 0, sizeof(der));
  19769. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19770. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19771. if (fp != XBADFILE)
  19772. XFCLOSE(fp);
  19773. #endif /* END USE_CERT_BUFFERS_1024 */
  19774. XMEMSET(&key, 0, sizeof(DsaKey));
  19775. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19776. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19777. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19778. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19779. /* Test bad args. */
  19780. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19781. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19782. wc_FreeDsaKey(&key);
  19783. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19784. return EXPECT_RESULT();
  19785. } /* END test_wc_DsaKeyToDer */
  19786. /*
  19787. * Testing wc_DsaKeyToPublicDer()
  19788. * (indirectly testing setDsaPublicKey())
  19789. */
  19790. static int test_wc_DsaKeyToPublicDer(void)
  19791. {
  19792. EXPECT_DECLS;
  19793. #ifndef HAVE_SELFTEST
  19794. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19795. DsaKey key;
  19796. WC_RNG rng;
  19797. byte* der = NULL;
  19798. word32 sz = 0;
  19799. word32 idx = 0;
  19800. XMEMSET(&key, 0, sizeof(DsaKey));
  19801. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19802. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19803. DYNAMIC_TYPE_TMP_BUFFER));
  19804. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19805. ExpectIntEQ(wc_InitRng(&rng), 0);
  19806. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19807. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19808. ExpectIntGE(sz = (word32)wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19809. wc_FreeDsaKey(&key);
  19810. idx = 0;
  19811. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19812. /* Test without the SubjectPublicKeyInfo header */
  19813. ExpectIntGE(sz = (word32)wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19814. wc_FreeDsaKey(&key);
  19815. idx = 0;
  19816. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19817. /* Test bad args. */
  19818. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19819. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19820. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19821. wc_FreeDsaKey(&key);
  19822. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19823. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19824. #endif /* !HAVE_SELFTEST */
  19825. return EXPECT_RESULT();
  19826. } /* END test_wc_DsaKeyToPublicDer */
  19827. /*
  19828. * Testing wc_DsaImportParamsRaw()
  19829. */
  19830. static int test_wc_DsaImportParamsRaw(void)
  19831. {
  19832. EXPECT_DECLS;
  19833. #if !defined(NO_DSA)
  19834. DsaKey key;
  19835. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19836. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19837. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19838. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19839. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19840. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19841. "80a0093113a8bd73";
  19842. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19843. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19844. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19845. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19846. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19847. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19848. "76341a7e7d9";
  19849. /* invalid p and q parameters */
  19850. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19851. const char* invalidQ = "96c5390a";
  19852. XMEMSET(&key, 0, sizeof(DsaKey));
  19853. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19854. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19855. /* test bad args */
  19856. /* null key struct */
  19857. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19858. /* null param pointers */
  19859. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19860. /* illegal p length */
  19861. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19862. /* illegal q length */
  19863. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19864. wc_FreeDsaKey(&key);
  19865. #endif
  19866. return EXPECT_RESULT();
  19867. } /* END test_wc_DsaImportParamsRaw */
  19868. /*
  19869. * Testing wc_DsaImportParamsRawCheck()
  19870. */
  19871. static int test_wc_DsaImportParamsRawCheck(void)
  19872. {
  19873. EXPECT_DECLS;
  19874. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19875. DsaKey key;
  19876. int trusted = 0;
  19877. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19878. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19879. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19880. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19881. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19882. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19883. "80a0093113a8bd73";
  19884. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19885. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19886. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19887. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19888. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19889. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19890. "76341a7e7d9";
  19891. /* invalid p and q parameters */
  19892. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19893. const char* invalidQ = "96c5390a";
  19894. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19895. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19896. /* test bad args */
  19897. /* null key struct */
  19898. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19899. BAD_FUNC_ARG);
  19900. /* null param pointers */
  19901. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19902. NULL), BAD_FUNC_ARG);
  19903. /* illegal p length */
  19904. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19905. BAD_FUNC_ARG);
  19906. /* illegal q length */
  19907. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19908. BAD_FUNC_ARG);
  19909. wc_FreeDsaKey(&key);
  19910. #endif
  19911. return EXPECT_RESULT();
  19912. } /* END test_wc_DsaImportParamsRawCheck */
  19913. /*
  19914. * Testing wc_DsaExportParamsRaw()
  19915. */
  19916. static int test_wc_DsaExportParamsRaw(void)
  19917. {
  19918. EXPECT_DECLS;
  19919. #if !defined(NO_DSA)
  19920. DsaKey key;
  19921. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19922. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19923. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19924. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19925. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19926. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19927. "80a0093113a8bd73";
  19928. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19929. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19930. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19931. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19932. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19933. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19934. "76341a7e7d9";
  19935. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19936. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19937. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19938. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19939. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19940. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19941. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19942. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19943. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19944. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19945. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19946. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19947. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19948. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19949. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19950. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19951. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19952. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19953. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19954. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19955. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19956. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19957. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19958. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19959. byte pOut[MAX_DSA_PARAM_SIZE];
  19960. byte qOut[MAX_DSA_PARAM_SIZE];
  19961. byte gOut[MAX_DSA_PARAM_SIZE];
  19962. word32 pOutSz;
  19963. word32 qOutSz;
  19964. word32 gOutSz;
  19965. XMEMSET(&key, 0, sizeof(DsaKey));
  19966. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19967. /* first test using imported raw parameters, for expected */
  19968. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19969. pOutSz = sizeof(pOut);
  19970. qOutSz = sizeof(qOut);
  19971. gOutSz = sizeof(gOut);
  19972. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19973. &gOutSz), 0);
  19974. /* validate exported parameters are correct */
  19975. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19976. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19977. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19978. /* test bad args */
  19979. /* null key struct */
  19980. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19981. &gOutSz), BAD_FUNC_ARG);
  19982. /* null output pointers */
  19983. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19984. &gOutSz), LENGTH_ONLY_E);
  19985. /* null output size pointers */
  19986. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19987. NULL), BAD_FUNC_ARG);
  19988. /* p output buffer size too small */
  19989. pOutSz = 1;
  19990. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19991. &gOutSz), BUFFER_E);
  19992. pOutSz = sizeof(pOut);
  19993. /* q output buffer size too small */
  19994. qOutSz = 1;
  19995. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19996. &gOutSz), BUFFER_E);
  19997. qOutSz = sizeof(qOut);
  19998. /* g output buffer size too small */
  19999. gOutSz = 1;
  20000. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  20001. &gOutSz), BUFFER_E);
  20002. wc_FreeDsaKey(&key);
  20003. #endif
  20004. return EXPECT_RESULT();
  20005. } /* END test_wc_DsaExportParamsRaw */
  20006. /*
  20007. * Testing wc_DsaExportKeyRaw()
  20008. */
  20009. static int test_wc_DsaExportKeyRaw(void)
  20010. {
  20011. EXPECT_DECLS;
  20012. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  20013. DsaKey key;
  20014. WC_RNG rng;
  20015. byte xOut[MAX_DSA_PARAM_SIZE];
  20016. byte yOut[MAX_DSA_PARAM_SIZE];
  20017. word32 xOutSz, yOutSz;
  20018. XMEMSET(&key, 0, sizeof(key));
  20019. XMEMSET(&rng, 0, sizeof(rng));
  20020. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  20021. ExpectIntEQ(wc_InitRng(&rng), 0);
  20022. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  20023. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  20024. /* try successful export */
  20025. xOutSz = sizeof(xOut);
  20026. yOutSz = sizeof(yOut);
  20027. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  20028. /* test bad args */
  20029. /* null key struct */
  20030. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  20031. BAD_FUNC_ARG);
  20032. /* null output pointers */
  20033. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  20034. LENGTH_ONLY_E);
  20035. /* null output size pointers */
  20036. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  20037. BAD_FUNC_ARG);
  20038. /* x output buffer size too small */
  20039. xOutSz = 1;
  20040. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  20041. BUFFER_E);
  20042. xOutSz = sizeof(xOut);
  20043. /* y output buffer size too small */
  20044. yOutSz = 1;
  20045. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  20046. BUFFER_E);
  20047. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20048. wc_FreeDsaKey(&key);
  20049. #endif
  20050. return EXPECT_RESULT();
  20051. } /* END test_wc_DsaExportParamsRaw */
  20052. /*
  20053. * Testing wc_ed25519_make_key().
  20054. */
  20055. static int test_wc_ed25519_make_key(void)
  20056. {
  20057. EXPECT_DECLS;
  20058. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  20059. ed25519_key key;
  20060. WC_RNG rng;
  20061. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  20062. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  20063. XMEMSET(&key, 0, sizeof(ed25519_key));
  20064. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20065. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20066. ExpectIntEQ(wc_InitRng(&rng), 0);
  20067. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, (word32)pubkey_sz),
  20068. ECC_PRIV_KEY_E);
  20069. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, (word32)pubkey_sz),
  20070. ECC_PRIV_KEY_E);
  20071. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20072. /* Test bad args. */
  20073. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  20074. BAD_FUNC_ARG);
  20075. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  20076. BAD_FUNC_ARG);
  20077. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  20078. BAD_FUNC_ARG);
  20079. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  20080. BAD_FUNC_ARG);
  20081. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20082. wc_ed25519_free(&key);
  20083. #endif
  20084. return EXPECT_RESULT();
  20085. } /* END test_wc_ed25519_make_key */
  20086. /*
  20087. * Testing wc_ed25519_init()
  20088. */
  20089. static int test_wc_ed25519_init(void)
  20090. {
  20091. EXPECT_DECLS;
  20092. #if defined(HAVE_ED25519)
  20093. ed25519_key key;
  20094. XMEMSET(&key, 0, sizeof(ed25519_key));
  20095. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20096. /* Test bad args. */
  20097. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  20098. wc_ed25519_free(&key);
  20099. #endif
  20100. return EXPECT_RESULT();
  20101. } /* END test_wc_ed25519_init */
  20102. /*
  20103. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  20104. */
  20105. static int test_wc_ed25519_sign_msg(void)
  20106. {
  20107. EXPECT_DECLS;
  20108. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  20109. WC_RNG rng;
  20110. ed25519_key key;
  20111. byte msg[] = "Everybody gets Friday off.\n";
  20112. byte sig[ED25519_SIG_SIZE+1];
  20113. word32 msglen = sizeof(msg);
  20114. word32 siglen = ED25519_SIG_SIZE;
  20115. word32 badSigLen = ED25519_SIG_SIZE - 1;
  20116. #ifdef HAVE_ED25519_VERIFY
  20117. int verify_ok = 0; /*1 = Verify success.*/
  20118. #endif
  20119. /* Initialize stack variables. */
  20120. XMEMSET(&key, 0, sizeof(ed25519_key));
  20121. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20122. XMEMSET(sig, 0, sizeof(sig));
  20123. /* Initialize key. */
  20124. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20125. ExpectIntEQ(wc_InitRng(&rng), 0);
  20126. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20127. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  20128. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  20129. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  20130. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  20131. /* Test bad args. */
  20132. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  20133. BAD_FUNC_ARG);
  20134. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  20135. BAD_FUNC_ARG);
  20136. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  20137. BAD_FUNC_ARG);
  20138. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  20139. BAD_FUNC_ARG);
  20140. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  20141. BUFFER_E);
  20142. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  20143. badSigLen -= 1;
  20144. #ifdef HAVE_ED25519_VERIFY
  20145. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20146. &key), 0);
  20147. ExpectIntEQ(verify_ok, 1);
  20148. /* Test bad args. */
  20149. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  20150. &verify_ok, &key), BAD_FUNC_ARG);
  20151. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  20152. &verify_ok, &key), BAD_FUNC_ARG);
  20153. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20154. &key), BAD_FUNC_ARG);
  20155. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  20156. &key), BAD_FUNC_ARG);
  20157. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  20158. BAD_FUNC_ARG);
  20159. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20160. NULL), BAD_FUNC_ARG);
  20161. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  20162. &key), BAD_FUNC_ARG);
  20163. #endif /* Verify. */
  20164. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20165. wc_ed25519_free(&key);
  20166. #endif
  20167. return EXPECT_RESULT();
  20168. } /* END test_wc_ed25519_sign_msg */
  20169. /*
  20170. * Testing wc_ed25519_import_public()
  20171. */
  20172. static int test_wc_ed25519_import_public(void)
  20173. {
  20174. EXPECT_DECLS;
  20175. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20176. ed25519_key pubKey;
  20177. WC_RNG rng;
  20178. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  20179. word32 inlen = sizeof(in);
  20180. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  20181. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20182. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  20183. ExpectIntEQ(wc_InitRng(&rng), 0);
  20184. #ifdef HAVE_ED25519_MAKE_KEY
  20185. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  20186. #endif
  20187. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  20188. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20189. /* Test bad args. */
  20190. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20191. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20192. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20193. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20194. wc_ed25519_free(&pubKey);
  20195. #endif
  20196. return EXPECT_RESULT();
  20197. } /* END wc_ed25519_import_public */
  20198. /*
  20199. * Testing wc_ed25519_import_private_key()
  20200. */
  20201. static int test_wc_ed25519_import_private_key(void)
  20202. {
  20203. EXPECT_DECLS;
  20204. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20205. ed25519_key key;
  20206. WC_RNG rng;
  20207. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  20208. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  20209. word32 privKeySz = sizeof(privKey);
  20210. word32 pubKeySz = sizeof(pubKey);
  20211. #ifdef HAVE_ED25519_KEY_EXPORT
  20212. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20213. word32 bothKeysSz = sizeof(bothKeys);
  20214. #endif
  20215. XMEMSET(&key, 0, sizeof(ed25519_key));
  20216. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20217. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20218. ExpectIntEQ(wc_InitRng(&rng), 0);
  20219. #ifdef HAVE_ED25519_MAKE_KEY
  20220. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20221. #endif
  20222. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  20223. pubKeySz, &key, 1), 0);
  20224. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20225. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20226. #ifdef HAVE_ED25519_KEY_EXPORT
  20227. PRIVATE_KEY_UNLOCK();
  20228. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  20229. PRIVATE_KEY_LOCK();
  20230. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20231. &key, 1), 0);
  20232. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20233. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20234. #endif
  20235. /* Test bad args. */
  20236. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20237. &key), BAD_FUNC_ARG);
  20238. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  20239. pubKeySz, &key), BAD_FUNC_ARG);
  20240. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20241. pubKeySz, NULL), BAD_FUNC_ARG);
  20242. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  20243. pubKeySz, &key), BAD_FUNC_ARG);
  20244. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20245. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20246. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  20247. &key), BAD_FUNC_ARG);
  20248. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20249. wc_ed25519_free(&key);
  20250. #endif
  20251. return EXPECT_RESULT();
  20252. } /* END test_wc_ed25519_import_private_key */
  20253. /*
  20254. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20255. */
  20256. static int test_wc_ed25519_export(void)
  20257. {
  20258. EXPECT_DECLS;
  20259. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20260. ed25519_key key;
  20261. WC_RNG rng;
  20262. byte priv[ED25519_PRV_KEY_SIZE];
  20263. byte pub[ED25519_PUB_KEY_SIZE];
  20264. word32 privSz = sizeof(priv);
  20265. word32 pubSz = sizeof(pub);
  20266. #ifndef HAVE_ED25519_MAKE_KEY
  20267. const byte privKey[] = {
  20268. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20269. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20270. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20271. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20272. };
  20273. const byte pubKey[] = {
  20274. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20275. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20276. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20277. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20278. };
  20279. #endif
  20280. XMEMSET(&key, 0, sizeof(ed25519_key));
  20281. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20282. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20283. ExpectIntEQ(wc_InitRng(&rng), 0);
  20284. #ifdef HAVE_ED25519_MAKE_KEY
  20285. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20286. #else
  20287. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20288. pubKey, sizeof(pubKey), &key, 1), 0);
  20289. #endif
  20290. PRIVATE_KEY_UNLOCK();
  20291. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  20292. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  20293. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20294. /* Test bad args. */
  20295. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20296. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20297. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20298. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  20299. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  20300. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20301. /* Test bad args. */
  20302. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  20303. BAD_FUNC_ARG);
  20304. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  20305. BAD_FUNC_ARG);
  20306. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  20307. BAD_FUNC_ARG);
  20308. PRIVATE_KEY_LOCK();
  20309. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20310. wc_ed25519_free(&key);
  20311. #endif
  20312. return EXPECT_RESULT();
  20313. } /* END test_wc_ed25519_export */
  20314. /*
  20315. * Testing wc_ed25519_size()
  20316. */
  20317. static int test_wc_ed25519_size(void)
  20318. {
  20319. EXPECT_DECLS;
  20320. #if defined(HAVE_ED25519)
  20321. ed25519_key key;
  20322. WC_RNG rng;
  20323. #ifndef HAVE_ED25519_MAKE_KEY
  20324. const byte privKey[] = {
  20325. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20326. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20327. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20328. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20329. };
  20330. const byte pubKey[] = {
  20331. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20332. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20333. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20334. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20335. };
  20336. #endif
  20337. XMEMSET(&key, 0, sizeof(ed25519_key));
  20338. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20339. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20340. ExpectIntEQ(wc_InitRng(&rng), 0);
  20341. #ifdef HAVE_ED25519_MAKE_KEY
  20342. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20343. #else
  20344. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20345. pubKey, sizeof(pubKey), &key, 1), 0);
  20346. #endif
  20347. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  20348. /* Test bad args. */
  20349. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  20350. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  20351. /* Test bad args. */
  20352. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  20353. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  20354. /* Test bad args. */
  20355. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  20356. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  20357. /* Test bad args. */
  20358. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  20359. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20360. wc_ed25519_free(&key);
  20361. #endif
  20362. return EXPECT_RESULT();
  20363. } /* END test_wc_ed25519_size */
  20364. /*
  20365. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20366. */
  20367. static int test_wc_ed25519_exportKey(void)
  20368. {
  20369. EXPECT_DECLS;
  20370. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20371. WC_RNG rng;
  20372. ed25519_key key;
  20373. byte priv[ED25519_PRV_KEY_SIZE];
  20374. byte pub[ED25519_PUB_KEY_SIZE];
  20375. byte privOnly[ED25519_PRV_KEY_SIZE];
  20376. word32 privSz = sizeof(priv);
  20377. word32 pubSz = sizeof(pub);
  20378. word32 privOnlySz = sizeof(privOnly);
  20379. #ifndef HAVE_ED25519_MAKE_KEY
  20380. const byte privKey[] = {
  20381. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20382. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20383. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20384. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20385. };
  20386. const byte pubKey[] = {
  20387. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20388. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20389. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20390. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20391. };
  20392. #endif
  20393. XMEMSET(&key, 0, sizeof(ed25519_key));
  20394. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20395. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20396. ExpectIntEQ(wc_InitRng(&rng), 0);
  20397. #ifdef HAVE_ED25519_MAKE_KEY
  20398. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20399. #else
  20400. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20401. pubKey, sizeof(pubKey), &key, 1), 0);
  20402. #endif
  20403. PRIVATE_KEY_UNLOCK();
  20404. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  20405. /* Test bad args. */
  20406. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  20407. BAD_FUNC_ARG);
  20408. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  20409. BAD_FUNC_ARG);
  20410. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20411. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20412. /* Test bad args. */
  20413. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  20414. BAD_FUNC_ARG);
  20415. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  20416. BAD_FUNC_ARG);
  20417. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  20418. BAD_FUNC_ARG);
  20419. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  20420. BAD_FUNC_ARG);
  20421. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  20422. BAD_FUNC_ARG);
  20423. PRIVATE_KEY_LOCK();
  20424. /* Cross check output. */
  20425. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20426. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20427. wc_ed25519_free(&key);
  20428. #endif
  20429. return EXPECT_RESULT();
  20430. } /* END test_wc_ed25519_exportKey */
  20431. /*
  20432. * Testing wc_Ed25519PublicKeyToDer
  20433. */
  20434. static int test_wc_Ed25519PublicKeyToDer(void)
  20435. {
  20436. EXPECT_DECLS;
  20437. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20438. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20439. ed25519_key key;
  20440. byte derBuf[1024];
  20441. XMEMSET(&key, 0, sizeof(ed25519_key));
  20442. /* Test bad args */
  20443. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20444. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20445. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20446. wc_ed25519_free(&key);
  20447. /* Test good args */
  20448. if (EXPECT_SUCCESS()) {
  20449. WC_RNG rng;
  20450. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20451. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20452. ExpectIntEQ(wc_InitRng(&rng), 0);
  20453. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20454. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20455. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20456. wc_ed25519_free(&key);
  20457. }
  20458. #endif
  20459. return EXPECT_RESULT();
  20460. } /* END testing wc_Ed25519PublicKeyToDer */
  20461. /*
  20462. * Testing wc_curve25519_init and wc_curve25519_free.
  20463. */
  20464. static int test_wc_curve25519_init(void)
  20465. {
  20466. EXPECT_DECLS;
  20467. #if defined(HAVE_CURVE25519)
  20468. curve25519_key key;
  20469. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20470. /* Test bad args for wc_curve25519_init */
  20471. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  20472. /* Test good args for wc_curve_25519_free */
  20473. wc_curve25519_free(&key);
  20474. /* Test bad args for wc_curve25519 free. */
  20475. wc_curve25519_free(NULL);
  20476. #endif
  20477. return EXPECT_RESULT();
  20478. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20479. /*
  20480. * Testing test_wc_curve25519_size.
  20481. */
  20482. static int test_wc_curve25519_size(void)
  20483. {
  20484. EXPECT_DECLS;
  20485. #if defined(HAVE_CURVE25519)
  20486. curve25519_key key;
  20487. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20488. /* Test good args for wc_curve25519_size */
  20489. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20490. /* Test bad args for wc_curve25519_size */
  20491. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  20492. wc_curve25519_free(&key);
  20493. #endif
  20494. return EXPECT_RESULT();
  20495. } /* END test_wc_curve25519_size*/
  20496. /*
  20497. * Testing test_wc_curve25519_export_key_raw().
  20498. */
  20499. static int test_wc_curve25519_export_key_raw(void)
  20500. {
  20501. EXPECT_DECLS;
  20502. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20503. curve25519_key key;
  20504. WC_RNG rng;
  20505. byte privateKey[CURVE25519_KEYSIZE];
  20506. byte publicKey[CURVE25519_KEYSIZE];
  20507. word32 prvkSz;
  20508. word32 pubkSz;
  20509. byte prik[CURVE25519_KEYSIZE];
  20510. byte pubk[CURVE25519_KEYSIZE];
  20511. word32 prksz;
  20512. word32 pbksz;
  20513. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20514. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20515. ExpectIntEQ(wc_InitRng(&rng), 0);
  20516. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20517. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20518. prvkSz = CURVE25519_KEYSIZE;
  20519. pubkSz = CURVE25519_KEYSIZE;
  20520. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  20521. publicKey, &pubkSz), BAD_FUNC_ARG);
  20522. prvkSz = CURVE25519_KEYSIZE;
  20523. pubkSz = CURVE25519_KEYSIZE;
  20524. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  20525. &pubkSz), BAD_FUNC_ARG);
  20526. prvkSz = CURVE25519_KEYSIZE;
  20527. pubkSz = CURVE25519_KEYSIZE;
  20528. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  20529. publicKey, &pubkSz), BAD_FUNC_ARG);
  20530. /* prvkSz = CURVE25519_KEYSIZE; */
  20531. pubkSz = CURVE25519_KEYSIZE;
  20532. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20533. NULL, &pubkSz), BAD_FUNC_ARG);
  20534. prvkSz = CURVE25519_KEYSIZE;
  20535. pubkSz = CURVE25519_KEYSIZE;
  20536. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20537. publicKey, NULL), BAD_FUNC_ARG);
  20538. /* cross-testing */
  20539. prksz = CURVE25519_KEYSIZE;
  20540. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  20541. pbksz = CURVE25519_KEYSIZE;
  20542. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  20543. prvkSz = CURVE25519_KEYSIZE;
  20544. /* pubkSz = CURVE25519_KEYSIZE; */
  20545. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20546. publicKey, &pubkSz), 0);
  20547. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20548. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20549. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20550. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20551. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20552. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20553. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20554. wc_curve25519_free(&key);
  20555. #endif
  20556. return EXPECT_RESULT();
  20557. } /* end of test_wc_curve25519_export_key_raw */
  20558. /*
  20559. * Testing test_wc_curve25519_export_key_raw_ex().
  20560. */
  20561. static int test_wc_curve25519_export_key_raw_ex(void)
  20562. {
  20563. EXPECT_DECLS;
  20564. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20565. curve25519_key key;
  20566. WC_RNG rng;
  20567. byte privateKey[CURVE25519_KEYSIZE];
  20568. byte publicKey[CURVE25519_KEYSIZE];
  20569. word32 prvkSz;
  20570. word32 pubkSz;
  20571. byte prik[CURVE25519_KEYSIZE];
  20572. byte pubk[CURVE25519_KEYSIZE];
  20573. word32 prksz;
  20574. word32 pbksz;
  20575. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20576. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20577. ExpectIntEQ(wc_InitRng(&rng), 0);
  20578. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20579. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20580. prvkSz = CURVE25519_KEYSIZE;
  20581. pubkSz = CURVE25519_KEYSIZE;
  20582. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20583. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20584. prvkSz = CURVE25519_KEYSIZE;
  20585. pubkSz = CURVE25519_KEYSIZE;
  20586. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20587. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20588. prvkSz = CURVE25519_KEYSIZE;
  20589. pubkSz = CURVE25519_KEYSIZE;
  20590. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20591. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20592. /* prvkSz = CURVE25519_KEYSIZE; */
  20593. pubkSz = CURVE25519_KEYSIZE;
  20594. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20595. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20596. prvkSz = CURVE25519_KEYSIZE;
  20597. pubkSz = CURVE25519_KEYSIZE;
  20598. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20599. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20600. prvkSz = CURVE25519_KEYSIZE;
  20601. /* pubkSz = CURVE25519_KEYSIZE; */
  20602. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20603. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20604. prvkSz = CURVE25519_KEYSIZE;
  20605. pubkSz = CURVE25519_KEYSIZE;
  20606. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20607. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20608. prvkSz = CURVE25519_KEYSIZE;
  20609. pubkSz = CURVE25519_KEYSIZE;
  20610. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20611. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20612. /* prvkSz = CURVE25519_KEYSIZE; */
  20613. pubkSz = CURVE25519_KEYSIZE;
  20614. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20615. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20616. prvkSz = CURVE25519_KEYSIZE;
  20617. pubkSz = CURVE25519_KEYSIZE;
  20618. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20619. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20620. /* illegal value for endian */
  20621. prvkSz = CURVE25519_KEYSIZE;
  20622. /* pubkSz = CURVE25519_KEYSIZE; */
  20623. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20624. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20625. /* cross-testing */
  20626. prksz = CURVE25519_KEYSIZE;
  20627. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20628. pbksz = CURVE25519_KEYSIZE;
  20629. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20630. prvkSz = CURVE25519_KEYSIZE;
  20631. /* pubkSz = CURVE25519_KEYSIZE; */
  20632. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20633. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20634. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20635. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20636. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20637. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20638. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20639. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20640. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20641. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20642. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20643. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20644. /* try once with another endian */
  20645. prvkSz = CURVE25519_KEYSIZE;
  20646. pubkSz = CURVE25519_KEYSIZE;
  20647. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20648. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20649. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20650. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20651. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20652. wc_curve25519_free(&key);
  20653. #endif
  20654. return EXPECT_RESULT();
  20655. } /* end of test_wc_curve25519_export_key_raw_ex */
  20656. /*
  20657. * Testing wc_curve25519_make_key
  20658. */
  20659. static int test_wc_curve25519_make_key(void)
  20660. {
  20661. EXPECT_DECLS;
  20662. #if defined(HAVE_CURVE25519)
  20663. curve25519_key key;
  20664. WC_RNG rng;
  20665. int keysize;
  20666. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20667. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20668. ExpectIntEQ(wc_InitRng(&rng), 0);
  20669. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20670. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20671. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20672. /* test bad cases*/
  20673. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20674. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20675. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20676. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20677. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20678. wc_curve25519_free(&key);
  20679. #endif
  20680. return EXPECT_RESULT();
  20681. } /* END test_wc_curve25519_make_key*/
  20682. /*
  20683. * Testing wc_curve25519_shared_secret_ex
  20684. */
  20685. static int test_wc_curve25519_shared_secret_ex(void)
  20686. {
  20687. EXPECT_DECLS;
  20688. #if defined(HAVE_CURVE25519)
  20689. curve25519_key private_key;
  20690. curve25519_key public_key;
  20691. WC_RNG rng;
  20692. byte out[CURVE25519_KEYSIZE];
  20693. word32 outLen = sizeof(out);
  20694. int endian = EC25519_BIG_ENDIAN;
  20695. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20696. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20697. ExpectIntEQ(wc_InitRng(&rng), 0);
  20698. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20699. 0);
  20700. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20701. 0);
  20702. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20703. &outLen, endian), 0);
  20704. /* test bad cases*/
  20705. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20706. BAD_FUNC_ARG);
  20707. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20708. endian), BAD_FUNC_ARG);
  20709. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20710. endian), BAD_FUNC_ARG);
  20711. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20712. &outLen, endian), BAD_FUNC_ARG);
  20713. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20714. NULL, endian), BAD_FUNC_ARG);
  20715. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20716. * increasing to 0x8f checks for error being returned*/
  20717. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20718. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20719. &outLen, endian), ECC_BAD_ARG_E);
  20720. outLen = outLen - 2;
  20721. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20722. &outLen, endian), BAD_FUNC_ARG);
  20723. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20724. wc_curve25519_free(&private_key);
  20725. wc_curve25519_free(&public_key);
  20726. #endif
  20727. return EXPECT_RESULT();
  20728. } /* END test_wc_curve25519_shared_secret_ex*/
  20729. /*
  20730. * Testing wc_curve25519_make_pub
  20731. */
  20732. static int test_wc_curve25519_make_pub(void)
  20733. {
  20734. EXPECT_DECLS;
  20735. #ifdef HAVE_CURVE25519
  20736. curve25519_key key;
  20737. WC_RNG rng;
  20738. byte out[CURVE25519_KEYSIZE];
  20739. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20740. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20741. ExpectIntEQ(wc_InitRng(&rng), 0);
  20742. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20743. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20744. (int)sizeof(key.k), key.k), 0);
  20745. /* test bad cases*/
  20746. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20747. (int)sizeof out, out), ECC_BAD_ARG_E);
  20748. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20749. NULL), ECC_BAD_ARG_E);
  20750. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20751. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20752. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20753. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20754. /* verify clamping test */
  20755. key.k[0] |= ~248;
  20756. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20757. key.k), ECC_BAD_ARG_E);
  20758. key.k[0] &= 248;
  20759. /* repeat the expected-to-succeed test. */
  20760. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20761. key.k), 0);
  20762. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20763. wc_curve25519_free(&key);
  20764. #endif
  20765. return EXPECT_RESULT();
  20766. } /* END test_wc_curve25519_make_pub */
  20767. /*
  20768. * Testing test_wc_curve25519_export_public_ex
  20769. */
  20770. static int test_wc_curve25519_export_public_ex(void)
  20771. {
  20772. EXPECT_DECLS;
  20773. #if defined(HAVE_CURVE25519)
  20774. curve25519_key key;
  20775. WC_RNG rng;
  20776. byte out[CURVE25519_KEYSIZE];
  20777. word32 outLen = sizeof(out);
  20778. int endian = EC25519_BIG_ENDIAN;
  20779. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20780. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20781. ExpectIntEQ(wc_InitRng(&rng), 0);
  20782. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20783. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20784. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20785. /* test bad cases*/
  20786. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20787. BAD_FUNC_ARG);
  20788. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20789. BAD_FUNC_ARG);
  20790. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20791. BAD_FUNC_ARG);
  20792. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20793. BAD_FUNC_ARG);
  20794. outLen = outLen - 2;
  20795. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20796. ECC_BAD_ARG_E);
  20797. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20798. wc_curve25519_free(&key);
  20799. #endif
  20800. return EXPECT_RESULT();
  20801. } /* END test_wc_curve25519_export_public_ex*/
  20802. /*
  20803. * Testing test_wc_curve25519_import_private_raw_ex
  20804. */
  20805. static int test_wc_curve25519_import_private_raw_ex(void)
  20806. {
  20807. EXPECT_DECLS;
  20808. #if defined(HAVE_CURVE25519)
  20809. curve25519_key key;
  20810. WC_RNG rng;
  20811. byte priv[CURVE25519_KEYSIZE];
  20812. byte pub[CURVE25519_KEYSIZE];
  20813. word32 privSz = sizeof(priv);
  20814. word32 pubSz = sizeof(pub);
  20815. int endian = EC25519_BIG_ENDIAN;
  20816. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20817. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20818. ExpectIntEQ(wc_InitRng(&rng), 0);
  20819. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20820. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20821. endian), 0);
  20822. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20823. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20824. &key, endian), 0);
  20825. /* test bad cases*/
  20826. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20827. endian), BAD_FUNC_ARG);
  20828. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20829. &key, endian), BAD_FUNC_ARG);
  20830. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20831. &key, endian), BAD_FUNC_ARG);
  20832. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20833. NULL, endian), BAD_FUNC_ARG);
  20834. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20835. &key, endian), ECC_BAD_ARG_E);
  20836. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20837. &key, endian), ECC_BAD_ARG_E);
  20838. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20839. &key, EC25519_LITTLE_ENDIAN), 0);
  20840. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20841. wc_curve25519_free(&key);
  20842. #endif
  20843. return EXPECT_RESULT();
  20844. } /* END test_wc_curve25519_import_private_raw_ex*/
  20845. /*
  20846. * Testing test_wc_curve25519_import_private
  20847. */
  20848. static int test_wc_curve25519_import_private(void)
  20849. {
  20850. EXPECT_DECLS;
  20851. #if defined(HAVE_CURVE25519)
  20852. curve25519_key key;
  20853. WC_RNG rng;
  20854. byte priv[CURVE25519_KEYSIZE];
  20855. word32 privSz = sizeof(priv);
  20856. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20857. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20858. ExpectIntEQ(wc_InitRng(&rng), 0);
  20859. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20860. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20861. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20862. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20863. wc_curve25519_free(&key);
  20864. #endif
  20865. return EXPECT_RESULT();
  20866. } /* END test_wc_curve25519_import*/
  20867. /*
  20868. * Testing test_wc_curve25519_export_private_raw_ex
  20869. */
  20870. static int test_wc_curve25519_export_private_raw_ex(void)
  20871. {
  20872. EXPECT_DECLS;
  20873. #if defined(HAVE_CURVE25519)
  20874. curve25519_key key;
  20875. byte out[CURVE25519_KEYSIZE];
  20876. word32 outLen = sizeof(out);
  20877. int endian = EC25519_BIG_ENDIAN;
  20878. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20879. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20880. 0);
  20881. /* test bad cases*/
  20882. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20883. BAD_FUNC_ARG);
  20884. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20885. BAD_FUNC_ARG);
  20886. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20887. endian), BAD_FUNC_ARG);
  20888. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20889. BAD_FUNC_ARG);
  20890. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20891. EC25519_LITTLE_ENDIAN), 0);
  20892. outLen = outLen - 2;
  20893. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20894. ECC_BAD_ARG_E);
  20895. wc_curve25519_free(&key);
  20896. #endif
  20897. return EXPECT_RESULT();
  20898. } /* END test_wc_curve25519_export_private_raw_ex*/
  20899. /*
  20900. * Testing wc_ed448_make_key().
  20901. */
  20902. static int test_wc_ed448_make_key(void)
  20903. {
  20904. EXPECT_DECLS;
  20905. #if defined(HAVE_ED448)
  20906. ed448_key key;
  20907. WC_RNG rng;
  20908. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20909. XMEMSET(&key, 0, sizeof(ed448_key));
  20910. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20911. ExpectIntEQ(wc_ed448_init(&key), 0);
  20912. ExpectIntEQ(wc_InitRng(&rng), 0);
  20913. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20914. ECC_PRIV_KEY_E);
  20915. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20916. /* Test bad args. */
  20917. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20918. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20919. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20920. BAD_FUNC_ARG);
  20921. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20922. BAD_FUNC_ARG);
  20923. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20924. wc_ed448_free(&key);
  20925. #endif
  20926. return EXPECT_RESULT();
  20927. } /* END test_wc_ed448_make_key */
  20928. /*
  20929. * Testing wc_ed448_init()
  20930. */
  20931. static int test_wc_ed448_init(void)
  20932. {
  20933. EXPECT_DECLS;
  20934. #if defined(HAVE_ED448)
  20935. ed448_key key;
  20936. XMEMSET(&key, 0, sizeof(ed448_key));
  20937. ExpectIntEQ(wc_ed448_init(&key), 0);
  20938. /* Test bad args. */
  20939. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20940. wc_ed448_free(&key);
  20941. #endif
  20942. return EXPECT_RESULT();
  20943. } /* END test_wc_ed448_init */
  20944. /*
  20945. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20946. */
  20947. static int test_wc_ed448_sign_msg(void)
  20948. {
  20949. EXPECT_DECLS;
  20950. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20951. ed448_key key;
  20952. WC_RNG rng;
  20953. byte msg[] = "Everybody gets Friday off.\n";
  20954. byte sig[ED448_SIG_SIZE];
  20955. word32 msglen = sizeof(msg);
  20956. word32 siglen = sizeof(sig);
  20957. word32 badSigLen = sizeof(sig) - 1;
  20958. #ifdef HAVE_ED448_VERIFY
  20959. int verify_ok = 0; /*1 = Verify success.*/
  20960. #endif
  20961. /* Initialize stack variables. */
  20962. XMEMSET(&key, 0, sizeof(ed448_key));
  20963. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20964. XMEMSET(sig, 0, siglen);
  20965. /* Initialize key. */
  20966. ExpectIntEQ(wc_ed448_init(&key), 0);
  20967. ExpectIntEQ(wc_InitRng(&rng), 0);
  20968. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20969. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20970. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20971. /* Test bad args. */
  20972. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20973. BAD_FUNC_ARG);
  20974. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20975. BAD_FUNC_ARG);
  20976. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20977. BAD_FUNC_ARG);
  20978. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20979. BAD_FUNC_ARG);
  20980. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20981. BUFFER_E);
  20982. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20983. badSigLen -= 1;
  20984. #ifdef HAVE_ED448_VERIFY
  20985. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20986. NULL, 0), 0);
  20987. ExpectIntEQ(verify_ok, 1);
  20988. /* Test bad args. */
  20989. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20990. &key, NULL, 0), BAD_FUNC_ARG);
  20991. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20992. &key, NULL, 0), BAD_FUNC_ARG);
  20993. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20994. &key, NULL, 0), BAD_FUNC_ARG);
  20995. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20996. &key, NULL, 0), BAD_FUNC_ARG);
  20997. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20998. &key, NULL, 0), BAD_FUNC_ARG);
  20999. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  21000. NULL, NULL, 0), BAD_FUNC_ARG);
  21001. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  21002. &key, NULL, 0), BAD_FUNC_ARG);
  21003. #endif /* Verify. */
  21004. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21005. wc_ed448_free(&key);
  21006. #endif
  21007. return EXPECT_RESULT();
  21008. } /* END test_wc_ed448_sign_msg */
  21009. /*
  21010. * Testing wc_ed448_import_public()
  21011. */
  21012. static int test_wc_ed448_import_public(void)
  21013. {
  21014. EXPECT_DECLS;
  21015. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21016. ed448_key pubKey;
  21017. WC_RNG rng;
  21018. const byte in[] =
  21019. "Ed448PublicKeyUnitTest.................................\n";
  21020. word32 inlen = sizeof(in);
  21021. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  21022. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21023. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  21024. ExpectIntEQ(wc_InitRng(&rng), 0);
  21025. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  21026. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  21027. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  21028. /* Test bad args. */
  21029. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  21030. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  21031. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  21032. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21033. wc_ed448_free(&pubKey);
  21034. #endif
  21035. return EXPECT_RESULT();
  21036. } /* END wc_ed448_import_public */
  21037. /*
  21038. * Testing wc_ed448_import_private_key()
  21039. */
  21040. static int test_wc_ed448_import_private_key(void)
  21041. {
  21042. EXPECT_DECLS;
  21043. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21044. ed448_key key;
  21045. WC_RNG rng;
  21046. const byte privKey[] =
  21047. "Ed448PrivateKeyUnitTest................................\n";
  21048. const byte pubKey[] =
  21049. "Ed448PublicKeyUnitTest.................................\n";
  21050. word32 privKeySz = sizeof(privKey);
  21051. word32 pubKeySz = sizeof(pubKey);
  21052. #ifdef HAVE_ED448_KEY_EXPORT
  21053. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  21054. word32 bothKeysSz = sizeof(bothKeys);
  21055. #endif
  21056. XMEMSET(&key, 0, sizeof(ed448_key));
  21057. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21058. ExpectIntEQ(wc_ed448_init(&key), 0);
  21059. ExpectIntEQ(wc_InitRng(&rng), 0);
  21060. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21061. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  21062. pubKeySz, &key, 1), 0);
  21063. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  21064. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  21065. #ifdef HAVE_ED448_KEY_EXPORT
  21066. PRIVATE_KEY_UNLOCK();
  21067. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  21068. PRIVATE_KEY_LOCK();
  21069. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  21070. &key, 1), 0);
  21071. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  21072. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  21073. #endif
  21074. /* Test bad args. */
  21075. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  21076. &key), BAD_FUNC_ARG);
  21077. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  21078. &key), BAD_FUNC_ARG);
  21079. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21080. pubKeySz, NULL), BAD_FUNC_ARG);
  21081. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  21082. pubKeySz, &key), BAD_FUNC_ARG);
  21083. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21084. pubKeySz - 1, &key), BAD_FUNC_ARG);
  21085. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  21086. BAD_FUNC_ARG);
  21087. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21088. wc_ed448_free(&key);
  21089. #endif
  21090. return EXPECT_RESULT();
  21091. } /* END test_wc_ed448_import_private_key */
  21092. /*
  21093. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  21094. */
  21095. static int test_wc_ed448_export(void)
  21096. {
  21097. EXPECT_DECLS;
  21098. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21099. ed448_key key;
  21100. WC_RNG rng;
  21101. byte priv[ED448_PRV_KEY_SIZE];
  21102. byte pub[ED448_PUB_KEY_SIZE];
  21103. word32 privSz = sizeof(priv);
  21104. word32 pubSz = sizeof(pub);
  21105. XMEMSET(&key, 0, sizeof(ed448_key));
  21106. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21107. ExpectIntEQ(wc_ed448_init(&key), 0);
  21108. ExpectIntEQ(wc_InitRng(&rng), 0);
  21109. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21110. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  21111. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  21112. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  21113. /* Test bad args. */
  21114. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  21115. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  21116. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  21117. PRIVATE_KEY_UNLOCK();
  21118. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  21119. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  21120. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  21121. /* Test bad args. */
  21122. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  21123. BAD_FUNC_ARG);
  21124. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  21125. BAD_FUNC_ARG);
  21126. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  21127. PRIVATE_KEY_LOCK();
  21128. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21129. wc_ed448_free(&key);
  21130. #endif
  21131. return EXPECT_RESULT();
  21132. } /* END test_wc_ed448_export */
  21133. /*
  21134. * Testing wc_ed448_size()
  21135. */
  21136. static int test_wc_ed448_size(void)
  21137. {
  21138. EXPECT_DECLS;
  21139. #if defined(HAVE_ED448)
  21140. ed448_key key;
  21141. WC_RNG rng;
  21142. XMEMSET(&key, 0, sizeof(ed448_key));
  21143. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21144. ExpectIntEQ(wc_ed448_init(&key), 0);
  21145. ExpectIntEQ(wc_InitRng(&rng), 0);
  21146. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21147. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  21148. /* Test bad args. */
  21149. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  21150. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  21151. /* Test bad args. */
  21152. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  21153. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  21154. /* Test bad args. */
  21155. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  21156. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  21157. /* Test bad args. */
  21158. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  21159. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21160. wc_ed448_free(&key);
  21161. #endif
  21162. return EXPECT_RESULT();
  21163. } /* END test_wc_ed448_size */
  21164. /*
  21165. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21166. */
  21167. static int test_wc_ed448_exportKey(void)
  21168. {
  21169. EXPECT_DECLS;
  21170. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21171. ed448_key key;
  21172. WC_RNG rng;
  21173. byte priv[ED448_PRV_KEY_SIZE];
  21174. byte pub[ED448_PUB_KEY_SIZE];
  21175. byte privOnly[ED448_PRV_KEY_SIZE];
  21176. word32 privSz = sizeof(priv);
  21177. word32 pubSz = sizeof(pub);
  21178. word32 privOnlySz = sizeof(privOnly);
  21179. XMEMSET(&key, 0, sizeof(ed448_key));
  21180. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21181. ExpectIntEQ(wc_ed448_init(&key), 0);
  21182. ExpectIntEQ(wc_InitRng(&rng), 0);
  21183. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21184. PRIVATE_KEY_UNLOCK();
  21185. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  21186. /* Test bad args. */
  21187. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  21188. BAD_FUNC_ARG);
  21189. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  21190. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  21191. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  21192. /* Test bad args. */
  21193. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  21194. BAD_FUNC_ARG);
  21195. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  21196. BAD_FUNC_ARG);
  21197. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  21198. BAD_FUNC_ARG);
  21199. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  21200. BAD_FUNC_ARG);
  21201. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  21202. BAD_FUNC_ARG);
  21203. PRIVATE_KEY_LOCK();
  21204. /* Cross check output. */
  21205. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  21206. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21207. wc_ed448_free(&key);
  21208. #endif
  21209. return EXPECT_RESULT();
  21210. } /* END test_wc_ed448_exportKey */
  21211. /*
  21212. * Testing wc_Ed448PublicKeyToDer
  21213. */
  21214. static int test_wc_Ed448PublicKeyToDer(void)
  21215. {
  21216. EXPECT_DECLS;
  21217. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21218. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21219. ed448_key key;
  21220. byte derBuf[1024];
  21221. XMEMSET(&key, 0, sizeof(ed448_key));
  21222. /* Test bad args */
  21223. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  21224. ExpectIntEQ(wc_ed448_init(&key), 0);
  21225. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  21226. wc_ed448_free(&key);
  21227. /* Test good args */
  21228. if (EXPECT_SUCCESS()) {
  21229. WC_RNG rng;
  21230. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21231. ExpectIntEQ(wc_ed448_init(&key), 0);
  21232. ExpectIntEQ(wc_InitRng(&rng), 0);
  21233. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21234. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  21235. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21236. wc_ed448_free(&key);
  21237. }
  21238. #endif
  21239. return EXPECT_RESULT();
  21240. } /* END testing wc_Ed448PublicKeyToDer */
  21241. /*
  21242. * Testing wc_curve448_init and wc_curve448_free.
  21243. */
  21244. static int test_wc_curve448_init(void)
  21245. {
  21246. EXPECT_DECLS;
  21247. #if defined(HAVE_CURVE448)
  21248. curve448_key key;
  21249. /* Test bad args for wc_curve448_init */
  21250. ExpectIntEQ(wc_curve448_init(&key), 0);
  21251. /* Test bad args for wc_curve448_init */
  21252. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  21253. /* Test good args for wc_curve_448_free */
  21254. wc_curve448_free(&key);
  21255. /* Test bad args for wc_curve448_free */
  21256. wc_curve448_free(NULL);
  21257. #endif
  21258. return EXPECT_RESULT();
  21259. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21260. /*
  21261. * Testing wc_curve448_make_key
  21262. */
  21263. static int test_wc_curve448_make_key(void)
  21264. {
  21265. EXPECT_DECLS;
  21266. #if defined(HAVE_CURVE448)
  21267. curve448_key key;
  21268. WC_RNG rng;
  21269. int keysize;
  21270. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21271. ExpectIntEQ(wc_curve448_init(&key), 0);
  21272. ExpectIntEQ(wc_InitRng(&rng), 0);
  21273. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21274. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21275. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  21276. /* test bad cases */
  21277. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21278. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21279. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21280. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21281. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21282. wc_curve448_free(&key);
  21283. #endif
  21284. return EXPECT_RESULT();
  21285. } /* END test_wc_curve448_make_key*/
  21286. /*
  21287. * Testing test_wc_curve448_shared_secret_ex
  21288. */
  21289. static int test_wc_curve448_shared_secret_ex(void)
  21290. {
  21291. EXPECT_DECLS;
  21292. #if defined(HAVE_CURVE448)
  21293. curve448_key private_key;
  21294. curve448_key public_key;
  21295. WC_RNG rng;
  21296. byte out[CURVE448_KEY_SIZE];
  21297. word32 outLen = sizeof(out);
  21298. int endian = EC448_BIG_ENDIAN;
  21299. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21300. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  21301. ExpectIntEQ(wc_InitRng(&rng), 0);
  21302. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  21303. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  21304. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  21305. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21306. &outLen, endian), 0);
  21307. /* test bad cases */
  21308. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21309. BAD_FUNC_ARG);
  21310. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  21311. endian), BAD_FUNC_ARG);
  21312. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  21313. endian), BAD_FUNC_ARG);
  21314. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21315. &outLen, endian), BAD_FUNC_ARG);
  21316. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21317. NULL, endian), BAD_FUNC_ARG);
  21318. outLen = outLen - 2;
  21319. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21320. &outLen, endian), BAD_FUNC_ARG);
  21321. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21322. wc_curve448_free(&private_key);
  21323. wc_curve448_free(&public_key);
  21324. #endif
  21325. return EXPECT_RESULT();
  21326. } /* END test_wc_curve448_shared_secret_ex*/
  21327. /*
  21328. * Testing test_wc_curve448_export_public_ex
  21329. */
  21330. static int test_wc_curve448_export_public_ex(void)
  21331. {
  21332. EXPECT_DECLS;
  21333. #if defined(HAVE_CURVE448)
  21334. WC_RNG rng;
  21335. curve448_key key;
  21336. byte out[CURVE448_KEY_SIZE];
  21337. word32 outLen = sizeof(out);
  21338. int endian = EC448_BIG_ENDIAN;
  21339. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21340. ExpectIntEQ(wc_curve448_init(&key), 0);
  21341. ExpectIntEQ(wc_InitRng(&rng), 0);
  21342. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21343. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  21344. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  21345. /* test bad cases*/
  21346. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  21347. BAD_FUNC_ARG);
  21348. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  21349. BAD_FUNC_ARG);
  21350. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  21351. BAD_FUNC_ARG);
  21352. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  21353. BAD_FUNC_ARG);
  21354. outLen = outLen - 2;
  21355. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  21356. ECC_BAD_ARG_E);
  21357. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21358. wc_curve448_free(&key);
  21359. #endif
  21360. return EXPECT_RESULT();
  21361. } /* END test_wc_curve448_export_public_ex*/
  21362. /*
  21363. * Testing test_wc_curve448_export_private_raw_ex
  21364. */
  21365. static int test_wc_curve448_export_private_raw_ex(void)
  21366. {
  21367. EXPECT_DECLS;
  21368. #if defined(HAVE_CURVE448)
  21369. curve448_key key;
  21370. byte out[CURVE448_KEY_SIZE];
  21371. word32 outLen = sizeof(out);
  21372. int endian = EC448_BIG_ENDIAN;
  21373. ExpectIntEQ(wc_curve448_init(&key), 0);
  21374. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21375. 0);
  21376. /* test bad cases*/
  21377. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  21378. BAD_FUNC_ARG);
  21379. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  21380. BAD_FUNC_ARG);
  21381. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  21382. BAD_FUNC_ARG);
  21383. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  21384. BAD_FUNC_ARG);
  21385. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21386. EC448_LITTLE_ENDIAN), 0);
  21387. outLen = outLen - 2;
  21388. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21389. ECC_BAD_ARG_E);
  21390. wc_curve448_free(&key);
  21391. #endif
  21392. return EXPECT_RESULT();
  21393. } /* END test_wc_curve448_export_private_raw_ex*/
  21394. /*
  21395. * Testing test_wc_curve448_import_private_raw_ex
  21396. */
  21397. static int test_wc_curve448_import_private_raw_ex(void)
  21398. {
  21399. EXPECT_DECLS;
  21400. #if defined(HAVE_CURVE448)
  21401. curve448_key key;
  21402. WC_RNG rng;
  21403. byte priv[CURVE448_KEY_SIZE];
  21404. byte pub[CURVE448_KEY_SIZE];
  21405. word32 privSz = sizeof(priv);
  21406. word32 pubSz = sizeof(pub);
  21407. int endian = EC448_BIG_ENDIAN;
  21408. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21409. ExpectIntEQ(wc_curve448_init(&key), 0);
  21410. ExpectIntEQ(wc_InitRng(&rng), 0);
  21411. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21412. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21413. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21414. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21415. &key, endian), 0);
  21416. /* test bad cases */
  21417. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  21418. BAD_FUNC_ARG);
  21419. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21420. &key, endian), BAD_FUNC_ARG);
  21421. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21422. &key, endian), BAD_FUNC_ARG);
  21423. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21424. NULL, endian), BAD_FUNC_ARG);
  21425. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21426. &key, endian), ECC_BAD_ARG_E);
  21427. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21428. &key, endian), ECC_BAD_ARG_E);
  21429. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21430. &key, EC448_LITTLE_ENDIAN), 0);
  21431. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21432. wc_curve448_free(&key);
  21433. #endif
  21434. return EXPECT_RESULT();
  21435. } /* END test_wc_curve448_import_private_raw_ex*/
  21436. /*
  21437. * Testing test_curve448_export_key_raw
  21438. */
  21439. static int test_wc_curve448_export_key_raw(void)
  21440. {
  21441. EXPECT_DECLS;
  21442. #if defined(HAVE_CURVE448)
  21443. curve448_key key;
  21444. WC_RNG rng;
  21445. byte priv[CURVE448_KEY_SIZE];
  21446. byte pub[CURVE448_KEY_SIZE];
  21447. word32 privSz = sizeof(priv);
  21448. word32 pubSz = sizeof(pub);
  21449. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21450. ExpectIntEQ(wc_curve448_init(&key), 0);
  21451. ExpectIntEQ(wc_InitRng(&rng), 0);
  21452. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21453. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21454. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21455. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  21456. 0);
  21457. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21458. wc_curve448_free(&key);
  21459. #endif
  21460. return EXPECT_RESULT();
  21461. } /* END test_wc_curve448_import_private_raw_ex*/
  21462. /*
  21463. * Testing test_wc_curve448_import_private
  21464. */
  21465. static int test_wc_curve448_import_private(void)
  21466. {
  21467. EXPECT_DECLS;
  21468. #if defined(HAVE_CURVE448)
  21469. curve448_key key;
  21470. WC_RNG rng;
  21471. byte priv[CURVE448_KEY_SIZE];
  21472. word32 privSz = sizeof(priv);
  21473. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21474. ExpectIntEQ(wc_curve448_init(&key), 0);
  21475. ExpectIntEQ(wc_InitRng(&rng), 0);
  21476. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21477. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21478. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  21479. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21480. wc_curve448_free(&key);
  21481. #endif
  21482. return EXPECT_RESULT();
  21483. } /* END test_wc_curve448_import*/
  21484. /*
  21485. * Testing test_wc_curve448_size.
  21486. */
  21487. static int test_wc_curve448_size(void)
  21488. {
  21489. EXPECT_DECLS;
  21490. #if defined(HAVE_CURVE448)
  21491. curve448_key key;
  21492. ExpectIntEQ(wc_curve448_init(&key), 0);
  21493. /* Test good args for wc_curve448_size */
  21494. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21495. /* Test bad args for wc_curve448_size */
  21496. ExpectIntEQ(wc_curve448_size(NULL), 0);
  21497. wc_curve448_free(&key);
  21498. #endif
  21499. return EXPECT_RESULT();
  21500. } /* END test_wc_curve448_size*/
  21501. /*
  21502. * Testing wc_ecc_make_key.
  21503. */
  21504. static int test_wc_ecc_make_key(void)
  21505. {
  21506. EXPECT_DECLS;
  21507. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21508. ecc_key key;
  21509. WC_RNG rng;
  21510. int ret;
  21511. XMEMSET(&key, 0, sizeof(ecc_key));
  21512. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21513. ExpectIntEQ(wc_ecc_init(&key), 0);
  21514. ExpectIntEQ(wc_InitRng(&rng), 0);
  21515. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21516. #if defined(WOLFSSL_ASYNC_CRYPT)
  21517. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21518. #endif
  21519. ExpectIntEQ(ret, 0);
  21520. /* Pass in bad args. */
  21521. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  21522. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  21523. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21524. wc_ecc_free(&key);
  21525. #ifdef FP_ECC
  21526. wc_ecc_fp_free();
  21527. #endif
  21528. #endif
  21529. return EXPECT_RESULT();
  21530. } /* END test_wc_ecc_make_key */
  21531. /*
  21532. * Testing wc_ecc_init()
  21533. */
  21534. static int test_wc_ecc_init(void)
  21535. {
  21536. EXPECT_DECLS;
  21537. #ifdef HAVE_ECC
  21538. ecc_key key;
  21539. XMEMSET(&key, 0, sizeof(ecc_key));
  21540. ExpectIntEQ(wc_ecc_init(&key), 0);
  21541. /* Pass in bad args. */
  21542. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  21543. wc_ecc_free(&key);
  21544. #endif
  21545. return EXPECT_RESULT();
  21546. } /* END test_wc_ecc_init */
  21547. /*
  21548. * Testing wc_ecc_check_key()
  21549. */
  21550. static int test_wc_ecc_check_key(void)
  21551. {
  21552. EXPECT_DECLS;
  21553. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21554. ecc_key key;
  21555. WC_RNG rng;
  21556. int ret;
  21557. XMEMSET(&rng, 0, sizeof(rng));
  21558. XMEMSET(&key, 0, sizeof(key));
  21559. ExpectIntEQ(wc_ecc_init(&key), 0);
  21560. ExpectIntEQ(wc_InitRng(&rng), 0);
  21561. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21562. #if defined(WOLFSSL_ASYNC_CRYPT)
  21563. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21564. #endif
  21565. ExpectIntEQ(ret, 0);
  21566. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  21567. /* Pass in bad args. */
  21568. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  21569. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21570. wc_ecc_free(&key);
  21571. #ifdef FP_ECC
  21572. wc_ecc_fp_free();
  21573. #endif
  21574. #endif
  21575. return EXPECT_RESULT();
  21576. } /* END test_wc_ecc_check_key */
  21577. /*
  21578. * Testing wc_ecc_get_generator()
  21579. */
  21580. static int test_wc_ecc_get_generator(void)
  21581. {
  21582. EXPECT_DECLS;
  21583. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21584. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21585. ecc_point* pt = NULL;
  21586. ExpectNotNull(pt = wc_ecc_new_point());
  21587. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21588. MP_OKAY);
  21589. /* Test bad args. */
  21590. /* Returns Zero for bad arg. */
  21591. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21592. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21593. MP_OKAY);
  21594. /* If we ever get to 1000 curves increase this number */
  21595. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21596. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21597. wc_ecc_del_point(pt);
  21598. #endif
  21599. return EXPECT_RESULT();
  21600. } /* END test_wc_ecc_get_generator */
  21601. /*
  21602. * Testing wc_ecc_size()
  21603. */
  21604. static int test_wc_ecc_size(void)
  21605. {
  21606. EXPECT_DECLS;
  21607. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21608. WC_RNG rng;
  21609. ecc_key key;
  21610. int ret;
  21611. XMEMSET(&key, 0, sizeof(ecc_key));
  21612. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21613. ExpectIntEQ(wc_ecc_init(&key), 0);
  21614. ExpectIntEQ(wc_InitRng(&rng), 0);
  21615. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21616. #if defined(WOLFSSL_ASYNC_CRYPT)
  21617. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21618. #endif
  21619. ExpectIntEQ(ret, 0);
  21620. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21621. /* Test bad args. */
  21622. /* Returns Zero for bad arg. */
  21623. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21624. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21625. wc_ecc_free(&key);
  21626. #endif
  21627. return EXPECT_RESULT();
  21628. } /* END test_wc_ecc_size */
  21629. static int test_wc_ecc_params(void)
  21630. {
  21631. EXPECT_DECLS;
  21632. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21633. It was added after certifications */
  21634. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21635. const ecc_set_type* ecc_set;
  21636. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21637. /* Test for SECP256R1 curve */
  21638. int curve_id = ECC_SECP256R1;
  21639. int curve_idx;
  21640. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21641. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21642. ExpectIntEQ(ecc_set->id, curve_id);
  21643. #endif
  21644. /* Test case when SECP256R1 is not enabled */
  21645. /* Test that we get curve params for index 0 */
  21646. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21647. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21648. return EXPECT_RESULT();
  21649. }
  21650. /*
  21651. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21652. */
  21653. static int test_wc_ecc_signVerify_hash(void)
  21654. {
  21655. EXPECT_DECLS;
  21656. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21657. ecc_key key;
  21658. WC_RNG rng;
  21659. int ret;
  21660. #ifdef HAVE_ECC_VERIFY
  21661. int verify = 0;
  21662. #endif
  21663. word32 siglen = ECC_BUFSIZE;
  21664. byte sig[ECC_BUFSIZE];
  21665. byte adjustedSig[ECC_BUFSIZE+1];
  21666. byte digest[] = TEST_STRING;
  21667. word32 digestlen = (word32)TEST_STRING_SZ;
  21668. /* Init stack var */
  21669. XMEMSET(&key, 0, sizeof(ecc_key));
  21670. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21671. XMEMSET(sig, 0, siglen);
  21672. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21673. /* Init structs. */
  21674. ExpectIntEQ(wc_ecc_init(&key), 0);
  21675. ExpectIntEQ(wc_InitRng(&rng), 0);
  21676. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21677. #if defined(WOLFSSL_ASYNC_CRYPT)
  21678. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21679. #endif
  21680. ExpectIntEQ(ret, 0);
  21681. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21682. 0);
  21683. /* Check bad args. */
  21684. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21685. ECC_BAD_ARG_E);
  21686. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21687. ECC_BAD_ARG_E);
  21688. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21689. ECC_BAD_ARG_E);
  21690. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21691. ECC_BAD_ARG_E);
  21692. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21693. ECC_BAD_ARG_E);
  21694. #ifdef HAVE_ECC_VERIFY
  21695. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21696. &key), 0);
  21697. ExpectIntEQ(verify, 1);
  21698. /* test check on length of signature passed in */
  21699. XMEMCPY(adjustedSig, sig, siglen);
  21700. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21701. #ifndef NO_STRICT_ECDSA_LEN
  21702. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21703. &verify, &key), 0);
  21704. #else
  21705. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21706. * is allowed */
  21707. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21708. &verify, &key), 0);
  21709. #endif
  21710. /* Test bad args. */
  21711. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21712. &key), ECC_BAD_ARG_E);
  21713. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21714. ECC_BAD_ARG_E);
  21715. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21716. ECC_BAD_ARG_E);
  21717. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21718. NULL), ECC_BAD_ARG_E);
  21719. #endif /* HAVE_ECC_VERIFY */
  21720. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21721. wc_ecc_free(&key);
  21722. #ifdef FP_ECC
  21723. wc_ecc_fp_free();
  21724. #endif
  21725. #endif
  21726. return EXPECT_RESULT();
  21727. } /* END test_wc_ecc_sign_hash */
  21728. /*
  21729. * Testing wc_ecc_shared_secret()
  21730. */
  21731. static int test_wc_ecc_shared_secret(void)
  21732. {
  21733. EXPECT_DECLS;
  21734. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21735. ecc_key key;
  21736. ecc_key pubKey;
  21737. WC_RNG rng;
  21738. #if defined(NO_ECC256)
  21739. int ret;
  21740. #endif
  21741. byte out[KEY32];
  21742. int keySz = sizeof(out);
  21743. word32 outlen = (word32)sizeof(out);
  21744. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21745. const char* qx =
  21746. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21747. const char* qy =
  21748. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21749. const char* d =
  21750. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21751. const char* curveName = "SECP256R1";
  21752. const byte expected_shared_secret[] =
  21753. {
  21754. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21755. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21756. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21757. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21758. };
  21759. #endif
  21760. PRIVATE_KEY_UNLOCK();
  21761. /* Initialize variables. */
  21762. XMEMSET(&key, 0, sizeof(ecc_key));
  21763. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21764. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21765. XMEMSET(out, 0, keySz);
  21766. ExpectIntEQ(wc_ecc_init(&key), 0);
  21767. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21768. ExpectIntEQ(wc_InitRng(&rng), 0);
  21769. #if !defined(NO_ECC256)
  21770. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21771. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21772. #else
  21773. ret = wc_ecc_make_key(&rng, keySz, &key);
  21774. #if defined(WOLFSSL_ASYNC_CRYPT)
  21775. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21776. #endif
  21777. ExpectIntEQ(ret, 0);
  21778. ret = wc_ecc_make_key(&rng, keySz, &key);
  21779. #if defined(WOLFSSL_ASYNC_CRYPT)
  21780. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21781. #endif
  21782. ExpectIntEQ(ret, 0);
  21783. #endif
  21784. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21785. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21786. !defined(HAVE_SELFTEST)
  21787. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21788. #endif
  21789. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21790. #if !defined(NO_ECC256)
  21791. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21792. #endif
  21793. /* Test bad args. */
  21794. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21795. BAD_FUNC_ARG);
  21796. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21797. BAD_FUNC_ARG);
  21798. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21799. BAD_FUNC_ARG);
  21800. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21801. BAD_FUNC_ARG);
  21802. /* Invalid length */
  21803. outlen = 1;
  21804. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21805. BUFFER_E);
  21806. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21807. wc_ecc_free(&pubKey);
  21808. wc_ecc_free(&key);
  21809. #ifdef FP_ECC
  21810. wc_ecc_fp_free();
  21811. #endif
  21812. PRIVATE_KEY_LOCK();
  21813. #endif
  21814. return EXPECT_RESULT();
  21815. } /* END tests_wc_ecc_shared_secret */
  21816. /*
  21817. * testint wc_ecc_export_x963()
  21818. */
  21819. static int test_wc_ecc_export_x963(void)
  21820. {
  21821. EXPECT_DECLS;
  21822. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21823. ecc_key key;
  21824. WC_RNG rng;
  21825. byte out[ECC_ASN963_MAX_BUF_SZ];
  21826. word32 outlen = sizeof(out);
  21827. int ret;
  21828. PRIVATE_KEY_UNLOCK();
  21829. /* Initialize variables. */
  21830. XMEMSET(&key, 0, sizeof(ecc_key));
  21831. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21832. XMEMSET(out, 0, outlen);
  21833. ExpectIntEQ(wc_ecc_init(&key), 0);
  21834. ExpectIntEQ(wc_InitRng(&rng), 0);
  21835. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21836. #if defined(WOLFSSL_ASYNC_CRYPT)
  21837. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21838. #endif
  21839. ExpectIntEQ(ret, 0);
  21840. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21841. /* Test bad args. */
  21842. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21843. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21844. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21845. key.idx = -4;
  21846. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21847. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21848. wc_ecc_free(&key);
  21849. #ifdef FP_ECC
  21850. wc_ecc_fp_free();
  21851. #endif
  21852. PRIVATE_KEY_LOCK();
  21853. #endif
  21854. return EXPECT_RESULT();
  21855. } /* END test_wc_ecc_export_x963 */
  21856. /*
  21857. * Testing wc_ecc_export_x963_ex()
  21858. * compile with --enable-compkey will use compression.
  21859. */
  21860. static int test_wc_ecc_export_x963_ex(void)
  21861. {
  21862. EXPECT_DECLS;
  21863. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21864. ecc_key key;
  21865. WC_RNG rng;
  21866. int ret;
  21867. byte out[ECC_ASN963_MAX_BUF_SZ];
  21868. word32 outlen = sizeof(out);
  21869. #ifdef HAVE_COMP_KEY
  21870. word32 badOutLen = 5;
  21871. #endif
  21872. /* Init stack variables. */
  21873. XMEMSET(&key, 0, sizeof(ecc_key));
  21874. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21875. XMEMSET(out, 0, outlen);
  21876. PRIVATE_KEY_UNLOCK();
  21877. ExpectIntEQ(wc_ecc_init(&key), 0);
  21878. ExpectIntEQ(wc_InitRng(&rng), 0);
  21879. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21880. #if defined(WOLFSSL_ASYNC_CRYPT)
  21881. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21882. #endif
  21883. ExpectIntEQ(ret, 0);
  21884. #ifdef HAVE_COMP_KEY
  21885. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21886. #else
  21887. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21888. #endif
  21889. /* Test bad args. */
  21890. #ifdef HAVE_COMP_KEY
  21891. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21892. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21893. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21894. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21895. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21896. #else
  21897. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21898. LENGTH_ONLY_E);
  21899. #endif
  21900. key.idx = -4;
  21901. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21902. #else
  21903. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21904. ECC_BAD_ARG_E);
  21905. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21906. LENGTH_ONLY_E);
  21907. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21908. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21909. ECC_BAD_ARG_E);
  21910. key.idx = -4;
  21911. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21912. ECC_BAD_ARG_E);
  21913. #endif
  21914. PRIVATE_KEY_LOCK();
  21915. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21916. wc_ecc_free(&key);
  21917. #ifdef FP_ECC
  21918. wc_ecc_fp_free();
  21919. #endif
  21920. #endif
  21921. return EXPECT_RESULT();
  21922. } /* END test_wc_ecc_export_x963_ex */
  21923. /*
  21924. * testing wc_ecc_import_x963()
  21925. */
  21926. static int test_wc_ecc_import_x963(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 pubKey;
  21932. ecc_key key;
  21933. WC_RNG rng;
  21934. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21935. word32 x963Len = (word32)sizeof(x963);
  21936. int ret;
  21937. /* Init stack variables. */
  21938. XMEMSET(&key, 0, sizeof(ecc_key));
  21939. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21940. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21941. XMEMSET(x963, 0, x963Len);
  21942. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21943. ExpectIntEQ(wc_ecc_init(&key), 0);
  21944. ExpectIntEQ(wc_InitRng(&rng), 0);
  21945. #if FIPS_VERSION3_GE(6,0,0)
  21946. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21947. #else
  21948. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21949. #endif
  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, x963, &x963Len), 0);
  21956. PRIVATE_KEY_LOCK();
  21957. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21958. /* Test bad args. */
  21959. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21960. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21961. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21962. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21963. wc_ecc_free(&key);
  21964. wc_ecc_free(&pubKey);
  21965. #ifdef FP_ECC
  21966. wc_ecc_fp_free();
  21967. #endif
  21968. #endif
  21969. return EXPECT_RESULT();
  21970. } /* END wc_ecc_import_x963 */
  21971. /*
  21972. * testing wc_ecc_import_private_key()
  21973. */
  21974. static int test_wc_ecc_import_private_key(void)
  21975. {
  21976. EXPECT_DECLS;
  21977. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21978. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21979. ecc_key key;
  21980. ecc_key keyImp;
  21981. WC_RNG rng;
  21982. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21983. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21984. word32 privKeySz = (word32)sizeof(privKey);
  21985. word32 x963KeySz = (word32)sizeof(x963Key);
  21986. int ret;
  21987. /* Init stack variables. */
  21988. XMEMSET(&key, 0, sizeof(ecc_key));
  21989. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21990. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21991. XMEMSET(privKey, 0, privKeySz);
  21992. XMEMSET(x963Key, 0, x963KeySz);
  21993. PRIVATE_KEY_UNLOCK();
  21994. ExpectIntEQ(wc_ecc_init(&key), 0);
  21995. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21996. ExpectIntEQ(wc_InitRng(&rng), 0);
  21997. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21998. #if defined(WOLFSSL_ASYNC_CRYPT)
  21999. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22000. #endif
  22001. ExpectIntEQ(ret, 0);
  22002. PRIVATE_KEY_UNLOCK();
  22003. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  22004. PRIVATE_KEY_LOCK();
  22005. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  22006. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22007. x963KeySz, &keyImp), 0);
  22008. /* Pass in bad args. */
  22009. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22010. x963KeySz, NULL), BAD_FUNC_ARG);
  22011. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  22012. &keyImp), BAD_FUNC_ARG);
  22013. PRIVATE_KEY_LOCK();
  22014. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22015. wc_ecc_free(&keyImp);
  22016. wc_ecc_free(&key);
  22017. #ifdef FP_ECC
  22018. wc_ecc_fp_free();
  22019. #endif
  22020. #endif
  22021. return EXPECT_RESULT();
  22022. } /* END test_wc_ecc_import_private_key */
  22023. /*
  22024. * Testing wc_ecc_export_private_only()
  22025. */
  22026. static int test_wc_ecc_export_private_only(void)
  22027. {
  22028. EXPECT_DECLS;
  22029. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22030. ecc_key key;
  22031. WC_RNG rng;
  22032. byte out[ECC_PRIV_KEY_BUF];
  22033. word32 outlen = sizeof(out);
  22034. int ret;
  22035. /* Init stack variables. */
  22036. XMEMSET(&key, 0, sizeof(ecc_key));
  22037. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22038. XMEMSET(out, 0, outlen);
  22039. PRIVATE_KEY_UNLOCK();
  22040. ExpectIntEQ(wc_ecc_init(&key), 0);
  22041. ExpectIntEQ(wc_InitRng(&rng), 0);
  22042. ret = wc_ecc_make_key(&rng, KEY32, &key);
  22043. #if defined(WOLFSSL_ASYNC_CRYPT)
  22044. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22045. #endif
  22046. ExpectIntEQ(ret, 0);
  22047. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  22048. /* Pass in bad args. */
  22049. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  22050. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  22051. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  22052. PRIVATE_KEY_LOCK();
  22053. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22054. wc_ecc_free(&key);
  22055. #ifdef FP_ECC
  22056. wc_ecc_fp_free();
  22057. #endif
  22058. #endif
  22059. return EXPECT_RESULT();
  22060. } /* END test_wc_ecc_export_private_only */
  22061. /*
  22062. * Testing wc_ecc_rs_to_sig()
  22063. */
  22064. static int test_wc_ecc_rs_to_sig(void)
  22065. {
  22066. EXPECT_DECLS;
  22067. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22068. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22069. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22070. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22071. const char* zeroStr = "0";
  22072. byte sig[ECC_MAX_SIG_SIZE];
  22073. word32 siglen = (word32)sizeof(sig);
  22074. /* R and S max size is the order of curve. 2^192.*/
  22075. int keySz = KEY24;
  22076. byte r[KEY24];
  22077. byte s[KEY24];
  22078. word32 rlen = (word32)sizeof(r);
  22079. word32 slen = (word32)sizeof(s);
  22080. /* Init stack variables. */
  22081. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22082. XMEMSET(r, 0, keySz);
  22083. XMEMSET(s, 0, keySz);
  22084. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  22085. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  22086. /* Test bad args. */
  22087. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  22088. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  22089. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  22090. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  22091. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  22092. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  22093. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  22094. ECC_BAD_ARG_E);
  22095. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  22096. ECC_BAD_ARG_E);
  22097. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  22098. ECC_BAD_ARG_E);
  22099. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  22100. ECC_BAD_ARG_E);
  22101. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  22102. ECC_BAD_ARG_E);
  22103. #endif
  22104. return EXPECT_RESULT();
  22105. } /* END test_wc_ecc_rs_to_sig */
  22106. static int test_wc_ecc_import_raw(void)
  22107. {
  22108. EXPECT_DECLS;
  22109. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22110. ecc_key key;
  22111. const char* qx =
  22112. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22113. const char* qy =
  22114. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22115. const char* d =
  22116. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22117. const char* curveName = "SECP256R1";
  22118. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22119. const char* kNullStr = "";
  22120. int ret;
  22121. #endif
  22122. XMEMSET(&key, 0, sizeof(ecc_key));
  22123. ExpectIntEQ(wc_ecc_init(&key), 0);
  22124. /* Test good import */
  22125. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  22126. /* Test bad args. */
  22127. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  22128. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  22129. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  22130. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  22131. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22132. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22133. wc_ecc_free(&key);
  22134. #endif
  22135. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  22136. curveName), 0);
  22137. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22138. #endif
  22139. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22140. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22141. wc_ecc_free(&key);
  22142. #endif
  22143. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22144. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22145. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22146. #else
  22147. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22148. #endif
  22149. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22150. wc_ecc_free(&key);
  22151. #endif
  22152. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22153. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22154. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22155. #else
  22156. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22157. #endif
  22158. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22159. wc_ecc_free(&key);
  22160. #endif
  22161. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  22162. #endif
  22163. wc_ecc_free(&key);
  22164. #endif
  22165. return EXPECT_RESULT();
  22166. } /* END test_wc_ecc_import_raw */
  22167. static int test_wc_ecc_import_unsigned(void)
  22168. {
  22169. EXPECT_DECLS;
  22170. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22171. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  22172. HAVE_FIPS_VERSION >= 2))
  22173. ecc_key key;
  22174. const byte qx[] = {
  22175. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22176. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22177. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22178. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22179. };
  22180. const byte qy[] = {
  22181. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22182. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22183. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22184. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22185. };
  22186. const byte d[] = {
  22187. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22188. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22189. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22190. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22191. };
  22192. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22193. const byte nullBytes[32] = {0};
  22194. int ret;
  22195. #endif
  22196. int curveId = ECC_SECP256R1;
  22197. XMEMSET(&key, 0, sizeof(ecc_key));
  22198. ExpectIntEQ(wc_ecc_init(&key), 0);
  22199. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22200. curveId), 0);
  22201. /* Test bad args. */
  22202. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22203. curveId), BAD_FUNC_ARG);
  22204. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22205. curveId), BAD_FUNC_ARG);
  22206. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22207. curveId), BAD_FUNC_ARG);
  22208. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22209. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  22210. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22211. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22212. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  22213. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22214. #endif
  22215. wc_ecc_free(&key);
  22216. #endif
  22217. return EXPECT_RESULT();
  22218. } /* END test_wc_ecc_import_unsigned */
  22219. /*
  22220. * Testing wc_ecc_sig_size()
  22221. */
  22222. static int test_wc_ecc_sig_size(void)
  22223. {
  22224. EXPECT_DECLS;
  22225. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22226. ecc_key key;
  22227. WC_RNG rng;
  22228. int keySz = KEY16;
  22229. int ret;
  22230. XMEMSET(&rng, 0, sizeof(rng));
  22231. XMEMSET(&key, 0, sizeof(key));
  22232. ExpectIntEQ(wc_ecc_init(&key), 0);
  22233. ExpectIntEQ(wc_InitRng(&rng), 0);
  22234. ret = wc_ecc_make_key(&rng, keySz, &key);
  22235. #if defined(WOLFSSL_ASYNC_CRYPT)
  22236. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22237. #endif
  22238. ExpectIntEQ(ret, 0);
  22239. ExpectIntLE(wc_ecc_sig_size(&key),
  22240. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  22241. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22242. wc_ecc_free(&key);
  22243. #endif
  22244. return EXPECT_RESULT();
  22245. } /* END test_wc_ecc_sig_size */
  22246. /*
  22247. * Testing wc_ecc_ctx_new()
  22248. */
  22249. static int test_wc_ecc_ctx_new(void)
  22250. {
  22251. EXPECT_DECLS;
  22252. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22253. WC_RNG rng;
  22254. ecEncCtx* cli = NULL;
  22255. ecEncCtx* srv = NULL;
  22256. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22257. ExpectIntEQ(wc_InitRng(&rng), 0);
  22258. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22259. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22260. wc_ecc_ctx_free(cli);
  22261. cli = NULL;
  22262. wc_ecc_ctx_free(srv);
  22263. /* Test bad args. */
  22264. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22265. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  22266. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  22267. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22268. wc_ecc_ctx_free(cli);
  22269. #endif
  22270. return EXPECT_RESULT();
  22271. } /* END test_wc_ecc_ctx_new */
  22272. /*
  22273. * Tesing wc_ecc_reset()
  22274. */
  22275. static int test_wc_ecc_ctx_reset(void)
  22276. {
  22277. EXPECT_DECLS;
  22278. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22279. ecEncCtx* ctx = NULL;
  22280. WC_RNG rng;
  22281. XMEMSET(&rng, 0, sizeof(rng));
  22282. ExpectIntEQ(wc_InitRng(&rng), 0);
  22283. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22284. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  22285. /* Pass in bad args. */
  22286. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  22287. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  22288. wc_ecc_ctx_free(ctx);
  22289. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22290. #endif
  22291. return EXPECT_RESULT();
  22292. } /* END test_wc_ecc_ctx_reset */
  22293. /*
  22294. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22295. */
  22296. static int test_wc_ecc_ctx_set_peer_salt(void)
  22297. {
  22298. EXPECT_DECLS;
  22299. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22300. WC_RNG rng;
  22301. ecEncCtx* cliCtx = NULL;
  22302. ecEncCtx* servCtx = NULL;
  22303. const byte* cliSalt = NULL;
  22304. const byte* servSalt = NULL;
  22305. XMEMSET(&rng, 0, sizeof(rng));
  22306. ExpectIntEQ(wc_InitRng(&rng), 0);
  22307. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22308. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22309. /* Test bad args. */
  22310. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  22311. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  22312. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  22313. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  22314. /* Test bad args. */
  22315. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  22316. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  22317. wc_ecc_ctx_free(cliCtx);
  22318. wc_ecc_ctx_free(servCtx);
  22319. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22320. #endif
  22321. return EXPECT_RESULT();
  22322. } /* END test_wc_ecc_ctx_set_peer_salt */
  22323. /*
  22324. * Testing wc_ecc_ctx_set_info()
  22325. */
  22326. static int test_wc_ecc_ctx_set_info(void)
  22327. {
  22328. EXPECT_DECLS;
  22329. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22330. ecEncCtx* ctx = NULL;
  22331. WC_RNG rng;
  22332. const char* optInfo = "Optional Test Info.";
  22333. int optInfoSz = (int)XSTRLEN(optInfo);
  22334. const char* badOptInfo = NULL;
  22335. XMEMSET(&rng, 0, sizeof(rng));
  22336. ExpectIntEQ(wc_InitRng(&rng), 0);
  22337. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22338. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  22339. /* Test bad args. */
  22340. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  22341. BAD_FUNC_ARG);
  22342. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  22343. BAD_FUNC_ARG);
  22344. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  22345. wc_ecc_ctx_free(ctx);
  22346. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22347. #endif
  22348. return EXPECT_RESULT();
  22349. } /* END test_wc_ecc_ctx_set_info */
  22350. /*
  22351. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22352. */
  22353. static int test_wc_ecc_encryptDecrypt(void)
  22354. {
  22355. EXPECT_DECLS;
  22356. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22357. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22358. ecc_key srvKey;
  22359. ecc_key cliKey;
  22360. ecc_key tmpKey;
  22361. WC_RNG rng;
  22362. int ret;
  22363. const char* msg = "EccBlock Size 16";
  22364. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22365. #ifdef WOLFSSL_ECIES_OLD
  22366. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22367. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22368. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22369. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22370. #else
  22371. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  22372. WC_SHA256_DIGEST_SIZE];
  22373. #endif
  22374. word32 outSz = (word32)sizeof(out);
  22375. byte plain[sizeof("EccBlock Size 16")];
  22376. word32 plainSz = (word32)sizeof(plain);
  22377. int keySz = KEY20;
  22378. /* Init stack variables. */
  22379. XMEMSET(out, 0, outSz);
  22380. XMEMSET(plain, 0, plainSz);
  22381. XMEMSET(&rng, 0, sizeof(rng));
  22382. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  22383. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  22384. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  22385. ExpectIntEQ(wc_InitRng(&rng), 0);
  22386. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  22387. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22388. #if defined(WOLFSSL_ASYNC_CRYPT)
  22389. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22390. #endif
  22391. ExpectIntEQ(ret, 0);
  22392. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  22393. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22394. #if defined(WOLFSSL_ASYNC_CRYPT)
  22395. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22396. #endif
  22397. ExpectIntEQ(ret, 0);
  22398. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  22399. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22400. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22401. !defined(HAVE_SELFTEST)
  22402. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  22403. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  22404. #endif
  22405. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22406. &outSz, NULL), 0);
  22407. /* Test bad args. */
  22408. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  22409. NULL), BAD_FUNC_ARG);
  22410. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  22411. NULL), BAD_FUNC_ARG);
  22412. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  22413. NULL), BAD_FUNC_ARG);
  22414. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22415. &outSz, NULL), BAD_FUNC_ARG);
  22416. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  22417. NULL), BAD_FUNC_ARG);
  22418. #ifdef WOLFSSL_ECIES_OLD
  22419. tmpKey.dp = cliKey.dp;
  22420. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  22421. #endif
  22422. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  22423. NULL), 0);
  22424. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  22425. NULL), BAD_FUNC_ARG);
  22426. #ifdef WOLFSSL_ECIES_OLD
  22427. /* NULL parameter allowed in new implementations - public key comes from
  22428. * the message. */
  22429. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  22430. NULL), BAD_FUNC_ARG);
  22431. #endif
  22432. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  22433. NULL), BAD_FUNC_ARG);
  22434. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  22435. NULL), BAD_FUNC_ARG);
  22436. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  22437. BAD_FUNC_ARG);
  22438. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  22439. wc_ecc_free(&tmpKey);
  22440. wc_ecc_free(&srvKey);
  22441. wc_ecc_free(&cliKey);
  22442. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22443. #endif
  22444. return EXPECT_RESULT();
  22445. } /* END test_wc_ecc_encryptDecrypt */
  22446. /*
  22447. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22448. */
  22449. static int test_wc_ecc_del_point(void)
  22450. {
  22451. EXPECT_DECLS;
  22452. #if defined(HAVE_ECC)
  22453. ecc_point* pt = NULL;
  22454. ExpectNotNull(pt = wc_ecc_new_point());
  22455. wc_ecc_del_point(pt);
  22456. #endif
  22457. return EXPECT_RESULT();
  22458. } /* END test_wc_ecc_del_point */
  22459. /*
  22460. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22461. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22462. * and wc_ecc_cmp_point()
  22463. */
  22464. static int test_wc_ecc_pointFns(void)
  22465. {
  22466. EXPECT_DECLS;
  22467. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22468. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22469. !defined(WOLFSSL_ATECC608A)
  22470. ecc_key key;
  22471. WC_RNG rng;
  22472. int ret;
  22473. ecc_point* point = NULL;
  22474. ecc_point* cpypt = NULL;
  22475. int idx = 0;
  22476. int keySz = KEY32;
  22477. byte der[DER_SZ(KEY32)];
  22478. word32 derlenChk = 0;
  22479. word32 derSz = DER_SZ(KEY32);
  22480. /* Init stack variables. */
  22481. XMEMSET(der, 0, derSz);
  22482. XMEMSET(&key, 0, sizeof(ecc_key));
  22483. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22484. ExpectIntEQ(wc_InitRng(&rng), 0);
  22485. ExpectIntEQ(wc_ecc_init(&key), 0);
  22486. ret = wc_ecc_make_key(&rng, keySz, &key);
  22487. #if defined(WOLFSSL_ASYNC_CRYPT)
  22488. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22489. #endif
  22490. ExpectIntEQ(ret, 0);
  22491. ExpectNotNull(point = wc_ecc_new_point());
  22492. ExpectNotNull(cpypt = wc_ecc_new_point());
  22493. /* Export */
  22494. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  22495. &derlenChk), LENGTH_ONLY_E);
  22496. /* Check length value. */
  22497. ExpectIntEQ(derSz, derlenChk);
  22498. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22499. &derSz), 0);
  22500. /* Test bad args. */
  22501. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  22502. ECC_BAD_ARG_E);
  22503. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  22504. ECC_BAD_ARG_E);
  22505. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22506. NULL), ECC_BAD_ARG_E);
  22507. /* Import */
  22508. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  22509. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  22510. /* Test bad args. */
  22511. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  22512. ECC_BAD_ARG_E);
  22513. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  22514. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  22515. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  22516. ECC_BAD_ARG_E);
  22517. /* Copy */
  22518. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  22519. /* Test bad args. */
  22520. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  22521. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  22522. /* Compare point */
  22523. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  22524. /* Test bad args. */
  22525. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  22526. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  22527. /* At infinity if return == 1, otherwise return == 0. */
  22528. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  22529. /* Test bad args. */
  22530. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  22531. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  22532. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  22533. #ifdef USE_ECC_B_PARAM
  22534. /* On curve if ret == 0 */
  22535. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  22536. /* Test bad args. */
  22537. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  22538. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  22539. #endif /* USE_ECC_B_PARAM */
  22540. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  22541. /* Free */
  22542. wc_ecc_del_point(point);
  22543. wc_ecc_del_point(cpypt);
  22544. wc_ecc_free(&key);
  22545. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22546. #endif
  22547. return EXPECT_RESULT();
  22548. } /* END test_wc_ecc_pointFns */
  22549. /*
  22550. * Testing wc_ecc_shared_secret_ssh()
  22551. */
  22552. static int test_wc_ecc_shared_secret_ssh(void)
  22553. {
  22554. EXPECT_DECLS;
  22555. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22556. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22557. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  22558. !defined(WOLFSSL_CRYPTOCELL)
  22559. ecc_key key;
  22560. ecc_key key2;
  22561. WC_RNG rng;
  22562. int ret;
  22563. int keySz = KEY32;
  22564. #if FIPS_VERSION3_GE(6,0,0)
  22565. int key2Sz = KEY28;
  22566. #else
  22567. int key2Sz = KEY24;
  22568. #endif
  22569. byte secret[KEY32];
  22570. word32 secretLen = (word32)keySz;
  22571. /* Init stack variables. */
  22572. XMEMSET(&key, 0, sizeof(ecc_key));
  22573. XMEMSET(&key2, 0, sizeof(ecc_key));
  22574. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22575. XMEMSET(secret, 0, secretLen);
  22576. PRIVATE_KEY_UNLOCK();
  22577. /* Make keys */
  22578. ExpectIntEQ(wc_ecc_init(&key), 0);
  22579. ExpectIntEQ(wc_InitRng(&rng), 0);
  22580. ret = wc_ecc_make_key(&rng, keySz, &key);
  22581. #if defined(WOLFSSL_ASYNC_CRYPT)
  22582. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22583. #endif
  22584. ExpectIntEQ(ret, 0);
  22585. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22586. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22587. ExpectIntEQ(wc_InitRng(&rng), 0);
  22588. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22589. #if defined(WOLFSSL_ASYNC_CRYPT)
  22590. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22591. #endif
  22592. ExpectIntEQ(ret, 0);
  22593. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22594. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22595. !defined(HAVE_SELFTEST)
  22596. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22597. #endif
  22598. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22599. &secretLen), 0);
  22600. /* Pass in bad args. */
  22601. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22602. &secretLen), BAD_FUNC_ARG);
  22603. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22604. BAD_FUNC_ARG);
  22605. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22606. BAD_FUNC_ARG);
  22607. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22608. BAD_FUNC_ARG);
  22609. key.type = ECC_PUBLICKEY;
  22610. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22611. &secretLen), ECC_BAD_ARG_E);
  22612. PRIVATE_KEY_LOCK();
  22613. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22614. wc_ecc_free(&key);
  22615. wc_ecc_free(&key2);
  22616. #ifdef FP_ECC
  22617. wc_ecc_fp_free();
  22618. #endif
  22619. #endif
  22620. return EXPECT_RESULT();
  22621. } /* END test_wc_ecc_shared_secret_ssh */
  22622. /*
  22623. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22624. */
  22625. static int test_wc_ecc_verify_hash_ex(void)
  22626. {
  22627. EXPECT_DECLS;
  22628. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22629. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22630. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22631. ecc_key key;
  22632. WC_RNG rng;
  22633. int ret;
  22634. mp_int r;
  22635. mp_int s;
  22636. mp_int z;
  22637. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22638. unsigned char iHash[] = "Everyone gets Friday off.......";
  22639. unsigned char shortHash[] = TEST_STRING;
  22640. word32 hashlen = sizeof(hash);
  22641. word32 iHashLen = sizeof(iHash);
  22642. word32 shortHashLen = sizeof(shortHash);
  22643. int keySz = KEY32;
  22644. int verify_ok = 0;
  22645. XMEMSET(&key, 0, sizeof(ecc_key));
  22646. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22647. XMEMSET(&r, 0, sizeof(mp_int));
  22648. XMEMSET(&s, 0, sizeof(mp_int));
  22649. XMEMSET(&z, 0, sizeof(mp_int));
  22650. /* Initialize r, s and z. */
  22651. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22652. ExpectIntEQ(wc_ecc_init(&key), 0);
  22653. ExpectIntEQ(wc_InitRng(&rng), 0);
  22654. ret = wc_ecc_make_key(&rng, keySz, &key);
  22655. #if defined(WOLFSSL_ASYNC_CRYPT)
  22656. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22657. #endif
  22658. ExpectIntEQ(ret, 0);
  22659. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22660. /* verify_ok should be 1. */
  22661. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22662. 0);
  22663. ExpectIntEQ(verify_ok, 1);
  22664. /* verify_ok should be 0 */
  22665. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22666. &key), 0);
  22667. ExpectIntEQ(verify_ok, 0);
  22668. /* verify_ok should be 0. */
  22669. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22670. &verify_ok, &key), 0);
  22671. ExpectIntEQ(verify_ok, 0);
  22672. /* Test bad args. */
  22673. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22674. ECC_BAD_ARG_E);
  22675. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22676. ECC_BAD_ARG_E);
  22677. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22678. ECC_BAD_ARG_E);
  22679. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22680. ECC_BAD_ARG_E);
  22681. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22682. ECC_BAD_ARG_E);
  22683. /* Test bad args. */
  22684. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22685. &verify_ok, &key), ECC_BAD_ARG_E);
  22686. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22687. &verify_ok, &key), ECC_BAD_ARG_E);
  22688. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22689. &verify_ok, &key), MP_ZERO_E);
  22690. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22691. &verify_ok, &key), MP_ZERO_E);
  22692. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22693. &verify_ok, &key), MP_ZERO_E);
  22694. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22695. &key), ECC_BAD_ARG_E);
  22696. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22697. &key), ECC_BAD_ARG_E);
  22698. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22699. &verify_ok, NULL), ECC_BAD_ARG_E);
  22700. wc_ecc_free(&key);
  22701. mp_free(&r);
  22702. mp_free(&s);
  22703. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22704. #endif
  22705. return EXPECT_RESULT();
  22706. } /* END test_wc_ecc_verify_hash_ex */
  22707. /*
  22708. * Testing wc_ecc_mulmod()
  22709. */
  22710. static int test_wc_ecc_mulmod(void)
  22711. {
  22712. EXPECT_DECLS;
  22713. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22714. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22715. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22716. ecc_key key1;
  22717. ecc_key key2;
  22718. ecc_key key3;
  22719. WC_RNG rng;
  22720. int ret;
  22721. XMEMSET(&key1, 0, sizeof(ecc_key));
  22722. XMEMSET(&key2, 0, sizeof(ecc_key));
  22723. XMEMSET(&key3, 0, sizeof(ecc_key));
  22724. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22725. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22726. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22727. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22728. ExpectIntEQ(wc_InitRng(&rng), 0);
  22729. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22730. #if defined(WOLFSSL_ASYNC_CRYPT)
  22731. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22732. #endif
  22733. ExpectIntEQ(ret, 0);
  22734. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22735. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22736. key1.dp->Af, ECC_SECP256R1), 0);
  22737. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22738. key1.dp->prime, ECC_SECP256R1), 0);
  22739. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22740. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22741. 1), 0);
  22742. /* Test bad args. */
  22743. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22744. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22745. ECC_BAD_ARG_E);
  22746. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22747. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22748. ECC_BAD_ARG_E);
  22749. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22750. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22751. ECC_BAD_ARG_E);
  22752. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22753. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22754. wc_ecc_free(&key1);
  22755. wc_ecc_free(&key2);
  22756. wc_ecc_free(&key3);
  22757. #ifdef FP_ECC
  22758. wc_ecc_fp_free();
  22759. #endif
  22760. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22761. return EXPECT_RESULT();
  22762. } /* END test_wc_ecc_mulmod */
  22763. /*
  22764. * Testing wc_ecc_is_valid_idx()
  22765. */
  22766. static int test_wc_ecc_is_valid_idx(void)
  22767. {
  22768. EXPECT_DECLS;
  22769. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22770. ecc_key key;
  22771. WC_RNG rng;
  22772. int ret;
  22773. int iVal = -2;
  22774. int iVal2 = 3000;
  22775. XMEMSET(&key, 0, sizeof(ecc_key));
  22776. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22777. ExpectIntEQ(wc_ecc_init(&key), 0);
  22778. ExpectIntEQ(wc_InitRng(&rng), 0);
  22779. ret = wc_ecc_make_key(&rng, 32, &key);
  22780. #if defined(WOLFSSL_ASYNC_CRYPT)
  22781. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22782. #endif
  22783. ExpectIntEQ(ret, 0);
  22784. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22785. /* Test bad args. */
  22786. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22787. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22788. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22789. wc_ecc_free(&key);
  22790. #ifdef FP_ECC
  22791. wc_ecc_fp_free();
  22792. #endif
  22793. #endif
  22794. return EXPECT_RESULT();
  22795. } /* END test_wc_ecc_is_valid_idx */
  22796. /*
  22797. * Testing wc_ecc_get_curve_id_from_oid()
  22798. */
  22799. static int test_wc_ecc_get_curve_id_from_oid(void)
  22800. {
  22801. EXPECT_DECLS;
  22802. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22803. !defined(HAVE_FIPS)
  22804. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22805. word32 len = sizeof(oid);
  22806. /* Bad Cases */
  22807. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22808. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22809. /* Good Case */
  22810. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22811. #endif
  22812. return EXPECT_RESULT();
  22813. } /* END test_wc_ecc_get_curve_id_from_oid */
  22814. /*
  22815. * Testing wc_ecc_sig_size_calc()
  22816. */
  22817. static int test_wc_ecc_sig_size_calc(void)
  22818. {
  22819. EXPECT_DECLS;
  22820. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22821. ecc_key key;
  22822. WC_RNG rng;
  22823. int sz = 0;
  22824. int ret;
  22825. XMEMSET(&key, 0, sizeof(ecc_key));
  22826. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22827. ExpectIntEQ(wc_ecc_init(&key), 0);
  22828. ExpectIntEQ(wc_InitRng(&rng), 0);
  22829. ret = wc_ecc_make_key(&rng, 16, &key);
  22830. #if defined(WOLFSSL_ASYNC_CRYPT)
  22831. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22832. #endif
  22833. #if FIPS_VERSION3_GE(6,0,0)
  22834. ExpectIntEQ(ret, BAD_FUNC_ARG);
  22835. #else
  22836. ExpectIntEQ(ret, 0);
  22837. #endif
  22838. #if FIPS_VERSION3_LT(6,0,0)
  22839. sz = key.dp->size;
  22840. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22841. #else
  22842. (void) sz;
  22843. #endif
  22844. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22845. wc_ecc_free(&key);
  22846. #endif
  22847. return EXPECT_RESULT();
  22848. } /* END test_wc_ecc_sig_size_calc */
  22849. /*
  22850. * Testing wc_ecc_sm2_make_key()
  22851. */
  22852. static int test_wc_ecc_sm2_make_key(void)
  22853. {
  22854. int res = TEST_SKIPPED;
  22855. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22856. EXPECT_DECLS;
  22857. WC_RNG rng[1];
  22858. ecc_key key[1];
  22859. XMEMSET(rng, 0, sizeof(*rng));
  22860. XMEMSET(key, 0, sizeof(*key));
  22861. ExpectIntEQ(wc_InitRng(rng), 0);
  22862. ExpectIntEQ(wc_ecc_init(key), 0);
  22863. /* Test invalid parameters. */
  22864. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22865. BAD_FUNC_ARG);
  22866. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22867. BAD_FUNC_ARG);
  22868. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22869. BAD_FUNC_ARG);
  22870. /* Test valid parameters. */
  22871. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22872. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22873. wc_ecc_free(key);
  22874. wc_FreeRng(rng);
  22875. #ifdef FP_ECC
  22876. wc_ecc_fp_free();
  22877. #endif
  22878. res = EXPECT_RESULT();
  22879. #endif
  22880. return res;
  22881. }
  22882. /*
  22883. * Testing wc_ecc_sm2_shared_secret()
  22884. */
  22885. static int test_wc_ecc_sm2_shared_secret(void)
  22886. {
  22887. int res = TEST_SKIPPED;
  22888. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22889. EXPECT_DECLS;
  22890. WC_RNG rng[1];
  22891. ecc_key keyA[1];
  22892. ecc_key keyB[1];
  22893. byte outA[32];
  22894. byte outB[32];
  22895. word32 outALen = 32;
  22896. word32 outBLen = 32;
  22897. XMEMSET(rng, 0, sizeof(*rng));
  22898. XMEMSET(keyA, 0, sizeof(*keyA));
  22899. XMEMSET(keyB, 0, sizeof(*keyB));
  22900. ExpectIntEQ(wc_InitRng(rng), 0);
  22901. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22902. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22903. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22904. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22905. #ifdef ECC_TIMING_RESISTANT
  22906. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22907. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22908. #endif
  22909. /* Test invalid parameters. */
  22910. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22911. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22912. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22913. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22914. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22915. BAD_FUNC_ARG);
  22916. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22917. BAD_FUNC_ARG);
  22918. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22919. BAD_FUNC_ARG);
  22920. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22921. BAD_FUNC_ARG);
  22922. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22923. /* Test valid parameters. */
  22924. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22925. ExpectIntLE(outALen, 32);
  22926. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22927. ExpectIntLE(outBLen, 32);
  22928. ExpectIntEQ(outALen, outBLen);
  22929. ExpectBufEQ(outA, outB, outALen);
  22930. wc_ecc_free(keyB);
  22931. wc_ecc_free(keyA);
  22932. wc_FreeRng(rng);
  22933. #ifdef FP_ECC
  22934. wc_ecc_fp_free();
  22935. #endif
  22936. res = EXPECT_RESULT();
  22937. #endif
  22938. return res;
  22939. }
  22940. /*
  22941. * Testing wc_ecc_sm2_create_digest()
  22942. */
  22943. static int test_wc_ecc_sm2_create_digest(void)
  22944. {
  22945. int res = TEST_SKIPPED;
  22946. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22947. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22948. EXPECT_DECLS;
  22949. ecc_key key[1];
  22950. enum wc_HashType hashType;
  22951. unsigned char pub[] = {
  22952. 0x04,
  22953. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22954. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22955. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22956. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22957. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22958. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22959. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22960. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22961. };
  22962. unsigned char id[] = {
  22963. 0x01, 0x02, 0x03,
  22964. };
  22965. unsigned char msg[] = {
  22966. 0x01, 0x02, 0x03,
  22967. };
  22968. unsigned char hash[32];
  22969. #ifdef WOLFSSL_SM3
  22970. unsigned char expHash[32] = {
  22971. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22972. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22973. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22974. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22975. };
  22976. #else
  22977. unsigned char expHash[32] = {
  22978. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22979. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22980. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22981. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22982. };
  22983. #endif
  22984. #ifdef WOLFSSL_SM3
  22985. hashType = WC_HASH_TYPE_SM3;
  22986. #else
  22987. hashType = WC_HASH_TYPE_SHA256;
  22988. #endif
  22989. XMEMSET(key, 0, sizeof(*key));
  22990. ExpectIntEQ(wc_ecc_init(key), 0);
  22991. /* Test with no curve set. */
  22992. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22993. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22994. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22995. /* Test invalid parameters. */
  22996. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22997. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22998. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22999. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  23000. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  23001. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  23002. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  23003. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  23004. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  23005. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  23006. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  23007. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  23008. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  23009. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  23010. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23011. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  23012. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23013. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  23014. /* Bad hash type. */
  23015. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  23016. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23017. -1, hash, 0, key), BAD_FUNC_ARG);
  23018. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  23019. /* Bad hash size. */
  23020. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23021. hashType, hash, 0, key), BUFFER_E);
  23022. /* Test valid parameters. */
  23023. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23024. hashType, hash, sizeof(hash), key), 0);
  23025. ExpectBufEQ(hash, expHash, sizeof(expHash));
  23026. wc_ecc_free(key);
  23027. res = EXPECT_RESULT();
  23028. #endif
  23029. return res;
  23030. }
  23031. /*
  23032. * Testing wc_ecc_sm2_verify_hash_ex()
  23033. */
  23034. static int test_wc_ecc_sm2_verify_hash_ex(void)
  23035. {
  23036. int res = TEST_SKIPPED;
  23037. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  23038. defined(WOLFSSL_PUBLIC_MP)
  23039. EXPECT_DECLS;
  23040. ecc_key key[1];
  23041. mp_int r[1];
  23042. mp_int s[1];
  23043. int verified;
  23044. unsigned char pub[] = {
  23045. 0x04,
  23046. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  23047. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23048. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23049. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23050. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23051. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23052. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23053. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23054. };
  23055. unsigned char hash[] = {
  23056. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23057. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23058. 0x50, 0x69, 0x5B, 0x20
  23059. };
  23060. unsigned char rData[] = {
  23061. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  23062. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  23063. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  23064. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  23065. };
  23066. unsigned char sData[] = {
  23067. 0x1D,
  23068. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23069. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23070. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23071. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23072. };
  23073. unsigned char rBadData[] = {
  23074. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  23075. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  23076. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  23077. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  23078. };
  23079. XMEMSET(key, 0, sizeof(*key));
  23080. XMEMSET(r, 0, sizeof(*r));
  23081. XMEMSET(s, 0, sizeof(*s));
  23082. ExpectIntEQ(mp_init(r), 0);
  23083. ExpectIntEQ(mp_init(s), 0);
  23084. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  23085. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  23086. ExpectIntEQ(wc_ecc_init(key), 0);
  23087. /* Test with no curve set. */
  23088. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23089. &verified, key), BAD_FUNC_ARG);
  23090. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23091. /* Test invalid parameters. */
  23092. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23093. NULL, NULL), BAD_FUNC_ARG);
  23094. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  23095. NULL, NULL), BAD_FUNC_ARG);
  23096. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  23097. NULL, NULL), BAD_FUNC_ARG);
  23098. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  23099. NULL, NULL), BAD_FUNC_ARG);
  23100. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23101. &verified, NULL), BAD_FUNC_ARG);
  23102. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23103. NULL, key), BAD_FUNC_ARG);
  23104. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  23105. &verified, key), BAD_FUNC_ARG);
  23106. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  23107. &verified, key), BAD_FUNC_ARG);
  23108. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  23109. &verified, key), BAD_FUNC_ARG);
  23110. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23111. NULL, key), BAD_FUNC_ARG);
  23112. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23113. &verified, NULL), BAD_FUNC_ARG);
  23114. /* Make key not on the SM2 curve. */
  23115. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23116. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23117. &verified, key), BAD_FUNC_ARG);
  23118. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23119. /* Test valid parameters. */
  23120. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23121. &verified, key), 0);
  23122. ExpectIntEQ(verified, 1);
  23123. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  23124. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23125. &verified, key), 0);
  23126. ExpectIntEQ(verified, 0);
  23127. mp_free(s);
  23128. mp_free(r);
  23129. wc_ecc_free(key);
  23130. #ifdef FP_ECC
  23131. wc_ecc_fp_free();
  23132. #endif
  23133. res = EXPECT_RESULT();
  23134. #endif
  23135. return res;
  23136. }
  23137. /*
  23138. * Testing wc_ecc_sm2_verify_hash()
  23139. */
  23140. static int test_wc_ecc_sm2_verify_hash(void)
  23141. {
  23142. int res = TEST_SKIPPED;
  23143. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  23144. EXPECT_DECLS;
  23145. ecc_key key[1];
  23146. int verified;
  23147. unsigned char pub[] = {
  23148. 0x04,
  23149. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  23150. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23151. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23152. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23153. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23154. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23155. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23156. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23157. };
  23158. unsigned char hash[] = {
  23159. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23160. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23161. 0x50, 0x69, 0x5B, 0x20
  23162. };
  23163. unsigned char sig[] = {
  23164. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23165. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23166. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23167. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23168. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23169. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23170. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23171. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23172. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23173. };
  23174. unsigned char sigBad[] = {
  23175. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23176. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23177. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23178. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23179. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23180. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23181. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23182. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23183. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  23184. };
  23185. XMEMSET(key, 0, sizeof(*key));
  23186. ExpectIntEQ(wc_ecc_init(key), 0);
  23187. /* Test with no curve set. */
  23188. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23189. &verified, key), BAD_FUNC_ARG);
  23190. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23191. /* Test invalid parameters. */
  23192. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23193. NULL, NULL), BAD_FUNC_ARG);
  23194. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23195. NULL, NULL), BAD_FUNC_ARG);
  23196. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23197. NULL, NULL), BAD_FUNC_ARG);
  23198. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23199. &verified, NULL), BAD_FUNC_ARG);
  23200. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23201. NULL, key), BAD_FUNC_ARG);
  23202. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23203. &verified, key), BAD_FUNC_ARG);
  23204. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23205. &verified, key), BAD_FUNC_ARG);
  23206. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23207. NULL, key), BAD_FUNC_ARG);
  23208. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23209. &verified, NULL), BAD_FUNC_ARG);
  23210. /* Make key not on the SM2 curve. */
  23211. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23212. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23213. &verified, key), BAD_FUNC_ARG);
  23214. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23215. /* Test valid parameters. */
  23216. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23217. &verified, key), 0);
  23218. ExpectIntEQ(verified, 1);
  23219. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  23220. sizeof(hash), &verified, key), 0);
  23221. ExpectIntEQ(verified, 0);
  23222. wc_ecc_free(key);
  23223. #ifdef FP_ECC
  23224. wc_ecc_fp_free();
  23225. #endif
  23226. res = EXPECT_RESULT();
  23227. #endif
  23228. return res;
  23229. }
  23230. /*
  23231. * Testing wc_ecc_sm2_verify_hash_ex()
  23232. */
  23233. static int test_wc_ecc_sm2_sign_hash_ex(void)
  23234. {
  23235. int res = TEST_SKIPPED;
  23236. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  23237. defined(WOLFSSL_PUBLIC_MP)
  23238. EXPECT_DECLS;
  23239. WC_RNG rng[1];
  23240. ecc_key key[1];
  23241. mp_int r[1];
  23242. mp_int s[1];
  23243. unsigned char hash[32];
  23244. #ifdef HAVE_ECC_VERIFY
  23245. int verified;
  23246. #endif
  23247. XMEMSET(rng, 0, sizeof(*rng));
  23248. XMEMSET(key, 0, sizeof(*key));
  23249. XMEMSET(r, 0, sizeof(*r));
  23250. XMEMSET(s, 0, sizeof(*s));
  23251. ExpectIntEQ(wc_InitRng(rng), 0);
  23252. ExpectIntEQ(mp_init(r), 0);
  23253. ExpectIntEQ(mp_init(s), 0);
  23254. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23255. ExpectIntEQ(wc_ecc_init(key), 0);
  23256. /* Test with no curve set. */
  23257. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23258. BAD_FUNC_ARG);
  23259. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23260. /* Test invalid parameters. */
  23261. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23262. NULL), BAD_FUNC_ARG);
  23263. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  23264. NULL), BAD_FUNC_ARG);
  23265. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  23266. NULL), BAD_FUNC_ARG);
  23267. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  23268. NULL), BAD_FUNC_ARG);
  23269. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  23270. NULL), BAD_FUNC_ARG);
  23271. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23272. s), BAD_FUNC_ARG);
  23273. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  23274. BAD_FUNC_ARG);
  23275. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  23276. BAD_FUNC_ARG);
  23277. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  23278. BAD_FUNC_ARG);
  23279. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  23280. BAD_FUNC_ARG);
  23281. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  23282. BAD_FUNC_ARG);
  23283. /* Make key not on the SM2 curve. */
  23284. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23285. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23286. BAD_FUNC_ARG);
  23287. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23288. #ifdef WOLFSSL_SP_MATH_ALL
  23289. {
  23290. mp_int smallR[1];
  23291. sp_init_size(smallR, 1);
  23292. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  23293. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  23294. smallR, s), 0);
  23295. }
  23296. #endif
  23297. /* Test valid parameters. */
  23298. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23299. 0);
  23300. #ifdef HAVE_ECC_VERIFY
  23301. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  23302. key), 0);
  23303. ExpectIntEQ(verified, 1);
  23304. #endif
  23305. mp_free(s);
  23306. mp_free(r);
  23307. wc_ecc_free(key);
  23308. wc_FreeRng(rng);
  23309. #ifdef FP_ECC
  23310. wc_ecc_fp_free();
  23311. #endif
  23312. res = EXPECT_RESULT();
  23313. #endif
  23314. return res;
  23315. }
  23316. /*
  23317. * Testing wc_ecc_sm2_verify_hash()
  23318. */
  23319. static int test_wc_ecc_sm2_sign_hash(void)
  23320. {
  23321. int res = TEST_SKIPPED;
  23322. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  23323. EXPECT_DECLS;
  23324. WC_RNG rng[1];
  23325. ecc_key key[1];
  23326. unsigned char hash[32];
  23327. unsigned char sig[72];
  23328. word32 sigSz = sizeof(sig);
  23329. #ifdef HAVE_ECC_VERIFY
  23330. int verified;
  23331. #endif
  23332. XMEMSET(rng, 0, sizeof(*rng));
  23333. XMEMSET(key, 0, sizeof(*key));
  23334. ExpectIntEQ(wc_InitRng(rng), 0);
  23335. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23336. ExpectIntEQ(wc_ecc_init(key), 0);
  23337. /* Test with no curve set. */
  23338. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23339. BAD_FUNC_ARG);
  23340. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23341. /* Test invalid parameters. */
  23342. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23343. NULL), BAD_FUNC_ARG);
  23344. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  23345. NULL), BAD_FUNC_ARG);
  23346. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  23347. NULL), BAD_FUNC_ARG);
  23348. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  23349. NULL), BAD_FUNC_ARG);
  23350. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  23351. NULL), BAD_FUNC_ARG);
  23352. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23353. key), BAD_FUNC_ARG);
  23354. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  23355. key), BAD_FUNC_ARG);
  23356. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  23357. key), BAD_FUNC_ARG);
  23358. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  23359. key), BAD_FUNC_ARG);
  23360. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  23361. key), BAD_FUNC_ARG);
  23362. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  23363. NULL), BAD_FUNC_ARG);
  23364. /* Make key not on the SM2 curve. */
  23365. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23366. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23367. BAD_FUNC_ARG);
  23368. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23369. /* Test valid parameters. */
  23370. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23371. 0);
  23372. #ifdef HAVE_ECC_VERIFY
  23373. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  23374. &verified, key), 0);
  23375. ExpectIntEQ(verified, 1);
  23376. #endif
  23377. wc_ecc_free(key);
  23378. wc_FreeRng(rng);
  23379. #ifdef FP_ECC
  23380. wc_ecc_fp_free();
  23381. #endif
  23382. res = EXPECT_RESULT();
  23383. #endif
  23384. return res;
  23385. }
  23386. /*
  23387. * Testing ToTraditional
  23388. */
  23389. static int test_ToTraditional(void)
  23390. {
  23391. EXPECT_DECLS;
  23392. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23393. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23394. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  23395. XFILE f = XBADFILE;
  23396. byte input[TWOK_BUF];
  23397. word32 sz = 0;
  23398. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  23399. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  23400. if (f != XBADFILE)
  23401. XFCLOSE(f);
  23402. /* Good case */
  23403. ExpectIntGT(ToTraditional(input, sz), 0);
  23404. /* Bad cases */
  23405. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  23406. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  23407. #ifdef WOLFSSL_ASN_TEMPLATE
  23408. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  23409. #else
  23410. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  23411. #endif
  23412. #endif
  23413. return EXPECT_RESULT();
  23414. } /* End test_ToTraditional*/
  23415. /*
  23416. * Testing wc_EccPrivateKeyToDer
  23417. */
  23418. static int test_wc_EccPrivateKeyToDer(void)
  23419. {
  23420. EXPECT_DECLS;
  23421. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23422. byte output[ONEK_BUF];
  23423. ecc_key eccKey;
  23424. WC_RNG rng;
  23425. word32 inLen;
  23426. word32 outLen = 0;
  23427. int ret;
  23428. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23429. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23430. PRIVATE_KEY_UNLOCK();
  23431. ExpectIntEQ(wc_InitRng(&rng), 0);
  23432. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23433. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23434. #if defined(WOLFSSL_ASYNC_CRYPT)
  23435. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23436. #endif
  23437. ExpectIntEQ(ret, 0);
  23438. inLen = (word32)sizeof(output);
  23439. /* Bad Cases */
  23440. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23441. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23442. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  23443. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  23444. /* Good Case */
  23445. ExpectIntGT(outLen = (word32)wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  23446. wc_ecc_free(&eccKey);
  23447. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23448. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23449. {
  23450. /* test importing private only into a PKEY struct */
  23451. EC_KEY* ec = NULL;
  23452. EVP_PKEY* pkey = NULL;
  23453. const unsigned char* der;
  23454. der = output;
  23455. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  23456. der = output;
  23457. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  23458. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23459. if (EXPECT_FAIL()) {
  23460. EC_KEY_free(ec);
  23461. }
  23462. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23463. }
  23464. #endif
  23465. PRIVATE_KEY_LOCK();
  23466. #endif
  23467. return EXPECT_RESULT();
  23468. } /* End test_wc_EccPrivateKeyToDer*/
  23469. /*
  23470. * Testing wc_DhPublicKeyDecode
  23471. */
  23472. static int test_wc_DhPublicKeyDecode(void)
  23473. {
  23474. EXPECT_DECLS;
  23475. #ifndef NO_DH
  23476. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23477. DhKey key;
  23478. word32 inOutIdx;
  23479. XMEMSET(&key, 0, sizeof(DhKey));
  23480. ExpectIntEQ(wc_InitDhKey(&key), 0);
  23481. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  23482. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23483. BAD_FUNC_ARG);
  23484. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23485. BAD_FUNC_ARG);
  23486. inOutIdx = 0;
  23487. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23488. BAD_FUNC_ARG);
  23489. inOutIdx = 0;
  23490. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23491. BAD_FUNC_ARG);
  23492. inOutIdx = 0;
  23493. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23494. sizeof_dh_pub_key_der_2048), 0);
  23495. ExpectIntNE(key.p.used, 0);
  23496. ExpectIntNE(key.g.used, 0);
  23497. ExpectIntEQ(key.q.used, 0);
  23498. ExpectIntNE(key.pub.used, 0);
  23499. ExpectIntEQ(key.priv.used, 0);
  23500. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  23501. #endif
  23502. #endif /* !NO_DH */
  23503. return EXPECT_RESULT();
  23504. }
  23505. /*
  23506. * Testing wc_Ed25519KeyToDer
  23507. */
  23508. static int test_wc_Ed25519KeyToDer(void)
  23509. {
  23510. EXPECT_DECLS;
  23511. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23512. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23513. byte output[ONEK_BUF];
  23514. ed25519_key ed25519Key;
  23515. WC_RNG rng;
  23516. word32 inLen;
  23517. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23518. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23519. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23520. ExpectIntEQ(wc_InitRng(&rng), 0);
  23521. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23522. inLen = (word32)sizeof(output);
  23523. /* Bad Cases */
  23524. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23525. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23526. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  23527. /* Good Cases */
  23528. /* length only */
  23529. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  23530. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  23531. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23532. wc_ed25519_free(&ed25519Key);
  23533. #endif
  23534. return EXPECT_RESULT();
  23535. } /* End test_wc_Ed25519KeyToDer*/
  23536. /*
  23537. * Testing wc_Ed25519PrivateKeyToDer
  23538. */
  23539. static int test_wc_Ed25519PrivateKeyToDer(void)
  23540. {
  23541. EXPECT_DECLS;
  23542. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23543. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23544. byte output[ONEK_BUF];
  23545. ed25519_key ed25519PrivKey;
  23546. WC_RNG rng;
  23547. word32 inLen;
  23548. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  23549. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23550. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  23551. ExpectIntEQ(wc_InitRng(&rng), 0);
  23552. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  23553. 0);
  23554. inLen = (word32)sizeof(output);
  23555. /* Bad Cases */
  23556. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23557. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23558. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  23559. BAD_FUNC_ARG);
  23560. /* Good Cases */
  23561. /* length only */
  23562. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  23563. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  23564. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23565. wc_ed25519_free(&ed25519PrivKey);
  23566. #endif
  23567. return EXPECT_RESULT();
  23568. } /* End test_wc_Ed25519PrivateKeyToDer*/
  23569. /*
  23570. * Testing wc_Ed448KeyToDer
  23571. */
  23572. static int test_wc_Ed448KeyToDer(void)
  23573. {
  23574. EXPECT_DECLS;
  23575. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23576. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23577. byte output[ONEK_BUF];
  23578. ed448_key ed448Key;
  23579. WC_RNG rng;
  23580. word32 inLen;
  23581. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23582. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23583. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23584. ExpectIntEQ(wc_InitRng(&rng), 0);
  23585. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23586. inLen = (word32)sizeof(output);
  23587. /* Bad Cases */
  23588. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23589. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23590. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  23591. /* Good Cases */
  23592. /* length only */
  23593. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  23594. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  23595. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23596. wc_ed448_free(&ed448Key);
  23597. #endif
  23598. return EXPECT_RESULT();
  23599. } /* End test_wc_Ed448KeyToDer*/
  23600. /*
  23601. * Testing wc_Ed448PrivateKeyToDer
  23602. */
  23603. static int test_wc_Ed448PrivateKeyToDer(void)
  23604. {
  23605. EXPECT_DECLS;
  23606. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23607. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23608. byte output[ONEK_BUF];
  23609. ed448_key ed448PrivKey;
  23610. WC_RNG rng;
  23611. word32 inLen;
  23612. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23613. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23614. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23615. ExpectIntEQ(wc_InitRng(&rng), 0);
  23616. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23617. 0);
  23618. inLen = (word32)sizeof(output);
  23619. /* Bad Cases */
  23620. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23621. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23622. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23623. BAD_FUNC_ARG);
  23624. /* Good cases */
  23625. /* length only */
  23626. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23627. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23628. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23629. wc_ed448_free(&ed448PrivKey);
  23630. #endif
  23631. return EXPECT_RESULT();
  23632. } /* End test_wc_Ed448PrivateKeyToDer*/
  23633. /*
  23634. * Testing wc_Curve448PrivateKeyToDer
  23635. */
  23636. static int test_wc_Curve448PrivateKeyToDer(void)
  23637. {
  23638. EXPECT_DECLS;
  23639. #if defined(HAVE_CURVE448) && defined(HAVE_CURVE448_KEY_EXPORT) && \
  23640. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23641. byte output[ONEK_BUF];
  23642. curve448_key curve448PrivKey;
  23643. WC_RNG rng;
  23644. word32 inLen;
  23645. XMEMSET(&curve448PrivKey, 0, sizeof(curve448PrivKey));
  23646. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23647. ExpectIntEQ(wc_curve448_init(&curve448PrivKey), 0);
  23648. ExpectIntEQ(wc_InitRng(&rng), 0);
  23649. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &curve448PrivKey),
  23650. 0);
  23651. inLen = (word32)sizeof(output);
  23652. /* Bad Cases */
  23653. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23654. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23655. ExpectIntEQ(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, 0),
  23656. BAD_FUNC_ARG);
  23657. /* Good cases */
  23658. /* length only */
  23659. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, inLen), 0);
  23660. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, inLen), 0);
  23661. /* Bad Cases */
  23662. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  23663. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, output, inLen, 0), BAD_FUNC_ARG);
  23664. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 0),
  23665. BUFFER_E);
  23666. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 1),
  23667. BUFFER_E);
  23668. /* Good cases */
  23669. /* length only */
  23670. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 0), 0);
  23671. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 1), 0);
  23672. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 0), 0);
  23673. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 1), 0);
  23674. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23675. wc_curve448_free(&curve448PrivKey);
  23676. #endif
  23677. return EXPECT_RESULT();
  23678. } /* End wc_Curve448PrivateKeyToDer*/
  23679. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  23680. !defined(WOLFSSL_DILITHIUM_NO_VERIFY) && !defined(WOLFSSL_NO_ML_DSA_44)
  23681. static const byte ml_dsa_44_pub_key[] = {
  23682. 0xf1, 0xdf, 0x1e, 0xfc, 0x6b, 0x41, 0xe7,
  23683. 0x5e, 0xcb, 0xb5, 0xb5, 0xd2, 0x3c, 0xc8,
  23684. 0xd3, 0x99, 0x73, 0x36, 0x9a, 0x0b, 0x32,
  23685. 0x71, 0x7a, 0x9f, 0x6d, 0x66, 0x07, 0xb8,
  23686. 0x31, 0x5f, 0x25, 0xb6, 0x2e, 0xee, 0x4f,
  23687. 0x63, 0x13, 0x02, 0x45, 0x3c, 0xd1, 0x3d,
  23688. 0x79, 0x6b, 0x3c, 0xfe, 0xd9, 0x2f, 0x39,
  23689. 0xe8, 0x62, 0x60, 0xf0, 0x04, 0x83, 0x28,
  23690. 0xaa, 0xdc, 0x15, 0x90, 0xef, 0x55, 0x48,
  23691. 0xf9, 0xd2, 0xcd, 0x53, 0x87, 0x0e, 0x42,
  23692. 0xba, 0x16, 0x87, 0x7b, 0x32, 0xa8, 0xbf,
  23693. 0xed, 0x32, 0xa1, 0x19, 0x66, 0x44, 0xfe,
  23694. 0x57, 0xec, 0x26, 0xed, 0x9e, 0x73, 0xa0,
  23695. 0x87, 0xe8, 0x8a, 0x93, 0x3c, 0xec, 0x1d,
  23696. 0xa0, 0xcc, 0x2e, 0x0d, 0x37, 0x5b, 0xb1,
  23697. 0x74, 0x77, 0x18, 0x4b, 0xde, 0x4b, 0xc9,
  23698. 0xac, 0xf8, 0xda, 0x23, 0x7a, 0x2a, 0x39,
  23699. 0xfa, 0x96, 0x01, 0xff, 0xf0, 0xc7, 0xa7,
  23700. 0x34, 0xca, 0x9d, 0xe9, 0xda, 0x4d, 0x85,
  23701. 0x00, 0xc9, 0xe9, 0xcf, 0xb0, 0x3e, 0x21,
  23702. 0xe6, 0xae, 0x52, 0x67, 0x4f, 0xe3, 0x93,
  23703. 0x2f, 0x50, 0x47, 0xdd, 0x89, 0xa2, 0x48,
  23704. 0xf8, 0xfe, 0x93, 0xfe, 0xce, 0x68, 0x9c,
  23705. 0xe9, 0x4d, 0xdd, 0xbd, 0x9f, 0xeb, 0x14,
  23706. 0x8d, 0x38, 0x7a, 0xc6, 0xf2, 0x50, 0x00,
  23707. 0x91, 0x65, 0xd0, 0xd1, 0xeb, 0x51, 0xab,
  23708. 0x3a, 0x0e, 0x45, 0x5c, 0xbd, 0x65, 0xf5,
  23709. 0x78, 0xc6, 0xa0, 0xaa, 0xae, 0x50, 0xf2,
  23710. 0x19, 0x1f, 0x90, 0x1a, 0x9f, 0x34, 0xa0,
  23711. 0xa1, 0x95, 0x94, 0x86, 0x30, 0xc2, 0xb2,
  23712. 0x95, 0x82, 0x13, 0xf6, 0x73, 0xe2, 0x03,
  23713. 0xe3, 0x7c, 0x09, 0x8e, 0x5d, 0x07, 0xd6,
  23714. 0x33, 0x93, 0x8a, 0x1b, 0x67, 0xc9, 0xb1,
  23715. 0x76, 0x74, 0x1c, 0x22, 0x58, 0x05, 0x5a,
  23716. 0xa8, 0x83, 0x68, 0xce, 0x64, 0xfc, 0x52,
  23717. 0x7f, 0x35, 0x80, 0x6e, 0xdf, 0xf5, 0x2d,
  23718. 0xd2, 0xd1, 0x17, 0xdc, 0xce, 0x95, 0xe8,
  23719. 0xe6, 0x42, 0xb1, 0xb1, 0x61, 0xc1, 0x24,
  23720. 0x79, 0x1c, 0x51, 0xfc, 0x3c, 0xba, 0x40,
  23721. 0xf7, 0x70, 0x35, 0x22, 0x73, 0x31, 0x53,
  23722. 0x21, 0xea, 0x09, 0xf7, 0xaa, 0x07, 0xb8,
  23723. 0xfa, 0x0b, 0xa0, 0xa9, 0xb4, 0x8c, 0x83,
  23724. 0xbb, 0x25, 0xfe, 0x39, 0x29, 0xef, 0x34,
  23725. 0xd5, 0xe2, 0xc7, 0x9e, 0x87, 0xbd, 0x50,
  23726. 0x86, 0x71, 0x12, 0x3e, 0x8a, 0x78, 0xe2,
  23727. 0xb3, 0xe1, 0xfa, 0x5b, 0x73, 0x3b, 0x34,
  23728. 0x9f, 0x4e, 0x7d, 0xd5, 0x1b, 0xb9, 0x8e,
  23729. 0x43, 0x76, 0xef, 0x3e, 0x37, 0x70, 0x33,
  23730. 0x36, 0xd1, 0xa1, 0xba, 0x1b, 0xb1, 0x79,
  23731. 0xfb, 0x2c, 0xb1, 0x9b, 0xc3, 0x1b, 0x26,
  23732. 0x83, 0x89, 0x4d, 0x53, 0x40, 0xa5, 0xf9,
  23733. 0x8b, 0xe2, 0xec, 0x30, 0x1f, 0xf6, 0x16,
  23734. 0xd6, 0x55, 0xce, 0x0e, 0x1b, 0xed, 0xe0,
  23735. 0xeb, 0xc9, 0x7a, 0x2e, 0x1a, 0x85, 0x81,
  23736. 0xa4, 0xe2, 0xa8, 0xbe, 0x9f, 0xac, 0x0b,
  23737. 0x23, 0xb4, 0xbb, 0xc2, 0x0f, 0x66, 0x43,
  23738. 0x45, 0x93, 0x20, 0x37, 0x4d, 0x47, 0x23,
  23739. 0x7f, 0x4a, 0x5e, 0x8b, 0x19, 0xec, 0xd9,
  23740. 0x57, 0x69, 0xc4, 0x91, 0xb0, 0xcd, 0x25,
  23741. 0x2a, 0x7d, 0x52, 0xdb, 0x59, 0x18, 0x8b,
  23742. 0x96, 0xad, 0x75, 0x21, 0x81, 0x1a, 0x2c,
  23743. 0xb3, 0x26, 0x30, 0x78, 0x19, 0x2b, 0x22,
  23744. 0x74, 0x6e, 0x92, 0x57, 0xec, 0x3c, 0x75,
  23745. 0x8b, 0xd8, 0x4b, 0x7c, 0xd1, 0x72, 0x1b,
  23746. 0x1f, 0xed, 0xae, 0x15, 0x82, 0xd3, 0xf6,
  23747. 0xaf, 0x01, 0x31, 0xec, 0x1b, 0xca, 0xa5,
  23748. 0xf8, 0x78, 0x7f, 0x8a, 0x8a, 0x03, 0xbd,
  23749. 0x03, 0x0a, 0xc5, 0x4e, 0x15, 0xab, 0xa4,
  23750. 0x76, 0x56, 0x5b, 0xf8, 0x50, 0xa9, 0xee,
  23751. 0x61, 0xbd, 0x05, 0xe0, 0xdf, 0xc6, 0xbe,
  23752. 0x4a, 0xaf, 0xdb, 0x96, 0x0a, 0x7e, 0xcb,
  23753. 0x2e, 0xb0, 0x68, 0x4e, 0x2d, 0x88, 0x32,
  23754. 0x1e, 0xe1, 0xbc, 0x08, 0x15, 0x15, 0x71,
  23755. 0xe6, 0x77, 0x2b, 0xeb, 0x47, 0x81, 0xb7,
  23756. 0xe8, 0x82, 0x9f, 0x5f, 0x94, 0xd2, 0xac,
  23757. 0xa5, 0x89, 0x52, 0xe1, 0x3c, 0x59, 0xe0,
  23758. 0x06, 0xe6, 0x66, 0xe1, 0xf9, 0x9d, 0x32,
  23759. 0x42, 0x9d, 0x77, 0xfe, 0x6a, 0x12, 0x4a,
  23760. 0xa3, 0xd2, 0x49, 0xbb, 0x39, 0xad, 0x42,
  23761. 0xb7, 0x37, 0xfb, 0xde, 0x9d, 0xaf, 0x1b,
  23762. 0xd5, 0x5a, 0x3b, 0x06, 0xa6, 0x51, 0x7d,
  23763. 0x6a, 0x5c, 0x32, 0xdb, 0xde, 0x5d, 0x0d,
  23764. 0x20, 0x88, 0xee, 0x8b, 0xa8, 0x49, 0x5b,
  23765. 0x6c, 0x50, 0x72, 0xdb, 0x68, 0x44, 0x17,
  23766. 0x28, 0xd4, 0xbb, 0x43, 0x8e, 0x00, 0xa5,
  23767. 0xc8, 0x27, 0x00, 0xaa, 0x2b, 0xa4, 0xc2,
  23768. 0x16, 0xcd, 0x2d, 0x59, 0xdc, 0x1a, 0xa2,
  23769. 0x66, 0xe2, 0x96, 0x6b, 0xcc, 0x39, 0xc6,
  23770. 0xe9, 0x2b, 0x14, 0xa7, 0x7d, 0x67, 0x5d,
  23771. 0x54, 0xfc, 0x93, 0x73, 0x52, 0x47, 0xc7,
  23772. 0x24, 0x1e, 0x7e, 0xc9, 0x2d, 0x87, 0x60,
  23773. 0xd3, 0xd8, 0x76, 0xf0, 0x51, 0x04, 0xc7,
  23774. 0xcb, 0x68, 0x0f, 0xd8, 0x4b, 0x22, 0xb2,
  23775. 0x51, 0x87, 0xe9, 0x1e, 0x05, 0x3d, 0xe2,
  23776. 0x8a, 0x6b, 0xb8, 0x96, 0xd6, 0xe0, 0x6e,
  23777. 0x38, 0x74, 0x96, 0xad, 0x7e, 0x4f, 0x52,
  23778. 0x35, 0xcf, 0x4a, 0x50, 0xe7, 0x60, 0x2e,
  23779. 0x58, 0xcf, 0xdc, 0x7a, 0x9a, 0x21, 0x76,
  23780. 0x1d, 0x2c, 0xd1, 0x98, 0xab, 0xab, 0xed,
  23781. 0xf9, 0xec, 0xd5, 0x7b, 0x09, 0xad, 0x2e,
  23782. 0xad, 0x5a, 0xdc, 0xad, 0xd6, 0x46, 0xba,
  23783. 0x2d, 0x55, 0xf7, 0x0c, 0x9a, 0x23, 0x10,
  23784. 0x50, 0x3e, 0x4f, 0xe1, 0xeb, 0x58, 0x8a,
  23785. 0xc0, 0x17, 0x48, 0x41, 0x40, 0x65, 0x0b,
  23786. 0xfb, 0x43, 0x9e, 0xf0, 0x37, 0x4a, 0x89,
  23787. 0x4e, 0x71, 0xad, 0x44, 0x19, 0x13, 0xbb,
  23788. 0x4a, 0x63, 0x83, 0x9e, 0x6a, 0x49, 0x1b,
  23789. 0x28, 0xb0, 0x8e, 0x9c, 0x7b, 0xaf, 0xf9,
  23790. 0x57, 0x5d, 0x35, 0x16, 0x5c, 0xa7, 0x5e,
  23791. 0xd1, 0x0d, 0x83, 0xdc, 0x49, 0xdd, 0x40,
  23792. 0x58, 0x9c, 0x97, 0x91, 0xa6, 0xb0, 0x68,
  23793. 0xb0, 0xfa, 0x9e, 0xc0, 0x3f, 0x81, 0xc6,
  23794. 0xce, 0x58, 0xc5, 0x87, 0xc6, 0xf4, 0x06,
  23795. 0xec, 0x91, 0x57, 0x81, 0xce, 0x3a, 0xe8,
  23796. 0xf1, 0x29, 0x3f, 0x01, 0x93, 0xf0, 0x74,
  23797. 0x22, 0xea, 0x6b, 0x06, 0xd8, 0x65, 0xdb,
  23798. 0xd7, 0x41, 0xd9, 0x60, 0x23, 0xe7, 0x83,
  23799. 0xc8, 0x69, 0x6b, 0x90, 0xc5, 0xc7, 0xb9,
  23800. 0xd5, 0xba, 0x79, 0xc9, 0x4a, 0x87, 0x23,
  23801. 0x1c, 0x95, 0x78, 0xf3, 0x73, 0x10, 0xbe,
  23802. 0xb2, 0x0f, 0x32, 0xec, 0xff, 0x15, 0x51,
  23803. 0x4d, 0xb5, 0x48, 0x3c, 0xca, 0x4c, 0x5b,
  23804. 0x32, 0x29, 0x47, 0x21, 0xba, 0x2a, 0x5d,
  23805. 0xc9, 0x59, 0xfa, 0x8f, 0x33, 0x10, 0x83,
  23806. 0x40, 0x80, 0xf3, 0xce, 0xee, 0x6d, 0xcd,
  23807. 0x9c, 0xbb, 0x23, 0x0b, 0x45, 0xba, 0x7a,
  23808. 0x07, 0xdc, 0x4d, 0x57, 0x97, 0xb4, 0xa4,
  23809. 0xef, 0x94, 0xe8, 0x43, 0xfe, 0x18, 0x47,
  23810. 0x1a, 0xb0, 0xf6, 0xb6, 0x0b, 0x55, 0x05,
  23811. 0xbd, 0x67, 0x2d, 0x37, 0x27, 0x17, 0x13,
  23812. 0x65, 0x22, 0xf2, 0x7c, 0xf7, 0x47, 0xd2,
  23813. 0x85, 0x63, 0x98, 0x83, 0xd2, 0xc1, 0xbf,
  23814. 0x8f, 0x4c, 0xda, 0xbf, 0xa4, 0x10, 0x6b,
  23815. 0x4e, 0x6b, 0x78, 0x5e, 0x3f, 0x7a, 0xec,
  23816. 0x15, 0x84, 0xbe, 0x1a, 0x94, 0xa2, 0x2b,
  23817. 0xb5, 0x3e, 0x55, 0x86, 0x51, 0xec, 0x2e,
  23818. 0x62, 0xcb, 0xd6, 0x9f, 0xe5, 0xa4, 0xb8,
  23819. 0xc0, 0xaa, 0x4e, 0x6d, 0x8a, 0xb1, 0xd6,
  23820. 0xf7, 0x8d, 0x1c, 0x04, 0x32, 0x8b, 0x20,
  23821. 0xf5, 0x80, 0x33, 0xbd, 0xcc, 0x3e, 0x4c,
  23822. 0x16, 0x04, 0xab, 0xd8, 0x64, 0x6d, 0xf9,
  23823. 0xc9, 0x15, 0x7d, 0x4b, 0x00, 0x86, 0xb2,
  23824. 0x70, 0x1d, 0x20, 0xcb, 0x7a, 0xed, 0x7e,
  23825. 0x81, 0x7f, 0x41, 0x33, 0xb8, 0x7b, 0xc0,
  23826. 0xa3, 0xbd, 0x12, 0xd1, 0x67, 0x48, 0xa0,
  23827. 0xb9, 0xeb, 0xd5, 0x29, 0xab, 0x91, 0x9c,
  23828. 0xa2, 0x2f, 0x8e, 0x01, 0x1c, 0x88, 0xc1,
  23829. 0x3e, 0x34, 0x47, 0x36, 0x8a, 0x35, 0x6c,
  23830. 0x2f, 0xc1, 0x8a, 0xb6, 0xd0, 0xa5, 0x01,
  23831. 0x82, 0xee, 0x4f, 0x44, 0xb9, 0xcd, 0x16,
  23832. 0x9c, 0x3a, 0xf8, 0xe9, 0x2a, 0xd2, 0xb6,
  23833. 0x1d, 0xfd, 0x3c, 0x06, 0xdc, 0x42, 0xdd,
  23834. 0x2d, 0x60, 0x6a, 0x44, 0x21, 0xc3, 0x37,
  23835. 0x75, 0x79, 0xc5, 0x29, 0x5c, 0x7e, 0xf5,
  23836. 0x86, 0xbb, 0x56, 0x05, 0x21, 0x46, 0xaf,
  23837. 0x6d, 0x3a, 0xa2, 0x9e, 0x11, 0x6d, 0x9e,
  23838. 0x05, 0x74, 0x8a, 0xfe, 0x84, 0x88, 0x3e,
  23839. 0x76, 0xb4, 0xef, 0x2f, 0xeb, 0x52, 0xcd,
  23840. 0x97, 0x82, 0xba, 0x0c, 0xcc, 0xcb, 0x72,
  23841. 0x8d, 0x8d, 0xd2, 0x32, 0x7c, 0x41, 0x39,
  23842. 0xa6, 0x22, 0xb7, 0xdc, 0x3f, 0x39, 0x43,
  23843. 0xf5, 0xee, 0x0c, 0xfc, 0xbb, 0x2b, 0x43,
  23844. 0xe8, 0xce, 0xae, 0x0c, 0xd9, 0x15, 0x22,
  23845. 0x32, 0xbd, 0x69, 0xad, 0x76, 0xd9, 0xdf,
  23846. 0x81, 0xdf, 0x24, 0x76, 0x7b, 0x53, 0x0b,
  23847. 0xe6, 0xc7, 0x6c, 0x38, 0x2c, 0xbf, 0x28,
  23848. 0x95, 0x03, 0x18, 0xef, 0x98, 0x88, 0xc2,
  23849. 0x6b, 0x1a, 0xf5, 0xb4, 0xf9, 0x19, 0x76,
  23850. 0x25, 0x1d, 0xcf, 0x9b, 0xcd, 0x4c, 0x00,
  23851. 0x06, 0xde, 0x55, 0x58, 0x95, 0x9a, 0x06,
  23852. 0xfb, 0xf9, 0x88, 0x20, 0x85, 0x80, 0xe3,
  23853. 0x27, 0xdf, 0xc5, 0x20, 0x29, 0x7c, 0x58,
  23854. 0x02, 0x07, 0x2e, 0xd2, 0xeb, 0xdc, 0x68,
  23855. 0x58, 0x91, 0x08, 0x71, 0x16, 0xb3, 0x82,
  23856. 0x2f, 0x6c, 0x45, 0xcd, 0xbe, 0xe5, 0x0c,
  23857. 0x07, 0x77, 0x95, 0x3b, 0x2c, 0x59, 0x8e,
  23858. 0xba, 0x07, 0xa8, 0xa1, 0xc6, 0xe5, 0x6a,
  23859. 0x49, 0xb5, 0x85, 0xf2, 0x70, 0x05, 0x22,
  23860. 0xc4, 0x2f, 0x8d, 0xdd, 0x48, 0x8d, 0x87,
  23861. 0xfa, 0xb6, 0xf8, 0x59, 0xc8, 0xb1, 0x18,
  23862. 0x03, 0x5f, 0xce, 0x53, 0x28, 0x96, 0x15,
  23863. 0xd4, 0xb4, 0x10, 0x2c, 0xe2, 0x22, 0x9e,
  23864. 0x88, 0xe5, 0xcd, 0xda, 0xfc, 0xf9, 0x64,
  23865. 0xa4, 0x7b, 0xfb, 0xeb, 0xa8, 0x6a, 0xb6,
  23866. 0xf6, 0x17, 0x84, 0x26, 0x3d, 0xe4, 0x66,
  23867. 0x7e, 0x5c, 0x85, 0x01, 0xaf, 0xdc, 0xdb,
  23868. 0x48, 0x33, 0x4a, 0x20, 0x7c, 0x22, 0x1b,
  23869. 0xd5, 0xeb, 0x2d,
  23870. };
  23871. static const byte ml_dsa_44_good_sig[] = {
  23872. 0xfc, 0x2d, 0xa0, 0x06, 0x85, 0xc2, 0xfc,
  23873. 0x92, 0x47, 0x77, 0x0b, 0x39, 0xbf, 0xe5,
  23874. 0xba, 0xd4, 0x44, 0xbf, 0xde, 0xce, 0x1f,
  23875. 0x04, 0xa2, 0x87, 0xed, 0x4a, 0xce, 0x0e,
  23876. 0xf3, 0x95, 0x61, 0x1e, 0x66, 0x4e, 0x9a,
  23877. 0x5d, 0x00, 0x31, 0x32, 0xf0, 0x90, 0x3d,
  23878. 0x7e, 0xf2, 0x9d, 0xe2, 0x93, 0xa1, 0xc8,
  23879. 0x64, 0x36, 0xf2, 0x59, 0xc7, 0x9e, 0xb6,
  23880. 0xb3, 0x6f, 0xe5, 0x80, 0x8d, 0x92, 0x77,
  23881. 0xd6, 0xb6, 0xe4, 0xc5, 0x5e, 0x79, 0x45,
  23882. 0x4b, 0xd0, 0xfe, 0x53, 0x55, 0xb6, 0x66,
  23883. 0x88, 0xfe, 0x95, 0x40, 0x07, 0xfd, 0xdb,
  23884. 0x40, 0x33, 0x39, 0x67, 0x03, 0x30, 0x8e,
  23885. 0x80, 0x4e, 0xa7, 0x0e, 0xe4, 0x05, 0x04,
  23886. 0xc5, 0x33, 0x72, 0x47, 0x5b, 0x85, 0x0f,
  23887. 0xe1, 0xeb, 0x98, 0x1a, 0x76, 0x79, 0x84,
  23888. 0xce, 0x26, 0x66, 0xe8, 0x92, 0xc9, 0x1f,
  23889. 0x40, 0x96, 0x72, 0xfe, 0x61, 0xae, 0xba,
  23890. 0x84, 0x70, 0xb7, 0x92, 0x2f, 0x7e, 0xc8,
  23891. 0xe8, 0xe4, 0x34, 0x73, 0xd4, 0x69, 0x57,
  23892. 0x3e, 0x28, 0x2b, 0x18, 0x0f, 0xef, 0xb1,
  23893. 0x06, 0xe2, 0xf8, 0x79, 0x70, 0x5a, 0x84,
  23894. 0x84, 0x6c, 0xb3, 0x57, 0x5b, 0x18, 0x42,
  23895. 0xdf, 0xd5, 0xdb, 0xf8, 0x35, 0x5f, 0x7b,
  23896. 0x23, 0x25, 0x2f, 0x0f, 0x17, 0x0b, 0x9a,
  23897. 0xb6, 0xe8, 0x31, 0x30, 0x6b, 0x90, 0x06,
  23898. 0x2c, 0xfd, 0xca, 0xaa, 0xa6, 0xc3, 0xdc,
  23899. 0x88, 0xa7, 0x31, 0x74, 0x67, 0xe2, 0x64,
  23900. 0x8f, 0x5c, 0xc4, 0xc0, 0x4d, 0x34, 0x15,
  23901. 0x0d, 0xd2, 0x23, 0x69, 0xfc, 0x6e, 0xbb,
  23902. 0x82, 0xca, 0xc4, 0xee, 0xf1, 0x14, 0xc1,
  23903. 0xd4, 0x5a, 0x71, 0x78, 0x9b, 0x40, 0x01,
  23904. 0xb9, 0xe4, 0x6e, 0x68, 0xf6, 0x13, 0xca,
  23905. 0xc1, 0xea, 0x70, 0x71, 0x3d, 0xc9, 0x1a,
  23906. 0x62, 0xb9, 0xa9, 0xe0, 0x1e, 0xe2, 0x34,
  23907. 0xf2, 0x9a, 0xf7, 0x23, 0xb3, 0xc1, 0xca,
  23908. 0x35, 0x0e, 0x5e, 0xa7, 0xd1, 0x3d, 0xea,
  23909. 0x51, 0xdc, 0xe2, 0x0e, 0xfc, 0x7d, 0x26,
  23910. 0x75, 0xec, 0x9a, 0x6e, 0x40, 0x1f, 0x60,
  23911. 0x06, 0xd7, 0x56, 0xf8, 0xa4, 0x2a, 0x82,
  23912. 0x9c, 0xef, 0x51, 0x4a, 0xe1, 0x01, 0x2b,
  23913. 0xb0, 0x8b, 0x34, 0x7b, 0xe1, 0x63, 0xa4,
  23914. 0xcc, 0x72, 0x81, 0xd9, 0xb4, 0x20, 0xcc,
  23915. 0x60, 0xe2, 0x15, 0x6d, 0xc7, 0x6c, 0x75,
  23916. 0x65, 0x4d, 0xb6, 0xc0, 0x36, 0x49, 0x87,
  23917. 0x06, 0x3e, 0xca, 0x1c, 0x32, 0x36, 0x2f,
  23918. 0xe3, 0xf6, 0x06, 0x0a, 0xb1, 0xd2, 0xfb,
  23919. 0xee, 0x4e, 0xd7, 0xce, 0x65, 0xcc, 0x89,
  23920. 0xf2, 0x77, 0x14, 0x27, 0x27, 0x84, 0x52,
  23921. 0x97, 0x1b, 0x89, 0x17, 0x31, 0x8d, 0xc4,
  23922. 0x0f, 0xc7, 0xc6, 0x45, 0x44, 0x9a, 0x97,
  23923. 0xd3, 0x88, 0x71, 0x73, 0x97, 0x64, 0xa6,
  23924. 0xe6, 0x3d, 0xf2, 0xd3, 0x7f, 0x7f, 0xfa,
  23925. 0x4f, 0xf4, 0xe9, 0x76, 0x8a, 0x2a, 0xfe,
  23926. 0x28, 0x8e, 0xa5, 0xb3, 0x46, 0x2b, 0xad,
  23927. 0x50, 0x5e, 0x12, 0xcd, 0xf8, 0x46, 0xe4,
  23928. 0x06, 0x12, 0xc2, 0xb8, 0xcd, 0x04, 0x76,
  23929. 0x07, 0x7c, 0xed, 0x2f, 0x0f, 0xd6, 0x97,
  23930. 0x31, 0xa4, 0x0c, 0x18, 0x85, 0x75, 0xd3,
  23931. 0x55, 0xfd, 0xe3, 0x1a, 0xbf, 0x43, 0xde,
  23932. 0x20, 0xa9, 0x19, 0xcd, 0x03, 0x01, 0xdf,
  23933. 0x04, 0x71, 0x09, 0x94, 0x99, 0x51, 0xb0,
  23934. 0x8e, 0x32, 0x80, 0xe6, 0x64, 0x4b, 0xdf,
  23935. 0xa5, 0xec, 0xfa, 0xce, 0xf6, 0xf3, 0xce,
  23936. 0x51, 0xe8, 0x6d, 0x03, 0x1e, 0x69, 0x59,
  23937. 0xef, 0x20, 0x98, 0x71, 0xe2, 0xc3, 0xec,
  23938. 0x19, 0x03, 0xa9, 0x2d, 0x12, 0x21, 0x79,
  23939. 0x7e, 0xb0, 0xcb, 0x76, 0x68, 0x2f, 0x11,
  23940. 0x01, 0x2b, 0x11, 0xef, 0xd6, 0xb7, 0x8b,
  23941. 0x5e, 0x31, 0x78, 0x7b, 0x2d, 0xe6, 0x4a,
  23942. 0xfb, 0xc1, 0xbb, 0x78, 0x92, 0x11, 0xcb,
  23943. 0x91, 0x97, 0x52, 0x1b, 0x8e, 0xfb, 0x59,
  23944. 0x02, 0x22, 0xbe, 0xf7, 0x33, 0xaa, 0x7b,
  23945. 0xfd, 0x93, 0xf7, 0xa8, 0x9b, 0xfc, 0x99,
  23946. 0x36, 0x22, 0x04, 0x1e, 0xdc, 0xa3, 0x2b,
  23947. 0xe6, 0xac, 0x2e, 0x4b, 0x38, 0x0a, 0x25,
  23948. 0xde, 0x3d, 0x8e, 0x0b, 0x95, 0x04, 0x48,
  23949. 0x3d, 0x66, 0x52, 0x99, 0x79, 0xe1, 0x8d,
  23950. 0xe0, 0xa7, 0xd5, 0x23, 0x0d, 0x45, 0x89,
  23951. 0x88, 0xa9, 0x59, 0x4e, 0xc7, 0x64, 0x39,
  23952. 0x93, 0xdd, 0xcb, 0xfe, 0x97, 0xe7, 0x7d,
  23953. 0xab, 0x61, 0x08, 0xf7, 0x7a, 0xff, 0x10,
  23954. 0x1d, 0x8d, 0x11, 0xa9, 0x97, 0xbd, 0x16,
  23955. 0xb5, 0x6c, 0x84, 0x71, 0x61, 0x72, 0x36,
  23956. 0x51, 0xe7, 0x43, 0x8b, 0x15, 0xb2, 0x48,
  23957. 0x6a, 0x14, 0x8a, 0xbe, 0x92, 0xa7, 0xfa,
  23958. 0xce, 0x02, 0x1a, 0x7f, 0xc5, 0xdb, 0x76,
  23959. 0x0a, 0x4c, 0xc7, 0x20, 0x2a, 0x34, 0xf4,
  23960. 0x92, 0x3b, 0x34, 0x69, 0x71, 0x3d, 0xe1,
  23961. 0xf2, 0x2f, 0x52, 0xe1, 0x48, 0xbe, 0x27,
  23962. 0x47, 0x1d, 0x55, 0x96, 0x6e, 0xa3, 0x39,
  23963. 0xc6, 0xd8, 0x12, 0xe0, 0xb2, 0x93, 0x56,
  23964. 0xef, 0x10, 0xf4, 0xa6, 0xf4, 0x5f, 0xa9,
  23965. 0xfd, 0x5d, 0x01, 0x87, 0xb8, 0xe5, 0x4f,
  23966. 0x86, 0x2c, 0xa5, 0x09, 0xfc, 0x29, 0x84,
  23967. 0x5b, 0x44, 0xf9, 0x8d, 0x9c, 0xbb, 0x19,
  23968. 0x97, 0x52, 0xbb, 0xac, 0x19, 0x57, 0x68,
  23969. 0x7d, 0x74, 0xb5, 0x4f, 0xda, 0x8a, 0x9c,
  23970. 0xcf, 0x5d, 0x7b, 0xd9, 0xf0, 0xb3, 0x11,
  23971. 0x76, 0x50, 0x03, 0x06, 0x44, 0xe7, 0x68,
  23972. 0x35, 0xe9, 0x14, 0x20, 0xbd, 0x0d, 0x90,
  23973. 0x96, 0x21, 0xa1, 0x17, 0x8f, 0xf7, 0x50,
  23974. 0x6c, 0xc0, 0x76, 0x3d, 0x34, 0x8d, 0xf2,
  23975. 0x75, 0xf0, 0xa2, 0x6c, 0x8a, 0xc1, 0x56,
  23976. 0x95, 0xb2, 0xd9, 0x87, 0xf2, 0xe4, 0x80,
  23977. 0x25, 0xc7, 0x97, 0xd2, 0xd2, 0xf8, 0x1c,
  23978. 0x7a, 0x48, 0x70, 0x99, 0x6b, 0xf2, 0x50,
  23979. 0x83, 0xf3, 0x10, 0xb1, 0x9b, 0x6d, 0x75,
  23980. 0x53, 0x86, 0x23, 0xc9, 0x60, 0x4d, 0x73,
  23981. 0xc7, 0x52, 0x90, 0x12, 0x6b, 0x92, 0x2d,
  23982. 0x35, 0xbc, 0x4d, 0x86, 0x67, 0xfe, 0x35,
  23983. 0x11, 0x6c, 0xbb, 0x9b, 0x76, 0xaf, 0x26,
  23984. 0xae, 0x50, 0x23, 0x76, 0x68, 0x16, 0x80,
  23985. 0xf0, 0xa4, 0xcc, 0x76, 0x6b, 0xf3, 0x99,
  23986. 0x04, 0x8b, 0x39, 0xf2, 0xa6, 0xa9, 0x72,
  23987. 0x6f, 0xbe, 0xa8, 0xdb, 0x53, 0xf3, 0x93,
  23988. 0x00, 0xac, 0x3e, 0x8f, 0xdd, 0x68, 0x9e,
  23989. 0x2f, 0xe3, 0x48, 0x0b, 0x11, 0xe2, 0x9a,
  23990. 0xfa, 0x98, 0x32, 0x40, 0x26, 0xf8, 0x83,
  23991. 0xc6, 0x00, 0x02, 0x7d, 0xb5, 0xd2, 0xd4,
  23992. 0xdd, 0xc6, 0x02, 0xec, 0xb3, 0x98, 0xd6,
  23993. 0x8e, 0xab, 0x75, 0x06, 0x37, 0x97, 0x4c,
  23994. 0x50, 0xc5, 0xe1, 0x43, 0x34, 0xd6, 0xbd,
  23995. 0xb6, 0xfc, 0xb7, 0x04, 0x0d, 0xd8, 0x35,
  23996. 0xb4, 0x3e, 0x0e, 0x43, 0x22, 0x83, 0xf5,
  23997. 0x5d, 0x2e, 0x0a, 0x8f, 0xa0, 0xec, 0x17,
  23998. 0xd9, 0xa1, 0x84, 0x98, 0x32, 0x5c, 0x99,
  23999. 0x66, 0x05, 0x70, 0x9a, 0xa4, 0x9b, 0xbe,
  24000. 0xd3, 0x3d, 0x2a, 0x13, 0xb1, 0x96, 0x37,
  24001. 0x4f, 0xe7, 0x6f, 0x6b, 0x5e, 0x80, 0xe4,
  24002. 0xb6, 0x98, 0x56, 0xed, 0xff, 0x5b, 0x21,
  24003. 0x5f, 0x79, 0x9a, 0x0f, 0x53, 0x69, 0x76,
  24004. 0xdb, 0xc0, 0x12, 0x9c, 0xed, 0xd4, 0x00,
  24005. 0x64, 0xca, 0xf4, 0xc3, 0x17, 0x49, 0xbb,
  24006. 0xef, 0xbc, 0x7b, 0x73, 0x24, 0x4f, 0x6e,
  24007. 0xcf, 0x25, 0x49, 0x30, 0x4f, 0x9a, 0xb7,
  24008. 0x2a, 0x2f, 0xc2, 0x69, 0x74, 0xe5, 0xa6,
  24009. 0xd7, 0x29, 0x4d, 0x80, 0xe6, 0xf6, 0x66,
  24010. 0x4b, 0xdf, 0xef, 0xfd, 0xb5, 0xaa, 0x53,
  24011. 0x75, 0x0e, 0xf5, 0x92, 0xb3, 0x30, 0x3d,
  24012. 0x5d, 0xa8, 0x49, 0x74, 0xa2, 0x13, 0xb3,
  24013. 0x99, 0x7e, 0xf9, 0x34, 0x08, 0xc2, 0xa6,
  24014. 0xc9, 0xd1, 0xb7, 0x65, 0xf4, 0xa9, 0xda,
  24015. 0x11, 0x07, 0x89, 0x08, 0x92, 0xdf, 0x1a,
  24016. 0x8d, 0xd7, 0x1e, 0xe9, 0xa3, 0x5c, 0x66,
  24017. 0x79, 0xa0, 0x2d, 0xd1, 0xd9, 0x65, 0xa2,
  24018. 0xd3, 0x47, 0xb1, 0xa1, 0xf4, 0xa1, 0x18,
  24019. 0x7f, 0xb0, 0xd1, 0x20, 0x05, 0x06, 0x6f,
  24020. 0xda, 0xe3, 0xef, 0xee, 0x47, 0xdf, 0x80,
  24021. 0x22, 0x14, 0x4b, 0xc2, 0xe4, 0xea, 0x02,
  24022. 0xe7, 0x06, 0xc8, 0x2d, 0x2d, 0xbd, 0xd2,
  24023. 0xef, 0xd3, 0x3b, 0xf0, 0xc8, 0xc1, 0x04,
  24024. 0x53, 0x81, 0x27, 0xb2, 0xf2, 0xf8, 0x5d,
  24025. 0xe1, 0x27, 0xd6, 0xd8, 0x2e, 0x0d, 0x43,
  24026. 0xf4, 0xf0, 0x7a, 0x8c, 0x78, 0x3f, 0x23,
  24027. 0x4d, 0x05, 0xf6, 0xc6, 0x9a, 0xc1, 0x19,
  24028. 0x58, 0x48, 0x02, 0x71, 0xc4, 0xf1, 0x16,
  24029. 0xc2, 0xbf, 0xc2, 0xf5, 0xa6, 0x70, 0x25,
  24030. 0x8b, 0x6c, 0x31, 0xa6, 0x2b, 0x6a, 0x1f,
  24031. 0x26, 0x4a, 0x6b, 0x05, 0x67, 0xa9, 0x5e,
  24032. 0xa6, 0xc7, 0x19, 0xf0, 0x6f, 0xc2, 0xa4,
  24033. 0x07, 0xe1, 0xf8, 0xb6, 0x70, 0x79, 0x85,
  24034. 0x97, 0x1f, 0xc2, 0x97, 0xf3, 0x57, 0x3d,
  24035. 0xd1, 0x70, 0xb1, 0xe8, 0x43, 0x11, 0xe6,
  24036. 0x2f, 0x8a, 0x12, 0x97, 0xe1, 0x1f, 0x86,
  24037. 0x33, 0xa1, 0x30, 0xb3, 0x8e, 0xd7, 0x43,
  24038. 0x99, 0x35, 0xd3, 0x48, 0x04, 0x29, 0xa3,
  24039. 0xf3, 0x2c, 0x0e, 0xda, 0x66, 0xc8, 0xa9,
  24040. 0xd3, 0x28, 0x7f, 0xf0, 0xaa, 0x53, 0xc8,
  24041. 0x4b, 0xa3, 0xde, 0xcc, 0x5f, 0xae, 0x9b,
  24042. 0x2f, 0x20, 0xf7, 0x9a, 0x41, 0xb7, 0xdb,
  24043. 0x7a, 0x92, 0xa1, 0x45, 0x2c, 0x49, 0xb7,
  24044. 0x2d, 0xe5, 0x6e, 0x84, 0xb1, 0xa4, 0x6e,
  24045. 0xea, 0xf9, 0xca, 0xc6, 0x0f, 0xd3, 0xdc,
  24046. 0xad, 0x79, 0xf4, 0x3b, 0xc0, 0x8c, 0x8c,
  24047. 0x6f, 0xdf, 0x2c, 0xbb, 0x2a, 0x9f, 0x74,
  24048. 0x2f, 0x31, 0x7b, 0x05, 0xc5, 0xc4, 0x22,
  24049. 0xfd, 0xfc, 0xdf, 0x95, 0x3b, 0x21, 0x41,
  24050. 0xab, 0x5b, 0xe9, 0x37, 0xdf, 0xd3, 0x4c,
  24051. 0x97, 0x73, 0xac, 0xbd, 0x82, 0x97, 0xd0,
  24052. 0x00, 0x55, 0x47, 0xa4, 0x67, 0x12, 0xcd,
  24053. 0xe3, 0x01, 0x31, 0xe6, 0x1c, 0x61, 0xf8,
  24054. 0xa4, 0xce, 0x23, 0xed, 0xeb, 0x8f, 0x57,
  24055. 0xbf, 0x97, 0x4b, 0x26, 0x75, 0xa0, 0x4b,
  24056. 0xf6, 0x7e, 0xda, 0x1c, 0x90, 0x8a, 0xc4,
  24057. 0xf4, 0x60, 0xfc, 0x65, 0x45, 0x8c, 0x1b,
  24058. 0x5c, 0x65, 0xc4, 0x5d, 0x99, 0x9a, 0xed,
  24059. 0x62, 0xd6, 0x74, 0x43, 0xac, 0x5e, 0x0c,
  24060. 0xfd, 0x7d, 0xf2, 0x4f, 0xc3, 0x02, 0xc8,
  24061. 0x24, 0xa7, 0x17, 0x49, 0xf9, 0xdd, 0xc9,
  24062. 0x64, 0x71, 0x08, 0xb9, 0xac, 0x47, 0x3d,
  24063. 0x6a, 0x4f, 0xdc, 0xa6, 0x56, 0x00, 0x01,
  24064. 0x53, 0x2d, 0xca, 0x32, 0x4e, 0x40, 0xec,
  24065. 0x14, 0x03, 0x69, 0x75, 0xfa, 0x93, 0x49,
  24066. 0x98, 0x04, 0x25, 0x29, 0xe5, 0x78, 0x6e,
  24067. 0x3e, 0x99, 0x58, 0x18, 0x48, 0xf3, 0x29,
  24068. 0x56, 0x8d, 0x23, 0xd1, 0x5d, 0xb2, 0x2a,
  24069. 0x74, 0xa7, 0x53, 0xc5, 0xc6, 0xc4, 0x12,
  24070. 0xfe, 0x65, 0x6c, 0xde, 0xc3, 0x6c, 0x18,
  24071. 0xde, 0xc3, 0x97, 0xaa, 0xed, 0x69, 0x3c,
  24072. 0x4d, 0xc9, 0xa9, 0x63, 0xa9, 0x4d, 0x91,
  24073. 0x63, 0xa3, 0x1c, 0x87, 0x36, 0x19, 0x4a,
  24074. 0xc5, 0xd1, 0xcb, 0xf4, 0x88, 0xfd, 0xa0,
  24075. 0x9b, 0x37, 0x9a, 0x7e, 0xcc, 0x09, 0xac,
  24076. 0x3b, 0xf0, 0xf5, 0xb5, 0x15, 0x72, 0x47,
  24077. 0xb0, 0x42, 0x0d, 0xed, 0x19, 0x42, 0x93,
  24078. 0x5a, 0x56, 0xbf, 0x2c, 0x4b, 0xec, 0xf4,
  24079. 0x13, 0x30, 0x0b, 0xdf, 0x0e, 0xc1, 0x22,
  24080. 0xa5, 0x6c, 0xf4, 0xcf, 0x09, 0x83, 0xe2,
  24081. 0xd0, 0x05, 0x62, 0x8d, 0xda, 0xea, 0x79,
  24082. 0xa9, 0x6c, 0xe1, 0x90, 0xc0, 0xe7, 0x6d,
  24083. 0x63, 0x8e, 0xe5, 0xe0, 0xa9, 0x67, 0x80,
  24084. 0xb7, 0x80, 0x43, 0xfc, 0xa1, 0x11, 0x6d,
  24085. 0xc9, 0x91, 0xa3, 0xcb, 0x1a, 0x6e, 0xf8,
  24086. 0x6e, 0xdb, 0xf0, 0x7d, 0xeb, 0x45, 0x6a,
  24087. 0xee, 0xd8, 0x3e, 0x6b, 0x9a, 0xc4, 0xcd,
  24088. 0x7b, 0x35, 0x19, 0x73, 0x14, 0x22, 0x47,
  24089. 0x86, 0x54, 0x34, 0xc9, 0x02, 0x49, 0xe6,
  24090. 0xb9, 0x45, 0xf7, 0x2c, 0xf7, 0xa4, 0x12,
  24091. 0xc4, 0x7a, 0x95, 0x41, 0x54, 0x8d, 0x51,
  24092. 0xc5, 0x1a, 0x19, 0x75, 0x0c, 0x11, 0x03,
  24093. 0xd0, 0x6f, 0x56, 0x04, 0xb2, 0x87, 0x21,
  24094. 0xdb, 0x47, 0xed, 0x9b, 0xe8, 0xec, 0xa3,
  24095. 0xe1, 0x74, 0x3e, 0x21, 0x50, 0xf2, 0x09,
  24096. 0x2c, 0x5e, 0x3d, 0xca, 0xa7, 0x31, 0x7c,
  24097. 0xbd, 0xe4, 0xf1, 0x15, 0x0d, 0xe6, 0x3d,
  24098. 0x4c, 0x8e, 0x36, 0x45, 0xf7, 0x08, 0x3d,
  24099. 0x56, 0x63, 0xb6, 0x99, 0x46, 0x34, 0x48,
  24100. 0xfa, 0xcf, 0x88, 0xe5, 0x43, 0xf7, 0x88,
  24101. 0xd5, 0x55, 0x13, 0xfe, 0x79, 0x02, 0x49,
  24102. 0x1e, 0x82, 0x45, 0x45, 0x29, 0x8e, 0x0a,
  24103. 0x7c, 0x77, 0x51, 0x1b, 0x8e, 0x75, 0xd5,
  24104. 0xd6, 0x61, 0xff, 0xa5, 0xfe, 0x59, 0x48,
  24105. 0xbf, 0xfc, 0xf2, 0xcd, 0x37, 0x09, 0x4c,
  24106. 0xb6, 0xfc, 0xb0, 0x20, 0x5f, 0x12, 0x7a,
  24107. 0x3f, 0x64, 0x96, 0xdb, 0xc8, 0xe8, 0xd0,
  24108. 0x11, 0x19, 0x47, 0x95, 0x2d, 0x3d, 0xbf,
  24109. 0x56, 0x9c, 0x23, 0x30, 0x07, 0x1f, 0x2c,
  24110. 0x04, 0x5c, 0x7e, 0x4e, 0x2e, 0xa6, 0x20,
  24111. 0x38, 0xa0, 0x88, 0x47, 0x8a, 0x3f, 0x8f,
  24112. 0x8a, 0xe7, 0x6d, 0x0a, 0xf1, 0x2d, 0xd6,
  24113. 0x10, 0x23, 0x01, 0x62, 0x71, 0x5a, 0xbb,
  24114. 0x51, 0x98, 0xa1, 0x27, 0x7d, 0xba, 0x5f,
  24115. 0xe4, 0xdc, 0xd6, 0xd7, 0x4c, 0x1a, 0xe0,
  24116. 0x4d, 0xe1, 0xee, 0x61, 0xb7, 0xc5, 0x50,
  24117. 0x92, 0x99, 0xc7, 0x7b, 0x18, 0xf9, 0x89,
  24118. 0x2b, 0x57, 0xf9, 0xe9, 0xac, 0x23, 0x6e,
  24119. 0xff, 0xbd, 0x5a, 0x93, 0xdf, 0x15, 0x74,
  24120. 0x92, 0x0b, 0x76, 0x84, 0x96, 0x94, 0x1b,
  24121. 0x8f, 0xe4, 0x6e, 0x2b, 0xd7, 0x47, 0xfd,
  24122. 0x24, 0x3b, 0xe4, 0xe4, 0x99, 0xf9, 0x0b,
  24123. 0xf9, 0x29, 0x25, 0x25, 0x6a, 0xc0, 0x1e,
  24124. 0xb8, 0x8c, 0xd6, 0xd0, 0x6e, 0x13, 0x86,
  24125. 0xa8, 0x7c, 0xc3, 0x31, 0x30, 0x2e, 0x9b,
  24126. 0x51, 0xc1, 0x21, 0xea, 0x15, 0x8e, 0xd8,
  24127. 0x06, 0xa2, 0xe9, 0x12, 0x9f, 0xcb, 0x6b,
  24128. 0x24, 0xef, 0x4e, 0x19, 0x6c, 0xa5, 0x98,
  24129. 0x47, 0x8b, 0x4d, 0xbe, 0x00, 0x0c, 0x04,
  24130. 0xe3, 0x4d, 0x84, 0x64, 0x36, 0x20, 0x9f,
  24131. 0xde, 0xe2, 0x55, 0x89, 0x3e, 0x40, 0xe1,
  24132. 0xe3, 0x63, 0x0a, 0xe7, 0x15, 0x4c, 0xcd,
  24133. 0x4b, 0x63, 0x6f, 0x70, 0xc2, 0x84, 0x30,
  24134. 0x5d, 0x22, 0xd0, 0xe4, 0x65, 0xfb, 0x8a,
  24135. 0x07, 0x1a, 0x54, 0xf5, 0x4b, 0x65, 0xad,
  24136. 0x64, 0x91, 0x6e, 0x23, 0x98, 0x31, 0x26,
  24137. 0x79, 0x70, 0x94, 0xff, 0xc0, 0x65, 0x70,
  24138. 0xb4, 0x9d, 0x74, 0x8e, 0x76, 0x6b, 0x9a,
  24139. 0x24, 0x28, 0x0d, 0x8a, 0x93, 0x87, 0x18,
  24140. 0x04, 0x10, 0xfd, 0x0b, 0xaf, 0xd3, 0x92,
  24141. 0xbb, 0xb5, 0x41, 0xd2, 0x87, 0xc7, 0x69,
  24142. 0x89, 0x0c, 0x52, 0xf8, 0x46, 0x70, 0x8e,
  24143. 0xf0, 0x99, 0x96, 0x57, 0x40, 0x9c, 0xef,
  24144. 0x9a, 0xc2, 0x63, 0x47, 0x13, 0x11, 0x68,
  24145. 0x40, 0xab, 0x36, 0x16, 0x53, 0xd6, 0x8f,
  24146. 0x58, 0x5a, 0xdd, 0x0c, 0xd7, 0x17, 0x6a,
  24147. 0x39, 0x34, 0xf1, 0xe7, 0x75, 0x3b, 0x41,
  24148. 0x7e, 0x5a, 0x70, 0xfe, 0x5b, 0x08, 0x30,
  24149. 0xf4, 0x7b, 0x1d, 0xd0, 0x70, 0xae, 0x18,
  24150. 0xad, 0xd4, 0xff, 0xbb, 0xa4, 0x31, 0xec,
  24151. 0x28, 0x72, 0x64, 0x9a, 0x24, 0x16, 0x30,
  24152. 0xe4, 0xc5, 0x3c, 0xb0, 0x3c, 0x81, 0x4f,
  24153. 0xb9, 0xfc, 0xe1, 0x3a, 0x05, 0x17, 0xb2,
  24154. 0x18, 0x4c, 0x98, 0x3b, 0xfc, 0x93, 0xf0,
  24155. 0x0d, 0xb6, 0x3c, 0x54, 0x7b, 0x10, 0xfd,
  24156. 0x7f, 0x63, 0xfe, 0xa5, 0xc6, 0xb5, 0x24,
  24157. 0xb4, 0xf2, 0x2f, 0xb2, 0x6f, 0x7f, 0xdb,
  24158. 0x01, 0xdb, 0xaf, 0x57, 0xdc, 0xdb, 0xf4,
  24159. 0xc8, 0x31, 0xb0, 0xdd, 0x05, 0x8b, 0x9b,
  24160. 0x6e, 0x7c, 0x5e, 0x5e, 0x31, 0x2c, 0x7f,
  24161. 0xbb, 0xa4, 0x26, 0x88, 0xe1, 0x55, 0x3d,
  24162. 0x8d, 0x36, 0x69, 0xb7, 0xc8, 0xcc, 0x05,
  24163. 0xfe, 0x6e, 0xcc, 0xcb, 0xfd, 0x81, 0x14,
  24164. 0x8b, 0xbc, 0x0c, 0xd4, 0x7e, 0xb4, 0x13,
  24165. 0xc0, 0xe2, 0x51, 0xf4, 0x07, 0xde, 0xc4,
  24166. 0x1c, 0xf0, 0xc8, 0x92, 0xd4, 0x38, 0xd7,
  24167. 0x1e, 0x57, 0xa7, 0x4b, 0xc7, 0xf8, 0xca,
  24168. 0xcc, 0x61, 0x86, 0x96, 0x50, 0x4e, 0x6a,
  24169. 0x71, 0xbd, 0x5f, 0xc6, 0x48, 0x8b, 0x6f,
  24170. 0xeb, 0x53, 0x50, 0xea, 0x35, 0x39, 0x79,
  24171. 0xcc, 0xee, 0xae, 0x81, 0x0f, 0xe2, 0xd2,
  24172. 0xbb, 0x81, 0x03, 0x8a, 0xeb, 0x98, 0xc2,
  24173. 0xad, 0xb7, 0xc0, 0x14, 0x68, 0xb9, 0x7e,
  24174. 0x8e, 0x30, 0x11, 0x43, 0x59, 0x8f, 0x04,
  24175. 0x2f, 0x7a, 0x99, 0x36, 0xd0, 0x75, 0x07,
  24176. 0x6b, 0x8e, 0xc2, 0x10, 0xac, 0xc6, 0x4d,
  24177. 0x30, 0x91, 0x9f, 0xde, 0x33, 0x0e, 0xe7,
  24178. 0xbc, 0xe5, 0x94, 0xbf, 0x80, 0xdb, 0xb7,
  24179. 0xe0, 0x8f, 0xb7, 0x74, 0xc3, 0x77, 0x18,
  24180. 0x76, 0x33, 0x34, 0xb8, 0xe4, 0x81, 0xa5,
  24181. 0xd9, 0xf7, 0x78, 0xba, 0xec, 0x62, 0x34,
  24182. 0xcb, 0x54, 0xbe, 0x90, 0xc1, 0x56, 0x59,
  24183. 0x22, 0x94, 0x5b, 0x1a, 0x8f, 0xc6, 0x8f,
  24184. 0xf2, 0x84, 0x1f, 0x61, 0x4e, 0xeb, 0x98,
  24185. 0x72, 0xdd, 0xde, 0xc9, 0xc6, 0xab, 0xd1,
  24186. 0xad, 0xd1, 0x02, 0x60, 0x82, 0x89, 0xba,
  24187. 0xf5, 0x1a, 0x6b, 0xde, 0x96, 0x19, 0x64,
  24188. 0x44, 0x0a, 0xd6, 0x27, 0x9f, 0x67, 0x96,
  24189. 0x44, 0xcc, 0x4b, 0xfd, 0x8f, 0x9e, 0xa1,
  24190. 0x1f, 0x06, 0xac, 0x2e, 0xcf, 0xdb, 0xc7,
  24191. 0x08, 0x5e, 0xe3, 0xa2, 0x59, 0xa2, 0x22,
  24192. 0xf1, 0x7a, 0xce, 0xf5, 0x30, 0x53, 0xc0,
  24193. 0xbb, 0x36, 0x13, 0x95, 0x69, 0xe2, 0x28,
  24194. 0x47, 0xad, 0xb1, 0x82, 0xfd, 0x14, 0xf6,
  24195. 0x6e, 0xed, 0x5b, 0xe0, 0xeb, 0x13, 0x5c,
  24196. 0xc6, 0x72, 0xd5, 0x2c, 0xd0, 0xae, 0xc3,
  24197. 0xad, 0xa9, 0x60, 0x2a, 0x68, 0x7b, 0x03,
  24198. 0x54, 0xc5, 0xd1, 0x71, 0xc4, 0x99, 0x48,
  24199. 0x75, 0x18, 0x1e, 0xda, 0x0a, 0x8b, 0xe1,
  24200. 0x2d, 0x67, 0x1f, 0xae, 0x91, 0xc0, 0x37,
  24201. 0x39, 0x8c, 0x7c, 0x9e, 0x42, 0xd3, 0x6e,
  24202. 0xaf, 0x7e, 0x8e, 0xa0, 0x01, 0x45, 0xc1,
  24203. 0xc6, 0xef, 0xc6, 0x2e, 0x87, 0x7b, 0x5a,
  24204. 0x60, 0xe0, 0xec, 0x3b, 0x76, 0x8e, 0xb3,
  24205. 0x0d, 0x57, 0x86, 0xa2, 0xc9, 0x1a, 0x1b,
  24206. 0x38, 0x6b, 0x75, 0x7a, 0x81, 0x8b, 0x97,
  24207. 0x99, 0xa1, 0xbb, 0xd8, 0xda, 0xdf, 0xea,
  24208. 0xef, 0xf0, 0xf9, 0x01, 0x08, 0x11, 0x18,
  24209. 0x26, 0x36, 0x4b, 0x52, 0x56, 0x68, 0x7b,
  24210. 0x98, 0xca, 0xd3, 0xd6, 0xef, 0xf6, 0x05,
  24211. 0x07, 0x09, 0x10, 0x1f, 0x21, 0x28, 0x32,
  24212. 0x39, 0x3a, 0x3c, 0x54, 0x77, 0x7b, 0x81,
  24213. 0x99, 0xa9, 0xb0, 0xb2, 0xce, 0xe5, 0xe7,
  24214. 0x1f, 0x23, 0x3f, 0x4e, 0x50, 0x5d, 0x71,
  24215. 0x7a, 0x7c, 0x91, 0xa7, 0xab, 0xae, 0xd0,
  24216. 0xd6, 0xe1, 0xe6, 0xf1, 0xf3, 0xfa, 0x00,
  24217. 0x00, 0x13, 0x24, 0x3a, 0x4e,
  24218. };
  24219. #endif
  24220. static int test_wc_dilithium(void)
  24221. {
  24222. EXPECT_DECLS;
  24223. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM)
  24224. dilithium_key* key;
  24225. byte level;
  24226. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  24227. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  24228. WC_RNG rng;
  24229. #endif
  24230. byte* privKey = NULL;
  24231. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  24232. word32 privKeyLen = DILITHIUM_MAX_KEY_SIZE;
  24233. #endif
  24234. byte* pubKey = NULL;
  24235. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  24236. word32 pubKeyLen = DILITHIUM_MAX_PUB_KEY_SIZE;
  24237. #endif
  24238. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24239. ExpectNotNull(key);
  24240. privKey = (byte*)XMALLOC(DILITHIUM_MAX_KEY_SIZE, NULL,
  24241. DYNAMIC_TYPE_TMP_BUFFER);
  24242. ExpectNotNull(privKey);
  24243. pubKey = (byte*)XMALLOC(DILITHIUM_MAX_PUB_KEY_SIZE, NULL,
  24244. DYNAMIC_TYPE_TMP_BUFFER);
  24245. ExpectNotNull(pubKey);
  24246. if (key != NULL) {
  24247. XMEMSET(key, 0, sizeof(*key));
  24248. }
  24249. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  24250. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  24251. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24252. #endif
  24253. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  24254. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  24255. ExpectIntEQ(wc_InitRng(&rng), 0);
  24256. #endif
  24257. ExpectIntEQ(wc_dilithium_init(NULL), BAD_FUNC_ARG);
  24258. ExpectIntEQ(wc_dilithium_init_ex(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  24259. wc_dilithium_free(NULL);
  24260. ExpectIntEQ(wc_dilithium_init(key), 0);
  24261. wc_dilithium_free(key);
  24262. ExpectIntEQ(wc_dilithium_init_ex(key, NULL, INVALID_DEVID), 0);
  24263. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  24264. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &pubKeyLen),
  24265. BAD_FUNC_ARG);
  24266. #endif
  24267. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  24268. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &privKeyLen),
  24269. BAD_FUNC_ARG);
  24270. #endif
  24271. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  24272. ExpectIntEQ(wc_dilithium_size(NULL), BAD_FUNC_ARG);
  24273. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24274. ExpectIntEQ(wc_dilithium_priv_size(NULL), BAD_FUNC_ARG);
  24275. #endif
  24276. #endif
  24277. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24278. ExpectIntEQ(wc_dilithium_pub_size(NULL), BAD_FUNC_ARG);
  24279. #endif
  24280. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  24281. ExpectIntEQ(wc_dilithium_sig_size(NULL), BAD_FUNC_ARG);
  24282. #endif
  24283. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  24284. ExpectIntEQ(wc_dilithium_size(key), BAD_FUNC_ARG);
  24285. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24286. ExpectIntEQ(wc_dilithium_priv_size(key), BAD_FUNC_ARG);
  24287. #endif
  24288. #endif
  24289. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24290. ExpectIntEQ(wc_dilithium_pub_size(key), BAD_FUNC_ARG);
  24291. #endif
  24292. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  24293. ExpectIntEQ(wc_dilithium_sig_size(key), BAD_FUNC_ARG);
  24294. #endif
  24295. ExpectIntEQ(wc_dilithium_set_level(NULL, 0), BAD_FUNC_ARG);
  24296. ExpectIntEQ(wc_dilithium_set_level(key, 0), BAD_FUNC_ARG);
  24297. ExpectIntEQ(wc_dilithium_set_level(NULL, WC_ML_DSA_44), BAD_FUNC_ARG);
  24298. ExpectIntEQ(wc_dilithium_set_level(key, 1), BAD_FUNC_ARG);
  24299. ExpectIntEQ(wc_dilithium_set_level(key, 4), BAD_FUNC_ARG);
  24300. ExpectIntEQ(wc_dilithium_get_level(NULL, NULL), BAD_FUNC_ARG);
  24301. ExpectIntEQ(wc_dilithium_get_level(key, NULL), BAD_FUNC_ARG);
  24302. ExpectIntEQ(wc_dilithium_get_level(NULL, &level), BAD_FUNC_ARG);
  24303. ExpectIntEQ(wc_dilithium_get_level(key, &level), BAD_FUNC_ARG);
  24304. #ifndef WOLFSSL_NO_ML_DSA_87
  24305. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  24306. ExpectIntEQ(wc_dilithium_get_level(key, &level), 0);
  24307. ExpectIntEQ(level, WC_ML_DSA_87);
  24308. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  24309. ExpectIntEQ(wc_dilithium_size(key), DILITHIUM_LEVEL5_KEY_SIZE);
  24310. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24311. ExpectIntEQ(wc_dilithium_priv_size(key), DILITHIUM_LEVEL5_PRV_KEY_SIZE);
  24312. #endif
  24313. #endif
  24314. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24315. ExpectIntEQ(wc_dilithium_pub_size(key), DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  24316. #endif
  24317. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  24318. ExpectIntEQ(wc_dilithium_sig_size(key), DILITHIUM_LEVEL5_SIG_SIZE);
  24319. #endif
  24320. #else
  24321. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), NOT_COMPILED_IN);
  24322. #endif
  24323. #ifndef WOLFSSL_NO_ML_DSA_65
  24324. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  24325. ExpectIntEQ(wc_dilithium_get_level(key, &level), 0);
  24326. ExpectIntEQ(level, WC_ML_DSA_65);
  24327. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  24328. ExpectIntEQ(wc_dilithium_size(key), DILITHIUM_LEVEL3_KEY_SIZE);
  24329. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24330. ExpectIntEQ(wc_dilithium_priv_size(key), DILITHIUM_LEVEL3_PRV_KEY_SIZE);
  24331. #endif
  24332. #endif
  24333. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24334. ExpectIntEQ(wc_dilithium_pub_size(key), DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  24335. #endif
  24336. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  24337. ExpectIntEQ(wc_dilithium_sig_size(key), DILITHIUM_LEVEL3_SIG_SIZE);
  24338. #endif
  24339. #else
  24340. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), NOT_COMPILED_IN);
  24341. #endif
  24342. #ifndef WOLFSSL_NO_ML_DSA_44
  24343. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  24344. ExpectIntEQ(wc_dilithium_get_level(key, &level), 0);
  24345. ExpectIntEQ(level, WC_ML_DSA_44);
  24346. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  24347. ExpectIntEQ(wc_dilithium_size(key), DILITHIUM_LEVEL2_KEY_SIZE);
  24348. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24349. ExpectIntEQ(wc_dilithium_priv_size(key), DILITHIUM_LEVEL2_PRV_KEY_SIZE);
  24350. #endif
  24351. #endif
  24352. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24353. ExpectIntEQ(wc_dilithium_pub_size(key), DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  24354. #endif
  24355. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  24356. ExpectIntEQ(wc_dilithium_sig_size(key), DILITHIUM_LEVEL2_SIG_SIZE);
  24357. #endif
  24358. #else
  24359. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), NOT_COMPILED_IN);
  24360. #endif
  24361. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  24362. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &pubKeyLen),
  24363. BAD_FUNC_ARG);
  24364. #endif
  24365. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  24366. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &privKeyLen),
  24367. BAD_FUNC_ARG);
  24368. #endif
  24369. wc_dilithium_free(key);
  24370. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  24371. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  24372. wc_FreeRng(&rng);
  24373. #endif
  24374. XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24375. XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24376. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24377. #endif
  24378. return EXPECT_RESULT();
  24379. }
  24380. static int test_wc_dilithium_make_key(void)
  24381. {
  24382. EXPECT_DECLS;
  24383. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  24384. !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
  24385. dilithium_key* key;
  24386. WC_RNG rng;
  24387. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24388. ExpectNotNull(key);
  24389. if (key != NULL) {
  24390. XMEMSET(key, 0, sizeof(*key));
  24391. }
  24392. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24393. ExpectIntEQ(wc_InitRng(&rng), 0);
  24394. ExpectIntEQ(wc_dilithium_init(key), 0);
  24395. ExpectIntEQ(wc_dilithium_make_key(key, &rng), BAD_STATE_E);
  24396. #ifndef WOLFSSL_NO_ML_DSA_44
  24397. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  24398. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24399. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  24400. #else
  24401. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  24402. #endif
  24403. ExpectIntEQ(wc_dilithium_make_key(NULL, NULL), BAD_FUNC_ARG);
  24404. ExpectIntEQ(wc_dilithium_make_key(key, NULL), BAD_FUNC_ARG);
  24405. ExpectIntEQ(wc_dilithium_make_key(NULL, &rng), BAD_FUNC_ARG);
  24406. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  24407. wc_dilithium_free(key);
  24408. wc_FreeRng(&rng);
  24409. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24410. #endif
  24411. return EXPECT_RESULT();
  24412. }
  24413. static int test_wc_dilithium_sign(void)
  24414. {
  24415. EXPECT_DECLS;
  24416. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  24417. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  24418. dilithium_key* key;
  24419. dilithium_key* importKey = NULL;
  24420. WC_RNG rng;
  24421. byte* privKey = NULL;
  24422. word32 privKeyLen = DILITHIUM_MAX_KEY_SIZE;
  24423. word32 badKeyLen;
  24424. byte msg[32];
  24425. byte* sig = NULL;
  24426. word32 sigLen = DILITHIUM_MAX_SIG_SIZE;
  24427. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24428. ExpectNotNull(key);
  24429. importKey = (dilithium_key*)XMALLOC(sizeof(*key), NULL,
  24430. DYNAMIC_TYPE_TMP_BUFFER);
  24431. ExpectNotNull(importKey);
  24432. privKey = (byte*)XMALLOC(DILITHIUM_MAX_KEY_SIZE, NULL,
  24433. DYNAMIC_TYPE_TMP_BUFFER);
  24434. ExpectNotNull(privKey);
  24435. sig = (byte*)XMALLOC(DILITHIUM_MAX_SIG_SIZE, NULL,
  24436. DYNAMIC_TYPE_TMP_BUFFER);
  24437. ExpectNotNull(sig);
  24438. if (key != NULL) {
  24439. XMEMSET(key, 0, sizeof(*key));
  24440. }
  24441. if (importKey != NULL) {
  24442. XMEMSET(importKey, 0, sizeof(*importKey));
  24443. }
  24444. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24445. XMEMSET(msg, 0x55, sizeof(msg));
  24446. ExpectIntEQ(wc_InitRng(&rng), 0);
  24447. ExpectIntEQ(wc_dilithium_init(key), 0);
  24448. #ifndef WOLFSSL_NO_ML_DSA_44
  24449. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  24450. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24451. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  24452. #else
  24453. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  24454. #endif
  24455. #ifdef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  24456. #ifndef WOLFSSL_NO_ML_DSA_44
  24457. ExpectIntEQ(wc_dilithium_import_private(bench_dilithium_level2_key,
  24458. sizeof_bench_dilithium_level2_key, key), 0);
  24459. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24460. ExpectIntEQ(wc_dilithium_import_private(bench_dilithium_level3_key,
  24461. sizeof_bench_dilithium_level3_key, key), 0);
  24462. #else
  24463. ExpectIntEQ(wc_dilithium_import_private(bench_dilithium_level5_key,
  24464. sizeof_bench_dilithium_level5_key, key), 0);
  24465. #endif
  24466. #else
  24467. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  24468. #endif
  24469. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, NULL, NULL, NULL),
  24470. BAD_FUNC_ARG);
  24471. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, NULL, NULL, NULL, NULL),
  24472. BAD_FUNC_ARG);
  24473. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, sig, NULL, NULL, NULL),
  24474. BAD_FUNC_ARG);
  24475. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, &sigLen, NULL, NULL),
  24476. BAD_FUNC_ARG);
  24477. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, NULL, key, NULL),
  24478. BAD_FUNC_ARG);
  24479. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, NULL, NULL, &rng),
  24480. BAD_FUNC_ARG);
  24481. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, sig, &sigLen, key, &rng),
  24482. BAD_FUNC_ARG);
  24483. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, NULL, &sigLen, key, &rng),
  24484. BAD_FUNC_ARG);
  24485. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, NULL, key, &rng),
  24486. BAD_FUNC_ARG);
  24487. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, NULL, &rng),
  24488. BAD_FUNC_ARG);
  24489. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, NULL),
  24490. BAD_FUNC_ARG);
  24491. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, &rng), 0);
  24492. ExpectIntEQ(wc_dilithium_export_private(NULL, NULL, NULL),
  24493. BAD_FUNC_ARG);
  24494. ExpectIntEQ(wc_dilithium_export_private(key, NULL, NULL),
  24495. BAD_FUNC_ARG);
  24496. ExpectIntEQ(wc_dilithium_export_private(NULL, privKey, NULL),
  24497. BAD_FUNC_ARG);
  24498. ExpectIntEQ(wc_dilithium_export_private(NULL, NULL, &privKeyLen),
  24499. BAD_FUNC_ARG);
  24500. ExpectIntEQ(wc_dilithium_export_private(NULL, privKey, &privKeyLen),
  24501. BAD_FUNC_ARG);
  24502. ExpectIntEQ(wc_dilithium_export_private(key, NULL, &privKeyLen),
  24503. BAD_FUNC_ARG);
  24504. ExpectIntEQ(wc_dilithium_export_private(key, privKey, NULL),
  24505. BAD_FUNC_ARG);
  24506. badKeyLen = 0;
  24507. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &badKeyLen),
  24508. BUFFER_E);
  24509. #ifndef WOLFSSL_NO_ML_DSA_44
  24510. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL2_KEY_SIZE);
  24511. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24512. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL3_KEY_SIZE);
  24513. #else
  24514. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL5_KEY_SIZE);
  24515. #endif
  24516. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &privKeyLen),
  24517. 0);
  24518. #ifndef WOLFSSL_NO_ML_DSA_44
  24519. ExpectIntEQ(privKeyLen, DILITHIUM_LEVEL2_KEY_SIZE);
  24520. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24521. ExpectIntEQ(privKeyLen, DILITHIUM_LEVEL3_KEY_SIZE);
  24522. #else
  24523. ExpectIntEQ(privKeyLen, DILITHIUM_LEVEL5_KEY_SIZE);
  24524. #endif
  24525. ExpectIntEQ(wc_dilithium_init(importKey), 0);
  24526. ExpectIntEQ(wc_dilithium_import_private(privKey, privKeyLen, importKey),
  24527. BAD_FUNC_ARG);
  24528. #ifndef WOLFSSL_NO_ML_DSA_44
  24529. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_44), 0);
  24530. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24531. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_65), 0);
  24532. #else
  24533. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_87), 0);
  24534. #endif
  24535. ExpectIntEQ(wc_dilithium_import_private(NULL, 0, NULL),
  24536. BAD_FUNC_ARG);
  24537. ExpectIntEQ(wc_dilithium_import_private(privKey, 0, NULL),
  24538. BAD_FUNC_ARG);
  24539. ExpectIntEQ(wc_dilithium_import_private(NULL, privKeyLen, NULL),
  24540. BAD_FUNC_ARG);
  24541. ExpectIntEQ(wc_dilithium_import_private(NULL, 0, importKey),
  24542. BAD_FUNC_ARG);
  24543. ExpectIntEQ(wc_dilithium_import_private(NULL, privKeyLen, importKey),
  24544. BAD_FUNC_ARG);
  24545. ExpectIntEQ(wc_dilithium_import_private(privKey, 0, importKey),
  24546. BAD_FUNC_ARG);
  24547. ExpectIntEQ(wc_dilithium_import_private(privKey, privKeyLen, NULL),
  24548. BAD_FUNC_ARG);
  24549. ExpectIntEQ(wc_dilithium_import_private(privKey, privKeyLen, importKey),
  24550. 0);
  24551. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, &rng), 0);
  24552. #ifdef WOLFSSL_DILITHIUM_CHECK_KEY
  24553. ExpectIntEQ(wc_dilithium_check_key(importKey), PUBLIC_KEY_E);
  24554. #endif
  24555. wc_dilithium_free(importKey);
  24556. wc_dilithium_free(key);
  24557. wc_FreeRng(&rng);
  24558. XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24559. XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24560. XFREE(importKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24561. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24562. #endif
  24563. return EXPECT_RESULT();
  24564. }
  24565. static int test_wc_dilithium_verify(void)
  24566. {
  24567. EXPECT_DECLS;
  24568. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  24569. !defined(WOLFSSL_DILITHIUM_NO_VERIFY) && \
  24570. (!defined(WOLFSSL_NO_ML_DSA_44) || !defined(WOLFSSL_DILITHIUM_NO_SIGN))
  24571. dilithium_key* key;
  24572. dilithium_key* importKey = NULL;
  24573. WC_RNG rng;
  24574. byte* pubKey = NULL;
  24575. word32 pubKeyLen = DILITHIUM_MAX_PUB_KEY_SIZE;
  24576. word32 badKeyLen;
  24577. byte msg[32];
  24578. byte* sig = NULL;
  24579. word32 sigLen = DILITHIUM_MAX_SIG_SIZE;
  24580. int res;
  24581. #ifndef WOLFSSL_NO_ML_DSA_44
  24582. byte b;
  24583. #endif
  24584. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24585. ExpectNotNull(key);
  24586. importKey = (dilithium_key*)XMALLOC(sizeof(*key), NULL,
  24587. DYNAMIC_TYPE_TMP_BUFFER);
  24588. ExpectNotNull(importKey);
  24589. pubKey = (byte*)XMALLOC(DILITHIUM_MAX_PUB_KEY_SIZE, NULL,
  24590. DYNAMIC_TYPE_TMP_BUFFER);
  24591. ExpectNotNull(pubKey);
  24592. sig = (byte*)XMALLOC(DILITHIUM_MAX_SIG_SIZE, NULL,
  24593. DYNAMIC_TYPE_TMP_BUFFER);
  24594. ExpectNotNull(sig);
  24595. if (key != NULL) {
  24596. XMEMSET(key, 0, sizeof(*key));
  24597. }
  24598. if (importKey != NULL) {
  24599. XMEMSET(importKey, 0, sizeof(*importKey));
  24600. }
  24601. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24602. XMEMSET(msg, 0x55, sizeof(msg));
  24603. ExpectIntEQ(wc_InitRng(&rng), 0);
  24604. ExpectIntEQ(wc_dilithium_init(key), 0);
  24605. #ifndef WOLFSSL_NO_ML_DSA_44
  24606. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  24607. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24608. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  24609. #else
  24610. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  24611. #endif
  24612. #if !defined(WOLFSSL_NO_ML_DSA_44)
  24613. ExpectIntEQ(wc_dilithium_import_public(ml_dsa_44_pub_key,
  24614. (word32)sizeof(ml_dsa_44_pub_key), key), 0);
  24615. if (sig != NULL) {
  24616. XMEMCPY(sig, ml_dsa_44_good_sig, sizeof(ml_dsa_44_good_sig));
  24617. }
  24618. sigLen = (word32)sizeof(ml_dsa_44_good_sig);
  24619. #else
  24620. #ifdef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  24621. #ifndef WOLFSSL_NO_ML_DSA_65
  24622. ExpectIntEQ(wc_dilithium_import_public(bench_dilithium_level3_pub_key,
  24623. sizeof_bench_dilithium_level3_pub_key, key), 0);
  24624. #else
  24625. ExpectIntEQ(wc_dilithium_import_public(bench_dilithium_level5_pub_key,
  24626. sizeof_bench_dilithium_level5_pub_key, key), 0);
  24627. #endif /* !WOLFSSL_NO_ML_DSA_65 */
  24628. #else
  24629. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  24630. #endif /* WOLFSSL_DILITHIUM_NO_MAKE_KEY */
  24631. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, &rng), 0);
  24632. #endif /* !WOLFSSL_NO_ML_DSA_44 */
  24633. ExpectIntEQ(wc_dilithium_export_public(NULL, NULL, NULL),
  24634. BAD_FUNC_ARG);
  24635. ExpectIntEQ(wc_dilithium_export_public(key, NULL, NULL),
  24636. BAD_FUNC_ARG);
  24637. ExpectIntEQ(wc_dilithium_export_public(NULL, pubKey, NULL),
  24638. BAD_FUNC_ARG);
  24639. ExpectIntEQ(wc_dilithium_export_public(NULL, NULL, &pubKeyLen),
  24640. BAD_FUNC_ARG);
  24641. ExpectIntEQ(wc_dilithium_export_public(NULL, pubKey, &pubKeyLen),
  24642. BAD_FUNC_ARG);
  24643. ExpectIntEQ(wc_dilithium_export_public(key, NULL, &pubKeyLen),
  24644. BAD_FUNC_ARG);
  24645. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, NULL),
  24646. BAD_FUNC_ARG);
  24647. badKeyLen = 0;
  24648. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &badKeyLen),
  24649. BUFFER_E);
  24650. #ifndef WOLFSSL_NO_ML_DSA_44
  24651. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  24652. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24653. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  24654. #else
  24655. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  24656. #endif
  24657. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &pubKeyLen), 0);
  24658. #ifndef WOLFSSL_NO_ML_DSA_44
  24659. ExpectIntEQ(pubKeyLen, DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  24660. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24661. ExpectIntEQ(pubKeyLen, DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  24662. #else
  24663. ExpectIntEQ(pubKeyLen, DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  24664. #endif
  24665. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, NULL, 32, NULL, NULL),
  24666. BAD_FUNC_ARG);
  24667. ExpectIntEQ(wc_dilithium_verify_msg(sig, 0, NULL, 32, NULL, NULL),
  24668. BAD_FUNC_ARG);
  24669. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, msg, 32, NULL, NULL),
  24670. BAD_FUNC_ARG);
  24671. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, NULL, 32, &res, NULL),
  24672. BAD_FUNC_ARG);
  24673. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, NULL, 32, NULL, key),
  24674. BAD_FUNC_ARG);
  24675. ExpectIntEQ(wc_dilithium_verify_msg(NULL, sigLen, msg, 32, &res, key),
  24676. BAD_FUNC_ARG);
  24677. ExpectIntEQ(wc_dilithium_verify_msg(sig, 0, msg, 32, &res, key),
  24678. BUFFER_E);
  24679. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, NULL, 32, &res, key),
  24680. BAD_FUNC_ARG);
  24681. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, NULL, key),
  24682. BAD_FUNC_ARG);
  24683. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, NULL),
  24684. BAD_FUNC_ARG);
  24685. res = 0;
  24686. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key), 0);
  24687. ExpectIntEQ(res, 1);
  24688. ExpectIntEQ(wc_dilithium_init(importKey), 0);
  24689. ExpectIntEQ(wc_dilithium_import_public(pubKey, pubKeyLen, importKey),
  24690. BAD_FUNC_ARG);
  24691. #ifndef WOLFSSL_NO_ML_DSA_44
  24692. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_44), 0);
  24693. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24694. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_65), 0);
  24695. #else
  24696. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_87), 0);
  24697. #endif
  24698. ExpectIntEQ(wc_dilithium_import_public(NULL, 0, NULL),
  24699. BAD_FUNC_ARG);
  24700. ExpectIntEQ(wc_dilithium_import_public(pubKey, 0, NULL),
  24701. BAD_FUNC_ARG);
  24702. ExpectIntEQ(wc_dilithium_import_public(NULL, pubKeyLen, NULL),
  24703. BAD_FUNC_ARG);
  24704. ExpectIntEQ(wc_dilithium_import_public(NULL, 0, importKey),
  24705. BAD_FUNC_ARG);
  24706. ExpectIntEQ(wc_dilithium_import_public(NULL, pubKeyLen, importKey),
  24707. BAD_FUNC_ARG);
  24708. ExpectIntEQ(wc_dilithium_import_public(pubKey, 0, importKey),
  24709. BAD_FUNC_ARG);
  24710. ExpectIntEQ(wc_dilithium_import_public(pubKey, pubKeyLen, NULL),
  24711. BAD_FUNC_ARG);
  24712. ExpectIntEQ(wc_dilithium_import_public(pubKey, pubKeyLen, importKey), 0);
  24713. res = 0;
  24714. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, importKey),
  24715. 0);
  24716. ExpectIntEQ(res, 1);
  24717. #ifdef WOLFSSL_DILITHIUM_CHECK_KEY
  24718. ExpectIntEQ(wc_dilithium_check_key(importKey), BAD_FUNC_ARG);
  24719. #endif
  24720. wc_dilithium_free(importKey);
  24721. #ifndef WOLFSSL_NO_ML_DSA_44
  24722. if (sig != NULL) {
  24723. if (sig[sigLen - 5] == 0) {
  24724. /* Unused hints meant to be 0. */
  24725. sig[sigLen - 5] = 0xff;
  24726. res = 1;
  24727. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res,
  24728. key), SIG_VERIFY_E);
  24729. ExpectIntEQ(res, 0);
  24730. sig[sigLen - 5] = 0x00;
  24731. }
  24732. /* Last count of hints must be less than PARAMS_ML_DSA_44_OMEGA == 80 */
  24733. b = sig[sigLen - 1];
  24734. sig[sigLen - 1] = 0xff;
  24735. res = 1;
  24736. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key),
  24737. SIG_VERIFY_E);
  24738. ExpectIntEQ(res, 0);
  24739. sig[sigLen - 1] = b;
  24740. if (sig[sigLen - 4] > 1) {
  24741. /* Index must be less than previous. */
  24742. b = sig[sigLen - 84];
  24743. sig[sigLen - 84] = 0xff;
  24744. res = 1;
  24745. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res,
  24746. key), SIG_VERIFY_E);
  24747. ExpectIntEQ(res, 0);
  24748. sig[sigLen - 84] = b;
  24749. }
  24750. /* Mess up commit hash. */
  24751. sig[0] ^= 0x80;
  24752. res = 1;
  24753. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key),
  24754. 0);
  24755. ExpectIntEQ(res, 0);
  24756. sig[0] ^= 0x80;
  24757. /* Mess up z. */
  24758. sig[100] ^= 0x80;
  24759. res = 1;
  24760. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key),
  24761. 0);
  24762. ExpectIntEQ(res, 0);
  24763. sig[100] ^= 0x80;
  24764. }
  24765. #endif
  24766. wc_dilithium_free(key);
  24767. wc_FreeRng(&rng);
  24768. XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24769. XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24770. XFREE(importKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24771. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24772. #endif
  24773. return EXPECT_RESULT();
  24774. }
  24775. static int test_wc_dilithium_check_key(void)
  24776. {
  24777. EXPECT_DECLS;
  24778. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  24779. defined(WOLFSSL_DILITHIUM_CHECK_KEY) && \
  24780. !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
  24781. dilithium_key* checkKey;
  24782. WC_RNG rng;
  24783. byte* privCheckKey = NULL;
  24784. word32 privCheckKeyLen = DILITHIUM_MAX_KEY_SIZE;
  24785. byte* pubCheckKey = NULL;
  24786. word32 pubCheckKeyLen = DILITHIUM_MAX_PUB_KEY_SIZE;
  24787. checkKey = (dilithium_key*)XMALLOC(sizeof(*checkKey), NULL,
  24788. DYNAMIC_TYPE_TMP_BUFFER);
  24789. ExpectNotNull(checkKey);
  24790. privCheckKey = (byte*)XMALLOC(DILITHIUM_MAX_KEY_SIZE, NULL,
  24791. DYNAMIC_TYPE_TMP_BUFFER);
  24792. ExpectNotNull(privCheckKey);
  24793. pubCheckKey = (byte*)XMALLOC(DILITHIUM_MAX_PUB_KEY_SIZE, NULL,
  24794. DYNAMIC_TYPE_TMP_BUFFER);
  24795. ExpectNotNull(pubCheckKey);
  24796. if (checkKey != NULL) {
  24797. XMEMSET(checkKey, 0, sizeof(*checkKey));
  24798. }
  24799. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24800. ExpectIntEQ(wc_InitRng(&rng), 0);
  24801. ExpectIntEQ(wc_dilithium_check_key(NULL), BAD_FUNC_ARG);
  24802. ExpectIntEQ(wc_dilithium_init(checkKey), 0);
  24803. ExpectIntEQ(wc_dilithium_export_key(NULL, privCheckKey,
  24804. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  24805. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24806. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, NULL), BAD_FUNC_ARG);
  24807. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  24808. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  24809. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24810. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, checkKey), BAD_FUNC_ARG);
  24811. #ifndef WOLFSSL_NO_ML_DSA_44
  24812. ExpectIntEQ(wc_dilithium_set_level(checkKey, WC_ML_DSA_44), 0);
  24813. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24814. ExpectIntEQ(wc_dilithium_set_level(checkKey, WC_ML_DSA_65), 0);
  24815. #else
  24816. ExpectIntEQ(wc_dilithium_set_level(checkKey, WC_ML_DSA_87), 0);
  24817. #endif
  24818. ExpectIntEQ(wc_dilithium_make_key(checkKey, &rng), 0);
  24819. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, NULL, NULL, NULL),
  24820. BAD_FUNC_ARG);
  24821. ExpectIntEQ(wc_dilithium_export_key(checkKey, NULL, NULL, NULL, NULL),
  24822. BAD_FUNC_ARG);
  24823. ExpectIntEQ(wc_dilithium_export_key(NULL, privCheckKey, NULL, NULL, NULL),
  24824. BAD_FUNC_ARG);
  24825. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, &privCheckKeyLen, NULL,
  24826. NULL), BAD_FUNC_ARG);
  24827. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, NULL, pubCheckKey, NULL),
  24828. BAD_FUNC_ARG);
  24829. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, NULL, NULL,
  24830. &pubCheckKeyLen), BAD_FUNC_ARG);
  24831. ExpectIntEQ(wc_dilithium_export_key(NULL , privCheckKey,
  24832. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  24833. ExpectIntEQ(wc_dilithium_export_key(checkKey, NULL ,
  24834. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  24835. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  24836. NULL , pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  24837. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  24838. &privCheckKeyLen, NULL , &pubCheckKeyLen), BAD_FUNC_ARG);
  24839. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  24840. &privCheckKeyLen, pubCheckKey, NULL ), BAD_FUNC_ARG);
  24841. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  24842. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), 0);
  24843. /* Modify hash. */
  24844. if (pubCheckKey != NULL) {
  24845. pubCheckKey[0] ^= 0x80;
  24846. ExpectIntEQ(wc_dilithium_import_key(NULL, 0, NULL, 0, NULL),
  24847. BAD_FUNC_ARG);
  24848. ExpectIntEQ(wc_dilithium_import_key(privCheckKey, 0, NULL, 0, NULL),
  24849. BAD_FUNC_ARG);
  24850. ExpectIntEQ(wc_dilithium_import_key(NULL, 0, pubCheckKey, 0, NULL),
  24851. BAD_FUNC_ARG);
  24852. ExpectIntEQ(wc_dilithium_import_key(NULL, 0, NULL, 0, checkKey),
  24853. BAD_FUNC_ARG);
  24854. ExpectIntEQ(wc_dilithium_import_key(NULL ,
  24855. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, checkKey),
  24856. BAD_FUNC_ARG);
  24857. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24858. 0 , pubCheckKey, pubCheckKeyLen, checkKey),
  24859. BAD_FUNC_ARG);
  24860. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24861. privCheckKeyLen, NULL , pubCheckKeyLen, checkKey),
  24862. BAD_FUNC_ARG);
  24863. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24864. privCheckKeyLen, pubCheckKey, 0 , checkKey),
  24865. BAD_FUNC_ARG);
  24866. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24867. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, NULL ),
  24868. BAD_FUNC_ARG);
  24869. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24870. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, checkKey), 0);
  24871. ExpectIntEQ(wc_dilithium_check_key(checkKey), PUBLIC_KEY_E);
  24872. pubCheckKey[0] ^= 0x80;
  24873. /* Modify encoded t1. */
  24874. pubCheckKey[48] ^= 0x80;
  24875. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24876. privCheckKeyLen,pubCheckKey, pubCheckKeyLen, checkKey), 0);
  24877. ExpectIntEQ(wc_dilithium_check_key(checkKey), PUBLIC_KEY_E);
  24878. pubCheckKey[48] ^= 0x80;
  24879. }
  24880. wc_dilithium_free(checkKey);
  24881. wc_FreeRng(&rng);
  24882. XFREE(pubCheckKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24883. XFREE(privCheckKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24884. XFREE(checkKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24885. #endif
  24886. return EXPECT_RESULT();
  24887. }
  24888. static int test_wc_dilithium_der(void)
  24889. {
  24890. EXPECT_DECLS;
  24891. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  24892. !defined(WOLFSSL_DILITHIUM_NO_ASN1) && \
  24893. !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
  24894. #define DILITHIUM_MAX_DER_SIZE 8192
  24895. dilithium_key* key;
  24896. WC_RNG rng;
  24897. byte* der = NULL;
  24898. int len;
  24899. int pubLen;
  24900. int pubDerLen;
  24901. int privDerLen;
  24902. int keyDerLen;
  24903. word32 idx;
  24904. #ifndef WOLFSSL_NO_ML_DSA_44
  24905. pubLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE;
  24906. pubDerLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE + 24;
  24907. privDerLen = DILITHIUM_LEVEL2_KEY_SIZE + 30;
  24908. keyDerLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE + DILITHIUM_LEVEL2_KEY_SIZE + 34;
  24909. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24910. pubLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE;
  24911. pubDerLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE + 24;
  24912. privDerLen = DILITHIUM_LEVEL3_KEY_SIZE + 30;
  24913. keyDerLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE + DILITHIUM_LEVEL3_KEY_SIZE + 34;
  24914. #else
  24915. pubLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE;
  24916. pubDerLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE + 24;
  24917. privDerLen = DILITHIUM_LEVEL5_KEY_SIZE + 30;
  24918. keyDerLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE + DILITHIUM_LEVEL5_KEY_SIZE + 34;
  24919. #endif
  24920. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24921. ExpectNotNull(key);
  24922. der = (byte*)XMALLOC(DILITHIUM_MAX_DER_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24923. ExpectNotNull(der);
  24924. if (key != NULL) {
  24925. XMEMSET(key, 0, sizeof(*key));
  24926. }
  24927. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24928. ExpectIntEQ(wc_InitRng(&rng), 0);
  24929. ExpectIntEQ(wc_dilithium_init(key), 0);
  24930. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  24931. 0), BAD_FUNC_ARG);
  24932. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  24933. 1), BAD_FUNC_ARG);
  24934. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
  24935. BAD_FUNC_ARG);
  24936. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der, &idx, key, pubDerLen),
  24937. BAD_FUNC_ARG);
  24938. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, privDerLen),
  24939. BAD_FUNC_ARG);
  24940. #ifndef WOLFSSL_NO_ML_DSA_44
  24941. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  24942. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24943. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  24944. #else
  24945. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  24946. #endif
  24947. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  24948. 0), BAD_FUNC_ARG);
  24949. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  24950. 1), BAD_FUNC_ARG);
  24951. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
  24952. BAD_FUNC_ARG);
  24953. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  24954. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, NULL, 0 ,
  24955. 0), BAD_FUNC_ARG);
  24956. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, der , 0 ,
  24957. 0), BAD_FUNC_ARG);
  24958. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, NULL, DILITHIUM_MAX_DER_SIZE,
  24959. 0), BAD_FUNC_ARG);
  24960. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, der , DILITHIUM_MAX_DER_SIZE,
  24961. 0), BAD_FUNC_ARG);
  24962. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , der , 0 ,
  24963. 0), BUFFER_E);
  24964. /* Get length only. */
  24965. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, 0 ,
  24966. 0), pubLen);
  24967. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE,
  24968. 0), pubLen);
  24969. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, 0 ,
  24970. 1), pubDerLen);
  24971. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE,
  24972. 1), pubDerLen);
  24973. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, NULL,
  24974. 0 ), BAD_FUNC_ARG);
  24975. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , NULL,
  24976. 0 ), BAD_FUNC_ARG);
  24977. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, der ,
  24978. 0 ), BAD_FUNC_ARG);
  24979. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, NULL,
  24980. DILITHIUM_MAX_DER_SIZE), BAD_FUNC_ARG);
  24981. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, der ,
  24982. DILITHIUM_MAX_DER_SIZE), BAD_FUNC_ARG);
  24983. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , der ,
  24984. 0 ), BAD_FUNC_ARG);
  24985. /* Get length only. */
  24986. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , NULL,
  24987. DILITHIUM_MAX_DER_SIZE), privDerLen);
  24988. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, NULL, 0 ),
  24989. BAD_FUNC_ARG);
  24990. ExpectIntEQ(wc_Dilithium_KeyToDer(key , NULL, 0 ),
  24991. BAD_FUNC_ARG);
  24992. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, der , 0 ),
  24993. BAD_FUNC_ARG);
  24994. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, NULL, DILITHIUM_MAX_DER_SIZE),
  24995. BAD_FUNC_ARG);
  24996. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, der , DILITHIUM_MAX_DER_SIZE),
  24997. BAD_FUNC_ARG);
  24998. ExpectIntEQ(wc_Dilithium_KeyToDer(key , der , 0 ),
  24999. BAD_FUNC_ARG);
  25000. /* Get length only. */
  25001. ExpectIntEQ(wc_Dilithium_KeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE),
  25002. keyDerLen);
  25003. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, NULL, NULL, 0 ),
  25004. BAD_FUNC_ARG);
  25005. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , NULL, NULL, 0 ),
  25006. BAD_FUNC_ARG);
  25007. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, &idx, NULL, 0 ),
  25008. BAD_FUNC_ARG);
  25009. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, NULL, key , 0 ),
  25010. BAD_FUNC_ARG);
  25011. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, NULL, NULL, pubDerLen),
  25012. BAD_FUNC_ARG);
  25013. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, &idx, key , pubDerLen),
  25014. BAD_FUNC_ARG);
  25015. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , NULL, key , pubDerLen),
  25016. BAD_FUNC_ARG);
  25017. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , &idx, NULL, pubDerLen),
  25018. BAD_FUNC_ARG);
  25019. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , &idx, key , 0 ),
  25020. BAD_FUNC_ARG);
  25021. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, NULL, NULL, 0 ),
  25022. BAD_FUNC_ARG);
  25023. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , NULL, NULL, 0 ),
  25024. BAD_FUNC_ARG);
  25025. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, &idx, NULL, 0 ),
  25026. BAD_FUNC_ARG);
  25027. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, NULL, key , 0 ),
  25028. BAD_FUNC_ARG);
  25029. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, NULL, NULL, privDerLen),
  25030. BAD_FUNC_ARG);
  25031. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, &idx, key , privDerLen),
  25032. BAD_FUNC_ARG);
  25033. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , NULL, key , privDerLen),
  25034. BAD_FUNC_ARG);
  25035. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , &idx, NULL, privDerLen),
  25036. BAD_FUNC_ARG);
  25037. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , &idx, key , 0 ),
  25038. BAD_FUNC_ARG);
  25039. ExpectIntEQ(len = wc_Dilithium_PublicKeyToDer(key, der,
  25040. DILITHIUM_MAX_DER_SIZE, 0), pubLen);
  25041. ExpectIntEQ(wc_dilithium_import_public(der, len, key), 0);
  25042. ExpectIntEQ(len = wc_Dilithium_PublicKeyToDer(key, der,
  25043. DILITHIUM_MAX_DER_SIZE, 1), pubDerLen);
  25044. idx = 0;
  25045. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der, &idx, key, len), 0);
  25046. ExpectIntEQ(len = wc_Dilithium_PrivateKeyToDer(key, der,
  25047. DILITHIUM_MAX_DER_SIZE), privDerLen);
  25048. idx = 0;
  25049. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, len), 0);
  25050. ExpectIntEQ(len = wc_Dilithium_KeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
  25051. keyDerLen);
  25052. idx = 0;
  25053. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, len), 0);
  25054. wc_dilithium_free(key);
  25055. wc_FreeRng(&rng);
  25056. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25057. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25058. #endif
  25059. return EXPECT_RESULT();
  25060. }
  25061. /*
  25062. * Testing wc_SetSubjectBuffer
  25063. */
  25064. static int test_wc_SetSubjectBuffer(void)
  25065. {
  25066. EXPECT_DECLS;
  25067. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  25068. Cert cert;
  25069. XFILE file = XBADFILE;
  25070. byte* der = NULL;
  25071. word32 derSz;
  25072. derSz = FOURK_BUF;
  25073. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25074. DYNAMIC_TYPE_TMP_BUFFER));
  25075. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  25076. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  25077. if (file != XBADFILE)
  25078. XFCLOSE(file);
  25079. ExpectIntEQ(wc_InitCert(&cert), 0);
  25080. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, (int)derSz), 0);
  25081. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, (int)derSz), BAD_FUNC_ARG);
  25082. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25083. #endif
  25084. return EXPECT_RESULT();
  25085. } /* End test_wc_SetSubjectBuffer*/
  25086. /*
  25087. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  25088. */
  25089. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  25090. {
  25091. EXPECT_DECLS;
  25092. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  25093. WC_RNG rng;
  25094. Cert cert;
  25095. #if !defined(NO_RSA) && defined(HAVE_RSA)
  25096. RsaKey rsaKey;
  25097. int bits = 2048;
  25098. #endif
  25099. #if defined(HAVE_ECC)
  25100. ecc_key eccKey;
  25101. int ret;
  25102. #endif
  25103. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25104. ed25519_key ed25519Key;
  25105. #endif
  25106. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25107. ed448_key ed448Key;
  25108. #endif
  25109. #ifndef HAVE_FIPS
  25110. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  25111. #else
  25112. ExpectIntEQ(wc_InitRng(&rng), 0);
  25113. #endif
  25114. ExpectIntEQ(wc_InitCert(&cert), 0);
  25115. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  25116. /* RSA */
  25117. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  25118. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  25119. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  25120. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  25121. 0);
  25122. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  25123. #endif
  25124. #if defined(HAVE_ECC)
  25125. /* ECC */
  25126. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  25127. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  25128. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  25129. #if defined(WOLFSSL_ASYNC_CRYPT)
  25130. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  25131. #endif
  25132. ExpectIntEQ(ret, 0);
  25133. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  25134. 0);
  25135. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  25136. #endif
  25137. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25138. /* ED25519 */
  25139. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  25140. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  25141. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  25142. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  25143. &ed25519Key), 0);
  25144. wc_ed25519_free(&ed25519Key);
  25145. #endif
  25146. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25147. /* ED448 */
  25148. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  25149. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  25150. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  25151. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  25152. &ed448Key), 0);
  25153. wc_ed448_free(&ed448Key);
  25154. #endif
  25155. wc_FreeRng(&rng);
  25156. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25157. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  25158. return EXPECT_RESULT();
  25159. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  25160. /*
  25161. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  25162. */
  25163. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  25164. {
  25165. EXPECT_DECLS;
  25166. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  25167. WC_RNG rng;
  25168. Cert cert;
  25169. #if !defined(NO_RSA) && defined(HAVE_RSA)
  25170. RsaKey rsaKey;
  25171. int bits = 2048;
  25172. #endif
  25173. #if defined(HAVE_ECC)
  25174. ecc_key eccKey;
  25175. int ret;
  25176. #endif
  25177. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25178. ed25519_key ed25519Key;
  25179. #endif
  25180. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25181. ed448_key ed448Key;
  25182. #endif
  25183. #ifndef HAVE_FIPS
  25184. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  25185. #else
  25186. ExpectIntEQ(wc_InitRng(&rng), 0);
  25187. #endif
  25188. ExpectIntEQ(wc_InitCert(&cert), 0);
  25189. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  25190. /* RSA */
  25191. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  25192. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  25193. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  25194. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  25195. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  25196. #endif
  25197. #if defined(HAVE_ECC)
  25198. /* ECC */
  25199. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  25200. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  25201. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  25202. #if defined(WOLFSSL_ASYNC_CRYPT)
  25203. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  25204. #endif
  25205. ExpectIntEQ(ret, 0);
  25206. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  25207. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  25208. #endif
  25209. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25210. /* ED25519 */
  25211. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  25212. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  25213. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  25214. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  25215. &ed25519Key), 0);
  25216. wc_ed25519_free(&ed25519Key);
  25217. #endif
  25218. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25219. /* ED448 */
  25220. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  25221. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  25222. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  25223. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  25224. 0);
  25225. wc_ed448_free(&ed448Key);
  25226. #endif
  25227. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25228. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  25229. return EXPECT_RESULT();
  25230. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  25231. /*
  25232. * Testing wc_PKCS7_New()
  25233. */
  25234. static int test_wc_PKCS7_New(void)
  25235. {
  25236. EXPECT_DECLS;
  25237. #if defined(HAVE_PKCS7)
  25238. PKCS7* pkcs7 = NULL;
  25239. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  25240. wc_PKCS7_Free(pkcs7);
  25241. #endif
  25242. return EXPECT_RESULT();
  25243. } /* END test-wc_PKCS7_New */
  25244. /*
  25245. * Testing wc_PKCS7_Init()
  25246. */
  25247. static int test_wc_PKCS7_Init(void)
  25248. {
  25249. EXPECT_DECLS;
  25250. #if defined(HAVE_PKCS7)
  25251. PKCS7* pkcs7 = NULL;
  25252. void* heap = NULL;
  25253. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25254. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25255. /* Pass in bad args. */
  25256. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  25257. wc_PKCS7_Free(pkcs7);
  25258. #endif
  25259. return EXPECT_RESULT();
  25260. } /* END test-wc_PKCS7_Init */
  25261. /*
  25262. * Testing wc_PKCS7_InitWithCert()
  25263. */
  25264. static int test_wc_PKCS7_InitWithCert(void)
  25265. {
  25266. EXPECT_DECLS;
  25267. #if defined(HAVE_PKCS7)
  25268. PKCS7* pkcs7 = NULL;
  25269. #ifndef NO_RSA
  25270. #if defined(USE_CERT_BUFFERS_2048)
  25271. unsigned char cert[sizeof(client_cert_der_2048)];
  25272. int certSz = (int)sizeof(cert);
  25273. XMEMSET(cert, 0, certSz);
  25274. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  25275. #elif defined(USE_CERT_BUFFERS_1024)
  25276. unsigned char cert[sizeof(client_cert_der_1024)];
  25277. int certSz = (int)sizeof(cert);
  25278. XMEMSET(cert, 0, certSz);
  25279. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  25280. #else
  25281. unsigned char cert[ONEK_BUF];
  25282. XFILE fp = XBADFILE;
  25283. int certSz;
  25284. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  25285. XBADFILE);
  25286. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  25287. fp), 0);
  25288. if (fp != XBADFILE)
  25289. XFCLOSE(fp);
  25290. #endif
  25291. #elif defined(HAVE_ECC)
  25292. #if defined(USE_CERT_BUFFERS_256)
  25293. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25294. int certSz = (int)sizeof(cert);
  25295. XMEMSET(cert, 0, certSz);
  25296. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  25297. #else
  25298. unsigned char cert[ONEK_BUF];
  25299. XFILE fp = XBADFILE;
  25300. int certSz;
  25301. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  25302. XBADFILE);
  25303. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  25304. fp), 0);
  25305. if (fp != XBADFILE)
  25306. XFCLOSE(fp);
  25307. #endif
  25308. #else
  25309. #error PKCS7 requires ECC or RSA
  25310. #endif
  25311. #ifdef HAVE_ECC
  25312. {
  25313. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  25314. static unsigned char certWithInvalidEccKey[] = {
  25315. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  25316. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  25317. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  25318. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  25319. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  25320. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  25321. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  25322. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  25323. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  25324. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  25325. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  25326. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  25327. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25328. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25329. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25330. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  25331. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  25332. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  25333. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  25334. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  25335. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  25336. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  25337. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  25338. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  25339. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  25340. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  25341. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  25342. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  25343. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25344. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  25345. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  25346. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  25347. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  25348. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  25349. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  25350. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  25351. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  25352. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  25353. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  25354. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  25355. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  25356. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  25357. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  25358. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  25359. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  25360. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  25361. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  25362. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  25363. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  25364. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  25365. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  25366. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  25367. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  25368. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25369. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25370. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25371. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  25372. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  25373. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  25374. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  25375. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  25376. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  25377. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  25378. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  25379. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  25380. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  25381. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  25382. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  25383. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  25384. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  25385. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  25386. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  25387. 0x08, 0xA8, 0xB4
  25388. };
  25389. #endif
  25390. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25391. /* If initialization is not successful, it's free'd in init func. */
  25392. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  25393. 0);
  25394. wc_PKCS7_Free(pkcs7);
  25395. pkcs7 = NULL;
  25396. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25397. /* Valid initialization usage. */
  25398. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25399. /* Pass in bad args. No need free for null checks, free at end.*/
  25400. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  25401. BAD_FUNC_ARG);
  25402. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  25403. BAD_FUNC_ARG);
  25404. #ifdef HAVE_ECC
  25405. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  25406. sizeof(certWithInvalidEccKey)), 0);
  25407. }
  25408. #endif
  25409. wc_PKCS7_Free(pkcs7);
  25410. #endif
  25411. return EXPECT_RESULT();
  25412. } /* END test_wc_PKCS7_InitWithCert */
  25413. /*
  25414. * Testing wc_PKCS7_EncodeData()
  25415. */
  25416. static int test_wc_PKCS7_EncodeData(void)
  25417. {
  25418. EXPECT_DECLS;
  25419. #if defined(HAVE_PKCS7)
  25420. PKCS7* pkcs7 = NULL;
  25421. byte output[FOURK_BUF];
  25422. byte data[] = "My encoded DER cert.";
  25423. #ifndef NO_RSA
  25424. #if defined(USE_CERT_BUFFERS_2048)
  25425. unsigned char cert[sizeof(client_cert_der_2048)];
  25426. unsigned char key[sizeof(client_key_der_2048)];
  25427. int certSz = (int)sizeof(cert);
  25428. int keySz = (int)sizeof(key);
  25429. XMEMSET(cert, 0, certSz);
  25430. XMEMSET(key, 0, keySz);
  25431. XMEMCPY(cert, client_cert_der_2048, certSz);
  25432. XMEMCPY(key, client_key_der_2048, keySz);
  25433. #elif defined(USE_CERT_BUFFERS_1024)
  25434. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  25435. unsigned char key[sizeof_client_key_der_1024];
  25436. int certSz = (int)sizeof(cert);
  25437. int keySz = (int)sizeof(key);
  25438. XMEMSET(cert, 0, certSz);
  25439. XMEMSET(key, 0, keySz);
  25440. XMEMCPY(cert, client_cert_der_1024, certSz);
  25441. XMEMCPY(key, client_key_der_1024, keySz);
  25442. #else
  25443. unsigned char cert[ONEK_BUF];
  25444. unsigned char key[ONEK_BUF];
  25445. XFILE fp = XBADFILE;
  25446. int certSz;
  25447. int keySz;
  25448. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  25449. XBADFILE);
  25450. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  25451. fp), 0);
  25452. if (fp != XBADFILE) {
  25453. XFCLOSE(fp);
  25454. fp = XBADFILE;
  25455. }
  25456. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  25457. XBADFILE);
  25458. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  25459. 0);
  25460. if (fp != XBADFILE)
  25461. XFCLOSE(fp);
  25462. #endif
  25463. #elif defined(HAVE_ECC)
  25464. #if defined(USE_CERT_BUFFERS_256)
  25465. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25466. unsigned char key[sizeof(ecc_clikey_der_256)];
  25467. int certSz = (int)sizeof(cert);
  25468. int keySz = (int)sizeof(key);
  25469. XMEMSET(cert, 0, certSz);
  25470. XMEMSET(key, 0, keySz);
  25471. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  25472. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  25473. #else
  25474. unsigned char cert[ONEK_BUF];
  25475. unsigned char key[ONEK_BUF];
  25476. XFILE fp = XBADFILE;
  25477. int certSz, keySz;
  25478. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  25479. XBADFILE);
  25480. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  25481. fp), 0);
  25482. if (fp != XBADFILE) {
  25483. XFCLOSE(fp);
  25484. fp = XBADFILE;
  25485. }
  25486. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  25487. XBADFILE);
  25488. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  25489. 0);
  25490. if (fp != XBADFILE)
  25491. XFCLOSE(fp);
  25492. #endif
  25493. #endif
  25494. XMEMSET(output, 0, sizeof(output));
  25495. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25496. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25497. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  25498. if (pkcs7 != NULL) {
  25499. pkcs7->content = data;
  25500. pkcs7->contentSz = sizeof(data);
  25501. pkcs7->privateKey = key;
  25502. pkcs7->privateKeySz = (word32)keySz;
  25503. }
  25504. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  25505. /* Test bad args. */
  25506. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  25507. BAD_FUNC_ARG);
  25508. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  25509. BAD_FUNC_ARG);
  25510. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  25511. wc_PKCS7_Free(pkcs7);
  25512. #endif
  25513. return EXPECT_RESULT();
  25514. } /* END test_wc_PKCS7_EncodeData */
  25515. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  25516. !defined(NO_RSA) && !defined(NO_SHA256)
  25517. /* RSA sign raw digest callback */
  25518. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  25519. byte* out, word32 outSz, byte* privateKey,
  25520. word32 privateKeySz, int devid, int hashOID)
  25521. {
  25522. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  25523. byte digInfoEncoding[] = {
  25524. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  25525. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  25526. 0x00, 0x04, 0x20
  25527. };
  25528. int ret;
  25529. byte digestInfo[ONEK_BUF];
  25530. byte sig[FOURK_BUF];
  25531. word32 digestInfoSz = 0;
  25532. word32 idx = 0;
  25533. RsaKey rsa;
  25534. /* SHA-256 required only for this example callback due to above
  25535. * digInfoEncoding[] */
  25536. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  25537. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  25538. (hashOID != SHA256h)) {
  25539. return -1;
  25540. }
  25541. /* build DigestInfo */
  25542. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  25543. digestInfoSz += sizeof(digInfoEncoding);
  25544. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  25545. digestInfoSz += digestSz;
  25546. /* set up RSA key */
  25547. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  25548. if (ret != 0) {
  25549. return ret;
  25550. }
  25551. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  25552. /* sign DigestInfo */
  25553. if (ret == 0) {
  25554. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  25555. &rsa, pkcs7->rng);
  25556. if (ret > 0) {
  25557. if (ret > (int)outSz) {
  25558. /* output buffer too small */
  25559. ret = -1;
  25560. }
  25561. else {
  25562. /* success, ret holds sig size */
  25563. XMEMCPY(out, sig, ret);
  25564. }
  25565. }
  25566. }
  25567. wc_FreeRsaKey(&rsa);
  25568. return ret;
  25569. }
  25570. #endif
  25571. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  25572. typedef struct encodeSignedDataStream {
  25573. byte out[FOURK_BUF*3];
  25574. int idx;
  25575. word32 outIdx;
  25576. } encodeSignedDataStream;
  25577. /* content is 8k of partially created bundle */
  25578. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  25579. {
  25580. int ret = 0;
  25581. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  25582. if (strm->outIdx < pkcs7->contentSz) {
  25583. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  25584. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  25585. *content = strm->out + strm->outIdx;
  25586. strm->outIdx += ret;
  25587. }
  25588. (void)pkcs7;
  25589. return ret;
  25590. }
  25591. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  25592. void* ctx)
  25593. {
  25594. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  25595. XMEMCPY(strm->out + strm->idx, output, outputSz);
  25596. strm->idx += outputSz;
  25597. (void)pkcs7;
  25598. return 0;
  25599. }
  25600. #endif
  25601. /*
  25602. * Testing wc_PKCS7_EncodeSignedData()
  25603. */
  25604. static int test_wc_PKCS7_EncodeSignedData(void)
  25605. {
  25606. EXPECT_DECLS;
  25607. #if defined(HAVE_PKCS7)
  25608. PKCS7* pkcs7 = NULL;
  25609. WC_RNG rng;
  25610. byte output[FOURK_BUF];
  25611. byte badOut[1];
  25612. word32 outputSz = (word32)sizeof(output);
  25613. word32 badOutSz = 0;
  25614. byte data[] = "Test data to encode.";
  25615. #ifndef NO_RSA
  25616. #if defined(USE_CERT_BUFFERS_2048)
  25617. byte key[sizeof(client_key_der_2048)];
  25618. byte cert[sizeof(client_cert_der_2048)];
  25619. word32 keySz = (word32)sizeof(key);
  25620. word32 certSz = (word32)sizeof(cert);
  25621. XMEMSET(key, 0, keySz);
  25622. XMEMSET(cert, 0, certSz);
  25623. XMEMCPY(key, client_key_der_2048, keySz);
  25624. XMEMCPY(cert, client_cert_der_2048, certSz);
  25625. #elif defined(USE_CERT_BUFFERS_1024)
  25626. byte key[sizeof_client_key_der_1024];
  25627. byte cert[sizeof(sizeof_client_cert_der_1024)];
  25628. word32 keySz = (word32)sizeof(key);
  25629. word32 certSz = (word32)sizeof(cert);
  25630. XMEMSET(key, 0, keySz);
  25631. XMEMSET(cert, 0, certSz);
  25632. XMEMCPY(key, client_key_der_1024, keySz);
  25633. XMEMCPY(cert, client_cert_der_1024, certSz);
  25634. #else
  25635. unsigned char cert[ONEK_BUF];
  25636. unsigned char key[ONEK_BUF];
  25637. XFILE fp = XBADFILE;
  25638. int certSz;
  25639. int keySz;
  25640. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  25641. XBADFILE);
  25642. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  25643. fp), 0);
  25644. if (fp != XBADFILE) {
  25645. XFCLOSE(fp);
  25646. fp = XBADFILE;
  25647. }
  25648. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  25649. XBADFILE);
  25650. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  25651. 0);
  25652. if (fp != XBADFILE)
  25653. XFCLOSE(fp);
  25654. #endif
  25655. #elif defined(HAVE_ECC)
  25656. #if defined(USE_CERT_BUFFERS_256)
  25657. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25658. unsigned char key[sizeof(ecc_clikey_der_256)];
  25659. int certSz = (int)sizeof(cert);
  25660. int keySz = (int)sizeof(key);
  25661. XMEMSET(cert, 0, certSz);
  25662. XMEMSET(key, 0, keySz);
  25663. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  25664. XMEMCPY(key, ecc_clikey_der_256, keySz);
  25665. #else
  25666. unsigned char cert[ONEK_BUF];
  25667. unsigned char key[ONEK_BUF];
  25668. XFILE fp = XBADFILE;
  25669. int certSz;
  25670. int keySz;
  25671. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  25672. XBADFILE);
  25673. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  25674. if (fp != XBADFILE) {
  25675. XFCLOSE(fp);
  25676. fp = XBADFILE;
  25677. }
  25678. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  25679. XBADFILE);
  25680. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  25681. if (fp != XBADFILE)
  25682. XFCLOSE(fp);
  25683. #endif
  25684. #endif
  25685. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25686. XMEMSET(output, 0, outputSz);
  25687. ExpectIntEQ(wc_InitRng(&rng), 0);
  25688. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25689. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25690. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25691. if (pkcs7 != NULL) {
  25692. pkcs7->content = data;
  25693. pkcs7->contentSz = (word32)sizeof(data);
  25694. pkcs7->privateKey = key;
  25695. pkcs7->privateKeySz = (word32)sizeof(key);
  25696. pkcs7->encryptOID = RSAk;
  25697. #ifdef NO_SHA
  25698. pkcs7->hashOID = SHA256h;
  25699. #else
  25700. pkcs7->hashOID = SHAh;
  25701. #endif
  25702. pkcs7->rng = &rng;
  25703. }
  25704. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  25705. wc_PKCS7_Free(pkcs7);
  25706. pkcs7 = NULL;
  25707. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25708. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25709. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25710. #ifdef ASN_BER_TO_DER
  25711. wc_PKCS7_Free(pkcs7);
  25712. /* reinitialize and test setting stream mode */
  25713. {
  25714. int signedSz = 0;
  25715. encodeSignedDataStream strm;
  25716. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25717. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25718. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25719. if (pkcs7 != NULL) {
  25720. pkcs7->content = data;
  25721. pkcs7->contentSz = (word32)sizeof(data);
  25722. pkcs7->privateKey = key;
  25723. pkcs7->privateKeySz = (word32)sizeof(key);
  25724. pkcs7->encryptOID = RSAk;
  25725. #ifdef NO_SHA
  25726. pkcs7->hashOID = SHA256h;
  25727. #else
  25728. pkcs7->hashOID = SHAh;
  25729. #endif
  25730. pkcs7->rng = &rng;
  25731. }
  25732. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25733. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25734. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  25735. BAD_FUNC_ARG);
  25736. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  25737. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  25738. outputSz), 0);
  25739. wc_PKCS7_Free(pkcs7);
  25740. pkcs7 = NULL;
  25741. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25742. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25743. /* use exact signed buffer size since BER encoded */
  25744. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)signedSz), 0);
  25745. wc_PKCS7_Free(pkcs7);
  25746. /* now try with using callbacks for IO */
  25747. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25748. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25749. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25750. if (pkcs7 != NULL) {
  25751. pkcs7->contentSz = FOURK_BUF*2;
  25752. pkcs7->privateKey = key;
  25753. pkcs7->privateKeySz = (word32)sizeof(key);
  25754. pkcs7->encryptOID = RSAk;
  25755. #ifdef NO_SHA
  25756. pkcs7->hashOID = SHA256h;
  25757. #else
  25758. pkcs7->hashOID = SHAh;
  25759. #endif
  25760. pkcs7->rng = &rng;
  25761. }
  25762. XMEMSET(&strm, 0, sizeof(strm));
  25763. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25764. StreamOutputCB, (void*)&strm), 0);
  25765. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  25766. wc_PKCS7_Free(pkcs7);
  25767. pkcs7 = NULL;
  25768. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25769. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25770. /* use exact signed buffer size since BER encoded */
  25771. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, (word32)signedSz), 0);
  25772. }
  25773. #endif
  25774. #ifndef NO_PKCS7_STREAM
  25775. wc_PKCS7_Free(pkcs7);
  25776. pkcs7 = NULL;
  25777. {
  25778. word32 z;
  25779. int ret;
  25780. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25781. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25782. /* test for streaming mode */
  25783. ret = -1;
  25784. for (z = 0; z < outputSz && ret != 0; z++) {
  25785. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25786. if (ret < 0){
  25787. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25788. }
  25789. }
  25790. ExpectIntEQ(ret, 0);
  25791. ExpectIntNE(pkcs7->contentSz, 0);
  25792. ExpectNotNull(pkcs7->contentDynamic);
  25793. }
  25794. #endif /* !NO_PKCS7_STREAM */
  25795. /* Pass in bad args. */
  25796. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  25797. BAD_FUNC_ARG);
  25798. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  25799. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  25800. badOutSz), BAD_FUNC_ARG);
  25801. if (pkcs7 != NULL) {
  25802. pkcs7->hashOID = 0; /* bad hashOID */
  25803. }
  25804. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  25805. BAD_FUNC_ARG);
  25806. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  25807. !defined(NO_RSA) && !defined(NO_SHA256)
  25808. /* test RSA sign raw digest callback, if using RSA and compiled in.
  25809. * Example callback assumes SHA-256, so only run test if compiled in. */
  25810. wc_PKCS7_Free(pkcs7);
  25811. pkcs7 = NULL;
  25812. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25813. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25814. if (pkcs7 != NULL) {
  25815. pkcs7->content = data;
  25816. pkcs7->contentSz = (word32)sizeof(data);
  25817. pkcs7->privateKey = key;
  25818. pkcs7->privateKeySz = (word32)sizeof(key);
  25819. pkcs7->encryptOID = RSAk;
  25820. pkcs7->hashOID = SHA256h;
  25821. pkcs7->rng = &rng;
  25822. }
  25823. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  25824. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  25825. #endif
  25826. wc_PKCS7_Free(pkcs7);
  25827. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25828. #endif
  25829. return EXPECT_RESULT();
  25830. } /* END test_wc_PKCS7_EncodeSignedData */
  25831. /*
  25832. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  25833. */
  25834. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  25835. {
  25836. EXPECT_DECLS;
  25837. #if defined(HAVE_PKCS7)
  25838. int i;
  25839. PKCS7* pkcs7 = NULL;
  25840. WC_RNG rng;
  25841. byte outputHead[FOURK_BUF/2];
  25842. byte outputFoot[FOURK_BUF/2];
  25843. word32 outputHeadSz = (word32)sizeof(outputHead);
  25844. word32 outputFootSz = (word32)sizeof(outputFoot);
  25845. byte data[FOURK_BUF];
  25846. wc_HashAlg hash;
  25847. #ifdef NO_SHA
  25848. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25849. #else
  25850. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25851. #endif
  25852. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25853. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  25854. #ifndef NO_RSA
  25855. #if defined(USE_CERT_BUFFERS_2048)
  25856. byte key[sizeof(client_key_der_2048)];
  25857. byte cert[sizeof(client_cert_der_2048)];
  25858. word32 keySz = (word32)sizeof(key);
  25859. word32 certSz = (word32)sizeof(cert);
  25860. XMEMSET(key, 0, keySz);
  25861. XMEMSET(cert, 0, certSz);
  25862. XMEMCPY(key, client_key_der_2048, keySz);
  25863. XMEMCPY(cert, client_cert_der_2048, certSz);
  25864. #elif defined(USE_CERT_BUFFERS_1024)
  25865. byte key[sizeof_client_key_der_1024];
  25866. byte cert[sizeof(sizeof_client_cert_der_1024)];
  25867. word32 keySz = (word32)sizeof(key);
  25868. word32 certSz = (word32)sizeof(cert);
  25869. XMEMSET(key, 0, keySz);
  25870. XMEMSET(cert, 0, certSz);
  25871. XMEMCPY(key, client_key_der_1024, keySz);
  25872. XMEMCPY(cert, client_cert_der_1024, certSz);
  25873. #else
  25874. unsigned char cert[ONEK_BUF];
  25875. unsigned char key[ONEK_BUF];
  25876. XFILE fp = XBADFILE;
  25877. int certSz;
  25878. int keySz;
  25879. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  25880. XBADFILE);
  25881. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  25882. fp), 0);
  25883. if (fp != XBADFILE) {
  25884. XFCLOSE(fp);
  25885. fp = XBADFILE;
  25886. }
  25887. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  25888. XBADFILE);
  25889. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  25890. 0);
  25891. if (fp != XBADFILE)
  25892. XFCLOSE(fp);
  25893. #endif
  25894. #elif defined(HAVE_ECC)
  25895. #if defined(USE_CERT_BUFFERS_256)
  25896. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25897. unsigned char key[sizeof(ecc_clikey_der_256)];
  25898. int certSz = (int)sizeof(cert);
  25899. int keySz = (int)sizeof(key);
  25900. XMEMSET(cert, 0, certSz);
  25901. XMEMSET(key, 0, keySz);
  25902. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  25903. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  25904. #else
  25905. unsigned char cert[ONEK_BUF];
  25906. unsigned char key[ONEK_BUF];
  25907. XFILE fp = XBADFILE;
  25908. int certSz;
  25909. int keySz;
  25910. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  25911. XBADFILE);
  25912. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  25913. fp), 0);
  25914. if (fp != XBADFILE) {
  25915. XFCLOSE(fp);
  25916. fp = XBADFILE;
  25917. }
  25918. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  25919. XBADFILE);
  25920. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  25921. 0);
  25922. if (fp != XBADFILE)
  25923. XFCLOSE(fp);
  25924. #endif
  25925. #endif
  25926. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25927. /* initialize large data with sequence */
  25928. for (i=0; i<(int)sizeof(data); i++)
  25929. data[i] = i & 0xff;
  25930. XMEMSET(outputHead, 0, outputHeadSz);
  25931. XMEMSET(outputFoot, 0, outputFootSz);
  25932. ExpectIntEQ(wc_InitRng(&rng), 0);
  25933. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25934. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25935. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25936. if (pkcs7 != NULL) {
  25937. pkcs7->content = NULL; /* not used for ex */
  25938. pkcs7->contentSz = (word32)sizeof(data);
  25939. pkcs7->privateKey = key;
  25940. pkcs7->privateKeySz = (word32)sizeof(key);
  25941. pkcs7->encryptOID = RSAk;
  25942. #ifdef NO_SHA
  25943. pkcs7->hashOID = SHA256h;
  25944. #else
  25945. pkcs7->hashOID = SHAh;
  25946. #endif
  25947. pkcs7->rng = &rng;
  25948. }
  25949. /* calculate hash for content */
  25950. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  25951. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25952. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25953. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25954. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25955. /* Perform PKCS7 sign using hash directly */
  25956. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25957. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  25958. ExpectIntGT(outputHeadSz, 0);
  25959. ExpectIntGT(outputFootSz, 0);
  25960. wc_PKCS7_Free(pkcs7);
  25961. pkcs7 = NULL;
  25962. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25963. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25964. /* required parameter even on verify when using _ex, if using outputHead
  25965. * and outputFoot */
  25966. if (pkcs7 != NULL) {
  25967. pkcs7->contentSz = (word32)sizeof(data);
  25968. }
  25969. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25970. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  25971. wc_PKCS7_Free(pkcs7);
  25972. pkcs7 = NULL;
  25973. /* assembly complete PKCS7 sign and use normal verify */
  25974. {
  25975. byte* output = NULL;
  25976. word32 outputSz = 0;
  25977. #ifndef NO_PKCS7_STREAM
  25978. word32 z;
  25979. int ret;
  25980. #endif /* !NO_PKCS7_STREAM */
  25981. ExpectNotNull(output = (byte*)XMALLOC(
  25982. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  25983. DYNAMIC_TYPE_TMP_BUFFER));
  25984. if (output != NULL) {
  25985. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  25986. outputSz += outputHeadSz;
  25987. XMEMCPY(&output[outputSz], data, sizeof(data));
  25988. outputSz += sizeof(data);
  25989. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  25990. outputSz += outputFootSz;
  25991. }
  25992. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25993. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25994. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25995. #ifndef NO_PKCS7_STREAM
  25996. wc_PKCS7_Free(pkcs7);
  25997. pkcs7 = NULL;
  25998. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25999. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26000. /* test for streaming mode */
  26001. ret = -1;
  26002. for (z = 0; z < outputSz && ret != 0; z++) {
  26003. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26004. if (ret < 0){
  26005. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26006. }
  26007. }
  26008. ExpectIntEQ(ret, 0);
  26009. ExpectIntNE(pkcs7->contentSz, 0);
  26010. ExpectNotNull(pkcs7->contentDynamic);
  26011. wc_PKCS7_Free(pkcs7);
  26012. pkcs7 = NULL;
  26013. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26014. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26015. #endif /* !NO_PKCS7_STREAM */
  26016. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26017. }
  26018. /* Pass in bad args. */
  26019. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  26020. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  26021. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  26022. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  26023. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  26024. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  26025. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  26026. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  26027. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  26028. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  26029. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  26030. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  26031. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  26032. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  26033. if (pkcs7 != NULL) {
  26034. pkcs7->hashOID = 0; /* bad hashOID */
  26035. }
  26036. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  26037. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  26038. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  26039. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  26040. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  26041. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  26042. #ifndef NO_PKCS7_STREAM
  26043. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  26044. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  26045. #else
  26046. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  26047. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  26048. #endif
  26049. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  26050. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  26051. #ifndef NO_PKCS7_STREAM
  26052. /* can pass in 0 buffer length with streaming API */
  26053. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26054. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  26055. #else
  26056. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26057. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  26058. #endif
  26059. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26060. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  26061. #ifndef NO_PKCS7_STREAM
  26062. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26063. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  26064. #else
  26065. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26066. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  26067. #endif
  26068. wc_PKCS7_Free(pkcs7);
  26069. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  26070. #endif
  26071. return EXPECT_RESULT();
  26072. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  26073. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26074. /**
  26075. * Loads certs/keys from files or buffers into the argument buffers,
  26076. * helper function called by CreatePKCS7SignedData().
  26077. *
  26078. * Returns 0 on success, negative on error.
  26079. */
  26080. static int LoadPKCS7SignedDataCerts(
  26081. int useIntermediateCertChain, int pkAlgoType,
  26082. byte* intCARoot, word32* intCARootSz,
  26083. byte* intCA1, word32* intCA1Sz,
  26084. byte* intCA2, word32* intCA2Sz,
  26085. byte* cert, word32* certSz,
  26086. byte* key, word32* keySz)
  26087. {
  26088. EXPECT_DECLS;
  26089. int ret = 0;
  26090. XFILE fp = XBADFILE;
  26091. #ifndef NO_RSA
  26092. const char* intCARootRSA = "./certs/ca-cert.der";
  26093. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  26094. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  26095. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  26096. const char* intServKeyRSA = "./certs/server-key.der";
  26097. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  26098. const char* cli1024Cert = "./certs/1024/client-cert.der";
  26099. const char* cli1024Key = "./certs/1024/client-key.der";
  26100. #endif
  26101. #endif
  26102. #ifdef HAVE_ECC
  26103. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  26104. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  26105. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  26106. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  26107. const char* intServKeyECC = "./certs/ecc-key.der";
  26108. #ifndef USE_CERT_BUFFERS_256
  26109. const char* cliEccCert = "./certs/client-ecc-cert.der";
  26110. const char* cliEccKey = "./certs/client-ecc-key.der";
  26111. #endif
  26112. #endif
  26113. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  26114. ((useIntermediateCertChain == 1) &&
  26115. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  26116. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  26117. return BAD_FUNC_ARG;
  26118. }
  26119. /* Read/load certs and keys to use for signing based on PK type and chain */
  26120. switch (pkAlgoType) {
  26121. #ifndef NO_RSA
  26122. case RSA_TYPE:
  26123. if (useIntermediateCertChain == 1) {
  26124. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  26125. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  26126. if (fp != XBADFILE) {
  26127. XFCLOSE(fp);
  26128. fp = XBADFILE;
  26129. }
  26130. ExpectIntGT(*intCARootSz, 0);
  26131. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  26132. if (fp != XBADFILE) {
  26133. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  26134. XFCLOSE(fp);
  26135. fp = XBADFILE;
  26136. }
  26137. ExpectIntGT(*intCA1Sz, 0);
  26138. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  26139. if (fp != XBADFILE) {
  26140. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  26141. XFCLOSE(fp);
  26142. fp = XBADFILE;
  26143. }
  26144. ExpectIntGT(*intCA2Sz, 0);
  26145. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  26146. if (fp != XBADFILE) {
  26147. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  26148. XFCLOSE(fp);
  26149. fp = XBADFILE;
  26150. }
  26151. ExpectIntGT(*certSz, 0);
  26152. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  26153. if (fp != XBADFILE) {
  26154. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  26155. XFCLOSE(fp);
  26156. fp = XBADFILE;
  26157. }
  26158. ExpectIntGT(*keySz, 0);
  26159. }
  26160. else {
  26161. #if defined(USE_CERT_BUFFERS_2048)
  26162. *keySz = sizeof_client_key_der_2048;
  26163. *certSz = sizeof_client_cert_der_2048;
  26164. XMEMCPY(key, client_key_der_2048, *keySz);
  26165. XMEMCPY(cert, client_cert_der_2048, *certSz);
  26166. #elif defined(USE_CERT_BUFFERS_1024)
  26167. *keySz = sizeof_client_key_der_1024;
  26168. *certSz = sizeof_client_cert_der_1024;
  26169. XMEMCPY(key, client_key_der_1024, *keySz);
  26170. XMEMCPY(cert, client_cert_der_1024, *certSz);
  26171. #else
  26172. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  26173. if (fp != XBADFILE) {
  26174. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  26175. XFCLOSE(fp);
  26176. fp = XBADFILE;
  26177. }
  26178. ExpectIntGT(*keySz, 0);
  26179. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  26180. if (fp != XBADFILE) {
  26181. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  26182. XFCLOSE(fp);
  26183. fp = XBADFILE;
  26184. }
  26185. ExpectIntGT(*certSz, 0);
  26186. #endif /* USE_CERT_BUFFERS_2048 */
  26187. }
  26188. break;
  26189. #endif /* !NO_RSA */
  26190. #ifdef HAVE_ECC
  26191. case ECC_TYPE:
  26192. if (useIntermediateCertChain == 1) {
  26193. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  26194. if (fp != XBADFILE) {
  26195. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  26196. fp);
  26197. XFCLOSE(fp);
  26198. fp = XBADFILE;
  26199. }
  26200. ExpectIntGT(*intCARootSz, 0);
  26201. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  26202. if (fp != XBADFILE) {
  26203. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  26204. XFCLOSE(fp);
  26205. fp = XBADFILE;
  26206. }
  26207. ExpectIntGT(*intCA1Sz, 0);
  26208. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  26209. if (fp != XBADFILE) {
  26210. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  26211. XFCLOSE(fp);
  26212. fp = XBADFILE;
  26213. }
  26214. ExpectIntGT(*intCA2Sz, 0);
  26215. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  26216. if (fp != XBADFILE) {
  26217. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  26218. XFCLOSE(fp);
  26219. fp = XBADFILE;
  26220. }
  26221. ExpectIntGT(*certSz, 0);
  26222. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  26223. if (fp != XBADFILE) {
  26224. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  26225. XFCLOSE(fp);
  26226. fp = XBADFILE;
  26227. }
  26228. ExpectIntGT(*keySz, 0);
  26229. }
  26230. else {
  26231. #if defined(USE_CERT_BUFFERS_256)
  26232. *keySz = sizeof_ecc_clikey_der_256;
  26233. *certSz = sizeof_cliecc_cert_der_256;
  26234. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  26235. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  26236. #else
  26237. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  26238. if (fp != XBADFILE) {
  26239. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  26240. XFCLOSE(fp);
  26241. fp = XBADFILE;
  26242. }
  26243. ExpectIntGT(*keySz, 0);
  26244. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  26245. if (fp != XBADFILE) {
  26246. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  26247. XFCLOSE(fp);
  26248. fp = XBADFILE;
  26249. }
  26250. ExpectIntGT(*certSz, 0);
  26251. #endif /* USE_CERT_BUFFERS_256 */
  26252. }
  26253. break;
  26254. #endif /* HAVE_ECC */
  26255. default:
  26256. WOLFSSL_MSG("Unsupported SignedData PK type");
  26257. ret = BAD_FUNC_ARG;
  26258. break;
  26259. }
  26260. if (EXPECT_FAIL() && (ret == 0)) {
  26261. ret = BAD_FUNC_ARG;
  26262. }
  26263. return ret;
  26264. }
  26265. /**
  26266. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  26267. *
  26268. * output output buffer to place SignedData
  26269. * outputSz size of output buffer
  26270. * data data buffer to be signed
  26271. * dataSz size of data buffer
  26272. * withAttribs [1/0] include attributes in SignedData message
  26273. * detachedSig [1/0] create detached signature, no content
  26274. * useIntCertChain [1/0] use certificate chain and include intermediate and
  26275. * root CAs in bundle
  26276. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  26277. *
  26278. * Return size of bundle created on success, negative on error */
  26279. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  26280. byte* data, word32 dataSz,
  26281. int withAttribs, int detachedSig,
  26282. int useIntermediateCertChain,
  26283. int pkAlgoType)
  26284. {
  26285. EXPECT_DECLS;
  26286. int ret = 0;
  26287. WC_RNG rng;
  26288. PKCS7* pkcs7 = NULL;
  26289. static byte messageTypeOid[] =
  26290. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  26291. 0x09, 0x02 };
  26292. static byte messageType[] = { 0x13, 2, '1', '9' };
  26293. PKCS7Attrib attribs[] =
  26294. {
  26295. { messageTypeOid, sizeof(messageTypeOid), messageType,
  26296. sizeof(messageType) }
  26297. };
  26298. byte intCARoot[TWOK_BUF];
  26299. byte intCA1[TWOK_BUF];
  26300. byte intCA2[TWOK_BUF];
  26301. byte cert[TWOK_BUF];
  26302. byte key[TWOK_BUF];
  26303. word32 intCARootSz = sizeof(intCARoot);
  26304. word32 intCA1Sz = sizeof(intCA1);
  26305. word32 intCA2Sz = sizeof(intCA2);
  26306. word32 certSz = sizeof(cert);
  26307. word32 keySz = sizeof(key);
  26308. XMEMSET(intCARoot, 0, intCARootSz);
  26309. XMEMSET(intCA1, 0, intCA1Sz);
  26310. XMEMSET(intCA2, 0, intCA2Sz);
  26311. XMEMSET(cert, 0, certSz);
  26312. XMEMSET(key, 0, keySz);
  26313. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  26314. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  26315. cert, &certSz, key, &keySz);
  26316. ExpectIntEQ(ret, 0);
  26317. XMEMSET(output, 0, outputSz);
  26318. ExpectIntEQ(wc_InitRng(&rng), 0);
  26319. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26320. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26321. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26322. if (useIntermediateCertChain == 1) {
  26323. /* Add intermediate and root CA certs into SignedData Certs SET */
  26324. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  26325. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  26326. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  26327. }
  26328. if (pkcs7 != NULL) {
  26329. pkcs7->content = data;
  26330. pkcs7->contentSz = dataSz;
  26331. pkcs7->privateKey = key;
  26332. pkcs7->privateKeySz = (word32)sizeof(key);
  26333. if (pkAlgoType == RSA_TYPE) {
  26334. pkcs7->encryptOID = RSAk;
  26335. }
  26336. else {
  26337. pkcs7->encryptOID = ECDSAk;
  26338. }
  26339. #ifdef NO_SHA
  26340. pkcs7->hashOID = SHA256h;
  26341. #else
  26342. pkcs7->hashOID = SHAh;
  26343. #endif
  26344. pkcs7->rng = &rng;
  26345. if (withAttribs) {
  26346. /* include a signed attribute */
  26347. pkcs7->signedAttribs = attribs;
  26348. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  26349. }
  26350. }
  26351. if (detachedSig) {
  26352. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  26353. }
  26354. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, (word32)outputSz);
  26355. ExpectIntGT(outputSz, 0);
  26356. wc_PKCS7_Free(pkcs7);
  26357. pkcs7 = NULL;
  26358. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26359. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26360. if (detachedSig && (pkcs7 != NULL)) {
  26361. pkcs7->content = data;
  26362. pkcs7->contentSz = dataSz;
  26363. }
  26364. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)outputSz), 0);
  26365. wc_PKCS7_Free(pkcs7);
  26366. wc_FreeRng(&rng);
  26367. if (EXPECT_FAIL()) {
  26368. outputSz = 0;
  26369. }
  26370. return outputSz;
  26371. }
  26372. #endif
  26373. /*
  26374. * Testing wc_PKCS_VerifySignedData()
  26375. */
  26376. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  26377. {
  26378. EXPECT_DECLS;
  26379. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26380. PKCS7* pkcs7 = NULL;
  26381. byte output[6000]; /* Large size needed for bundles with int CA certs */
  26382. word32 outputSz = sizeof(output);
  26383. byte data[] = "Test data to encode.";
  26384. byte badOut[1];
  26385. word32 badOutSz = 0;
  26386. byte badContent[] = "This is different content than was signed";
  26387. wc_HashAlg hash;
  26388. #ifdef NO_SHA
  26389. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  26390. #else
  26391. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  26392. #endif
  26393. byte hashBuf[WC_MAX_DIGEST_SIZE];
  26394. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  26395. #ifndef NO_RSA
  26396. PKCS7DecodedAttrib* decodedAttrib = NULL;
  26397. /* contentType OID (1.2.840.113549.1.9.3) */
  26398. static const byte contentTypeOid[] =
  26399. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  26400. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  26401. static const byte dataType[] =
  26402. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  26403. /* messageDigest OID (1.2.840.113549.1.9.4) */
  26404. static const byte messageDigestOid[] =
  26405. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  26406. #ifndef NO_ASN_TIME
  26407. /* signingTime OID () */
  26408. static const byte signingTimeOid[] =
  26409. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  26410. #endif
  26411. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  26412. int dateLength = 0;
  26413. byte dateFormat;
  26414. const byte* datePart = NULL;
  26415. struct tm timearg;
  26416. time_t now;
  26417. struct tm* nowTm = NULL;
  26418. #ifdef NEED_TMP_TIME
  26419. struct tm tmpTimeStorage;
  26420. struct tm* tmpTime = &tmpTimeStorage;
  26421. #endif
  26422. #endif /* !NO_ASN && !NO_ASN_TIME */
  26423. #ifndef NO_PKCS7_STREAM
  26424. word32 z;
  26425. int ret;
  26426. #endif /* !NO_PKCS7_STREAM */
  26427. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  26428. /* Success test with RSA certs/key */
  26429. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26430. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  26431. /* calculate hash for content, used later */
  26432. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  26433. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  26434. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  26435. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  26436. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26437. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26438. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26439. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26440. #ifndef NO_PKCS7_STREAM
  26441. wc_PKCS7_Free(pkcs7);
  26442. pkcs7 = NULL;
  26443. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26444. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26445. /* test for streaming */
  26446. ret = -1;
  26447. for (z = 0; z < outputSz && ret != 0; z++) {
  26448. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26449. if (ret < 0){
  26450. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26451. }
  26452. }
  26453. ExpectIntEQ(ret, 0);
  26454. ExpectIntNE(pkcs7->contentSz, 0);
  26455. ExpectNotNull(pkcs7->contentDynamic);
  26456. #endif /* !NO_PKCS7_STREAM */
  26457. /* Check that decoded signed attributes are correct */
  26458. /* messageDigest should be first */
  26459. if (pkcs7 != NULL) {
  26460. decodedAttrib = pkcs7->decodedAttrib;
  26461. }
  26462. ExpectNotNull(decodedAttrib);
  26463. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  26464. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  26465. decodedAttrib->oidSz), 0);
  26466. /* + 2 for OCTET STRING and length bytes */
  26467. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  26468. ExpectNotNull(decodedAttrib->value);
  26469. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  26470. #ifndef NO_ASN_TIME
  26471. /* signingTime should be second */
  26472. if (decodedAttrib != NULL) {
  26473. decodedAttrib = decodedAttrib->next;
  26474. }
  26475. ExpectNotNull(decodedAttrib);
  26476. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  26477. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  26478. decodedAttrib->oidSz), 0);
  26479. ExpectIntGT(decodedAttrib->valueSz, 0);
  26480. ExpectNotNull(decodedAttrib->value);
  26481. #endif
  26482. /* Verify signingTime if ASN and time are available */
  26483. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  26484. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  26485. &datePart, &dateFormat, &dateLength), 0);
  26486. ExpectNotNull(datePart);
  26487. ExpectIntGT(dateLength, 0);
  26488. XMEMSET(&timearg, 0, sizeof(timearg));
  26489. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  26490. &timearg), 0);
  26491. /* Get current time and compare year/month/day against attribute value */
  26492. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  26493. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  26494. ExpectNotNull(nowTm);
  26495. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  26496. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  26497. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  26498. #endif /* !NO_ASN && !NO_ASN_TIME */
  26499. /* contentType should be third */
  26500. if (decodedAttrib != NULL) {
  26501. decodedAttrib = decodedAttrib->next;
  26502. }
  26503. ExpectNotNull(decodedAttrib);
  26504. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  26505. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  26506. decodedAttrib->oidSz), 0);
  26507. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  26508. ExpectNotNull(decodedAttrib->value);
  26509. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  26510. 0);
  26511. #endif /* !NO_RSA */
  26512. /* Test bad args. */
  26513. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  26514. BAD_FUNC_ARG);
  26515. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  26516. BAD_FUNC_ARG);
  26517. #ifndef NO_PKCS7_STREAM
  26518. /* can pass in 0 buffer length with streaming API */
  26519. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  26520. badOutSz), WC_PKCS7_WANT_READ_E);
  26521. #else
  26522. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  26523. badOutSz), BAD_FUNC_ARG);
  26524. #endif
  26525. wc_PKCS7_Free(pkcs7);
  26526. pkcs7 = NULL;
  26527. #ifndef NO_RSA
  26528. /* Try RSA certs/key/sig first */
  26529. outputSz = sizeof(output);
  26530. XMEMSET(output, 0, outputSz);
  26531. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26532. (word32)sizeof(data),
  26533. 1, 1, 0, RSA_TYPE)), 0);
  26534. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26535. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26536. if (pkcs7 != NULL) {
  26537. pkcs7->content = badContent;
  26538. pkcs7->contentSz = sizeof(badContent);
  26539. }
  26540. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  26541. SIG_VERIFY_E);
  26542. wc_PKCS7_Free(pkcs7);
  26543. pkcs7 = NULL;
  26544. #ifndef NO_PKCS7_STREAM
  26545. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26546. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26547. if (pkcs7 != NULL) {
  26548. pkcs7->content = badContent;
  26549. pkcs7->contentSz = sizeof(badContent);
  26550. }
  26551. /* test for streaming */
  26552. ret = -1;
  26553. for (z = 0; z < outputSz && ret != 0; z++) {
  26554. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26555. if (ret == WC_PKCS7_WANT_READ_E){
  26556. continue;
  26557. }
  26558. else if (ret < 0) {
  26559. break;
  26560. }
  26561. }
  26562. ExpectIntEQ(ret, SIG_VERIFY_E);
  26563. ExpectIntNE(pkcs7->contentSz, 0);
  26564. ExpectNotNull(pkcs7->contentDynamic);
  26565. wc_PKCS7_Free(pkcs7);
  26566. pkcs7 = NULL;
  26567. #endif /* !NO_PKCS7_STREAM */
  26568. /* Test success case with detached signature and valid content */
  26569. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26570. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26571. if (pkcs7 != NULL) {
  26572. pkcs7->content = data;
  26573. pkcs7->contentSz = sizeof(data);
  26574. }
  26575. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26576. wc_PKCS7_Free(pkcs7);
  26577. pkcs7 = NULL;
  26578. #ifndef NO_PKCS7_STREAM
  26579. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26580. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26581. if (pkcs7 != NULL) {
  26582. pkcs7->content = data;
  26583. pkcs7->contentSz = sizeof(data);
  26584. }
  26585. /* test for streaming */
  26586. ret = -1;
  26587. for (z = 0; z < outputSz && ret != 0; z++) {
  26588. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26589. if (ret < 0){
  26590. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26591. }
  26592. }
  26593. ExpectIntEQ(ret, 0);
  26594. ExpectIntNE(pkcs7->contentSz, 0);
  26595. ExpectNotNull(pkcs7->contentDynamic);
  26596. wc_PKCS7_Free(pkcs7);
  26597. pkcs7 = NULL;
  26598. #endif /* !NO_PKCS7_STREAM */
  26599. /* verify using pre-computed content digest only (no content) */
  26600. {
  26601. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26602. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  26603. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26604. output, outputSz, NULL, 0), 0);
  26605. wc_PKCS7_Free(pkcs7);
  26606. pkcs7 = NULL;
  26607. }
  26608. #endif /* !NO_RSA */
  26609. /* Test verify on signedData containing intermediate/root CA certs */
  26610. #ifndef NO_RSA
  26611. outputSz = sizeof(output);
  26612. XMEMSET(output, 0, outputSz);
  26613. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26614. (word32)sizeof(data),
  26615. 0, 0, 1, RSA_TYPE)), 0);
  26616. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26617. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26618. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26619. wc_PKCS7_Free(pkcs7);
  26620. pkcs7 = NULL;
  26621. #ifndef NO_PKCS7_STREAM
  26622. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26623. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26624. /* test for streaming */
  26625. ret = -1;
  26626. for (z = 0; z < outputSz && ret != 0; z++) {
  26627. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26628. if (ret < 0){
  26629. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26630. }
  26631. }
  26632. ExpectIntEQ(ret, 0);
  26633. ExpectIntNE(pkcs7->contentSz, 0);
  26634. ExpectNotNull(pkcs7->contentDynamic);
  26635. wc_PKCS7_Free(pkcs7);
  26636. pkcs7 = NULL;
  26637. #endif /* !NO_PKCS7_STREAM */
  26638. #endif /* !NO_RSA */
  26639. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  26640. !defined(NO_FILESYSTEM)
  26641. {
  26642. XFILE signedBundle = XBADFILE;
  26643. int signedBundleSz = 0;
  26644. int chunkSz = 1;
  26645. int i, rc = 0;
  26646. byte* buf = NULL;
  26647. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  26648. "rb")) != XBADFILE);
  26649. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  26650. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  26651. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  26652. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  26653. DYNAMIC_TYPE_FILE));
  26654. if (buf != NULL) {
  26655. ExpectIntEQ(XFREAD(buf, 1, (size_t)signedBundleSz, signedBundle),
  26656. signedBundleSz);
  26657. }
  26658. if (signedBundle != XBADFILE) {
  26659. XFCLOSE(signedBundle);
  26660. signedBundle = XBADFILE;
  26661. }
  26662. if (buf != NULL) {
  26663. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26664. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26665. for (i = 0; i < signedBundleSz;) {
  26666. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  26667. chunkSz;
  26668. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  26669. if (rc < 0 ) {
  26670. if (rc == WC_PKCS7_WANT_READ_E) {
  26671. i += sz;
  26672. continue;
  26673. }
  26674. break;
  26675. }
  26676. else {
  26677. break;
  26678. }
  26679. }
  26680. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  26681. wc_PKCS7_Free(pkcs7);
  26682. pkcs7 = NULL;
  26683. }
  26684. /* now try with malformed bundle */
  26685. if (buf != NULL) {
  26686. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26687. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26688. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  26689. for (i = 0; i < signedBundleSz;) {
  26690. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  26691. chunkSz;
  26692. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  26693. if (rc < 0 ) {
  26694. if (rc == WC_PKCS7_WANT_READ_E) {
  26695. i += sz;
  26696. continue;
  26697. }
  26698. break;
  26699. }
  26700. else {
  26701. break;
  26702. }
  26703. }
  26704. ExpectIntEQ(rc, ASN_PARSE_E);
  26705. wc_PKCS7_Free(pkcs7);
  26706. pkcs7 = NULL;
  26707. }
  26708. if (buf != NULL)
  26709. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  26710. }
  26711. #endif /* BER and stream */
  26712. #endif
  26713. return EXPECT_RESULT();
  26714. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  26715. /*
  26716. * Testing wc_PKCS_VerifySignedData()
  26717. */
  26718. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  26719. {
  26720. EXPECT_DECLS;
  26721. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  26722. PKCS7* pkcs7 = NULL;
  26723. byte output[6000]; /* Large size needed for bundles with int CA certs */
  26724. word32 outputSz = sizeof(output);
  26725. byte data[] = "Test data to encode.";
  26726. byte badContent[] = "This is different content than was signed";
  26727. wc_HashAlg hash;
  26728. #ifndef NO_PKCS7_STREAM
  26729. word32 z;
  26730. int ret;
  26731. #endif /* !NO_PKCS7_STREAM */
  26732. #ifdef NO_SHA
  26733. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  26734. #else
  26735. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  26736. #endif
  26737. byte hashBuf[WC_MAX_DIGEST_SIZE];
  26738. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  26739. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  26740. /* Success test with ECC certs/key */
  26741. outputSz = sizeof(output);
  26742. XMEMSET(output, 0, outputSz);
  26743. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26744. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  26745. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26746. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26747. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26748. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26749. wc_PKCS7_Free(pkcs7);
  26750. pkcs7 = NULL;
  26751. #ifndef NO_PKCS7_STREAM
  26752. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26753. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26754. /* test for streaming */
  26755. ret = -1;
  26756. for (z = 0; z < outputSz && ret != 0; z++) {
  26757. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26758. if (ret < 0){
  26759. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26760. }
  26761. }
  26762. ExpectIntEQ(ret, 0);
  26763. ExpectIntNE(pkcs7->contentSz, 0);
  26764. ExpectNotNull(pkcs7->contentDynamic);
  26765. wc_PKCS7_Free(pkcs7);
  26766. pkcs7 = NULL;
  26767. #endif /* !NO_PKCS7_STREAM */
  26768. /* Invalid content should error, use detached signature so we can
  26769. * easily change content */
  26770. outputSz = sizeof(output);
  26771. XMEMSET(output, 0, outputSz);
  26772. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26773. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  26774. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26775. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26776. if (pkcs7 != NULL) {
  26777. pkcs7->content = badContent;
  26778. pkcs7->contentSz = sizeof(badContent);
  26779. }
  26780. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  26781. SIG_VERIFY_E);
  26782. wc_PKCS7_Free(pkcs7);
  26783. pkcs7 = NULL;
  26784. #ifndef NO_PKCS7_STREAM
  26785. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26786. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26787. if (pkcs7 != NULL) {
  26788. pkcs7->content = badContent;
  26789. pkcs7->contentSz = sizeof(badContent);
  26790. }
  26791. /* test for streaming */
  26792. ret = -1;
  26793. for (z = 0; z < outputSz && ret != 0; z++) {
  26794. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26795. if (ret == WC_PKCS7_WANT_READ_E){
  26796. continue;
  26797. }
  26798. else if (ret < 0) {
  26799. break;
  26800. }
  26801. }
  26802. ExpectIntEQ(ret, SIG_VERIFY_E);
  26803. ExpectIntNE(pkcs7->contentSz, 0);
  26804. ExpectNotNull(pkcs7->contentDynamic);
  26805. wc_PKCS7_Free(pkcs7);
  26806. pkcs7 = NULL;
  26807. #endif /* !NO_PKCS7_STREAM */
  26808. /* Test success case with detached signature and valid content */
  26809. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26810. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26811. if (pkcs7 != NULL) {
  26812. pkcs7->content = data;
  26813. pkcs7->contentSz = sizeof(data);
  26814. }
  26815. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26816. wc_PKCS7_Free(pkcs7);
  26817. pkcs7 = NULL;
  26818. #ifndef NO_PKCS7_STREAM
  26819. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26820. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26821. if (pkcs7 != NULL) {
  26822. pkcs7->content = data;
  26823. pkcs7->contentSz = sizeof(data);
  26824. }
  26825. /* test for streaming */
  26826. ret = -1;
  26827. for (z = 0; z < outputSz && ret != 0; z++) {
  26828. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26829. if (ret < 0){
  26830. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26831. }
  26832. }
  26833. ExpectIntEQ(ret, 0);
  26834. ExpectIntNE(pkcs7->contentSz, 0);
  26835. ExpectNotNull(pkcs7->contentDynamic);
  26836. wc_PKCS7_Free(pkcs7);
  26837. pkcs7 = NULL;
  26838. #endif /* !NO_PKCS7_STREAM */
  26839. /* verify using pre-computed content digest only (no content) */
  26840. {
  26841. /* calculate hash for content */
  26842. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  26843. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  26844. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  26845. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  26846. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26847. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  26848. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26849. output, outputSz, NULL, 0), 0);
  26850. wc_PKCS7_Free(pkcs7);
  26851. pkcs7 = NULL;
  26852. }
  26853. /* Test verify on signedData containing intermediate/root CA certs */
  26854. outputSz = sizeof(output);
  26855. XMEMSET(output, 0, outputSz);
  26856. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26857. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  26858. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26859. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26860. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26861. wc_PKCS7_Free(pkcs7);
  26862. pkcs7 = NULL;
  26863. #ifndef NO_PKCS7_STREAM
  26864. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26865. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26866. /* test for streaming */
  26867. ret = -1;
  26868. for (z = 0; z < outputSz && ret != 0; z++) {
  26869. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26870. if (ret < 0){
  26871. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26872. }
  26873. }
  26874. ExpectIntEQ(ret, 0);
  26875. ExpectIntNE(pkcs7->contentSz, 0);
  26876. ExpectNotNull(pkcs7->contentDynamic);
  26877. wc_PKCS7_Free(pkcs7);
  26878. pkcs7 = NULL;
  26879. #endif /* !NO_PKCS7_STREAM */
  26880. #endif
  26881. return EXPECT_RESULT();
  26882. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  26883. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  26884. !defined(NO_AES_256)
  26885. static const byte defKey[] = {
  26886. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26887. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26888. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26889. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26890. };
  26891. static byte aesHandle[32]; /* simulated hardware key handle */
  26892. /* return 0 on success */
  26893. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  26894. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  26895. byte* in, int inSz, byte* out, void* usrCtx)
  26896. {
  26897. int ret;
  26898. Aes aes;
  26899. if (usrCtx == NULL) {
  26900. /* no simulated handle passed in */
  26901. return -1;
  26902. }
  26903. switch (encryptOID) {
  26904. case AES256CBCb:
  26905. if (ivSz != AES_BLOCK_SIZE)
  26906. return BAD_FUNC_ARG;
  26907. break;
  26908. default:
  26909. WOLFSSL_MSG("Unsupported content cipher type for test");
  26910. return ALGO_ID_E;
  26911. };
  26912. /* simulate using handle to get key */
  26913. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  26914. if (ret == 0) {
  26915. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  26916. if (ret == 0)
  26917. ret = wc_AesCbcDecrypt(&aes, out, in, (word32)inSz);
  26918. wc_AesFree(&aes);
  26919. }
  26920. (void)aad;
  26921. (void)aadSz;
  26922. (void)authTag;
  26923. (void)authTagSz;
  26924. (void)pkcs7;
  26925. return ret;
  26926. }
  26927. /* returns key size on success */
  26928. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  26929. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  26930. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  26931. {
  26932. int ret = -1;
  26933. (void)cekSz;
  26934. (void)cek;
  26935. (void)outSz;
  26936. (void)keyIdSz;
  26937. (void)direction;
  26938. (void)orginKey; /* used with KAKRI */
  26939. (void)orginKeySz;
  26940. if (out == NULL)
  26941. return BAD_FUNC_ARG;
  26942. if (keyId[0] != 0x00) {
  26943. return -1;
  26944. }
  26945. if (type != (int)PKCS7_KEKRI) {
  26946. return -1;
  26947. }
  26948. switch (keyWrapAlgo) {
  26949. case AES256_WRAP:
  26950. /* simulate setting a handle for later decryption but use key
  26951. * as handle in the test case here */
  26952. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  26953. aesHandle, sizeof(aesHandle), NULL);
  26954. if (ret < 0)
  26955. return ret;
  26956. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  26957. if (ret < 0)
  26958. return ret;
  26959. /* return key size on success */
  26960. return sizeof(defKey);
  26961. default:
  26962. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  26963. return BAD_KEYWRAP_ALG_E;
  26964. };
  26965. }
  26966. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  26967. /*
  26968. * Testing wc_PKCS7_EncodeEnvelopedData()
  26969. */
  26970. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  26971. {
  26972. EXPECT_DECLS;
  26973. #if defined(HAVE_PKCS7)
  26974. PKCS7* pkcs7 = NULL;
  26975. #ifdef ASN_BER_TO_DER
  26976. int encodedSz = 0;
  26977. #endif
  26978. #ifdef ECC_TIMING_RESISTANT
  26979. WC_RNG rng;
  26980. #endif
  26981. word32 tempWrd32 = 0;
  26982. byte* tmpBytePtr = NULL;
  26983. const char input[] = "Test data to encode.";
  26984. int i;
  26985. int testSz = 0;
  26986. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  26987. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  26988. byte* rsaCert = NULL;
  26989. byte* rsaPrivKey = NULL;
  26990. word32 rsaCertSz;
  26991. word32 rsaPrivKeySz;
  26992. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  26993. !defined(USE_CERT_BUFFERS_2048) )
  26994. static const char* rsaClientCert = "./certs/client-cert.der";
  26995. static const char* rsaClientKey = "./certs/client-key.der";
  26996. rsaCertSz = (word32)sizeof(rsaClientCert);
  26997. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  26998. #endif
  26999. #endif
  27000. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  27001. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  27002. byte* eccCert = NULL;
  27003. byte* eccPrivKey = NULL;
  27004. word32 eccCertSz;
  27005. word32 eccPrivKeySz;
  27006. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  27007. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  27008. static const char* eccClientKey = "./certs/ecc-client-key.der";
  27009. #endif
  27010. #endif
  27011. /* Generic buffer size. */
  27012. byte output[ONEK_BUF];
  27013. byte decoded[sizeof(input)/sizeof(char)];
  27014. int decodedSz = 0;
  27015. #ifndef NO_FILESYSTEM
  27016. XFILE certFile = XBADFILE;
  27017. XFILE keyFile = XBADFILE;
  27018. #endif
  27019. #ifdef ECC_TIMING_RESISTANT
  27020. XMEMSET(&rng, 0, sizeof(WC_RNG));
  27021. #endif
  27022. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  27023. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  27024. /* RSA certs and keys. */
  27025. #if defined(USE_CERT_BUFFERS_1024)
  27026. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  27027. /* Allocate buffer space. */
  27028. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  27029. DYNAMIC_TYPE_TMP_BUFFER));
  27030. /* Init buffer. */
  27031. if (rsaCert != NULL) {
  27032. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  27033. }
  27034. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  27035. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  27036. DYNAMIC_TYPE_TMP_BUFFER));
  27037. if (rsaPrivKey != NULL) {
  27038. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  27039. }
  27040. #elif defined(USE_CERT_BUFFERS_2048)
  27041. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  27042. /* Allocate buffer */
  27043. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  27044. DYNAMIC_TYPE_TMP_BUFFER));
  27045. /* Init buffer. */
  27046. if (rsaCert != NULL) {
  27047. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  27048. }
  27049. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  27050. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  27051. DYNAMIC_TYPE_TMP_BUFFER));
  27052. if (rsaPrivKey != NULL) {
  27053. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  27054. }
  27055. #else
  27056. /* File system. */
  27057. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  27058. rsaCertSz = (word32)FOURK_BUF;
  27059. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  27060. DYNAMIC_TYPE_TMP_BUFFER));
  27061. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  27062. certFile)) > 0);
  27063. if (certFile != XBADFILE)
  27064. XFCLOSE(certFile);
  27065. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  27066. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  27067. DYNAMIC_TYPE_TMP_BUFFER));
  27068. rsaPrivKeySz = (word32)FOURK_BUF;
  27069. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  27070. keyFile)) > 0);
  27071. if (keyFile != XBADFILE)
  27072. XFCLOSE(keyFile);
  27073. #endif /* USE_CERT_BUFFERS */
  27074. #endif /* NO_RSA */
  27075. /* ECC */
  27076. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  27077. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  27078. #ifdef USE_CERT_BUFFERS_256
  27079. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  27080. DYNAMIC_TYPE_TMP_BUFFER));
  27081. /* Init buffer. */
  27082. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  27083. if (eccCert != NULL) {
  27084. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  27085. }
  27086. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  27087. DYNAMIC_TYPE_TMP_BUFFER));
  27088. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  27089. if (eccPrivKey != NULL) {
  27090. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  27091. }
  27092. #else /* File system. */
  27093. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  27094. eccCertSz = (word32)FOURK_BUF;
  27095. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  27096. DYNAMIC_TYPE_TMP_BUFFER));
  27097. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  27098. certFile)) > 0);
  27099. if (certFile != XBADFILE) {
  27100. XFCLOSE(certFile);
  27101. }
  27102. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  27103. eccPrivKeySz = (word32)FOURK_BUF;
  27104. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  27105. DYNAMIC_TYPE_TMP_BUFFER));
  27106. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  27107. keyFile)) > 0);
  27108. if (keyFile != XBADFILE) {
  27109. XFCLOSE(keyFile);
  27110. }
  27111. #endif /* USE_CERT_BUFFERS_256 */
  27112. #endif /* END HAVE_ECC */
  27113. #ifndef NO_FILESYSTEM
  27114. /* Silence. */
  27115. (void)keyFile;
  27116. (void)certFile;
  27117. #endif
  27118. {
  27119. const pkcs7EnvelopedVector testVectors[] = {
  27120. /* DATA is a global variable defined in the makefile. */
  27121. #if !defined(NO_RSA)
  27122. #ifndef NO_DES3
  27123. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  27124. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  27125. #endif /* NO_DES3 */
  27126. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27127. #ifndef NO_AES_128
  27128. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  27129. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  27130. #endif
  27131. #ifndef NO_AES_192
  27132. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  27133. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  27134. #endif
  27135. #ifndef NO_AES_256
  27136. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  27137. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  27138. #endif
  27139. #endif /* NO_AES && HAVE_AES_CBC */
  27140. #endif /* NO_RSA */
  27141. #if defined(HAVE_ECC)
  27142. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27143. #if !defined(NO_SHA) && !defined(NO_AES_128)
  27144. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  27145. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  27146. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  27147. #endif
  27148. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  27149. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  27150. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  27151. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  27152. #endif
  27153. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  27154. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  27155. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  27156. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  27157. #endif
  27158. #endif /* NO_AES && HAVE_AES_CBC*/
  27159. #endif /* END HAVE_ECC */
  27160. }; /* END pkcs7EnvelopedVector */
  27161. #ifdef ECC_TIMING_RESISTANT
  27162. ExpectIntEQ(wc_InitRng(&rng), 0);
  27163. #endif
  27164. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27165. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  27166. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  27167. for (i = 0; i < testSz; i++) {
  27168. #ifdef ASN_BER_TO_DER
  27169. encodeSignedDataStream strm;
  27170. /* test setting stream mode, the first one using IO callbacks */
  27171. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  27172. (word32)(testVectors + i)->certSz), 0);
  27173. if (pkcs7 != NULL) {
  27174. #ifdef ECC_TIMING_RESISTANT
  27175. pkcs7->rng = &rng;
  27176. #endif
  27177. if (i != 0)
  27178. pkcs7->content = (byte*)(testVectors + i)->content;
  27179. pkcs7->contentSz = (testVectors + i)->contentSz;
  27180. pkcs7->contentOID = (testVectors + i)->contentOID;
  27181. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  27182. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  27183. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  27184. pkcs7->privateKey = (testVectors + i)->privateKey;
  27185. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  27186. }
  27187. if (i == 0) {
  27188. XMEMSET(&strm, 0, sizeof(strm));
  27189. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  27190. StreamOutputCB, (void*)&strm), 0);
  27191. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  27192. }
  27193. else {
  27194. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  27195. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  27196. (word32)sizeof(output));
  27197. }
  27198. switch ((testVectors + i)->encryptOID) {
  27199. #ifndef NO_DES3
  27200. case DES3b:
  27201. case DESb:
  27202. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  27203. break;
  27204. #endif
  27205. #ifdef HAVE_AESCCM
  27206. #ifdef WOLFSSL_AES_128
  27207. case AES128CCMb:
  27208. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  27209. break;
  27210. #endif
  27211. #ifdef WOLFSSL_AES_192
  27212. case AES192CCMb:
  27213. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  27214. break;
  27215. #endif
  27216. #ifdef WOLFSSL_AES_256
  27217. case AES256CCMb:
  27218. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  27219. break;
  27220. #endif
  27221. #endif
  27222. default:
  27223. ExpectIntGE(encodedSz, 0);
  27224. }
  27225. if (encodedSz > 0) {
  27226. if (i == 0) {
  27227. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  27228. strm.out, (word32)encodedSz, decoded,
  27229. (word32)sizeof(decoded));
  27230. }
  27231. else {
  27232. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27233. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  27234. }
  27235. ExpectIntGE(decodedSz, 0);
  27236. /* Verify the size of each buffer. */
  27237. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  27238. }
  27239. wc_PKCS7_Free(pkcs7);
  27240. pkcs7 = NULL;
  27241. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27242. #endif
  27243. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  27244. (word32)(testVectors + i)->certSz), 0);
  27245. if (pkcs7 != NULL) {
  27246. #ifdef ECC_TIMING_RESISTANT
  27247. pkcs7->rng = &rng;
  27248. #endif
  27249. pkcs7->content = (byte*)(testVectors + i)->content;
  27250. pkcs7->contentSz = (testVectors + i)->contentSz;
  27251. pkcs7->contentOID = (testVectors + i)->contentOID;
  27252. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  27253. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  27254. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  27255. pkcs7->privateKey = (testVectors + i)->privateKey;
  27256. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  27257. }
  27258. #ifdef ASN_BER_TO_DER
  27259. /* test without setting stream mode */
  27260. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  27261. #endif
  27262. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  27263. (word32)sizeof(output)), 0);
  27264. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27265. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  27266. ExpectIntGE(decodedSz, 0);
  27267. /* Verify the size of each buffer. */
  27268. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  27269. /* Don't free the last time through the loop. */
  27270. if (i < testSz - 1) {
  27271. wc_PKCS7_Free(pkcs7);
  27272. pkcs7 = NULL;
  27273. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27274. }
  27275. } /* END test loop. */
  27276. }
  27277. /* Test bad args. */
  27278. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  27279. (word32)sizeof(output)), BAD_FUNC_ARG);
  27280. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  27281. (word32)sizeof(output)), BAD_FUNC_ARG);
  27282. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  27283. /* Decode. */
  27284. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  27285. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27286. BAD_FUNC_ARG);
  27287. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27288. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  27289. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27290. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  27291. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  27292. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27293. BAD_FUNC_ARG);
  27294. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  27295. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  27296. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  27297. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  27298. /* only a failure for KARI test cases */
  27299. if (pkcs7 != NULL) {
  27300. tempWrd32 = pkcs7->singleCertSz;
  27301. pkcs7->singleCertSz = 0;
  27302. }
  27303. #if defined(WOLFSSL_ASN_TEMPLATE)
  27304. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27305. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27306. BUFFER_E);
  27307. #else
  27308. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27309. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27310. ASN_PARSE_E);
  27311. #endif
  27312. if (pkcs7 != NULL) {
  27313. pkcs7->singleCertSz = tempWrd32;
  27314. tmpBytePtr = pkcs7->singleCert;
  27315. pkcs7->singleCert = NULL;
  27316. }
  27317. #ifndef NO_RSA
  27318. #if defined(NO_PKCS7_STREAM)
  27319. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  27320. * is returned from kari parse which gets set to bad func arg */
  27321. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27322. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27323. BAD_FUNC_ARG);
  27324. #else
  27325. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27326. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27327. ASN_PARSE_E);
  27328. #endif
  27329. #endif /* !NO_RSA */
  27330. if (pkcs7 != NULL) {
  27331. pkcs7->singleCert = tmpBytePtr;
  27332. }
  27333. #endif
  27334. if (pkcs7 != NULL) {
  27335. tempWrd32 = pkcs7->privateKeySz;
  27336. pkcs7->privateKeySz = 0;
  27337. }
  27338. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27339. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27340. BAD_FUNC_ARG);
  27341. if (pkcs7 != NULL) {
  27342. pkcs7->privateKeySz = tempWrd32;
  27343. tmpBytePtr = pkcs7->privateKey;
  27344. pkcs7->privateKey = NULL;
  27345. }
  27346. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27347. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27348. BAD_FUNC_ARG);
  27349. if (pkcs7 != NULL) {
  27350. pkcs7->privateKey = tmpBytePtr;
  27351. }
  27352. wc_PKCS7_Free(pkcs7);
  27353. pkcs7 = NULL;
  27354. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  27355. /* test of decrypt callback with KEKRI enveloped data */
  27356. {
  27357. int envelopedSz = 0;
  27358. const byte keyId[] = { 0x00 };
  27359. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27360. if (pkcs7 != NULL) {
  27361. pkcs7->content = (byte*)input;
  27362. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  27363. pkcs7->contentOID = DATA;
  27364. pkcs7->encryptOID = AES256CBCb;
  27365. }
  27366. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  27367. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  27368. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  27369. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  27370. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  27371. (word32)sizeof(output))), 0);
  27372. wc_PKCS7_Free(pkcs7);
  27373. pkcs7 = NULL;
  27374. /* decode envelopedData */
  27375. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27376. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  27377. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  27378. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27379. (word32)envelopedSz, decoded, sizeof(decoded))), 0);
  27380. wc_PKCS7_Free(pkcs7);
  27381. pkcs7 = NULL;
  27382. }
  27383. #endif /* !NO_AES && !NO_AES_256 */
  27384. #ifndef NO_RSA
  27385. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27386. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27387. #endif /* NO_RSA */
  27388. #ifdef HAVE_ECC
  27389. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27390. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27391. #endif /* HAVE_ECC */
  27392. #ifdef ECC_TIMING_RESISTANT
  27393. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  27394. #endif
  27395. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  27396. !defined(NO_RSA) && !defined(NO_SHA)
  27397. {
  27398. byte out[7];
  27399. byte *cms = NULL;
  27400. word32 cmsSz;
  27401. XFILE cmsFile = XBADFILE;
  27402. XMEMSET(out, 0, sizeof(out));
  27403. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27404. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  27405. != XBADFILE);
  27406. cmsSz = (word32)FOURK_BUF;
  27407. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  27408. DYNAMIC_TYPE_TMP_BUFFER));
  27409. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  27410. if (cmsFile != XBADFILE)
  27411. XFCLOSE(cmsFile);
  27412. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  27413. sizeof_client_cert_der_2048), 0);
  27414. if (pkcs7 != NULL) {
  27415. pkcs7->privateKey = (byte*)client_key_der_2048;
  27416. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  27417. }
  27418. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  27419. 2), 0);
  27420. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  27421. sizeof(out)), 0);
  27422. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27423. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  27424. wc_PKCS7_Free(pkcs7);
  27425. pkcs7 = NULL;
  27426. }
  27427. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  27428. #endif /* HAVE_PKCS7 */
  27429. return EXPECT_RESULT();
  27430. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  27431. /*
  27432. * Testing wc_PKCS7_EncodeEncryptedData()
  27433. */
  27434. static int test_wc_PKCS7_EncodeEncryptedData(void)
  27435. {
  27436. EXPECT_DECLS;
  27437. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  27438. PKCS7* pkcs7 = NULL;
  27439. byte* tmpBytePtr = NULL;
  27440. byte encrypted[TWOK_BUF];
  27441. byte decoded[TWOK_BUF];
  27442. word32 tmpWrd32 = 0;
  27443. int tmpInt = 0;
  27444. int decodedSz = 0;
  27445. int encryptedSz = 0;
  27446. int testSz = 0;
  27447. int i = 0;
  27448. const byte data[] = { /* Hello World */
  27449. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  27450. 0x72,0x6c,0x64
  27451. };
  27452. #ifndef NO_DES3
  27453. byte desKey[] = {
  27454. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  27455. };
  27456. byte des3Key[] = {
  27457. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  27458. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  27459. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  27460. };
  27461. #endif
  27462. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27463. #ifndef NO_AES_128
  27464. byte aes128Key[] = {
  27465. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27466. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  27467. };
  27468. #endif
  27469. #ifndef NO_AES_192
  27470. byte aes192Key[] = {
  27471. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27472. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27473. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  27474. };
  27475. #endif
  27476. #ifndef NO_AES_256
  27477. byte aes256Key[] = {
  27478. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27479. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27480. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27481. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  27482. };
  27483. #endif
  27484. #endif /* !NO_AES && HAVE_AES_CBC */
  27485. const pkcs7EncryptedVector testVectors[] =
  27486. {
  27487. #ifndef NO_DES3
  27488. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  27489. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  27490. #endif /* !NO_DES3 */
  27491. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27492. #ifndef NO_AES_128
  27493. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  27494. sizeof(aes128Key)},
  27495. #endif
  27496. #ifndef NO_AES_192
  27497. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  27498. sizeof(aes192Key)},
  27499. #endif
  27500. #ifndef NO_AES_256
  27501. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  27502. sizeof(aes256Key)},
  27503. #endif
  27504. #endif /* !NO_AES && HAVE_AES_CBC */
  27505. };
  27506. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  27507. for (i = 0; i < testSz; i++) {
  27508. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27509. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  27510. if (pkcs7 != NULL) {
  27511. pkcs7->content = (byte*)testVectors[i].content;
  27512. pkcs7->contentSz = testVectors[i].contentSz;
  27513. pkcs7->contentOID = testVectors[i].contentOID;
  27514. pkcs7->encryptOID = testVectors[i].encryptOID;
  27515. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  27516. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  27517. pkcs7->heap = HEAP_HINT;
  27518. }
  27519. /* encode encryptedData */
  27520. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27521. sizeof(encrypted)), 0);
  27522. /* Decode encryptedData */
  27523. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  27524. (word32)encryptedSz, decoded, sizeof(decoded)), 0);
  27525. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  27526. /* Keep values for last itr. */
  27527. if (i < testSz - 1) {
  27528. wc_PKCS7_Free(pkcs7);
  27529. pkcs7 = NULL;
  27530. }
  27531. }
  27532. if (pkcs7 == NULL || testSz == 0) {
  27533. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27534. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  27535. }
  27536. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  27537. sizeof(encrypted)),BAD_FUNC_ARG);
  27538. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  27539. sizeof(encrypted)), BAD_FUNC_ARG);
  27540. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27541. 0), BAD_FUNC_ARG);
  27542. /* Testing the struct. */
  27543. if (pkcs7 != NULL) {
  27544. tmpBytePtr = pkcs7->content;
  27545. pkcs7->content = NULL;
  27546. }
  27547. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27548. sizeof(encrypted)), BAD_FUNC_ARG);
  27549. if (pkcs7 != NULL) {
  27550. pkcs7->content = tmpBytePtr;
  27551. tmpWrd32 = pkcs7->contentSz;
  27552. pkcs7->contentSz = 0;
  27553. }
  27554. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27555. sizeof(encrypted)), BAD_FUNC_ARG);
  27556. if (pkcs7 != NULL) {
  27557. pkcs7->contentSz = tmpWrd32;
  27558. tmpInt = pkcs7->encryptOID;
  27559. pkcs7->encryptOID = 0;
  27560. }
  27561. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27562. sizeof(encrypted)), BAD_FUNC_ARG);
  27563. if (pkcs7 != NULL) {
  27564. pkcs7->encryptOID = tmpInt;
  27565. tmpBytePtr = pkcs7->encryptionKey;
  27566. pkcs7->encryptionKey = NULL;
  27567. }
  27568. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27569. sizeof(encrypted)), BAD_FUNC_ARG);
  27570. if (pkcs7 != NULL) {
  27571. pkcs7->encryptionKey = tmpBytePtr;
  27572. tmpWrd32 = pkcs7->encryptionKeySz;
  27573. pkcs7->encryptionKeySz = 0;
  27574. }
  27575. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27576. sizeof(encrypted)), BAD_FUNC_ARG);
  27577. if (pkcs7 != NULL) {
  27578. pkcs7->encryptionKeySz = tmpWrd32;
  27579. }
  27580. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, (word32)encryptedSz,
  27581. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  27582. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, (word32)encryptedSz,
  27583. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  27584. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  27585. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  27586. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  27587. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  27588. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  27589. decoded, 0), BAD_FUNC_ARG);
  27590. /* Test struct fields */
  27591. if (pkcs7 != NULL) {
  27592. tmpBytePtr = pkcs7->encryptionKey;
  27593. pkcs7->encryptionKey = NULL;
  27594. }
  27595. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  27596. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  27597. if (pkcs7 != NULL) {
  27598. pkcs7->encryptionKey = tmpBytePtr;
  27599. pkcs7->encryptionKeySz = 0;
  27600. }
  27601. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  27602. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  27603. wc_PKCS7_Free(pkcs7);
  27604. #endif
  27605. return EXPECT_RESULT();
  27606. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  27607. /*
  27608. * Testing wc_PKCS7_Degenerate()
  27609. */
  27610. static int test_wc_PKCS7_Degenerate(void)
  27611. {
  27612. EXPECT_DECLS;
  27613. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  27614. PKCS7* pkcs7 = NULL;
  27615. char fName[] = "./certs/test-degenerate.p7b";
  27616. XFILE f = XBADFILE;
  27617. byte der[4096];
  27618. word32 derSz = 0;
  27619. #ifndef NO_PKCS7_STREAM
  27620. word32 z;
  27621. int ret;
  27622. #endif /* !NO_PKCS7_STREAM */
  27623. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  27624. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  27625. if (f != XBADFILE)
  27626. XFCLOSE(f);
  27627. /* test degenerate success */
  27628. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27629. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27630. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27631. #ifndef NO_RSA
  27632. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  27633. #ifndef NO_PKCS7_STREAM
  27634. wc_PKCS7_Free(pkcs7);
  27635. pkcs7 = NULL;
  27636. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27637. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27638. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27639. /* test for streaming */
  27640. ret = -1;
  27641. for (z = 0; z < derSz && ret != 0; z++) {
  27642. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  27643. if (ret < 0){
  27644. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  27645. }
  27646. }
  27647. ExpectIntEQ(ret, 0);
  27648. #endif /* !NO_PKCS7_STREAM */
  27649. #else
  27650. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  27651. #endif /* NO_RSA */
  27652. wc_PKCS7_Free(pkcs7);
  27653. pkcs7 = NULL;
  27654. /* test with turning off degenerate cases */
  27655. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27656. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27657. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27658. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  27659. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  27660. PKCS7_NO_SIGNER_E);
  27661. #ifndef NO_PKCS7_STREAM
  27662. wc_PKCS7_Free(pkcs7);
  27663. pkcs7 = NULL;
  27664. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27665. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27666. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27667. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  27668. /* test for streaming */
  27669. ret = -1;
  27670. for (z = 0; z < derSz && ret != 0; z++) {
  27671. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  27672. if (ret == WC_PKCS7_WANT_READ_E){
  27673. continue;
  27674. }
  27675. else
  27676. break;
  27677. }
  27678. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  27679. #endif /* !NO_PKCS7_STREAM */
  27680. wc_PKCS7_Free(pkcs7);
  27681. #endif
  27682. return EXPECT_RESULT();
  27683. } /* END test_wc_PKCS7_Degenerate() */
  27684. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  27685. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  27686. static byte berContent[] = {
  27687. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  27688. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  27689. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  27690. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  27691. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  27692. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  27693. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  27694. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  27695. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  27696. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  27697. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  27698. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  27699. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  27700. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  27701. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  27702. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  27703. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  27704. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  27705. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  27706. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  27707. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  27708. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  27709. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  27710. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  27711. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  27712. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  27713. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  27714. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  27715. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  27716. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  27717. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  27718. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  27719. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  27720. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  27721. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  27722. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  27723. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  27724. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  27725. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  27726. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  27727. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  27728. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  27729. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  27730. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  27731. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  27732. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  27733. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  27734. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  27735. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  27736. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  27737. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  27738. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  27739. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  27740. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  27741. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  27742. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  27743. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  27744. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  27745. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  27746. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  27747. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  27748. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  27749. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  27750. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  27751. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  27752. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  27753. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  27754. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  27755. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  27756. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  27757. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  27758. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  27759. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  27760. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  27761. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  27762. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  27763. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  27764. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  27765. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  27766. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  27767. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  27768. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  27769. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  27770. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  27771. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  27772. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  27773. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  27774. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  27775. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  27776. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  27777. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  27778. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  27779. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  27780. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  27781. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  27782. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  27783. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  27784. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  27785. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  27786. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  27787. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  27788. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  27789. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  27790. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  27791. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  27792. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  27793. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  27794. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  27795. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  27796. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  27797. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  27798. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  27799. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  27800. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  27801. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  27802. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  27803. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  27804. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  27805. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  27806. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  27807. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  27808. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  27809. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  27810. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  27811. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  27812. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  27813. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  27814. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  27815. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  27816. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  27817. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  27818. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  27819. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  27820. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  27821. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  27822. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  27823. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  27824. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  27825. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  27826. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  27827. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  27828. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  27829. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  27830. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  27831. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  27832. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  27833. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  27834. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  27835. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  27836. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  27837. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  27838. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  27839. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  27840. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  27841. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  27842. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  27843. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  27844. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  27845. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  27846. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  27847. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  27848. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  27849. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  27850. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  27851. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  27852. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  27853. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  27854. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  27855. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  27856. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  27857. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  27858. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  27859. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  27860. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  27861. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  27862. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  27863. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  27864. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  27865. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  27866. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  27867. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  27868. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  27869. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  27870. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  27871. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  27872. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  27873. 0x00, 0x00, 0x00, 0x00, 0x00
  27874. };
  27875. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  27876. * !NO_DES3 && !NO_SHA
  27877. */
  27878. /*
  27879. * Testing wc_PKCS7_BER()
  27880. */
  27881. static int test_wc_PKCS7_BER(void)
  27882. {
  27883. EXPECT_DECLS;
  27884. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  27885. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  27886. PKCS7* pkcs7 = NULL;
  27887. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  27888. XFILE f = XBADFILE;
  27889. byte der[4096];
  27890. #ifndef NO_DES3
  27891. byte decoded[2048];
  27892. #endif
  27893. word32 derSz = 0;
  27894. #ifndef NO_PKCS7_STREAM
  27895. word32 z;
  27896. int ret;
  27897. #endif /* !NO_PKCS7_STREAM */
  27898. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  27899. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  27900. if (f != XBADFILE) {
  27901. XFCLOSE(f);
  27902. f = XBADFILE;
  27903. }
  27904. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27905. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27906. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27907. #ifndef NO_RSA
  27908. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  27909. #ifndef NO_PKCS7_STREAM
  27910. wc_PKCS7_Free(pkcs7);
  27911. pkcs7 = NULL;
  27912. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27913. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27914. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27915. /* test for streaming */
  27916. ret = -1;
  27917. for (z = 0; z < derSz && ret != 0; z++) {
  27918. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  27919. if (ret < 0){
  27920. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  27921. }
  27922. }
  27923. ExpectIntEQ(ret, 0);
  27924. #endif /* !NO_PKCS7_STREAM */
  27925. #else
  27926. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  27927. #endif
  27928. wc_PKCS7_Free(pkcs7);
  27929. pkcs7 = NULL;
  27930. #ifndef NO_DES3
  27931. /* decode BER content */
  27932. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  27933. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  27934. if (f != XBADFILE) {
  27935. XFCLOSE(f);
  27936. f = XBADFILE;
  27937. }
  27938. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27939. #ifndef NO_RSA
  27940. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  27941. #else
  27942. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  27943. #endif
  27944. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  27945. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  27946. if (f != XBADFILE) {
  27947. XFCLOSE(f);
  27948. f = XBADFILE;
  27949. }
  27950. if (pkcs7 != NULL) {
  27951. pkcs7->privateKey = der;
  27952. pkcs7->privateKeySz = derSz;
  27953. }
  27954. #ifndef NO_RSA
  27955. #ifdef WOLFSSL_SP_MATH
  27956. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  27957. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  27958. #else
  27959. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  27960. sizeof(berContent), decoded, sizeof(decoded)), 0);
  27961. #endif
  27962. #else
  27963. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  27964. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  27965. #endif
  27966. wc_PKCS7_Free(pkcs7);
  27967. #endif /* !NO_DES3 */
  27968. #endif
  27969. return EXPECT_RESULT();
  27970. } /* END test_wc_PKCS7_BER() */
  27971. static int test_wc_PKCS7_signed_enveloped(void)
  27972. {
  27973. EXPECT_DECLS;
  27974. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  27975. !defined(NO_FILESYSTEM)
  27976. XFILE f = XBADFILE;
  27977. PKCS7* pkcs7 = NULL;
  27978. #ifdef HAVE_AES_CBC
  27979. PKCS7* inner = NULL;
  27980. #endif
  27981. WC_RNG rng;
  27982. unsigned char key[FOURK_BUF/2];
  27983. unsigned char cert[FOURK_BUF/2];
  27984. unsigned char env[FOURK_BUF];
  27985. int envSz = FOURK_BUF;
  27986. int keySz = 0;
  27987. int certSz = 0;
  27988. unsigned char sig[FOURK_BUF * 2];
  27989. int sigSz = FOURK_BUF * 2;
  27990. #ifdef HAVE_AES_CBC
  27991. unsigned char decoded[FOURK_BUF];
  27992. int decodedSz = FOURK_BUF;
  27993. #endif
  27994. #ifndef NO_PKCS7_STREAM
  27995. int z;
  27996. int ret;
  27997. #endif /* !NO_PKCS7_STREAM */
  27998. XMEMSET(&rng, 0, sizeof(WC_RNG));
  27999. /* load cert */
  28000. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  28001. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  28002. if (f != XBADFILE) {
  28003. XFCLOSE(f);
  28004. f = XBADFILE;
  28005. }
  28006. /* load key */
  28007. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  28008. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  28009. if (f != XBADFILE) {
  28010. XFCLOSE(f);
  28011. f = XBADFILE;
  28012. }
  28013. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  28014. /* sign cert for envelope */
  28015. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28016. ExpectIntEQ(wc_InitRng(&rng), 0);
  28017. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  28018. if (pkcs7 != NULL) {
  28019. pkcs7->content = cert;
  28020. pkcs7->contentSz = (word32)certSz;
  28021. pkcs7->contentOID = DATA;
  28022. pkcs7->privateKey = key;
  28023. pkcs7->privateKeySz = (word32)keySz;
  28024. pkcs7->encryptOID = RSAk;
  28025. pkcs7->hashOID = SHA256h;
  28026. pkcs7->rng = &rng;
  28027. }
  28028. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  28029. wc_PKCS7_Free(pkcs7);
  28030. pkcs7 = NULL;
  28031. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  28032. #ifdef HAVE_AES_CBC
  28033. /* create envelope */
  28034. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28035. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  28036. if (pkcs7 != NULL) {
  28037. pkcs7->content = sig;
  28038. pkcs7->contentSz = (word32)sigSz;
  28039. pkcs7->contentOID = DATA;
  28040. pkcs7->encryptOID = AES256CBCb;
  28041. pkcs7->privateKey = key;
  28042. pkcs7->privateKeySz = (word32)keySz;
  28043. }
  28044. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, (word32)envSz)), 0);
  28045. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  28046. wc_PKCS7_Free(pkcs7);
  28047. pkcs7 = NULL;
  28048. #endif
  28049. /* create bad signed enveloped data */
  28050. sigSz = FOURK_BUF * 2;
  28051. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28052. ExpectIntEQ(wc_InitRng(&rng), 0);
  28053. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  28054. if (pkcs7 != NULL) {
  28055. pkcs7->content = env;
  28056. pkcs7->contentSz = (word32)envSz;
  28057. pkcs7->contentOID = DATA;
  28058. pkcs7->privateKey = key;
  28059. pkcs7->privateKeySz = (word32)keySz;
  28060. pkcs7->encryptOID = RSAk;
  28061. pkcs7->hashOID = SHA256h;
  28062. pkcs7->rng = &rng;
  28063. }
  28064. /* Set no certs in bundle for this test. */
  28065. if (pkcs7 != NULL) {
  28066. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  28067. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  28068. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  28069. }
  28070. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  28071. wc_PKCS7_Free(pkcs7);
  28072. pkcs7 = NULL;
  28073. /* check verify fails */
  28074. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28075. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  28076. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz),
  28077. PKCS7_SIGNEEDS_CHECK);
  28078. /* try verifying the signature manually */
  28079. {
  28080. RsaKey rKey;
  28081. word32 idx = 0;
  28082. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  28083. WC_MAX_DIGEST_SIZE];
  28084. int digestSz = 0;
  28085. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  28086. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, (word32)keySz), 0);
  28087. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  28088. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  28089. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  28090. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  28091. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  28092. /* verify was success */
  28093. }
  28094. wc_PKCS7_Free(pkcs7);
  28095. pkcs7 = NULL;
  28096. /* initializing the PKCS7 struct with the signing certificate should pass */
  28097. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28098. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  28099. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  28100. #ifndef NO_PKCS7_STREAM
  28101. wc_PKCS7_Free(pkcs7);
  28102. pkcs7 = NULL;
  28103. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  28104. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  28105. /* test for streaming */
  28106. ret = -1;
  28107. for (z = 0; z < sigSz && ret != 0; z++) {
  28108. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  28109. if (ret < 0){
  28110. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  28111. }
  28112. }
  28113. ExpectIntEQ(ret, 0);
  28114. #endif /* !NO_PKCS7_STREAM */
  28115. wc_PKCS7_Free(pkcs7);
  28116. pkcs7 = NULL;
  28117. /* create valid degenerate bundle */
  28118. sigSz = FOURK_BUF * 2;
  28119. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28120. if (pkcs7 != NULL) {
  28121. pkcs7->content = env;
  28122. pkcs7->contentSz = (word32)envSz;
  28123. pkcs7->contentOID = DATA;
  28124. pkcs7->privateKey = key;
  28125. pkcs7->privateKeySz = (word32)keySz;
  28126. pkcs7->encryptOID = RSAk;
  28127. pkcs7->hashOID = SHA256h;
  28128. pkcs7->rng = &rng;
  28129. }
  28130. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  28131. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  28132. wc_PKCS7_Free(pkcs7);
  28133. pkcs7 = NULL;
  28134. wc_FreeRng(&rng);
  28135. /* check verify */
  28136. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28137. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  28138. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  28139. ExpectNotNull(pkcs7->content);
  28140. #ifndef NO_PKCS7_STREAM
  28141. wc_PKCS7_Free(pkcs7);
  28142. pkcs7 = NULL;
  28143. /* create valid degenerate bundle */
  28144. sigSz = FOURK_BUF * 2;
  28145. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28146. if (pkcs7 != NULL) {
  28147. pkcs7->content = env;
  28148. pkcs7->contentSz = (word32)envSz;
  28149. pkcs7->contentOID = DATA;
  28150. pkcs7->privateKey = key;
  28151. pkcs7->privateKeySz = (word32)keySz;
  28152. pkcs7->encryptOID = RSAk;
  28153. pkcs7->hashOID = SHA256h;
  28154. pkcs7->rng = &rng;
  28155. }
  28156. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  28157. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  28158. wc_PKCS7_Free(pkcs7);
  28159. pkcs7 = NULL;
  28160. wc_FreeRng(&rng);
  28161. /* check verify */
  28162. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28163. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  28164. /* test for streaming */
  28165. ret = -1;
  28166. for (z = 0; z < sigSz && ret != 0; z++) {
  28167. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  28168. if (ret < 0){
  28169. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  28170. }
  28171. }
  28172. ExpectIntEQ(ret, 0);
  28173. #endif /* !NO_PKCS7_STREAM */
  28174. #ifdef HAVE_AES_CBC
  28175. /* check decode */
  28176. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  28177. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, (word32)certSz), 0);
  28178. if (inner != NULL) {
  28179. inner->privateKey = key;
  28180. inner->privateKeySz = (word32)keySz;
  28181. }
  28182. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  28183. pkcs7->contentSz, decoded, (word32)decodedSz)), 0);
  28184. wc_PKCS7_Free(inner);
  28185. inner = NULL;
  28186. #endif
  28187. wc_PKCS7_Free(pkcs7);
  28188. pkcs7 = NULL;
  28189. #ifdef HAVE_AES_CBC
  28190. /* check cert set */
  28191. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28192. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  28193. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, (word32)decodedSz), 0);
  28194. ExpectNotNull(pkcs7->singleCert);
  28195. ExpectIntNE(pkcs7->singleCertSz, 0);
  28196. wc_PKCS7_Free(pkcs7);
  28197. pkcs7 = NULL;
  28198. #ifndef NO_PKCS7_STREAM
  28199. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28200. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  28201. /* test for streaming */
  28202. ret = -1;
  28203. for (z = 0; z < decodedSz && ret != 0; z++) {
  28204. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  28205. if (ret < 0){
  28206. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  28207. }
  28208. }
  28209. ExpectIntEQ(ret, 0);
  28210. ExpectNotNull(pkcs7->singleCert);
  28211. ExpectIntNE(pkcs7->singleCertSz, 0);
  28212. wc_PKCS7_Free(pkcs7);
  28213. pkcs7 = NULL;
  28214. #endif /* !NO_PKCS7_STREAM */
  28215. #endif
  28216. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  28217. return EXPECT_RESULT();
  28218. }
  28219. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  28220. {
  28221. EXPECT_DECLS;
  28222. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  28223. && !defined(NO_AES)
  28224. PKCS7* pkcs7 = NULL;
  28225. void* heap = NULL;
  28226. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  28227. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  28228. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  28229. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  28230. wc_PKCS7_Free(pkcs7);
  28231. #endif
  28232. return EXPECT_RESULT();
  28233. }
  28234. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  28235. {
  28236. EXPECT_DECLS;
  28237. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  28238. && !defined(NO_AES)
  28239. PKCS7* pkcs7 = NULL;
  28240. void* heap = NULL;
  28241. WOLFSSL_CTX* ctx = NULL;
  28242. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  28243. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  28244. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  28245. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  28246. wc_PKCS7_Free(pkcs7);
  28247. #endif
  28248. return EXPECT_RESULT();
  28249. }
  28250. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  28251. {
  28252. EXPECT_DECLS;
  28253. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  28254. && !defined(NO_AES)
  28255. PKCS7* pkcs7 = NULL;
  28256. void* heap = NULL;
  28257. WOLFSSL_CTX* ctx = NULL;
  28258. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  28259. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  28260. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  28261. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  28262. wc_PKCS7_Free(pkcs7);
  28263. #endif
  28264. return EXPECT_RESULT();
  28265. }
  28266. static int test_wc_PKCS7_DecodeCompressedData(void)
  28267. {
  28268. EXPECT_DECLS;
  28269. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  28270. && !defined(NO_AES) && defined(HAVE_LIBZ)
  28271. PKCS7* pkcs7 = NULL;
  28272. void* heap = NULL;
  28273. byte out[4096];
  28274. byte* decompressed = NULL;
  28275. int outSz;
  28276. int decompressedSz;
  28277. const char* cert = "./certs/client-cert.pem";
  28278. byte* cert_buf = NULL;
  28279. size_t cert_sz = 0;
  28280. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  28281. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  28282. DYNAMIC_TYPE_TMP_BUFFER)));
  28283. decompressedSz = (int)cert_sz;
  28284. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  28285. if (pkcs7 != NULL) {
  28286. pkcs7->content = (byte*)cert_buf;
  28287. pkcs7->contentSz = (word32)cert_sz;
  28288. pkcs7->contentOID = DATA;
  28289. }
  28290. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  28291. sizeof(out))), 0);
  28292. wc_PKCS7_Free(pkcs7);
  28293. pkcs7 = NULL;
  28294. /* compressed key should be smaller than when started */
  28295. ExpectIntLT(outSz, cert_sz);
  28296. /* test decompression */
  28297. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  28298. ExpectIntEQ(pkcs7->contentOID, 0);
  28299. /* fail case with out buffer too small */
  28300. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  28301. decompressed, outSz), 0);
  28302. /* success case */
  28303. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  28304. decompressed, decompressedSz), cert_sz);
  28305. ExpectIntEQ(pkcs7->contentOID, DATA);
  28306. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  28307. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  28308. decompressed = NULL;
  28309. /* test decompression function with different 'max' inputs */
  28310. outSz = sizeof(out);
  28311. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  28312. 0);
  28313. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  28314. out, outSz, 0, heap), 0);
  28315. ExpectNull(decompressed);
  28316. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  28317. out, outSz, 0, heap), 0);
  28318. ExpectNotNull(decompressed);
  28319. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  28320. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  28321. decompressed = NULL;
  28322. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  28323. out, outSz, 0, heap), 0);
  28324. ExpectNotNull(decompressed);
  28325. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  28326. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  28327. if (cert_buf != NULL)
  28328. free(cert_buf);
  28329. wc_PKCS7_Free(pkcs7);
  28330. #endif
  28331. return EXPECT_RESULT();
  28332. }
  28333. static int test_wc_i2d_PKCS12(void)
  28334. {
  28335. EXPECT_DECLS;
  28336. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  28337. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  28338. && !defined(NO_AES) && !defined(NO_SHA)
  28339. WC_PKCS12* pkcs12 = NULL;
  28340. unsigned char der[FOURK_BUF * 2];
  28341. unsigned char* pt;
  28342. int derSz = 0;
  28343. unsigned char out[FOURK_BUF * 2];
  28344. int outSz = FOURK_BUF * 2;
  28345. const char p12_f[] = "./certs/test-servercert.p12";
  28346. XFILE f = XBADFILE;
  28347. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  28348. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  28349. if (f != XBADFILE)
  28350. XFCLOSE(f);
  28351. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  28352. ExpectIntEQ(wc_d2i_PKCS12(der, (word32)derSz, pkcs12), 0);
  28353. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  28354. ExpectIntEQ(outSz, derSz);
  28355. outSz = derSz - 1;
  28356. pt = out;
  28357. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  28358. outSz = derSz;
  28359. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  28360. ExpectIntEQ((pt == out), 0);
  28361. pt = NULL;
  28362. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  28363. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  28364. wc_PKCS12_free(pkcs12);
  28365. pkcs12 = NULL;
  28366. /* Run the same test but use wc_d2i_PKCS12_fp. */
  28367. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  28368. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  28369. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  28370. ExpectIntEQ(outSz, derSz);
  28371. wc_PKCS12_free(pkcs12);
  28372. pkcs12 = NULL;
  28373. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  28374. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  28375. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  28376. ExpectIntEQ(outSz, derSz);
  28377. wc_PKCS12_free(pkcs12);
  28378. pkcs12 = NULL;
  28379. #endif
  28380. return EXPECT_RESULT();
  28381. }
  28382. /* Testing wc_SignatureGetSize() for signature type ECC */
  28383. static int test_wc_SignatureGetSize_ecc(void)
  28384. {
  28385. EXPECT_DECLS;
  28386. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  28387. enum wc_SignatureType sig_type;
  28388. word32 key_len;
  28389. ecc_key ecc;
  28390. const char* qx =
  28391. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  28392. const char* qy =
  28393. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  28394. const char* d =
  28395. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  28396. XMEMSET(&ecc, 0, sizeof(ecc_key));
  28397. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  28398. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  28399. /* Input for signature type ECC */
  28400. sig_type = WC_SIGNATURE_TYPE_ECC;
  28401. key_len = sizeof(ecc_key);
  28402. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  28403. /* Test bad args */
  28404. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  28405. sig_type = (enum wc_SignatureType) 100;
  28406. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  28407. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  28408. sig_type = WC_SIGNATURE_TYPE_ECC;
  28409. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  28410. key_len = (word32)0;
  28411. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  28412. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  28413. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  28414. return EXPECT_RESULT();
  28415. } /* END test_wc_SignatureGetSize_ecc() */
  28416. /* Testing wc_SignatureGetSize() for signature type rsa */
  28417. static int test_wc_SignatureGetSize_rsa(void)
  28418. {
  28419. EXPECT_DECLS;
  28420. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  28421. enum wc_SignatureType sig_type;
  28422. word32 key_len;
  28423. word32 idx = 0;
  28424. RsaKey rsa_key;
  28425. byte* tmp = NULL;
  28426. size_t bytes;
  28427. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  28428. #ifdef USE_CERT_BUFFERS_1024
  28429. bytes = (size_t)sizeof_client_key_der_1024;
  28430. if (bytes < (size_t)sizeof_client_key_der_1024)
  28431. bytes = (size_t)sizeof_client_cert_der_1024;
  28432. #elif defined(USE_CERT_BUFFERS_2048)
  28433. bytes = (size_t)sizeof_client_key_der_2048;
  28434. if (bytes < (size_t)sizeof_client_cert_der_2048)
  28435. bytes = (size_t)sizeof_client_cert_der_2048;
  28436. #else
  28437. bytes = FOURK_BUF;
  28438. #endif
  28439. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  28440. DYNAMIC_TYPE_TMP_BUFFER));
  28441. if (tmp != NULL) {
  28442. #ifdef USE_CERT_BUFFERS_1024
  28443. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  28444. #elif defined(USE_CERT_BUFFERS_2048)
  28445. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  28446. #elif !defined(NO_FILESYSTEM)
  28447. XFILE file = XBADFILE;
  28448. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  28449. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  28450. if (file != XBADFILE)
  28451. XFCLOSE(file);
  28452. }
  28453. #else
  28454. ExpectFail();
  28455. #endif
  28456. }
  28457. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  28458. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  28459. /* Input for signature type RSA */
  28460. sig_type = WC_SIGNATURE_TYPE_RSA;
  28461. key_len = sizeof(RsaKey);
  28462. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  28463. /* Test bad args */
  28464. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  28465. sig_type = (enum wc_SignatureType)100;
  28466. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  28467. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  28468. sig_type = WC_SIGNATURE_TYPE_RSA;
  28469. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  28470. key_len = (word32)0;
  28471. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  28472. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  28473. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28474. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  28475. return EXPECT_RESULT();
  28476. } /* END test_wc_SignatureGetSize_rsa(void) */
  28477. /*----------------------------------------------------------------------------*
  28478. | hash.h Tests
  28479. *----------------------------------------------------------------------------*/
  28480. static int test_wc_HashInit(void)
  28481. {
  28482. EXPECT_DECLS;
  28483. int i; /* 0 indicates tests passed, 1 indicates failure */
  28484. wc_HashAlg hash;
  28485. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  28486. enum wc_HashType enumArray[] = {
  28487. #ifndef NO_MD5
  28488. WC_HASH_TYPE_MD5,
  28489. #endif
  28490. #ifndef NO_SHA
  28491. WC_HASH_TYPE_SHA,
  28492. #endif
  28493. #ifdef WOLFSSL_SHA224
  28494. WC_HASH_TYPE_SHA224,
  28495. #endif
  28496. #ifndef NO_SHA256
  28497. WC_HASH_TYPE_SHA256,
  28498. #endif
  28499. #ifdef WOLFSSL_SHA384
  28500. WC_HASH_TYPE_SHA384,
  28501. #endif
  28502. #ifdef WOLFSSL_SHA512
  28503. WC_HASH_TYPE_SHA512,
  28504. #endif
  28505. };
  28506. /* dynamically finds the length */
  28507. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  28508. /* For loop to test various arguments... */
  28509. for (i = 0; i < enumlen; i++) {
  28510. /* check for bad args */
  28511. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  28512. wc_HashFree(&hash, enumArray[i]);
  28513. /* check for null ptr */
  28514. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  28515. } /* end of for loop */
  28516. return EXPECT_RESULT();
  28517. } /* end of test_wc_HashInit */
  28518. /*
  28519. * Unit test function for wc_HashSetFlags()
  28520. */
  28521. static int test_wc_HashSetFlags(void)
  28522. {
  28523. EXPECT_DECLS;
  28524. #ifdef WOLFSSL_HASH_FLAGS
  28525. wc_HashAlg hash;
  28526. word32 flags = 0;
  28527. int i, j;
  28528. int notSupportedLen;
  28529. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  28530. enum wc_HashType enumArray[] = {
  28531. #ifndef NO_MD5
  28532. WC_HASH_TYPE_MD5,
  28533. #endif
  28534. #ifndef NO_SHA
  28535. WC_HASH_TYPE_SHA,
  28536. #endif
  28537. #ifdef WOLFSSL_SHA224
  28538. WC_HASH_TYPE_SHA224,
  28539. #endif
  28540. #ifndef NO_SHA256
  28541. WC_HASH_TYPE_SHA256,
  28542. #endif
  28543. #ifdef WOLFSSL_SHA384
  28544. WC_HASH_TYPE_SHA384,
  28545. #endif
  28546. #ifdef WOLFSSL_SHA512
  28547. WC_HASH_TYPE_SHA512,
  28548. #endif
  28549. #ifdef WOLFSSL_SHA3
  28550. WC_HASH_TYPE_SHA3_224,
  28551. #endif
  28552. };
  28553. enum wc_HashType notSupported[] = {
  28554. WC_HASH_TYPE_MD5_SHA,
  28555. WC_HASH_TYPE_MD2,
  28556. WC_HASH_TYPE_MD4,
  28557. WC_HASH_TYPE_BLAKE2B,
  28558. WC_HASH_TYPE_BLAKE2S,
  28559. WC_HASH_TYPE_NONE,
  28560. };
  28561. /* dynamically finds the length */
  28562. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  28563. /* For loop to test various arguments... */
  28564. for (i = 0; i < enumlen; i++) {
  28565. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  28566. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  28567. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  28568. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  28569. wc_HashFree(&hash, enumArray[i]);
  28570. }
  28571. /* For loop to test not supported cases */
  28572. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  28573. for (j = 0; j < notSupportedLen; j++) {
  28574. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  28575. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  28576. BAD_FUNC_ARG);
  28577. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  28578. }
  28579. #endif
  28580. return EXPECT_RESULT();
  28581. } /* END test_wc_HashSetFlags */
  28582. /*
  28583. * Unit test function for wc_HashGetFlags()
  28584. */
  28585. static int test_wc_HashGetFlags(void)
  28586. {
  28587. EXPECT_DECLS;
  28588. #ifdef WOLFSSL_HASH_FLAGS
  28589. wc_HashAlg hash;
  28590. word32 flags = 0;
  28591. int i, j;
  28592. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  28593. enum wc_HashType enumArray[] = {
  28594. #ifndef NO_MD5
  28595. WC_HASH_TYPE_MD5,
  28596. #endif
  28597. #ifndef NO_SHA
  28598. WC_HASH_TYPE_SHA,
  28599. #endif
  28600. #ifdef WOLFSSL_SHA224
  28601. WC_HASH_TYPE_SHA224,
  28602. #endif
  28603. #ifndef NO_SHA256
  28604. WC_HASH_TYPE_SHA256,
  28605. #endif
  28606. #ifdef WOLFSSL_SHA384
  28607. WC_HASH_TYPE_SHA384,
  28608. #endif
  28609. #ifdef WOLFSSL_SHA512
  28610. WC_HASH_TYPE_SHA512,
  28611. #endif
  28612. #ifdef WOLFSSL_SHA3
  28613. WC_HASH_TYPE_SHA3_224,
  28614. #endif
  28615. };
  28616. enum wc_HashType notSupported[] = {
  28617. WC_HASH_TYPE_MD5_SHA,
  28618. WC_HASH_TYPE_MD2,
  28619. WC_HASH_TYPE_MD4,
  28620. WC_HASH_TYPE_BLAKE2B,
  28621. WC_HASH_TYPE_BLAKE2S,
  28622. WC_HASH_TYPE_NONE,
  28623. };
  28624. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  28625. int notSupportedLen;
  28626. /* For loop to test various arguments... */
  28627. for (i = 0; i < enumlen; i++) {
  28628. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  28629. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  28630. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  28631. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  28632. wc_HashFree(&hash, enumArray[i]);
  28633. }
  28634. /* For loop to test not supported cases */
  28635. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  28636. for (j = 0; j < notSupportedLen; j++) {
  28637. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  28638. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  28639. BAD_FUNC_ARG);
  28640. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  28641. }
  28642. #endif
  28643. return EXPECT_RESULT();
  28644. } /* END test_wc_HashGetFlags */
  28645. /*----------------------------------------------------------------------------*
  28646. | Compatibility Tests
  28647. *----------------------------------------------------------------------------*/
  28648. /*----------------------------------------------------------------------------*
  28649. | ASN.1 Tests
  28650. *----------------------------------------------------------------------------*/
  28651. static int test_wolfSSL_ASN1_BIT_STRING(void)
  28652. {
  28653. EXPECT_DECLS;
  28654. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  28655. ASN1_BIT_STRING* str = NULL;
  28656. ExpectNotNull(str = ASN1_BIT_STRING_new());
  28657. /* Empty data testing. */
  28658. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  28659. ASN1_BIT_STRING_free(str);
  28660. str = NULL;
  28661. ExpectNotNull(str = ASN1_BIT_STRING_new());
  28662. /* Invalid parameter testing. */
  28663. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  28664. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  28665. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  28666. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  28667. /* No bit string - bit is always 0. */
  28668. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  28669. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  28670. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  28671. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  28672. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  28673. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  28674. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  28675. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  28676. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  28677. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  28678. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  28679. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  28680. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  28681. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  28682. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  28683. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  28684. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  28685. ASN1_BIT_STRING_free(str);
  28686. ASN1_BIT_STRING_free(NULL);
  28687. #endif
  28688. return EXPECT_RESULT();
  28689. }
  28690. static int test_wolfSSL_ASN1_INTEGER(void)
  28691. {
  28692. EXPECT_DECLS;
  28693. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28694. ASN1_INTEGER* a = NULL;
  28695. ASN1_INTEGER* dup = NULL;
  28696. const unsigned char invalidLenDer[] = {
  28697. 0x02, 0x20, 0x00
  28698. };
  28699. const unsigned char longDer[] = {
  28700. 0x02, 0x20,
  28701. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  28702. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  28703. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  28704. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  28705. };
  28706. const unsigned char* p;
  28707. /* Invalid parameter testing. */
  28708. ASN1_INTEGER_free(NULL);
  28709. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  28710. ExpectNotNull(a = ASN1_INTEGER_new());
  28711. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  28712. ASN1_INTEGER_free(dup);
  28713. dup = NULL;
  28714. ASN1_INTEGER_free(a);
  28715. a = NULL;
  28716. p = longDer;
  28717. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  28718. p = longDer;
  28719. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  28720. ExpectPtrNE(p, longDer);
  28721. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  28722. ASN1_INTEGER_free(dup);
  28723. ASN1_INTEGER_free(a);
  28724. #endif
  28725. return EXPECT_RESULT();
  28726. }
  28727. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  28728. {
  28729. EXPECT_DECLS;
  28730. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28731. ASN1_INTEGER* a = NULL;
  28732. ASN1_INTEGER* b = NULL;
  28733. ExpectNotNull(a = ASN1_INTEGER_new());
  28734. ExpectNotNull(b = ASN1_INTEGER_new());
  28735. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  28736. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  28737. /* Invalid parameter testing. */
  28738. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  28739. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  28740. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  28741. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28742. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  28743. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28744. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  28745. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28746. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  28747. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28748. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  28749. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  28750. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28751. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  28752. ASN1_INTEGER_free(b);
  28753. ASN1_INTEGER_free(a);
  28754. #endif
  28755. return EXPECT_RESULT();
  28756. }
  28757. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  28758. {
  28759. EXPECT_DECLS;
  28760. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28761. ASN1_INTEGER* ai = NULL;
  28762. ASN1_INTEGER* ai2 = NULL;
  28763. BIGNUM* bn = NULL;
  28764. BIGNUM* bn2 = NULL;
  28765. ExpectNotNull(ai = ASN1_INTEGER_new());
  28766. ExpectNotNull(bn2 = BN_new());
  28767. /* Invalid parameter testing. */
  28768. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  28769. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  28770. /* at the moment hard setting since no set function */
  28771. if (ai != NULL) {
  28772. ai->data[0] = 0xff; /* No DER encoding. */
  28773. ai->length = 1;
  28774. }
  28775. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  28776. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  28777. BN_free(bn);
  28778. bn = NULL;
  28779. #else
  28780. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  28781. #endif
  28782. if (ai != NULL) {
  28783. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28784. ai->data[1] = 0x04; /* bad length of integer */
  28785. ai->data[2] = 0x03;
  28786. ai->length = 3;
  28787. }
  28788. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  28789. /* Interpreted as a number 0x020403. */
  28790. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  28791. BN_free(bn);
  28792. bn = NULL;
  28793. #else
  28794. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  28795. #endif
  28796. if (ai != NULL) {
  28797. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28798. ai->data[1] = 0x01; /* length of integer */
  28799. ai->data[2] = 0x03;
  28800. ai->length = 3;
  28801. }
  28802. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  28803. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  28804. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28805. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28806. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28807. if (ai != NULL) {
  28808. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28809. ai->data[1] = 0x02; /* length of integer */
  28810. ai->data[2] = 0x00; /* padding byte to ensure positive */
  28811. ai->data[3] = 0xff;
  28812. ai->length = 4;
  28813. }
  28814. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  28815. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  28816. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28817. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28818. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28819. if (ai != NULL) {
  28820. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28821. ai->data[1] = 0x01; /* length of integer */
  28822. ai->data[2] = 0x00;
  28823. ai->length = 3;
  28824. }
  28825. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  28826. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  28827. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28828. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28829. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28830. if (ai != NULL) {
  28831. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28832. ai->data[1] = 0x01; /* length of integer */
  28833. ai->data[2] = 0x01;
  28834. ai->length = 3;
  28835. ai->negative = 1;
  28836. }
  28837. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  28838. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  28839. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28840. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28841. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28842. BN_free(bn2);
  28843. BN_free(bn);
  28844. ASN1_INTEGER_free(ai2);
  28845. ASN1_INTEGER_free(ai);
  28846. #endif
  28847. return EXPECT_RESULT();
  28848. }
  28849. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  28850. {
  28851. EXPECT_DECLS;
  28852. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28853. ASN1_INTEGER *a = NULL;
  28854. long val;
  28855. ExpectNotNull(a = ASN1_INTEGER_new());
  28856. /* Invalid parameter testing. */
  28857. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  28858. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  28859. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  28860. #else
  28861. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  28862. #endif
  28863. ASN1_INTEGER_free(a);
  28864. a = NULL;
  28865. ExpectNotNull(a = ASN1_INTEGER_new());
  28866. val = 0;
  28867. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  28868. ASN1_INTEGER_free(a);
  28869. a = NULL;
  28870. /* 0 */
  28871. ExpectNotNull(a = ASN1_INTEGER_new());
  28872. val = 0;
  28873. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28874. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28875. ASN1_INTEGER_free(a);
  28876. a = NULL;
  28877. /* 40 */
  28878. ExpectNotNull(a = ASN1_INTEGER_new());
  28879. val = 40;
  28880. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28881. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28882. ASN1_INTEGER_free(a);
  28883. a = NULL;
  28884. /* -40 */
  28885. ExpectNotNull(a = ASN1_INTEGER_new());
  28886. val = -40;
  28887. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28888. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28889. ASN1_INTEGER_free(a);
  28890. a = NULL;
  28891. /* 128 */
  28892. ExpectNotNull(a = ASN1_INTEGER_new());
  28893. val = 128;
  28894. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28895. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28896. ASN1_INTEGER_free(a);
  28897. a = NULL;
  28898. /* -128 */
  28899. ExpectNotNull(a = ASN1_INTEGER_new());
  28900. val = -128;
  28901. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28902. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28903. ASN1_INTEGER_free(a);
  28904. a = NULL;
  28905. /* 200 */
  28906. ExpectNotNull(a = ASN1_INTEGER_new());
  28907. val = 200;
  28908. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28909. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28910. ASN1_INTEGER_free(a);
  28911. a = NULL;
  28912. /* int max (2147483647) */
  28913. ExpectNotNull(a = ASN1_INTEGER_new());
  28914. val = 2147483647;
  28915. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28916. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28917. ASN1_INTEGER_free(a);
  28918. a = NULL;
  28919. /* int min (-2147483648) */
  28920. ExpectNotNull(a = ASN1_INTEGER_new());
  28921. val = -2147483647 - 1;
  28922. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28923. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28924. ASN1_INTEGER_free(a);
  28925. a = NULL;
  28926. /* long max positive */
  28927. ExpectNotNull(a = ASN1_INTEGER_new());
  28928. val = (long)(((unsigned long)-1) >> 1);
  28929. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28930. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28931. ASN1_INTEGER_free(a);
  28932. #endif
  28933. return EXPECT_RESULT();
  28934. }
  28935. #if defined(OPENSSL_EXTRA)
  28936. typedef struct ASN1IntTestVector {
  28937. const byte* der;
  28938. const size_t derSz;
  28939. const long value;
  28940. } ASN1IntTestVector;
  28941. #endif
  28942. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  28943. {
  28944. EXPECT_DECLS;
  28945. #if defined(OPENSSL_EXTRA)
  28946. size_t i;
  28947. WOLFSSL_ASN1_INTEGER* a = NULL;
  28948. WOLFSSL_ASN1_INTEGER* b = NULL;
  28949. WOLFSSL_ASN1_INTEGER* c = NULL;
  28950. const byte* p = NULL;
  28951. byte* p2 = NULL;
  28952. byte* reEncoded = NULL;
  28953. int reEncodedSz = 0;
  28954. static const byte zeroDer[] = {
  28955. 0x02, 0x01, 0x00
  28956. };
  28957. static const byte oneDer[] = {
  28958. 0x02, 0x01, 0x01
  28959. };
  28960. static const byte negativeDer[] = {
  28961. 0x02, 0x03, 0xC1, 0x16, 0x0D
  28962. };
  28963. static const byte positiveDer[] = {
  28964. 0x02, 0x03, 0x01, 0x00, 0x01
  28965. };
  28966. static const byte primeDer[] = {
  28967. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  28968. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  28969. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  28970. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  28971. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  28972. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  28973. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  28974. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  28975. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  28976. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  28977. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  28978. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  28979. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  28980. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  28981. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  28982. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  28983. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  28984. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  28985. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  28986. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  28987. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  28988. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  28989. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  28990. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  28991. };
  28992. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  28993. static const ASN1IntTestVector testVectors[] = {
  28994. {zeroDer, sizeof(zeroDer), 0},
  28995. {oneDer, sizeof(oneDer), 1},
  28996. {negativeDer, sizeof(negativeDer), -4123123},
  28997. {positiveDer, sizeof(positiveDer), 65537},
  28998. {primeDer, sizeof(primeDer), 0}
  28999. };
  29000. static const size_t NUM_TEST_VECTORS =
  29001. sizeof(testVectors)/sizeof(testVectors[0]);
  29002. /* Check d2i error conditions */
  29003. /* NULL pointer to input. */
  29004. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  29005. ExpectNull(b);
  29006. /* NULL input. */
  29007. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  29008. ExpectNull(b);
  29009. /* 0 length. */
  29010. p = testVectors[0].der;
  29011. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  29012. ExpectNull(b);
  29013. /* Negative length. */
  29014. p = testVectors[0].der;
  29015. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  29016. ExpectNull(b);
  29017. /* Garbage DER input. */
  29018. p = garbageDer;
  29019. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  29020. ExpectNull(b);
  29021. /* Check i2d error conditions */
  29022. /* NULL input. */
  29023. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  29024. /* 0 length input data buffer (a->length == 0). */
  29025. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  29026. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  29027. if (a != NULL)
  29028. a->data = NULL;
  29029. /* NULL input data buffer. */
  29030. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  29031. if (a != NULL) {
  29032. /* Reset a->data. */
  29033. a->data = a->intData;
  29034. }
  29035. /* Set a to valid value. */
  29036. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  29037. /* NULL output buffer. */
  29038. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  29039. wolfSSL_ASN1_INTEGER_free(a);
  29040. a = NULL;
  29041. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  29042. p = testVectors[i].der;
  29043. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  29044. testVectors[i].derSz));
  29045. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  29046. if (testVectors[i].derSz <= sizeof(long)) {
  29047. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  29048. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  29049. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  29050. wolfSSL_ASN1_INTEGER_free(c);
  29051. c = NULL;
  29052. }
  29053. /* Convert to DER without a pre-allocated output buffer. */
  29054. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  29055. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  29056. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  29057. /* Convert to DER with a pre-allocated output buffer. In this case, the
  29058. * output buffer pointer should be incremented just past the end of the
  29059. * encoded data. */
  29060. p2 = reEncoded;
  29061. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  29062. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  29063. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  29064. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  29065. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  29066. reEncoded = NULL;
  29067. wolfSSL_ASN1_INTEGER_free(a);
  29068. a = NULL;
  29069. }
  29070. #endif /* OPENSSL_EXTRA */
  29071. return EXPECT_RESULT();
  29072. }
  29073. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  29074. {
  29075. EXPECT_DECLS;
  29076. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  29077. BIO* bio = NULL;
  29078. BIO* out = NULL;
  29079. BIO* fixed = NULL;
  29080. ASN1_INTEGER* ai = NULL;
  29081. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  29082. char tmp[1024];
  29083. int tmpSz;
  29084. const char expected1[] = "123456";
  29085. const char expected2[] = "112345678912345678901234567890";
  29086. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  29087. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  29088. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  29089. ExpectNotNull(ai = ASN1_INTEGER_new());
  29090. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  29091. /* Invalid parameter testing. */
  29092. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  29093. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  29094. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  29095. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  29096. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  29097. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  29098. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  29099. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  29100. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  29101. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  29102. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  29103. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  29104. /* No data to read from BIO. */
  29105. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  29106. /* read first line */
  29107. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  29108. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  29109. XMEMSET(tmp, 0, 1024);
  29110. tmpSz = BIO_read(out, tmp, 1024);
  29111. ExpectIntEQ(tmpSz, 6);
  29112. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  29113. /* fail on second line (not % 2) */
  29114. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  29115. /* read 3rd long line */
  29116. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  29117. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  29118. XMEMSET(tmp, 0, 1024);
  29119. tmpSz = BIO_read(out, tmp, 1024);
  29120. ExpectIntEQ(tmpSz, 30);
  29121. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  29122. /* fail on empty line */
  29123. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  29124. BIO_free(bio);
  29125. bio = NULL;
  29126. /* Make long integer, requiring dynamic memory, even longer. */
  29127. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  29128. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  29129. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  29130. XMEMSET(tmp, 0, 1024);
  29131. tmpSz = BIO_read(out, tmp, 1024);
  29132. ExpectIntEQ(tmpSz, 48);
  29133. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  29134. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  29135. XMEMSET(tmp, 0, 1024);
  29136. tmpSz = BIO_read(out, tmp, 1024);
  29137. ExpectIntEQ(tmpSz, 56);
  29138. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  29139. BIO_free(bio);
  29140. BIO_free(out);
  29141. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29142. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29143. /* Ensure there is 0 bytes available to write into. */
  29144. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  29145. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  29146. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29147. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  29148. BIO_free(fixed);
  29149. ASN1_INTEGER_free(ai);
  29150. #endif
  29151. return EXPECT_RESULT();
  29152. }
  29153. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  29154. {
  29155. EXPECT_DECLS;
  29156. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  29157. ASN1_INTEGER *a = NULL;
  29158. unsigned char *pp = NULL,*tpp = NULL;
  29159. int ret = 0;
  29160. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  29161. /* Invalid parameter testing. */
  29162. /* Set pp to an invalid value. */
  29163. pp = NULL;
  29164. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  29165. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  29166. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  29167. /* 40 */
  29168. if (a != NULL) {
  29169. a->intData[0] = ASN_INTEGER;
  29170. a->intData[1] = 1;
  29171. a->intData[2] = 40;
  29172. }
  29173. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  29174. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29175. DYNAMIC_TYPE_TMP_BUFFER));
  29176. tpp = pp;
  29177. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29178. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  29179. tpp--;
  29180. ExpectIntEQ(*tpp, 40);
  29181. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29182. pp = NULL;
  29183. /* 128 */
  29184. if (a != NULL) {
  29185. a->intData[0] = ASN_INTEGER;
  29186. a->intData[1] = 1;
  29187. a->intData[2] = 128;
  29188. }
  29189. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  29190. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29191. DYNAMIC_TYPE_TMP_BUFFER));
  29192. tpp = pp;
  29193. if (tpp != NULL) {
  29194. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29195. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  29196. tpp--;
  29197. ExpectIntEQ(*(tpp--), 128);
  29198. ExpectIntEQ(*tpp, 0);
  29199. }
  29200. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29201. pp = NULL;
  29202. /* -40 */
  29203. if (a != NULL) {
  29204. a->intData[0] = ASN_INTEGER;
  29205. a->intData[1] = 1;
  29206. a->intData[2] = 40;
  29207. a->negative = 1;
  29208. }
  29209. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  29210. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29211. DYNAMIC_TYPE_TMP_BUFFER));
  29212. tpp = pp;
  29213. if (tpp != NULL) {
  29214. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29215. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  29216. tpp--;
  29217. ExpectIntEQ(*tpp, 216);
  29218. }
  29219. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29220. pp = NULL;
  29221. /* -128 */
  29222. if (a != NULL) {
  29223. a->intData[0] = ASN_INTEGER;
  29224. a->intData[1] = 1;
  29225. a->intData[2] = 128;
  29226. a->negative = 1;
  29227. }
  29228. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  29229. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29230. DYNAMIC_TYPE_TMP_BUFFER));
  29231. tpp = pp;
  29232. if (tpp != NULL) {
  29233. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29234. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  29235. tpp--;
  29236. ExpectIntEQ(*tpp, 128);
  29237. }
  29238. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29239. pp = NULL;
  29240. /* -200 */
  29241. if (a != NULL) {
  29242. a->intData[0] = ASN_INTEGER;
  29243. a->intData[1] = 1;
  29244. a->intData[2] = 200;
  29245. a->negative = 1;
  29246. }
  29247. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  29248. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29249. DYNAMIC_TYPE_TMP_BUFFER));
  29250. tpp = pp;
  29251. if (tpp != NULL) {
  29252. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29253. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  29254. tpp--;
  29255. ExpectIntEQ(*(tpp--), 56);
  29256. ExpectIntEQ(*tpp, 255);
  29257. }
  29258. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29259. pp = NULL;
  29260. /* Empty */
  29261. if (a != NULL) {
  29262. a->intData[0] = ASN_INTEGER;
  29263. a->intData[1] = 0;
  29264. a->negative = 0;
  29265. }
  29266. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  29267. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29268. DYNAMIC_TYPE_TMP_BUFFER));
  29269. tpp = pp;
  29270. if (tpp != NULL) {
  29271. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29272. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  29273. tpp--;
  29274. ExpectIntEQ(*tpp, 0);
  29275. }
  29276. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29277. pp = NULL;
  29278. /* 0 */
  29279. if (a != NULL) {
  29280. a->intData[0] = ASN_INTEGER;
  29281. a->intData[1] = 1;
  29282. a->intData[2] = 0;
  29283. a->negative = 1;
  29284. }
  29285. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  29286. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29287. DYNAMIC_TYPE_TMP_BUFFER));
  29288. if (tpp != NULL) {
  29289. tpp = pp;
  29290. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29291. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  29292. tpp--;
  29293. ExpectIntEQ(*tpp, 0);
  29294. }
  29295. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29296. pp = NULL;
  29297. /* 0x100 */
  29298. if (a != NULL) {
  29299. a->intData[0] = ASN_INTEGER;
  29300. a->intData[1] = 2;
  29301. a->intData[2] = 0x01;
  29302. a->intData[3] = 0x00;
  29303. a->negative = 0;
  29304. }
  29305. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  29306. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29307. DYNAMIC_TYPE_TMP_BUFFER));
  29308. if (tpp != NULL) {
  29309. tpp = pp;
  29310. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29311. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  29312. tpp -= 2;
  29313. ExpectIntEQ(tpp[0], 0x01);
  29314. ExpectIntEQ(tpp[1], 0x00);
  29315. }
  29316. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29317. pp = NULL;
  29318. /* -0x8000 => 0x8000 */
  29319. if (a != NULL) {
  29320. a->intData[0] = ASN_INTEGER;
  29321. a->intData[1] = 2;
  29322. a->intData[2] = 0x80;
  29323. a->intData[3] = 0x00;
  29324. a->negative = 1;
  29325. }
  29326. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  29327. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29328. DYNAMIC_TYPE_TMP_BUFFER));
  29329. tpp = pp;
  29330. if (tpp != NULL) {
  29331. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29332. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  29333. tpp -= 2;
  29334. ExpectIntEQ(tpp[0], 0x80);
  29335. ExpectIntEQ(tpp[1], 0x00);
  29336. }
  29337. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29338. pp = NULL;
  29339. /* -0x8001 => 0xFF7FFF */
  29340. if (a != NULL) {
  29341. a->intData[0] = ASN_INTEGER;
  29342. a->intData[1] = 2;
  29343. a->intData[2] = 0x80;
  29344. a->intData[3] = 0x01;
  29345. a->negative = 1;
  29346. }
  29347. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  29348. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29349. DYNAMIC_TYPE_TMP_BUFFER));
  29350. tpp = pp;
  29351. if (tpp != NULL) {
  29352. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29353. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  29354. tpp -= 3;
  29355. ExpectIntEQ(tpp[0], 0xFF);
  29356. ExpectIntEQ(tpp[1], 0x7F);
  29357. ExpectIntEQ(tpp[2], 0xFF);
  29358. }
  29359. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29360. wolfSSL_ASN1_INTEGER_free(a);
  29361. #endif /* OPENSSL_EXTRA && !NO_ASN */
  29362. return EXPECT_RESULT();
  29363. }
  29364. static int test_wolfSSL_ASN1_OBJECT(void)
  29365. {
  29366. EXPECT_DECLS;
  29367. #if defined(OPENSSL_EXTRA)
  29368. ASN1_OBJECT* a = NULL;
  29369. ASN1_OBJECT s;
  29370. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  29371. /* Invalid parameter testing. */
  29372. ASN1_OBJECT_free(NULL);
  29373. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  29374. /* Test that a static ASN1_OBJECT can be freed. */
  29375. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  29376. ASN1_OBJECT_free(&s);
  29377. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  29378. ASN1_OBJECT_free(a);
  29379. a = NULL;
  29380. s.obj = der;
  29381. s.objSz = sizeof(der);
  29382. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  29383. ASN1_OBJECT_free(a);
  29384. ASN1_OBJECT_free(&s);
  29385. #endif /* OPENSSL_EXTRA */
  29386. return EXPECT_RESULT();
  29387. }
  29388. static int test_wolfSSL_ASN1_get_object(void)
  29389. {
  29390. EXPECT_DECLS;
  29391. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29392. const unsigned char* derBuf = cliecc_cert_der_256;
  29393. const unsigned char* nullPtr = NULL;
  29394. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  29395. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  29396. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  29397. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  29398. const unsigned char* p;
  29399. unsigned char objDer[8];
  29400. unsigned char* der;
  29401. unsigned char* derPtr;
  29402. int len = sizeof_cliecc_cert_der_256;
  29403. long asnLen = 0;
  29404. int tag = 0;
  29405. int cls = 0;
  29406. ASN1_OBJECT* a = NULL;
  29407. ASN1_OBJECT s;
  29408. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  29409. /* Invalid encoding at length. */
  29410. p = objDerInvalidLen;
  29411. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  29412. 0x80);
  29413. p = objDerBadLen;
  29414. /* Error = 0x80, Constructed = 0x20 */
  29415. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  29416. 0x80 | 0x20);
  29417. /* Read a couple TLV triplets and make sure they match the expected values
  29418. */
  29419. /* SEQUENCE */
  29420. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  29421. ExpectIntEQ(asnLen, 861);
  29422. ExpectIntEQ(tag, 0x10);
  29423. ExpectIntEQ(cls, 0);
  29424. /* SEQUENCE */
  29425. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  29426. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  29427. ExpectIntEQ(asnLen, 772);
  29428. ExpectIntEQ(tag, 0x10);
  29429. ExpectIntEQ(cls, 0);
  29430. /* [0] */
  29431. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  29432. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  29433. ExpectIntEQ(asnLen, 3);
  29434. ExpectIntEQ(tag, 0);
  29435. ExpectIntEQ(cls, 0x80);
  29436. /* INTEGER */
  29437. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  29438. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  29439. ExpectIntEQ(asnLen, 1);
  29440. ExpectIntEQ(tag, 0x2);
  29441. ExpectIntEQ(cls, 0);
  29442. derBuf += asnLen;
  29443. /* INTEGER */
  29444. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  29445. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  29446. ExpectIntEQ(asnLen, 20);
  29447. ExpectIntEQ(tag, 0x2);
  29448. ExpectIntEQ(cls, 0);
  29449. derBuf += asnLen;
  29450. /* SEQUENCE */
  29451. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  29452. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  29453. ExpectIntEQ(asnLen, 10);
  29454. ExpectIntEQ(tag, 0x10);
  29455. ExpectIntEQ(cls, 0);
  29456. /* Found OBJECT_ID. */
  29457. /* Invalid parameter testing. */
  29458. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  29459. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  29460. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  29461. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  29462. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  29463. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  29464. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  29465. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  29466. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  29467. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  29468. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  29469. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  29470. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  29471. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  29472. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  29473. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  29474. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  29475. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  29476. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  29477. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  29478. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  29479. /* Invalid encoding at length. */
  29480. p = objDerInvalidLen;
  29481. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  29482. p = objDerBadLen;
  29483. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  29484. p = objDerNotObj;
  29485. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  29486. p = objDerNoData;
  29487. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  29488. /* Create an ASN OBJECT from content */
  29489. p = derBuf + 2;
  29490. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  29491. ASN1_OBJECT_free(a);
  29492. a = NULL;
  29493. /* Create an ASN OBJECT from DER */
  29494. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  29495. /* Invalid parameter testing. */
  29496. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  29497. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  29498. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  29499. der = NULL;
  29500. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  29501. derPtr = objDer;
  29502. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  29503. ExpectPtrNE(derPtr, objDer);
  29504. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  29505. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  29506. ASN1_OBJECT_free(a);
  29507. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29508. return EXPECT_RESULT();
  29509. }
  29510. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  29511. {
  29512. EXPECT_DECLS;
  29513. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  29514. ASN1_OBJECT* obj = NULL;
  29515. ASN1_OBJECT* a = NULL;
  29516. BIO *bio = NULL;
  29517. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  29518. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  29519. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  29520. const unsigned char* p;
  29521. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  29522. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  29523. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  29524. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  29525. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  29526. /* No DER encoding in ASN1_OBJECT. */
  29527. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  29528. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  29529. ASN1_OBJECT_free(a);
  29530. a = NULL;
  29531. /* DER encoding - not OBJECT_ID */
  29532. p = notObjDer;
  29533. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  29534. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  29535. ASN1_OBJECT_free(a);
  29536. a = NULL;
  29537. /* Bad length encoding. */
  29538. p = badLenDer;
  29539. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  29540. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  29541. ASN1_OBJECT_free(a);
  29542. a = NULL;
  29543. /* Good encoding - but unknown. */
  29544. p = goodDer;
  29545. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  29546. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  29547. ASN1_OBJECT_free(a);
  29548. BIO_free(bio);
  29549. ASN1_OBJECT_free(obj);
  29550. #endif
  29551. return EXPECT_RESULT();
  29552. }
  29553. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  29554. {
  29555. EXPECT_DECLS;
  29556. #if defined(OPENSSL_EXTRA) && \
  29557. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  29558. char buf[50] = {0};
  29559. ASN1_OBJECT* obj;
  29560. const char* oid = "2.5.29.19";
  29561. const char* ln = "X509v3 Basic Constraints";
  29562. obj = NULL;
  29563. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  29564. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  29565. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  29566. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  29567. XMEMSET(buf, 0, sizeof(buf));
  29568. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  29569. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  29570. ASN1_OBJECT_free(obj);
  29571. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  29572. return EXPECT_RESULT();
  29573. }
  29574. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  29575. {
  29576. EXPECT_DECLS;
  29577. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  29578. WOLFSSL_STACK* sk = NULL;
  29579. WOLFSSL_ASN1_OBJECT* obj;
  29580. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  29581. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  29582. wolfSSL_sk_ASN1_OBJECT_free(sk);
  29583. sk = NULL;
  29584. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  29585. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  29586. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  29587. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  29588. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  29589. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  29590. sk = NULL;
  29591. /* obj freed in pop_free call. */
  29592. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  29593. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  29594. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  29595. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  29596. wolfSSL_sk_ASN1_OBJECT_free(sk);
  29597. wolfSSL_ASN1_OBJECT_free(obj);
  29598. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  29599. return EXPECT_RESULT();
  29600. }
  29601. static int test_wolfSSL_ASN1_STRING(void)
  29602. {
  29603. EXPECT_DECLS;
  29604. #if defined(OPENSSL_EXTRA)
  29605. ASN1_STRING* str = NULL;
  29606. ASN1_STRING* c = NULL;
  29607. const char data[] = "hello wolfSSL";
  29608. const char data2[] = "Same len data";
  29609. const char longData[] =
  29610. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  29611. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29612. ASN1_STRING_free(str);
  29613. str = NULL;
  29614. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29615. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  29616. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  29617. /* Check setting to NULL works. */
  29618. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  29619. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  29620. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  29621. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  29622. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  29623. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  29624. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  29625. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  29626. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  29627. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  29628. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  29629. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  29630. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  29631. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  29632. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  29633. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  29634. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  29635. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  29636. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  29637. /* Check setting back to smaller size frees dynamic data. */
  29638. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  29639. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  29640. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  29641. ExpectNull(ASN1_STRING_get0_data(NULL));
  29642. ExpectNotNull(ASN1_STRING_get0_data(str));
  29643. ExpectNull(ASN1_STRING_data(NULL));
  29644. ExpectNotNull(ASN1_STRING_data(str));
  29645. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  29646. ExpectIntGT(ASN1_STRING_length(str), 0);
  29647. ASN1_STRING_free(c);
  29648. ASN1_STRING_free(str);
  29649. ASN1_STRING_free(NULL);
  29650. #ifndef NO_WOLFSSL_STUB
  29651. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  29652. #endif
  29653. #endif
  29654. return EXPECT_RESULT();
  29655. }
  29656. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  29657. {
  29658. EXPECT_DECLS;
  29659. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  29660. !defined(NO_FILESYSTEM)
  29661. WOLFSSL_X509* x509 = NULL;
  29662. WOLFSSL_X509_NAME* subject = NULL;
  29663. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  29664. WOLFSSL_ASN1_STRING* a = NULL;
  29665. FILE* file = XBADFILE;
  29666. int idx = 0;
  29667. char targetOutput[16] = "www.wolfssl.com";
  29668. unsigned char* actual_output = NULL;
  29669. int len = 0;
  29670. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  29671. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  29672. if (file != NULL)
  29673. fclose(file);
  29674. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  29675. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  29676. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  29677. NID_commonName, -1)), 5);
  29678. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  29679. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  29680. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  29681. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, (size_t)len), 0);
  29682. a = NULL;
  29683. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  29684. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  29685. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  29686. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  29687. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  29688. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  29689. wolfSSL_X509_free(x509);
  29690. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29691. ExpectNotNull(a = ASN1_STRING_new());
  29692. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  29693. ASN1_STRING_free(a);
  29694. #endif
  29695. return EXPECT_RESULT();
  29696. }
  29697. static int test_wolfSSL_i2s_ASN1_STRING(void)
  29698. {
  29699. EXPECT_DECLS;
  29700. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  29701. WOLFSSL_ASN1_STRING* str = NULL;
  29702. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  29703. char* ret = NULL;
  29704. ExpectNotNull(str = ASN1_STRING_new());
  29705. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  29706. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29707. ret = NULL;
  29708. /* No data. */
  29709. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  29710. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29711. ret = NULL;
  29712. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  29713. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  29714. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29715. ret = NULL;
  29716. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  29717. /* No type. */
  29718. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  29719. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29720. ASN1_STRING_free(str);
  29721. #endif
  29722. return EXPECT_RESULT();
  29723. }
  29724. static int test_wolfSSL_ASN1_STRING_canon(void)
  29725. {
  29726. EXPECT_DECLS;
  29727. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  29728. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  29729. defined(OPENSSL_EXTRA_X509_SMALL))
  29730. WOLFSSL_ASN1_STRING* orig = NULL;
  29731. WOLFSSL_ASN1_STRING* canon = NULL;
  29732. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  29733. const char* whitespaceOnly = "\t\r\n";
  29734. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  29735. const char* canonData = "\x01 \x02 \xff tt";
  29736. const char longData[] =
  29737. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  29738. ExpectNotNull(orig = ASN1_STRING_new());
  29739. ExpectNotNull(canon = ASN1_STRING_new());
  29740. /* Invalid parameter testing. */
  29741. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  29742. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  29743. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  29744. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29745. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29746. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  29747. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29748. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29749. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  29750. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29751. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29752. ASN1_STRING_free(orig);
  29753. orig = NULL;
  29754. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  29755. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  29756. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29757. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  29758. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29759. ASN1_STRING_free(orig);
  29760. orig = NULL;
  29761. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  29762. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  29763. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29764. ASN1_STRING_free(orig);
  29765. orig = NULL;
  29766. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  29767. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29768. ASN1_STRING_free(orig);
  29769. ASN1_STRING_free(canon);
  29770. #endif
  29771. #endif
  29772. return EXPECT_RESULT();
  29773. }
  29774. static int test_wolfSSL_ASN1_STRING_print(void)
  29775. {
  29776. EXPECT_DECLS;
  29777. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  29778. !defined(NO_BIO)
  29779. ASN1_STRING* asnStr = NULL;
  29780. const char HELLO_DATA[]= \
  29781. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  29782. #define MAX_UNPRINTABLE_CHAR 32
  29783. #define MAX_BUF 255
  29784. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  29785. unsigned char expected[sizeof(unprintableData)+1];
  29786. unsigned char rbuf[MAX_BUF];
  29787. BIO *bio = NULL;
  29788. int p_len;
  29789. int i;
  29790. /* setup */
  29791. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  29792. unprintableData[i] = (unsigned char)HELLO_DATA[i];
  29793. expected[i] = (unsigned char)HELLO_DATA[i];
  29794. }
  29795. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  29796. unprintableData[sizeof(HELLO_DATA)+i] = i;
  29797. if (i == (int)'\n' || i == (int)'\r')
  29798. expected[sizeof(HELLO_DATA)+i] = i;
  29799. else
  29800. expected[sizeof(HELLO_DATA)+i] = '.';
  29801. }
  29802. unprintableData[sizeof(unprintableData)-1] = '\0';
  29803. expected[sizeof(expected)-1] = '\0';
  29804. XMEMSET(rbuf, 0, MAX_BUF);
  29805. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29806. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  29807. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29808. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  29809. (int)sizeof(unprintableData)), 1);
  29810. /* test */
  29811. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  29812. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  29813. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  29814. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  29815. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  29816. ExpectStrEQ((char*)rbuf, (const char*)expected);
  29817. BIO_free(bio);
  29818. bio = NULL;
  29819. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29820. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  29821. /* Ensure there is 0 bytes available to write into. */
  29822. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  29823. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  29824. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  29825. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  29826. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  29827. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  29828. BIO_free(bio);
  29829. ASN1_STRING_free(asnStr);
  29830. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  29831. return EXPECT_RESULT();
  29832. }
  29833. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  29834. {
  29835. EXPECT_DECLS;
  29836. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  29837. ASN1_STRING* asn_str = NULL;
  29838. const char data[] = "Hello wolfSSL!";
  29839. ASN1_STRING* esc_str = NULL;
  29840. const char esc_data[] = "a+;<>";
  29841. ASN1_STRING* neg_int = NULL;
  29842. const char neg_int_data[] = "\xff";
  29843. ASN1_STRING* neg_enum = NULL;
  29844. const char neg_enum_data[] = "\xff";
  29845. BIO *bio = NULL;
  29846. BIO *fixed = NULL;
  29847. unsigned long flags;
  29848. int p_len;
  29849. unsigned char rbuf[255];
  29850. /* setup */
  29851. XMEMSET(rbuf, 0, 255);
  29852. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29853. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  29854. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29855. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29856. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  29857. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29858. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  29859. sizeof(esc_data)), 1);
  29860. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  29861. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  29862. sizeof(neg_int_data) - 1), 1);
  29863. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  29864. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  29865. sizeof(neg_enum_data) - 1), 1);
  29866. /* Invalid parameter testing. */
  29867. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  29868. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  29869. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  29870. /* no flags */
  29871. XMEMSET(rbuf, 0, 255);
  29872. flags = 0;
  29873. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  29874. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  29875. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  29876. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29877. /* Ensure there is 0 bytes available to write into. */
  29878. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29879. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29880. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29881. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29882. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  29883. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29884. /* RFC2253 Escape */
  29885. XMEMSET(rbuf, 0, 255);
  29886. flags = ASN1_STRFLGS_ESC_2253;
  29887. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  29888. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  29889. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  29890. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29891. /* Ensure there is 0 bytes available to write into. */
  29892. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29893. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  29894. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29895. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  29896. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  29897. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  29898. /* Show type */
  29899. XMEMSET(rbuf, 0, 255);
  29900. flags = ASN1_STRFLGS_SHOW_TYPE;
  29901. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  29902. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  29903. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  29904. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29905. /* Ensure there is 0 bytes available to write into. */
  29906. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29907. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29908. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29909. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29910. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  29911. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29912. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  29913. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29914. /* Dump All */
  29915. XMEMSET(rbuf, 0, 255);
  29916. flags = ASN1_STRFLGS_DUMP_ALL;
  29917. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  29918. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  29919. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  29920. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29921. /* Ensure there is 0 bytes available to write into. */
  29922. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29923. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29924. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29925. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29926. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  29927. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29928. /* Dump Der */
  29929. XMEMSET(rbuf, 0, 255);
  29930. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  29931. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  29932. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  29933. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  29934. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29935. /* Ensure there is 0 bytes available to write into. */
  29936. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29937. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29938. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29939. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29940. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  29941. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29942. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  29943. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29944. /* Dump All + Show type */
  29945. XMEMSET(rbuf, 0, 255);
  29946. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  29947. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  29948. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  29949. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  29950. /* Dump All + Show type - Negative Integer. */
  29951. XMEMSET(rbuf, 0, 255);
  29952. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  29953. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  29954. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  29955. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  29956. /* Dump All + Show type - Negative Enumerated. */
  29957. XMEMSET(rbuf, 0, 255);
  29958. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  29959. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  29960. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  29961. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  29962. BIO_free(fixed);
  29963. BIO_free(bio);
  29964. ASN1_STRING_free(asn_str);
  29965. ASN1_STRING_free(esc_str);
  29966. ASN1_STRING_free(neg_int);
  29967. ASN1_STRING_free(neg_enum);
  29968. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  29969. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  29970. #endif
  29971. return EXPECT_RESULT();
  29972. }
  29973. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  29974. {
  29975. EXPECT_DECLS;
  29976. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  29977. ASN1_STRING* asn1str_test = NULL;
  29978. ASN1_STRING* asn1str_answer = NULL;
  29979. /* Each character is encoded using 4 bytes */
  29980. char input[] = {
  29981. 0, 0, 0, 'T',
  29982. 0, 0, 0, 'e',
  29983. 0, 0, 0, 's',
  29984. 0, 0, 0, 't',
  29985. };
  29986. char output[] = "Test";
  29987. char badInput[] = {
  29988. 1, 0, 0, 'T',
  29989. 0, 1, 0, 'e',
  29990. 0, 0, 1, 's',
  29991. };
  29992. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  29993. /* Test wrong type. */
  29994. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29995. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29996. ASN1_STRING_free(asn1str_test);
  29997. asn1str_test = NULL;
  29998. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  29999. /* Test bad length. */
  30000. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  30001. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  30002. /* Test bad input. */
  30003. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  30004. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  30005. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  30006. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  30007. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  30008. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  30009. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  30010. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  30011. ExpectNotNull(
  30012. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  30013. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  30014. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  30015. ASN1_STRING_free(asn1str_test);
  30016. ASN1_STRING_free(asn1str_answer);
  30017. #endif /* OPENSSL_ALL && !NO_ASN */
  30018. return EXPECT_RESULT();
  30019. }
  30020. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  30021. {
  30022. EXPECT_DECLS;
  30023. #if defined(OPENSSL_EXTRA)
  30024. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  30025. ExpectNotNull(asn1_gtime = ASN1_GENERALIZEDTIME_new());
  30026. if (asn1_gtime != NULL)
  30027. XMEMCPY(asn1_gtime->data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  30028. ASN1_GENERALIZEDTIME_free(asn1_gtime);
  30029. #endif /* OPENSSL_EXTRA */
  30030. return EXPECT_RESULT();
  30031. }
  30032. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  30033. {
  30034. EXPECT_DECLS;
  30035. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  30036. WOLFSSL_ASN1_GENERALIZEDTIME* gtime = NULL;
  30037. BIO* bio = NULL;
  30038. unsigned char buf[24];
  30039. int i;
  30040. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  30041. BIO_set_write_buf_size(bio, 24);
  30042. ExpectNotNull(gtime = ASN1_GENERALIZEDTIME_new());
  30043. /* Type not set. */
  30044. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  30045. ExpectIntEQ(wolfSSL_ASN1_TIME_set_string(gtime, "20180504123500Z"), 1);
  30046. /* Invalid parameters testing. */
  30047. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  30048. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  30049. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, gtime), BAD_FUNC_ARG);
  30050. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 1);
  30051. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  30052. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  30053. BIO_free(bio);
  30054. bio = NULL;
  30055. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  30056. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  30057. /* Ensure there is 0 bytes available to write into. */
  30058. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  30059. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  30060. for (i = 1; i < 20; i++) {
  30061. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  30062. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  30063. }
  30064. BIO_free(bio);
  30065. wolfSSL_ASN1_GENERALIZEDTIME_free(gtime);
  30066. #endif /* OPENSSL_EXTRA */
  30067. return EXPECT_RESULT();
  30068. }
  30069. static int test_wolfSSL_ASN1_TIME(void)
  30070. {
  30071. EXPECT_DECLS;
  30072. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  30073. WOLFSSL_ASN1_TIME* asn_time = NULL;
  30074. unsigned char *data;
  30075. ExpectNotNull(asn_time = ASN1_TIME_new());
  30076. #ifndef NO_WOLFSSL_STUB
  30077. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  30078. #endif
  30079. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  30080. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  30081. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  30082. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  30083. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  30084. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  30085. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  30086. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  30087. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  30088. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  30089. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  30090. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  30091. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  30092. ASN1_TIME_free(asn_time);
  30093. ASN1_TIME_free(NULL);
  30094. #endif
  30095. return EXPECT_RESULT();
  30096. }
  30097. static int test_wolfSSL_ASN1_TIME_to_string(void)
  30098. {
  30099. EXPECT_DECLS;
  30100. #ifndef NO_ASN_TIME
  30101. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  30102. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  30103. WOLFSSL_ASN1_TIME* t = NULL;
  30104. char buf[ASN_GENERALIZED_TIME_SIZE];
  30105. ExpectNotNull((t = ASN1_TIME_new()));
  30106. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  30107. /* Invalid parameter testing. */
  30108. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  30109. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  30110. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  30111. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  30112. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  30113. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  30114. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  30115. /* Buffer needs to be longer than minimum of 5 characters. */
  30116. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  30117. ASN1_TIME_free(t);
  30118. #endif
  30119. #endif /* NO_ASN_TIME */
  30120. return EXPECT_RESULT();
  30121. }
  30122. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  30123. {
  30124. EXPECT_DECLS;
  30125. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  30126. ASN1_TIME* fromTime = NULL;
  30127. ASN1_TIME* closeToTime = NULL;
  30128. ASN1_TIME* toTime = NULL;
  30129. ASN1_TIME* invalidTime = NULL;
  30130. int daysDiff = 0;
  30131. int secsDiff = 0;
  30132. ExpectNotNull((fromTime = ASN1_TIME_new()));
  30133. /* Feb 22, 2003, 21:15:15 */
  30134. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  30135. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  30136. /* Feb 22, 2003, 21:16:15 */
  30137. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  30138. ExpectNotNull((toTime = ASN1_TIME_new()));
  30139. /* Dec 19, 2010, 18:10:11 */
  30140. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  30141. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  30142. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  30143. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  30144. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  30145. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  30146. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  30147. /* Error conditions. */
  30148. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  30149. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  30150. /* If both times are NULL, difference is 0. */
  30151. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  30152. ExpectIntEQ(daysDiff, 0);
  30153. ExpectIntEQ(secsDiff, 0);
  30154. /* If one time is NULL, it defaults to the current time. */
  30155. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  30156. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  30157. /* Normal operation. Both times non-NULL. */
  30158. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  30159. ExpectIntEQ(daysDiff, 2856);
  30160. ExpectIntEQ(secsDiff, 75296);
  30161. /* Swapping the times should return negative values. */
  30162. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  30163. ExpectIntEQ(daysDiff, -2856);
  30164. ExpectIntEQ(secsDiff, -75296);
  30165. /* Compare with invalid time string. */
  30166. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  30167. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  30168. /* Compare with days difference of 0. */
  30169. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  30170. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  30171. /* Days and seconds differences not 0. */
  30172. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  30173. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  30174. /* Same time. */
  30175. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  30176. /* Compare regression test: No seconds difference, just difference in days.
  30177. */
  30178. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  30179. ASN1_TIME_set_string(toTime, "19800101000000Z");
  30180. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  30181. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  30182. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  30183. /* Edge case with Unix epoch. */
  30184. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  30185. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  30186. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  30187. ExpectIntEQ(daysDiff, 3652);
  30188. ExpectIntEQ(secsDiff, 0);
  30189. /* Edge case with year > 2038 (year 2038 problem). */
  30190. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  30191. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  30192. ExpectIntEQ(daysDiff, 2932896);
  30193. ExpectIntEQ(secsDiff, 86399);
  30194. ASN1_TIME_free(fromTime);
  30195. ASN1_TIME_free(closeToTime);
  30196. ASN1_TIME_free(toTime);
  30197. ASN1_TIME_free(invalidTime);
  30198. #endif
  30199. return EXPECT_RESULT();
  30200. }
  30201. static int test_wolfSSL_ASN1_TIME_adj(void)
  30202. {
  30203. EXPECT_DECLS;
  30204. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  30205. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  30206. const int year = 365*24*60*60;
  30207. const int day = 24*60*60;
  30208. const int hour = 60*60;
  30209. const int mini = 60;
  30210. const byte asn_utc_time = ASN_UTC_TIME;
  30211. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  30212. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  30213. #endif
  30214. WOLFSSL_ASN1_TIME* asn_time = NULL;
  30215. WOLFSSL_ASN1_TIME* s = NULL;
  30216. int offset_day;
  30217. long offset_sec;
  30218. char date_str[CTC_DATE_SIZE + 1];
  30219. time_t t;
  30220. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  30221. /* UTC notation test */
  30222. /* 2000/2/15 20:30:00 */
  30223. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  30224. offset_day = 7;
  30225. offset_sec = 45 * mini;
  30226. /* offset_sec = -45 * min;*/
  30227. ExpectNotNull(asn_time =
  30228. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  30229. ExpectTrue(asn_time->type == asn_utc_time);
  30230. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  30231. CTC_DATE_SIZE));
  30232. date_str[CTC_DATE_SIZE] = '\0';
  30233. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  30234. /* negative offset */
  30235. offset_sec = -45 * mini;
  30236. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  30237. ExpectNotNull(asn_time);
  30238. ExpectTrue(asn_time->type == asn_utc_time);
  30239. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  30240. CTC_DATE_SIZE));
  30241. date_str[CTC_DATE_SIZE] = '\0';
  30242. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  30243. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  30244. s = NULL;
  30245. XMEMSET(date_str, 0, sizeof(date_str));
  30246. /* Generalized time will overflow time_t if not long */
  30247. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  30248. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  30249. DYNAMIC_TYPE_OPENSSL);
  30250. /* GeneralizedTime notation test */
  30251. /* 2055/03/01 09:00:00 */
  30252. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  30253. offset_day = 12;
  30254. offset_sec = 10 * mini;
  30255. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  30256. offset_sec));
  30257. ExpectTrue(asn_time->type == asn_gen_time);
  30258. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  30259. CTC_DATE_SIZE));
  30260. date_str[CTC_DATE_SIZE] = '\0';
  30261. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  30262. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  30263. s = NULL;
  30264. XMEMSET(date_str, 0, sizeof(date_str));
  30265. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  30266. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  30267. s = NULL;
  30268. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  30269. offset_day = 7;
  30270. offset_sec = 45 * mini;
  30271. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  30272. offset_sec));
  30273. ExpectTrue(asn_time->type == asn_utc_time);
  30274. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  30275. CTC_DATE_SIZE));
  30276. date_str[CTC_DATE_SIZE] = '\0';
  30277. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  30278. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  30279. asn_time = NULL;
  30280. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  30281. offset_sec));
  30282. ExpectTrue(asn_time->type == asn_utc_time);
  30283. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  30284. CTC_DATE_SIZE));
  30285. date_str[CTC_DATE_SIZE] = '\0';
  30286. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  30287. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  30288. #endif
  30289. return EXPECT_RESULT();
  30290. }
  30291. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  30292. {
  30293. EXPECT_DECLS;
  30294. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  30295. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  30296. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  30297. ASN1_TIME asnTime;
  30298. struct tm tm;
  30299. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  30300. XMEMSET(&tm, 0, sizeof(struct tm));
  30301. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  30302. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  30303. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  30304. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  30305. ExpectIntEQ(tm.tm_sec, 15);
  30306. ExpectIntEQ(tm.tm_min, 15);
  30307. ExpectIntEQ(tm.tm_hour, 21);
  30308. ExpectIntEQ(tm.tm_mday, 22);
  30309. ExpectIntEQ(tm.tm_mon, 1);
  30310. ExpectIntEQ(tm.tm_year, 100);
  30311. ExpectIntEQ(tm.tm_isdst, 0);
  30312. #ifdef XMKTIME
  30313. ExpectIntEQ(tm.tm_wday, 2);
  30314. ExpectIntEQ(tm.tm_yday, 52);
  30315. #endif
  30316. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  30317. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  30318. ExpectIntEQ(tm.tm_year, 50);
  30319. /* Get current time. */
  30320. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  30321. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  30322. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  30323. /* 0 length. */
  30324. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30325. /* No type. */
  30326. asnTime.length = 1;
  30327. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30328. /* Not UTCTIME length. */
  30329. asnTime.type = V_ASN1_UTCTIME;
  30330. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30331. /* Not GENERALIZEDTIME length. */
  30332. asnTime.type = V_ASN1_GENERALIZEDTIME;
  30333. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30334. /* Not Zulu timezone. */
  30335. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  30336. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30337. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  30338. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30339. #ifdef XMKTIME
  30340. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  30341. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  30342. ExpectIntEQ(tm.tm_sec, 47);
  30343. ExpectIntEQ(tm.tm_min, 22);
  30344. ExpectIntEQ(tm.tm_hour, 21);
  30345. ExpectIntEQ(tm.tm_mday, 12);
  30346. ExpectIntEQ(tm.tm_mon, 4);
  30347. ExpectIntEQ(tm.tm_year, 123);
  30348. ExpectIntEQ(tm.tm_wday, 5);
  30349. ExpectIntEQ(tm.tm_yday, 131);
  30350. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  30351. fields are zeroed out as expected. */
  30352. ExpectIntEQ(tm.tm_isdst, 0);
  30353. #endif
  30354. #endif
  30355. return EXPECT_RESULT();
  30356. }
  30357. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  30358. {
  30359. EXPECT_DECLS;
  30360. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  30361. WOLFSSL_ASN1_TIME *t = NULL;
  30362. WOLFSSL_ASN1_TIME *out = NULL;
  30363. WOLFSSL_ASN1_TIME *gtime = NULL;
  30364. int tlen = 0;
  30365. unsigned char *data = NULL;
  30366. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  30367. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  30368. /* type not set. */
  30369. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  30370. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30371. t = NULL;
  30372. /* UTC Time test */
  30373. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  30374. if (t != NULL) {
  30375. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  30376. t->type = ASN_UTC_TIME;
  30377. t->length = ASN_UTC_TIME_SIZE;
  30378. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  30379. }
  30380. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  30381. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  30382. out = NULL;
  30383. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  30384. wolfSSL_ASN1_TIME_free(gtime);
  30385. gtime = NULL;
  30386. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  30387. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  30388. ExpectPtrEq(gtime, out);
  30389. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  30390. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  30391. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  30392. /* Generalized Time test */
  30393. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  30394. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  30395. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  30396. if (t != NULL) {
  30397. t->type = ASN_GENERALIZED_TIME;
  30398. t->length = ASN_GENERALIZED_TIME_SIZE;
  30399. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  30400. }
  30401. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  30402. ASN_GENERALIZED_TIME_SIZE);
  30403. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  30404. "20050727123456Z");
  30405. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  30406. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  30407. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  30408. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  30409. /* UTC Time to Generalized Time 1900's test */
  30410. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  30411. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  30412. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  30413. if (t != NULL) {
  30414. t->type = ASN_UTC_TIME;
  30415. t->length = ASN_UTC_TIME_SIZE;
  30416. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  30417. }
  30418. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  30419. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  30420. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  30421. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  30422. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30423. /* Null parameter test */
  30424. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  30425. gtime = NULL;
  30426. out = NULL;
  30427. if (t != NULL) {
  30428. t->type = ASN_UTC_TIME;
  30429. t->length = ASN_UTC_TIME_SIZE;
  30430. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  30431. }
  30432. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  30433. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  30434. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  30435. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  30436. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30437. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30438. #endif
  30439. return EXPECT_RESULT();
  30440. }
  30441. static int test_wolfSSL_ASN1_TIME_print(void)
  30442. {
  30443. EXPECT_DECLS;
  30444. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  30445. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  30446. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  30447. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  30448. !defined(NO_ASN_TIME)
  30449. BIO* bio = NULL;
  30450. BIO* fixed = NULL;
  30451. X509* x509 = NULL;
  30452. const unsigned char* der = client_cert_der_2048;
  30453. ASN1_TIME* notAfter = NULL;
  30454. ASN1_TIME* notBefore = NULL;
  30455. unsigned char buf[25];
  30456. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  30457. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  30458. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  30459. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  30460. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  30461. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  30462. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  30463. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  30464. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  30465. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  30466. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  30467. /* Test BIO_write fails. */
  30468. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  30469. /* Ensure there is 0 bytes available to write into. */
  30470. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  30471. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  30472. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  30473. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  30474. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  30475. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  30476. /* create a bad time and test results */
  30477. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  30478. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  30479. if (EXPECT_SUCCESS()) {
  30480. notAfter->data[8] = 0;
  30481. notAfter->data[3] = 0;
  30482. }
  30483. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  30484. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  30485. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  30486. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  30487. BIO_free(bio);
  30488. BIO_free(fixed);
  30489. X509_free(x509);
  30490. #endif
  30491. return EXPECT_RESULT();
  30492. }
  30493. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  30494. {
  30495. EXPECT_DECLS;
  30496. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  30497. BIO* bio = NULL;
  30498. ASN1_UTCTIME* utc = NULL;
  30499. unsigned char buf[25];
  30500. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  30501. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  30502. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  30503. /* Valid date */
  30504. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  30505. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  30506. DYNAMIC_TYPE_ASN1));
  30507. if (utc != NULL) {
  30508. utc->type = ASN_UTC_TIME;
  30509. utc->length = ASN_UTC_TIME_SIZE;
  30510. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  30511. }
  30512. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  30513. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  30514. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  30515. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  30516. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  30517. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  30518. XMEMSET(buf, 0, sizeof(buf));
  30519. BIO_free(bio);
  30520. bio = NULL;
  30521. /* Invalid format */
  30522. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  30523. if (utc != NULL) {
  30524. utc->type = ASN_UTC_TIME;
  30525. utc->length = ASN_UTC_TIME_SIZE;
  30526. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  30527. }
  30528. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  30529. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  30530. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  30531. /* Invalid type */
  30532. if (utc != NULL) {
  30533. utc->type = ASN_GENERALIZED_TIME;
  30534. utc->length = ASN_GENERALIZED_TIME_SIZE;
  30535. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  30536. }
  30537. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  30538. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  30539. BIO_free(bio);
  30540. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  30541. return EXPECT_RESULT();
  30542. }
  30543. static int test_wolfSSL_ASN1_TYPE(void)
  30544. {
  30545. EXPECT_DECLS;
  30546. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  30547. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  30548. WOLFSSL_ASN1_TYPE* t = NULL;
  30549. WOLFSSL_ASN1_OBJECT* obj = NULL;
  30550. #ifndef NO_ASN_TIME
  30551. WOLFSSL_ASN1_TIME* time = NULL;
  30552. #endif
  30553. WOLFSSL_ASN1_STRING* str = NULL;
  30554. unsigned char data[] = { 0x00 };
  30555. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  30556. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30557. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  30558. wolfSSL_ASN1_TYPE_free(t);
  30559. t = NULL;
  30560. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30561. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  30562. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  30563. wolfSSL_ASN1_TYPE_free(t);
  30564. t = NULL;
  30565. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30566. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  30567. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  30568. wolfSSL_ASN1_TYPE_free(t);
  30569. t = NULL;
  30570. #ifndef NO_ASN_TIME
  30571. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30572. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  30573. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  30574. wolfSSL_ASN1_TYPE_free(t);
  30575. t = NULL;
  30576. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30577. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  30578. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  30579. wolfSSL_ASN1_TYPE_free(t);
  30580. t = NULL;
  30581. #endif
  30582. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30583. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30584. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  30585. wolfSSL_ASN1_TYPE_free(t);
  30586. t = NULL;
  30587. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30588. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30589. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  30590. wolfSSL_ASN1_TYPE_free(t);
  30591. t = NULL;
  30592. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30593. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30594. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  30595. wolfSSL_ASN1_TYPE_free(t);
  30596. t = NULL;
  30597. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30598. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30599. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  30600. wolfSSL_ASN1_TYPE_free(t);
  30601. t = NULL;
  30602. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30603. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30604. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  30605. wolfSSL_ASN1_TYPE_free(t);
  30606. t = NULL;
  30607. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30608. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30609. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  30610. wolfSSL_ASN1_TYPE_free(t);
  30611. t = NULL;
  30612. #endif
  30613. return EXPECT_RESULT();
  30614. }
  30615. /* Testing code used in dpp.c in hostap */
  30616. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30617. typedef struct {
  30618. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  30619. * as an OID identifying the curve */
  30620. X509_ALGOR *alg;
  30621. /* Compressed format public key per ANSI X9.63 */
  30622. ASN1_BIT_STRING *pub_key;
  30623. } DPP_BOOTSTRAPPING_KEY;
  30624. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  30625. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  30626. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  30627. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  30628. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  30629. typedef struct {
  30630. ASN1_INTEGER *integer;
  30631. } TEST_ASN1;
  30632. ASN1_SEQUENCE(TEST_ASN1) = {
  30633. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  30634. } ASN1_SEQUENCE_END(TEST_ASN1)
  30635. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  30636. typedef struct {
  30637. ASN1_OCTET_STRING *octet_string;
  30638. } TEST_FAIL_ASN1;
  30639. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  30640. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  30641. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  30642. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  30643. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  30644. #endif
  30645. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  30646. {
  30647. EXPECT_DECLS;
  30648. /* Testing code used in dpp.c in hostap */
  30649. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30650. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  30651. EC_KEY *eckey = NULL;
  30652. EVP_PKEY *key = NULL;
  30653. size_t len = 0;
  30654. unsigned char *der = NULL;
  30655. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  30656. const unsigned char *in = ecc_clikey_der_256;
  30657. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  30658. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  30659. const EC_GROUP *group = NULL;
  30660. const EC_POINT *point = NULL;
  30661. int nid;
  30662. TEST_ASN1 *test_asn1 = NULL;
  30663. TEST_FAIL_ASN1 test_fail_asn1;
  30664. const unsigned char badObjDer[] = { 0x06, 0x00 };
  30665. const unsigned char goodObjDer[] = {
  30666. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  30667. };
  30668. WOLFSSL_ASN1_ITEM emptyTemplate;
  30669. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  30670. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  30671. der = NULL;
  30672. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  30673. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  30674. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30675. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  30676. (long)sizeof_ecc_clikey_der_256));
  30677. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  30678. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  30679. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  30680. nid = EC_GROUP_get_curve_name(group);
  30681. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  30682. group_obj = OBJ_nid2obj(nid);
  30683. if ((ec_obj != NULL) && (group_obj != NULL)) {
  30684. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  30685. group_obj), 1);
  30686. if (EXPECT_SUCCESS()) {
  30687. ec_obj = NULL;
  30688. group_obj = NULL;
  30689. }
  30690. }
  30691. wolfSSL_ASN1_OBJECT_free(group_obj);
  30692. wolfSSL_ASN1_OBJECT_free(ec_obj);
  30693. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  30694. #ifdef HAVE_COMP_KEY
  30695. ExpectIntGT((len = EC_POINT_point2oct(
  30696. group, point, POINT_CONVERSION_COMPRESSED,
  30697. NULL, 0, NULL)), 0);
  30698. #else
  30699. ExpectIntGT((len = EC_POINT_point2oct(
  30700. group, point, POINT_CONVERSION_UNCOMPRESSED,
  30701. NULL, 0, NULL)), 0);
  30702. #endif
  30703. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  30704. #ifdef HAVE_COMP_KEY
  30705. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  30706. der, len-1, NULL), 0);
  30707. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  30708. der, len, NULL), len);
  30709. #else
  30710. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  30711. der, len-1, NULL), 0);
  30712. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  30713. der, len, NULL), len);
  30714. #endif
  30715. if (EXPECT_SUCCESS()) {
  30716. bootstrap->pub_key->data = der;
  30717. bootstrap->pub_key->length = (int)len;
  30718. /* Not actually used */
  30719. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  30720. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  30721. }
  30722. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  30723. der = NULL;
  30724. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30725. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30726. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30727. EVP_PKEY_free(key);
  30728. EC_KEY_free(eckey);
  30729. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  30730. bootstrap = NULL;
  30731. DPP_BOOTSTRAPPING_KEY_free(NULL);
  30732. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  30733. * a NULL and an empty BIT_STRING. */
  30734. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  30735. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  30736. if (EXPECT_SUCCESS()) {
  30737. bootstrap->alg->algorithm->obj = badObjDer;
  30738. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  30739. }
  30740. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  30741. if (EXPECT_SUCCESS()) {
  30742. bootstrap->alg->parameter->type = V_ASN1_NULL;
  30743. bootstrap->alg->parameter->value.ptr = NULL;
  30744. bootstrap->pub_key->data = NULL;
  30745. bootstrap->pub_key->length = 0;
  30746. /* Not actually used */
  30747. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  30748. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  30749. }
  30750. /* Encode with bad OBJECT_ID. */
  30751. der = NULL;
  30752. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30753. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  30754. if (EXPECT_SUCCESS()) {
  30755. bootstrap->alg->algorithm->obj = goodObjDer;
  30756. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  30757. bootstrap->alg->algorithm->grp = 2;
  30758. }
  30759. der = NULL;
  30760. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  30761. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  30762. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30763. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  30764. /* Test integer */
  30765. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  30766. der = NULL;
  30767. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  30768. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  30769. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30770. TEST_ASN1_free(test_asn1);
  30771. /* Test integer cases. */
  30772. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  30773. TEST_ASN1_free(NULL);
  30774. /* Test error cases. */
  30775. ExpectNull(TEST_FAIL_ASN1_new());
  30776. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  30777. TEST_FAIL_ASN1_free(NULL);
  30778. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  30779. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  30780. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  30781. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  30782. return EXPECT_RESULT();
  30783. }
  30784. static int test_wolfSSL_lhash(void)
  30785. {
  30786. EXPECT_DECLS;
  30787. #ifdef OPENSSL_ALL
  30788. const char testStr[] = "Like a true nature's child\n"
  30789. "We were born\n"
  30790. "Born to be wild";
  30791. #ifdef NO_SHA
  30792. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  30793. #else
  30794. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  30795. #endif
  30796. #endif
  30797. return EXPECT_RESULT();
  30798. }
  30799. static int test_wolfSSL_X509_NAME(void)
  30800. {
  30801. EXPECT_DECLS;
  30802. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  30803. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30804. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  30805. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  30806. defined(OPENSSL_EXTRA))
  30807. X509* x509 = NULL;
  30808. const unsigned char* c = NULL;
  30809. unsigned char buf[4096];
  30810. int bytes = 0;
  30811. XFILE f = XBADFILE;
  30812. const X509_NAME* a = NULL;
  30813. const X509_NAME* b = NULL;
  30814. X509_NAME* d2i_name = NULL;
  30815. int sz = 0;
  30816. unsigned char* tmp = NULL;
  30817. char file[] = "./certs/ca-cert.der";
  30818. #ifndef OPENSSL_EXTRA_X509_SMALL
  30819. byte empty[] = { /* CN=empty emailAddress= */
  30820. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  30821. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  30822. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  30823. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  30824. 0x01, 0x16, 0x00
  30825. };
  30826. #endif
  30827. #ifndef OPENSSL_EXTRA_X509_SMALL
  30828. /* test compile of deprecated function, returns 0 */
  30829. ExpectIntEQ(CRYPTO_thread_id(), 0);
  30830. #endif
  30831. ExpectNotNull(a = X509_NAME_new());
  30832. X509_NAME_free((X509_NAME*)a);
  30833. a = NULL;
  30834. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  30835. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  30836. if (f != XBADFILE)
  30837. XFCLOSE(f);
  30838. c = buf;
  30839. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  30840. /* test cmp function */
  30841. ExpectNotNull(a = X509_get_issuer_name(x509));
  30842. ExpectNotNull(b = X509_get_subject_name(x509));
  30843. #ifndef OPENSSL_EXTRA_X509_SMALL
  30844. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  30845. #endif
  30846. tmp = buf;
  30847. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  30848. if (sz > 0 && tmp == buf) {
  30849. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  30850. __LINE__);
  30851. fprintf(stderr, " Expected pointer to be incremented\n");
  30852. abort();
  30853. }
  30854. #ifndef OPENSSL_EXTRA_X509_SMALL
  30855. tmp = buf;
  30856. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  30857. #endif
  30858. /* if output parameter is NULL, should still return required size. */
  30859. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  30860. /* retry but with the function creating a buffer */
  30861. tmp = NULL;
  30862. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  30863. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  30864. tmp = NULL;
  30865. #ifdef WOLFSSL_CERT_NAME_ALL
  30866. /* test for givenName and name */
  30867. {
  30868. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  30869. const byte gName[] = "test-given-name";
  30870. const byte name[] = "test-name";
  30871. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  30872. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  30873. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  30874. 1);
  30875. wolfSSL_X509_NAME_ENTRY_free(entry);
  30876. entry = NULL;
  30877. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  30878. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  30879. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  30880. 1);
  30881. wolfSSL_X509_NAME_ENTRY_free(entry);
  30882. tmp = NULL;
  30883. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  30884. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  30885. }
  30886. #endif
  30887. b = NULL;
  30888. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  30889. #ifndef OPENSSL_EXTRA_X509_SMALL
  30890. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  30891. #endif
  30892. X509_NAME_free((X509_NAME*)b);
  30893. X509_NAME_free(d2i_name);
  30894. d2i_name = NULL;
  30895. X509_free(x509);
  30896. #ifndef OPENSSL_EXTRA_X509_SMALL
  30897. /* test with an empty domain component */
  30898. tmp = empty;
  30899. sz = sizeof(empty);
  30900. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  30901. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  30902. /* size of empty emailAddress will be 0 */
  30903. tmp = buf;
  30904. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  30905. (char*)tmp, sizeof(buf)), 0);
  30906. /* should contain no organization name */
  30907. tmp = buf;
  30908. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  30909. (char*)tmp, sizeof(buf)), -1);
  30910. X509_NAME_free(d2i_name);
  30911. #endif
  30912. #endif
  30913. return EXPECT_RESULT();
  30914. }
  30915. static int test_wolfSSL_X509_NAME_hash(void)
  30916. {
  30917. EXPECT_DECLS;
  30918. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  30919. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  30920. BIO* bio = NULL;
  30921. X509* x509 = NULL;
  30922. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  30923. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  30924. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  30925. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  30926. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  30927. X509_free(x509);
  30928. BIO_free(bio);
  30929. #endif
  30930. return EXPECT_RESULT();
  30931. }
  30932. static int test_wolfSSL_X509_NAME_print_ex(void)
  30933. {
  30934. EXPECT_DECLS;
  30935. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  30936. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  30937. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  30938. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  30939. !defined(NO_BIO) && !defined(NO_RSA)
  30940. int memSz = 0;
  30941. byte* mem = NULL;
  30942. BIO* bio = NULL;
  30943. BIO* membio = NULL;
  30944. X509* x509 = NULL;
  30945. X509_NAME* name = NULL;
  30946. const char* expNormal = "C=US, CN=wolfssl.com";
  30947. const char* expEqSpace = "C = US, CN = wolfssl.com";
  30948. const char* expReverse = "CN=wolfssl.com, C=US";
  30949. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  30950. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  30951. const char* expRFC5523 =
  30952. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  30953. /* Test with real cert (svrCertFile) first */
  30954. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  30955. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  30956. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  30957. ExpectNotNull(name = X509_get_subject_name(x509));
  30958. /* Test without flags */
  30959. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30960. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  30961. BIO_free(membio);
  30962. membio = NULL;
  30963. /* Test flag: XN_FLAG_RFC2253 */
  30964. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30965. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30966. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  30967. BIO_free(membio);
  30968. membio = NULL;
  30969. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  30970. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30971. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30972. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  30973. BIO_free(membio);
  30974. membio = NULL;
  30975. X509_free(x509);
  30976. BIO_free(bio);
  30977. name = NULL;
  30978. /* Test normal case without escaped characters */
  30979. {
  30980. /* Create name: "/C=US/CN=wolfssl.com" */
  30981. ExpectNotNull(name = X509_NAME_new());
  30982. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  30983. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  30984. WOLFSSL_SUCCESS);
  30985. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  30986. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  30987. WOLFSSL_SUCCESS);
  30988. /* Test without flags */
  30989. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30990. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  30991. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30992. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  30993. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  30994. BIO_free(membio);
  30995. membio = NULL;
  30996. /* Test with XN_FLAG_ONELINE which should enable XN_FLAG_SPC_EQ for
  30997. spaces aroun '=' */
  30998. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30999. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, XN_FLAG_ONELINE),
  31000. WOLFSSL_SUCCESS);
  31001. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  31002. ExpectIntEQ(memSz, XSTRLEN(expEqSpace));
  31003. ExpectIntEQ(XSTRNCMP((char*)mem, expEqSpace, XSTRLEN(expEqSpace)), 0);
  31004. BIO_free(membio);
  31005. membio = NULL;
  31006. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  31007. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  31008. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  31009. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  31010. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  31011. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  31012. BIO_free(membio);
  31013. membio = NULL;
  31014. /* Test flags: XN_FLAG_DN_REV - reversed */
  31015. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  31016. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  31017. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  31018. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  31019. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  31020. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  31021. BIO_free(membio);
  31022. membio = NULL;
  31023. X509_NAME_free(name);
  31024. name = NULL;
  31025. }
  31026. /* Test RFC2253 characters are escaped with backslashes */
  31027. {
  31028. ExpectNotNull(name = X509_NAME_new());
  31029. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  31030. /* space at beginning and end, and: ,+"\ */
  31031. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  31032. WOLFSSL_SUCCESS);
  31033. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  31034. /* # at beginning, and: <>;*/
  31035. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  31036. WOLFSSL_SUCCESS);
  31037. /* Test without flags */
  31038. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  31039. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  31040. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  31041. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  31042. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  31043. XSTRLEN(expNotEscaped)), 0);
  31044. BIO_free(membio);
  31045. membio = NULL;
  31046. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  31047. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  31048. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  31049. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  31050. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  31051. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  31052. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  31053. BIO_free(membio);
  31054. membio = NULL;
  31055. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  31056. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  31057. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  31058. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  31059. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  31060. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  31061. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  31062. XSTRLEN(expNotEscapedRev)), 0);
  31063. BIO_free(membio);
  31064. X509_NAME_free(name);
  31065. }
  31066. #endif
  31067. return EXPECT_RESULT();
  31068. }
  31069. #ifndef NO_BIO
  31070. static int test_wolfSSL_X509_INFO_multiple_info(void)
  31071. {
  31072. EXPECT_DECLS;
  31073. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  31074. STACK_OF(X509_INFO) *info_stack = NULL;
  31075. X509_INFO *info = NULL;
  31076. int len;
  31077. int i;
  31078. const char* files[] = {
  31079. cliCertFile,
  31080. cliKeyFile,
  31081. /* This needs to be the order as svrCertFile contains the
  31082. * intermediate cert as well. */
  31083. svrKeyFile,
  31084. svrCertFile,
  31085. NULL,
  31086. };
  31087. const char** curFile;
  31088. BIO *fileBIO = NULL;
  31089. BIO *concatBIO = NULL;
  31090. byte tmp[FOURK_BUF];
  31091. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  31092. * to group objects together. */
  31093. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  31094. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  31095. int fileLen = 0;
  31096. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  31097. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  31098. if (EXPECT_SUCCESS()) {
  31099. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  31100. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  31101. fileLen -= len;
  31102. if (EXPECT_FAIL())
  31103. break;
  31104. }
  31105. /* Make sure we read the entire file */
  31106. ExpectIntEQ(fileLen, 0);
  31107. }
  31108. BIO_free(fileBIO);
  31109. fileBIO = NULL;
  31110. }
  31111. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  31112. NULL));
  31113. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  31114. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  31115. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  31116. ExpectNotNull(info->x509);
  31117. ExpectNull(info->crl);
  31118. if (i != 0) {
  31119. ExpectNotNull(info->x_pkey);
  31120. ExpectIntEQ(X509_check_private_key(info->x509,
  31121. info->x_pkey->dec_pkey), 1);
  31122. }
  31123. else {
  31124. ExpectNull(info->x_pkey);
  31125. }
  31126. }
  31127. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  31128. BIO_free(concatBIO);
  31129. #endif
  31130. return EXPECT_RESULT();
  31131. }
  31132. #endif
  31133. #ifndef NO_BIO
  31134. static int test_wolfSSL_X509_INFO(void)
  31135. {
  31136. EXPECT_DECLS;
  31137. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  31138. STACK_OF(X509_INFO) *info_stack = NULL;
  31139. X509_INFO *info = NULL;
  31140. BIO *cert = NULL;
  31141. int i;
  31142. /* PEM in hex format to avoid null terminator */
  31143. byte data[] = {
  31144. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  31145. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  31146. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  31147. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  31148. 0x2d, 0x2d, 0x2d
  31149. };
  31150. /* PEM in hex format to avoid null terminator */
  31151. byte data2[] = {
  31152. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  31153. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  31154. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  31155. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  31156. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  31157. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  31158. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  31159. };
  31160. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  31161. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  31162. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  31163. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  31164. ExpectNotNull(info->x509);
  31165. ExpectNull(info->crl);
  31166. ExpectNull(info->x_pkey);
  31167. }
  31168. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  31169. info_stack = NULL;
  31170. BIO_free(cert);
  31171. cert = NULL;
  31172. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  31173. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  31174. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  31175. info_stack = NULL;
  31176. BIO_free(cert);
  31177. cert = NULL;
  31178. /* This case should fail due to invalid input. */
  31179. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  31180. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  31181. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  31182. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  31183. info_stack = NULL;
  31184. BIO_free(cert);
  31185. cert = NULL;
  31186. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  31187. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  31188. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  31189. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  31190. BIO_free(cert);
  31191. #endif
  31192. return EXPECT_RESULT();
  31193. }
  31194. #endif
  31195. static int test_wolfSSL_X509_subject_name_hash(void)
  31196. {
  31197. EXPECT_DECLS;
  31198. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31199. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  31200. X509* x509 = NULL;
  31201. X509_NAME* subjectName = NULL;
  31202. unsigned long ret1 = 0;
  31203. unsigned long ret2 = 0;
  31204. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31205. SSL_FILETYPE_PEM));
  31206. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  31207. /* These two
  31208. * - X509_subject_name_hash(x509)
  31209. * - X509_NAME_hash(X509_get_subject_name(x509))
  31210. * should give the same hash, if !defined(NO_SHA) is true. */
  31211. ret1 = X509_subject_name_hash(x509);
  31212. ExpectIntNE(ret1, 0);
  31213. #if !defined(NO_SHA)
  31214. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  31215. ExpectIntNE(ret2, 0);
  31216. ExpectIntEQ(ret1, ret2);
  31217. #else
  31218. (void) ret2;
  31219. #endif
  31220. X509_free(x509);
  31221. #endif
  31222. return EXPECT_RESULT();
  31223. }
  31224. static int test_wolfSSL_X509_issuer_name_hash(void)
  31225. {
  31226. EXPECT_DECLS;
  31227. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  31228. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  31229. X509* x509 = NULL;
  31230. X509_NAME* issuertName = NULL;
  31231. unsigned long ret1 = 0;
  31232. unsigned long ret2 = 0;
  31233. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31234. SSL_FILETYPE_PEM));
  31235. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  31236. /* These two
  31237. * - X509_issuer_name_hash(x509)
  31238. * - X509_NAME_hash(X509_get_issuer_name(x509))
  31239. * should give the same hash, if !defined(NO_SHA) is true. */
  31240. ret1 = X509_issuer_name_hash(x509);
  31241. ExpectIntNE(ret1, 0);
  31242. #if !defined(NO_SHA)
  31243. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  31244. ExpectIntNE(ret2, 0);
  31245. ExpectIntEQ(ret1, ret2);
  31246. #else
  31247. (void) ret2;
  31248. #endif
  31249. X509_free(x509);
  31250. #endif
  31251. return EXPECT_RESULT();
  31252. }
  31253. static int test_wolfSSL_X509_check_host(void)
  31254. {
  31255. EXPECT_DECLS;
  31256. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  31257. && !defined(NO_SHA) && !defined(NO_RSA)
  31258. X509* x509 = NULL;
  31259. const char altName[] = "example.com";
  31260. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31261. SSL_FILETYPE_PEM));
  31262. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  31263. WOLFSSL_SUCCESS);
  31264. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  31265. WOLFSSL_FAILURE);
  31266. X509_free(x509);
  31267. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  31268. WOLFSSL_FAILURE);
  31269. #endif
  31270. return EXPECT_RESULT();
  31271. }
  31272. static int test_wolfSSL_X509_check_email(void)
  31273. {
  31274. EXPECT_DECLS;
  31275. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  31276. X509* x509 = NULL;
  31277. const char goodEmail[] = "info@wolfssl.com";
  31278. const char badEmail[] = "disinfo@wolfssl.com";
  31279. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31280. SSL_FILETYPE_PEM));
  31281. /* Should fail on non-matching email address */
  31282. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  31283. WOLFSSL_FAILURE);
  31284. /* Should succeed on matching email address */
  31285. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  31286. WOLFSSL_SUCCESS);
  31287. /* Should compute length internally when not provided */
  31288. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  31289. WOLFSSL_SUCCESS);
  31290. /* Should fail when email address is NULL */
  31291. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  31292. WOLFSSL_FAILURE);
  31293. X509_free(x509);
  31294. /* Should fail when x509 is NULL */
  31295. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  31296. WOLFSSL_FAILURE);
  31297. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  31298. return EXPECT_RESULT();
  31299. }
  31300. static int test_wc_PemToDer(void)
  31301. {
  31302. EXPECT_DECLS;
  31303. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  31304. int ret;
  31305. DerBuffer* pDer = NULL;
  31306. const char* ca_cert = "./certs/server-cert.pem";
  31307. byte* cert_buf = NULL;
  31308. size_t cert_sz = 0;
  31309. int eccKey = 0;
  31310. EncryptedInfo info;
  31311. XMEMSET(&info, 0, sizeof(info));
  31312. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  31313. ExpectIntEQ(ret = wc_PemToDer(cert_buf, (long int)cert_sz, CERT_TYPE, &pDer, NULL,
  31314. &info, &eccKey), 0);
  31315. wc_FreeDer(&pDer);
  31316. pDer = NULL;
  31317. if (cert_buf != NULL) {
  31318. free(cert_buf);
  31319. cert_buf = NULL;
  31320. }
  31321. #ifdef HAVE_ECC
  31322. {
  31323. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  31324. byte key_buf[256] = {0};
  31325. /* Test fail of loading a key with cert type */
  31326. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  31327. key_buf[0] = '\n';
  31328. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  31329. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  31330. &pDer, NULL, &info, &eccKey)), 0);
  31331. #ifdef OPENSSL_EXTRA
  31332. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  31333. &pDer, NULL, &info, &eccKey)), 0);
  31334. #endif
  31335. wc_FreeDer(&pDer);
  31336. if (cert_buf != NULL)
  31337. free(cert_buf);
  31338. }
  31339. #endif
  31340. #endif
  31341. return EXPECT_RESULT();
  31342. }
  31343. static int test_wc_AllocDer(void)
  31344. {
  31345. EXPECT_DECLS;
  31346. #if !defined(NO_CERTS)
  31347. DerBuffer* pDer = NULL;
  31348. word32 testSize = 1024;
  31349. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  31350. BAD_FUNC_ARG);
  31351. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  31352. ExpectNotNull(pDer);
  31353. wc_FreeDer(&pDer);
  31354. #endif
  31355. return EXPECT_RESULT();
  31356. }
  31357. static int test_wc_CertPemToDer(void)
  31358. {
  31359. EXPECT_DECLS;
  31360. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  31361. const char* ca_cert = "./certs/ca-cert.pem";
  31362. byte* cert_buf = NULL;
  31363. size_t cert_sz = 0;
  31364. size_t cert_dersz = 0;
  31365. byte* cert_der = NULL;
  31366. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  31367. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  31368. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31369. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  31370. (int)cert_dersz, CERT_TYPE), 0);
  31371. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  31372. BAD_FUNC_ARG);
  31373. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  31374. BAD_FUNC_ARG);
  31375. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  31376. BAD_FUNC_ARG);
  31377. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  31378. CERT_TYPE), BAD_FUNC_ARG);
  31379. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  31380. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  31381. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  31382. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  31383. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  31384. CERT_TYPE), BAD_FUNC_ARG);
  31385. if (cert_der != NULL)
  31386. free(cert_der);
  31387. if (cert_buf != NULL)
  31388. free(cert_buf);
  31389. #endif
  31390. return EXPECT_RESULT();
  31391. }
  31392. static int test_wc_KeyPemToDer(void)
  31393. {
  31394. EXPECT_DECLS;
  31395. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31396. int ret = 0;
  31397. const byte cert_buf[] = \
  31398. "-----BEGIN PRIVATE KEY-----\n"
  31399. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  31400. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  31401. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  31402. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  31403. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  31404. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  31405. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  31406. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  31407. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  31408. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  31409. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  31410. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  31411. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  31412. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  31413. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  31414. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  31415. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  31416. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  31417. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  31418. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  31419. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  31420. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  31421. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  31422. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  31423. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  31424. "AQue5o6oVfhgLiJzMdo/77gw\n"
  31425. "-----END PRIVATE KEY-----\n";
  31426. const int cert_sz = sizeof(cert_buf);
  31427. const char cert_pw[] = "password";
  31428. int cert_dersz = 0;
  31429. byte* cert_der = NULL;
  31430. /* Bad arg: Cert buffer is NULL */
  31431. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  31432. BAD_FUNC_ARG);
  31433. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  31434. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  31435. BAD_FUNC_ARG);
  31436. /* Test normal operation */
  31437. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  31438. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  31439. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  31440. cert_pw), 0);
  31441. ExpectIntLE(ret, cert_sz);
  31442. if (cert_der != NULL) {
  31443. free(cert_der);
  31444. cert_der = NULL;
  31445. }
  31446. /* Test NULL for DER buffer to return needed DER buffer size */
  31447. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  31448. ExpectIntLE(ret, cert_sz);
  31449. if (EXPECT_SUCCESS())
  31450. cert_dersz = ret;
  31451. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  31452. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  31453. cert_pw), 0);
  31454. ExpectIntLE(ret, cert_sz);
  31455. if (cert_der != NULL)
  31456. free(cert_der);
  31457. #endif
  31458. return EXPECT_RESULT();
  31459. }
  31460. static int test_wc_PubKeyPemToDer(void)
  31461. {
  31462. EXPECT_DECLS;
  31463. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  31464. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  31465. int ret = 0;
  31466. const char* key = "./certs/ecc-client-keyPub.pem";
  31467. byte* cert_buf = NULL;
  31468. size_t cert_sz = 0, cert_dersz = 0;
  31469. byte* cert_der = NULL;
  31470. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  31471. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  31472. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  31473. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  31474. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31475. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  31476. (int)cert_dersz), 0);
  31477. if (cert_der != NULL) {
  31478. free(cert_der);
  31479. cert_der = NULL;
  31480. }
  31481. /* Test NULL for DER buffer to return needed DER buffer size */
  31482. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  31483. ExpectIntLE(ret, cert_sz);
  31484. cert_dersz = (size_t)ret;
  31485. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31486. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  31487. (int)cert_dersz), 0);
  31488. if (cert_der != NULL) {
  31489. free(cert_der);
  31490. }
  31491. if (cert_buf != NULL) {
  31492. free(cert_buf);
  31493. }
  31494. #endif
  31495. return EXPECT_RESULT();
  31496. }
  31497. static int test_wc_PemPubKeyToDer(void)
  31498. {
  31499. EXPECT_DECLS;
  31500. #if !defined(NO_FILESYSTEM) && \
  31501. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  31502. const char* key = "./certs/ecc-client-keyPub.pem";
  31503. size_t cert_dersz = 1024;
  31504. byte* cert_der = NULL;
  31505. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  31506. BAD_FUNC_ARG);
  31507. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31508. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  31509. if (cert_der != NULL) {
  31510. free(cert_der);
  31511. }
  31512. #endif
  31513. return EXPECT_RESULT();
  31514. }
  31515. static int test_wc_GetPubKeyDerFromCert(void)
  31516. {
  31517. EXPECT_DECLS;
  31518. #if !defined(NO_RSA) || defined(HAVE_ECC)
  31519. int ret;
  31520. word32 idx = 0;
  31521. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  31522. word32 keyDerSz = (word32)sizeof(keyDer);
  31523. DecodedCert decoded;
  31524. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  31525. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  31526. word32 certBufSz = sizeof(certBuf);
  31527. #endif
  31528. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  31529. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  31530. XFILE fp = XBADFILE;
  31531. #endif
  31532. #ifndef NO_RSA
  31533. RsaKey rsaKey;
  31534. #if defined(USE_CERT_BUFFERS_2048)
  31535. byte* rsaCertDer = (byte*)client_cert_der_2048;
  31536. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  31537. #elif defined(USE_CERT_BUFFERS_1024)
  31538. byte* rsaCertDer = (byte*)client_cert_der_1024;
  31539. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  31540. #else
  31541. unsigned char rsaCertDer[TWOK_BUF];
  31542. word32 rsaCertDerSz;
  31543. #endif
  31544. #endif
  31545. #ifdef HAVE_ECC
  31546. ecc_key eccKey;
  31547. #if defined(USE_CERT_BUFFERS_256)
  31548. byte* eccCert = (byte*)cliecc_cert_der_256;
  31549. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  31550. #else
  31551. unsigned char eccCert[ONEK_BUF];
  31552. word32 eccCertSz;
  31553. XFILE fp2 = XBADFILE;
  31554. #endif
  31555. #endif
  31556. #ifndef NO_RSA
  31557. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  31558. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  31559. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  31560. fp), 0);
  31561. if (fp != XBADFILE) {
  31562. XFCLOSE(fp);
  31563. fp = XBADFILE;
  31564. }
  31565. #endif
  31566. /* good test case - RSA DER cert */
  31567. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  31568. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  31569. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  31570. ExpectIntGT(keyDerSz, 0);
  31571. /* sanity check, verify we can import DER public key */
  31572. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  31573. ExpectIntEQ(ret, 0);
  31574. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  31575. if (ret == 0) {
  31576. wc_FreeRsaKey(&rsaKey);
  31577. }
  31578. /* test LENGTH_ONLY_E case */
  31579. keyDerSz = 0;
  31580. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  31581. LENGTH_ONLY_E);
  31582. ExpectIntGT(keyDerSz, 0);
  31583. /* bad args: DecodedCert NULL */
  31584. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  31585. /* bad args: output key buff size */
  31586. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  31587. /* bad args: zero size output key buffer */
  31588. keyDerSz = 0;
  31589. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  31590. BAD_FUNC_ARG);
  31591. wc_FreeDecodedCert(&decoded);
  31592. /* Certificate Request Tests */
  31593. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  31594. {
  31595. XMEMSET(certBuf, 0, sizeof(certBuf));
  31596. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  31597. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  31598. if (fp != XBADFILE) {
  31599. XFCLOSE(fp);
  31600. }
  31601. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  31602. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  31603. /* good test case - RSA DER certificate request */
  31604. keyDerSz = sizeof(keyDer);
  31605. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  31606. 0);
  31607. ExpectIntGT(keyDerSz, 0);
  31608. /* sanity check, verify we can import DER public key */
  31609. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  31610. ExpectIntEQ(ret, 0);
  31611. idx = 0;
  31612. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  31613. if (ret == 0) {
  31614. wc_FreeRsaKey(&rsaKey);
  31615. }
  31616. wc_FreeDecodedCert(&decoded);
  31617. }
  31618. #endif /* WOLFSSL_CERT_REQ */
  31619. #endif /* NO_RSA */
  31620. #ifdef HAVE_ECC
  31621. #ifndef USE_CERT_BUFFERS_256
  31622. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  31623. XBADFILE);
  31624. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  31625. if (fp2 != XBADFILE) {
  31626. XFCLOSE(fp2);
  31627. }
  31628. #endif
  31629. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  31630. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  31631. /* good test case - ECC */
  31632. XMEMSET(keyDer, 0, sizeof(keyDer));
  31633. keyDerSz = sizeof(keyDer);
  31634. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  31635. ExpectIntGT(keyDerSz, 0);
  31636. /* sanity check, verify we can import DER public key */
  31637. ret = wc_ecc_init(&eccKey);
  31638. ExpectIntEQ(ret, 0);
  31639. idx = 0; /* reset idx to 0, used above in RSA case */
  31640. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  31641. if (ret == 0) {
  31642. wc_ecc_free(&eccKey);
  31643. }
  31644. /* test LENGTH_ONLY_E case */
  31645. keyDerSz = 0;
  31646. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  31647. LENGTH_ONLY_E);
  31648. ExpectIntGT(keyDerSz, 0);
  31649. wc_FreeDecodedCert(&decoded);
  31650. #endif
  31651. #endif /* !NO_RSA || HAVE_ECC */
  31652. return EXPECT_RESULT();
  31653. }
  31654. static int test_wc_CheckCertSigPubKey(void)
  31655. {
  31656. EXPECT_DECLS;
  31657. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31658. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  31659. int ret = 0;
  31660. const char* ca_cert = "./certs/ca-cert.pem";
  31661. byte* cert_buf = NULL;
  31662. size_t cert_sz = 0;
  31663. byte* cert_der = NULL;
  31664. word32 cert_dersz = 0;
  31665. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  31666. word32 keyDerSz = (word32)sizeof(keyDer);
  31667. DecodedCert decoded;
  31668. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  31669. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  31670. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31671. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  31672. (int)cert_dersz, CERT_TYPE), 0);
  31673. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  31674. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  31675. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  31676. ExpectIntGT(keyDerSz, 0);
  31677. /* Good test case. */
  31678. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  31679. keyDerSz, RSAk), 0);
  31680. /* No certificate. */
  31681. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  31682. ECDSAk), BAD_FUNC_ARG);
  31683. /* Bad cert size. */
  31684. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  31685. RSAk), 0);
  31686. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  31687. /* No public key. */
  31688. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  31689. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  31690. /* Bad public key size. */
  31691. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  31692. RSAk), BAD_FUNC_ARG);
  31693. /* Wrong aglo. */
  31694. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  31695. keyDerSz, ECDSAk), ASN_PARSE_E);
  31696. wc_FreeDecodedCert(&decoded);
  31697. if (cert_der != NULL)
  31698. free(cert_der);
  31699. if (cert_buf != NULL)
  31700. free(cert_buf);
  31701. #endif
  31702. return EXPECT_RESULT();
  31703. }
  31704. static int test_wolfSSL_certs(void)
  31705. {
  31706. EXPECT_DECLS;
  31707. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31708. !defined(NO_RSA)
  31709. X509* x509ext = NULL;
  31710. #ifdef OPENSSL_ALL
  31711. X509* x509 = NULL;
  31712. WOLFSSL_X509_EXTENSION* ext = NULL;
  31713. ASN1_OBJECT* obj = NULL;
  31714. #endif
  31715. WOLFSSL* ssl = NULL;
  31716. WOLFSSL_CTX* ctx = NULL;
  31717. STACK_OF(ASN1_OBJECT)* sk = NULL;
  31718. ASN1_STRING* asn1_str = NULL;
  31719. AUTHORITY_KEYID* akey = NULL;
  31720. BASIC_CONSTRAINTS* bc = NULL;
  31721. int crit = 0;
  31722. #ifndef NO_WOLFSSL_SERVER
  31723. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31724. #else
  31725. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31726. #endif
  31727. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  31728. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31729. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  31730. #if !defined(NO_CHECK_PRIVATE_KEY)
  31731. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  31732. #endif
  31733. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31734. #if !defined(NO_CHECK_PRIVATE_KEY)
  31735. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  31736. #endif
  31737. ExpectNotNull(ssl = SSL_new(ctx));
  31738. #if !defined(NO_CHECK_PRIVATE_KEY)
  31739. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31740. #endif
  31741. #ifdef HAVE_PK_CALLBACKS
  31742. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  31743. #endif /* HAVE_PK_CALLBACKS */
  31744. /* create and use x509 */
  31745. #ifdef OPENSSL_ALL
  31746. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31747. WOLFSSL_FILETYPE_PEM));
  31748. #endif
  31749. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  31750. WOLFSSL_FILETYPE_PEM));
  31751. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  31752. #if !defined(NO_CHECK_PRIVATE_KEY)
  31753. /* with loading in a new cert the check on private key should now fail */
  31754. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31755. #endif
  31756. #if defined(USE_CERT_BUFFERS_2048)
  31757. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  31758. (unsigned char*)server_cert_der_2048,
  31759. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  31760. #endif
  31761. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31762. /************* Get Digest of Certificate ******************/
  31763. {
  31764. byte digest[64]; /* max digest size */
  31765. word32 digestSz;
  31766. XMEMSET(digest, 0, sizeof(digest));
  31767. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  31768. WOLFSSL_SUCCESS);
  31769. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  31770. WOLFSSL_SUCCESS);
  31771. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  31772. WOLFSSL_FAILURE);
  31773. }
  31774. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  31775. /* test and checkout X509 extensions */
  31776. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  31777. NID_basic_constraints, &crit, NULL));
  31778. ExpectIntEQ(crit, 0);
  31779. #ifdef OPENSSL_ALL
  31780. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  31781. X509_EXTENSION_free(ext);
  31782. ext = NULL;
  31783. ExpectNotNull(ext = X509_EXTENSION_new());
  31784. X509_EXTENSION_set_critical(ext, 1);
  31785. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  31786. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  31787. ASN1_OBJECT_free(obj);
  31788. obj = NULL;
  31789. X509_EXTENSION_free(ext);
  31790. ext = NULL;
  31791. ExpectNotNull(ext = X509_EXTENSION_new());
  31792. X509_EXTENSION_set_critical(ext, 0);
  31793. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  31794. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  31795. NULL);
  31796. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  31797. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  31798. * and X509_get_ext_d2i has created new */
  31799. asn1_str = NULL;
  31800. X509_EXTENSION_free(ext);
  31801. ext = NULL;
  31802. #endif
  31803. BASIC_CONSTRAINTS_free(bc);
  31804. bc = NULL;
  31805. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  31806. NID_key_usage, &crit, NULL));
  31807. ExpectIntEQ(crit, 1);
  31808. ExpectIntEQ(asn1_str->type, NID_key_usage);
  31809. #ifdef OPENSSL_ALL
  31810. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  31811. X509_EXTENSION_free(ext);
  31812. ext = NULL;
  31813. #endif
  31814. ASN1_STRING_free(asn1_str);
  31815. asn1_str = NULL;
  31816. #ifdef OPENSSL_ALL
  31817. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  31818. NID_ext_key_usage, &crit, NULL));
  31819. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  31820. X509_EXTENSION_free(ext);
  31821. ext = NULL;
  31822. EXTENDED_KEY_USAGE_free(sk);
  31823. sk = NULL;
  31824. #else
  31825. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  31826. &crit, NULL);
  31827. ExpectNull(sk);
  31828. #endif
  31829. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  31830. NID_authority_key_identifier, &crit, NULL));
  31831. #ifdef OPENSSL_ALL
  31832. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  31833. akey));
  31834. X509_EXTENSION_free(ext);
  31835. ext = NULL;
  31836. #endif
  31837. wolfSSL_AUTHORITY_KEYID_free(akey);
  31838. akey = NULL;
  31839. /* NID not yet supported */
  31840. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31841. NID_private_key_usage_period, &crit, NULL));
  31842. ExpectIntEQ(crit, -1);
  31843. sk_ASN1_OBJECT_free(sk);
  31844. sk = NULL;
  31845. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  31846. NID_subject_alt_name, &crit, NULL));
  31847. {
  31848. int i;
  31849. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  31850. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  31851. ExpectIntEQ(gen->type, GEN_DNS);
  31852. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  31853. }
  31854. }
  31855. sk_GENERAL_NAME_free(sk);
  31856. sk = NULL;
  31857. /* NID not yet supported */
  31858. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31859. NID_issuer_alt_name, &crit, NULL));
  31860. ExpectIntEQ(crit, -1);
  31861. sk_ASN1_OBJECT_free(sk);
  31862. sk = NULL;
  31863. /* NID not yet supported */
  31864. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31865. NID_info_access, &crit, NULL));
  31866. sk_ASN1_OBJECT_free(sk);
  31867. sk = NULL;
  31868. /* NID not yet supported */
  31869. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31870. NID_sinfo_access, &crit, NULL));
  31871. ExpectIntEQ(crit, -1);
  31872. sk_ASN1_OBJECT_free(sk);
  31873. sk = NULL;
  31874. /* NID not yet supported */
  31875. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31876. NID_name_constraints, &crit, NULL));
  31877. ExpectIntEQ(crit, -1);
  31878. sk_ASN1_OBJECT_free(sk);
  31879. sk = NULL;
  31880. /* no cert policy set */
  31881. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31882. NID_certificate_policies, &crit, NULL));
  31883. sk_ASN1_OBJECT_free(sk);
  31884. sk = NULL;
  31885. /* NID not yet supported */
  31886. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31887. NID_policy_mappings, &crit, NULL));
  31888. ExpectIntEQ(crit, -1);
  31889. sk_ASN1_OBJECT_free(sk);
  31890. sk = NULL;
  31891. /* NID not yet supported */
  31892. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31893. NID_policy_constraints, &crit, NULL));
  31894. ExpectIntEQ(crit, -1);
  31895. sk_ASN1_OBJECT_free(sk);
  31896. sk = NULL;
  31897. /* NID not yet supported */
  31898. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31899. NID_inhibit_any_policy, &crit, NULL));
  31900. ExpectIntEQ(crit, -1);
  31901. sk_ASN1_OBJECT_free(sk);
  31902. sk = NULL;
  31903. /* NID not yet supported */
  31904. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31905. NID_tlsfeature, &crit, NULL));
  31906. ExpectIntEQ(crit, -1);
  31907. sk_ASN1_OBJECT_free(sk);
  31908. sk = NULL;
  31909. /* test invalid cases */
  31910. crit = 0;
  31911. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  31912. NULL));
  31913. ExpectIntEQ(crit, -1);
  31914. /* NULL passed for criticality. */
  31915. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  31916. NID_tlsfeature, NULL, NULL));
  31917. ExpectIntEQ(SSL_get_hit(ssl), 0);
  31918. #ifdef OPENSSL_ALL
  31919. X509_free(x509);
  31920. #endif
  31921. X509_free(x509ext);
  31922. SSL_free(ssl);
  31923. SSL_CTX_free(ctx);
  31924. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  31925. return EXPECT_RESULT();
  31926. }
  31927. static int test_wolfSSL_X509_check_private_key(void)
  31928. {
  31929. EXPECT_DECLS;
  31930. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  31931. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  31932. X509* x509 = NULL;
  31933. EVP_PKEY* pkey = NULL;
  31934. const byte* key;
  31935. /* Check with correct key */
  31936. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  31937. SSL_FILETYPE_PEM)));
  31938. key = client_key_der_2048;
  31939. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  31940. (long)sizeof_client_key_der_2048));
  31941. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  31942. EVP_PKEY_free(pkey);
  31943. pkey = NULL;
  31944. /* Check with wrong key */
  31945. key = server_key_der_2048;
  31946. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  31947. (long)sizeof_server_key_der_2048));
  31948. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  31949. /* test for incorrect parameter */
  31950. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  31951. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  31952. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  31953. EVP_PKEY_free(pkey);
  31954. X509_free(x509);
  31955. #endif
  31956. return EXPECT_RESULT();
  31957. }
  31958. static int test_wolfSSL_private_keys(void)
  31959. {
  31960. EXPECT_DECLS;
  31961. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31962. !defined(NO_FILESYSTEM)
  31963. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31964. WOLFSSL* ssl = NULL;
  31965. WOLFSSL_CTX* ctx = NULL;
  31966. EVP_PKEY* pkey = NULL;
  31967. OpenSSL_add_all_digests();
  31968. OpenSSL_add_all_algorithms();
  31969. #ifndef NO_RSA
  31970. #ifndef NO_WOLFSSL_SERVER
  31971. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31972. #else
  31973. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31974. #endif
  31975. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  31976. /* Have to load a cert before you can check the private key against that
  31977. * certificates public key! */
  31978. #if !defined(NO_CHECK_PRIVATE_KEY)
  31979. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  31980. #endif
  31981. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  31982. #if !defined(NO_CHECK_PRIVATE_KEY)
  31983. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  31984. #endif
  31985. ExpectNotNull(ssl = SSL_new(ctx));
  31986. #if !defined(NO_CHECK_PRIVATE_KEY)
  31987. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31988. #endif
  31989. #ifdef USE_CERT_BUFFERS_2048
  31990. {
  31991. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  31992. unsigned char buf[FOURK_BUF];
  31993. word32 bufSz;
  31994. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  31995. (unsigned char*)client_key_der_2048,
  31996. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  31997. #if !defined(NO_CHECK_PRIVATE_KEY)
  31998. /* Should mismatch now that a different private key loaded */
  31999. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32000. #endif
  32001. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  32002. (unsigned char*)server_key,
  32003. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  32004. #if !defined(NO_CHECK_PRIVATE_KEY)
  32005. /* After loading back in DER format of original key, should match */
  32006. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32007. #endif
  32008. /* test loading private key to the WOLFSSL_CTX */
  32009. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  32010. (unsigned char*)client_key_der_2048,
  32011. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  32012. #if !defined(NO_CHECK_PRIVATE_KEY)
  32013. /* Should mismatch now that a different private key loaded */
  32014. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  32015. #endif
  32016. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  32017. (unsigned char*)server_key,
  32018. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  32019. #if !defined(NO_CHECK_PRIVATE_KEY)
  32020. /* After loading back in DER format of original key, should match */
  32021. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  32022. #endif
  32023. /* pkey not set yet, expecting to fail */
  32024. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  32025. /* set PKEY and test again */
  32026. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  32027. &server_key, (long)sizeof_server_key_der_2048));
  32028. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  32029. /* reuse PKEY structure and test
  32030. * this should be checked with a memory management sanity checker */
  32031. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  32032. server_key = (const unsigned char*)server_key_der_2048;
  32033. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  32034. &server_key, (long)sizeof_server_key_der_2048));
  32035. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  32036. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  32037. bufSz = FOURK_BUF;
  32038. ExpectIntGT((bufSz = (word32)wc_CreatePKCS8Key(buf, &bufSz,
  32039. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  32040. RSAk, NULL, 0)), 0);
  32041. server_key = (const unsigned char*)buf;
  32042. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  32043. (long)bufSz));
  32044. }
  32045. #endif
  32046. EVP_PKEY_free(pkey);
  32047. pkey = NULL;
  32048. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  32049. ssl = NULL;
  32050. SSL_CTX_free(ctx);
  32051. ctx = NULL;
  32052. #endif /* end of RSA private key match tests */
  32053. #ifdef HAVE_ECC
  32054. #ifndef NO_WOLFSSL_SERVER
  32055. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32056. #else
  32057. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32058. #endif
  32059. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  32060. WOLFSSL_FILETYPE_PEM));
  32061. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  32062. WOLFSSL_FILETYPE_PEM));
  32063. ExpectNotNull(ssl = SSL_new(ctx));
  32064. #if !defined(NO_CHECK_PRIVATE_KEY)
  32065. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32066. #endif
  32067. SSL_free(ssl);
  32068. ssl = NULL;
  32069. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  32070. WOLFSSL_FILETYPE_PEM));
  32071. ExpectNotNull(ssl = SSL_new(ctx));
  32072. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  32073. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32074. #endif
  32075. SSL_free(ssl);
  32076. ssl = NULL;
  32077. SSL_CTX_free(ctx);
  32078. ctx = NULL;
  32079. #endif /* end of ECC private key match tests */
  32080. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  32081. #ifndef NO_WOLFSSL_SERVER
  32082. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32083. #else
  32084. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32085. #endif
  32086. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  32087. WOLFSSL_FILETYPE_PEM));
  32088. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  32089. WOLFSSL_FILETYPE_PEM));
  32090. ExpectNotNull(ssl = SSL_new(ctx));
  32091. #if !defined(NO_CHECK_PRIVATE_KEY)
  32092. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32093. #endif
  32094. SSL_free(ssl);
  32095. ssl = NULL;
  32096. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  32097. WOLFSSL_FILETYPE_PEM));
  32098. ExpectNotNull(ssl = SSL_new(ctx));
  32099. #if !defined(NO_CHECK_PRIVATE_KEY)
  32100. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32101. #endif
  32102. SSL_free(ssl);
  32103. ssl = NULL;
  32104. SSL_CTX_free(ctx);
  32105. ctx = NULL;
  32106. #endif /* end of Ed25519 private key match tests */
  32107. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  32108. #ifndef NO_WOLFSSL_SERVER
  32109. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32110. #else
  32111. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32112. #endif
  32113. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  32114. WOLFSSL_FILETYPE_PEM));
  32115. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  32116. WOLFSSL_FILETYPE_PEM));
  32117. ExpectNotNull(ssl = SSL_new(ctx));
  32118. #if !defined(NO_CHECK_PRIVATE_KEY)
  32119. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32120. #endif
  32121. SSL_free(ssl);
  32122. ssl = NULL;
  32123. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  32124. WOLFSSL_FILETYPE_PEM));
  32125. ExpectNotNull(ssl = SSL_new(ctx));
  32126. #if !defined(NO_CHECK_PRIVATE_KEY)
  32127. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32128. #endif
  32129. SSL_free(ssl);
  32130. ssl = NULL;
  32131. SSL_CTX_free(ctx);
  32132. ctx = NULL;
  32133. #endif /* end of Ed448 private key match tests */
  32134. EVP_cleanup();
  32135. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  32136. CONF_modules_free();
  32137. ENGINE_cleanup();
  32138. CONF_modules_unload();
  32139. (void)ssl;
  32140. (void)ctx;
  32141. (void)pkey;
  32142. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32143. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32144. return EXPECT_RESULT();
  32145. }
  32146. static int test_wolfSSL_PEM_def_callback(void)
  32147. {
  32148. EXPECT_DECLS;
  32149. #ifdef OPENSSL_EXTRA
  32150. char buf[10];
  32151. const char* defpwd = "DEF PWD";
  32152. int defpwdLen = (int)XSTRLEN(defpwd);
  32153. int smallLen = 1;
  32154. /* Bad parameters. */
  32155. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, NULL), 0);
  32156. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, (void*)defpwd),
  32157. 0);
  32158. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, NULL), 0);
  32159. XMEMSET(buf, 0, sizeof(buf));
  32160. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, (void*)defpwd),
  32161. defpwdLen);
  32162. ExpectIntEQ(XMEMCMP(buf, defpwd, defpwdLen), 0);
  32163. ExpectIntEQ(buf[defpwdLen], 0);
  32164. /* Size of buffer is smaller than default password. */
  32165. XMEMSET(buf, 0, sizeof(buf));
  32166. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, smallLen, 0, (void*)defpwd),
  32167. smallLen);
  32168. ExpectIntEQ(XMEMCMP(buf, defpwd, smallLen), 0);
  32169. ExpectIntEQ(buf[smallLen], 0);
  32170. #endif /* OPENSSL_EXTRA */
  32171. return EXPECT_RESULT();
  32172. }
  32173. static int test_wolfSSL_PEM_read_PrivateKey(void)
  32174. {
  32175. EXPECT_DECLS;
  32176. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || \
  32177. !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH))
  32178. XFILE file = XBADFILE;
  32179. #if !defined(NO_RSA)
  32180. const char* fname_rsa = "./certs/server-key.pem";
  32181. RSA* rsa = NULL;
  32182. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  32183. unsigned char* sig = NULL;
  32184. size_t sigLen = 0;
  32185. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  32186. size_t tbsLen = sizeof(tbs);
  32187. #endif
  32188. #if !defined(NO_DSA)
  32189. const char* fname_dsa = "./certs/dsa2048.pem";
  32190. #endif
  32191. #if defined(HAVE_ECC)
  32192. const char* fname_ec = "./certs/ecc-key.pem";
  32193. #endif
  32194. #if !defined(NO_DH)
  32195. const char* fname_dh = "./certs/dh-priv-2048.pem";
  32196. #endif
  32197. EVP_PKEY* pkey = NULL;
  32198. /* Check error case. */
  32199. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  32200. /* not a PEM key. */
  32201. ExpectTrue((file = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  32202. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32203. if (file != XBADFILE)
  32204. XFCLOSE(file);
  32205. file = XBADFILE;
  32206. #ifndef NO_RSA
  32207. /* Read in an RSA key. */
  32208. ExpectTrue((file = XFOPEN(fname_rsa, "rb")) != XBADFILE);
  32209. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32210. if (file != XBADFILE)
  32211. XFCLOSE(file);
  32212. file = XBADFILE;
  32213. /* Make sure the key is usable by signing some data with it. */
  32214. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  32215. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  32216. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  32217. DYNAMIC_TYPE_TMP_BUFFER));
  32218. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  32219. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  32220. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  32221. WOLFSSL_SUCCESS);
  32222. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32223. EVP_PKEY_CTX_free(ctx);
  32224. EVP_PKEY_free(pkey);
  32225. pkey = NULL;
  32226. #endif
  32227. #ifndef NO_DSA
  32228. /* Read in a DSA key. */
  32229. ExpectTrue((file = XFOPEN(fname_dsa, "rb")) != XBADFILE);
  32230. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  32231. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32232. EVP_PKEY_free(pkey);
  32233. pkey = NULL;
  32234. #else
  32235. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32236. #endif
  32237. if (file != XBADFILE)
  32238. XFCLOSE(file);
  32239. file = XBADFILE;
  32240. #endif
  32241. #ifdef HAVE_ECC
  32242. /* Read in an EC key. */
  32243. ExpectTrue((file = XFOPEN(fname_ec, "rb")) != XBADFILE);
  32244. ExpectNotNull(pkey = EVP_PKEY_new());
  32245. ExpectPtrEq(PEM_read_PrivateKey(file, &pkey, NULL, NULL), pkey);
  32246. if (file != XBADFILE)
  32247. XFCLOSE(file);
  32248. file = XBADFILE;
  32249. EVP_PKEY_free(pkey);
  32250. pkey = NULL;
  32251. #endif
  32252. #ifndef NO_DH
  32253. /* Read in a DH key. */
  32254. ExpectTrue((file = XFOPEN(fname_dh, "rb")) != XBADFILE);
  32255. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  32256. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  32257. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  32258. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32259. EVP_PKEY_free(pkey);
  32260. pkey = NULL;
  32261. #else
  32262. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32263. #endif
  32264. if (file != XBADFILE)
  32265. XFCLOSE(file);
  32266. file = XBADFILE;
  32267. #endif
  32268. #endif
  32269. return EXPECT_RESULT();
  32270. }
  32271. static int test_wolfSSL_PEM_read_PUBKEY(void)
  32272. {
  32273. EXPECT_DECLS;
  32274. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  32275. && !defined(NO_FILESYSTEM)
  32276. XFILE file = XBADFILE;
  32277. const char* fname = "./certs/client-keyPub.pem";
  32278. EVP_PKEY* pkey = NULL;
  32279. /* Check error case. */
  32280. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  32281. /* Read in an RSA key. */
  32282. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32283. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  32284. EVP_PKEY_free(pkey);
  32285. pkey = NULL;
  32286. if (file != XBADFILE)
  32287. XFCLOSE(file);
  32288. file = XBADFILE;
  32289. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32290. ExpectNotNull(pkey = EVP_PKEY_new());
  32291. ExpectPtrEq(PEM_read_PUBKEY(file, &pkey, NULL, NULL), pkey);
  32292. EVP_PKEY_free(pkey);
  32293. if (file != XBADFILE)
  32294. XFCLOSE(file);
  32295. #endif
  32296. return EXPECT_RESULT();
  32297. }
  32298. /* test loading RSA key using BIO */
  32299. static int test_wolfSSL_PEM_PrivateKey_rsa(void)
  32300. {
  32301. EXPECT_DECLS;
  32302. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  32303. defined(USE_CERT_BUFFERS_2048) && !defined(NO_FILESYSTEM) && \
  32304. !defined(NO_BIO)
  32305. BIO* bio = NULL;
  32306. XFILE file = XBADFILE;
  32307. const char* fname = "./certs/server-key.pem";
  32308. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  32309. EVP_PKEY* pkey = NULL;
  32310. size_t sz = 0;
  32311. byte* buf = NULL;
  32312. EVP_PKEY* pkey2 = NULL;
  32313. EVP_PKEY* pkey3 = NULL;
  32314. RSA* rsa_key = NULL;
  32315. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  32316. unsigned char extra[10];
  32317. int i;
  32318. BIO* pub_bio = NULL;
  32319. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  32320. #endif
  32321. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32322. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  32323. ExpectIntGT(sz = XFTELL(file), 0);
  32324. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  32325. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  32326. if (buf != NULL) {
  32327. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  32328. }
  32329. if (file != XBADFILE) {
  32330. XFCLOSE(file);
  32331. file = XBADFILE;
  32332. }
  32333. /* Test using BIO new mem and loading PEM private key */
  32334. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  32335. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  32336. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  32337. buf = NULL;
  32338. BIO_free(bio);
  32339. bio = NULL;
  32340. /* New empty EVP_PKEY */
  32341. ExpectNotNull(pkey2 = EVP_PKEY_new());
  32342. if (pkey2 != NULL) {
  32343. pkey2->type = EVP_PKEY_RSA;
  32344. }
  32345. /* Test parameter copy */
  32346. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  32347. EVP_PKEY_free(pkey2);
  32348. EVP_PKEY_free(pkey);
  32349. pkey = NULL;
  32350. /* Qt unit test case : rsa pkcs8 key */
  32351. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  32352. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  32353. ExpectIntGT(sz = XFTELL(file), 0);
  32354. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  32355. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  32356. if (buf) {
  32357. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  32358. }
  32359. if (file != XBADFILE) {
  32360. XFCLOSE(file);
  32361. file = XBADFILE;
  32362. }
  32363. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  32364. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  32365. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  32366. buf = NULL;
  32367. BIO_free(bio);
  32368. bio = NULL;
  32369. ExpectNotNull(pkey3 = EVP_PKEY_new());
  32370. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  32371. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  32372. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32373. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  32374. #else
  32375. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  32376. #endif
  32377. RSA_free(rsa_key);
  32378. EVP_PKEY_free(pkey3);
  32379. EVP_PKEY_free(pkey);
  32380. pkey = NULL;
  32381. pkey2 = NULL;
  32382. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  32383. #define BIO_PEM_TEST_CHAR 'a'
  32384. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  32385. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32386. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  32387. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32388. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  32389. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key,
  32390. (long)sizeof_server_key_der_2048));
  32391. ExpectNull(pkey);
  32392. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  32393. (long)sizeof_server_key_der_2048));
  32394. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL, NULL),
  32395. WOLFSSL_FAILURE);
  32396. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL, NULL),
  32397. WOLFSSL_FAILURE);
  32398. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32399. WOLFSSL_SUCCESS);
  32400. ExpectIntGT(BIO_pending(bio), 0);
  32401. ExpectIntEQ(BIO_pending(bio), 1679);
  32402. /* Check if the pubkey API writes only the public key */
  32403. #ifdef WOLFSSL_KEY_GEN
  32404. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  32405. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  32406. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  32407. ExpectIntGT(BIO_pending(pub_bio), 0);
  32408. /* Previously both the private key and the pubkey calls would write
  32409. * out the private key and the PEM header was the only difference.
  32410. * The public PEM should be significantly shorter than the
  32411. * private key versison. */
  32412. ExpectIntEQ(BIO_pending(pub_bio), 451);
  32413. #else
  32414. /* Not supported. */
  32415. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), 0);
  32416. #endif
  32417. /* test creating new EVP_PKEY with good args */
  32418. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  32419. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  32420. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  32421. pkey->pkey_sz), 0);
  32422. }
  32423. /* test of reuse of EVP_PKEY */
  32424. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  32425. ExpectIntEQ(BIO_pending(bio), 0);
  32426. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32427. SSL_SUCCESS);
  32428. /* add 10 extra bytes after PEM */
  32429. ExpectIntEQ(BIO_write(bio, extra, 10), 10);
  32430. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  32431. ExpectNotNull(pkey);
  32432. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  32433. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  32434. pkey->pkey_sz), 0);
  32435. }
  32436. /* check 10 extra bytes still there */
  32437. ExpectIntEQ(BIO_pending(bio), 10);
  32438. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  32439. for (i = 0; i < 10; i++) {
  32440. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  32441. }
  32442. BIO_free(pub_bio);
  32443. BIO_free(bio);
  32444. bio = NULL;
  32445. EVP_PKEY_free(pkey);
  32446. pkey = NULL;
  32447. EVP_PKEY_free(pkey2);
  32448. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  32449. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 &&
  32450. * !NO_FILESYSTEM && !NO_BIO */
  32451. return EXPECT_RESULT();
  32452. }
  32453. /* test loading ECC key using BIO */
  32454. static int test_wolfSSL_PEM_PrivateKey_ecc(void)
  32455. {
  32456. EXPECT_DECLS;
  32457. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_ECC) && \
  32458. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  32459. BIO* bio = NULL;
  32460. EVP_PKEY* pkey = NULL;
  32461. XFILE file = XBADFILE;
  32462. const char* fname = "./certs/ecc-key.pem";
  32463. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  32464. size_t sz = 0;
  32465. byte* buf = NULL;
  32466. EVP_PKEY* pkey2 = NULL;
  32467. EVP_PKEY* pkey3 = NULL;
  32468. EC_KEY* ec_key = NULL;
  32469. int nid = 0;
  32470. BIO* pub_bio = NULL;
  32471. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32472. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  32473. ExpectIntGT(sz = XFTELL(file), 0);
  32474. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  32475. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  32476. if (buf) {
  32477. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  32478. }
  32479. if (file != XBADFILE) {
  32480. XFCLOSE(file);
  32481. file = XBADFILE;
  32482. }
  32483. /* Test using BIO new mem and loading PEM private key */
  32484. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  32485. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  32486. BIO_free(bio);
  32487. bio = NULL;
  32488. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  32489. buf = NULL;
  32490. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32491. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32492. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32493. WOLFSSL_SUCCESS);
  32494. ExpectIntGT(BIO_pending(bio), 0);
  32495. /* No parmeters. */
  32496. ExpectIntEQ(BIO_pending(bio), 227);
  32497. /* Check if the pubkey API writes only the public key */
  32498. #ifdef WOLFSSL_KEY_GEN
  32499. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  32500. ExpectIntGT(BIO_pending(pub_bio), 0);
  32501. /* Previously both the private key and the pubkey calls would write
  32502. * out the private key and the PEM header was the only difference.
  32503. * The public PEM should be significantly shorter than the
  32504. * private key versison. */
  32505. ExpectIntEQ(BIO_pending(pub_bio), 178);
  32506. #endif
  32507. BIO_free(pub_bio);
  32508. BIO_free(bio);
  32509. bio = NULL;
  32510. ExpectNotNull(pkey2 = EVP_PKEY_new());
  32511. ExpectNotNull(pkey3 = EVP_PKEY_new());
  32512. if (pkey2 != NULL) {
  32513. pkey2->type = EVP_PKEY_EC;
  32514. }
  32515. /* Test parameter copy */
  32516. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  32517. /* Qt unit test case 1*/
  32518. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  32519. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  32520. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32521. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  32522. #else
  32523. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  32524. #endif
  32525. /* Test default digest */
  32526. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  32527. ExpectIntEQ(nid, NID_sha256);
  32528. EC_KEY_free(ec_key);
  32529. ec_key = NULL;
  32530. EVP_PKEY_free(pkey3);
  32531. pkey3 = NULL;
  32532. EVP_PKEY_free(pkey2);
  32533. pkey2 = NULL;
  32534. EVP_PKEY_free(pkey);
  32535. pkey = NULL;
  32536. /* Qt unit test case ec pkcs8 key */
  32537. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  32538. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  32539. ExpectIntGT(sz = XFTELL(file), 0);
  32540. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  32541. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  32542. if (buf) {
  32543. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  32544. }
  32545. if (file != XBADFILE) {
  32546. XFCLOSE(file);
  32547. file = XBADFILE;
  32548. }
  32549. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  32550. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  32551. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  32552. buf = NULL;
  32553. BIO_free(bio);
  32554. bio = NULL;
  32555. ExpectNotNull(pkey3 = EVP_PKEY_new());
  32556. /* Qt unit test case */
  32557. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  32558. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  32559. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32560. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  32561. #else
  32562. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  32563. #endif
  32564. EC_KEY_free(ec_key);
  32565. EVP_PKEY_free(pkey3);
  32566. EVP_PKEY_free(pkey);
  32567. pkey = NULL;
  32568. #endif
  32569. return EXPECT_RESULT();
  32570. }
  32571. /* test loading DSA key using BIO */
  32572. static int test_wolfSSL_PEM_PrivateKey_dsa(void)
  32573. {
  32574. EXPECT_DECLS;
  32575. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DSA) && \
  32576. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  32577. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  32578. BIO* bio = NULL;
  32579. EVP_PKEY* pkey = NULL;
  32580. ExpectNotNull(bio = BIO_new_file("./certs/dsa2048.pem", "rb"));
  32581. /* Private DSA EVP_PKEY */
  32582. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  32583. NULL));
  32584. BIO_free(bio);
  32585. bio = NULL;
  32586. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32587. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32588. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32589. NULL), 0);
  32590. #endif
  32591. #ifdef WOLFSSL_KEY_GEN
  32592. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 1);
  32593. ExpectIntEQ(BIO_pending(bio), 1178);
  32594. BIO_reset(bio);
  32595. #endif
  32596. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32597. 1);
  32598. ExpectIntEQ(BIO_pending(bio), 1196);
  32599. BIO_free(bio);
  32600. bio = NULL;
  32601. EVP_PKEY_free(pkey);
  32602. pkey = NULL;
  32603. #endif
  32604. #endif
  32605. return EXPECT_RESULT();
  32606. }
  32607. /* test loading DH key using BIO */
  32608. static int test_wolfSSL_PEM_PrivateKey_dh(void)
  32609. {
  32610. EXPECT_DECLS;
  32611. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DH) && \
  32612. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  32613. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  32614. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  32615. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  32616. BIO* bio = NULL;
  32617. EVP_PKEY* pkey = NULL;
  32618. ExpectNotNull(bio = BIO_new_file("./certs/dh-priv-2048.pem", "rb"));
  32619. /* Private DH EVP_PKEY */
  32620. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  32621. NULL));
  32622. BIO_free(bio);
  32623. bio = NULL;
  32624. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32625. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32626. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32627. NULL), 0);
  32628. #endif
  32629. #ifdef WOLFSSL_KEY_GEN
  32630. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 0);
  32631. #endif
  32632. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32633. 1);
  32634. ExpectIntEQ(BIO_pending(bio), 806);
  32635. BIO_free(bio);
  32636. bio = NULL;
  32637. EVP_PKEY_free(pkey);
  32638. pkey = NULL;
  32639. #endif
  32640. #endif
  32641. return EXPECT_RESULT();
  32642. }
  32643. static int test_wolfSSL_PEM_PrivateKey(void)
  32644. {
  32645. EXPECT_DECLS;
  32646. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32647. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  32648. #ifndef NO_BIO
  32649. BIO* bio = NULL;
  32650. #endif
  32651. EVP_PKEY* pkey = NULL;
  32652. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  32653. #ifndef NO_BIO
  32654. /* test creating new EVP_PKEY with bad arg */
  32655. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  32656. /* Test bad EVP_PKEY type. */
  32657. /* New HMAC EVP_PKEY */
  32658. ExpectNotNull(bio = BIO_new_mem_buf("", 1));
  32659. ExpectNotNull(pkey = EVP_PKEY_new());
  32660. if (pkey != NULL) {
  32661. pkey->type = EVP_PKEY_HMAC;
  32662. }
  32663. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32664. 0);
  32665. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32666. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32667. NULL), 0);
  32668. #endif
  32669. #ifdef WOLFSSL_KEY_GEN
  32670. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), WOLFSSL_FAILURE);
  32671. #endif
  32672. EVP_PKEY_free(pkey);
  32673. pkey = NULL;
  32674. BIO_free(bio);
  32675. bio = NULL;
  32676. /* key is DES encrypted */
  32677. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  32678. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  32679. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  32680. {
  32681. XFILE f = XBADFILE;
  32682. wc_pem_password_cb* passwd_cb = NULL;
  32683. void* passwd_cb_userdata;
  32684. SSL_CTX* ctx = NULL;
  32685. char passwd[] = "bad password";
  32686. #ifndef WOLFSSL_NO_TLS12
  32687. #ifndef NO_WOLFSSL_SERVER
  32688. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  32689. #else
  32690. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  32691. #endif
  32692. #else
  32693. #ifndef NO_WOLFSSL_SERVER
  32694. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  32695. #else
  32696. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  32697. #endif
  32698. #endif
  32699. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  32700. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  32701. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  32702. ExpectNull(passwd_cb_userdata =
  32703. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32704. /* fail case with password call back */
  32705. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  32706. (void*)passwd));
  32707. BIO_free(bio);
  32708. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  32709. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  32710. (void*)passwd));
  32711. BIO_free(bio);
  32712. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  32713. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  32714. if ((bio == NULL) && (f != XBADFILE)) {
  32715. XFCLOSE(f);
  32716. }
  32717. /* use callback that works */
  32718. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  32719. (void*)"yassl123"));
  32720. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  32721. EVP_PKEY_free(pkey);
  32722. pkey = NULL;
  32723. BIO_free(bio);
  32724. bio = NULL;
  32725. SSL_CTX_free(ctx);
  32726. }
  32727. #endif /* !defined(NO_DES3) */
  32728. #endif /* !NO_BIO */
  32729. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  32730. {
  32731. unsigned char buf[2048];
  32732. size_t bytes = 0;
  32733. XFILE f = XBADFILE;
  32734. SSL_CTX* ctx = NULL;
  32735. #ifndef WOLFSSL_NO_TLS12
  32736. #ifndef NO_WOLFSSL_SERVER
  32737. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  32738. #else
  32739. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  32740. #endif
  32741. #else
  32742. #ifndef NO_WOLFSSL_SERVER
  32743. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  32744. #else
  32745. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  32746. #endif
  32747. #endif
  32748. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  32749. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  32750. if (f != XBADFILE)
  32751. XFCLOSE(f);
  32752. server_key = buf;
  32753. pkey = NULL;
  32754. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, (long int)bytes));
  32755. ExpectNull(pkey);
  32756. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, (long int)bytes));
  32757. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  32758. EVP_PKEY_free(pkey);
  32759. pkey = NULL;
  32760. SSL_CTX_free(ctx);
  32761. server_key = NULL;
  32762. }
  32763. #endif
  32764. #ifndef NO_BIO
  32765. (void)bio;
  32766. #endif
  32767. (void)pkey;
  32768. (void)server_key;
  32769. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  32770. return EXPECT_RESULT();
  32771. }
  32772. static int test_wolfSSL_PEM_file_RSAKey(void)
  32773. {
  32774. EXPECT_DECLS;
  32775. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32776. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  32777. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  32778. RSA* rsa = NULL;
  32779. XFILE fp = XBADFILE;
  32780. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  32781. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  32782. if (fp != XBADFILE)
  32783. XFCLOSE(fp);
  32784. ExpectIntEQ(RSA_size(rsa), 256);
  32785. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  32786. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  32787. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  32788. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  32789. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  32790. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  32791. RSA_free(rsa);
  32792. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32793. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  32794. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  32795. return EXPECT_RESULT();
  32796. }
  32797. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  32798. {
  32799. EXPECT_DECLS;
  32800. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  32801. !defined(NO_FILESYSTEM) && \
  32802. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  32803. RSA* rsa = NULL;
  32804. XFILE f = NULL;
  32805. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  32806. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  32807. ExpectIntEQ(RSA_size(rsa), 256);
  32808. if (f != XBADFILE) {
  32809. XFCLOSE(f);
  32810. f = XBADFILE;
  32811. }
  32812. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  32813. NULL), WOLFSSL_FAILURE);
  32814. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  32815. NULL), WOLFSSL_FAILURE);
  32816. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  32817. WOLFSSL_SUCCESS);
  32818. RSA_free(rsa);
  32819. #ifdef HAVE_ECC
  32820. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  32821. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  32822. if (f != XBADFILE)
  32823. XFCLOSE(f);
  32824. #endif /* HAVE_ECC */
  32825. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32826. return EXPECT_RESULT();
  32827. }
  32828. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  32829. {
  32830. EXPECT_DECLS;
  32831. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32832. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32833. XFILE file = XBADFILE;
  32834. const char* fname = "./certs/client-keyPub.pem";
  32835. RSA *rsa = NULL;
  32836. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  32837. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32838. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  32839. ExpectIntEQ(RSA_size(rsa), 256);
  32840. RSA_free(rsa);
  32841. if (file != XBADFILE)
  32842. XFCLOSE(file);
  32843. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32844. return EXPECT_RESULT();
  32845. }
  32846. #ifndef NO_BIO
  32847. static int test_wolfSSL_PEM_bio_RSAKey(void)
  32848. {
  32849. EXPECT_DECLS;
  32850. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32851. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  32852. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  32853. RSA* rsa = NULL;
  32854. BIO* bio = NULL;
  32855. /* PrivateKey */
  32856. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  32857. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  32858. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  32859. ExpectNotNull(rsa);
  32860. ExpectIntEQ(RSA_size(rsa), 256);
  32861. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  32862. NULL), WOLFSSL_FAILURE);
  32863. BIO_free(bio);
  32864. bio = NULL;
  32865. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32866. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  32867. NULL), WOLFSSL_SUCCESS);
  32868. BIO_free(bio);
  32869. bio = NULL;
  32870. RSA_free(rsa);
  32871. rsa = NULL;
  32872. /* PUBKEY */
  32873. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  32874. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  32875. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  32876. ExpectIntEQ(RSA_size(rsa), 256);
  32877. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  32878. BIO_free(bio);
  32879. bio = NULL;
  32880. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32881. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  32882. BIO_free(bio);
  32883. bio = NULL;
  32884. RSA_free(rsa);
  32885. rsa = NULL;
  32886. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  32887. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  32888. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  32889. BIO_free(bio);
  32890. bio = NULL;
  32891. RSA_free(rsa);
  32892. rsa = NULL;
  32893. #ifdef HAVE_ECC
  32894. /* ensure that non-rsa keys do not work */
  32895. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  32896. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  32897. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  32898. BIO_free(bio);
  32899. bio = NULL;
  32900. RSA_free(rsa);
  32901. rsa = NULL;
  32902. #endif /* HAVE_ECC */
  32903. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32904. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  32905. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  32906. return EXPECT_RESULT();
  32907. }
  32908. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  32909. {
  32910. EXPECT_DECLS;
  32911. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32912. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32913. RSA* rsa = NULL;
  32914. RSA* rsa_dup = NULL;
  32915. BIO* bio = NULL;
  32916. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  32917. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  32918. ExpectIntEQ(RSA_size(rsa), 256);
  32919. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  32920. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  32921. /* Test duplicating empty key. */
  32922. ExpectNotNull(rsa_dup = RSA_new());
  32923. ExpectNull(RSAPublicKey_dup(rsa_dup));
  32924. RSA_free(rsa_dup);
  32925. rsa_dup = NULL;
  32926. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  32927. ExpectPtrNE(rsa_dup, rsa);
  32928. #endif
  32929. /* test if valgrind complains about unreleased memory */
  32930. RSA_up_ref(rsa);
  32931. RSA_free(rsa);
  32932. BIO_free(bio);
  32933. bio = NULL;
  32934. RSA_free(rsa);
  32935. rsa = NULL;
  32936. RSA_free(rsa_dup);
  32937. rsa_dup = NULL;
  32938. #ifdef HAVE_ECC
  32939. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  32940. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  32941. BIO_free(bio);
  32942. #endif /* HAVE_ECC */
  32943. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32944. return EXPECT_RESULT();
  32945. }
  32946. static int test_wolfSSL_PEM_bio_DSAKey(void)
  32947. {
  32948. EXPECT_DECLS;
  32949. #ifndef HAVE_SELFTEST
  32950. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  32951. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  32952. DSA* dsa = NULL;
  32953. BIO* bio = NULL;
  32954. /* PrivateKey */
  32955. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  32956. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  32957. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  32958. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  32959. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  32960. NULL), WOLFSSL_FAILURE);
  32961. BIO_free(bio);
  32962. bio = NULL;
  32963. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32964. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  32965. NULL), WOLFSSL_SUCCESS);
  32966. BIO_free(bio);
  32967. bio = NULL;
  32968. DSA_free(dsa);
  32969. dsa = NULL;
  32970. /* PUBKEY */
  32971. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  32972. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  32973. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  32974. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  32975. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  32976. BIO_free(bio);
  32977. bio = NULL;
  32978. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32979. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  32980. BIO_free(bio);
  32981. bio = NULL;
  32982. DSA_free(dsa);
  32983. dsa = NULL;
  32984. #ifdef HAVE_ECC
  32985. /* ensure that non-dsa keys do not work */
  32986. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  32987. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  32988. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  32989. BIO_free(bio);
  32990. bio = NULL;
  32991. DSA_free(dsa);
  32992. dsa = NULL;
  32993. #endif /* HAVE_ECC */
  32994. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  32995. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  32996. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  32997. #endif /* HAVE_SELFTEST */
  32998. return EXPECT_RESULT();
  32999. }
  33000. static int test_wolfSSL_PEM_bio_ECKey(void)
  33001. {
  33002. EXPECT_DECLS;
  33003. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  33004. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  33005. EC_KEY* ec = NULL;
  33006. EC_KEY* ec2;
  33007. BIO* bio = NULL;
  33008. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  33009. unsigned char* pem = NULL;
  33010. int pLen;
  33011. #endif
  33012. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  33013. "MAA=\n"
  33014. "-----END PUBLIC KEY-----";
  33015. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  33016. "MAA=\n"
  33017. "-----END EC PRIVATE KEY-----";
  33018. /* PrivateKey */
  33019. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  33020. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  33021. ec2 = NULL;
  33022. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  33023. ExpectIntEQ(ec == ec2, 1);
  33024. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  33025. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  33026. NULL), WOLFSSL_FAILURE);
  33027. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  33028. NULL), WOLFSSL_FAILURE);
  33029. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  33030. WOLFSSL_FAILURE);
  33031. BIO_free(bio);
  33032. bio = NULL;
  33033. /* Public key data - fail. */
  33034. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  33035. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  33036. BIO_free(bio);
  33037. bio = NULL;
  33038. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  33039. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  33040. NULL), WOLFSSL_SUCCESS);
  33041. BIO_free(bio);
  33042. bio = NULL;
  33043. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  33044. NULL),WOLFSSL_FAILURE);
  33045. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  33046. WOLFSSL_FAILURE);
  33047. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  33048. WOLFSSL_FAILURE);
  33049. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  33050. WOLFSSL_SUCCESS);
  33051. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  33052. NULL), 0);
  33053. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  33054. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  33055. NULL), 0);
  33056. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  33057. NULL), 0);
  33058. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  33059. &pLen), 0);
  33060. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  33061. &pLen), 0);
  33062. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  33063. &pLen), 0);
  33064. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  33065. NULL), 0);
  33066. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  33067. &pLen), 1);
  33068. ExpectIntGT(pLen, 0);
  33069. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33070. #endif
  33071. EC_KEY_free(ec);
  33072. ec = NULL;
  33073. /* PUBKEY */
  33074. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  33075. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  33076. ec2 = NULL;
  33077. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  33078. ExpectIntEQ(ec == ec2, 1);
  33079. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  33080. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  33081. BIO_free(bio);
  33082. bio = NULL;
  33083. /* Test 0x30, 0x00 fails. */
  33084. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  33085. sizeof(ec_key_bad_1)));
  33086. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  33087. BIO_free(bio);
  33088. bio = NULL;
  33089. /* Private key data - fail. */
  33090. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  33091. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  33092. BIO_free(bio);
  33093. bio = NULL;
  33094. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  33095. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  33096. BIO_free(bio);
  33097. bio = NULL;
  33098. /* Same test as above, but with a file pointer rather than a BIO. */
  33099. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  33100. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  33101. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  33102. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  33103. EC_KEY_free(ec);
  33104. ec = NULL;
  33105. #ifndef NO_RSA
  33106. /* ensure that non-ec keys do not work */
  33107. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  33108. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  33109. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  33110. BIO_free(bio);
  33111. bio = NULL;
  33112. EC_KEY_free(ec);
  33113. ec = NULL;
  33114. #endif /* !NO_RSA */
  33115. /* Test 0x30, 0x00 fails. */
  33116. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  33117. sizeof(ec_priv_key_bad_1)));
  33118. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  33119. BIO_free(bio);
  33120. bio = NULL;
  33121. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  33122. return EXPECT_RESULT();
  33123. }
  33124. static int test_wolfSSL_PEM_PUBKEY(void)
  33125. {
  33126. EXPECT_DECLS;
  33127. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  33128. BIO* bio = NULL;
  33129. EVP_PKEY* pkey = NULL;
  33130. /* test creating new EVP_PKEY with bad arg */
  33131. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  33132. /* test loading ECC key using BIO */
  33133. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  33134. {
  33135. XFILE file = XBADFILE;
  33136. const char* fname = "./certs/ecc-client-keyPub.pem";
  33137. size_t sz = 0;
  33138. byte* buf = NULL;
  33139. EVP_PKEY* pkey2 = NULL;
  33140. EC_KEY* ec_key = NULL;
  33141. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  33142. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  33143. ExpectIntGT(sz = XFTELL(file), 0);
  33144. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  33145. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  33146. if (buf != NULL) {
  33147. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  33148. }
  33149. if (file != XBADFILE) {
  33150. XFCLOSE(file);
  33151. }
  33152. /* Test using BIO new mem and loading PEM private key */
  33153. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  33154. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  33155. BIO_free(bio);
  33156. bio = NULL;
  33157. EVP_PKEY_free(pkey);
  33158. pkey = NULL;
  33159. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  33160. ExpectNotNull(pkey = EVP_PKEY_new());
  33161. ExpectPtrEq(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL), pkey);
  33162. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  33163. BIO_free(bio);
  33164. bio = NULL;
  33165. /* Qt unit test case*/
  33166. ExpectNotNull(pkey2 = EVP_PKEY_new());
  33167. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  33168. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  33169. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  33170. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  33171. #else
  33172. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  33173. #endif
  33174. EC_KEY_free(ec_key);
  33175. EVP_PKEY_free(pkey2);
  33176. EVP_PKEY_free(pkey);
  33177. pkey = NULL;
  33178. }
  33179. #endif
  33180. (void)bio;
  33181. (void)pkey;
  33182. #endif
  33183. return EXPECT_RESULT();
  33184. }
  33185. #endif /* !NO_BIO */
  33186. static int test_DSA_do_sign_verify(void)
  33187. {
  33188. EXPECT_DECLS;
  33189. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  33190. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  33191. !defined(NO_DSA)
  33192. unsigned char digest[WC_SHA_DIGEST_SIZE];
  33193. DSA_SIG* sig = NULL;
  33194. DSA* dsa = NULL;
  33195. word32 bytes;
  33196. byte sigBin[DSA_SIG_SIZE];
  33197. int dsacheck;
  33198. #ifdef USE_CERT_BUFFERS_1024
  33199. byte tmp[ONEK_BUF];
  33200. XMEMSET(tmp, 0, sizeof(tmp));
  33201. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  33202. bytes = sizeof_dsa_key_der_1024;
  33203. #elif defined(USE_CERT_BUFFERS_2048)
  33204. byte tmp[TWOK_BUF];
  33205. XMEMSET(tmp, 0, sizeof(tmp));
  33206. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  33207. bytes = sizeof_dsa_key_der_2048;
  33208. #else
  33209. byte tmp[TWOK_BUF];
  33210. XFILE fp = XBADFILE;
  33211. XMEMSET(tmp, 0, sizeof(tmp));
  33212. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  33213. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  33214. if (fp != XBADFILE)
  33215. XFCLOSE(fp);
  33216. #endif /* END USE_CERT_BUFFERS_1024 */
  33217. XMEMSET(digest, 202, sizeof(digest));
  33218. ExpectNotNull(dsa = DSA_new());
  33219. ExpectIntEQ(DSA_LoadDer(dsa, tmp, (int)bytes), 1);
  33220. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  33221. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  33222. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  33223. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  33224. DSA_SIG_free(sig);
  33225. DSA_free(dsa);
  33226. #endif
  33227. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  33228. return EXPECT_RESULT();
  33229. }
  33230. static int test_wolfSSL_tmp_dh(void)
  33231. {
  33232. EXPECT_DECLS;
  33233. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  33234. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  33235. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33236. byte buff[6000];
  33237. char file[] = "./certs/dsaparams.pem";
  33238. XFILE f = XBADFILE;
  33239. int bytes = 0;
  33240. DSA* dsa = NULL;
  33241. DH* dh = NULL;
  33242. #if defined(WOLFSSL_DH_EXTRA) && \
  33243. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  33244. DH* dh2 = NULL;
  33245. #endif
  33246. BIO* bio = NULL;
  33247. SSL* ssl = NULL;
  33248. SSL_CTX* ctx = NULL;
  33249. #ifndef NO_WOLFSSL_SERVER
  33250. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33251. #else
  33252. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33253. #endif
  33254. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33255. WOLFSSL_FILETYPE_PEM));
  33256. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33257. WOLFSSL_FILETYPE_PEM));
  33258. ExpectNotNull(ssl = SSL_new(ctx));
  33259. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  33260. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  33261. if (f != XBADFILE)
  33262. XFCLOSE(f);
  33263. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  33264. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  33265. ExpectNotNull(dsa);
  33266. dh = wolfSSL_DSA_dup_DH(dsa);
  33267. ExpectNotNull(dh);
  33268. #if defined(WOLFSSL_DH_EXTRA) && \
  33269. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  33270. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  33271. #endif
  33272. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  33273. #ifndef NO_WOLFSSL_SERVER
  33274. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  33275. #else
  33276. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  33277. #endif
  33278. BIO_free(bio);
  33279. DSA_free(dsa);
  33280. DH_free(dh);
  33281. dh = NULL;
  33282. #if defined(WOLFSSL_DH_EXTRA) && \
  33283. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  33284. DH_free(dh2);
  33285. dh2 = NULL;
  33286. #endif
  33287. SSL_free(ssl);
  33288. SSL_CTX_free(ctx);
  33289. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33290. #endif
  33291. return EXPECT_RESULT();
  33292. }
  33293. static int test_wolfSSL_ctrl(void)
  33294. {
  33295. EXPECT_DECLS;
  33296. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  33297. byte buff[6000];
  33298. BIO* bio = NULL;
  33299. int bytes;
  33300. BUF_MEM* ptr = NULL;
  33301. XMEMSET(buff, 0, sizeof(buff));
  33302. bytes = sizeof(buff);
  33303. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  33304. ExpectNotNull(BIO_s_socket());
  33305. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  33306. /* needs tested after stubs filled out @TODO
  33307. SSL_ctrl
  33308. SSL_CTX_ctrl
  33309. */
  33310. BIO_free(bio);
  33311. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  33312. return EXPECT_RESULT();
  33313. }
  33314. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  33315. {
  33316. EXPECT_DECLS;
  33317. #ifdef OPENSSL_EXTRA
  33318. static const unsigned char pw[] = "password";
  33319. static const int pwSz = sizeof(pw) - 1;
  33320. size_t checkPwSz = 0;
  33321. const unsigned char* checkPw = NULL;
  33322. WOLFSSL_EVP_PKEY* key = NULL;
  33323. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  33324. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  33325. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  33326. pwSz));
  33327. if (key != NULL) {
  33328. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  33329. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  33330. ExpectIntEQ(key->pkey_sz, pwSz);
  33331. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  33332. }
  33333. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  33334. ExpectIntEQ((int)checkPwSz, pwSz);
  33335. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  33336. wolfSSL_EVP_PKEY_free(key);
  33337. key = NULL;
  33338. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  33339. 0));
  33340. ExpectIntEQ(key->pkey_sz, 0);
  33341. if (EXPECT_SUCCESS()) {
  33342. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  33343. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  33344. }
  33345. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  33346. ExpectIntEQ((int)checkPwSz, 0);
  33347. wolfSSL_EVP_PKEY_free(key);
  33348. key = NULL;
  33349. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  33350. 0));
  33351. ExpectIntEQ(key->pkey_sz, 0);
  33352. if (EXPECT_SUCCESS()) {
  33353. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  33354. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  33355. }
  33356. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  33357. ExpectIntEQ((int)checkPwSz, 0);
  33358. wolfSSL_EVP_PKEY_free(key);
  33359. key = NULL;
  33360. #endif /* OPENSSL_EXTRA */
  33361. return EXPECT_RESULT();
  33362. }
  33363. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  33364. {
  33365. EXPECT_DECLS;
  33366. #ifdef OPENSSL_EXTRA
  33367. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  33368. const char *priv = "ABCDEFGHIJKLMNOP";
  33369. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  33370. WOLFSSL_EVP_PKEY* key = NULL;
  33371. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  33372. NULL, NULL, AES_128_KEY_SIZE, cipher));
  33373. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  33374. NULL, (const unsigned char *)priv, 0, cipher));
  33375. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  33376. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  33377. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  33378. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  33379. wolfSSL_EVP_PKEY_free(key);
  33380. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  33381. #endif /* OPENSSL_EXTRA */
  33382. return EXPECT_RESULT();
  33383. }
  33384. static int test_wolfSSL_EVP_Digest(void)
  33385. {
  33386. EXPECT_DECLS;
  33387. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  33388. const char* in = "abc";
  33389. int inLen = (int)XSTRLEN(in);
  33390. byte out[WC_SHA256_DIGEST_SIZE];
  33391. unsigned int outLen;
  33392. const char* expOut =
  33393. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  33394. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  33395. "\x15\xAD";
  33396. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  33397. "SHA256", NULL), 1);
  33398. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  33399. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  33400. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  33401. return EXPECT_RESULT();
  33402. }
  33403. static int test_wolfSSL_EVP_Digest_all(void)
  33404. {
  33405. EXPECT_DECLS;
  33406. #ifdef OPENSSL_EXTRA
  33407. const char* digests[] = {
  33408. #ifndef NO_MD5
  33409. "MD5",
  33410. #endif
  33411. #ifndef NO_SHA
  33412. "SHA",
  33413. #endif
  33414. #ifdef WOLFSSL_SHA224
  33415. "SHA224",
  33416. #endif
  33417. #ifndef NO_SHA256
  33418. "SHA256",
  33419. #endif
  33420. #ifdef WOLFSSL_SHA384
  33421. "SHA384",
  33422. #endif
  33423. #ifdef WOLFSSL_SHA512
  33424. "SHA512",
  33425. #endif
  33426. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  33427. "SHA512_224",
  33428. #endif
  33429. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  33430. "SHA512_256",
  33431. #endif
  33432. #ifdef WOLFSSL_SHA3
  33433. #ifndef WOLFSSL_NOSHA3_224
  33434. "SHA3_224",
  33435. #endif
  33436. #ifndef WOLFSSL_NOSHA3_256
  33437. "SHA3_256",
  33438. #endif
  33439. "SHA3_384",
  33440. #ifndef WOLFSSL_NOSHA3_512
  33441. "SHA3_512",
  33442. #endif
  33443. #endif /* WOLFSSL_SHA3 */
  33444. NULL
  33445. };
  33446. const char** d;
  33447. const unsigned char in[] = "abc";
  33448. int inLen = XSTR_SIZEOF(in);
  33449. byte out[WC_MAX_DIGEST_SIZE];
  33450. unsigned int outLen;
  33451. for (d = digests; *d != NULL; d++) {
  33452. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  33453. ExpectIntGT(outLen, 0);
  33454. ExpectIntEQ(EVP_MD_size(*d), outLen);
  33455. }
  33456. #endif
  33457. return EXPECT_RESULT();
  33458. }
  33459. static int test_wolfSSL_EVP_MD_size(void)
  33460. {
  33461. EXPECT_DECLS;
  33462. #ifdef OPENSSL_EXTRA
  33463. WOLFSSL_EVP_MD_CTX mdCtx;
  33464. #ifdef WOLFSSL_SHA3
  33465. #ifndef WOLFSSL_NOSHA3_224
  33466. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33467. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  33468. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  33469. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  33470. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33471. #endif
  33472. #ifndef WOLFSSL_NOSHA3_256
  33473. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33474. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  33475. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  33476. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  33477. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33478. #endif
  33479. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33480. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  33481. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  33482. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  33483. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33484. #ifndef WOLFSSL_NOSHA3_512
  33485. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33486. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  33487. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  33488. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  33489. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33490. #endif
  33491. #endif /* WOLFSSL_SHA3 */
  33492. #ifndef NO_SHA256
  33493. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33494. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  33495. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33496. WC_SHA256_DIGEST_SIZE);
  33497. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33498. WC_SHA256_BLOCK_SIZE);
  33499. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  33500. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  33501. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33502. #endif
  33503. #ifndef NO_MD5
  33504. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33505. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  33506. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33507. WC_MD5_DIGEST_SIZE);
  33508. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33509. WC_MD5_BLOCK_SIZE);
  33510. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  33511. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  33512. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33513. #endif
  33514. #ifdef WOLFSSL_SHA224
  33515. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33516. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  33517. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33518. WC_SHA224_DIGEST_SIZE);
  33519. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33520. WC_SHA224_BLOCK_SIZE);
  33521. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  33522. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  33523. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33524. #endif
  33525. #ifdef WOLFSSL_SHA384
  33526. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33527. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  33528. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33529. WC_SHA384_DIGEST_SIZE);
  33530. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33531. WC_SHA384_BLOCK_SIZE);
  33532. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  33533. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  33534. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33535. #endif
  33536. #ifdef WOLFSSL_SHA512
  33537. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33538. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  33539. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33540. WC_SHA512_DIGEST_SIZE);
  33541. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33542. WC_SHA512_BLOCK_SIZE);
  33543. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  33544. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  33545. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33546. #endif
  33547. #ifndef NO_SHA
  33548. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33549. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  33550. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33551. WC_SHA_DIGEST_SIZE);
  33552. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33553. WC_SHA_BLOCK_SIZE);
  33554. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  33555. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  33556. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33557. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33558. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  33559. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33560. WC_SHA_DIGEST_SIZE);
  33561. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33562. WC_SHA_BLOCK_SIZE);
  33563. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  33564. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  33565. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33566. #endif
  33567. /* error case */
  33568. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33569. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  33570. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33571. BAD_FUNC_ARG);
  33572. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  33573. /* Cleanup is valid on uninit'ed struct */
  33574. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33575. #endif /* OPENSSL_EXTRA */
  33576. return EXPECT_RESULT();
  33577. }
  33578. static int test_wolfSSL_EVP_MD_pkey_type(void)
  33579. {
  33580. EXPECT_DECLS;
  33581. #ifdef OPENSSL_EXTRA
  33582. const WOLFSSL_EVP_MD* md;
  33583. #ifndef NO_MD5
  33584. ExpectNotNull(md = EVP_md5());
  33585. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  33586. #endif
  33587. #ifndef NO_SHA
  33588. ExpectNotNull(md = EVP_sha1());
  33589. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  33590. #endif
  33591. #ifdef WOLFSSL_SHA224
  33592. ExpectNotNull(md = EVP_sha224());
  33593. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  33594. #endif
  33595. ExpectNotNull(md = EVP_sha256());
  33596. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  33597. #ifdef WOLFSSL_SHA384
  33598. ExpectNotNull(md = EVP_sha384());
  33599. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  33600. #endif
  33601. #ifdef WOLFSSL_SHA512
  33602. ExpectNotNull(md = EVP_sha512());
  33603. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  33604. #endif
  33605. #endif
  33606. return EXPECT_RESULT();
  33607. }
  33608. #ifdef OPENSSL_EXTRA
  33609. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  33610. size_t testKeySz, const char* testData, size_t testDataSz,
  33611. const byte* testResult, size_t testResultSz)
  33612. {
  33613. EXPECT_DECLS;
  33614. unsigned char check[WC_MAX_DIGEST_SIZE];
  33615. size_t checkSz = -1;
  33616. WOLFSSL_EVP_PKEY* key = NULL;
  33617. WOLFSSL_EVP_MD_CTX mdCtx;
  33618. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  33619. testKey, (int)testKeySz));
  33620. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33621. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  33622. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33623. (unsigned int)testDataSz), 1);
  33624. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33625. ExpectIntEQ((int)checkSz, (int)testResultSz);
  33626. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33627. ExpectIntEQ((int)checkSz,(int)testResultSz);
  33628. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  33629. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33630. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  33631. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33632. (unsigned int)testDataSz), 1);
  33633. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  33634. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33635. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33636. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  33637. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  33638. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33639. ExpectIntEQ((int)checkSz, (int)testResultSz);
  33640. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33641. ExpectIntEQ((int)checkSz,(int)testResultSz);
  33642. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  33643. (unsigned int)testDataSz - 4), 1);
  33644. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33645. ExpectIntEQ((int)checkSz,(int)testResultSz);
  33646. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  33647. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33648. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  33649. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  33650. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  33651. (unsigned int)testDataSz - 4), 1);
  33652. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  33653. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33654. wolfSSL_EVP_PKEY_free(key);
  33655. return EXPECT_RESULT();
  33656. }
  33657. #endif
  33658. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  33659. {
  33660. EXPECT_DECLS;
  33661. #ifdef OPENSSL_EXTRA
  33662. static const unsigned char testKey[] =
  33663. {
  33664. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  33665. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  33666. 0x0b, 0x0b, 0x0b, 0x0b
  33667. };
  33668. static const char testData[] = "Hi There";
  33669. #ifdef WOLFSSL_SHA224
  33670. static const unsigned char testResultSha224[] =
  33671. {
  33672. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  33673. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  33674. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  33675. 0x53, 0x68, 0x4b, 0x22
  33676. };
  33677. #endif
  33678. #ifndef NO_SHA256
  33679. static const unsigned char testResultSha256[] =
  33680. {
  33681. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  33682. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  33683. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  33684. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  33685. };
  33686. #endif
  33687. #ifdef WOLFSSL_SHA384
  33688. static const unsigned char testResultSha384[] =
  33689. {
  33690. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  33691. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  33692. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  33693. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  33694. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  33695. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  33696. };
  33697. #endif
  33698. #ifdef WOLFSSL_SHA512
  33699. static const unsigned char testResultSha512[] =
  33700. {
  33701. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  33702. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  33703. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  33704. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  33705. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  33706. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  33707. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  33708. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  33709. };
  33710. #endif
  33711. #ifdef WOLFSSL_SHA3
  33712. #ifndef WOLFSSL_NOSHA3_224
  33713. static const unsigned char testResultSha3_224[] =
  33714. {
  33715. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  33716. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  33717. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  33718. 0xf3, 0xc8, 0x60, 0xf7
  33719. };
  33720. #endif
  33721. #ifndef WOLFSSL_NOSHA3_256
  33722. static const unsigned char testResultSha3_256[] =
  33723. {
  33724. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  33725. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  33726. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  33727. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  33728. };
  33729. #endif
  33730. #ifndef WOLFSSL_NOSHA3_384
  33731. static const unsigned char testResultSha3_384[] =
  33732. {
  33733. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  33734. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  33735. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  33736. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  33737. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  33738. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  33739. };
  33740. #endif
  33741. #ifndef WOLFSSL_NOSHA3_512
  33742. static const unsigned char testResultSha3_512[] =
  33743. {
  33744. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  33745. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  33746. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  33747. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  33748. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  33749. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  33750. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  33751. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  33752. };
  33753. #endif
  33754. #endif
  33755. #ifndef NO_SHA256
  33756. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  33757. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  33758. sizeof(testResultSha256)), TEST_SUCCESS);
  33759. #endif
  33760. #ifdef WOLFSSL_SHA224
  33761. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  33762. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  33763. sizeof(testResultSha224)), TEST_SUCCESS);
  33764. #endif
  33765. #ifdef WOLFSSL_SHA384
  33766. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  33767. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  33768. sizeof(testResultSha384)), TEST_SUCCESS);
  33769. #endif
  33770. #ifdef WOLFSSL_SHA512
  33771. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  33772. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  33773. sizeof(testResultSha512)), TEST_SUCCESS);
  33774. #endif
  33775. #ifdef WOLFSSL_SHA3
  33776. #ifndef WOLFSSL_NOSHA3_224
  33777. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  33778. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  33779. sizeof(testResultSha3_224)), TEST_SUCCESS);
  33780. #endif
  33781. #ifndef WOLFSSL_NOSHA3_256
  33782. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  33783. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  33784. sizeof(testResultSha3_256)), TEST_SUCCESS);
  33785. #endif
  33786. #ifndef WOLFSSL_NOSHA3_384
  33787. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  33788. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  33789. sizeof(testResultSha3_384)), TEST_SUCCESS);
  33790. #endif
  33791. #ifndef WOLFSSL_NOSHA3_512
  33792. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  33793. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  33794. sizeof(testResultSha3_512)), TEST_SUCCESS);
  33795. #endif
  33796. #endif
  33797. #endif /* OPENSSL_EXTRA */
  33798. return EXPECT_RESULT();
  33799. }
  33800. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  33801. {
  33802. EXPECT_DECLS;
  33803. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  33804. WOLFSSL_EVP_PKEY* privKey = NULL;
  33805. WOLFSSL_EVP_PKEY* pubKey = NULL;
  33806. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  33807. const char testData[] = "Hi There";
  33808. WOLFSSL_EVP_MD_CTX mdCtx;
  33809. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  33810. int ret;
  33811. size_t checkSz = -1;
  33812. int sz = 2048 / 8;
  33813. const unsigned char* cp;
  33814. const unsigned char* p;
  33815. unsigned char check[2048/8];
  33816. size_t i;
  33817. int paddings[] = {
  33818. RSA_PKCS1_PADDING,
  33819. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  33820. RSA_PKCS1_PSS_PADDING,
  33821. #endif
  33822. };
  33823. cp = client_key_der_2048;
  33824. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  33825. sizeof_client_key_der_2048)));
  33826. p = client_keypub_der_2048;
  33827. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  33828. sizeof_client_keypub_der_2048)));
  33829. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33830. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  33831. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33832. NULL, privKey), 1);
  33833. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33834. (unsigned int)XSTRLEN(testData)), 1);
  33835. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33836. ExpectIntEQ((int)checkSz, sz);
  33837. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33838. ExpectIntEQ((int)checkSz,sz);
  33839. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  33840. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  33841. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  33842. ExpectIntEQ(ret, 1);
  33843. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33844. ExpectIntEQ(ret, 1);
  33845. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33846. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33847. NULL, pubKey), 1);
  33848. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33849. (unsigned int)XSTRLEN(testData)),
  33850. 1);
  33851. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33852. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33853. ExpectIntEQ(ret, 1);
  33854. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33855. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33856. NULL, privKey), 1);
  33857. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  33858. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33859. ExpectIntEQ((int)checkSz, sz);
  33860. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33861. ExpectIntEQ((int)checkSz, sz);
  33862. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  33863. (unsigned int)XSTRLEN(testData) - 4), 1);
  33864. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33865. ExpectIntEQ((int)checkSz, sz);
  33866. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33867. ExpectIntEQ(ret, 1);
  33868. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33869. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33870. NULL, pubKey), 1);
  33871. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  33872. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  33873. (unsigned int)XSTRLEN(testData) - 4),
  33874. 1);
  33875. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33876. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33877. ExpectIntEQ(ret, 1);
  33878. /* Check all signing padding types */
  33879. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  33880. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33881. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  33882. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  33883. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  33884. paddings[i]), 1);
  33885. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33886. (unsigned int)XSTRLEN(testData)), 1);
  33887. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33888. ExpectIntEQ((int)checkSz, sz);
  33889. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33890. ExpectIntEQ((int)checkSz,sz);
  33891. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33892. ExpectIntEQ(ret, 1);
  33893. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33894. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  33895. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  33896. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  33897. paddings[i]), 1);
  33898. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33899. (unsigned int)XSTRLEN(testData)), 1);
  33900. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33901. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33902. ExpectIntEQ(ret, 1);
  33903. }
  33904. wolfSSL_EVP_PKEY_free(pubKey);
  33905. wolfSSL_EVP_PKEY_free(privKey);
  33906. #endif
  33907. return EXPECT_RESULT();
  33908. }
  33909. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  33910. {
  33911. EXPECT_DECLS;
  33912. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  33913. WOLFSSL_EVP_PKEY* privKey = NULL;
  33914. WOLFSSL_EVP_PKEY* pubKey = NULL;
  33915. const char testData[] = "Hi There";
  33916. WOLFSSL_EVP_MD_CTX mdCtx;
  33917. int ret;
  33918. size_t checkSz = -1;
  33919. const unsigned char* cp;
  33920. const unsigned char* p;
  33921. unsigned char check[2048/8];
  33922. cp = ecc_clikey_der_256;
  33923. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  33924. sizeof_ecc_clikey_der_256));
  33925. p = ecc_clikeypub_der_256;
  33926. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  33927. sizeof_ecc_clikeypub_der_256)));
  33928. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33929. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33930. NULL, privKey), 1);
  33931. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33932. (unsigned int)XSTRLEN(testData)), 1);
  33933. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33934. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33935. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33936. ExpectIntEQ(ret, 1);
  33937. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33938. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33939. NULL, pubKey), 1);
  33940. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33941. (unsigned int)XSTRLEN(testData)),
  33942. 1);
  33943. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33944. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33945. ExpectIntEQ(ret, 1);
  33946. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33947. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33948. NULL, privKey), 1);
  33949. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  33950. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33951. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33952. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  33953. (unsigned int)XSTRLEN(testData) - 4), 1);
  33954. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33955. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33956. ExpectIntEQ(ret, 1);
  33957. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33958. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33959. NULL, pubKey), 1);
  33960. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  33961. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  33962. (unsigned int)XSTRLEN(testData) - 4),
  33963. 1);
  33964. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33965. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33966. ExpectIntEQ(ret, 1);
  33967. wolfSSL_EVP_PKEY_free(pubKey);
  33968. wolfSSL_EVP_PKEY_free(privKey);
  33969. #endif
  33970. return EXPECT_RESULT();
  33971. }
  33972. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  33973. {
  33974. EXPECT_DECLS;
  33975. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33976. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  33977. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33978. char caFile[] = "./certs/client-ca.pem";
  33979. char clientFile[] = "./certs/client-cert.pem";
  33980. SSL_CTX* ctx = NULL;
  33981. X509* x509 = NULL;
  33982. BIO *bio = NULL;
  33983. X509 *cert = NULL;
  33984. X509 *ca = NULL;
  33985. STACK_OF(X509) *chain = NULL;
  33986. STACK_OF(X509) *chain2 = NULL;
  33987. #ifndef NO_WOLFSSL_SERVER
  33988. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33989. #else
  33990. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33991. #endif
  33992. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  33993. WOLFSSL_FILETYPE_PEM));
  33994. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  33995. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  33996. WOLFSSL_FILETYPE_PEM));
  33997. /* additional test of getting EVP_PKEY key size from X509
  33998. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  33999. * allowed with user RSA */
  34000. {
  34001. EVP_PKEY* pkey = NULL;
  34002. #if defined(HAVE_ECC)
  34003. X509* ecX509 = NULL;
  34004. #endif /* HAVE_ECC */
  34005. ExpectNotNull(pkey = X509_get_pubkey(x509));
  34006. /* current RSA key is 2048 bit (256 bytes) */
  34007. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  34008. EVP_PKEY_free(pkey);
  34009. pkey = NULL;
  34010. #if defined(HAVE_ECC)
  34011. #if defined(USE_CERT_BUFFERS_256)
  34012. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  34013. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  34014. SSL_FILETYPE_ASN1));
  34015. #else
  34016. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  34017. cliEccCertFile, SSL_FILETYPE_PEM));
  34018. #endif
  34019. pkey = X509_get_pubkey(ecX509);
  34020. ExpectNotNull(pkey);
  34021. /* current ECC key is 256 bit (32 bytes) */
  34022. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  34023. X509_free(ecX509);
  34024. ecX509 = NULL;
  34025. EVP_PKEY_free(pkey);
  34026. pkey = NULL;
  34027. #endif /* HAVE_ECC */
  34028. }
  34029. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  34030. if (EXPECT_SUCCESS()) {
  34031. x509 = NULL;
  34032. }
  34033. #ifdef WOLFSSL_ENCRYPTED_KEYS
  34034. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  34035. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  34036. #endif
  34037. SSL_CTX_free(ctx);
  34038. ctx = NULL;
  34039. #ifndef NO_WOLFSSL_SERVER
  34040. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34041. #else
  34042. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34043. #endif
  34044. /* Test haproxy use case */
  34045. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  34046. /* Read Certificate */
  34047. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  34048. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  34049. ExpectNotNull(chain = sk_X509_new_null());
  34050. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  34051. if (EXPECT_SUCCESS()) {
  34052. ca = NULL;
  34053. }
  34054. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  34055. ExpectNotNull(ca = sk_X509_shift(chain2));
  34056. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  34057. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  34058. if (EXPECT_SUCCESS()) {
  34059. ca = NULL;
  34060. }
  34061. BIO_free(bio);
  34062. X509_free(cert);
  34063. X509_free(ca);
  34064. X509_free(x509);
  34065. sk_X509_pop_free(chain, X509_free);
  34066. sk_X509_pop_free(chain2, X509_free);
  34067. SSL_CTX_free(ctx);
  34068. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34069. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34070. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  34071. return EXPECT_RESULT();
  34072. }
  34073. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  34074. static int test_wolfSSL_ERR_peek_last_error_line(void)
  34075. {
  34076. EXPECT_DECLS;
  34077. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34078. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  34079. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  34080. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  34081. callback_functions client_cb;
  34082. callback_functions server_cb;
  34083. int line = 0;
  34084. int flag = ERR_TXT_STRING;
  34085. const char* file = NULL;
  34086. const char* data = NULL;
  34087. /* create a failed connection and inspect the error */
  34088. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  34089. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  34090. client_cb.method = wolfTLSv1_1_client_method;
  34091. server_cb.method = wolfTLSv1_2_server_method;
  34092. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  34093. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  34094. ExpectNotNull(data);
  34095. /* check clearing error state */
  34096. ERR_remove_state(0);
  34097. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  34098. ERR_peek_last_error_line(NULL, &line);
  34099. ExpectIntEQ(line, 0);
  34100. ERR_peek_last_error_line(&file, NULL);
  34101. ExpectNull(file);
  34102. /* retry connection to fill error queue */
  34103. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  34104. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  34105. client_cb.method = wolfTLSv1_1_client_method;
  34106. server_cb.method = wolfTLSv1_2_server_method;
  34107. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  34108. /* check that error code was stored */
  34109. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  34110. ERR_peek_last_error_line(NULL, &line);
  34111. ExpectIntNE(line, 0);
  34112. ERR_peek_last_error_line(&file, NULL);
  34113. ExpectNotNull(file);
  34114. fprintf(stderr, "\nTesting error print out\n");
  34115. ERR_print_errors_fp(stderr);
  34116. fprintf(stderr, "Done testing print out\n\n");
  34117. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  34118. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  34119. return EXPECT_RESULT();
  34120. }
  34121. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  34122. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34123. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34124. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  34125. {
  34126. (void) ok;
  34127. (void) ctx;
  34128. fprintf(stderr, "ENTER verify_cb\n");
  34129. return SSL_SUCCESS;
  34130. }
  34131. #endif
  34132. static int test_wolfSSL_X509_Name_canon(void)
  34133. {
  34134. EXPECT_DECLS;
  34135. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  34136. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  34137. defined(WOLFSSL_CERT_GEN) && \
  34138. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  34139. const long ex_hash1 = 0x0fdb2da4;
  34140. const long ex_hash2 = 0x9f3e8c9e;
  34141. X509_NAME *name = NULL;
  34142. X509 *x509 = NULL;
  34143. XFILE file = XBADFILE;
  34144. unsigned long hash = 0;
  34145. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  34146. byte *pbuf = NULL;
  34147. word32 len = 0;
  34148. (void) ex_hash2;
  34149. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  34150. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  34151. ExpectNotNull(name = X509_get_issuer_name(x509));
  34152. /* When output buffer is NULL, should return necessary output buffer
  34153. * length.*/
  34154. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  34155. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  34156. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  34157. hash = (((unsigned long)digest[3] << 24) |
  34158. ((unsigned long)digest[2] << 16) |
  34159. ((unsigned long)digest[1] << 8) |
  34160. ((unsigned long)digest[0]));
  34161. ExpectIntEQ(hash, ex_hash1);
  34162. if (file != XBADFILE) {
  34163. XFCLOSE(file);
  34164. file = XBADFILE;
  34165. }
  34166. X509_free(x509);
  34167. x509 = NULL;
  34168. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  34169. pbuf = NULL;
  34170. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  34171. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  34172. ExpectNotNull(name = X509_get_issuer_name(x509));
  34173. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  34174. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  34175. hash = (((unsigned long)digest[3] << 24) |
  34176. ((unsigned long)digest[2] << 16) |
  34177. ((unsigned long)digest[1] << 8) |
  34178. ((unsigned long)digest[0]));
  34179. ExpectIntEQ(hash, ex_hash2);
  34180. if (file != XBADFILE)
  34181. XFCLOSE(file);
  34182. X509_free(x509);
  34183. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  34184. #endif
  34185. return EXPECT_RESULT();
  34186. }
  34187. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  34188. {
  34189. EXPECT_DECLS;
  34190. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  34191. const int MAX_DIR = 4;
  34192. const char paths[][32] = {
  34193. "./certs/ed25519",
  34194. "./certs/ecc",
  34195. "./certs/crl",
  34196. "./certs/",
  34197. };
  34198. char CertCrl_path[MAX_FILENAME_SZ];
  34199. char *p;
  34200. X509_STORE* str = NULL;
  34201. X509_LOOKUP* lookup = NULL;
  34202. WOLFSSL_STACK* sk = NULL;
  34203. int len, total_len, i;
  34204. (void)sk;
  34205. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  34206. /* illegal string */
  34207. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34208. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34209. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  34210. SSL_FILETYPE_PEM,NULL), 0);
  34211. /* free store */
  34212. X509_STORE_free(str);
  34213. str = NULL;
  34214. /* short folder string */
  34215. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34216. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34217. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  34218. SSL_FILETYPE_PEM,NULL), 1);
  34219. #if defined(WOLFSSL_INT_H)
  34220. /* only available when including internal.h */
  34221. ExpectNotNull(sk = lookup->dirs->dir_entry);
  34222. #endif
  34223. /* free store */
  34224. X509_STORE_free(str);
  34225. str = NULL;
  34226. /* typical function check */
  34227. p = &CertCrl_path[0];
  34228. total_len = 0;
  34229. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  34230. len = (int)XSTRLEN((const char*)&paths[i]);
  34231. total_len += len;
  34232. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  34233. p += len;
  34234. if (i != 0) *(p++) = SEPARATOR_CHAR;
  34235. }
  34236. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34237. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34238. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  34239. SSL_FILETYPE_PEM,NULL), 1);
  34240. #if defined(WOLFSSL_INT_H)
  34241. /* only available when including internal.h */
  34242. ExpectNotNull(sk = lookup->dirs->dir_entry);
  34243. #endif
  34244. X509_STORE_free(str);
  34245. #endif
  34246. return EXPECT_RESULT();
  34247. }
  34248. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  34249. {
  34250. EXPECT_DECLS;
  34251. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  34252. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  34253. defined(WOLFSSL_SIGNER_DER_CERT)
  34254. X509_STORE_CTX* ctx = NULL;
  34255. X509_STORE* str = NULL;
  34256. X509_LOOKUP* lookup = NULL;
  34257. X509* cert1 = NULL;
  34258. X509* x509Ca = NULL;
  34259. X509* x509Svr = NULL;
  34260. X509* issuer = NULL;
  34261. WOLFSSL_STACK* sk = NULL;
  34262. X509_NAME* caName = NULL;
  34263. X509_NAME* issuerName = NULL;
  34264. XFILE file1 = XBADFILE;
  34265. int i;
  34266. int cert_count = 0;
  34267. int cmp;
  34268. char der[] = "certs/ca-cert.der";
  34269. #ifdef HAVE_CRL
  34270. char pem[][100] = {
  34271. "./certs/crl/crl.pem",
  34272. "./certs/crl/crl2.pem",
  34273. "./certs/crl/caEccCrl.pem",
  34274. "./certs/crl/eccCliCRL.pem",
  34275. "./certs/crl/eccSrvCRL.pem",
  34276. ""
  34277. };
  34278. #endif
  34279. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  34280. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  34281. if (file1 != XBADFILE)
  34282. XFCLOSE(file1);
  34283. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34284. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34285. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34286. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  34287. SSL_FILETYPE_PEM,NULL), 1);
  34288. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  34289. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  34290. /* check if CA cert is loaded into the store */
  34291. for (i = 0; i < cert_count; i++) {
  34292. x509Ca = sk_X509_value(sk, i);
  34293. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  34294. }
  34295. ExpectNotNull((x509Svr =
  34296. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  34297. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  34298. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  34299. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  34300. ExpectNotNull(issuer);
  34301. caName = X509_get_subject_name(x509Ca);
  34302. ExpectNotNull(caName);
  34303. issuerName = X509_get_subject_name(issuer);
  34304. ExpectNotNull(issuerName);
  34305. cmp = X509_NAME_cmp(caName, issuerName);
  34306. ExpectIntEQ(cmp, 0);
  34307. /* load der format */
  34308. X509_free(issuer);
  34309. issuer = NULL;
  34310. X509_STORE_CTX_free(ctx);
  34311. ctx = NULL;
  34312. X509_STORE_free(str);
  34313. str = NULL;
  34314. sk_X509_pop_free(sk, NULL);
  34315. sk = NULL;
  34316. X509_free(x509Svr);
  34317. x509Svr = NULL;
  34318. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34319. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34320. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  34321. SSL_FILETYPE_ASN1,NULL), 1);
  34322. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  34323. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  34324. /* check if CA cert is loaded into the store */
  34325. for (i = 0; i < cert_count; i++) {
  34326. x509Ca = sk_X509_value(sk, i);
  34327. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  34328. }
  34329. X509_STORE_free(str);
  34330. str = NULL;
  34331. sk_X509_pop_free(sk, NULL);
  34332. sk = NULL;
  34333. X509_free(cert1);
  34334. cert1 = NULL;
  34335. #ifdef HAVE_CRL
  34336. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  34337. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34338. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  34339. SSL_FILETYPE_PEM,NULL), 1);
  34340. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  34341. "certs/server-revoked-cert.pem",
  34342. SSL_FILETYPE_PEM,NULL), 1);
  34343. if (str) {
  34344. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  34345. WOLFSSL_FILETYPE_PEM), 1);
  34346. /* since store hasn't yet known the revoked cert*/
  34347. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  34348. "certs/server-revoked-cert.pem",
  34349. WOLFSSL_FILETYPE_PEM), 1);
  34350. }
  34351. for (i = 0; pem[i][0] != '\0'; i++)
  34352. {
  34353. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  34354. SSL_FILETYPE_PEM, NULL), 1);
  34355. }
  34356. if (str) {
  34357. /* since store knows crl list */
  34358. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  34359. "certs/server-revoked-cert.pem",
  34360. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  34361. }
  34362. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  34363. X509_STORE_free(str);
  34364. #endif
  34365. #endif
  34366. return EXPECT_RESULT();
  34367. }
  34368. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  34369. {
  34370. int res = TEST_SKIPPED;
  34371. #if defined(OPENSSL_EXTRA)
  34372. X509_STORE_CTX_cleanup(NULL);
  34373. X509_STORE_CTX_trusted_stack(NULL, NULL);
  34374. res = TEST_SUCCESS;
  34375. #endif
  34376. return res;
  34377. }
  34378. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  34379. {
  34380. EXPECT_DECLS;
  34381. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34382. X509_STORE_CTX* ctx = NULL;
  34383. X509_STORE* str = NULL;
  34384. X509* x509Ca = NULL;
  34385. X509* x509Svr = NULL;
  34386. X509* issuer = NULL;
  34387. X509_NAME* caName = NULL;
  34388. X509_NAME* issuerName = NULL;
  34389. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34390. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34391. ExpectNotNull((x509Ca =
  34392. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  34393. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  34394. ExpectNotNull((x509Svr =
  34395. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  34396. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  34397. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  34398. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  34399. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  34400. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  34401. #ifdef WOLFSSL_SIGNER_DER_CERT
  34402. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  34403. #endif
  34404. X509_free(issuer);
  34405. X509_STORE_CTX_free(ctx);
  34406. X509_free(x509Svr);
  34407. X509_STORE_free(str);
  34408. X509_free(x509Ca);
  34409. #endif
  34410. return EXPECT_RESULT();
  34411. }
  34412. static int test_wolfSSL_PKCS7_certs(void)
  34413. {
  34414. EXPECT_DECLS;
  34415. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  34416. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  34417. STACK_OF(X509)* sk = NULL;
  34418. STACK_OF(X509_INFO)* info_sk = NULL;
  34419. PKCS7 *p7 = NULL;
  34420. BIO* bio = NULL;
  34421. const byte* p = NULL;
  34422. int buflen = 0;
  34423. int i;
  34424. /* Test twice. Once with d2i and once without to test
  34425. * that everything is free'd correctly. */
  34426. for (i = 0; i < 2; i++) {
  34427. ExpectNotNull(p7 = PKCS7_new());
  34428. if (p7 != NULL) {
  34429. p7->version = 1;
  34430. #ifdef NO_SHA
  34431. p7->hashOID = SHA256h;
  34432. #else
  34433. p7->hashOID = SHAh;
  34434. #endif
  34435. }
  34436. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  34437. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  34438. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  34439. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  34440. ExpectNotNull(sk = sk_X509_new_null());
  34441. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  34442. X509_INFO* info = NULL;
  34443. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  34444. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  34445. if (EXPECT_SUCCESS() && (info != NULL)) {
  34446. info->x509 = NULL;
  34447. }
  34448. X509_INFO_free(info);
  34449. }
  34450. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  34451. info_sk = NULL;
  34452. BIO_free(bio);
  34453. bio = NULL;
  34454. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34455. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  34456. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  34457. sk_X509_pop_free(sk, X509_free);
  34458. }
  34459. sk = NULL;
  34460. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  34461. if (i == 0) {
  34462. PKCS7_free(p7);
  34463. p7 = NULL;
  34464. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  34465. if (p7 != NULL) {
  34466. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  34467. * them */
  34468. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  34469. }
  34470. /* PKCS7_free free's the certs */
  34471. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  34472. }
  34473. BIO_free(bio);
  34474. bio = NULL;
  34475. PKCS7_free(p7);
  34476. p7 = NULL;
  34477. }
  34478. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  34479. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  34480. return EXPECT_RESULT();
  34481. }
  34482. static int test_wolfSSL_X509_STORE_CTX(void)
  34483. {
  34484. EXPECT_DECLS;
  34485. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34486. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34487. X509_STORE_CTX* ctx = NULL;
  34488. X509_STORE* str = NULL;
  34489. X509* x509 = NULL;
  34490. #ifdef OPENSSL_ALL
  34491. X509* x5092 = NULL;
  34492. STACK_OF(X509) *sk = NULL;
  34493. STACK_OF(X509) *sk2 = NULL;
  34494. STACK_OF(X509) *sk3 = NULL;
  34495. #endif
  34496. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34497. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34498. ExpectNotNull((x509 =
  34499. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  34500. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  34501. #ifdef OPENSSL_ALL
  34502. /* sk_X509_new only in OPENSSL_ALL */
  34503. sk = sk_X509_new_null();
  34504. ExpectNotNull(sk);
  34505. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  34506. #else
  34507. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  34508. #endif
  34509. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  34510. X509_STORE_CTX_set_error(ctx, -5);
  34511. X509_STORE_CTX_set_error(NULL, -5);
  34512. X509_STORE_CTX_free(ctx);
  34513. ctx = NULL;
  34514. #ifdef OPENSSL_ALL
  34515. sk_X509_pop_free(sk, NULL);
  34516. sk = NULL;
  34517. #endif
  34518. X509_STORE_free(str);
  34519. str = NULL;
  34520. X509_free(x509);
  34521. x509 = NULL;
  34522. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34523. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  34524. X509_STORE_CTX_free(ctx);
  34525. ctx = NULL;
  34526. #ifdef OPENSSL_ALL
  34527. /* test X509_STORE_CTX_get(1)_chain */
  34528. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  34529. SSL_FILETYPE_PEM)));
  34530. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  34531. SSL_FILETYPE_PEM)));
  34532. ExpectNotNull((sk = sk_X509_new_null()));
  34533. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  34534. if (EXPECT_FAIL()) {
  34535. X509_free(x509);
  34536. x509 = NULL;
  34537. }
  34538. ExpectNotNull((str = X509_STORE_new()));
  34539. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  34540. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  34541. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  34542. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  34543. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  34544. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  34545. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  34546. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  34547. X509_STORE_CTX_free(ctx);
  34548. ctx = NULL;
  34549. X509_STORE_free(str);
  34550. str = NULL;
  34551. /* CTX certs not freed yet */
  34552. X509_free(x5092);
  34553. x5092 = NULL;
  34554. sk_X509_pop_free(sk, NULL);
  34555. sk = NULL;
  34556. /* sk3 is dup so free here */
  34557. sk_X509_pop_free(sk3, NULL);
  34558. sk3 = NULL;
  34559. #endif
  34560. /* test X509_STORE_CTX_get/set_ex_data */
  34561. {
  34562. int i = 0, tmpData = 5;
  34563. void* tmpDataRet;
  34564. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34565. #ifdef HAVE_EX_DATA
  34566. for (i = 0; i < MAX_EX_DATA; i++) {
  34567. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  34568. WOLFSSL_SUCCESS);
  34569. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  34570. ExpectNotNull(tmpDataRet);
  34571. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  34572. }
  34573. #else
  34574. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  34575. WOLFSSL_FAILURE);
  34576. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  34577. ExpectNull(tmpDataRet);
  34578. #endif
  34579. X509_STORE_CTX_free(ctx);
  34580. ctx = NULL;
  34581. }
  34582. /* test X509_STORE_get/set_ex_data */
  34583. {
  34584. int i = 0, tmpData = 99;
  34585. void* tmpDataRet;
  34586. ExpectNotNull(str = X509_STORE_new());
  34587. #ifdef HAVE_EX_DATA
  34588. for (i = 0; i < MAX_EX_DATA; i++) {
  34589. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  34590. WOLFSSL_SUCCESS);
  34591. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  34592. ExpectNotNull(tmpDataRet);
  34593. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  34594. }
  34595. #else
  34596. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  34597. WOLFSSL_FAILURE);
  34598. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  34599. ExpectNull(tmpDataRet);
  34600. #endif
  34601. X509_STORE_free(str);
  34602. str = NULL;
  34603. }
  34604. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34605. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34606. return EXPECT_RESULT();
  34607. }
  34608. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34609. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  34610. STACK_OF(X509)* chain)
  34611. {
  34612. EXPECT_DECLS;
  34613. XFILE fp = XBADFILE;
  34614. X509* cert = NULL;
  34615. ExpectTrue((fp = XFOPEN(filename, "rb"))
  34616. != XBADFILE);
  34617. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  34618. if (fp != XBADFILE) {
  34619. XFCLOSE(fp);
  34620. fp = XBADFILE;
  34621. }
  34622. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  34623. if (EXPECT_FAIL())
  34624. X509_free(cert);
  34625. return EXPECT_RESULT();
  34626. }
  34627. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  34628. int err, int loadCA)
  34629. {
  34630. EXPECT_DECLS;
  34631. X509_STORE_CTX* ctx = NULL;
  34632. X509_STORE* str = NULL;
  34633. XFILE fp = XBADFILE;
  34634. X509* cert = NULL;
  34635. STACK_OF(X509)* untrusted = NULL;
  34636. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  34637. != XBADFILE);
  34638. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  34639. if (fp != XBADFILE) {
  34640. XFCLOSE(fp);
  34641. fp = XBADFILE;
  34642. }
  34643. ExpectNotNull(str = X509_STORE_new());
  34644. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34645. ExpectNotNull(untrusted = sk_X509_new_null());
  34646. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  34647. if (loadCA) {
  34648. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  34649. 1);
  34650. }
  34651. for (; *filenames; filenames++) {
  34652. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  34653. untrusted), TEST_SUCCESS);
  34654. }
  34655. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  34656. ExpectIntEQ(X509_verify_cert(ctx), ret);
  34657. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  34658. X509_free(cert);
  34659. X509_STORE_free(str);
  34660. X509_STORE_CTX_free(ctx);
  34661. sk_X509_pop_free(untrusted, NULL);
  34662. return EXPECT_RESULT();
  34663. }
  34664. #endif
  34665. static int test_X509_STORE_untrusted(void)
  34666. {
  34667. EXPECT_DECLS;
  34668. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34669. const char* untrusted1[] = {
  34670. "./certs/intermediate/ca-int2-cert.pem",
  34671. NULL
  34672. };
  34673. const char* untrusted2[] = {
  34674. "./certs/intermediate/ca-int-cert.pem",
  34675. "./certs/intermediate/ca-int2-cert.pem",
  34676. NULL
  34677. };
  34678. const char* untrusted3[] = {
  34679. "./certs/intermediate/ca-int-cert.pem",
  34680. "./certs/intermediate/ca-int2-cert.pem",
  34681. "./certs/ca-cert.pem",
  34682. NULL
  34683. };
  34684. /* Adding unrelated certs that should be ignored */
  34685. const char* untrusted4[] = {
  34686. "./certs/client-ca.pem",
  34687. "./certs/intermediate/ca-int-cert.pem",
  34688. "./certs/server-cert.pem",
  34689. "./certs/intermediate/ca-int2-cert.pem",
  34690. NULL
  34691. };
  34692. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  34693. * to loaded CA. */
  34694. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  34695. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  34696. /* Succeeds because path to loaded CA is available. */
  34697. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  34698. TEST_SUCCESS);
  34699. /* Fails because root CA is in the untrusted stack */
  34700. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  34701. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  34702. /* Succeeds because path to loaded CA is available. */
  34703. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  34704. TEST_SUCCESS);
  34705. #endif
  34706. return EXPECT_RESULT();
  34707. }
  34708. static int test_wolfSSL_X509_STORE_set_flags(void)
  34709. {
  34710. EXPECT_DECLS;
  34711. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34712. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34713. X509_STORE* store = NULL;
  34714. X509* x509 = NULL;
  34715. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  34716. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  34717. WOLFSSL_FILETYPE_PEM)));
  34718. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  34719. #ifdef HAVE_CRL
  34720. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  34721. WOLFSSL_SUCCESS);
  34722. #else
  34723. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  34724. NOT_COMPILED_IN);
  34725. #endif
  34726. wolfSSL_X509_free(x509);
  34727. wolfSSL_X509_STORE_free(store);
  34728. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  34729. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34730. return EXPECT_RESULT();
  34731. }
  34732. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  34733. {
  34734. EXPECT_DECLS;
  34735. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  34736. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  34737. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  34738. WOLFSSL_X509_STORE* store = NULL;
  34739. WOLFSSL_X509_LOOKUP* lookup = NULL;
  34740. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  34741. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  34742. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  34743. X509_FILETYPE_PEM), 1);
  34744. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  34745. X509_FILETYPE_PEM), 1);
  34746. if (store != NULL) {
  34747. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  34748. WOLFSSL_FILETYPE_PEM), 1);
  34749. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  34750. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  34751. }
  34752. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  34753. X509_FILETYPE_PEM), 1);
  34754. if (store != NULL) {
  34755. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  34756. WOLFSSL_FILETYPE_PEM), 1);
  34757. }
  34758. wolfSSL_X509_STORE_free(store);
  34759. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  34760. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34761. return EXPECT_RESULT();
  34762. }
  34763. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  34764. {
  34765. EXPECT_DECLS;
  34766. #if defined(OPENSSL_EXTRA)
  34767. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  34768. time_t c_time;
  34769. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  34770. c_time = 365*24*60*60;
  34771. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  34772. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  34773. WOLFSSL_USE_CHECK_TIME);
  34774. ExpectTrue(ctx->param->check_time == c_time);
  34775. wolfSSL_X509_STORE_CTX_free(ctx);
  34776. #endif /* OPENSSL_EXTRA */
  34777. return EXPECT_RESULT();
  34778. }
  34779. static int test_wolfSSL_CTX_get0_set1_param(void)
  34780. {
  34781. EXPECT_DECLS;
  34782. #if defined(OPENSSL_EXTRA)
  34783. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34784. SSL_CTX* ctx = NULL;
  34785. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  34786. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  34787. char testIPv4[] = "127.0.0.1";
  34788. char testhostName[] = "foo.hoge.com";
  34789. #ifndef NO_WOLFSSL_SERVER
  34790. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34791. #else
  34792. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34793. #endif
  34794. ExpectNull(SSL_CTX_get0_param(NULL));
  34795. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  34796. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  34797. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  34798. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  34799. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  34800. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  34801. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  34802. WOLFSSL_SUCCESS);
  34803. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  34804. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  34805. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  34806. (int)XSTRLEN(testhostName)));
  34807. ExpectIntEQ(0x01, pParam->hostFlags);
  34808. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  34809. /* test for incorrect parameter */
  34810. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  34811. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  34812. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  34813. SSL_CTX_free(ctx);
  34814. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  34815. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34816. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  34817. return EXPECT_RESULT();
  34818. }
  34819. static int test_wolfSSL_get0_param(void)
  34820. {
  34821. EXPECT_DECLS;
  34822. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34823. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34824. SSL_CTX* ctx = NULL;
  34825. SSL* ssl = NULL;
  34826. #ifndef NO_WOLFSSL_SERVER
  34827. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34828. #else
  34829. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34830. #endif
  34831. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34832. SSL_FILETYPE_PEM));
  34833. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34834. ExpectNotNull(ssl = SSL_new(ctx));
  34835. ExpectNotNull(SSL_get0_param(ssl));
  34836. SSL_free(ssl);
  34837. SSL_CTX_free(ctx);
  34838. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34839. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  34840. return EXPECT_RESULT();
  34841. }
  34842. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  34843. {
  34844. EXPECT_DECLS;
  34845. #if defined(OPENSSL_EXTRA)
  34846. const char host[] = "www.example.com";
  34847. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  34848. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  34849. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34850. if (pParam != NULL) {
  34851. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  34852. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  34853. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34854. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  34855. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34856. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34857. }
  34858. #endif /* OPENSSL_EXTRA */
  34859. return EXPECT_RESULT();
  34860. }
  34861. static int test_wolfSSL_set1_host(void)
  34862. {
  34863. EXPECT_DECLS;
  34864. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34865. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34866. const char host[] = "www.test_wolfSSL_set1_host.com";
  34867. const char emptyStr[] = "";
  34868. SSL_CTX* ctx = NULL;
  34869. SSL* ssl = NULL;
  34870. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  34871. #ifndef NO_WOLFSSL_SERVER
  34872. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34873. #else
  34874. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34875. #endif
  34876. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34877. SSL_FILETYPE_PEM));
  34878. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34879. ExpectNotNull(ssl = SSL_new(ctx));
  34880. pParam = SSL_get0_param(ssl);
  34881. /* we should get back host string */
  34882. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  34883. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34884. /* we should get back empty string */
  34885. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  34886. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  34887. /* we should get back host string */
  34888. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  34889. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34890. /* we should get back empty string */
  34891. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  34892. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  34893. SSL_free(ssl);
  34894. SSL_CTX_free(ctx);
  34895. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34896. #endif /* OPENSSL_EXTRA */
  34897. return EXPECT_RESULT();
  34898. }
  34899. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  34900. {
  34901. EXPECT_DECLS;
  34902. #if defined(OPENSSL_EXTRA)
  34903. unsigned char buf[16] = {0};
  34904. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  34905. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  34906. /* test 127.0.0.1 */
  34907. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  34908. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  34909. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  34910. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  34911. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34912. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  34913. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  34914. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  34915. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34916. ExpectIntEQ(XSTRNCMP(param->ipasc,
  34917. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  34918. /* test 2001:db8:: */
  34919. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34920. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  34921. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  34922. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  34923. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34924. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  34925. /* test ::1234:5678 */
  34926. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  34927. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  34928. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  34929. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  34930. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34931. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  34932. /* test 2001:db8::1234:5678 */
  34933. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34934. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  34935. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  34936. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  34937. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34938. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  34939. sizeof(param->ipasc)), 0);
  34940. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  34941. /* 2001:db8:1::ab9:c0a8:102 */
  34942. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34943. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  34944. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  34945. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  34946. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34947. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  34948. sizeof(param->ipasc)), 0);
  34949. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34950. #endif /* OPENSSL_EXTRA */
  34951. return EXPECT_RESULT();
  34952. }
  34953. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  34954. {
  34955. EXPECT_DECLS;
  34956. #if defined(OPENSSL_EXTRA)
  34957. X509_STORE* store = NULL;
  34958. X509_STORE_CTX* ctx = NULL;
  34959. X509_STORE_CTX* ctx_no_init = NULL;
  34960. ExpectNotNull((store = X509_STORE_new()));
  34961. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34962. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  34963. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  34964. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  34965. /* should return NULL if ctx has not bee initialized */
  34966. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  34967. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  34968. wolfSSL_X509_STORE_CTX_free(ctx);
  34969. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  34970. X509_STORE_free(store);
  34971. #endif /* OPENSSL_EXTRA */
  34972. return EXPECT_RESULT();
  34973. }
  34974. static int test_wolfSSL_CTX_set_client_CA_list(void)
  34975. {
  34976. EXPECT_DECLS;
  34977. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  34978. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  34979. WOLFSSL_CTX* ctx = NULL;
  34980. WOLFSSL* ssl = NULL;
  34981. X509_NAME* name = NULL;
  34982. STACK_OF(X509_NAME)* names = NULL;
  34983. STACK_OF(X509_NAME)* ca_list = NULL;
  34984. int names_len = 0;
  34985. int i;
  34986. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  34987. /* Send two X501 names in cert request */
  34988. names = SSL_load_client_CA_file(cliCertFile);
  34989. ExpectNotNull(names);
  34990. ca_list = SSL_load_client_CA_file(caCertFile);
  34991. ExpectNotNull(ca_list);
  34992. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  34993. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  34994. if (EXPECT_FAIL()) {
  34995. wolfSSL_X509_NAME_free(name);
  34996. name = NULL;
  34997. }
  34998. SSL_CTX_set_client_CA_list(ctx, names);
  34999. /* This should only free the stack structure */
  35000. sk_X509_NAME_free(ca_list);
  35001. ca_list = NULL;
  35002. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  35003. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  35004. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  35005. for (i = 0; i < names_len; i++) {
  35006. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  35007. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  35008. }
  35009. /* Needed to be able to create ssl object */
  35010. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35011. SSL_FILETYPE_PEM));
  35012. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35013. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35014. /* load again as old names are responsibility of ctx to free*/
  35015. names = SSL_load_client_CA_file(cliCertFile);
  35016. ExpectNotNull(names);
  35017. SSL_set_client_CA_list(ssl, names);
  35018. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  35019. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  35020. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  35021. for (i = 0; i < names_len; i++) {
  35022. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  35023. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  35024. }
  35025. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  35026. {
  35027. tcp_ready ready;
  35028. func_args server_args;
  35029. callback_functions server_cb;
  35030. THREAD_TYPE serverThread;
  35031. WOLFSSL* ssl_client = NULL;
  35032. WOLFSSL_CTX* ctx_client = NULL;
  35033. SOCKET_T sockfd = 0;
  35034. /* wolfSSL_get_client_CA_list() with handshake */
  35035. StartTCP();
  35036. InitTcpReady(&ready);
  35037. XMEMSET(&server_args, 0, sizeof(func_args));
  35038. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  35039. server_args.signal = &ready;
  35040. server_args.callbacks = &server_cb;
  35041. /* we are responsible for free'ing WOLFSSL_CTX */
  35042. server_cb.ctx = ctx;
  35043. server_cb.isSharedCtx = 1;
  35044. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  35045. cliCertFile, 0));
  35046. start_thread(test_server_nofail, &server_args, &serverThread);
  35047. wait_tcp_ready(&server_args);
  35048. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  35049. ExpectNotNull(ctx_client =
  35050. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  35051. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  35052. ctx_client, caCertFile, 0));
  35053. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  35054. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  35055. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  35056. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  35057. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  35058. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  35059. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  35060. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  35061. /* We are expecting two cert names to be sent */
  35062. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  35063. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  35064. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  35065. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  35066. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  35067. }
  35068. wolfSSL_shutdown(ssl_client);
  35069. wolfSSL_free(ssl_client);
  35070. wolfSSL_CTX_free(ctx_client);
  35071. CloseSocket(sockfd);
  35072. join_thread(serverThread);
  35073. FreeTcpReady(&ready);
  35074. }
  35075. #endif
  35076. wolfSSL_free(ssl);
  35077. wolfSSL_CTX_free(ctx);
  35078. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  35079. * !NO_BIO */
  35080. return EXPECT_RESULT();
  35081. }
  35082. static int test_wolfSSL_CTX_add_client_CA(void)
  35083. {
  35084. EXPECT_DECLS;
  35085. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  35086. !defined(NO_WOLFSSL_CLIENT)
  35087. WOLFSSL_CTX* ctx = NULL;
  35088. WOLFSSL_X509* x509 = NULL;
  35089. WOLFSSL_X509* x509_a = NULL;
  35090. STACK_OF(X509_NAME)* ca_list = NULL;
  35091. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35092. /* Add client cert */
  35093. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  35094. SSL_FILETYPE_PEM));
  35095. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  35096. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  35097. /* Add another client cert */
  35098. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  35099. SSL_FILETYPE_PEM));
  35100. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  35101. /* test for incorrect parameter */
  35102. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  35103. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  35104. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  35105. X509_free(x509);
  35106. X509_free(x509_a);
  35107. SSL_CTX_free(ctx);
  35108. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  35109. return EXPECT_RESULT();
  35110. }
  35111. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  35112. defined(HAVE_IO_TESTS_DEPENDENCIES)
  35113. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  35114. {
  35115. callback_functions* callbacks = ((func_args*)args)->callbacks;
  35116. WOLFSSL_CTX* ctx = callbacks->ctx;
  35117. WOLFSSL* ssl = NULL;
  35118. SOCKET_T sfd = 0;
  35119. SOCKET_T cfd = 0;
  35120. word16 port;
  35121. char input[1024];
  35122. int idx;
  35123. int ret, err = 0;
  35124. const char* privateName = "ech-private-name.com";
  35125. int privateNameLen = (int)XSTRLEN(privateName);
  35126. ((func_args*)args)->return_code = TEST_FAIL;
  35127. port = ((func_args*)args)->signal->port;
  35128. AssertIntEQ(WOLFSSL_SUCCESS,
  35129. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  35130. AssertIntEQ(WOLFSSL_SUCCESS,
  35131. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35132. WOLFSSL_FILETYPE_PEM));
  35133. AssertIntEQ(WOLFSSL_SUCCESS,
  35134. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35135. WOLFSSL_FILETYPE_PEM));
  35136. if (callbacks->ctx_ready)
  35137. callbacks->ctx_ready(ctx);
  35138. ssl = wolfSSL_new(ctx);
  35139. /* set the sni for the server */
  35140. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  35141. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  35142. CloseSocket(sfd);
  35143. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  35144. if (callbacks->ssl_ready)
  35145. callbacks->ssl_ready(ssl);
  35146. do {
  35147. err = 0; /* Reset error */
  35148. ret = wolfSSL_accept(ssl);
  35149. if (ret != WOLFSSL_SUCCESS) {
  35150. err = wolfSSL_get_error(ssl, 0);
  35151. }
  35152. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  35153. if (ret != WOLFSSL_SUCCESS) {
  35154. char buff[WOLFSSL_MAX_ERROR_SZ];
  35155. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  35156. }
  35157. else {
  35158. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  35159. input[idx] = 0;
  35160. fprintf(stderr, "Client message: %s\n", input);
  35161. }
  35162. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  35163. privateNameLen));
  35164. ((func_args*)args)->return_code = TEST_SUCCESS;
  35165. }
  35166. if (callbacks->on_result)
  35167. callbacks->on_result(ssl);
  35168. wolfSSL_shutdown(ssl);
  35169. wolfSSL_free(ssl);
  35170. wolfSSL_CTX_free(ctx);
  35171. CloseSocket(cfd);
  35172. #ifdef FP_ECC
  35173. wc_ecc_fp_free();
  35174. #endif
  35175. WOLFSSL_RETURN_FROM_THREAD(0);
  35176. }
  35177. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  35178. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  35179. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  35180. {
  35181. AssertNotNull(ssl);
  35182. AssertNotNull(line);
  35183. XFILE fp;
  35184. const byte lf = '\n';
  35185. fp = XFOPEN("./MyKeyLog.txt", "a");
  35186. XFWRITE( line, 1, strlen(line),fp);
  35187. XFWRITE( (void*)&lf,1,1,fp);
  35188. XFFLUSH(fp);
  35189. XFCLOSE(fp);
  35190. }
  35191. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  35192. static int test_wolfSSL_CTX_set_keylog_callback(void)
  35193. {
  35194. EXPECT_DECLS;
  35195. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  35196. !defined(NO_WOLFSSL_CLIENT)
  35197. SSL_CTX* ctx = NULL;
  35198. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35199. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  35200. SSL_CTX_free(ctx);
  35201. SSL_CTX_set_keylog_callback(NULL, NULL);
  35202. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  35203. return EXPECT_RESULT();
  35204. }
  35205. static int test_wolfSSL_CTX_get_keylog_callback(void)
  35206. {
  35207. EXPECT_DECLS;
  35208. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  35209. !defined(NO_WOLFSSL_CLIENT)
  35210. SSL_CTX* ctx = NULL;
  35211. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35212. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  35213. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  35214. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  35215. SSL_CTX_set_keylog_callback(ctx, NULL );
  35216. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  35217. SSL_CTX_free(ctx);
  35218. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  35219. return EXPECT_RESULT();
  35220. }
  35221. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  35222. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  35223. {
  35224. /* set keylog callback */
  35225. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  35226. return TEST_SUCCESS;
  35227. }
  35228. #endif
  35229. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  35230. {
  35231. EXPECT_DECLS;
  35232. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  35233. /* This test is intended for checking whether keylog callback is called
  35234. * in client during TLS handshake between the client and a server.
  35235. */
  35236. test_ssl_cbf server_cbf;
  35237. test_ssl_cbf client_cbf;
  35238. XFILE fp = XBADFILE;
  35239. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  35240. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  35241. server_cbf.method = wolfTLSv1_2_server_method;
  35242. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  35243. /* clean up keylog file */
  35244. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  35245. if (fp != XBADFILE) {
  35246. XFFLUSH(fp);
  35247. XFCLOSE(fp);
  35248. fp = XBADFILE;
  35249. }
  35250. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  35251. &server_cbf, NULL), TEST_SUCCESS);
  35252. XSLEEP_MS(100);
  35253. /* check if the keylog file exists */
  35254. char buff[300] = {0};
  35255. int found = 0;
  35256. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  35257. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  35258. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  35259. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  35260. found = 1;
  35261. break;
  35262. }
  35263. }
  35264. if (fp != XBADFILE) {
  35265. XFCLOSE(fp);
  35266. }
  35267. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  35268. ExpectIntEQ(found, 1);
  35269. /* clean up */
  35270. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  35271. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  35272. return EXPECT_RESULT();
  35273. }
  35274. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  35275. defined(HAVE_SECRET_CALLBACK)
  35276. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  35277. {
  35278. /* set keylog callback */
  35279. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  35280. return TEST_SUCCESS;
  35281. }
  35282. #endif
  35283. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  35284. {
  35285. EXPECT_DECLS;
  35286. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  35287. defined(HAVE_SECRET_CALLBACK)
  35288. /* This test is intended for checking whether keylog callback is called
  35289. * in client during TLS handshake between the client and a server.
  35290. */
  35291. test_ssl_cbf server_cbf;
  35292. test_ssl_cbf client_cbf;
  35293. XFILE fp = XBADFILE;
  35294. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  35295. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  35296. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  35297. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  35298. /* clean up keylog file */
  35299. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  35300. if (fp != XBADFILE) {
  35301. XFCLOSE(fp);
  35302. fp = XBADFILE;
  35303. }
  35304. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  35305. &server_cbf, NULL), TEST_SUCCESS);
  35306. /* check if the keylog file exists */
  35307. {
  35308. char buff[300] = {0};
  35309. int found[4] = {0};
  35310. int numfnd = 0;
  35311. int i;
  35312. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  35313. while (EXPECT_SUCCESS() &&
  35314. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  35315. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  35316. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  35317. found[0] = 1;
  35318. continue;
  35319. }
  35320. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  35321. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  35322. found[1] = 1;
  35323. continue;
  35324. }
  35325. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  35326. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  35327. found[2] = 1;
  35328. continue;
  35329. }
  35330. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  35331. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  35332. found[3] = 1;
  35333. continue;
  35334. }
  35335. }
  35336. if (fp != XBADFILE)
  35337. XFCLOSE(fp);
  35338. for (i = 0; i < 4; i++) {
  35339. if (found[i] != 0)
  35340. numfnd++;
  35341. }
  35342. ExpectIntEQ(numfnd, 4);
  35343. }
  35344. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  35345. return EXPECT_RESULT();
  35346. }
  35347. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  35348. defined(HAVE_IO_TESTS_DEPENDENCIES)
  35349. static int test_wolfSSL_Tls13_ECH_params(void)
  35350. {
  35351. EXPECT_DECLS;
  35352. #if !defined(NO_WOLFSSL_CLIENT)
  35353. word32 outputLen = 0;
  35354. byte testBuf[72];
  35355. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  35356. WOLFSSL *ssl = wolfSSL_new(ctx);
  35357. ExpectNotNull(ctx);
  35358. ExpectNotNull(ssl);
  35359. /* invalid ctx */
  35360. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  35361. "ech-public-name.com", 0, 0, 0));
  35362. /* invalid public name */
  35363. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  35364. 0, 0));
  35365. /* invalid algorithms */
  35366. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  35367. "ech-public-name.com", 1000, 1000, 1000));
  35368. /* invalid ctx */
  35369. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  35370. &outputLen));
  35371. /* invalid output len */
  35372. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  35373. /* invalid ssl */
  35374. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  35375. (char*)testBuf, sizeof(testBuf)));
  35376. /* invalid configs64 */
  35377. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  35378. sizeof(testBuf)));
  35379. /* invalid size */
  35380. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  35381. (char*)testBuf, 0));
  35382. /* invalid ssl */
  35383. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  35384. sizeof(testBuf)));
  35385. /* invalid configs */
  35386. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  35387. sizeof(testBuf)));
  35388. /* invalid size */
  35389. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  35390. /* invalid ssl */
  35391. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  35392. /* invalid size */
  35393. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  35394. wolfSSL_free(ssl);
  35395. wolfSSL_CTX_free(ctx);
  35396. #endif /* !NO_WOLFSSL_CLIENT */
  35397. return EXPECT_RESULT();
  35398. }
  35399. static int test_wolfSSL_Tls13_ECH(void)
  35400. {
  35401. EXPECT_DECLS;
  35402. tcp_ready ready;
  35403. func_args client_args;
  35404. func_args server_args;
  35405. THREAD_TYPE serverThread;
  35406. callback_functions server_cbf;
  35407. callback_functions client_cbf;
  35408. SOCKET_T sockfd = 0;
  35409. WOLFSSL_CTX* ctx = NULL;
  35410. WOLFSSL* ssl = NULL;
  35411. const char* publicName = "ech-public-name.com";
  35412. const char* privateName = "ech-private-name.com";
  35413. int privateNameLen = 20;
  35414. char reply[1024];
  35415. int replyLen = 0;
  35416. byte rawEchConfig[128];
  35417. word32 rawEchConfigLen = sizeof(rawEchConfig);
  35418. InitTcpReady(&ready);
  35419. ready.port = 22222;
  35420. XMEMSET(&client_args, 0, sizeof(func_args));
  35421. XMEMSET(&server_args, 0, sizeof(func_args));
  35422. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  35423. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  35424. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  35425. /* create the server context here so we can get the ech config */
  35426. ExpectNotNull(server_cbf.ctx =
  35427. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  35428. /* generate ech config */
  35429. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  35430. publicName, 0, 0, 0));
  35431. /* get the config for the client to use */
  35432. ExpectIntEQ(WOLFSSL_SUCCESS,
  35433. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  35434. &rawEchConfigLen));
  35435. server_args.callbacks = &server_cbf;
  35436. server_args.signal = &ready;
  35437. /* start server task */
  35438. start_thread(server_task_ech, &server_args, &serverThread);
  35439. wait_tcp_ready(&server_args);
  35440. /* run as a TLS1.3 client */
  35441. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  35442. ExpectIntEQ(WOLFSSL_SUCCESS,
  35443. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35444. ExpectIntEQ(WOLFSSL_SUCCESS,
  35445. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35446. ExpectIntEQ(WOLFSSL_SUCCESS,
  35447. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35448. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  35449. /* get connected the server task */
  35450. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35451. /* set the ech configs for the client */
  35452. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  35453. rawEchConfigLen));
  35454. /* set the sni for the client */
  35455. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  35456. privateName, privateNameLen));
  35457. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35458. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  35459. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  35460. privateNameLen);
  35461. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  35462. /* add th null terminator for string compare */
  35463. reply[replyLen] = 0;
  35464. /* check that the server replied with the private name */
  35465. ExpectStrEQ(privateName, reply);
  35466. wolfSSL_free(ssl);
  35467. wolfSSL_CTX_free(ctx);
  35468. CloseSocket(sockfd);
  35469. join_thread(serverThread);
  35470. FreeTcpReady(&ready);
  35471. return EXPECT_RESULT();
  35472. }
  35473. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  35474. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35475. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35476. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  35477. static int post_auth_version_cb(WOLFSSL* ssl)
  35478. {
  35479. EXPECT_DECLS;
  35480. /* do handshake and then test version error */
  35481. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  35482. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  35483. return EXPECT_RESULT();
  35484. }
  35485. static int post_auth_version_client_cb(WOLFSSL* ssl)
  35486. {
  35487. EXPECT_DECLS;
  35488. /* do handshake and then test version error */
  35489. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  35490. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  35491. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  35492. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  35493. /* check was added to error queue */
  35494. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  35495. /* check the string matches expected string */
  35496. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  35497. "WRONG_SSL_VERSION");
  35498. #endif
  35499. return EXPECT_RESULT();
  35500. }
  35501. static int post_auth_cb(WOLFSSL* ssl)
  35502. {
  35503. EXPECT_DECLS;
  35504. WOLFSSL_X509* x509 = NULL;
  35505. /* do handshake and then test version error */
  35506. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  35507. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  35508. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  35509. wolfSSL_X509_free(x509);
  35510. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  35511. return EXPECT_RESULT();
  35512. }
  35513. static int set_post_auth_cb(WOLFSSL* ssl)
  35514. {
  35515. if (!wolfSSL_is_server(ssl)) {
  35516. EXPECT_DECLS;
  35517. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  35518. return EXPECT_RESULT();
  35519. }
  35520. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  35521. return TEST_SUCCESS;
  35522. }
  35523. #endif
  35524. static int test_wolfSSL_Tls13_postauth(void)
  35525. {
  35526. EXPECT_DECLS;
  35527. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35528. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35529. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  35530. test_ssl_cbf server_cbf;
  35531. test_ssl_cbf client_cbf;
  35532. /* test version failure doing post auth with TLS 1.2 connection */
  35533. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  35534. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  35535. server_cbf.method = wolfTLSv1_2_server_method;
  35536. server_cbf.ssl_ready = set_post_auth_cb;
  35537. server_cbf.on_result = post_auth_version_cb;
  35538. client_cbf.ssl_ready = set_post_auth_cb;
  35539. client_cbf.on_result = post_auth_version_client_cb;
  35540. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  35541. &server_cbf, NULL), TEST_SUCCESS);
  35542. /* tests on post auth with TLS 1.3 */
  35543. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  35544. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  35545. server_cbf.method = wolfTLSv1_3_server_method;
  35546. server_cbf.ssl_ready = set_post_auth_cb;
  35547. client_cbf.ssl_ready = set_post_auth_cb;
  35548. server_cbf.on_result = post_auth_cb;
  35549. client_cbf.on_result = NULL;
  35550. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  35551. &server_cbf, NULL), TEST_SUCCESS);
  35552. #endif
  35553. return EXPECT_RESULT();
  35554. }
  35555. static int test_wolfSSL_X509_NID(void)
  35556. {
  35557. EXPECT_DECLS;
  35558. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  35559. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  35560. int sigType;
  35561. int nameSz;
  35562. X509* cert = NULL;
  35563. EVP_PKEY* pubKeyTmp = NULL;
  35564. X509_NAME* name = NULL;
  35565. char commonName[80];
  35566. char countryName[80];
  35567. char localityName[80];
  35568. char stateName[80];
  35569. char orgName[80];
  35570. char orgUnit[80];
  35571. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  35572. /* convert cert from DER to internal WOLFSSL_X509 struct */
  35573. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  35574. sizeof_client_cert_der_2048, HEAP_HINT));
  35575. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  35576. /* extract PUBLIC KEY from cert */
  35577. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  35578. /* extract signatureType */
  35579. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  35580. /* extract subjectName info */
  35581. ExpectNotNull(name = X509_get_subject_name(cert));
  35582. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  35583. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  35584. NULL, 0)), 0);
  35585. ExpectIntEQ(nameSz, 15);
  35586. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  35587. commonName, sizeof(commonName))), 0);
  35588. ExpectIntEQ(nameSz, 15);
  35589. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  35590. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  35591. commonName, 9)), 0);
  35592. ExpectIntEQ(nameSz, 8);
  35593. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  35594. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  35595. countryName, sizeof(countryName))), 0);
  35596. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  35597. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  35598. localityName, sizeof(localityName))), 0);
  35599. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  35600. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  35601. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  35602. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  35603. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  35604. orgName, sizeof(orgName))), 0);
  35605. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  35606. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  35607. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  35608. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  35609. EVP_PKEY_free(pubKeyTmp);
  35610. X509_free(cert);
  35611. #endif
  35612. return EXPECT_RESULT();
  35613. }
  35614. static int test_wolfSSL_CTX_set_srp_username(void)
  35615. {
  35616. EXPECT_DECLS;
  35617. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  35618. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  35619. WOLFSSL_CTX* ctx = NULL;
  35620. WOLFSSL* ssl = NULL;
  35621. const char *username = "TESTUSER";
  35622. const char *password = "TESTPASSWORD";
  35623. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35624. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  35625. SSL_SUCCESS);
  35626. wolfSSL_CTX_free(ctx);
  35627. ctx = NULL;
  35628. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35629. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  35630. SSL_SUCCESS);
  35631. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  35632. SSL_SUCCESS);
  35633. ExpectNotNull(ssl = SSL_new(ctx));
  35634. ExpectNotNull(SSL_get_srp_username(ssl));
  35635. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  35636. wolfSSL_free(ssl);
  35637. wolfSSL_CTX_free(ctx);
  35638. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  35639. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  35640. return EXPECT_RESULT();
  35641. }
  35642. static int test_wolfSSL_CTX_set_srp_password(void)
  35643. {
  35644. EXPECT_DECLS;
  35645. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  35646. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  35647. WOLFSSL_CTX* ctx = NULL;
  35648. const char *username = "TESTUSER";
  35649. const char *password = "TESTPASSWORD";
  35650. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35651. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  35652. SSL_SUCCESS);
  35653. wolfSSL_CTX_free(ctx);
  35654. ctx = NULL;
  35655. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35656. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  35657. SSL_SUCCESS);
  35658. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  35659. SSL_SUCCESS);
  35660. wolfSSL_CTX_free(ctx);
  35661. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  35662. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  35663. return EXPECT_RESULT();
  35664. }
  35665. static int test_wolfSSL_X509_STORE(void)
  35666. {
  35667. EXPECT_DECLS;
  35668. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  35669. X509_STORE *store = NULL;
  35670. #ifdef HAVE_CRL
  35671. X509_STORE_CTX *storeCtx = NULL;
  35672. X509_CRL *crl = NULL;
  35673. X509 *ca = NULL;
  35674. X509 *cert = NULL;
  35675. const char crlPem[] = "./certs/crl/crl.revoked";
  35676. const char srvCert[] = "./certs/server-revoked-cert.pem";
  35677. const char caCert[] = "./certs/ca-cert.pem";
  35678. XFILE fp = XBADFILE;
  35679. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  35680. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  35681. SSL_FILETYPE_PEM)));
  35682. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  35683. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  35684. SSL_FILETYPE_PEM)));
  35685. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  35686. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  35687. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  35688. X509_STORE_free(store);
  35689. store = NULL;
  35690. X509_STORE_CTX_free(storeCtx);
  35691. storeCtx = NULL;
  35692. X509_free(cert);
  35693. cert = NULL;
  35694. X509_free(ca);
  35695. ca = NULL;
  35696. /* should fail to verify now after adding in CRL */
  35697. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  35698. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  35699. SSL_FILETYPE_PEM)));
  35700. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  35701. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  35702. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  35703. NULL, NULL));
  35704. if (fp != XBADFILE)
  35705. XFCLOSE(fp);
  35706. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  35707. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  35708. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  35709. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  35710. SSL_FILETYPE_PEM)));
  35711. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  35712. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  35713. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  35714. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  35715. X509_CRL_free(crl);
  35716. crl = NULL;
  35717. X509_STORE_free(store);
  35718. store = NULL;
  35719. X509_STORE_CTX_free(storeCtx);
  35720. storeCtx = NULL;
  35721. X509_free(cert);
  35722. cert = NULL;
  35723. X509_free(ca);
  35724. ca = NULL;
  35725. #endif /* HAVE_CRL */
  35726. #ifndef WOLFCRYPT_ONLY
  35727. {
  35728. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35729. SSL_CTX* ctx = NULL;
  35730. SSL* ssl = NULL;
  35731. int i;
  35732. for (i = 0; i < 2; i++) {
  35733. #ifndef NO_WOLFSSL_SERVER
  35734. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35735. #else
  35736. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35737. #endif
  35738. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  35739. SSL_CTX_set_cert_store(ctx, store);
  35740. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  35741. SSL_CTX_set_cert_store(ctx, store);
  35742. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  35743. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35744. SSL_FILETYPE_PEM), SSL_SUCCESS);
  35745. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35746. SSL_FILETYPE_PEM), SSL_SUCCESS);
  35747. ExpectNotNull(ssl = SSL_new(ctx));
  35748. if (i == 0) {
  35749. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  35750. SSL_SUCCESS);
  35751. }
  35752. else {
  35753. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  35754. #ifdef OPENSSL_ALL
  35755. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  35756. #endif
  35757. }
  35758. if (EXPECT_FAIL() || (i == 1)) {
  35759. X509_STORE_free(store);
  35760. store = NULL;
  35761. }
  35762. SSL_free(ssl);
  35763. ssl = NULL;
  35764. SSL_CTX_free(ctx);
  35765. ctx = NULL;
  35766. }
  35767. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35768. }
  35769. #endif
  35770. #endif
  35771. return EXPECT_RESULT();
  35772. }
  35773. static int test_wolfSSL_X509_STORE_load_locations(void)
  35774. {
  35775. EXPECT_DECLS;
  35776. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35777. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  35778. SSL_CTX *ctx = NULL;
  35779. X509_STORE *store = NULL;
  35780. const char ca_file[] = "./certs/ca-cert.pem";
  35781. const char client_pem_file[] = "./certs/client-cert.pem";
  35782. const char client_der_file[] = "./certs/client-cert.der";
  35783. const char ecc_file[] = "./certs/ecc-key.pem";
  35784. const char certs_path[] = "./certs/";
  35785. const char bad_path[] = "./bad-path/";
  35786. #ifdef HAVE_CRL
  35787. const char crl_path[] = "./certs/crl/";
  35788. const char crl_file[] = "./certs/crl/crl.pem";
  35789. #endif
  35790. #ifndef NO_WOLFSSL_SERVER
  35791. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  35792. #else
  35793. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  35794. #endif
  35795. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  35796. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  35797. WOLFSSL_SUCCESS);
  35798. /* Test bad arguments */
  35799. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  35800. WOLFSSL_FAILURE);
  35801. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  35802. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  35803. WOLFSSL_FAILURE);
  35804. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  35805. WOLFSSL_FAILURE);
  35806. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  35807. WOLFSSL_FAILURE);
  35808. #ifdef HAVE_CRL
  35809. /* Test with CRL */
  35810. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  35811. WOLFSSL_SUCCESS);
  35812. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  35813. WOLFSSL_SUCCESS);
  35814. #endif
  35815. /* Test with CA */
  35816. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  35817. WOLFSSL_SUCCESS);
  35818. /* Test with client_cert and certs path */
  35819. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  35820. WOLFSSL_SUCCESS);
  35821. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  35822. WOLFSSL_SUCCESS);
  35823. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  35824. /* Clear nodes */
  35825. ERR_clear_error();
  35826. #endif
  35827. SSL_CTX_free(ctx);
  35828. #endif
  35829. return EXPECT_RESULT();
  35830. }
  35831. static int test_X509_STORE_get0_objects(void)
  35832. {
  35833. EXPECT_DECLS;
  35834. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  35835. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  35836. X509_STORE *store = NULL;
  35837. X509_STORE *store_cpy = NULL;
  35838. SSL_CTX *ctx = NULL;
  35839. X509_OBJECT *obj = NULL;
  35840. STACK_OF(X509_OBJECT) *objs = NULL;
  35841. int i;
  35842. /* Setup store */
  35843. #ifndef NO_WOLFSSL_SERVER
  35844. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  35845. #else
  35846. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  35847. #endif
  35848. ExpectNotNull(store_cpy = X509_STORE_new());
  35849. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  35850. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  35851. WOLFSSL_SUCCESS);
  35852. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  35853. WOLFSSL_SUCCESS);
  35854. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  35855. WOLFSSL_SUCCESS);
  35856. #ifdef HAVE_CRL
  35857. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  35858. WOLFSSL_SUCCESS);
  35859. #endif
  35860. /* Store ready */
  35861. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  35862. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  35863. #ifdef HAVE_CRL
  35864. #ifdef WOLFSSL_SIGNER_DER_CERT
  35865. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  35866. #else
  35867. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  35868. #endif
  35869. #else
  35870. #ifdef WOLFSSL_SIGNER_DER_CERT
  35871. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  35872. #else
  35873. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  35874. #endif
  35875. #endif
  35876. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  35877. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  35878. switch (X509_OBJECT_get_type(obj)) {
  35879. case X509_LU_X509:
  35880. {
  35881. WOLFSSL_X509* x509;
  35882. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  35883. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  35884. break;
  35885. }
  35886. case X509_LU_CRL:
  35887. #ifdef HAVE_CRL
  35888. {
  35889. WOLFSSL_CRL* crl = NULL;
  35890. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  35891. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  35892. break;
  35893. }
  35894. #endif
  35895. case X509_LU_NONE:
  35896. default:
  35897. Fail(("X509_OBJECT_get_type should return x509 or crl "
  35898. "(when built with crl support)"),
  35899. ("Unrecognized X509_OBJECT type or none"));
  35900. }
  35901. }
  35902. X509_STORE_free(store_cpy);
  35903. SSL_CTX_free(ctx);
  35904. #endif
  35905. return EXPECT_RESULT();
  35906. }
  35907. static int test_wolfSSL_BN_CTX(void)
  35908. {
  35909. EXPECT_DECLS;
  35910. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35911. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35912. WOLFSSL_BN_CTX* bn_ctx = NULL;
  35913. WOLFSSL_BIGNUM* t = NULL;
  35914. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  35915. /* No implementation. */
  35916. BN_CTX_init(NULL);
  35917. ExpectNotNull(t = BN_CTX_get(NULL));
  35918. BN_free(t);
  35919. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  35920. BN_free(t);
  35921. #ifndef NO_WOLFSSL_STUB
  35922. /* No implementation. */
  35923. BN_CTX_start(NULL);
  35924. BN_CTX_start(bn_ctx);
  35925. #endif
  35926. BN_CTX_free(NULL);
  35927. BN_CTX_free(bn_ctx);
  35928. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35929. return EXPECT_RESULT();
  35930. }
  35931. static int test_wolfSSL_BN(void)
  35932. {
  35933. EXPECT_DECLS;
  35934. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35935. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35936. BIGNUM* a = NULL;
  35937. BIGNUM* b = NULL;
  35938. BIGNUM* c = NULL;
  35939. BIGNUM* d = NULL;
  35940. BIGNUM emptyBN;
  35941. /* Setup */
  35942. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35943. /* internal not set emptyBN. */
  35944. ExpectNotNull(a = BN_new());
  35945. ExpectNotNull(b = BN_new());
  35946. ExpectNotNull(c = BN_dup(b));
  35947. ExpectNotNull(d = BN_new());
  35948. /* Invalid parameter testing. */
  35949. BN_free(NULL);
  35950. ExpectNull(BN_dup(NULL));
  35951. ExpectNull(BN_dup(&emptyBN));
  35952. ExpectNull(BN_copy(NULL, NULL));
  35953. ExpectNull(BN_copy(b, NULL));
  35954. ExpectNull(BN_copy(NULL, c));
  35955. ExpectNull(BN_copy(b, &emptyBN));
  35956. ExpectNull(BN_copy(&emptyBN, c));
  35957. BN_clear(NULL);
  35958. BN_clear(&emptyBN);
  35959. ExpectIntEQ(BN_num_bytes(NULL), 0);
  35960. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  35961. ExpectIntEQ(BN_num_bits(NULL), 0);
  35962. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  35963. ExpectIntEQ(BN_is_negative(NULL), 0);
  35964. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  35965. /* END Invalid Parameters */
  35966. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  35967. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  35968. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  35969. ExpectIntEQ(BN_num_bits(a), 2);
  35970. ExpectIntEQ(BN_num_bytes(a), 1);
  35971. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  35972. defined(WOLFSSL_SP_INT_NEGATIVE))
  35973. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  35974. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  35975. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  35976. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  35977. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  35978. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  35979. {
  35980. /* Do additional tests on negative BN conversions. */
  35981. char* ret = NULL;
  35982. ASN1_INTEGER* asn1 = NULL;
  35983. BIGNUM* tmp = NULL;
  35984. /* Sanity check we have a negative BN. */
  35985. ExpectIntEQ(BN_is_negative(c), 1);
  35986. ExpectNotNull(ret = BN_bn2dec(c));
  35987. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  35988. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  35989. ret = NULL;
  35990. /* Convert to ASN1_INTEGER and back to BN. */
  35991. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  35992. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  35993. /* After converting back BN should be negative and correct. */
  35994. ExpectIntEQ(BN_is_negative(tmp), 1);
  35995. ExpectNotNull(ret = BN_bn2dec(tmp));
  35996. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  35997. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  35998. ASN1_INTEGER_free(asn1);
  35999. BN_free(tmp);
  36000. }
  36001. #endif
  36002. ExpectIntEQ(BN_get_word(c), 4);
  36003. #endif
  36004. ExpectIntEQ(BN_set_word(a, 3), 1);
  36005. ExpectIntEQ(BN_set_word(b, 3), 1);
  36006. ExpectIntEQ(BN_set_word(c, 4), 1);
  36007. /* NULL == NULL, NULL < num, num > NULL */
  36008. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  36009. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  36010. ExpectIntLT(BN_cmp(NULL, b), 0);
  36011. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  36012. ExpectIntGT(BN_cmp(a, NULL), 0);
  36013. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  36014. ExpectIntEQ(BN_cmp(a, b), 0);
  36015. ExpectIntLT(BN_cmp(a, c), 0);
  36016. ExpectIntGT(BN_cmp(c, b), 0);
  36017. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  36018. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  36019. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  36020. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  36021. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  36022. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  36023. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  36024. #endif
  36025. BN_clear(a);
  36026. BN_free(a);
  36027. BN_free(b);
  36028. BN_free(c);
  36029. BN_clear_free(d);
  36030. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  36031. return EXPECT_RESULT();
  36032. }
  36033. static int test_wolfSSL_BN_init(void)
  36034. {
  36035. EXPECT_DECLS;
  36036. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36037. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36038. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  36039. BIGNUM* ap = NULL;
  36040. BIGNUM bv;
  36041. BIGNUM cv;
  36042. BIGNUM dv;
  36043. ExpectNotNull(ap = BN_new());
  36044. BN_init(NULL);
  36045. XMEMSET(&bv, 0, sizeof(bv));
  36046. ExpectNull(BN_dup(&bv));
  36047. BN_init(&bv);
  36048. BN_init(&cv);
  36049. BN_init(&dv);
  36050. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  36051. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  36052. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  36053. /* a^b mod c = */
  36054. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  36055. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  36056. /* check result 3^2 mod 5 */
  36057. ExpectIntEQ(BN_get_word(&dv), 4);
  36058. /* a*b mod c = */
  36059. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  36060. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  36061. /* check result 3*2 mod 5 */
  36062. ExpectIntEQ(BN_get_word(&dv), 1);
  36063. BN_free(ap);
  36064. #endif
  36065. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  36066. return EXPECT_RESULT();
  36067. }
  36068. static int test_wolfSSL_BN_enc_dec(void)
  36069. {
  36070. EXPECT_DECLS;
  36071. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  36072. BIGNUM* a = NULL;
  36073. BIGNUM* b = NULL;
  36074. BIGNUM* c = NULL;
  36075. BIGNUM emptyBN;
  36076. char* str = NULL;
  36077. const char* emptyStr = "";
  36078. const char* numberStr = "12345";
  36079. const char* badStr = "g12345";
  36080. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  36081. const char* twoStr = "2";
  36082. #endif
  36083. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  36084. unsigned char outNum[5];
  36085. /* Setup */
  36086. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36087. ExpectNotNull(a = BN_new());
  36088. ExpectNotNull(b = BN_new());
  36089. ExpectIntEQ(BN_set_word(a, 2), 1);
  36090. /* Invalid parameters */
  36091. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  36092. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  36093. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  36094. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  36095. ExpectNull(BN_bn2hex(NULL));
  36096. ExpectNull(BN_bn2hex(&emptyBN));
  36097. ExpectNull(BN_bn2dec(NULL));
  36098. ExpectNull(BN_bn2dec(&emptyBN));
  36099. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  36100. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  36101. ExpectNull(BN_bin2bn(binNum, -1, a));
  36102. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  36103. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  36104. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  36105. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  36106. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  36107. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  36108. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  36109. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  36110. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  36111. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  36112. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  36113. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  36114. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  36115. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  36116. ExpectIntEQ(BN_set_word(a, 2), 1);
  36117. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  36118. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  36119. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  36120. ExpectIntEQ(BN_cmp(a, b), 0);
  36121. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  36122. ExpectIntEQ(BN_cmp(a, b), -1);
  36123. ExpectNotNull(str = BN_bn2hex(a));
  36124. ExpectNotNull(BN_hex2bn(&b, str));
  36125. ExpectIntEQ(BN_cmp(a, b), 0);
  36126. ExpectNotNull(BN_hex2bn(&b, numberStr));
  36127. ExpectIntEQ(BN_cmp(a, b), -1);
  36128. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  36129. str = NULL;
  36130. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  36131. ExpectNotNull(str = BN_bn2dec(a));
  36132. ExpectStrEQ(str, twoStr);
  36133. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  36134. str = NULL;
  36135. #ifndef NO_RSA
  36136. ExpectNotNull(str = BN_bn2dec(a));
  36137. ExpectNotNull(BN_dec2bn(&b, str));
  36138. ExpectIntEQ(BN_cmp(a, b), 0);
  36139. ExpectNotNull(BN_dec2bn(&b, numberStr));
  36140. ExpectIntEQ(BN_cmp(a, b), -1);
  36141. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  36142. str = NULL;
  36143. #else
  36144. /* No implementation - fail with good parameters. */
  36145. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  36146. #endif
  36147. #endif
  36148. BN_free(b);
  36149. BN_free(a);
  36150. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  36151. return EXPECT_RESULT();
  36152. }
  36153. static int test_wolfSSL_BN_word(void)
  36154. {
  36155. EXPECT_DECLS;
  36156. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  36157. BIGNUM* a = NULL;
  36158. BIGNUM* b = NULL;
  36159. BIGNUM* c = NULL;
  36160. BIGNUM av;
  36161. ExpectNotNull(a = BN_new());
  36162. ExpectNotNull(b = BN_new());
  36163. ExpectNotNull(c = BN_new());
  36164. XMEMSET(&av, 0, sizeof(av));
  36165. /* Invalid parameter. */
  36166. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  36167. ExpectIntEQ(BN_add_word(&av, 3), 0);
  36168. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  36169. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  36170. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  36171. ExpectIntEQ(BN_set_word(&av, 3), 0);
  36172. ExpectIntEQ(BN_get_word(NULL), 0);
  36173. ExpectIntEQ(BN_get_word(&av), 0);
  36174. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  36175. ExpectIntEQ(BN_is_word(&av, 3), 0);
  36176. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  36177. !defined(NO_DSA))
  36178. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  36179. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  36180. #endif
  36181. ExpectIntEQ(BN_one(NULL), 0);
  36182. ExpectIntEQ(BN_one(&av), 0);
  36183. BN_zero(NULL);
  36184. BN_zero(&av);
  36185. ExpectIntEQ(BN_is_one(NULL), 0);
  36186. ExpectIntEQ(BN_is_one(&av), 0);
  36187. ExpectIntEQ(BN_is_zero(NULL), 0);
  36188. ExpectIntEQ(BN_is_zero(&av), 0);
  36189. ExpectIntEQ(BN_set_word(a, 3), 1);
  36190. ExpectIntEQ(BN_set_word(b, 2), 1);
  36191. ExpectIntEQ(BN_set_word(c, 5), 1);
  36192. /* a + 3 = */
  36193. ExpectIntEQ(BN_add_word(a, 3), 1);
  36194. /* check result 3 + 3*/
  36195. ExpectIntEQ(BN_get_word(a), 6);
  36196. ExpectIntEQ(BN_is_word(a, 6), 1);
  36197. ExpectIntEQ(BN_is_word(a, 5), 0);
  36198. /* set a back to 3 */
  36199. ExpectIntEQ(BN_set_word(a, 3), 1);
  36200. /* a - 3 = */
  36201. ExpectIntEQ(BN_sub_word(a, 3), 1);
  36202. /* check result 3 - 3*/
  36203. ExpectIntEQ(BN_get_word(a), 0);
  36204. ExpectIntEQ(BN_one(a), 1);
  36205. ExpectIntEQ(BN_is_word(a, 1), 1);
  36206. ExpectIntEQ(BN_is_word(a, 0), 0);
  36207. ExpectIntEQ(BN_is_one(a), 1);
  36208. ExpectIntEQ(BN_is_zero(a), 0);
  36209. BN_zero(a);
  36210. ExpectIntEQ(BN_is_word(a, 0), 1);
  36211. ExpectIntEQ(BN_is_word(a, 1), 0);
  36212. ExpectIntEQ(BN_is_zero(a), 1);
  36213. ExpectIntEQ(BN_is_one(a), 0);
  36214. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  36215. !defined(NO_DSA))
  36216. ExpectIntEQ(BN_set_word(a, 5), 1);
  36217. ExpectIntEQ(BN_mod_word(a, 3), 2);
  36218. ExpectIntEQ(BN_mod_word(a, 0), -1);
  36219. #endif
  36220. BN_free(c);
  36221. BN_free(b);
  36222. BN_free(a);
  36223. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  36224. return EXPECT_RESULT();
  36225. }
  36226. static int test_wolfSSL_BN_bits(void)
  36227. {
  36228. EXPECT_DECLS;
  36229. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36230. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36231. BIGNUM* a = NULL;
  36232. BIGNUM emptyBN;
  36233. /* Setup */
  36234. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36235. ExpectNotNull(a = BN_new());
  36236. /* Invalid parameters. */
  36237. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  36238. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  36239. ExpectIntEQ(BN_set_bit(a, -1), 0);
  36240. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  36241. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  36242. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  36243. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  36244. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  36245. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  36246. ExpectIntEQ(BN_is_odd(NULL), 0);
  36247. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  36248. ExpectIntEQ(BN_set_word(a, 0), 1);
  36249. ExpectIntEQ(BN_is_zero(a), 1);
  36250. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  36251. ExpectIntEQ(BN_is_zero(a), 0);
  36252. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  36253. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  36254. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  36255. ExpectIntEQ(BN_is_zero(a), 1);
  36256. ExpectIntEQ(BN_set_bit(a, 0), 1);
  36257. ExpectIntEQ(BN_is_odd(a), 1);
  36258. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  36259. ExpectIntEQ(BN_is_odd(a), 0);
  36260. ExpectIntEQ(BN_set_bit(a, 1), 1);
  36261. ExpectIntEQ(BN_is_odd(a), 0);
  36262. ExpectIntEQ(BN_set_bit(a, 129), 1);
  36263. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  36264. #ifndef NO_WOLFSSL_STUB
  36265. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  36266. #endif
  36267. BN_free(a);
  36268. #endif
  36269. return EXPECT_RESULT();
  36270. }
  36271. static int test_wolfSSL_BN_shift(void)
  36272. {
  36273. EXPECT_DECLS;
  36274. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36275. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36276. BIGNUM* a = NULL;
  36277. BIGNUM* b = NULL;
  36278. BIGNUM emptyBN;
  36279. /* Setup */
  36280. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36281. ExpectNotNull(a = BN_new());
  36282. ExpectNotNull(b = BN_new());
  36283. /* Invalid parameters. */
  36284. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  36285. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  36286. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  36287. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  36288. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  36289. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  36290. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  36291. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  36292. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  36293. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  36294. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  36295. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  36296. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  36297. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  36298. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  36299. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  36300. ExpectIntEQ(BN_set_word(a, 1), 1);
  36301. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  36302. ExpectIntEQ(BN_is_word(b, 2), 1);
  36303. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  36304. ExpectIntEQ(BN_is_word(a, 2), 1);
  36305. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  36306. ExpectIntEQ(BN_is_word(b, 1), 1);
  36307. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  36308. ExpectIntEQ(BN_is_word(a, 1), 1);
  36309. BN_free(b);
  36310. BN_free(a);
  36311. #endif
  36312. return EXPECT_RESULT();
  36313. }
  36314. static int test_wolfSSL_BN_math(void)
  36315. {
  36316. EXPECT_DECLS;
  36317. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36318. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36319. BIGNUM* a = NULL;
  36320. BIGNUM* b = NULL;
  36321. BIGNUM* r = NULL;
  36322. BIGNUM* rem = NULL;
  36323. BIGNUM emptyBN;
  36324. BN_ULONG val1;
  36325. BN_ULONG val2;
  36326. /* Setup */
  36327. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36328. ExpectNotNull(a = BN_new());
  36329. ExpectNotNull(b = BN_new());
  36330. ExpectNotNull(r = BN_new());
  36331. ExpectNotNull(rem = BN_new());
  36332. /* Invalid parameters. */
  36333. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  36334. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  36335. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  36336. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  36337. ExpectIntEQ(BN_add(r, a, NULL), 0);
  36338. ExpectIntEQ(BN_add(r, NULL, b), 0);
  36339. ExpectIntEQ(BN_add(NULL, a, b), 0);
  36340. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  36341. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  36342. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  36343. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  36344. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  36345. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  36346. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  36347. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  36348. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  36349. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  36350. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  36351. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  36352. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  36353. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  36354. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  36355. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  36356. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  36357. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  36358. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  36359. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  36360. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  36361. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  36362. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  36363. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  36364. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  36365. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  36366. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  36367. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  36368. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36369. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  36370. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  36371. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  36372. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  36373. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  36374. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  36375. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  36376. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  36377. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  36378. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  36379. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  36380. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  36381. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  36382. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  36383. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  36384. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36385. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36386. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  36387. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  36388. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  36389. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  36390. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  36391. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  36392. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  36393. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  36394. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  36395. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  36396. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  36397. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  36398. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  36399. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  36400. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36401. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  36402. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  36403. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  36404. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  36405. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  36406. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  36407. /* END Invalid parameters. */
  36408. val1 = 8;
  36409. val2 = 3;
  36410. ExpectIntEQ(BN_set_word(a, val1), 1);
  36411. ExpectIntEQ(BN_set_word(b, val2), 1);
  36412. ExpectIntEQ(BN_add(r, a, b), 1);
  36413. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  36414. ExpectIntEQ(BN_sub(r, a, b), 1);
  36415. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  36416. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  36417. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  36418. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  36419. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  36420. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  36421. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  36422. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  36423. BN_free(rem);
  36424. BN_free(r);
  36425. BN_free(b);
  36426. BN_free(a);
  36427. #endif
  36428. return EXPECT_RESULT();
  36429. }
  36430. static int test_wolfSSL_BN_math_mod(void)
  36431. {
  36432. EXPECT_DECLS;
  36433. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36434. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36435. BIGNUM* a = NULL;
  36436. BIGNUM* b = NULL;
  36437. BIGNUM* m = NULL;
  36438. BIGNUM* r = NULL;
  36439. BIGNUM* t = NULL;
  36440. BIGNUM emptyBN;
  36441. BN_ULONG val1;
  36442. BN_ULONG val2;
  36443. BN_ULONG val3;
  36444. /* Setup */
  36445. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36446. ExpectNotNull(a = BN_new());
  36447. ExpectNotNull(b = BN_new());
  36448. ExpectNotNull(m = BN_new());
  36449. ExpectNotNull(r = BN_new());
  36450. /* Invalid parameters. */
  36451. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  36452. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  36453. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  36454. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  36455. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  36456. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  36457. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  36458. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  36459. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  36460. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36461. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36462. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  36463. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  36464. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  36465. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  36466. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  36467. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  36468. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  36469. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  36470. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  36471. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  36472. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  36473. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  36474. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  36475. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  36476. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  36477. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  36478. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36479. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36480. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  36481. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  36482. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  36483. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  36484. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  36485. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  36486. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  36487. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  36488. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  36489. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  36490. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  36491. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  36492. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  36493. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  36494. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  36495. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  36496. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36497. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36498. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  36499. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  36500. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  36501. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  36502. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  36503. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  36504. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  36505. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  36506. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  36507. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  36508. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  36509. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  36510. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  36511. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  36512. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  36513. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  36514. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  36515. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  36516. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  36517. /* END Invalid parameters. */
  36518. val1 = 9;
  36519. val2 = 13;
  36520. val3 = 5;
  36521. ExpectIntEQ(BN_set_word(a, val1), 1);
  36522. ExpectIntEQ(BN_set_word(b, val2), 1);
  36523. ExpectIntEQ(BN_set_word(m, val3), 1);
  36524. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  36525. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  36526. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  36527. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  36528. ExpectIntEQ(BN_set_word(a, 2), 1);
  36529. ExpectIntEQ(BN_set_word(b, 3), 1);
  36530. ExpectIntEQ(BN_set_word(m, 5), 1);
  36531. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  36532. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  36533. ExpectIntEQ(BN_is_word(r, 3), 1);
  36534. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  36535. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  36536. ExpectIntEQ(BN_is_word(r, 3), 1);
  36537. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  36538. ExpectIntEQ(BN_is_word(t, 3), 1);
  36539. BN_free(t);
  36540. /* No inverse case. No inverse when a divides b. */
  36541. ExpectIntEQ(BN_set_word(a, 3), 1);
  36542. ExpectIntEQ(BN_set_word(m, 9), 1);
  36543. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  36544. BN_free(r);
  36545. BN_free(m);
  36546. BN_free(b);
  36547. BN_free(a);
  36548. #endif
  36549. return EXPECT_RESULT();
  36550. }
  36551. static int test_wolfSSL_BN_math_other(void)
  36552. {
  36553. EXPECT_DECLS;
  36554. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36555. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36556. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  36557. BIGNUM* a = NULL;
  36558. BIGNUM* b = NULL;
  36559. BIGNUM* r = NULL;
  36560. BIGNUM emptyBN;
  36561. /* Setup */
  36562. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36563. ExpectNotNull(a = BN_new());
  36564. ExpectNotNull(b = BN_new());
  36565. ExpectNotNull(r = BN_new());
  36566. /* Invalid parameters. */
  36567. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  36568. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  36569. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  36570. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  36571. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  36572. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  36573. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  36574. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36575. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  36576. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  36577. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  36578. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  36579. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  36580. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  36581. /* END Invalid parameters. */
  36582. /* No common factors between 2 and 3. */
  36583. ExpectIntEQ(BN_set_word(a, 2), 1);
  36584. ExpectIntEQ(BN_set_word(b, 3), 1);
  36585. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  36586. ExpectIntEQ(BN_is_word(r, 1), 1);
  36587. /* 3 is largest value that divides both 6 and 9. */
  36588. ExpectIntEQ(BN_set_word(a, 6), 1);
  36589. ExpectIntEQ(BN_set_word(b, 9), 1);
  36590. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  36591. ExpectIntEQ(BN_is_word(r, 3), 1);
  36592. /* GCD of 0 and 0 is undefined. */
  36593. ExpectIntEQ(BN_set_word(a, 0), 1);
  36594. ExpectIntEQ(BN_set_word(b, 0), 1);
  36595. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  36596. /* Teardown */
  36597. BN_free(r);
  36598. BN_free(b);
  36599. BN_free(a);
  36600. #endif
  36601. #endif
  36602. return EXPECT_RESULT();
  36603. }
  36604. static int test_wolfSSL_BN_rand(void)
  36605. {
  36606. EXPECT_DECLS;
  36607. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  36608. BIGNUM* bn = NULL;
  36609. BIGNUM* range = NULL;
  36610. BIGNUM emptyBN;
  36611. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36612. ExpectNotNull(bn = BN_new());
  36613. ExpectNotNull(range = BN_new());
  36614. /* Invalid parameters. */
  36615. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  36616. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  36617. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  36618. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  36619. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  36620. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  36621. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  36622. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  36623. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  36624. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  36625. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  36626. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  36627. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  36628. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  36629. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  36630. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  36631. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  36632. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  36633. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  36634. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  36635. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36636. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  36637. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36638. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  36639. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36640. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  36641. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36642. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  36643. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36644. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  36645. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36646. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  36647. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36648. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  36649. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36650. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  36651. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36652. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  36653. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36654. /* 1 bit random value must have no more than one top bit set. */
  36655. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  36656. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36657. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  36658. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36659. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  36660. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36661. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  36662. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36663. /* END Invalid parameters. */
  36664. /* 0 bit random: 0. */
  36665. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  36666. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36667. ExpectIntEQ(BN_is_zero(bn), 1);
  36668. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  36669. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  36670. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36671. ExpectIntEQ(BN_is_zero(bn), 1);
  36672. /* 1 bit random: 0 or 1. */
  36673. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  36674. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36675. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  36676. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  36677. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36678. ExpectIntEQ(BN_get_word(bn), 1);
  36679. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  36680. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  36681. ExpectIntEQ(BN_get_word(bn), 1);
  36682. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  36683. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36684. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  36685. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  36686. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36687. ExpectIntEQ(BN_get_word(bn), 1);
  36688. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  36689. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  36690. ExpectIntEQ(BN_get_word(bn), 1);
  36691. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  36692. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36693. ExpectIntEQ(BN_num_bits(bn), 8);
  36694. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  36695. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  36696. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36697. ExpectIntEQ(BN_num_bits(bn), 8);
  36698. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  36699. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  36700. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36701. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  36702. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  36703. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  36704. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36705. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  36706. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  36707. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  36708. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  36709. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  36710. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  36711. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  36712. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  36713. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  36714. * requested number of bits up to the nearest multiple of 8. E.g. in this
  36715. * case, requesting a 13-bit random number would actually return a 16-bit
  36716. * random number. */
  36717. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  36718. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36719. ExpectIntEQ(BN_num_bits(bn), 13);
  36720. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  36721. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36722. ExpectIntEQ(BN_rand_range(bn, range), 1);
  36723. ExpectIntEQ(BN_set_word(range, 0), 1);
  36724. ExpectIntEQ(BN_rand_range(bn, range), 1);
  36725. ExpectIntEQ(BN_set_word(range, 1), 1);
  36726. ExpectIntEQ(BN_rand_range(bn, range), 1);
  36727. BN_free(bn);
  36728. BN_free(range);
  36729. #endif
  36730. return EXPECT_RESULT();
  36731. }
  36732. static int test_wolfSSL_BN_prime(void)
  36733. {
  36734. EXPECT_DECLS;
  36735. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36736. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36737. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  36738. BIGNUM* a = NULL;
  36739. BIGNUM* add = NULL;
  36740. BIGNUM* rem = NULL;
  36741. BIGNUM emptyBN;
  36742. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36743. ExpectNotNull(a = BN_new());
  36744. ExpectNotNull(add = BN_new());
  36745. ExpectNotNull(rem = BN_new());
  36746. /* Invalid parameters. */
  36747. /* BN_generate_prime_ex()
  36748. * prime - must have valid BIGNUM
  36749. * bits - Greater then 0
  36750. * safe - not supported, must be 0
  36751. * add - not supported, must be NULL
  36752. * rem - not supported, must be NULL
  36753. * cb - anything
  36754. */
  36755. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  36756. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  36757. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  36758. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  36759. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  36760. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  36761. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  36762. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  36763. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  36764. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  36765. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  36766. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  36767. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  36768. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  36769. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  36770. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  36771. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  36772. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  36773. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  36774. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  36775. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  36776. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  36777. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  36778. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  36779. /* END Invalid parameters. */
  36780. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  36781. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  36782. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  36783. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  36784. BN_free(rem);
  36785. BN_free(add);
  36786. BN_free(a);
  36787. #endif
  36788. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  36789. return EXPECT_RESULT();
  36790. }
  36791. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  36792. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36793. #define TEST_ARG 0x1234
  36794. static void msg_cb(int write_p, int version, int content_type,
  36795. const void *buf, size_t len, SSL *ssl, void *arg)
  36796. {
  36797. (void)write_p;
  36798. (void)version;
  36799. (void)content_type;
  36800. (void)buf;
  36801. (void)len;
  36802. (void)ssl;
  36803. AssertTrue(arg == (void*)TEST_ARG);
  36804. }
  36805. #endif
  36806. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  36807. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36808. #if defined(SESSION_CERTS)
  36809. #include "wolfssl/internal.h"
  36810. #endif
  36811. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  36812. {
  36813. EXPECT_DECLS;
  36814. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  36815. STACK_OF(X509)* sk = NULL;
  36816. X509* x509 = NULL;
  36817. int i, num;
  36818. BIO* bio = NULL;
  36819. #endif
  36820. ExpectNotNull(ctx);
  36821. ExpectNotNull(ssl);
  36822. fprintf(stderr, "\n===== msgcb called ====\n");
  36823. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  36824. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  36825. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  36826. ExpectNotNull(SSL_get0_verified_chain(ssl));
  36827. #endif
  36828. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  36829. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  36830. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  36831. if (sk == NULL) {
  36832. BIO_free(bio);
  36833. return TEST_FAIL;
  36834. }
  36835. num = sk_X509_num(sk);
  36836. ExpectTrue(num > 0);
  36837. for (i = 0; i < num; i++) {
  36838. ExpectNotNull(x509 = sk_X509_value(sk,i));
  36839. if (x509 == NULL)
  36840. break;
  36841. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  36842. X509_print(bio,x509);
  36843. fprintf(stderr, "\n\n");
  36844. }
  36845. BIO_free(bio);
  36846. #endif
  36847. return EXPECT_RESULT();
  36848. }
  36849. #endif
  36850. static int test_wolfSSL_msgCb(void)
  36851. {
  36852. EXPECT_DECLS;
  36853. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  36854. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36855. test_ssl_cbf client_cb;
  36856. test_ssl_cbf server_cb;
  36857. XMEMSET(&client_cb, 0, sizeof(client_cb));
  36858. XMEMSET(&server_cb, 0, sizeof(server_cb));
  36859. #ifndef WOLFSSL_NO_TLS12
  36860. client_cb.method = wolfTLSv1_2_client_method;
  36861. server_cb.method = wolfTLSv1_2_server_method;
  36862. #else
  36863. client_cb.method = wolfTLSv1_3_client_method;
  36864. server_cb.method = wolfTLSv1_3_server_method;
  36865. #endif
  36866. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  36867. &server_cb, msgCb), TEST_SUCCESS);
  36868. #endif
  36869. return EXPECT_RESULT();
  36870. }
  36871. static int test_wolfSSL_either_side(void)
  36872. {
  36873. EXPECT_DECLS;
  36874. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  36875. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36876. test_ssl_cbf client_cb;
  36877. test_ssl_cbf server_cb;
  36878. XMEMSET(&client_cb, 0, sizeof(client_cb));
  36879. XMEMSET(&server_cb, 0, sizeof(server_cb));
  36880. /* Use different CTX for client and server */
  36881. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  36882. ExpectNotNull(client_cb.ctx);
  36883. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  36884. ExpectNotNull(server_cb.ctx);
  36885. /* we are responsible for free'ing WOLFSSL_CTX */
  36886. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  36887. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  36888. &server_cb, NULL), TEST_SUCCESS);
  36889. wolfSSL_CTX_free(client_cb.ctx);
  36890. wolfSSL_CTX_free(server_cb.ctx);
  36891. #endif
  36892. return EXPECT_RESULT();
  36893. }
  36894. static int test_wolfSSL_DTLS_either_side(void)
  36895. {
  36896. EXPECT_DECLS;
  36897. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  36898. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  36899. test_ssl_cbf client_cb;
  36900. test_ssl_cbf server_cb;
  36901. XMEMSET(&client_cb, 0, sizeof(client_cb));
  36902. XMEMSET(&server_cb, 0, sizeof(server_cb));
  36903. /* Use different CTX for client and server */
  36904. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  36905. ExpectNotNull(client_cb.ctx);
  36906. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  36907. ExpectNotNull(server_cb.ctx);
  36908. /* we are responsible for free'ing WOLFSSL_CTX */
  36909. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  36910. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  36911. &server_cb, NULL), TEST_SUCCESS);
  36912. wolfSSL_CTX_free(client_cb.ctx);
  36913. wolfSSL_CTX_free(server_cb.ctx);
  36914. #endif
  36915. return EXPECT_RESULT();
  36916. }
  36917. static int test_generate_cookie(void)
  36918. {
  36919. EXPECT_DECLS;
  36920. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  36921. SSL_CTX* ctx = NULL;
  36922. SSL* ssl = NULL;
  36923. byte buf[FOURK_BUF] = {0};
  36924. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  36925. ExpectNotNull(ssl = SSL_new(ctx));
  36926. /* Test unconnected */
  36927. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  36928. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  36929. wolfSSL_SetCookieCtx(ssl, ctx);
  36930. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  36931. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  36932. SSL_free(ssl);
  36933. SSL_CTX_free(ctx);
  36934. #endif
  36935. return EXPECT_RESULT();
  36936. }
  36937. static int test_wolfSSL_set_options(void)
  36938. {
  36939. EXPECT_DECLS;
  36940. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36941. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  36942. WOLFSSL* ssl = NULL;
  36943. WOLFSSL_CTX* ctx = NULL;
  36944. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  36945. char appData[] = "extra msg";
  36946. #endif
  36947. #ifdef OPENSSL_EXTRA
  36948. unsigned char protos[] = {
  36949. 7, 't', 'l', 's', '/', '1', '.', '2',
  36950. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  36951. };
  36952. unsigned int len = sizeof(protos);
  36953. void *arg = (void *)TEST_ARG;
  36954. #endif
  36955. #ifndef NO_WOLFSSL_SERVER
  36956. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36957. #else
  36958. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36959. #endif
  36960. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  36961. WOLFSSL_FILETYPE_PEM));
  36962. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  36963. WOLFSSL_FILETYPE_PEM));
  36964. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  36965. == WOLFSSL_OP_NO_TLSv1);
  36966. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  36967. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  36968. WOLFSSL_OP_NO_SSLv2)), 0);
  36969. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  36970. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  36971. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  36972. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  36973. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  36974. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  36975. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  36976. WOLFSSL_OP_NO_COMPRESSION));
  36977. wolfSSL_CTX_free(ctx);
  36978. ctx = NULL;
  36979. #ifndef NO_WOLFSSL_SERVER
  36980. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36981. #else
  36982. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36983. #endif
  36984. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  36985. WOLFSSL_FILETYPE_PEM));
  36986. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  36987. WOLFSSL_FILETYPE_PEM));
  36988. #ifdef OPENSSL_EXTRA
  36989. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  36990. #endif
  36991. ExpectNotNull(ssl = wolfSSL_new(ctx));
  36992. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  36993. #ifdef HAVE_EX_DATA
  36994. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  36995. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  36996. if (ssl != NULL) {
  36997. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  36998. appData, sizeof(appData)), 0);
  36999. }
  37000. #else
  37001. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  37002. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  37003. #endif
  37004. #endif
  37005. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  37006. WOLFSSL_OP_NO_TLSv1);
  37007. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  37008. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  37009. WOLFSSL_OP_NO_SSLv2)), 0);
  37010. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  37011. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  37012. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  37013. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  37014. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  37015. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  37016. #ifdef OPENSSL_EXTRA
  37017. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  37018. WOLFSSL_OP_NO_COMPRESSION));
  37019. #endif
  37020. #ifdef OPENSSL_EXTRA
  37021. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  37022. wolfSSL_set_msg_callback_arg(ssl, arg);
  37023. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  37024. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  37025. #else
  37026. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  37027. #endif
  37028. #endif
  37029. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  37030. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  37031. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  37032. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  37033. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  37034. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  37035. #else
  37036. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  37037. #endif
  37038. #endif /* HAVE_ALPN && !NO_BIO */
  37039. #endif
  37040. wolfSSL_free(ssl);
  37041. wolfSSL_CTX_free(ctx);
  37042. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  37043. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  37044. return EXPECT_RESULT();
  37045. }
  37046. static int test_wolfSSL_sk_SSL_CIPHER(void)
  37047. {
  37048. EXPECT_DECLS;
  37049. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  37050. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37051. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  37052. SSL* ssl = NULL;
  37053. SSL_CTX* ctx = NULL;
  37054. STACK_OF(SSL_CIPHER) *sk = NULL;
  37055. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  37056. #ifndef NO_WOLFSSL_SERVER
  37057. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  37058. #else
  37059. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  37060. #endif
  37061. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  37062. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  37063. ExpectNotNull(ssl = SSL_new(ctx));
  37064. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  37065. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  37066. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  37067. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  37068. /* error case because connection has not been established yet */
  37069. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  37070. sk_SSL_CIPHER_free(dupSk);
  37071. /* sk is pointer to internal struct that should be free'd in SSL_free */
  37072. SSL_free(ssl);
  37073. SSL_CTX_free(ctx);
  37074. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  37075. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  37076. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  37077. return EXPECT_RESULT();
  37078. }
  37079. static int test_wolfSSL_set1_curves_list(void)
  37080. {
  37081. EXPECT_DECLS;
  37082. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  37083. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  37084. SSL* ssl = NULL;
  37085. SSL_CTX* ctx = NULL;
  37086. #ifndef NO_WOLFSSL_SERVER
  37087. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  37088. #else
  37089. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  37090. #endif
  37091. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  37092. SSL_FILETYPE_PEM));
  37093. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  37094. ExpectNotNull(ssl = SSL_new(ctx));
  37095. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  37096. #ifdef HAVE_ECC
  37097. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  37098. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  37099. #endif
  37100. #ifdef HAVE_CURVE25519
  37101. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  37102. #else
  37103. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  37104. #endif
  37105. #ifdef HAVE_CURVE448
  37106. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  37107. #else
  37108. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  37109. #endif
  37110. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  37111. #ifdef HAVE_ECC
  37112. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  37113. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  37114. #endif
  37115. #ifdef HAVE_CURVE25519
  37116. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  37117. #else
  37118. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  37119. #endif
  37120. #ifdef HAVE_CURVE448
  37121. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  37122. #else
  37123. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  37124. #endif
  37125. SSL_free(ssl);
  37126. SSL_CTX_free(ctx);
  37127. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  37128. #endif
  37129. return EXPECT_RESULT();
  37130. }
  37131. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  37132. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  37133. static int test_wolfSSL_curves_mismatch_ctx_ready(WOLFSSL_CTX* ctx)
  37134. {
  37135. static int counter = 0;
  37136. EXPECT_DECLS;
  37137. if (counter % 2) {
  37138. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-256"),
  37139. WOLFSSL_SUCCESS);
  37140. }
  37141. else {
  37142. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-384"),
  37143. WOLFSSL_SUCCESS);
  37144. }
  37145. /* Ciphersuites that require curves */
  37146. wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384:"
  37147. "TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-GCM-SHA256:"
  37148. "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:"
  37149. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  37150. "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-CHACHA20-POLY1305:"
  37151. "ECDHE-ECDSA-CHACHA20-POLY1305");
  37152. counter++;
  37153. return EXPECT_RESULT();
  37154. }
  37155. #endif
  37156. static int test_wolfSSL_curves_mismatch(void)
  37157. {
  37158. EXPECT_DECLS;
  37159. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  37160. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  37161. test_ssl_cbf func_cb_client;
  37162. test_ssl_cbf func_cb_server;
  37163. size_t i;
  37164. struct {
  37165. method_provider client_meth;
  37166. method_provider server_meth;
  37167. const char* desc;
  37168. int client_last_err;
  37169. int server_last_err;
  37170. } test_params[] = {
  37171. #ifdef WOLFSSL_TLS13
  37172. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3",
  37173. FATAL_ERROR, BAD_KEY_SHARE_DATA},
  37174. #endif
  37175. #ifndef WOLFSSL_NO_TLS12
  37176. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2",
  37177. FATAL_ERROR, MATCH_SUITE_ERROR},
  37178. #endif
  37179. #ifndef NO_OLD_TLS
  37180. {wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLS 1.1",
  37181. FATAL_ERROR, MATCH_SUITE_ERROR},
  37182. #endif
  37183. };
  37184. for (i = 0; i < XELEM_CNT(test_params) && !EXPECT_FAIL(); i++) {
  37185. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  37186. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  37187. printf("\tTesting with %s...\n", test_params[i].desc);
  37188. func_cb_client.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  37189. func_cb_server.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  37190. func_cb_client.method = test_params[i].client_meth;
  37191. func_cb_server.method = test_params[i].server_meth;
  37192. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  37193. &func_cb_server, NULL), TEST_FAIL);
  37194. ExpectIntEQ(func_cb_client.last_err, test_params[i].client_last_err);
  37195. ExpectIntEQ(func_cb_server.last_err, test_params[i].server_last_err);
  37196. if (!EXPECT_SUCCESS())
  37197. break;
  37198. printf("\t%s passed\n", test_params[i].desc);
  37199. }
  37200. #endif
  37201. return EXPECT_RESULT();
  37202. }
  37203. static int test_wolfSSL_set1_sigalgs_list(void)
  37204. {
  37205. EXPECT_DECLS;
  37206. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37207. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  37208. SSL* ssl = NULL;
  37209. SSL_CTX* ctx = NULL;
  37210. #ifndef NO_WOLFSSL_SERVER
  37211. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  37212. #else
  37213. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  37214. #endif
  37215. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  37216. SSL_FILETYPE_PEM));
  37217. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  37218. ExpectNotNull(ssl = SSL_new(ctx));
  37219. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  37220. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  37221. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  37222. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  37223. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  37224. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  37225. #ifndef NO_RSA
  37226. #ifndef NO_SHA256
  37227. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  37228. WOLFSSL_FAILURE);
  37229. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  37230. WOLFSSL_FAILURE);
  37231. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  37232. WOLFSSL_SUCCESS);
  37233. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  37234. WOLFSSL_SUCCESS);
  37235. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  37236. WOLFSSL_FAILURE);
  37237. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  37238. WOLFSSL_FAILURE);
  37239. #ifdef WC_RSA_PSS
  37240. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  37241. WOLFSSL_SUCCESS);
  37242. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  37243. WOLFSSL_SUCCESS);
  37244. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  37245. WOLFSSL_SUCCESS);
  37246. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  37247. WOLFSSL_SUCCESS);
  37248. #endif
  37249. #ifdef WOLFSSL_SHA512
  37250. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  37251. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  37252. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  37253. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  37254. #elif defined(WOLFSSL_SHA384)
  37255. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  37256. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  37257. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  37258. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  37259. #endif
  37260. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  37261. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  37262. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  37263. WOLFSSL_FAILURE);
  37264. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  37265. WOLFSSL_FAILURE);
  37266. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  37267. WOLFSSL_FAILURE);
  37268. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  37269. WOLFSSL_FAILURE);
  37270. #endif
  37271. #endif
  37272. #ifdef HAVE_ECC
  37273. #ifndef NO_SHA256
  37274. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  37275. WOLFSSL_SUCCESS);
  37276. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  37277. WOLFSSL_SUCCESS);
  37278. #ifdef WOLFSSL_SHA512
  37279. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  37280. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  37281. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  37282. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  37283. #elif defined(WOLFSSL_SHA384)
  37284. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  37285. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  37286. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  37287. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  37288. #endif
  37289. #endif
  37290. #endif
  37291. #ifdef HAVE_ED25519
  37292. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  37293. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  37294. #endif
  37295. #ifdef HAVE_ED448
  37296. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  37297. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  37298. #endif
  37299. #ifndef NO_DSA
  37300. #ifndef NO_SHA256
  37301. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  37302. WOLFSSL_SUCCESS);
  37303. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  37304. WOLFSSL_SUCCESS);
  37305. #endif
  37306. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  37307. defined(WOLFSSL_ALLOW_TLS_SHA1))
  37308. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  37309. WOLFSSL_SUCCESS);
  37310. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  37311. WOLFSSL_SUCCESS);
  37312. #endif
  37313. #endif
  37314. SSL_free(ssl);
  37315. SSL_CTX_free(ctx);
  37316. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  37317. #endif
  37318. return EXPECT_RESULT();
  37319. }
  37320. /* Testing wolfSSL_set_tlsext_status_type function.
  37321. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  37322. */
  37323. static int test_wolfSSL_set_tlsext_status_type(void)
  37324. {
  37325. EXPECT_DECLS;
  37326. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  37327. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  37328. SSL* ssl = NULL;
  37329. SSL_CTX* ctx = NULL;
  37330. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  37331. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  37332. SSL_FILETYPE_PEM));
  37333. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  37334. ExpectNotNull(ssl = SSL_new(ctx));
  37335. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  37336. SSL_SUCCESS);
  37337. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  37338. SSL_free(ssl);
  37339. SSL_CTX_free(ctx);
  37340. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  37341. return EXPECT_RESULT();
  37342. }
  37343. #ifndef NO_BIO
  37344. static int test_wolfSSL_PEM_read_bio(void)
  37345. {
  37346. EXPECT_DECLS;
  37347. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  37348. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37349. byte buff[6000];
  37350. XFILE f = XBADFILE;
  37351. int bytes;
  37352. X509* x509 = NULL;
  37353. BIO* bio = NULL;
  37354. BUF_MEM* buf = NULL;
  37355. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  37356. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  37357. if (f != XBADFILE)
  37358. XFCLOSE(f);
  37359. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  37360. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  37361. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  37362. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  37363. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  37364. /* BIO should return the set EOF value */
  37365. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  37366. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  37367. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  37368. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  37369. BIO_free(bio);
  37370. BUF_MEM_free(buf);
  37371. X509_free(x509);
  37372. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  37373. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  37374. return EXPECT_RESULT();
  37375. }
  37376. #if defined(OPENSSL_EXTRA)
  37377. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  37378. long argl, long ret)
  37379. {
  37380. (void)bio;
  37381. (void)cmd;
  37382. (void)argp;
  37383. (void)argi;
  37384. (void)argl;
  37385. return ret;
  37386. }
  37387. #endif
  37388. static int test_wolfSSL_BIO(void)
  37389. {
  37390. EXPECT_DECLS;
  37391. #if defined(OPENSSL_EXTRA)
  37392. const unsigned char* p = NULL;
  37393. byte buff[20];
  37394. BIO* bio1 = NULL;
  37395. BIO* bio2 = NULL;
  37396. BIO* bio3 = NULL;
  37397. char* bufPt = NULL;
  37398. int i;
  37399. for (i = 0; i < 20; i++) {
  37400. buff[i] = i;
  37401. }
  37402. /* test BIO_free with NULL */
  37403. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  37404. /* Creating and testing type BIO_s_bio */
  37405. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  37406. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  37407. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  37408. /* read/write before set up */
  37409. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  37410. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  37411. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  37412. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  37413. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  37414. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  37415. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  37416. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  37417. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  37418. /* write buffer full */
  37419. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  37420. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  37421. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  37422. /* write the other direction with pair */
  37423. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  37424. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  37425. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  37426. /* try read */
  37427. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  37428. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  37429. /* try read using ctrl function */
  37430. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  37431. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  37432. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  37433. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  37434. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  37435. for (i = 0; i < 20; i++) {
  37436. ExpectIntEQ((int)bufPt[i], i);
  37437. }
  37438. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  37439. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  37440. for (i = 0; i < 8; i++) {
  37441. ExpectIntEQ((int)bufPt[i], i);
  37442. }
  37443. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  37444. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  37445. /* new pair */
  37446. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  37447. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  37448. bio2 = NULL;
  37449. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  37450. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  37451. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  37452. /* test wrap around... */
  37453. ExpectIntEQ(BIO_reset(bio1), 0);
  37454. ExpectIntEQ(BIO_reset(bio3), 0);
  37455. /* fill write buffer, read only small amount then write again */
  37456. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  37457. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  37458. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  37459. for (i = 0; i < 4; i++) {
  37460. ExpectIntEQ(bufPt[i], i);
  37461. }
  37462. /* try writing over read index */
  37463. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  37464. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  37465. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  37466. /* read and write 0 bytes */
  37467. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  37468. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  37469. /* should read only to end of write buffer then need to read again */
  37470. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  37471. for (i = 0; i < 16; i++) {
  37472. ExpectIntEQ(bufPt[i], buff[4 + i]);
  37473. }
  37474. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  37475. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  37476. for (i = 0; i < 4; i++) {
  37477. ExpectIntEQ(bufPt[i], 0);
  37478. }
  37479. /* read index should not have advanced with nread0 */
  37480. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  37481. for (i = 0; i < 4; i++) {
  37482. ExpectIntEQ(bufPt[i], 0);
  37483. }
  37484. /* write and fill up buffer checking reset of index state */
  37485. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  37486. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  37487. /* test reset on data in bio1 write buffer */
  37488. ExpectIntEQ(BIO_reset(bio1), 0);
  37489. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  37490. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  37491. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  37492. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  37493. ExpectNotNull(p);
  37494. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  37495. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  37496. for (i = 0; i < 6; i++) {
  37497. ExpectIntEQ(bufPt[i], i);
  37498. }
  37499. /* test case of writing twice with offset read index */
  37500. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  37501. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  37502. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  37503. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  37504. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  37505. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  37506. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  37507. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  37508. BIO_free(bio1);
  37509. bio1 = NULL;
  37510. BIO_free(bio3);
  37511. bio3 = NULL;
  37512. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  37513. {
  37514. BIO* bioA = NULL;
  37515. BIO* bioB = NULL;
  37516. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  37517. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  37518. BIO_free(bioA);
  37519. bioA = NULL;
  37520. BIO_free(bioB);
  37521. bioB = NULL;
  37522. }
  37523. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  37524. /* BIOs with file pointers */
  37525. #if !defined(NO_FILESYSTEM)
  37526. {
  37527. XFILE f1 = XBADFILE;
  37528. XFILE f2 = XBADFILE;
  37529. BIO* f_bio1 = NULL;
  37530. BIO* f_bio2 = NULL;
  37531. unsigned char cert[300];
  37532. char testFile[] = "tests/bio_write_test.txt";
  37533. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  37534. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  37535. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  37536. /* Failure due to wrong BIO type */
  37537. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  37538. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  37539. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  37540. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  37541. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  37542. WOLFSSL_SUCCESS);
  37543. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  37544. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  37545. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  37546. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  37547. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  37548. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  37549. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  37550. ExpectIntEQ(BIO_reset(f_bio2), 0);
  37551. ExpectIntEQ(BIO_tell(NULL),-1);
  37552. ExpectIntEQ(BIO_tell(f_bio2),0);
  37553. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  37554. ExpectIntEQ(BIO_tell(f_bio2),4);
  37555. BIO_free(f_bio1);
  37556. f_bio1 = NULL;
  37557. BIO_free(f_bio2);
  37558. f_bio2 = NULL;
  37559. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  37560. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  37561. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  37562. BIO_free(f_bio1);
  37563. f_bio1 = NULL;
  37564. }
  37565. #endif /* !defined(NO_FILESYSTEM) */
  37566. /* BIO info callback */
  37567. {
  37568. const char* testArg = "test";
  37569. BIO* cb_bio = NULL;
  37570. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  37571. BIO_set_callback(cb_bio, bioCallback);
  37572. ExpectNotNull(BIO_get_callback(cb_bio));
  37573. BIO_set_callback(cb_bio, NULL);
  37574. ExpectNull(BIO_get_callback(cb_bio));
  37575. BIO_set_callback_arg(cb_bio, (char*)testArg);
  37576. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  37577. ExpectNull(BIO_get_callback_arg(NULL));
  37578. BIO_free(cb_bio);
  37579. cb_bio = NULL;
  37580. }
  37581. /* BIO_vfree */
  37582. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  37583. BIO_vfree(NULL);
  37584. BIO_vfree(bio1);
  37585. #endif
  37586. return EXPECT_RESULT();
  37587. }
  37588. static int test_wolfSSL_BIO_BIO_ring_read(void)
  37589. {
  37590. EXPECT_DECLS;
  37591. #if defined(OPENSSL_ALL)
  37592. BIO* bio1 = NULL;
  37593. BIO* bio2 = NULL;
  37594. byte data[50];
  37595. byte tmp[50];
  37596. XMEMSET(data, 42, sizeof(data));
  37597. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  37598. SSL_SUCCESS);
  37599. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  37600. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  37601. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  37602. ExpectBufEQ(tmp, data, 20);
  37603. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  37604. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  37605. ExpectBufEQ(tmp, data, 40);
  37606. BIO_free(bio1);
  37607. BIO_free(bio2);
  37608. #endif
  37609. return EXPECT_RESULT();
  37610. }
  37611. #endif /* !NO_BIO */
  37612. static int test_wolfSSL_a2i_IPADDRESS(void)
  37613. {
  37614. EXPECT_DECLS;
  37615. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  37616. const unsigned char* data = NULL;
  37617. int dataSz = 0;
  37618. ASN1_OCTET_STRING *st = NULL;
  37619. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  37620. const unsigned char ipv6_exp[] = {
  37621. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  37622. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  37623. };
  37624. const unsigned char ipv6_home[] = {
  37625. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37626. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  37627. };
  37628. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  37629. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  37630. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  37631. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  37632. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  37633. ASN1_STRING_free(st);
  37634. st = NULL;
  37635. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  37636. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  37637. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  37638. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  37639. ASN1_STRING_free(st);
  37640. st = NULL;
  37641. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  37642. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  37643. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  37644. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  37645. ASN1_STRING_free(st);
  37646. #endif
  37647. return EXPECT_RESULT();
  37648. }
  37649. static int test_wolfSSL_X509_cmp_time(void)
  37650. {
  37651. EXPECT_DECLS;
  37652. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  37653. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  37654. WOLFSSL_ASN1_TIME asn_time;
  37655. time_t t;
  37656. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  37657. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  37658. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  37659. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  37660. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  37661. #endif
  37662. return EXPECT_RESULT();
  37663. }
  37664. static int test_wolfSSL_X509_time_adj(void)
  37665. {
  37666. EXPECT_DECLS;
  37667. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  37668. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  37669. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  37670. !defined(NO_ASN_TIME)
  37671. X509* x509 = NULL;
  37672. time_t t;
  37673. time_t not_before;
  37674. time_t not_after;
  37675. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  37676. client_cert_der_2048, sizeof_client_cert_der_2048,
  37677. WOLFSSL_FILETYPE_ASN1));
  37678. t = 0;
  37679. not_before = wc_Time(0);
  37680. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  37681. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  37682. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  37683. /* Check X509_gmtime_adj, too. */
  37684. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  37685. X509_free(x509);
  37686. #endif
  37687. return EXPECT_RESULT();
  37688. }
  37689. static int test_wolfSSL_X509_bad_altname(void)
  37690. {
  37691. EXPECT_DECLS;
  37692. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37693. const unsigned char malformed_alt_name_cert[] = {
  37694. 0x30, 0x82, 0x02, 0xf9, 0x30, 0x82, 0x01, 0xe1, 0xa0, 0x03, 0x02, 0x01,
  37695. 0x02, 0x02, 0x02, 0x10, 0x21, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  37696. 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x0f, 0x31, 0x0d,
  37697. 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61, 0x31,
  37698. 0x31, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x32, 0x30, 0x37, 0x31,
  37699. 0x37, 0x32, 0x34, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34, 0x30, 0x32,
  37700. 0x31, 0x34, 0x30, 0x36, 0x32, 0x36, 0x35, 0x33, 0x5a, 0x30, 0x0f, 0x31,
  37701. 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61,
  37702. 0x61, 0x61, 0x30, 0x82, 0x01, 0x20, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  37703. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
  37704. 0x0d, 0x00, 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa8,
  37705. 0x8a, 0x5e, 0x26, 0x23, 0x1b, 0x31, 0xd3, 0x37, 0x1a, 0x70, 0xb2, 0xec,
  37706. 0x3f, 0x74, 0xd4, 0xb4, 0x44, 0xe3, 0x7a, 0xa5, 0xc0, 0xf5, 0xaa, 0x97,
  37707. 0x26, 0x9a, 0x04, 0xff, 0xda, 0xbe, 0xe5, 0x09, 0x03, 0x98, 0x3d, 0xb5,
  37708. 0xbf, 0x01, 0x2c, 0x9a, 0x0a, 0x3a, 0xfb, 0xbc, 0x3c, 0xe7, 0xbe, 0x83,
  37709. 0x5c, 0xb3, 0x70, 0xe8, 0x5c, 0xe3, 0xd1, 0x83, 0xc3, 0x94, 0x08, 0xcd,
  37710. 0x1a, 0x87, 0xe5, 0xe0, 0x5b, 0x9c, 0x5c, 0x6e, 0xb0, 0x7d, 0xe2, 0x58,
  37711. 0x6c, 0xc3, 0xb5, 0xc8, 0x9d, 0x11, 0xf1, 0x5d, 0x96, 0x0d, 0x66, 0x1e,
  37712. 0x56, 0x7f, 0x8f, 0x59, 0xa7, 0xa5, 0xe1, 0xc5, 0xe7, 0x81, 0x4c, 0x09,
  37713. 0x9d, 0x5e, 0x96, 0xf0, 0x9a, 0xc2, 0x8b, 0x70, 0xd5, 0xab, 0x79, 0x58,
  37714. 0x5d, 0xb7, 0x58, 0xaa, 0xfd, 0x75, 0x52, 0xaa, 0x4b, 0xa7, 0x25, 0x68,
  37715. 0x76, 0x59, 0x00, 0xee, 0x78, 0x2b, 0x91, 0xc6, 0x59, 0x91, 0x99, 0x38,
  37716. 0x3e, 0xa1, 0x76, 0xc3, 0xf5, 0x23, 0x6b, 0xe6, 0x07, 0xea, 0x63, 0x1c,
  37717. 0x97, 0x49, 0xef, 0xa0, 0xfe, 0xfd, 0x13, 0xc9, 0xa9, 0x9f, 0xc2, 0x0b,
  37718. 0xe6, 0x87, 0x92, 0x5b, 0xcc, 0xf5, 0x42, 0x95, 0x4a, 0xa4, 0x6d, 0x64,
  37719. 0xba, 0x7d, 0xce, 0xcb, 0x04, 0xd0, 0xf8, 0xe7, 0xe3, 0xda, 0x75, 0x60,
  37720. 0xd3, 0x8b, 0x6a, 0x64, 0xfc, 0x78, 0x56, 0x21, 0x69, 0x5a, 0xe8, 0xa7,
  37721. 0x8f, 0xfb, 0x8f, 0x82, 0xe3, 0xae, 0x36, 0xa2, 0x93, 0x66, 0x92, 0xcb,
  37722. 0x82, 0xa3, 0xbe, 0x84, 0x00, 0x86, 0xdc, 0x7e, 0x6d, 0x53, 0x77, 0x84,
  37723. 0x17, 0xb9, 0x55, 0x43, 0x0d, 0xf1, 0x16, 0x1f, 0xd5, 0x43, 0x75, 0x99,
  37724. 0x66, 0x19, 0x52, 0xd0, 0xac, 0x5f, 0x74, 0xad, 0xb2, 0x90, 0x15, 0x50,
  37725. 0x04, 0x74, 0x43, 0xdf, 0x6c, 0x35, 0xd0, 0xfd, 0x32, 0x37, 0xb3, 0x8d,
  37726. 0xf5, 0xe5, 0x09, 0x02, 0x01, 0x03, 0xa3, 0x61, 0x30, 0x5f, 0x30, 0x0c,
  37727. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00,
  37728. 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82,
  37729. 0x04, 0x61, 0x2a, 0x00, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
  37730. 0x04, 0x16, 0x04, 0x14, 0x92, 0x6a, 0x1e, 0x52, 0x3a, 0x1a, 0x57, 0x9f,
  37731. 0xc9, 0x82, 0x9a, 0xce, 0xc8, 0xc0, 0xa9, 0x51, 0x9d, 0x2f, 0xc7, 0x72,
  37732. 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
  37733. 0x14, 0x6b, 0xf9, 0xa4, 0x2d, 0xa5, 0xe9, 0x39, 0x89, 0xa8, 0x24, 0x58,
  37734. 0x79, 0x87, 0x11, 0xfc, 0x6f, 0x07, 0x91, 0xef, 0xa6, 0x30, 0x0d, 0x06,
  37735. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
  37736. 0x03, 0x82, 0x01, 0x01, 0x00, 0x3f, 0xd5, 0x37, 0x2f, 0xc7, 0xf8, 0x8b,
  37737. 0x39, 0x1c, 0xe3, 0xdf, 0x77, 0xee, 0xc6, 0x4b, 0x5f, 0x84, 0xcf, 0xfa,
  37738. 0x33, 0x2c, 0xb2, 0xb5, 0x4b, 0x09, 0xee, 0x56, 0xc0, 0xf2, 0xf0, 0xeb,
  37739. 0xad, 0x1c, 0x02, 0xef, 0xae, 0x09, 0x53, 0xc0, 0x06, 0xad, 0x4e, 0xfd,
  37740. 0x3e, 0x8c, 0x13, 0xb3, 0xbf, 0x80, 0x05, 0x36, 0xb5, 0x3f, 0x2b, 0xc7,
  37741. 0x60, 0x53, 0x14, 0xbf, 0x33, 0x63, 0x47, 0xc3, 0xc6, 0x28, 0xda, 0x10,
  37742. 0x12, 0xe2, 0xc4, 0xeb, 0xc5, 0x64, 0x66, 0xc0, 0xcc, 0x6b, 0x84, 0xda,
  37743. 0x0c, 0xe9, 0xf6, 0xe3, 0xf8, 0x8e, 0x3d, 0x95, 0x5f, 0xba, 0x9f, 0xe1,
  37744. 0xc7, 0xed, 0x6e, 0x97, 0xcc, 0xbd, 0x7d, 0xe5, 0x4e, 0xab, 0xbc, 0x1b,
  37745. 0xf1, 0x3a, 0x09, 0x33, 0x09, 0xe1, 0xcc, 0xec, 0x21, 0x16, 0x8e, 0xb1,
  37746. 0x74, 0x9e, 0xc8, 0x13, 0x7c, 0xdf, 0x07, 0xaa, 0xeb, 0x70, 0xd7, 0x91,
  37747. 0x5c, 0xc4, 0xef, 0x83, 0x88, 0xc3, 0xe4, 0x97, 0xfa, 0xe4, 0xdf, 0xd7,
  37748. 0x0d, 0xff, 0xba, 0x78, 0x22, 0xfc, 0x3f, 0xdc, 0xd8, 0x02, 0x8d, 0x93,
  37749. 0x57, 0xf9, 0x9e, 0x39, 0x3a, 0x77, 0x00, 0xd9, 0x19, 0xaa, 0x68, 0xa1,
  37750. 0xe6, 0x9e, 0x13, 0xeb, 0x37, 0x16, 0xf5, 0x77, 0xa4, 0x0b, 0x40, 0x04,
  37751. 0xd3, 0xa5, 0x49, 0x78, 0x35, 0xfa, 0x3b, 0xf6, 0x02, 0xab, 0x85, 0xee,
  37752. 0xcb, 0x9b, 0x62, 0xda, 0x05, 0x00, 0x22, 0x2f, 0xf8, 0xbd, 0x0b, 0xe5,
  37753. 0x2c, 0xb2, 0x53, 0x78, 0x0a, 0xcb, 0x69, 0xc0, 0xb6, 0x9f, 0x96, 0xff,
  37754. 0x58, 0x22, 0x70, 0x9c, 0x01, 0x2e, 0x56, 0x60, 0x5d, 0x37, 0xe3, 0x40,
  37755. 0x25, 0xc9, 0x90, 0xc8, 0x0f, 0x41, 0x68, 0xb4, 0xfd, 0x10, 0xe2, 0x09,
  37756. 0x99, 0x08, 0x5d, 0x7b, 0xc9, 0xe3, 0x29, 0xd4, 0x5a, 0xcf, 0xc9, 0x34,
  37757. 0x55, 0xa1, 0x40, 0x44, 0xd6, 0x88, 0x16, 0xbb, 0xdd
  37758. };
  37759. X509* x509 = NULL;
  37760. int certSize = (int)sizeof(malformed_alt_name_cert) / sizeof(unsigned char);
  37761. const char *name = "aaaaa";
  37762. int nameLen = (int)XSTRLEN(name);
  37763. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  37764. malformed_alt_name_cert, certSize, SSL_FILETYPE_ASN1));
  37765. /* malformed_alt_name_cert has a malformed alternative
  37766. * name of "a*\0*". Ensure that it does not match "aaaaa" */
  37767. ExpectIntNE(wolfSSL_X509_check_host(x509, name, nameLen,
  37768. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  37769. X509_free(x509);
  37770. #endif
  37771. return EXPECT_RESULT();
  37772. }
  37773. static int test_wolfSSL_X509_name_match(void)
  37774. {
  37775. EXPECT_DECLS;
  37776. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37777. /* A certificate with the subject alternative name a* */
  37778. const unsigned char cert_der[] = {
  37779. 0x30, 0x82, 0x03, 0xac, 0x30, 0x82, 0x02, 0x94, 0xa0, 0x03, 0x02, 0x01,
  37780. 0x02, 0x02, 0x14, 0x0f, 0xa5, 0x10, 0x85, 0xef, 0x58, 0x10, 0x59, 0xfc,
  37781. 0x0f, 0x20, 0x1f, 0x53, 0xf5, 0x30, 0x39, 0x34, 0x49, 0x54, 0x05, 0x30,
  37782. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  37783. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  37784. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  37785. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  37786. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  37787. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  37788. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  37789. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  37790. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  37791. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  37792. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  37793. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  37794. 0x30, 0x32, 0x30, 0x31, 0x35, 0x35, 0x38, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  37795. 0x30, 0x35, 0x32, 0x38, 0x32, 0x30, 0x31, 0x35, 0x35, 0x38, 0x5a, 0x30,
  37796. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  37797. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  37798. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  37799. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  37800. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  37801. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  37802. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  37803. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  37804. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  37805. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  37806. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  37807. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  37808. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xf4, 0xca, 0x3d,
  37809. 0xd4, 0xbc, 0x9b, 0xea, 0x74, 0xfe, 0x73, 0xf4, 0x16, 0x23, 0x0b, 0x4a,
  37810. 0x09, 0x54, 0xf6, 0x7b, 0x10, 0x99, 0x11, 0x93, 0xb2, 0xdb, 0x4d, 0x7d,
  37811. 0x23, 0xab, 0xf9, 0xcd, 0xf6, 0x54, 0xd4, 0xf6, 0x39, 0x57, 0xee, 0x97,
  37812. 0xb2, 0xb9, 0xfc, 0x7e, 0x9c, 0xb3, 0xfb, 0x56, 0xb6, 0x84, 0xd6, 0x2d,
  37813. 0x59, 0x1c, 0xed, 0xda, 0x9b, 0x19, 0xf5, 0x8a, 0xa7, 0x8a, 0x89, 0xd6,
  37814. 0xa1, 0xc0, 0xe6, 0x16, 0xad, 0x04, 0xcf, 0x5a, 0x1f, 0xdf, 0x62, 0x6c,
  37815. 0x68, 0x45, 0xe9, 0x55, 0x2e, 0x42, 0xa3, 0x1b, 0x3b, 0x86, 0x23, 0x22,
  37816. 0xa1, 0x20, 0x48, 0xd1, 0x52, 0xc0, 0x8b, 0xab, 0xe2, 0x8a, 0x15, 0x68,
  37817. 0xbd, 0x89, 0x6f, 0x9f, 0x45, 0x75, 0xb4, 0x27, 0xc1, 0x72, 0x41, 0xfd,
  37818. 0x79, 0x89, 0xb0, 0x74, 0xa2, 0xe9, 0x61, 0x48, 0x4c, 0x54, 0xad, 0x6b,
  37819. 0x61, 0xbf, 0x0e, 0x27, 0x58, 0xb4, 0xf6, 0x9c, 0x2c, 0x9f, 0xc2, 0x3e,
  37820. 0x3b, 0xb3, 0x90, 0x41, 0xbc, 0x61, 0xcd, 0x01, 0x57, 0x90, 0x82, 0xec,
  37821. 0x46, 0xba, 0x4f, 0x89, 0x8e, 0x7f, 0x49, 0x4f, 0x46, 0x69, 0x37, 0x8b,
  37822. 0xa0, 0xba, 0x85, 0xe8, 0x42, 0xff, 0x9a, 0xa1, 0x53, 0x81, 0x5c, 0xf3,
  37823. 0x8e, 0x85, 0x1c, 0xd4, 0x90, 0x60, 0xa0, 0x37, 0x59, 0x04, 0x65, 0xa6,
  37824. 0xb5, 0x12, 0x00, 0xc3, 0x04, 0x51, 0xa7, 0x83, 0x96, 0x62, 0x3d, 0x49,
  37825. 0x97, 0xe8, 0x6b, 0x9a, 0x5d, 0x51, 0x24, 0xee, 0xad, 0x45, 0x18, 0x0f,
  37826. 0x3f, 0x97, 0xec, 0xdf, 0xcf, 0x42, 0x8a, 0x96, 0xc7, 0xd8, 0x82, 0x87,
  37827. 0x7f, 0x57, 0x70, 0x22, 0xfb, 0x29, 0x3e, 0x3c, 0xa3, 0xc1, 0xd5, 0x71,
  37828. 0xb3, 0x84, 0x06, 0x53, 0xa3, 0x86, 0x20, 0x35, 0xe3, 0x41, 0xb9, 0xd8,
  37829. 0x00, 0x22, 0x4f, 0x6d, 0xe6, 0xfd, 0xf0, 0xf4, 0xa2, 0x39, 0x0a, 0x1a,
  37830. 0x23, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x30, 0x30, 0x2e, 0x30, 0x0d,
  37831. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x06, 0x30, 0x04, 0x82, 0x02, 0x61,
  37832. 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  37833. 0x45, 0x05, 0xf3, 0x4d, 0x3e, 0x7e, 0x9c, 0xf5, 0x08, 0xee, 0x2c, 0x13,
  37834. 0x32, 0xe3, 0xf2, 0x14, 0xe8, 0x0e, 0x71, 0x21, 0x30, 0x0d, 0x06, 0x09,
  37835. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  37836. 0x82, 0x01, 0x01, 0x00, 0xa8, 0x28, 0xe5, 0x22, 0x65, 0xcf, 0x47, 0xfe,
  37837. 0x82, 0x17, 0x99, 0x20, 0xdb, 0xb1, 0x57, 0xd4, 0x91, 0x1a, 0x83, 0xde,
  37838. 0xc1, 0xaf, 0xc4, 0x1f, 0xfb, 0xa4, 0x6a, 0xad, 0xdc, 0x58, 0x72, 0xd9,
  37839. 0x9b, 0xab, 0xa5, 0xbb, 0xf4, 0x98, 0xd4, 0xdf, 0x36, 0xcb, 0xb5, 0x78,
  37840. 0xce, 0x4b, 0x25, 0x5b, 0x24, 0x92, 0xfe, 0xe8, 0xd4, 0xe4, 0xbd, 0x6f,
  37841. 0x71, 0x1a, 0x81, 0x2a, 0x6f, 0x35, 0x93, 0xf7, 0xcc, 0xed, 0xe5, 0x06,
  37842. 0xd2, 0x96, 0x41, 0xb5, 0xa9, 0x8a, 0xc0, 0xc9, 0x17, 0xe3, 0x13, 0x5e,
  37843. 0x94, 0x5e, 0xfa, 0xfc, 0xf0, 0x00, 0x2e, 0xe1, 0xd8, 0x1b, 0x23, 0x3f,
  37844. 0x7c, 0x4d, 0x9f, 0xfb, 0xb7, 0x95, 0xc1, 0x94, 0x7f, 0x7f, 0xb5, 0x4f,
  37845. 0x93, 0x6d, 0xc3, 0x2b, 0xb2, 0x28, 0x36, 0xd2, 0x7c, 0x01, 0x3c, 0xae,
  37846. 0x35, 0xdb, 0xc8, 0x95, 0x1b, 0x5f, 0x6c, 0x0f, 0x57, 0xb3, 0xcc, 0x97,
  37847. 0x98, 0x80, 0x06, 0xaa, 0xe4, 0x93, 0x1f, 0xb7, 0xa0, 0x54, 0xf1, 0x4f,
  37848. 0x6f, 0x11, 0xdf, 0xab, 0xd3, 0xbf, 0xf0, 0x3a, 0x81, 0x60, 0xaf, 0x7a,
  37849. 0xf7, 0x09, 0xd5, 0xae, 0x0c, 0x7d, 0xae, 0x8d, 0x47, 0x06, 0xbe, 0x11,
  37850. 0x6e, 0xf8, 0x7e, 0x49, 0xf8, 0xac, 0x24, 0x0a, 0x4b, 0xc2, 0xf6, 0xe8,
  37851. 0x2c, 0xec, 0x35, 0xef, 0xa9, 0x13, 0xb8, 0xd2, 0x9c, 0x92, 0x61, 0x91,
  37852. 0xec, 0x7b, 0x0c, 0xea, 0x9a, 0x71, 0x36, 0x15, 0x34, 0x2b, 0x7a, 0x25,
  37853. 0xac, 0xfe, 0xc7, 0x26, 0x89, 0x70, 0x3e, 0x64, 0x68, 0x97, 0x4b, 0xaa,
  37854. 0xc1, 0x24, 0x14, 0xbd, 0x45, 0x2f, 0xe0, 0xfe, 0xf4, 0x2b, 0x8e, 0x08,
  37855. 0x3e, 0xe4, 0xb5, 0x3d, 0x5d, 0xf4, 0xc3, 0xd6, 0x9c, 0xb5, 0x33, 0x1b,
  37856. 0x3b, 0xda, 0x6e, 0x99, 0x7b, 0x09, 0xd1, 0x30, 0x97, 0x23, 0x52, 0x6d,
  37857. 0x1b, 0x71, 0x3a, 0xf4, 0x54, 0xf0, 0xe5, 0x9e
  37858. };
  37859. WOLFSSL_X509* x509 = NULL;
  37860. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  37861. const char *name1 = "aaaaa";
  37862. int nameLen1 = (int)(XSTRLEN(name1));
  37863. const char *name2 = "a";
  37864. int nameLen2 = (int)(XSTRLEN(name2));
  37865. const char *name3 = "abbbb";
  37866. int nameLen3 = (int)(XSTRLEN(name3));
  37867. const char *name4 = "bbb";
  37868. int nameLen4 = (int)(XSTRLEN(name4));
  37869. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  37870. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  37871. /* Ensure that "a*" matches "aaaaa" */
  37872. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  37873. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37874. /* Ensure that "a*" matches "a" */
  37875. ExpectIntEQ(wolfSSL_X509_check_host(x509, name2, nameLen2,
  37876. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37877. /* Ensure that "a*" matches "abbbb" */
  37878. ExpectIntEQ(wolfSSL_X509_check_host(x509, name3, nameLen3,
  37879. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37880. /* Ensure that "a*" does not match "bbb" */
  37881. ExpectIntNE(wolfSSL_X509_check_host(x509, name4, nameLen4,
  37882. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  37883. wolfSSL_X509_free(x509);
  37884. #endif
  37885. return EXPECT_RESULT();
  37886. }
  37887. static int test_wolfSSL_X509_name_match2(void)
  37888. {
  37889. EXPECT_DECLS;
  37890. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37891. /* A certificate with the subject alternative name a*b* */
  37892. const unsigned char cert_der[] = {
  37893. 0x30, 0x82, 0x03, 0xae, 0x30, 0x82, 0x02, 0x96, 0xa0, 0x03, 0x02, 0x01,
  37894. 0x02, 0x02, 0x14, 0x41, 0x8c, 0x8b, 0xaa, 0x0e, 0xd8, 0x5a, 0xc0, 0x52,
  37895. 0x46, 0x0e, 0xe5, 0xd8, 0xb9, 0x48, 0x93, 0x7e, 0x8a, 0x7c, 0x65, 0x30,
  37896. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  37897. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  37898. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  37899. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  37900. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  37901. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  37902. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  37903. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  37904. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  37905. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  37906. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  37907. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  37908. 0x30, 0x32, 0x30, 0x34, 0x33, 0x34, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  37909. 0x30, 0x35, 0x32, 0x38, 0x32, 0x30, 0x34, 0x33, 0x34, 0x30, 0x5a, 0x30,
  37910. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  37911. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  37912. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  37913. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  37914. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  37915. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  37916. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  37917. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  37918. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  37919. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  37920. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  37921. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  37922. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa5, 0x60, 0x80,
  37923. 0xf3, 0xee, 0x19, 0xd2, 0xe4, 0x15, 0x94, 0x54, 0x12, 0x88, 0xee, 0xda,
  37924. 0x11, 0x11, 0x87, 0x99, 0x88, 0xb3, 0x71, 0xc7, 0x97, 0x78, 0x1b, 0x57,
  37925. 0x37, 0x1d, 0x0b, 0x1f, 0x2f, 0x2c, 0x35, 0x13, 0x75, 0xd3, 0x31, 0x3e,
  37926. 0x6f, 0x80, 0x21, 0xa5, 0xa3, 0xad, 0x10, 0x81, 0xb6, 0x37, 0xd4, 0x55,
  37927. 0x2e, 0xc1, 0xb8, 0x37, 0xa3, 0x3c, 0xe8, 0x81, 0x03, 0x3c, 0xda, 0x5f,
  37928. 0x6f, 0x45, 0x32, 0x2b, 0x0e, 0x99, 0x27, 0xfd, 0xe5, 0x6c, 0x07, 0xd9,
  37929. 0x4e, 0x0a, 0x8b, 0x23, 0x74, 0x96, 0x25, 0x97, 0xae, 0x6d, 0x19, 0xba,
  37930. 0xbf, 0x0f, 0xc8, 0xa1, 0xe5, 0xea, 0xa8, 0x00, 0x09, 0xc3, 0x9a, 0xef,
  37931. 0x09, 0x33, 0xc1, 0x33, 0x2e, 0x7b, 0x6d, 0xa7, 0x66, 0x87, 0xb6, 0x3a,
  37932. 0xb9, 0xdb, 0x4c, 0x5e, 0xb5, 0x55, 0x69, 0x37, 0x17, 0x92, 0x1f, 0xe3,
  37933. 0x53, 0x1a, 0x2d, 0x25, 0xd0, 0xcf, 0x72, 0x37, 0xc2, 0x89, 0x83, 0x78,
  37934. 0xcf, 0xac, 0x2e, 0x46, 0x92, 0x5c, 0x4a, 0xba, 0x7d, 0xa0, 0x22, 0x34,
  37935. 0xb1, 0x22, 0x26, 0x99, 0xda, 0xe8, 0x97, 0xe2, 0x0c, 0xd3, 0xbc, 0x97,
  37936. 0x7e, 0xa8, 0xb9, 0xe3, 0xe2, 0x7f, 0x56, 0xef, 0x22, 0xee, 0x15, 0x95,
  37937. 0xa6, 0xd1, 0xf4, 0xa7, 0xac, 0x4a, 0xab, 0xc1, 0x1a, 0xda, 0xc5, 0x5f,
  37938. 0xa5, 0x5e, 0x2f, 0x15, 0x9c, 0x36, 0xbe, 0xd3, 0x47, 0xb6, 0x86, 0xb9,
  37939. 0xc6, 0x59, 0x39, 0x36, 0xad, 0x84, 0x53, 0x95, 0x72, 0x91, 0x89, 0x51,
  37940. 0x32, 0x77, 0xf1, 0xa5, 0x93, 0xfe, 0xf0, 0x41, 0x7c, 0x64, 0xf1, 0xb0,
  37941. 0x8b, 0x81, 0x8d, 0x3a, 0x2c, 0x9e, 0xbe, 0x2e, 0x8b, 0xf7, 0x80, 0x63,
  37942. 0x35, 0x32, 0xfa, 0x26, 0xe0, 0x63, 0xbf, 0x5e, 0xaf, 0xf0, 0x08, 0xe0,
  37943. 0x80, 0x65, 0x38, 0xfa, 0x21, 0xaa, 0x91, 0x34, 0x48, 0x3d, 0x32, 0x5c,
  37944. 0xbf, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x32, 0x30, 0x30, 0x30, 0x0f,
  37945. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82, 0x04, 0x61,
  37946. 0x2a, 0x62, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16,
  37947. 0x04, 0x14, 0x3d, 0x55, 0x74, 0xf8, 0x3a, 0x26, 0x03, 0x8c, 0x6a, 0x2e,
  37948. 0x91, 0x0e, 0x18, 0x70, 0xb4, 0xa4, 0xcc, 0x04, 0x00, 0xd3, 0x30, 0x0d,
  37949. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  37950. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x8f, 0x3b, 0xff, 0x46, 0x0c, 0xb5,
  37951. 0x21, 0xdc, 0xcf, 0x61, 0x9a, 0x25, 0x93, 0x99, 0x68, 0x2f, 0x16, 0x71,
  37952. 0x15, 0x00, 0x5f, 0xb0, 0x9b, 0x43, 0x5c, 0x47, 0xe2, 0x8e, 0xc8, 0xea,
  37953. 0xb3, 0x30, 0x4d, 0x87, 0x90, 0xcf, 0x24, 0x37, 0x5c, 0xfd, 0xc8, 0xc6,
  37954. 0x09, 0x36, 0xb2, 0xfb, 0xfd, 0xc1, 0x82, 0x92, 0x77, 0x5b, 0x9d, 0xeb,
  37955. 0xac, 0x47, 0xbc, 0xda, 0x7c, 0x89, 0x19, 0x03, 0x9e, 0xcd, 0x96, 0x2a,
  37956. 0x90, 0x55, 0x23, 0x19, 0xac, 0x9d, 0x49, 0xfb, 0xa0, 0x31, 0x7d, 0x6b,
  37957. 0x1a, 0x16, 0x13, 0xb1, 0xa9, 0xc9, 0xc4, 0xaf, 0xf1, 0xb4, 0xa7, 0x9b,
  37958. 0x08, 0x64, 0x6a, 0x09, 0xcd, 0x4a, 0x03, 0x4c, 0x93, 0xb6, 0xcf, 0x29,
  37959. 0xdb, 0x56, 0x88, 0x8e, 0xed, 0x08, 0x6d, 0x8d, 0x76, 0xa3, 0xd7, 0xc6,
  37960. 0x69, 0xa1, 0xf5, 0xd2, 0xd0, 0x0a, 0x4b, 0xfa, 0x88, 0x66, 0x6c, 0xe5,
  37961. 0x4a, 0xee, 0x13, 0xad, 0xad, 0x22, 0x25, 0x73, 0x39, 0x56, 0x74, 0x0e,
  37962. 0xda, 0xcd, 0x35, 0x67, 0xe3, 0x81, 0x5c, 0xc5, 0xae, 0x3c, 0x4f, 0x47,
  37963. 0x3e, 0x97, 0xde, 0xac, 0xf6, 0xe1, 0x26, 0xe2, 0xe0, 0x66, 0x48, 0x20,
  37964. 0x7c, 0x02, 0x81, 0x3e, 0x7d, 0x34, 0xb7, 0x73, 0x3e, 0x2e, 0xd6, 0x20,
  37965. 0x1c, 0xdf, 0xf1, 0xae, 0x86, 0x8b, 0xb2, 0xc2, 0x9b, 0x68, 0x9c, 0xf6,
  37966. 0x1a, 0x5e, 0x30, 0x06, 0x39, 0x0a, 0x1f, 0x7b, 0xd7, 0x18, 0x4b, 0x06,
  37967. 0x9d, 0xff, 0x84, 0x57, 0xcc, 0x92, 0xad, 0x81, 0x0a, 0x19, 0x11, 0xc4,
  37968. 0xac, 0x59, 0x00, 0xe8, 0x5a, 0x70, 0x78, 0xd6, 0x9f, 0xe0, 0x82, 0x2a,
  37969. 0x1f, 0x09, 0x36, 0x1c, 0x52, 0x98, 0xf7, 0x95, 0x8f, 0xf9, 0x48, 0x4f,
  37970. 0x30, 0x52, 0xb5, 0xf3, 0x8d, 0x13, 0x93, 0x27, 0xbe, 0xb4, 0x75, 0x39,
  37971. 0x65, 0xc6, 0x48, 0x4e, 0x32, 0xd7, 0xf4, 0xc3, 0x26, 0x8d
  37972. };
  37973. WOLFSSL_X509* x509 = NULL;
  37974. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  37975. const char *name1 = "ab";
  37976. int nameLen1 = (int)(XSTRLEN(name1));
  37977. const char *name2 = "acccbccc";
  37978. int nameLen2 = (int)(XSTRLEN(name2));
  37979. const char *name3 = "accb";
  37980. int nameLen3 = (int)(XSTRLEN(name3));
  37981. const char *name4 = "accda";
  37982. int nameLen4 = (int)(XSTRLEN(name4));
  37983. const char *name5 = "acc\0bcc";
  37984. int nameLen5 = 7;
  37985. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  37986. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  37987. /* Ensure that "a*b*" matches "ab" */
  37988. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  37989. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37990. /* Ensure that "a*b*" matches "acccbccc" */
  37991. ExpectIntEQ(wolfSSL_X509_check_host(x509, name2, nameLen2,
  37992. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37993. /* Ensure that "a*b*" matches "accb" */
  37994. ExpectIntEQ(wolfSSL_X509_check_host(x509, name3, nameLen3,
  37995. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37996. /* Ensure that "a*b*" does not match "accda" */
  37997. ExpectIntNE(wolfSSL_X509_check_host(x509, name4, nameLen4,
  37998. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37999. /* Ensure that "a*b*" matches "ab", testing openssl behavior replication
  38000. * on check len input handling, 0 for len is OK as it should then use
  38001. * strlen(name1) */
  38002. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, 0,
  38003. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  38004. /* Openssl also allows for len to include NULL terminator */
  38005. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1 + 1,
  38006. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  38007. /* Ensure that check string with NULL terminator in middle is
  38008. * rejected */
  38009. ExpectIntNE(wolfSSL_X509_check_host(x509, name5, nameLen5,
  38010. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  38011. wolfSSL_X509_free(x509);
  38012. #endif
  38013. return EXPECT_RESULT();
  38014. }
  38015. static int test_wolfSSL_X509_name_match3(void)
  38016. {
  38017. EXPECT_DECLS;
  38018. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  38019. /* A certificate with the subject alternative name *.example.com */
  38020. const unsigned char cert_der[] = {
  38021. 0x30, 0x82, 0x03, 0xb7, 0x30, 0x82, 0x02, 0x9f, 0xa0, 0x03, 0x02, 0x01,
  38022. 0x02, 0x02, 0x14, 0x59, 0xbb, 0xf6, 0xde, 0xb8, 0x3d, 0x0e, 0x8c, 0xe4,
  38023. 0xbd, 0x98, 0xa3, 0xbe, 0x3e, 0x8f, 0xdc, 0xbd, 0x7f, 0xcc, 0xae, 0x30,
  38024. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  38025. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  38026. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  38027. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  38028. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  38029. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  38030. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  38031. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  38032. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  38033. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  38034. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  38035. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  38036. 0x31, 0x30, 0x30, 0x33, 0x37, 0x34, 0x39, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  38037. 0x30, 0x35, 0x32, 0x39, 0x30, 0x30, 0x33, 0x37, 0x34, 0x39, 0x5a, 0x30,
  38038. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  38039. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  38040. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  38041. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  38042. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  38043. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  38044. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  38045. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  38046. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  38047. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  38048. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  38049. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  38050. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xda, 0x78, 0x16,
  38051. 0x05, 0x65, 0xf2, 0x85, 0xf2, 0x61, 0x7f, 0xb1, 0x4d, 0x73, 0xe2, 0x82,
  38052. 0xb5, 0x3d, 0xf7, 0x9d, 0x05, 0x65, 0xed, 0x9d, 0xc3, 0x29, 0x7a, 0x92,
  38053. 0x2c, 0x06, 0x5f, 0xc8, 0x13, 0x55, 0x42, 0x4e, 0xbd, 0xe2, 0x56, 0x2a,
  38054. 0x4b, 0xac, 0xe6, 0x1b, 0x10, 0xc9, 0xdb, 0x9a, 0x45, 0x36, 0xed, 0xf3,
  38055. 0x26, 0x8c, 0x22, 0x88, 0x1e, 0x6d, 0x2b, 0x41, 0xfa, 0x0d, 0x43, 0x88,
  38056. 0x88, 0xde, 0x8d, 0x2e, 0xca, 0x6e, 0x7c, 0x62, 0x66, 0x3e, 0xfa, 0x4e,
  38057. 0x71, 0xea, 0x7d, 0x3b, 0x32, 0x33, 0x5c, 0x7a, 0x7e, 0xea, 0x74, 0xbd,
  38058. 0xb6, 0x8f, 0x4c, 0x1c, 0x7a, 0x79, 0x94, 0xf1, 0xe8, 0x02, 0x67, 0x98,
  38059. 0x25, 0xb4, 0x31, 0x80, 0xc1, 0xae, 0xbf, 0xef, 0xf2, 0x6c, 0x78, 0x42,
  38060. 0xef, 0xb5, 0xc6, 0x01, 0x47, 0x79, 0x8d, 0x92, 0xce, 0xc1, 0xb5, 0x98,
  38061. 0x76, 0xf0, 0x84, 0xa2, 0x53, 0x90, 0xe5, 0x39, 0xc7, 0xbd, 0xf2, 0xbb,
  38062. 0xe3, 0x3f, 0x00, 0xf6, 0xf0, 0x46, 0x86, 0xee, 0x55, 0xbd, 0x2c, 0x1f,
  38063. 0x97, 0x24, 0x7c, 0xbc, 0xda, 0x2f, 0x1b, 0x53, 0xef, 0x26, 0x56, 0xcc,
  38064. 0xb7, 0xd8, 0xca, 0x17, 0x20, 0x4e, 0x62, 0x03, 0x66, 0x32, 0xb3, 0xd1,
  38065. 0x71, 0x26, 0x6c, 0xff, 0xd1, 0x9e, 0x44, 0x86, 0x2a, 0xae, 0xba, 0x43,
  38066. 0x00, 0x13, 0x7e, 0x50, 0xdd, 0x3e, 0x27, 0x39, 0x70, 0x1c, 0x0c, 0x0b,
  38067. 0xe8, 0xa2, 0xae, 0x03, 0x09, 0x2e, 0xd8, 0x71, 0xee, 0x7b, 0x1a, 0x09,
  38068. 0x2d, 0xe1, 0xd5, 0xde, 0xf5, 0xa3, 0x36, 0x77, 0x90, 0x97, 0x99, 0xd7,
  38069. 0x6c, 0xb7, 0x5c, 0x9d, 0xf7, 0x7e, 0x41, 0x89, 0xfe, 0xe4, 0x08, 0xc6,
  38070. 0x0b, 0xe4, 0x9b, 0x5f, 0x51, 0xa6, 0x08, 0xb8, 0x99, 0x81, 0xe9, 0xce,
  38071. 0xb4, 0x2d, 0xb2, 0x92, 0x9f, 0xe5, 0x1a, 0x98, 0x76, 0x20, 0x70, 0x54,
  38072. 0x93, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x3b, 0x30, 0x39, 0x30, 0x18,
  38073. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x11, 0x30, 0x0f, 0x82, 0x0d, 0x2a,
  38074. 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
  38075. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x60,
  38076. 0xd4, 0x26, 0xbb, 0xcc, 0x7c, 0x29, 0xa2, 0x88, 0x3c, 0x76, 0x7d, 0xb4,
  38077. 0x86, 0x8b, 0x47, 0x64, 0x5b, 0x87, 0xe0, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  38078. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
  38079. 0x01, 0x01, 0x00, 0xc3, 0x0d, 0x03, 0x67, 0xbb, 0x47, 0x8b, 0xf3, 0x20,
  38080. 0xdc, 0x7d, 0x2e, 0xe1, 0xd9, 0xf0, 0x01, 0xc4, 0x66, 0xc2, 0xe1, 0xcd,
  38081. 0xc3, 0x4a, 0x72, 0xf0, 0x6e, 0x38, 0xcf, 0x63, 0x01, 0x96, 0x9e, 0x84,
  38082. 0xb9, 0xce, 0x1d, 0xba, 0x4b, 0xe0, 0x70, 0x86, 0x2b, 0x5a, 0xab, 0xec,
  38083. 0xbf, 0xc2, 0xaa, 0x64, 0xa2, 0x6c, 0xd2, 0x42, 0x52, 0xd4, 0xbe, 0x8a,
  38084. 0xca, 0x9c, 0x03, 0xf3, 0xd6, 0x5f, 0xcd, 0x23, 0x9f, 0xf5, 0xa9, 0x04,
  38085. 0x40, 0x5b, 0x66, 0x78, 0xc0, 0xac, 0xa1, 0xdb, 0x5d, 0xd1, 0x94, 0xfc,
  38086. 0x47, 0x94, 0xf5, 0x45, 0xe3, 0x70, 0x13, 0x3f, 0x66, 0x6d, 0xdd, 0x73,
  38087. 0x68, 0x68, 0xe2, 0xd2, 0x89, 0xcb, 0x7f, 0xc6, 0xca, 0xd6, 0x96, 0x0b,
  38088. 0xcc, 0xdd, 0xa1, 0x74, 0xda, 0x33, 0xe8, 0x9e, 0xda, 0xb7, 0xd9, 0x12,
  38089. 0xab, 0x85, 0x9d, 0x0c, 0xde, 0xa0, 0x7d, 0x7e, 0xa1, 0x91, 0xed, 0xe5,
  38090. 0x32, 0x7c, 0xc5, 0xea, 0x1d, 0x4a, 0xb5, 0x38, 0x63, 0x17, 0xf3, 0x4f,
  38091. 0x2c, 0x4a, 0x58, 0x86, 0x09, 0x33, 0x86, 0xc4, 0xe7, 0x56, 0x6f, 0x32,
  38092. 0x71, 0xb7, 0xd0, 0x83, 0x12, 0x9e, 0x26, 0x0a, 0x3a, 0x45, 0xcb, 0xd7,
  38093. 0x4e, 0xab, 0xa4, 0xc3, 0xee, 0x4c, 0xc0, 0x38, 0xa1, 0xfa, 0xba, 0xfa,
  38094. 0xb7, 0x80, 0x69, 0x67, 0xa3, 0xef, 0x89, 0xba, 0xce, 0x89, 0x91, 0x3d,
  38095. 0x6a, 0x76, 0xe9, 0x3b, 0x32, 0x86, 0x76, 0x85, 0x6b, 0x4f, 0x7f, 0xbc,
  38096. 0x7a, 0x5b, 0x31, 0x92, 0x79, 0x35, 0xf8, 0xb9, 0xb1, 0xd7, 0xdb, 0xa9,
  38097. 0x6a, 0x8a, 0x91, 0x60, 0x65, 0xd4, 0x76, 0x54, 0x55, 0x57, 0xb9, 0x35,
  38098. 0xe0, 0xf5, 0xbb, 0x8f, 0xd4, 0x40, 0x75, 0xbb, 0x47, 0xa8, 0xf9, 0x0f,
  38099. 0xea, 0xc9, 0x6e, 0x84, 0xd5, 0xf5, 0x58, 0x2d, 0xe5, 0x76, 0x7b, 0xdf,
  38100. 0x97, 0x05, 0x5e, 0xaf, 0x50, 0xf5, 0x48
  38101. };
  38102. WOLFSSL_X509* x509 = NULL;
  38103. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  38104. const char *name1 = "foo.example.com";
  38105. int nameLen1 = (int)(XSTRLEN(name1));
  38106. const char *name2 = "x.y.example.com";
  38107. int nameLen2 = (int)(XSTRLEN(name2));
  38108. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  38109. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  38110. /* Ensure that "*.example.com" matches "foo.example.com" */
  38111. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  38112. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  38113. /* Ensure that "*.example.com" does NOT match "x.y.example.com" */
  38114. ExpectIntNE(wolfSSL_X509_check_host(x509, name2, nameLen2,
  38115. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  38116. wolfSSL_X509_free(x509);
  38117. #endif
  38118. return EXPECT_RESULT();
  38119. }
  38120. static int test_wolfSSL_X509_max_altnames(void)
  38121. {
  38122. EXPECT_DECLS;
  38123. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  38124. /* Only test if max alt names has not been modified */
  38125. #if WOLFSSL_MAX_ALT_NAMES == 128
  38126. WOLFSSL_CTX* ctx = NULL;
  38127. /* File contains a certificate encoded with 130 subject alternative names */
  38128. const char* over_max_altnames_cert = \
  38129. "./certs/test/cert-over-max-altnames.pem";
  38130. #ifndef NO_WOLFSSL_SERVER
  38131. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  38132. #else
  38133. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38134. #endif
  38135. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx,
  38136. over_max_altnames_cert, NULL, WOLFSSL_LOAD_FLAG_NONE),
  38137. WOLFSSL_SUCCESS);
  38138. wolfSSL_CTX_free(ctx);
  38139. #endif
  38140. #endif
  38141. return EXPECT_RESULT();
  38142. }
  38143. static int test_wolfSSL_X509_max_name_constraints(void)
  38144. {
  38145. EXPECT_DECLS;
  38146. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  38147. !defined(IGNORE_NAME_CONSTRAINTS)
  38148. /* Only test if max name constraints has not been modified */
  38149. #if WOLFSSL_MAX_NAME_CONSTRAINTS == 128
  38150. WOLFSSL_CTX* ctx = NULL;
  38151. /* File contains a certificate with 130 name constraints */
  38152. const char* over_max_nc = "./certs/test/cert-over-max-nc.pem";
  38153. #ifndef NO_WOLFSSL_SERVER
  38154. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  38155. #else
  38156. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38157. #endif
  38158. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, over_max_nc,
  38159. NULL, WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  38160. wolfSSL_CTX_free(ctx);
  38161. #endif
  38162. #endif
  38163. return EXPECT_RESULT();
  38164. }
  38165. static int test_wolfSSL_X509(void)
  38166. {
  38167. EXPECT_DECLS;
  38168. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38169. !defined(NO_RSA)
  38170. X509* x509 = NULL;
  38171. #ifndef NO_BIO
  38172. BIO* bio = NULL;
  38173. X509_STORE_CTX* ctx = NULL;
  38174. X509_STORE* store = NULL;
  38175. #endif
  38176. char der[] = "certs/ca-cert.der";
  38177. XFILE fp = XBADFILE;
  38178. ExpectNotNull(x509 = X509_new());
  38179. X509_free(x509);
  38180. x509 = NULL;
  38181. #ifndef NO_BIO
  38182. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  38183. SSL_FILETYPE_PEM));
  38184. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38185. #ifdef WOLFSSL_CERT_GEN
  38186. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  38187. #endif
  38188. ExpectNotNull(ctx = X509_STORE_CTX_new());
  38189. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  38190. ExpectNotNull(store = X509_STORE_new());
  38191. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  38192. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  38193. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  38194. X509_STORE_CTX_free(ctx);
  38195. X509_STORE_free(store);
  38196. X509_free(x509);
  38197. x509 = NULL;
  38198. BIO_free(bio);
  38199. #endif
  38200. /** d2i_X509_fp test **/
  38201. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  38202. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  38203. ExpectNotNull(x509);
  38204. X509_free(x509);
  38205. x509 = NULL;
  38206. if (fp != XBADFILE) {
  38207. XFCLOSE(fp);
  38208. fp = XBADFILE;
  38209. }
  38210. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  38211. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  38212. ExpectNotNull(x509);
  38213. X509_free(x509);
  38214. if (fp != XBADFILE)
  38215. XFCLOSE(fp);
  38216. /* X509_up_ref test */
  38217. ExpectIntEQ(X509_up_ref(NULL), 0);
  38218. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  38219. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  38220. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  38221. X509_free(x509); /* refCount = 2 */
  38222. X509_free(x509); /* refCount = 1 */
  38223. X509_free(x509); /* refCount = 0, free */
  38224. #endif
  38225. return EXPECT_RESULT();
  38226. }
  38227. static int test_wolfSSL_X509_get_ext_count(void)
  38228. {
  38229. EXPECT_DECLS;
  38230. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38231. !defined(NO_RSA)
  38232. int ret = 0;
  38233. WOLFSSL_X509* x509 = NULL;
  38234. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  38235. XFILE f = XBADFILE;
  38236. /* NULL parameter check */
  38237. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  38238. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  38239. SSL_FILETYPE_PEM));
  38240. ExpectIntEQ(X509_get_ext_count(x509), 5);
  38241. wolfSSL_X509_free(x509);
  38242. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  38243. SSL_FILETYPE_PEM));
  38244. ExpectIntEQ(X509_get_ext_count(x509), 5);
  38245. wolfSSL_X509_free(x509);
  38246. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  38247. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  38248. if (f != XBADFILE)
  38249. XFCLOSE(f);
  38250. /* wolfSSL_X509_get_ext_count() valid input */
  38251. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  38252. /* wolfSSL_X509_get_ext_count() NULL argument */
  38253. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  38254. wolfSSL_X509_free(x509);
  38255. #endif
  38256. return EXPECT_RESULT();
  38257. }
  38258. static int test_wolfSSL_X509_sign2(void)
  38259. {
  38260. EXPECT_DECLS;
  38261. /* test requires WOLFSSL_AKID_NAME to match expected output */
  38262. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  38263. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  38264. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  38265. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  38266. defined(WOLFSSL_IP_ALT_NAME))
  38267. WOLFSSL_X509 *x509 = NULL;
  38268. WOLFSSL_X509 *ca = NULL;
  38269. const unsigned char *der = NULL;
  38270. const unsigned char *pt = NULL;
  38271. WOLFSSL_EVP_PKEY *priv = NULL;
  38272. WOLFSSL_X509_NAME *name = NULL;
  38273. int derSz;
  38274. #ifndef NO_ASN_TIME
  38275. WOLFSSL_ASN1_TIME *notBefore = NULL;
  38276. WOLFSSL_ASN1_TIME *notAfter = NULL;
  38277. const int year = 365*24*60*60;
  38278. const int day = 24*60*60;
  38279. const int hour = 60*60;
  38280. const int mini = 60;
  38281. time_t t;
  38282. #endif
  38283. const unsigned char expected[] = {
  38284. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  38285. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  38286. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  38287. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  38288. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  38289. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  38290. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  38291. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  38292. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  38293. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  38294. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  38295. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  38296. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  38297. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  38298. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  38299. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  38300. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  38301. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  38302. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  38303. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  38304. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  38305. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  38306. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  38307. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  38308. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  38309. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  38310. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  38311. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  38312. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  38313. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  38314. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  38315. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  38316. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  38317. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  38318. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  38319. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  38320. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  38321. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  38322. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  38323. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  38324. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  38325. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  38326. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  38327. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  38328. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  38329. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  38330. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  38331. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  38332. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  38333. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  38334. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  38335. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  38336. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  38337. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  38338. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  38339. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  38340. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  38341. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  38342. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  38343. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  38344. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  38345. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  38346. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  38347. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  38348. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  38349. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  38350. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  38351. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  38352. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  38353. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  38354. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  38355. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  38356. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  38357. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  38358. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  38359. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  38360. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  38361. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  38362. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  38363. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  38364. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  38365. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  38366. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  38367. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  38368. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  38369. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  38370. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  38371. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  38372. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  38373. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  38374. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  38375. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  38376. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  38377. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  38378. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  38379. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  38380. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  38381. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  38382. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  38383. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  38384. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  38385. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  38386. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  38387. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  38388. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  38389. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  38390. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  38391. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  38392. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  38393. };
  38394. pt = ca_key_der_2048;
  38395. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  38396. sizeof_ca_key_der_2048));
  38397. pt = client_cert_der_2048;
  38398. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  38399. sizeof_client_cert_der_2048));
  38400. pt = ca_cert_der_2048;
  38401. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  38402. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  38403. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  38404. #ifndef NO_ASN_TIME
  38405. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  38406. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  38407. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  38408. ExpectIntEQ(notAfter->length, 13);
  38409. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  38410. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  38411. #endif
  38412. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  38413. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  38414. ExpectIntEQ(derSz, sizeof(expected));
  38415. #ifndef NO_ASN_TIME
  38416. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  38417. #endif
  38418. wolfSSL_X509_free(ca);
  38419. wolfSSL_X509_free(x509);
  38420. wolfSSL_EVP_PKEY_free(priv);
  38421. #ifndef NO_ASN_TIME
  38422. wolfSSL_ASN1_TIME_free(notBefore);
  38423. wolfSSL_ASN1_TIME_free(notAfter);
  38424. #endif
  38425. #endif
  38426. return EXPECT_RESULT();
  38427. }
  38428. static int test_wolfSSL_X509_sign(void)
  38429. {
  38430. EXPECT_DECLS;
  38431. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  38432. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  38433. int ret = 0;
  38434. char *cn = NULL;
  38435. word32 cnSz = 0;
  38436. X509_NAME *name = NULL;
  38437. X509 *x509 = NULL;
  38438. X509 *ca = NULL;
  38439. DecodedCert dCert;
  38440. EVP_PKEY *pub = NULL;
  38441. EVP_PKEY *priv = NULL;
  38442. EVP_MD_CTX *mctx = NULL;
  38443. #if defined(USE_CERT_BUFFERS_1024)
  38444. const unsigned char* rsaPriv = client_key_der_1024;
  38445. const unsigned char* rsaPub = client_keypub_der_1024;
  38446. const unsigned char* certIssuer = client_cert_der_1024;
  38447. long clientKeySz = (long)sizeof_client_key_der_1024;
  38448. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  38449. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  38450. #elif defined(USE_CERT_BUFFERS_2048)
  38451. const unsigned char* rsaPriv = client_key_der_2048;
  38452. const unsigned char* rsaPub = client_keypub_der_2048;
  38453. const unsigned char* certIssuer = client_cert_der_2048;
  38454. long clientKeySz = (long)sizeof_client_key_der_2048;
  38455. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  38456. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  38457. #endif
  38458. byte sn[16];
  38459. int snSz = sizeof(sn);
  38460. /* Set X509_NAME fields */
  38461. ExpectNotNull(name = X509_NAME_new());
  38462. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  38463. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  38464. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  38465. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  38466. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  38467. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  38468. /* Get private and public keys */
  38469. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  38470. clientKeySz));
  38471. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  38472. ExpectNotNull(x509 = X509_new());
  38473. /* Set version 3 */
  38474. ExpectIntNE(X509_set_version(x509, 2L), 0);
  38475. /* Set subject name, add pubkey, and sign certificate */
  38476. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  38477. X509_NAME_free(name);
  38478. name = NULL;
  38479. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  38480. #ifdef WOLFSSL_ALT_NAMES
  38481. /* Add some subject alt names */
  38482. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  38483. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  38484. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  38485. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  38486. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  38487. "sphygmomanometer",
  38488. ASN_DNS_TYPE), SSL_SUCCESS);
  38489. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  38490. "supercalifragilisticexpialidocious",
  38491. ASN_DNS_TYPE), SSL_SUCCESS);
  38492. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  38493. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  38494. ASN_DNS_TYPE), SSL_SUCCESS);
  38495. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  38496. {
  38497. unsigned char ip4_type[] = {127,128,0,255};
  38498. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  38499. 0xff, 0xee, 0x99, 0x88,
  38500. 0x77, 0x66, 0x55, 0x44,
  38501. 0x00, 0x33, 0x22, 0x11};
  38502. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  38503. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  38504. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  38505. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  38506. }
  38507. #endif
  38508. #endif /* WOLFSSL_ALT_NAMES */
  38509. {
  38510. ASN1_UTCTIME* infinite_past = NULL;
  38511. ExpectNotNull(infinite_past = ASN1_UTCTIME_set(NULL, 0));
  38512. ExpectIntEQ(X509_set1_notBefore(x509, infinite_past), 1);
  38513. ASN1_UTCTIME_free(infinite_past);
  38514. }
  38515. /* test valid sign case */
  38516. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  38517. /* test valid X509_sign_ctx case */
  38518. ExpectNotNull(mctx = EVP_MD_CTX_new());
  38519. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  38520. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  38521. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  38522. ExpectIntEQ(X509_get_ext_count(x509), 1);
  38523. #endif
  38524. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  38525. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  38526. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  38527. #endif
  38528. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  38529. WOLFSSL_SUCCESS);
  38530. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  38531. /* Variation in size depends on ASN.1 encoding when MSB is set.
  38532. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  38533. * with the MSB set. See GenerateInteger in asn.c */
  38534. #ifndef USE_CERT_BUFFERS_1024
  38535. #ifndef WOLFSSL_ALT_NAMES
  38536. /* Valid case - size should be 781-786 with 16 byte serial number */
  38537. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  38538. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  38539. /* Valid case - size should be 955-960 with 16 byte serial number */
  38540. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  38541. #else
  38542. /* Valid case - size should be 926-931 with 16 byte serial number */
  38543. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  38544. #endif
  38545. #else
  38546. #ifndef WOLFSSL_ALT_NAMES
  38547. /* Valid case - size should be 537-542 with 16 byte serial number */
  38548. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  38549. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  38550. /* Valid case - size should be 695-670 with 16 byte serial number */
  38551. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  38552. #else
  38553. /* Valid case - size should be 666-671 with 16 byte serial number */
  38554. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  38555. #endif
  38556. #endif
  38557. /* check that issuer name is as expected after signature */
  38558. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  38559. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  38560. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  38561. ExpectNotNull(name = X509_get_subject_name(ca));
  38562. cnSz = X509_NAME_get_sz(name);
  38563. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  38564. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  38565. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  38566. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  38567. cn = NULL;
  38568. #ifdef WOLFSSL_MULTI_ATTRIB
  38569. /* test adding multiple OU's to the signer */
  38570. ExpectNotNull(name = X509_get_subject_name(ca));
  38571. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  38572. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  38573. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  38574. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  38575. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  38576. #endif
  38577. ExpectNotNull(name = X509_get_subject_name(ca));
  38578. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  38579. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  38580. ExpectNotNull(name = X509_get_issuer_name(x509));
  38581. cnSz = X509_NAME_get_sz(name);
  38582. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  38583. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  38584. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  38585. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  38586. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  38587. cn = NULL;
  38588. FreeDecodedCert(&dCert);
  38589. /* Test invalid parameters */
  38590. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  38591. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  38592. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  38593. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  38594. EVP_MD_CTX_free(mctx);
  38595. mctx = NULL;
  38596. ExpectNotNull(mctx = EVP_MD_CTX_new());
  38597. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  38598. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  38599. /* test invalid version number */
  38600. #if defined(OPENSSL_ALL)
  38601. ExpectIntNE(X509_set_version(x509, 6L), 0);
  38602. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  38603. /* uses ParseCert which fails on bad version number */
  38604. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  38605. #endif
  38606. EVP_MD_CTX_free(mctx);
  38607. EVP_PKEY_free(priv);
  38608. EVP_PKEY_free(pub);
  38609. X509_free(x509);
  38610. X509_free(ca);
  38611. #endif
  38612. return EXPECT_RESULT();
  38613. }
  38614. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  38615. {
  38616. EXPECT_DECLS;
  38617. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  38618. X509* x509 = NULL;
  38619. const X509_ALGOR* alg;
  38620. ExpectNotNull(x509 = X509_new());
  38621. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  38622. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  38623. X509_free(x509);
  38624. #endif
  38625. return EXPECT_RESULT();
  38626. }
  38627. static int test_wolfSSL_X509_ALGOR_get0(void)
  38628. {
  38629. EXPECT_DECLS;
  38630. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  38631. !defined(NO_SHA256) && !defined(NO_RSA)
  38632. X509* x509 = NULL;
  38633. const ASN1_OBJECT* obj = NULL;
  38634. const X509_ALGOR* alg = NULL;
  38635. int pptype = 0;
  38636. const void *ppval = NULL;
  38637. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  38638. SSL_FILETYPE_PEM));
  38639. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  38640. /* Invalid case */
  38641. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  38642. ExpectNull(obj);
  38643. /* Valid case */
  38644. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  38645. ExpectNotNull(obj);
  38646. ExpectNull(ppval);
  38647. ExpectIntNE(pptype, 0);
  38648. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  38649. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  38650. X509_free(x509);
  38651. #endif
  38652. return EXPECT_RESULT();
  38653. }
  38654. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  38655. {
  38656. EXPECT_DECLS;
  38657. #if defined(OPENSSL_EXTRA)
  38658. X509_VERIFY_PARAM *paramTo = NULL;
  38659. X509_VERIFY_PARAM *paramFrom = NULL;
  38660. char testIPv4[] = "127.0.0.1";
  38661. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  38662. char testhostName1[] = "foo.hoge.com";
  38663. char testhostName2[] = "foobar.hoge.com";
  38664. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  38665. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  38666. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  38667. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  38668. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  38669. (int)XSTRLEN(testhostName1)), 1);
  38670. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  38671. (int)XSTRLEN(testhostName1)));
  38672. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  38673. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  38674. ExpectIntEQ(0x01, paramFrom->hostFlags);
  38675. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  38676. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  38677. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  38678. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  38679. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  38680. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  38681. /* null pointer */
  38682. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  38683. /* in the case of "from" null, returns success */
  38684. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  38685. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  38686. /* inherit flags test : VPARAM_DEFAULT */
  38687. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  38688. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  38689. (int)XSTRLEN(testhostName1)));
  38690. ExpectIntEQ(0x01, paramTo->hostFlags);
  38691. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  38692. /* inherit flags test : VPARAM OVERWRITE */
  38693. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  38694. (int)XSTRLEN(testhostName2)), 1);
  38695. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  38696. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  38697. if (paramTo != NULL) {
  38698. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  38699. }
  38700. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  38701. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  38702. (int)XSTRLEN(testhostName1)));
  38703. ExpectIntEQ(0x01, paramTo->hostFlags);
  38704. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  38705. /* inherit flags test : VPARAM_RESET_FLAGS */
  38706. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  38707. (int)XSTRLEN(testhostName2)), 1);
  38708. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  38709. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  38710. if (paramTo != NULL) {
  38711. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  38712. }
  38713. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  38714. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  38715. (int)XSTRLEN(testhostName1)));
  38716. ExpectIntEQ(0x01, paramTo->hostFlags);
  38717. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  38718. /* inherit flags test : VPARAM_LOCKED */
  38719. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  38720. (int)XSTRLEN(testhostName2)), 1);
  38721. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  38722. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  38723. if (paramTo != NULL) {
  38724. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  38725. }
  38726. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  38727. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  38728. (int)XSTRLEN(testhostName2)));
  38729. ExpectIntEQ(0x00, paramTo->hostFlags);
  38730. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  38731. /* test for incorrect parameters */
  38732. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  38733. 0);
  38734. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  38735. /* inherit flags test : VPARAM_ONCE, not testable yet */
  38736. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  38737. 1);
  38738. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  38739. X509_V_FLAG_CRL_CHECK_ALL);
  38740. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  38741. X509_V_FLAG_CRL_CHECK_ALL), 1);
  38742. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  38743. X509_VERIFY_PARAM_free(paramTo);
  38744. X509_VERIFY_PARAM_free(paramFrom);
  38745. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  38746. #endif
  38747. return EXPECT_RESULT();
  38748. }
  38749. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  38750. !defined(WOLFSSL_HOSTNAME_VERIFY_ALT_NAME_ONLY)
  38751. static int test_wolfSSL_check_domain_verify_count = 0;
  38752. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  38753. WOLFSSL_X509_STORE_CTX* store)
  38754. {
  38755. EXPECT_DECLS;
  38756. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  38757. ExpectIntEQ(preverify, 1);
  38758. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  38759. return EXPECT_SUCCESS();
  38760. }
  38761. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  38762. {
  38763. EXPECT_DECLS;
  38764. X509_VERIFY_PARAM *param = NULL;
  38765. ExpectNotNull(param = SSL_get0_param(ssl));
  38766. /* Domain check should only be done on the leaf cert */
  38767. X509_VERIFY_PARAM_set_hostflags(param,
  38768. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  38769. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  38770. "wolfSSL Server Chain", 0), 1);
  38771. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  38772. test_wolfSSL_check_domain_verify_cb);
  38773. return EXPECT_RESULT();
  38774. }
  38775. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  38776. {
  38777. EXPECT_DECLS;
  38778. /* Use a cert with different domains in chain */
  38779. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  38780. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  38781. return EXPECT_RESULT();
  38782. }
  38783. static int test_wolfSSL_check_domain(void)
  38784. {
  38785. EXPECT_DECLS;
  38786. test_ssl_cbf func_cb_client;
  38787. test_ssl_cbf func_cb_server;
  38788. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  38789. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  38790. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  38791. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  38792. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  38793. &func_cb_server, NULL), TEST_SUCCESS);
  38794. /* Should have been called once for each cert in sent chain */
  38795. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  38796. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  38797. #else
  38798. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  38799. #endif
  38800. return EXPECT_RESULT();
  38801. }
  38802. #else
  38803. static int test_wolfSSL_check_domain(void)
  38804. {
  38805. EXPECT_DECLS;
  38806. return EXPECT_RESULT();
  38807. }
  38808. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  38809. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  38810. {
  38811. EXPECT_DECLS;
  38812. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  38813. X509* x509 = NULL;
  38814. X509_PUBKEY* pubKey;
  38815. ExpectNotNull(x509 = X509_new());
  38816. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  38817. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  38818. X509_free(x509);
  38819. #endif
  38820. return EXPECT_RESULT();
  38821. }
  38822. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  38823. {
  38824. EXPECT_DECLS;
  38825. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  38826. !defined(NO_SHA256) && !defined(NO_RSA)
  38827. X509* x509 = NULL;
  38828. ASN1_OBJECT* obj = NULL;
  38829. const ASN1_OBJECT* pa_oid = NULL;
  38830. X509_PUBKEY* pubKey = NULL;
  38831. X509_PUBKEY* pubKey2 = NULL;
  38832. EVP_PKEY* evpKey = NULL;
  38833. const unsigned char *pk = NULL;
  38834. int ppklen;
  38835. int pptype;
  38836. X509_ALGOR *pa = NULL;
  38837. const void *pval;
  38838. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  38839. SSL_FILETYPE_PEM));
  38840. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  38841. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  38842. ExpectNotNull(pk);
  38843. ExpectNotNull(pa);
  38844. ExpectNotNull(pubKey);
  38845. ExpectIntGT(ppklen, 0);
  38846. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  38847. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  38848. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  38849. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  38850. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  38851. ExpectNotNull(pk);
  38852. ExpectNotNull(pa);
  38853. ExpectIntGT(ppklen, 0);
  38854. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  38855. ExpectNotNull(pa_oid);
  38856. ExpectNull(pval);
  38857. ExpectIntEQ(pptype, V_ASN1_NULL);
  38858. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  38859. X509_PUBKEY_free(pubKey2);
  38860. X509_free(x509);
  38861. EVP_PKEY_free(evpKey);
  38862. #endif
  38863. return EXPECT_RESULT();
  38864. }
  38865. static int test_wolfSSL_X509_PUBKEY_EC(void)
  38866. {
  38867. EXPECT_DECLS;
  38868. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  38869. X509* x509 = NULL;
  38870. ASN1_OBJECT* obj = NULL;
  38871. ASN1_OBJECT* poid = NULL;
  38872. const ASN1_OBJECT* pa_oid = NULL;
  38873. X509_PUBKEY* pubKey = NULL;
  38874. X509_PUBKEY* pubKey2 = NULL;
  38875. EVP_PKEY* evpKey = NULL;
  38876. const unsigned char *pk = NULL;
  38877. int ppklen;
  38878. int pptype;
  38879. X509_ALGOR *pa = NULL;
  38880. const void *pval;
  38881. char buf[50];
  38882. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  38883. SSL_FILETYPE_PEM));
  38884. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  38885. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  38886. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  38887. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  38888. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  38889. ExpectNotNull(pk);
  38890. ExpectNotNull(pa);
  38891. ExpectIntGT(ppklen, 0);
  38892. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  38893. ExpectNotNull(pa_oid);
  38894. ExpectNotNull(pval);
  38895. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  38896. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  38897. poid = (ASN1_OBJECT *)pval;
  38898. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  38899. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  38900. X509_PUBKEY_free(pubKey2);
  38901. X509_free(x509);
  38902. EVP_PKEY_free(evpKey);
  38903. #endif
  38904. return EXPECT_RESULT();
  38905. }
  38906. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  38907. {
  38908. EXPECT_DECLS;
  38909. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  38910. word32 bytes;
  38911. #ifdef USE_CERT_BUFFERS_1024
  38912. byte tmp[ONEK_BUF];
  38913. #elif defined(USE_CERT_BUFFERS_2048)
  38914. byte tmp[TWOK_BUF];
  38915. #else
  38916. byte tmp[TWOK_BUF];
  38917. #endif /* END USE_CERT_BUFFERS_1024 */
  38918. const unsigned char* dsaKeyDer = tmp;
  38919. ASN1_OBJECT* obj = NULL;
  38920. ASN1_STRING* str;
  38921. const ASN1_OBJECT* pa_oid = NULL;
  38922. X509_PUBKEY* pubKey = NULL;
  38923. EVP_PKEY* evpKey = NULL;
  38924. const unsigned char *pk = NULL;
  38925. int ppklen, pptype;
  38926. X509_ALGOR *pa = NULL;
  38927. const void *pval;
  38928. #ifdef USE_CERT_BUFFERS_1024
  38929. XMEMSET(tmp, 0, sizeof(tmp));
  38930. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  38931. bytes = sizeof_dsa_key_der_1024;
  38932. #elif defined(USE_CERT_BUFFERS_2048)
  38933. XMEMSET(tmp, 0, sizeof(tmp));
  38934. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  38935. bytes = sizeof_dsa_key_der_2048;
  38936. #else
  38937. {
  38938. XFILE fp = XBADFILE;
  38939. XMEMSET(tmp, 0, sizeof(tmp));
  38940. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  38941. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  38942. if (fp != XBADFILE)
  38943. XFCLOSE(fp);
  38944. }
  38945. #endif
  38946. /* Initialize pkey with der format dsa key */
  38947. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  38948. ExpectNotNull(pubKey = X509_PUBKEY_new());
  38949. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  38950. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  38951. ExpectNotNull(pk);
  38952. ExpectNotNull(pa);
  38953. ExpectIntGT(ppklen, 0);
  38954. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  38955. ExpectNotNull(pa_oid);
  38956. ExpectNotNull(pval);
  38957. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  38958. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  38959. str = (ASN1_STRING *)pval;
  38960. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  38961. #ifdef USE_CERT_BUFFERS_1024
  38962. ExpectIntEQ(ASN1_STRING_length(str), 291);
  38963. #else
  38964. ExpectIntEQ(ASN1_STRING_length(str), 549);
  38965. #endif /* END USE_CERT_BUFFERS_1024 */
  38966. X509_PUBKEY_free(pubKey);
  38967. EVP_PKEY_free(evpKey);
  38968. #endif
  38969. return EXPECT_RESULT();
  38970. }
  38971. static int test_wolfSSL_BUF(void)
  38972. {
  38973. EXPECT_DECLS;
  38974. #if defined(OPENSSL_EXTRA)
  38975. BUF_MEM* buf = NULL;
  38976. ExpectNotNull(buf = BUF_MEM_new());
  38977. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  38978. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  38979. BUF_MEM_free(buf);
  38980. #endif
  38981. return EXPECT_RESULT();
  38982. }
  38983. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  38984. static int stub_rand_seed(const void *buf, int num)
  38985. {
  38986. (void)buf;
  38987. (void)num;
  38988. return 123;
  38989. }
  38990. static int stub_rand_bytes(unsigned char *buf, int num)
  38991. {
  38992. (void)buf;
  38993. (void)num;
  38994. return 456;
  38995. }
  38996. static byte* was_stub_rand_cleanup_called(void)
  38997. {
  38998. static byte was_called = 0;
  38999. return &was_called;
  39000. }
  39001. static void stub_rand_cleanup(void)
  39002. {
  39003. byte* was_called = was_stub_rand_cleanup_called();
  39004. *was_called = 1;
  39005. return;
  39006. }
  39007. static byte* was_stub_rand_add_called(void)
  39008. {
  39009. static byte was_called = 0;
  39010. return &was_called;
  39011. }
  39012. static int stub_rand_add(const void *buf, int num, double entropy)
  39013. {
  39014. byte* was_called = was_stub_rand_add_called();
  39015. (void)buf;
  39016. (void)num;
  39017. (void)entropy;
  39018. *was_called = 1;
  39019. return 0;
  39020. }
  39021. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  39022. {
  39023. (void)buf;
  39024. (void)num;
  39025. return 9876;
  39026. }
  39027. static int stub_rand_status(void)
  39028. {
  39029. return 5432;
  39030. }
  39031. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  39032. static int test_wolfSSL_RAND_set_rand_method(void)
  39033. {
  39034. EXPECT_DECLS;
  39035. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  39036. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  39037. unsigned char* buf = NULL;
  39038. int num = 0;
  39039. double entropy = 0;
  39040. int ret;
  39041. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  39042. byte* was_add_called = was_stub_rand_add_called();
  39043. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  39044. DYNAMIC_TYPE_TMP_BUFFER));
  39045. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  39046. ExpectIntEQ(*was_cleanup_called, 0);
  39047. RAND_cleanup();
  39048. ExpectIntEQ(*was_cleanup_called, 0);
  39049. rand_methods.seed = &stub_rand_seed;
  39050. rand_methods.bytes = &stub_rand_bytes;
  39051. rand_methods.cleanup = &stub_rand_cleanup;
  39052. rand_methods.add = &stub_rand_add;
  39053. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  39054. rand_methods.status = &stub_rand_status;
  39055. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  39056. ExpectIntEQ(RAND_seed(buf, num), 123);
  39057. ExpectIntEQ(RAND_bytes(buf, num), 456);
  39058. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  39059. ExpectIntEQ(RAND_status(), 5432);
  39060. ExpectIntEQ(*was_add_called, 0);
  39061. /* The function pointer for RAND_add returns int, but RAND_add itself
  39062. * returns void. */
  39063. RAND_add(buf, num, entropy);
  39064. ExpectIntEQ(*was_add_called, 1);
  39065. was_add_called = 0;
  39066. ExpectIntEQ(*was_cleanup_called, 0);
  39067. RAND_cleanup();
  39068. ExpectIntEQ(*was_cleanup_called, 1);
  39069. *was_cleanup_called = 0;
  39070. ret = RAND_set_rand_method(NULL);
  39071. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  39072. ExpectIntNE(RAND_status(), 5432);
  39073. ExpectIntEQ(*was_cleanup_called, 0);
  39074. RAND_cleanup();
  39075. ExpectIntEQ(*was_cleanup_called, 0);
  39076. RAND_set_rand_method(NULL);
  39077. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39078. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  39079. return EXPECT_RESULT();
  39080. }
  39081. static int test_wolfSSL_RAND_bytes(void)
  39082. {
  39083. EXPECT_DECLS;
  39084. #if defined(OPENSSL_EXTRA)
  39085. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  39086. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  39087. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  39088. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  39089. int max_bufsize;
  39090. byte *my_buf = NULL;
  39091. /* sanity check */
  39092. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  39093. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  39094. max_bufsize = size4;
  39095. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  39096. DYNAMIC_TYPE_TMP_BUFFER));
  39097. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  39098. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  39099. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  39100. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  39101. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  39102. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  39103. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  39104. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39105. #endif
  39106. return EXPECT_RESULT();
  39107. }
  39108. static int test_wolfSSL_RAND(void)
  39109. {
  39110. EXPECT_DECLS;
  39111. #if defined(OPENSSL_EXTRA)
  39112. byte seed[16];
  39113. XMEMSET(seed, 0, sizeof(seed));
  39114. /* No global methods set. */
  39115. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  39116. ExpectIntEQ(RAND_poll(), 1);
  39117. RAND_cleanup();
  39118. ExpectIntEQ(RAND_egd(NULL), -1);
  39119. #ifndef NO_FILESYSTEM
  39120. {
  39121. char fname[100];
  39122. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  39123. ExpectIntEQ(RAND_write_file(NULL), 0);
  39124. }
  39125. #endif
  39126. #endif
  39127. return EXPECT_RESULT();
  39128. }
  39129. static int test_wolfSSL_PKCS8_Compat(void)
  39130. {
  39131. EXPECT_DECLS;
  39132. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  39133. !defined(NO_BIO)
  39134. PKCS8_PRIV_KEY_INFO* pt = NULL;
  39135. BIO* bio = NULL;
  39136. XFILE f = XBADFILE;
  39137. int bytes;
  39138. char pkcs8_buffer[512];
  39139. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  39140. EVP_PKEY *pkey = NULL;
  39141. #endif
  39142. /* file from wolfssl/certs/ directory */
  39143. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  39144. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  39145. 0);
  39146. if (f != XBADFILE)
  39147. XFCLOSE(f);
  39148. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  39149. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  39150. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  39151. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  39152. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  39153. /* gets PKCS8 pointer to pkey */
  39154. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  39155. EVP_PKEY_free(pkey);
  39156. #endif
  39157. BIO_free(bio);
  39158. PKCS8_PRIV_KEY_INFO_free(pt);
  39159. #endif
  39160. return EXPECT_RESULT();
  39161. }
  39162. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  39163. static int NoPasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  39164. {
  39165. (void)passwd;
  39166. (void)sz;
  39167. (void)rw;
  39168. (void)userdata;
  39169. return -1;
  39170. }
  39171. #endif
  39172. static int test_wolfSSL_PKCS8_d2i(void)
  39173. {
  39174. EXPECT_DECLS;
  39175. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  39176. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  39177. * requires a 12 byte password in FIPS mode. This test ends up
  39178. * trying to use an 8 byte password. */
  39179. #ifndef NO_FILESYSTEM
  39180. unsigned char pkcs8_buffer[2048];
  39181. const unsigned char* p = NULL;
  39182. int bytes = 0;
  39183. XFILE file = XBADFILE;
  39184. WOLFSSL_EVP_PKEY* pkey = NULL;
  39185. #ifndef NO_BIO
  39186. BIO* bio = NULL;
  39187. #if defined(OPENSSL_ALL) && \
  39188. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  39189. defined(HAVE_ECC)) && \
  39190. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  39191. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  39192. #endif
  39193. #endif
  39194. #ifndef NO_RSA
  39195. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  39196. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  39197. #ifndef NO_DES3
  39198. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  39199. #endif
  39200. #endif /* NO_RSA */
  39201. #ifdef HAVE_ECC
  39202. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  39203. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  39204. #ifndef NO_DES3
  39205. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  39206. #endif
  39207. #endif /* HAVE_ECC */
  39208. #endif /* !NO_FILESYSTEM */
  39209. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  39210. #ifndef NO_RSA
  39211. #ifdef USE_CERT_BUFFERS_1024
  39212. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  39213. int rsaSz = sizeof_server_key_der_1024;
  39214. #else
  39215. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  39216. int rsaSz = sizeof_server_key_der_2048;
  39217. #endif
  39218. #endif
  39219. #ifdef HAVE_ECC
  39220. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  39221. int ecSz = sizeof_ecc_key_der_256;
  39222. #endif
  39223. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  39224. #ifndef NO_FILESYSTEM
  39225. (void)pkcs8_buffer;
  39226. (void)p;
  39227. (void)bytes;
  39228. (void)file;
  39229. #ifndef NO_BIO
  39230. (void)bio;
  39231. #endif
  39232. #endif
  39233. #ifdef OPENSSL_ALL
  39234. #ifndef NO_RSA
  39235. /* Try to auto-detect normal RSA private key */
  39236. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  39237. EVP_PKEY_free(pkey);
  39238. pkey = NULL;
  39239. #endif
  39240. #ifdef HAVE_ECC
  39241. /* Try to auto-detect normal EC private key */
  39242. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  39243. EVP_PKEY_free(pkey);
  39244. pkey = NULL;
  39245. #endif
  39246. #endif /* OPENSSL_ALL */
  39247. #ifndef NO_FILESYSTEM
  39248. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  39249. ExpectIntEQ(PEM_write_PKCS8PrivateKey(XBADFILE, pkey, NULL, NULL, 0, NULL,
  39250. NULL), 0);
  39251. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  39252. NULL), 0);
  39253. #endif
  39254. #ifndef NO_RSA
  39255. /* Get DER encoded RSA PKCS#8 data. */
  39256. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  39257. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  39258. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39259. file)), 0);
  39260. if (file != XBADFILE) {
  39261. XFCLOSE(file);
  39262. file = XBADFILE;
  39263. }
  39264. p = pkcs8_buffer;
  39265. #ifdef OPENSSL_ALL
  39266. /* Try to decode - auto-detect key type. */
  39267. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  39268. #else
  39269. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  39270. #endif
  39271. /* Get PEM encoded RSA PKCS#8 data. */
  39272. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  39273. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39274. file)), 0);
  39275. if (file != XBADFILE) {
  39276. XFCLOSE(file);
  39277. file = XBADFILE;
  39278. }
  39279. #if defined(OPENSSL_ALL) && \
  39280. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  39281. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39282. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  39283. NULL), 0);
  39284. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  39285. NULL), 0);
  39286. /* Write PKCS#8 PEM to BIO. */
  39287. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  39288. NULL), bytes);
  39289. /* Write PKCS#8 PEM to stderr. */
  39290. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  39291. NULL), bytes);
  39292. /* Compare file and written data */
  39293. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  39294. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  39295. BIO_free(bio);
  39296. bio = NULL;
  39297. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  39298. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_gcm(),
  39299. NULL, 0, PasswordCallBack, (void*)"yassl123"), 0);
  39300. #endif
  39301. #if !defined(NO_DES3) && !defined(NO_SHA)
  39302. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39303. /* Write Encrypted PKCS#8 PEM to BIO. */
  39304. bytes = 1834;
  39305. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  39306. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  39307. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
  39308. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  39309. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  39310. (void*)"yassl123"));
  39311. EVP_PKEY_free(evpPkey);
  39312. evpPkey = NULL;
  39313. BIO_free(bio);
  39314. bio = NULL;
  39315. #endif /* !NO_DES3 && !NO_SHA */
  39316. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  39317. EVP_PKEY_free(pkey);
  39318. pkey = NULL;
  39319. /* PKCS#8 encrypted RSA key */
  39320. #ifndef NO_DES3
  39321. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  39322. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  39323. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39324. file)), 0);
  39325. if (file != XBADFILE) {
  39326. XFCLOSE(file);
  39327. file = XBADFILE;
  39328. }
  39329. #if defined(OPENSSL_ALL) && \
  39330. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  39331. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  39332. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  39333. (void*)"yassl123"));
  39334. EVP_PKEY_free(pkey);
  39335. pkey = NULL;
  39336. BIO_free(bio);
  39337. bio = NULL;
  39338. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  39339. #endif /* !NO_DES3 */
  39340. #endif /* NO_RSA */
  39341. #ifdef HAVE_ECC
  39342. /* PKCS#8 encode EC key */
  39343. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  39344. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  39345. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39346. file)), 0);
  39347. if (file != XBADFILE) {
  39348. XFCLOSE(file);
  39349. file = XBADFILE;
  39350. }
  39351. p = pkcs8_buffer;
  39352. #ifdef OPENSSL_ALL
  39353. /* Try to decode - auto-detect key type. */
  39354. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  39355. #else
  39356. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  39357. #endif
  39358. /* Get PEM encoded RSA PKCS#8 data. */
  39359. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  39360. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  39361. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39362. file)), 0);
  39363. if (file != XBADFILE) {
  39364. XFCLOSE(file);
  39365. file = XBADFILE;
  39366. }
  39367. #if defined(OPENSSL_ALL) && \
  39368. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  39369. defined(HAVE_AES_CBC)
  39370. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39371. /* Write PKCS#8 PEM to BIO. */
  39372. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  39373. NULL), bytes);
  39374. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  39375. NULL), bytes);
  39376. /* Compare file and written data */
  39377. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  39378. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  39379. BIO_free(bio);
  39380. bio = NULL;
  39381. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39382. /* Write Encrypted PKCS#8 PEM to BIO. */
  39383. bytes = 379;
  39384. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  39385. NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
  39386. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  39387. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  39388. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  39389. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  39390. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  39391. (char*)"yassl123", 8, PasswordCallBack, NULL), bytes);
  39392. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_256_cbc(),
  39393. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  39394. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  39395. (void*)"yassl123"));
  39396. EVP_PKEY_free(evpPkey);
  39397. evpPkey = NULL;
  39398. BIO_free(bio);
  39399. bio = NULL;
  39400. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  39401. EVP_PKEY_free(pkey);
  39402. pkey = NULL;
  39403. /* PKCS#8 encrypted EC key */
  39404. #ifndef NO_DES3
  39405. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  39406. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  39407. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39408. file)), 0);
  39409. if (file != XBADFILE) {
  39410. XFCLOSE(file);
  39411. file = XBADFILE;
  39412. }
  39413. #if defined(OPENSSL_ALL) && \
  39414. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  39415. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  39416. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  39417. (void*)"yassl123"));
  39418. EVP_PKEY_free(pkey);
  39419. pkey = NULL;
  39420. BIO_free(bio);
  39421. bio = NULL;
  39422. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  39423. #endif /* !NO_DES3 */
  39424. #endif /* HAVE_ECC */
  39425. #endif /* !NO_FILESYSTEM */
  39426. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  39427. return EXPECT_RESULT();
  39428. }
  39429. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  39430. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  39431. #define LOGGING_THREADS 5
  39432. #define ERROR_COUNT 10
  39433. /* copied from logging.c since this is not exposed otherwise */
  39434. #ifndef ERROR_QUEUE_MAX
  39435. #ifdef ERROR_QUEUE_PER_THREAD
  39436. #define ERROR_QUEUE_MAX 16
  39437. #else
  39438. /* this breaks from compat of unlimited error queue size */
  39439. #define ERROR_QUEUE_MAX 100
  39440. #endif
  39441. #endif
  39442. static volatile int loggingThreadsReady;
  39443. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  39444. {
  39445. const char* file;
  39446. int line;
  39447. unsigned long err;
  39448. int errorCount = 0;
  39449. int i;
  39450. (void)args;
  39451. while (!loggingThreadsReady);
  39452. for (i = 0; i < ERROR_COUNT; i++)
  39453. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  39454. while ((err = ERR_get_error_line(&file, &line))) {
  39455. AssertIntEQ(err, 990 + errorCount);
  39456. errorCount++;
  39457. }
  39458. AssertIntEQ(errorCount, ERROR_COUNT);
  39459. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  39460. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  39461. errorCount = 0;
  39462. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  39463. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  39464. while ((err = ERR_get_error_line(&file, &line))) {
  39465. AssertIntEQ(err, 990 + errorCount);
  39466. errorCount++;
  39467. }
  39468. /* test that the 3 errors over the max were dropped */
  39469. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  39470. WOLFSSL_RETURN_FROM_THREAD(0);
  39471. }
  39472. #endif
  39473. static int test_error_queue_per_thread(void)
  39474. {
  39475. int res = TEST_SKIPPED;
  39476. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  39477. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  39478. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  39479. int i;
  39480. ERR_clear_error(); /* clear out any error nodes */
  39481. loggingThreadsReady = 0;
  39482. for (i = 0; i < LOGGING_THREADS; i++)
  39483. start_thread(test_logging, NULL, &loggingThreads[i]);
  39484. loggingThreadsReady = 1;
  39485. for (i = 0; i < LOGGING_THREADS; i++)
  39486. join_thread(loggingThreads[i]);
  39487. res = TEST_SUCCESS;
  39488. #endif
  39489. return res;
  39490. }
  39491. static int test_wolfSSL_ERR_put_error(void)
  39492. {
  39493. EXPECT_DECLS;
  39494. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  39495. defined(DEBUG_WOLFSSL)
  39496. const char* file;
  39497. int line;
  39498. ERR_clear_error(); /* clear out any error nodes */
  39499. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  39500. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  39501. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  39502. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  39503. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  39504. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  39505. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  39506. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  39507. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  39508. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  39509. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  39510. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  39511. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  39512. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  39513. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  39514. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  39515. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  39516. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  39517. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  39518. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  39519. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  39520. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  39521. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  39522. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  39523. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  39524. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  39525. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  39526. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  39527. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  39528. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  39529. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  39530. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  39531. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  39532. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  39533. "this file", 100);
  39534. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  39535. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  39536. ExpectIntEQ(line, 100);
  39537. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  39538. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  39539. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  39540. #endif
  39541. /* try reading past end of error queue */
  39542. file = NULL;
  39543. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  39544. ExpectNull(file);
  39545. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  39546. PEMerr(4,4);
  39547. ExpectIntEQ(ERR_get_error(), 4);
  39548. /* Empty and free up all error nodes */
  39549. ERR_clear_error();
  39550. /* Verify all nodes are cleared */
  39551. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  39552. ERR_clear_error();
  39553. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  39554. #endif
  39555. return EXPECT_RESULT();
  39556. }
  39557. /*
  39558. * This is a regression test for a bug where the peek/get error functions were
  39559. * drawing from the end of the queue rather than the front.
  39560. */
  39561. static int test_wolfSSL_ERR_get_error_order(void)
  39562. {
  39563. EXPECT_DECLS;
  39564. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  39565. /* Empty the queue. */
  39566. wolfSSL_ERR_clear_error();
  39567. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  39568. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  39569. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  39570. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  39571. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  39572. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  39573. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  39574. return EXPECT_RESULT();
  39575. }
  39576. #ifndef NO_BIO
  39577. static int test_wolfSSL_ERR_print_errors(void)
  39578. {
  39579. EXPECT_DECLS;
  39580. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  39581. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  39582. BIO* bio = NULL;
  39583. char buf[1024];
  39584. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39585. ERR_clear_error(); /* clear out any error nodes */
  39586. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  39587. /* Choosing -600 as an unused errno. */
  39588. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  39589. ERR_print_errors(bio);
  39590. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  39591. ExpectIntEQ(XSTRNCMP(
  39592. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  39593. buf, 55), 0);
  39594. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  39595. ExpectIntEQ(XSTRNCMP(
  39596. "error:600:wolfSSL library:unknown error number:asn.c:100",
  39597. buf, 56), 0);
  39598. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  39599. ExpectIntEQ(buf[0], '\0');
  39600. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  39601. BIO_free(bio);
  39602. #endif
  39603. return EXPECT_RESULT();
  39604. }
  39605. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  39606. defined(DEBUG_WOLFSSL)
  39607. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  39608. {
  39609. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  39610. return 0;
  39611. }
  39612. #endif
  39613. static int test_wolfSSL_ERR_print_errors_cb(void)
  39614. {
  39615. EXPECT_DECLS;
  39616. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  39617. defined(DEBUG_WOLFSSL)
  39618. BIO* bio = NULL;
  39619. char buf[1024];
  39620. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39621. ERR_clear_error(); /* clear out any error nodes */
  39622. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  39623. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  39624. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  39625. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  39626. ExpectIntEQ(XSTRNCMP(
  39627. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  39628. buf, 53), 0);
  39629. ExpectIntEQ(XSTRNCMP(
  39630. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  39631. buf + 53, 55), 0);
  39632. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  39633. BIO_free(bio);
  39634. #endif
  39635. return EXPECT_RESULT();
  39636. }
  39637. /*
  39638. * Testing WOLFSSL_ERROR_MSG
  39639. */
  39640. static int test_WOLFSSL_ERROR_MSG(void)
  39641. {
  39642. int res = TEST_SKIPPED;
  39643. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  39644. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  39645. const char* msg = TEST_STRING;
  39646. WOLFSSL_ERROR_MSG(msg);
  39647. res = TEST_SUCCESS;
  39648. #endif
  39649. return res;
  39650. } /* End test_WOLFSSL_ERROR_MSG */
  39651. /*
  39652. * Testing wc_ERR_remove_state
  39653. */
  39654. static int test_wc_ERR_remove_state(void)
  39655. {
  39656. int res = TEST_SKIPPED;
  39657. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  39658. wc_ERR_remove_state();
  39659. res = TEST_SUCCESS;
  39660. #endif
  39661. return res;
  39662. } /* End test_wc_ERR_remove_state */
  39663. /*
  39664. * Testing wc_ERR_print_errors_fp
  39665. */
  39666. static int test_wc_ERR_print_errors_fp(void)
  39667. {
  39668. EXPECT_DECLS;
  39669. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  39670. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  39671. long sz;
  39672. XFILE fp = XBADFILE;
  39673. WOLFSSL_ERROR(BAD_FUNC_ARG);
  39674. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  39675. XBADFILE);
  39676. wc_ERR_print_errors_fp(fp);
  39677. #if defined(DEBUG_WOLFSSL)
  39678. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  39679. #ifdef NO_ERROR_QUEUE
  39680. ExpectIntEQ(sz = XFTELL(fp), 0);
  39681. #else
  39682. ExpectIntNE(sz = XFTELL(fp), 0);
  39683. #endif
  39684. #endif
  39685. if (fp != XBADFILE)
  39686. XFCLOSE(fp);
  39687. (void)sz;
  39688. #endif
  39689. return EXPECT_RESULT();
  39690. } /* End test_wc_ERR_print_errors_fp */
  39691. #ifdef DEBUG_WOLFSSL
  39692. static void Logging_cb(const int logLevel, const char *const logMessage)
  39693. {
  39694. (void)logLevel;
  39695. (void)logMessage;
  39696. }
  39697. #endif
  39698. /*
  39699. * Testing wolfSSL_GetLoggingCb
  39700. */
  39701. static int test_wolfSSL_GetLoggingCb(void)
  39702. {
  39703. EXPECT_DECLS;
  39704. #ifdef DEBUG_WOLFSSL
  39705. /* Testing without wolfSSL_SetLoggingCb() */
  39706. ExpectNull(wolfSSL_GetLoggingCb());
  39707. /* Testing with wolfSSL_SetLoggingCb() */
  39708. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  39709. ExpectNotNull(wolfSSL_GetLoggingCb());
  39710. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  39711. #endif
  39712. ExpectNull(wolfSSL_GetLoggingCb());
  39713. return EXPECT_RESULT();
  39714. } /* End test_wolfSSL_GetLoggingCb */
  39715. #endif /* !NO_BIO */
  39716. static int test_wolfSSL_MD4(void)
  39717. {
  39718. EXPECT_DECLS;
  39719. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  39720. MD4_CTX md4;
  39721. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  39722. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  39723. "789012345678901234567890";
  39724. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  39725. "\xcc\x05\x36";
  39726. int msgSz = (int)XSTRLEN(msg);
  39727. XMEMSET(out, 0, sizeof(out));
  39728. MD4_Init(&md4);
  39729. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  39730. MD4_Final(out, &md4);
  39731. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  39732. #endif
  39733. return EXPECT_RESULT();
  39734. }
  39735. static int test_wolfSSL_MD5(void)
  39736. {
  39737. EXPECT_DECLS;
  39738. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  39739. byte input1[] = "";
  39740. byte input2[] = "message digest";
  39741. byte hash[WC_MD5_DIGEST_SIZE];
  39742. unsigned char output1[] =
  39743. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  39744. unsigned char output2[] =
  39745. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  39746. WOLFSSL_MD5_CTX md5;
  39747. XMEMSET(&md5, 0, sizeof(md5));
  39748. /* Test cases for illegal parameters */
  39749. ExpectIntEQ(MD5_Init(NULL), 0);
  39750. ExpectIntEQ(MD5_Init(&md5), 1);
  39751. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  39752. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  39753. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  39754. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  39755. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  39756. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  39757. /* Init MD5 CTX */
  39758. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  39759. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  39760. 1);
  39761. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  39762. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  39763. /* Init MD5 CTX */
  39764. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  39765. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  39766. (int)XSTRLEN((const char*)input2)), 1);
  39767. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  39768. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  39769. #if !defined(NO_OLD_NAMES) && \
  39770. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  39771. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  39772. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  39773. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  39774. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  39775. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  39776. &hash);
  39777. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  39778. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  39779. &hash);
  39780. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  39781. {
  39782. byte data[] = "Data to be hashed.";
  39783. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  39784. ExpectNotNull(MD5(data, sizeof(data), NULL));
  39785. ExpectNotNull(MD5(data, sizeof(data), hash));
  39786. ExpectNotNull(MD5(NULL, 0, hash));
  39787. ExpectNull(MD5(NULL, sizeof(data), hash));
  39788. }
  39789. #endif
  39790. #endif
  39791. return EXPECT_RESULT();
  39792. }
  39793. static int test_wolfSSL_MD5_Transform(void)
  39794. {
  39795. EXPECT_DECLS;
  39796. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  39797. byte input1[] = "";
  39798. byte input2[] = "abc";
  39799. byte local[WC_MD5_BLOCK_SIZE];
  39800. word32 sLen = 0;
  39801. #ifdef BIG_ENDIAN_ORDER
  39802. unsigned char output1[] =
  39803. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  39804. unsigned char output2[] =
  39805. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  39806. #else
  39807. unsigned char output1[] =
  39808. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  39809. unsigned char output2[] =
  39810. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  39811. #endif
  39812. union {
  39813. wc_Md5 native;
  39814. MD5_CTX compat;
  39815. } md5;
  39816. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  39817. XMEMSET(&local, 0, sizeof(local));
  39818. /* sanity check */
  39819. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  39820. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  39821. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  39822. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  39823. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  39824. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  39825. /* Init MD5 CTX */
  39826. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  39827. /* Do Transform*/
  39828. sLen = (word32)XSTRLEN((char*)input1);
  39829. XMEMCPY(local, input1, sLen);
  39830. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  39831. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  39832. /* Init MD5 CTX */
  39833. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  39834. sLen = (word32)XSTRLEN((char*)input2);
  39835. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  39836. XMEMCPY(local, input2, sLen);
  39837. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  39838. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  39839. #endif
  39840. return EXPECT_RESULT();
  39841. }
  39842. static int test_wolfSSL_SHA(void)
  39843. {
  39844. EXPECT_DECLS;
  39845. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  39846. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  39847. (!defined(HAVE_FIPS) || \
  39848. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  39849. {
  39850. const unsigned char in[] = "abc";
  39851. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  39852. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  39853. unsigned char out[WC_SHA_DIGEST_SIZE];
  39854. unsigned char* p = NULL;
  39855. WOLFSSL_SHA_CTX sha;
  39856. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  39857. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  39858. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  39859. /* SHA interface test */
  39860. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  39861. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  39862. ExpectNotNull(SHA(in, 0, out));
  39863. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  39864. ExpectNotNull(SHA(NULL, 0, out));
  39865. ExpectNotNull(SHA(NULL, 0, NULL));
  39866. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  39867. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  39868. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  39869. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  39870. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  39871. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  39872. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  39873. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  39874. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  39875. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  39876. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  39877. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  39878. }
  39879. #endif
  39880. #if !defined(NO_SHA256)
  39881. {
  39882. const unsigned char in[] = "abc";
  39883. unsigned char expected[] =
  39884. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  39885. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  39886. "\x15\xAD";
  39887. unsigned char out[WC_SHA256_DIGEST_SIZE];
  39888. unsigned char* p = NULL;
  39889. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  39890. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39891. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  39892. #else
  39893. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  39894. #endif
  39895. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  39896. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39897. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  39898. #else
  39899. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  39900. #endif
  39901. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  39902. }
  39903. #endif
  39904. #if defined(WOLFSSL_SHA384)
  39905. {
  39906. const unsigned char in[] = "abc";
  39907. unsigned char expected[] =
  39908. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  39909. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  39910. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  39911. "\xc8\x25\xa7";
  39912. unsigned char out[WC_SHA384_DIGEST_SIZE];
  39913. unsigned char* p = NULL;
  39914. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  39915. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39916. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  39917. #else
  39918. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  39919. #endif
  39920. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  39921. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39922. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  39923. #else
  39924. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  39925. #endif
  39926. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  39927. }
  39928. #endif
  39929. #if defined(WOLFSSL_SHA512)
  39930. {
  39931. const unsigned char in[] = "abc";
  39932. unsigned char expected[] =
  39933. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  39934. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  39935. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  39936. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  39937. "\xa5\x4c\xa4\x9f";
  39938. unsigned char out[WC_SHA512_DIGEST_SIZE];
  39939. unsigned char* p = NULL;
  39940. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  39941. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39942. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  39943. #else
  39944. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  39945. #endif
  39946. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  39947. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39948. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  39949. #else
  39950. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  39951. #endif
  39952. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  39953. }
  39954. #endif
  39955. #endif
  39956. return EXPECT_RESULT();
  39957. }
  39958. static int test_wolfSSL_SHA_Transform(void)
  39959. {
  39960. EXPECT_DECLS;
  39961. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  39962. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  39963. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  39964. byte input1[] = "";
  39965. byte input2[] = "abc";
  39966. byte local[WC_SHA_BLOCK_SIZE];
  39967. word32 sLen = 0;
  39968. #ifdef BIG_ENDIAN_ORDER
  39969. unsigned char output1[] =
  39970. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  39971. "\x09\xf7\x3a\x93";
  39972. unsigned char output2[] =
  39973. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  39974. "\x7c\x6e\x08\xb8";
  39975. #else
  39976. unsigned char output1[] =
  39977. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  39978. "\x93\x3a\xf7\x09";
  39979. unsigned char output2[] =
  39980. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  39981. "\xb8\x08\x6e\x7c";
  39982. #endif
  39983. union {
  39984. wc_Sha native;
  39985. SHA_CTX compat;
  39986. } sha;
  39987. union {
  39988. wc_Sha native;
  39989. SHA_CTX compat;
  39990. } sha1;
  39991. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  39992. XMEMSET(&local, 0, sizeof(local));
  39993. /* sanity check */
  39994. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  39995. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  39996. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  39997. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  39998. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  39999. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  40000. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  40001. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40002. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  40003. /* Init SHA CTX */
  40004. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  40005. /* Do Transform*/
  40006. sLen = (word32)XSTRLEN((char*)input1);
  40007. XMEMCPY(local, input1, sLen);
  40008. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  40009. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  40010. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  40011. /* Init SHA CTX */
  40012. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  40013. sLen = (word32)XSTRLEN((char*)input2);
  40014. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  40015. XMEMCPY(local, input2, sLen);
  40016. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  40017. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  40018. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  40019. /* SHA1 */
  40020. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  40021. /* Init SHA CTX */
  40022. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  40023. /* Do Transform*/
  40024. sLen = (word32)XSTRLEN((char*)input1);
  40025. XMEMCPY(local, input1, sLen);
  40026. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  40027. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  40028. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  40029. /* Init SHA CTX */
  40030. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  40031. sLen = (word32)XSTRLEN((char*)input2);
  40032. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  40033. XMEMCPY(local, input2, sLen);
  40034. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  40035. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  40036. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  40037. #endif
  40038. #endif
  40039. return EXPECT_RESULT();
  40040. }
  40041. static int test_wolfSSL_SHA224(void)
  40042. {
  40043. EXPECT_DECLS;
  40044. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  40045. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40046. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  40047. unsigned char input[] =
  40048. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  40049. unsigned char output[] =
  40050. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  40051. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  40052. size_t inLen;
  40053. byte hash[WC_SHA224_DIGEST_SIZE];
  40054. unsigned char* p;
  40055. inLen = XSTRLEN((char*)input);
  40056. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  40057. ExpectNull(SHA224(NULL, inLen, hash));
  40058. ExpectNotNull(SHA224(input, 0, hash));
  40059. ExpectNotNull(SHA224(input, inLen, NULL));
  40060. ExpectNotNull(SHA224(NULL, 0, hash));
  40061. ExpectNotNull(SHA224(NULL, 0, NULL));
  40062. ExpectNotNull(SHA224(input, inLen, hash));
  40063. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  40064. ExpectNotNull(p = SHA224(input, inLen, NULL));
  40065. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  40066. #endif
  40067. return EXPECT_RESULT();
  40068. }
  40069. static int test_wolfSSL_SHA256(void)
  40070. {
  40071. EXPECT_DECLS;
  40072. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  40073. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  40074. unsigned char input[] =
  40075. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  40076. unsigned char output[] =
  40077. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  40078. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  40079. "\x06\xC1";
  40080. size_t inLen;
  40081. byte hash[WC_SHA256_DIGEST_SIZE];
  40082. inLen = XSTRLEN((char*)input);
  40083. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  40084. ExpectNotNull(SHA256(input, inLen, hash));
  40085. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  40086. #endif
  40087. return EXPECT_RESULT();
  40088. }
  40089. static int test_wolfSSL_SHA256_Transform(void)
  40090. {
  40091. EXPECT_DECLS;
  40092. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  40093. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40094. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  40095. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  40096. !defined(WOLFSSL_KCAPI_HASH)
  40097. byte input1[] = "";
  40098. byte input2[] = "abc";
  40099. byte local[WC_SHA256_BLOCK_SIZE];
  40100. word32 sLen = 0;
  40101. #ifdef BIG_ENDIAN_ORDER
  40102. unsigned char output1[] =
  40103. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  40104. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  40105. unsigned char output2[] =
  40106. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  40107. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  40108. #else
  40109. unsigned char output1[] =
  40110. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  40111. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  40112. unsigned char output2[] =
  40113. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  40114. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  40115. #endif
  40116. union {
  40117. wc_Sha256 native;
  40118. SHA256_CTX compat;
  40119. } sha256;
  40120. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  40121. XMEMSET(&local, 0, sizeof(local));
  40122. /* sanity check */
  40123. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  40124. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  40125. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  40126. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  40127. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40128. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  40129. /* Init SHA256 CTX */
  40130. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  40131. /* Do Transform*/
  40132. sLen = (word32)XSTRLEN((char*)input1);
  40133. XMEMCPY(local, input1, sLen);
  40134. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  40135. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  40136. 0);
  40137. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  40138. /* Init SHA256 CTX */
  40139. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  40140. sLen = (word32)XSTRLEN((char*)input2);
  40141. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  40142. XMEMCPY(local, input2, sLen);
  40143. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  40144. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  40145. 0);
  40146. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  40147. #endif
  40148. #endif
  40149. return EXPECT_RESULT();
  40150. }
  40151. static int test_wolfSSL_SHA512_Transform(void)
  40152. {
  40153. EXPECT_DECLS;
  40154. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  40155. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40156. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  40157. !defined(WOLFSSL_KCAPI_HASH)
  40158. byte input1[] = "";
  40159. byte input2[] = "abc";
  40160. byte local[WC_SHA512_BLOCK_SIZE];
  40161. word32 sLen = 0;
  40162. #ifdef BIG_ENDIAN_ORDER
  40163. unsigned char output1[] =
  40164. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  40165. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  40166. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  40167. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  40168. unsigned char output2[] =
  40169. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  40170. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  40171. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  40172. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  40173. #else
  40174. unsigned char output1[] =
  40175. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  40176. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  40177. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  40178. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  40179. unsigned char output2[] =
  40180. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  40181. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  40182. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  40183. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  40184. #endif
  40185. union {
  40186. wc_Sha512 native;
  40187. SHA512_CTX compat;
  40188. } sha512;
  40189. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  40190. XMEMSET(&local, 0, sizeof(local));
  40191. /* sanity check */
  40192. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  40193. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  40194. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  40195. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  40196. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40197. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  40198. /* Init SHA512 CTX */
  40199. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  40200. /* Do Transform*/
  40201. sLen = (word32)XSTRLEN((char*)input1);
  40202. XMEMCPY(local, input1, sLen);
  40203. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  40204. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  40205. WC_SHA512_DIGEST_SIZE), 0);
  40206. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  40207. /* Init SHA512 CTX */
  40208. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  40209. sLen = (word32)XSTRLEN((char*)input2);
  40210. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  40211. XMEMCPY(local, input2, sLen);
  40212. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  40213. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  40214. WC_SHA512_DIGEST_SIZE), 0);
  40215. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  40216. (void)input1;
  40217. #endif
  40218. #endif
  40219. return EXPECT_RESULT();
  40220. }
  40221. static int test_wolfSSL_SHA512_224_Transform(void)
  40222. {
  40223. EXPECT_DECLS;
  40224. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  40225. !defined(WOLFSSL_NOSHA512_224)
  40226. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40227. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  40228. !defined(WOLFSSL_KCAPI_HASH)
  40229. byte input1[] = "";
  40230. byte input2[] = "abc";
  40231. byte local[WC_SHA512_BLOCK_SIZE];
  40232. word32 sLen = 0;
  40233. unsigned char output1[] =
  40234. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  40235. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  40236. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  40237. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  40238. unsigned char output2[] =
  40239. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  40240. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  40241. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  40242. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  40243. union {
  40244. wc_Sha512 native;
  40245. SHA512_CTX compat;
  40246. } sha512;
  40247. #ifdef BIG_ENDIAN_ORDER
  40248. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  40249. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  40250. #endif
  40251. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  40252. XMEMSET(&local, 0, sizeof(local));
  40253. /* sanity check */
  40254. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  40255. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  40256. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  40257. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  40258. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  40259. BAD_FUNC_ARG);
  40260. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  40261. /* Init SHA512 CTX */
  40262. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  40263. /* Do Transform*/
  40264. sLen = (word32)XSTRLEN((char*)input1);
  40265. XMEMCPY(local, input1, sLen);
  40266. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  40267. 1);
  40268. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  40269. WC_SHA512_DIGEST_SIZE), 0);
  40270. /* frees resources */
  40271. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  40272. /* Init SHA512 CTX */
  40273. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  40274. sLen = (word32)XSTRLEN((char*)input2);
  40275. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  40276. XMEMCPY(local, input2, sLen);
  40277. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  40278. 1);
  40279. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  40280. WC_SHA512_DIGEST_SIZE), 0);
  40281. /* frees resources */
  40282. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  40283. #endif
  40284. #endif
  40285. return EXPECT_RESULT();
  40286. }
  40287. static int test_wolfSSL_SHA512_256_Transform(void)
  40288. {
  40289. EXPECT_DECLS;
  40290. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  40291. !defined(WOLFSSL_NOSHA512_256)
  40292. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40293. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  40294. !defined(WOLFSSL_KCAPI_HASH)
  40295. byte input1[] = "";
  40296. byte input2[] = "abc";
  40297. byte local[WC_SHA512_BLOCK_SIZE];
  40298. word32 sLen = 0;
  40299. unsigned char output1[] =
  40300. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  40301. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  40302. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  40303. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  40304. unsigned char output2[] =
  40305. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  40306. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  40307. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  40308. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  40309. union {
  40310. wc_Sha512 native;
  40311. SHA512_CTX compat;
  40312. } sha512;
  40313. #ifdef BIG_ENDIAN_ORDER
  40314. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  40315. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  40316. #endif
  40317. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  40318. XMEMSET(&local, 0, sizeof(local));
  40319. /* sanity check */
  40320. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  40321. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  40322. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  40323. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  40324. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  40325. BAD_FUNC_ARG);
  40326. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  40327. /* Init SHA512 CTX */
  40328. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  40329. /* Do Transform*/
  40330. sLen = (word32)XSTRLEN((char*)input1);
  40331. XMEMCPY(local, input1, sLen);
  40332. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  40333. 1);
  40334. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  40335. WC_SHA512_DIGEST_SIZE), 0);
  40336. /* frees resources */
  40337. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  40338. /* Init SHA512 CTX */
  40339. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  40340. sLen = (word32)XSTRLEN((char*)input2);
  40341. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  40342. XMEMCPY(local, input2, sLen);
  40343. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  40344. 1);
  40345. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  40346. WC_SHA512_DIGEST_SIZE), 0);
  40347. /* frees resources */
  40348. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  40349. #endif
  40350. #endif
  40351. return EXPECT_RESULT();
  40352. }
  40353. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  40354. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  40355. * buffer of 64 bytes.
  40356. *
  40357. * returns the size of the digest buffer on success and a negative value on
  40358. * failure.
  40359. */
  40360. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  40361. int* sz)
  40362. {
  40363. EXPECT_DECLS;
  40364. HMAC_CTX ctx1;
  40365. HMAC_CTX ctx2;
  40366. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  40367. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  40368. unsigned char long_key[] =
  40369. "0123456789012345678901234567890123456789"
  40370. "0123456789012345678901234567890123456789"
  40371. "0123456789012345678901234567890123456789"
  40372. "0123456789012345678901234567890123456789";
  40373. unsigned char msg[] = "message to hash";
  40374. unsigned int digestSz = 64;
  40375. int keySz = sizeof(key);
  40376. int long_keySz = sizeof(long_key);
  40377. int msgSz = sizeof(msg);
  40378. unsigned char digest2[64];
  40379. unsigned int digestSz2 = 64;
  40380. HMAC_CTX_init(&ctx1);
  40381. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40382. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40383. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40384. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40385. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40386. HMAC_CTX_cleanup(&ctx1);
  40387. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40388. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  40389. HMAC_CTX_cleanup(&ctx2);
  40390. ExpectIntEQ(digestSz, digestSz2);
  40391. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40392. /* test HMAC_Init with NULL key */
  40393. /* init after copy */
  40394. HMAC_CTX_init(&ctx1);
  40395. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40396. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40397. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40398. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40399. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40400. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40401. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40402. HMAC_CTX_cleanup(&ctx1);
  40403. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  40404. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40405. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40406. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40407. HMAC_CTX_cleanup(&ctx2);
  40408. ExpectIntEQ(digestSz, digestSz2);
  40409. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40410. /* long key */
  40411. HMAC_CTX_init(&ctx1);
  40412. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  40413. SSL_SUCCESS);
  40414. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40415. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40416. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40417. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40418. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40419. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40420. HMAC_CTX_cleanup(&ctx1);
  40421. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  40422. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40423. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40424. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40425. HMAC_CTX_cleanup(&ctx2);
  40426. ExpectIntEQ(digestSz, digestSz2);
  40427. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40428. /* init before copy */
  40429. HMAC_CTX_init(&ctx1);
  40430. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40431. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40432. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40433. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40434. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40435. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40436. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40437. HMAC_CTX_cleanup(&ctx1);
  40438. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40439. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40440. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40441. HMAC_CTX_cleanup(&ctx2);
  40442. ExpectIntEQ(digestSz, digestSz2);
  40443. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40444. *sz = (int)digestSz;
  40445. return EXPECT_RESULT();
  40446. }
  40447. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  40448. static int test_wolfSSL_HMAC_CTX(void)
  40449. {
  40450. EXPECT_DECLS;
  40451. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  40452. unsigned char digest[64];
  40453. int digestSz;
  40454. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  40455. WOLFSSL_HMAC_CTX ctx1;
  40456. WOLFSSL_HMAC_CTX ctx2;
  40457. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  40458. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  40459. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  40460. wolfSSL_HMAC_CTX_free(NULL);
  40461. wolfSSL_HMAC_CTX_free(hmac_ctx);
  40462. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  40463. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  40464. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  40465. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  40466. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  40467. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  40468. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  40469. ((! defined(HAVE_FIPS_VERSION)) || \
  40470. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  40471. /* Copy object that hasn't had a digest set - MD5. */
  40472. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  40473. #else
  40474. /* Copy object that hasn't had a digest set. */
  40475. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  40476. #endif
  40477. HMAC_CTX_cleanup(NULL);
  40478. HMAC_CTX_cleanup(&ctx2);
  40479. ExpectNull(HMAC_CTX_get_md(NULL));
  40480. #ifndef NO_SHA
  40481. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  40482. TEST_SUCCESS);
  40483. ExpectIntEQ(digestSz, 20);
  40484. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  40485. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  40486. #endif /* !NO_SHA */
  40487. #ifdef WOLFSSL_SHA224
  40488. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  40489. TEST_SUCCESS);
  40490. ExpectIntEQ(digestSz, 28);
  40491. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  40492. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  40493. "\x02\x0E", digest, digestSz), 0);
  40494. #endif /* WOLFSSL_SHA224 */
  40495. #ifndef NO_SHA256
  40496. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  40497. TEST_SUCCESS);
  40498. ExpectIntEQ(digestSz, 32);
  40499. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  40500. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  40501. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  40502. #endif /* !NO_SHA256 */
  40503. #ifdef WOLFSSL_SHA384
  40504. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  40505. TEST_SUCCESS);
  40506. ExpectIntEQ(digestSz, 48);
  40507. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  40508. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  40509. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  40510. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  40511. digest, digestSz), 0);
  40512. #endif /* WOLFSSL_SHA384 */
  40513. #ifdef WOLFSSL_SHA512
  40514. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  40515. TEST_SUCCESS);
  40516. ExpectIntEQ(digestSz, 64);
  40517. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  40518. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  40519. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  40520. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  40521. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  40522. digest, digestSz), 0);
  40523. #endif /* WOLFSSL_SHA512 */
  40524. #ifdef WOLFSSL_SHA3
  40525. #ifndef WOLFSSL_NOSHA3_224
  40526. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  40527. TEST_SUCCESS);
  40528. ExpectIntEQ(digestSz, 28);
  40529. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  40530. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  40531. "\x29\x0f", digest, digestSz), 0);
  40532. #endif
  40533. #ifndef WOLFSSL_NOSHA3_256
  40534. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  40535. TEST_SUCCESS);
  40536. ExpectIntEQ(digestSz, 32);
  40537. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  40538. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  40539. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  40540. #endif
  40541. #ifndef WOLFSSL_NOSHA3_384
  40542. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  40543. TEST_SUCCESS);
  40544. ExpectIntEQ(digestSz, 48);
  40545. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  40546. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  40547. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  40548. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  40549. digest, digestSz), 0);
  40550. #endif
  40551. #ifndef WOLFSSL_NOSHA3_512
  40552. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  40553. TEST_SUCCESS);
  40554. ExpectIntEQ(digestSz, 64);
  40555. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  40556. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  40557. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  40558. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  40559. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  40560. digest, digestSz), 0);
  40561. #endif
  40562. #endif
  40563. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  40564. HAVE_FIPS_VERSION <= 2)
  40565. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  40566. TEST_SUCCESS);
  40567. ExpectIntEQ(digestSz, 16);
  40568. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  40569. "\xE4\x98\xDD", digest, digestSz), 0);
  40570. #endif /* !NO_MD5 */
  40571. #endif
  40572. return EXPECT_RESULT();
  40573. }
  40574. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  40575. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  40576. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  40577. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  40578. {
  40579. EXPECT_DECLS;
  40580. static const unsigned char key[] = "simple test key";
  40581. HMAC_CTX* hmac = NULL;
  40582. ENGINE* e = NULL;
  40583. unsigned char hash[WC_MAX_DIGEST_SIZE];
  40584. unsigned int len;
  40585. ExpectNotNull(hmac = HMAC_CTX_new());
  40586. HMAC_CTX_init(hmac);
  40587. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  40588. ((! defined(HAVE_FIPS_VERSION)) || \
  40589. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  40590. /* Get size on object that hasn't had a digest set - MD5. */
  40591. ExpectIntEQ(HMAC_size(hmac), 16);
  40592. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  40593. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  40594. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  40595. #else
  40596. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  40597. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  40598. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  40599. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  40600. #endif
  40601. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  40602. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  40603. /* re-using test key as data to hash */
  40604. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  40605. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  40606. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  40607. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  40608. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  40609. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  40610. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  40611. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  40612. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  40613. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  40614. ExpectIntEQ(len, md_len);
  40615. ExpectIntEQ(HMAC_size(NULL), 0);
  40616. ExpectIntEQ(HMAC_size(hmac), md_len);
  40617. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  40618. HMAC_cleanup(NULL);
  40619. HMAC_cleanup(hmac);
  40620. HMAC_CTX_free(hmac);
  40621. len = 0;
  40622. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  40623. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  40624. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  40625. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  40626. ExpectIntEQ(len, md_len);
  40627. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  40628. /* With data. */
  40629. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  40630. &len));
  40631. /* With NULL data. */
  40632. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  40633. &len));
  40634. /* With zero length data. */
  40635. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  40636. return EXPECT_RESULT();
  40637. }
  40638. #endif
  40639. static int test_wolfSSL_HMAC(void)
  40640. {
  40641. EXPECT_DECLS;
  40642. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  40643. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  40644. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  40645. #ifndef NO_SHA256
  40646. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  40647. TEST_SUCCESS);
  40648. #endif
  40649. #ifdef WOLFSSL_SHA224
  40650. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  40651. TEST_SUCCESS);
  40652. #endif
  40653. #ifdef WOLFSSL_SHA384
  40654. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  40655. TEST_SUCCESS);
  40656. #endif
  40657. #ifdef WOLFSSL_SHA512
  40658. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  40659. TEST_SUCCESS);
  40660. #endif
  40661. #ifdef WOLFSSL_SHA3
  40662. #ifndef WOLFSSL_NOSHA3_224
  40663. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  40664. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  40665. #endif
  40666. #ifndef WOLFSSL_NOSHA3_256
  40667. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  40668. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  40669. #endif
  40670. #ifndef WOLFSSL_NOSHA3_384
  40671. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  40672. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  40673. #endif
  40674. #ifndef WOLFSSL_NOSHA3_512
  40675. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  40676. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  40677. #endif
  40678. #endif
  40679. #ifndef NO_SHA
  40680. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  40681. TEST_SUCCESS);
  40682. #endif
  40683. #endif
  40684. return EXPECT_RESULT();
  40685. }
  40686. static int test_wolfSSL_CMAC(void)
  40687. {
  40688. EXPECT_DECLS;
  40689. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  40690. defined(WOLFSSL_AES_DIRECT)
  40691. int i;
  40692. byte key[AES_256_KEY_SIZE];
  40693. CMAC_CTX* cmacCtx = NULL;
  40694. byte out[AES_BLOCK_SIZE];
  40695. size_t outLen = AES_BLOCK_SIZE;
  40696. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  40697. key[i] = i;
  40698. }
  40699. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40700. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  40701. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  40702. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  40703. NULL), 1);
  40704. /* re-using test key as data to hash */
  40705. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  40706. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  40707. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  40708. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  40709. /* No Update works. */
  40710. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  40711. NULL), 1);
  40712. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  40713. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  40714. NULL), 1);
  40715. /* Test parameters with CMAC_Update. */
  40716. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  40717. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  40718. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  40719. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  40720. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  40721. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  40722. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  40723. /* Test parameters with CMAC_Final. */
  40724. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  40725. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  40726. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  40727. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  40728. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  40729. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  40730. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  40731. CMAC_CTX_free(cmacCtx);
  40732. /* Test parameters with CMAC Init. */
  40733. cmacCtx = NULL;
  40734. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40735. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  40736. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  40737. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  40738. NULL), 0);
  40739. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  40740. NULL), 0);
  40741. /* give a key too small for the cipher, verify we get failure */
  40742. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  40743. NULL), 0);
  40744. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  40745. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  40746. /* Only AES-CBC supported. */
  40747. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  40748. NULL), 0);
  40749. #endif
  40750. CMAC_CTX_free(cmacCtx);
  40751. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  40752. cmacCtx = NULL;
  40753. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40754. /* No Init. */
  40755. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  40756. CMAC_CTX_free(cmacCtx);
  40757. /* Test AES-256-CBC */
  40758. cmacCtx = NULL;
  40759. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40760. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  40761. NULL), 1);
  40762. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  40763. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  40764. CMAC_CTX_free(cmacCtx);
  40765. /* Test AES-192-CBC */
  40766. cmacCtx = NULL;
  40767. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40768. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  40769. NULL), 1);
  40770. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  40771. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  40772. CMAC_CTX_free(cmacCtx);
  40773. cmacCtx = NULL;
  40774. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40775. CMAC_CTX_free(cmacCtx);
  40776. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  40777. return EXPECT_RESULT();
  40778. }
  40779. static int test_wolfSSL_DES(void)
  40780. {
  40781. EXPECT_DECLS;
  40782. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  40783. const_DES_cblock myDes;
  40784. DES_cblock iv;
  40785. DES_key_schedule key;
  40786. word32 i = 0;
  40787. DES_LONG dl = 0;
  40788. unsigned char msg[] = "hello wolfssl";
  40789. unsigned char weakKey[][8] = {
  40790. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  40791. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  40792. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  40793. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  40794. };
  40795. unsigned char semiWeakKey[][8] = {
  40796. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  40797. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  40798. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  40799. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  40800. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  40801. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  40802. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  40803. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  40804. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  40805. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  40806. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  40807. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  40808. };
  40809. DES_check_key(1);
  40810. DES_set_key(&myDes, &key);
  40811. /* check, check of odd parity */
  40812. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  40813. myDes[0] = 6; /* set even parity */
  40814. XMEMSET(key, 5, sizeof(DES_key_schedule));
  40815. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  40816. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  40817. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  40818. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  40819. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  40820. /* set odd parity for success case */
  40821. DES_set_odd_parity(&myDes);
  40822. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  40823. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  40824. myDes[3]);
  40825. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  40826. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  40827. ExpectIntEQ(key[i], myDes[i]);
  40828. }
  40829. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  40830. /* check weak key */
  40831. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  40832. XMEMSET(key, 5, sizeof(DES_key_schedule));
  40833. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  40834. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  40835. DES_set_key_unchecked(NULL, NULL);
  40836. DES_set_key_unchecked(&myDes, NULL);
  40837. DES_set_key_unchecked(NULL, &key);
  40838. /* compare arrays, should be the same */
  40839. /* now do unchecked copy of a weak key over */
  40840. DES_set_key_unchecked(&myDes, &key);
  40841. /* compare arrays, should be the same */
  40842. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  40843. ExpectIntEQ(key[i], myDes[i]);
  40844. }
  40845. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  40846. myDes[7] = 2;
  40847. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  40848. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  40849. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  40850. /* Test all weak keys. */
  40851. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  40852. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  40853. }
  40854. /* Test all semi-weak keys. */
  40855. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  40856. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  40857. }
  40858. /* check DES_key_sched API */
  40859. XMEMSET(key, 1, sizeof(DES_key_schedule));
  40860. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  40861. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  40862. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  40863. /* compare arrays, should be the same */
  40864. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  40865. ExpectIntEQ(key[i], myDes[i]);
  40866. }
  40867. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  40868. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  40869. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  40870. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  40871. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  40872. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  40873. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  40874. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  40875. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  40876. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  40877. * DES_cbc_encrypt on the input */
  40878. XMEMSET(iv, 0, sizeof(DES_cblock));
  40879. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  40880. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  40881. ExpectIntEQ(dl, 480052723);
  40882. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  40883. return EXPECT_RESULT();
  40884. }
  40885. static int test_wolfSSL_DES_ncbc(void)
  40886. {
  40887. EXPECT_DECLS;
  40888. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  40889. const_DES_cblock myDes;
  40890. DES_cblock iv = {1};
  40891. DES_key_schedule key = {0};
  40892. unsigned char msg[] = "hello wolfssl";
  40893. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  40894. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  40895. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  40896. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  40897. /* partial block test */
  40898. DES_set_key(&key, &myDes);
  40899. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  40900. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  40901. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  40902. DES_set_key(&key, &myDes);
  40903. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  40904. *((byte*)&iv) = 1;
  40905. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  40906. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  40907. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  40908. /* full block test */
  40909. DES_set_key(&key, &myDes);
  40910. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  40911. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  40912. *((byte*)&iv) = 1;
  40913. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  40914. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  40915. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  40916. DES_set_key(&key, &myDes);
  40917. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  40918. *((byte*)&iv) = 1;
  40919. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  40920. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  40921. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  40922. #endif
  40923. return EXPECT_RESULT();
  40924. }
  40925. static int test_wolfSSL_DES_ecb_encrypt(void)
  40926. {
  40927. EXPECT_DECLS;
  40928. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  40929. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  40930. WOLFSSL_DES_key_schedule key;
  40931. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  40932. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  40933. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  40934. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  40935. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  40936. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  40937. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  40938. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  40939. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  40940. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  40941. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  40942. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  40943. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  40944. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  40945. /* Encrypt messages */
  40946. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  40947. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  40948. {
  40949. /* Decrypt messages */
  40950. int ret1 = 0;
  40951. int ret2 = 0;
  40952. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  40953. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  40954. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  40955. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  40956. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  40957. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  40958. }
  40959. #endif
  40960. return EXPECT_RESULT();
  40961. }
  40962. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  40963. {
  40964. EXPECT_DECLS;
  40965. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  40966. unsigned char input1[8], input2[8];
  40967. unsigned char output1[8], output2[8];
  40968. unsigned char back1[8], back2[8];
  40969. WOLFSSL_DES_cblock iv1, iv2;
  40970. WOLFSSL_DES_key_schedule key1, key2, key3;
  40971. int i;
  40972. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  40973. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  40974. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  40975. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  40976. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  40977. XMEMSET(output1, 0, sizeof(output1));
  40978. XMEMSET(output2, 0, sizeof(output2));
  40979. XMEMSET(back1, 0, sizeof(back1));
  40980. XMEMSET(back2, 0, sizeof(back2));
  40981. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  40982. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  40983. /* Encrypt messages */
  40984. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  40985. DES_ENCRYPT);
  40986. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  40987. DES_ENCRYPT);
  40988. {
  40989. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  40990. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  40991. /* Decrypt messages */
  40992. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  40993. &iv1, DES_DECRYPT);
  40994. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  40995. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  40996. &iv2, DES_DECRYPT);
  40997. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  40998. }
  40999. for (i = 0; i < 8; i++) {
  41000. XMEMSET(output1, 0, sizeof(output1));
  41001. XMEMSET(output2, 0, sizeof(output2));
  41002. XMEMSET(back1, 0, sizeof(back1));
  41003. XMEMSET(back2, 0, sizeof(back2));
  41004. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  41005. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  41006. /* Encrypt partial messages */
  41007. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  41008. &iv1, DES_ENCRYPT);
  41009. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  41010. &iv2, DES_ENCRYPT);
  41011. {
  41012. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  41013. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  41014. /* Decrypt messages */
  41015. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  41016. &key3, &iv1, DES_DECRYPT);
  41017. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  41018. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  41019. &key3, &iv2, DES_DECRYPT);
  41020. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  41021. }
  41022. }
  41023. #endif
  41024. return EXPECT_RESULT();
  41025. }
  41026. static int test_wolfSSL_AES_encrypt(void)
  41027. {
  41028. EXPECT_DECLS;
  41029. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  41030. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  41031. AES_KEY enc;
  41032. AES_KEY dec;
  41033. const byte msg[] = {
  41034. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  41035. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  41036. };
  41037. const byte exp[] = {
  41038. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  41039. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  41040. };
  41041. const byte key[] = {
  41042. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  41043. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  41044. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  41045. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  41046. };
  41047. byte eout[sizeof(msg)];
  41048. byte dout[sizeof(msg)];
  41049. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  41050. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  41051. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  41052. wolfSSL_AES_encrypt(msg, NULL, NULL);
  41053. wolfSSL_AES_encrypt(NULL, eout, NULL);
  41054. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  41055. wolfSSL_AES_encrypt(msg, eout, NULL);
  41056. wolfSSL_AES_encrypt(msg, NULL, &enc);
  41057. wolfSSL_AES_encrypt(NULL, eout, &enc);
  41058. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  41059. wolfSSL_AES_decrypt(eout, NULL, NULL);
  41060. wolfSSL_AES_decrypt(NULL, dout, NULL);
  41061. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  41062. wolfSSL_AES_decrypt(eout, dout, NULL);
  41063. wolfSSL_AES_decrypt(eout, NULL, &dec);
  41064. wolfSSL_AES_decrypt(NULL, dout, &dec);
  41065. wolfSSL_AES_encrypt(msg, eout, &enc);
  41066. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  41067. wolfSSL_AES_decrypt(eout, dout, &dec);
  41068. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  41069. #endif
  41070. return EXPECT_RESULT();
  41071. }
  41072. static int test_wolfSSL_AES_ecb_encrypt(void)
  41073. {
  41074. EXPECT_DECLS;
  41075. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  41076. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  41077. AES_KEY aes;
  41078. const byte msg[] =
  41079. {
  41080. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41081. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  41082. };
  41083. const byte verify[] =
  41084. {
  41085. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  41086. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  41087. };
  41088. const byte key[] =
  41089. {
  41090. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  41091. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  41092. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  41093. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  41094. };
  41095. byte out[AES_BLOCK_SIZE];
  41096. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  41097. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41098. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  41099. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  41100. #ifdef HAVE_AES_DECRYPT
  41101. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  41102. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41103. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  41104. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  41105. #endif
  41106. /* test bad arguments */
  41107. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  41108. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  41109. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  41110. #endif
  41111. return EXPECT_RESULT();
  41112. }
  41113. static int test_wolfSSL_AES_cbc_encrypt(void)
  41114. {
  41115. EXPECT_DECLS;
  41116. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  41117. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  41118. AES_KEY aes;
  41119. AES_KEY* aesN = NULL;
  41120. size_t len = 0;
  41121. size_t lenB = 0;
  41122. int keySz0 = 0;
  41123. int keySzN = -1;
  41124. byte out[AES_BLOCK_SIZE] = {0};
  41125. byte* outN = NULL;
  41126. /* Test vectors retrieved from:
  41127. * <begin URL>
  41128. * https://csrc.nist.gov/
  41129. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  41130. * documents/aes/KAT_AES.zip
  41131. * </end URL>
  41132. */
  41133. const byte* pt128N = NULL;
  41134. byte* key128N = NULL;
  41135. byte* iv128N = NULL;
  41136. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  41137. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41138. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  41139. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  41140. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  41141. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41142. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  41143. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  41144. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  41145. len = sizeof(pt128);
  41146. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  41147. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  41148. ExpectIntNE(XMEMCMP(b, g, h), i)
  41149. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  41150. /* Stressing wolfSSL_AES_cbc_encrypt() */
  41151. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  41152. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  41153. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  41154. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41155. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  41156. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41157. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  41158. /* Stressing wolfSSL_AES_set_encrypt_key */
  41159. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  41160. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  41161. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  41162. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  41163. /* Stressing wolfSSL_AES_set_decrypt_key */
  41164. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  41165. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  41166. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  41167. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  41168. #ifdef WOLFSSL_AES_128
  41169. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  41170. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41171. RESET_IV(iv128tmp, iv128);
  41172. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  41173. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  41174. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41175. wc_AesFree((Aes*)&aes);
  41176. #ifdef HAVE_AES_DECRYPT
  41177. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  41178. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41179. RESET_IV(iv128tmp, iv128);
  41180. len = sizeof(ct128);
  41181. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  41182. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  41183. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  41184. wc_AesFree((Aes*)&aes);
  41185. #endif
  41186. #endif /* WOLFSSL_AES_128 */
  41187. #ifdef WOLFSSL_AES_192
  41188. {
  41189. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  41190. * Appendix F.2.3 */
  41191. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  41192. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41193. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  41194. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  41195. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  41196. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  41197. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  41198. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  41199. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  41200. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  41201. len = sizeof(pt192);
  41202. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  41203. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41204. RESET_IV(iv192tmp, iv192);
  41205. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  41206. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  41207. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  41208. wc_AesFree((Aes*)&aes);
  41209. #ifdef HAVE_AES_DECRYPT
  41210. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  41211. len = sizeof(ct192);
  41212. RESET_IV(iv192tmp, iv192);
  41213. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41214. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  41215. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  41216. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  41217. wc_AesFree((Aes*)&aes);
  41218. #endif
  41219. }
  41220. #endif /* WOLFSSL_AES_192 */
  41221. #ifdef WOLFSSL_AES_256
  41222. {
  41223. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  41224. * Appendix F.2.5 */
  41225. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  41226. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41227. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  41228. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  41229. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  41230. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  41231. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  41232. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  41233. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  41234. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  41235. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  41236. len = sizeof(pt256);
  41237. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  41238. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41239. RESET_IV(iv256tmp, iv256);
  41240. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41241. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  41242. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  41243. wc_AesFree((Aes*)&aes);
  41244. #ifdef HAVE_AES_DECRYPT
  41245. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  41246. len = sizeof(ct256);
  41247. RESET_IV(iv256tmp, iv256);
  41248. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41249. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41250. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  41251. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  41252. wc_AesFree((Aes*)&aes);
  41253. #endif
  41254. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  41255. !defined(HAVE_SELFTEST)
  41256. {
  41257. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  41258. byte wrapPlain[sizeof(key256)] = { 0 };
  41259. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  41260. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  41261. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41262. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  41263. 15), WOLFSSL_FAILURE);
  41264. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  41265. sizeof(key256)), sizeof(wrapCipher));
  41266. wc_AesFree((Aes*)&aes);
  41267. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  41268. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41269. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  41270. 23), WOLFSSL_FAILURE);
  41271. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  41272. sizeof(wrapCipher)), sizeof(wrapPlain));
  41273. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  41274. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  41275. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  41276. wc_AesFree((Aes*)&aes);
  41277. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  41278. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41279. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  41280. sizeof(key256)), sizeof(wrapCipher));
  41281. wc_AesFree((Aes*)&aes);
  41282. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  41283. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41284. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  41285. sizeof(wrapCipher)), sizeof(wrapPlain));
  41286. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  41287. wc_AesFree((Aes*)&aes);
  41288. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  41289. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  41290. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  41291. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  41292. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  41293. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  41294. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  41295. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  41296. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  41297. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  41298. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  41299. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  41300. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  41301. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  41302. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  41303. 0);
  41304. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  41305. 0);
  41306. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  41307. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  41308. }
  41309. #endif /* HAVE_AES_KEYWRAP */
  41310. }
  41311. #endif /* WOLFSSL_AES_256 */
  41312. #endif
  41313. return EXPECT_RESULT();
  41314. }
  41315. static int test_wolfSSL_AES_cfb128_encrypt(void)
  41316. {
  41317. EXPECT_DECLS;
  41318. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  41319. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  41320. AES_KEY aesEnc;
  41321. AES_KEY aesDec;
  41322. const byte msg[] = {
  41323. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  41324. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  41325. };
  41326. const byte exp[] = {
  41327. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  41328. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  41329. };
  41330. const byte key[] = {
  41331. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  41332. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  41333. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  41334. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  41335. };
  41336. const byte ivData[] = {
  41337. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  41338. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  41339. };
  41340. byte out[AES_BLOCK_SIZE];
  41341. byte iv[AES_BLOCK_SIZE];
  41342. word32 i;
  41343. int num;
  41344. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  41345. XMEMCPY(iv, ivData, sizeof(iv));
  41346. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41347. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  41348. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  41349. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41350. #ifdef HAVE_AES_DECRYPT
  41351. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  41352. XMEMCPY(iv, ivData, sizeof(iv));
  41353. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41354. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  41355. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  41356. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41357. #endif
  41358. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  41359. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  41360. XMEMCPY(iv, ivData, sizeof(iv));
  41361. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41362. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  41363. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  41364. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  41365. if (i == 0) {
  41366. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41367. }
  41368. else {
  41369. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41370. }
  41371. #ifdef HAVE_AES_DECRYPT
  41372. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  41373. XMEMCPY(iv, ivData, sizeof(iv));
  41374. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41375. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  41376. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  41377. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  41378. if (i == 0) {
  41379. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41380. }
  41381. else {
  41382. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41383. }
  41384. #endif
  41385. }
  41386. if (EXPECT_SUCCESS()) {
  41387. /* test bad arguments */
  41388. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  41389. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  41390. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  41391. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  41392. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  41393. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  41394. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  41395. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  41396. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  41397. }
  41398. #endif
  41399. return EXPECT_RESULT();
  41400. }
  41401. static int test_wolfSSL_CRYPTO_cts128(void)
  41402. {
  41403. EXPECT_DECLS;
  41404. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  41405. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  41406. byte tmp[64]; /* Largest vector size */
  41407. /* Test vectors taken form RFC3962 Appendix B */
  41408. const testVector vects[] = {
  41409. {
  41410. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41411. "\x20",
  41412. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  41413. "\x97",
  41414. 17, 17
  41415. },
  41416. {
  41417. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41418. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  41419. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  41420. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  41421. 31, 31
  41422. },
  41423. {
  41424. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41425. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  41426. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  41427. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  41428. 32, 32
  41429. },
  41430. {
  41431. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41432. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41433. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  41434. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41435. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  41436. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  41437. 47, 47
  41438. },
  41439. {
  41440. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41441. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41442. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  41443. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41444. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  41445. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  41446. 48, 48
  41447. },
  41448. {
  41449. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41450. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41451. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  41452. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  41453. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41454. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  41455. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  41456. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  41457. 64, 64
  41458. }
  41459. };
  41460. byte keyBytes[AES_128_KEY_SIZE] = {
  41461. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  41462. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  41463. };
  41464. size_t i;
  41465. AES_KEY encKey;
  41466. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  41467. XMEMSET(tmp, 0, sizeof(tmp));
  41468. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  41469. AES_KEY decKey;
  41470. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  41471. &encKey), 0);
  41472. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  41473. &decKey), 0);
  41474. XMEMSET(iv, 0, sizeof(iv));
  41475. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  41476. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  41477. vects[i].outLen);
  41478. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  41479. XMEMSET(iv, 0, sizeof(iv));
  41480. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  41481. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  41482. vects[i].inLen);
  41483. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  41484. }
  41485. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  41486. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  41487. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  41488. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  41489. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  41490. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  41491. (cbc128_f)AES_cbc_encrypt), 0);
  41492. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  41493. (cbc128_f)AES_cbc_encrypt), 0);
  41494. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  41495. (cbc128_f)AES_cbc_encrypt), 0);
  41496. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  41497. (cbc128_f)AES_cbc_encrypt), 0);
  41498. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  41499. (cbc128_f)AES_cbc_encrypt), 0);
  41500. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  41501. /* Length too small. */
  41502. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  41503. (cbc128_f)AES_cbc_encrypt), 0);
  41504. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  41505. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  41506. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  41507. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  41508. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  41509. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  41510. (cbc128_f)AES_cbc_encrypt), 0);
  41511. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  41512. (cbc128_f)AES_cbc_encrypt), 0);
  41513. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  41514. (cbc128_f)AES_cbc_encrypt), 0);
  41515. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  41516. (cbc128_f)AES_cbc_encrypt), 0);
  41517. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  41518. (cbc128_f)AES_cbc_encrypt), 0);
  41519. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  41520. /* Length too small. */
  41521. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  41522. (cbc128_f)AES_cbc_encrypt), 0);
  41523. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  41524. return EXPECT_RESULT();
  41525. }
  41526. static int test_wolfSSL_RC4(void)
  41527. {
  41528. EXPECT_DECLS;
  41529. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  41530. WOLFSSL_RC4_KEY rc4Key;
  41531. unsigned char key[] = {
  41532. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  41533. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  41534. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  41535. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  41536. };
  41537. unsigned char data[] = {
  41538. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  41539. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  41540. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  41541. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  41542. };
  41543. unsigned char enc[sizeof(data)];
  41544. unsigned char dec[sizeof(data)];
  41545. word32 i;
  41546. word32 j;
  41547. wolfSSL_RC4_set_key(NULL, -1, NULL);
  41548. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  41549. wolfSSL_RC4_set_key(NULL, 0, NULL);
  41550. wolfSSL_RC4_set_key(NULL, -1, key);
  41551. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  41552. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  41553. wolfSSL_RC4_set_key(NULL, 0, key);
  41554. wolfSSL_RC4(NULL, 0, NULL, NULL);
  41555. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  41556. wolfSSL_RC4(NULL, 0, data, NULL);
  41557. wolfSSL_RC4(NULL, 0, NULL, enc);
  41558. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  41559. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  41560. wolfSSL_RC4(NULL, 0, data, enc);
  41561. ExpectIntEQ(1, 1);
  41562. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  41563. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  41564. XMEMSET(enc, 0, sizeof(enc));
  41565. XMEMSET(dec, 0, sizeof(dec));
  41566. /* Encrypt */
  41567. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  41568. wolfSSL_RC4(&rc4Key, j, data, enc);
  41569. /* Decrypt */
  41570. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  41571. wolfSSL_RC4(&rc4Key, j, enc, dec);
  41572. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  41573. }
  41574. }
  41575. #endif
  41576. return EXPECT_RESULT();
  41577. }
  41578. static int test_wolfSSL_OBJ(void)
  41579. {
  41580. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  41581. * mode
  41582. */
  41583. EXPECT_DECLS;
  41584. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  41585. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  41586. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  41587. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  41588. ASN1_OBJECT *obj = NULL;
  41589. ASN1_OBJECT *obj2 = NULL;
  41590. char buf[50];
  41591. XFILE fp = XBADFILE;
  41592. X509 *x509 = NULL;
  41593. X509_NAME *x509Name = NULL;
  41594. X509_NAME_ENTRY *x509NameEntry = NULL;
  41595. ASN1_OBJECT *asn1Name = NULL;
  41596. int numNames = 0;
  41597. BIO *bio = NULL;
  41598. int nid;
  41599. int i, j;
  41600. const char *f[] = {
  41601. #ifndef NO_RSA
  41602. "./certs/ca-cert.der",
  41603. #endif
  41604. #ifdef HAVE_ECC
  41605. "./certs/ca-ecc-cert.der",
  41606. "./certs/ca-ecc384-cert.der",
  41607. #endif
  41608. NULL};
  41609. ASN1_OBJECT *field_name_obj = NULL;
  41610. int lastpos = -1;
  41611. int tmp = -1;
  41612. ASN1_STRING *asn1 = NULL;
  41613. unsigned char *buf_dyn = NULL;
  41614. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  41615. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  41616. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  41617. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  41618. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  41619. ASN1_OBJECT_free(obj);
  41620. obj = NULL;
  41621. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  41622. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  41623. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  41624. #ifdef WOLFSSL_CERT_EXT
  41625. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  41626. #endif
  41627. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  41628. ExpectNotNull(obj2 = OBJ_dup(obj));
  41629. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  41630. ASN1_OBJECT_free(obj);
  41631. obj = NULL;
  41632. ASN1_OBJECT_free(obj2);
  41633. obj2 = NULL;
  41634. for (i = 0; f[i] != NULL; i++)
  41635. {
  41636. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  41637. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  41638. if (fp != XBADFILE) {
  41639. XFCLOSE(fp);
  41640. fp = XBADFILE;
  41641. }
  41642. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  41643. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  41644. /* Get the Common Name by using OBJ_txt2obj */
  41645. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  41646. do
  41647. {
  41648. lastpos = tmp;
  41649. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  41650. } while (tmp > -1);
  41651. ExpectIntNE(lastpos, -1);
  41652. ASN1_OBJECT_free(field_name_obj);
  41653. field_name_obj = NULL;
  41654. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  41655. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  41656. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  41657. /*
  41658. * All Common Names should be www.wolfssl.com
  41659. * This makes testing easier as we can test for the expected value.
  41660. */
  41661. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  41662. OPENSSL_free(buf_dyn);
  41663. buf_dyn = NULL;
  41664. bio = BIO_new(BIO_s_mem());
  41665. ExpectTrue(bio != NULL);
  41666. for (j = 0; j < numNames; j++)
  41667. {
  41668. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  41669. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  41670. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  41671. }
  41672. BIO_free(bio);
  41673. bio = NULL;
  41674. X509_free(x509);
  41675. x509 = NULL;
  41676. }
  41677. #ifdef HAVE_PKCS12
  41678. {
  41679. PKCS12 *p12 = NULL;
  41680. int boolRet;
  41681. EVP_PKEY *pkey = NULL;
  41682. const char *p12_f[] = {
  41683. #if !defined(NO_DES3) && !defined(NO_RSA)
  41684. "./certs/test-servercert.p12",
  41685. #endif
  41686. NULL};
  41687. for (i = 0; p12_f[i] != NULL; i++)
  41688. {
  41689. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  41690. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  41691. if (fp != XBADFILE) {
  41692. XFCLOSE(fp);
  41693. fp = XBADFILE;
  41694. }
  41695. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  41696. &pkey, &x509, NULL)) > 0);
  41697. wc_PKCS12_free(p12);
  41698. p12 = NULL;
  41699. EVP_PKEY_free(pkey);
  41700. x509Name = X509_get_issuer_name(x509);
  41701. ExpectNotNull(x509Name);
  41702. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  41703. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  41704. for (j = 0; j < numNames; j++)
  41705. {
  41706. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  41707. ExpectNotNull(asn1Name =
  41708. X509_NAME_ENTRY_get_object(x509NameEntry));
  41709. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  41710. }
  41711. BIO_free(bio);
  41712. bio = NULL;
  41713. X509_free(x509);
  41714. x509 = NULL;
  41715. }
  41716. }
  41717. #endif /* HAVE_PKCS12 */
  41718. #endif
  41719. return EXPECT_RESULT();
  41720. }
  41721. static int test_wolfSSL_OBJ_cmp(void)
  41722. {
  41723. EXPECT_DECLS;
  41724. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  41725. ASN1_OBJECT *obj = NULL;
  41726. ASN1_OBJECT *obj2 = NULL;
  41727. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  41728. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  41729. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  41730. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  41731. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  41732. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  41733. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  41734. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  41735. ASN1_OBJECT_free(obj);
  41736. ASN1_OBJECT_free(obj2);
  41737. #endif
  41738. return EXPECT_RESULT();
  41739. }
  41740. static int test_wolfSSL_OBJ_txt2nid(void)
  41741. {
  41742. EXPECT_DECLS;
  41743. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  41744. defined(WOLFSSL_APACHE_HTTPD)
  41745. int i;
  41746. static const struct {
  41747. const char* sn;
  41748. const char* ln;
  41749. const char* oid;
  41750. int nid;
  41751. } testVals[] = {
  41752. #ifdef WOLFSSL_APACHE_HTTPD
  41753. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  41754. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  41755. NID_id_on_dnsSRV },
  41756. { "msUPN", "Microsoft User Principal Name",
  41757. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  41758. #endif
  41759. { NULL, NULL, NULL, NID_undef }
  41760. };
  41761. /* Invalid cases */
  41762. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  41763. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  41764. /* Valid cases */
  41765. for (i = 0; testVals[i].sn != NULL; i++) {
  41766. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  41767. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  41768. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  41769. }
  41770. #endif
  41771. return EXPECT_RESULT();
  41772. }
  41773. static int test_wolfSSL_OBJ_txt2obj(void)
  41774. {
  41775. EXPECT_DECLS;
  41776. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  41777. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  41778. int i;
  41779. char buf[50];
  41780. ASN1_OBJECT* obj = NULL;
  41781. static const struct {
  41782. const char* oidStr;
  41783. const char* sn;
  41784. const char* ln;
  41785. } objs_list[] = {
  41786. #if defined(WOLFSSL_APACHE_HTTPD)
  41787. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  41788. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  41789. #endif
  41790. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  41791. { NULL, NULL, NULL }
  41792. };
  41793. static const struct {
  41794. const char* numeric;
  41795. const char* name;
  41796. } objs_named[] = {
  41797. /* In dictionary but not in normal list. */
  41798. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  41799. /* Made up OID. */
  41800. { "1.3.5.7", "1.3.5.7" },
  41801. { NULL, NULL }
  41802. };
  41803. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  41804. ASN1_OBJECT_free(obj);
  41805. obj = NULL;
  41806. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  41807. ASN1_OBJECT_free(obj);
  41808. obj = NULL;
  41809. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  41810. /* Test numerical value of oid (oidStr) */
  41811. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  41812. /* Convert object back to text to confirm oid is correct */
  41813. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  41814. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  41815. ASN1_OBJECT_free(obj);
  41816. obj = NULL;
  41817. XMEMSET(buf, 0, sizeof(buf));
  41818. /* Test short name (sn) */
  41819. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  41820. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  41821. /* Convert object back to text to confirm oid is correct */
  41822. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  41823. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  41824. ASN1_OBJECT_free(obj);
  41825. obj = NULL;
  41826. XMEMSET(buf, 0, sizeof(buf));
  41827. /* Test long name (ln) - should fail when no_name = 1 */
  41828. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  41829. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  41830. /* Convert object back to text to confirm oid is correct */
  41831. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  41832. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  41833. ASN1_OBJECT_free(obj);
  41834. obj = NULL;
  41835. XMEMSET(buf, 0, sizeof(buf));
  41836. }
  41837. for (i = 0; objs_named[i].numeric != NULL; i++) {
  41838. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  41839. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  41840. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  41841. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  41842. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  41843. ASN1_OBJECT_free(obj);
  41844. obj = NULL;
  41845. }
  41846. #endif
  41847. return EXPECT_RESULT();
  41848. }
  41849. static int test_wolfSSL_PEM_write_bio_X509(void)
  41850. {
  41851. EXPECT_DECLS;
  41852. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  41853. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  41854. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  41855. !defined(NO_FILESYSTEM)
  41856. /* This test contains the hard coded expected
  41857. * lengths. Update if necessary */
  41858. XFILE fp = XBADFILE;
  41859. WOLFSSL_EVP_PKEY *priv = NULL;
  41860. BIO* input = NULL;
  41861. BIO* output = NULL;
  41862. X509* x509a = NULL;
  41863. X509* x509b = NULL;
  41864. ASN1_TIME* notBeforeA = NULL;
  41865. ASN1_TIME* notAfterA = NULL;
  41866. #ifndef NO_ASN_TIME
  41867. ASN1_TIME* notBeforeB = NULL;
  41868. ASN1_TIME* notAfterB = NULL;
  41869. #endif
  41870. int expectedLen;
  41871. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  41872. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  41873. if (fp != XBADFILE) {
  41874. XFCLOSE(fp);
  41875. fp = XBADFILE;
  41876. }
  41877. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  41878. "rb"));
  41879. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  41880. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  41881. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  41882. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  41883. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  41884. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  41885. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  41886. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  41887. /* compare length against expected */
  41888. expectedLen = 2000;
  41889. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  41890. #ifndef NO_ASN_TIME
  41891. /* read exported X509 PEM back into struct, sanity check on export,
  41892. * make sure notBefore/notAfter are the same and certs are identical. */
  41893. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  41894. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  41895. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  41896. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  41897. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  41898. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  41899. X509_free(x509b);
  41900. x509b = NULL;
  41901. #endif
  41902. /* Reset output buffer */
  41903. BIO_free(output);
  41904. output = NULL;
  41905. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  41906. /* Test forcing the AKID to be generated just from KeyIdentifier */
  41907. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  41908. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  41909. x509a->authKeyId = x509a->authKeyIdSrc;
  41910. x509a->authKeyIdSrc = NULL;
  41911. x509a->authKeyIdSrcSz = 0;
  41912. }
  41913. /* Resign to re-generate the der */
  41914. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  41915. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  41916. /* Check that we generate a smaller output since the AKID will
  41917. * only contain the KeyIdentifier without any additional
  41918. * information */
  41919. /* Here we copy the validity struct from the original */
  41920. expectedLen = 1688;
  41921. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  41922. /* Reset buffers and x509 */
  41923. BIO_free(input);
  41924. input = NULL;
  41925. BIO_free(output);
  41926. output = NULL;
  41927. X509_free(x509a);
  41928. x509a = NULL;
  41929. /* test CA and basicConstSet values are encoded when
  41930. * the cert is a CA */
  41931. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  41932. /* read PEM into X509 struct */
  41933. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  41934. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  41935. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  41936. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  41937. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  41938. * values are maintained and certs are identical.*/
  41939. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  41940. ExpectIntEQ(x509b->isCa, 1);
  41941. ExpectIntEQ(x509b->basicConstSet, 1);
  41942. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  41943. X509_free(x509a);
  41944. x509a = NULL;
  41945. X509_free(x509b);
  41946. x509b = NULL;
  41947. BIO_free(input);
  41948. input = NULL;
  41949. BIO_free(output);
  41950. output = NULL;
  41951. /* test CA and basicConstSet values are encoded when
  41952. * the cert is not CA */
  41953. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  41954. /* read PEM into X509 struct */
  41955. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  41956. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  41957. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  41958. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  41959. /* read exported X509 PEM back into struct, ensure isCa and
  41960. * basicConstSet values are maintained and certs are identical */
  41961. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  41962. ExpectIntEQ(x509b->isCa, 0);
  41963. ExpectIntEQ(x509b->basicConstSet, 1);
  41964. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  41965. wolfSSL_EVP_PKEY_free(priv);
  41966. X509_free(x509a);
  41967. X509_free(x509b);
  41968. BIO_free(input);
  41969. BIO_free(output);
  41970. #endif
  41971. return EXPECT_RESULT();
  41972. }
  41973. static int test_wolfSSL_X509_NAME_ENTRY(void)
  41974. {
  41975. EXPECT_DECLS;
  41976. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  41977. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  41978. X509* x509 = NULL;
  41979. #ifndef NO_BIO
  41980. BIO* bio = NULL;
  41981. #endif
  41982. X509_NAME* nm = NULL;
  41983. X509_NAME_ENTRY* entry = NULL;
  41984. unsigned char cn[] = "another name to add";
  41985. #ifdef OPENSSL_ALL
  41986. int i;
  41987. int names_len = 0;
  41988. #endif
  41989. ExpectNotNull(x509 =
  41990. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  41991. #ifndef NO_BIO
  41992. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41993. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  41994. #endif
  41995. #ifdef WOLFSSL_CERT_REQ
  41996. {
  41997. X509_REQ* req = NULL;
  41998. #ifndef NO_BIO
  41999. BIO* bReq = NULL;
  42000. #endif
  42001. ExpectNotNull(req =
  42002. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  42003. #ifndef NO_BIO
  42004. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  42005. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  42006. BIO_free(bReq);
  42007. #endif
  42008. X509_free(req);
  42009. }
  42010. #endif
  42011. ExpectNotNull(nm = X509_get_subject_name(x509));
  42012. /* Test add entry */
  42013. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  42014. 0x0c, cn, (int)sizeof(cn)));
  42015. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  42016. #ifdef WOLFSSL_CERT_EXT
  42017. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  42018. (byte*)"support@wolfssl.com", 19, -1,
  42019. 1), WOLFSSL_SUCCESS);
  42020. #endif
  42021. X509_NAME_ENTRY_free(entry);
  42022. entry = NULL;
  42023. #ifdef WOLFSSL_CERT_REQ
  42024. {
  42025. unsigned char srv_pkcs9p[] = "Server";
  42026. unsigned char fvrtDrnk[] = "tequila";
  42027. unsigned char* der = NULL;
  42028. char* subject = NULL;
  42029. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  42030. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  42031. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  42032. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  42033. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  42034. ExpectNotNull(der);
  42035. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  42036. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  42037. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  42038. #ifdef DEBUG_WOLFSSL
  42039. if (subject != NULL) {
  42040. fprintf(stderr, "\n\t%s\n", subject);
  42041. }
  42042. #endif
  42043. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  42044. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42045. }
  42046. #endif
  42047. /* Test add entry by text */
  42048. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  42049. 0x0c, cn, (int)sizeof(cn)));
  42050. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  42051. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  42052. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  42053. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  42054. #endif
  42055. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  42056. X509_NAME_ENTRY_free(entry);
  42057. entry = NULL;
  42058. /* Test add entry by NID */
  42059. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  42060. cn, -1, -1, 0), SSL_SUCCESS);
  42061. #ifdef OPENSSL_ALL
  42062. /* stack of name entry */
  42063. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  42064. for (i = 0; i < names_len; i++) {
  42065. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  42066. }
  42067. #endif
  42068. #ifndef NO_BIO
  42069. BIO_free(bio);
  42070. #endif
  42071. X509_free(x509); /* free's nm */
  42072. #endif
  42073. return EXPECT_RESULT();
  42074. }
  42075. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  42076. static int test_GENERAL_NAME_set0_othername(void) {
  42077. EXPECT_DECLS;
  42078. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42079. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  42080. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  42081. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  42082. defined(WOLFSSL_FPKI)
  42083. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  42084. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  42085. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  42086. const char * cert_fname = "./certs/server-cert.der";
  42087. const char * key_fname = "./certs/server-key.der";
  42088. X509* x509 = NULL;
  42089. GENERAL_NAME* gn = NULL;
  42090. GENERAL_NAMES* gns = NULL;
  42091. ASN1_OBJECT* upn_oid = NULL;
  42092. ASN1_UTF8STRING *utf8str = NULL;
  42093. ASN1_TYPE *value = NULL;
  42094. X509_EXTENSION * ext = NULL;
  42095. byte* pt = NULL;
  42096. byte der[4096];
  42097. int derSz = 0;
  42098. EVP_PKEY* priv = NULL;
  42099. XFILE f = XBADFILE;
  42100. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  42101. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  42102. if (f != XBADFILE) {
  42103. XFCLOSE(f);
  42104. f = XBADFILE;
  42105. }
  42106. ExpectNotNull(gn = GENERAL_NAME_new());
  42107. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  42108. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  42109. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  42110. ExpectNotNull(value = ASN1_TYPE_new());
  42111. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  42112. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  42113. wolfSSL_ASN1_STRING_free(utf8str);
  42114. }
  42115. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  42116. if (EXPECT_FAIL()) {
  42117. ASN1_TYPE_free(value);
  42118. }
  42119. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  42120. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  42121. if (EXPECT_FAIL()) {
  42122. GENERAL_NAME_free(gn);
  42123. gn = NULL;
  42124. }
  42125. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  42126. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  42127. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  42128. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  42129. if (f != XBADFILE) {
  42130. XFCLOSE(f);
  42131. f = XBADFILE;
  42132. }
  42133. pt = der;
  42134. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42135. (const unsigned char**)&pt, derSz));
  42136. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  42137. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  42138. gns = NULL;
  42139. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  42140. NULL));
  42141. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  42142. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  42143. ExpectIntEQ(gn->type, 0);
  42144. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  42145. ASN1_OBJECT_free(upn_oid);
  42146. X509_EXTENSION_free(ext);
  42147. X509_free(x509);
  42148. EVP_PKEY_free(priv);
  42149. #endif
  42150. return EXPECT_RESULT();
  42151. }
  42152. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  42153. static int test_othername_and_SID_ext(void) {
  42154. EXPECT_DECLS;
  42155. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42156. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  42157. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  42158. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  42159. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  42160. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  42161. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  42162. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  42163. const char* csr_fname = "./certs/csr.signed.der";
  42164. const char* key_fname = "./certs/server-key.der";
  42165. byte der[4096];
  42166. int derSz = 0;
  42167. X509_REQ* x509 = NULL;
  42168. STACK_OF(X509_EXTENSION) *exts = NULL;
  42169. X509_EXTENSION * san_ext = NULL;
  42170. X509_EXTENSION * ext = NULL;
  42171. GENERAL_NAME* gn = NULL;
  42172. GENERAL_NAMES* gns = NULL;
  42173. ASN1_OBJECT* upn_oid = NULL;
  42174. ASN1_UTF8STRING *utf8str = NULL;
  42175. ASN1_TYPE *value = NULL;
  42176. ASN1_STRING *extval = NULL;
  42177. /* SID extension. SID data format explained here:
  42178. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  42179. */
  42180. byte SidExtension[] = {
  42181. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  42182. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  42183. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  42184. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  42185. byte expectedAltName[] = {
  42186. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  42187. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  42188. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  42189. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  42190. X509_EXTENSION *sid_ext = NULL;
  42191. ASN1_OBJECT* sid_oid = NULL;
  42192. ASN1_OCTET_STRING *sid_data = NULL;
  42193. EVP_PKEY* priv = NULL;
  42194. XFILE f = XBADFILE;
  42195. byte* pt = NULL;
  42196. BIO* bio = NULL;
  42197. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  42198. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  42199. if (f != XBADFILE) {
  42200. XFCLOSE(f);
  42201. f = XBADFILE;
  42202. }
  42203. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  42204. ExpectNotNull(gn = GENERAL_NAME_new());
  42205. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  42206. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  42207. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  42208. ExpectNotNull(value = ASN1_TYPE_new());
  42209. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  42210. if (EXPECT_FAIL()) {
  42211. ASN1_UTF8STRING_free(utf8str);
  42212. }
  42213. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  42214. if (EXPECT_FAIL()) {
  42215. ASN1_TYPE_free(value);
  42216. GENERAL_NAME_free(gn);
  42217. gn = NULL;
  42218. }
  42219. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  42220. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  42221. if (EXPECT_FAIL()) {
  42222. GENERAL_NAME_free(gn);
  42223. }
  42224. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  42225. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  42226. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  42227. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  42228. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  42229. sid_data));
  42230. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  42231. /* Ensure an empty stack doesn't raise an error. */
  42232. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  42233. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  42234. if (EXPECT_FAIL()) {
  42235. X509_EXTENSION_free(san_ext);
  42236. }
  42237. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  42238. if (EXPECT_FAIL()) {
  42239. X509_EXTENSION_free(sid_ext);
  42240. }
  42241. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  42242. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  42243. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  42244. if (f != XBADFILE)
  42245. XFCLOSE(f);
  42246. pt = der;
  42247. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42248. (const unsigned char**)&pt, derSz));
  42249. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  42250. pt = der;
  42251. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  42252. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  42253. gns = NULL;
  42254. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  42255. exts = NULL;
  42256. ASN1_OBJECT_free(upn_oid);
  42257. ASN1_OBJECT_free(sid_oid);
  42258. ASN1_OCTET_STRING_free(sid_data);
  42259. X509_REQ_free(x509);
  42260. x509 = NULL;
  42261. EVP_PKEY_free(priv);
  42262. /* At this point everything used to generate what is in der is cleaned up.
  42263. * We now read back from der to confirm the extensions were inserted
  42264. * correctly. */
  42265. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  42266. ExpectNotNull(bio);
  42267. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  42268. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  42269. ExpectNotNull(x509);
  42270. BIO_free(bio);
  42271. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  42272. x509));
  42273. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  42274. /* Check the SID extension. */
  42275. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  42276. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  42277. ExpectIntEQ(extval->length, sizeof(SidExtension));
  42278. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  42279. /* Check the AltNames extension. */
  42280. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  42281. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  42282. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  42283. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  42284. 0);
  42285. /* Cleanup */
  42286. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  42287. NULL));
  42288. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  42289. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  42290. ExpectIntEQ(gn->type, 0);
  42291. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  42292. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  42293. X509_REQ_free(x509);
  42294. #endif
  42295. return EXPECT_RESULT();
  42296. }
  42297. static int test_wolfSSL_X509_set_name(void)
  42298. {
  42299. EXPECT_DECLS;
  42300. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42301. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  42302. X509* x509 = NULL;
  42303. X509_NAME* name = NULL;
  42304. ExpectNotNull(name = X509_NAME_new());
  42305. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  42306. (byte*)"wolfssl.com", 11, 0, 1),
  42307. WOLFSSL_SUCCESS);
  42308. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  42309. (byte*)"support@wolfssl.com", 19, -1,
  42310. 1), WOLFSSL_SUCCESS);
  42311. ExpectNotNull(x509 = X509_new());
  42312. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  42313. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  42314. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  42315. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  42316. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  42317. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  42318. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  42319. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  42320. X509_free(x509);
  42321. X509_NAME_free(name);
  42322. #endif /* OPENSSL_ALL && !NO_CERTS */
  42323. return EXPECT_RESULT();
  42324. }
  42325. static int test_wolfSSL_X509_set_notAfter(void)
  42326. {
  42327. EXPECT_DECLS;
  42328. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  42329. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  42330. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  42331. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  42332. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  42333. /* Generalized time will overflow time_t if not long */
  42334. X509* x = NULL;
  42335. BIO* bio = NULL;
  42336. ASN1_TIME *asn_time = NULL;
  42337. ASN1_TIME *time_check = NULL;
  42338. const int year = 365*24*60*60;
  42339. const int day = 24*60*60;
  42340. const int hour = 60*60;
  42341. const int mini = 60;
  42342. int offset_day;
  42343. unsigned char buf[25];
  42344. time_t t;
  42345. /*
  42346. * Setup asn_time. APACHE HTTPD uses time(NULL)
  42347. */
  42348. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  42349. offset_day = 7;
  42350. /*
  42351. * Free these.
  42352. */
  42353. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  42354. ExpectNotNull(asn_time);
  42355. ExpectNotNull(x = X509_new());
  42356. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42357. /*
  42358. * Tests
  42359. */
  42360. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  42361. /* time_check is simply (ANS1_TIME*)x->notAfter */
  42362. ExpectNotNull(time_check = X509_get_notAfter(x));
  42363. /* ANS1_TIME_check validates by checking if argument can be parsed */
  42364. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  42365. /* Convert to human readable format and compare to intended date */
  42366. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  42367. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  42368. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  42369. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  42370. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  42371. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  42372. /*
  42373. * Cleanup
  42374. */
  42375. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  42376. X509_free(x);
  42377. BIO_free(bio);
  42378. #endif
  42379. return EXPECT_RESULT();
  42380. }
  42381. static int test_wolfSSL_X509_set_notBefore(void)
  42382. {
  42383. EXPECT_DECLS;
  42384. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  42385. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  42386. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  42387. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  42388. X509* x = NULL;
  42389. BIO* bio = NULL;
  42390. ASN1_TIME *asn_time = NULL;
  42391. ASN1_TIME *time_check = NULL;
  42392. const int year = 365*24*60*60;
  42393. const int day = 24*60*60;
  42394. const int hour = 60*60;
  42395. const int mini = 60;
  42396. int offset_day;
  42397. unsigned char buf[25];
  42398. time_t t;
  42399. /*
  42400. * Setup asn_time. APACHE HTTPD uses time(NULL)
  42401. */
  42402. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  42403. offset_day = 7;
  42404. /*
  42405. * Free these.
  42406. */
  42407. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  42408. ExpectNotNull(asn_time);
  42409. ExpectNotNull(x = X509_new());
  42410. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42411. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  42412. /*
  42413. * Main Tests
  42414. */
  42415. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  42416. /* time_check == (ANS1_TIME*)x->notBefore */
  42417. ExpectNotNull(time_check = X509_get_notBefore(x));
  42418. /* ANS1_TIME_check validates by checking if argument can be parsed */
  42419. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  42420. /* Convert to human readable format and compare to intended date */
  42421. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  42422. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  42423. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  42424. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  42425. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  42426. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  42427. /*
  42428. * Cleanup
  42429. */
  42430. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  42431. X509_free(x);
  42432. BIO_free(bio);
  42433. #endif
  42434. return EXPECT_RESULT();
  42435. }
  42436. static int test_wolfSSL_X509_set_version(void)
  42437. {
  42438. EXPECT_DECLS;
  42439. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  42440. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  42441. X509* x509 = NULL;
  42442. long v = 2L;
  42443. long maxInt = INT_MAX;
  42444. ExpectNotNull(x509 = X509_new());
  42445. /* These should pass. */
  42446. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  42447. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  42448. /* Fail Case: When v(long) is greater than x509->version(int). */
  42449. v = maxInt+1;
  42450. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  42451. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  42452. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  42453. /* Cleanup */
  42454. X509_free(x509);
  42455. #endif
  42456. return EXPECT_RESULT();
  42457. }
  42458. #ifndef NO_BIO
  42459. static int test_wolfSSL_BIO_gets(void)
  42460. {
  42461. EXPECT_DECLS;
  42462. #if defined(OPENSSL_EXTRA)
  42463. BIO* bio = NULL;
  42464. BIO* bio2 = NULL;
  42465. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  42466. char emp[] = "";
  42467. char bio_buffer[20];
  42468. int bufferSz = 20;
  42469. #ifdef OPENSSL_ALL
  42470. BUF_MEM* emp_bm = NULL;
  42471. BUF_MEM* msg_bm = NULL;
  42472. #endif
  42473. /* try with bad args */
  42474. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  42475. #ifdef OPENSSL_ALL
  42476. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  42477. #endif
  42478. /* try with real msg */
  42479. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  42480. XMEMSET(bio_buffer, 0, bufferSz);
  42481. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  42482. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  42483. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  42484. ExpectFalse(bio2 != BIO_next(bio));
  42485. /* make buffer filled with no terminating characters */
  42486. XMEMSET(bio_buffer, 1, bufferSz);
  42487. /* BIO_gets reads a line of data */
  42488. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  42489. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  42490. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  42491. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  42492. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  42493. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  42494. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  42495. #ifdef OPENSSL_ALL
  42496. /* test setting the mem_buf manually */
  42497. BIO_free(bio);
  42498. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  42499. ExpectNotNull(emp_bm = BUF_MEM_new());
  42500. ExpectNotNull(msg_bm = BUF_MEM_new());
  42501. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  42502. if (EXPECT_SUCCESS()) {
  42503. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  42504. msg_bm->data = NULL;
  42505. }
  42506. /* emp size is 1 for terminator */
  42507. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  42508. if (EXPECT_SUCCESS()) {
  42509. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  42510. emp_bm->data = emp;
  42511. msg_bm->data = msg;
  42512. }
  42513. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  42514. /* check reading an empty string */
  42515. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  42516. ExpectStrEQ(emp, bio_buffer);
  42517. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  42518. /* BIO_gets reads a line of data */
  42519. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  42520. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  42521. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  42522. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  42523. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  42524. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  42525. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  42526. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  42527. if (EXPECT_SUCCESS())
  42528. emp_bm->data = NULL;
  42529. BUF_MEM_free(emp_bm);
  42530. if (EXPECT_SUCCESS())
  42531. msg_bm->data = NULL;
  42532. BUF_MEM_free(msg_bm);
  42533. #endif
  42534. /* check not null terminated string */
  42535. BIO_free(bio);
  42536. bio = NULL;
  42537. msg[0] = 0x33;
  42538. msg[1] = 0x33;
  42539. msg[2] = 0x33;
  42540. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  42541. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  42542. ExpectIntEQ(bio_buffer[0], msg[0]);
  42543. ExpectIntEQ(bio_buffer[1], msg[1]);
  42544. ExpectIntNE(bio_buffer[2], msg[2]);
  42545. BIO_free(bio);
  42546. bio = NULL;
  42547. msg[3] = 0x33;
  42548. bio_buffer[3] = 0x33;
  42549. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  42550. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  42551. ExpectIntEQ(bio_buffer[0], msg[0]);
  42552. ExpectIntEQ(bio_buffer[1], msg[1]);
  42553. ExpectIntEQ(bio_buffer[2], msg[2]);
  42554. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  42555. /* check reading an empty string */
  42556. BIO_free(bio);
  42557. bio = NULL;
  42558. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  42559. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  42560. ExpectStrEQ(emp, bio_buffer);
  42561. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  42562. /* check error cases */
  42563. BIO_free(bio);
  42564. bio = NULL;
  42565. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  42566. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42567. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  42568. #if !defined(NO_FILESYSTEM)
  42569. {
  42570. BIO* f_bio = NULL;
  42571. XFILE f = XBADFILE;
  42572. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  42573. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  42574. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  42575. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  42576. if (EXPECT_FAIL() && (f != XBADFILE)) {
  42577. XFCLOSE(f);
  42578. }
  42579. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  42580. BIO_free(f_bio);
  42581. f_bio = NULL;
  42582. }
  42583. #endif /* NO_FILESYSTEM */
  42584. BIO_free(bio);
  42585. bio = NULL;
  42586. BIO_free(bio2);
  42587. bio2 = NULL;
  42588. /* try with type BIO */
  42589. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  42590. sizeof(msg));
  42591. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  42592. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  42593. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  42594. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  42595. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  42596. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  42597. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  42598. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  42599. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  42600. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  42601. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  42602. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  42603. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  42604. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  42605. BIO_free(bio);
  42606. bio = NULL;
  42607. BIO_free(bio2);
  42608. bio2 = NULL;
  42609. /* check reading an empty string */
  42610. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  42611. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  42612. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  42613. ExpectStrEQ(emp, bio_buffer);
  42614. BIO_free(bio);
  42615. bio = NULL;
  42616. #endif
  42617. return EXPECT_RESULT();
  42618. }
  42619. static int test_wolfSSL_BIO_puts(void)
  42620. {
  42621. EXPECT_DECLS;
  42622. #if defined(OPENSSL_EXTRA)
  42623. BIO* bio = NULL;
  42624. char input[] = "hello\0world\n.....ok\n\0";
  42625. char output[128];
  42626. XMEMSET(output, 0, sizeof(output));
  42627. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42628. ExpectIntEQ(BIO_puts(bio, input), 5);
  42629. ExpectIntEQ(BIO_pending(bio), 5);
  42630. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  42631. ExpectIntEQ(BIO_pending(bio), 19);
  42632. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  42633. ExpectStrEQ(output, "helloworld\n");
  42634. ExpectIntEQ(BIO_pending(bio), 8);
  42635. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  42636. ExpectStrEQ(output, ".....ok\n");
  42637. ExpectIntEQ(BIO_pending(bio), 0);
  42638. ExpectIntEQ(BIO_puts(bio, ""), -1);
  42639. BIO_free(bio);
  42640. #endif
  42641. return EXPECT_RESULT();
  42642. }
  42643. static int test_wolfSSL_BIO_dump(void)
  42644. {
  42645. EXPECT_DECLS;
  42646. #if defined(OPENSSL_EXTRA)
  42647. BIO* bio;
  42648. static const unsigned char data[] = {
  42649. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  42650. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  42651. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  42652. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  42653. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  42654. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  42655. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  42656. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  42657. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  42658. 0xB4
  42659. };
  42660. /* Generated with OpenSSL. */
  42661. static const char expected[] =
  42662. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  42663. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  42664. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  42665. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  42666. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  42667. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  42668. static const char expectedAll[] =
  42669. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  42670. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  42671. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  42672. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  42673. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  42674. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  42675. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  42676. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  42677. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  42678. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  42679. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  42680. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  42681. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  42682. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  42683. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  42684. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  42685. char output[16 * 80];
  42686. int i;
  42687. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42688. /* Example key dumped. */
  42689. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  42690. sizeof(expected) - 1);
  42691. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  42692. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  42693. /* Try every possible value for a character. */
  42694. for (i = 0; i < 256; i++)
  42695. output[i] = i;
  42696. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  42697. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  42698. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  42699. BIO_free(bio);
  42700. #endif
  42701. return EXPECT_RESULT();
  42702. }
  42703. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42704. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  42705. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  42706. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  42707. {
  42708. (void)ssl;
  42709. (void)buf;
  42710. (void)sz;
  42711. (void)ctx;
  42712. return WOLFSSL_CBIO_ERR_WANT_READ;
  42713. }
  42714. #endif
  42715. static int test_wolfSSL_BIO_should_retry(void)
  42716. {
  42717. EXPECT_DECLS;
  42718. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42719. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  42720. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  42721. tcp_ready ready;
  42722. func_args server_args;
  42723. THREAD_TYPE serverThread;
  42724. SOCKET_T sockfd = 0;
  42725. WOLFSSL_CTX* ctx = NULL;
  42726. WOLFSSL* ssl = NULL;
  42727. char msg[64] = "hello wolfssl!";
  42728. char reply[1024];
  42729. int msgSz = (int)XSTRLEN(msg);
  42730. int ret;
  42731. BIO* bio = NULL;
  42732. XMEMSET(&server_args, 0, sizeof(func_args));
  42733. #ifdef WOLFSSL_TIRTOS
  42734. fdOpenSession(Task_self());
  42735. #endif
  42736. StartTCP();
  42737. InitTcpReady(&ready);
  42738. #if defined(USE_WINDOWS_API)
  42739. /* use RNG to get random port if using windows */
  42740. ready.port = GetRandomPort();
  42741. #endif
  42742. server_args.signal = &ready;
  42743. start_thread(test_server_nofail, &server_args, &serverThread);
  42744. wait_tcp_ready(&server_args);
  42745. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42746. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  42747. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  42748. #endif
  42749. ExpectIntEQ(WOLFSSL_SUCCESS,
  42750. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  42751. ExpectIntEQ(WOLFSSL_SUCCESS,
  42752. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  42753. ExpectIntEQ(WOLFSSL_SUCCESS,
  42754. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  42755. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  42756. /* force retry */
  42757. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  42758. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  42759. ExpectNotNull(ssl);
  42760. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  42761. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  42762. if (EXPECT_FAIL()) {
  42763. wolfSSL_free(ssl);
  42764. ssl = NULL;
  42765. }
  42766. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  42767. ExpectIntNE(BIO_should_retry(bio), 0);
  42768. ExpectIntEQ(BIO_should_read(bio), 0);
  42769. ExpectIntEQ(BIO_should_write(bio), 0);
  42770. /* now perform successful connection */
  42771. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  42772. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  42773. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  42774. ret = wolfSSL_get_error(ssl, -1);
  42775. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  42776. ExpectIntNE(BIO_should_retry(bio), 0);
  42777. if (ret == WOLFSSL_ERROR_WANT_READ)
  42778. ExpectIntEQ(BIO_should_read(bio), 1);
  42779. else
  42780. ExpectIntEQ(BIO_should_read(bio), 0);
  42781. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  42782. ExpectIntEQ(BIO_should_write(bio), 1);
  42783. else
  42784. ExpectIntEQ(BIO_should_write(bio), 0);
  42785. }
  42786. else {
  42787. ExpectIntEQ(BIO_should_retry(bio), 0);
  42788. ExpectIntEQ(BIO_should_read(bio), 0);
  42789. ExpectIntEQ(BIO_should_write(bio), 0);
  42790. }
  42791. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  42792. XSTRLEN("I hear you fa shizzle!")), 0);
  42793. BIO_free(bio);
  42794. wolfSSL_CTX_free(ctx);
  42795. CloseSocket(sockfd);
  42796. join_thread(serverThread);
  42797. FreeTcpReady(&ready);
  42798. #ifdef WOLFSSL_TIRTOS
  42799. fdOpenSession(Task_self());
  42800. #endif
  42801. #endif
  42802. return EXPECT_RESULT();
  42803. }
  42804. static int test_wolfSSL_BIO_connect(void)
  42805. {
  42806. EXPECT_DECLS;
  42807. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42808. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  42809. tcp_ready ready;
  42810. func_args server_args;
  42811. THREAD_TYPE serverThread;
  42812. BIO *tcpBio = NULL;
  42813. BIO *sslBio = NULL;
  42814. SSL_CTX* ctx = NULL;
  42815. SSL *ssl = NULL;
  42816. SSL *sslPtr;
  42817. char msg[] = "hello wolfssl!";
  42818. char reply[30];
  42819. char buff[10] = {0};
  42820. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42821. ExpectIntEQ(WOLFSSL_SUCCESS,
  42822. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  42823. ExpectIntEQ(WOLFSSL_SUCCESS,
  42824. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  42825. ExpectIntEQ(WOLFSSL_SUCCESS,
  42826. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  42827. /* Setup server */
  42828. XMEMSET(&server_args, 0, sizeof(func_args));
  42829. StartTCP();
  42830. InitTcpReady(&ready);
  42831. #if defined(USE_WINDOWS_API)
  42832. /* use RNG to get random port if using windows */
  42833. ready.port = GetRandomPort();
  42834. #endif
  42835. server_args.signal = &ready;
  42836. start_thread(test_server_nofail, &server_args, &serverThread);
  42837. wait_tcp_ready(&server_args);
  42838. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  42839. /* Start the test proper */
  42840. /* Setup the TCP BIO */
  42841. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  42842. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  42843. /* Setup the SSL object */
  42844. ExpectNotNull(ssl = SSL_new(ctx));
  42845. SSL_set_connect_state(ssl);
  42846. /* Setup the SSL BIO */
  42847. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  42848. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  42849. if (EXPECT_FAIL()) {
  42850. wolfSSL_free(ssl);
  42851. }
  42852. /* Verify that BIO_get_ssl works. */
  42853. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  42854. ExpectPtrEq(ssl, sslPtr);
  42855. /* Link BIO's so that sslBio uses tcpBio for IO */
  42856. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  42857. /* Do TCP connect */
  42858. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  42859. /* Do TLS handshake */
  42860. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  42861. /* Test writing */
  42862. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  42863. /* Expect length of default wolfSSL reply */
  42864. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  42865. /* Clean it all up */
  42866. BIO_free_all(sslBio);
  42867. /* Server clean up */
  42868. join_thread(serverThread);
  42869. FreeTcpReady(&ready);
  42870. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  42871. * after. */
  42872. XMEMSET(&server_args, 0, sizeof(func_args));
  42873. StartTCP();
  42874. InitTcpReady(&ready);
  42875. #if defined(USE_WINDOWS_API)
  42876. /* use RNG to get random port if using windows */
  42877. ready.port = GetRandomPort();
  42878. #endif
  42879. server_args.signal = &ready;
  42880. start_thread(test_server_nofail, &server_args, &serverThread);
  42881. wait_tcp_ready(&server_args);
  42882. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  42883. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  42884. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  42885. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  42886. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  42887. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  42888. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  42889. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  42890. /* Attempt to close the TLS connection gracefully. */
  42891. BIO_ssl_shutdown(sslBio);
  42892. BIO_free_all(sslBio);
  42893. join_thread(serverThread);
  42894. FreeTcpReady(&ready);
  42895. SSL_CTX_free(ctx);
  42896. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  42897. wc_ecc_fp_free(); /* free per thread cache */
  42898. #endif
  42899. #endif
  42900. return EXPECT_RESULT();
  42901. }
  42902. static int test_wolfSSL_BIO_tls(void)
  42903. {
  42904. EXPECT_DECLS;
  42905. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  42906. SSL_CTX* ctx = NULL;
  42907. SSL *ssl = NULL;
  42908. BIO *readBio = NULL;
  42909. BIO *writeBio = NULL;
  42910. int ret;
  42911. int err = 0;
  42912. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  42913. ExpectNotNull(ssl = SSL_new(ctx));
  42914. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  42915. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  42916. /* Qt reads data from write-bio,
  42917. * then writes the read data into plain packet.
  42918. * Qt reads data from plain packet,
  42919. * then writes the read data into read-bio.
  42920. */
  42921. SSL_set_bio(ssl, readBio, writeBio);
  42922. do {
  42923. #ifdef WOLFSSL_ASYNC_CRYPT
  42924. if (err == WC_PENDING_E) {
  42925. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  42926. if (ret < 0) { break; } else if (ret == 0) { continue; }
  42927. }
  42928. #endif
  42929. ret = SSL_connect(ssl);
  42930. err = SSL_get_error(ssl, 0);
  42931. } while (err == WC_PENDING_E);
  42932. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  42933. /* in this use case, should return WANT READ
  42934. * so that Qt will read the data from plain packet for next state.
  42935. */
  42936. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  42937. SSL_free(ssl);
  42938. SSL_CTX_free(ctx);
  42939. #endif
  42940. return EXPECT_RESULT();
  42941. }
  42942. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42943. defined(HAVE_HTTP_CLIENT)
  42944. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  42945. {
  42946. BIO* clientBio;
  42947. SSL* sslClient;
  42948. SSL_CTX* ctx;
  42949. char connectAddr[20]; /* IP + port */;
  42950. (void)args;
  42951. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  42952. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  42953. AssertIntEQ(BIO_do_connect(clientBio), 1);
  42954. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  42955. AssertNotNull(sslClient = SSL_new(ctx));
  42956. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  42957. SSL_set_bio(sslClient, clientBio, clientBio);
  42958. AssertIntEQ(SSL_connect(sslClient), 1);
  42959. SSL_free(sslClient);
  42960. SSL_CTX_free(ctx);
  42961. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  42962. wc_ecc_fp_free(); /* free per thread cache */
  42963. #endif
  42964. WOLFSSL_RETURN_FROM_THREAD(0);
  42965. }
  42966. #endif
  42967. static int test_wolfSSL_BIO_accept(void)
  42968. {
  42969. EXPECT_DECLS;
  42970. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42971. defined(HAVE_HTTP_CLIENT)
  42972. BIO* serverBindBio = NULL;
  42973. BIO* serverAcceptBio = NULL;
  42974. SSL* sslServer = NULL;
  42975. SSL_CTX* ctx = NULL;
  42976. func_args args;
  42977. THREAD_TYPE thread;
  42978. char port[10]; /* 10 bytes should be enough to store the string
  42979. * representation of the port */
  42980. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  42981. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  42982. /* First BIO_do_accept binds the port */
  42983. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  42984. XMEMSET(&args, 0, sizeof(func_args));
  42985. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  42986. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  42987. /* Let's plug it into SSL to test */
  42988. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  42989. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  42990. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  42991. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  42992. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  42993. ExpectNotNull(sslServer = SSL_new(ctx));
  42994. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  42995. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  42996. ExpectIntEQ(SSL_accept(sslServer), 1);
  42997. join_thread(thread);
  42998. BIO_free(serverBindBio);
  42999. SSL_free(sslServer);
  43000. SSL_CTX_free(ctx);
  43001. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  43002. wc_ecc_fp_free(); /* free per thread cache */
  43003. #endif
  43004. #endif
  43005. return EXPECT_RESULT();
  43006. }
  43007. static int test_wolfSSL_BIO_write(void)
  43008. {
  43009. EXPECT_DECLS;
  43010. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  43011. BIO* bio = NULL;
  43012. BIO* bio64 = NULL;
  43013. BIO* bio_mem = NULL;
  43014. BIO* ptr = NULL;
  43015. int sz;
  43016. char msg[] = "conversion test";
  43017. char out[40];
  43018. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  43019. void* bufPtr = NULL;
  43020. BUF_MEM* buf = NULL;
  43021. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  43022. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  43023. if (EXPECT_FAIL()) {
  43024. BIO_free(bio64);
  43025. }
  43026. /* now should convert to base64 then write to memory */
  43027. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  43028. BIO_flush(bio);
  43029. /* test BIO chain */
  43030. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  43031. ExpectNotNull(buf);
  43032. ExpectIntEQ(buf->length, 25);
  43033. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  43034. ExpectPtrEq(buf->data, bufPtr);
  43035. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  43036. sz = sizeof(out);
  43037. XMEMSET(out, 0, sz);
  43038. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  43039. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  43040. /* write then read should return the same message */
  43041. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  43042. sz = sizeof(out);
  43043. XMEMSET(out, 0, sz);
  43044. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  43045. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  43046. /* now try encoding with no line ending */
  43047. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  43048. #ifdef HAVE_EX_DATA
  43049. BIO_set_ex_data(bio64, 0, (void*) "data");
  43050. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  43051. #endif
  43052. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  43053. BIO_flush(bio);
  43054. sz = sizeof(out);
  43055. XMEMSET(out, 0, sz);
  43056. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  43057. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  43058. BIO_free_all(bio); /* frees bio64 also */
  43059. bio = NULL;
  43060. /* test with more than one bio64 in list */
  43061. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  43062. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  43063. if (EXPECT_FAIL()) {
  43064. BIO_free(bio64);
  43065. bio64 = NULL;
  43066. }
  43067. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  43068. ExpectNotNull(BIO_push(bio64, bio_mem));
  43069. if (EXPECT_FAIL()) {
  43070. BIO_free(bio_mem);
  43071. }
  43072. /* now should convert to base64 when stored and then decode with read */
  43073. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  43074. BIO_flush(bio);
  43075. sz = sizeof(out);
  43076. XMEMSET(out, 0, sz);
  43077. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  43078. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  43079. BIO_clear_flags(bio64, ~0);
  43080. BIO_set_retry_read(bio);
  43081. BIO_free_all(bio); /* frees bio64s also */
  43082. bio = NULL;
  43083. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  43084. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  43085. BIO_free(bio);
  43086. #endif
  43087. return EXPECT_RESULT();
  43088. }
  43089. static int test_wolfSSL_BIO_printf(void)
  43090. {
  43091. EXPECT_DECLS;
  43092. #if defined(OPENSSL_ALL)
  43093. BIO* bio = NULL;
  43094. int sz = 7;
  43095. char msg[] = "TLS 1.3 for the world";
  43096. char out[60];
  43097. char expected[] = "TLS 1.3 for the world : sz = 7";
  43098. XMEMSET(out, 0, sizeof(out));
  43099. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  43100. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  43101. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  43102. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  43103. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  43104. BIO_free(bio);
  43105. #endif
  43106. return EXPECT_RESULT();
  43107. }
  43108. static int test_wolfSSL_BIO_f_md(void)
  43109. {
  43110. EXPECT_DECLS;
  43111. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  43112. BIO* bio = NULL;
  43113. BIO* mem = NULL;
  43114. char msg[] = "message to hash";
  43115. char out[60];
  43116. EVP_MD_CTX* ctx = NULL;
  43117. const unsigned char testKey[] =
  43118. {
  43119. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  43120. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  43121. 0x0b, 0x0b, 0x0b, 0x0b
  43122. };
  43123. const char testData[] = "Hi There";
  43124. const unsigned char testResult[] =
  43125. {
  43126. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  43127. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  43128. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  43129. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  43130. };
  43131. const unsigned char expectedHash[] =
  43132. {
  43133. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  43134. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  43135. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  43136. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  43137. };
  43138. const unsigned char emptyHash[] =
  43139. {
  43140. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  43141. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  43142. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  43143. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  43144. };
  43145. unsigned char check[sizeof(testResult) + 1];
  43146. size_t checkSz = -1;
  43147. EVP_PKEY* key = NULL;
  43148. XMEMSET(out, 0, sizeof(out));
  43149. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  43150. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  43151. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  43152. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  43153. /* should not be able to write/read yet since just digest wrapper and no
  43154. * data is passing through the bio */
  43155. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  43156. ExpectIntEQ(BIO_pending(bio), 0);
  43157. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  43158. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  43159. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  43160. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  43161. BIO_reset(bio);
  43162. /* append BIO mem to bio in order to read/write */
  43163. ExpectNotNull(bio = BIO_push(bio, mem));
  43164. XMEMSET(out, 0, sizeof(out));
  43165. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  43166. ExpectIntEQ(BIO_pending(bio), 16);
  43167. /* this just reads the message and does not hash it (gets calls final) */
  43168. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  43169. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  43170. /* create a message digest using BIO */
  43171. XMEMSET(out, 0, sizeof(out));
  43172. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  43173. ExpectIntEQ(BIO_pending(mem), 16);
  43174. ExpectIntEQ(BIO_pending(bio), 16);
  43175. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  43176. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  43177. BIO_free(bio);
  43178. bio = NULL;
  43179. BIO_free(mem);
  43180. mem = NULL;
  43181. /* test with HMAC */
  43182. XMEMSET(out, 0, sizeof(out));
  43183. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  43184. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  43185. BIO_get_md_ctx(bio, &ctx);
  43186. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  43187. (int)sizeof(testKey)));
  43188. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  43189. ExpectNotNull(bio = BIO_push(bio, mem));
  43190. BIO_write(bio, testData, (int)strlen(testData));
  43191. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  43192. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  43193. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  43194. EVP_PKEY_free(key);
  43195. BIO_free(bio);
  43196. BIO_free(mem);
  43197. #endif
  43198. return EXPECT_RESULT();
  43199. }
  43200. static int test_wolfSSL_BIO_up_ref(void)
  43201. {
  43202. EXPECT_DECLS;
  43203. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  43204. BIO* bio = NULL;
  43205. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  43206. ExpectIntEQ(BIO_up_ref(NULL), 0);
  43207. ExpectIntEQ(BIO_up_ref(bio), 1);
  43208. BIO_free(bio);
  43209. ExpectIntEQ(BIO_up_ref(bio), 1);
  43210. BIO_free(bio);
  43211. BIO_free(bio);
  43212. #endif
  43213. return EXPECT_RESULT();
  43214. }
  43215. static int test_wolfSSL_BIO_reset(void)
  43216. {
  43217. EXPECT_DECLS;
  43218. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  43219. BIO* bio = NULL;
  43220. byte buf[16];
  43221. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  43222. (word32)XSTRLEN("secure your data")));
  43223. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  43224. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  43225. XMEMSET(buf, 0, 16);
  43226. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  43227. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  43228. /* You cannot write to MEM BIO with read-only mode. */
  43229. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  43230. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  43231. XMEMSET(buf, 0, 16);
  43232. ExpectIntEQ(BIO_reset(bio), 0);
  43233. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  43234. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  43235. BIO_free(bio);
  43236. #endif
  43237. return EXPECT_RESULT();
  43238. }
  43239. #endif /* !NO_BIO */
  43240. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  43241. /* test that the callback arg is correct */
  43242. static int certCbArg = 0;
  43243. static int certCb(WOLFSSL* ssl, void* arg)
  43244. {
  43245. if (ssl == NULL || arg != &certCbArg)
  43246. return 0;
  43247. if (wolfSSL_is_server(ssl)) {
  43248. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  43249. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  43250. return 0;
  43251. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  43252. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  43253. return 0;
  43254. }
  43255. else {
  43256. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  43257. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  43258. return 0;
  43259. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  43260. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  43261. return 0;
  43262. }
  43263. return 1;
  43264. }
  43265. static int certSetupCb(WOLFSSL_CTX* ctx)
  43266. {
  43267. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  43268. return TEST_SUCCESS;
  43269. }
  43270. /**
  43271. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  43272. * to stop certificate and key loading
  43273. */
  43274. static int certClearCb(WOLFSSL* ssl)
  43275. {
  43276. /* Clear the loaded certs to force the callbacks to set them up */
  43277. SSL_certs_clear(ssl);
  43278. return TEST_SUCCESS;
  43279. }
  43280. #endif
  43281. static int test_wolfSSL_cert_cb(void)
  43282. {
  43283. EXPECT_DECLS;
  43284. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  43285. test_ssl_cbf func_cb_client;
  43286. test_ssl_cbf func_cb_server;
  43287. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  43288. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  43289. func_cb_client.ctx_ready = certSetupCb;
  43290. func_cb_client.ssl_ready = certClearCb;
  43291. func_cb_server.ctx_ready = certSetupCb;
  43292. func_cb_server.ssl_ready = certClearCb;
  43293. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  43294. &func_cb_server, NULL), TEST_SUCCESS);
  43295. #endif
  43296. return EXPECT_RESULT();
  43297. }
  43298. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  43299. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  43300. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  43301. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  43302. {
  43303. EXPECT_DECLS;
  43304. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  43305. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  43306. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  43307. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  43308. return EXPECT_RESULT();
  43309. }
  43310. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  43311. {
  43312. const byte* suites = NULL;
  43313. word16 suiteSz = 0;
  43314. const byte* hashSigAlgo = NULL;
  43315. word16 hashSigAlgoSz = 0;
  43316. word16 idx = 0;
  43317. int haveRSA = 0;
  43318. int haveECC = 0;
  43319. (void)arg;
  43320. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  43321. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  43322. return 0;
  43323. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  43324. hashSigAlgoSz == 0)
  43325. return 0;
  43326. for (idx = 0; idx < suiteSz; idx += 2) {
  43327. WOLFSSL_CIPHERSUITE_INFO info =
  43328. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  43329. if (info.rsaAuth)
  43330. haveRSA = 1;
  43331. else if (info.eccAuth)
  43332. haveECC = 1;
  43333. }
  43334. if (hashSigAlgoSz > 0) {
  43335. /* sigalgs extension takes precedence over ciphersuites */
  43336. haveRSA = 0;
  43337. haveECC = 0;
  43338. }
  43339. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  43340. int hashAlgo = 0;
  43341. int sigAlgo = 0;
  43342. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  43343. &hashAlgo, &sigAlgo) != 0)
  43344. return 0;
  43345. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  43346. haveRSA = 1;
  43347. else if (sigAlgo == ECDSAk)
  43348. haveECC = 1;
  43349. }
  43350. if (haveRSA) {
  43351. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  43352. != WOLFSSL_SUCCESS)
  43353. return 0;
  43354. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  43355. != WOLFSSL_SUCCESS)
  43356. return 0;
  43357. }
  43358. else if (haveECC) {
  43359. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  43360. != WOLFSSL_SUCCESS)
  43361. return 0;
  43362. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  43363. != WOLFSSL_SUCCESS)
  43364. return 0;
  43365. }
  43366. return 1;
  43367. }
  43368. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  43369. {
  43370. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  43371. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  43372. return TEST_SUCCESS;
  43373. }
  43374. #endif
  43375. /* Testing dynamic ciphers offered by client */
  43376. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  43377. {
  43378. EXPECT_DECLS;
  43379. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  43380. test_ssl_cbf func_cb_client;
  43381. test_ssl_cbf func_cb_server;
  43382. struct {
  43383. method_provider client_meth;
  43384. const char* client_ciphers;
  43385. const char* client_sigalgs;
  43386. const char* client_ca;
  43387. method_provider server_meth;
  43388. } test_params[] = {
  43389. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  43390. #ifdef WOLFSSL_TLS13
  43391. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  43392. {wolfTLSv1_3_client_method,
  43393. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  43394. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  43395. #endif
  43396. #ifdef HAVE_ECC
  43397. {wolfTLSv1_3_client_method,
  43398. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  43399. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  43400. #endif
  43401. #endif
  43402. #ifndef WOLFSSL_NO_TLS12
  43403. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  43404. {wolfTLSv1_2_client_method,
  43405. "DHE-RSA-AES128-GCM-SHA256",
  43406. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  43407. #endif
  43408. #ifdef HAVE_ECC
  43409. {wolfTLSv1_2_client_method,
  43410. "ECDHE-ECDSA-AES128-GCM-SHA256",
  43411. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  43412. #endif
  43413. #endif
  43414. #endif
  43415. };
  43416. size_t i;
  43417. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  43418. if (testCount > 0) {
  43419. for (i = 0; i < testCount; i++) {
  43420. printf("\tTesting %s ciphers with %s sigalgs\n",
  43421. test_params[i].client_ciphers,
  43422. test_params[i].client_sigalgs);
  43423. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  43424. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  43425. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  43426. test_params[i].client_ciphers;
  43427. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  43428. test_params[i].client_sigalgs;
  43429. func_cb_client.method = test_params[i].client_meth;
  43430. func_cb_client.caPemFile = test_params[i].client_ca;
  43431. func_cb_client.ctx_ready =
  43432. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  43433. func_cb_server.ctx_ready =
  43434. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  43435. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  43436. func_cb_server.method = test_params[i].server_meth;
  43437. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  43438. &func_cb_server, NULL), TEST_SUCCESS);
  43439. }
  43440. }
  43441. #endif
  43442. return EXPECT_RESULT();
  43443. }
  43444. static int test_wolfSSL_ciphersuite_auth(void)
  43445. {
  43446. EXPECT_DECLS;
  43447. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  43448. WOLFSSL_CIPHERSUITE_INFO info;
  43449. (void)info;
  43450. #ifndef WOLFSSL_NO_TLS12
  43451. #ifdef HAVE_CHACHA
  43452. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  43453. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  43454. ExpectIntEQ(info.rsaAuth, 1);
  43455. ExpectIntEQ(info.eccAuth, 0);
  43456. ExpectIntEQ(info.eccStatic, 0);
  43457. ExpectIntEQ(info.psk, 0);
  43458. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  43459. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  43460. ExpectIntEQ(info.rsaAuth, 0);
  43461. ExpectIntEQ(info.eccAuth, 1);
  43462. ExpectIntEQ(info.eccStatic, 0);
  43463. ExpectIntEQ(info.psk, 0);
  43464. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  43465. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  43466. ExpectIntEQ(info.rsaAuth, 0);
  43467. ExpectIntEQ(info.eccAuth, 0);
  43468. ExpectIntEQ(info.eccStatic, 0);
  43469. ExpectIntEQ(info.psk, 1);
  43470. #endif
  43471. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  43472. #ifndef NO_RSA
  43473. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  43474. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  43475. ExpectIntEQ(info.rsaAuth, 1);
  43476. ExpectIntEQ(info.eccAuth, 0);
  43477. ExpectIntEQ(info.eccStatic, 0);
  43478. ExpectIntEQ(info.psk, 0);
  43479. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  43480. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  43481. ExpectIntEQ(info.rsaAuth, 1);
  43482. ExpectIntEQ(info.eccAuth, 0);
  43483. ExpectIntEQ(info.eccStatic, 1);
  43484. ExpectIntEQ(info.psk, 0);
  43485. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  43486. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  43487. ExpectIntEQ(info.rsaAuth, 1);
  43488. ExpectIntEQ(info.eccAuth, 0);
  43489. ExpectIntEQ(info.eccStatic, 1);
  43490. ExpectIntEQ(info.psk, 0);
  43491. #endif
  43492. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  43493. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  43494. ExpectIntEQ(info.rsaAuth, 0);
  43495. ExpectIntEQ(info.eccAuth, 1);
  43496. ExpectIntEQ(info.eccStatic, 0);
  43497. ExpectIntEQ(info.psk, 0);
  43498. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  43499. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  43500. ExpectIntEQ(info.rsaAuth, 0);
  43501. ExpectIntEQ(info.eccAuth, 1);
  43502. ExpectIntEQ(info.eccStatic, 1);
  43503. ExpectIntEQ(info.psk, 0);
  43504. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  43505. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  43506. ExpectIntEQ(info.rsaAuth, 0);
  43507. ExpectIntEQ(info.eccAuth, 0);
  43508. ExpectIntEQ(info.eccStatic, 0);
  43509. ExpectIntEQ(info.psk, 1);
  43510. #endif
  43511. #endif
  43512. #ifdef WOLFSSL_TLS13
  43513. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  43514. TLS_AES_128_GCM_SHA256);
  43515. ExpectIntEQ(info.rsaAuth, 0);
  43516. ExpectIntEQ(info.eccAuth, 0);
  43517. ExpectIntEQ(info.eccStatic, 0);
  43518. ExpectIntEQ(info.psk, 0);
  43519. #endif
  43520. #endif
  43521. return EXPECT_RESULT();
  43522. }
  43523. static int test_wolfSSL_sigalg_info(void)
  43524. {
  43525. EXPECT_DECLS;
  43526. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  43527. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  43528. word16 len = 0;
  43529. word16 idx = 0;
  43530. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  43531. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  43532. for (idx = 0; idx < len; idx += 2) {
  43533. int hashAlgo = 0;
  43534. int sigAlgo = 0;
  43535. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  43536. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  43537. ExpectIntNE(hashAlgo, 0);
  43538. ExpectIntNE(sigAlgo, 0);
  43539. }
  43540. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  43541. 0xFFFFFFFF, &len);
  43542. for (idx = 0; idx < len; idx += 2) {
  43543. int hashAlgo = 0;
  43544. int sigAlgo = 0;
  43545. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  43546. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  43547. ExpectIntNE(hashAlgo, 0);
  43548. }
  43549. #endif
  43550. return EXPECT_RESULT();
  43551. }
  43552. static int test_wolfSSL_SESSION(void)
  43553. {
  43554. EXPECT_DECLS;
  43555. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  43556. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  43557. !defined(NO_SESSION_CACHE)
  43558. WOLFSSL* ssl = NULL;
  43559. WOLFSSL_CTX* ctx = NULL;
  43560. WOLFSSL_SESSION* sess = NULL;
  43561. WOLFSSL_SESSION* sess_copy = NULL;
  43562. #ifdef OPENSSL_EXTRA
  43563. #ifdef HAVE_EXT_CACHE
  43564. unsigned char* sessDer = NULL;
  43565. unsigned char* ptr = NULL;
  43566. int sz = 0;
  43567. #endif
  43568. const unsigned char context[] = "user app context";
  43569. unsigned int contextSz = (unsigned int)sizeof(context);
  43570. #endif
  43571. int ret = 0, err = 0;
  43572. SOCKET_T sockfd;
  43573. tcp_ready ready;
  43574. func_args server_args;
  43575. THREAD_TYPE serverThread;
  43576. char msg[80];
  43577. const char* sendGET = "GET";
  43578. /* TLS v1.3 requires session tickets */
  43579. /* CHACHA and POLY1305 required for myTicketEncCb */
  43580. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  43581. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  43582. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  43583. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  43584. #else
  43585. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43586. #endif
  43587. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  43588. WOLFSSL_FILETYPE_PEM));
  43589. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  43590. WOLFSSL_FILETYPE_PEM));
  43591. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  43592. WOLFSSL_SUCCESS);
  43593. #ifdef WOLFSSL_ENCRYPTED_KEYS
  43594. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  43595. #endif
  43596. #ifdef HAVE_SESSION_TICKET
  43597. /* Use session tickets, for ticket tests below */
  43598. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  43599. #endif
  43600. XMEMSET(&server_args, 0, sizeof(func_args));
  43601. #ifdef WOLFSSL_TIRTOS
  43602. fdOpenSession(Task_self());
  43603. #endif
  43604. StartTCP();
  43605. InitTcpReady(&ready);
  43606. #if defined(USE_WINDOWS_API)
  43607. /* use RNG to get random port if using windows */
  43608. ready.port = GetRandomPort();
  43609. #endif
  43610. server_args.signal = &ready;
  43611. start_thread(test_server_nofail, &server_args, &serverThread);
  43612. wait_tcp_ready(&server_args);
  43613. /* client connection */
  43614. ExpectNotNull(ssl = wolfSSL_new(ctx));
  43615. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  43616. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  43617. #ifdef WOLFSSL_ASYNC_CRYPT
  43618. err = 0; /* Reset error */
  43619. #endif
  43620. do {
  43621. #ifdef WOLFSSL_ASYNC_CRYPT
  43622. if (err == WC_PENDING_E) {
  43623. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  43624. if (ret < 0) { break; } else if (ret == 0) { continue; }
  43625. }
  43626. #endif
  43627. ret = wolfSSL_connect(ssl);
  43628. err = wolfSSL_get_error(ssl, 0);
  43629. } while (err == WC_PENDING_E);
  43630. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  43631. #ifdef WOLFSSL_ASYNC_CRYPT
  43632. err = 0; /* Reset error */
  43633. #endif
  43634. do {
  43635. #ifdef WOLFSSL_ASYNC_CRYPT
  43636. if (err == WC_PENDING_E) {
  43637. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  43638. if (ret < 0) { break; } else if (ret == 0) { continue; }
  43639. }
  43640. #endif
  43641. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  43642. err = wolfSSL_get_error(ssl, 0);
  43643. } while (err == WC_PENDING_E);
  43644. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  43645. #ifdef WOLFSSL_ASYNC_CRYPT
  43646. err = 0; /* Reset error */
  43647. #endif
  43648. do {
  43649. #ifdef WOLFSSL_ASYNC_CRYPT
  43650. if (err == WC_PENDING_E) {
  43651. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  43652. if (ret < 0) { break; } else if (ret == 0) { continue; }
  43653. }
  43654. #endif
  43655. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  43656. err = wolfSSL_get_error(ssl, 0);
  43657. } while (err == WC_PENDING_E);
  43658. ExpectIntEQ(ret, 23);
  43659. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  43660. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  43661. ExpectIntEQ(wolfSSL_SessionIsSetup(sess), 1);
  43662. #ifdef HAVE_EXT_CACHE
  43663. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  43664. * HAVE_EXT_CACHE we get new objects each time */
  43665. #endif
  43666. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  43667. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  43668. sess = wolfSSL_get_session(ssl);
  43669. #ifdef OPENSSL_EXTRA
  43670. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  43671. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  43672. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  43673. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  43674. #ifdef HAVE_SESSION_TICKET
  43675. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  43676. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  43677. SESSION_TICKET_HINT_DEFAULT);
  43678. #else
  43679. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  43680. #endif
  43681. #else
  43682. (void)sess;
  43683. #endif /* OPENSSL_EXTRA */
  43684. /* Retain copy of the session for later testing */
  43685. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  43686. wolfSSL_shutdown(ssl);
  43687. wolfSSL_free(ssl); ssl = NULL;
  43688. CloseSocket(sockfd);
  43689. join_thread(serverThread);
  43690. FreeTcpReady(&ready);
  43691. #ifdef WOLFSSL_TIRTOS
  43692. fdOpenSession(Task_self());
  43693. #endif
  43694. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  43695. {
  43696. X509 *x509 = NULL;
  43697. char buf[30];
  43698. int bufSz = 0;
  43699. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  43700. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  43701. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  43702. sizeof(buf))), 0);
  43703. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  43704. if (bufSz == 7) {
  43705. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  43706. }
  43707. if (bufSz == 16) {
  43708. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  43709. }
  43710. }
  43711. #endif
  43712. #ifdef HAVE_EXT_CACHE
  43713. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  43714. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  43715. sess_copy = NULL;
  43716. #endif
  43717. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  43718. /* get session from DER and update the timeout */
  43719. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  43720. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  43721. wolfSSL_SESSION_free(sess); sess = NULL;
  43722. sess = NULL;
  43723. ptr = sessDer;
  43724. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  43725. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  43726. (const unsigned char**)&ptr, sz));
  43727. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  43728. sessDer = NULL;
  43729. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  43730. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  43731. #endif
  43732. /* successful set session test */
  43733. ExpectNotNull(ssl = wolfSSL_new(ctx));
  43734. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  43735. #ifdef HAVE_SESSION_TICKET
  43736. /* Test set/get session ticket */
  43737. {
  43738. const char* ticket = "This is a session ticket";
  43739. char buf[64] = {0};
  43740. word32 bufSz = (word32)sizeof(buf);
  43741. ExpectIntEQ(SSL_SUCCESS,
  43742. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  43743. (word32)XSTRLEN(ticket)));
  43744. ExpectIntEQ(SSL_SUCCESS,
  43745. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  43746. ExpectStrEQ(ticket, buf);
  43747. }
  43748. #endif
  43749. #ifdef OPENSSL_EXTRA
  43750. /* session timeout case */
  43751. /* make the session to be expired */
  43752. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  43753. XSLEEP_MS(1200);
  43754. /* SSL_set_session should reject specified session but return success
  43755. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  43756. */
  43757. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  43758. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  43759. #else
  43760. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  43761. #endif
  43762. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  43763. #ifdef WOLFSSL_SESSION_ID_CTX
  43764. /* fail case with miss match session context IDs (use compatibility API) */
  43765. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  43766. SSL_SUCCESS);
  43767. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  43768. wolfSSL_free(ssl); ssl = NULL;
  43769. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  43770. SSL_FAILURE);
  43771. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  43772. SSL_SUCCESS);
  43773. ExpectNotNull(ssl = wolfSSL_new(ctx));
  43774. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  43775. #endif
  43776. #endif /* OPENSSL_EXTRA */
  43777. wolfSSL_free(ssl);
  43778. wolfSSL_SESSION_free(sess);
  43779. wolfSSL_CTX_free(ctx);
  43780. #endif
  43781. return EXPECT_RESULT();
  43782. }
  43783. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  43784. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  43785. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  43786. !defined(WOLFSSL_NO_TLS12)
  43787. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  43788. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  43789. {
  43790. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  43791. /* returns previous timeout value */
  43792. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  43793. #else
  43794. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  43795. #endif
  43796. }
  43797. /* set the session to timeout in a second */
  43798. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  43799. {
  43800. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  43801. }
  43802. /* store the client side session from the first successful connection */
  43803. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  43804. {
  43805. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  43806. NULL); /* ref count 1 */
  43807. }
  43808. /* wait till session is expired then set it in the WOLFSSL struct for use */
  43809. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  43810. {
  43811. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  43812. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  43813. WOLFSSL_SUCCESS);
  43814. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  43815. }
  43816. /* set expired session in the WOLFSSL struct for use */
  43817. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  43818. {
  43819. XSLEEP_MS(1200); /* wait a second for session to expire */
  43820. /* set the expired session, call to set session fails but continuing on
  43821. after failure should be handled here */
  43822. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  43823. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  43824. WOLFSSL_SUCCESS);
  43825. #else
  43826. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  43827. WOLFSSL_SUCCESS);
  43828. #endif
  43829. }
  43830. /* check that the expired session was not reused */
  43831. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  43832. {
  43833. /* since the session has expired it should not have been reused */
  43834. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  43835. }
  43836. #endif
  43837. static int test_wolfSSL_SESSION_expire_downgrade(void)
  43838. {
  43839. EXPECT_DECLS;
  43840. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  43841. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  43842. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  43843. !defined(WOLFSSL_NO_TLS12)
  43844. WOLFSSL_CTX* ctx = NULL;
  43845. callback_functions server_cbf, client_cbf;
  43846. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  43847. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  43848. /* force server side to use TLS 1.2 */
  43849. server_cbf.ctx = ctx;
  43850. server_cbf.method = wolfTLSv1_2_server_method;
  43851. client_cbf.method = wolfSSLv23_client_method;
  43852. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  43853. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  43854. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  43855. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  43856. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  43857. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  43858. /* set the previously created session and wait till expired */
  43859. server_cbf.ctx = ctx;
  43860. client_cbf.method = wolfSSLv23_client_method;
  43861. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  43862. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  43863. client_cbf.on_result =
  43864. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  43865. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  43866. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  43867. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  43868. /* set the previously created expired session */
  43869. server_cbf.ctx = ctx;
  43870. client_cbf.method = wolfSSLv23_client_method;
  43871. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  43872. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  43873. client_cbf.on_result =
  43874. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  43875. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  43876. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  43877. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  43878. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  43879. wolfSSL_CTX_free(ctx);
  43880. #endif
  43881. return EXPECT_RESULT();
  43882. }
  43883. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  43884. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  43885. static int clientSessRemCountMalloc = 0;
  43886. static int serverSessRemCountMalloc = 0;
  43887. static int clientSessRemCountFree = 0;
  43888. static int serverSessRemCountFree = 0;
  43889. static WOLFSSL_CTX* serverSessCtx = NULL;
  43890. static WOLFSSL_SESSION* serverSess = NULL;
  43891. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  43892. !defined(NO_SESSION_CACHE_REF)
  43893. static WOLFSSL_CTX* clientSessCtx = NULL;
  43894. static WOLFSSL_SESSION* clientSess = NULL;
  43895. #endif
  43896. static int serverSessRemIdx = 3;
  43897. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  43898. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  43899. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  43900. {
  43901. int* side;
  43902. (void)ctx;
  43903. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  43904. if (side != NULL) {
  43905. if (*side == WOLFSSL_CLIENT_END)
  43906. clientSessRemCountFree++;
  43907. else
  43908. serverSessRemCountFree++;
  43909. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  43910. }
  43911. }
  43912. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  43913. {
  43914. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  43915. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  43916. !defined(NO_SESSION_CACHE_REF)
  43917. {
  43918. EXPECT_DECLS;
  43919. /* Allow downgrade, set min version, and disable TLS 1.3.
  43920. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  43921. * reference to the session cache. But with WOLFSSL_TLS13 and without
  43922. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  43923. * the session in the cache. In this case we need to downgrade to
  43924. * previous versions to just use the legacy session ID field. */
  43925. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  43926. SSL_SUCCESS);
  43927. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  43928. SSL_SUCCESS);
  43929. return EXPECT_RESULT();
  43930. }
  43931. #else
  43932. return TEST_SUCCESS;
  43933. #endif
  43934. }
  43935. static int SessRemSslSetupCb(WOLFSSL* ssl)
  43936. {
  43937. EXPECT_DECLS;
  43938. int* side;
  43939. if (SSL_is_server(ssl)) {
  43940. side = &sessRemCtx_Server;
  43941. serverSessRemCountMalloc++;
  43942. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  43943. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  43944. SSL_SUCCESS);
  43945. }
  43946. else {
  43947. side = &sessRemCtx_Client;
  43948. clientSessRemCountMalloc++;
  43949. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  43950. !defined(NO_SESSION_CACHE_REF)
  43951. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  43952. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  43953. SSL_SUCCESS);
  43954. #endif
  43955. }
  43956. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  43957. serverSessRemIdx, side), SSL_SUCCESS);
  43958. return EXPECT_RESULT();
  43959. }
  43960. #endif
  43961. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  43962. {
  43963. EXPECT_DECLS;
  43964. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  43965. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  43966. /* Check that the remove callback gets called for external data in a
  43967. * session object */
  43968. test_ssl_cbf func_cb;
  43969. XMEMSET(&func_cb, 0, sizeof(func_cb));
  43970. func_cb.ctx_ready = SessRemCtxSetupCb;
  43971. func_cb.on_result = SessRemSslSetupCb;
  43972. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  43973. NULL), TEST_SUCCESS);
  43974. /* Both should have been allocated */
  43975. ExpectIntEQ(clientSessRemCountMalloc, 1);
  43976. ExpectIntEQ(serverSessRemCountMalloc, 1);
  43977. /* This should not be called yet. Session wasn't evicted from cache yet. */
  43978. ExpectIntEQ(clientSessRemCountFree, 0);
  43979. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  43980. !defined(NO_SESSION_CACHE_REF)
  43981. /* Force a cache lookup */
  43982. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  43983. /* Force a cache update */
  43984. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  43985. /* This should set the timeout to 0 and call the remove callback from within
  43986. * the session cache. */
  43987. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  43988. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  43989. ExpectIntEQ(clientSessRemCountFree, 1);
  43990. #endif
  43991. /* Server session is in the cache so ex_data isn't free'd with the SSL
  43992. * object */
  43993. ExpectIntEQ(serverSessRemCountFree, 0);
  43994. /* Force a cache lookup */
  43995. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  43996. /* Force a cache update */
  43997. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  43998. /* This should set the timeout to 0 and call the remove callback from within
  43999. * the session cache. */
  44000. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  44001. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  44002. ExpectIntEQ(serverSessRemCountFree, 1);
  44003. /* Need to free the references that we kept */
  44004. SSL_CTX_free(serverSessCtx);
  44005. SSL_SESSION_free(serverSess);
  44006. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  44007. !defined(NO_SESSION_CACHE_REF)
  44008. SSL_CTX_free(clientSessCtx);
  44009. SSL_SESSION_free(clientSess);
  44010. #endif
  44011. #endif
  44012. return EXPECT_RESULT();
  44013. }
  44014. static int test_wolfSSL_ticket_keys(void)
  44015. {
  44016. EXPECT_DECLS;
  44017. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  44018. !defined(NO_WOLFSSL_SERVER)
  44019. WOLFSSL_CTX* ctx = NULL;
  44020. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  44021. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  44022. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  44023. WOLFSSL_FAILURE);
  44024. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  44025. WOLFSSL_FAILURE);
  44026. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  44027. WOLFSSL_FAILURE);
  44028. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  44029. WOLFSSL_FAILURE);
  44030. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  44031. WOLFSSL_FAILURE);
  44032. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  44033. WOLFSSL_FAILURE);
  44034. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  44035. WOLFSSL_FAILURE);
  44036. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  44037. WOLFSSL_FAILURE);
  44038. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  44039. WOLFSSL_FAILURE);
  44040. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  44041. WOLFSSL_FAILURE);
  44042. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  44043. WOLFSSL_FAILURE);
  44044. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  44045. WOLFSSL_FAILURE);
  44046. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  44047. WOLFSSL_FAILURE);
  44048. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  44049. WOLFSSL_FAILURE);
  44050. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  44051. WOLFSSL_SUCCESS);
  44052. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  44053. WOLFSSL_SUCCESS);
  44054. wolfSSL_CTX_free(ctx);
  44055. #endif
  44056. return EXPECT_RESULT();
  44057. }
  44058. #ifndef NO_BIO
  44059. static int test_wolfSSL_d2i_PUBKEY(void)
  44060. {
  44061. EXPECT_DECLS;
  44062. #if defined(OPENSSL_EXTRA)
  44063. BIO* bio = NULL;
  44064. EVP_PKEY* pkey = NULL;
  44065. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44066. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  44067. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  44068. /* RSA PUBKEY test */
  44069. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  44070. sizeof_client_keypub_der_2048), 0);
  44071. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  44072. EVP_PKEY_free(pkey);
  44073. pkey = NULL;
  44074. #endif
  44075. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  44076. /* ECC PUBKEY test */
  44077. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  44078. sizeof_ecc_clikeypub_der_256), 0);
  44079. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  44080. EVP_PKEY_free(pkey);
  44081. pkey = NULL;
  44082. #endif
  44083. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  44084. /* DSA PUBKEY test */
  44085. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  44086. sizeof_dsa_pub_key_der_2048), 0);
  44087. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  44088. EVP_PKEY_free(pkey);
  44089. pkey = NULL;
  44090. #endif
  44091. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  44092. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  44093. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44094. (HAVE_FIPS_VERSION > 2))
  44095. /* DH PUBKEY test */
  44096. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  44097. sizeof_dh_pub_key_der_2048), 0);
  44098. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  44099. EVP_PKEY_free(pkey);
  44100. pkey = NULL;
  44101. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  44102. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  44103. BIO_free(bio);
  44104. (void)pkey;
  44105. #endif
  44106. return EXPECT_RESULT();
  44107. }
  44108. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  44109. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  44110. {
  44111. EXPECT_DECLS;
  44112. BIO* bio = NULL;
  44113. EVP_PKEY* pkey = NULL;
  44114. WOLFSSL_CTX* ctx = NULL;
  44115. #if defined(WOLFSSL_KEY_GEN)
  44116. unsigned char buff[4096];
  44117. unsigned char* bufPtr = buff;
  44118. #endif
  44119. /* test creating new EVP_PKEY with bad arg */
  44120. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  44121. /* test loading RSA key using BIO */
  44122. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  44123. {
  44124. XFILE file = XBADFILE;
  44125. const char* fname = "./certs/server-key.der";
  44126. size_t sz = 0;
  44127. byte* buf = NULL;
  44128. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  44129. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  44130. ExpectTrue((sz = XFTELL(file)) != 0);
  44131. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  44132. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  44133. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  44134. if (file != XBADFILE) {
  44135. XFCLOSE(file);
  44136. }
  44137. /* Test using BIO new mem and loading DER private key */
  44138. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  44139. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  44140. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  44141. BIO_free(bio);
  44142. bio = NULL;
  44143. EVP_PKEY_free(pkey);
  44144. pkey = NULL;
  44145. }
  44146. #endif
  44147. /* test loading ECC key using BIO */
  44148. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  44149. {
  44150. XFILE file = XBADFILE;
  44151. const char* fname = "./certs/ecc-key.der";
  44152. size_t sz = 0;
  44153. byte* buf = NULL;
  44154. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  44155. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  44156. ExpectTrue((sz = XFTELL(file)) != 0);
  44157. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  44158. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  44159. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  44160. if (file != XBADFILE)
  44161. XFCLOSE(file);
  44162. /* Test using BIO new mem and loading DER private key */
  44163. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  44164. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  44165. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  44166. BIO_free(bio);
  44167. bio = NULL;
  44168. EVP_PKEY_free(pkey);
  44169. pkey = NULL;
  44170. }
  44171. #endif
  44172. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44173. #ifndef NO_WOLFSSL_SERVER
  44174. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  44175. #else
  44176. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  44177. #endif
  44178. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  44179. {
  44180. RSA* rsa = NULL;
  44181. /* Tests bad parameters */
  44182. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  44183. /* RSA not set yet, expecting to fail*/
  44184. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  44185. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  44186. /* set RSA using bio*/
  44187. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  44188. sizeof_client_key_der_2048), 0);
  44189. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  44190. ExpectNotNull(rsa);
  44191. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  44192. /* i2d RSAprivate key tests */
  44193. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  44194. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  44195. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  44196. sizeof_client_key_der_2048);
  44197. bufPtr -= sizeof_client_key_der_2048;
  44198. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  44199. sizeof_client_key_der_2048), 0);
  44200. bufPtr = NULL;
  44201. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  44202. sizeof_client_key_der_2048);
  44203. ExpectNotNull(bufPtr);
  44204. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  44205. sizeof_client_key_der_2048), 0);
  44206. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  44207. RSA_free(rsa);
  44208. rsa = RSA_new();
  44209. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  44210. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  44211. RSA_free(rsa);
  44212. }
  44213. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  44214. SSL_CTX_free(ctx);
  44215. ctx = NULL;
  44216. BIO_free(bio);
  44217. bio = NULL;
  44218. return EXPECT_RESULT();
  44219. }
  44220. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  44221. #endif /* !NO_BIO */
  44222. static int test_wolfSSL_sk_GENERAL_NAME(void)
  44223. {
  44224. EXPECT_DECLS;
  44225. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  44226. !defined(NO_RSA)
  44227. X509* x509 = NULL;
  44228. GENERAL_NAME* gn = NULL;
  44229. unsigned char buf[4096];
  44230. const unsigned char* bufPt = NULL;
  44231. int bytes = 0;
  44232. int i;
  44233. int j;
  44234. XFILE f = XBADFILE;
  44235. STACK_OF(GENERAL_NAME)* sk = NULL;
  44236. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  44237. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  44238. if (f != XBADFILE)
  44239. XFCLOSE(f);
  44240. for (j = 0; j < 2; ++j) {
  44241. bufPt = buf;
  44242. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  44243. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  44244. NID_subject_alt_name, NULL, NULL));
  44245. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  44246. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  44247. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  44248. if (gn != NULL) {
  44249. switch (gn->type) {
  44250. case GEN_DNS:
  44251. fprintf(stderr, "found type GEN_DNS\n");
  44252. break;
  44253. case GEN_EMAIL:
  44254. fprintf(stderr, "found type GEN_EMAIL\n");
  44255. break;
  44256. case GEN_URI:
  44257. fprintf(stderr, "found type GEN_URI\n");
  44258. break;
  44259. }
  44260. }
  44261. }
  44262. X509_free(x509);
  44263. x509 = NULL;
  44264. if (j == 0) {
  44265. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  44266. }
  44267. else {
  44268. /*
  44269. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  44270. * it was supposed to. This is a regression test for that bug.
  44271. */
  44272. GENERAL_NAMES_free(sk);
  44273. }
  44274. sk = NULL;
  44275. }
  44276. #endif
  44277. return EXPECT_RESULT();
  44278. }
  44279. static int test_wolfSSL_GENERAL_NAME_print(void)
  44280. {
  44281. EXPECT_DECLS;
  44282. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  44283. X509* x509 = NULL;
  44284. GENERAL_NAME* gn = NULL;
  44285. unsigned char buf[4096];
  44286. const unsigned char* bufPt = NULL;
  44287. int bytes;
  44288. XFILE f = XBADFILE;
  44289. STACK_OF(GENERAL_NAME)* sk = NULL;
  44290. BIO* out = NULL;
  44291. unsigned char outbuf[128];
  44292. X509_EXTENSION* ext = NULL;
  44293. AUTHORITY_INFO_ACCESS* aia = NULL;
  44294. ACCESS_DESCRIPTION* ad = NULL;
  44295. ASN1_IA5STRING *dnsname = NULL;
  44296. const unsigned char v4Addr[] = {192,168,53,1};
  44297. const unsigned char v6Addr[] =
  44298. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  44299. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  44300. const unsigned char email[] =
  44301. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  44302. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  44303. const char* dnsStr = "DNS:example.com";
  44304. const char* uriStr = "URI:http://127.0.0.1:22220";
  44305. const char* v4addStr = "IP Address:192.168.53.1";
  44306. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  44307. const char* emailStr = "email:info@wolfssl.com";
  44308. const char* othrStr = "othername:<unsupported>";
  44309. const char* x400Str = "X400Name:<unsupported>";
  44310. const char* ediStr = "EdiPartyName:<unsupported>";
  44311. /* BIO to output */
  44312. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  44313. /* test for NULL param */
  44314. gn = NULL;
  44315. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  44316. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  44317. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  44318. /* test for GEN_DNS */
  44319. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  44320. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  44321. if (f != XBADFILE) {
  44322. XFCLOSE(f);
  44323. f = XBADFILE;
  44324. }
  44325. bufPt = buf;
  44326. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  44327. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  44328. NID_subject_alt_name, NULL, NULL));
  44329. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  44330. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44331. XMEMSET(outbuf, 0, sizeof(outbuf));
  44332. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44333. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  44334. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  44335. gn = NULL;
  44336. sk = NULL;
  44337. X509_free(x509);
  44338. x509 = NULL;
  44339. /* Lets test for setting as well. */
  44340. ExpectNotNull(gn = GENERAL_NAME_new());
  44341. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  44342. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  44343. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  44344. dnsname = NULL;
  44345. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44346. XMEMSET(outbuf, 0, sizeof(outbuf));
  44347. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44348. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  44349. GENERAL_NAME_free(gn);
  44350. /* test for GEN_URI */
  44351. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  44352. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44353. if (f != XBADFILE) {
  44354. XFCLOSE(f);
  44355. f = XBADFILE;
  44356. }
  44357. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  44358. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  44359. ext));
  44360. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  44361. if (ad != NULL) {
  44362. gn = ad->location;
  44363. }
  44364. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44365. gn = NULL;
  44366. XMEMSET(outbuf,0,sizeof(outbuf));
  44367. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44368. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  44369. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  44370. aia = NULL;
  44371. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  44372. ExpectNotNull(aia);
  44373. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  44374. aia = NULL;
  44375. X509_free(x509);
  44376. x509 = NULL;
  44377. /* test for GEN_IPADD */
  44378. /* ip v4 address */
  44379. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44380. if (gn != NULL) {
  44381. gn->type = GEN_IPADD;
  44382. if (gn->d.iPAddress != NULL) {
  44383. gn->d.iPAddress->length = sizeof(v4Addr);
  44384. }
  44385. }
  44386. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  44387. sizeof(v4Addr)), 1);
  44388. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44389. XMEMSET(outbuf,0,sizeof(outbuf));
  44390. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44391. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  44392. GENERAL_NAME_free(gn);
  44393. gn = NULL;
  44394. /* ip v6 address */
  44395. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44396. if (gn != NULL) {
  44397. gn->type = GEN_IPADD;
  44398. if (gn->d.iPAddress != NULL) {
  44399. gn->d.iPAddress->length = sizeof(v6Addr);
  44400. }
  44401. }
  44402. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  44403. sizeof(v6Addr)), 1);
  44404. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44405. XMEMSET(outbuf,0,sizeof(outbuf));
  44406. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44407. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  44408. GENERAL_NAME_free(gn);
  44409. gn = NULL;
  44410. /* test for GEN_EMAIL */
  44411. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44412. if (gn != NULL) {
  44413. gn->type = GEN_EMAIL;
  44414. if (gn->d.rfc822Name != NULL) {
  44415. gn->d.rfc822Name->length = sizeof(email);
  44416. }
  44417. }
  44418. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  44419. 1);
  44420. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44421. XMEMSET(outbuf,0,sizeof(outbuf));
  44422. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44423. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  44424. GENERAL_NAME_free(gn);
  44425. gn = NULL;
  44426. /* test for GEN_OTHERNAME */
  44427. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44428. if (gn != NULL) {
  44429. gn->type = GEN_OTHERNAME;
  44430. }
  44431. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44432. XMEMSET(outbuf,0,sizeof(outbuf));
  44433. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44434. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  44435. GENERAL_NAME_free(gn);
  44436. gn = NULL;
  44437. /* test for GEN_X400 */
  44438. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44439. if (gn != NULL) {
  44440. gn->type = GEN_X400;
  44441. }
  44442. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44443. XMEMSET(outbuf,0,sizeof(outbuf));
  44444. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44445. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  44446. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  44447. if (gn != NULL) {
  44448. gn->type = GEN_IA5;
  44449. }
  44450. GENERAL_NAME_free(gn);
  44451. gn = NULL;
  44452. /* test for GEN_EDIPARTY */
  44453. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44454. if (gn != NULL) {
  44455. gn->type = GEN_EDIPARTY;
  44456. }
  44457. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44458. XMEMSET(outbuf,0,sizeof(outbuf));
  44459. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44460. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  44461. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  44462. if (gn != NULL) {
  44463. gn->type = GEN_IA5;
  44464. }
  44465. GENERAL_NAME_free(gn);
  44466. gn = NULL;
  44467. BIO_free(out);
  44468. #endif /* OPENSSL_ALL */
  44469. return EXPECT_RESULT();
  44470. }
  44471. static int test_wolfSSL_sk_DIST_POINT(void)
  44472. {
  44473. EXPECT_DECLS;
  44474. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  44475. !defined(NO_RSA)
  44476. X509* x509 = NULL;
  44477. unsigned char buf[4096];
  44478. const unsigned char* bufPt;
  44479. int bytes = 0;
  44480. int i = 0;
  44481. int j = 0;
  44482. XFILE f = XBADFILE;
  44483. DIST_POINT* dp = NULL;
  44484. DIST_POINT_NAME* dpn = NULL;
  44485. GENERAL_NAME* gn = NULL;
  44486. ASN1_IA5STRING* uri = NULL;
  44487. STACK_OF(DIST_POINT)* dps = NULL;
  44488. STACK_OF(GENERAL_NAME)* gns = NULL;
  44489. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  44490. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  44491. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  44492. if (f != XBADFILE)
  44493. XFCLOSE(f);
  44494. bufPt = buf;
  44495. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  44496. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  44497. NID_crl_distribution_points, NULL, NULL));
  44498. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  44499. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  44500. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  44501. ExpectNotNull(dpn = dp->distpoint);
  44502. /* this should be type 0, fullname */
  44503. ExpectIntEQ(dpn->type, 0);
  44504. ExpectNotNull(gns = dp->distpoint->name.fullname);
  44505. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  44506. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  44507. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  44508. ExpectIntEQ(gn->type, GEN_URI);
  44509. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  44510. ExpectNotNull(uri->data);
  44511. ExpectIntGT(uri->length, 0);
  44512. }
  44513. }
  44514. X509_free(x509);
  44515. CRL_DIST_POINTS_free(dps);
  44516. #endif
  44517. return EXPECT_RESULT();
  44518. }
  44519. static int test_wolfSSL_verify_mode(void)
  44520. {
  44521. EXPECT_DECLS;
  44522. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44523. WOLFSSL* ssl = NULL;
  44524. WOLFSSL_CTX* ctx = NULL;
  44525. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44526. ExpectNotNull(ssl = SSL_new(ctx));
  44527. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  44528. SSL_free(ssl);
  44529. ssl = NULL;
  44530. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  44531. ExpectNotNull(ssl = SSL_new(ctx));
  44532. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  44533. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  44534. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  44535. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  44536. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  44537. SSL_free(ssl);
  44538. ssl = NULL;
  44539. wolfSSL_CTX_set_verify(ctx,
  44540. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  44541. ExpectNotNull(ssl = SSL_new(ctx));
  44542. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  44543. ExpectIntEQ(SSL_get_verify_mode(ssl),
  44544. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  44545. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  44546. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  44547. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  44548. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  44549. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  44550. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  44551. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  44552. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  44553. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  44554. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  44555. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  44556. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  44557. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  44558. #endif
  44559. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  44560. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  44561. SSL_free(ssl);
  44562. SSL_CTX_free(ctx);
  44563. #endif
  44564. return EXPECT_RESULT();
  44565. }
  44566. static int test_wolfSSL_verify_depth(void)
  44567. {
  44568. EXPECT_DECLS;
  44569. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  44570. WOLFSSL* ssl = NULL;
  44571. WOLFSSL_CTX* ctx = NULL;
  44572. long depth;
  44573. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44574. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  44575. ExpectNotNull(ssl = SSL_new(ctx));
  44576. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  44577. SSL_free(ssl);
  44578. ssl = NULL;
  44579. SSL_CTX_set_verify_depth(ctx, -1);
  44580. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  44581. SSL_CTX_set_verify_depth(ctx, 2);
  44582. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  44583. ExpectNotNull(ssl = SSL_new(ctx));
  44584. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  44585. SSL_free(ssl);
  44586. SSL_CTX_free(ctx);
  44587. #endif
  44588. return EXPECT_RESULT();
  44589. }
  44590. static int test_wolfSSL_verify_result(void)
  44591. {
  44592. EXPECT_DECLS;
  44593. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  44594. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  44595. WOLFSSL* ssl = NULL;
  44596. WOLFSSL_CTX* ctx = NULL;
  44597. long result = 0xDEADBEEF;
  44598. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  44599. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44600. ExpectNotNull(ssl = SSL_new(ctx));
  44601. wolfSSL_set_verify_result(ssl, result);
  44602. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  44603. SSL_free(ssl);
  44604. SSL_CTX_free(ctx);
  44605. #endif
  44606. return EXPECT_RESULT();
  44607. }
  44608. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  44609. static void sslMsgCb(int w, int version, int type, const void* buf,
  44610. size_t sz, SSL* ssl, void* arg)
  44611. {
  44612. int i;
  44613. unsigned char* pt = (unsigned char*)buf;
  44614. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  44615. (w)?"Writing":"Reading", (int)sz, version, type);
  44616. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  44617. fprintf(stderr, "\n");
  44618. (void)ssl;
  44619. (void)arg;
  44620. }
  44621. #endif /* OPENSSL_EXTRA */
  44622. static int test_wolfSSL_msg_callback(void)
  44623. {
  44624. EXPECT_DECLS;
  44625. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  44626. WOLFSSL* ssl = NULL;
  44627. WOLFSSL_CTX* ctx = NULL;
  44628. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44629. ExpectNotNull(ssl = SSL_new(ctx));
  44630. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  44631. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  44632. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  44633. SSL_free(ssl);
  44634. SSL_CTX_free(ctx);
  44635. #endif
  44636. return EXPECT_RESULT();
  44637. }
  44638. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  44639. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  44640. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  44641. static void binary_dump(void *ptr, int size)
  44642. {
  44643. #ifdef WOLFSSL_EVP_PRINT
  44644. int i = 0;
  44645. unsigned char *p = (unsigned char *) ptr;
  44646. fprintf(stderr, "{");
  44647. while ((p != NULL) && (i < size)) {
  44648. if ((i % 8) == 0) {
  44649. fprintf(stderr, "\n");
  44650. fprintf(stderr, " ");
  44651. }
  44652. fprintf(stderr, "0x%02x, ", p[i]);
  44653. i++;
  44654. }
  44655. fprintf(stderr, "\n};\n");
  44656. #else
  44657. (void) ptr;
  44658. (void) size;
  44659. #endif
  44660. }
  44661. static int last_val = 0x0f;
  44662. static int check_result(unsigned char *data, int len)
  44663. {
  44664. int i;
  44665. for ( ; len; ) {
  44666. last_val = (last_val + 1) % 16;
  44667. for (i = 0; i < 16; len--, i++, data++)
  44668. if (*data != last_val) {
  44669. return -1;
  44670. }
  44671. }
  44672. return 0;
  44673. }
  44674. static int r_offset;
  44675. static int w_offset;
  44676. static void init_offset(void)
  44677. {
  44678. r_offset = 0;
  44679. w_offset = 0;
  44680. }
  44681. static void get_record(unsigned char *data, unsigned char *buf, int len)
  44682. {
  44683. XMEMCPY(buf, data+r_offset, len);
  44684. r_offset += len;
  44685. }
  44686. static void set_record(unsigned char *data, unsigned char *buf, int len)
  44687. {
  44688. XMEMCPY(data+w_offset, buf, len);
  44689. w_offset += len;
  44690. }
  44691. static void set_plain(unsigned char *plain, int rec)
  44692. {
  44693. int i, j;
  44694. unsigned char *p = plain;
  44695. #define BLOCKSZ 16
  44696. for (i=0; i<(rec/BLOCKSZ); i++) {
  44697. for (j=0; j<BLOCKSZ; j++)
  44698. *p++ = (i % 16);
  44699. }
  44700. }
  44701. #endif
  44702. static int test_wolfSSL_EVP_Cipher_extra(void)
  44703. {
  44704. EXPECT_DECLS;
  44705. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  44706. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  44707. /* aes128-cbc, keylen=16, ivlen=16 */
  44708. byte aes128_cbc_key[] = {
  44709. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  44710. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  44711. };
  44712. byte aes128_cbc_iv[] = {
  44713. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  44714. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  44715. };
  44716. /* teset data size table */
  44717. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  44718. int test_drive2[] = {8, 3, 8, 512, 0};
  44719. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  44720. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  44721. int test_drive_len[100];
  44722. int ret = 0;
  44723. EVP_CIPHER_CTX *evp = NULL;
  44724. int ilen = 0;
  44725. int klen = 0;
  44726. int i, j;
  44727. const EVP_CIPHER *type;
  44728. byte *iv;
  44729. byte *key;
  44730. int ivlen;
  44731. int keylen;
  44732. #define RECORDS 16
  44733. #define BUFFSZ 512
  44734. byte plain [BUFFSZ * RECORDS];
  44735. byte cipher[BUFFSZ * RECORDS];
  44736. byte inb[BUFFSZ];
  44737. byte outb[BUFFSZ+16];
  44738. int outl = 0;
  44739. int inl;
  44740. iv = aes128_cbc_iv;
  44741. ivlen = sizeof(aes128_cbc_iv);
  44742. key = aes128_cbc_key;
  44743. keylen = sizeof(aes128_cbc_key);
  44744. type = EVP_aes_128_cbc();
  44745. set_plain(plain, BUFFSZ * RECORDS);
  44746. SSL_library_init();
  44747. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  44748. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  44749. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  44750. klen = EVP_CIPHER_CTX_key_length(evp);
  44751. if (klen > 0 && keylen != klen) {
  44752. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  44753. }
  44754. ilen = EVP_CIPHER_CTX_iv_length(evp);
  44755. if (ilen > 0 && ivlen != ilen) {
  44756. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  44757. }
  44758. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  44759. for (j = 0; j<RECORDS; j++)
  44760. {
  44761. inl = BUFFSZ;
  44762. get_record(plain, inb, inl);
  44763. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  44764. set_record(cipher, outb, outl);
  44765. }
  44766. for (i = 0; test_drive[i]; i++) {
  44767. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  44768. init_offset();
  44769. test_drive_len[i] = 0;
  44770. for (j = 0; test_drive[i][j]; j++)
  44771. {
  44772. inl = test_drive[i][j];
  44773. test_drive_len[i] += inl;
  44774. get_record(plain, inb, inl);
  44775. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  44776. 0);
  44777. /* output to cipher buffer, so that following Dec test can detect
  44778. if any error */
  44779. set_record(cipher, outb, outl);
  44780. }
  44781. EVP_CipherFinal(evp, outb, &outl);
  44782. if (outl > 0)
  44783. set_record(cipher, outb, outl);
  44784. }
  44785. for (i = 0; test_drive[i]; i++) {
  44786. last_val = 0x0f;
  44787. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  44788. init_offset();
  44789. for (j = 0; test_drive[i][j]; j++) {
  44790. inl = test_drive[i][j];
  44791. get_record(cipher, inb, inl);
  44792. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  44793. 0);
  44794. binary_dump(outb, outl);
  44795. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  44796. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  44797. }
  44798. ret = EVP_CipherFinal(evp, outb, &outl);
  44799. binary_dump(outb, outl);
  44800. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  44801. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  44802. ExpectTrue(ret);
  44803. }
  44804. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  44805. EVP_CIPHER_CTX_free(evp);
  44806. evp = NULL;
  44807. /* Do an extra test to verify correct behavior with empty input. */
  44808. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  44809. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  44810. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  44811. klen = EVP_CIPHER_CTX_key_length(evp);
  44812. if (klen > 0 && keylen != klen) {
  44813. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  44814. }
  44815. ilen = EVP_CIPHER_CTX_iv_length(evp);
  44816. if (ilen > 0 && ivlen != ilen) {
  44817. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  44818. }
  44819. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  44820. /* outl should be set to 0 after passing NULL, 0 for input args. */
  44821. outl = -1;
  44822. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  44823. ExpectIntEQ(outl, 0);
  44824. EVP_CIPHER_CTX_free(evp);
  44825. #endif /* test_EVP_Cipher */
  44826. return EXPECT_RESULT();
  44827. }
  44828. static int test_wolfSSL_PEM_read_DHparams(void)
  44829. {
  44830. EXPECT_DECLS;
  44831. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  44832. !defined(NO_FILESYSTEM)
  44833. DH* dh = NULL;
  44834. XFILE fp = XBADFILE;
  44835. unsigned char derOut[300];
  44836. unsigned char* derOutBuf = derOut;
  44837. int derOutSz = 0;
  44838. unsigned char derExpected[300];
  44839. int derExpectedSz = 0;
  44840. XMEMSET(derOut, 0, sizeof(derOut));
  44841. XMEMSET(derExpected, 0, sizeof(derExpected));
  44842. /* open DH param file, read into DH struct */
  44843. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  44844. /* bad args */
  44845. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  44846. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  44847. /* good args */
  44848. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  44849. if (fp != XBADFILE) {
  44850. XFCLOSE(fp);
  44851. fp = XBADFILE;
  44852. }
  44853. /* read in certs/dh2048.der for comparison against exported params */
  44854. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  44855. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  44856. fp), 0);
  44857. if (fp != XBADFILE) {
  44858. XFCLOSE(fp);
  44859. fp = XBADFILE;
  44860. }
  44861. /* export DH back to DER and compare */
  44862. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  44863. ExpectIntEQ(derOutSz, derExpectedSz);
  44864. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  44865. DH_free(dh);
  44866. dh = NULL;
  44867. /* Test parsing with X9.42 header */
  44868. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  44869. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  44870. if (fp != XBADFILE)
  44871. XFCLOSE(fp);
  44872. DH_free(dh);
  44873. dh = NULL;
  44874. #endif
  44875. return EXPECT_RESULT();
  44876. }
  44877. static int test_wolfSSL_X509_get_serialNumber(void)
  44878. {
  44879. EXPECT_DECLS;
  44880. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  44881. ASN1_INTEGER* a = NULL;
  44882. BIGNUM* bn = NULL;
  44883. X509* x509 = NULL;
  44884. char *serialHex = NULL;
  44885. byte serial[3];
  44886. int serialSz;
  44887. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  44888. SSL_FILETYPE_PEM));
  44889. ExpectNotNull(a = X509_get_serialNumber(x509));
  44890. /* check on value of ASN1 Integer */
  44891. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  44892. a = NULL;
  44893. /* test setting serial number and then retrieving it */
  44894. ExpectNotNull(a = ASN1_INTEGER_new());
  44895. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  44896. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  44897. serialSz = sizeof(serial);
  44898. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  44899. WOLFSSL_SUCCESS);
  44900. ExpectIntEQ(serialSz, 1);
  44901. ExpectIntEQ(serial[0], 3);
  44902. ASN1_INTEGER_free(a);
  44903. a = NULL;
  44904. /* test setting serial number with 0's in it */
  44905. serial[0] = 0x01;
  44906. serial[1] = 0x00;
  44907. serial[2] = 0x02;
  44908. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  44909. if (a != NULL) {
  44910. a->data[0] = ASN_INTEGER;
  44911. a->data[1] = sizeof(serial);
  44912. XMEMCPY(&a->data[2], serial, sizeof(serial));
  44913. a->length = sizeof(serial) + 2;
  44914. }
  44915. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  44916. XMEMSET(serial, 0, sizeof(serial));
  44917. serialSz = sizeof(serial);
  44918. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  44919. WOLFSSL_SUCCESS);
  44920. ExpectIntEQ(serialSz, 3);
  44921. ExpectIntEQ(serial[0], 0x01);
  44922. ExpectIntEQ(serial[1], 0x00);
  44923. ExpectIntEQ(serial[2], 0x02);
  44924. ASN1_INTEGER_free(a);
  44925. a = NULL;
  44926. X509_free(x509); /* free's a */
  44927. ExpectNotNull(serialHex = BN_bn2hex(bn));
  44928. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  44929. ExpectStrEQ(serialHex, "01");
  44930. #else
  44931. ExpectStrEQ(serialHex, "1");
  44932. #endif
  44933. OPENSSL_free(serialHex);
  44934. ExpectIntEQ(BN_get_word(bn), 1);
  44935. BN_free(bn);
  44936. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  44937. ExpectNotNull(a = ASN1_INTEGER_new());
  44938. if (a != NULL) {
  44939. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  44940. DYNAMIC_TYPE_OPENSSL));
  44941. a->isDynamic = 1;
  44942. ASN1_INTEGER_free(a);
  44943. }
  44944. #endif
  44945. return EXPECT_RESULT();
  44946. }
  44947. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  44948. {
  44949. EXPECT_DECLS;
  44950. #if defined(OPENSSL_EXTRA)
  44951. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  44952. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  44953. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  44954. #endif
  44955. return EXPECT_RESULT();
  44956. }
  44957. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  44958. {
  44959. EXPECT_DECLS;
  44960. #if defined(OPENSSL_EXTRA)
  44961. #define MAX_HEXSTR_BUFSZ 9
  44962. #define NUM_CASES 5
  44963. struct Output {
  44964. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  44965. long ret;
  44966. };
  44967. int i;
  44968. int j;
  44969. const char* inputs[NUM_CASES] = {
  44970. "aabcd1357e",
  44971. "01:12:23:34:a5:b6:c7:d8:e9",
  44972. ":01:02",
  44973. "012",
  44974. ":ab:ac:d"
  44975. };
  44976. struct Output expectedOutputs[NUM_CASES] = {
  44977. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  44978. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  44979. {{0x01, 0x02}, 2},
  44980. {{0x00}, 0},
  44981. {{0x00}, 0}
  44982. };
  44983. long len = 0;
  44984. unsigned char* returnedBuf = NULL;
  44985. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  44986. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  44987. if (returnedBuf == NULL) {
  44988. ExpectIntEQ(expectedOutputs[i].ret, 0);
  44989. continue;
  44990. }
  44991. ExpectIntEQ(expectedOutputs[i].ret, len);
  44992. for (j = 0; j < len; ++j) {
  44993. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  44994. }
  44995. OPENSSL_free(returnedBuf);
  44996. returnedBuf = NULL;
  44997. }
  44998. #endif
  44999. return EXPECT_RESULT();
  45000. }
  45001. static int test_wolfSSL_X509_CA_num(void)
  45002. {
  45003. EXPECT_DECLS;
  45004. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  45005. defined(HAVE_ECC) && !defined(NO_RSA)
  45006. WOLFSSL_X509_STORE *store = NULL;
  45007. WOLFSSL_X509 *x509_1 = NULL;
  45008. WOLFSSL_X509 *x509_2 = NULL;
  45009. int ca_num = 0;
  45010. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  45011. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  45012. WOLFSSL_FILETYPE_PEM));
  45013. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  45014. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  45015. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  45016. WOLFSSL_FILETYPE_PEM));
  45017. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  45018. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  45019. wolfSSL_X509_free(x509_1);
  45020. wolfSSL_X509_free(x509_2);
  45021. wolfSSL_X509_STORE_free(store);
  45022. #endif
  45023. return EXPECT_RESULT();
  45024. }
  45025. static int test_wolfSSL_X509_check_ca(void)
  45026. {
  45027. EXPECT_DECLS;
  45028. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  45029. WOLFSSL_X509 *x509 = NULL;
  45030. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  45031. WOLFSSL_FILETYPE_PEM));
  45032. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  45033. wolfSSL_X509_free(x509);
  45034. #endif
  45035. return EXPECT_RESULT();
  45036. }
  45037. static int test_wolfSSL_X509_check_ip_asc(void)
  45038. {
  45039. EXPECT_DECLS;
  45040. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  45041. WOLFSSL_X509 *x509 = NULL;
  45042. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  45043. WOLFSSL_FILETYPE_PEM));
  45044. #if 0
  45045. /* TODO: add cert gen for testing positive case */
  45046. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  45047. #endif
  45048. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  45049. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  45050. wolfSSL_X509_free(x509);
  45051. #endif
  45052. return EXPECT_RESULT();
  45053. }
  45054. static int test_wolfSSL_make_cert(void)
  45055. {
  45056. EXPECT_DECLS;
  45057. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  45058. defined(WOLFSSL_CERT_EXT)
  45059. int ret = 0;
  45060. Cert cert;
  45061. CertName name;
  45062. RsaKey key;
  45063. WC_RNG rng;
  45064. byte der[FOURK_BUF];
  45065. word32 idx = 0;
  45066. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  45067. #ifdef OPENSSL_EXTRA
  45068. const unsigned char* pt = NULL;
  45069. int certSz = 0;
  45070. X509* x509 = NULL;
  45071. X509_NAME* x509name = NULL;
  45072. X509_NAME_ENTRY* entry = NULL;
  45073. ASN1_STRING* entryValue = NULL;
  45074. #endif
  45075. XMEMSET(&name, 0, sizeof(CertName));
  45076. /* set up cert name */
  45077. XMEMCPY(name.country, "US", sizeof("US"));
  45078. name.countryEnc = CTC_PRINTABLE;
  45079. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  45080. name.stateEnc = CTC_UTF8;
  45081. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  45082. name.localityEnc = CTC_UTF8;
  45083. XMEMCPY(name.sur, "Test", sizeof("Test"));
  45084. name.surEnc = CTC_UTF8;
  45085. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  45086. name.orgEnc = CTC_UTF8;
  45087. XMEMCPY(name.unit, "Development", sizeof("Development"));
  45088. name.unitEnc = CTC_UTF8;
  45089. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  45090. name.commonNameEnc = CTC_UTF8;
  45091. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  45092. name.serialDevEnc = CTC_PRINTABLE;
  45093. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  45094. name.userIdEnc = CTC_PRINTABLE;
  45095. #ifdef WOLFSSL_MULTI_ATTRIB
  45096. #if CTC_MAX_ATTRIB > 2
  45097. {
  45098. NameAttrib* n;
  45099. n = &name.name[0];
  45100. n->id = ASN_DOMAIN_COMPONENT;
  45101. n->type = CTC_UTF8;
  45102. n->sz = sizeof("com");
  45103. XMEMCPY(n->value, "com", sizeof("com"));
  45104. n = &name.name[1];
  45105. n->id = ASN_DOMAIN_COMPONENT;
  45106. n->type = CTC_UTF8;
  45107. n->sz = sizeof("wolfssl");
  45108. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  45109. }
  45110. #endif
  45111. #endif /* WOLFSSL_MULTI_ATTRIB */
  45112. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  45113. #ifndef HAVE_FIPS
  45114. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  45115. #else
  45116. ExpectIntEQ(wc_InitRng(&rng), 0);
  45117. #endif
  45118. /* load test RSA key */
  45119. idx = 0;
  45120. #if defined(USE_CERT_BUFFERS_1024)
  45121. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  45122. sizeof_server_key_der_1024), 0);
  45123. #elif defined(USE_CERT_BUFFERS_2048)
  45124. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  45125. sizeof_server_key_der_2048), 0);
  45126. #else
  45127. /* error case, no RSA key loaded, happens later */
  45128. (void)idx;
  45129. #endif
  45130. XMEMSET(&cert, 0 , sizeof(Cert));
  45131. ExpectIntEQ(wc_InitCert(&cert), 0);
  45132. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  45133. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  45134. cert.serialSz = (int)sizeof(mySerial);
  45135. cert.isCA = 1;
  45136. #ifndef NO_SHA256
  45137. cert.sigType = CTC_SHA256wRSA;
  45138. #else
  45139. cert.sigType = CTC_SHAwRSA;
  45140. #endif
  45141. /* add SKID from the Public Key */
  45142. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  45143. /* add AKID from the Public Key */
  45144. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  45145. ret = 0;
  45146. do {
  45147. #if defined(WOLFSSL_ASYNC_CRYPT)
  45148. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  45149. #endif
  45150. if (ret >= 0) {
  45151. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  45152. }
  45153. } while (ret == WC_PENDING_E);
  45154. ExpectIntGT(ret, 0);
  45155. #ifdef OPENSSL_EXTRA
  45156. /* der holds a certificate with DC's now check X509 parsing of it */
  45157. certSz = ret;
  45158. pt = der;
  45159. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  45160. ExpectNotNull(x509name = X509_get_subject_name(x509));
  45161. #ifdef WOLFSSL_MULTI_ATTRIB
  45162. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  45163. -1)), 5);
  45164. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  45165. (int)idx)), 6);
  45166. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  45167. (int)idx)), -1);
  45168. #endif /* WOLFSSL_MULTI_ATTRIB */
  45169. /* compare DN at index 0 */
  45170. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  45171. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  45172. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  45173. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  45174. #ifndef WOLFSSL_MULTI_ATTRIB
  45175. /* compare Serial Number */
  45176. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  45177. -1)), 7);
  45178. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  45179. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  45180. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  45181. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  45182. #endif
  45183. #ifdef WOLFSSL_MULTI_ATTRIB
  45184. /* get first and second DC and compare result */
  45185. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  45186. -1)), 5);
  45187. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  45188. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  45189. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  45190. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  45191. (int)idx)), 6);
  45192. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  45193. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  45194. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  45195. #endif /* WOLFSSL_MULTI_ATTRIB */
  45196. /* try invalid index locations for regression test and sanity check */
  45197. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  45198. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  45199. X509_free(x509);
  45200. #endif /* OPENSSL_EXTRA */
  45201. wc_FreeRsaKey(&key);
  45202. wc_FreeRng(&rng);
  45203. #endif
  45204. return EXPECT_RESULT();
  45205. }
  45206. static int test_wolfSSL_X509_get_version(void)
  45207. {
  45208. EXPECT_DECLS;
  45209. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45210. WOLFSSL_X509 *x509 = NULL;
  45211. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  45212. WOLFSSL_FILETYPE_PEM));
  45213. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  45214. wolfSSL_X509_free(x509);
  45215. #endif
  45216. return EXPECT_RESULT();
  45217. }
  45218. #if defined(OPENSSL_ALL)
  45219. static int test_wolfSSL_sk_CIPHER_description(void)
  45220. {
  45221. EXPECT_DECLS;
  45222. #if !defined(NO_RSA)
  45223. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  45224. int i;
  45225. int numCiphers = 0;
  45226. const SSL_METHOD *method = NULL;
  45227. const SSL_CIPHER *cipher = NULL;
  45228. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  45229. SSL_CTX *ctx = NULL;
  45230. SSL *ssl = NULL;
  45231. char buf[256];
  45232. char test_str[9] = "0000000";
  45233. const char badStr[] = "unknown";
  45234. const char certPath[] = "./certs/client-cert.pem";
  45235. XMEMSET(buf, 0, sizeof(buf));
  45236. ExpectNotNull(method = TLSv1_2_client_method());
  45237. ExpectNotNull(ctx = SSL_CTX_new(method));
  45238. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  45239. SSL_CTX_set_verify_depth(ctx, 4);
  45240. SSL_CTX_set_options(ctx, flags);
  45241. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  45242. WOLFSSL_SUCCESS);
  45243. ExpectNotNull(ssl = SSL_new(ctx));
  45244. /* SSL_get_ciphers returns a stack of all configured ciphers
  45245. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  45246. */
  45247. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  45248. /* loop through the amount of supportedCiphers */
  45249. numCiphers = sk_num(supportedCiphers);
  45250. for (i = 0; i < numCiphers; ++i) {
  45251. int j;
  45252. /* sk_value increments "sk->data.cipher->cipherOffset".
  45253. * wolfSSL_sk_CIPHER_description sets the description for
  45254. * the cipher based on the provided offset.
  45255. */
  45256. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  45257. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  45258. }
  45259. /* Search cipher description string for "unknown" descriptor */
  45260. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  45261. int k = 0;
  45262. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  45263. test_str[k] = badStr[k];
  45264. j++;
  45265. k++;
  45266. }
  45267. }
  45268. /* Fail if test_str == badStr == "unknown" */
  45269. ExpectStrNE(test_str,badStr);
  45270. }
  45271. SSL_free(ssl);
  45272. SSL_CTX_free(ctx);
  45273. #endif
  45274. return EXPECT_RESULT();
  45275. }
  45276. static int test_wolfSSL_get_ciphers_compat(void)
  45277. {
  45278. EXPECT_DECLS;
  45279. #if !defined(NO_RSA)
  45280. const SSL_METHOD *method = NULL;
  45281. const char certPath[] = "./certs/client-cert.pem";
  45282. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  45283. SSL_CTX *ctx = NULL;
  45284. WOLFSSL *ssl = NULL;
  45285. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  45286. ExpectNotNull(method = SSLv23_client_method());
  45287. ExpectNotNull(ctx = SSL_CTX_new(method));
  45288. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  45289. SSL_CTX_set_verify_depth(ctx, 4);
  45290. SSL_CTX_set_options(ctx, flags);
  45291. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  45292. WOLFSSL_SUCCESS);
  45293. ExpectNotNull(ssl = SSL_new(ctx));
  45294. /* Test Bad NULL input */
  45295. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  45296. /* Test for Good input */
  45297. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  45298. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  45299. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  45300. SSL_free(ssl);
  45301. SSL_CTX_free(ctx);
  45302. #endif
  45303. return EXPECT_RESULT();
  45304. }
  45305. static int test_wolfSSL_X509_PUBKEY_get(void)
  45306. {
  45307. EXPECT_DECLS;
  45308. WOLFSSL_X509_PUBKEY pubkey;
  45309. WOLFSSL_X509_PUBKEY* key;
  45310. WOLFSSL_EVP_PKEY evpkey ;
  45311. WOLFSSL_EVP_PKEY* evpPkey;
  45312. WOLFSSL_EVP_PKEY* retEvpPkey;
  45313. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  45314. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  45315. key = &pubkey;
  45316. evpPkey = &evpkey;
  45317. evpPkey->type = WOLFSSL_SUCCESS;
  45318. key->pkey = evpPkey;
  45319. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  45320. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  45321. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  45322. key->pkey = NULL;
  45323. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  45324. return EXPECT_RESULT();
  45325. }
  45326. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  45327. {
  45328. EXPECT_DECLS;
  45329. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45330. DSA *dsa = NULL;
  45331. DSA *setDsa = NULL;
  45332. EVP_PKEY *pkey = NULL;
  45333. EVP_PKEY *set1Pkey = NULL;
  45334. SHA_CTX sha;
  45335. byte signature[DSA_SIG_SIZE];
  45336. byte hash[WC_SHA_DIGEST_SIZE];
  45337. word32 bytes;
  45338. int answer;
  45339. #ifdef USE_CERT_BUFFERS_1024
  45340. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  45341. int dsaKeySz = sizeof_dsa_key_der_1024;
  45342. byte tmp[ONEK_BUF];
  45343. XMEMSET(tmp, 0, sizeof(tmp));
  45344. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  45345. bytes = dsaKeySz;
  45346. #elif defined(USE_CERT_BUFFERS_2048)
  45347. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  45348. int dsaKeySz = sizeof_dsa_key_der_2048;
  45349. byte tmp[TWOK_BUF];
  45350. XMEMSET(tmp, 0, sizeof(tmp));
  45351. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  45352. bytes = (word32)dsaKeySz;
  45353. #else
  45354. byte tmp[TWOK_BUF];
  45355. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  45356. int dsaKeySz;
  45357. XFILE fp = XBADFILE;
  45358. XMEMSET(tmp, 0, sizeof(tmp));
  45359. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  45360. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  45361. if (fp != XBADFILE)
  45362. XFCLOSE(fp);
  45363. #endif /* END USE_CERT_BUFFERS_1024 */
  45364. /* Create hash to later Sign and Verify */
  45365. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  45366. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  45367. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  45368. /* Initialize pkey with der format dsa key */
  45369. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  45370. (long)dsaKeySz));
  45371. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  45372. /* Should Fail: NULL argument */
  45373. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  45374. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  45375. /* Should Pass: Initialized pkey argument */
  45376. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  45377. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  45378. #ifdef USE_CERT_BUFFERS_1024
  45379. ExpectIntEQ(DSA_bits(dsa), 1024);
  45380. #else
  45381. ExpectIntEQ(DSA_bits(dsa), 2048);
  45382. #endif
  45383. /* Sign */
  45384. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  45385. /* Verify. */
  45386. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  45387. WOLFSSL_SUCCESS);
  45388. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  45389. /* Should Fail: set1Pkey not initialized */
  45390. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  45391. /* Initialize set1Pkey */
  45392. set1Pkey = EVP_PKEY_new();
  45393. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  45394. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  45395. WOLFSSL_SUCCESS);
  45396. /* Should Pass: set dsa into set1Pkey */
  45397. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  45398. DSA_free(dsa);
  45399. DSA_free(setDsa);
  45400. EVP_PKEY_free(pkey);
  45401. EVP_PKEY_free(set1Pkey);
  45402. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45403. return EXPECT_RESULT();
  45404. } /* END test_EVP_PKEY_set1_get1_DSA */
  45405. static int test_wolfSSL_DSA_generate_parameters(void)
  45406. {
  45407. EXPECT_DECLS;
  45408. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  45409. !defined(HAVE_FIPS)
  45410. DSA *dsa = NULL;
  45411. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  45412. NULL));
  45413. DSA_free(dsa);
  45414. #endif
  45415. return EXPECT_RESULT();
  45416. }
  45417. static int test_wolfSSL_DSA_SIG(void)
  45418. {
  45419. EXPECT_DECLS;
  45420. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  45421. !defined(HAVE_FIPS)
  45422. DSA *dsa = NULL;
  45423. DSA *dsa2 = NULL;
  45424. DSA_SIG *sig = NULL;
  45425. const BIGNUM *p = NULL;
  45426. const BIGNUM *q = NULL;
  45427. const BIGNUM *g = NULL;
  45428. const BIGNUM *pub = NULL;
  45429. const BIGNUM *priv = NULL;
  45430. BIGNUM *dup_p = NULL;
  45431. BIGNUM *dup_q = NULL;
  45432. BIGNUM *dup_g = NULL;
  45433. BIGNUM *dup_pub = NULL;
  45434. BIGNUM *dup_priv = NULL;
  45435. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  45436. ExpectNotNull(dsa = DSA_new());
  45437. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  45438. NULL), 1);
  45439. ExpectIntEQ(DSA_generate_key(dsa), 1);
  45440. DSA_get0_pqg(dsa, &p, &q, &g);
  45441. DSA_get0_key(dsa, &pub, &priv);
  45442. ExpectNotNull(dup_p = BN_dup(p));
  45443. ExpectNotNull(dup_q = BN_dup(q));
  45444. ExpectNotNull(dup_g = BN_dup(g));
  45445. ExpectNotNull(dup_pub = BN_dup(pub));
  45446. ExpectNotNull(dup_priv = BN_dup(priv));
  45447. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  45448. ExpectNotNull(dsa2 = DSA_new());
  45449. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  45450. if (EXPECT_FAIL()) {
  45451. BN_free(dup_p);
  45452. BN_free(dup_q);
  45453. BN_free(dup_g);
  45454. }
  45455. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  45456. if (EXPECT_FAIL()) {
  45457. BN_free(dup_pub);
  45458. BN_free(dup_priv);
  45459. }
  45460. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  45461. DSA_free(dsa);
  45462. DSA_free(dsa2);
  45463. DSA_SIG_free(sig);
  45464. #endif
  45465. return EXPECT_RESULT();
  45466. }
  45467. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  45468. {
  45469. EXPECT_DECLS;
  45470. #ifdef HAVE_ECC
  45471. WOLFSSL_EC_KEY* ecKey = NULL;
  45472. WOLFSSL_EC_KEY* ecGet1 = NULL;
  45473. EVP_PKEY* pkey = NULL;
  45474. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  45475. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45476. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  45477. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  45478. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  45479. /* Should fail since ecKey is empty */
  45480. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  45481. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  45482. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  45483. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  45484. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  45485. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  45486. wolfSSL_EC_KEY_free(ecKey);
  45487. wolfSSL_EC_KEY_free(ecGet1);
  45488. EVP_PKEY_free(pkey);
  45489. #endif /* HAVE_ECC */
  45490. return EXPECT_RESULT();
  45491. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  45492. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  45493. {
  45494. EXPECT_DECLS;
  45495. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  45496. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45497. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  45498. DH *dh = NULL;
  45499. DH *setDh = NULL;
  45500. EVP_PKEY *pkey = NULL;
  45501. XFILE f = XBADFILE;
  45502. unsigned char buf[4096];
  45503. const unsigned char* pt = buf;
  45504. const char* dh2048 = "./certs/dh2048.der";
  45505. long len = 0;
  45506. int code = -1;
  45507. XMEMSET(buf, 0, sizeof(buf));
  45508. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  45509. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  45510. if (f != XBADFILE)
  45511. XFCLOSE(f);
  45512. /* Load dh2048.der into DH with internal format */
  45513. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  45514. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  45515. ExpectIntEQ(code, 0);
  45516. code = -1;
  45517. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45518. /* Set DH into PKEY */
  45519. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  45520. /* Get DH from PKEY */
  45521. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  45522. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  45523. ExpectIntEQ(code, 0);
  45524. EVP_PKEY_free(pkey);
  45525. DH_free(setDh);
  45526. setDh = NULL;
  45527. DH_free(dh);
  45528. dh = NULL;
  45529. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  45530. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  45531. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  45532. return EXPECT_RESULT();
  45533. } /* END test_EVP_PKEY_set1_get1_DH */
  45534. static int test_wolfSSL_CTX_ctrl(void)
  45535. {
  45536. EXPECT_DECLS;
  45537. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  45538. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45539. char caFile[] = "./certs/client-ca.pem";
  45540. char clientFile[] = "./certs/client-cert.pem";
  45541. SSL_CTX* ctx = NULL;
  45542. X509* x509 = NULL;
  45543. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  45544. byte buf[6000];
  45545. char file[] = "./certs/dsaparams.pem";
  45546. XFILE f = XBADFILE;
  45547. int bytes = 0;
  45548. BIO* bio = NULL;
  45549. DSA* dsa = NULL;
  45550. DH* dh = NULL;
  45551. #endif
  45552. #ifdef HAVE_ECC
  45553. WOLFSSL_EC_KEY* ecKey = NULL;
  45554. #endif
  45555. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  45556. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  45557. WOLFSSL_FILETYPE_PEM));
  45558. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  45559. if (EXPECT_FAIL()) {
  45560. wolfSSL_X509_free(x509);
  45561. }
  45562. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  45563. WOLFSSL_FILETYPE_PEM));
  45564. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  45565. /* Initialize DH */
  45566. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  45567. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  45568. if (f != XBADFILE)
  45569. XFCLOSE(f);
  45570. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  45571. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  45572. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  45573. #endif
  45574. #ifdef HAVE_ECC
  45575. /* Initialize WOLFSSL_EC_KEY */
  45576. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  45577. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  45578. #endif
  45579. /* additional test of getting EVP_PKEY key size from X509
  45580. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  45581. * allowed with user RSA */
  45582. {
  45583. EVP_PKEY* pkey = NULL;
  45584. #if defined(HAVE_ECC)
  45585. X509* ecX509 = NULL;
  45586. #endif /* HAVE_ECC */
  45587. ExpectNotNull(pkey = X509_get_pubkey(x509));
  45588. /* current RSA key is 2048 bit (256 bytes) */
  45589. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  45590. EVP_PKEY_free(pkey);
  45591. pkey = NULL;
  45592. #if defined(HAVE_ECC)
  45593. #if defined(USE_CERT_BUFFERS_256)
  45594. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  45595. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  45596. SSL_FILETYPE_ASN1));
  45597. #else
  45598. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  45599. cliEccCertFile, SSL_FILETYPE_PEM));
  45600. #endif
  45601. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  45602. /* current ECC key is 256 bit (32 bytes) */
  45603. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  45604. X509_free(ecX509);
  45605. EVP_PKEY_free(pkey);
  45606. #endif /* HAVE_ECC */
  45607. }
  45608. /* Tests should fail with passed in NULL pointer */
  45609. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  45610. SSL_FAILURE);
  45611. #if !defined(NO_DH) && !defined(NO_DSA)
  45612. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  45613. SSL_FAILURE);
  45614. #endif
  45615. #ifdef HAVE_ECC
  45616. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  45617. SSL_FAILURE);
  45618. #endif
  45619. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  45620. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  45621. */
  45622. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  45623. SSL_SUCCESS);
  45624. if (EXPECT_FAIL()) {
  45625. wolfSSL_X509_free(x509);
  45626. }
  45627. /* Test with SSL_CTRL_OPTIONS
  45628. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  45629. */
  45630. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  45631. NULL) == SSL_OP_NO_TLSv1);
  45632. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  45633. /* Test with SSL_CTRL_SET_TMP_DH
  45634. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  45635. */
  45636. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  45637. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  45638. SSL_SUCCESS);
  45639. #endif
  45640. /* Test with SSL_CTRL_SET_TMP_ECDH
  45641. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  45642. */
  45643. #ifdef HAVE_ECC
  45644. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  45645. SSL_SUCCESS);
  45646. #endif
  45647. #ifdef WOLFSSL_ENCRYPTED_KEYS
  45648. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  45649. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  45650. #endif
  45651. /* Test for min/max proto */
  45652. #ifndef WOLFSSL_NO_TLS12
  45653. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  45654. 0, NULL), SSL_SUCCESS);
  45655. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  45656. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  45657. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  45658. #endif
  45659. #ifdef WOLFSSL_TLS13
  45660. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  45661. 0, NULL), SSL_SUCCESS);
  45662. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  45663. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  45664. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  45665. #ifndef WOLFSSL_NO_TLS12
  45666. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  45667. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  45668. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  45669. #endif
  45670. #endif
  45671. /* Cleanup and Pass */
  45672. #if !defined(NO_DH) && !defined(NO_DSA)
  45673. #ifndef NO_BIO
  45674. BIO_free(bio);
  45675. DSA_free(dsa);
  45676. DH_free(dh);
  45677. dh = NULL;
  45678. #endif
  45679. #endif
  45680. #ifdef HAVE_ECC
  45681. wolfSSL_EC_KEY_free(ecKey);
  45682. #endif
  45683. SSL_CTX_free(ctx);
  45684. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  45685. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  45686. return EXPECT_RESULT();
  45687. }
  45688. static int test_wolfSSL_EVP_PKEY_assign(void)
  45689. {
  45690. EXPECT_DECLS;
  45691. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  45692. int type;
  45693. WOLFSSL_EVP_PKEY* pkey = NULL;
  45694. #ifndef NO_RSA
  45695. WOLFSSL_RSA* rsa = NULL;
  45696. #endif
  45697. #ifndef NO_DSA
  45698. WOLFSSL_DSA* dsa = NULL;
  45699. #endif
  45700. #ifdef HAVE_ECC
  45701. WOLFSSL_EC_KEY* ecKey = NULL;
  45702. #endif
  45703. #ifndef NO_RSA
  45704. type = EVP_PKEY_RSA;
  45705. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45706. ExpectNotNull(rsa = wolfSSL_RSA_new());
  45707. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  45708. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  45709. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  45710. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  45711. if (EXPECT_FAIL()) {
  45712. wolfSSL_RSA_free(rsa);
  45713. }
  45714. wolfSSL_EVP_PKEY_free(pkey);
  45715. pkey = NULL;
  45716. #endif /* NO_RSA */
  45717. #ifndef NO_DSA
  45718. type = EVP_PKEY_DSA;
  45719. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45720. ExpectNotNull(dsa = wolfSSL_DSA_new());
  45721. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  45722. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  45723. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  45724. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  45725. if (EXPECT_FAIL()) {
  45726. wolfSSL_DSA_free(dsa);
  45727. }
  45728. wolfSSL_EVP_PKEY_free(pkey);
  45729. pkey = NULL;
  45730. #endif /* NO_DSA */
  45731. #ifdef HAVE_ECC
  45732. type = EVP_PKEY_EC;
  45733. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45734. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  45735. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  45736. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  45737. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  45738. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  45739. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  45740. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  45741. if (EXPECT_FAIL()) {
  45742. wolfSSL_EC_KEY_free(ecKey);
  45743. }
  45744. wolfSSL_EVP_PKEY_free(pkey);
  45745. pkey = NULL;
  45746. #endif /* HAVE_ECC */
  45747. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  45748. return EXPECT_RESULT();
  45749. }
  45750. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  45751. {
  45752. EXPECT_DECLS;
  45753. #if !defined(NO_DH) && \
  45754. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  45755. XFILE f = XBADFILE;
  45756. unsigned char buf[4096];
  45757. const unsigned char* pt = buf;
  45758. const char* params1 = "./certs/dh2048.der";
  45759. long len = 0;
  45760. WOLFSSL_DH* dh = NULL;
  45761. WOLFSSL_EVP_PKEY* pkey = NULL;
  45762. XMEMSET(buf, 0, sizeof(buf));
  45763. /* Load DH parameters DER. */
  45764. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  45765. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  45766. if (f != XBADFILE)
  45767. XFCLOSE(f);
  45768. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  45769. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  45770. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45771. /* Bad cases */
  45772. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  45773. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  45774. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  45775. /* Good case */
  45776. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  45777. if (EXPECT_FAIL()) {
  45778. wolfSSL_DH_free(dh);
  45779. }
  45780. EVP_PKEY_free(pkey);
  45781. #endif
  45782. return EXPECT_RESULT();
  45783. }
  45784. static int test_wolfSSL_EVP_PKEY_base_id(void)
  45785. {
  45786. EXPECT_DECLS;
  45787. WOLFSSL_EVP_PKEY* pkey = NULL;
  45788. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45789. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  45790. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  45791. EVP_PKEY_free(pkey);
  45792. return EXPECT_RESULT();
  45793. }
  45794. static int test_wolfSSL_EVP_PKEY_id(void)
  45795. {
  45796. EXPECT_DECLS;
  45797. WOLFSSL_EVP_PKEY* pkey = NULL;
  45798. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45799. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  45800. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  45801. EVP_PKEY_free(pkey);
  45802. return EXPECT_RESULT();
  45803. }
  45804. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  45805. {
  45806. EXPECT_DECLS;
  45807. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  45808. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  45809. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  45810. ECC_MIN_KEY_SZ <= 256)
  45811. EVP_PKEY_CTX* ctx = NULL;
  45812. EVP_PKEY* pkey = NULL;
  45813. /* Test error conditions. */
  45814. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  45815. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  45816. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  45817. #ifndef NO_RSA
  45818. EVP_PKEY_CTX_free(ctx);
  45819. /* Parameter generation for RSA not supported yet. */
  45820. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  45821. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  45822. #endif
  45823. #ifdef HAVE_ECC
  45824. EVP_PKEY_CTX_free(ctx);
  45825. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  45826. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  45827. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  45828. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  45829. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  45830. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  45831. WOLFSSL_SUCCESS);
  45832. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  45833. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  45834. #endif
  45835. EVP_PKEY_CTX_free(ctx);
  45836. EVP_PKEY_free(pkey);
  45837. #endif
  45838. return EXPECT_RESULT();
  45839. }
  45840. static int test_wolfSSL_EVP_PKEY_keygen(void)
  45841. {
  45842. EXPECT_DECLS;
  45843. WOLFSSL_EVP_PKEY* pkey = NULL;
  45844. EVP_PKEY_CTX* ctx = NULL;
  45845. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  45846. WOLFSSL_EVP_PKEY* params = NULL;
  45847. DH* dh = NULL;
  45848. const BIGNUM* pubkey = NULL;
  45849. const BIGNUM* privkey = NULL;
  45850. ASN1_INTEGER* asn1int = NULL;
  45851. unsigned int length = 0;
  45852. byte* derBuffer = NULL;
  45853. #endif
  45854. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45855. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45856. /* Bad cases */
  45857. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  45858. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  45859. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  45860. /* Good case */
  45861. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  45862. EVP_PKEY_CTX_free(ctx);
  45863. ctx = NULL;
  45864. EVP_PKEY_free(pkey);
  45865. pkey = NULL;
  45866. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  45867. /* Test DH keygen */
  45868. {
  45869. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  45870. ExpectNotNull(dh = DH_get_2048_256());
  45871. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  45872. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  45873. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  45874. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  45875. DH_free(dh);
  45876. dh = NULL;
  45877. EVP_PKEY_CTX_free(ctx);
  45878. EVP_PKEY_free(params);
  45879. /* try exporting generated key to DER, to verify */
  45880. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  45881. DH_get0_key(dh, &pubkey, &privkey);
  45882. ExpectNotNull(pubkey);
  45883. ExpectNotNull(privkey);
  45884. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  45885. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  45886. ASN1_INTEGER_free(asn1int);
  45887. DH_free(dh);
  45888. dh = NULL;
  45889. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45890. EVP_PKEY_free(pkey);
  45891. }
  45892. #endif
  45893. return EXPECT_RESULT();
  45894. }
  45895. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  45896. {
  45897. EXPECT_DECLS;
  45898. WOLFSSL_EVP_PKEY* pkey = NULL;
  45899. EVP_PKEY_CTX *ctx = NULL;
  45900. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45901. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45902. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  45903. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  45904. EVP_PKEY_CTX_free(ctx);
  45905. EVP_PKEY_free(pkey);
  45906. return EXPECT_RESULT();
  45907. }
  45908. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  45909. {
  45910. EXPECT_DECLS;
  45911. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  45912. WOLFSSL_EVP_PKEY* pkey = NULL;
  45913. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45914. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  45915. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  45916. EVP_PKEY_free(pkey);
  45917. #endif
  45918. return EXPECT_RESULT();
  45919. }
  45920. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  45921. {
  45922. EXPECT_DECLS;
  45923. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  45924. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  45925. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  45926. !defined(NO_FILESYSTEM)
  45927. WOLFSSL_EVP_PKEY* params = NULL;
  45928. WOLFSSL_EVP_PKEY* copy = NULL;
  45929. DH* dh = NULL;
  45930. BIGNUM* p1;
  45931. BIGNUM* g1;
  45932. BIGNUM* q1;
  45933. BIGNUM* p2;
  45934. BIGNUM* g2;
  45935. BIGNUM* q2;
  45936. /* create DH with DH_get_2048_256 params */
  45937. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  45938. ExpectNotNull(dh = DH_get_2048_256());
  45939. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  45940. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  45941. (const BIGNUM**)&q1,
  45942. (const BIGNUM**)&g1);
  45943. DH_free(dh);
  45944. dh = NULL;
  45945. /* create DH with random generated DH params */
  45946. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  45947. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  45948. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  45949. DH_free(dh);
  45950. dh = NULL;
  45951. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  45952. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  45953. ExpectNotNull(dh->p);
  45954. ExpectNotNull(dh->g);
  45955. ExpectNotNull(dh->q);
  45956. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  45957. (const BIGNUM**)&q2,
  45958. (const BIGNUM**)&g2);
  45959. ExpectIntEQ(BN_cmp(p1, p2), 0);
  45960. ExpectIntEQ(BN_cmp(q1, q2), 0);
  45961. ExpectIntEQ(BN_cmp(g1, g2), 0);
  45962. DH_free(dh);
  45963. dh = NULL;
  45964. EVP_PKEY_free(copy);
  45965. EVP_PKEY_free(params);
  45966. #endif
  45967. return EXPECT_RESULT();
  45968. }
  45969. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  45970. {
  45971. EXPECT_DECLS;
  45972. WOLFSSL_EVP_PKEY* pkey = NULL;
  45973. EVP_PKEY_CTX* ctx = NULL;
  45974. int bits = 2048;
  45975. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45976. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45977. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  45978. WOLFSSL_SUCCESS);
  45979. EVP_PKEY_CTX_free(ctx);
  45980. EVP_PKEY_free(pkey);
  45981. return EXPECT_RESULT();
  45982. }
  45983. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  45984. {
  45985. EXPECT_DECLS;
  45986. /* This is large enough to be used for all key sizes */
  45987. byte key[AES_256_KEY_SIZE] = {0};
  45988. byte iv[AES_BLOCK_SIZE] = {0};
  45989. int i;
  45990. int nids[] = {
  45991. #ifdef HAVE_AES_CBC
  45992. NID_aes_128_cbc,
  45993. #endif
  45994. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  45995. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  45996. #ifdef HAVE_AESGCM
  45997. NID_aes_128_gcm,
  45998. #endif
  45999. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  46000. #ifdef WOLFSSL_AES_COUNTER
  46001. NID_aes_128_ctr,
  46002. #endif
  46003. #ifndef NO_DES3
  46004. NID_des_cbc,
  46005. NID_des_ede3_cbc,
  46006. #endif
  46007. };
  46008. int iv_lengths[] = {
  46009. #ifdef HAVE_AES_CBC
  46010. AES_BLOCK_SIZE,
  46011. #endif
  46012. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  46013. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  46014. #ifdef HAVE_AESGCM
  46015. GCM_NONCE_MID_SZ,
  46016. #endif
  46017. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  46018. #ifdef WOLFSSL_AES_COUNTER
  46019. AES_BLOCK_SIZE,
  46020. #endif
  46021. #ifndef NO_DES3
  46022. DES_BLOCK_SIZE,
  46023. DES_BLOCK_SIZE,
  46024. #endif
  46025. };
  46026. int nidsLen = (sizeof(nids)/sizeof(int));
  46027. for (i = 0; i < nidsLen; i++) {
  46028. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  46029. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  46030. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46031. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46032. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  46033. EVP_CIPHER_CTX_free(ctx);
  46034. }
  46035. return EXPECT_RESULT();
  46036. }
  46037. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  46038. {
  46039. EXPECT_DECLS;
  46040. byte key[AES_256_KEY_SIZE] = {0};
  46041. byte iv[AES_BLOCK_SIZE] = {0};
  46042. int i;
  46043. int nids[] = {
  46044. #ifdef HAVE_AES_CBC
  46045. NID_aes_128_cbc,
  46046. NID_aes_256_cbc,
  46047. #endif
  46048. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  46049. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  46050. #ifdef HAVE_AESGCM
  46051. NID_aes_128_gcm,
  46052. NID_aes_256_gcm,
  46053. #endif
  46054. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  46055. #ifdef WOLFSSL_AES_COUNTER
  46056. NID_aes_128_ctr,
  46057. NID_aes_256_ctr,
  46058. #endif
  46059. #ifndef NO_DES3
  46060. NID_des_cbc,
  46061. NID_des_ede3_cbc,
  46062. #endif
  46063. };
  46064. int key_lengths[] = {
  46065. #ifdef HAVE_AES_CBC
  46066. AES_128_KEY_SIZE,
  46067. AES_256_KEY_SIZE,
  46068. #endif
  46069. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  46070. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  46071. #ifdef HAVE_AESGCM
  46072. AES_128_KEY_SIZE,
  46073. AES_256_KEY_SIZE,
  46074. #endif
  46075. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  46076. #ifdef WOLFSSL_AES_COUNTER
  46077. AES_128_KEY_SIZE,
  46078. AES_256_KEY_SIZE,
  46079. #endif
  46080. #ifndef NO_DES3
  46081. DES_KEY_SIZE,
  46082. DES3_KEY_SIZE,
  46083. #endif
  46084. };
  46085. int nidsLen = (sizeof(nids)/sizeof(int));
  46086. for (i = 0; i < nidsLen; i++) {
  46087. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  46088. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  46089. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46090. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46091. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  46092. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  46093. WOLFSSL_SUCCESS);
  46094. EVP_CIPHER_CTX_free(ctx);
  46095. }
  46096. return EXPECT_RESULT();
  46097. }
  46098. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  46099. {
  46100. EXPECT_DECLS;
  46101. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  46102. int ivLen, keyLen;
  46103. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  46104. #ifdef HAVE_AESGCM
  46105. byte key[AES_128_KEY_SIZE] = {0};
  46106. byte iv[AES_BLOCK_SIZE] = {0};
  46107. const EVP_CIPHER *init = EVP_aes_128_gcm();
  46108. #else
  46109. byte key[DES3_KEY_SIZE] = {0};
  46110. byte iv[DES_BLOCK_SIZE] = {0};
  46111. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  46112. #endif
  46113. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46114. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46115. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  46116. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  46117. /* Bad cases */
  46118. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  46119. WOLFSSL_FAILURE);
  46120. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  46121. WOLFSSL_FAILURE);
  46122. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  46123. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  46124. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  46125. WOLFSSL_FAILURE);
  46126. /* Good case */
  46127. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  46128. EVP_CIPHER_CTX_free(ctx);
  46129. #endif
  46130. return EXPECT_RESULT();
  46131. }
  46132. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  46133. {
  46134. EXPECT_DECLS;
  46135. WOLFSSL_ENGINE* e = NULL;
  46136. int id = 0;
  46137. EVP_PKEY_CTX *ctx = NULL;
  46138. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  46139. EVP_PKEY_CTX_free(ctx);
  46140. return EXPECT_RESULT();
  46141. }
  46142. static int test_wolfSSL_EVP_rc4(void)
  46143. {
  46144. EXPECT_DECLS;
  46145. #if !defined(NO_RC4)
  46146. ExpectNotNull(wolfSSL_EVP_rc4());
  46147. #endif
  46148. return EXPECT_RESULT();
  46149. }
  46150. static int test_wolfSSL_EVP_enc_null(void)
  46151. {
  46152. EXPECT_DECLS;
  46153. ExpectNotNull(wolfSSL_EVP_enc_null());
  46154. return EXPECT_RESULT();
  46155. }
  46156. static int test_wolfSSL_EVP_rc2_cbc(void)
  46157. {
  46158. EXPECT_DECLS;
  46159. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  46160. ExpectNull(wolfSSL_EVP_rc2_cbc());
  46161. #endif
  46162. return EXPECT_RESULT();
  46163. }
  46164. static int test_wolfSSL_EVP_mdc2(void)
  46165. {
  46166. EXPECT_DECLS;
  46167. #if !defined(NO_WOLFSSL_STUB)
  46168. ExpectNull(wolfSSL_EVP_mdc2());
  46169. #endif
  46170. return EXPECT_RESULT();
  46171. }
  46172. static int test_wolfSSL_EVP_md4(void)
  46173. {
  46174. EXPECT_DECLS;
  46175. #if !defined(NO_MD4)
  46176. ExpectNotNull(wolfSSL_EVP_md4());
  46177. #endif
  46178. return EXPECT_RESULT();
  46179. }
  46180. static int test_wolfSSL_EVP_aes_256_gcm(void)
  46181. {
  46182. EXPECT_DECLS;
  46183. #ifdef HAVE_AESGCM
  46184. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  46185. #endif
  46186. return EXPECT_RESULT();
  46187. }
  46188. static int test_wolfSSL_EVP_aes_192_gcm(void)
  46189. {
  46190. EXPECT_DECLS;
  46191. #ifdef HAVE_AESGCM
  46192. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  46193. #endif
  46194. return EXPECT_RESULT();
  46195. }
  46196. static int test_wolfSSL_EVP_aes_256_ccm(void)
  46197. {
  46198. EXPECT_DECLS;
  46199. #ifdef HAVE_AESCCM
  46200. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  46201. #endif
  46202. return EXPECT_RESULT();
  46203. }
  46204. static int test_wolfSSL_EVP_aes_192_ccm(void)
  46205. {
  46206. EXPECT_DECLS;
  46207. #ifdef HAVE_AESCCM
  46208. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  46209. #endif
  46210. return EXPECT_RESULT();
  46211. }
  46212. static int test_wolfSSL_EVP_aes_128_ccm(void)
  46213. {
  46214. EXPECT_DECLS;
  46215. #ifdef HAVE_AESCCM
  46216. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  46217. #endif
  46218. return EXPECT_RESULT();
  46219. }
  46220. static int test_wolfSSL_EVP_ripemd160(void)
  46221. {
  46222. EXPECT_DECLS;
  46223. #if !defined(NO_WOLFSSL_STUB)
  46224. ExpectNull(wolfSSL_EVP_ripemd160());
  46225. #endif
  46226. return EXPECT_RESULT();
  46227. }
  46228. static int test_wolfSSL_EVP_get_digestbynid(void)
  46229. {
  46230. EXPECT_DECLS;
  46231. #ifndef NO_MD5
  46232. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  46233. #endif
  46234. #ifndef NO_SHA
  46235. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  46236. #endif
  46237. #ifndef NO_SHA256
  46238. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  46239. #endif
  46240. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  46241. return EXPECT_RESULT();
  46242. }
  46243. static int test_wolfSSL_EVP_MD_nid(void)
  46244. {
  46245. EXPECT_DECLS;
  46246. #ifndef NO_MD5
  46247. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  46248. #endif
  46249. #ifndef NO_SHA
  46250. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  46251. #endif
  46252. #ifndef NO_SHA256
  46253. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  46254. #endif
  46255. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  46256. return EXPECT_RESULT();
  46257. }
  46258. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  46259. {
  46260. EXPECT_DECLS;
  46261. #if defined(HAVE_ECC)
  46262. WOLFSSL_EVP_PKEY* pkey = NULL;
  46263. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  46264. ExpectNotNull(pkey = EVP_PKEY_new());
  46265. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  46266. EVP_PKEY_free(pkey);
  46267. #endif
  46268. return EXPECT_RESULT();
  46269. }
  46270. static int test_wolfSSL_EVP_X_STATE(void)
  46271. {
  46272. EXPECT_DECLS;
  46273. #if !defined(NO_DES3) && !defined(NO_RC4)
  46274. byte key[DES3_KEY_SIZE] = {0};
  46275. byte iv[DES_IV_SIZE] = {0};
  46276. EVP_CIPHER_CTX *ctx = NULL;
  46277. const EVP_CIPHER *init = NULL;
  46278. /* Bad test cases */
  46279. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46280. ExpectNotNull(init = EVP_des_ede3_cbc());
  46281. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46282. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46283. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  46284. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  46285. EVP_CIPHER_CTX_free(ctx);
  46286. ctx = NULL;
  46287. /* Good test case */
  46288. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46289. ExpectNotNull(init = wolfSSL_EVP_rc4());
  46290. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46291. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46292. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  46293. EVP_CIPHER_CTX_free(ctx);
  46294. #endif
  46295. return EXPECT_RESULT();
  46296. }
  46297. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  46298. {
  46299. EXPECT_DECLS;
  46300. #if !defined(NO_DES3) && !defined(NO_RC4)
  46301. byte key[DES3_KEY_SIZE] = {0};
  46302. byte iv[DES_IV_SIZE] = {0};
  46303. EVP_CIPHER_CTX *ctx = NULL;
  46304. const EVP_CIPHER *init = NULL;
  46305. /* Bad test cases */
  46306. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46307. ExpectNotNull(init = EVP_des_ede3_cbc());
  46308. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46309. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46310. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  46311. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  46312. EVP_CIPHER_CTX_free(ctx);
  46313. ctx = NULL;
  46314. /* Good test case */
  46315. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46316. ExpectNotNull(init = wolfSSL_EVP_rc4());
  46317. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46318. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46319. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  46320. EVP_CIPHER_CTX_free(ctx);
  46321. #endif
  46322. return EXPECT_RESULT();
  46323. }
  46324. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  46325. {
  46326. EXPECT_DECLS;
  46327. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  46328. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  46329. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  46330. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  46331. #ifdef HAVE_AES_CBC
  46332. #ifdef WOLFSSL_AES_128
  46333. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  46334. #endif
  46335. #ifdef WOLFSSL_AES_192
  46336. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  46337. #endif
  46338. #ifdef WOLFSSL_AES_256
  46339. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  46340. #endif
  46341. #endif
  46342. #ifdef HAVE_AESGCM
  46343. #ifdef WOLFSSL_AES_128
  46344. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  46345. #endif
  46346. #ifdef WOLFSSL_AES_192
  46347. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  46348. #endif
  46349. #ifdef WOLFSSL_AES_256
  46350. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  46351. #endif
  46352. #endif
  46353. #ifdef HAVE_AESCCM
  46354. #ifdef WOLFSSL_AES_128
  46355. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  46356. #endif
  46357. #ifdef WOLFSSL_AES_192
  46358. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  46359. #endif
  46360. #ifdef WOLFSSL_AES_256
  46361. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  46362. #endif
  46363. #endif
  46364. #ifdef WOLFSSL_AES_COUNTER
  46365. #ifdef WOLFSSL_AES_128
  46366. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  46367. #endif
  46368. #ifdef WOLFSSL_AES_192
  46369. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  46370. #endif
  46371. #ifdef WOLFSSL_AES_256
  46372. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  46373. #endif
  46374. #endif
  46375. #ifdef HAVE_AES_ECB
  46376. #ifdef WOLFSSL_AES_128
  46377. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  46378. #endif
  46379. #ifdef WOLFSSL_AES_192
  46380. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  46381. #endif
  46382. #ifdef WOLFSSL_AES_256
  46383. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  46384. #endif
  46385. #endif
  46386. #ifdef WOLFSSL_AES_OFB
  46387. #ifdef WOLFSSL_AES_128
  46388. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  46389. #endif
  46390. #ifdef WOLFSSL_AES_192
  46391. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  46392. #endif
  46393. #ifdef WOLFSSL_AES_256
  46394. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  46395. #endif
  46396. #endif
  46397. #ifndef NO_RC4
  46398. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  46399. #endif
  46400. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  46401. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  46402. #endif
  46403. #endif
  46404. #ifdef WOLFSSL_SM4_ECB
  46405. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  46406. #endif
  46407. #ifdef WOLFSSL_SM4_CBC
  46408. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  46409. #endif
  46410. #ifdef WOLFSSL_SM4_CTR
  46411. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  46412. #endif
  46413. #ifdef WOLFSSL_SM4_GCM
  46414. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  46415. #endif
  46416. #ifdef WOLFSSL_SM4_CCM
  46417. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  46418. #endif
  46419. return EXPECT_RESULT();
  46420. }
  46421. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  46422. {
  46423. EXPECT_DECLS;
  46424. int nids[] = {
  46425. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  46426. #ifdef WOLFSSL_AES_128
  46427. NID_aes_128_cbc,
  46428. #endif
  46429. #ifdef WOLFSSL_AES_192
  46430. NID_aes_192_cbc,
  46431. #endif
  46432. #ifdef WOLFSSL_AES_256
  46433. NID_aes_256_cbc,
  46434. #endif
  46435. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  46436. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  46437. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  46438. #ifdef HAVE_AESGCM
  46439. #ifdef WOLFSSL_AES_128
  46440. NID_aes_128_gcm,
  46441. #endif
  46442. #ifdef WOLFSSL_AES_192
  46443. NID_aes_192_gcm,
  46444. #endif
  46445. #ifdef WOLFSSL_AES_256
  46446. NID_aes_256_gcm,
  46447. #endif
  46448. #endif /* HAVE_AESGCM */
  46449. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  46450. #ifdef WOLFSSL_AES_COUNTER
  46451. #ifdef WOLFSSL_AES_128
  46452. NID_aes_128_ctr,
  46453. #endif
  46454. #ifdef WOLFSSL_AES_192
  46455. NID_aes_192_ctr,
  46456. #endif
  46457. #ifdef WOLFSSL_AES_256
  46458. NID_aes_256_ctr,
  46459. #endif
  46460. #endif
  46461. #ifndef NO_DES3
  46462. NID_des_cbc,
  46463. NID_des_ede3_cbc,
  46464. #endif
  46465. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  46466. NID_chacha20_poly1305,
  46467. #endif
  46468. };
  46469. int iv_lengths[] = {
  46470. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  46471. #ifdef WOLFSSL_AES_128
  46472. AES_BLOCK_SIZE,
  46473. #endif
  46474. #ifdef WOLFSSL_AES_192
  46475. AES_BLOCK_SIZE,
  46476. #endif
  46477. #ifdef WOLFSSL_AES_256
  46478. AES_BLOCK_SIZE,
  46479. #endif
  46480. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  46481. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  46482. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  46483. #ifdef HAVE_AESGCM
  46484. #ifdef WOLFSSL_AES_128
  46485. GCM_NONCE_MID_SZ,
  46486. #endif
  46487. #ifdef WOLFSSL_AES_192
  46488. GCM_NONCE_MID_SZ,
  46489. #endif
  46490. #ifdef WOLFSSL_AES_256
  46491. GCM_NONCE_MID_SZ,
  46492. #endif
  46493. #endif /* HAVE_AESGCM */
  46494. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  46495. #ifdef WOLFSSL_AES_COUNTER
  46496. #ifdef WOLFSSL_AES_128
  46497. AES_BLOCK_SIZE,
  46498. #endif
  46499. #ifdef WOLFSSL_AES_192
  46500. AES_BLOCK_SIZE,
  46501. #endif
  46502. #ifdef WOLFSSL_AES_256
  46503. AES_BLOCK_SIZE,
  46504. #endif
  46505. #endif
  46506. #ifndef NO_DES3
  46507. DES_BLOCK_SIZE,
  46508. DES_BLOCK_SIZE,
  46509. #endif
  46510. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  46511. CHACHA20_POLY1305_AEAD_IV_SIZE,
  46512. #endif
  46513. };
  46514. int i;
  46515. int nidsLen = (sizeof(nids)/sizeof(int));
  46516. for (i = 0; i < nidsLen; i++) {
  46517. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  46518. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  46519. }
  46520. return EXPECT_RESULT();
  46521. }
  46522. static int test_wolfSSL_EVP_SignInit_ex(void)
  46523. {
  46524. EXPECT_DECLS;
  46525. WOLFSSL_EVP_MD_CTX mdCtx;
  46526. WOLFSSL_ENGINE* e = 0;
  46527. const EVP_MD* md = EVP_sha256();
  46528. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  46529. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  46530. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  46531. return EXPECT_RESULT();
  46532. }
  46533. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  46534. {
  46535. EXPECT_DECLS;
  46536. #if !defined(NO_SHA256)
  46537. WOLFSSL_EVP_MD_CTX mdCtx;
  46538. unsigned int s = 0;
  46539. unsigned char md[WC_SHA256_DIGEST_SIZE];
  46540. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  46541. /* Bad Case */
  46542. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  46543. (HAVE_FIPS_VERSION > 2))
  46544. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  46545. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  46546. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  46547. #else
  46548. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  46549. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  46550. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  46551. #endif
  46552. /* Good Case */
  46553. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  46554. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  46555. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  46556. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  46557. #endif
  46558. return EXPECT_RESULT();
  46559. }
  46560. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  46561. {
  46562. EXPECT_DECLS;
  46563. #if defined(OPENSSL_EXTRA)
  46564. EVP_PKEY* pkey = NULL;
  46565. EVP_PKEY_CTX* ctx = NULL;
  46566. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46567. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46568. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  46569. /* void */
  46570. EVP_PKEY_CTX_free(ctx);
  46571. #else
  46572. /* int */
  46573. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  46574. #endif
  46575. EVP_PKEY_free(pkey);
  46576. #endif
  46577. return EXPECT_RESULT();
  46578. }
  46579. static int test_wolfSSL_EVP_PKEY_param_check(void)
  46580. {
  46581. EXPECT_DECLS;
  46582. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  46583. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  46584. DH *dh = NULL;
  46585. DH *setDh = NULL;
  46586. EVP_PKEY *pkey = NULL;
  46587. EVP_PKEY_CTX* ctx = NULL;
  46588. FILE* f = NULL;
  46589. unsigned char buf[512];
  46590. const unsigned char* pt = buf;
  46591. const char* dh2048 = "./certs/dh2048.der";
  46592. long len = 0;
  46593. int code = -1;
  46594. XMEMSET(buf, 0, sizeof(buf));
  46595. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  46596. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  46597. if (f != XBADFILE)
  46598. XFCLOSE(f);
  46599. /* Load dh2048.der into DH with internal format */
  46600. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  46601. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  46602. ExpectIntEQ(code, 0);
  46603. code = -1;
  46604. pkey = wolfSSL_EVP_PKEY_new();
  46605. /* Set DH into PKEY */
  46606. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  46607. /* create ctx from pkey */
  46608. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46609. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  46610. /* TODO: more invalid cases */
  46611. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  46612. EVP_PKEY_CTX_free(ctx);
  46613. EVP_PKEY_free(pkey);
  46614. DH_free(setDh);
  46615. setDh = NULL;
  46616. DH_free(dh);
  46617. dh = NULL;
  46618. #endif
  46619. #endif
  46620. return EXPECT_RESULT();
  46621. }
  46622. static int test_wolfSSL_EVP_BytesToKey(void)
  46623. {
  46624. EXPECT_DECLS;
  46625. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  46626. byte key[AES_BLOCK_SIZE] = {0};
  46627. byte iv[AES_BLOCK_SIZE] = {0};
  46628. int count = 0;
  46629. const EVP_MD* md = EVP_sha256();
  46630. const EVP_CIPHER *type;
  46631. const unsigned char *salt = (unsigned char *)"salt1234";
  46632. int sz = 5;
  46633. const byte data[] = {
  46634. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  46635. 0x72,0x6c,0x64
  46636. };
  46637. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  46638. /* Bad cases */
  46639. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  46640. 0);
  46641. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  46642. 16);
  46643. md = "2";
  46644. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  46645. WOLFSSL_FAILURE);
  46646. /* Good case */
  46647. md = EVP_sha256();
  46648. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  46649. 16);
  46650. #endif
  46651. return EXPECT_RESULT();
  46652. }
  46653. static int test_evp_cipher_aes_gcm(void)
  46654. {
  46655. EXPECT_DECLS;
  46656. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  46657. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  46658. (HAVE_FIPS_VERSION >= 2)))
  46659. /*
  46660. * This test checks data at various points in the encrypt/decrypt process
  46661. * against known values produced using the same test with OpenSSL. This
  46662. * interop testing is critical for verifying the correctness of our
  46663. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  46664. * a flow supported by OpenSSL that uses the control command
  46665. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  46666. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  46667. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  46668. * wolfSSL OpenSSH clients because there was a bug in this flow that
  46669. * happened to "cancel out" if both sides of the connection had the bug.
  46670. */
  46671. enum {
  46672. NUM_ENCRYPTIONS = 3,
  46673. AAD_SIZE = 4
  46674. };
  46675. byte plainText1[] = {
  46676. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  46677. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  46678. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  46679. };
  46680. byte plainText2[] = {
  46681. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  46682. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  46683. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  46684. };
  46685. byte plainText3[] = {
  46686. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  46687. 0x5b, 0x57, 0x10
  46688. };
  46689. byte* plainTexts[NUM_ENCRYPTIONS] = {
  46690. plainText1,
  46691. plainText2,
  46692. plainText3
  46693. };
  46694. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  46695. sizeof(plainText1),
  46696. sizeof(plainText2),
  46697. sizeof(plainText3)
  46698. };
  46699. byte aad1[AAD_SIZE] = {
  46700. 0x00, 0x00, 0x00, 0x01
  46701. };
  46702. byte aad2[AAD_SIZE] = {
  46703. 0x00, 0x00, 0x00, 0x10
  46704. };
  46705. byte aad3[AAD_SIZE] = {
  46706. 0x00, 0x00, 0x01, 0x00
  46707. };
  46708. byte* aads[NUM_ENCRYPTIONS] = {
  46709. aad1,
  46710. aad2,
  46711. aad3
  46712. };
  46713. const byte iv[GCM_NONCE_MID_SZ] = {
  46714. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  46715. };
  46716. byte currentIv[GCM_NONCE_MID_SZ];
  46717. const byte key[] = {
  46718. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  46719. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  46720. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  46721. };
  46722. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  46723. {
  46724. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  46725. 0xEF
  46726. },
  46727. {
  46728. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  46729. 0xF0
  46730. },
  46731. {
  46732. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  46733. 0xF1
  46734. }
  46735. };
  46736. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  46737. {
  46738. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  46739. 0x3D, 0x32, 0x18, 0x34, 0xA9
  46740. },
  46741. {
  46742. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  46743. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  46744. },
  46745. {
  46746. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  46747. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  46748. }
  46749. };
  46750. const byte expCipherText1[] = {
  46751. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  46752. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  46753. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  46754. };
  46755. const byte expCipherText2[] = {
  46756. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  46757. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  46758. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  46759. };
  46760. const byte expCipherText3[] = {
  46761. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  46762. 0x80, 0x5E, 0x6B,
  46763. };
  46764. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  46765. expCipherText1,
  46766. expCipherText2,
  46767. expCipherText3
  46768. };
  46769. byte* cipherText = NULL;
  46770. byte* calcPlainText = NULL;
  46771. byte tag[AES_BLOCK_SIZE];
  46772. EVP_CIPHER_CTX* encCtx = NULL;
  46773. EVP_CIPHER_CTX* decCtx = NULL;
  46774. int i, j, outl;
  46775. /****************************************************/
  46776. for (i = 0; i < 3; ++i) {
  46777. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  46778. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  46779. /* First iteration, set key before IV. */
  46780. if (i == 0) {
  46781. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  46782. SSL_SUCCESS);
  46783. /*
  46784. * The call to EVP_CipherInit below (with NULL key) should clear the
  46785. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  46786. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  46787. * behavior.
  46788. */
  46789. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  46790. (void*)iv), SSL_SUCCESS);
  46791. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  46792. SSL_SUCCESS);
  46793. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  46794. currentIv), SSL_FAILURE);
  46795. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  46796. SSL_SUCCESS);
  46797. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  46798. SSL_SUCCESS);
  46799. }
  46800. /* Second iteration, IV before key. */
  46801. else {
  46802. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  46803. SSL_SUCCESS);
  46804. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  46805. SSL_SUCCESS);
  46806. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  46807. SSL_SUCCESS);
  46808. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  46809. SSL_SUCCESS);
  46810. }
  46811. /*
  46812. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  46813. * been issued first.
  46814. */
  46815. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  46816. currentIv), SSL_FAILURE);
  46817. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  46818. (void*)iv), SSL_SUCCESS);
  46819. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  46820. (void*)iv), SSL_SUCCESS);
  46821. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  46822. /*************** Encrypt ***************/
  46823. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  46824. currentIv), SSL_SUCCESS);
  46825. /* Check current IV against expected. */
  46826. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  46827. /* Add AAD. */
  46828. if (i == 2) {
  46829. /* Test streaming API. */
  46830. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  46831. AAD_SIZE), SSL_SUCCESS);
  46832. }
  46833. else {
  46834. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  46835. AAD_SIZE);
  46836. }
  46837. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  46838. DYNAMIC_TYPE_TMP_BUFFER));
  46839. /* Encrypt plaintext. */
  46840. if (i == 2) {
  46841. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  46842. plainTexts[j], plainTextSzs[j]),
  46843. SSL_SUCCESS);
  46844. }
  46845. else {
  46846. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  46847. plainTextSzs[j]), plainTextSzs[j]);
  46848. }
  46849. if (i == 2) {
  46850. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  46851. SSL_SUCCESS);
  46852. }
  46853. else {
  46854. /*
  46855. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  46856. * akin to calling EVP_CipherFinal.
  46857. */
  46858. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  46859. }
  46860. /* Check ciphertext against expected. */
  46861. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  46862. 0);
  46863. /* Get and check tag against expected. */
  46864. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  46865. sizeof(tag), tag), SSL_SUCCESS);
  46866. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  46867. /*************** Decrypt ***************/
  46868. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  46869. currentIv), SSL_SUCCESS);
  46870. /* Check current IV against expected. */
  46871. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  46872. /* Add AAD. */
  46873. if (i == 2) {
  46874. /* Test streaming API. */
  46875. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  46876. AAD_SIZE), SSL_SUCCESS);
  46877. }
  46878. else {
  46879. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  46880. AAD_SIZE);
  46881. }
  46882. /* Set expected tag. */
  46883. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  46884. sizeof(tag), tag), SSL_SUCCESS);
  46885. /* Decrypt ciphertext. */
  46886. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  46887. DYNAMIC_TYPE_TMP_BUFFER));
  46888. if (i == 2) {
  46889. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  46890. cipherText, plainTextSzs[j]),
  46891. SSL_SUCCESS);
  46892. }
  46893. else {
  46894. /* This first EVP_Cipher call will check the tag, too. */
  46895. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  46896. plainTextSzs[j]), plainTextSzs[j]);
  46897. }
  46898. if (i == 2) {
  46899. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  46900. SSL_SUCCESS);
  46901. }
  46902. else {
  46903. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  46904. }
  46905. /* Check plaintext against expected. */
  46906. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  46907. 0);
  46908. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46909. cipherText = NULL;
  46910. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46911. calcPlainText = NULL;
  46912. }
  46913. EVP_CIPHER_CTX_free(encCtx);
  46914. encCtx = NULL;
  46915. EVP_CIPHER_CTX_free(decCtx);
  46916. decCtx = NULL;
  46917. }
  46918. #endif
  46919. return EXPECT_RESULT();
  46920. }
  46921. static int test_wolfSSL_OBJ_ln(void)
  46922. {
  46923. EXPECT_DECLS;
  46924. const int nid_set[] = {
  46925. NID_commonName,
  46926. NID_serialNumber,
  46927. NID_countryName,
  46928. NID_localityName,
  46929. NID_stateOrProvinceName,
  46930. NID_organizationName,
  46931. NID_organizationalUnitName,
  46932. NID_domainComponent,
  46933. NID_businessCategory,
  46934. NID_jurisdictionCountryName,
  46935. NID_jurisdictionStateOrProvinceName,
  46936. NID_emailAddress
  46937. };
  46938. const char* ln_set[] = {
  46939. "commonName",
  46940. "serialNumber",
  46941. "countryName",
  46942. "localityName",
  46943. "stateOrProvinceName",
  46944. "organizationName",
  46945. "organizationalUnitName",
  46946. "domainComponent",
  46947. "businessCategory",
  46948. "jurisdictionCountryName",
  46949. "jurisdictionStateOrProvinceName",
  46950. "emailAddress",
  46951. };
  46952. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  46953. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  46954. #ifdef HAVE_ECC
  46955. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46956. {
  46957. EC_builtin_curve r[27];
  46958. size_t nCurves = sizeof(r) / sizeof(r[0]);
  46959. nCurves = EC_get_builtin_curves(r, nCurves);
  46960. for (i = 0; i < nCurves; i++) {
  46961. /* skip ECC_CURVE_INVALID */
  46962. if (r[i].nid != ECC_CURVE_INVALID) {
  46963. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  46964. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  46965. }
  46966. }
  46967. }
  46968. #endif
  46969. #endif
  46970. for (i = 0; i < maxIdx; i++) {
  46971. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  46972. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  46973. }
  46974. return EXPECT_RESULT();
  46975. }
  46976. static int test_wolfSSL_OBJ_sn(void)
  46977. {
  46978. EXPECT_DECLS;
  46979. int i = 0, maxIdx = 7;
  46980. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  46981. NID_stateOrProvinceName,NID_organizationName,
  46982. NID_organizationalUnitName,NID_emailAddress};
  46983. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  46984. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  46985. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  46986. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  46987. WOLFSSL_EMAIL_ADDR};
  46988. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  46989. for (i = 0; i < maxIdx; i++) {
  46990. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  46991. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  46992. }
  46993. return EXPECT_RESULT();
  46994. }
  46995. #if !defined(NO_BIO)
  46996. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  46997. {
  46998. return lh_strhash(s[3]);
  46999. }
  47000. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  47001. {
  47002. return XSTRCMP(a[3], b[3]);
  47003. }
  47004. #endif
  47005. static int test_wolfSSL_TXT_DB(void)
  47006. {
  47007. EXPECT_DECLS;
  47008. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  47009. BIO *bio = NULL;
  47010. TXT_DB *db = NULL;
  47011. const int columns = 6;
  47012. const char *fields[6] = {
  47013. "V",
  47014. "320926161116Z",
  47015. "",
  47016. "12BD",
  47017. "unknown",
  47018. "/CN=rsa doe",
  47019. };
  47020. char** fields_copy = NULL;
  47021. /* Test read */
  47022. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  47023. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  47024. ExpectNotNull(db = TXT_DB_read(bio, columns));
  47025. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  47026. DYNAMIC_TYPE_OPENSSL));
  47027. if (fields_copy != NULL) {
  47028. XMEMCPY(fields_copy, fields, sizeof(fields));
  47029. }
  47030. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  47031. if (EXPECT_FAIL()) {
  47032. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  47033. }
  47034. BIO_free(bio);
  47035. bio = NULL;
  47036. /* Test write */
  47037. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47038. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  47039. BIO_free(bio);
  47040. /* Test index */
  47041. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  47042. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  47043. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  47044. fields[3] = "12DA";
  47045. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  47046. fields[3] = "FFFF";
  47047. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  47048. fields[3] = "";
  47049. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  47050. TXT_DB_free(db);
  47051. #endif
  47052. return EXPECT_RESULT();
  47053. }
  47054. static int test_wolfSSL_NCONF(void)
  47055. {
  47056. EXPECT_DECLS;
  47057. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  47058. const char* confFile = "./tests/NCONF_test.cnf";
  47059. CONF* conf = NULL;
  47060. long eline = 0;
  47061. long num = 0;
  47062. ExpectNotNull(conf = NCONF_new(NULL));
  47063. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  47064. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  47065. ExpectIntEQ(num, 1234);
  47066. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  47067. ExpectIntEQ(num, 4321);
  47068. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  47069. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  47070. "./test-dir/file1");
  47071. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  47072. "./test-dir/file1:./test-dir/file2:./section1:file2");
  47073. NCONF_free(conf);
  47074. #endif
  47075. return EXPECT_RESULT();
  47076. }
  47077. #endif /* OPENSSL_ALL */
  47078. static int test_wolfSSL_X509V3_EXT_get(void) {
  47079. EXPECT_DECLS;
  47080. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47081. XFILE f = XBADFILE;
  47082. int numOfExt =0;
  47083. int extNid = 0;
  47084. int i = 0;
  47085. WOLFSSL_X509* x509 = NULL;
  47086. WOLFSSL_X509_EXTENSION* ext = NULL;
  47087. const WOLFSSL_v3_ext_method* method = NULL;
  47088. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47089. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47090. if (f != XBADFILE)
  47091. XFCLOSE(f);
  47092. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  47093. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  47094. for (i = 0; i < numOfExt; i++) {
  47095. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47096. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  47097. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  47098. ExpectIntEQ(method->ext_nid, extNid);
  47099. }
  47100. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  47101. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  47102. wolfSSL_X509_free(x509);
  47103. #endif
  47104. return EXPECT_RESULT();
  47105. }
  47106. static int test_wolfSSL_X509V3_EXT_nconf(void)
  47107. {
  47108. EXPECT_DECLS;
  47109. #ifdef OPENSSL_ALL
  47110. const char *ext_names[] = {
  47111. "subjectKeyIdentifier",
  47112. "authorityKeyIdentifier",
  47113. "subjectAltName",
  47114. "keyUsage",
  47115. "extendedKeyUsage",
  47116. };
  47117. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  47118. int ext_nids[] = {
  47119. NID_subject_key_identifier,
  47120. NID_authority_key_identifier,
  47121. NID_subject_alt_name,
  47122. NID_key_usage,
  47123. NID_ext_key_usage,
  47124. };
  47125. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  47126. const char *ext_values[] = {
  47127. "hash",
  47128. "hash",
  47129. "DNS:example.com, IP:127.0.0.1",
  47130. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  47131. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  47132. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  47133. "OCSPSigning",
  47134. };
  47135. size_t i;
  47136. X509_EXTENSION* ext = NULL;
  47137. X509* x509 = NULL;
  47138. unsigned int keyUsageFlags;
  47139. unsigned int extKeyUsageFlags;
  47140. ExpectNotNull(x509 = X509_new());
  47141. /* keyUsage / extKeyUsage should match string above */
  47142. keyUsageFlags = KU_DIGITAL_SIGNATURE
  47143. | KU_NON_REPUDIATION
  47144. | KU_KEY_ENCIPHERMENT
  47145. | KU_DATA_ENCIPHERMENT
  47146. | KU_KEY_AGREEMENT
  47147. | KU_KEY_CERT_SIGN
  47148. | KU_CRL_SIGN
  47149. | KU_ENCIPHER_ONLY
  47150. | KU_DECIPHER_ONLY;
  47151. extKeyUsageFlags = XKU_SSL_CLIENT
  47152. | XKU_SSL_SERVER
  47153. | XKU_CODE_SIGN
  47154. | XKU_SMIME
  47155. | XKU_TIMESTAMP
  47156. | XKU_OCSP_SIGN;
  47157. for (i = 0; i < ext_names_count; i++) {
  47158. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  47159. ext_values[i]));
  47160. X509_EXTENSION_free(ext);
  47161. ext = NULL;
  47162. }
  47163. for (i = 0; i < ext_nids_count; i++) {
  47164. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  47165. ext_values[i]));
  47166. X509_EXTENSION_free(ext);
  47167. ext = NULL;
  47168. }
  47169. /* Test adding extension to X509 */
  47170. for (i = 0; i < ext_nids_count; i++) {
  47171. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  47172. ext_values[i]));
  47173. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  47174. if (ext_nids[i] == NID_key_usage) {
  47175. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  47176. }
  47177. else if (ext_nids[i] == NID_ext_key_usage) {
  47178. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  47179. }
  47180. X509_EXTENSION_free(ext);
  47181. ext = NULL;
  47182. }
  47183. X509_free(x509);
  47184. #endif
  47185. return EXPECT_RESULT();
  47186. }
  47187. static int test_wolfSSL_X509V3_EXT(void) {
  47188. EXPECT_DECLS;
  47189. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47190. XFILE f = XBADFILE;
  47191. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  47192. char* str = NULL;
  47193. unsigned char* data = NULL;
  47194. const WOLFSSL_v3_ext_method* method = NULL;
  47195. WOLFSSL_X509* x509 = NULL;
  47196. WOLFSSL_X509_EXTENSION* ext = NULL;
  47197. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  47198. WOLFSSL_ASN1_OBJECT *obj = NULL;
  47199. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  47200. WOLFSSL_ASN1_STRING* asn1str = NULL;
  47201. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  47202. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  47203. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  47204. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  47205. WOLFSSL_GENERAL_NAME* gn = NULL;
  47206. /* Check NULL argument */
  47207. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  47208. /* Using OCSP cert with X509V3 extensions */
  47209. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  47210. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47211. if (f != XBADFILE)
  47212. XFCLOSE(f);
  47213. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  47214. /* Basic Constraints */
  47215. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47216. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  47217. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  47218. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  47219. ExpectIntEQ(bc->ca, 1);
  47220. ExpectNull(bc->pathlen);
  47221. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  47222. bc = NULL;
  47223. i++;
  47224. /* Subject Key Identifier */
  47225. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47226. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  47227. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  47228. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  47229. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  47230. asn1str));
  47231. X509_EXTENSION_free(ext2);
  47232. ext2 = NULL;
  47233. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  47234. ExpectNotNull(method->i2s);
  47235. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  47236. wolfSSL_ASN1_STRING_free(asn1str);
  47237. asn1str = NULL;
  47238. if (str != NULL) {
  47239. actual = strcmp(str,
  47240. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  47241. }
  47242. ExpectIntEQ(actual, 0);
  47243. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47244. str = NULL;
  47245. i++;
  47246. /* Authority Key Identifier */
  47247. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47248. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  47249. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  47250. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  47251. ext));
  47252. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  47253. ExpectNotNull(asn1str = aKeyId->keyid);
  47254. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  47255. asn1str));
  47256. asn1str = NULL;
  47257. if (str != NULL) {
  47258. actual = strcmp(str,
  47259. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  47260. }
  47261. ExpectIntEQ(actual, 0);
  47262. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47263. str = NULL;
  47264. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  47265. aKeyId = NULL;
  47266. i++;
  47267. /* Key Usage */
  47268. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47269. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  47270. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  47271. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  47272. #if defined(WOLFSSL_QT)
  47273. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  47274. #else
  47275. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  47276. #endif
  47277. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  47278. if (data != NULL) {
  47279. #ifdef BIG_ENDIAN_ORDER
  47280. actual = data[1];
  47281. #else
  47282. actual = data[0];
  47283. #endif
  47284. }
  47285. ExpectIntEQ(actual, expected);
  47286. wolfSSL_ASN1_STRING_free(asn1str);
  47287. asn1str = NULL;
  47288. #if 1
  47289. i++;
  47290. /* Authority Info Access */
  47291. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47292. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  47293. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  47294. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  47295. ext));
  47296. #if defined(WOLFSSL_QT)
  47297. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  47298. #else
  47299. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  47300. #endif
  47301. /* URI entry is an ACCESS_DESCRIPTION type */
  47302. #if defined(WOLFSSL_QT)
  47303. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  47304. #else
  47305. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  47306. #endif
  47307. ExpectNotNull(adObj = ad->method);
  47308. /* Make sure nid is OCSP */
  47309. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  47310. /* GENERAL_NAME stores URI as an ASN1_STRING */
  47311. ExpectNotNull(gn = ad->location);
  47312. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  47313. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  47314. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  47315. #if defined(WOLFSSL_QT)
  47316. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  47317. #else
  47318. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  47319. #endif
  47320. if (str != NULL) {
  47321. actual = strcmp(str, "http://127.0.0.1:22220");
  47322. }
  47323. ExpectIntEQ(actual, 0);
  47324. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  47325. aia = NULL;
  47326. #else
  47327. (void) aia; (void) ad; (void) adObj; (void) gn;
  47328. #endif
  47329. wolfSSL_X509_free(x509);
  47330. #endif
  47331. return EXPECT_RESULT();
  47332. }
  47333. static int test_wolfSSL_X509_get_extension_flags(void)
  47334. {
  47335. EXPECT_DECLS;
  47336. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  47337. XFILE f = XBADFILE;
  47338. X509* x509 = NULL;
  47339. unsigned int extFlags;
  47340. unsigned int keyUsageFlags;
  47341. unsigned int extKeyUsageFlags;
  47342. /* client-int-cert.pem has the following extension flags. */
  47343. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  47344. /* and the following key usage flags. */
  47345. keyUsageFlags = KU_DIGITAL_SIGNATURE
  47346. | KU_NON_REPUDIATION
  47347. | KU_KEY_ENCIPHERMENT;
  47348. /* and the following extended key usage flags. */
  47349. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  47350. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  47351. XBADFILE);
  47352. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  47353. if (f != XBADFILE) {
  47354. XFCLOSE(f);
  47355. f = XBADFILE;
  47356. }
  47357. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  47358. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  47359. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  47360. X509_free(x509);
  47361. x509 = NULL;
  47362. /* client-cert-ext.pem has the following extension flags. */
  47363. extFlags = EXFLAG_KUSAGE;
  47364. /* and the following key usage flags. */
  47365. keyUsageFlags = KU_DIGITAL_SIGNATURE
  47366. | KU_KEY_CERT_SIGN
  47367. | KU_CRL_SIGN;
  47368. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  47369. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  47370. if (f != XBADFILE)
  47371. XFCLOSE(f);
  47372. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  47373. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  47374. X509_free(x509);
  47375. #endif /* OPENSSL_ALL */
  47376. return EXPECT_RESULT();
  47377. }
  47378. static int test_wolfSSL_X509_get_ext(void)
  47379. {
  47380. EXPECT_DECLS;
  47381. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47382. int ret = 0;
  47383. XFILE f = XBADFILE;
  47384. WOLFSSL_X509* x509 = NULL;
  47385. WOLFSSL_X509_EXTENSION* foundExtension;
  47386. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47387. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47388. if (f != XBADFILE)
  47389. XFCLOSE(f);
  47390. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  47391. /* wolfSSL_X509_get_ext() valid input */
  47392. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  47393. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  47394. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  47395. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  47396. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  47397. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  47398. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  47399. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  47400. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  47401. wolfSSL_X509_free(x509);
  47402. #endif
  47403. return EXPECT_RESULT();
  47404. }
  47405. static int test_wolfSSL_X509_get_ext_by_NID(void)
  47406. {
  47407. EXPECT_DECLS;
  47408. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  47409. int rc = 0;
  47410. XFILE f = XBADFILE;
  47411. WOLFSSL_X509* x509 = NULL;
  47412. ASN1_OBJECT* obj = NULL;
  47413. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47414. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47415. if (f != XBADFILE)
  47416. XFCLOSE(f);
  47417. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  47418. -1), 0);
  47419. /* Start search from last location (should fail) */
  47420. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  47421. rc), -1);
  47422. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  47423. -2), -1);
  47424. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  47425. -1), -1);
  47426. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  47427. /* NID_ext_key_usage, check also its nid and oid */
  47428. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  47429. -1);
  47430. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  47431. x509, rc)));
  47432. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  47433. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  47434. wolfSSL_X509_free(x509);
  47435. #endif
  47436. return EXPECT_RESULT();
  47437. }
  47438. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  47439. {
  47440. EXPECT_DECLS;
  47441. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  47442. int rc = 0;
  47443. XFILE f = XBADFILE;
  47444. WOLFSSL_X509* x509 = NULL;
  47445. WOLFSSL_X509_EXTENSION* ext = NULL;
  47446. WOLFSSL_ASN1_STRING* sanString = NULL;
  47447. byte* sanDer = NULL;
  47448. const byte expectedDer[] = {
  47449. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  47450. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  47451. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47452. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  47453. if (f != XBADFILE)
  47454. XFCLOSE(f);
  47455. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  47456. ExpectNotNull(ext = X509_get_ext(x509, rc));
  47457. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  47458. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  47459. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  47460. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  47461. X509_free(x509);
  47462. #endif
  47463. return EXPECT_RESULT();
  47464. }
  47465. static int test_wolfSSL_X509_EXTENSION_new(void)
  47466. {
  47467. EXPECT_DECLS;
  47468. #if defined (OPENSSL_ALL)
  47469. WOLFSSL_X509_EXTENSION* ext = NULL;
  47470. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  47471. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  47472. wolfSSL_X509_EXTENSION_free(ext);
  47473. #endif
  47474. return EXPECT_RESULT();
  47475. }
  47476. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  47477. {
  47478. EXPECT_DECLS;
  47479. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47480. WOLFSSL_X509* x509 = NULL;
  47481. WOLFSSL_X509_EXTENSION* ext = NULL;
  47482. WOLFSSL_ASN1_OBJECT* o = NULL;
  47483. XFILE file = XBADFILE;
  47484. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47485. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  47486. if (file != XBADFILE)
  47487. XFCLOSE(file);
  47488. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  47489. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  47490. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  47491. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  47492. ExpectIntEQ(o->nid, 128);
  47493. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  47494. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  47495. wolfSSL_X509_free(x509);
  47496. #endif
  47497. return EXPECT_RESULT();
  47498. }
  47499. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  47500. {
  47501. EXPECT_DECLS;
  47502. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47503. WOLFSSL_X509* x509 = NULL;
  47504. WOLFSSL_X509_EXTENSION* ext = NULL;
  47505. WOLFSSL_ASN1_STRING* str = NULL;
  47506. XFILE file = XBADFILE;
  47507. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47508. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  47509. if (file != XBADFILE)
  47510. XFCLOSE(file);
  47511. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  47512. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  47513. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  47514. wolfSSL_X509_free(x509);
  47515. #endif
  47516. return EXPECT_RESULT();
  47517. }
  47518. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  47519. {
  47520. EXPECT_DECLS;
  47521. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47522. WOLFSSL_X509* x509 = NULL;
  47523. WOLFSSL_X509_EXTENSION* ext = NULL;
  47524. XFILE file = XBADFILE;
  47525. int crit = 0;
  47526. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47527. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  47528. if (file != XBADFILE)
  47529. XFCLOSE(file);
  47530. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  47531. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  47532. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  47533. wolfSSL_X509_free(x509);
  47534. #endif
  47535. return EXPECT_RESULT();
  47536. }
  47537. static int test_wolfSSL_X509V3_EXT_print(void)
  47538. {
  47539. EXPECT_DECLS;
  47540. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  47541. !defined(NO_RSA)
  47542. {
  47543. XFILE f = XBADFILE;
  47544. WOLFSSL_X509* x509 = NULL;
  47545. X509_EXTENSION * ext = NULL;
  47546. int loc = 0;
  47547. BIO *bio = NULL;
  47548. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  47549. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47550. if (f != XBADFILE)
  47551. fclose(f);
  47552. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  47553. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  47554. NID_basic_constraints, -1), -1);
  47555. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  47556. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  47557. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  47558. NID_subject_key_identifier, -1), -1);
  47559. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  47560. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  47561. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  47562. NID_authority_key_identifier, -1), -1);
  47563. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  47564. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  47565. wolfSSL_BIO_free(bio);
  47566. wolfSSL_X509_free(x509);
  47567. }
  47568. {
  47569. X509 *x509 = NULL;
  47570. BIO *bio = NULL;
  47571. X509_EXTENSION *ext = NULL;
  47572. unsigned int i = 0;
  47573. unsigned int idx = 0;
  47574. /* Some NIDs to test with */
  47575. int nids[] = {
  47576. /* NID_key_usage, currently X509_get_ext returns this as a bit
  47577. * string, which messes up X509V3_EXT_print */
  47578. /* NID_ext_key_usage, */
  47579. NID_subject_alt_name,
  47580. };
  47581. int* n = NULL;
  47582. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  47583. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  47584. WOLFSSL_FILETYPE_PEM));
  47585. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  47586. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  47587. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  47588. * update the index */
  47589. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  47590. ExpectNotNull(ext = X509_get_ext(x509, (int)idx));
  47591. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  47592. ExpectIntGT(fprintf(stderr, "\n"), 0);
  47593. }
  47594. BIO_free(bio);
  47595. X509_free(x509);
  47596. }
  47597. #endif
  47598. return EXPECT_RESULT();
  47599. }
  47600. static int test_wolfSSL_X509_cmp(void)
  47601. {
  47602. EXPECT_DECLS;
  47603. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  47604. XFILE file1 = XBADFILE;
  47605. XFILE file2 = XBADFILE;
  47606. WOLFSSL_X509* cert1 = NULL;
  47607. WOLFSSL_X509* cert2 = NULL;
  47608. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47609. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  47610. XBADFILE);
  47611. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  47612. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  47613. if (file1 != XBADFILE)
  47614. fclose(file1);
  47615. if (file2 != XBADFILE)
  47616. fclose(file2);
  47617. /* wolfSSL_X509_cmp() testing matching certs */
  47618. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  47619. /* wolfSSL_X509_cmp() testing mismatched certs */
  47620. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  47621. /* wolfSSL_X509_cmp() testing NULL, valid args */
  47622. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  47623. /* wolfSSL_X509_cmp() testing valid, NULL args */
  47624. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  47625. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  47626. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  47627. wolfSSL_X509_free(cert1);
  47628. wolfSSL_X509_free(cert2);
  47629. #endif
  47630. return EXPECT_RESULT();
  47631. }
  47632. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  47633. {
  47634. EXPECT_DECLS;
  47635. #if defined(OPENSSL_ALL)
  47636. EVP_PKEY* pkey;
  47637. pkey = EVP_PKEY_new();
  47638. ExpectNotNull(pkey);
  47639. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  47640. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  47641. EVP_PKEY_free(pkey);
  47642. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  47643. EVP_PKEY_free(pkey);
  47644. EVP_PKEY_free(pkey);
  47645. #endif
  47646. return EXPECT_RESULT();
  47647. }
  47648. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  47649. {
  47650. EXPECT_DECLS;
  47651. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47652. EVP_PKEY* pkey = NULL;
  47653. const unsigned char* p;
  47654. unsigned char *der = NULL;
  47655. unsigned char *tmp = NULL;
  47656. int derLen;
  47657. p = client_keypub_der_2048;
  47658. /* Check that key can be successfully decoded. */
  47659. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  47660. sizeof_client_keypub_der_2048));
  47661. /* Check that key can be successfully encoded. */
  47662. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  47663. /* Ensure that the encoded version matches the original. */
  47664. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  47665. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  47666. /* Do same test except with pre-allocated buffer to ensure the der pointer
  47667. * is advanced. */
  47668. tmp = der;
  47669. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  47670. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  47671. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  47672. ExpectTrue(der + derLen == tmp);
  47673. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  47674. EVP_PKEY_free(pkey);
  47675. #endif
  47676. return EXPECT_RESULT();
  47677. }
  47678. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  47679. {
  47680. EXPECT_DECLS;
  47681. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  47682. !defined(NO_ASN) && !defined(NO_PWDBASED)
  47683. EVP_PKEY* pkey = NULL;
  47684. const unsigned char* p;
  47685. unsigned char *der = NULL;
  47686. unsigned char *tmp = NULL;
  47687. int derLen;
  47688. unsigned char pub_buf[65];
  47689. const int pub_len = 65;
  47690. BN_CTX* ctx;
  47691. EC_GROUP* curve = NULL;
  47692. EC_KEY* ephemeral_key = NULL;
  47693. const EC_POINT* h;
  47694. /* Generate an x963 key pair and get public part into pub_buf */
  47695. ExpectNotNull(ctx = BN_CTX_new());
  47696. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  47697. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  47698. NID_X9_62_prime256v1));
  47699. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  47700. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  47701. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  47702. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  47703. /* Prepare the EVP_PKEY */
  47704. ExpectNotNull(pkey = EVP_PKEY_new());
  47705. p = pub_buf;
  47706. /* Check that key can be successfully decoded. */
  47707. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  47708. pub_len));
  47709. /* Check that key can be successfully encoded. */
  47710. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  47711. /* Ensure that the encoded version matches the original. */
  47712. ExpectIntEQ(derLen, pub_len);
  47713. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  47714. /* Do same test except with pre-allocated buffer to ensure the der pointer
  47715. * is advanced. */
  47716. tmp = der;
  47717. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  47718. ExpectIntEQ(derLen, pub_len);
  47719. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  47720. ExpectTrue(der + derLen == tmp);
  47721. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  47722. EVP_PKEY_free(pkey);
  47723. EC_KEY_free(ephemeral_key);
  47724. EC_GROUP_free(curve);
  47725. #endif
  47726. return EXPECT_RESULT();
  47727. }
  47728. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  47729. {
  47730. EXPECT_DECLS;
  47731. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  47732. DSA* dsa = NULL;
  47733. byte derIn[] = {
  47734. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  47735. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  47736. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  47737. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  47738. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  47739. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  47740. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  47741. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  47742. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  47743. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  47744. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  47745. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  47746. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  47747. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  47748. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  47749. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  47750. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  47751. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  47752. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  47753. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  47754. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  47755. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  47756. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  47757. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  47758. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  47759. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  47760. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  47761. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  47762. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  47763. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  47764. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  47765. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  47766. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  47767. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  47768. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  47769. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  47770. 0x1e, 0xde, 0xc4
  47771. };
  47772. int derInLen = sizeof(derIn);
  47773. byte* derOut = NULL;
  47774. int derOutLen;
  47775. byte* p = derIn;
  47776. /* Check that params can be successfully decoded. */
  47777. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  47778. /* Check that params can be successfully encoded. */
  47779. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  47780. /* Ensure that the encoded version matches the original. */
  47781. ExpectIntEQ(derInLen, derOutLen);
  47782. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  47783. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  47784. DSA_free(dsa);
  47785. #endif
  47786. return EXPECT_RESULT();
  47787. }
  47788. static int test_wolfSSL_i2d_PrivateKey(void)
  47789. {
  47790. EXPECT_DECLS;
  47791. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  47792. !defined(NO_ASN) && !defined(NO_PWDBASED)
  47793. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  47794. {
  47795. EVP_PKEY* pkey = NULL;
  47796. const unsigned char* server_key =
  47797. (const unsigned char*)server_key_der_2048;
  47798. unsigned char buf[FOURK_BUF];
  47799. unsigned char* pt = NULL;
  47800. int bufSz = 0;
  47801. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  47802. (long)sizeof_server_key_der_2048));
  47803. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  47804. pt = buf;
  47805. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  47806. ExpectIntNE((pt - buf), 0);
  47807. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  47808. EVP_PKEY_free(pkey);
  47809. }
  47810. #endif
  47811. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47812. {
  47813. EVP_PKEY* pkey = NULL;
  47814. const unsigned char* client_key =
  47815. (const unsigned char*)ecc_clikey_der_256;
  47816. unsigned char buf[FOURK_BUF];
  47817. unsigned char* pt = NULL;
  47818. int bufSz = 0;
  47819. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  47820. (long)sizeof_ecc_clikey_der_256)));
  47821. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  47822. pt = buf;
  47823. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  47824. ExpectIntNE((pt - buf), 0);
  47825. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  47826. EVP_PKEY_free(pkey);
  47827. }
  47828. #endif
  47829. #endif
  47830. return EXPECT_RESULT();
  47831. }
  47832. static int test_wolfSSL_OCSP_id_get0_info(void)
  47833. {
  47834. EXPECT_DECLS;
  47835. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  47836. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47837. X509* cert = NULL;
  47838. X509* issuer = NULL;
  47839. OCSP_CERTID* id = NULL;
  47840. OCSP_CERTID* id2 = NULL;
  47841. ASN1_STRING* name = NULL;
  47842. ASN1_OBJECT* pmd = NULL;
  47843. ASN1_STRING* keyHash = NULL;
  47844. ASN1_INTEGER* serial = NULL;
  47845. ASN1_INTEGER* x509Int = NULL;
  47846. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  47847. SSL_FILETYPE_PEM));
  47848. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  47849. SSL_FILETYPE_PEM));
  47850. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  47851. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  47852. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  47853. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  47854. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  47855. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  47856. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  47857. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  47858. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  47859. ExpectNotNull(serial);
  47860. /* compare serial number to one in cert, should be equal */
  47861. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  47862. ExpectIntEQ(x509Int->length, serial->length);
  47863. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  47864. /* test OCSP_id_cmp */
  47865. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  47866. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  47867. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  47868. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  47869. if (id != NULL) {
  47870. id->issuerHash[0] = ~id->issuerHash[0];
  47871. }
  47872. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  47873. OCSP_CERTID_free(id);
  47874. OCSP_CERTID_free(id2);
  47875. X509_free(cert); /* free's x509Int */
  47876. X509_free(issuer);
  47877. #endif
  47878. return EXPECT_RESULT();
  47879. }
  47880. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  47881. {
  47882. EXPECT_DECLS;
  47883. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  47884. WOLFSSL_OCSP_CERTID certId;
  47885. byte* targetBuffer = NULL;
  47886. byte* p;
  47887. /* OCSP CertID bytes taken from PCAP */
  47888. byte rawCertId[] = {
  47889. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  47890. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  47891. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  47892. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  47893. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  47894. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  47895. 0xcf, 0xbc, 0x91
  47896. };
  47897. int ret = 0;
  47898. int i;
  47899. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  47900. certId.rawCertId = rawCertId;
  47901. certId.rawCertIdSize = sizeof(rawCertId);
  47902. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  47903. DYNAMIC_TYPE_TMP_BUFFER));
  47904. p = targetBuffer;
  47905. /* Function returns the size of the encoded data. */
  47906. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  47907. /* If target buffer is not null, function increments targetBuffer to point
  47908. * just past the end of the encoded data. */
  47909. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  47910. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  47911. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  47912. }
  47913. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47914. targetBuffer = NULL;
  47915. /* If target buffer is null, function allocates memory for a buffer and
  47916. * copies the encoded data into it. targetBuffer then points to the start of
  47917. * this newly allocate buffer. */
  47918. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  47919. sizeof(rawCertId));
  47920. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  47921. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  47922. }
  47923. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  47924. #endif
  47925. return EXPECT_RESULT();
  47926. }
  47927. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  47928. {
  47929. EXPECT_DECLS;
  47930. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  47931. WOLFSSL_OCSP_CERTID* certId;
  47932. WOLFSSL_OCSP_CERTID* certIdGood;
  47933. WOLFSSL_OCSP_CERTID* certIdBad;
  47934. const unsigned char* rawCertIdPtr;
  47935. const unsigned char rawCertId[] = {
  47936. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  47937. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  47938. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  47939. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  47940. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  47941. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  47942. 0xcf, 0xbc, 0x91
  47943. };
  47944. rawCertIdPtr = &rawCertId[0];
  47945. /* If the cert ID is NULL the function should allocate it and copy the
  47946. * data to it. */
  47947. certId = NULL;
  47948. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  47949. sizeof(rawCertId)));
  47950. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  47951. if (certId != NULL) {
  47952. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  47953. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  47954. }
  47955. /* If the cert ID is not NULL the function will just copy the data to it. */
  47956. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  47957. DYNAMIC_TYPE_TMP_BUFFER));
  47958. ExpectNotNull(certId);
  47959. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  47960. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  47961. rawCertIdPtr = &rawCertId[0];
  47962. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  47963. sizeof(rawCertId)));
  47964. ExpectPtrEq(certIdGood, certId);
  47965. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  47966. if (certId != NULL) {
  47967. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  47968. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47969. certId = NULL;
  47970. }
  47971. /* The below tests should fail when passed bad parameters. NULL should
  47972. * always be returned. */
  47973. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  47974. sizeof(rawCertId)));
  47975. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  47976. sizeof(rawCertId)));
  47977. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  47978. #endif
  47979. return EXPECT_RESULT();
  47980. }
  47981. static int test_wolfSSL_OCSP_id_cmp(void)
  47982. {
  47983. EXPECT_DECLS;
  47984. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  47985. OCSP_CERTID id1;
  47986. OCSP_CERTID id2;
  47987. XMEMSET(&id1, 0, sizeof(id1));
  47988. XMEMSET(&id2, 0, sizeof(id2));
  47989. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  47990. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  47991. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  47992. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  47993. #endif
  47994. return EXPECT_RESULT();
  47995. }
  47996. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  47997. {
  47998. EXPECT_DECLS;
  47999. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  48000. WOLFSSL_OCSP_SINGLERESP single;
  48001. const WOLFSSL_OCSP_CERTID* certId;
  48002. XMEMSET(&single, 0, sizeof(single));
  48003. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  48004. ExpectPtrEq(&single, certId);
  48005. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  48006. #endif
  48007. return EXPECT_RESULT();
  48008. }
  48009. static int test_wolfSSL_OCSP_single_get0_status(void)
  48010. {
  48011. EXPECT_DECLS;
  48012. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  48013. WOLFSSL_OCSP_SINGLERESP single;
  48014. CertStatus certStatus;
  48015. WOLFSSL_ASN1_TIME* thisDate;
  48016. WOLFSSL_ASN1_TIME* nextDate;
  48017. int ret, i;
  48018. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  48019. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  48020. /* Fill the date fields with some dummy data. */
  48021. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  48022. certStatus.thisDateParsed.data[i] = i;
  48023. certStatus.nextDateParsed.data[i] = i;
  48024. }
  48025. certStatus.status = CERT_GOOD;
  48026. single.status = &certStatus;
  48027. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  48028. &nextDate);
  48029. ExpectIntEQ(ret, CERT_GOOD);
  48030. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  48031. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  48032. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  48033. CERT_GOOD);
  48034. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  48035. NULL), CERT_GOOD);
  48036. #endif
  48037. return EXPECT_RESULT();
  48038. }
  48039. static int test_wolfSSL_OCSP_resp_count(void)
  48040. {
  48041. EXPECT_DECLS;
  48042. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  48043. WOLFSSL_OCSP_BASICRESP basicResp;
  48044. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  48045. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  48046. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  48047. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  48048. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  48049. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  48050. basicResp.single = &singleRespOne;
  48051. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  48052. singleRespOne.next = &singleRespTwo;
  48053. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  48054. #endif
  48055. return EXPECT_RESULT();
  48056. }
  48057. static int test_wolfSSL_OCSP_resp_get0(void)
  48058. {
  48059. EXPECT_DECLS;
  48060. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  48061. WOLFSSL_OCSP_BASICRESP basicResp;
  48062. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  48063. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  48064. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  48065. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  48066. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  48067. basicResp.single = &singleRespOne;
  48068. singleRespOne.next = &singleRespTwo;
  48069. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  48070. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  48071. #endif
  48072. return EXPECT_RESULT();
  48073. }
  48074. static int test_wolfSSL_EVP_PKEY_derive(void)
  48075. {
  48076. EXPECT_DECLS;
  48077. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  48078. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  48079. EVP_PKEY_CTX *ctx = NULL;
  48080. unsigned char *skey = NULL;
  48081. size_t skeylen;
  48082. EVP_PKEY *pkey = NULL;
  48083. EVP_PKEY *peerkey = NULL;
  48084. const unsigned char* key;
  48085. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  48086. /* DH */
  48087. key = dh_key_der_2048;
  48088. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  48089. sizeof_dh_key_der_2048)));
  48090. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  48091. key = dh_key_der_2048;
  48092. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  48093. sizeof_dh_key_der_2048)));
  48094. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  48095. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  48096. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  48097. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  48098. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  48099. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  48100. DYNAMIC_TYPE_OPENSSL));
  48101. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  48102. EVP_PKEY_CTX_free(ctx);
  48103. ctx = NULL;
  48104. EVP_PKEY_free(peerkey);
  48105. peerkey = NULL;
  48106. EVP_PKEY_free(pkey);
  48107. pkey = NULL;
  48108. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  48109. skey = NULL;
  48110. #endif
  48111. #ifdef HAVE_ECC
  48112. /* ECDH */
  48113. key = ecc_clikey_der_256;
  48114. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  48115. sizeof_ecc_clikey_der_256)));
  48116. key = ecc_clikeypub_der_256;
  48117. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  48118. sizeof_ecc_clikeypub_der_256)));
  48119. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  48120. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  48121. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  48122. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  48123. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  48124. DYNAMIC_TYPE_OPENSSL));
  48125. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  48126. EVP_PKEY_CTX_free(ctx);
  48127. EVP_PKEY_free(peerkey);
  48128. EVP_PKEY_free(pkey);
  48129. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  48130. #endif /* HAVE_ECC */
  48131. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  48132. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  48133. return EXPECT_RESULT();
  48134. }
  48135. static int test_wolfSSL_EVP_PBE_scrypt(void)
  48136. {
  48137. EXPECT_DECLS;
  48138. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  48139. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  48140. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  48141. int ret;
  48142. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  48143. int pwdlen = sizeof(pwd);
  48144. const byte salt[] = {'N','a','C','l'};
  48145. int saltlen = sizeof(salt);
  48146. byte key[80];
  48147. word64 numOvr32 = (word64)INT32_MAX + 1;
  48148. /* expected derived key for N:16, r:1, p:1 */
  48149. const byte expectedKey[] = {
  48150. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  48151. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  48152. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  48153. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  48154. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  48155. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  48156. 0xE7, 0xE9, 0xC0, 0x9A};
  48157. /* N r p mx key keylen */
  48158. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  48159. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  48160. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  48161. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  48162. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  48163. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  48164. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  48165. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  48166. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  48167. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  48168. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  48169. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  48170. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  48171. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  48172. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  48173. ExpectIntEQ(ret, 1); /* should succeed */
  48174. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  48175. key, 64);
  48176. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  48177. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  48178. key, 64);
  48179. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  48180. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  48181. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  48182. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  48183. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  48184. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  48185. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  48186. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  48187. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  48188. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  48189. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  48190. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  48191. ExpectIntEQ(ret, 1);
  48192. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  48193. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  48194. #endif /* !NO_PWDBASED && !NO_SHA256 */
  48195. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  48196. return EXPECT_RESULT();
  48197. }
  48198. static int test_no_op_functions(void)
  48199. {
  48200. EXPECT_DECLS;
  48201. #if defined(OPENSSL_EXTRA)
  48202. /* this makes sure wolfSSL can compile and run these no-op functions */
  48203. SSL_load_error_strings();
  48204. ENGINE_load_builtin_engines();
  48205. OpenSSL_add_all_ciphers();
  48206. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  48207. #endif
  48208. return EXPECT_RESULT();
  48209. }
  48210. static int test_wolfSSL_CRYPTO_memcmp(void)
  48211. {
  48212. EXPECT_DECLS;
  48213. #ifdef OPENSSL_EXTRA
  48214. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  48215. "implementation of TLS/SSL for embedded devices to the cloud.";
  48216. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  48217. "implementation of TLS/SSL for embedded devices to the cloud.";
  48218. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  48219. "implementation of TLS/SSL for embedded devices to the cloud!";
  48220. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  48221. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  48222. #endif
  48223. return EXPECT_RESULT();
  48224. }
  48225. /*----------------------------------------------------------------------------*
  48226. | wolfCrypt ASN
  48227. *----------------------------------------------------------------------------*/
  48228. static int test_wc_CreateEncryptedPKCS8Key(void)
  48229. {
  48230. EXPECT_DECLS;
  48231. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  48232. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  48233. WC_RNG rng;
  48234. byte* encKey = NULL;
  48235. word32 encKeySz = 0;
  48236. word32 decKeySz = 0;
  48237. const char password[] = "Lorem ipsum dolor sit amet";
  48238. word32 passwordSz = (word32)XSTRLEN(password);
  48239. word32 tradIdx = 0;
  48240. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48241. ExpectIntEQ(wc_InitRng(&rng), 0);
  48242. PRIVATE_KEY_UNLOCK();
  48243. /* Call with NULL for out buffer to get necessary length. */
  48244. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  48245. sizeof_server_key_der_2048, NULL, &encKeySz, password, (int)passwordSz,
  48246. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  48247. LENGTH_ONLY_E);
  48248. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  48249. DYNAMIC_TYPE_TMP_BUFFER));
  48250. /* Call with the allocated out buffer. */
  48251. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  48252. sizeof_server_key_der_2048, encKey, &encKeySz, password, (int)passwordSz,
  48253. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  48254. 0);
  48255. /* Decrypt the encrypted PKCS8 key we just made. */
  48256. ExpectIntGT((decKeySz = (word32)wc_DecryptPKCS8Key(encKey, encKeySz, password,
  48257. (int)passwordSz)), 0);
  48258. /* encKey now holds the decrypted key (decrypted in place). */
  48259. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  48260. /* Check that the decrypted key matches the key prior to encryption. */
  48261. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  48262. sizeof_server_key_der_2048), 0);
  48263. PRIVATE_KEY_LOCK();
  48264. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48265. wc_FreeRng(&rng);
  48266. #endif
  48267. return EXPECT_RESULT();
  48268. }
  48269. static int test_wc_GetPkcs8TraditionalOffset(void)
  48270. {
  48271. EXPECT_DECLS;
  48272. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  48273. int length;
  48274. int derSz = 0;
  48275. word32 inOutIdx;
  48276. const char* path = "./certs/server-keyPkcs8.der";
  48277. XFILE file = XBADFILE;
  48278. byte der[2048];
  48279. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  48280. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  48281. if (file != XBADFILE)
  48282. XFCLOSE(file);
  48283. /* valid case */
  48284. inOutIdx = 0;
  48285. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  48286. 0);
  48287. /* inOutIdx > sz */
  48288. inOutIdx = 4000;
  48289. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  48290. BAD_FUNC_ARG);
  48291. /* null input */
  48292. inOutIdx = 0;
  48293. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  48294. BAD_FUNC_ARG);
  48295. /* invalid input, fill buffer with 1's */
  48296. XMEMSET(der, 1, sizeof(der));
  48297. inOutIdx = 0;
  48298. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  48299. ASN_PARSE_E);
  48300. #endif /* NO_ASN */
  48301. return EXPECT_RESULT();
  48302. }
  48303. static int test_wc_SetSubjectRaw(void)
  48304. {
  48305. EXPECT_DECLS;
  48306. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48307. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  48308. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  48309. WOLFSSL_X509* x509 = NULL;
  48310. int peerCertSz;
  48311. const byte* peerCertBuf = NULL;
  48312. Cert forgedCert;
  48313. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  48314. WOLFSSL_FILETYPE_ASN1));
  48315. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  48316. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  48317. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  48318. wolfSSL_FreeX509(x509);
  48319. #endif
  48320. return EXPECT_RESULT();
  48321. }
  48322. static int test_wc_GetSubjectRaw(void)
  48323. {
  48324. EXPECT_DECLS;
  48325. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48326. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  48327. Cert cert;
  48328. byte *subjectRaw;
  48329. ExpectIntEQ(0, wc_InitCert(&cert));
  48330. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  48331. #endif
  48332. return EXPECT_RESULT();
  48333. }
  48334. static int test_wc_SetIssuerRaw(void)
  48335. {
  48336. EXPECT_DECLS;
  48337. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48338. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  48339. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  48340. WOLFSSL_X509* x509 = NULL;
  48341. int peerCertSz;
  48342. const byte* peerCertBuf;
  48343. Cert forgedCert;
  48344. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  48345. WOLFSSL_FILETYPE_ASN1));
  48346. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  48347. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  48348. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  48349. wolfSSL_FreeX509(x509);
  48350. #endif
  48351. return EXPECT_RESULT();
  48352. }
  48353. static int test_wc_SetIssueBuffer(void)
  48354. {
  48355. EXPECT_DECLS;
  48356. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48357. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  48358. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  48359. WOLFSSL_X509* x509 = NULL;
  48360. int peerCertSz;
  48361. const byte* peerCertBuf;
  48362. Cert forgedCert;
  48363. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  48364. WOLFSSL_FILETYPE_ASN1));
  48365. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  48366. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  48367. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  48368. wolfSSL_FreeX509(x509);
  48369. #endif
  48370. return EXPECT_RESULT();
  48371. }
  48372. /*
  48373. * Testing wc_SetSubjectKeyId
  48374. */
  48375. static int test_wc_SetSubjectKeyId(void)
  48376. {
  48377. EXPECT_DECLS;
  48378. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48379. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  48380. Cert cert;
  48381. const char* file = "certs/ecc-client-keyPub.pem";
  48382. ExpectIntEQ(0, wc_InitCert(&cert));
  48383. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  48384. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  48385. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  48386. #endif
  48387. return EXPECT_RESULT();
  48388. } /* END test_wc_SetSubjectKeyId */
  48389. /*
  48390. * Testing wc_SetSubject
  48391. */
  48392. static int test_wc_SetSubject(void)
  48393. {
  48394. EXPECT_DECLS;
  48395. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48396. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  48397. Cert cert;
  48398. const char* file = "./certs/ca-ecc-cert.pem";
  48399. ExpectIntEQ(0, wc_InitCert(&cert));
  48400. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  48401. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  48402. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  48403. #endif
  48404. return EXPECT_RESULT();
  48405. } /* END test_wc_SetSubject */
  48406. static int test_CheckCertSignature(void)
  48407. {
  48408. EXPECT_DECLS;
  48409. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  48410. WOLFSSL_CERT_MANAGER* cm = NULL;
  48411. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  48412. XFILE fp = XBADFILE;
  48413. byte cert[4096];
  48414. int certSz;
  48415. #endif
  48416. ExpectIntEQ(BAD_FUNC_ARG, wc_CheckCertSignature(NULL, 0, NULL, NULL));
  48417. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  48418. ExpectIntEQ(BAD_FUNC_ARG, wc_CheckCertSignature(NULL, 0, NULL, cm));
  48419. #ifndef NO_RSA
  48420. #ifdef USE_CERT_BUFFERS_1024
  48421. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(server_cert_der_1024,
  48422. sizeof_server_cert_der_1024, NULL, cm));
  48423. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  48424. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  48425. WOLFSSL_FILETYPE_ASN1));
  48426. ExpectIntEQ(0, wc_CheckCertSignature(server_cert_der_1024,
  48427. sizeof_server_cert_der_1024, NULL, cm));
  48428. #elif defined(USE_CERT_BUFFERS_2048)
  48429. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(server_cert_der_2048,
  48430. sizeof_server_cert_der_2048, NULL, cm));
  48431. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  48432. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  48433. WOLFSSL_FILETYPE_ASN1));
  48434. ExpectIntEQ(0, wc_CheckCertSignature(server_cert_der_2048,
  48435. sizeof_server_cert_der_2048, NULL, cm));
  48436. #endif
  48437. #endif
  48438. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  48439. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(serv_ecc_der_256,
  48440. sizeof_serv_ecc_der_256, NULL, cm));
  48441. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  48442. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  48443. WOLFSSL_FILETYPE_ASN1));
  48444. ExpectIntEQ(0, wc_CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  48445. NULL, cm));
  48446. #endif
  48447. #if !defined(NO_FILESYSTEM)
  48448. wolfSSL_CertManagerFree(cm);
  48449. cm = NULL;
  48450. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  48451. #ifndef NO_RSA
  48452. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  48453. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  48454. if (fp != XBADFILE) {
  48455. XFCLOSE(fp);
  48456. fp = XBADFILE;
  48457. }
  48458. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(cert, certSz, NULL, cm));
  48459. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  48460. "./certs/ca-cert.pem", NULL));
  48461. ExpectIntEQ(0, wc_CheckCertSignature(cert, certSz, NULL, cm));
  48462. #endif
  48463. #ifdef HAVE_ECC
  48464. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  48465. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  48466. if (fp != XBADFILE) {
  48467. XFCLOSE(fp);
  48468. fp = XBADFILE;
  48469. }
  48470. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(cert, certSz, NULL, cm));
  48471. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  48472. "./certs/ca-ecc-cert.pem", NULL));
  48473. ExpectIntEQ(0, wc_CheckCertSignature(cert, certSz, NULL, cm));
  48474. #endif
  48475. #endif
  48476. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  48477. (void)fp;
  48478. (void)cert;
  48479. (void)certSz;
  48480. #endif
  48481. wolfSSL_CertManagerFree(cm);
  48482. #endif
  48483. return EXPECT_RESULT();
  48484. }
  48485. static int test_wc_ParseCert(void)
  48486. {
  48487. EXPECT_DECLS;
  48488. #if !defined(NO_CERTS) && !defined(NO_RSA)
  48489. DecodedCert decodedCert;
  48490. const byte* rawCert = client_cert_der_2048;
  48491. const int rawCertSize = sizeof_client_cert_der_2048;
  48492. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  48493. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  48494. #ifndef IGNORE_NAME_CONSTRAINTS
  48495. /* check that the subjects emailAddress was not put in the alt name list */
  48496. ExpectNotNull(decodedCert.subjectEmail);
  48497. ExpectNull(decodedCert.altEmailNames);
  48498. #endif
  48499. wc_FreeDecodedCert(&decodedCert);
  48500. #endif
  48501. return EXPECT_RESULT();
  48502. }
  48503. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  48504. * the API safely errors out on badly-formed ASN input.
  48505. * NOTE: Test not compatible with released FIPS implementations!
  48506. */
  48507. static int test_wc_ParseCert_Error(void)
  48508. {
  48509. EXPECT_DECLS;
  48510. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  48511. (!defined(HAVE_FIPS) || \
  48512. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  48513. DecodedCert decodedCert;
  48514. int i;
  48515. /* Certificate data */
  48516. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  48517. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  48518. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  48519. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  48520. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  48521. /* Test data */
  48522. const struct testStruct {
  48523. const byte* c;
  48524. const int cSz;
  48525. const int expRet;
  48526. } t[] = {
  48527. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  48528. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  48529. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  48530. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  48531. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  48532. };
  48533. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  48534. for (i = 0; i < tSz; i++) {
  48535. WOLFSSL_MSG_EX("i == %d", i);
  48536. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  48537. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  48538. wc_FreeDecodedCert(&decodedCert);
  48539. }
  48540. #endif
  48541. return EXPECT_RESULT();
  48542. }
  48543. static int test_MakeCertWithPathLen(void)
  48544. {
  48545. EXPECT_DECLS;
  48546. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  48547. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  48548. const byte expectedPathLen = 7;
  48549. Cert cert;
  48550. DecodedCert decodedCert;
  48551. byte der[FOURK_BUF];
  48552. int derSize = 0;
  48553. WC_RNG rng;
  48554. ecc_key key;
  48555. int ret;
  48556. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48557. XMEMSET(&key, 0, sizeof(ecc_key));
  48558. XMEMSET(&cert, 0, sizeof(Cert));
  48559. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  48560. ExpectIntEQ(wc_InitRng(&rng), 0);
  48561. ExpectIntEQ(wc_ecc_init(&key), 0);
  48562. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  48563. ExpectIntEQ(wc_InitCert(&cert), 0);
  48564. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  48565. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  48566. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  48567. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  48568. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  48569. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  48570. CTC_NAME_SIZE);
  48571. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  48572. CTC_NAME_SIZE);
  48573. cert.selfSigned = 1;
  48574. cert.isCA = 1;
  48575. cert.pathLen = expectedPathLen;
  48576. cert.pathLenSet = 1;
  48577. cert.sigType = CTC_SHA256wECDSA;
  48578. #ifdef WOLFSSL_CERT_EXT
  48579. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  48580. #endif
  48581. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  48582. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  48583. FOURK_BUF, NULL, &key, &rng), 0);
  48584. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  48585. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  48586. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  48587. wc_FreeDecodedCert(&decodedCert);
  48588. ret = wc_ecc_free(&key);
  48589. ExpectIntEQ(ret, 0);
  48590. ret = wc_FreeRng(&rng);
  48591. ExpectIntEQ(ret, 0);
  48592. #endif
  48593. return EXPECT_RESULT();
  48594. }
  48595. static int test_MakeCertWith0Ser(void)
  48596. {
  48597. EXPECT_DECLS;
  48598. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  48599. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC) && \
  48600. defined(WOLFSSL_ASN_TEMPLATE)
  48601. Cert cert;
  48602. DecodedCert decodedCert;
  48603. byte der[FOURK_BUF];
  48604. int derSize = 0;
  48605. WC_RNG rng;
  48606. ecc_key key;
  48607. int ret;
  48608. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48609. XMEMSET(&key, 0, sizeof(ecc_key));
  48610. XMEMSET(&cert, 0, sizeof(Cert));
  48611. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  48612. ExpectIntEQ(wc_InitRng(&rng), 0);
  48613. ExpectIntEQ(wc_ecc_init(&key), 0);
  48614. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  48615. ExpectIntEQ(wc_InitCert(&cert), 0);
  48616. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  48617. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  48618. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  48619. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  48620. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  48621. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  48622. CTC_NAME_SIZE);
  48623. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  48624. CTC_NAME_SIZE);
  48625. cert.selfSigned = 1;
  48626. cert.isCA = 1;
  48627. cert.sigType = CTC_SHA256wECDSA;
  48628. #ifdef WOLFSSL_CERT_EXT
  48629. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  48630. #endif
  48631. /* set serial number to 0 */
  48632. cert.serialSz = 1;
  48633. cert.serial[0] = 0;
  48634. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  48635. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  48636. FOURK_BUF, NULL, &key, &rng), 0);
  48637. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  48638. #if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON)
  48639. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL),
  48640. ASN_PARSE_E);
  48641. #else
  48642. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  48643. #endif
  48644. wc_FreeDecodedCert(&decodedCert);
  48645. ret = wc_ecc_free(&key);
  48646. ExpectIntEQ(ret, 0);
  48647. ret = wc_FreeRng(&rng);
  48648. ExpectIntEQ(ret, 0);
  48649. #endif
  48650. return EXPECT_RESULT();
  48651. }
  48652. static int test_MakeCertWithCaFalse(void)
  48653. {
  48654. EXPECT_DECLS;
  48655. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  48656. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  48657. const byte expectedIsCa = 0;
  48658. Cert cert;
  48659. DecodedCert decodedCert;
  48660. byte der[FOURK_BUF];
  48661. int derSize = 0;
  48662. WC_RNG rng;
  48663. ecc_key key;
  48664. int ret;
  48665. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48666. XMEMSET(&key, 0, sizeof(ecc_key));
  48667. XMEMSET(&cert, 0, sizeof(Cert));
  48668. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  48669. ExpectIntEQ(wc_InitRng(&rng), 0);
  48670. ExpectIntEQ(wc_ecc_init(&key), 0);
  48671. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  48672. ExpectIntEQ(wc_InitCert(&cert), 0);
  48673. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  48674. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  48675. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  48676. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  48677. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  48678. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  48679. CTC_NAME_SIZE);
  48680. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  48681. CTC_NAME_SIZE);
  48682. cert.selfSigned = 1;
  48683. cert.isCA = expectedIsCa;
  48684. cert.isCaSet = 1;
  48685. cert.sigType = CTC_SHA256wECDSA;
  48686. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  48687. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  48688. FOURK_BUF, NULL, &key, &rng), 0);
  48689. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  48690. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  48691. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  48692. wc_FreeDecodedCert(&decodedCert);
  48693. ret = wc_ecc_free(&key);
  48694. ExpectIntEQ(ret, 0);
  48695. ret = wc_FreeRng(&rng);
  48696. ExpectIntEQ(ret, 0);
  48697. #endif
  48698. return EXPECT_RESULT();
  48699. }
  48700. /*----------------------------------------------------------------------------*
  48701. | wolfCrypt ECC
  48702. *----------------------------------------------------------------------------*/
  48703. static int test_wc_ecc_get_curve_size_from_name(void)
  48704. {
  48705. EXPECT_DECLS;
  48706. #ifdef HAVE_ECC
  48707. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  48708. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  48709. #endif
  48710. /* invalid case */
  48711. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  48712. /* NULL input */
  48713. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  48714. #endif /* HAVE_ECC */
  48715. return EXPECT_RESULT();
  48716. }
  48717. static int test_wc_ecc_get_curve_id_from_name(void)
  48718. {
  48719. EXPECT_DECLS;
  48720. #ifdef HAVE_ECC
  48721. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  48722. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  48723. ECC_SECP256R1);
  48724. #endif
  48725. /* invalid case */
  48726. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  48727. /* NULL input */
  48728. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  48729. #endif /* HAVE_ECC */
  48730. return EXPECT_RESULT();
  48731. }
  48732. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  48733. !defined(HAVE_SELFTEST) && \
  48734. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  48735. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  48736. {
  48737. EXPECT_DECLS;
  48738. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  48739. ecc_key* key;
  48740. const ecc_set_type* params = NULL;
  48741. int ret;
  48742. #endif
  48743. WOLFSSL_EC_KEY *ecKey = NULL;
  48744. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  48745. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  48746. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  48747. if (EXPECT_SUCCESS()) {
  48748. ret = EC_KEY_generate_key(ecKey);
  48749. } else
  48750. ret = 0;
  48751. if (ret == 1) {
  48752. /* normal test */
  48753. key = (ecc_key*)ecKey->internal;
  48754. if (key != NULL) {
  48755. params = key->dp;
  48756. }
  48757. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  48758. ECC_SECP256R1);
  48759. }
  48760. #endif
  48761. /* invalid case, NULL input*/
  48762. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  48763. wolfSSL_EC_KEY_free(ecKey);
  48764. return EXPECT_RESULT();
  48765. }
  48766. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  48767. static int test_wc_ecc_get_curve_id_from_params(void)
  48768. {
  48769. EXPECT_DECLS;
  48770. #ifdef HAVE_ECC
  48771. const byte prime[] =
  48772. {
  48773. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  48774. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48775. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  48776. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  48777. };
  48778. const byte primeInvalid[] =
  48779. {
  48780. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  48781. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48782. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  48783. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  48784. };
  48785. const byte Af[] =
  48786. {
  48787. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  48788. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48789. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  48790. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  48791. };
  48792. const byte Bf[] =
  48793. {
  48794. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  48795. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  48796. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  48797. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  48798. };
  48799. const byte order[] =
  48800. {
  48801. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  48802. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  48803. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  48804. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  48805. };
  48806. const byte Gx[] =
  48807. {
  48808. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  48809. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  48810. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  48811. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  48812. };
  48813. const byte Gy[] =
  48814. {
  48815. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  48816. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  48817. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  48818. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  48819. };
  48820. int cofactor = 1;
  48821. int fieldSize = 256;
  48822. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  48823. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  48824. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  48825. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  48826. ECC_SECP256R1);
  48827. #endif
  48828. /* invalid case, fieldSize = 0 */
  48829. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  48830. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  48831. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  48832. /* invalid case, NULL prime */
  48833. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  48834. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  48835. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  48836. /* invalid case, invalid prime */
  48837. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  48838. primeInvalid, sizeof(primeInvalid),
  48839. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  48840. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  48841. #endif
  48842. return EXPECT_RESULT();
  48843. }
  48844. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  48845. {
  48846. EXPECT_DECLS;
  48847. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  48848. WOLFSSL_RSA* rsa = NULL;
  48849. WOLFSSL_EVP_PKEY* pkey = NULL;
  48850. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  48851. const char* in = "What is easy to do is easy not to do.";
  48852. size_t inlen = XSTRLEN(in);
  48853. size_t outEncLen = 0;
  48854. byte* outEnc = NULL;
  48855. byte* outDec = NULL;
  48856. size_t outDecLen = 0;
  48857. size_t rsaKeySz = 2048/8; /* Bytes */
  48858. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  48859. byte* inTmp = NULL;
  48860. byte* outEncTmp = NULL;
  48861. byte* outDecTmp = NULL;
  48862. #endif
  48863. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  48864. DYNAMIC_TYPE_TMP_BUFFER));
  48865. if (outEnc != NULL) {
  48866. XMEMSET(outEnc, 0, rsaKeySz);
  48867. }
  48868. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  48869. DYNAMIC_TYPE_TMP_BUFFER));
  48870. if (outDec != NULL) {
  48871. XMEMSET(outDec, 0, rsaKeySz);
  48872. }
  48873. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  48874. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  48875. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  48876. if (EXPECT_FAIL()) {
  48877. RSA_free(rsa);
  48878. }
  48879. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  48880. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  48881. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  48882. WOLFSSL_SUCCESS);
  48883. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  48884. since ctx uses it.*/
  48885. ExpectIntEQ(pkey->ref.count, 2);
  48886. EVP_PKEY_free(pkey);
  48887. ExpectIntEQ(pkey->ref.count, 1);
  48888. /* Encrypt data */
  48889. /* Check that we can get the required output buffer length by passing in a
  48890. * NULL output buffer. */
  48891. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  48892. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  48893. ExpectIntEQ(rsaKeySz, outEncLen);
  48894. /* Now do the actual encryption. */
  48895. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  48896. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  48897. /* Decrypt data */
  48898. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  48899. /* Check that we can get the required output buffer length by passing in a
  48900. * NULL output buffer. */
  48901. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  48902. WOLFSSL_SUCCESS);
  48903. ExpectIntEQ(rsaKeySz, outDecLen);
  48904. /* Now do the actual decryption. */
  48905. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  48906. WOLFSSL_SUCCESS);
  48907. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  48908. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  48909. /* The input length must be the same size as the RSA key.*/
  48910. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  48911. DYNAMIC_TYPE_TMP_BUFFER));
  48912. if (inTmp != NULL) {
  48913. XMEMSET(inTmp, 9, rsaKeySz);
  48914. }
  48915. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  48916. DYNAMIC_TYPE_TMP_BUFFER));
  48917. if (outEncTmp != NULL) {
  48918. XMEMSET(outEncTmp, 0, rsaKeySz);
  48919. }
  48920. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  48921. DYNAMIC_TYPE_TMP_BUFFER));
  48922. if (outDecTmp != NULL) {
  48923. XMEMSET(outDecTmp, 0, rsaKeySz);
  48924. }
  48925. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  48926. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  48927. WOLFSSL_SUCCESS);
  48928. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  48929. WOLFSSL_SUCCESS);
  48930. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  48931. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  48932. outEncLen), WOLFSSL_SUCCESS);
  48933. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  48934. #endif
  48935. EVP_PKEY_CTX_free(ctx);
  48936. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48937. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48938. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  48939. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48940. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48941. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48942. #endif
  48943. #endif
  48944. return EXPECT_RESULT();
  48945. }
  48946. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  48947. !defined(HAVE_SELFTEST)
  48948. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48949. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48950. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48951. #endif
  48952. #endif
  48953. #endif
  48954. #if defined(OPENSSL_EXTRA)
  48955. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  48956. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48957. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48958. #endif
  48959. #endif
  48960. #endif
  48961. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  48962. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48963. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48964. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48965. #endif
  48966. #endif
  48967. #endif
  48968. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48969. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  48970. {
  48971. EXPECT_DECLS;
  48972. #if defined(OPENSSL_EXTRA)
  48973. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  48974. !defined(HAVE_SELFTEST)
  48975. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48976. WOLFSSL_RSA* rsa = NULL;
  48977. #endif
  48978. #endif
  48979. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  48980. WOLFSSL_DSA* dsa = NULL;
  48981. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  48982. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  48983. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48984. WOLFSSL_EC_KEY* ecKey = NULL;
  48985. #endif
  48986. #endif
  48987. WOLFSSL_EVP_PKEY* pkey = NULL;
  48988. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  48989. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  48990. const char* in = "What is easy to do is easy not to do.";
  48991. size_t inlen = XSTRLEN(in);
  48992. byte hash[SHA256_DIGEST_LENGTH] = {0};
  48993. byte zero[SHA256_DIGEST_LENGTH] = {0};
  48994. SHA256_CTX c;
  48995. byte* sig = NULL;
  48996. byte* sigVerify = NULL;
  48997. size_t siglen;
  48998. size_t siglenOnlyLen;
  48999. size_t keySz = 2048/8; /* Bytes */
  49000. ExpectNotNull(sig =
  49001. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  49002. ExpectNotNull(sigVerify =
  49003. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  49004. siglen = keySz;
  49005. ExpectNotNull(XMEMSET(sig, 0, keySz));
  49006. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  49007. /* Generate hash */
  49008. SHA256_Init(&c);
  49009. SHA256_Update(&c, in, inlen);
  49010. SHA256_Final(hash, &c);
  49011. #ifdef WOLFSSL_SMALL_STACK_CACHE
  49012. /* workaround for small stack cache case */
  49013. wc_Sha256Free((wc_Sha256*)&c);
  49014. #endif
  49015. /* Generate key */
  49016. ExpectNotNull(pkey = EVP_PKEY_new());
  49017. switch (keyType) {
  49018. case EVP_PKEY_RSA:
  49019. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49020. !defined(HAVE_SELFTEST)
  49021. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49022. {
  49023. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49024. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  49025. }
  49026. #endif
  49027. #endif
  49028. break;
  49029. case EVP_PKEY_DSA:
  49030. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  49031. ExpectNotNull(dsa = DSA_new());
  49032. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  49033. NULL, 0, NULL, NULL, NULL), 1);
  49034. ExpectIntEQ(DSA_generate_key(dsa), 1);
  49035. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  49036. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  49037. break;
  49038. case EVP_PKEY_EC:
  49039. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49040. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49041. {
  49042. ExpectNotNull(ecKey = EC_KEY_new());
  49043. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  49044. ExpectIntEQ(
  49045. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  49046. if (EXPECT_FAIL()) {
  49047. EC_KEY_free(ecKey);
  49048. }
  49049. }
  49050. #endif
  49051. #endif
  49052. break;
  49053. }
  49054. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  49055. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  49056. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49057. !defined(HAVE_SELFTEST)
  49058. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49059. if (keyType == EVP_PKEY_RSA)
  49060. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  49061. WOLFSSL_SUCCESS);
  49062. #endif
  49063. #endif
  49064. /* Check returning only length */
  49065. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  49066. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  49067. ExpectIntGT(siglenOnlyLen, 0);
  49068. /* Sign data */
  49069. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  49070. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  49071. ExpectIntGE(siglenOnlyLen, siglen);
  49072. /* Verify signature */
  49073. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  49074. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  49075. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49076. !defined(HAVE_SELFTEST)
  49077. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49078. if (keyType == EVP_PKEY_RSA)
  49079. ExpectIntEQ(
  49080. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  49081. WOLFSSL_SUCCESS);
  49082. #endif
  49083. #endif
  49084. ExpectIntEQ(EVP_PKEY_verify(
  49085. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  49086. WOLFSSL_SUCCESS);
  49087. ExpectIntEQ(EVP_PKEY_verify(
  49088. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  49089. WOLFSSL_FAILURE);
  49090. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49091. !defined(HAVE_SELFTEST)
  49092. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49093. if (keyType == EVP_PKEY_RSA) {
  49094. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  49095. /* Try RSA sign/verify with no padding. */
  49096. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  49097. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  49098. WOLFSSL_SUCCESS);
  49099. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  49100. siglen), WOLFSSL_SUCCESS);
  49101. ExpectIntGE(siglenOnlyLen, siglen);
  49102. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  49103. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  49104. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  49105. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  49106. siglen), WOLFSSL_SUCCESS);
  49107. #endif
  49108. /* Wrong padding schemes. */
  49109. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  49110. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  49111. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  49112. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  49113. siglen), WOLFSSL_SUCCESS);
  49114. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  49115. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  49116. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  49117. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  49118. siglen), WOLFSSL_SUCCESS);
  49119. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  49120. WOLFSSL_SUCCESS);
  49121. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  49122. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  49123. }
  49124. #endif
  49125. #endif
  49126. /* error cases */
  49127. siglen = keySz; /* Reset because sig size may vary slightly */
  49128. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  49129. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  49130. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  49131. WOLFSSL_SUCCESS);
  49132. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  49133. WOLFSSL_SUCCESS);
  49134. EVP_PKEY_free(pkey);
  49135. pkey = NULL;
  49136. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  49137. DSA_free(dsa);
  49138. dsa = NULL;
  49139. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  49140. EVP_PKEY_CTX_free(ctx_verify);
  49141. ctx_verify = NULL;
  49142. EVP_PKEY_CTX_free(ctx);
  49143. ctx = NULL;
  49144. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  49145. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  49146. #endif /* OPENSSL_EXTRA */
  49147. return EXPECT_RESULT();
  49148. }
  49149. #endif
  49150. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  49151. {
  49152. EXPECT_DECLS;
  49153. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49154. !defined(HAVE_SELFTEST)
  49155. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49156. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  49157. #endif
  49158. #endif
  49159. return EXPECT_RESULT();
  49160. }
  49161. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  49162. {
  49163. EXPECT_DECLS;
  49164. #if defined(OPENSSL_EXTRA)
  49165. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  49166. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  49167. #endif
  49168. #endif
  49169. return EXPECT_RESULT();
  49170. }
  49171. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  49172. {
  49173. EXPECT_DECLS;
  49174. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49175. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49176. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  49177. #endif
  49178. #endif
  49179. return EXPECT_RESULT();
  49180. }
  49181. static int test_EVP_PKEY_rsa(void)
  49182. {
  49183. EXPECT_DECLS;
  49184. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  49185. WOLFSSL_RSA* rsa = NULL;
  49186. WOLFSSL_EVP_PKEY* pkey = NULL;
  49187. ExpectNotNull(rsa = wolfSSL_RSA_new());
  49188. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  49189. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  49190. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  49191. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  49192. if (EXPECT_FAIL()) {
  49193. wolfSSL_RSA_free(rsa);
  49194. }
  49195. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  49196. wolfSSL_EVP_PKEY_free(pkey);
  49197. #endif
  49198. return EXPECT_RESULT();
  49199. }
  49200. static int test_EVP_PKEY_ec(void)
  49201. {
  49202. EXPECT_DECLS;
  49203. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49204. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49205. WOLFSSL_EC_KEY* ecKey = NULL;
  49206. WOLFSSL_EVP_PKEY* pkey = NULL;
  49207. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49208. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  49209. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  49210. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  49211. /* Should fail since ecKey is empty */
  49212. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  49213. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49214. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  49215. if (EXPECT_FAIL()) {
  49216. wolfSSL_EC_KEY_free(ecKey);
  49217. }
  49218. wolfSSL_EVP_PKEY_free(pkey);
  49219. #endif
  49220. #endif
  49221. return EXPECT_RESULT();
  49222. }
  49223. static int test_EVP_PKEY_cmp(void)
  49224. {
  49225. EXPECT_DECLS;
  49226. #if defined(OPENSSL_EXTRA)
  49227. EVP_PKEY *a = NULL;
  49228. EVP_PKEY *b = NULL;
  49229. const unsigned char *in;
  49230. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  49231. in = client_key_der_2048;
  49232. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  49233. &in, (long)sizeof_client_key_der_2048));
  49234. in = client_key_der_2048;
  49235. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  49236. &in, (long)sizeof_client_key_der_2048));
  49237. /* Test success case RSA */
  49238. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49239. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  49240. #else
  49241. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  49242. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  49243. EVP_PKEY_free(b);
  49244. b = NULL;
  49245. EVP_PKEY_free(a);
  49246. a = NULL;
  49247. #endif
  49248. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  49249. in = ecc_clikey_der_256;
  49250. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  49251. &in, (long)sizeof_ecc_clikey_der_256));
  49252. in = ecc_clikey_der_256;
  49253. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  49254. &in, (long)sizeof_ecc_clikey_der_256));
  49255. /* Test success case ECC */
  49256. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49257. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  49258. #else
  49259. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  49260. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  49261. EVP_PKEY_free(b);
  49262. b = NULL;
  49263. EVP_PKEY_free(a);
  49264. a = NULL;
  49265. #endif
  49266. /* Test failure cases */
  49267. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  49268. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  49269. in = client_key_der_2048;
  49270. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  49271. &in, (long)sizeof_client_key_der_2048));
  49272. in = ecc_clikey_der_256;
  49273. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  49274. &in, (long)sizeof_ecc_clikey_der_256));
  49275. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49276. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  49277. #else
  49278. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  49279. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  49280. EVP_PKEY_free(b);
  49281. b = NULL;
  49282. EVP_PKEY_free(a);
  49283. a = NULL;
  49284. #endif
  49285. /* invalid or empty failure cases */
  49286. a = EVP_PKEY_new();
  49287. b = EVP_PKEY_new();
  49288. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49289. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  49290. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  49291. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  49292. #ifdef NO_RSA
  49293. /* Type check will fail since RSA is the default EVP key type */
  49294. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  49295. #else
  49296. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  49297. #endif
  49298. #else
  49299. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  49300. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  49301. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  49302. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  49303. #endif
  49304. EVP_PKEY_free(b);
  49305. EVP_PKEY_free(a);
  49306. (void)in;
  49307. #endif
  49308. return EXPECT_RESULT();
  49309. }
  49310. static int test_ERR_load_crypto_strings(void)
  49311. {
  49312. #if defined(OPENSSL_ALL)
  49313. ERR_load_crypto_strings();
  49314. return TEST_SUCCESS;
  49315. #else
  49316. return TEST_SKIPPED;
  49317. #endif
  49318. }
  49319. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  49320. static void free_x509(X509* x)
  49321. {
  49322. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  49323. }
  49324. #endif
  49325. static int test_sk_X509(void)
  49326. {
  49327. EXPECT_DECLS;
  49328. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  49329. {
  49330. STACK_OF(X509)* s = NULL;
  49331. ExpectNotNull(s = sk_X509_new_null());
  49332. ExpectIntEQ(sk_X509_num(s), 0);
  49333. sk_X509_pop_free(s, NULL);
  49334. ExpectNotNull(s = sk_X509_new_null());
  49335. ExpectIntEQ(sk_X509_num(s), 0);
  49336. sk_X509_pop_free(s, NULL);
  49337. ExpectNotNull(s = sk_X509_new_null());
  49338. sk_X509_push(s, (X509*)1);
  49339. ExpectIntEQ(sk_X509_num(s), 1);
  49340. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  49341. sk_X509_push(s, (X509*)2);
  49342. ExpectIntEQ(sk_X509_num(s), 2);
  49343. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  49344. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  49345. sk_X509_push(s, (X509*)2);
  49346. sk_X509_pop_free(s, free_x509);
  49347. }
  49348. {
  49349. /* Push a list of 10 X509s onto stack, then verify that
  49350. * value(), push(), shift(), and pop() behave as expected. */
  49351. STACK_OF(X509)* s = NULL;
  49352. X509* xList[10];
  49353. int i = 0;
  49354. const int len = (sizeof(xList) / sizeof(xList[0]));
  49355. for (i = 0; i < len; ++i) {
  49356. xList[i] = NULL;
  49357. ExpectNotNull(xList[i] = X509_new());
  49358. }
  49359. /* test push, pop, and free */
  49360. ExpectNotNull(s = sk_X509_new_null());
  49361. for (i = 0; i < len; ++i) {
  49362. sk_X509_push(s, xList[i]);
  49363. ExpectIntEQ(sk_X509_num(s), i + 1);
  49364. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  49365. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  49366. }
  49367. /* pop returns and removes last pushed on stack, which is index 0
  49368. * in sk_x509_value */
  49369. for (i = 0; i < len; ++i) {
  49370. X509 * x = sk_X509_value(s, 0);
  49371. X509 * y = sk_X509_pop(s);
  49372. X509 * z = xList[len - 1 - i];
  49373. ExpectIntEQ((x == y), 1);
  49374. ExpectIntEQ((x == z), 1);
  49375. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  49376. }
  49377. sk_free(s);
  49378. s = NULL;
  49379. /* test push, shift, and free */
  49380. ExpectNotNull(s = sk_X509_new_null());
  49381. for (i = 0; i < len; ++i) {
  49382. sk_X509_push(s, xList[i]);
  49383. ExpectIntEQ(sk_X509_num(s), i + 1);
  49384. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  49385. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  49386. }
  49387. /* shift returns and removes first pushed on stack, which is index i
  49388. * in sk_x509_value() */
  49389. for (i = 0; i < len; ++i) {
  49390. X509 * x = sk_X509_value(s, len - 1 - i);
  49391. X509 * y = sk_X509_shift(s);
  49392. X509 * z = xList[i];
  49393. ExpectIntEQ((x == y), 1);
  49394. ExpectIntEQ((x == z), 1);
  49395. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  49396. }
  49397. sk_free(s);
  49398. for (i = 0; i < len; ++i)
  49399. X509_free(xList[i]);
  49400. }
  49401. #endif
  49402. return EXPECT_RESULT();
  49403. }
  49404. static int test_sk_X509_CRL(void)
  49405. {
  49406. EXPECT_DECLS;
  49407. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  49408. X509_CRL* crl = NULL;
  49409. XFILE fp = XBADFILE;
  49410. STACK_OF(X509_CRL)* s = NULL;
  49411. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  49412. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49413. NULL, NULL));
  49414. if (fp != XBADFILE)
  49415. XFCLOSE(fp);
  49416. ExpectNotNull(s = sk_X509_CRL_new());
  49417. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  49418. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  49419. if (EXPECT_FAIL()) {
  49420. X509_CRL_free(crl);
  49421. }
  49422. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  49423. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  49424. sk_X509_CRL_free(s);
  49425. #endif
  49426. return EXPECT_RESULT();
  49427. }
  49428. static int test_X509_get_signature_nid(void)
  49429. {
  49430. EXPECT_DECLS;
  49431. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49432. X509* x509 = NULL;
  49433. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  49434. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  49435. SSL_FILETYPE_PEM));
  49436. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  49437. X509_free(x509);
  49438. #endif
  49439. return EXPECT_RESULT();
  49440. }
  49441. static int test_X509_REQ(void)
  49442. {
  49443. EXPECT_DECLS;
  49444. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  49445. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  49446. X509_NAME* name = NULL;
  49447. #ifndef NO_RSA
  49448. X509_NAME* subject = NULL;
  49449. #endif
  49450. #if !defined(NO_RSA) || defined(HAVE_ECC)
  49451. X509_REQ* req = NULL;
  49452. EVP_PKEY* priv = NULL;
  49453. EVP_PKEY* pub = NULL;
  49454. unsigned char* der = NULL;
  49455. int len;
  49456. #endif
  49457. #ifndef NO_RSA
  49458. EVP_MD_CTX *mctx = NULL;
  49459. EVP_PKEY_CTX *pkctx = NULL;
  49460. #ifdef USE_CERT_BUFFERS_1024
  49461. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  49462. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  49463. #elif defined(USE_CERT_BUFFERS_2048)
  49464. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  49465. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  49466. #endif
  49467. #endif
  49468. #ifdef HAVE_ECC
  49469. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  49470. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  49471. #endif
  49472. ExpectNotNull(name = X509_NAME_new());
  49473. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  49474. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  49475. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  49476. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  49477. #ifndef NO_RSA
  49478. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  49479. (long)sizeof_client_key_der_2048));
  49480. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  49481. (long)sizeof_client_keypub_der_2048));
  49482. ExpectNotNull(req = X509_REQ_new());
  49483. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  49484. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  49485. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  49486. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  49487. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  49488. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  49489. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  49490. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  49491. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  49492. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  49493. len = i2d_X509_REQ(req, &der);
  49494. DEBUG_WRITE_DER(der, len, "req.der");
  49495. #ifdef USE_CERT_BUFFERS_1024
  49496. ExpectIntEQ(len, 381);
  49497. #else
  49498. ExpectIntEQ(len, 643);
  49499. #endif
  49500. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  49501. der = NULL;
  49502. mctx = EVP_MD_CTX_new();
  49503. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  49504. WOLFSSL_SUCCESS);
  49505. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  49506. EVP_MD_CTX_free(mctx);
  49507. mctx = NULL;
  49508. X509_REQ_free(NULL);
  49509. X509_REQ_free(req);
  49510. req = NULL;
  49511. /* Test getting the subject from a newly created X509_REQ */
  49512. ExpectNotNull(req = X509_REQ_new());
  49513. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  49514. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  49515. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  49516. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  49517. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  49518. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  49519. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  49520. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  49521. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  49522. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  49523. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  49524. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  49525. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  49526. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  49527. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  49528. len = i2d_X509_REQ(req, &der);
  49529. DEBUG_WRITE_DER(der, len, "req2.der");
  49530. #ifdef USE_CERT_BUFFERS_1024
  49531. ExpectIntEQ(len, 435);
  49532. #else
  49533. ExpectIntEQ(len, 696);
  49534. #endif
  49535. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  49536. der = NULL;
  49537. EVP_PKEY_free(pub);
  49538. pub = NULL;
  49539. EVP_PKEY_free(priv);
  49540. priv = NULL;
  49541. X509_REQ_free(req);
  49542. req = NULL;
  49543. #endif
  49544. #ifdef HAVE_ECC
  49545. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  49546. sizeof_ecc_clikey_der_256));
  49547. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  49548. sizeof_ecc_clikeypub_der_256));
  49549. ExpectNotNull(req = X509_REQ_new());
  49550. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  49551. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  49552. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  49553. /* Signature is random and may be shorter or longer. */
  49554. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  49555. ExpectIntLE(len, 253);
  49556. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  49557. X509_REQ_free(req);
  49558. EVP_PKEY_free(pub);
  49559. EVP_PKEY_free(priv);
  49560. #ifdef FP_ECC
  49561. wc_ecc_fp_free();
  49562. #endif
  49563. #endif /* HAVE_ECC */
  49564. X509_NAME_free(name);
  49565. #endif
  49566. return EXPECT_RESULT();
  49567. }
  49568. static int test_wolfssl_PKCS7(void)
  49569. {
  49570. EXPECT_DECLS;
  49571. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  49572. !defined(NO_RSA)
  49573. PKCS7* pkcs7 = NULL;
  49574. byte data[FOURK_BUF];
  49575. word32 len = sizeof(data);
  49576. const byte* p = data;
  49577. byte content[] = "Test data to encode.";
  49578. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  49579. BIO* bio = NULL;
  49580. byte key[sizeof(client_key_der_2048)];
  49581. word32 keySz = (word32)sizeof(key);
  49582. byte* out = NULL;
  49583. #endif
  49584. ExpectIntGT((len = (word32)CreatePKCS7SignedData(data, (int)len, content,
  49585. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  49586. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, (int)len));
  49587. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  49588. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  49589. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  49590. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  49591. PKCS7_free(pkcs7);
  49592. pkcs7 = NULL;
  49593. /* fail case, without PKCS7_NOVERIFY */
  49594. p = data;
  49595. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  49596. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  49597. 0), WOLFSSL_FAILURE);
  49598. PKCS7_free(pkcs7);
  49599. pkcs7 = NULL;
  49600. /* success case, with PKCS7_NOVERIFY */
  49601. p = data;
  49602. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  49603. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  49604. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  49605. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  49606. /* test i2d */
  49607. XMEMCPY(key, client_key_der_2048, keySz);
  49608. if (pkcs7 != NULL) {
  49609. pkcs7->privateKey = key;
  49610. pkcs7->privateKeySz = (word32)sizeof(key);
  49611. pkcs7->encryptOID = RSAk;
  49612. #ifdef NO_SHA
  49613. pkcs7->hashOID = SHA256h;
  49614. #else
  49615. pkcs7->hashOID = SHAh;
  49616. #endif
  49617. }
  49618. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49619. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  49620. #ifndef NO_ASN_TIME
  49621. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  49622. #else
  49623. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  49624. #endif
  49625. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49626. BIO_free(bio);
  49627. #endif
  49628. PKCS7_free(NULL);
  49629. PKCS7_free(pkcs7);
  49630. #endif
  49631. return EXPECT_RESULT();
  49632. }
  49633. static int test_wolfSSL_PKCS7_sign(void)
  49634. {
  49635. EXPECT_DECLS;
  49636. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  49637. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49638. PKCS7* p7 = NULL;
  49639. PKCS7* p7Ver = NULL;
  49640. byte* out = NULL;
  49641. byte* tmpPtr = NULL;
  49642. int outLen = 0;
  49643. int flags = 0;
  49644. byte data[] = "Test data to encode.";
  49645. const char* cert = "./certs/server-cert.pem";
  49646. const char* key = "./certs/server-key.pem";
  49647. const char* ca = "./certs/ca-cert.pem";
  49648. WOLFSSL_BIO* certBio = NULL;
  49649. WOLFSSL_BIO* keyBio = NULL;
  49650. WOLFSSL_BIO* caBio = NULL;
  49651. WOLFSSL_BIO* inBio = NULL;
  49652. X509* signCert = NULL;
  49653. EVP_PKEY* signKey = NULL;
  49654. X509* caCert = NULL;
  49655. X509_STORE* store = NULL;
  49656. #ifndef NO_PKCS7_STREAM
  49657. int z;
  49658. int ret;
  49659. #endif /* !NO_PKCS7_STREAM */
  49660. /* read signer cert/key into BIO */
  49661. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  49662. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  49663. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  49664. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  49665. /* read CA cert into store (for verify) */
  49666. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  49667. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  49668. ExpectNotNull(store = X509_STORE_new());
  49669. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  49670. /* data to be signed into BIO */
  49671. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  49672. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  49673. /* PKCS7_sign, bad args: signer NULL */
  49674. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  49675. /* PKCS7_sign, bad args: signer key NULL */
  49676. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  49677. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  49678. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  49679. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  49680. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  49681. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  49682. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  49683. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  49684. {
  49685. flags = PKCS7_BINARY;
  49686. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  49687. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  49688. /* verify with d2i_PKCS7 */
  49689. tmpPtr = out;
  49690. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  49691. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  49692. PKCS7_free(p7Ver);
  49693. p7Ver = NULL;
  49694. /* verify with wc_PKCS7_VerifySignedData */
  49695. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49696. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  49697. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  49698. #ifndef NO_PKCS7_STREAM
  49699. /* verify with wc_PKCS7_VerifySignedData streaming */
  49700. wc_PKCS7_Free(p7Ver);
  49701. p7Ver = NULL;
  49702. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49703. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  49704. /* test for streaming */
  49705. ret = -1;
  49706. for (z = 0; z < outLen && ret != 0; z++) {
  49707. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  49708. if (ret < 0){
  49709. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  49710. }
  49711. }
  49712. ExpectIntEQ(ret, 0);
  49713. #endif /* !NO_PKCS7_STREAM */
  49714. /* compare the signer found to expected signer */
  49715. ExpectIntNE(p7Ver->verifyCertSz, 0);
  49716. tmpPtr = NULL;
  49717. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  49718. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  49719. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  49720. tmpPtr = NULL;
  49721. wc_PKCS7_Free(p7Ver);
  49722. p7Ver = NULL;
  49723. ExpectNotNull(out);
  49724. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49725. out = NULL;
  49726. PKCS7_free(p7);
  49727. p7 = NULL;
  49728. }
  49729. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  49730. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  49731. {
  49732. /* re-populate input BIO, may have been consumed */
  49733. BIO_free(inBio);
  49734. inBio = NULL;
  49735. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  49736. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  49737. flags = PKCS7_BINARY | PKCS7_STREAM;
  49738. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  49739. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  49740. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  49741. /* PKCS7_final, bad args: PKCS7 null */
  49742. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  49743. /* PKCS7_final, bad args: PKCS7 null */
  49744. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  49745. tmpPtr = out;
  49746. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  49747. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  49748. PKCS7_free(p7Ver);
  49749. p7Ver = NULL;
  49750. ExpectNotNull(out);
  49751. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49752. out = NULL;
  49753. PKCS7_free(p7);
  49754. p7 = NULL;
  49755. }
  49756. /* TEST SUCCESS: Detached, not streaming, not MIME */
  49757. {
  49758. /* re-populate input BIO, may have been consumed */
  49759. BIO_free(inBio);
  49760. inBio = NULL;
  49761. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  49762. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  49763. flags = PKCS7_BINARY | PKCS7_DETACHED;
  49764. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  49765. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  49766. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  49767. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49768. if (p7Ver != NULL) {
  49769. p7Ver->content = data;
  49770. p7Ver->contentSz = sizeof(data);
  49771. }
  49772. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  49773. wc_PKCS7_Free(p7Ver);
  49774. p7Ver = NULL;
  49775. #ifndef NO_PKCS7_STREAM
  49776. /* verify with wc_PKCS7_VerifySignedData streaming */
  49777. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49778. if (p7Ver != NULL) {
  49779. p7Ver->content = data;
  49780. p7Ver->contentSz = sizeof(data);
  49781. }
  49782. /* test for streaming */
  49783. ret = -1;
  49784. for (z = 0; z < outLen && ret != 0; z++) {
  49785. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  49786. if (ret < 0){
  49787. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  49788. }
  49789. }
  49790. ExpectIntEQ(ret, 0);
  49791. wc_PKCS7_Free(p7Ver);
  49792. p7Ver = NULL;
  49793. #endif /* !NO_PKCS7_STREAM */
  49794. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  49795. * yet support detached content */
  49796. tmpPtr = out;
  49797. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  49798. PKCS7_free(p7Ver);
  49799. p7Ver = NULL;
  49800. ExpectNotNull(out);
  49801. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49802. out = NULL;
  49803. PKCS7_free(p7);
  49804. p7 = NULL;
  49805. }
  49806. /* TEST SUCCESS: Detached, streaming, not MIME */
  49807. {
  49808. /* re-populate input BIO, may have been consumed */
  49809. BIO_free(inBio);
  49810. inBio = NULL;
  49811. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  49812. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  49813. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  49814. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  49815. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  49816. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  49817. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  49818. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49819. if (p7Ver != NULL) {
  49820. p7Ver->content = data;
  49821. p7Ver->contentSz = sizeof(data);
  49822. }
  49823. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  49824. wc_PKCS7_Free(p7Ver);
  49825. p7Ver = NULL;
  49826. ExpectNotNull(out);
  49827. #ifndef NO_PKCS7_STREAM
  49828. /* verify with wc_PKCS7_VerifySignedData streaming */
  49829. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49830. if (p7Ver != NULL) {
  49831. p7Ver->content = data;
  49832. p7Ver->contentSz = sizeof(data);
  49833. }
  49834. /* test for streaming */
  49835. ret = -1;
  49836. for (z = 0; z < outLen && ret != 0; z++) {
  49837. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  49838. if (ret < 0){
  49839. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  49840. }
  49841. }
  49842. ExpectIntEQ(ret, 0);
  49843. ExpectNotNull(out);
  49844. wc_PKCS7_Free(p7Ver);
  49845. p7Ver = NULL;
  49846. #endif /* !NO_PKCS7_STREAM */
  49847. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49848. PKCS7_free(p7);
  49849. p7 = NULL;
  49850. }
  49851. X509_STORE_free(store);
  49852. X509_free(caCert);
  49853. X509_free(signCert);
  49854. EVP_PKEY_free(signKey);
  49855. BIO_free(inBio);
  49856. BIO_free(keyBio);
  49857. BIO_free(certBio);
  49858. BIO_free(caBio);
  49859. #endif
  49860. return EXPECT_RESULT();
  49861. }
  49862. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  49863. {
  49864. EXPECT_DECLS;
  49865. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  49866. PKCS7_SIGNED* pkcs7 = NULL;
  49867. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  49868. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  49869. PKCS7_SIGNED_free(pkcs7);
  49870. #endif
  49871. return EXPECT_RESULT();
  49872. }
  49873. #ifndef NO_BIO
  49874. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  49875. {
  49876. EXPECT_DECLS;
  49877. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  49878. PKCS7* pkcs7 = NULL;
  49879. BIO* bio = NULL;
  49880. const byte* cert_buf = NULL;
  49881. int ret = 0;
  49882. WC_RNG rng;
  49883. const byte data[] = { /* Hello World */
  49884. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  49885. 0x72,0x6c,0x64
  49886. };
  49887. #ifndef NO_RSA
  49888. #if defined(USE_CERT_BUFFERS_2048)
  49889. byte key[sizeof(client_key_der_2048)];
  49890. byte cert[sizeof(client_cert_der_2048)];
  49891. word32 keySz = (word32)sizeof(key);
  49892. word32 certSz = (word32)sizeof(cert);
  49893. XMEMSET(key, 0, keySz);
  49894. XMEMSET(cert, 0, certSz);
  49895. XMEMCPY(key, client_key_der_2048, keySz);
  49896. XMEMCPY(cert, client_cert_der_2048, certSz);
  49897. #elif defined(USE_CERT_BUFFERS_1024)
  49898. byte key[sizeof_client_key_der_1024];
  49899. byte cert[sizeof(sizeof_client_cert_der_1024)];
  49900. word32 keySz = (word32)sizeof(key);
  49901. word32 certSz = (word32)sizeof(cert);
  49902. XMEMSET(key, 0, keySz);
  49903. XMEMSET(cert, 0, certSz);
  49904. XMEMCPY(key, client_key_der_1024, keySz);
  49905. XMEMCPY(cert, client_cert_der_1024, certSz);
  49906. #else
  49907. unsigned char cert[ONEK_BUF];
  49908. unsigned char key[ONEK_BUF];
  49909. XFILE fp = XBADFILE;
  49910. int certSz;
  49911. int keySz;
  49912. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  49913. XBADFILE);
  49914. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  49915. fp), 0);
  49916. if (fp != XBADFILE) {
  49917. XFCLOSE(fp);
  49918. fp = XBADFILE;
  49919. }
  49920. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  49921. XBADFILE);
  49922. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  49923. 0);
  49924. if (fp != XBADFILE) {
  49925. XFCLOSE(fp);
  49926. fp = XBADFILE;
  49927. }
  49928. #endif
  49929. #elif defined(HAVE_ECC)
  49930. #if defined(USE_CERT_BUFFERS_256)
  49931. unsigned char cert[sizeof(cliecc_cert_der_256)];
  49932. unsigned char key[sizeof(ecc_clikey_der_256)];
  49933. int certSz = (int)sizeof(cert);
  49934. int keySz = (int)sizeof(key);
  49935. XMEMSET(cert, 0, certSz);
  49936. XMEMSET(key, 0, keySz);
  49937. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  49938. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  49939. #else
  49940. unsigned char cert[ONEK_BUF];
  49941. unsigned char key[ONEK_BUF];
  49942. XFILE fp = XBADFILE;
  49943. int certSz, keySz;
  49944. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  49945. XBADFILE);
  49946. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  49947. fp), 0);
  49948. if (fp != XBADFILE) {
  49949. XFCLOSE(fp);
  49950. fp = XBADFILE;
  49951. }
  49952. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  49953. XBADFILE);
  49954. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  49955. 0);
  49956. if (fp != XBADFILE) {
  49957. XFCLOSE(fp);
  49958. fp = XBADFILE;
  49959. }
  49960. #endif
  49961. #else
  49962. #error PKCS7 requires ECC or RSA
  49963. #endif
  49964. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  49965. /* initialize with DER encoded cert */
  49966. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  49967. /* init rng */
  49968. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49969. ExpectIntEQ(wc_InitRng(&rng), 0);
  49970. if (pkcs7 != NULL) {
  49971. pkcs7->rng = &rng;
  49972. pkcs7->content = (byte*)data; /* not used for ex */
  49973. pkcs7->contentSz = (word32)sizeof(data);
  49974. pkcs7->contentOID = SIGNED_DATA;
  49975. pkcs7->privateKey = key;
  49976. pkcs7->privateKeySz = (word32)sizeof(key);
  49977. pkcs7->encryptOID = RSAk;
  49978. #ifdef NO_SHA
  49979. pkcs7->hashOID = SHA256h;
  49980. #else
  49981. pkcs7->hashOID = SHAh;
  49982. #endif
  49983. pkcs7->signedAttribs = NULL;
  49984. pkcs7->signedAttribsSz = 0;
  49985. }
  49986. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49987. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  49988. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  49989. /* Read PKCS#7 PEM from BIO */
  49990. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  49991. ExpectIntGE(ret, 0);
  49992. BIO_free(bio);
  49993. wc_PKCS7_Free(pkcs7);
  49994. wc_FreeRng(&rng);
  49995. #endif
  49996. return EXPECT_RESULT();
  49997. }
  49998. #ifdef HAVE_SMIME
  49999. /* // NOLINTBEGIN(clang-analyzer-unix.Stream) */
  50000. static int test_wolfSSL_SMIME_read_PKCS7(void)
  50001. {
  50002. EXPECT_DECLS;
  50003. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  50004. !defined(NO_RSA)
  50005. PKCS7* pkcs7 = NULL;
  50006. BIO* bio = NULL;
  50007. BIO* bcont = NULL;
  50008. BIO* out = NULL;
  50009. const byte* outBuf = NULL;
  50010. int outBufLen = 0;
  50011. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  50012. XFILE smimeTestFile = XBADFILE;
  50013. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  50014. XBADFILE);
  50015. /* smime-test.p7s */
  50016. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  50017. ExpectNotNull(bio);
  50018. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  50019. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  50020. ExpectNotNull(pkcs7);
  50021. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  50022. PKCS7_NOVERIFY), SSL_SUCCESS);
  50023. if (smimeTestFile != XBADFILE) {
  50024. XFCLOSE(smimeTestFile);
  50025. smimeTestFile = XBADFILE;
  50026. }
  50027. if (bcont) BIO_free(bcont);
  50028. bcont = NULL;
  50029. wolfSSL_PKCS7_free(pkcs7);
  50030. pkcs7 = NULL;
  50031. /* smime-test-multipart.p7s */
  50032. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  50033. ExpectFalse(smimeTestFile == XBADFILE);
  50034. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  50035. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  50036. ExpectNotNull(pkcs7);
  50037. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  50038. PKCS7_NOVERIFY), SSL_SUCCESS);
  50039. if (smimeTestFile != XBADFILE) {
  50040. XFCLOSE(smimeTestFile);
  50041. smimeTestFile = XBADFILE;
  50042. }
  50043. if (bcont) BIO_free(bcont);
  50044. bcont = NULL;
  50045. wolfSSL_PKCS7_free(pkcs7);
  50046. pkcs7 = NULL;
  50047. /* smime-test-multipart-badsig.p7s */
  50048. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  50049. ExpectFalse(smimeTestFile == XBADFILE);
  50050. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  50051. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  50052. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  50053. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  50054. PKCS7_NOVERIFY), SSL_FAILURE);
  50055. if (smimeTestFile != XBADFILE) {
  50056. XFCLOSE(smimeTestFile);
  50057. smimeTestFile = XBADFILE;
  50058. }
  50059. if (bcont) BIO_free(bcont);
  50060. bcont = NULL;
  50061. wolfSSL_PKCS7_free(pkcs7);
  50062. pkcs7 = NULL;
  50063. /* smime-test-canon.p7s */
  50064. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  50065. ExpectFalse(smimeTestFile == XBADFILE);
  50066. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  50067. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  50068. ExpectNotNull(pkcs7);
  50069. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  50070. PKCS7_NOVERIFY), SSL_SUCCESS);
  50071. if (smimeTestFile != XBADFILE) {
  50072. XFCLOSE(smimeTestFile);
  50073. smimeTestFile = XBADFILE;
  50074. }
  50075. if (bcont) BIO_free(bcont);
  50076. bcont = NULL;
  50077. wolfSSL_PKCS7_free(pkcs7);
  50078. pkcs7 = NULL;
  50079. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  50080. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  50081. ExpectFalse(smimeTestFile == XBADFILE);
  50082. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  50083. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  50084. ExpectNotNull(pkcs7);
  50085. out = wolfSSL_BIO_new(BIO_s_mem());
  50086. ExpectNotNull(out);
  50087. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  50088. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  50089. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  50090. /* Content-Type should not show up at beginning of output buffer */
  50091. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  50092. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  50093. BIO_free(out);
  50094. BIO_free(bio);
  50095. if (bcont) BIO_free(bcont);
  50096. wolfSSL_PKCS7_free(pkcs7);
  50097. #endif
  50098. return EXPECT_RESULT();
  50099. }
  50100. /* // NOLINTEND(clang-analyzer-unix.Stream) */
  50101. static int test_wolfSSL_SMIME_write_PKCS7(void)
  50102. {
  50103. EXPECT_DECLS;
  50104. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  50105. PKCS7* p7 = NULL;
  50106. PKCS7* p7Ver = NULL;
  50107. int flags = 0;
  50108. byte data[] = "Test data to encode.";
  50109. const char* cert = "./certs/server-cert.pem";
  50110. const char* key = "./certs/server-key.pem";
  50111. const char* ca = "./certs/ca-cert.pem";
  50112. WOLFSSL_BIO* certBio = NULL;
  50113. WOLFSSL_BIO* keyBio = NULL;
  50114. WOLFSSL_BIO* caBio = NULL;
  50115. WOLFSSL_BIO* inBio = NULL;
  50116. WOLFSSL_BIO* outBio = NULL;
  50117. WOLFSSL_BIO* content = NULL;
  50118. X509* signCert = NULL;
  50119. EVP_PKEY* signKey = NULL;
  50120. X509* caCert = NULL;
  50121. X509_STORE* store = NULL;
  50122. /* read signer cert/key into BIO */
  50123. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  50124. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  50125. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  50126. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  50127. /* read CA cert into store (for verify) */
  50128. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  50129. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  50130. ExpectNotNull(store = X509_STORE_new());
  50131. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  50132. /* generate and verify SMIME: not detached */
  50133. {
  50134. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  50135. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  50136. flags = PKCS7_STREAM;
  50137. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  50138. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  50139. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  50140. /* bad arg: out NULL */
  50141. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  50142. /* bad arg: pkcs7 NULL */
  50143. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  50144. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  50145. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  50146. BIO_free(content);
  50147. content = NULL;
  50148. BIO_free(inBio);
  50149. inBio = NULL;
  50150. BIO_free(outBio);
  50151. outBio = NULL;
  50152. PKCS7_free(p7Ver);
  50153. p7Ver = NULL;
  50154. PKCS7_free(p7);
  50155. p7 = NULL;
  50156. }
  50157. /* generate and verify SMIME: not detached, add Content-Type */
  50158. {
  50159. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  50160. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  50161. flags = PKCS7_STREAM | PKCS7_TEXT;
  50162. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  50163. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  50164. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  50165. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  50166. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  50167. BIO_free(content);
  50168. content = NULL;
  50169. BIO_free(inBio);
  50170. inBio = NULL;
  50171. BIO_free(outBio);
  50172. outBio = NULL;
  50173. PKCS7_free(p7Ver);
  50174. p7Ver = NULL;
  50175. PKCS7_free(p7);
  50176. p7 = NULL;
  50177. }
  50178. /* generate and verify SMIME: detached */
  50179. {
  50180. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  50181. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  50182. flags = PKCS7_DETACHED | PKCS7_STREAM;
  50183. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  50184. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  50185. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  50186. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  50187. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  50188. BIO_free(content);
  50189. content = NULL;
  50190. BIO_free(inBio);
  50191. inBio = NULL;
  50192. BIO_free(outBio);
  50193. outBio = NULL;
  50194. PKCS7_free(p7Ver);
  50195. p7Ver = NULL;
  50196. PKCS7_free(p7);
  50197. p7 = NULL;
  50198. }
  50199. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  50200. {
  50201. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  50202. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  50203. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  50204. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  50205. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  50206. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  50207. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  50208. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  50209. BIO_free(content);
  50210. content = NULL;
  50211. BIO_free(inBio);
  50212. inBio = NULL;
  50213. BIO_free(outBio);
  50214. outBio = NULL;
  50215. PKCS7_free(p7Ver);
  50216. p7Ver = NULL;
  50217. PKCS7_free(p7);
  50218. p7 = NULL;
  50219. }
  50220. X509_STORE_free(store);
  50221. X509_free(caCert);
  50222. X509_free(signCert);
  50223. EVP_PKEY_free(signKey);
  50224. BIO_free(keyBio);
  50225. BIO_free(certBio);
  50226. BIO_free(caBio);
  50227. #endif
  50228. return EXPECT_RESULT();
  50229. }
  50230. #endif /* HAVE_SMIME */
  50231. #endif /* !NO_BIO */
  50232. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  50233. * returns 0) */
  50234. static int test_X509_STORE_No_SSL_CTX(void)
  50235. {
  50236. EXPECT_DECLS;
  50237. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  50238. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  50239. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  50240. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  50241. defined(HAVE_CRL) && !defined(NO_RSA)
  50242. X509_STORE * store = NULL;
  50243. X509_STORE_CTX * storeCtx = NULL;
  50244. X509_CRL * crl = NULL;
  50245. X509 * ca = NULL;
  50246. X509 * cert = NULL;
  50247. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  50248. const char srvCert[] = "./certs/server-cert.pem";
  50249. const char caCert[] = "./certs/ca-cert.pem";
  50250. const char caDir[] = "./certs/crl/hash_pem";
  50251. XFILE fp = XBADFILE;
  50252. X509_LOOKUP * lookup = NULL;
  50253. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  50254. /* Set up store with CA */
  50255. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  50256. SSL_FILETYPE_PEM)));
  50257. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  50258. /* Add CRL lookup directory to store
  50259. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  50260. * of crl.pem */
  50261. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  50262. X509_LOOKUP_hash_dir())));
  50263. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  50264. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  50265. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  50266. SSL_SUCCESS);
  50267. /* Add CRL to store NOT containing the verified certificate, which
  50268. * forces use of the CRL lookup directory */
  50269. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  50270. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50271. NULL, NULL));
  50272. if (fp != XBADFILE)
  50273. XFCLOSE(fp);
  50274. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  50275. /* Create verification context outside of an SSL session */
  50276. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  50277. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  50278. SSL_FILETYPE_PEM)));
  50279. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  50280. /* Perform verification, which should NOT indicate CRL missing due to the
  50281. * store CM's X509 store pointer being NULL */
  50282. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  50283. X509_CRL_free(crl);
  50284. X509_STORE_free(store);
  50285. X509_STORE_CTX_free(storeCtx);
  50286. X509_free(cert);
  50287. X509_free(ca);
  50288. #endif
  50289. return EXPECT_RESULT();
  50290. }
  50291. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  50292. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  50293. static int test_X509_LOOKUP_add_dir(void)
  50294. {
  50295. EXPECT_DECLS;
  50296. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  50297. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  50298. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  50299. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  50300. defined(HAVE_CRL) && !defined(NO_RSA)
  50301. X509_STORE * store = NULL;
  50302. X509_STORE_CTX * storeCtx = NULL;
  50303. X509_CRL * crl = NULL;
  50304. X509 * ca = NULL;
  50305. X509 * cert = NULL;
  50306. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  50307. const char srvCert[] = "./certs/server-cert.pem";
  50308. const char caCert[] = "./certs/ca-cert.pem";
  50309. const char caDir[] = "./certs/crl/hash_der";
  50310. XFILE fp = XBADFILE;
  50311. X509_LOOKUP * lookup = NULL;
  50312. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  50313. /* Set up store with CA */
  50314. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  50315. SSL_FILETYPE_PEM)));
  50316. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  50317. /* Add CRL lookup directory to store.
  50318. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  50319. * of crl.der */
  50320. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  50321. X509_LOOKUP_hash_dir())));
  50322. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  50323. SSL_SUCCESS);
  50324. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  50325. SSL_SUCCESS);
  50326. /* Add CRL to store NOT containing the verified certificate, which
  50327. * forces use of the CRL lookup directory */
  50328. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  50329. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50330. NULL, NULL));
  50331. if (fp != XBADFILE) {
  50332. XFCLOSE(fp);
  50333. fp = XBADFILE;
  50334. }
  50335. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  50336. /* Create verification context outside of an SSL session */
  50337. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  50338. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  50339. SSL_FILETYPE_PEM)));
  50340. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  50341. /* Perform verification, which should NOT return CRL missing */
  50342. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  50343. X509_CRL_free(crl);
  50344. crl = NULL;
  50345. X509_STORE_free(store);
  50346. store = NULL;
  50347. X509_STORE_CTX_free(storeCtx);
  50348. storeCtx = NULL;
  50349. X509_free(cert);
  50350. cert = NULL;
  50351. X509_free(ca);
  50352. ca = NULL;
  50353. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  50354. * We should get CRL_MISSING at the end, because the lookup
  50355. * dir has only ASN1 CRLs. */
  50356. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  50357. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  50358. SSL_FILETYPE_PEM)));
  50359. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  50360. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  50361. X509_LOOKUP_hash_dir())));
  50362. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  50363. SSL_SUCCESS);
  50364. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  50365. SSL_SUCCESS);
  50366. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  50367. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50368. NULL, NULL));
  50369. if (fp != XBADFILE) {
  50370. XFCLOSE(fp);
  50371. fp = XBADFILE;
  50372. }
  50373. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  50374. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  50375. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  50376. SSL_FILETYPE_PEM)));
  50377. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  50378. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  50379. * in dir containing only ASN1/DER. */
  50380. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  50381. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  50382. X509_V_ERR_UNABLE_TO_GET_CRL);
  50383. X509_CRL_free(crl);
  50384. X509_STORE_free(store);
  50385. X509_STORE_CTX_free(storeCtx);
  50386. X509_free(cert);
  50387. X509_free(ca);
  50388. #endif
  50389. return EXPECT_RESULT();
  50390. }
  50391. /*----------------------------------------------------------------------------*
  50392. | Certificate Failure Checks
  50393. *----------------------------------------------------------------------------*/
  50394. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  50395. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  50396. #if !defined(NO_RSA) || defined(HAVE_ECC)
  50397. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  50398. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  50399. int type)
  50400. {
  50401. int ret;
  50402. WOLFSSL_CERT_MANAGER* cm = NULL;
  50403. switch (type) {
  50404. case TESTING_RSA:
  50405. #ifdef NO_RSA
  50406. fprintf(stderr, "RSA disabled, skipping test\n");
  50407. return ASN_SIG_CONFIRM_E;
  50408. #else
  50409. break;
  50410. #endif
  50411. case TESTING_ECC:
  50412. #ifndef HAVE_ECC
  50413. fprintf(stderr, "ECC disabled, skipping test\n");
  50414. return ASN_SIG_CONFIRM_E;
  50415. #else
  50416. break;
  50417. #endif
  50418. default:
  50419. fprintf(stderr, "Bad function argument\n");
  50420. return BAD_FUNC_ARG;
  50421. }
  50422. cm = wolfSSL_CertManagerNew();
  50423. if (cm == NULL) {
  50424. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  50425. return -1;
  50426. }
  50427. #ifndef NO_FILESYSTEM
  50428. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  50429. if (ret != WOLFSSL_SUCCESS) {
  50430. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  50431. wolfSSL_CertManagerFree(cm);
  50432. return ret;
  50433. }
  50434. #else
  50435. (void)ca;
  50436. #endif
  50437. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, (long int)cert_sz,
  50438. WOLFSSL_FILETYPE_ASN1);
  50439. /* Let ExpectIntEQ handle return code */
  50440. wolfSSL_CertManagerFree(cm);
  50441. return ret;
  50442. }
  50443. #endif
  50444. #if !defined(NO_FILESYSTEM)
  50445. static int test_RsaSigFailure_cm(void)
  50446. {
  50447. EXPECT_DECLS;
  50448. #ifndef NO_RSA
  50449. const char* ca_cert = "./certs/ca-cert.pem";
  50450. const char* server_cert = "./certs/server-cert.der";
  50451. byte* cert_buf = NULL;
  50452. size_t cert_sz = 0;
  50453. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  50454. if (cert_buf != NULL) {
  50455. /* corrupt DER - invert last byte, which is signature */
  50456. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  50457. /* test bad cert */
  50458. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  50459. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  50460. WOLFSSL_FATAL_ERROR);
  50461. #else
  50462. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  50463. ASN_SIG_CONFIRM_E);
  50464. #endif
  50465. }
  50466. /* load_file() uses malloc. */
  50467. if (cert_buf != NULL) {
  50468. free(cert_buf);
  50469. }
  50470. #endif /* !NO_RSA */
  50471. return EXPECT_RESULT();
  50472. }
  50473. static int test_EccSigFailure_cm(void)
  50474. {
  50475. EXPECT_DECLS;
  50476. #ifdef HAVE_ECC
  50477. /* self-signed ECC cert, so use server cert as CA */
  50478. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  50479. const char* server_cert = "./certs/server-ecc.der";
  50480. byte* cert_buf = NULL;
  50481. size_t cert_sz = 0;
  50482. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  50483. if (cert_buf != NULL) {
  50484. /* corrupt DER - invert last byte, which is signature */
  50485. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  50486. /* test bad cert */
  50487. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  50488. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  50489. WOLFSSL_FATAL_ERROR);
  50490. #else
  50491. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  50492. ASN_SIG_CONFIRM_E);
  50493. #endif
  50494. }
  50495. /* load_file() uses malloc. */
  50496. if (cert_buf != NULL) {
  50497. free(cert_buf);
  50498. }
  50499. #ifdef FP_ECC
  50500. wc_ecc_fp_free();
  50501. #endif
  50502. #endif /* HAVE_ECC */
  50503. return EXPECT_RESULT();
  50504. }
  50505. #endif /* !NO_FILESYSTEM */
  50506. #endif /* NO_CERTS */
  50507. #ifdef WOLFSSL_TLS13
  50508. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  50509. #ifdef WC_SHA384_DIGEST_SIZE
  50510. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  50511. #else
  50512. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  50513. #endif
  50514. #endif
  50515. #ifdef WOLFSSL_EARLY_DATA
  50516. static const char earlyData[] = "Early Data";
  50517. static char earlyDataBuffer[1];
  50518. #endif
  50519. static int test_tls13_apis(void)
  50520. {
  50521. EXPECT_DECLS;
  50522. int ret;
  50523. #ifndef WOLFSSL_NO_TLS12
  50524. #ifndef NO_WOLFSSL_CLIENT
  50525. WOLFSSL_CTX* clientTls12Ctx = NULL;
  50526. WOLFSSL* clientTls12Ssl = NULL;
  50527. #endif
  50528. #ifndef NO_WOLFSSL_SERVER
  50529. WOLFSSL_CTX* serverTls12Ctx = NULL;
  50530. WOLFSSL* serverTls12Ssl = NULL;
  50531. #endif
  50532. #endif
  50533. #ifndef NO_WOLFSSL_CLIENT
  50534. WOLFSSL_CTX* clientCtx = NULL;
  50535. WOLFSSL* clientSsl = NULL;
  50536. #endif
  50537. #ifndef NO_WOLFSSL_SERVER
  50538. WOLFSSL_CTX* serverCtx = NULL;
  50539. WOLFSSL* serverSsl = NULL;
  50540. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  50541. const char* ourCert = svrCertFile;
  50542. const char* ourKey = svrKeyFile;
  50543. #endif
  50544. #endif
  50545. int required;
  50546. #ifdef WOLFSSL_EARLY_DATA
  50547. int outSz;
  50548. #endif
  50549. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  50550. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  50551. #ifdef WOLFSSL_HAVE_KYBER
  50552. WOLFSSL_KYBER_LEVEL1
  50553. #else
  50554. WOLFSSL_ECC_SECP256R1
  50555. #endif
  50556. };
  50557. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  50558. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  50559. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  50560. int numGroups = 2;
  50561. #endif
  50562. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  50563. char groupList[] =
  50564. #ifdef HAVE_CURVE25519
  50565. "X25519:"
  50566. #endif
  50567. #ifdef HAVE_CURVE448
  50568. "X448:"
  50569. #endif
  50570. #ifndef NO_ECC_SECP
  50571. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  50572. "P-521:secp521r1:"
  50573. #endif
  50574. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  50575. "P-384:secp384r1:"
  50576. #endif
  50577. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  50578. "P-256:secp256r1"
  50579. #if defined(WOLFSSL_HAVE_KYBER)
  50580. ":P256_KYBER_LEVEL1"
  50581. #endif
  50582. #endif
  50583. #endif /* !defined(NO_ECC_SECP) */
  50584. #if defined(WOLFSSL_HAVE_KYBER)
  50585. ":KYBER_LEVEL1"
  50586. #endif
  50587. "";
  50588. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  50589. (void)ret;
  50590. #ifndef WOLFSSL_NO_TLS12
  50591. #ifndef NO_WOLFSSL_CLIENT
  50592. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  50593. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  50594. #endif
  50595. #ifndef NO_WOLFSSL_SERVER
  50596. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  50597. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  50598. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  50599. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  50600. WOLFSSL_FILETYPE_PEM);
  50601. #endif
  50602. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  50603. #endif
  50604. #endif
  50605. #ifndef NO_WOLFSSL_CLIENT
  50606. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  50607. clientSsl = wolfSSL_new(clientCtx);
  50608. #endif
  50609. #ifndef NO_WOLFSSL_SERVER
  50610. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  50611. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  50612. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  50613. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  50614. #endif
  50615. serverSsl = wolfSSL_new(serverCtx);
  50616. ExpectNotNull(serverSsl);
  50617. #endif
  50618. #ifdef WOLFSSL_SEND_HRR_COOKIE
  50619. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  50620. #ifndef NO_WOLFSSL_CLIENT
  50621. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  50622. #endif
  50623. #ifndef NO_WOLFSSL_SERVER
  50624. #ifndef WOLFSSL_NO_TLS12
  50625. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  50626. BAD_FUNC_ARG);
  50627. #endif
  50628. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  50629. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  50630. WOLFSSL_SUCCESS);
  50631. #endif
  50632. #endif
  50633. #ifdef HAVE_SUPPORTED_CURVES
  50634. #ifdef HAVE_ECC
  50635. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  50636. BAD_FUNC_ARG);
  50637. #ifndef NO_WOLFSSL_SERVER
  50638. do {
  50639. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  50640. #ifdef WOLFSSL_ASYNC_CRYPT
  50641. if (ret == WC_PENDING_E)
  50642. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  50643. #endif
  50644. }
  50645. while (ret == WC_PENDING_E);
  50646. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  50647. #endif
  50648. #ifndef NO_WOLFSSL_CLIENT
  50649. #ifndef WOLFSSL_NO_TLS12
  50650. do {
  50651. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  50652. #ifdef WOLFSSL_ASYNC_CRYPT
  50653. if (ret == WC_PENDING_E)
  50654. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  50655. #endif
  50656. }
  50657. while (ret == WC_PENDING_E);
  50658. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  50659. #endif
  50660. do {
  50661. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  50662. #ifdef WOLFSSL_ASYNC_CRYPT
  50663. if (ret == WC_PENDING_E)
  50664. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  50665. #endif
  50666. }
  50667. while (ret == WC_PENDING_E);
  50668. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  50669. #endif
  50670. #elif defined(HAVE_CURVE25519)
  50671. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  50672. #ifndef NO_WOLFSSL_SERVER
  50673. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  50674. WOLFSSL_SUCCESS);
  50675. #endif
  50676. #ifndef NO_WOLFSSL_CLIENT
  50677. #ifndef WOLFSSL_NO_TLS12
  50678. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  50679. WOLFSSL_SUCCESS);
  50680. #endif
  50681. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  50682. WOLFSSL_SUCCESS);
  50683. #endif
  50684. #elif defined(HAVE_CURVE448)
  50685. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  50686. #ifndef NO_WOLFSSL_SERVER
  50687. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  50688. WOLFSSL_SUCCESS);
  50689. #endif
  50690. #ifndef NO_WOLFSSL_CLIENT
  50691. #ifndef WOLFSSL_NO_TLS12
  50692. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  50693. WOLFSSL_SUCCESS);
  50694. #endif
  50695. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  50696. WOLFSSL_SUCCESS);
  50697. #endif
  50698. #else
  50699. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  50700. BAD_FUNC_ARG);
  50701. #ifndef NO_WOLFSSL_CLIENT
  50702. #ifndef WOLFSSL_NO_TLS12
  50703. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  50704. NOT_COMPILED_IN);
  50705. #endif
  50706. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  50707. NOT_COMPILED_IN);
  50708. #endif
  50709. #endif
  50710. #if defined(WOLFSSL_HAVE_KYBER)
  50711. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  50712. #ifndef NO_WOLFSSL_SERVER
  50713. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  50714. WOLFSSL_SUCCESS);
  50715. #endif
  50716. #ifndef NO_WOLFSSL_CLIENT
  50717. #ifndef WOLFSSL_NO_TLS12
  50718. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  50719. BAD_FUNC_ARG);
  50720. #endif
  50721. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  50722. WOLFSSL_SUCCESS);
  50723. #endif
  50724. #endif
  50725. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  50726. #ifndef NO_WOLFSSL_SERVER
  50727. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  50728. #endif
  50729. #ifndef NO_WOLFSSL_CLIENT
  50730. #ifndef WOLFSSL_NO_TLS12
  50731. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  50732. #endif
  50733. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  50734. #endif
  50735. #endif /* HAVE_SUPPORTED_CURVES */
  50736. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  50737. #ifndef NO_WOLFSSL_CLIENT
  50738. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  50739. #endif
  50740. #ifndef NO_WOLFSSL_SERVER
  50741. #ifndef WOLFSSL_NO_TLS12
  50742. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  50743. #endif
  50744. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  50745. #endif
  50746. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  50747. #ifndef NO_WOLFSSL_CLIENT
  50748. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  50749. #endif
  50750. #ifndef NO_WOLFSSL_SERVER
  50751. #ifndef WOLFSSL_NO_TLS12
  50752. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  50753. #endif
  50754. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  50755. #endif
  50756. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  50757. #ifndef NO_WOLFSSL_CLIENT
  50758. #ifndef WOLFSSL_NO_TLS12
  50759. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  50760. #endif
  50761. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  50762. #endif
  50763. #ifndef NO_WOLFSSL_SERVER
  50764. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  50765. #endif
  50766. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  50767. #ifndef NO_WOLFSSL_CLIENT
  50768. #ifndef WOLFSSL_NO_TLS12
  50769. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  50770. #endif
  50771. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  50772. #endif
  50773. #ifndef NO_WOLFSSL_SERVER
  50774. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  50775. #endif
  50776. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  50777. #ifndef NO_WOLFSSL_CLIENT
  50778. #ifndef WOLFSSL_NO_TLS12
  50779. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  50780. #endif
  50781. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  50782. #endif
  50783. #ifndef NO_WOLFSSL_SERVER
  50784. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  50785. #endif
  50786. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  50787. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  50788. #ifndef NO_WOLFSSL_CLIENT
  50789. #ifndef WOLFSSL_NO_TLS12
  50790. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  50791. BAD_FUNC_ARG);
  50792. #endif
  50793. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  50794. #endif
  50795. #ifndef NO_WOLFSSL_SERVER
  50796. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  50797. #endif
  50798. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  50799. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  50800. #ifndef NO_WOLFSSL_SERVER
  50801. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  50802. #endif
  50803. #ifndef NO_WOLFSSL_CLIENT
  50804. #ifndef WOLFSSL_NO_TLS12
  50805. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  50806. BAD_FUNC_ARG);
  50807. #endif
  50808. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  50809. #endif
  50810. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  50811. #ifndef NO_WOLFSSL_SERVER
  50812. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  50813. #endif
  50814. #ifndef NO_WOLFSSL_CLIENT
  50815. #ifndef WOLFSSL_NO_TLS12
  50816. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  50817. BAD_FUNC_ARG);
  50818. #endif
  50819. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  50820. #endif
  50821. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  50822. #ifndef NO_WOLFSSL_CLIENT
  50823. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  50824. #endif
  50825. #ifndef NO_WOLFSSL_SERVER
  50826. #ifndef WOLFSSL_NO_TLS12
  50827. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  50828. BAD_FUNC_ARG);
  50829. #endif
  50830. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  50831. #endif
  50832. #endif
  50833. #ifdef HAVE_ECC
  50834. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  50835. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  50836. #ifndef NO_WOLFSSL_SERVER
  50837. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  50838. #endif
  50839. #ifndef NO_WOLFSSL_CLIENT
  50840. #ifndef WOLFSSL_NO_TLS12
  50841. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  50842. #endif
  50843. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  50844. #endif
  50845. #endif
  50846. #ifdef HAVE_SUPPORTED_CURVES
  50847. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  50848. #ifndef NO_WOLFSSL_CLIENT
  50849. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  50850. #endif
  50851. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  50852. #ifndef NO_WOLFSSL_CLIENT
  50853. #ifndef WOLFSSL_NO_TLS12
  50854. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  50855. WOLFSSL_SUCCESS);
  50856. #endif
  50857. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  50858. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  50859. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  50860. WOLFSSL_SUCCESS);
  50861. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  50862. BAD_FUNC_ARG);
  50863. #endif
  50864. #ifndef NO_WOLFSSL_SERVER
  50865. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  50866. WOLFSSL_SUCCESS);
  50867. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  50868. BAD_FUNC_ARG);
  50869. #endif
  50870. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  50871. #ifndef NO_WOLFSSL_CLIENT
  50872. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  50873. #endif
  50874. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  50875. #ifndef NO_WOLFSSL_CLIENT
  50876. #ifndef WOLFSSL_NO_TLS12
  50877. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  50878. WOLFSSL_SUCCESS);
  50879. #endif
  50880. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  50881. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  50882. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  50883. WOLFSSL_SUCCESS);
  50884. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  50885. BAD_FUNC_ARG);
  50886. #endif
  50887. #ifndef NO_WOLFSSL_SERVER
  50888. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  50889. WOLFSSL_SUCCESS);
  50890. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  50891. BAD_FUNC_ARG);
  50892. #endif
  50893. #ifdef OPENSSL_EXTRA
  50894. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  50895. #ifndef NO_WOLFSSL_CLIENT
  50896. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  50897. WOLFSSL_FAILURE);
  50898. #endif
  50899. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  50900. WOLFSSL_FAILURE);
  50901. #ifndef NO_WOLFSSL_CLIENT
  50902. #ifndef WOLFSSL_NO_TLS12
  50903. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  50904. WOLFSSL_SUCCESS);
  50905. #endif
  50906. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  50907. WOLFSSL_SUCCESS);
  50908. #endif
  50909. #ifndef NO_WOLFSSL_SERVER
  50910. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  50911. WOLFSSL_SUCCESS);
  50912. #endif
  50913. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  50914. #ifndef NO_WOLFSSL_CLIENT
  50915. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  50916. #endif
  50917. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  50918. #ifndef NO_WOLFSSL_CLIENT
  50919. #ifndef WOLFSSL_NO_TLS12
  50920. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  50921. WOLFSSL_SUCCESS);
  50922. #endif
  50923. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  50924. WOLFSSL_SUCCESS);
  50925. #endif
  50926. #ifndef NO_WOLFSSL_SERVER
  50927. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  50928. WOLFSSL_SUCCESS);
  50929. #endif
  50930. #endif /* OPENSSL_EXTRA */
  50931. #endif /* HAVE_SUPPORTED_CURVES */
  50932. #endif /* HAVE_ECC */
  50933. #ifdef WOLFSSL_EARLY_DATA
  50934. #ifndef OPENSSL_EXTRA
  50935. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  50936. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  50937. #else
  50938. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  50939. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  50940. #endif
  50941. #ifndef NO_WOLFSSL_CLIENT
  50942. #ifndef OPENSSL_EXTRA
  50943. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  50944. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  50945. #else
  50946. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  50947. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  50948. #endif
  50949. #endif
  50950. #ifndef NO_WOLFSSL_SERVER
  50951. #ifndef WOLFSSL_NO_TLS12
  50952. #ifndef OPENSSL_EXTRA
  50953. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  50954. BAD_FUNC_ARG);
  50955. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  50956. #else
  50957. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  50958. BAD_FUNC_ARG);
  50959. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  50960. #endif
  50961. #endif
  50962. #ifndef OPENSSL_EXTRA
  50963. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  50964. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  50965. WOLFSSL_SUCCESS);
  50966. #else
  50967. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  50968. #endif
  50969. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  50970. #else
  50971. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  50972. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  50973. #endif
  50974. #endif
  50975. #ifndef OPENSSL_EXTRA
  50976. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  50977. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  50978. #else
  50979. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  50980. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  50981. #endif
  50982. #ifndef NO_WOLFSSL_CLIENT
  50983. #ifndef OPENSSL_EXTRA
  50984. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  50985. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  50986. #else
  50987. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  50988. #endif
  50989. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  50990. #else
  50991. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  50992. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  50993. #endif
  50994. #endif
  50995. #ifndef NO_WOLFSSL_SERVER
  50996. #ifndef WOLFSSL_NO_TLS12
  50997. #ifndef OPENSSL_EXTRA
  50998. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  50999. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  51000. #else
  51001. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  51002. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  51003. #endif
  51004. #endif
  51005. #ifndef OPENSSL_EXTRA
  51006. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  51007. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  51008. #else
  51009. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  51010. #endif
  51011. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  51012. #else
  51013. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  51014. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  51015. #endif
  51016. #endif
  51017. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  51018. &outSz), BAD_FUNC_ARG);
  51019. #ifndef NO_WOLFSSL_CLIENT
  51020. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  51021. &outSz), BAD_FUNC_ARG);
  51022. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  51023. BAD_FUNC_ARG);
  51024. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  51025. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  51026. #endif
  51027. #ifndef NO_WOLFSSL_SERVER
  51028. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  51029. sizeof(earlyData), &outSz), SIDE_ERROR);
  51030. #endif
  51031. #ifndef NO_WOLFSSL_CLIENT
  51032. #ifndef WOLFSSL_NO_TLS12
  51033. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  51034. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  51035. #endif
  51036. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  51037. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  51038. #endif
  51039. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  51040. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  51041. #ifndef NO_WOLFSSL_SERVER
  51042. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  51043. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  51044. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  51045. &outSz), BAD_FUNC_ARG);
  51046. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  51047. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  51048. #endif
  51049. #ifndef NO_WOLFSSL_CLIENT
  51050. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  51051. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  51052. #endif
  51053. #ifndef NO_WOLFSSL_SERVER
  51054. #ifndef WOLFSSL_NO_TLS12
  51055. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  51056. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  51057. #endif
  51058. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  51059. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  51060. #endif
  51061. #endif
  51062. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  51063. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  51064. #endif
  51065. #ifndef NO_WOLFSSL_SERVER
  51066. wolfSSL_free(serverSsl);
  51067. wolfSSL_CTX_free(serverCtx);
  51068. #endif
  51069. #ifndef NO_WOLFSSL_CLIENT
  51070. wolfSSL_free(clientSsl);
  51071. wolfSSL_CTX_free(clientCtx);
  51072. #endif
  51073. #ifndef WOLFSSL_NO_TLS12
  51074. #ifndef NO_WOLFSSL_SERVER
  51075. wolfSSL_free(serverTls12Ssl);
  51076. wolfSSL_CTX_free(serverTls12Ctx);
  51077. #endif
  51078. #ifndef NO_WOLFSSL_CLIENT
  51079. wolfSSL_free(clientTls12Ssl);
  51080. wolfSSL_CTX_free(clientTls12Ctx);
  51081. #endif
  51082. #endif
  51083. return EXPECT_RESULT();
  51084. }
  51085. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  51086. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  51087. defined(BUILD_TLS_AES_256_GCM_SHA384)
  51088. /* Called when writing. */
  51089. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  51090. {
  51091. (void)ssl;
  51092. (void)buf;
  51093. (void)sz;
  51094. (void)ctx;
  51095. /* Force error return from wolfSSL_accept_TLSv13(). */
  51096. return WANT_WRITE;
  51097. }
  51098. /* Called when reading. */
  51099. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  51100. {
  51101. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  51102. int len = (int)msg->length;
  51103. (void)ssl;
  51104. (void)sz;
  51105. /* Pass back as much of message as will fit in buffer. */
  51106. if (len > sz)
  51107. len = sz;
  51108. XMEMCPY(buf, msg->buffer, len);
  51109. /* Move over returned data. */
  51110. msg->buffer += len;
  51111. msg->length -= len;
  51112. /* Amount actually copied. */
  51113. return len;
  51114. }
  51115. #endif
  51116. static int test_tls13_cipher_suites(void)
  51117. {
  51118. EXPECT_DECLS;
  51119. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  51120. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  51121. defined(BUILD_TLS_AES_256_GCM_SHA384)
  51122. WOLFSSL_CTX* ctx = NULL;
  51123. WOLFSSL *ssl = NULL;
  51124. int i;
  51125. byte clientHello[] = {
  51126. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  51127. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  51128. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  51129. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  51130. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  51131. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  51132. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  51133. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  51134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  51136. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  51137. 0x13, 0x01,
  51138. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  51139. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  51140. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  51141. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  51142. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  51143. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  51144. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  51145. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  51146. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  51147. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  51148. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  51149. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  51150. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  51151. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  51152. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  51153. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  51154. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  51155. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  51156. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  51157. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  51158. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  51159. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  51160. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  51161. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  51162. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  51163. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  51164. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  51165. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  51166. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  51167. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  51168. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  51169. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  51170. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  51171. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  51172. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  51173. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51175. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  51176. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  51177. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  51178. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  51179. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  51180. };
  51181. WOLFSSL_BUFFER_INFO msg;
  51182. /* Offset into ClientHello message data of first cipher suite. */
  51183. const int csOff = 78;
  51184. /* Server cipher list. */
  51185. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  51186. /* Suite list with duplicates. */
  51187. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  51188. "TLS13-AES128-GCM-SHA256:"
  51189. "TLS13-AES256-GCM-SHA384:"
  51190. "TLS13-AES256-GCM-SHA384:"
  51191. "TLS13-AES128-GCM-SHA256";
  51192. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  51193. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  51194. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  51195. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  51196. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  51197. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  51198. #endif
  51199. /* Set up wolfSSL context. */
  51200. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  51201. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  51202. WOLFSSL_FILETYPE_PEM));
  51203. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  51204. WOLFSSL_FILETYPE_PEM));
  51205. /* Read from 'msg'. */
  51206. wolfSSL_SetIORecv(ctx, CsRecv);
  51207. /* No where to send to - dummy sender. */
  51208. wolfSSL_SetIOSend(ctx, CsSend);
  51209. /* Test cipher suite list with many copies of a cipher suite. */
  51210. ExpectNotNull(ssl = wolfSSL_new(ctx));
  51211. msg.buffer = clientHello;
  51212. msg.length = (unsigned int)sizeof(clientHello);
  51213. wolfSSL_SetIOReadCtx(ssl, &msg);
  51214. /* Force server to have as many occurrences of same cipher suite as
  51215. * possible. */
  51216. if (ssl != NULL) {
  51217. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  51218. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  51219. for (i = 0; i < suites->suiteSz; i += 2) {
  51220. suites->suites[i + 0] = TLS13_BYTE;
  51221. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  51222. }
  51223. }
  51224. /* Test multiple occurrences of same cipher suite. */
  51225. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  51226. wolfSSL_free(ssl);
  51227. ssl = NULL;
  51228. /* Set client order opposite to server order:
  51229. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  51230. clientHello[csOff + 0] = TLS13_BYTE;
  51231. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  51232. clientHello[csOff + 2] = TLS13_BYTE;
  51233. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  51234. /* Test server order negotiation. */
  51235. ExpectNotNull(ssl = wolfSSL_new(ctx));
  51236. msg.buffer = clientHello;
  51237. msg.length = (unsigned int)sizeof(clientHello);
  51238. wolfSSL_SetIOReadCtx(ssl, &msg);
  51239. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  51240. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  51241. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  51242. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  51243. /* Check refined order - server order. */
  51244. ExpectIntEQ(ssl->suites->suiteSz, 4);
  51245. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  51246. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  51247. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  51248. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  51249. wolfSSL_free(ssl);
  51250. ssl = NULL;
  51251. /* Test client order negotiation. */
  51252. ExpectNotNull(ssl = wolfSSL_new(ctx));
  51253. msg.buffer = clientHello;
  51254. msg.length = (unsigned int)sizeof(clientHello);
  51255. wolfSSL_SetIOReadCtx(ssl, &msg);
  51256. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  51257. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  51258. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  51259. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  51260. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  51261. /* Check refined order - client order. */
  51262. ExpectIntEQ(ssl->suites->suiteSz, 4);
  51263. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  51264. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  51265. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  51266. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  51267. wolfSSL_free(ssl);
  51268. ssl = NULL;
  51269. /* Check duplicate detection is working. */
  51270. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  51271. ExpectIntEQ(ctx->suites->suiteSz, 4);
  51272. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  51273. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  51274. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  51275. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  51276. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  51277. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  51278. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  51279. ExpectIntEQ(ctx->suites->suiteSz, 4);
  51280. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  51281. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  51282. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  51283. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  51284. #endif
  51285. wolfSSL_CTX_free(ctx);
  51286. #endif
  51287. return EXPECT_RESULT();
  51288. }
  51289. #endif
  51290. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  51291. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  51292. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  51293. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  51294. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  51295. const unsigned char* priv, unsigned int privSz,
  51296. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  51297. unsigned char* out, unsigned int* outlen,
  51298. void* ctx)
  51299. {
  51300. int result;
  51301. /* Test fail when context associated with WOLFSSL is NULL */
  51302. if (ctx == NULL) {
  51303. return -1;
  51304. }
  51305. (void)ssl;
  51306. /* return 0 on success */
  51307. PRIVATE_KEY_UNLOCK();
  51308. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  51309. PRIVATE_KEY_LOCK();
  51310. return result;
  51311. }
  51312. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  51313. EXPECT_DECLS;
  51314. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  51315. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  51316. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  51317. WOLFSSL_SUCCESS);
  51318. #endif
  51319. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  51320. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  51321. WOLFSSL_SUCCESS);
  51322. #endif
  51323. return EXPECT_RESULT();
  51324. }
  51325. static int test_dh_ssl_setup(WOLFSSL* ssl)
  51326. {
  51327. EXPECT_DECLS;
  51328. static int dh_test_ctx = 1;
  51329. int ret;
  51330. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  51331. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  51332. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  51333. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  51334. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  51335. }
  51336. return EXPECT_RESULT();
  51337. }
  51338. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  51339. {
  51340. EXPECT_DECLS;
  51341. int ret;
  51342. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  51343. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  51344. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  51345. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  51346. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  51347. }
  51348. return EXPECT_RESULT();
  51349. }
  51350. #endif
  51351. static int test_DhCallbacks(void)
  51352. {
  51353. EXPECT_DECLS;
  51354. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  51355. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  51356. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  51357. WOLFSSL_CTX *ctx = NULL;
  51358. WOLFSSL *ssl = NULL;
  51359. int test;
  51360. test_ssl_cbf func_cb_client;
  51361. test_ssl_cbf func_cb_server;
  51362. /* Test that DH callback APIs work. */
  51363. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  51364. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  51365. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  51366. /* load client ca cert */
  51367. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  51368. WOLFSSL_SUCCESS);
  51369. /* test with NULL arguments */
  51370. wolfSSL_SetDhAgreeCtx(NULL, &test);
  51371. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  51372. /* test success case */
  51373. test = 1;
  51374. ExpectNotNull(ssl = wolfSSL_new(ctx));
  51375. wolfSSL_SetDhAgreeCtx(ssl, &test);
  51376. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  51377. wolfSSL_free(ssl);
  51378. wolfSSL_CTX_free(ctx);
  51379. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  51380. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  51381. /* set callbacks to use DH functions */
  51382. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  51383. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  51384. func_cb_client.method = wolfTLSv1_2_client_method;
  51385. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  51386. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  51387. func_cb_server.method = wolfTLSv1_2_server_method;
  51388. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  51389. &func_cb_server, NULL), TEST_SUCCESS);
  51390. /* Test fail */
  51391. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  51392. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  51393. /* set callbacks to use DH functions */
  51394. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  51395. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  51396. func_cb_client.method = wolfTLSv1_2_client_method;
  51397. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  51398. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  51399. func_cb_server.method = wolfTLSv1_2_server_method;
  51400. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  51401. &func_cb_server, NULL), TEST_FAIL);
  51402. #endif
  51403. return EXPECT_RESULT();
  51404. }
  51405. #endif /* HAVE_PK_CALLBACKS */
  51406. #ifdef HAVE_HASHDRBG
  51407. #ifdef TEST_RESEED_INTERVAL
  51408. static int test_wc_RNG_GenerateBlock_Reseed(void)
  51409. {
  51410. EXPECT_DECLS;
  51411. int i;
  51412. WC_RNG rng;
  51413. byte key[32];
  51414. XMEMSET(&rng, 0, sizeof(WC_RNG));
  51415. ExpectIntEQ(wc_InitRng(&rng), 0);
  51416. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  51417. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  51418. }
  51419. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  51420. return EXPECT_RESULT();
  51421. }
  51422. #endif /* TEST_RESEED_INTERVAL */
  51423. static int test_wc_RNG_GenerateBlock(void)
  51424. {
  51425. EXPECT_DECLS;
  51426. int i;
  51427. WC_RNG rng;
  51428. byte key[32];
  51429. XMEMSET(&rng, 0, sizeof(WC_RNG));
  51430. ExpectIntEQ(wc_InitRng(&rng), 0);
  51431. for (i = 0; i < 10; i++) {
  51432. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  51433. }
  51434. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  51435. return EXPECT_RESULT();
  51436. }
  51437. #endif /* HAVE_HASHDRBG */
  51438. /*
  51439. * Testing get_rand_digit
  51440. */
  51441. static int test_get_rand_digit(void)
  51442. {
  51443. EXPECT_DECLS;
  51444. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  51445. WC_RNG rng;
  51446. mp_digit d;
  51447. XMEMSET(&rng, 0, sizeof(WC_RNG));
  51448. ExpectIntEQ(wc_InitRng(&rng), 0);
  51449. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  51450. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  51451. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  51452. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  51453. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  51454. #endif
  51455. return EXPECT_RESULT();
  51456. } /* End test_get_rand_digit*/
  51457. /*
  51458. * Testing get_digit_count
  51459. */
  51460. static int test_get_digit_count(void)
  51461. {
  51462. EXPECT_DECLS;
  51463. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  51464. mp_int a;
  51465. XMEMSET(&a, 0, sizeof(mp_int));
  51466. ExpectIntEQ(mp_init(&a), 0);
  51467. ExpectIntEQ(get_digit_count(NULL), 0);
  51468. ExpectIntEQ(get_digit_count(&a), 0);
  51469. mp_clear(&a);
  51470. #endif
  51471. return EXPECT_RESULT();
  51472. } /* End test_get_digit_count*/
  51473. /*
  51474. * Testing mp_cond_copy
  51475. */
  51476. static int test_mp_cond_copy(void)
  51477. {
  51478. EXPECT_DECLS;
  51479. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  51480. defined(WOLFSSL_PUBLIC_MP)
  51481. mp_int a;
  51482. mp_int b;
  51483. int copy = 0;
  51484. XMEMSET(&a, 0, sizeof(mp_int));
  51485. XMEMSET(&b, 0, sizeof(mp_int));
  51486. ExpectIntEQ(mp_init(&a), MP_OKAY);
  51487. ExpectIntEQ(mp_init(&b), MP_OKAY);
  51488. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  51489. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  51490. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  51491. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  51492. mp_clear(&a);
  51493. mp_clear(&b);
  51494. #endif
  51495. return EXPECT_RESULT();
  51496. } /* End test_mp_cond_copy*/
  51497. /*
  51498. * Testing mp_rand
  51499. */
  51500. static int test_mp_rand(void)
  51501. {
  51502. EXPECT_DECLS;
  51503. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  51504. mp_int a;
  51505. WC_RNG rng;
  51506. int digits = 1;
  51507. XMEMSET(&a, 0, sizeof(mp_int));
  51508. XMEMSET(&rng, 0, sizeof(WC_RNG));
  51509. ExpectIntEQ(mp_init(&a), MP_OKAY);
  51510. ExpectIntEQ(wc_InitRng(&rng), 0);
  51511. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  51512. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  51513. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  51514. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  51515. mp_clear(&a);
  51516. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  51517. #endif
  51518. return EXPECT_RESULT();
  51519. } /* End test_mp_rand*/
  51520. /*
  51521. * Testing get_digit
  51522. */
  51523. static int test_get_digit(void)
  51524. {
  51525. EXPECT_DECLS;
  51526. #if defined(WOLFSSL_PUBLIC_MP)
  51527. mp_int a;
  51528. int n = 0;
  51529. XMEMSET(&a, 0, sizeof(mp_int));
  51530. ExpectIntEQ(mp_init(&a), MP_OKAY);
  51531. ExpectIntEQ(get_digit(NULL, n), 0);
  51532. ExpectIntEQ(get_digit(&a, n), 0);
  51533. mp_clear(&a);
  51534. #endif
  51535. return EXPECT_RESULT();
  51536. } /* End test_get_digit*/
  51537. /*
  51538. * Testing wc_export_int
  51539. */
  51540. static int test_wc_export_int(void)
  51541. {
  51542. EXPECT_DECLS;
  51543. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  51544. defined(WOLFSSL_PUBLIC_MP)
  51545. mp_int mp;
  51546. byte buf[32];
  51547. word32 keySz = (word32)sizeof(buf);
  51548. word32 len = (word32)sizeof(buf);
  51549. XMEMSET(&mp, 0, sizeof(mp_int));
  51550. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  51551. ExpectIntEQ(mp_set(&mp, 1234), 0);
  51552. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  51553. BAD_FUNC_ARG);
  51554. len = sizeof(buf)-1;
  51555. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  51556. BUFFER_E);
  51557. len = sizeof(buf);
  51558. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  51559. len = 4; /* test input too small */
  51560. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  51561. len = sizeof(buf);
  51562. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  51563. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  51564. ExpectIntEQ(len, 5);
  51565. mp_clear(&mp);
  51566. #endif
  51567. return EXPECT_RESULT();
  51568. } /* End test_wc_export_int*/
  51569. static int test_wc_InitRngNonce(void)
  51570. {
  51571. EXPECT_DECLS;
  51572. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  51573. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  51574. HAVE_FIPS_VERSION >= 2))
  51575. WC_RNG rng;
  51576. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  51577. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  51578. word32 nonceSz = sizeof(nonce);
  51579. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  51580. ExpectIntEQ(wc_FreeRng(&rng), 0);
  51581. #endif
  51582. return EXPECT_RESULT();
  51583. } /* End test_wc_InitRngNonce*/
  51584. /*
  51585. * Testing wc_InitRngNonce_ex
  51586. */
  51587. static int test_wc_InitRngNonce_ex(void)
  51588. {
  51589. EXPECT_DECLS;
  51590. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  51591. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  51592. HAVE_FIPS_VERSION >= 2))
  51593. WC_RNG rng;
  51594. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  51595. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  51596. word32 nonceSz = sizeof(nonce);
  51597. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  51598. 0);
  51599. ExpectIntEQ(wc_FreeRng(&rng), 0);
  51600. #endif
  51601. return EXPECT_RESULT();
  51602. } /* End test_wc_InitRngNonce_ex */
  51603. static int test_wolfSSL_X509_CRL(void)
  51604. {
  51605. EXPECT_DECLS;
  51606. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  51607. X509_CRL *crl = NULL;
  51608. char pem[][100] = {
  51609. "./certs/crl/crl.pem",
  51610. "./certs/crl/crl2.pem",
  51611. "./certs/crl/caEccCrl.pem",
  51612. "./certs/crl/eccCliCRL.pem",
  51613. "./certs/crl/eccSrvCRL.pem",
  51614. ""
  51615. };
  51616. #ifndef NO_BIO
  51617. BIO *bio = NULL;
  51618. #endif
  51619. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  51620. char der[][100] = {
  51621. "./certs/crl/crl.der",
  51622. "./certs/crl/crl2.der",
  51623. ""};
  51624. #endif
  51625. XFILE fp = XBADFILE;
  51626. int i;
  51627. for (i = 0; pem[i][0] != '\0'; i++)
  51628. {
  51629. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  51630. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  51631. NULL, NULL));
  51632. ExpectNotNull(crl);
  51633. X509_CRL_free(crl);
  51634. if (fp != XBADFILE) {
  51635. XFCLOSE(fp);
  51636. fp = XBADFILE;
  51637. }
  51638. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  51639. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  51640. NULL));
  51641. if (EXPECT_FAIL()) {
  51642. crl = NULL;
  51643. }
  51644. ExpectNotNull(crl);
  51645. X509_CRL_free(crl);
  51646. crl = NULL;
  51647. if (fp != XBADFILE) {
  51648. XFCLOSE(fp);
  51649. fp = XBADFILE;
  51650. }
  51651. }
  51652. #ifndef NO_BIO
  51653. for (i = 0; pem[i][0] != '\0'; i++)
  51654. {
  51655. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  51656. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  51657. X509_CRL_free(crl);
  51658. crl = NULL;
  51659. BIO_free(bio);
  51660. bio = NULL;
  51661. }
  51662. #endif
  51663. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  51664. for (i = 0; der[i][0] != '\0'; i++) {
  51665. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  51666. ExpectTrue((fp != XBADFILE));
  51667. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  51668. ExpectNotNull(crl);
  51669. X509_CRL_free(crl);
  51670. if (fp != XBADFILE) {
  51671. XFCLOSE(fp);
  51672. fp = XBADFILE;
  51673. }
  51674. fp = XFOPEN(der[i], "rb");
  51675. ExpectTrue((fp != XBADFILE));
  51676. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  51677. if (EXPECT_FAIL()) {
  51678. crl = NULL;
  51679. }
  51680. ExpectNotNull(crl);
  51681. X509_CRL_free(crl);
  51682. crl = NULL;
  51683. if (fp != XBADFILE) {
  51684. XFCLOSE(fp);
  51685. fp = XBADFILE;
  51686. }
  51687. }
  51688. #endif
  51689. #endif
  51690. return EXPECT_RESULT();
  51691. }
  51692. static int test_wolfSSL_X509_load_crl_file(void)
  51693. {
  51694. EXPECT_DECLS;
  51695. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  51696. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  51697. int i;
  51698. char pem[][100] = {
  51699. "./certs/crl/crl.pem",
  51700. "./certs/crl/crl2.pem",
  51701. "./certs/crl/caEccCrl.pem",
  51702. "./certs/crl/eccCliCRL.pem",
  51703. "./certs/crl/eccSrvCRL.pem",
  51704. #ifdef WC_RSA_PSS
  51705. "./certs/crl/crl_rsapss.pem",
  51706. #endif
  51707. ""
  51708. };
  51709. char der[][100] = {
  51710. "./certs/crl/crl.der",
  51711. "./certs/crl/crl2.der",
  51712. ""
  51713. };
  51714. WOLFSSL_X509_STORE* store = NULL;
  51715. WOLFSSL_X509_LOOKUP* lookup = NULL;
  51716. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  51717. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  51718. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  51719. X509_FILETYPE_PEM), 1);
  51720. #ifdef WC_RSA_PSS
  51721. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  51722. X509_FILETYPE_PEM), 1);
  51723. #endif
  51724. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  51725. X509_FILETYPE_PEM), 1);
  51726. if (store) {
  51727. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  51728. WOLFSSL_FILETYPE_PEM), 1);
  51729. /* since store hasn't yet known the revoked cert*/
  51730. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  51731. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  51732. }
  51733. for (i = 0; pem[i][0] != '\0'; i++) {
  51734. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  51735. 1);
  51736. }
  51737. if (store) {
  51738. /* since store knows crl list */
  51739. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  51740. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  51741. CRL_CERT_REVOKED);
  51742. #ifdef WC_RSA_PSS
  51743. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  51744. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  51745. CRL_CERT_REVOKED);
  51746. #endif
  51747. }
  51748. /* once feeing store */
  51749. X509_STORE_free(store);
  51750. store = NULL;
  51751. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  51752. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  51753. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  51754. X509_FILETYPE_PEM), 1);
  51755. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  51756. X509_FILETYPE_PEM), 1);
  51757. if (store) {
  51758. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  51759. WOLFSSL_FILETYPE_PEM), 1);
  51760. /* since store hasn't yet known the revoked cert*/
  51761. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  51762. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  51763. }
  51764. for (i = 0; der[i][0] != '\0'; i++) {
  51765. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  51766. 1);
  51767. }
  51768. if (store) {
  51769. /* since store knows crl list */
  51770. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  51771. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  51772. CRL_CERT_REVOKED);
  51773. }
  51774. /* test for incorrect parameter */
  51775. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  51776. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  51777. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  51778. X509_STORE_free(store);
  51779. store = NULL;
  51780. #endif
  51781. return EXPECT_RESULT();
  51782. }
  51783. static int test_wolfSSL_i2d_X509(void)
  51784. {
  51785. EXPECT_DECLS;
  51786. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  51787. const unsigned char* cert_buf = server_cert_der_2048;
  51788. unsigned char* out = NULL;
  51789. unsigned char* tmp = NULL;
  51790. X509* cert = NULL;
  51791. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  51792. /* Pointer should be advanced */
  51793. ExpectPtrGT(cert_buf, server_cert_der_2048);
  51794. ExpectIntGT(i2d_X509(cert, &out), 0);
  51795. ExpectNotNull(out);
  51796. tmp = out;
  51797. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  51798. ExpectPtrGT(tmp, out);
  51799. if (out != NULL)
  51800. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  51801. X509_free(cert);
  51802. #endif
  51803. return EXPECT_RESULT();
  51804. }
  51805. static int test_wolfSSL_d2i_X509_REQ(void)
  51806. {
  51807. EXPECT_DECLS;
  51808. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  51809. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  51810. !defined(WOLFSSL_SP_MATH)
  51811. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  51812. * were generated by libest
  51813. * ./certs/csr.attr.der contains sample attributes
  51814. * ./certs/csr.ext.der contains sample extensions */
  51815. const char* csrFile = "./certs/csr.signed.der";
  51816. const char* csrPopFile = "./certs/csr.attr.der";
  51817. const char* csrExtFile = "./certs/csr.ext.der";
  51818. /* ./certs/csr.dsa.pem is generated using
  51819. * openssl req -newkey dsa:certs/dsaparams.pem \
  51820. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  51821. * -outform PEM
  51822. * with the passphrase "wolfSSL"
  51823. */
  51824. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  51825. const char* csrDsaFile = "./certs/csr.dsa.pem";
  51826. XFILE f = XBADFILE;
  51827. #endif
  51828. BIO* bio = NULL;
  51829. X509* req = NULL;
  51830. EVP_PKEY *pub_key = NULL;
  51831. {
  51832. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  51833. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  51834. /*
  51835. * Extract the public key from the CSR
  51836. */
  51837. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  51838. /*
  51839. * Verify the signature in the CSR
  51840. */
  51841. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  51842. X509_free(req);
  51843. req = NULL;
  51844. BIO_free(bio);
  51845. bio = NULL;
  51846. EVP_PKEY_free(pub_key);
  51847. pub_key = NULL;
  51848. }
  51849. {
  51850. #ifdef OPENSSL_ALL
  51851. X509_ATTRIBUTE* attr = NULL;
  51852. ASN1_TYPE *at = NULL;
  51853. #endif
  51854. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  51855. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  51856. /*
  51857. * Extract the public key from the CSR
  51858. */
  51859. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  51860. /*
  51861. * Verify the signature in the CSR
  51862. */
  51863. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  51864. #ifdef OPENSSL_ALL
  51865. /*
  51866. * Obtain the challenge password from the CSR
  51867. */
  51868. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  51869. -1), 1);
  51870. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  51871. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  51872. ExpectNotNull(at->value.asn1_string);
  51873. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  51874. "2xIE+qqp/rhyTXP+");
  51875. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  51876. #endif
  51877. X509_free(req);
  51878. req = NULL;
  51879. BIO_free(bio);
  51880. bio = NULL;
  51881. EVP_PKEY_free(pub_key);
  51882. pub_key = NULL;
  51883. }
  51884. {
  51885. #ifdef OPENSSL_ALL
  51886. X509_ATTRIBUTE* attr = NULL;
  51887. ASN1_TYPE *at = NULL;
  51888. STACK_OF(X509_EXTENSION) *exts = NULL;
  51889. #endif
  51890. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  51891. /* This CSR contains an Extension Request attribute so
  51892. * we test extension parsing in a CSR attribute here. */
  51893. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  51894. /*
  51895. * Extract the public key from the CSR
  51896. */
  51897. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  51898. /*
  51899. * Verify the signature in the CSR
  51900. */
  51901. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  51902. #ifdef OPENSSL_ALL
  51903. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  51904. req));
  51905. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  51906. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  51907. /*
  51908. * Obtain the challenge password from the CSR
  51909. */
  51910. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  51911. -1), 0);
  51912. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  51913. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  51914. ExpectNotNull(at->value.asn1_string);
  51915. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  51916. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  51917. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  51918. #endif
  51919. X509_free(req);
  51920. req = NULL;
  51921. BIO_free(bio);
  51922. bio = NULL;
  51923. EVP_PKEY_free(pub_key);
  51924. pub_key = NULL;
  51925. }
  51926. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  51927. {
  51928. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  51929. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  51930. /*
  51931. * Extract the public key from the CSR
  51932. */
  51933. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  51934. /*
  51935. * Verify the signature in the CSR
  51936. */
  51937. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  51938. X509_free(req);
  51939. req = NULL;
  51940. BIO_free(bio);
  51941. /* Run the same test, but with a file pointer instead of a BIO.
  51942. * (PEM_read_X509_REQ)*/
  51943. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  51944. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  51945. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  51946. X509_free(req);
  51947. EVP_PKEY_free(pub_key);
  51948. }
  51949. #endif /* !NO_DSA && !HAVE_SELFTEST */
  51950. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  51951. return EXPECT_RESULT();
  51952. }
  51953. static int test_wolfSSL_PEM_read_X509(void)
  51954. {
  51955. EXPECT_DECLS;
  51956. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  51957. !defined(NO_RSA)
  51958. X509 *x509 = NULL;
  51959. XFILE fp = XBADFILE;
  51960. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  51961. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  51962. X509_free(x509);
  51963. if (fp != XBADFILE)
  51964. XFCLOSE(fp);
  51965. #endif
  51966. return EXPECT_RESULT();
  51967. }
  51968. static int test_wolfSSL_PEM_read(void)
  51969. {
  51970. EXPECT_DECLS;
  51971. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  51972. const char* filename = "./certs/server-keyEnc.pem";
  51973. XFILE fp = XBADFILE;
  51974. char* name = NULL;
  51975. char* header = NULL;
  51976. byte* data = NULL;
  51977. long len;
  51978. EVP_CIPHER_INFO cipher;
  51979. WOLFSSL_BIO* bio = NULL;
  51980. byte* fileData = NULL;
  51981. size_t fileDataSz = 0;
  51982. byte* out;
  51983. ExpectNotNull(bio = BIO_new_file(filename, "rb"));
  51984. ExpectIntEQ(PEM_read_bio(bio, NULL, &header, &data, &len), 0);
  51985. ExpectIntEQ(PEM_read_bio(bio, &name, NULL, &data, &len), 0);
  51986. ExpectIntEQ(PEM_read_bio(bio, &name, &header, NULL, &len), 0);
  51987. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, NULL), 0);
  51988. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  51989. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  51990. ExpectIntGT(XSTRLEN(header), 0);
  51991. ExpectIntGT(len, 0);
  51992. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51993. name = NULL;
  51994. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51995. header = NULL;
  51996. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51997. data = NULL;
  51998. BIO_free(bio);
  51999. bio = NULL;
  52000. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  52001. /* Fail cases. */
  52002. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  52003. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  52004. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  52005. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  52006. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  52007. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  52008. ExpectIntGT(XSTRLEN(header), 0);
  52009. ExpectIntGT(len, 0);
  52010. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  52011. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  52012. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  52013. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  52014. DYNAMIC_TYPE_TMP_BUFFER));
  52015. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  52016. if (fp != XBADFILE) {
  52017. XFCLOSE(fp);
  52018. fp = XBADFILE;
  52019. }
  52020. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  52021. /* Fail cases. */
  52022. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  52023. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  52024. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  52025. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  52026. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  52027. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  52028. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  52029. /* Fail cases. */
  52030. ExpectIntEQ(PEM_write(XBADFILE, name, header, data, len), 0);
  52031. ExpectIntEQ(PEM_write(stderr, NULL, header, data, len), 0);
  52032. ExpectIntEQ(PEM_write(stderr, name, NULL, data, len), 0);
  52033. ExpectIntEQ(PEM_write(stderr, name, header, NULL, len), 0);
  52034. /* Pass case */
  52035. ExpectIntEQ(PEM_write(stderr, name, header, data, len), fileDataSz);
  52036. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52037. name = NULL;
  52038. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52039. header = NULL;
  52040. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52041. data = NULL;
  52042. /* Read out of a fixed buffer BIO - forces malloc in PEM_read_bio. */
  52043. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  52044. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  52045. ExpectIntGT(XSTRLEN(header), 0);
  52046. ExpectIntGT(len, 0);
  52047. /* Fail cases. */
  52048. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  52049. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  52050. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  52051. #ifndef NO_DES3
  52052. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  52053. #endif
  52054. /* Fail cases. */
  52055. ExpectIntEQ(PEM_do_header(NULL, data, &len, PasswordCallBack,
  52056. (void*)"yassl123"), WOLFSSL_FAILURE);
  52057. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  52058. (void*)"yassl123"), WOLFSSL_FAILURE);
  52059. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  52060. (void*)"yassl123"), WOLFSSL_FAILURE);
  52061. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  52062. (void*)"yassl123"), WOLFSSL_FAILURE);
  52063. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NoPasswordCallBack,
  52064. (void*)"yassl123"), WOLFSSL_FAILURE);
  52065. #if !defined(NO_DES3) && !defined(NO_MD5)
  52066. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  52067. (void*)"yassl123"), WOLFSSL_SUCCESS);
  52068. #else
  52069. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  52070. (void*)"yassl123"), WOLFSSL_FAILURE);
  52071. #endif
  52072. BIO_free(bio);
  52073. bio = NULL;
  52074. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52075. fileData = NULL;
  52076. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52077. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52078. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52079. name = NULL;
  52080. header = NULL;
  52081. data = NULL;
  52082. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  52083. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  52084. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  52085. ExpectIntEQ(XSTRLEN(header), 0);
  52086. ExpectIntGT(len, 0);
  52087. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  52088. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  52089. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  52090. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  52091. DYNAMIC_TYPE_TMP_BUFFER));
  52092. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  52093. if (fp != XBADFILE)
  52094. XFCLOSE(fp);
  52095. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  52096. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  52097. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  52098. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  52099. BIO_free(bio);
  52100. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52101. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52102. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52103. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52104. #endif
  52105. return EXPECT_RESULT();
  52106. }
  52107. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  52108. {
  52109. EXPECT_DECLS;
  52110. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  52111. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52112. const byte iv[12] = { 0 };
  52113. const byte key[16] = { 0 };
  52114. const byte cleartext[16] = { 0 };
  52115. const byte aad[] = {
  52116. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  52117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  52118. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  52119. 0x4f
  52120. };
  52121. byte out1Part[16];
  52122. byte outTag1Part[16];
  52123. byte out2Part[16];
  52124. byte outTag2Part[16];
  52125. byte decryptBuf[16];
  52126. int len = 0;
  52127. int tlen;
  52128. EVP_CIPHER_CTX* ctx = NULL;
  52129. /* ENCRYPT */
  52130. /* Send AAD and data in 1 part */
  52131. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  52132. tlen = 0;
  52133. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  52134. 1);
  52135. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  52136. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  52137. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  52138. sizeof(cleartext)), 1);
  52139. tlen += len;
  52140. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  52141. tlen += len;
  52142. ExpectIntEQ(tlen, sizeof(cleartext));
  52143. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  52144. outTag1Part), 1);
  52145. EVP_CIPHER_CTX_free(ctx);
  52146. ctx = NULL;
  52147. /* DECRYPT */
  52148. /* Send AAD and data in 1 part */
  52149. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  52150. tlen = 0;
  52151. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  52152. 1);
  52153. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  52154. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  52155. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  52156. sizeof(cleartext)), 1);
  52157. tlen += len;
  52158. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  52159. outTag1Part), 1);
  52160. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  52161. tlen += len;
  52162. ExpectIntEQ(tlen, sizeof(cleartext));
  52163. EVP_CIPHER_CTX_free(ctx);
  52164. ctx = NULL;
  52165. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  52166. /* ENCRYPT */
  52167. /* Send AAD and data in 2 parts */
  52168. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  52169. tlen = 0;
  52170. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  52171. 1);
  52172. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  52173. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  52174. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  52175. 1);
  52176. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  52177. tlen += len;
  52178. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  52179. sizeof(cleartext) - 1), 1);
  52180. tlen += len;
  52181. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  52182. tlen += len;
  52183. ExpectIntEQ(tlen, sizeof(cleartext));
  52184. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  52185. outTag2Part), 1);
  52186. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  52187. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  52188. EVP_CIPHER_CTX_free(ctx);
  52189. ctx = NULL;
  52190. /* DECRYPT */
  52191. /* Send AAD and data in 2 parts */
  52192. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  52193. tlen = 0;
  52194. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  52195. 1);
  52196. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  52197. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  52198. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  52199. 1);
  52200. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  52201. tlen += len;
  52202. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  52203. sizeof(cleartext) - 1), 1);
  52204. tlen += len;
  52205. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  52206. outTag1Part), 1);
  52207. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  52208. tlen += len;
  52209. ExpectIntEQ(tlen, sizeof(cleartext));
  52210. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  52211. /* Test AAD reuse */
  52212. EVP_CIPHER_CTX_free(ctx);
  52213. #endif
  52214. return EXPECT_RESULT();
  52215. }
  52216. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  52217. {
  52218. EXPECT_DECLS;
  52219. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  52220. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52221. /* Zero length plain text */
  52222. byte key[] = {
  52223. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52224. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52225. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52226. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  52227. }; /* align */
  52228. byte iv[] = {
  52229. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  52230. }; /* align */
  52231. byte plaintxt[1];
  52232. int ivSz = 12;
  52233. int plaintxtSz = 0;
  52234. unsigned char tag[16];
  52235. unsigned char tag_kat[] = {
  52236. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  52237. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  52238. };
  52239. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  52240. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  52241. int ciphertxtSz = 0;
  52242. int decryptedtxtSz = 0;
  52243. int len = 0;
  52244. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  52245. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  52246. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  52247. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52248. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  52249. plaintxtSz));
  52250. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  52251. ciphertxtSz += len;
  52252. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  52253. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  52254. ExpectIntEQ(0, ciphertxtSz);
  52255. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  52256. EVP_CIPHER_CTX_init(de);
  52257. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  52258. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52259. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  52260. decryptedtxtSz = len;
  52261. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  52262. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  52263. decryptedtxtSz += len;
  52264. ExpectIntEQ(0, decryptedtxtSz);
  52265. EVP_CIPHER_CTX_free(en);
  52266. EVP_CIPHER_CTX_free(de);
  52267. #endif
  52268. return EXPECT_RESULT();
  52269. }
  52270. static int test_wolfssl_EVP_aes_gcm(void)
  52271. {
  52272. EXPECT_DECLS;
  52273. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  52274. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52275. /* A 256 bit key, AES_128 will use the first 128 bit*/
  52276. byte *key = (byte*)"01234567890123456789012345678901";
  52277. /* A 128 bit IV */
  52278. byte *iv = (byte*)"0123456789012345";
  52279. int ivSz = AES_BLOCK_SIZE;
  52280. /* Message to be encrypted */
  52281. byte *plaintxt = (byte*)"for things to change you have to change";
  52282. /* Additional non-confidential data */
  52283. byte *aad = (byte*)"Don't spend major time on minor things.";
  52284. unsigned char tag[AES_BLOCK_SIZE] = {0};
  52285. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  52286. int aadSz = (int)XSTRLEN((char*)aad);
  52287. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  52288. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  52289. int ciphertxtSz = 0;
  52290. int decryptedtxtSz = 0;
  52291. int len = 0;
  52292. int i = 0;
  52293. EVP_CIPHER_CTX en[2];
  52294. EVP_CIPHER_CTX de[2];
  52295. for (i = 0; i < 2; i++) {
  52296. EVP_CIPHER_CTX_init(&en[i]);
  52297. if (i == 0) {
  52298. /* Default uses 96-bits IV length */
  52299. #ifdef WOLFSSL_AES_128
  52300. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  52301. key, iv));
  52302. #elif defined(WOLFSSL_AES_192)
  52303. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  52304. key, iv));
  52305. #elif defined(WOLFSSL_AES_256)
  52306. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  52307. key, iv));
  52308. #endif
  52309. }
  52310. else {
  52311. #ifdef WOLFSSL_AES_128
  52312. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  52313. NULL, NULL));
  52314. #elif defined(WOLFSSL_AES_192)
  52315. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  52316. NULL, NULL));
  52317. #elif defined(WOLFSSL_AES_256)
  52318. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  52319. NULL, NULL));
  52320. #endif
  52321. /* non-default must to set the IV length first */
  52322. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  52323. ivSz, NULL));
  52324. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  52325. }
  52326. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  52327. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  52328. plaintxtSz));
  52329. ciphertxtSz = len;
  52330. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  52331. ciphertxtSz += len;
  52332. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  52333. AES_BLOCK_SIZE, tag));
  52334. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  52335. EVP_CIPHER_CTX_init(&de[i]);
  52336. if (i == 0) {
  52337. /* Default uses 96-bits IV length */
  52338. #ifdef WOLFSSL_AES_128
  52339. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  52340. key, iv));
  52341. #elif defined(WOLFSSL_AES_192)
  52342. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  52343. key, iv));
  52344. #elif defined(WOLFSSL_AES_256)
  52345. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  52346. key, iv));
  52347. #endif
  52348. }
  52349. else {
  52350. #ifdef WOLFSSL_AES_128
  52351. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  52352. NULL, NULL));
  52353. #elif defined(WOLFSSL_AES_192)
  52354. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  52355. NULL, NULL));
  52356. #elif defined(WOLFSSL_AES_256)
  52357. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  52358. NULL, NULL));
  52359. #endif
  52360. /* non-default must to set the IV length first */
  52361. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  52362. ivSz, NULL));
  52363. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52364. }
  52365. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52366. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  52367. ciphertxtSz));
  52368. decryptedtxtSz = len;
  52369. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  52370. AES_BLOCK_SIZE, tag));
  52371. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52372. decryptedtxtSz += len;
  52373. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  52374. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  52375. /* modify tag*/
  52376. if (i == 0) {
  52377. /* Default uses 96-bits IV length */
  52378. #ifdef WOLFSSL_AES_128
  52379. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  52380. key, iv));
  52381. #elif defined(WOLFSSL_AES_192)
  52382. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  52383. key, iv));
  52384. #elif defined(WOLFSSL_AES_256)
  52385. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  52386. key, iv));
  52387. #endif
  52388. }
  52389. else {
  52390. #ifdef WOLFSSL_AES_128
  52391. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  52392. NULL, NULL));
  52393. #elif defined(WOLFSSL_AES_192)
  52394. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  52395. NULL, NULL));
  52396. #elif defined(WOLFSSL_AES_256)
  52397. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  52398. NULL, NULL));
  52399. #endif
  52400. /* non-default must to set the IV length first */
  52401. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  52402. ivSz, NULL));
  52403. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52404. }
  52405. tag[AES_BLOCK_SIZE-1]+=0xBB;
  52406. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52407. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  52408. AES_BLOCK_SIZE, tag));
  52409. /* fail due to wrong tag */
  52410. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  52411. ciphertxtSz));
  52412. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52413. ExpectIntEQ(0, len);
  52414. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  52415. }
  52416. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  52417. return EXPECT_RESULT();
  52418. }
  52419. static int test_wolfssl_EVP_aria_gcm(void)
  52420. {
  52421. int res = TEST_SKIPPED;
  52422. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  52423. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52424. /* A 256 bit key, AES_128 will use the first 128 bit*/
  52425. byte *key = (byte*)"01234567890123456789012345678901";
  52426. /* A 128 bit IV */
  52427. byte *iv = (byte*)"0123456789012345";
  52428. int ivSz = ARIA_BLOCK_SIZE;
  52429. /* Message to be encrypted */
  52430. const int plaintxtSz = 40;
  52431. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  52432. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  52433. /* Additional non-confidential data */
  52434. byte *aad = (byte*)"Don't spend major time on minor things.";
  52435. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  52436. int aadSz = (int)XSTRLEN((char*)aad);
  52437. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  52438. byte decryptedtxt[plaintxtSz];
  52439. int ciphertxtSz = 0;
  52440. int decryptedtxtSz = 0;
  52441. int len = 0;
  52442. int i = 0;
  52443. #define TEST_ARIA_GCM_COUNT 6
  52444. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  52445. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  52446. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  52447. EVP_CIPHER_CTX_init(&en[i]);
  52448. switch (i) {
  52449. case 0:
  52450. /* Default uses 96-bits IV length */
  52451. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  52452. break;
  52453. case 1:
  52454. /* Default uses 96-bits IV length */
  52455. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  52456. break;
  52457. case 2:
  52458. /* Default uses 96-bits IV length */
  52459. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  52460. break;
  52461. case 3:
  52462. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  52463. /* non-default must to set the IV length first */
  52464. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52465. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  52466. break;
  52467. case 4:
  52468. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  52469. /* non-default must to set the IV length first */
  52470. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52471. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  52472. break;
  52473. case 5:
  52474. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  52475. /* non-default must to set the IV length first */
  52476. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52477. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  52478. break;
  52479. }
  52480. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  52481. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  52482. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  52483. ciphertxtSz = len;
  52484. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  52485. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  52486. ciphertxtSz += len;
  52487. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  52488. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  52489. EVP_CIPHER_CTX_init(&de[i]);
  52490. switch (i) {
  52491. case 0:
  52492. /* Default uses 96-bits IV length */
  52493. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  52494. break;
  52495. case 1:
  52496. /* Default uses 96-bits IV length */
  52497. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  52498. break;
  52499. case 2:
  52500. /* Default uses 96-bits IV length */
  52501. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  52502. break;
  52503. case 3:
  52504. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  52505. /* non-default must to set the IV length first */
  52506. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52507. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52508. break;
  52509. case 4:
  52510. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  52511. /* non-default must to set the IV length first */
  52512. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52513. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52514. break;
  52515. case 5:
  52516. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  52517. /* non-default must to set the IV length first */
  52518. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52519. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52520. break;
  52521. }
  52522. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  52523. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52524. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  52525. decryptedtxtSz = len;
  52526. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  52527. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52528. decryptedtxtSz += len;
  52529. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  52530. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  52531. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  52532. /* modify tag*/
  52533. tag[AES_BLOCK_SIZE-1]+=0xBB;
  52534. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52535. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  52536. /* fail due to wrong tag */
  52537. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  52538. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52539. AssertIntEQ(0, len);
  52540. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  52541. }
  52542. res = TEST_RES_CHECK(1);
  52543. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  52544. return res;
  52545. }
  52546. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  52547. {
  52548. EXPECT_DECLS;
  52549. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  52550. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52551. /* Zero length plain text */
  52552. byte key[] = {
  52553. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52554. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52555. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52556. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  52557. }; /* align */
  52558. byte iv[] = {
  52559. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  52560. }; /* align */
  52561. byte plaintxt[1];
  52562. int ivSz = 12;
  52563. int plaintxtSz = 0;
  52564. unsigned char tag[16];
  52565. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  52566. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  52567. int ciphertxtSz = 0;
  52568. int decryptedtxtSz = 0;
  52569. int len = 0;
  52570. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  52571. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  52572. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  52573. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  52574. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  52575. plaintxtSz));
  52576. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  52577. ciphertxtSz += len;
  52578. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  52579. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  52580. ExpectIntEQ(0, ciphertxtSz);
  52581. EVP_CIPHER_CTX_init(de);
  52582. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  52583. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  52584. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  52585. decryptedtxtSz = len;
  52586. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  52587. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  52588. decryptedtxtSz += len;
  52589. ExpectIntEQ(0, decryptedtxtSz);
  52590. EVP_CIPHER_CTX_free(en);
  52591. EVP_CIPHER_CTX_free(de);
  52592. #endif
  52593. return EXPECT_RESULT();
  52594. }
  52595. static int test_wolfssl_EVP_aes_ccm(void)
  52596. {
  52597. EXPECT_DECLS;
  52598. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  52599. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52600. /* A 256 bit key, AES_128 will use the first 128 bit*/
  52601. byte *key = (byte*)"01234567890123456789012345678901";
  52602. /* A 128 bit IV */
  52603. byte *iv = (byte*)"0123456789012";
  52604. int ivSz = (int)XSTRLEN((char*)iv);
  52605. /* Message to be encrypted */
  52606. byte *plaintxt = (byte*)"for things to change you have to change";
  52607. /* Additional non-confidential data */
  52608. byte *aad = (byte*)"Don't spend major time on minor things.";
  52609. unsigned char tag[AES_BLOCK_SIZE] = {0};
  52610. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  52611. int aadSz = (int)XSTRLEN((char*)aad);
  52612. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  52613. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  52614. int ciphertxtSz = 0;
  52615. int decryptedtxtSz = 0;
  52616. int len = 0;
  52617. int i = 0;
  52618. int ret;
  52619. EVP_CIPHER_CTX en[2];
  52620. EVP_CIPHER_CTX de[2];
  52621. for (i = 0; i < 2; i++) {
  52622. EVP_CIPHER_CTX_init(&en[i]);
  52623. if (i == 0) {
  52624. /* Default uses 96-bits IV length */
  52625. #ifdef WOLFSSL_AES_128
  52626. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  52627. key, iv));
  52628. #elif defined(WOLFSSL_AES_192)
  52629. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  52630. key, iv));
  52631. #elif defined(WOLFSSL_AES_256)
  52632. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  52633. key, iv));
  52634. #endif
  52635. }
  52636. else {
  52637. #ifdef WOLFSSL_AES_128
  52638. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  52639. NULL, NULL));
  52640. #elif defined(WOLFSSL_AES_192)
  52641. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  52642. NULL, NULL));
  52643. #elif defined(WOLFSSL_AES_256)
  52644. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  52645. NULL, NULL));
  52646. #endif
  52647. /* non-default must to set the IV length first */
  52648. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  52649. ivSz, NULL));
  52650. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  52651. }
  52652. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  52653. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  52654. plaintxtSz));
  52655. ciphertxtSz = len;
  52656. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  52657. ciphertxtSz += len;
  52658. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  52659. AES_BLOCK_SIZE, tag));
  52660. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  52661. ExpectIntEQ(ret, 1);
  52662. EVP_CIPHER_CTX_init(&de[i]);
  52663. if (i == 0) {
  52664. /* Default uses 96-bits IV length */
  52665. #ifdef WOLFSSL_AES_128
  52666. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  52667. key, iv));
  52668. #elif defined(WOLFSSL_AES_192)
  52669. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  52670. key, iv));
  52671. #elif defined(WOLFSSL_AES_256)
  52672. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  52673. key, iv));
  52674. #endif
  52675. }
  52676. else {
  52677. #ifdef WOLFSSL_AES_128
  52678. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  52679. NULL, NULL));
  52680. #elif defined(WOLFSSL_AES_192)
  52681. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  52682. NULL, NULL));
  52683. #elif defined(WOLFSSL_AES_256)
  52684. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  52685. NULL, NULL));
  52686. #endif
  52687. /* non-default must to set the IV length first */
  52688. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  52689. ivSz, NULL));
  52690. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52691. }
  52692. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52693. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  52694. ciphertxtSz));
  52695. decryptedtxtSz = len;
  52696. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  52697. AES_BLOCK_SIZE, tag));
  52698. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52699. decryptedtxtSz += len;
  52700. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  52701. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  52702. /* modify tag*/
  52703. tag[AES_BLOCK_SIZE-1]+=0xBB;
  52704. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52705. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  52706. AES_BLOCK_SIZE, tag));
  52707. /* fail due to wrong tag */
  52708. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  52709. ciphertxtSz));
  52710. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52711. ExpectIntEQ(0, len);
  52712. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  52713. ExpectIntEQ(ret, 1);
  52714. }
  52715. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  52716. return EXPECT_RESULT();
  52717. }
  52718. static int test_wolfssl_EVP_chacha20_poly1305(void)
  52719. {
  52720. EXPECT_DECLS;
  52721. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  52722. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  52723. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  52724. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  52725. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  52726. byte cipherText[sizeof(plainText)];
  52727. byte decryptedText[sizeof(plainText)];
  52728. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  52729. EVP_CIPHER_CTX* ctx = NULL;
  52730. int outSz;
  52731. /* Encrypt. */
  52732. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52733. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  52734. NULL), WOLFSSL_SUCCESS);
  52735. /* Invalid IV length. */
  52736. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  52737. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  52738. /* Valid IV length. */
  52739. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  52740. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  52741. /* Invalid tag length. */
  52742. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  52743. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  52744. /* Valid tag length. */
  52745. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  52746. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  52747. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52748. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  52749. WOLFSSL_SUCCESS);
  52750. ExpectIntEQ(outSz, sizeof(aad));
  52751. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  52752. sizeof(plainText)), WOLFSSL_SUCCESS);
  52753. ExpectIntEQ(outSz, sizeof(plainText));
  52754. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  52755. ExpectIntEQ(outSz, 0);
  52756. /* Invalid tag length. */
  52757. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  52758. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  52759. /* Valid tag length. */
  52760. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  52761. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  52762. EVP_CIPHER_CTX_free(ctx);
  52763. ctx = NULL;
  52764. /* Decrypt. */
  52765. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52766. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  52767. NULL), WOLFSSL_SUCCESS);
  52768. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  52769. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  52770. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  52771. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  52772. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52773. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  52774. WOLFSSL_SUCCESS);
  52775. ExpectIntEQ(outSz, sizeof(aad));
  52776. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52777. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52778. ExpectIntEQ(outSz, sizeof(cipherText));
  52779. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  52780. WOLFSSL_SUCCESS);
  52781. ExpectIntEQ(outSz, 0);
  52782. EVP_CIPHER_CTX_free(ctx);
  52783. ctx = NULL;
  52784. /* Test partial Inits. CipherInit() allow setting of key and iv
  52785. * in separate calls. */
  52786. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52787. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  52788. key, NULL, 1), WOLFSSL_SUCCESS);
  52789. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  52790. WOLFSSL_SUCCESS);
  52791. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  52792. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  52793. ExpectIntEQ(outSz, sizeof(aad));
  52794. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52795. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52796. ExpectIntEQ(outSz, sizeof(cipherText));
  52797. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  52798. WOLFSSL_SUCCESS);
  52799. ExpectIntEQ(outSz, 0);
  52800. EVP_CIPHER_CTX_free(ctx);
  52801. #endif
  52802. return EXPECT_RESULT();
  52803. }
  52804. static int test_wolfssl_EVP_chacha20(void)
  52805. {
  52806. EXPECT_DECLS;
  52807. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  52808. byte key[CHACHA_MAX_KEY_SZ];
  52809. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  52810. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  52811. byte cipherText[sizeof(plainText)];
  52812. byte decryptedText[sizeof(plainText)];
  52813. EVP_CIPHER_CTX* ctx = NULL;
  52814. int outSz;
  52815. XMEMSET(key, 0, sizeof(key));
  52816. XMEMSET(iv, 0, sizeof(iv));
  52817. /* Encrypt. */
  52818. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52819. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  52820. NULL), WOLFSSL_SUCCESS);
  52821. /* Any tag length must fail - not an AEAD cipher. */
  52822. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  52823. 16, NULL), WOLFSSL_FAILURE);
  52824. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52825. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  52826. sizeof(plainText)), WOLFSSL_SUCCESS);
  52827. ExpectIntEQ(outSz, sizeof(plainText));
  52828. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  52829. ExpectIntEQ(outSz, 0);
  52830. EVP_CIPHER_CTX_free(ctx);
  52831. ctx = NULL;
  52832. /* Decrypt. */
  52833. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52834. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  52835. NULL), WOLFSSL_SUCCESS);
  52836. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52837. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52838. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52839. ExpectIntEQ(outSz, sizeof(cipherText));
  52840. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  52841. WOLFSSL_SUCCESS);
  52842. ExpectIntEQ(outSz, 0);
  52843. EVP_CIPHER_CTX_free(ctx);
  52844. ctx = NULL;
  52845. /* Test partial Inits. CipherInit() allow setting of key and iv
  52846. * in separate calls. */
  52847. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52848. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  52849. key, NULL, 1), WOLFSSL_SUCCESS);
  52850. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  52851. WOLFSSL_SUCCESS);
  52852. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52853. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52854. ExpectIntEQ(outSz, sizeof(cipherText));
  52855. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  52856. WOLFSSL_SUCCESS);
  52857. ExpectIntEQ(outSz, 0);
  52858. EVP_CIPHER_CTX_free(ctx);
  52859. #endif
  52860. return EXPECT_RESULT();
  52861. }
  52862. static int test_wolfssl_EVP_sm4_ecb(void)
  52863. {
  52864. int res = TEST_SKIPPED;
  52865. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  52866. EXPECT_DECLS;
  52867. byte key[SM4_KEY_SIZE];
  52868. byte plainText[SM4_BLOCK_SIZE] = {
  52869. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  52870. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  52871. };
  52872. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  52873. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  52874. EVP_CIPHER_CTX* ctx;
  52875. int outSz;
  52876. XMEMSET(key, 0, sizeof(key));
  52877. /* Encrypt. */
  52878. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52879. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  52880. WOLFSSL_SUCCESS);
  52881. /* Any tag length must fail - not an AEAD cipher. */
  52882. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  52883. WOLFSSL_FAILURE);
  52884. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  52885. WOLFSSL_SUCCESS);
  52886. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  52887. sizeof(plainText)), WOLFSSL_SUCCESS);
  52888. ExpectIntEQ(outSz, sizeof(plainText));
  52889. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  52890. WOLFSSL_SUCCESS);
  52891. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  52892. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  52893. EVP_CIPHER_CTX_free(ctx);
  52894. /* Decrypt. */
  52895. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52896. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  52897. WOLFSSL_SUCCESS);
  52898. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  52899. WOLFSSL_SUCCESS);
  52900. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52901. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52902. ExpectIntEQ(outSz, sizeof(plainText));
  52903. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  52904. WOLFSSL_SUCCESS);
  52905. ExpectIntEQ(outSz, 0);
  52906. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  52907. EVP_CIPHER_CTX_free(ctx);
  52908. res = EXPECT_RESULT();
  52909. #endif
  52910. return res;
  52911. }
  52912. static int test_wolfssl_EVP_sm4_cbc(void)
  52913. {
  52914. int res = TEST_SKIPPED;
  52915. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  52916. EXPECT_DECLS;
  52917. byte key[SM4_KEY_SIZE];
  52918. byte iv[SM4_BLOCK_SIZE];
  52919. byte plainText[SM4_BLOCK_SIZE] = {
  52920. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  52921. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  52922. };
  52923. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  52924. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  52925. EVP_CIPHER_CTX* ctx;
  52926. int outSz;
  52927. XMEMSET(key, 0, sizeof(key));
  52928. XMEMSET(iv, 0, sizeof(iv));
  52929. /* Encrypt. */
  52930. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52931. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  52932. WOLFSSL_SUCCESS);
  52933. /* Any tag length must fail - not an AEAD cipher. */
  52934. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  52935. WOLFSSL_FAILURE);
  52936. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52937. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  52938. sizeof(plainText)), WOLFSSL_SUCCESS);
  52939. ExpectIntEQ(outSz, sizeof(plainText));
  52940. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  52941. WOLFSSL_SUCCESS);
  52942. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  52943. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  52944. EVP_CIPHER_CTX_free(ctx);
  52945. /* Decrypt. */
  52946. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52947. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  52948. WOLFSSL_SUCCESS);
  52949. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52950. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52951. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52952. ExpectIntEQ(outSz, sizeof(plainText));
  52953. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  52954. WOLFSSL_SUCCESS);
  52955. ExpectIntEQ(outSz, 0);
  52956. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  52957. EVP_CIPHER_CTX_free(ctx);
  52958. /* Test partial Inits. CipherInit() allow setting of key and iv
  52959. * in separate calls. */
  52960. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52961. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  52962. WOLFSSL_SUCCESS);
  52963. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  52964. WOLFSSL_SUCCESS);
  52965. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52966. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52967. ExpectIntEQ(outSz, sizeof(plainText));
  52968. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  52969. WOLFSSL_SUCCESS);
  52970. ExpectIntEQ(outSz, 0);
  52971. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  52972. EVP_CIPHER_CTX_free(ctx);
  52973. res = EXPECT_RESULT();
  52974. #endif
  52975. return res;
  52976. }
  52977. static int test_wolfssl_EVP_sm4_ctr(void)
  52978. {
  52979. int res = TEST_SKIPPED;
  52980. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  52981. EXPECT_DECLS;
  52982. byte key[SM4_KEY_SIZE];
  52983. byte iv[SM4_BLOCK_SIZE];
  52984. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  52985. byte cipherText[sizeof(plainText)];
  52986. byte decryptedText[sizeof(plainText)];
  52987. EVP_CIPHER_CTX* ctx;
  52988. int outSz;
  52989. XMEMSET(key, 0, sizeof(key));
  52990. XMEMSET(iv, 0, sizeof(iv));
  52991. /* Encrypt. */
  52992. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52993. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  52994. WOLFSSL_SUCCESS);
  52995. /* Any tag length must fail - not an AEAD cipher. */
  52996. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  52997. WOLFSSL_FAILURE);
  52998. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52999. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  53000. sizeof(plainText)), WOLFSSL_SUCCESS);
  53001. ExpectIntEQ(outSz, sizeof(plainText));
  53002. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  53003. ExpectIntEQ(outSz, 0);
  53004. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  53005. EVP_CIPHER_CTX_free(ctx);
  53006. /* Decrypt. */
  53007. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  53008. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  53009. WOLFSSL_SUCCESS);
  53010. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  53011. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  53012. sizeof(cipherText)), WOLFSSL_SUCCESS);
  53013. ExpectIntEQ(outSz, sizeof(cipherText));
  53014. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  53015. WOLFSSL_SUCCESS);
  53016. ExpectIntEQ(outSz, 0);
  53017. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  53018. EVP_CIPHER_CTX_free(ctx);
  53019. /* Test partial Inits. CipherInit() allow setting of key and iv
  53020. * in separate calls. */
  53021. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  53022. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  53023. WOLFSSL_SUCCESS);
  53024. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  53025. WOLFSSL_SUCCESS);
  53026. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  53027. sizeof(cipherText)), WOLFSSL_SUCCESS);
  53028. ExpectIntEQ(outSz, sizeof(cipherText));
  53029. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  53030. WOLFSSL_SUCCESS);
  53031. ExpectIntEQ(outSz, 0);
  53032. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  53033. EVP_CIPHER_CTX_free(ctx);
  53034. res = EXPECT_RESULT();
  53035. #endif
  53036. return res;
  53037. }
  53038. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  53039. {
  53040. int res = TEST_SKIPPED;
  53041. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  53042. /* Zero length plain text */
  53043. EXPECT_DECLS;
  53044. byte key[] = {
  53045. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53047. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53048. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  53049. }; /* align */
  53050. byte iv[] = {
  53051. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  53052. }; /* align */
  53053. byte plaintxt[1];
  53054. int ivSz = 12;
  53055. int plaintxtSz = 0;
  53056. unsigned char tag[16];
  53057. unsigned char tag_kat[16] = {
  53058. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  53059. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  53060. };
  53061. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  53062. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  53063. int ciphertxtSz = 0;
  53064. int decryptedtxtSz = 0;
  53065. int len = 0;
  53066. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  53067. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  53068. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  53069. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  53070. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  53071. plaintxtSz));
  53072. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  53073. ciphertxtSz += len;
  53074. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  53075. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  53076. ExpectIntEQ(0, ciphertxtSz);
  53077. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  53078. EVP_CIPHER_CTX_init(de);
  53079. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  53080. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  53081. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  53082. decryptedtxtSz = len;
  53083. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  53084. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  53085. decryptedtxtSz += len;
  53086. ExpectIntEQ(0, decryptedtxtSz);
  53087. EVP_CIPHER_CTX_free(en);
  53088. EVP_CIPHER_CTX_free(de);
  53089. res = EXPECT_RESULT();
  53090. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  53091. return res;
  53092. }
  53093. static int test_wolfssl_EVP_sm4_gcm(void)
  53094. {
  53095. int res = TEST_SKIPPED;
  53096. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  53097. EXPECT_DECLS;
  53098. byte *key = (byte*)"0123456789012345";
  53099. /* A 128 bit IV */
  53100. byte *iv = (byte*)"0123456789012345";
  53101. int ivSz = SM4_BLOCK_SIZE;
  53102. /* Message to be encrypted */
  53103. byte *plaintxt = (byte*)"for things to change you have to change";
  53104. /* Additional non-confidential data */
  53105. byte *aad = (byte*)"Don't spend major time on minor things.";
  53106. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  53107. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  53108. int aadSz = (int)XSTRLEN((char*)aad);
  53109. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  53110. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  53111. int ciphertxtSz = 0;
  53112. int decryptedtxtSz = 0;
  53113. int len = 0;
  53114. int i = 0;
  53115. EVP_CIPHER_CTX en[2];
  53116. EVP_CIPHER_CTX de[2];
  53117. for (i = 0; i < 2; i++) {
  53118. EVP_CIPHER_CTX_init(&en[i]);
  53119. if (i == 0) {
  53120. /* Default uses 96-bits IV length */
  53121. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  53122. iv));
  53123. }
  53124. else {
  53125. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  53126. NULL));
  53127. /* non-default must to set the IV length first */
  53128. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  53129. ivSz, NULL));
  53130. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  53131. }
  53132. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  53133. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  53134. plaintxtSz));
  53135. ciphertxtSz = len;
  53136. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  53137. ciphertxtSz += len;
  53138. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  53139. SM4_BLOCK_SIZE, tag));
  53140. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  53141. EVP_CIPHER_CTX_init(&de[i]);
  53142. if (i == 0) {
  53143. /* Default uses 96-bits IV length */
  53144. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  53145. iv));
  53146. }
  53147. else {
  53148. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  53149. NULL));
  53150. /* non-default must to set the IV length first */
  53151. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  53152. ivSz, NULL));
  53153. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  53154. }
  53155. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  53156. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  53157. ciphertxtSz));
  53158. decryptedtxtSz = len;
  53159. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  53160. SM4_BLOCK_SIZE, tag));
  53161. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  53162. decryptedtxtSz += len;
  53163. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  53164. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  53165. /* modify tag*/
  53166. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  53167. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  53168. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  53169. SM4_BLOCK_SIZE, tag));
  53170. /* fail due to wrong tag */
  53171. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  53172. ciphertxtSz));
  53173. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  53174. ExpectIntEQ(0, len);
  53175. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  53176. }
  53177. res = EXPECT_RESULT();
  53178. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  53179. return res;
  53180. }
  53181. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  53182. {
  53183. int res = TEST_SKIPPED;
  53184. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  53185. /* Zero length plain text */
  53186. EXPECT_DECLS;
  53187. byte key[] = {
  53188. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53189. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53190. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53191. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  53192. }; /* align */
  53193. byte iv[] = {
  53194. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  53195. }; /* align */
  53196. byte plaintxt[1];
  53197. int ivSz = 12;
  53198. int plaintxtSz = 0;
  53199. unsigned char tag[16];
  53200. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  53201. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  53202. int ciphertxtSz = 0;
  53203. int decryptedtxtSz = 0;
  53204. int len = 0;
  53205. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  53206. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  53207. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  53208. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  53209. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  53210. plaintxtSz));
  53211. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  53212. ciphertxtSz += len;
  53213. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  53214. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  53215. ExpectIntEQ(0, ciphertxtSz);
  53216. EVP_CIPHER_CTX_init(de);
  53217. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  53218. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  53219. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  53220. decryptedtxtSz = len;
  53221. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  53222. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  53223. decryptedtxtSz += len;
  53224. ExpectIntEQ(0, decryptedtxtSz);
  53225. EVP_CIPHER_CTX_free(en);
  53226. EVP_CIPHER_CTX_free(de);
  53227. res = EXPECT_RESULT();
  53228. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  53229. return res;
  53230. }
  53231. static int test_wolfssl_EVP_sm4_ccm(void)
  53232. {
  53233. int res = TEST_SKIPPED;
  53234. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  53235. EXPECT_DECLS;
  53236. byte *key = (byte*)"0123456789012345";
  53237. byte *iv = (byte*)"0123456789012";
  53238. int ivSz = (int)XSTRLEN((char*)iv);
  53239. /* Message to be encrypted */
  53240. byte *plaintxt = (byte*)"for things to change you have to change";
  53241. /* Additional non-confidential data */
  53242. byte *aad = (byte*)"Don't spend major time on minor things.";
  53243. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  53244. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  53245. int aadSz = (int)XSTRLEN((char*)aad);
  53246. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  53247. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  53248. int ciphertxtSz = 0;
  53249. int decryptedtxtSz = 0;
  53250. int len = 0;
  53251. int i = 0;
  53252. EVP_CIPHER_CTX en[2];
  53253. EVP_CIPHER_CTX de[2];
  53254. for (i = 0; i < 2; i++) {
  53255. EVP_CIPHER_CTX_init(&en[i]);
  53256. if (i == 0) {
  53257. /* Default uses 96-bits IV length */
  53258. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  53259. iv));
  53260. }
  53261. else {
  53262. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  53263. NULL));
  53264. /* non-default must to set the IV length first */
  53265. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  53266. ivSz, NULL));
  53267. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  53268. }
  53269. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  53270. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  53271. plaintxtSz));
  53272. ciphertxtSz = len;
  53273. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  53274. ciphertxtSz += len;
  53275. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  53276. SM4_BLOCK_SIZE, tag));
  53277. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  53278. EVP_CIPHER_CTX_init(&de[i]);
  53279. if (i == 0) {
  53280. /* Default uses 96-bits IV length */
  53281. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  53282. iv));
  53283. }
  53284. else {
  53285. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  53286. NULL));
  53287. /* non-default must to set the IV length first */
  53288. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  53289. ivSz, NULL));
  53290. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  53291. }
  53292. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  53293. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  53294. ciphertxtSz));
  53295. decryptedtxtSz = len;
  53296. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  53297. SM4_BLOCK_SIZE, tag));
  53298. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  53299. decryptedtxtSz += len;
  53300. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  53301. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  53302. /* modify tag*/
  53303. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  53304. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  53305. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  53306. SM4_BLOCK_SIZE, tag));
  53307. /* fail due to wrong tag */
  53308. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  53309. ciphertxtSz));
  53310. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  53311. ExpectIntEQ(0, len);
  53312. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  53313. }
  53314. res = EXPECT_RESULT();
  53315. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  53316. return res;
  53317. }
  53318. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  53319. {
  53320. EXPECT_DECLS;
  53321. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  53322. EVP_PKEY_CTX* ctx = NULL;
  53323. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  53324. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  53325. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  53326. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  53327. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  53328. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  53329. byte outKey[34];
  53330. size_t outKeySz = sizeof(outKey);
  53331. /* These expected outputs were gathered by running the same test below using
  53332. * OpenSSL. */
  53333. const byte extractAndExpand[] = {
  53334. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  53335. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  53336. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  53337. };
  53338. const byte extractOnly[] = {
  53339. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  53340. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  53341. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  53342. };
  53343. const byte expandOnly[] = {
  53344. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  53345. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  53346. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  53347. };
  53348. const byte extractAndExpandAddInfo[] = {
  53349. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  53350. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  53351. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  53352. };
  53353. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  53354. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  53355. /* NULL ctx. */
  53356. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  53357. /* NULL md. */
  53358. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  53359. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  53360. /* NULL ctx. */
  53361. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  53362. WOLFSSL_FAILURE);
  53363. /* NULL salt is ok. */
  53364. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  53365. WOLFSSL_SUCCESS);
  53366. /* Salt length <= 0. */
  53367. /* Length 0 salt is ok. */
  53368. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  53369. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  53370. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  53371. WOLFSSL_SUCCESS);
  53372. /* NULL ctx. */
  53373. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  53374. WOLFSSL_FAILURE);
  53375. /* NULL key. */
  53376. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  53377. WOLFSSL_FAILURE);
  53378. /* Key length <= 0 */
  53379. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  53380. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  53381. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  53382. WOLFSSL_SUCCESS);
  53383. /* NULL ctx. */
  53384. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  53385. WOLFSSL_FAILURE);
  53386. /* NULL info is ok. */
  53387. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  53388. WOLFSSL_SUCCESS);
  53389. /* Info length <= 0 */
  53390. /* Length 0 info is ok. */
  53391. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  53392. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  53393. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  53394. WOLFSSL_SUCCESS);
  53395. /* NULL ctx. */
  53396. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  53397. WOLFSSL_FAILURE);
  53398. /* Extract and expand (default). */
  53399. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  53400. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  53401. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  53402. /* Extract only. */
  53403. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  53404. WOLFSSL_SUCCESS);
  53405. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  53406. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  53407. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  53408. outKeySz = sizeof(outKey);
  53409. /* Expand only. */
  53410. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  53411. WOLFSSL_SUCCESS);
  53412. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  53413. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  53414. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  53415. outKeySz = sizeof(outKey);
  53416. /* Extract and expand with appended additional info. */
  53417. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  53418. WOLFSSL_SUCCESS);
  53419. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  53420. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  53421. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  53422. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  53423. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  53424. EVP_PKEY_CTX_free(ctx);
  53425. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  53426. return EXPECT_RESULT();
  53427. }
  53428. #ifndef NO_BIO
  53429. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  53430. {
  53431. EXPECT_DECLS;
  53432. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  53433. BIO* bio = NULL;
  53434. X509_INFO* info = NULL;
  53435. STACK_OF(X509_INFO)* sk = NULL;
  53436. char* subject = NULL;
  53437. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  53438. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  53439. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  53440. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  53441. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  53442. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  53443. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  53444. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  53445. /* using dereference to maintain testing for Apache port*/
  53446. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  53447. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  53448. 0, 0));
  53449. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  53450. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  53451. X509_INFO_free(info);
  53452. info = NULL;
  53453. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  53454. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  53455. 0, 0));
  53456. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  53457. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  53458. X509_INFO_free(info);
  53459. ExpectNull(info = sk_X509_INFO_pop(sk));
  53460. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  53461. BIO_free(bio);
  53462. #endif
  53463. return EXPECT_RESULT();
  53464. }
  53465. #endif /* !NO_BIO */
  53466. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  53467. {
  53468. EXPECT_DECLS;
  53469. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  53470. X509 *x509 = NULL;
  53471. X509_NAME* name = NULL;
  53472. int idx = 0;
  53473. X509_NAME_ENTRY *ne = NULL;
  53474. ASN1_OBJECT *object = NULL;
  53475. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  53476. WOLFSSL_FILETYPE_PEM));
  53477. ExpectNotNull(name = X509_get_subject_name(x509));
  53478. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  53479. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  53480. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  53481. X509_free(x509);
  53482. #endif
  53483. return EXPECT_RESULT();
  53484. }
  53485. static int test_wolfSSL_X509_STORE_get1_certs(void)
  53486. {
  53487. EXPECT_DECLS;
  53488. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  53489. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  53490. X509_STORE_CTX *storeCtx = NULL;
  53491. X509_STORE *store = NULL;
  53492. X509 *caX509 = NULL;
  53493. X509 *svrX509 = NULL;
  53494. X509_NAME *subject = NULL;
  53495. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  53496. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  53497. SSL_FILETYPE_PEM));
  53498. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  53499. SSL_FILETYPE_PEM)));
  53500. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  53501. ExpectNotNull(store = X509_STORE_new());
  53502. ExpectNotNull(subject = X509_get_subject_name(caX509));
  53503. /* Errors */
  53504. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  53505. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  53506. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  53507. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  53508. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  53509. SSL_SUCCESS);
  53510. /* Should find the cert */
  53511. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  53512. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  53513. sk_X509_pop_free(certs, NULL);
  53514. certs = NULL;
  53515. /* Should not find the cert */
  53516. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  53517. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  53518. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  53519. sk_X509_pop_free(certs, NULL);
  53520. certs = NULL;
  53521. X509_STORE_free(store);
  53522. X509_STORE_CTX_free(storeCtx);
  53523. X509_free(svrX509);
  53524. X509_free(caX509);
  53525. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  53526. return EXPECT_RESULT();
  53527. }
  53528. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  53529. defined(WOLFSSL_LOCAL_X509_STORE) && \
  53530. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  53531. static int test_wolfSSL_X509_STORE_set_get_crl_provider(X509_STORE_CTX* ctx,
  53532. X509_CRL** crl_out, X509* cert) {
  53533. X509_CRL *crl = NULL;
  53534. XFILE fp = XBADFILE;
  53535. char* cert_issuer = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
  53536. int ret = 0;
  53537. (void)ctx;
  53538. if (cert_issuer == NULL)
  53539. return 0;
  53540. if ((fp = XFOPEN("certs/crl/crl.pem", "rb")) != XBADFILE) {
  53541. PEM_read_X509_CRL(fp, &crl, NULL, NULL);
  53542. XFCLOSE(fp);
  53543. if (crl != NULL) {
  53544. char* crl_issuer = X509_NAME_oneline(
  53545. X509_CRL_get_issuer(crl), NULL, 0);
  53546. if ((crl_issuer != NULL) &&
  53547. (XSTRCMP(cert_issuer, crl_issuer) == 0)) {
  53548. *crl_out = X509_CRL_dup(crl);
  53549. if (*crl_out != NULL)
  53550. ret = 1;
  53551. }
  53552. OPENSSL_free(crl_issuer);
  53553. }
  53554. }
  53555. X509_CRL_free(crl);
  53556. OPENSSL_free(cert_issuer);
  53557. return ret;
  53558. }
  53559. static int test_wolfSSL_X509_STORE_set_get_crl_provider2(X509_STORE_CTX* ctx,
  53560. X509_CRL** crl_out, X509* cert) {
  53561. (void)ctx;
  53562. (void)cert;
  53563. *crl_out = NULL;
  53564. return 1;
  53565. }
  53566. #ifndef NO_WOLFSSL_STUB
  53567. static int test_wolfSSL_X509_STORE_set_get_crl_check(X509_STORE_CTX* ctx,
  53568. X509_CRL* crl) {
  53569. (void)ctx;
  53570. (void)crl;
  53571. return 1;
  53572. }
  53573. #endif
  53574. static int test_wolfSSL_X509_STORE_set_get_crl_verify(int ok,
  53575. X509_STORE_CTX* ctx) {
  53576. int cert_error = X509_STORE_CTX_get_error(ctx);
  53577. X509_VERIFY_PARAM* param = X509_STORE_CTX_get0_param(ctx);
  53578. int flags = X509_VERIFY_PARAM_get_flags(param);
  53579. if ((flags & (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) !=
  53580. (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) {
  53581. /* Make sure the flags are set */
  53582. return 0;
  53583. }
  53584. /* Ignore CRL missing error */
  53585. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  53586. if (cert_error == CRL_MISSING)
  53587. #else
  53588. if (cert_error == X509_V_ERR_UNABLE_TO_GET_CRL)
  53589. #endif
  53590. return 1;
  53591. return ok;
  53592. }
  53593. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready(WOLFSSL_CTX* ctx)
  53594. {
  53595. EXPECT_DECLS;
  53596. X509_STORE* cert_store = NULL;
  53597. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  53598. WOLFSSL_SUCCESS);
  53599. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  53600. X509_STORE_set_get_crl(cert_store,
  53601. test_wolfSSL_X509_STORE_set_get_crl_provider);
  53602. #ifndef NO_WOLFSSL_STUB
  53603. X509_STORE_set_check_crl(cert_store,
  53604. test_wolfSSL_X509_STORE_set_get_crl_check);
  53605. #endif
  53606. return EXPECT_RESULT();
  53607. }
  53608. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2(WOLFSSL_CTX* ctx)
  53609. {
  53610. EXPECT_DECLS;
  53611. X509_STORE* cert_store = NULL;
  53612. X509_VERIFY_PARAM* param = NULL;
  53613. SSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  53614. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  53615. WOLFSSL_SUCCESS);
  53616. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  53617. X509_STORE_set_get_crl(cert_store,
  53618. test_wolfSSL_X509_STORE_set_get_crl_provider2);
  53619. #ifndef NO_WOLFSSL_STUB
  53620. X509_STORE_set_check_crl(cert_store,
  53621. test_wolfSSL_X509_STORE_set_get_crl_check);
  53622. #endif
  53623. X509_STORE_set_verify_cb(cert_store,
  53624. test_wolfSSL_X509_STORE_set_get_crl_verify);
  53625. ExpectNotNull(param = X509_STORE_get0_param(cert_store));
  53626. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(
  53627. param, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  53628. ExpectIntEQ(X509_STORE_set_flags(cert_store,
  53629. X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  53630. return EXPECT_RESULT();
  53631. }
  53632. #endif
  53633. /* This test mimics the usage of the CRL provider in gRPC */
  53634. static int test_wolfSSL_X509_STORE_set_get_crl(void)
  53635. {
  53636. EXPECT_DECLS;
  53637. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  53638. defined(WOLFSSL_LOCAL_X509_STORE) && \
  53639. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  53640. test_ssl_cbf func_cb_client;
  53641. test_ssl_cbf func_cb_server;
  53642. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  53643. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  53644. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready;
  53645. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  53646. &func_cb_server, NULL), TEST_SUCCESS);
  53647. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  53648. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  53649. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2;
  53650. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  53651. &func_cb_server, NULL), TEST_SUCCESS);
  53652. #endif
  53653. return EXPECT_RESULT();
  53654. }
  53655. static int test_wolfSSL_dup_CA_list(void)
  53656. {
  53657. int res = TEST_SKIPPED;
  53658. #if defined(OPENSSL_ALL)
  53659. EXPECT_DECLS;
  53660. STACK_OF(X509_NAME) *originalStack = NULL;
  53661. STACK_OF(X509_NAME) *copyStack = NULL;
  53662. int originalCount = 0;
  53663. int copyCount = 0;
  53664. X509_NAME *name = NULL;
  53665. int i;
  53666. originalStack = sk_X509_NAME_new_null();
  53667. ExpectNotNull(originalStack);
  53668. for (i = 0; i < 3; i++) {
  53669. name = X509_NAME_new();
  53670. ExpectNotNull(name);
  53671. ExpectIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  53672. if (EXPECT_FAIL()) {
  53673. X509_NAME_free(name);
  53674. }
  53675. }
  53676. copyStack = SSL_dup_CA_list(originalStack);
  53677. ExpectNotNull(copyStack);
  53678. originalCount = sk_X509_NAME_num(originalStack);
  53679. copyCount = sk_X509_NAME_num(copyStack);
  53680. ExpectIntEQ(originalCount, copyCount);
  53681. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  53682. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  53683. originalStack = NULL;
  53684. copyStack = NULL;
  53685. res = EXPECT_RESULT();
  53686. #endif /* OPENSSL_ALL */
  53687. return res;
  53688. }
  53689. static int test_ForceZero(void)
  53690. {
  53691. EXPECT_DECLS;
  53692. unsigned char data[32];
  53693. unsigned int i, j, len;
  53694. /* Test case with 0 length */
  53695. ForceZero(data, 0);
  53696. /* Test ForceZero */
  53697. for (i = 0; i < sizeof(data); i++) {
  53698. for (len = 1; len < sizeof(data) - i; len++) {
  53699. for (j = 0; j < sizeof(data); j++)
  53700. data[j] = j + 1;
  53701. ForceZero(data + i, len);
  53702. for (j = 0; j < sizeof(data); j++) {
  53703. if (j < i || j >= i + len) {
  53704. ExpectIntNE(data[j], 0x00);
  53705. }
  53706. else {
  53707. ExpectIntEQ(data[j], 0x00);
  53708. }
  53709. }
  53710. }
  53711. }
  53712. return EXPECT_RESULT();
  53713. }
  53714. #ifndef NO_BIO
  53715. static int test_wolfSSL_X509_print(void)
  53716. {
  53717. EXPECT_DECLS;
  53718. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  53719. !defined(NO_RSA) && defined(XSNPRINTF)
  53720. X509 *x509 = NULL;
  53721. BIO *bio = NULL;
  53722. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  53723. const X509_ALGOR *cert_sig_alg = NULL;
  53724. #endif
  53725. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  53726. WOLFSSL_FILETYPE_PEM));
  53727. /* print to memory */
  53728. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53729. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  53730. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  53731. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  53732. /* Will print IP address subject alt name. */
  53733. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  53734. #elif defined(NO_ASN_TIME)
  53735. /* Will print IP address subject alt name but not Validity. */
  53736. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  53737. #else
  53738. /* Will print IP address subject alt name. */
  53739. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  53740. #endif
  53741. #elif defined(NO_ASN_TIME)
  53742. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  53743. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  53744. #else
  53745. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  53746. #endif
  53747. BIO_free(bio);
  53748. bio = NULL;
  53749. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  53750. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  53751. /* Print signature */
  53752. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  53753. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  53754. #endif
  53755. /* print to stderr */
  53756. #if !defined(NO_WOLFSSL_DIR)
  53757. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  53758. #endif
  53759. /* print again */
  53760. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  53761. X509_free(x509);
  53762. BIO_free(bio);
  53763. #endif
  53764. return EXPECT_RESULT();
  53765. }
  53766. static int test_wolfSSL_X509_CRL_print(void)
  53767. {
  53768. EXPECT_DECLS;
  53769. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  53770. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  53771. X509_CRL* crl = NULL;
  53772. BIO *bio = NULL;
  53773. XFILE fp = XBADFILE;
  53774. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  53775. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  53776. NULL, NULL));
  53777. if (fp != XBADFILE)
  53778. XFCLOSE(fp);
  53779. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53780. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  53781. X509_CRL_free(crl);
  53782. BIO_free(bio);
  53783. #endif
  53784. return EXPECT_RESULT();
  53785. }
  53786. static int test_wolfSSL_BIO_get_len(void)
  53787. {
  53788. EXPECT_DECLS;
  53789. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53790. BIO *bio = NULL;
  53791. const char txt[] = "Some example text to push to the BIO.";
  53792. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  53793. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  53794. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  53795. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  53796. BIO_free(bio);
  53797. bio = NULL;
  53798. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  53799. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  53800. BIO_free(bio);
  53801. #endif
  53802. return EXPECT_RESULT();
  53803. }
  53804. #endif /* !NO_BIO */
  53805. static int test_wolfSSL_RSA(void)
  53806. {
  53807. EXPECT_DECLS;
  53808. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  53809. RSA* rsa = NULL;
  53810. const BIGNUM *n = NULL;
  53811. const BIGNUM *e = NULL;
  53812. const BIGNUM *d = NULL;
  53813. const BIGNUM *p = NULL;
  53814. const BIGNUM *q = NULL;
  53815. const BIGNUM *dmp1 = NULL;
  53816. const BIGNUM *dmq1 = NULL;
  53817. const BIGNUM *iqmp = NULL;
  53818. ExpectNotNull(rsa = RSA_new());
  53819. ExpectIntEQ(RSA_size(NULL), 0);
  53820. ExpectIntEQ(RSA_size(rsa), 0);
  53821. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  53822. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  53823. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  53824. #ifdef WOLFSSL_RSA_KEY_CHECK
  53825. ExpectIntEQ(RSA_check_key(rsa), 0);
  53826. #endif
  53827. RSA_free(rsa);
  53828. rsa = NULL;
  53829. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  53830. ExpectIntEQ(RSA_size(rsa), 256);
  53831. #ifdef WOLFSSL_RSA_KEY_CHECK
  53832. ExpectIntEQ(RSA_check_key(NULL), 0);
  53833. ExpectIntEQ(RSA_check_key(rsa), 1);
  53834. #endif
  53835. /* sanity check */
  53836. ExpectIntEQ(RSA_bits(NULL), 0);
  53837. /* key */
  53838. ExpectIntEQ(RSA_bits(rsa), 2048);
  53839. RSA_get0_key(rsa, &n, &e, &d);
  53840. ExpectPtrEq(rsa->n, n);
  53841. ExpectPtrEq(rsa->e, e);
  53842. ExpectPtrEq(rsa->d, d);
  53843. n = NULL;
  53844. e = NULL;
  53845. d = NULL;
  53846. ExpectNotNull(n = BN_new());
  53847. ExpectNotNull(e = BN_new());
  53848. ExpectNotNull(d = BN_new());
  53849. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  53850. if (EXPECT_FAIL()) {
  53851. BN_free((BIGNUM*)n);
  53852. BN_free((BIGNUM*)e);
  53853. BN_free((BIGNUM*)d);
  53854. }
  53855. ExpectPtrEq(rsa->n, n);
  53856. ExpectPtrEq(rsa->e, e);
  53857. ExpectPtrEq(rsa->d, d);
  53858. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  53859. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  53860. /* crt_params */
  53861. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  53862. ExpectPtrEq(rsa->dmp1, dmp1);
  53863. ExpectPtrEq(rsa->dmq1, dmq1);
  53864. ExpectPtrEq(rsa->iqmp, iqmp);
  53865. dmp1 = NULL;
  53866. dmq1 = NULL;
  53867. iqmp = NULL;
  53868. ExpectNotNull(dmp1 = BN_new());
  53869. ExpectNotNull(dmq1 = BN_new());
  53870. ExpectNotNull(iqmp = BN_new());
  53871. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  53872. (BIGNUM*)iqmp), 1);
  53873. if (EXPECT_FAIL()) {
  53874. BN_free((BIGNUM*)dmp1);
  53875. BN_free((BIGNUM*)dmq1);
  53876. BN_free((BIGNUM*)iqmp);
  53877. }
  53878. ExpectPtrEq(rsa->dmp1, dmp1);
  53879. ExpectPtrEq(rsa->dmq1, dmq1);
  53880. ExpectPtrEq(rsa->iqmp, iqmp);
  53881. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  53882. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  53883. (BIGNUM*)iqmp), 0);
  53884. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  53885. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  53886. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  53887. ExpectNull(dmp1);
  53888. ExpectNull(dmq1);
  53889. ExpectNull(iqmp);
  53890. /* factors */
  53891. RSA_get0_factors(rsa, NULL, NULL);
  53892. RSA_get0_factors(rsa, &p, &q);
  53893. ExpectPtrEq(rsa->p, p);
  53894. ExpectPtrEq(rsa->q, q);
  53895. p = NULL;
  53896. q = NULL;
  53897. ExpectNotNull(p = BN_new());
  53898. ExpectNotNull(q = BN_new());
  53899. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  53900. if (EXPECT_FAIL()) {
  53901. BN_free((BIGNUM*)p);
  53902. BN_free((BIGNUM*)q);
  53903. }
  53904. ExpectPtrEq(rsa->p, p);
  53905. ExpectPtrEq(rsa->q, q);
  53906. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  53907. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  53908. RSA_get0_factors(NULL, NULL, NULL);
  53909. RSA_get0_factors(NULL, &p, &q);
  53910. ExpectNull(p);
  53911. ExpectNull(q);
  53912. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  53913. ExpectIntEQ(RSA_bits(rsa), 21);
  53914. RSA_free(rsa);
  53915. rsa = NULL;
  53916. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  53917. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  53918. ExpectIntEQ(RSA_size(rsa), 384);
  53919. ExpectIntEQ(RSA_bits(rsa), 3072);
  53920. RSA_free(rsa);
  53921. rsa = NULL;
  53922. #endif
  53923. /* remove for now with odd key size until adjusting rsa key size check with
  53924. wc_MakeRsaKey()
  53925. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  53926. RSA_free(rsa);
  53927. rsa = NULL;
  53928. */
  53929. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  53930. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  53931. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  53932. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  53933. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  53934. {
  53935. byte buff[FOURK_BUF];
  53936. byte der[FOURK_BUF];
  53937. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  53938. XFILE f = XBADFILE;
  53939. int bytes = 0;
  53940. /* test loading encrypted RSA private pem w/o password */
  53941. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  53942. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  53943. if (f != XBADFILE)
  53944. XFCLOSE(f);
  53945. XMEMSET(der, 0, sizeof(der));
  53946. /* test that error value is returned with no password */
  53947. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  53948. 0);
  53949. }
  53950. #endif
  53951. #endif
  53952. return EXPECT_RESULT();
  53953. }
  53954. static int test_wolfSSL_RSA_DER(void)
  53955. {
  53956. EXPECT_DECLS;
  53957. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  53958. RSA *rsa = NULL;
  53959. int i;
  53960. const unsigned char *buff = NULL;
  53961. unsigned char *newBuff = NULL;
  53962. struct tbl_s
  53963. {
  53964. const unsigned char *der;
  53965. int sz;
  53966. } tbl[] = {
  53967. #ifdef USE_CERT_BUFFERS_1024
  53968. {client_key_der_1024, sizeof_client_key_der_1024},
  53969. {server_key_der_1024, sizeof_server_key_der_1024},
  53970. #endif
  53971. #ifdef USE_CERT_BUFFERS_2048
  53972. {client_key_der_2048, sizeof_client_key_der_2048},
  53973. {server_key_der_2048, sizeof_server_key_der_2048},
  53974. #endif
  53975. {NULL, 0}
  53976. };
  53977. /* Public Key DER */
  53978. struct tbl_s pub[] = {
  53979. #ifdef USE_CERT_BUFFERS_1024
  53980. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  53981. #endif
  53982. #ifdef USE_CERT_BUFFERS_2048
  53983. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  53984. #endif
  53985. {NULL, 0}
  53986. };
  53987. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  53988. buff = pub[0].der;
  53989. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  53990. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  53991. buff = tbl[0].der;
  53992. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  53993. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  53994. rsa = RSA_new();
  53995. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  53996. RSA_free(rsa);
  53997. rsa = NULL;
  53998. for (i = 0; tbl[i].der != NULL; i++)
  53999. {
  54000. /* Passing in pointer results in pointer moving. */
  54001. buff = tbl[i].der;
  54002. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  54003. ExpectNotNull(rsa);
  54004. RSA_free(rsa);
  54005. rsa = NULL;
  54006. }
  54007. for (i = 0; tbl[i].der != NULL; i++)
  54008. {
  54009. /* Passing in pointer results in pointer moving. */
  54010. buff = tbl[i].der;
  54011. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  54012. ExpectNotNull(rsa);
  54013. RSA_free(rsa);
  54014. rsa = NULL;
  54015. }
  54016. for (i = 0; pub[i].der != NULL; i++)
  54017. {
  54018. buff = pub[i].der;
  54019. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  54020. ExpectNotNull(rsa);
  54021. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  54022. newBuff = NULL;
  54023. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  54024. ExpectNotNull(newBuff);
  54025. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  54026. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54027. RSA_free(rsa);
  54028. rsa = NULL;
  54029. }
  54030. #endif
  54031. return EXPECT_RESULT();
  54032. }
  54033. static int test_wolfSSL_RSA_print(void)
  54034. {
  54035. EXPECT_DECLS;
  54036. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  54037. !defined(NO_STDIO_FILESYSTEM) && \
  54038. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  54039. !defined(NO_BIO) && defined(XFPRINTF)
  54040. BIO *bio = NULL;
  54041. WOLFSSL_RSA* rsa = NULL;
  54042. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  54043. ExpectNotNull(rsa = RSA_new());
  54044. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  54045. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  54046. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  54047. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  54048. /* Some very large number of indent spaces. */
  54049. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  54050. /* RSA is empty. */
  54051. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  54052. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  54053. RSA_free(rsa);
  54054. rsa = NULL;
  54055. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  54056. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  54057. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  54058. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  54059. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  54060. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  54061. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  54062. BIO_free(bio);
  54063. RSA_free(rsa);
  54064. #endif
  54065. return EXPECT_RESULT();
  54066. }
  54067. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  54068. {
  54069. EXPECT_DECLS;
  54070. #ifndef NO_RSA
  54071. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  54072. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54073. RSA *rsa = NULL;
  54074. const unsigned char *derBuf = client_key_der_2048;
  54075. unsigned char em[256] = {0}; /* len = 2048/8 */
  54076. /* Random data simulating a hash */
  54077. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  54078. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  54079. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  54080. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  54081. };
  54082. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  54083. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  54084. RSA_PSS_SALTLEN_DIGEST), 0);
  54085. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  54086. RSA_PSS_SALTLEN_DIGEST), 0);
  54087. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  54088. RSA_PSS_SALTLEN_DIGEST), 0);
  54089. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  54090. RSA_PSS_SALTLEN_DIGEST), 0);
  54091. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  54092. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  54093. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  54094. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  54095. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  54096. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  54097. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  54098. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  54099. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  54100. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  54101. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  54102. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  54103. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  54104. RSA_PSS_SALTLEN_DIGEST), 1);
  54105. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  54106. RSA_PSS_SALTLEN_DIGEST), 1);
  54107. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  54108. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  54109. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  54110. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  54111. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  54112. RSA_PSS_SALTLEN_MAX), 1);
  54113. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  54114. RSA_PSS_SALTLEN_MAX), 1);
  54115. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  54116. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  54117. RSA_free(rsa);
  54118. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54119. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  54120. #endif
  54121. return EXPECT_RESULT();
  54122. }
  54123. static int test_wolfSSL_RSA_sign_sha3(void)
  54124. {
  54125. EXPECT_DECLS;
  54126. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  54127. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  54128. RSA* rsa = NULL;
  54129. const unsigned char *derBuf = client_key_der_2048;
  54130. unsigned char sigRet[256] = {0};
  54131. unsigned int sigLen = sizeof(sigRet);
  54132. /* Random data simulating a hash */
  54133. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  54134. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  54135. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  54136. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  54137. };
  54138. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  54139. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  54140. rsa), 1);
  54141. RSA_free(rsa);
  54142. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  54143. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  54144. return EXPECT_RESULT();
  54145. }
  54146. static int test_wolfSSL_RSA_get0_key(void)
  54147. {
  54148. EXPECT_DECLS;
  54149. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54150. RSA *rsa = NULL;
  54151. const BIGNUM* n = NULL;
  54152. const BIGNUM* e = NULL;
  54153. const BIGNUM* d = NULL;
  54154. const unsigned char* der;
  54155. int derSz;
  54156. #ifdef USE_CERT_BUFFERS_1024
  54157. der = client_key_der_1024;
  54158. derSz = sizeof_client_key_der_1024;
  54159. #elif defined(USE_CERT_BUFFERS_2048)
  54160. der = client_key_der_2048;
  54161. derSz = sizeof_client_key_der_2048;
  54162. #else
  54163. der = NULL;
  54164. derSz = 0;
  54165. #endif
  54166. if (der != NULL) {
  54167. RSA_get0_key(NULL, NULL, NULL, NULL);
  54168. RSA_get0_key(rsa, NULL, NULL, NULL);
  54169. RSA_get0_key(NULL, &n, &e, &d);
  54170. ExpectNull(n);
  54171. ExpectNull(e);
  54172. ExpectNull(d);
  54173. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  54174. ExpectNotNull(rsa);
  54175. RSA_get0_key(rsa, NULL, NULL, NULL);
  54176. RSA_get0_key(rsa, &n, NULL, NULL);
  54177. ExpectNotNull(n);
  54178. RSA_get0_key(rsa, NULL, &e, NULL);
  54179. ExpectNotNull(e);
  54180. RSA_get0_key(rsa, NULL, NULL, &d);
  54181. ExpectNotNull(d);
  54182. RSA_get0_key(rsa, &n, &e, &d);
  54183. ExpectNotNull(n);
  54184. ExpectNotNull(e);
  54185. ExpectNotNull(d);
  54186. RSA_free(rsa);
  54187. }
  54188. #endif
  54189. return EXPECT_RESULT();
  54190. }
  54191. static int test_wolfSSL_RSA_meth(void)
  54192. {
  54193. EXPECT_DECLS;
  54194. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54195. RSA *rsa = NULL;
  54196. RSA_METHOD *rsa_meth = NULL;
  54197. #ifdef WOLFSSL_KEY_GEN
  54198. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  54199. RSA_free(rsa);
  54200. rsa = NULL;
  54201. #else
  54202. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  54203. #endif
  54204. ExpectNotNull(RSA_get_default_method());
  54205. wolfSSL_RSA_meth_free(NULL);
  54206. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  54207. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  54208. RSA_METHOD_FLAG_NO_CHECK));
  54209. #ifndef NO_WOLFSSL_STUB
  54210. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  54211. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  54212. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  54213. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  54214. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  54215. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  54216. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  54217. #endif
  54218. ExpectIntEQ(RSA_flags(NULL), 0);
  54219. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  54220. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  54221. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  54222. ExpectNotNull(rsa = RSA_new());
  54223. /* No method set. */
  54224. ExpectIntEQ(RSA_flags(rsa), 0);
  54225. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  54226. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  54227. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  54228. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  54229. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  54230. if (EXPECT_FAIL()) {
  54231. wolfSSL_RSA_meth_free(rsa_meth);
  54232. }
  54233. ExpectNull(RSA_get_method(NULL));
  54234. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  54235. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  54236. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  54237. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  54238. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  54239. RSA_METHOD_FLAG_NO_CHECK);
  54240. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  54241. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  54242. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  54243. /* rsa_meth is freed here */
  54244. RSA_free(rsa);
  54245. #endif
  54246. return EXPECT_RESULT();
  54247. }
  54248. static int test_wolfSSL_RSA_verify(void)
  54249. {
  54250. EXPECT_DECLS;
  54251. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  54252. #ifndef NO_BIO
  54253. XFILE fp = XBADFILE;
  54254. RSA *pKey = NULL;
  54255. RSA *pubKey = NULL;
  54256. X509 *cert = NULL;
  54257. const char *text = "Hello wolfSSL !";
  54258. unsigned char hash[SHA256_DIGEST_LENGTH];
  54259. unsigned char signature[2048/8];
  54260. unsigned int signatureLength;
  54261. byte *buf = NULL;
  54262. BIO *bio = NULL;
  54263. SHA256_CTX c;
  54264. EVP_PKEY *evpPkey = NULL;
  54265. EVP_PKEY *evpPubkey = NULL;
  54266. size_t sz;
  54267. /* generate hash */
  54268. SHA256_Init(&c);
  54269. SHA256_Update(&c, text, strlen(text));
  54270. SHA256_Final(hash, &c);
  54271. #ifdef WOLFSSL_SMALL_STACK_CACHE
  54272. /* workaround for small stack cache case */
  54273. wc_Sha256Free((wc_Sha256*)&c);
  54274. #endif
  54275. /* read privete key file */
  54276. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  54277. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  54278. ExpectTrue((sz = XFTELL(fp)) > 0);
  54279. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  54280. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  54281. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  54282. if (fp != XBADFILE) {
  54283. XFCLOSE(fp);
  54284. fp = XBADFILE;
  54285. }
  54286. /* read private key and sign hash data */
  54287. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  54288. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  54289. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  54290. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  54291. signature, &signatureLength, pKey), SSL_SUCCESS);
  54292. /* read public key and verify signed data */
  54293. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  54294. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  54295. if (fp != XBADFILE)
  54296. XFCLOSE(fp);
  54297. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  54298. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  54299. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  54300. signatureLength, pubKey), SSL_SUCCESS);
  54301. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  54302. signatureLength, NULL), SSL_FAILURE);
  54303. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  54304. signatureLength, pubKey), SSL_FAILURE);
  54305. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  54306. signatureLength, pubKey), SSL_FAILURE);
  54307. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  54308. signatureLength, NULL), SSL_FAILURE);
  54309. RSA_free(pKey);
  54310. EVP_PKEY_free(evpPkey);
  54311. RSA_free(pubKey);
  54312. EVP_PKEY_free(evpPubkey);
  54313. X509_free(cert);
  54314. BIO_free(bio);
  54315. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  54316. #endif
  54317. #endif
  54318. return EXPECT_RESULT();
  54319. }
  54320. static int test_wolfSSL_RSA_sign(void)
  54321. {
  54322. EXPECT_DECLS;
  54323. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54324. RSA *rsa;
  54325. unsigned char hash[SHA256_DIGEST_LENGTH];
  54326. #ifdef USE_CERT_BUFFERS_1024
  54327. const unsigned char* privDer = client_key_der_1024;
  54328. size_t privDerSz = sizeof_client_key_der_1024;
  54329. const unsigned char* pubDer = client_keypub_der_1024;
  54330. size_t pubDerSz = sizeof_client_keypub_der_1024;
  54331. unsigned char signature[1024/8];
  54332. #else
  54333. const unsigned char* privDer = client_key_der_2048;
  54334. size_t privDerSz = sizeof_client_key_der_2048;
  54335. const unsigned char* pubDer = client_keypub_der_2048;
  54336. size_t pubDerSz = sizeof_client_keypub_der_2048;
  54337. unsigned char signature[2048/8];
  54338. #endif
  54339. unsigned int signatureLen;
  54340. const unsigned char* der;
  54341. XMEMSET(hash, 0, sizeof(hash));
  54342. der = privDer;
  54343. rsa = NULL;
  54344. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54345. /* Invalid parameters. */
  54346. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  54347. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  54348. &signatureLen, rsa), 0);
  54349. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  54350. &signatureLen, rsa), 0);
  54351. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  54352. &signatureLen, rsa), 0);
  54353. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  54354. NULL, rsa), 0);
  54355. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  54356. &signatureLen, NULL), 0);
  54357. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  54358. &signatureLen, rsa), 1);
  54359. RSA_free(rsa);
  54360. der = pubDer;
  54361. rsa = NULL;
  54362. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  54363. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  54364. signatureLen, rsa), 1);
  54365. RSA_free(rsa);
  54366. #endif
  54367. return EXPECT_RESULT();
  54368. }
  54369. static int test_wolfSSL_RSA_sign_ex(void)
  54370. {
  54371. EXPECT_DECLS;
  54372. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54373. RSA *rsa = NULL;
  54374. unsigned char hash[SHA256_DIGEST_LENGTH];
  54375. #ifdef USE_CERT_BUFFERS_1024
  54376. const unsigned char* privDer = client_key_der_1024;
  54377. size_t privDerSz = sizeof_client_key_der_1024;
  54378. const unsigned char* pubDer = client_keypub_der_1024;
  54379. size_t pubDerSz = sizeof_client_keypub_der_1024;
  54380. unsigned char signature[1024/8];
  54381. #else
  54382. const unsigned char* privDer = client_key_der_2048;
  54383. size_t privDerSz = sizeof_client_key_der_2048;
  54384. const unsigned char* pubDer = client_keypub_der_2048;
  54385. size_t pubDerSz = sizeof_client_keypub_der_2048;
  54386. unsigned char signature[2048/8];
  54387. #endif
  54388. unsigned int signatureLen;
  54389. const unsigned char* der;
  54390. unsigned char encodedHash[51];
  54391. unsigned int encodedHashLen;
  54392. const unsigned char expEncHash[] = {
  54393. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  54394. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  54395. 0x00, 0x04, 0x20,
  54396. /* Hash data */
  54397. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54398. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54399. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54400. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54401. };
  54402. XMEMSET(hash, 0, sizeof(hash));
  54403. ExpectNotNull(rsa = wolfSSL_RSA_new());
  54404. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54405. &signatureLen, rsa, 1), 0);
  54406. wolfSSL_RSA_free(rsa);
  54407. der = privDer;
  54408. rsa = NULL;
  54409. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54410. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  54411. -1), 0);
  54412. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  54413. signature, &signatureLen, rsa, 1), 0);
  54414. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  54415. &signatureLen, rsa, 1), 0);
  54416. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  54417. &signatureLen, rsa, 1), 0);
  54418. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54419. NULL, rsa, 1), 0);
  54420. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54421. &signatureLen, NULL, 1), 0);
  54422. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54423. &signatureLen, rsa, -1), 0);
  54424. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  54425. &signatureLen, rsa, 0), 0);
  54426. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  54427. &signatureLen, rsa, 0), 0);
  54428. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54429. NULL, rsa, 0), 0);
  54430. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54431. &signatureLen, rsa, 1), 1);
  54432. /* Test returning encoded hash. */
  54433. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  54434. &encodedHashLen, rsa, 0), 1);
  54435. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  54436. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  54437. RSA_free(rsa);
  54438. der = pubDer;
  54439. rsa = NULL;
  54440. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  54441. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  54442. signatureLen, rsa), 1);
  54443. RSA_free(rsa);
  54444. #endif
  54445. return EXPECT_RESULT();
  54446. }
  54447. static int test_wolfSSL_RSA_public_decrypt(void)
  54448. {
  54449. EXPECT_DECLS;
  54450. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54451. RSA *rsa;
  54452. unsigned char msg[SHA256_DIGEST_LENGTH];
  54453. #ifdef USE_CERT_BUFFERS_1024
  54454. const unsigned char* pubDer = client_keypub_der_1024;
  54455. size_t pubDerSz = sizeof_client_keypub_der_1024;
  54456. unsigned char decMsg[1024/8];
  54457. const unsigned char encMsg[] = {
  54458. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  54459. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  54460. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  54461. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  54462. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  54463. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  54464. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  54465. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  54466. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  54467. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  54468. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  54469. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  54470. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  54471. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  54472. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  54473. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  54474. };
  54475. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  54476. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  54477. defined(WC_RSA_NO_PADDING)
  54478. const unsigned char encMsgNoPad[] = {
  54479. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  54480. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  54481. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  54482. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  54483. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  54484. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  54485. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  54486. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  54487. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  54488. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  54489. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  54490. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  54491. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  54492. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  54493. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  54494. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  54495. };
  54496. #endif
  54497. #else
  54498. const unsigned char* pubDer = client_keypub_der_2048;
  54499. size_t pubDerSz = sizeof_client_keypub_der_2048;
  54500. unsigned char decMsg[2048/8];
  54501. const unsigned char encMsg[] = {
  54502. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  54503. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  54504. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  54505. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  54506. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  54507. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  54508. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  54509. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  54510. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  54511. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  54512. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  54513. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  54514. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  54515. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  54516. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  54517. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  54518. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  54519. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  54520. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  54521. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  54522. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  54523. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  54524. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  54525. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  54526. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  54527. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  54528. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  54529. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  54530. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  54531. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  54532. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  54533. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  54534. };
  54535. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  54536. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  54537. defined(WC_RSA_NO_PADDING)
  54538. const unsigned char encMsgNoPad[] = {
  54539. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  54540. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  54541. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  54542. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  54543. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  54544. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  54545. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  54546. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  54547. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  54548. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  54549. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  54550. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  54551. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  54552. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  54553. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  54554. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  54555. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  54556. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  54557. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  54558. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  54559. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  54560. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  54561. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  54562. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  54563. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  54564. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  54565. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  54566. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  54567. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  54568. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  54569. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  54570. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  54571. };
  54572. #endif
  54573. #endif
  54574. const unsigned char* der;
  54575. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  54576. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  54577. defined(WC_RSA_NO_PADDING)
  54578. int i;
  54579. #endif
  54580. XMEMSET(msg, 0, sizeof(msg));
  54581. der = pubDer;
  54582. rsa = NULL;
  54583. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  54584. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  54585. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  54586. RSA_PKCS1_PADDING), -1);
  54587. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  54588. RSA_PKCS1_PADDING), -1);
  54589. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  54590. RSA_PKCS1_PADDING), -1);
  54591. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  54592. RSA_PKCS1_PADDING), -1);
  54593. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  54594. RSA_PKCS1_PSS_PADDING), -1);
  54595. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  54596. RSA_PKCS1_PADDING), 32);
  54597. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  54598. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  54599. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  54600. defined(WC_RSA_NO_PADDING)
  54601. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  54602. rsa, RSA_NO_PADDING), sizeof(decMsg));
  54603. /* Zeros before actual data. */
  54604. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  54605. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  54606. }
  54607. /* Check actual data. */
  54608. XMEMSET(msg, 0x01, sizeof(msg));
  54609. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  54610. #endif
  54611. RSA_free(rsa);
  54612. #endif
  54613. return EXPECT_RESULT();
  54614. }
  54615. static int test_wolfSSL_RSA_private_encrypt(void)
  54616. {
  54617. EXPECT_DECLS;
  54618. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54619. RSA *rsa;
  54620. unsigned char msg[SHA256_DIGEST_LENGTH];
  54621. #ifdef USE_CERT_BUFFERS_1024
  54622. const unsigned char* privDer = client_key_der_1024;
  54623. size_t privDerSz = sizeof_client_key_der_1024;
  54624. unsigned char encMsg[1024/8];
  54625. const unsigned char expEncMsg[] = {
  54626. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  54627. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  54628. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  54629. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  54630. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  54631. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  54632. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  54633. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  54634. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  54635. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  54636. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  54637. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  54638. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  54639. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  54640. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  54641. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  54642. };
  54643. #ifdef WC_RSA_NO_PADDING
  54644. const unsigned char expEncMsgNoPad[] = {
  54645. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  54646. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  54647. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  54648. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  54649. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  54650. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  54651. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  54652. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  54653. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  54654. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  54655. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  54656. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  54657. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  54658. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  54659. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  54660. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  54661. };
  54662. #endif
  54663. #else
  54664. const unsigned char* privDer = client_key_der_2048;
  54665. size_t privDerSz = sizeof_client_key_der_2048;
  54666. unsigned char encMsg[2048/8];
  54667. const unsigned char expEncMsg[] = {
  54668. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  54669. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  54670. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  54671. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  54672. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  54673. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  54674. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  54675. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  54676. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  54677. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  54678. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  54679. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  54680. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  54681. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  54682. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  54683. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  54684. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  54685. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  54686. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  54687. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  54688. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  54689. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  54690. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  54691. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  54692. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  54693. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  54694. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  54695. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  54696. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  54697. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  54698. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  54699. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  54700. };
  54701. #ifdef WC_RSA_NO_PADDING
  54702. const unsigned char expEncMsgNoPad[] = {
  54703. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  54704. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  54705. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  54706. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  54707. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  54708. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  54709. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  54710. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  54711. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  54712. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  54713. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  54714. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  54715. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  54716. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  54717. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  54718. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  54719. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  54720. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  54721. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  54722. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  54723. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  54724. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  54725. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  54726. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  54727. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  54728. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  54729. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  54730. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  54731. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  54732. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  54733. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  54734. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  54735. };
  54736. #endif
  54737. #endif
  54738. const unsigned char* der;
  54739. XMEMSET(msg, 0x00, sizeof(msg));
  54740. der = privDer;
  54741. rsa = NULL;
  54742. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54743. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  54744. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  54745. -1);
  54746. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  54747. RSA_PKCS1_PADDING), -1);
  54748. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  54749. RSA_PKCS1_PADDING), -1);
  54750. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  54751. RSA_PKCS1_PADDING), -1);
  54752. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  54753. RSA_PKCS1_PSS_PADDING), -1);
  54754. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  54755. RSA_PKCS1_PADDING), sizeof(encMsg));
  54756. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  54757. #ifdef WC_RSA_NO_PADDING
  54758. /* Non-zero message. */
  54759. XMEMSET(msg, 0x01, sizeof(msg));
  54760. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  54761. RSA_NO_PADDING), sizeof(encMsg));
  54762. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  54763. #endif
  54764. RSA_free(rsa);
  54765. #endif
  54766. return EXPECT_RESULT();
  54767. }
  54768. static int test_wolfSSL_RSA_public_encrypt(void)
  54769. {
  54770. EXPECT_DECLS;
  54771. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54772. RSA* rsa = NULL;
  54773. const unsigned char msg[2048/8] = { 0 };
  54774. unsigned char encMsg[2048/8];
  54775. ExpectNotNull(rsa = RSA_new());
  54776. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  54777. RSA_PKCS1_PADDING), -1);
  54778. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  54779. RSA_PKCS1_PADDING), -1);
  54780. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  54781. RSA_PKCS1_PADDING), -1);
  54782. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  54783. RSA_PKCS1_PADDING), -1);
  54784. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  54785. RSA_PKCS1_PSS_PADDING), -1);
  54786. /* Empty RSA key. */
  54787. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  54788. RSA_PKCS1_PADDING), -1);
  54789. RSA_free(rsa);
  54790. #endif
  54791. return EXPECT_RESULT();
  54792. }
  54793. static int test_wolfSSL_RSA_private_decrypt(void)
  54794. {
  54795. EXPECT_DECLS;
  54796. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54797. RSA* rsa = NULL;
  54798. unsigned char msg[2048/8];
  54799. const unsigned char encMsg[2048/8] = { 0 };
  54800. ExpectNotNull(rsa = RSA_new());
  54801. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  54802. RSA_PKCS1_PADDING), -1);
  54803. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  54804. RSA_PKCS1_PADDING), -1);
  54805. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  54806. RSA_PKCS1_PADDING), -1);
  54807. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  54808. RSA_PKCS1_PADDING), -1);
  54809. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  54810. RSA_PKCS1_PSS_PADDING), -1);
  54811. /* Empty RSA key. */
  54812. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  54813. RSA_PKCS1_PADDING), -1);
  54814. RSA_free(rsa);
  54815. #endif
  54816. return EXPECT_RESULT();
  54817. }
  54818. static int test_wolfSSL_RSA_GenAdd(void)
  54819. {
  54820. EXPECT_DECLS;
  54821. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54822. RSA *rsa;
  54823. #ifdef USE_CERT_BUFFERS_1024
  54824. const unsigned char* privDer = client_key_der_1024;
  54825. size_t privDerSz = sizeof_client_key_der_1024;
  54826. const unsigned char* pubDer = client_keypub_der_1024;
  54827. size_t pubDerSz = sizeof_client_keypub_der_1024;
  54828. #else
  54829. const unsigned char* privDer = client_key_der_2048;
  54830. size_t privDerSz = sizeof_client_key_der_2048;
  54831. const unsigned char* pubDer = client_keypub_der_2048;
  54832. size_t pubDerSz = sizeof_client_keypub_der_2048;
  54833. #endif
  54834. const unsigned char* der;
  54835. der = privDer;
  54836. rsa = NULL;
  54837. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54838. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  54839. #ifndef RSA_LOW_MEM
  54840. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  54841. #else
  54842. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  54843. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  54844. #endif
  54845. RSA_free(rsa);
  54846. der = pubDer;
  54847. rsa = NULL;
  54848. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  54849. /* Need private values. */
  54850. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  54851. RSA_free(rsa);
  54852. #endif
  54853. return EXPECT_RESULT();
  54854. }
  54855. static int test_wolfSSL_RSA_blinding_on(void)
  54856. {
  54857. EXPECT_DECLS;
  54858. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  54859. RSA *rsa;
  54860. WOLFSSL_BN_CTX *bnCtx = NULL;
  54861. #ifdef USE_CERT_BUFFERS_1024
  54862. const unsigned char* privDer = client_key_der_1024;
  54863. size_t privDerSz = sizeof_client_key_der_1024;
  54864. #else
  54865. const unsigned char* privDer = client_key_der_2048;
  54866. size_t privDerSz = sizeof_client_key_der_2048;
  54867. #endif
  54868. const unsigned char* der;
  54869. der = privDer;
  54870. rsa = NULL;
  54871. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54872. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  54873. /* Does nothing so all parameters are valid. */
  54874. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  54875. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  54876. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  54877. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  54878. wolfSSL_BN_CTX_free(bnCtx);
  54879. RSA_free(rsa);
  54880. #endif
  54881. return EXPECT_RESULT();
  54882. }
  54883. static int test_wolfSSL_RSA_ex_data(void)
  54884. {
  54885. EXPECT_DECLS;
  54886. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  54887. RSA* rsa = NULL;
  54888. unsigned char data[1];
  54889. ExpectNotNull(rsa = RSA_new());
  54890. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  54891. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  54892. #ifdef MAX_EX_DATA
  54893. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  54894. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  54895. #endif
  54896. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  54897. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  54898. #ifdef HAVE_EX_DATA
  54899. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  54900. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  54901. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  54902. #else
  54903. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  54904. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  54905. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  54906. #endif
  54907. RSA_free(rsa);
  54908. #endif /* !NO_RSA && OPENSSL_EXTRA */
  54909. return EXPECT_RESULT();
  54910. }
  54911. static int test_wolfSSL_RSA_LoadDer(void)
  54912. {
  54913. EXPECT_DECLS;
  54914. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  54915. defined(OPENSSL_EXTRA_X509_SMALL))
  54916. RSA *rsa = NULL;
  54917. #ifdef USE_CERT_BUFFERS_1024
  54918. const unsigned char* privDer = client_key_der_1024;
  54919. size_t privDerSz = sizeof_client_key_der_1024;
  54920. #else
  54921. const unsigned char* privDer = client_key_der_2048;
  54922. size_t privDerSz = sizeof_client_key_der_2048;
  54923. #endif
  54924. ExpectNotNull(rsa = RSA_new());
  54925. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  54926. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  54927. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  54928. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  54929. RSA_free(rsa);
  54930. #endif /* !NO_RSA && OPENSSL_EXTRA */
  54931. return EXPECT_RESULT();
  54932. }
  54933. /* Local API. */
  54934. static int test_wolfSSL_RSA_To_Der(void)
  54935. {
  54936. EXPECT_DECLS;
  54937. #ifdef WOLFSSL_TEST_STATIC_BUILD
  54938. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54939. RSA* rsa;
  54940. #ifdef USE_CERT_BUFFERS_1024
  54941. const unsigned char* privDer = client_key_der_1024;
  54942. size_t privDerSz = sizeof_client_key_der_1024;
  54943. const unsigned char* pubDer = client_keypub_der_1024;
  54944. size_t pubDerSz = sizeof_client_keypub_der_1024;
  54945. unsigned char out[sizeof(client_key_der_1024)];
  54946. #else
  54947. const unsigned char* privDer = client_key_der_2048;
  54948. size_t privDerSz = sizeof_client_key_der_2048;
  54949. const unsigned char* pubDer = client_keypub_der_2048;
  54950. size_t pubDerSz = sizeof_client_keypub_der_2048;
  54951. unsigned char out[sizeof(client_key_der_2048)];
  54952. #endif
  54953. const unsigned char* der;
  54954. unsigned char* outDer = NULL;
  54955. der = privDer;
  54956. rsa = NULL;
  54957. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54958. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  54959. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  54960. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  54961. outDer = out;
  54962. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  54963. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  54964. outDer = NULL;
  54965. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  54966. ExpectNotNull(outDer);
  54967. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  54968. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  54969. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  54970. outDer = out;
  54971. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  54972. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  54973. RSA_free(rsa);
  54974. ExpectNotNull(rsa = RSA_new());
  54975. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  54976. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  54977. RSA_free(rsa);
  54978. der = pubDer;
  54979. rsa = NULL;
  54980. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  54981. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  54982. RSA_free(rsa);
  54983. #endif
  54984. #endif
  54985. return EXPECT_RESULT();
  54986. }
  54987. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  54988. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  54989. {
  54990. EXPECT_DECLS;
  54991. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  54992. XFILE file = XBADFILE;
  54993. const char* fname = "./certs/server-keyPub.pem";
  54994. RSA *rsa = NULL;
  54995. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  54996. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  54997. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  54998. ExpectIntEQ(RSA_size(rsa), 256);
  54999. RSA_free(rsa);
  55000. if (file != XBADFILE)
  55001. XFCLOSE(file);
  55002. #endif
  55003. return EXPECT_RESULT();
  55004. }
  55005. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  55006. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  55007. {
  55008. EXPECT_DECLS;
  55009. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  55010. defined(WOLFSSL_KEY_GEN)
  55011. RSA* rsa = NULL;
  55012. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  55013. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  55014. /* Valid but stub so returns 0. */
  55015. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  55016. #endif
  55017. return EXPECT_RESULT();
  55018. }
  55019. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  55020. {
  55021. EXPECT_DECLS;
  55022. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  55023. (defined(WOLFSSL_PEM_TO_DER) || \
  55024. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  55025. RSA* rsa = NULL;
  55026. #ifdef USE_CERT_BUFFERS_1024
  55027. const unsigned char* privDer = client_key_der_1024;
  55028. size_t privDerSz = sizeof_client_key_der_1024;
  55029. #else
  55030. const unsigned char* privDer = client_key_der_2048;
  55031. size_t privDerSz = sizeof_client_key_der_2048;
  55032. #endif
  55033. const unsigned char* der;
  55034. #ifndef NO_AES
  55035. unsigned char passwd[] = "password";
  55036. #endif
  55037. ExpectNotNull(rsa = RSA_new());
  55038. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  55039. NULL, NULL), 0);
  55040. RSA_free(rsa);
  55041. der = privDer;
  55042. rsa = NULL;
  55043. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  55044. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  55045. NULL, NULL), 0);
  55046. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  55047. NULL, NULL), 0);
  55048. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  55049. NULL, NULL), 1);
  55050. #ifndef NO_AES
  55051. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  55052. NULL, 0, NULL, NULL), 1);
  55053. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  55054. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  55055. #endif
  55056. RSA_free(rsa);
  55057. #endif
  55058. return EXPECT_RESULT();
  55059. }
  55060. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  55061. {
  55062. EXPECT_DECLS;
  55063. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  55064. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  55065. RSA* rsa = NULL;
  55066. #ifdef USE_CERT_BUFFERS_1024
  55067. const unsigned char* privDer = client_key_der_1024;
  55068. size_t privDerSz = sizeof_client_key_der_1024;
  55069. #else
  55070. const unsigned char* privDer = client_key_der_2048;
  55071. size_t privDerSz = sizeof_client_key_der_2048;
  55072. #endif
  55073. const unsigned char* der;
  55074. #ifndef NO_AES
  55075. unsigned char passwd[] = "password";
  55076. #endif
  55077. unsigned char* pem = NULL;
  55078. int plen;
  55079. ExpectNotNull(rsa = RSA_new());
  55080. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  55081. &plen), 0);
  55082. RSA_free(rsa);
  55083. der = privDer;
  55084. rsa = NULL;
  55085. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  55086. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  55087. &plen), 0);
  55088. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  55089. &plen), 0);
  55090. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  55091. NULL), 0);
  55092. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  55093. &plen), 1);
  55094. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  55095. pem = NULL;
  55096. #ifndef NO_AES
  55097. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  55098. NULL, 0, &pem, &plen), 1);
  55099. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  55100. pem = NULL;
  55101. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  55102. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  55103. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  55104. #endif
  55105. RSA_free(rsa);
  55106. #endif
  55107. return EXPECT_RESULT();
  55108. }
  55109. static int test_wolfSSL_DH(void)
  55110. {
  55111. EXPECT_DECLS;
  55112. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  55113. DH *dh = NULL;
  55114. BIGNUM* p;
  55115. BIGNUM* q;
  55116. BIGNUM* g;
  55117. BIGNUM* pub = NULL;
  55118. BIGNUM* priv = NULL;
  55119. #if defined(OPENSSL_ALL)
  55120. #if !defined(HAVE_FIPS) || \
  55121. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  55122. FILE* f = NULL;
  55123. unsigned char buf[268];
  55124. const unsigned char* pt = buf;
  55125. long len = 0;
  55126. dh = NULL;
  55127. XMEMSET(buf, 0, sizeof(buf));
  55128. /* Test 2048 bit parameters */
  55129. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  55130. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55131. if (f != XBADFILE)
  55132. XFCLOSE(f);
  55133. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  55134. ExpectNotNull(dh->p);
  55135. ExpectNotNull(dh->g);
  55136. ExpectTrue(pt == buf);
  55137. ExpectIntEQ(DH_generate_key(dh), 1);
  55138. ExpectIntEQ(DH_generate_key(dh), 1);
  55139. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  55140. ExpectNotNull(pub = BN_new());
  55141. ExpectIntEQ(BN_set_word(pub, 1), 1);
  55142. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  55143. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  55144. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  55145. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  55146. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  55147. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  55148. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  55149. BN_free(pub);
  55150. pub = NULL;
  55151. DH_get0_pqg(dh, (const BIGNUM**)&p,
  55152. (const BIGNUM**)&q,
  55153. (const BIGNUM**)&g);
  55154. ExpectPtrEq(p, dh->p);
  55155. ExpectPtrEq(q, dh->q);
  55156. ExpectPtrEq(g, dh->g);
  55157. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  55158. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  55159. ExpectPtrEq(pub, dh->pub_key);
  55160. ExpectPtrEq(priv, dh->priv_key);
  55161. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  55162. ExpectPtrEq(pub, dh->pub_key);
  55163. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  55164. ExpectPtrEq(priv, dh->priv_key);
  55165. pub = NULL;
  55166. priv = NULL;
  55167. ExpectNotNull(pub = BN_new());
  55168. ExpectNotNull(priv = BN_new());
  55169. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  55170. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  55171. if (EXPECT_FAIL()) {
  55172. BN_free(pub);
  55173. BN_free(priv);
  55174. }
  55175. pub = NULL;
  55176. priv = NULL;
  55177. ExpectNotNull(pub = BN_new());
  55178. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  55179. if (EXPECT_FAIL()) {
  55180. BN_free(pub);
  55181. }
  55182. ExpectNotNull(priv = BN_new());
  55183. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  55184. if (EXPECT_FAIL()) {
  55185. BN_free(priv);
  55186. }
  55187. ExpectPtrEq(pub, dh->pub_key);
  55188. ExpectPtrEq(priv, dh->priv_key);
  55189. pub = NULL;
  55190. priv = NULL;
  55191. DH_free(dh);
  55192. dh = NULL;
  55193. ExpectNotNull(dh = DH_new());
  55194. p = NULL;
  55195. ExpectNotNull(p = BN_new());
  55196. ExpectIntEQ(BN_set_word(p, 1), 1);
  55197. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  55198. ExpectNotNull(pub = BN_new());
  55199. ExpectNotNull(priv = BN_new());
  55200. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  55201. if (EXPECT_FAIL()) {
  55202. BN_free(pub);
  55203. BN_free(priv);
  55204. }
  55205. pub = NULL;
  55206. priv = NULL;
  55207. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  55208. BN_free(p);
  55209. p = NULL;
  55210. DH_free(dh);
  55211. dh = NULL;
  55212. #ifdef WOLFSSL_KEY_GEN
  55213. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  55214. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  55215. DH_free(dh);
  55216. dh = NULL;
  55217. #endif
  55218. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  55219. #endif /* OPENSSL_ALL */
  55220. (void)dh;
  55221. (void)p;
  55222. (void)q;
  55223. (void)g;
  55224. (void)pub;
  55225. (void)priv;
  55226. ExpectNotNull(dh = wolfSSL_DH_new());
  55227. /* invalid parameters test */
  55228. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  55229. (const BIGNUM**)&q,
  55230. (const BIGNUM**)&g);
  55231. DH_get0_pqg(dh, NULL,
  55232. (const BIGNUM**)&q,
  55233. (const BIGNUM**)&g);
  55234. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  55235. DH_get0_pqg(dh, NULL, NULL, NULL);
  55236. DH_get0_pqg(dh, (const BIGNUM**)&p,
  55237. (const BIGNUM**)&q,
  55238. (const BIGNUM**)&g);
  55239. ExpectPtrEq(p, NULL);
  55240. ExpectPtrEq(q, NULL);
  55241. ExpectPtrEq(g, NULL);
  55242. DH_free(dh);
  55243. dh = NULL;
  55244. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  55245. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  55246. #if defined(OPENSSL_ALL) || \
  55247. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  55248. dh = wolfSSL_DH_new();
  55249. ExpectNotNull(dh);
  55250. p = wolfSSL_BN_new();
  55251. ExpectNotNull(p);
  55252. ExpectIntEQ(BN_set_word(p, 11), 1);
  55253. g = wolfSSL_BN_new();
  55254. ExpectNotNull(g);
  55255. ExpectIntEQ(BN_set_word(g, 2), 1);
  55256. q = wolfSSL_BN_new();
  55257. ExpectNotNull(q);
  55258. ExpectIntEQ(BN_set_word(q, 5), 1);
  55259. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  55260. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  55261. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  55262. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  55263. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  55264. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  55265. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  55266. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  55267. /* Don't need q. */
  55268. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  55269. if (EXPECT_FAIL()) {
  55270. BN_free(p);
  55271. BN_free(g);
  55272. }
  55273. p = NULL;
  55274. g = NULL;
  55275. /* Setting again will free the p and g. */
  55276. wolfSSL_BN_free(q);
  55277. q = NULL;
  55278. DH_free(dh);
  55279. dh = NULL;
  55280. dh = wolfSSL_DH_new();
  55281. ExpectNotNull(dh);
  55282. p = wolfSSL_BN_new();
  55283. ExpectNotNull(p);
  55284. ExpectIntEQ(BN_set_word(p, 11), 1);
  55285. g = wolfSSL_BN_new();
  55286. ExpectNotNull(g);
  55287. ExpectIntEQ(BN_set_word(g, 2), 1);
  55288. q = wolfSSL_BN_new();
  55289. ExpectNotNull(q);
  55290. ExpectIntEQ(BN_set_word(q, 5), 1);
  55291. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  55292. /* p, q and g are now owned by dh - don't free. */
  55293. if (EXPECT_FAIL()) {
  55294. BN_free(p);
  55295. BN_free(q);
  55296. BN_free(g);
  55297. }
  55298. p = NULL;
  55299. q = NULL;
  55300. g = NULL;
  55301. p = wolfSSL_BN_new();
  55302. ExpectNotNull(p);
  55303. ExpectIntEQ(BN_set_word(p, 11), 1);
  55304. g = wolfSSL_BN_new();
  55305. ExpectNotNull(g);
  55306. ExpectIntEQ(BN_set_word(g, 2), 1);
  55307. q = wolfSSL_BN_new();
  55308. ExpectNotNull(q);
  55309. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  55310. if (EXPECT_FAIL()) {
  55311. BN_free(p);
  55312. }
  55313. p = NULL;
  55314. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  55315. if (EXPECT_FAIL()) {
  55316. BN_free(q);
  55317. }
  55318. q = NULL;
  55319. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  55320. if (EXPECT_FAIL()) {
  55321. BN_free(g);
  55322. }
  55323. g = NULL;
  55324. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  55325. /* p, q and g are now owned by dh - don't free. */
  55326. DH_free(dh);
  55327. dh = NULL;
  55328. ExpectIntEQ(DH_generate_key(NULL), 0);
  55329. ExpectNotNull(dh = DH_new());
  55330. ExpectIntEQ(DH_generate_key(dh), 0);
  55331. p = wolfSSL_BN_new();
  55332. ExpectNotNull(p);
  55333. ExpectIntEQ(BN_set_word(p, 0), 1);
  55334. g = wolfSSL_BN_new();
  55335. ExpectNotNull(g);
  55336. ExpectIntEQ(BN_set_word(g, 2), 1);
  55337. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  55338. if (EXPECT_FAIL()) {
  55339. BN_free(p);
  55340. BN_free(g);
  55341. }
  55342. p = NULL;
  55343. g = NULL;
  55344. ExpectIntEQ(DH_generate_key(dh), 0);
  55345. DH_free(dh);
  55346. dh = NULL;
  55347. #endif
  55348. #endif
  55349. /* Test DH_up_ref() */
  55350. dh = wolfSSL_DH_new();
  55351. ExpectNotNull(dh);
  55352. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  55353. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  55354. DH_free(dh); /* decrease ref count */
  55355. DH_free(dh); /* free WOLFSSL_DH */
  55356. dh = NULL;
  55357. q = NULL;
  55358. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  55359. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  55360. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  55361. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  55362. #ifdef HAVE_FFDHE_2048
  55363. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  55364. DH_free(dh);
  55365. dh = NULL;
  55366. q = NULL;
  55367. #endif
  55368. #ifdef HAVE_FFDHE_3072
  55369. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  55370. DH_free(dh);
  55371. dh = NULL;
  55372. q = NULL;
  55373. #endif
  55374. #ifdef HAVE_FFDHE_4096
  55375. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  55376. DH_free(dh);
  55377. dh = NULL;
  55378. q = NULL;
  55379. #endif
  55380. #else
  55381. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  55382. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  55383. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  55384. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  55385. #endif /* OPENSSL_EXTRA && !NO_DH */
  55386. return EXPECT_RESULT();
  55387. }
  55388. static int test_wolfSSL_DH_dup(void)
  55389. {
  55390. EXPECT_DECLS;
  55391. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  55392. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  55393. defined(OPENSSL_EXTRA)
  55394. DH *dh = NULL;
  55395. DH *dhDup = NULL;
  55396. ExpectNotNull(dh = wolfSSL_DH_new());
  55397. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  55398. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  55399. #if defined(OPENSSL_ALL) || \
  55400. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  55401. {
  55402. WOLFSSL_BIGNUM* p = NULL;
  55403. WOLFSSL_BIGNUM* g = NULL;
  55404. ExpectNotNull(p = wolfSSL_BN_new());
  55405. ExpectNotNull(g = wolfSSL_BN_new());
  55406. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  55407. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  55408. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  55409. if (EXPECT_FAIL()) {
  55410. wolfSSL_BN_free(p);
  55411. wolfSSL_BN_free(g);
  55412. }
  55413. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  55414. wolfSSL_DH_free(dhDup);
  55415. }
  55416. #endif
  55417. wolfSSL_DH_free(dh);
  55418. #endif
  55419. #endif
  55420. return EXPECT_RESULT();
  55421. }
  55422. static int test_wolfSSL_DH_check(void)
  55423. {
  55424. EXPECT_DECLS;
  55425. #ifdef OPENSSL_ALL
  55426. #ifndef NO_DH
  55427. #ifndef NO_BIO
  55428. #ifndef NO_DSA
  55429. byte buf[6000];
  55430. char file[] = "./certs/dsaparams.pem";
  55431. XFILE f = XBADFILE;
  55432. int bytes;
  55433. BIO* bio = NULL;
  55434. DSA* dsa = NULL;
  55435. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  55436. static const byte dh2048[] = {
  55437. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  55438. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  55439. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  55440. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  55441. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  55442. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  55443. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  55444. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  55445. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  55446. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  55447. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  55448. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  55449. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  55450. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  55451. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  55452. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  55453. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  55454. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  55455. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  55456. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  55457. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  55458. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  55459. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  55460. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  55461. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  55462. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  55463. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  55464. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  55465. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  55466. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  55467. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  55468. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  55469. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  55470. 0x93, 0x02, 0x01, 0x02
  55471. };
  55472. const byte* params;
  55473. #endif
  55474. DH* dh = NULL;
  55475. WOLFSSL_BIGNUM* p = NULL;
  55476. WOLFSSL_BIGNUM* g = NULL;
  55477. WOLFSSL_BIGNUM* pTmp = NULL;
  55478. WOLFSSL_BIGNUM* gTmp = NULL;
  55479. int codes = -1;
  55480. #ifndef NO_DSA
  55481. /* Initialize DH */
  55482. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  55483. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  55484. if (f != XBADFILE)
  55485. XFCLOSE(f);
  55486. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  55487. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  55488. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  55489. ExpectNotNull(dh);
  55490. BIO_free(bio);
  55491. DSA_free(dsa);
  55492. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  55493. params = dh2048;
  55494. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  55495. (long)sizeof(dh2048)));
  55496. #else
  55497. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  55498. #endif
  55499. /* Test assumed to be valid dh.
  55500. * Should return WOLFSSL_SUCCESS
  55501. * codes should be 0
  55502. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  55503. */
  55504. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  55505. ExpectIntEQ(codes, 0);
  55506. /* Test NULL dh: expected BAD_FUNC_ARG */
  55507. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  55508. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  55509. if (dh != NULL) {
  55510. pTmp = dh->p;
  55511. dh->p = NULL;
  55512. }
  55513. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  55514. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  55515. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  55516. /* set dh->p back to normal so it won't fail on next tests */
  55517. if (dh != NULL) {
  55518. dh->p = pTmp;
  55519. pTmp = NULL;
  55520. }
  55521. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  55522. if (dh != NULL) {
  55523. gTmp = dh->g;
  55524. dh->g = NULL;
  55525. }
  55526. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  55527. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  55528. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  55529. if (dh != NULL) {
  55530. dh->g = gTmp;
  55531. gTmp = NULL;
  55532. }
  55533. /* Cleanup */
  55534. DH_free(dh);
  55535. dh = NULL;
  55536. dh = DH_new();
  55537. ExpectNotNull(dh);
  55538. /* Check empty DH. */
  55539. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  55540. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  55541. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  55542. /* Check non-prime valued p. */
  55543. ExpectNotNull(p = BN_new());
  55544. ExpectIntEQ(BN_set_word(p, 4), 1);
  55545. ExpectNotNull(g = BN_new());
  55546. ExpectIntEQ(BN_set_word(g, 2), 1);
  55547. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  55548. if (EXPECT_FAIL()) {
  55549. wolfSSL_BN_free(p);
  55550. wolfSSL_BN_free(g);
  55551. }
  55552. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  55553. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  55554. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  55555. DH_free(dh);
  55556. dh = NULL;
  55557. #endif
  55558. #endif /* !NO_DH && !NO_DSA */
  55559. #endif
  55560. return EXPECT_RESULT();
  55561. }
  55562. static int test_wolfSSL_DH_prime(void)
  55563. {
  55564. EXPECT_DECLS;
  55565. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  55566. WOLFSSL_BIGNUM* bn = NULL;
  55567. #if WOLFSSL_MAX_BN_BITS >= 768
  55568. WOLFSSL_BIGNUM* bn2 = NULL;
  55569. #endif
  55570. bn = wolfSSL_DH_768_prime(NULL);
  55571. #if WOLFSSL_MAX_BN_BITS >= 768
  55572. ExpectNotNull(bn);
  55573. bn2 = wolfSSL_DH_768_prime(bn);
  55574. ExpectNotNull(bn2);
  55575. ExpectTrue(bn == bn2);
  55576. wolfSSL_BN_free(bn);
  55577. bn = NULL;
  55578. #else
  55579. ExpectNull(bn);
  55580. #endif
  55581. bn = wolfSSL_DH_1024_prime(NULL);
  55582. #if WOLFSSL_MAX_BN_BITS >= 1024
  55583. ExpectNotNull(bn);
  55584. wolfSSL_BN_free(bn);
  55585. bn = NULL;
  55586. #else
  55587. ExpectNull(bn);
  55588. #endif
  55589. bn = wolfSSL_DH_2048_prime(NULL);
  55590. #if WOLFSSL_MAX_BN_BITS >= 2048
  55591. ExpectNotNull(bn);
  55592. wolfSSL_BN_free(bn);
  55593. bn = NULL;
  55594. #else
  55595. ExpectNull(bn);
  55596. #endif
  55597. bn = wolfSSL_DH_3072_prime(NULL);
  55598. #if WOLFSSL_MAX_BN_BITS >= 3072
  55599. ExpectNotNull(bn);
  55600. wolfSSL_BN_free(bn);
  55601. bn = NULL;
  55602. #else
  55603. ExpectNull(bn);
  55604. #endif
  55605. bn = wolfSSL_DH_4096_prime(NULL);
  55606. #if WOLFSSL_MAX_BN_BITS >= 4096
  55607. ExpectNotNull(bn);
  55608. wolfSSL_BN_free(bn);
  55609. bn = NULL;
  55610. #else
  55611. ExpectNull(bn);
  55612. #endif
  55613. bn = wolfSSL_DH_6144_prime(NULL);
  55614. #if WOLFSSL_MAX_BN_BITS >= 6144
  55615. ExpectNotNull(bn);
  55616. wolfSSL_BN_free(bn);
  55617. bn = NULL;
  55618. #else
  55619. ExpectNull(bn);
  55620. #endif
  55621. bn = wolfSSL_DH_8192_prime(NULL);
  55622. #if WOLFSSL_MAX_BN_BITS >= 8192
  55623. ExpectNotNull(bn);
  55624. wolfSSL_BN_free(bn);
  55625. bn = NULL;
  55626. #else
  55627. ExpectNull(bn);
  55628. #endif
  55629. #endif
  55630. return EXPECT_RESULT();
  55631. }
  55632. static int test_wolfSSL_DH_1536_prime(void)
  55633. {
  55634. EXPECT_DECLS;
  55635. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  55636. BIGNUM* bn = NULL;
  55637. unsigned char bits[200];
  55638. int sz = 192; /* known binary size */
  55639. const byte expected[] = {
  55640. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  55641. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  55642. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  55643. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  55644. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  55645. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  55646. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  55647. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  55648. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  55649. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  55650. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  55651. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  55652. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  55653. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  55654. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  55655. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  55656. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  55657. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  55658. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  55659. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  55660. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  55661. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  55662. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  55663. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  55664. };
  55665. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  55666. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  55667. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  55668. BN_free(bn);
  55669. #endif
  55670. return EXPECT_RESULT();
  55671. }
  55672. static int test_wolfSSL_DH_get_2048_256(void)
  55673. {
  55674. EXPECT_DECLS;
  55675. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  55676. WOLFSSL_DH* dh = NULL;
  55677. const WOLFSSL_BIGNUM* pBn;
  55678. const WOLFSSL_BIGNUM* gBn;
  55679. const WOLFSSL_BIGNUM* qBn;
  55680. const byte pExpected[] = {
  55681. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  55682. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  55683. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  55684. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  55685. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  55686. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  55687. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  55688. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  55689. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  55690. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  55691. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  55692. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  55693. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  55694. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  55695. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  55696. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  55697. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  55698. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  55699. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  55700. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  55701. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  55702. 0x1E, 0x1A, 0x15, 0x97
  55703. };
  55704. const byte gExpected[] = {
  55705. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  55706. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  55707. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  55708. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  55709. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  55710. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  55711. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  55712. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  55713. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  55714. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  55715. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  55716. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  55717. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  55718. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  55719. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  55720. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  55721. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  55722. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  55723. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  55724. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  55725. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  55726. 0x6C, 0xC4, 0x16, 0x59
  55727. };
  55728. const byte qExpected[] = {
  55729. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  55730. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  55731. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  55732. };
  55733. int pSz = 0;
  55734. int qSz = 0;
  55735. int gSz = 0;
  55736. byte* pReturned = NULL;
  55737. byte* qReturned = NULL;
  55738. byte* gReturned = NULL;
  55739. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  55740. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  55741. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  55742. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  55743. DYNAMIC_TYPE_TMP_BUFFER));
  55744. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  55745. ExpectIntEQ(pSz, sizeof(pExpected));
  55746. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  55747. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  55748. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  55749. DYNAMIC_TYPE_TMP_BUFFER));
  55750. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  55751. ExpectIntEQ(qSz, sizeof(qExpected));
  55752. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  55753. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  55754. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  55755. DYNAMIC_TYPE_TMP_BUFFER));
  55756. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  55757. ExpectIntEQ(gSz, sizeof(gExpected));
  55758. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  55759. wolfSSL_DH_free(dh);
  55760. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55761. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55762. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55763. #endif
  55764. return EXPECT_RESULT();
  55765. }
  55766. static int test_wolfSSL_PEM_write_DHparams(void)
  55767. {
  55768. EXPECT_DECLS;
  55769. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  55770. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  55771. DH* dh = NULL;
  55772. BIO* bio = NULL;
  55773. XFILE fp = XBADFILE;
  55774. byte pem[2048];
  55775. int pemSz = 0;
  55776. const char expected[] =
  55777. "-----BEGIN DH PARAMETERS-----\n"
  55778. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  55779. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  55780. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  55781. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  55782. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  55783. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  55784. "-----END DH PARAMETERS-----\n";
  55785. const char badPem[] =
  55786. "-----BEGIN DH PARAMETERS-----\n"
  55787. "-----END DH PARAMETERS-----\n";
  55788. const char emptySeqPem[] =
  55789. "-----BEGIN DH PARAMETERS-----\n"
  55790. "MAA=\n"
  55791. "-----END DH PARAMETERS-----\n";
  55792. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  55793. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  55794. if (fp != XBADFILE) {
  55795. XFCLOSE(fp);
  55796. fp = XBADFILE;
  55797. }
  55798. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  55799. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  55800. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  55801. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  55802. (int)sizeof(badPem));
  55803. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  55804. BIO_free(bio);
  55805. bio = NULL;
  55806. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  55807. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  55808. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  55809. (int)sizeof(emptySeqPem));
  55810. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  55811. BIO_free(bio);
  55812. bio = NULL;
  55813. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  55814. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  55815. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  55816. BIO_free(bio);
  55817. bio = NULL;
  55818. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  55819. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  55820. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  55821. DH_free(dh);
  55822. dh = NULL;
  55823. dh = wolfSSL_DH_new();
  55824. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  55825. if (fp != XBADFILE) {
  55826. XFCLOSE(fp);
  55827. fp = XBADFILE;
  55828. }
  55829. wolfSSL_DH_free(dh);
  55830. dh = NULL;
  55831. /* check results */
  55832. XMEMSET(pem, 0, sizeof(pem));
  55833. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  55834. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  55835. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  55836. if (fp != XBADFILE)
  55837. XFCLOSE(fp);
  55838. #endif
  55839. return EXPECT_RESULT();
  55840. }
  55841. static int test_wolfSSL_d2i_DHparams(void)
  55842. {
  55843. EXPECT_DECLS;
  55844. #ifdef OPENSSL_ALL
  55845. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  55846. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  55847. XFILE f = XBADFILE;
  55848. unsigned char buf[4096];
  55849. const unsigned char* pt = buf;
  55850. #ifdef HAVE_FFDHE_2048
  55851. const char* params1 = "./certs/dh2048.der";
  55852. #endif
  55853. #ifdef HAVE_FFDHE_3072
  55854. const char* params2 = "./certs/dh3072.der";
  55855. #endif
  55856. long len = 0;
  55857. WOLFSSL_DH* dh = NULL;
  55858. XMEMSET(buf, 0, sizeof(buf));
  55859. /* Test 2048 bit parameters */
  55860. #ifdef HAVE_FFDHE_2048
  55861. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  55862. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55863. if (f != XBADFILE) {
  55864. XFCLOSE(f);
  55865. f = XBADFILE;
  55866. }
  55867. /* Valid case */
  55868. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  55869. ExpectNotNull(dh->p);
  55870. ExpectNotNull(dh->g);
  55871. ExpectTrue(pt == buf);
  55872. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  55873. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  55874. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  55875. /* Invalid cases */
  55876. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  55877. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  55878. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  55879. DH_free(dh);
  55880. dh = NULL;
  55881. *buf = 0;
  55882. pt = buf;
  55883. #endif /* HAVE_FFDHE_2048 */
  55884. /* Test 3072 bit parameters */
  55885. #ifdef HAVE_FFDHE_3072
  55886. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  55887. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55888. if (f != XBADFILE) {
  55889. XFCLOSE(f);
  55890. f = XBADFILE;
  55891. }
  55892. /* Valid case */
  55893. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  55894. ExpectNotNull(dh->p);
  55895. ExpectNotNull(dh->g);
  55896. ExpectTrue(pt != buf);
  55897. ExpectIntEQ(DH_generate_key(dh), 1);
  55898. /* Invalid cases */
  55899. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  55900. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  55901. DH_free(dh);
  55902. dh = NULL;
  55903. #endif /* HAVE_FFDHE_3072 */
  55904. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  55905. #endif /* !NO_DH */
  55906. #endif
  55907. return EXPECT_RESULT();
  55908. }
  55909. static int test_wolfSSL_DH_LoadDer(void)
  55910. {
  55911. EXPECT_DECLS;
  55912. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  55913. defined(OPENSSL_EXTRA)
  55914. static const byte dh2048[] = {
  55915. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  55916. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  55917. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  55918. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  55919. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  55920. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  55921. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  55922. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  55923. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  55924. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  55925. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  55926. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  55927. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  55928. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  55929. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  55930. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  55931. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  55932. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  55933. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  55934. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  55935. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  55936. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  55937. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  55938. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  55939. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  55940. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  55941. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  55942. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  55943. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  55944. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  55945. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  55946. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  55947. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  55948. 0x93, 0x02, 0x01, 0x02
  55949. };
  55950. WOLFSSL_DH* dh = NULL;
  55951. ExpectNotNull(dh = wolfSSL_DH_new());
  55952. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  55953. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  55954. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  55955. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  55956. wolfSSL_DH_free(dh);
  55957. #endif
  55958. return EXPECT_RESULT();
  55959. }
  55960. static int test_wolfSSL_i2d_DHparams(void)
  55961. {
  55962. EXPECT_DECLS;
  55963. #ifdef OPENSSL_ALL
  55964. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  55965. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  55966. XFILE f = XBADFILE;
  55967. unsigned char buf[4096];
  55968. const unsigned char* pt;
  55969. unsigned char* pt2;
  55970. #ifdef HAVE_FFDHE_2048
  55971. const char* params1 = "./certs/dh2048.der";
  55972. #endif
  55973. #ifdef HAVE_FFDHE_3072
  55974. const char* params2 = "./certs/dh3072.der";
  55975. #endif
  55976. long len = 0;
  55977. WOLFSSL_DH* dh = NULL;
  55978. /* Test 2048 bit parameters */
  55979. #ifdef HAVE_FFDHE_2048
  55980. pt = buf;
  55981. pt2 = buf;
  55982. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  55983. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55984. if (f != XBADFILE) {
  55985. XFCLOSE(f);
  55986. f = XBADFILE;
  55987. }
  55988. /* Valid case */
  55989. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  55990. ExpectTrue(pt == buf);
  55991. ExpectIntEQ(DH_generate_key(dh), 1);
  55992. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  55993. /* Invalid case */
  55994. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  55995. /* Return length only */
  55996. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  55997. DH_free(dh);
  55998. dh = NULL;
  55999. *buf = 0;
  56000. #endif
  56001. /* Test 3072 bit parameters */
  56002. #ifdef HAVE_FFDHE_3072
  56003. pt = buf;
  56004. pt2 = buf;
  56005. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  56006. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  56007. if (f != XBADFILE) {
  56008. XFCLOSE(f);
  56009. f = XBADFILE;
  56010. }
  56011. /* Valid case */
  56012. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  56013. ExpectTrue(pt == buf);
  56014. ExpectIntEQ(DH_generate_key(dh), 1);
  56015. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  56016. /* Invalid case */
  56017. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  56018. /* Return length only */
  56019. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  56020. DH_free(dh);
  56021. dh = NULL;
  56022. #endif
  56023. dh = DH_new();
  56024. ExpectNotNull(dh);
  56025. pt2 = buf;
  56026. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  56027. DH_free(dh);
  56028. dh = NULL;
  56029. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  56030. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  56031. #endif
  56032. return EXPECT_RESULT();
  56033. }
  56034. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  56035. /*----------------------------------------------------------------------------*
  56036. | EC
  56037. *----------------------------------------------------------------------------*/
  56038. static int test_wolfSSL_EC_GROUP(void)
  56039. {
  56040. EXPECT_DECLS;
  56041. #ifdef OPENSSL_EXTRA
  56042. EC_GROUP *group = NULL;
  56043. EC_GROUP *group2 = NULL;
  56044. EC_GROUP *group3 = NULL;
  56045. #ifndef HAVE_ECC_BRAINPOOL
  56046. EC_GROUP *group4 = NULL;
  56047. #endif
  56048. WOLFSSL_BIGNUM* order = NULL;
  56049. int group_bits;
  56050. int i;
  56051. static const int knownEccNids[] = {
  56052. NID_X9_62_prime192v1,
  56053. NID_X9_62_prime192v2,
  56054. NID_X9_62_prime192v3,
  56055. NID_X9_62_prime239v1,
  56056. NID_X9_62_prime239v2,
  56057. NID_X9_62_prime239v3,
  56058. NID_X9_62_prime256v1,
  56059. NID_secp112r1,
  56060. NID_secp112r2,
  56061. NID_secp128r1,
  56062. NID_secp128r2,
  56063. NID_secp160r1,
  56064. NID_secp160r2,
  56065. NID_secp224r1,
  56066. NID_secp384r1,
  56067. NID_secp521r1,
  56068. NID_secp160k1,
  56069. NID_secp192k1,
  56070. NID_secp224k1,
  56071. NID_secp256k1,
  56072. NID_brainpoolP160r1,
  56073. NID_brainpoolP192r1,
  56074. NID_brainpoolP224r1,
  56075. NID_brainpoolP256r1,
  56076. NID_brainpoolP320r1,
  56077. NID_brainpoolP384r1,
  56078. NID_brainpoolP512r1,
  56079. };
  56080. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  56081. static const int knownEccEnums[] = {
  56082. ECC_SECP192R1,
  56083. ECC_PRIME192V2,
  56084. ECC_PRIME192V3,
  56085. ECC_PRIME239V1,
  56086. ECC_PRIME239V2,
  56087. ECC_PRIME239V3,
  56088. ECC_SECP256R1,
  56089. ECC_SECP112R1,
  56090. ECC_SECP112R2,
  56091. ECC_SECP128R1,
  56092. ECC_SECP128R2,
  56093. ECC_SECP160R1,
  56094. ECC_SECP160R2,
  56095. ECC_SECP224R1,
  56096. ECC_SECP384R1,
  56097. ECC_SECP521R1,
  56098. ECC_SECP160K1,
  56099. ECC_SECP192K1,
  56100. ECC_SECP224K1,
  56101. ECC_SECP256K1,
  56102. ECC_BRAINPOOLP160R1,
  56103. ECC_BRAINPOOLP192R1,
  56104. ECC_BRAINPOOLP224R1,
  56105. ECC_BRAINPOOLP256R1,
  56106. ECC_BRAINPOOLP320R1,
  56107. ECC_BRAINPOOLP384R1,
  56108. ECC_BRAINPOOLP512R1,
  56109. };
  56110. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  56111. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  56112. ExpectNotNull(group2 = EC_GROUP_dup(group));
  56113. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  56114. #ifndef HAVE_ECC_BRAINPOOL
  56115. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  56116. NID_brainpoolP256r1));
  56117. #endif
  56118. ExpectNull(EC_GROUP_dup(NULL));
  56119. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  56120. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  56121. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  56122. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  56123. #ifndef HAVE_ECC_BRAINPOOL
  56124. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  56125. #endif
  56126. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  56127. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  56128. ExpectNotNull(order = BN_new());
  56129. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  56130. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  56131. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  56132. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  56133. wolfSSL_BN_free(order);
  56134. ExpectNotNull(EC_GROUP_method_of(group));
  56135. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  56136. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  56137. NID_X9_62_prime_field);
  56138. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  56139. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  56140. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  56141. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  56142. #ifndef NO_WOLFSSL_STUB
  56143. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  56144. #endif
  56145. #ifndef HAVE_ECC_BRAINPOOL
  56146. EC_GROUP_free(group4);
  56147. #endif
  56148. EC_GROUP_free(group3);
  56149. EC_GROUP_free(group2);
  56150. EC_GROUP_free(group);
  56151. for (i = 0; i < knowEccNidsLen; i++) {
  56152. group = NULL;
  56153. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  56154. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  56155. EC_GROUP_free(group);
  56156. }
  56157. for (i = 0; i < knowEccEnumsLen; i++) {
  56158. group = NULL;
  56159. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  56160. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  56161. EC_GROUP_free(group);
  56162. }
  56163. #endif
  56164. return EXPECT_RESULT();
  56165. }
  56166. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  56167. {
  56168. EXPECT_DECLS;
  56169. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  56170. EC_GROUP *group = NULL;
  56171. BIO* bio = NULL;
  56172. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  56173. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  56174. EC_GROUP *ret = NULL;
  56175. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  56176. "BgUrgQQAIg==\n"
  56177. "-----END EC PARAMETERS-----";
  56178. #endif
  56179. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  56180. "MAA=\n"
  56181. "-----END EC PARAMETERS-----";
  56182. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  56183. "BgA=\n"
  56184. "-----END EC PARAMETERS-----";
  56185. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  56186. "BgE=\n"
  56187. "-----END EC PARAMETERS-----";
  56188. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  56189. "BgE*\n"
  56190. "-----END EC PARAMETERS-----";
  56191. /* Test that first parameter, bio, being NULL fails. */
  56192. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  56193. /* Test that reading named parameters works. */
  56194. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  56195. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  56196. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  56197. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  56198. BIO_free(bio);
  56199. bio = NULL;
  56200. EC_GROUP_free(group);
  56201. group = NULL;
  56202. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  56203. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  56204. /* Test that reusing group works. */
  56205. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  56206. sizeof(ec_nc_p384)));
  56207. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  56208. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  56209. BIO_free(bio);
  56210. bio = NULL;
  56211. EC_GROUP_free(group);
  56212. group = NULL;
  56213. /* Test that returning through group works. */
  56214. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  56215. sizeof(ec_nc_p384)));
  56216. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  56217. ExpectIntEQ(group == ret, 1);
  56218. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  56219. BIO_free(bio);
  56220. bio = NULL;
  56221. EC_GROUP_free(group);
  56222. group = NULL;
  56223. #endif
  56224. /* Test 0x30, 0x00 (not and object id) fails. */
  56225. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  56226. sizeof(ec_nc_bad_1)));
  56227. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  56228. BIO_free(bio);
  56229. bio = NULL;
  56230. /* Test 0x06, 0x00 (empty object id) fails. */
  56231. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  56232. sizeof(ec_nc_bad_2)));
  56233. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  56234. BIO_free(bio);
  56235. bio = NULL;
  56236. /* Test 0x06, 0x01 (badly formed object id) fails. */
  56237. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  56238. sizeof(ec_nc_bad_3)));
  56239. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  56240. BIO_free(bio);
  56241. bio = NULL;
  56242. /* Test invalid PEM encoding - invalid character. */
  56243. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  56244. sizeof(ec_nc_bad_4)));
  56245. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  56246. BIO_free(bio);
  56247. #endif
  56248. return EXPECT_RESULT();
  56249. }
  56250. static int test_wolfSSL_EC_POINT(void)
  56251. {
  56252. EXPECT_DECLS;
  56253. #if !defined(WOLFSSL_SP_MATH) && \
  56254. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  56255. #ifdef OPENSSL_EXTRA
  56256. BN_CTX* ctx = NULL;
  56257. EC_GROUP* group = NULL;
  56258. #ifndef HAVE_ECC_BRAINPOOL
  56259. EC_GROUP* group2 = NULL;
  56260. #endif
  56261. EC_POINT* Gxy = NULL;
  56262. EC_POINT* new_point = NULL;
  56263. EC_POINT* set_point = NULL;
  56264. EC_POINT* get_point = NULL;
  56265. EC_POINT* infinity = NULL;
  56266. BIGNUM* k = NULL;
  56267. BIGNUM* Gx = NULL;
  56268. BIGNUM* Gy = NULL;
  56269. BIGNUM* Gz = NULL;
  56270. BIGNUM* X = NULL;
  56271. BIGNUM* Y = NULL;
  56272. BIGNUM* set_point_bn = NULL;
  56273. char* hexStr = NULL;
  56274. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  56275. "17AF381913FF7A96314EA47055EA0FD0";
  56276. /* NISTP256R1 Gx/Gy */
  56277. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  56278. "77037D812DEB33A0F4A13945D898C296";
  56279. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  56280. "2BCE33576B315ECECBB6406837BF51F5";
  56281. const char* uncompG
  56282. = "046B17D1F2E12C4247F8BCE6E563A440F2"
  56283. "77037D812DEB33A0F4A13945D898C296"
  56284. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  56285. "2BCE33576B315ECECBB6406837BF51F5";
  56286. const char* compG
  56287. = "036B17D1F2E12C4247F8BCE6E563A440F2"
  56288. "77037D812DEB33A0F4A13945D898C296";
  56289. #ifndef HAVE_SELFTEST
  56290. EC_POINT *tmp = NULL;
  56291. size_t bin_len;
  56292. unsigned int blen = 0;
  56293. unsigned char* buf = NULL;
  56294. unsigned char bufInf[1] = { 0x00 };
  56295. const unsigned char binUncompG[] = {
  56296. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  56297. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  56298. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  56299. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  56300. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  56301. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  56302. };
  56303. const unsigned char binUncompGBad[] = {
  56304. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  56305. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  56306. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  56307. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  56308. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  56309. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  56310. };
  56311. #ifdef HAVE_COMP_KEY
  56312. const unsigned char binCompG[] = {
  56313. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  56314. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  56315. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  56316. };
  56317. #endif
  56318. #endif
  56319. ExpectNotNull(ctx = BN_CTX_new());
  56320. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  56321. #ifndef HAVE_ECC_BRAINPOOL
  56322. /* Used to make groups curve_idx == -1. */
  56323. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  56324. #endif
  56325. ExpectNull(EC_POINT_new(NULL));
  56326. ExpectNotNull(Gxy = EC_POINT_new(group));
  56327. ExpectNotNull(new_point = EC_POINT_new(group));
  56328. ExpectNotNull(set_point = EC_POINT_new(group));
  56329. ExpectNotNull(X = BN_new());
  56330. ExpectNotNull(Y = BN_new());
  56331. ExpectNotNull(set_point_bn = BN_new());
  56332. ExpectNotNull(infinity = EC_POINT_new(group));
  56333. /* load test values */
  56334. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  56335. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  56336. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  56337. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  56338. /* populate coordinates for input point */
  56339. if (Gxy != NULL) {
  56340. Gxy->X = Gx;
  56341. Gxy->Y = Gy;
  56342. Gxy->Z = Gz;
  56343. }
  56344. /* Test handling of NULL point. */
  56345. EC_POINT_clear_free(NULL);
  56346. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  56347. NULL, NULL, ctx), 0);
  56348. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  56349. NULL, NULL, ctx), 0);
  56350. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  56351. NULL, NULL, ctx), 0);
  56352. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  56353. X, NULL, ctx), 0);
  56354. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  56355. NULL, Y, ctx), 0);
  56356. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  56357. X, Y, ctx), 0);
  56358. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  56359. X, Y, ctx), 0);
  56360. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  56361. NULL, Y, ctx), 0);
  56362. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  56363. X, NULL, ctx), 0);
  56364. /* Getting point at infinity returns an error. */
  56365. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  56366. X, Y, ctx), 0);
  56367. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  56368. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  56369. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  56370. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  56371. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  56372. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  56373. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  56374. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  56375. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  56376. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  56377. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  56378. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  56379. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  56380. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  56381. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  56382. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  56383. /* perform point multiplication */
  56384. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  56385. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  56386. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  56387. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  56388. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  56389. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  56390. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  56391. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  56392. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  56393. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  56394. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  56395. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  56396. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  56397. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  56398. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  56399. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  56400. /* Set point to something. */
  56401. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  56402. #else
  56403. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  56404. ctx), 1);
  56405. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  56406. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  56407. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  56408. #endif
  56409. /* check if point X coordinate is zero */
  56410. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  56411. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  56412. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  56413. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  56414. #endif
  56415. /* extract the coordinates from point */
  56416. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  56417. ctx), WOLFSSL_SUCCESS);
  56418. /* check if point X coordinate is zero */
  56419. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  56420. /* set the same X and Y points in another object */
  56421. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  56422. ctx), WOLFSSL_SUCCESS);
  56423. /* compare points as they should be the same */
  56424. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  56425. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  56426. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  56427. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  56428. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  56429. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  56430. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  56431. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  56432. /* Test copying */
  56433. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  56434. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  56435. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  56436. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  56437. /* Test inverting */
  56438. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  56439. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  56440. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  56441. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  56442. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  56443. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  56444. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  56445. {
  56446. EC_POINT* orig_point = NULL;
  56447. ExpectNotNull(orig_point = EC_POINT_new(group));
  56448. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  56449. 1);
  56450. /* new_point should be set_point inverted so adding it will revert
  56451. * the point back to set_point */
  56452. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  56453. NULL), 1);
  56454. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  56455. EC_POINT_free(orig_point);
  56456. }
  56457. #endif
  56458. /* Test getting affine converts from projective. */
  56459. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  56460. /* Force non-affine coordinates */
  56461. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  56462. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  56463. if (new_point != NULL) {
  56464. new_point->inSet = 0;
  56465. }
  56466. /* extract the coordinates from point */
  56467. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  56468. ctx), WOLFSSL_SUCCESS);
  56469. /* check if point ordinates have changed. */
  56470. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  56471. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  56472. /* Test check for infinity */
  56473. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  56474. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  56475. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  56476. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  56477. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  56478. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  56479. #else
  56480. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  56481. #endif
  56482. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  56483. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  56484. /* check bn2hex */
  56485. hexStr = BN_bn2hex(k);
  56486. ExpectStrEQ(hexStr, kTest);
  56487. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  56488. defined(XFPRINTF)
  56489. BN_print_fp(stderr, k);
  56490. fprintf(stderr, "\n");
  56491. #endif
  56492. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  56493. hexStr = BN_bn2hex(Gx);
  56494. ExpectStrEQ(hexStr, kGx);
  56495. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  56496. defined(XFPRINTF)
  56497. BN_print_fp(stderr, Gx);
  56498. fprintf(stderr, "\n");
  56499. #endif
  56500. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  56501. hexStr = BN_bn2hex(Gy);
  56502. ExpectStrEQ(hexStr, kGy);
  56503. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  56504. defined(XFPRINTF)
  56505. BN_print_fp(stderr, Gy);
  56506. fprintf(stderr, "\n");
  56507. #endif
  56508. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  56509. /* Test point to hex */
  56510. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  56511. ctx));
  56512. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  56513. ctx));
  56514. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  56515. ctx));
  56516. #ifndef HAVE_ECC_BRAINPOOL
  56517. /* Group not supported in wolfCrypt. */
  56518. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  56519. ctx));
  56520. #endif
  56521. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  56522. ExpectNotNull(hexStr);
  56523. ExpectStrEQ(hexStr, uncompG);
  56524. ExpectNotNull(get_point = EC_POINT_hex2point(group, hexStr, NULL, ctx));
  56525. ExpectIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
  56526. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  56527. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  56528. ExpectNotNull(hexStr);
  56529. ExpectStrEQ(hexStr, compG);
  56530. #ifdef HAVE_COMP_KEY
  56531. ExpectNotNull(get_point = EC_POINT_hex2point
  56532. (group, hexStr, get_point, ctx));
  56533. ExpectIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
  56534. #endif
  56535. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  56536. EC_POINT_free(get_point);
  56537. #ifndef HAVE_SELFTEST
  56538. /* Test point to oct */
  56539. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  56540. NULL, 0, ctx), 0);
  56541. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  56542. NULL, 0, ctx), 0);
  56543. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  56544. NULL, 0, ctx), 0);
  56545. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  56546. NULL, 0, ctx);
  56547. ExpectIntEQ(bin_len, sizeof(binUncompG));
  56548. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  56549. DYNAMIC_TYPE_ECC));
  56550. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  56551. buf, bin_len, ctx), bin_len);
  56552. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  56553. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  56554. /* Infinity (x=0, y=0) encodes as '0x00'. */
  56555. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  56556. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  56557. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  56558. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  56559. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  56560. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  56561. ExpectIntEQ(bufInf[0], 0);
  56562. wolfSSL_EC_POINT_dump(NULL, NULL);
  56563. /* Test point i2d */
  56564. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  56565. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  56566. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  56567. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  56568. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  56569. ExpectIntEQ(blen, sizeof(binUncompG));
  56570. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  56571. blen -= 1;
  56572. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  56573. blen += 1;
  56574. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  56575. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  56576. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  56577. #ifdef HAVE_COMP_KEY
  56578. /* Test point to oct compressed */
  56579. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  56580. 0, ctx);
  56581. ExpectIntEQ(bin_len, sizeof(binCompG));
  56582. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  56583. DYNAMIC_TYPE_ECC));
  56584. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  56585. bin_len, ctx), bin_len);
  56586. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  56587. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  56588. #endif
  56589. /* Test point BN */
  56590. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  56591. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  56592. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  56593. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  56594. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  56595. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  56596. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  56597. /* Test oct to point */
  56598. ExpectNotNull(tmp = EC_POINT_new(group));
  56599. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  56600. ctx), 0);
  56601. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  56602. ctx), 0);
  56603. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  56604. ctx), 0);
  56605. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  56606. sizeof(binUncompGBad), ctx), 0);
  56607. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  56608. ctx), 1);
  56609. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  56610. EC_POINT_free(tmp);
  56611. tmp = NULL;
  56612. /* Test setting BN ordinates. */
  56613. ExpectNotNull(tmp = EC_POINT_new(group));
  56614. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  56615. NULL, ctx), 0);
  56616. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  56617. NULL, ctx), 0);
  56618. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  56619. NULL, ctx), 0);
  56620. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  56621. NULL, ctx), 0);
  56622. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  56623. Gy, ctx), 0);
  56624. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  56625. ctx), 0);
  56626. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  56627. ctx), 0);
  56628. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  56629. Gy, ctx), 0);
  56630. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  56631. NULL, ctx), 0);
  56632. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  56633. ctx), 1);
  56634. EC_POINT_free(tmp);
  56635. tmp = NULL;
  56636. /* Test point d2i */
  56637. ExpectNotNull(tmp = EC_POINT_new(group));
  56638. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  56639. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  56640. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  56641. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  56642. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  56643. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  56644. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  56645. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  56646. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  56647. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  56648. EC_POINT_free(tmp);
  56649. tmp = NULL;
  56650. #ifdef HAVE_COMP_KEY
  56651. /* Test oct compressed to point */
  56652. ExpectNotNull(tmp = EC_POINT_new(group));
  56653. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  56654. 1);
  56655. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  56656. EC_POINT_free(tmp);
  56657. tmp = NULL;
  56658. /* Test point d2i - compressed */
  56659. ExpectNotNull(tmp = EC_POINT_new(group));
  56660. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  56661. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  56662. EC_POINT_free(tmp);
  56663. tmp = NULL;
  56664. #endif
  56665. #endif
  56666. /* test BN_mod_add */
  56667. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  56668. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  56669. 1);
  56670. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  56671. /* cleanup */
  56672. BN_free(X);
  56673. BN_free(Y);
  56674. BN_free(k);
  56675. BN_free(set_point_bn);
  56676. EC_POINT_free(infinity);
  56677. EC_POINT_free(new_point);
  56678. EC_POINT_free(set_point);
  56679. EC_POINT_clear_free(Gxy);
  56680. #ifndef HAVE_ECC_BRAINPOOL
  56681. EC_GROUP_free(group2);
  56682. #endif
  56683. EC_GROUP_free(group);
  56684. BN_CTX_free(ctx);
  56685. #endif
  56686. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  56687. return EXPECT_RESULT();
  56688. }
  56689. static int test_wolfSSL_SPAKE(void)
  56690. {
  56691. EXPECT_DECLS;
  56692. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  56693. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  56694. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  56695. BIGNUM* x = NULL; /* kdc priv */
  56696. BIGNUM* y = NULL; /* client priv */
  56697. BIGNUM* w = NULL; /* shared value */
  56698. byte M_bytes[] = {
  56699. /* uncompressed */
  56700. 0x04,
  56701. /* x */
  56702. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  56703. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  56704. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  56705. /* y */
  56706. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  56707. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  56708. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  56709. };
  56710. EC_POINT* M = NULL; /* shared value */
  56711. byte N_bytes[] = {
  56712. /* uncompressed */
  56713. 0x04,
  56714. /* x */
  56715. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  56716. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  56717. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  56718. /* y */
  56719. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  56720. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  56721. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  56722. };
  56723. EC_POINT* N = NULL; /* shared value */
  56724. EC_POINT* T = NULL; /* kdc pub */
  56725. EC_POINT* tmp1 = NULL; /* kdc pub */
  56726. EC_POINT* tmp2 = NULL; /* kdc pub */
  56727. EC_POINT* S = NULL; /* client pub */
  56728. EC_POINT* client_secret = NULL;
  56729. EC_POINT* kdc_secret = NULL;
  56730. EC_GROUP* group = NULL;
  56731. BN_CTX* bn_ctx = NULL;
  56732. /* Values taken from a test run of Kerberos 5 */
  56733. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  56734. ExpectNotNull(bn_ctx = BN_CTX_new());
  56735. ExpectNotNull(M = EC_POINT_new(group));
  56736. ExpectNotNull(N = EC_POINT_new(group));
  56737. ExpectNotNull(T = EC_POINT_new(group));
  56738. ExpectNotNull(tmp1 = EC_POINT_new(group));
  56739. ExpectNotNull(tmp2 = EC_POINT_new(group));
  56740. ExpectNotNull(S = EC_POINT_new(group));
  56741. ExpectNotNull(client_secret = EC_POINT_new(group));
  56742. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  56743. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  56744. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  56745. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  56746. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  56747. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  56748. "506AB395478F0AAB647752CF117B36250"), 1);
  56749. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  56750. 1);
  56751. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  56752. 1);
  56753. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  56754. /* kdc */
  56755. /* T=x*P+w*M */
  56756. /* All in one function call */
  56757. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  56758. /* Spread into separate calls */
  56759. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  56760. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  56761. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  56762. 1);
  56763. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  56764. /* client */
  56765. /* S=y*P+w*N */
  56766. /* All in one function call */
  56767. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  56768. /* Spread into separate calls */
  56769. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  56770. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  56771. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  56772. 1);
  56773. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  56774. /* K=y*(T-w*M) */
  56775. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  56776. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  56777. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  56778. 1);
  56779. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  56780. bn_ctx), 1);
  56781. /* kdc */
  56782. /* K=x*(S-w*N) */
  56783. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  56784. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  56785. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  56786. 1);
  56787. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  56788. 1);
  56789. /* kdc_secret == client_secret */
  56790. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  56791. BN_free(x);
  56792. BN_free(y);
  56793. BN_free(w);
  56794. EC_POINT_free(M);
  56795. EC_POINT_free(N);
  56796. EC_POINT_free(T);
  56797. EC_POINT_free(tmp1);
  56798. EC_POINT_free(tmp2);
  56799. EC_POINT_free(S);
  56800. EC_POINT_free(client_secret);
  56801. EC_POINT_free(kdc_secret);
  56802. EC_GROUP_free(group);
  56803. BN_CTX_free(bn_ctx);
  56804. #endif
  56805. return EXPECT_RESULT();
  56806. }
  56807. static int test_wolfSSL_EC_KEY_generate(void)
  56808. {
  56809. EXPECT_DECLS;
  56810. #ifdef OPENSSL_EXTRA
  56811. WOLFSSL_EC_KEY* key = NULL;
  56812. #ifndef HAVE_ECC_BRAINPOOL
  56813. WOLFSSL_EC_GROUP* group = NULL;
  56814. #endif
  56815. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  56816. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  56817. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  56818. wolfSSL_EC_KEY_free(key);
  56819. key = NULL;
  56820. #ifndef HAVE_ECC_BRAINPOOL
  56821. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  56822. NID_brainpoolP256r1));
  56823. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  56824. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  56825. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  56826. wolfSSL_EC_KEY_free(key);
  56827. wolfSSL_EC_GROUP_free(group);
  56828. #endif
  56829. #endif
  56830. return EXPECT_RESULT();
  56831. }
  56832. static int test_EC_i2d(void)
  56833. {
  56834. EXPECT_DECLS;
  56835. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  56836. EC_KEY *key = NULL;
  56837. EC_KEY *copy = NULL;
  56838. int len = 0;
  56839. unsigned char *buf = NULL;
  56840. unsigned char *p = NULL;
  56841. const unsigned char *tmp = NULL;
  56842. const unsigned char octBad[] = {
  56843. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  56844. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  56845. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  56846. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  56847. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  56848. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  56849. };
  56850. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  56851. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  56852. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  56853. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  56854. DYNAMIC_TYPE_TMP_BUFFER));
  56855. p = buf;
  56856. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  56857. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  56858. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  56859. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  56860. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  56861. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  56862. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  56863. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  56864. tmp = buf;
  56865. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  56866. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  56867. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  56868. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  56869. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  56870. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  56871. tmp = buf;
  56872. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  56873. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  56874. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  56875. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  56876. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  56877. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  56878. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  56879. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  56880. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  56881. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  56882. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  56883. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  56884. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  56885. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  56886. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  56887. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  56888. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  56889. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  56890. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  56891. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  56892. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  56893. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  56894. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  56895. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  56896. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  56897. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  56898. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  56899. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  56900. buf = NULL;
  56901. buf = NULL;
  56902. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  56903. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  56904. DYNAMIC_TYPE_TMP_BUFFER));
  56905. p = buf;
  56906. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  56907. p = NULL;
  56908. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  56909. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  56910. p = NULL;
  56911. /* Bad point is also an invalid private key. */
  56912. tmp = octBad;
  56913. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  56914. tmp = buf;
  56915. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  56916. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  56917. buf = NULL;
  56918. buf = NULL;
  56919. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  56920. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  56921. DYNAMIC_TYPE_TMP_BUFFER));
  56922. p = buf;
  56923. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  56924. p = NULL;
  56925. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  56926. tmp = buf;
  56927. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  56928. tmp = octBad;
  56929. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  56930. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  56931. ExpectIntEQ(EC_KEY_check_key(key), 1);
  56932. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  56933. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  56934. EC_KEY_free(key);
  56935. EC_KEY_free(copy);
  56936. #endif
  56937. return EXPECT_RESULT();
  56938. }
  56939. static int test_wolfSSL_EC_curve(void)
  56940. {
  56941. EXPECT_DECLS;
  56942. #if defined(OPENSSL_EXTRA)
  56943. int nid = NID_secp160k1;
  56944. const char* nid_name = NULL;
  56945. ExpectNull(EC_curve_nid2nist(NID_sha256));
  56946. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  56947. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  56948. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  56949. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  56950. #endif
  56951. return EXPECT_RESULT();
  56952. }
  56953. static int test_wolfSSL_EC_KEY_dup(void)
  56954. {
  56955. EXPECT_DECLS;
  56956. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  56957. WOLFSSL_EC_KEY* ecKey = NULL;
  56958. WOLFSSL_EC_KEY* dupKey = NULL;
  56959. ecc_key* srcKey = NULL;
  56960. ecc_key* destKey = NULL;
  56961. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  56962. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  56963. /* Valid cases */
  56964. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  56965. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  56966. /* Compare pubkey */
  56967. if (ecKey != NULL) {
  56968. srcKey = (ecc_key*)ecKey->internal;
  56969. }
  56970. if (dupKey != NULL) {
  56971. destKey = (ecc_key*)dupKey->internal;
  56972. }
  56973. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  56974. /* compare EC_GROUP */
  56975. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  56976. /* compare EC_POINT */
  56977. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  56978. dupKey->pub_key, NULL), MP_EQ);
  56979. /* compare BIGNUM */
  56980. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  56981. wolfSSL_EC_KEY_free(dupKey);
  56982. dupKey = NULL;
  56983. /* Invalid cases */
  56984. /* NULL key */
  56985. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  56986. /* NULL ecc_key */
  56987. if (ecKey != NULL) {
  56988. wc_ecc_free((ecc_key*)ecKey->internal);
  56989. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  56990. ecKey->internal = NULL; /* Set ecc_key to NULL */
  56991. }
  56992. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  56993. wolfSSL_EC_KEY_free(ecKey);
  56994. ecKey = NULL;
  56995. wolfSSL_EC_KEY_free(dupKey);
  56996. dupKey = NULL;
  56997. /* NULL Group */
  56998. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  56999. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  57000. if (ecKey != NULL) {
  57001. wolfSSL_EC_GROUP_free(ecKey->group);
  57002. ecKey->group = NULL; /* Set group to NULL */
  57003. }
  57004. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  57005. wolfSSL_EC_KEY_free(ecKey);
  57006. ecKey = NULL;
  57007. wolfSSL_EC_KEY_free(dupKey);
  57008. dupKey = NULL;
  57009. /* NULL public key */
  57010. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  57011. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  57012. if (ecKey != NULL) {
  57013. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  57014. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  57015. }
  57016. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  57017. if (ecKey != NULL) {
  57018. wolfSSL_EC_POINT_free(ecKey->pub_key);
  57019. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  57020. }
  57021. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  57022. wolfSSL_EC_KEY_free(ecKey);
  57023. ecKey = NULL;
  57024. wolfSSL_EC_KEY_free(dupKey);
  57025. dupKey = NULL;
  57026. /* NULL private key */
  57027. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  57028. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  57029. if (ecKey != NULL) {
  57030. wolfSSL_BN_free(ecKey->priv_key);
  57031. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  57032. }
  57033. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  57034. wolfSSL_EC_KEY_free(ecKey);
  57035. ecKey = NULL;
  57036. wolfSSL_EC_KEY_free(dupKey);
  57037. dupKey = NULL;
  57038. /* Test EC_KEY_up_ref */
  57039. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  57040. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  57041. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  57042. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  57043. /* reference count doesn't follow duplicate */
  57044. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  57045. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  57046. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  57047. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  57048. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  57049. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  57050. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  57051. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  57052. #endif
  57053. return EXPECT_RESULT();
  57054. }
  57055. static int test_wolfSSL_EC_KEY_set_group(void)
  57056. {
  57057. EXPECT_DECLS;
  57058. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  57059. defined(OPENSSL_EXTRA)
  57060. EC_KEY *key = NULL;
  57061. EC_GROUP *group = NULL;
  57062. const EC_GROUP *group2 = NULL;
  57063. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  57064. ExpectNotNull(key = EC_KEY_new());
  57065. ExpectNull(EC_KEY_get0_group(NULL));
  57066. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  57067. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  57068. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  57069. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  57070. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  57071. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  57072. EC_GROUP_free(group);
  57073. EC_KEY_free(key);
  57074. #endif
  57075. return EXPECT_RESULT();
  57076. }
  57077. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  57078. {
  57079. EXPECT_DECLS;
  57080. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  57081. BIO* bio = NULL;
  57082. EC_KEY* key = NULL;
  57083. /* Error condition: NULL key. */
  57084. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  57085. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  57086. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  57087. /* Conversion form defaults to uncompressed. */
  57088. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  57089. #ifdef HAVE_COMP_KEY
  57090. /* Explicitly set to compressed. */
  57091. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  57092. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  57093. #else
  57094. /* Will still work just won't change anything. */
  57095. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  57096. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  57097. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  57098. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  57099. #endif
  57100. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  57101. BIO_free(bio);
  57102. EC_KEY_free(key);
  57103. #endif
  57104. return EXPECT_RESULT();
  57105. }
  57106. static int test_wolfSSL_EC_KEY_private_key(void)
  57107. {
  57108. EXPECT_DECLS;
  57109. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  57110. WOLFSSL_EC_KEY* key = NULL;
  57111. WOLFSSL_BIGNUM* priv = NULL;
  57112. WOLFSSL_BIGNUM* priv2 = NULL;
  57113. WOLFSSL_BIGNUM* bn;
  57114. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57115. ExpectNotNull(priv = wolfSSL_BN_new());
  57116. ExpectNotNull(priv2 = wolfSSL_BN_new());
  57117. ExpectIntNE(BN_set_word(priv, 2), 0);
  57118. ExpectIntNE(BN_set_word(priv2, 2), 0);
  57119. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  57120. /* No private key set. */
  57121. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  57122. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  57123. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  57124. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  57125. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  57126. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  57127. ExpectPtrNE(bn, priv);
  57128. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  57129. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  57130. ExpectPtrNE(bn, priv2);
  57131. wolfSSL_BN_free(priv2);
  57132. wolfSSL_BN_free(priv);
  57133. wolfSSL_EC_KEY_free(key);
  57134. #endif
  57135. return EXPECT_RESULT();
  57136. }
  57137. static int test_wolfSSL_EC_KEY_public_key(void)
  57138. {
  57139. EXPECT_DECLS;
  57140. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  57141. WOLFSSL_EC_KEY* key = NULL;
  57142. WOLFSSL_EC_POINT* pub = NULL;
  57143. WOLFSSL_EC_POINT* point = NULL;
  57144. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57145. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  57146. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  57147. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  57148. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  57149. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  57150. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  57151. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  57152. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  57153. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  57154. ExpectPtrEq(point, pub);
  57155. wolfSSL_EC_KEY_free(key);
  57156. #endif
  57157. return EXPECT_RESULT();
  57158. }
  57159. static int test_wolfSSL_EC_KEY_print_fp(void)
  57160. {
  57161. EXPECT_DECLS;
  57162. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  57163. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  57164. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  57165. !defined(NO_STDIO_FILESYSTEM)
  57166. EC_KEY* key = NULL;
  57167. /* Bad file pointer. */
  57168. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  57169. /* NULL key. */
  57170. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  57171. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  57172. /* Negative indent. */
  57173. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  57174. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  57175. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  57176. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  57177. wolfSSL_EC_KEY_free(key);
  57178. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  57179. NID_X9_62_prime256v1)));
  57180. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  57181. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  57182. wolfSSL_EC_KEY_free(key);
  57183. #endif
  57184. return EXPECT_RESULT();
  57185. }
  57186. static int test_wolfSSL_EC_get_builtin_curves(void)
  57187. {
  57188. EXPECT_DECLS;
  57189. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  57190. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  57191. EC_builtin_curve* curves = NULL;
  57192. size_t crv_len = 0;
  57193. size_t i = 0;
  57194. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  57195. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  57196. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  57197. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  57198. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  57199. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  57200. if (curves[i].comment != NULL) {
  57201. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  57202. }
  57203. }
  57204. if (crv_len > 1) {
  57205. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  57206. }
  57207. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  57208. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  57209. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  57210. return EXPECT_RESULT();
  57211. }
  57212. static int test_wolfSSL_ECDSA_SIG(void)
  57213. {
  57214. EXPECT_DECLS;
  57215. #ifdef OPENSSL_EXTRA
  57216. WOLFSSL_ECDSA_SIG* sig = NULL;
  57217. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  57218. WOLFSSL_BIGNUM* r = NULL;
  57219. WOLFSSL_BIGNUM* s = NULL;
  57220. const WOLFSSL_BIGNUM* r2 = NULL;
  57221. const WOLFSSL_BIGNUM* s2 = NULL;
  57222. const unsigned char* cp = NULL;
  57223. unsigned char* p = NULL;
  57224. unsigned char outSig[8];
  57225. unsigned char sigData[8] =
  57226. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  57227. unsigned char sigDataBad[8] =
  57228. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  57229. wolfSSL_ECDSA_SIG_free(NULL);
  57230. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  57231. ExpectNotNull(r = wolfSSL_BN_new());
  57232. ExpectNotNull(s = wolfSSL_BN_new());
  57233. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  57234. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  57235. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  57236. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  57237. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  57238. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  57239. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  57240. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  57241. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  57242. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  57243. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  57244. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  57245. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  57246. r2 = NULL;
  57247. s2 = NULL;
  57248. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  57249. ExpectNull(r2);
  57250. ExpectNull(s2);
  57251. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  57252. if (EXPECT_FAIL()) {
  57253. wolfSSL_BN_free(r);
  57254. wolfSSL_BN_free(s);
  57255. }
  57256. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  57257. ExpectPtrEq(r2, r);
  57258. ExpectPtrEq(s2, s);
  57259. r2 = NULL;
  57260. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  57261. ExpectPtrEq(r2, r);
  57262. s2 = NULL;
  57263. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  57264. ExpectPtrEq(s2, s);
  57265. /* r and s are freed when sig is freed. */
  57266. wolfSSL_ECDSA_SIG_free(sig);
  57267. sig = NULL;
  57268. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  57269. cp = sigDataBad;
  57270. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  57271. cp = sigData;
  57272. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  57273. ExpectIntEQ((cp == sigData + 8), 1);
  57274. cp = sigData;
  57275. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  57276. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  57277. ExpectIntEQ((sig == sig2), 1);
  57278. cp = outSig;
  57279. p = outSig;
  57280. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  57281. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  57282. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  57283. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  57284. ExpectIntEQ((p == outSig + 8), 1);
  57285. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  57286. p = NULL;
  57287. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), 8);
  57288. #ifndef WOLFSSL_I2D_ECDSA_SIG_ALLOC
  57289. ExpectNull(p);
  57290. #else
  57291. ExpectNotNull(p);
  57292. ExpectIntEQ(XMEMCMP(p, outSig, 8), 0);
  57293. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  57294. #endif
  57295. wolfSSL_ECDSA_SIG_free(sig);
  57296. #endif
  57297. return EXPECT_RESULT();
  57298. }
  57299. static int test_ECDSA_size_sign(void)
  57300. {
  57301. EXPECT_DECLS;
  57302. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  57303. EC_KEY* key = NULL;
  57304. ECDSA_SIG* ecdsaSig = NULL;
  57305. int id;
  57306. byte hash[WC_MAX_DIGEST_SIZE];
  57307. byte hash2[WC_MAX_DIGEST_SIZE];
  57308. byte sig[ECC_MAX_SIG_SIZE];
  57309. unsigned int sigSz = sizeof(sig);
  57310. XMEMSET(hash, 123, sizeof(hash));
  57311. XMEMSET(hash2, 234, sizeof(hash2));
  57312. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  57313. ExpectIntEQ(id, ECC_SECP256R1);
  57314. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57315. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  57316. ExpectIntGE(ECDSA_size(NULL), 0);
  57317. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  57318. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  57319. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  57320. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, NULL), 0);
  57321. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, (int)sigSz, key), 0);
  57322. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, (int)sigSz, key), 0);
  57323. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  57324. ExpectIntGE(ECDSA_size(key), sigSz);
  57325. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, key), 1);
  57326. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, (int)sigSz, key), 0);
  57327. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  57328. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  57329. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  57330. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  57331. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  57332. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  57333. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  57334. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  57335. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  57336. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  57337. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  57338. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  57339. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  57340. ECDSA_SIG_free(ecdsaSig);
  57341. EC_KEY_free(key);
  57342. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  57343. return EXPECT_RESULT();
  57344. }
  57345. static int test_ECDH_compute_key(void)
  57346. {
  57347. EXPECT_DECLS;
  57348. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  57349. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  57350. EC_KEY* key1 = NULL;
  57351. EC_KEY* key2 = NULL;
  57352. EC_POINT* pub1 = NULL;
  57353. EC_POINT* pub2 = NULL;
  57354. byte secret1[32];
  57355. byte secret2[32];
  57356. int i;
  57357. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57358. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  57359. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  57360. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57361. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  57362. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  57363. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  57364. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  57365. 0);
  57366. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  57367. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  57368. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  57369. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  57370. 0);
  57371. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  57372. 0);
  57373. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  57374. NULL), 0);
  57375. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  57376. sizeof(secret1));
  57377. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  57378. sizeof(secret2));
  57379. for (i = 0; i < (int)sizeof(secret1); i++) {
  57380. ExpectIntEQ(secret1[i], secret2[i]);
  57381. }
  57382. EC_KEY_free(key2);
  57383. EC_KEY_free(key1);
  57384. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  57385. * !WOLF_CRYPTO_CB_ONLY_ECC */
  57386. return EXPECT_RESULT();
  57387. }
  57388. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  57389. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  57390. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  57391. !defined(NO_ASN_TIME)
  57392. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  57393. int expectedDerSz)
  57394. {
  57395. EXPECT_DECLS;
  57396. X509* x509 = NULL;
  57397. BIGNUM* serial_number = NULL;
  57398. X509_NAME* name = NULL;
  57399. time_t epoch_off = 0;
  57400. ASN1_INTEGER* asn1_serial_number;
  57401. long not_before, not_after;
  57402. int derSz;
  57403. ExpectNotNull(x509 = X509_new());
  57404. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  57405. ExpectNotNull(serial_number = BN_new());
  57406. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  57407. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  57408. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  57409. /* version 3 */
  57410. ExpectIntNE(X509_set_version(x509, 2L), 0);
  57411. ExpectNotNull(name = X509_NAME_new());
  57412. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  57413. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  57414. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  57415. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  57416. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  57417. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  57418. not_before = (long)wc_Time(NULL);
  57419. not_after = not_before + (365 * 24 * 60 * 60);
  57420. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  57421. &epoch_off));
  57422. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  57423. &epoch_off));
  57424. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  57425. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  57426. ExpectIntGE(derSz, expectedDerSz);
  57427. BN_free(serial_number);
  57428. X509_NAME_free(name);
  57429. X509_free(x509);
  57430. return EXPECT_RESULT();
  57431. }
  57432. #endif
  57433. static int test_openssl_generate_key_and_cert(void)
  57434. {
  57435. EXPECT_DECLS;
  57436. #if defined(OPENSSL_EXTRA)
  57437. int expectedDerSz;
  57438. EVP_PKEY* pkey = NULL;
  57439. #ifdef HAVE_ECC
  57440. EC_KEY* ec_key = NULL;
  57441. #endif
  57442. #if !defined(NO_RSA)
  57443. int key_length = 2048;
  57444. BIGNUM* exponent = NULL;
  57445. RSA* rsa = NULL;
  57446. ExpectNotNull(pkey = EVP_PKEY_new());
  57447. ExpectNotNull(exponent = BN_new());
  57448. ExpectNotNull(rsa = RSA_new());
  57449. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  57450. #ifndef WOLFSSL_KEY_GEN
  57451. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  57452. #if defined(USE_CERT_BUFFERS_1024)
  57453. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  57454. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  57455. key_length = 1024;
  57456. #elif defined(USE_CERT_BUFFERS_2048)
  57457. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  57458. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  57459. #else
  57460. RSA_free(rsa);
  57461. rsa = NULL;
  57462. #endif
  57463. #else
  57464. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  57465. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  57466. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  57467. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  57468. #endif
  57469. if (rsa) {
  57470. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  57471. if (EXPECT_FAIL()) {
  57472. RSA_free(rsa);
  57473. }
  57474. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  57475. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  57476. expectedDerSz = 743;
  57477. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  57478. expectedDerSz), TEST_SUCCESS);
  57479. #endif
  57480. }
  57481. EVP_PKEY_free(pkey);
  57482. pkey = NULL;
  57483. BN_free(exponent);
  57484. #endif /* !NO_RSA */
  57485. #ifdef HAVE_ECC
  57486. ExpectNotNull(pkey = EVP_PKEY_new());
  57487. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57488. #ifndef NO_WOLFSSL_STUB
  57489. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  57490. #endif
  57491. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  57492. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  57493. if (EXPECT_FAIL()) {
  57494. EC_KEY_free(ec_key);
  57495. }
  57496. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  57497. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  57498. expectedDerSz = 344;
  57499. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  57500. TEST_SUCCESS);
  57501. #endif
  57502. EVP_PKEY_free(pkey);
  57503. #endif /* HAVE_ECC */
  57504. (void)pkey;
  57505. (void)expectedDerSz;
  57506. #endif /* OPENSSL_EXTRA */
  57507. return EXPECT_RESULT();
  57508. }
  57509. static int test_stubs_are_stubs(void)
  57510. {
  57511. EXPECT_DECLS;
  57512. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  57513. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  57514. WOLFSSL_CTX* ctx = NULL;
  57515. WOLFSSL_CTX* ctxN = NULL;
  57516. #ifndef NO_WOLFSSL_CLIENT
  57517. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57518. #elif !defined(NO_WOLFSSL_SERVER)
  57519. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57520. #endif
  57521. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  57522. ExpectIntEQ((int) x(z), 0)
  57523. /* test logic, all stubs return same result regardless of ctx being NULL
  57524. * as there are no sanity checks, it's just a stub! If at some
  57525. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  57526. * if invalid inputs are supplied. Test calling both
  57527. * with and without valid inputs, if a stub functionality remains unchanged.
  57528. */
  57529. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  57530. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  57531. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  57532. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  57533. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  57534. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  57535. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  57536. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  57537. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  57538. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  57539. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  57540. /* when implemented this should take WOLFSSL object insted, right now
  57541. * always returns 0 */
  57542. ExpectIntEQ(SSL_get_current_expansion(NULL), 0);
  57543. wolfSSL_CTX_free(ctx);
  57544. ctx = NULL;
  57545. ExpectStrEQ(SSL_COMP_get_name(NULL), "not supported");
  57546. ExpectIntEQ(SSL_get_current_expansion(), 0);
  57547. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  57548. * !NO_WOLFSSL_SERVER) */
  57549. return EXPECT_RESULT();
  57550. }
  57551. static int test_CONF_modules_xxx(void)
  57552. {
  57553. int res = TEST_SKIPPED;
  57554. #if defined(OPENSSL_EXTRA)
  57555. CONF_modules_free();
  57556. CONF_modules_unload(0);
  57557. CONF_modules_unload(1);
  57558. CONF_modules_unload(-1);
  57559. res = TEST_SUCCESS;
  57560. #endif /* OPENSSL_EXTRA */
  57561. return res;
  57562. }
  57563. static int test_CRYPTO_set_dynlock_xxx(void)
  57564. {
  57565. int res = TEST_SKIPPED;
  57566. #if defined(OPENSSL_EXTRA)
  57567. CRYPTO_set_dynlock_create_callback(
  57568. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  57569. CRYPTO_set_dynlock_create_callback(
  57570. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  57571. CRYPTO_set_dynlock_destroy_callback(
  57572. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  57573. CRYPTO_set_dynlock_destroy_callback(
  57574. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  57575. CRYPTO_set_dynlock_lock_callback(
  57576. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  57577. CRYPTO_set_dynlock_lock_callback(
  57578. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  57579. res = TEST_SUCCESS;
  57580. #endif /* OPENSSL_EXTRA */
  57581. return res;
  57582. }
  57583. static int test_CRYPTO_THREADID_xxx(void)
  57584. {
  57585. EXPECT_DECLS;
  57586. #if defined(OPENSSL_EXTRA)
  57587. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  57588. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  57589. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  57590. #endif /* OPENSSL_EXTRA */
  57591. return EXPECT_RESULT();
  57592. }
  57593. static int test_ENGINE_cleanup(void)
  57594. {
  57595. int res = TEST_SKIPPED;
  57596. #if defined(OPENSSL_EXTRA)
  57597. ENGINE_cleanup();
  57598. res = TEST_SUCCESS;
  57599. #endif /* OPENSSL_EXTRA */
  57600. return res;
  57601. }
  57602. static int test_wolfSSL_CTX_LoadCRL(void)
  57603. {
  57604. EXPECT_DECLS;
  57605. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  57606. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  57607. WOLFSSL_CTX* ctx = NULL;
  57608. WOLFSSL* ssl = NULL;
  57609. const char* badPath = "dummypath";
  57610. const char* validPath = "./certs/crl";
  57611. const char* validFilePath = "./certs/crl/cliCrl.pem";
  57612. const char* issuerCert = "./certs/client-cert.pem";
  57613. int derType = WOLFSSL_FILETYPE_ASN1;
  57614. int pemType = WOLFSSL_FILETYPE_PEM;
  57615. #ifdef HAVE_CRL_MONITOR
  57616. int monitor = WOLFSSL_CRL_MONITOR;
  57617. #else
  57618. int monitor = 0;
  57619. #endif
  57620. WOLFSSL_CERT_MANAGER* cm = NULL;
  57621. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  57622. BAD_FUNC_ARG)
  57623. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  57624. NOT_COMPILED_IN)
  57625. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  57626. WOLFSSL_SUCCESS)
  57627. #ifndef NO_WOLFSSL_CLIENT
  57628. #define NEW_CTX(ctx) ExpectNotNull( \
  57629. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  57630. #elif !defined(NO_WOLFSSL_SERVER)
  57631. #define NEW_CTX(ctx) ExpectNotNull( \
  57632. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  57633. #else
  57634. #define NEW_CTX(ctx) return
  57635. #endif
  57636. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  57637. NEW_CTX(ctx);
  57638. #ifndef HAVE_CRL_MONITOR
  57639. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  57640. wolfSSL_CTX_free(ctx);
  57641. NEW_CTX(ctx);
  57642. #endif
  57643. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  57644. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  57645. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  57646. wolfSSL_CTX_free(ctx);
  57647. ctx = NULL;
  57648. NEW_CTX(ctx);
  57649. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  57650. WOLFSSL_SUCCESS);
  57651. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  57652. wolfSSL_CTX_free(ctx);
  57653. ctx = NULL;
  57654. NEW_CTX(ctx);
  57655. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  57656. WOLFSSL_SUCCESS);
  57657. ExpectNotNull(ssl = wolfSSL_new(ctx));
  57658. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  57659. wolfSSL_free(ssl);
  57660. ssl = NULL;
  57661. wolfSSL_CTX_free(ctx);
  57662. ctx = NULL;
  57663. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57664. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  57665. WOLFSSL_SUCCESS);
  57666. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  57667. WOLFSSL_SUCCESS);
  57668. wolfSSL_CertManagerFree(cm);
  57669. #endif
  57670. return EXPECT_RESULT();
  57671. }
  57672. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  57673. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  57674. {
  57675. EXPECT_DECLS;
  57676. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  57677. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  57678. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57679. return EXPECT_RESULT();
  57680. }
  57681. #endif
  57682. static int test_multiple_crls_same_issuer(void)
  57683. {
  57684. EXPECT_DECLS;
  57685. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  57686. test_ssl_cbf client_cbs, server_cbs;
  57687. struct {
  57688. const char* server_cert;
  57689. const char* server_key;
  57690. } test_params[] = {
  57691. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  57692. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  57693. };
  57694. size_t i;
  57695. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  57696. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57697. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57698. server_cbs.certPemFile = test_params[i].server_cert;
  57699. server_cbs.keyPemFile = test_params[i].server_key;
  57700. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  57701. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  57702. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57703. &server_cbs, NULL), TEST_FAIL);
  57704. }
  57705. #endif
  57706. return EXPECT_RESULT();
  57707. }
  57708. static int test_SetTmpEC_DHE_Sz(void)
  57709. {
  57710. EXPECT_DECLS;
  57711. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  57712. WOLFSSL_CTX *ctx = NULL;
  57713. WOLFSSL *ssl = NULL;
  57714. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57715. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  57716. ExpectNotNull(ssl = wolfSSL_new(ctx));
  57717. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  57718. wolfSSL_free(ssl);
  57719. wolfSSL_CTX_free(ctx);
  57720. #endif
  57721. return EXPECT_RESULT();
  57722. }
  57723. static int test_wolfSSL_CTX_get0_privatekey(void)
  57724. {
  57725. EXPECT_DECLS;
  57726. #ifdef OPENSSL_ALL
  57727. WOLFSSL_CTX* ctx = NULL;
  57728. (void)ctx;
  57729. #ifndef NO_RSA
  57730. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57731. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  57732. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  57733. WOLFSSL_FILETYPE_PEM));
  57734. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  57735. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  57736. WOLFSSL_FILETYPE_PEM));
  57737. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  57738. wolfSSL_CTX_free(ctx);
  57739. ctx = NULL;
  57740. #endif
  57741. #ifdef HAVE_ECC
  57742. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57743. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  57744. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  57745. WOLFSSL_FILETYPE_PEM));
  57746. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  57747. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  57748. WOLFSSL_FILETYPE_PEM));
  57749. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  57750. wolfSSL_CTX_free(ctx);
  57751. #endif
  57752. #endif
  57753. return EXPECT_RESULT();
  57754. }
  57755. static int test_wolfSSL_dtls_set_mtu(void)
  57756. {
  57757. EXPECT_DECLS;
  57758. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  57759. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  57760. !defined(WOLFSSL_NO_TLS12)
  57761. WOLFSSL_CTX* ctx = NULL;
  57762. WOLFSSL* ssl = NULL;
  57763. const char* testCertFile;
  57764. const char* testKeyFile;
  57765. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  57766. #ifndef NO_RSA
  57767. testCertFile = svrCertFile;
  57768. testKeyFile = svrKeyFile;
  57769. #elif defined(HAVE_ECC)
  57770. testCertFile = eccCertFile;
  57771. testKeyFile = eccKeyFile;
  57772. #endif
  57773. if (testCertFile != NULL && testKeyFile != NULL) {
  57774. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  57775. WOLFSSL_FILETYPE_PEM));
  57776. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57777. WOLFSSL_FILETYPE_PEM));
  57778. }
  57779. ExpectNotNull(ssl = wolfSSL_new(ctx));
  57780. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  57781. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  57782. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  57783. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  57784. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  57785. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  57786. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  57787. wolfSSL_free(ssl);
  57788. wolfSSL_CTX_free(ctx);
  57789. #endif
  57790. return EXPECT_RESULT();
  57791. }
  57792. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  57793. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57794. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  57795. enum HandShakeType hsType, word16 length)
  57796. {
  57797. size_t idx = 0;
  57798. byte* l;
  57799. /* record layer */
  57800. /* handshake type */
  57801. out[idx++] = handshake;
  57802. /* protocol version */
  57803. out[idx++] = 0xfe;
  57804. out[idx++] = 0xfd; /* DTLS 1.2 */
  57805. /* epoch 0 */
  57806. XMEMSET(out + idx, 0, 2);
  57807. idx += 2;
  57808. /* sequence number */
  57809. XMEMSET(out + idx, 0, 6);
  57810. c32toa(seq, out + idx + 2);
  57811. idx += 6;
  57812. /* length in BE */
  57813. if (length)
  57814. c16toa(length, out + idx);
  57815. else
  57816. c16toa(outSz - idx - 2, out + idx);
  57817. idx += 2;
  57818. /* handshake layer */
  57819. /* handshake type */
  57820. out[idx++] = (byte)hsType;
  57821. /* length */
  57822. l = out + idx;
  57823. idx += 3;
  57824. /* message seq */
  57825. c16toa(0, out + idx);
  57826. idx += 2;
  57827. /* frag offset */
  57828. c32to24(0, out + idx);
  57829. idx += 3;
  57830. /* frag length */
  57831. c32to24((word32)outSz - (word32)idx - 3, l);
  57832. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  57833. idx += 3;
  57834. XMEMSET(out + idx, 0, outSz - idx);
  57835. }
  57836. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  57837. {
  57838. byte msg[] = "This is a msg for the client";
  57839. byte reply[40];
  57840. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  57841. reply[sizeof(reply) - 1] = '\0';
  57842. fprintf(stderr, "Client message: %s\n", reply);
  57843. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  57844. }
  57845. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  57846. {
  57847. byte ch[50];
  57848. int fd = wolfSSL_get_fd(ssl);
  57849. byte msg[] = "This is a msg for the server";
  57850. byte reply[40];
  57851. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  57852. /* Server should ignore this datagram */
  57853. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  57854. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  57855. /* Server should ignore this datagram */
  57856. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  57857. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  57858. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  57859. reply[sizeof(reply) - 1] = '\0';
  57860. fprintf(stderr, "Server response: %s\n", reply);
  57861. }
  57862. static int test_wolfSSL_dtls_plaintext(void)
  57863. {
  57864. callback_functions func_cb_client;
  57865. callback_functions func_cb_server;
  57866. size_t i;
  57867. struct test_params {
  57868. method_provider client_meth;
  57869. method_provider server_meth;
  57870. ssl_callback on_result_server;
  57871. ssl_callback on_result_client;
  57872. } params[] = {
  57873. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  57874. test_wolfSSL_dtls_plaintext_server,
  57875. test_wolfSSL_dtls_plaintext_client},
  57876. };
  57877. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57878. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  57879. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  57880. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  57881. func_cb_server.method = params[i].server_meth;
  57882. func_cb_client.method = params[i].client_meth;
  57883. func_cb_client.on_result = params[i].on_result_client;
  57884. func_cb_server.on_result = params[i].on_result_server;
  57885. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  57886. if (!func_cb_client.return_code)
  57887. return TEST_FAIL;
  57888. if (!func_cb_server.return_code)
  57889. return TEST_FAIL;
  57890. }
  57891. return TEST_RES_CHECK(1);
  57892. }
  57893. #else
  57894. static int test_wolfSSL_dtls_plaintext(void) {
  57895. return TEST_SKIPPED;
  57896. }
  57897. #endif
  57898. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  57899. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57900. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  57901. {
  57902. byte b[1100]; /* buffer for the messages to send */
  57903. size_t idx = 0;
  57904. size_t seq_offset = 0;
  57905. size_t msg_offset = 0;
  57906. int i;
  57907. int fd = wolfSSL_get_fd(ssl);
  57908. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  57909. word32 seq_number = 100; /* start high so server definitely reads this */
  57910. word16 msg_number = 50; /* start high so server has to buffer this */
  57911. AssertIntEQ(ret, 1);
  57912. /* Now let's start spamming the peer with fragments it needs to store */
  57913. XMEMSET(b, -1, sizeof(b));
  57914. /* record layer */
  57915. /* handshake type */
  57916. b[idx++] = 22;
  57917. /* protocol version */
  57918. b[idx++] = 0xfe;
  57919. b[idx++] = 0xfd; /* DTLS 1.2 */
  57920. /* epoch 0 */
  57921. XMEMSET(b + idx, 0, 2);
  57922. idx += 2;
  57923. /* sequence number */
  57924. XMEMSET(b + idx, 0, 6);
  57925. seq_offset = idx + 2; /* increment only the low 32 bits */
  57926. idx += 6;
  57927. /* static length in BE */
  57928. c16toa(42, b + idx);
  57929. idx += 2;
  57930. /* handshake layer */
  57931. /* cert type */
  57932. b[idx++] = 11;
  57933. /* length */
  57934. c32to24(1000, b + idx);
  57935. idx += 3;
  57936. /* message seq */
  57937. c16toa(0, b + idx);
  57938. msg_offset = idx;
  57939. idx += 2;
  57940. /* frag offset */
  57941. c32to24(500, b + idx);
  57942. idx += 3;
  57943. /* frag length */
  57944. c32to24(30, b + idx);
  57945. idx += 3;
  57946. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  57947. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  57948. seq_number++, msg_number++, i++) {
  57949. struct timespec delay;
  57950. XMEMSET(&delay, 0, sizeof(delay));
  57951. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  57952. c32toa(seq_number, b + seq_offset);
  57953. c16toa(msg_number, b + msg_offset);
  57954. ret = (int)send(fd, b, 55, 0);
  57955. nanosleep(&delay, NULL);
  57956. }
  57957. }
  57958. #ifdef WOLFSSL_DTLS13
  57959. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  57960. {
  57961. const word16 sendCountMax = 100;
  57962. byte b[150]; /* buffer for the messages to send */
  57963. size_t idx = 0;
  57964. size_t msg_offset = 0;
  57965. int fd = wolfSSL_get_fd(ssl);
  57966. word16 msg_number = 10; /* start high so server has to buffer this */
  57967. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  57968. AssertIntEQ(ret, 1);
  57969. /* Now let's start spamming the peer with fragments it needs to store */
  57970. XMEMSET(b, -1, sizeof(b));
  57971. /* handshake type */
  57972. b[idx++] = 11;
  57973. /* length */
  57974. c32to24(10000, b + idx);
  57975. idx += 3;
  57976. /* message_seq */
  57977. msg_offset = idx;
  57978. idx += 2;
  57979. /* fragment_offset */
  57980. c32to24(5000, b + idx);
  57981. idx += 3;
  57982. /* fragment_length */
  57983. c32to24(100, b + idx);
  57984. idx += 3;
  57985. /* fragment contents */
  57986. idx += 100;
  57987. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  57988. byte sendBuf[150];
  57989. int sendSz = sizeof(sendBuf);
  57990. struct timespec delay;
  57991. XMEMSET(&delay, 0, sizeof(delay));
  57992. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  57993. c16toa(msg_number, b + msg_offset);
  57994. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  57995. (int)idx, handshake, 0, 0, 0);
  57996. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  57997. nanosleep(&delay, NULL);
  57998. }
  57999. }
  58000. #endif
  58001. static int test_wolfSSL_dtls_fragments(void)
  58002. {
  58003. EXPECT_DECLS;
  58004. callback_functions func_cb_client;
  58005. callback_functions func_cb_server;
  58006. size_t i;
  58007. struct test_params {
  58008. method_provider client_meth;
  58009. method_provider server_meth;
  58010. ssl_callback spammer;
  58011. } params[] = {
  58012. #if !defined(WOLFSSL_NO_TLS12)
  58013. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  58014. test_wolfSSL_dtls12_fragments_spammer},
  58015. #endif
  58016. #ifdef WOLFSSL_DTLS13
  58017. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  58018. test_wolfSSL_dtls13_fragments_spammer},
  58019. #endif
  58020. };
  58021. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  58022. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  58023. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  58024. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  58025. func_cb_server.method = params[i].server_meth;
  58026. func_cb_client.method = params[i].client_meth;
  58027. func_cb_client.ssl_ready = params[i].spammer;
  58028. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58029. ExpectFalse(func_cb_client.return_code);
  58030. ExpectFalse(func_cb_server.return_code);
  58031. /* The socket should be closed by the server resulting in a
  58032. * socket error, fatal error or reading a close notify alert */
  58033. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  58034. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  58035. func_cb_client.last_err != FATAL_ERROR) {
  58036. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  58037. }
  58038. /* Check the server returned an error indicating the msg buffer
  58039. * was full */
  58040. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  58041. if (EXPECT_FAIL())
  58042. break;
  58043. }
  58044. return EXPECT_RESULT();
  58045. }
  58046. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  58047. {
  58048. int fd, ret;
  58049. byte alert_msg[] = {
  58050. 0x15, /* alert type */
  58051. 0xfe, 0xfd, /* version */
  58052. 0x00, 0x00, /* epoch */
  58053. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  58054. 0x00, 0x02, /* length */
  58055. 0x02, /* level: fatal */
  58056. 0x46 /* protocol version */
  58057. };
  58058. fd = wolfSSL_get_fd(ssl);
  58059. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  58060. AssertIntGT(ret, 0);
  58061. }
  58062. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  58063. {
  58064. callback_functions client_cbs, server_cbs;
  58065. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58066. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58067. client_cbs.doUdp = server_cbs.doUdp = 1;
  58068. if (version12) {
  58069. #if !defined(WOLFSSL_NO_TLS12)
  58070. client_cbs.method = wolfDTLSv1_2_client_method;
  58071. server_cbs.method = wolfDTLSv1_2_server_method;
  58072. #else
  58073. return TEST_SKIPPED;
  58074. #endif
  58075. }
  58076. else
  58077. {
  58078. #ifdef WOLFSSL_DTLS13
  58079. client_cbs.method = wolfDTLSv1_3_client_method;
  58080. server_cbs.method = wolfDTLSv1_3_server_method;
  58081. #else
  58082. return TEST_SKIPPED;
  58083. #endif /* WOLFSSL_DTLS13 */
  58084. }
  58085. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  58086. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58087. if (!client_cbs.return_code)
  58088. return TEST_FAIL;
  58089. if (!server_cbs.return_code)
  58090. return TEST_FAIL;
  58091. return TEST_SUCCESS;
  58092. }
  58093. static int test_wolfSSL_ignore_alert_before_cookie(void)
  58094. {
  58095. int ret;
  58096. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  58097. if (ret != 0)
  58098. return ret;
  58099. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  58100. if (ret != 0)
  58101. return ret;
  58102. return 0;
  58103. }
  58104. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  58105. {
  58106. int ret;
  58107. int fd;
  58108. byte bad_msg[] = {
  58109. 0x17, /* app data */
  58110. 0xaa, 0xfd, /* bad version */
  58111. 0x00, 0x01, /* epoch 1 */
  58112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  58113. 0x00, 0x26, /* length: 38 bytes */
  58114. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  58115. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  58116. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  58117. 0x03, 0x18, 0x72
  58118. };
  58119. fd = wolfSSL_get_fd(ssl);
  58120. AssertIntGE(fd, 0);
  58121. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  58122. AssertIntEQ(ret, sizeof(bad_msg));
  58123. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  58124. AssertIntEQ(ret, sizeof("badrecordtest"));
  58125. }
  58126. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  58127. {
  58128. byte buf[100];
  58129. int ret;
  58130. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  58131. AssertIntGT(ret, 0);
  58132. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  58133. }
  58134. static int _test_wolfSSL_dtls_bad_record(
  58135. method_provider client_method, method_provider server_method)
  58136. {
  58137. callback_functions client_cbs, server_cbs;
  58138. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58139. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58140. client_cbs.doUdp = server_cbs.doUdp = 1;
  58141. client_cbs.method = client_method;
  58142. server_cbs.method = server_method;
  58143. client_cbs.on_result = test_wolfSSL_send_bad_record;
  58144. server_cbs.on_result = test_wolfSSL_read_string;
  58145. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58146. if (!client_cbs.return_code)
  58147. return TEST_FAIL;
  58148. if (!server_cbs.return_code)
  58149. return TEST_FAIL;
  58150. return TEST_SUCCESS;
  58151. }
  58152. static int test_wolfSSL_dtls_bad_record(void)
  58153. {
  58154. int ret = TEST_SUCCESS;
  58155. #if !defined(WOLFSSL_NO_TLS12)
  58156. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  58157. wolfDTLSv1_2_server_method);
  58158. #endif
  58159. #ifdef WOLFSSL_DTLS13
  58160. if (ret == TEST_SUCCESS) {
  58161. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  58162. wolfDTLSv1_3_server_method);
  58163. }
  58164. #endif /* WOLFSSL_DTLS13 */
  58165. return ret;
  58166. }
  58167. #else
  58168. static int test_wolfSSL_dtls_fragments(void) {
  58169. return TEST_SKIPPED;
  58170. }
  58171. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  58172. return TEST_SKIPPED;
  58173. }
  58174. static int test_wolfSSL_dtls_bad_record(void) {
  58175. return TEST_SKIPPED;
  58176. }
  58177. #endif
  58178. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  58179. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58180. defined(HAVE_IO_TESTS_DEPENDENCIES)
  58181. static byte test_AEAD_fail_decryption = 0;
  58182. static byte test_AEAD_seq_num = 0;
  58183. static byte test_AEAD_done = 0;
  58184. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  58185. {
  58186. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  58187. if (ret > 0) {
  58188. if (test_AEAD_fail_decryption) {
  58189. /* Modify the packet to trigger a decryption failure */
  58190. buf[ret/2] ^= 0xFF;
  58191. if (test_AEAD_fail_decryption == 1)
  58192. test_AEAD_fail_decryption = 0;
  58193. }
  58194. }
  58195. (void)ctx;
  58196. return ret;
  58197. }
  58198. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  58199. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  58200. {
  58201. if (sendLimit)
  58202. w64Zero(sendLimit);
  58203. switch (ssl->specs.bulk_cipher_algorithm) {
  58204. case wolfssl_aes_gcm:
  58205. if (sendLimit)
  58206. *sendLimit = AEAD_AES_LIMIT;
  58207. FALL_THROUGH;
  58208. case wolfssl_chacha:
  58209. if (hardLimit)
  58210. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  58211. if (keyUpdateLimit)
  58212. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  58213. break;
  58214. case wolfssl_aes_ccm:
  58215. if (sendLimit)
  58216. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  58217. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  58218. if (hardLimit)
  58219. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  58220. if (keyUpdateLimit)
  58221. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  58222. }
  58223. else {
  58224. if (hardLimit)
  58225. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  58226. if (keyUpdateLimit)
  58227. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  58228. }
  58229. break;
  58230. default:
  58231. fprintf(stderr, "Unrecognized bulk cipher");
  58232. AssertFalse(1);
  58233. break;
  58234. }
  58235. }
  58236. static void test_AEAD_limit_client(WOLFSSL* ssl)
  58237. {
  58238. int ret;
  58239. int i;
  58240. int didReKey = 0;
  58241. char msgBuf[20];
  58242. w64wrapper hardLimit;
  58243. w64wrapper keyUpdateLimit;
  58244. w64wrapper counter;
  58245. w64wrapper sendLimit;
  58246. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  58247. w64Zero(&counter);
  58248. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  58249. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  58250. for (i = 0; i < 10; i++) {
  58251. /* Test some failed decryptions */
  58252. test_AEAD_fail_decryption = 1;
  58253. w64Increment(&counter);
  58254. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58255. /* Should succeed since decryption failures are dropped */
  58256. AssertIntGT(ret, 0);
  58257. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  58258. }
  58259. test_AEAD_fail_decryption = 1;
  58260. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  58261. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  58262. /* 100 read calls should be enough to complete the key update */
  58263. w64Zero(&counter);
  58264. for (i = 0; i < 100; i++) {
  58265. /* Key update should be sent and negotiated */
  58266. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58267. AssertIntGT(ret, 0);
  58268. /* Epoch after one key update is 4 */
  58269. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  58270. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  58271. didReKey = 1;
  58272. break;
  58273. }
  58274. }
  58275. AssertTrue(didReKey);
  58276. if (!w64IsZero(sendLimit)) {
  58277. /* Test the sending limit for AEAD ciphers */
  58278. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  58279. test_AEAD_seq_num = 1;
  58280. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  58281. AssertIntGT(ret, 0);
  58282. didReKey = 0;
  58283. w64Zero(&counter);
  58284. /* 100 read calls should be enough to complete the key update */
  58285. for (i = 0; i < 100; i++) {
  58286. /* Key update should be sent and negotiated */
  58287. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58288. AssertIntGT(ret, 0);
  58289. /* Epoch after another key update is 5 */
  58290. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  58291. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  58292. didReKey = 1;
  58293. break;
  58294. }
  58295. }
  58296. AssertTrue(didReKey);
  58297. }
  58298. test_AEAD_fail_decryption = 2;
  58299. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  58300. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  58301. /* Connection should fail with a DECRYPT_ERROR */
  58302. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58303. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  58304. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  58305. test_AEAD_done = 1;
  58306. }
  58307. int counter = 0;
  58308. static void test_AEAD_limit_server(WOLFSSL* ssl)
  58309. {
  58310. char msgBuf[] = "Sending data";
  58311. int ret = WOLFSSL_SUCCESS;
  58312. w64wrapper sendLimit;
  58313. SOCKET_T fd = wolfSSL_get_fd(ssl);
  58314. struct timespec delay;
  58315. XMEMSET(&delay, 0, sizeof(delay));
  58316. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  58317. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  58318. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  58319. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  58320. while (!test_AEAD_done && ret > 0) {
  58321. counter++;
  58322. if (test_AEAD_seq_num) {
  58323. /* We need to update the seq number so that we can understand the
  58324. * peer. Otherwise we will incorrectly interpret the seq number. */
  58325. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  58326. AssertNotNull(e);
  58327. e->nextPeerSeqNumber = sendLimit;
  58328. test_AEAD_seq_num = 0;
  58329. }
  58330. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58331. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  58332. nanosleep(&delay, NULL);
  58333. }
  58334. }
  58335. static int test_wolfSSL_dtls_AEAD_limit(void)
  58336. {
  58337. callback_functions func_cb_client;
  58338. callback_functions func_cb_server;
  58339. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  58340. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  58341. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  58342. func_cb_server.method = wolfDTLSv1_3_server_method;
  58343. func_cb_client.method = wolfDTLSv1_3_client_method;
  58344. func_cb_server.on_result = test_AEAD_limit_server;
  58345. func_cb_client.on_result = test_AEAD_limit_client;
  58346. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58347. if (!func_cb_client.return_code)
  58348. return TEST_FAIL;
  58349. if (!func_cb_server.return_code)
  58350. return TEST_FAIL;
  58351. return TEST_SUCCESS;
  58352. }
  58353. #else
  58354. static int test_wolfSSL_dtls_AEAD_limit(void)
  58355. {
  58356. return TEST_SKIPPED;
  58357. }
  58358. #endif
  58359. #if defined(WOLFSSL_DTLS) && \
  58360. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  58361. !defined(DEBUG_VECTOR_REGISTER_ACCESS_FUZZING)
  58362. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  58363. {
  58364. int fd, ret;
  58365. byte ch_msg[] = {
  58366. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  58367. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  58368. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  58369. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  58370. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  58371. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  58372. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  58373. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  58374. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  58375. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  58376. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  58377. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  58378. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  58379. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  58380. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  58381. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  58382. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  58383. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  58384. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  58385. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  58386. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  58387. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  58388. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  58389. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  58390. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  58391. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  58392. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  58393. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  58394. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  58395. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  58396. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  58397. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  58398. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  58399. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  58400. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  58401. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  58402. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  58403. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  58404. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  58405. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  58406. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  58407. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  58408. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  58409. 0x31, 0x68, 0x4c
  58410. };
  58411. fd = wolfSSL_get_fd(ssl);
  58412. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  58413. AssertIntGT(ret, 0);
  58414. /* consume the HRR otherwise handshake will fail */
  58415. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  58416. AssertIntGT(ret, 0);
  58417. }
  58418. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  58419. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  58420. {
  58421. int fd, ret;
  58422. byte ch_msh_invalid_cookie[] = {
  58423. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  58424. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  58425. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  58426. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  58427. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  58428. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  58429. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  58430. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  58431. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  58432. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  58433. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  58434. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  58435. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  58436. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  58437. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58438. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58439. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58440. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58441. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58442. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  58443. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  58444. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  58445. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  58446. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  58447. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  58448. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  58449. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  58450. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  58451. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  58452. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  58453. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  58454. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  58455. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  58456. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  58457. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  58458. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  58459. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  58460. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  58461. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  58462. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  58463. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  58464. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  58465. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  58466. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  58467. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  58468. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  58469. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  58470. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  58471. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  58472. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  58473. 0x23, 0x00, 0x00
  58474. };
  58475. byte alert_reply[50];
  58476. byte expected_alert_reply[] = {
  58477. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  58478. 0x02, 0x02, 0x2f
  58479. };
  58480. fd = wolfSSL_get_fd(ssl);
  58481. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  58482. AssertIntGT(ret, 0);
  58483. /* should reply with an illegal_parameter reply */
  58484. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  58485. AssertIntEQ(ret, sizeof(expected_alert_reply));
  58486. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  58487. }
  58488. #endif
  58489. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  58490. {
  58491. #ifndef NO_MD5
  58492. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  58493. #elif !defined(NO_SHA)
  58494. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  58495. #elif !defined(NO_SHA256)
  58496. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  58497. #else
  58498. #error "We need a digest to hash the WOLFSSL object"
  58499. #endif
  58500. byte hashBuf[WC_MAX_DIGEST_SIZE];
  58501. wc_HashAlg hash;
  58502. const TLSX* exts = ssl->extensions;
  58503. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  58504. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  58505. * InitHandshakeHashes */
  58506. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  58507. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  58508. /* Following fields are not important to compare */
  58509. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  58510. sslCopy.buffers.inputBuffer.buffer = NULL;
  58511. sslCopy.buffers.inputBuffer.bufferSize = 0;
  58512. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  58513. sslCopy.buffers.inputBuffer.offset = 0;
  58514. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  58515. sslCopy.buffers.outputBuffer.buffer = NULL;
  58516. sslCopy.buffers.outputBuffer.bufferSize = 0;
  58517. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  58518. sslCopy.buffers.outputBuffer.offset = 0;
  58519. sslCopy.error = 0;
  58520. sslCopy.curSize = 0;
  58521. sslCopy.curStartIdx = 0;
  58522. sslCopy.keys.curSeq_lo = 0;
  58523. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  58524. #ifdef WOLFSSL_DTLS13
  58525. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  58526. sslCopy.dtls13FastTimeout = 0;
  58527. #endif
  58528. sslCopy.keys.dtls_peer_handshake_number = 0;
  58529. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  58530. sslCopy.hsHashes = NULL;
  58531. #ifdef WOLFSSL_ASYNC_IO
  58532. #ifdef WOLFSSL_ASYNC_CRYPT
  58533. sslCopy.asyncDev = NULL;
  58534. #endif
  58535. sslCopy.async = NULL;
  58536. #endif
  58537. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  58538. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  58539. /* hash extension list */
  58540. while (exts != NULL) {
  58541. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  58542. exts = exts->next;
  58543. }
  58544. /* Hash suites */
  58545. if (sslCopy.suites != NULL) {
  58546. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  58547. sizeof(struct Suites)), 0);
  58548. }
  58549. /* Hash hsHashes */
  58550. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  58551. sizeof(*hsHashes)), 0);
  58552. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  58553. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  58554. return MakeWordFromHash(hashBuf);
  58555. }
  58556. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  58557. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  58558. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  58559. char *buf, int sz, void *ctx)
  58560. {
  58561. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  58562. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  58563. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  58564. }
  58565. else {
  58566. return WOLFSSL_CBIO_ERR_WANT_READ;
  58567. }
  58568. }
  58569. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  58570. {
  58571. /* Compare the ssl object before and after one ClientHello msg */
  58572. SOCKET_T fd = wolfSSL_get_fd(ssl);
  58573. int res;
  58574. int err;
  58575. word32 initHash;
  58576. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  58577. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  58578. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  58579. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  58580. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  58581. (void)initHash;
  58582. res = tcp_select(fd, 5);
  58583. /* We are expecting a msg. A timeout indicates failure. */
  58584. AssertIntEQ(res, TEST_RECV_READY);
  58585. res = wolfSSL_accept(ssl);
  58586. err = wolfSSL_get_error(ssl, res);
  58587. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  58588. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  58589. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  58590. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  58591. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  58592. }
  58593. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  58594. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  58595. {
  58596. int ret;
  58597. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  58598. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  58599. test_wolfSSL_dtls_compare_stateless(ssl);
  58600. }
  58601. #endif
  58602. static int test_wolfSSL_dtls_stateless(void)
  58603. {
  58604. callback_functions client_cbs, server_cbs;
  58605. size_t i;
  58606. struct {
  58607. method_provider client_meth;
  58608. method_provider server_meth;
  58609. ssl_callback client_ssl_ready;
  58610. ssl_callback server_ssl_ready;
  58611. } test_params[] = {
  58612. #if !defined(WOLFSSL_NO_TLS12)
  58613. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  58614. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  58615. #endif
  58616. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  58617. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  58618. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  58619. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  58620. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  58621. #endif
  58622. };
  58623. if (0 == sizeof(test_params)){
  58624. return TEST_SKIPPED;
  58625. }
  58626. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  58627. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58628. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58629. client_cbs.doUdp = server_cbs.doUdp = 1;
  58630. client_cbs.method = test_params[i].client_meth;
  58631. server_cbs.method = test_params[i].server_meth;
  58632. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  58633. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  58634. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58635. if (!client_cbs.return_code)
  58636. return TEST_FAIL;
  58637. if (!server_cbs.return_code)
  58638. return TEST_FAIL;
  58639. }
  58640. return TEST_SUCCESS;
  58641. }
  58642. #else
  58643. static int test_wolfSSL_dtls_stateless(void)
  58644. {
  58645. return TEST_SKIPPED;
  58646. }
  58647. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  58648. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  58649. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  58650. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  58651. !defined(WOLFSSL_NO_CLIENT_AUTH))
  58652. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  58653. {
  58654. int ret;
  58655. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  58656. fprintf(stderr, "loading cert %s failed\n", certA);
  58657. fprintf(stderr, "Error: (%d): %s\n", ret,
  58658. wolfSSL_ERR_reason_error_string(ret));
  58659. return -1;
  58660. }
  58661. return 0;
  58662. }
  58663. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  58664. {
  58665. int ret;
  58666. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  58667. != WOLFSSL_SUCCESS) {
  58668. fprintf(stderr, "could not verify the cert: %s\n", certA);
  58669. fprintf(stderr, "Error: (%d): %s\n", ret,
  58670. wolfSSL_ERR_reason_error_string(ret));
  58671. return -1;
  58672. }
  58673. else {
  58674. fprintf(stderr, "successfully verified: %s\n", certA);
  58675. }
  58676. return 0;
  58677. }
  58678. #define LOAD_ONE_CA(a, b, c, d) \
  58679. do { \
  58680. (a) = load_ca_into_cm(c, d); \
  58681. if ((a) != 0) \
  58682. return (b); \
  58683. else \
  58684. (b)--; \
  58685. } while(0)
  58686. #define VERIFY_ONE_CERT(a, b, c, d) \
  58687. do { \
  58688. (a) = verify_cert_with_cm(c, d);\
  58689. if ((a) != 0) \
  58690. return (b); \
  58691. else \
  58692. (b)--; \
  58693. } while(0)
  58694. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  58695. {
  58696. int ret;
  58697. int i = -1;
  58698. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  58699. char chainGArr[9][50] = {"certs/ca-cert.pem",
  58700. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  58701. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  58702. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  58703. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  58704. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  58705. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  58706. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  58707. "certs/test-pathlen/chainG-entity.pem"};
  58708. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  58709. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  58710. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  58711. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  58712. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  58713. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  58714. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  58715. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  58716. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  58717. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  58718. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  58719. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  58720. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  58721. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  58722. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  58723. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  58724. /* test validating the entity twice, should have no effect on pathLen since
  58725. * entity/leaf cert */
  58726. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  58727. return ret;
  58728. }
  58729. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  58730. {
  58731. int ret;
  58732. int i = -1;
  58733. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  58734. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  58735. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  58736. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  58737. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  58738. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  58739. */
  58740. char chainHArr[6][50] = {"certs/ca-cert.pem",
  58741. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  58742. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  58743. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  58744. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  58745. "certs/test-pathlen/chainH-entity.pem"};
  58746. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  58747. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  58748. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  58749. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  58750. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  58751. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  58752. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  58753. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  58754. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  58755. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  58756. return ret;
  58757. }
  58758. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  58759. {
  58760. int ret;
  58761. int i = -1;
  58762. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  58763. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  58764. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  58765. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  58766. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  58767. */
  58768. char chainIArr[5][50] = {"certs/ca-cert.pem",
  58769. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  58770. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  58771. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  58772. "certs/test-pathlen/chainI-entity.pem"};
  58773. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  58774. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  58775. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  58776. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  58777. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  58778. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  58779. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  58780. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  58781. return ret;
  58782. }
  58783. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  58784. {
  58785. int ret;
  58786. int i = -1;
  58787. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  58788. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  58789. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  58790. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  58791. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  58792. */
  58793. char chainJArr[6][50] = {"certs/ca-cert.pem",
  58794. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  58795. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  58796. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  58797. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  58798. "certs/test-pathlen/chainJ-entity.pem"};
  58799. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  58800. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  58801. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  58802. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  58803. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  58804. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  58805. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  58806. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  58807. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  58808. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  58809. return ret;
  58810. }
  58811. static int test_various_pathlen_chains(void)
  58812. {
  58813. EXPECT_DECLS;
  58814. WOLFSSL_CERT_MANAGER* cm = NULL;
  58815. /* Test chain G (large chain with varying pathLens) */
  58816. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58817. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  58818. ExpectIntEQ(test_chainG(cm), -1);
  58819. #else
  58820. ExpectIntEQ(test_chainG(cm), 0);
  58821. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  58822. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58823. wolfSSL_CertManagerFree(cm);
  58824. /* end test chain G */
  58825. /* Test chain H (5 chain with same pathLens) */
  58826. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58827. ExpectIntLT(test_chainH(cm), 0);
  58828. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58829. wolfSSL_CertManagerFree(cm);
  58830. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58831. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58832. wolfSSL_CertManagerFree(cm);
  58833. /* end test chain H */
  58834. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  58835. * followed by 2 ICA's, should pass) */
  58836. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58837. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  58838. ExpectIntEQ(test_chainI(cm), -1);
  58839. #else
  58840. ExpectIntEQ(test_chainI(cm), 0);
  58841. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  58842. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58843. wolfSSL_CertManagerFree(cm);
  58844. cm = NULL;
  58845. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58846. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58847. wolfSSL_CertManagerFree(cm);
  58848. cm = NULL;
  58849. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  58850. * this time followed by 3 ICA's, should fail */
  58851. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58852. ExpectIntLT(test_chainJ(cm), 0);
  58853. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58854. wolfSSL_CertManagerFree(cm);
  58855. cm = NULL;
  58856. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58857. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58858. wolfSSL_CertManagerFree(cm);
  58859. return EXPECT_RESULT();
  58860. }
  58861. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  58862. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58863. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  58864. {
  58865. EXPECT_DECLS;
  58866. byte ekm[100] = {0};
  58867. (void)ctx;
  58868. /* Success Cases */
  58869. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58870. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  58871. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58872. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  58873. /* Use some random context */
  58874. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58875. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  58876. /* Failure cases */
  58877. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58878. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  58879. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58880. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  58881. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58882. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  58883. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58884. "extended master secret", XSTR_SIZEOF("extended master secret"),
  58885. NULL, 0, 0), 0);
  58886. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58887. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  58888. return EXPECT_RESULT();
  58889. }
  58890. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  58891. {
  58892. wolfSSL_KeepArrays(ssl);
  58893. return TEST_SUCCESS;
  58894. }
  58895. static int test_export_keying_material(void)
  58896. {
  58897. EXPECT_DECLS;
  58898. test_ssl_cbf serverCb;
  58899. test_ssl_cbf clientCb;
  58900. XMEMSET(&serverCb, 0, sizeof(serverCb));
  58901. XMEMSET(&clientCb, 0, sizeof(clientCb));
  58902. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  58903. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  58904. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  58905. return EXPECT_RESULT();
  58906. }
  58907. #endif /* HAVE_KEYING_MATERIAL */
  58908. static int test_wolfSSL_THREADID_hash(void)
  58909. {
  58910. EXPECT_DECLS;
  58911. #if defined(OPENSSL_EXTRA)
  58912. CRYPTO_THREADID id;
  58913. CRYPTO_THREADID_current(NULL);
  58914. /* Hash result is unsigned long. */
  58915. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  58916. XMEMSET(&id, 0, sizeof(id));
  58917. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  58918. #endif /* OPENSSL_EXTRA */
  58919. return EXPECT_RESULT();
  58920. }
  58921. static int test_wolfSSL_set_ecdh_auto(void)
  58922. {
  58923. EXPECT_DECLS;
  58924. #if defined(OPENSSL_EXTRA)
  58925. WOLFSSL* ssl = NULL;
  58926. ExpectIntEQ(SSL_set_ecdh_auto(NULL,0), 1);
  58927. ExpectIntEQ(SSL_set_ecdh_auto(NULL,1), 1);
  58928. ExpectIntEQ(SSL_set_ecdh_auto(ssl,0), 1);
  58929. ExpectIntEQ(SSL_set_ecdh_auto(ssl,1), 1);
  58930. #endif /* OPENSSL_EXTRA */
  58931. return EXPECT_RESULT();
  58932. }
  58933. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  58934. {
  58935. EXPECT_DECLS;
  58936. #if defined(OPENSSL_EXTRA)
  58937. WOLFSSL_CTX* ctx = NULL;
  58938. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  58939. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  58940. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  58941. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  58942. #endif /* OPENSSL_EXTRA */
  58943. return EXPECT_RESULT();
  58944. }
  58945. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  58946. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  58947. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  58948. {
  58949. EXPECT_DECLS;
  58950. callback_functions* callbacks = NULL;
  58951. WOLFSSL_CTX* ctx = NULL;
  58952. WOLFSSL* ssl = NULL;
  58953. SOCKET_T sfd = 0;
  58954. SOCKET_T cfd = 0;
  58955. word16 port;
  58956. char msg[] = "I hear you fa shizzle!";
  58957. int len = (int) XSTRLEN(msg);
  58958. char input[1024];
  58959. int ret = 0;
  58960. int err = 0;
  58961. if (!args)
  58962. WOLFSSL_RETURN_FROM_THREAD(0);
  58963. ((func_args*)args)->return_code = TEST_FAIL;
  58964. callbacks = ((func_args*)args)->callbacks;
  58965. ctx = wolfSSL_CTX_new(callbacks->method());
  58966. #if defined(USE_WINDOWS_API)
  58967. port = ((func_args*)args)->signal->port;
  58968. #else
  58969. /* Let tcp_listen assign port */
  58970. port = 0;
  58971. #endif
  58972. #ifdef WOLFSSL_TIRTOS
  58973. fdOpenSession(Task_self());
  58974. #endif
  58975. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  58976. caCertFile, 0));
  58977. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  58978. svrCertFile, WOLFSSL_FILETYPE_PEM));
  58979. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  58980. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  58981. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  58982. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  58983. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58984. #elif !defined(NO_DH)
  58985. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  58986. #endif
  58987. if (callbacks->ctx_ready)
  58988. callbacks->ctx_ready(ctx);
  58989. ssl = wolfSSL_new(ctx);
  58990. ExpectNotNull(ssl);
  58991. /* listen and accept */
  58992. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  58993. CloseSocket(sfd);
  58994. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  58995. if (callbacks->ssl_ready)
  58996. callbacks->ssl_ready(ssl);
  58997. if (EXPECT_SUCCESS()) {
  58998. do {
  58999. err = 0; /* Reset error */
  59000. ret = wolfSSL_accept(ssl);
  59001. if (ret != WOLFSSL_SUCCESS) {
  59002. err = wolfSSL_get_error(ssl, 0);
  59003. }
  59004. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  59005. }
  59006. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  59007. /* read and write data */
  59008. XMEMSET(input, 0, sizeof(input));
  59009. while (EXPECT_SUCCESS()) {
  59010. ret = wolfSSL_read(ssl, input, sizeof(input));
  59011. if (ret > 0) {
  59012. break;
  59013. }
  59014. else {
  59015. err = wolfSSL_get_error(ssl,ret);
  59016. if (err == WOLFSSL_ERROR_WANT_READ) {
  59017. continue;
  59018. }
  59019. break;
  59020. }
  59021. }
  59022. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  59023. do {
  59024. ret = wolfSSL_write(ssl, msg, len);
  59025. if (ret > 0) {
  59026. break;
  59027. }
  59028. } while (ret < 0);
  59029. }
  59030. /* bidirectional shutdown */
  59031. while (EXPECT_SUCCESS()) {
  59032. ret = wolfSSL_shutdown(ssl);
  59033. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  59034. if (ret == WOLFSSL_SUCCESS) {
  59035. break;
  59036. }
  59037. }
  59038. if (EXPECT_SUCCESS()) {
  59039. /* wait for the peer to disconnect the tcp connection */
  59040. do {
  59041. ret = wolfSSL_read(ssl, input, sizeof(input));
  59042. err = wolfSSL_get_error(ssl, ret);
  59043. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  59044. }
  59045. /* detect TCP disconnect */
  59046. ExpectIntLE(ret,WOLFSSL_FAILURE);
  59047. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  59048. ((func_args*)args)->return_code = EXPECT_RESULT();
  59049. wolfSSL_free(ssl);
  59050. wolfSSL_CTX_free(ctx);
  59051. CloseSocket(cfd);
  59052. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  59053. wc_ecc_fp_free(); /* free per thread cache */
  59054. #endif
  59055. WOLFSSL_RETURN_FROM_THREAD(0);
  59056. }
  59057. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  59058. {
  59059. EXPECT_DECLS;
  59060. callback_functions* callbacks = NULL;
  59061. WOLFSSL_CTX* ctx = NULL;
  59062. WOLFSSL* ssl = NULL;
  59063. SOCKET_T sfd = 0;
  59064. char msg[] = "hello wolfssl server!";
  59065. int len = (int) XSTRLEN(msg);
  59066. char input[1024];
  59067. int idx;
  59068. int ret, err;
  59069. if (!args)
  59070. WOLFSSL_RETURN_FROM_THREAD(0);
  59071. ((func_args*)args)->return_code = TEST_FAIL;
  59072. callbacks = ((func_args*)args)->callbacks;
  59073. ctx = wolfSSL_CTX_new(callbacks->method());
  59074. #ifdef WOLFSSL_TIRTOS
  59075. fdOpenSession(Task_self());
  59076. #endif
  59077. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  59078. caCertFile, 0));
  59079. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  59080. cliCertFile, WOLFSSL_FILETYPE_PEM));
  59081. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  59082. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  59083. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  59084. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  59085. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  59086. if (EXPECT_SUCCESS()) {
  59087. do {
  59088. err = 0; /* Reset error */
  59089. ret = wolfSSL_connect(ssl);
  59090. if (ret != WOLFSSL_SUCCESS) {
  59091. err = wolfSSL_get_error(ssl, 0);
  59092. }
  59093. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  59094. }
  59095. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  59096. if (EXPECT_SUCCESS()) {
  59097. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  59098. input[idx] = 0;
  59099. }
  59100. }
  59101. if (EXPECT_SUCCESS()) {
  59102. ret = wolfSSL_shutdown(ssl);
  59103. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  59104. ret = wolfSSL_shutdown(ssl);
  59105. }
  59106. }
  59107. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  59108. ((func_args*)args)->return_code = EXPECT_RESULT();
  59109. wolfSSL_free(ssl);
  59110. wolfSSL_CTX_free(ctx);
  59111. CloseSocket(sfd);
  59112. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  59113. wc_ecc_fp_free(); /* free per thread cache */
  59114. #endif
  59115. WOLFSSL_RETURN_FROM_THREAD(0);
  59116. }
  59117. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  59118. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  59119. /* This test is to check wolfSSL_read behaves as same as
  59120. * openSSL when it is called after SSL_shutdown completes.
  59121. */
  59122. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  59123. {
  59124. EXPECT_DECLS;
  59125. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  59126. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  59127. tcp_ready ready;
  59128. func_args client_args;
  59129. func_args server_args;
  59130. THREAD_TYPE serverThread;
  59131. THREAD_TYPE clientThread;
  59132. callback_functions server_cbf;
  59133. callback_functions client_cbf;
  59134. #ifdef WOLFSSL_TIRTOS
  59135. fdOpenSession(Task_self());
  59136. #endif
  59137. StartTCP();
  59138. InitTcpReady(&ready);
  59139. #if defined(USE_WINDOWS_API)
  59140. /* use RNG to get random port if using windows */
  59141. ready.port = GetRandomPort();
  59142. #endif
  59143. XMEMSET(&client_args, 0, sizeof(func_args));
  59144. XMEMSET(&server_args, 0, sizeof(func_args));
  59145. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  59146. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  59147. server_cbf.method = wolfTLSv1_2_server_method;
  59148. client_cbf.method = wolfTLSv1_2_client_method;
  59149. server_args.callbacks = &server_cbf;
  59150. client_args.callbacks = &client_cbf;
  59151. server_args.signal = &ready;
  59152. client_args.signal = &ready;
  59153. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  59154. wait_tcp_ready(&server_args);
  59155. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  59156. join_thread(clientThread);
  59157. join_thread(serverThread);
  59158. ExpectTrue(client_args.return_code);
  59159. ExpectTrue(server_args.return_code);
  59160. FreeTcpReady(&ready);
  59161. #endif
  59162. return EXPECT_RESULT();
  59163. }
  59164. static int test_wolfSSL_CTX_get_min_proto_version(void)
  59165. {
  59166. EXPECT_DECLS;
  59167. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  59168. WOLFSSL_CTX *ctx = NULL;
  59169. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  59170. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  59171. WOLFSSL_SUCCESS);
  59172. #ifdef WOLFSSL_ALLOW_SSLV3
  59173. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  59174. #else
  59175. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  59176. #endif
  59177. wolfSSL_CTX_free(ctx);
  59178. ctx = NULL;
  59179. #ifndef NO_OLD_TLS
  59180. #ifdef WOLFSSL_ALLOW_TLSV10
  59181. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  59182. #else
  59183. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  59184. #endif
  59185. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  59186. WOLFSSL_SUCCESS);
  59187. #ifdef WOLFSSL_ALLOW_TLSV10
  59188. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  59189. #else
  59190. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  59191. #endif
  59192. wolfSSL_CTX_free(ctx);
  59193. ctx = NULL;
  59194. #endif
  59195. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  59196. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  59197. WOLFSSL_SUCCESS);
  59198. #ifndef NO_OLD_TLS
  59199. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  59200. #else
  59201. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  59202. #endif
  59203. wolfSSL_CTX_free(ctx);
  59204. ctx = NULL;
  59205. #ifndef WOLFSSL_NO_TLS12
  59206. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  59207. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  59208. WOLFSSL_SUCCESS);
  59209. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  59210. wolfSSL_CTX_free(ctx);
  59211. ctx = NULL;
  59212. #endif
  59213. #ifdef WOLFSSL_TLS13
  59214. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  59215. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  59216. WOLFSSL_SUCCESS);
  59217. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  59218. wolfSSL_CTX_free(ctx);
  59219. ctx = NULL;
  59220. #endif
  59221. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  59222. return EXPECT_RESULT();
  59223. }
  59224. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  59225. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  59226. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  59227. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  59228. static int test_wolfSSL_set_SSL_CTX(void)
  59229. {
  59230. EXPECT_DECLS;
  59231. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  59232. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  59233. !defined(NO_RSA)
  59234. WOLFSSL_CTX *ctx1 = NULL;
  59235. WOLFSSL_CTX *ctx2 = NULL;
  59236. WOLFSSL *ssl = NULL;
  59237. const byte *session_id1 = (const byte *)"CTX1";
  59238. const byte *session_id2 = (const byte *)"CTX2";
  59239. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  59240. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  59241. WOLFSSL_FILETYPE_PEM));
  59242. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  59243. WOLFSSL_FILETYPE_PEM));
  59244. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  59245. WOLFSSL_SUCCESS);
  59246. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  59247. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  59248. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  59249. WOLFSSL_SUCCESS);
  59250. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  59251. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  59252. WOLFSSL_FILETYPE_PEM));
  59253. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  59254. WOLFSSL_FILETYPE_PEM));
  59255. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  59256. WOLFSSL_SUCCESS);
  59257. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  59258. WOLFSSL_SUCCESS);
  59259. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  59260. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  59261. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  59262. WOLFSSL_SUCCESS);
  59263. #ifdef HAVE_SESSION_TICKET
  59264. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  59265. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  59266. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  59267. #endif
  59268. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  59269. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  59270. #ifdef WOLFSSL_INT_H
  59271. #ifdef WOLFSSL_SESSION_ID_CTX
  59272. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  59273. #endif
  59274. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  59275. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  59276. #endif
  59277. #ifdef HAVE_SESSION_TICKET
  59278. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  59279. #endif
  59280. /* Set the ctx1 that has TLSv1.3 as max proto version */
  59281. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  59282. /* MUST not change proto versions of ssl */
  59283. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  59284. #ifdef HAVE_SESSION_TICKET
  59285. /* MUST not change */
  59286. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  59287. #endif
  59288. /* MUST change */
  59289. #ifdef WOLFSSL_INT_H
  59290. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  59291. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  59292. #ifdef WOLFSSL_SESSION_ID_CTX
  59293. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  59294. #endif
  59295. #endif
  59296. wolfSSL_free(ssl);
  59297. wolfSSL_CTX_free(ctx1);
  59298. wolfSSL_CTX_free(ctx2);
  59299. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  59300. return EXPECT_RESULT();
  59301. }
  59302. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  59303. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  59304. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  59305. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  59306. static int test_wolfSSL_security_level(void)
  59307. {
  59308. EXPECT_DECLS;
  59309. #if defined(OPENSSL_EXTRA)
  59310. SSL_CTX *ctx = NULL;
  59311. #ifdef WOLFSSL_TLS13
  59312. #ifdef NO_WOLFSSL_SERVER
  59313. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  59314. #else
  59315. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  59316. #endif
  59317. SSL_CTX_set_security_level(NULL, 1);
  59318. SSL_CTX_set_security_level(ctx, 1);
  59319. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  59320. /* Stub so nothing happens. */
  59321. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  59322. SSL_CTX_free(ctx);
  59323. #else
  59324. (void)ctx;
  59325. #endif
  59326. #endif
  59327. return EXPECT_RESULT();
  59328. }
  59329. static int test_wolfSSL_SSL_in_init(void)
  59330. {
  59331. EXPECT_DECLS;
  59332. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  59333. SSL_CTX* ctx = NULL;
  59334. SSL* ssl = NULL;
  59335. const char* testCertFile;
  59336. const char* testKeyFile;
  59337. #ifdef WOLFSSL_TLS13
  59338. #ifdef NO_WOLFSSL_SERVER
  59339. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  59340. #else
  59341. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  59342. #endif
  59343. #else
  59344. #ifdef NO_WOLFSSL_SERVER
  59345. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  59346. #else
  59347. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59348. #endif
  59349. #endif
  59350. #ifndef NO_RSA
  59351. testCertFile = svrCertFile;
  59352. testKeyFile = svrKeyFile;
  59353. #elif defined(HAVE_ECC)
  59354. testCertFile = eccCertFile;
  59355. testKeyFile = eccKeyFile;
  59356. #else
  59357. testCertFile = NULL;
  59358. testKeyFile = NULL;
  59359. #endif
  59360. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  59361. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  59362. SSL_FILETYPE_PEM));
  59363. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  59364. SSL_FILETYPE_PEM));
  59365. }
  59366. ExpectNotNull(ssl = SSL_new(ctx));
  59367. ExpectIntEQ(SSL_in_init(ssl), 1);
  59368. SSL_CTX_free(ctx);
  59369. SSL_free(ssl);
  59370. #endif
  59371. return EXPECT_RESULT();
  59372. }
  59373. static int test_wolfSSL_CTX_set_timeout(void)
  59374. {
  59375. EXPECT_DECLS;
  59376. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  59377. int timeout;
  59378. WOLFSSL_CTX* ctx = NULL;
  59379. (void)timeout;
  59380. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59381. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  59382. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  59383. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  59384. */
  59385. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  59386. /* giving 0 as timeout value sets default timeout */
  59387. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  59388. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  59389. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  59390. #else
  59391. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  59392. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  59393. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  59394. #endif
  59395. wolfSSL_CTX_free(ctx);
  59396. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  59397. return EXPECT_RESULT();
  59398. }
  59399. static int test_wolfSSL_OpenSSL_version(void)
  59400. {
  59401. EXPECT_DECLS;
  59402. #if defined(OPENSSL_EXTRA)
  59403. const char* ver;
  59404. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  59405. ExpectNotNull(ver = OpenSSL_version(0));
  59406. #else
  59407. ExpectNotNull(ver = OpenSSL_version());
  59408. #endif
  59409. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  59410. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  59411. #endif
  59412. return EXPECT_RESULT();
  59413. }
  59414. static int test_CONF_CTX_CMDLINE(void)
  59415. {
  59416. EXPECT_DECLS;
  59417. #if defined(OPENSSL_ALL)
  59418. SSL_CTX* ctx = NULL;
  59419. SSL_CONF_CTX* cctx = NULL;
  59420. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  59421. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59422. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  59423. /* set flags */
  59424. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  59425. WOLFSSL_CONF_FLAG_CMDLINE);
  59426. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  59427. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  59428. /* cmd invalid command */
  59429. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  59430. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  59431. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  59432. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  59433. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  59434. /* cmd Certificate and Private Key*/
  59435. {
  59436. #if !defined(NO_CERTS) && !defined(NO_RSA)
  59437. const char* ourCert = svrCertFile;
  59438. const char* ourKey = svrKeyFile;
  59439. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  59440. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  59441. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  59442. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  59443. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59444. #endif
  59445. }
  59446. /* cmd curves */
  59447. {
  59448. #if defined(HAVE_ECC)
  59449. const char* curve = "secp256r1";
  59450. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  59451. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  59452. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59453. #endif
  59454. }
  59455. /* cmd CipherString */
  59456. {
  59457. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  59458. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  59459. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  59460. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59461. }
  59462. /* cmd DH parameter */
  59463. {
  59464. #if !defined(NO_DH) && !defined(NO_BIO)
  59465. const char* ourdhcert = "./certs/dh2048.pem";
  59466. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  59467. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  59468. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59469. #endif
  59470. }
  59471. SSL_CTX_free(ctx);
  59472. SSL_CONF_CTX_free(cctx);
  59473. #endif /* OPENSSL_EXTRA */
  59474. return EXPECT_RESULT();
  59475. }
  59476. static int test_CONF_CTX_FILE(void)
  59477. {
  59478. EXPECT_DECLS;
  59479. #if defined(OPENSSL_ALL)
  59480. SSL_CTX* ctx = NULL;
  59481. SSL_CONF_CTX* cctx = NULL;
  59482. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  59483. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59484. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  59485. /* set flags */
  59486. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  59487. WOLFSSL_CONF_FLAG_FILE);
  59488. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  59489. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  59490. /* sanity check */
  59491. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  59492. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  59493. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  59494. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  59495. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  59496. /* cmd Certificate and Private Key*/
  59497. {
  59498. #if !defined(NO_CERTS) && !defined(NO_RSA)
  59499. const char* ourCert = svrCertFile;
  59500. const char* ourKey = svrKeyFile;
  59501. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  59502. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  59503. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  59504. WOLFSSL_SUCCESS);
  59505. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  59506. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59507. #endif
  59508. }
  59509. /* cmd curves */
  59510. {
  59511. #if defined(HAVE_ECC)
  59512. const char* curve = "secp256r1";
  59513. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  59514. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  59515. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59516. #endif
  59517. }
  59518. /* cmd CipherString */
  59519. {
  59520. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  59521. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  59522. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  59523. WOLFSSL_SUCCESS);
  59524. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59525. }
  59526. /* cmd DH parameter */
  59527. {
  59528. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  59529. const char* ourdhcert = "./certs/dh3072.pem";
  59530. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  59531. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  59532. WOLFSSL_SUCCESS);
  59533. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59534. #endif
  59535. }
  59536. SSL_CTX_free(ctx);
  59537. SSL_CONF_CTX_free(cctx);
  59538. #endif /* OPENSSL_EXTRA */
  59539. return EXPECT_RESULT();
  59540. }
  59541. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  59542. {
  59543. EXPECT_DECLS;
  59544. #ifdef HAVE_EX_DATA
  59545. int idx1, idx2;
  59546. /* test for unsupported class index */
  59547. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  59548. 0,NULL, NULL, NULL, NULL ), -1);
  59549. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  59550. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  59551. 0,NULL, NULL, NULL, NULL ), -1);
  59552. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  59553. 0,NULL, NULL, NULL, NULL ), -1);
  59554. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  59555. 0,NULL, NULL, NULL, NULL ), -1);
  59556. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  59557. 0,NULL, NULL, NULL, NULL ), -1);
  59558. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  59559. 0,NULL, NULL, NULL, NULL ), -1);
  59560. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  59561. 0,NULL, NULL, NULL, NULL ), -1);
  59562. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  59563. 0,NULL, NULL, NULL, NULL ), -1);
  59564. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  59565. 0,NULL, NULL, NULL, NULL ), -1);
  59566. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  59567. 0,NULL, NULL, NULL, NULL ), -1);
  59568. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  59569. 0,NULL, NULL, NULL, NULL ), -1);
  59570. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  59571. 0,NULL, NULL, NULL, NULL ), -1);
  59572. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  59573. 0,NULL, NULL, NULL, NULL ), -1);
  59574. /* test for supported class index */
  59575. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  59576. 0,NULL, NULL, NULL, NULL );
  59577. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  59578. 0,NULL, NULL, NULL, NULL );
  59579. ExpectIntNE(idx1, -1);
  59580. ExpectIntNE(idx2, -1);
  59581. ExpectIntNE(idx1, idx2);
  59582. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  59583. 0,NULL, NULL, NULL, NULL );
  59584. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  59585. 0,NULL, NULL, NULL, NULL );
  59586. ExpectIntNE(idx1, -1);
  59587. ExpectIntNE(idx2, -1);
  59588. ExpectIntNE(idx1, idx2);
  59589. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  59590. 0,NULL, NULL, NULL, NULL );
  59591. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  59592. 0,NULL, NULL, NULL, NULL );
  59593. ExpectIntNE(idx1, -1);
  59594. ExpectIntNE(idx2, -1);
  59595. ExpectIntNE(idx1, idx2);
  59596. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  59597. 0,NULL, NULL, NULL, NULL );
  59598. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  59599. 0,NULL, NULL, NULL, NULL );
  59600. ExpectIntNE(idx1, -1);
  59601. ExpectIntNE(idx2, -1);
  59602. ExpectIntNE(idx1, idx2);
  59603. #endif /* HAVE_EX_DATA */
  59604. return EXPECT_RESULT();
  59605. }
  59606. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  59607. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  59608. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  59609. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  59610. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  59611. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  59612. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  59613. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  59614. #define SESSION_NEW_IDX_PTR "Testing"
  59615. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  59616. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  59617. {
  59618. AssertNotNull(p);
  59619. AssertNull(ptr);
  59620. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  59621. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  59622. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  59623. }
  59624. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  59625. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  59626. void* arg)
  59627. {
  59628. EXPECT_DECLS;
  59629. ExpectNotNull(out);
  59630. ExpectNotNull(in);
  59631. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  59632. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  59633. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  59634. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  59635. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  59636. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  59637. if (EXPECT_SUCCESS()) {
  59638. return SSL_SUCCESS;
  59639. }
  59640. else {
  59641. return SSL_FAILURE;
  59642. }
  59643. }
  59644. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  59645. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  59646. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  59647. {
  59648. EXPECT_DECLS;
  59649. ExpectNotNull(p);
  59650. ExpectNull(ptr);
  59651. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  59652. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  59653. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  59654. if (EXPECT_SUCCESS()) {
  59655. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  59656. }
  59657. }
  59658. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  59659. {
  59660. EXPECT_DECLS;
  59661. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  59662. (void*)SESSION_NEW_IDX_PTR,
  59663. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  59664. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  59665. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  59666. SSL_SESSION* s = SSL_SESSION_new();
  59667. SSL_SESSION* d = NULL;
  59668. ExpectNotNull(s);
  59669. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  59670. ExpectNotNull(d = SSL_SESSION_dup(s));
  59671. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  59672. SSL_SESSION_free(s);
  59673. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  59674. SSL_SESSION_free(d);
  59675. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  59676. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  59677. crypto_ex_cb_ctx_session = NULL;
  59678. return EXPECT_RESULT();
  59679. }
  59680. #else
  59681. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  59682. {
  59683. return TEST_SKIPPED;
  59684. }
  59685. #endif
  59686. static int test_wolfSSL_set_psk_use_session_callback(void)
  59687. {
  59688. EXPECT_DECLS;
  59689. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  59690. SSL_CTX* ctx = NULL;
  59691. SSL* ssl = NULL;
  59692. const char* testCertFile;
  59693. const char* testKeyFile;
  59694. #ifdef WOLFSSL_TLS13
  59695. #ifdef NO_WOLFSSL_SERVER
  59696. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  59697. #else
  59698. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  59699. #endif
  59700. #else
  59701. #ifdef NO_WOLFSSL_SERVER
  59702. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  59703. #else
  59704. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59705. #endif
  59706. #endif
  59707. #ifndef NO_RSA
  59708. testCertFile = svrCertFile;
  59709. testKeyFile = svrKeyFile;
  59710. #elif defined(HAVE_ECC)
  59711. testCertFile = eccCertFile;
  59712. testKeyFile = eccKeyFile;
  59713. #else
  59714. testCertFile = NULL;
  59715. testKeyFile = NULL;
  59716. #endif
  59717. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  59718. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  59719. SSL_FILETYPE_PEM));
  59720. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  59721. SSL_FILETYPE_PEM));
  59722. }
  59723. ExpectNotNull(ssl = SSL_new(ctx));
  59724. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  59725. SSL_CTX_free(ctx);
  59726. SSL_free(ssl);
  59727. #endif
  59728. return EXPECT_RESULT();
  59729. }
  59730. static int test_wolfSSL_ERR_strings(void)
  59731. {
  59732. EXPECT_DECLS;
  59733. #if !defined(NO_ERROR_STRINGS)
  59734. const char* err1 = "unsupported cipher suite";
  59735. const char* err2 = "wolfSSL PEM routines";
  59736. const char* err = NULL;
  59737. (void)err;
  59738. (void)err1;
  59739. (void)err2;
  59740. #if defined(OPENSSL_EXTRA)
  59741. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  59742. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  59743. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  59744. ExpectIntEQ((*err == '\0'), 1);
  59745. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  59746. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  59747. #else
  59748. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  59749. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  59750. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  59751. ExpectIntEQ((*err == '\0'), 1);
  59752. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  59753. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  59754. ExpectIntEQ((*err == '\0'), 1);
  59755. #endif
  59756. #endif
  59757. return EXPECT_RESULT();
  59758. }
  59759. static int test_wolfSSL_EVP_shake128(void)
  59760. {
  59761. EXPECT_DECLS;
  59762. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  59763. defined(WOLFSSL_SHAKE128)
  59764. const EVP_MD* md = NULL;
  59765. ExpectNotNull(md = EVP_shake128());
  59766. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  59767. #endif
  59768. return EXPECT_RESULT();
  59769. }
  59770. static int test_wolfSSL_EVP_shake256(void)
  59771. {
  59772. EXPECT_DECLS;
  59773. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  59774. defined(WOLFSSL_SHAKE256)
  59775. const EVP_MD* md = NULL;
  59776. ExpectNotNull(md = EVP_shake256());
  59777. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  59778. #endif
  59779. return EXPECT_RESULT();
  59780. }
  59781. /*
  59782. * Testing EVP digest API with SM3
  59783. */
  59784. static int test_wolfSSL_EVP_sm3(void)
  59785. {
  59786. int res = TEST_SKIPPED;
  59787. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  59788. EXPECT_DECLS;
  59789. const EVP_MD* md = NULL;
  59790. EVP_MD_CTX* mdCtx = NULL;
  59791. byte data[WC_SM3_BLOCK_SIZE * 4];
  59792. byte hash[WC_SM3_DIGEST_SIZE];
  59793. byte calcHash[WC_SM3_DIGEST_SIZE];
  59794. byte expHash[WC_SM3_DIGEST_SIZE] = {
  59795. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  59796. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  59797. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  59798. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  59799. };
  59800. word32 chunk;
  59801. word32 i;
  59802. unsigned int sz;
  59803. int ret;
  59804. XMEMSET(data, 0, sizeof(data));
  59805. md = EVP_sm3();
  59806. ExpectTrue(md != NULL);
  59807. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  59808. mdCtx = EVP_MD_CTX_new();
  59809. ExpectTrue(mdCtx != NULL);
  59810. /* Invalid Parameters */
  59811. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  59812. /* Valid Parameters */
  59813. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  59814. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  59815. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  59816. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  59817. /* Valid Parameters */
  59818. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  59819. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  59820. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  59821. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  59822. WOLFSSL_SUCCESS);
  59823. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  59824. WOLFSSL_SUCCESS);
  59825. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  59826. WOLFSSL_SUCCESS);
  59827. /* Ensure too many bytes for lengths. */
  59828. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  59829. WOLFSSL_SUCCESS);
  59830. /* Invalid Parameters */
  59831. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  59832. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  59833. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  59834. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  59835. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  59836. /* Valid Parameters */
  59837. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  59838. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  59839. /* Chunk tests. */
  59840. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  59841. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  59842. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  59843. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  59844. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  59845. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  59846. WOLFSSL_SUCCESS);
  59847. }
  59848. if (i < (word32)sizeof(data)) {
  59849. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  59850. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  59851. }
  59852. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  59853. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  59854. }
  59855. /* Not testing when the low 32-bit length overflows. */
  59856. ret = EVP_MD_CTX_cleanup(mdCtx);
  59857. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  59858. wolfSSL_EVP_MD_CTX_free(mdCtx);
  59859. res = EXPECT_RESULT();
  59860. #endif
  59861. return res;
  59862. } /* END test_EVP_sm3 */
  59863. static int test_EVP_blake2(void)
  59864. {
  59865. EXPECT_DECLS;
  59866. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  59867. const EVP_MD* md = NULL;
  59868. (void)md;
  59869. #if defined(HAVE_BLAKE2)
  59870. ExpectNotNull(md = EVP_blake2b512());
  59871. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  59872. #endif
  59873. #if defined(HAVE_BLAKE2S)
  59874. ExpectNotNull(md = EVP_blake2s256());
  59875. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  59876. #endif
  59877. #endif
  59878. return EXPECT_RESULT();
  59879. }
  59880. #if defined(OPENSSL_EXTRA)
  59881. static void list_md_fn(const EVP_MD* m, const char* from,
  59882. const char* to, void* arg)
  59883. {
  59884. const char* mn;
  59885. BIO *bio;
  59886. (void) from;
  59887. (void) to;
  59888. (void) arg;
  59889. (void) mn;
  59890. (void) bio;
  59891. if (!m) {
  59892. /* alias */
  59893. AssertNull(m);
  59894. AssertNotNull(to);
  59895. }
  59896. else {
  59897. AssertNotNull(m);
  59898. AssertNull(to);
  59899. }
  59900. AssertNotNull(from);
  59901. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  59902. mn = EVP_get_digestbyname(from);
  59903. /* print to stderr */
  59904. AssertNotNull(arg);
  59905. bio = BIO_new(BIO_s_file());
  59906. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  59907. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  59908. BIO_free(bio);
  59909. #endif
  59910. }
  59911. #endif
  59912. static int test_EVP_MD_do_all(void)
  59913. {
  59914. int res = TEST_SKIPPED;
  59915. #if defined(OPENSSL_EXTRA)
  59916. EVP_MD_do_all(NULL, stderr);
  59917. EVP_MD_do_all(list_md_fn, stderr);
  59918. res = TEST_SUCCESS;
  59919. #endif
  59920. return res;
  59921. }
  59922. #if defined(OPENSSL_EXTRA)
  59923. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  59924. {
  59925. (void)arg;
  59926. (void)nm;
  59927. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  59928. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  59929. /* print to stderr */
  59930. AssertNotNull(arg);
  59931. BIO *bio = BIO_new(BIO_s_file());
  59932. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  59933. BIO_printf(bio, "%s\n", nm);
  59934. BIO_free(bio);
  59935. #endif
  59936. }
  59937. #endif
  59938. static int test_OBJ_NAME_do_all(void)
  59939. {
  59940. int res = TEST_SKIPPED;
  59941. #if defined(OPENSSL_EXTRA)
  59942. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  59943. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  59944. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  59945. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  59946. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  59947. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  59948. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  59949. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  59950. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  59951. res = TEST_SUCCESS;
  59952. #endif
  59953. return res;
  59954. }
  59955. static int test_SSL_CIPHER_get_xxx(void)
  59956. {
  59957. EXPECT_DECLS;
  59958. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  59959. !defined(NO_FILESYSTEM)
  59960. const SSL_CIPHER* cipher = NULL;
  59961. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  59962. int i, numCiphers = 0;
  59963. SSL_CTX* ctx = NULL;
  59964. SSL* ssl = NULL;
  59965. const char* testCertFile;
  59966. const char* testKeyFile;
  59967. char buf[256] = {0};
  59968. const char* cipher_id = NULL;
  59969. int expect_nid1 = NID_undef;
  59970. int expect_nid2 = NID_undef;
  59971. int expect_nid3 = NID_undef;
  59972. int expect_nid4 = NID_undef;
  59973. int expect_nid5 = 0;
  59974. const char* cipher_id2 = NULL;
  59975. int expect_nid21 = NID_undef;
  59976. int expect_nid22 = NID_undef;
  59977. int expect_nid23 = NID_undef;
  59978. int expect_nid24 = NID_undef;
  59979. int expect_nid25 = 0;
  59980. (void)cipher;
  59981. (void)supportedCiphers;
  59982. (void)i;
  59983. (void)numCiphers;
  59984. (void)ctx;
  59985. (void)ssl;
  59986. (void)testCertFile;
  59987. (void)testKeyFile;
  59988. #if defined(WOLFSSL_TLS13)
  59989. cipher_id = "TLS13-AES128-GCM-SHA256";
  59990. expect_nid1 = NID_auth_rsa;
  59991. expect_nid2 = NID_aes_128_gcm;
  59992. expect_nid3 = NID_sha256;
  59993. expect_nid4 = NID_kx_any;
  59994. expect_nid5 = 1;
  59995. #if !defined(WOLFSSL_NO_TLS12)
  59996. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  59997. expect_nid21 = NID_auth_rsa;
  59998. expect_nid22 = NID_aes_256_gcm;
  59999. expect_nid23 = NID_sha384;
  60000. expect_nid24 = NID_kx_ecdhe;
  60001. expect_nid25 = 1;
  60002. #endif
  60003. #endif
  60004. #ifdef NO_WOLFSSL_SERVER
  60005. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  60006. #else
  60007. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  60008. #endif
  60009. if (cipher_id) {
  60010. #ifndef NO_RSA
  60011. testCertFile = svrCertFile;
  60012. testKeyFile = svrKeyFile;
  60013. #elif defined(HAVE_ECC)
  60014. testCertFile = eccCertFile;
  60015. testKeyFile = eccKeyFile;
  60016. #else
  60017. testCertFile = NULL;
  60018. testKeyFile = NULL;
  60019. #endif
  60020. if (testCertFile != NULL && testKeyFile != NULL) {
  60021. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  60022. SSL_FILETYPE_PEM));
  60023. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  60024. SSL_FILETYPE_PEM));
  60025. }
  60026. ExpectNotNull(ssl = SSL_new(ctx));
  60027. ExpectIntEQ(SSL_in_init(ssl), 1);
  60028. supportedCiphers = SSL_get_ciphers(ssl);
  60029. numCiphers = sk_num(supportedCiphers);
  60030. for (i = 0; i < numCiphers; ++i) {
  60031. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  60032. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  60033. }
  60034. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  60035. break;
  60036. }
  60037. }
  60038. /* test case for */
  60039. if (i != numCiphers) {
  60040. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  60041. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  60042. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  60043. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  60044. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  60045. }
  60046. if (cipher_id2) {
  60047. for (i = 0; i < numCiphers; ++i) {
  60048. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  60049. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  60050. }
  60051. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  60052. break;
  60053. }
  60054. }
  60055. /* test case for */
  60056. if (i != numCiphers) {
  60057. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  60058. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  60059. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  60060. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  60061. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  60062. }
  60063. }
  60064. }
  60065. SSL_CTX_free(ctx);
  60066. SSL_free(ssl);
  60067. #endif
  60068. return EXPECT_RESULT();
  60069. }
  60070. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  60071. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  60072. int* keyFormat)
  60073. {
  60074. int ret;
  60075. byte* key_buf = NULL;
  60076. size_t key_sz = 0;
  60077. EncryptedInfo encInfo;
  60078. XMEMSET(&encInfo, 0, sizeof(encInfo));
  60079. ret = load_file(privKeyFile, &key_buf, &key_sz);
  60080. if (ret == 0) {
  60081. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  60082. NULL, &encInfo, keyFormat);
  60083. }
  60084. if (key_buf != NULL) {
  60085. free(key_buf); key_buf = NULL;
  60086. }
  60087. (void)encInfo; /* not used in this test */
  60088. #ifdef DEBUG_WOLFSSL
  60089. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  60090. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  60091. (*pDer)->length);
  60092. #endif
  60093. return ret;
  60094. }
  60095. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  60096. {
  60097. int ret = CRYPTOCB_UNAVAILABLE;
  60098. const char* privKeyFile = (const char*)ctx;
  60099. DerBuffer* pDer = NULL;
  60100. int keyFormat = 0;
  60101. if (info->algo_type == WC_ALGO_TYPE_PK) {
  60102. #ifdef DEBUG_WOLFSSL
  60103. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  60104. #endif
  60105. #ifndef NO_RSA
  60106. if (info->pk.type == WC_PK_TYPE_RSA) {
  60107. switch (info->pk.rsa.type) {
  60108. case RSA_PUBLIC_ENCRYPT:
  60109. case RSA_PUBLIC_DECRYPT:
  60110. /* perform software based RSA public op */
  60111. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  60112. break;
  60113. case RSA_PRIVATE_ENCRYPT:
  60114. case RSA_PRIVATE_DECRYPT:
  60115. {
  60116. RsaKey key;
  60117. /* perform software based RSA private op */
  60118. #ifdef DEBUG_WOLFSSL
  60119. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  60120. #endif
  60121. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  60122. &keyFormat);
  60123. if (ret != 0) {
  60124. return ret;
  60125. }
  60126. ret = wc_InitRsaKey(&key, HEAP_HINT);
  60127. if (ret == 0) {
  60128. word32 keyIdx = 0;
  60129. /* load RSA private key and perform private transform */
  60130. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  60131. &key, pDer->length);
  60132. if (ret == 0) {
  60133. ret = wc_RsaFunction(
  60134. info->pk.rsa.in, info->pk.rsa.inLen,
  60135. info->pk.rsa.out, info->pk.rsa.outLen,
  60136. info->pk.rsa.type, &key, info->pk.rsa.rng);
  60137. }
  60138. else {
  60139. /* if decode fails, then fall-back to software based crypto */
  60140. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  60141. "key decode failed %d, falling back to "
  60142. "software\n", ret);
  60143. ret = CRYPTOCB_UNAVAILABLE;
  60144. }
  60145. wc_FreeRsaKey(&key);
  60146. }
  60147. wc_FreeDer(&pDer); pDer = NULL;
  60148. break;
  60149. }
  60150. }
  60151. #ifdef DEBUG_WOLFSSL
  60152. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  60153. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  60154. #endif
  60155. }
  60156. #endif /* !NO_RSA */
  60157. #ifdef HAVE_ECC
  60158. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  60159. /* mark this key as ephemeral */
  60160. if (info->pk.eckg.key != NULL) {
  60161. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  60162. sizeof(info->pk.eckg.key->label));
  60163. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  60164. }
  60165. }
  60166. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  60167. ecc_key key;
  60168. /* perform software based ECC sign */
  60169. #ifdef DEBUG_WOLFSSL
  60170. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  60171. #endif
  60172. if (info->pk.eccsign.key != NULL &&
  60173. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  60174. /* this is an empheral key */
  60175. #ifdef DEBUG_WOLFSSL
  60176. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  60177. "ephemeral key\n");
  60178. #endif
  60179. return CRYPTOCB_UNAVAILABLE;
  60180. }
  60181. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  60182. if (ret != 0) {
  60183. return ret;
  60184. }
  60185. ret = wc_ecc_init(&key);
  60186. if (ret == 0) {
  60187. word32 keyIdx = 0;
  60188. /* load ECC private key and perform private transform */
  60189. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  60190. &key, pDer->length);
  60191. if (ret == 0) {
  60192. ret = wc_ecc_sign_hash(
  60193. info->pk.eccsign.in, info->pk.eccsign.inlen,
  60194. info->pk.eccsign.out, info->pk.eccsign.outlen,
  60195. info->pk.eccsign.rng, &key);
  60196. }
  60197. else {
  60198. /* if decode fails, then fall-back to software based crypto */
  60199. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  60200. "decode failed %d, falling back to software\n", ret);
  60201. ret = CRYPTOCB_UNAVAILABLE;
  60202. }
  60203. wc_ecc_free(&key);
  60204. }
  60205. wc_FreeDer(&pDer); pDer = NULL;
  60206. #ifdef DEBUG_WOLFSSL
  60207. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  60208. ret, *info->pk.eccsign.outlen);
  60209. #endif
  60210. }
  60211. #endif /* HAVE_ECC */
  60212. #ifdef HAVE_ED25519
  60213. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  60214. ed25519_key key;
  60215. /* perform software based ED25519 sign */
  60216. #ifdef DEBUG_WOLFSSL
  60217. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  60218. #endif
  60219. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  60220. if (ret != 0) {
  60221. return ret;
  60222. }
  60223. ret = wc_ed25519_init(&key);
  60224. if (ret == 0) {
  60225. word32 keyIdx = 0;
  60226. /* load ED25519 private key and perform private transform */
  60227. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  60228. &key, pDer->length);
  60229. if (ret == 0) {
  60230. /* calculate public key */
  60231. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  60232. if (ret == 0) {
  60233. key.pubKeySet = 1;
  60234. ret = wc_ed25519_sign_msg_ex(
  60235. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  60236. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  60237. &key, info->pk.ed25519sign.type,
  60238. info->pk.ed25519sign.context,
  60239. info->pk.ed25519sign.contextLen);
  60240. }
  60241. }
  60242. else {
  60243. /* if decode fails, then fall-back to software based crypto */
  60244. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  60245. "decode failed %d, falling back to software\n", ret);
  60246. ret = CRYPTOCB_UNAVAILABLE;
  60247. }
  60248. wc_ed25519_free(&key);
  60249. }
  60250. wc_FreeDer(&pDer); pDer = NULL;
  60251. #ifdef DEBUG_WOLFSSL
  60252. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  60253. ret, *info->pk.ed25519sign.outLen);
  60254. #endif
  60255. }
  60256. #endif /* HAVE_ED25519 */
  60257. }
  60258. (void)thisDevId;
  60259. (void)keyFormat;
  60260. return ret;
  60261. }
  60262. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  60263. static int test_wc_CryptoCb_TLS(int tlsVer,
  60264. const char* cliCaPemFile, const char* cliCertPemFile,
  60265. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  60266. const char* svrCaPemFile, const char* svrCertPemFile,
  60267. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  60268. {
  60269. EXPECT_DECLS;
  60270. callback_functions client_cbf;
  60271. callback_functions server_cbf;
  60272. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60273. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60274. if (tlsVer == WOLFSSL_TLSV1_3) {
  60275. #ifdef WOLFSSL_TLS13
  60276. server_cbf.method = wolfTLSv1_3_server_method;
  60277. client_cbf.method = wolfTLSv1_3_client_method;
  60278. #endif
  60279. }
  60280. else if (tlsVer == WOLFSSL_TLSV1_2) {
  60281. #ifndef WOLFSSL_NO_TLS12
  60282. server_cbf.method = wolfTLSv1_2_server_method;
  60283. client_cbf.method = wolfTLSv1_2_client_method;
  60284. #endif
  60285. }
  60286. else if (tlsVer == WOLFSSL_TLSV1_1) {
  60287. #ifndef NO_OLD_TLS
  60288. server_cbf.method = wolfTLSv1_1_server_method;
  60289. client_cbf.method = wolfTLSv1_1_client_method;
  60290. #endif
  60291. }
  60292. else if (tlsVer == WOLFSSL_TLSV1) {
  60293. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  60294. server_cbf.method = wolfTLSv1_server_method;
  60295. client_cbf.method = wolfTLSv1_client_method;
  60296. #endif
  60297. }
  60298. else if (tlsVer == WOLFSSL_SSLV3) {
  60299. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  60300. defined(WOLFSSL_STATIC_RSA)
  60301. server_cbf.method = wolfSSLv3_server_method;
  60302. client_cbf.method = wolfSSLv3_client_method;
  60303. #endif
  60304. }
  60305. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  60306. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  60307. server_cbf.method = wolfDTLSv1_2_server_method;
  60308. client_cbf.method = wolfDTLSv1_2_client_method;
  60309. #endif
  60310. }
  60311. else if (tlsVer == WOLFSSL_DTLSV1) {
  60312. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  60313. server_cbf.method = wolfDTLSv1_server_method;
  60314. client_cbf.method = wolfDTLSv1_client_method;
  60315. #endif
  60316. }
  60317. if (server_cbf.method == NULL) {
  60318. /* not enabled */
  60319. return TEST_SUCCESS;
  60320. }
  60321. /* Setup the keys for the TLS test */
  60322. client_cbf.certPemFile = cliCertPemFile;
  60323. client_cbf.keyPemFile = cliPubKeyPemFile;
  60324. client_cbf.caPemFile = cliCaPemFile;
  60325. server_cbf.certPemFile = svrCertPemFile;
  60326. server_cbf.keyPemFile = svrPubKeyPemFile;
  60327. server_cbf.caPemFile = svrCaPemFile;
  60328. /* Setup a crypto callback with pointer to private key file for testing */
  60329. client_cbf.devId = 1;
  60330. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  60331. (void*)cliPrivKeyPemFile);
  60332. server_cbf.devId = 2;
  60333. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  60334. (void*)svrPrivKeyPemFile);
  60335. /* Perform TLS server and client test */
  60336. /* First test is at WOLFSSL_CTX level */
  60337. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  60338. /* Check for success */
  60339. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  60340. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  60341. if (EXPECT_SUCCESS()) {
  60342. /* Second test is a WOLFSSL object level */
  60343. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  60344. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  60345. }
  60346. /* Check for success */
  60347. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  60348. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  60349. /* Un register the devId's */
  60350. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  60351. client_cbf.devId = INVALID_DEVID;
  60352. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  60353. server_cbf.devId = INVALID_DEVID;
  60354. return EXPECT_RESULT();
  60355. }
  60356. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  60357. static int test_wc_CryptoCb(void)
  60358. {
  60359. EXPECT_DECLS;
  60360. #ifdef WOLF_CRYPTO_CB
  60361. /* TODO: Add crypto callback API tests */
  60362. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60363. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  60364. int tlsVer;
  60365. #endif
  60366. #ifndef NO_RSA
  60367. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  60368. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  60369. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  60370. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  60371. TEST_SUCCESS);
  60372. }
  60373. #endif
  60374. #ifdef HAVE_ECC
  60375. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  60376. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  60377. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  60378. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  60379. TEST_SUCCESS);
  60380. }
  60381. #endif
  60382. #ifdef HAVE_ED25519
  60383. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  60384. if (tlsVer == WOLFSSL_DTLSV1) continue;
  60385. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  60386. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  60387. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  60388. TEST_SUCCESS);
  60389. }
  60390. #endif
  60391. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  60392. #endif /* WOLF_CRYPTO_CB */
  60393. return EXPECT_RESULT();
  60394. }
  60395. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  60396. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  60397. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  60398. const char* cliCaPemFile, const char* cliCertPemFile,
  60399. const char* cliPrivKeyPemFile,
  60400. const char* svrCaPemFile, const char* svrCertPemFile,
  60401. const char* svrPrivKeyPemFile,
  60402. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  60403. {
  60404. EXPECT_DECLS;
  60405. callback_functions client_cbf;
  60406. callback_functions server_cbf;
  60407. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60408. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60409. if (tlsVer == WOLFSSL_TLSV1_3) {
  60410. #ifdef WOLFSSL_TLS13
  60411. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  60412. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  60413. #endif
  60414. }
  60415. else if (tlsVer == WOLFSSL_TLSV1_2) {
  60416. #ifndef WOLFSSL_NO_TLS12
  60417. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  60418. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  60419. #endif
  60420. }
  60421. else if (tlsVer == WOLFSSL_TLSV1_1) {
  60422. #ifndef NO_OLD_TLS
  60423. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  60424. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  60425. #endif
  60426. }
  60427. else if (tlsVer == WOLFSSL_TLSV1) {
  60428. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  60429. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  60430. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  60431. #endif
  60432. }
  60433. else if (tlsVer == WOLFSSL_SSLV3) {
  60434. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  60435. defined(WOLFSSL_STATIC_RSA)
  60436. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  60437. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  60438. #endif
  60439. }
  60440. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  60441. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  60442. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  60443. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  60444. #endif
  60445. }
  60446. else if (tlsVer == WOLFSSL_DTLSV1) {
  60447. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  60448. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  60449. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  60450. #endif
  60451. }
  60452. if (server_cbf.method_ex == NULL) {
  60453. /* not enabled */
  60454. return TEST_SUCCESS;
  60455. }
  60456. /* Setup the keys for the TLS test */
  60457. client_cbf.certPemFile = cliCertPemFile;
  60458. client_cbf.keyPemFile = cliPrivKeyPemFile;
  60459. client_cbf.caPemFile = cliCaPemFile;
  60460. server_cbf.certPemFile = svrCertPemFile;
  60461. server_cbf.keyPemFile = svrPrivKeyPemFile;
  60462. server_cbf.caPemFile = svrCaPemFile;
  60463. client_cbf.mem = cliMem;
  60464. client_cbf.memSz = cliMemSz;
  60465. server_cbf.mem = svrMem;
  60466. server_cbf.memSz = svrMemSz;
  60467. client_cbf.devId = INVALID_DEVID;
  60468. server_cbf.devId = INVALID_DEVID;
  60469. /* Perform TLS server and client test */
  60470. /* First test is at WOLFSSL_CTX level */
  60471. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  60472. /* Check for success */
  60473. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  60474. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  60475. if (EXPECT_SUCCESS()) {
  60476. /* Second test is a WOLFSSL object level */
  60477. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  60478. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  60479. }
  60480. /* Check for success */
  60481. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  60482. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  60483. return EXPECT_RESULT();
  60484. }
  60485. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  60486. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  60487. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  60488. {
  60489. EXPECT_DECLS;
  60490. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  60491. WOLFSSL_MEM_STATS mem_stats;
  60492. WOLFSSL_MEM_CONN_STATS ssl_stats;
  60493. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  60494. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  60495. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60496. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  60497. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60498. #endif
  60499. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  60500. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  60501. /* this should fail because kMaxCtxClients == 2 */
  60502. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  60503. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  60504. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  60505. wolfSSL_PrintStatsConn(&ssl_stats);
  60506. #endif
  60507. (void)ssl_stats;
  60508. }
  60509. /* display collected statistics */
  60510. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  60511. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  60512. wolfSSL_PrintStats(&mem_stats);
  60513. #endif
  60514. (void)mem_stats;
  60515. }
  60516. wolfSSL_free(ssl1);
  60517. wolfSSL_free(ssl2);
  60518. return EXPECT_RESULT();
  60519. }
  60520. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  60521. static int test_wolfSSL_CTX_StaticMemory(void)
  60522. {
  60523. EXPECT_DECLS;
  60524. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  60525. wolfSSL_method_func method_func;
  60526. WOLFSSL_CTX* ctx;
  60527. const int kMaxCtxClients = 2;
  60528. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60529. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  60530. int tlsVer;
  60531. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  60532. #endif
  60533. #endif
  60534. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  60535. #ifndef NO_WOLFSSL_SERVER
  60536. #ifndef WOLFSSL_NO_TLS12
  60537. method_func = wolfTLSv1_2_server_method_ex;
  60538. #else
  60539. method_func = wolfTLSv1_3_server_method_ex;
  60540. #endif
  60541. #else
  60542. #ifndef WOLFSSL_NO_TLS12
  60543. method_func = wolfTLSv1_2_client_method_ex;
  60544. #else
  60545. method_func = wolfTLSv1_3_client_method_ex;
  60546. #endif
  60547. #endif
  60548. /* Test creating CTX directly from static memory pool */
  60549. ctx = NULL;
  60550. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  60551. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  60552. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  60553. wolfSSL_CTX_free(ctx);
  60554. ctx = NULL;
  60555. /* Test for heap allocated CTX, then assigning static pool to it */
  60556. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  60557. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  60558. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  60559. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  60560. wolfSSL_CTX_free(ctx);
  60561. /* TLS Level Tests using static memory */
  60562. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60563. #ifndef NO_RSA
  60564. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  60565. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  60566. svrCertFile, cliCertFile, cliKeyFile,
  60567. cliCertFile, svrCertFile, svrKeyFile,
  60568. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  60569. TEST_SUCCESS);
  60570. }
  60571. #endif
  60572. #ifdef HAVE_ECC
  60573. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  60574. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  60575. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  60576. cliEccCertFile, eccCertFile, eccKeyFile,
  60577. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  60578. TEST_SUCCESS);
  60579. }
  60580. #endif
  60581. #ifdef HAVE_ED25519
  60582. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  60583. if (tlsVer == WOLFSSL_DTLSV1) continue;
  60584. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  60585. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  60586. cliEdCertFile, edCertFile, edKeyFile,
  60587. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  60588. TEST_SUCCESS);
  60589. }
  60590. #endif
  60591. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  60592. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  60593. return EXPECT_RESULT();
  60594. }
  60595. static int test_openssl_FIPS_drbg(void)
  60596. {
  60597. EXPECT_DECLS;
  60598. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  60599. DRBG_CTX* dctx = NULL;
  60600. byte data1[32], data2[32], zeroData[32];
  60601. byte testSeed[16];
  60602. size_t dlen = sizeof(data1);
  60603. int i;
  60604. XMEMSET(data1, 0, dlen);
  60605. XMEMSET(data2, 0, dlen);
  60606. XMEMSET(zeroData, 0, sizeof(zeroData));
  60607. for (i = 0; i < (int)sizeof(testSeed); i++) {
  60608. testSeed[i] = (byte)i;
  60609. }
  60610. ExpectNotNull(dctx = FIPS_get_default_drbg());
  60611. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  60612. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  60613. WOLFSSL_SUCCESS);
  60614. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  60615. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  60616. WOLFSSL_SUCCESS);
  60617. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  60618. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  60619. WOLFSSL_SUCCESS);
  60620. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  60621. WOLFSSL_SUCCESS);
  60622. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  60623. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  60624. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  60625. #ifndef HAVE_GLOBAL_RNG
  60626. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  60627. wolfSSL_FIPS_drbg_free(dctx);
  60628. #endif
  60629. #endif
  60630. return EXPECT_RESULT();
  60631. }
  60632. static int test_wolfSSL_FIPS_mode(void)
  60633. {
  60634. EXPECT_DECLS;
  60635. #if defined(OPENSSL_ALL)
  60636. #ifdef HAVE_FIPS
  60637. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  60638. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  60639. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  60640. #else
  60641. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  60642. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  60643. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  60644. #endif
  60645. #endif
  60646. return EXPECT_RESULT();
  60647. }
  60648. #ifdef WOLFSSL_DTLS
  60649. /* Prints out the current window */
  60650. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  60651. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  60652. int i;
  60653. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  60654. word32 b = w[i];
  60655. int j;
  60656. /* Prints out a 32 bit binary number in big endian order */
  60657. for (j = 0; j < 32; j++, b <<= 1) {
  60658. if (b & (((word32)1) << 31))
  60659. fprintf(stderr, "1");
  60660. else
  60661. fprintf(stderr, "0");
  60662. }
  60663. fprintf(stderr, " ");
  60664. }
  60665. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  60666. #else
  60667. (void)h;
  60668. (void)l;
  60669. (void)w;
  60670. #endif
  60671. }
  60672. /* a - cur_hi
  60673. * b - cur_lo
  60674. * c - next_hi
  60675. * d - next_lo
  60676. * e - window
  60677. * f - expected next_hi
  60678. * g - expected next_lo
  60679. * h - expected window[1]
  60680. * i - expected window[0]
  60681. */
  60682. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  60683. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  60684. DUW_TEST_print_window_binary((a), (b), (e)); \
  60685. ExpectIntEQ((c), (f)); \
  60686. ExpectIntEQ((d), (g)); \
  60687. ExpectIntEQ((e)[1], (h)); \
  60688. ExpectIntEQ((e)[0], (i)); \
  60689. } while (0)
  60690. static int test_wolfSSL_DtlsUpdateWindow(void)
  60691. {
  60692. EXPECT_DECLS;
  60693. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  60694. word32 next_lo = 0;
  60695. word16 next_hi = 0;
  60696. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  60697. fprintf(stderr, "\n");
  60698. #endif
  60699. XMEMSET(window, 0, sizeof window);
  60700. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  60701. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  60702. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  60703. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  60704. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  60705. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  60706. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  60707. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  60708. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  60709. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  60710. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  60711. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  60712. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  60713. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  60714. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  60715. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  60716. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  60717. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  60718. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  60719. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  60720. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  60721. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  60722. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  60723. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  60724. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  60725. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  60726. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  60727. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  60728. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  60729. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  60730. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  60731. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  60732. return EXPECT_RESULT();
  60733. }
  60734. #endif /* WOLFSSL_DTLS */
  60735. #ifdef WOLFSSL_DTLS
  60736. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  60737. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  60738. {
  60739. DtlsMsg* cur;
  60740. static byte msg[100];
  60741. static byte msgInit = 0;
  60742. if (!msgInit) {
  60743. int i;
  60744. for (i = 0; i < 100; i++)
  60745. msg[i] = i + 1;
  60746. msgInit = 1;
  60747. }
  60748. /* Sanitize test parameters */
  60749. if (len > sizeof(msg))
  60750. return -1;
  60751. if (f_offset + f_len > sizeof(msg))
  60752. return -1;
  60753. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  60754. if (ssl->dtls_rx_msg_list == NULL)
  60755. return -100;
  60756. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  60757. return -200;
  60758. if (cur->fragBucketListCount != f_count)
  60759. return -300;
  60760. if (cur->ready != ready)
  60761. return -400;
  60762. if (cur->bytesReceived != bytesReceived)
  60763. return -500;
  60764. if (ready) {
  60765. if (cur->fragBucketList != NULL)
  60766. return -600;
  60767. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  60768. return -700;
  60769. }
  60770. else {
  60771. DtlsFragBucket* fb;
  60772. if (cur->fragBucketList == NULL)
  60773. return -800;
  60774. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  60775. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  60776. return -900;
  60777. }
  60778. }
  60779. return 0;
  60780. }
  60781. static int test_wolfSSL_DTLS_fragment_buckets(void)
  60782. {
  60783. EXPECT_DECLS;
  60784. WOLFSSL ssl[1];
  60785. XMEMSET(ssl, 0, sizeof(*ssl));
  60786. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  60787. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  60788. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  60789. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  60790. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  60791. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  60792. /* Test all permutations of 3 regions */
  60793. /* 1 2 3 */
  60794. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  60795. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  60796. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  60797. /* 1 3 2 */
  60798. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  60799. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  60800. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  60801. /* 2 1 3 */
  60802. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  60803. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  60804. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  60805. /* 2 3 1 */
  60806. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  60807. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  60808. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  60809. /* 3 1 2 */
  60810. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  60811. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  60812. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  60813. /* 3 2 1 */
  60814. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  60815. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  60816. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  60817. /* Test overlapping regions */
  60818. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  60819. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  60820. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  60821. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  60822. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  60823. /* Test overlapping multiple regions */
  60824. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  60825. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  60826. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  60827. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  60828. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  60829. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  60830. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  60831. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  60832. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  60833. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  60834. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  60835. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  60836. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  60837. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  60838. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  60839. ssl->dtls_rx_msg_list = NULL;
  60840. ssl->dtls_rx_msg_list_sz = 0;
  60841. return EXPECT_RESULT();
  60842. }
  60843. #endif
  60844. #if !defined(NO_FILESYSTEM) && \
  60845. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60846. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  60847. static int test_wolfSSL_dtls_stateless2(void)
  60848. {
  60849. EXPECT_DECLS;
  60850. WOLFSSL *ssl_c = NULL;
  60851. WOLFSSL *ssl_c2 = NULL;
  60852. WOLFSSL *ssl_s = NULL;
  60853. struct test_memio_ctx test_ctx;
  60854. WOLFSSL_CTX *ctx_c = NULL;
  60855. WOLFSSL_CTX *ctx_s = NULL;
  60856. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60857. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60858. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  60859. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  60860. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  60861. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  60862. /* send CH */
  60863. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  60864. (ssl_c2->error == WANT_READ));
  60865. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  60866. (ssl_s->error == WANT_READ));
  60867. ExpectIntNE(test_ctx.c_len, 0);
  60868. /* consume HRR */
  60869. test_ctx.c_len = 0;
  60870. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60871. wolfSSL_free(ssl_c2);
  60872. wolfSSL_free(ssl_c);
  60873. wolfSSL_free(ssl_s);
  60874. wolfSSL_CTX_free(ctx_c);
  60875. wolfSSL_CTX_free(ctx_s);
  60876. return EXPECT_RESULT();
  60877. }
  60878. #ifdef HAVE_MAX_FRAGMENT
  60879. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  60880. {
  60881. EXPECT_DECLS;
  60882. WOLFSSL *ssl_c = NULL;
  60883. WOLFSSL *ssl_c2 = NULL;
  60884. WOLFSSL *ssl_s = NULL;
  60885. struct test_memio_ctx test_ctx;
  60886. WOLFSSL_CTX *ctx_c = NULL;
  60887. WOLFSSL_CTX *ctx_s = NULL;
  60888. word16 max_fragment = 0;
  60889. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60890. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60891. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  60892. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  60893. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  60894. WOLFSSL_SUCCESS);
  60895. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  60896. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  60897. if (ssl_s != NULL) {
  60898. max_fragment = ssl_s->max_fragment;
  60899. }
  60900. /* send CH */
  60901. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  60902. (ssl_c2->error == WANT_READ));
  60903. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  60904. (ssl_s->error == WANT_READ));
  60905. /* CH without cookie shouldn't change state */
  60906. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  60907. ExpectIntNE(test_ctx.c_len, 0);
  60908. /* consume HRR from buffer */
  60909. test_ctx.c_len = 0;
  60910. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60911. wolfSSL_free(ssl_c2);
  60912. wolfSSL_free(ssl_c);
  60913. wolfSSL_free(ssl_s);
  60914. wolfSSL_CTX_free(ctx_c);
  60915. wolfSSL_CTX_free(ctx_s);
  60916. return EXPECT_RESULT();
  60917. }
  60918. #endif /* HAVE_MAX_FRAGMENT */
  60919. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  60920. #define ROUNDS_WITH_HVR 4
  60921. #define ROUNDS_WITHOUT_HVR 2
  60922. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  60923. static int buf_is_hvr(const byte *data, int len)
  60924. {
  60925. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  60926. return 0;
  60927. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  60928. }
  60929. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  60930. {
  60931. EXPECT_DECLS;
  60932. struct test_memio_ctx test_ctx;
  60933. WOLFSSL_CTX *ctx_c = NULL;
  60934. WOLFSSL_CTX *ctx_s = NULL;
  60935. WOLFSSL *ssl_c = NULL;
  60936. WOLFSSL *ssl_s = NULL;
  60937. WOLFSSL_SESSION *sess = NULL;
  60938. int round_trips;
  60939. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60940. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  60941. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  60942. #ifdef HAVE_SESSION_TICKET
  60943. if (useticket) {
  60944. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  60945. }
  60946. #endif
  60947. round_trips = ROUNDS_WITH_HVR;
  60948. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  60949. &round_trips), 0);
  60950. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  60951. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  60952. wolfSSL_shutdown(ssl_c);
  60953. wolfSSL_shutdown(ssl_s);
  60954. wolfSSL_free(ssl_c);
  60955. ssl_c = NULL;
  60956. wolfSSL_free(ssl_s);
  60957. ssl_s = NULL;
  60958. test_ctx.c_len = test_ctx.s_len = 0;
  60959. /* make resumption invalid */
  60960. if (bad && (sess != NULL)) {
  60961. if (useticket) {
  60962. #ifdef HAVE_SESSION_TICKET
  60963. if (sess->ticket != NULL) {
  60964. sess->ticket[0] = !sess->ticket[0];
  60965. }
  60966. #endif /* HAVE_SESSION_TICKET */
  60967. }
  60968. else {
  60969. sess->sessionID[0] = !sess->sessionID[0];
  60970. }
  60971. }
  60972. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  60973. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  60974. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  60975. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  60976. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  60977. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  60978. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  60979. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  60980. (ssl_c->error == WANT_READ));
  60981. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  60982. (ssl_s->error == WANT_READ));
  60983. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  60984. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  60985. if (!useticket) {
  60986. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  60987. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  60988. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  60989. }
  60990. wolfSSL_SESSION_free(sess);
  60991. wolfSSL_free(ssl_c);
  60992. wolfSSL_free(ssl_s);
  60993. wolfSSL_CTX_free(ctx_c);
  60994. wolfSSL_CTX_free(ctx_s);
  60995. return EXPECT_RESULT();
  60996. }
  60997. static int test_wolfSSL_dtls_stateless_resume(void)
  60998. {
  60999. EXPECT_DECLS;
  61000. #ifdef HAVE_SESSION_TICKET
  61001. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  61002. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  61003. #endif /* HAVE_SESION_TICKET */
  61004. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  61005. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  61006. return EXPECT_RESULT();
  61007. }
  61008. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  61009. #if !defined(NO_OLD_TLS)
  61010. static int test_wolfSSL_dtls_stateless_downgrade(void)
  61011. {
  61012. EXPECT_DECLS;
  61013. WOLFSSL_CTX *ctx_c = NULL;
  61014. WOLFSSL_CTX *ctx_c2 = NULL;
  61015. WOLFSSL_CTX *ctx_s = NULL;
  61016. WOLFSSL *ssl_c = NULL;
  61017. WOLFSSL *ssl_c2 = NULL;
  61018. WOLFSSL *ssl_s = NULL;
  61019. struct test_memio_ctx test_ctx;
  61020. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61021. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61022. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  61023. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  61024. WOLFSSL_SUCCESS);
  61025. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  61026. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  61027. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  61028. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  61029. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  61030. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  61031. /* send CH */
  61032. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  61033. (ssl_c2->error == WANT_READ));
  61034. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  61035. (ssl_s->error == WANT_READ));
  61036. ExpectIntNE(test_ctx.c_len, 0);
  61037. /* consume HRR */
  61038. test_ctx.c_len = 0;
  61039. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61040. wolfSSL_free(ssl_c2);
  61041. wolfSSL_free(ssl_c);
  61042. wolfSSL_free(ssl_s);
  61043. wolfSSL_CTX_free(ctx_c);
  61044. wolfSSL_CTX_free(ctx_c2);
  61045. wolfSSL_CTX_free(ctx_s);
  61046. return EXPECT_RESULT();
  61047. }
  61048. #endif /* !defined(NO_OLD_TLS) */
  61049. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61050. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  61051. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61052. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  61053. static int test_WOLFSSL_dtls_version_alert(void)
  61054. {
  61055. EXPECT_DECLS;
  61056. struct test_memio_ctx test_ctx;
  61057. WOLFSSL_CTX *ctx_c = NULL;
  61058. WOLFSSL_CTX *ctx_s = NULL;
  61059. WOLFSSL *ssl_c = NULL;
  61060. WOLFSSL *ssl_s = NULL;
  61061. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61062. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61063. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  61064. /* client hello */
  61065. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  61066. (ssl_c->error == WANT_READ));
  61067. /* hrr */
  61068. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  61069. (ssl_s->error == WANT_READ));
  61070. /* client hello 1 */
  61071. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  61072. (ssl_c->error == WANT_READ));
  61073. /* server hello */
  61074. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  61075. (ssl_s->error == WANT_READ));
  61076. /* should fail */
  61077. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  61078. (ssl_c->error == VERSION_ERROR));
  61079. /* shuould fail */
  61080. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  61081. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  61082. wolfSSL_free(ssl_c);
  61083. wolfSSL_free(ssl_s);
  61084. wolfSSL_CTX_free(ctx_c);
  61085. wolfSSL_CTX_free(ctx_s);
  61086. return EXPECT_RESULT();
  61087. }
  61088. #else
  61089. static int test_WOLFSSL_dtls_version_alert(void)
  61090. {
  61091. return TEST_SKIPPED;
  61092. }
  61093. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  61094. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  61095. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  61096. */
  61097. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  61098. && defined(WOLFSSL_TLS13) && \
  61099. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  61100. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  61101. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  61102. {
  61103. struct test_memio_ctx *test_ctx;
  61104. byte buf[2048];
  61105. int idx, sz;
  61106. word32 tmp;
  61107. int ret;
  61108. idx = 5; /* space for record header */
  61109. buf[idx] = session_ticket; /* type */
  61110. idx++;
  61111. tmp = OPAQUE32_LEN +
  61112. OPAQUE32_LEN +
  61113. OPAQUE8_LEN + nonceLength +
  61114. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  61115. c32to24(tmp, buf + idx);
  61116. idx += OPAQUE24_LEN;
  61117. c32toa((word32)12345, buf+idx); /* lifetime */
  61118. idx += OPAQUE32_LEN;
  61119. c32toa((word32)12345, buf+idx); /* add */
  61120. idx += OPAQUE32_LEN;
  61121. buf[idx] = nonceLength; /* nonce length */
  61122. idx++;
  61123. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  61124. idx += nonceLength;
  61125. tmp = 1; /* ticket len */
  61126. c16toa((word16)tmp, buf+idx);
  61127. idx += 2;
  61128. buf[idx] = 0xFF; /* ticket */
  61129. idx++;
  61130. tmp = 0; /* ext len */
  61131. c16toa((word16)tmp, buf+idx);
  61132. idx += 2;
  61133. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  61134. handshake, 0, 0, 0);
  61135. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  61136. AssertNotNull(test_ctx);
  61137. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  61138. return !(ret == sz);
  61139. }
  61140. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  61141. {
  61142. int ret = 0;
  61143. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  61144. ret = -1;
  61145. }
  61146. else {
  61147. int i;
  61148. for (i = 0; i < len; i++) {
  61149. if (sess->ticketNonce.data[i] != len) {
  61150. ret = -1;
  61151. break;
  61152. }
  61153. }
  61154. }
  61155. return ret;
  61156. }
  61157. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  61158. {
  61159. EXPECT_DECLS;
  61160. char *buf[1024];
  61161. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  61162. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  61163. (ssl_c->error == WANT_READ));
  61164. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  61165. return EXPECT_RESULT();
  61166. }
  61167. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  61168. {
  61169. EXPECT_DECLS;
  61170. WOLFSSL_SESSION *sess = NULL;
  61171. WOLFSSL_SESSION *cached = NULL;
  61172. WOLFSSL_CTX *ctx = ssl_c->ctx;
  61173. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  61174. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61175. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  61176. NULL, ssl_c->options.side, 1,NULL), 0);
  61177. ExpectNotNull(cached = wolfSSL_SESSION_new());
  61178. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  61179. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  61180. wolfSSL_SESSION_free(cached);
  61181. wolfSSL_SESSION_free(sess);
  61182. return EXPECT_RESULT();
  61183. }
  61184. static int test_ticket_nonce_malloc(void)
  61185. {
  61186. EXPECT_DECLS;
  61187. struct test_memio_ctx test_ctx;
  61188. WOLFSSL_CTX *ctx_c = NULL;
  61189. WOLFSSL_CTX *ctx_s = NULL;
  61190. WOLFSSL *ssl_c = NULL;
  61191. WOLFSSL *ssl_s = NULL;
  61192. byte small;
  61193. byte medium;
  61194. byte big;
  61195. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61196. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61197. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61198. /* will send ticket manually */
  61199. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  61200. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  61201. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  61202. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  61203. (ssl_s->options.handShakeDone == 0)) {
  61204. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  61205. (ssl_c->error == WANT_READ));
  61206. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  61207. (ssl_s->error == WANT_READ));
  61208. }
  61209. small = TLS13_TICKET_NONCE_STATIC_SZ;
  61210. medium = small + 20 <= 255 ? small + 20 : 255;
  61211. big = medium + 20 <= 255 ? small + 20 : 255;
  61212. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  61213. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  61214. ssl_c->session->ticketNonce.dataStatic);
  61215. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  61216. TEST_SUCCESS);
  61217. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  61218. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  61219. TEST_SUCCESS);
  61220. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  61221. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  61222. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  61223. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  61224. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  61225. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  61226. wolfSSL_free(ssl_c);
  61227. wolfSSL_free(ssl_s);
  61228. wolfSSL_CTX_free(ctx_c);
  61229. wolfSSL_CTX_free(ctx_s);
  61230. return EXPECT_RESULT();
  61231. }
  61232. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  61233. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  61234. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  61235. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61236. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  61237. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  61238. static int test_ticket_ret_create(void)
  61239. {
  61240. EXPECT_DECLS;
  61241. WOLFSSL_CTX *ctx_c = NULL;
  61242. WOLFSSL_CTX *ctx_s = NULL;
  61243. WOLFSSL *ssl_c = NULL;
  61244. WOLFSSL *ssl_s = NULL;
  61245. byte ticket[SESSION_TICKET_LEN];
  61246. struct test_memio_ctx test_ctx;
  61247. WOLFSSL_SESSION *sess = NULL;
  61248. word16 ticketLen = 0;
  61249. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61250. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61251. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  61252. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  61253. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  61254. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  61255. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61256. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61257. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  61258. if (sess != NULL) {
  61259. ticketLen = sess->ticketLen;
  61260. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  61261. }
  61262. wolfSSL_free(ssl_c);
  61263. ssl_c = NULL;
  61264. wolfSSL_free(ssl_s);
  61265. ssl_s = NULL;
  61266. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  61267. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  61268. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  61269. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  61270. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  61271. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  61272. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  61273. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61274. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  61275. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  61276. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  61277. wolfSSL_SESSION_free(sess);
  61278. wolfSSL_free(ssl_c);
  61279. wolfSSL_free(ssl_s);
  61280. wolfSSL_CTX_free(ctx_c);
  61281. wolfSSL_CTX_free(ctx_s);
  61282. return EXPECT_RESULT();
  61283. }
  61284. #else
  61285. static int test_ticket_ret_create(void)
  61286. {
  61287. return TEST_SKIPPED;
  61288. }
  61289. #endif
  61290. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  61291. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  61292. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  61293. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  61294. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61295. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  61296. {
  61297. int ret;
  61298. WOLFSSL_SESSION* session = NULL;
  61299. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  61300. if (!wolfSSL_is_server(ssl)) {
  61301. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  61302. AssertNotNull(session);
  61303. }
  61304. do {
  61305. ret = wolfSSL_shutdown(ssl);
  61306. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  61307. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  61308. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  61309. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  61310. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  61311. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  61312. #endif
  61313. if (!wolfSSL_is_server(ssl)) {
  61314. /* client */
  61315. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  61316. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  61317. wolfSSL_set_session(ssl, session);
  61318. wolfSSL_SESSION_free(session);
  61319. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  61320. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  61321. }
  61322. else {
  61323. /* server */
  61324. /* Different ciphersuite so that the ticket will be invalidated based on
  61325. * the ciphersuite */
  61326. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  61327. WOLFSSL_SUCCESS);
  61328. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  61329. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  61330. }
  61331. }
  61332. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  61333. {
  61334. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  61335. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  61336. WOLFSSL_SUCCESS);
  61337. }
  61338. static int test_ticket_and_psk_mixing(void)
  61339. {
  61340. EXPECT_DECLS;
  61341. /* Test mixing tickets and regular PSK */
  61342. callback_functions client_cbs, server_cbs;
  61343. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61344. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61345. client_cbs.method = wolfTLSv1_3_client_method;
  61346. server_cbs.method = wolfTLSv1_3_server_method;
  61347. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  61348. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  61349. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  61350. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  61351. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  61352. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  61353. return EXPECT_RESULT();
  61354. }
  61355. #else
  61356. static int test_ticket_and_psk_mixing(void)
  61357. {
  61358. return TEST_SKIPPED;
  61359. }
  61360. #endif
  61361. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  61362. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  61363. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  61364. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61365. static int test_prioritize_psk_cb_called = FALSE;
  61366. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  61367. const char* identity, unsigned char* key, unsigned int key_max_len,
  61368. const char** ciphersuite)
  61369. {
  61370. test_prioritize_psk_cb_called = TRUE;
  61371. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  61372. }
  61373. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  61374. {
  61375. int ret;
  61376. WOLFSSL_SESSION* session = NULL;
  61377. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  61378. if (!wolfSSL_is_server(ssl)) {
  61379. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  61380. AssertNotNull(session);
  61381. }
  61382. do {
  61383. ret = wolfSSL_shutdown(ssl);
  61384. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  61385. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  61386. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  61387. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  61388. * important. */
  61389. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  61390. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  61391. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  61392. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  61393. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  61394. #endif
  61395. if (!wolfSSL_is_server(ssl)) {
  61396. /* client */
  61397. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  61398. wolfSSL_set_session(ssl, session);
  61399. wolfSSL_SESSION_free(session);
  61400. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  61401. }
  61402. else {
  61403. /* server */
  61404. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  61405. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  61406. #ifdef WOLFSSL_PRIORITIZE_PSK
  61407. /* The ticket should be first tried with all ciphersuites and chosen */
  61408. AssertFalse(test_prioritize_psk_cb_called);
  61409. #else
  61410. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  61411. * callback that sets this var. */
  61412. AssertTrue(test_prioritize_psk_cb_called);
  61413. #endif
  61414. }
  61415. }
  61416. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  61417. {
  61418. if (!wolfSSL_is_server(ssl))
  61419. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  61420. WOLFSSL_SUCCESS);
  61421. else
  61422. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  61423. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  61424. }
  61425. static int test_prioritize_psk(void)
  61426. {
  61427. EXPECT_DECLS;
  61428. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  61429. * of the PSK's will be followed instead of the ciphersuite. */
  61430. callback_functions client_cbs, server_cbs;
  61431. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61432. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61433. client_cbs.method = wolfTLSv1_3_client_method;
  61434. server_cbs.method = wolfTLSv1_3_server_method;
  61435. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  61436. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  61437. client_cbs.on_result = test_prioritize_psk_on_result;
  61438. server_cbs.on_result = test_prioritize_psk_on_result;
  61439. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  61440. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  61441. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  61442. return EXPECT_RESULT();
  61443. }
  61444. #else
  61445. static int test_prioritize_psk(void)
  61446. {
  61447. return TEST_SKIPPED;
  61448. }
  61449. #endif
  61450. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  61451. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  61452. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  61453. !defined(WOLFSSL_NO_TLS12)
  61454. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  61455. {
  61456. EXPECT_DECLS;
  61457. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  61458. /* Set TLS 1.3 specific suite */
  61459. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  61460. return EXPECT_RESULT();
  61461. }
  61462. static int test_wolfSSL_CTX_set_ciphersuites(void)
  61463. {
  61464. EXPECT_DECLS;
  61465. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  61466. * do a 1.2 connection. */
  61467. test_ssl_cbf client_cbs, server_cbs;
  61468. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61469. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61470. client_cbs.method = wolfTLSv1_2_client_method;
  61471. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  61472. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  61473. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  61474. &server_cbs, NULL), TEST_SUCCESS);
  61475. return EXPECT_RESULT();
  61476. }
  61477. #else
  61478. static int test_wolfSSL_CTX_set_ciphersuites(void)
  61479. {
  61480. return TEST_SKIPPED;
  61481. }
  61482. #endif
  61483. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  61484. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61485. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  61486. {
  61487. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  61488. return TEST_SUCCESS;
  61489. }
  61490. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  61491. {
  61492. EXPECT_DECLS;
  61493. WOLFSSL_ALERT_HISTORY h;
  61494. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  61495. ExpectIntEQ(h.last_rx.level, alert_fatal);
  61496. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  61497. return EXPECT_RESULT();
  61498. }
  61499. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  61500. {
  61501. EXPECT_DECLS;
  61502. test_ssl_cbf client_cbs, server_cbs;
  61503. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61504. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61505. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  61506. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  61507. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  61508. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  61509. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  61510. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  61511. &server_cbs, NULL), TEST_FAIL);
  61512. return EXPECT_RESULT();
  61513. }
  61514. #else
  61515. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  61516. {
  61517. return TEST_SKIPPED;
  61518. }
  61519. #endif
  61520. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  61521. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  61522. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  61523. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  61524. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  61525. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  61526. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  61527. static int test_TLS_13_ticket_different_ciphers_run = 0;
  61528. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  61529. {
  61530. EXPECT_DECLS;
  61531. switch (test_TLS_13_ticket_different_ciphers_run) {
  61532. case 0:
  61533. /* First run */
  61534. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  61535. WOLFSSL_SUCCESS);
  61536. if (wolfSSL_is_server(ssl)) {
  61537. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  61538. wolfSSL_get_SSL_CTX(ssl));
  61539. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  61540. test_TLS_13_ticket_different_ciphers_ctx));
  61541. }
  61542. break;
  61543. case 1:
  61544. /* Second run */
  61545. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  61546. "TLS13-AES128-GCM-SHA256"),
  61547. WOLFSSL_SUCCESS);
  61548. if (!wolfSSL_is_server(ssl)) {
  61549. ExpectIntEQ(wolfSSL_set_session(ssl,
  61550. test_TLS_13_ticket_different_ciphers_session),
  61551. WOLFSSL_SUCCESS);
  61552. }
  61553. break;
  61554. default:
  61555. /* Bad state? */
  61556. Fail(("Should not enter here"), ("Should not enter here"));
  61557. }
  61558. return EXPECT_RESULT();
  61559. }
  61560. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  61561. {
  61562. EXPECT_DECLS;
  61563. switch (test_TLS_13_ticket_different_ciphers_run) {
  61564. case 0:
  61565. /* First run */
  61566. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  61567. wolfSSL_get1_session(ssl));
  61568. break;
  61569. case 1:
  61570. /* Second run */
  61571. ExpectTrue(wolfSSL_session_reused(ssl));
  61572. break;
  61573. default:
  61574. /* Bad state? */
  61575. Fail(("Should not enter here"), ("Should not enter here"));
  61576. }
  61577. return EXPECT_RESULT();
  61578. }
  61579. static int test_TLS_13_ticket_different_ciphers(void)
  61580. {
  61581. EXPECT_DECLS;
  61582. /* Check that we handle the connection when the ticket doesn't match
  61583. * the first ciphersuite. */
  61584. test_ssl_cbf client_cbs, server_cbs;
  61585. struct test_params {
  61586. method_provider client_meth;
  61587. method_provider server_meth;
  61588. int doUdp;
  61589. } params[] = {
  61590. #ifdef WOLFSSL_DTLS13
  61591. /* Test that the stateless code handles sessions correctly */
  61592. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  61593. #endif
  61594. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  61595. };
  61596. size_t i;
  61597. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  61598. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61599. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61600. test_TLS_13_ticket_different_ciphers_run = 0;
  61601. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  61602. client_cbs.method = params[i].client_meth;
  61603. server_cbs.method = params[i].server_meth;
  61604. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  61605. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  61606. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  61607. server_cbs.ticNoInit = 1;
  61608. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  61609. &server_cbs, NULL), TEST_SUCCESS);
  61610. test_TLS_13_ticket_different_ciphers_run++;
  61611. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  61612. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  61613. &server_cbs, NULL), TEST_SUCCESS);
  61614. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  61615. test_TLS_13_ticket_different_ciphers_session = NULL;
  61616. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  61617. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  61618. }
  61619. return EXPECT_RESULT();
  61620. }
  61621. #else
  61622. static int test_TLS_13_ticket_different_ciphers(void)
  61623. {
  61624. return TEST_SKIPPED;
  61625. }
  61626. #endif
  61627. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  61628. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  61629. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  61630. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  61631. byte test_extra_alerts_wrong_cs_sh[] = {
  61632. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  61633. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  61634. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  61635. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  61636. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  61637. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  61638. 0xbb, 0x07, 0x54, 0x1c,
  61639. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  61640. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  61641. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  61642. };
  61643. static int test_extra_alerts_wrong_cs(void)
  61644. {
  61645. EXPECT_DECLS;
  61646. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  61647. struct test_memio_ctx test_ctx;
  61648. WOLFSSL_CTX *ctx_c = NULL;
  61649. WOLFSSL_ALERT_HISTORY h;
  61650. WOLFSSL *ssl_c = NULL;
  61651. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61652. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61653. wolfTLSv1_2_client_method, NULL), 0);
  61654. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  61655. WOLFSSL_SUCCESS);
  61656. /* CH */
  61657. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61658. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61659. WOLFSSL_ERROR_WANT_READ);
  61660. /* consume CH */
  61661. test_ctx.s_len = 0;
  61662. /* inject SH */
  61663. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  61664. sizeof(test_extra_alerts_wrong_cs_sh));
  61665. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  61666. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61667. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61668. WOLFSSL_ERROR_WANT_READ);
  61669. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  61670. ExpectIntEQ(h.last_tx.code, handshake_failure);
  61671. ExpectIntEQ(h.last_tx.level, alert_fatal);
  61672. wolfSSL_free(ssl_c);
  61673. wolfSSL_CTX_free(ctx_c);
  61674. #endif
  61675. return EXPECT_RESULT();
  61676. }
  61677. #else
  61678. static int test_extra_alerts_wrong_cs(void)
  61679. {
  61680. return TEST_SKIPPED;
  61681. }
  61682. #endif
  61683. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  61684. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  61685. #define TEST_CS_DOWNGRADE_CLIENT "ECDHE-RSA-AES256-GCM-SHA384"
  61686. byte test_wrong_cs_downgrade_sh[] = {
  61687. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0x10,
  61688. 0x2c, 0x88, 0xd9, 0x7a, 0x23, 0xc9, 0xbd, 0x11, 0x3b, 0x64, 0x24, 0xab,
  61689. 0x5b, 0x45, 0x33, 0xf6, 0x2c, 0x34, 0xe4, 0xcf, 0xf4, 0x78, 0xc8, 0x62,
  61690. 0x06, 0xc7, 0xe5, 0x30, 0x39, 0xbf, 0xa1, 0x20, 0xa3, 0x06, 0x74, 0xc3,
  61691. 0xa9, 0x74, 0x52, 0x8a, 0xfb, 0xae, 0xf0, 0xd8, 0x6f, 0xb2, 0x9d, 0xfe,
  61692. 0x78, 0xf0, 0x3f, 0x51, 0x8f, 0x9c, 0xcf, 0xbe, 0x61, 0x43, 0x9d, 0xf8,
  61693. 0x85, 0xe5, 0x2f, 0x54,
  61694. 0xc0, 0x2f, /* ECDHE-RSA-AES128-GCM-SHA256 */
  61695. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  61696. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  61697. };
  61698. static int test_wrong_cs_downgrade(void)
  61699. {
  61700. EXPECT_DECLS;
  61701. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  61702. struct test_memio_ctx test_ctx;
  61703. WOLFSSL_CTX *ctx_c = NULL;
  61704. WOLFSSL *ssl_c = NULL;
  61705. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61706. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61707. wolfSSLv23_client_method, NULL), 0);
  61708. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_CS_DOWNGRADE_CLIENT),
  61709. WOLFSSL_SUCCESS);
  61710. /* CH */
  61711. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61712. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61713. WOLFSSL_ERROR_WANT_READ);
  61714. /* consume CH */
  61715. test_ctx.s_len = 0;
  61716. /* inject SH */
  61717. XMEMCPY(test_ctx.c_buff, test_wrong_cs_downgrade_sh,
  61718. sizeof(test_wrong_cs_downgrade_sh));
  61719. test_ctx.c_len = sizeof(test_wrong_cs_downgrade_sh);
  61720. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61721. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61722. MATCH_SUITE_ERROR);
  61723. wolfSSL_free(ssl_c);
  61724. wolfSSL_CTX_free(ctx_c);
  61725. #endif
  61726. return EXPECT_RESULT();
  61727. }
  61728. #else
  61729. static int test_wrong_cs_downgrade(void)
  61730. {
  61731. return TEST_SKIPPED;
  61732. }
  61733. #endif
  61734. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  61735. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  61736. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  61737. {
  61738. tail_len -= msg_length;
  61739. XMEMMOVE(msg, msg + msg_length, tail_len);
  61740. *len = *len - msg_length;
  61741. }
  61742. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  61743. byte *found)
  61744. {
  61745. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  61746. const unsigned int _RECORD_HEADER_SZ = 5;
  61747. const int _change_cipher_hs = 55;
  61748. const int _change_cipher = 20;
  61749. const int _handshake = 22;
  61750. unsigned int tail_len;
  61751. byte *idx, *curr;
  61752. word8 currType;
  61753. word16 rLength;
  61754. word32 hLength;
  61755. idx = buf;
  61756. tail_len = (unsigned int)*len;
  61757. *found = 0;
  61758. while (tail_len > _RECORD_HEADER_SZ) {
  61759. curr = idx;
  61760. currType = *idx;
  61761. ato16(idx + 3, &rLength);
  61762. idx += _RECORD_HEADER_SZ;
  61763. tail_len -= _RECORD_HEADER_SZ;
  61764. if (tail_len < rLength)
  61765. return -1;
  61766. if (type == _change_cipher_hs && currType == _change_cipher) {
  61767. if (rLength != 1)
  61768. return -1;
  61769. /* match */
  61770. test_remove_msg(curr, *len - (int)(curr - buf),
  61771. len, _RECORD_HEADER_SZ + 1);
  61772. *found = 1;
  61773. return 0;
  61774. }
  61775. if (currType != _handshake) {
  61776. idx += rLength;
  61777. tail_len -= rLength;
  61778. continue;
  61779. }
  61780. if (rLength < _HANDSHAKE_HEADER_SZ)
  61781. return -1;
  61782. currType = *idx;
  61783. ato24(idx+1, &hLength);
  61784. hLength += _HANDSHAKE_HEADER_SZ;
  61785. if (tail_len < hLength)
  61786. return -1;
  61787. if (currType != type) {
  61788. idx += hLength;
  61789. tail_len -= hLength;
  61790. continue;
  61791. }
  61792. /* match */
  61793. test_remove_msg(curr, *len - (int)(curr - buf), len,
  61794. hLength + _RECORD_HEADER_SZ);
  61795. *found = 1;
  61796. return 0;
  61797. }
  61798. /* not found */
  61799. return 0;
  61800. }
  61801. static int test_remove_hs_message(byte hs_message_type,
  61802. int extra_round, byte alert_type)
  61803. {
  61804. EXPECT_DECLS;
  61805. WOLFSSL_CTX *ctx_c = NULL;
  61806. WOLFSSL_CTX *ctx_s = NULL;
  61807. WOLFSSL *ssl_c = NULL;
  61808. WOLFSSL *ssl_s = NULL;
  61809. struct test_memio_ctx test_ctx;
  61810. WOLFSSL_ALERT_HISTORY h;
  61811. byte found = 0;
  61812. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61813. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61814. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  61815. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61816. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61817. WOLFSSL_ERROR_WANT_READ);
  61818. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61819. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61820. WOLFSSL_ERROR_WANT_READ);
  61821. if (extra_round) {
  61822. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61823. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61824. WOLFSSL_ERROR_WANT_READ);
  61825. /* this will complete handshake from server side */
  61826. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61827. }
  61828. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  61829. &test_ctx.c_len, hs_message_type, &found), 0);
  61830. if (!found) {
  61831. wolfSSL_free(ssl_c);
  61832. wolfSSL_CTX_free(ctx_c);
  61833. wolfSSL_free(ssl_s);
  61834. wolfSSL_CTX_free(ctx_s);
  61835. return TEST_SKIPPED;
  61836. }
  61837. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61838. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61839. WOLFSSL_ERROR_WANT_READ);
  61840. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  61841. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  61842. ExpectIntEQ(h.last_tx.level, alert_fatal);
  61843. wolfSSL_free(ssl_c);
  61844. wolfSSL_CTX_free(ctx_c);
  61845. wolfSSL_free(ssl_s);
  61846. wolfSSL_CTX_free(ctx_s);
  61847. return EXPECT_RESULT();
  61848. }
  61849. static int test_extra_alerts_skip_hs(void)
  61850. {
  61851. EXPECT_DECLS;
  61852. const byte _server_key_exchange = 12;
  61853. const byte _server_hello = 2;
  61854. const byte _certificate = 11;
  61855. /* server_hello */
  61856. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  61857. unexpected_message), TEST_FAIL);
  61858. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  61859. 0xff), TEST_FAIL);
  61860. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  61861. unexpected_message), TEST_FAIL);
  61862. return EXPECT_RESULT();
  61863. }
  61864. #else
  61865. static int test_extra_alerts_skip_hs(void)
  61866. {
  61867. return TEST_SKIPPED;
  61868. }
  61869. #endif
  61870. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  61871. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  61872. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  61873. unsigned char* key, unsigned int key_max_len)
  61874. {
  61875. (void)ssl;
  61876. (void)id;
  61877. (void)key_max_len;
  61878. /* zero means error */
  61879. key[0] = 0x10;
  61880. return 1;
  61881. }
  61882. static int test_extra_alerts_bad_psk(void)
  61883. {
  61884. EXPECT_DECLS;
  61885. WOLFSSL_CTX *ctx_c = NULL;
  61886. WOLFSSL_CTX *ctx_s = NULL;
  61887. WOLFSSL *ssl_c = NULL;
  61888. WOLFSSL *ssl_s = NULL;
  61889. struct test_memio_ctx test_ctx;
  61890. WOLFSSL_ALERT_HISTORY h;
  61891. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61892. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61893. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  61894. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  61895. WOLFSSL_SUCCESS);
  61896. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  61897. WOLFSSL_SUCCESS);
  61898. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  61899. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61900. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61901. WOLFSSL_ERROR_WANT_READ);
  61902. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61903. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  61904. WOLFSSL_ERROR_WANT_READ);
  61905. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61906. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61907. WOLFSSL_ERROR_WANT_READ);
  61908. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  61909. ExpectIntEQ(h.last_tx.code, handshake_failure);
  61910. ExpectIntEQ(h.last_tx.level, alert_fatal);
  61911. wolfSSL_free(ssl_c);
  61912. wolfSSL_CTX_free(ctx_c);
  61913. wolfSSL_free(ssl_s);
  61914. wolfSSL_CTX_free(ctx_s);
  61915. return EXPECT_RESULT();
  61916. }
  61917. #else
  61918. static int test_extra_alerts_bad_psk(void)
  61919. {
  61920. return TEST_SKIPPED;
  61921. }
  61922. #endif
  61923. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  61924. && !defined(NO_PSK)
  61925. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  61926. const char* hint, char* identity, unsigned int id_max_len,
  61927. unsigned char* key, unsigned int key_max_len)
  61928. {
  61929. (void)ssl;
  61930. (void)hint;
  61931. (void)key_max_len;
  61932. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  61933. XSTRNCPY(identity, "Client_identity", id_max_len);
  61934. key[0] = 0x20;
  61935. return 1;
  61936. }
  61937. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  61938. const char* id, unsigned char* key, unsigned int key_max_len)
  61939. {
  61940. (void)ssl;
  61941. (void)id;
  61942. (void)key_max_len;
  61943. /* zero means error */
  61944. key[0] = 0x10;
  61945. return 1;
  61946. }
  61947. #endif
  61948. static int test_tls13_bad_psk_binder(void)
  61949. {
  61950. EXPECT_DECLS;
  61951. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  61952. && !defined(NO_PSK)
  61953. WOLFSSL_CTX *ctx_c = NULL;
  61954. WOLFSSL_CTX *ctx_s = NULL;
  61955. WOLFSSL *ssl_c = NULL;
  61956. WOLFSSL *ssl_s = NULL;
  61957. struct test_memio_ctx test_ctx;
  61958. WOLFSSL_ALERT_HISTORY h;
  61959. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61960. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61961. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61962. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  61963. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  61964. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61965. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61966. WOLFSSL_ERROR_WANT_READ);
  61967. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61968. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  61969. BAD_BINDER);
  61970. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61971. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61972. FATAL_ERROR);
  61973. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  61974. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  61975. ExpectIntEQ(h.last_rx.level, alert_fatal);
  61976. wolfSSL_free(ssl_c);
  61977. wolfSSL_CTX_free(ctx_c);
  61978. wolfSSL_free(ssl_s);
  61979. wolfSSL_CTX_free(ctx_s);
  61980. #endif
  61981. return EXPECT_RESULT();
  61982. }
  61983. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  61984. defined(HAVE_IO_TESTS_DEPENDENCIES)
  61985. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  61986. int sz, void *ctx)
  61987. {
  61988. static int sentServerHello = FALSE;
  61989. if (!sentServerHello) {
  61990. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  61991. size_t i;
  61992. if (sz < (int)sizeof(renegExt))
  61993. return WOLFSSL_CBIO_ERR_GENERAL;
  61994. /* Remove SCR from ServerHello */
  61995. for (i = 0; i < sz - sizeof(renegExt); i++) {
  61996. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  61997. /* Found the extension. Change it to something unrecognized. */
  61998. buf[i+1] = 0x11;
  61999. break;
  62000. }
  62001. }
  62002. sentServerHello = TRUE;
  62003. }
  62004. return EmbedSend(ssl, buf, sz, ctx);
  62005. }
  62006. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  62007. {
  62008. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  62009. }
  62010. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  62011. {
  62012. WOLFSSL_ALERT_HISTORY h;
  62013. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  62014. AssertIntEQ(h.last_rx.code, handshake_failure);
  62015. AssertIntEQ(h.last_rx.level, alert_fatal);
  62016. }
  62017. static int test_harden_no_secure_renegotiation(void)
  62018. {
  62019. EXPECT_DECLS;
  62020. callback_functions client_cbs, server_cbs;
  62021. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  62022. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  62023. client_cbs.method = wolfTLSv1_2_client_method;
  62024. server_cbs.method = wolfTLSv1_2_server_method;
  62025. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  62026. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  62027. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  62028. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  62029. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  62030. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  62031. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  62032. server_cbs.last_err == FATAL_ERROR);
  62033. return EXPECT_RESULT();
  62034. }
  62035. #else
  62036. static int test_harden_no_secure_renegotiation(void)
  62037. {
  62038. return TEST_SKIPPED;
  62039. }
  62040. #endif
  62041. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62042. static int test_override_alt_cert_chain_cert_cb(int preverify,
  62043. WOLFSSL_X509_STORE_CTX* store)
  62044. {
  62045. fprintf(stderr, "preverify: %d\n", preverify);
  62046. fprintf(stderr, "store->error: %d\n", store->error);
  62047. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  62048. if (store->error == OCSP_INVALID_STATUS) {
  62049. fprintf(stderr, "Overriding OCSP error\n");
  62050. return 1;
  62051. }
  62052. #ifndef WOLFSSL_ALT_CERT_CHAINS
  62053. else if ((store->error == ASN_NO_SIGNER_E ||
  62054. store->error == ASN_SELF_SIGNED_E
  62055. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  62056. defined(HAVE_WEBSERVER)
  62057. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  62058. #endif
  62059. ) && store->error_depth == store->totalCerts - 1) {
  62060. fprintf(stderr, "Overriding no signer error only for root cert\n");
  62061. return 1;
  62062. }
  62063. #endif
  62064. else
  62065. return preverify;
  62066. }
  62067. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  62068. int urlSz, unsigned char* request, int requestSz,
  62069. unsigned char** response)
  62070. {
  62071. (void)ioCtx;
  62072. (void)url;
  62073. (void)urlSz;
  62074. (void)request;
  62075. (void)requestSz;
  62076. (void)response;
  62077. return -1;
  62078. }
  62079. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  62080. {
  62081. EXPECT_DECLS;
  62082. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  62083. test_override_alt_cert_chain_cert_cb);
  62084. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  62085. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  62086. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  62087. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  62088. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  62089. WOLFSSL_SUCCESS);
  62090. return EXPECT_RESULT();
  62091. }
  62092. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  62093. {
  62094. EXPECT_DECLS;
  62095. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  62096. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  62097. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  62098. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  62099. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  62100. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  62101. WOLFSSL_SUCCESS);
  62102. return EXPECT_RESULT();
  62103. }
  62104. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  62105. {
  62106. EXPECT_DECLS;
  62107. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  62108. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  62109. return EXPECT_RESULT();
  62110. }
  62111. static int test_override_alt_cert_chain(void)
  62112. {
  62113. EXPECT_DECLS;
  62114. size_t i;
  62115. struct test_params {
  62116. ctx_cb client_ctx_cb;
  62117. ctx_cb server_ctx_cb;
  62118. int result;
  62119. } params[] = {
  62120. {test_override_alt_cert_chain_client_ctx_ready,
  62121. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  62122. {test_override_alt_cert_chain_client_ctx_ready2,
  62123. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  62124. };
  62125. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  62126. test_ssl_cbf client_cbs, server_cbs;
  62127. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  62128. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  62129. fprintf(stderr, "test config: %d\n", (int)i);
  62130. client_cbs.ctx_ready = params[i].client_ctx_cb;
  62131. server_cbs.ctx_ready = params[i].server_ctx_cb;
  62132. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  62133. &server_cbs, NULL), params[i].result);
  62134. ExpectIntEQ(client_cbs.return_code, params[i].result);
  62135. ExpectIntEQ(server_cbs.return_code, params[i].result);
  62136. }
  62137. return EXPECT_RESULT();
  62138. }
  62139. #else
  62140. static int test_override_alt_cert_chain(void)
  62141. {
  62142. return TEST_SKIPPED;
  62143. }
  62144. #endif
  62145. #if defined(HAVE_RPK)
  62146. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  62147. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  62148. #if defined(WOLFSSL_ALWAYS_VERIFY_CB) && defined(WOLFSSL_TLS13)
  62149. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  62150. {
  62151. int ret = WOLFSSL_SUCCESS;
  62152. (void)mode;
  62153. (void)strctx;
  62154. WOLFSSL_ENTER("MyRpkVerifyCb");
  62155. return ret;
  62156. }
  62157. #endif /* WOLFSSL_ALWAYS_VERIFY_CB && WOLFSSL_TLS13 */
  62158. static WC_INLINE int test_rpk_memio_setup(
  62159. struct test_memio_ctx *ctx,
  62160. WOLFSSL_CTX **ctx_c,
  62161. WOLFSSL_CTX **ctx_s,
  62162. WOLFSSL **ssl_c,
  62163. WOLFSSL **ssl_s,
  62164. method_provider method_c,
  62165. method_provider method_s,
  62166. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  62167. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  62168. const char* pkey_c, int fmt_kc, /* client private key and format */
  62169. const char* pkey_s, int fmt_ks /* server private key and format */
  62170. )
  62171. {
  62172. int ret;
  62173. if (ctx_c != NULL && *ctx_c == NULL) {
  62174. *ctx_c = wolfSSL_CTX_new(method_c());
  62175. if (*ctx_c == NULL) {
  62176. return -1;
  62177. }
  62178. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  62179. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  62180. if (ret != WOLFSSL_SUCCESS) {
  62181. return -1;
  62182. }
  62183. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  62184. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  62185. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  62186. if (ret != WOLFSSL_SUCCESS) {
  62187. return -1;
  62188. }
  62189. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  62190. if (ret != WOLFSSL_SUCCESS) {
  62191. return -1;
  62192. }
  62193. }
  62194. if (ctx_s != NULL && *ctx_s == NULL) {
  62195. *ctx_s = wolfSSL_CTX_new(method_s());
  62196. if (*ctx_s == NULL) {
  62197. return -1;
  62198. }
  62199. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  62200. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  62201. if (ret != WOLFSSL_SUCCESS) {
  62202. return -1;
  62203. }
  62204. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  62205. if (ret != WOLFSSL_SUCCESS) {
  62206. return -1;
  62207. }
  62208. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  62209. if (ret != WOLFSSL_SUCCESS) {
  62210. return -1;
  62211. }
  62212. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  62213. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  62214. if (ctx->s_ciphers != NULL) {
  62215. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  62216. if (ret != WOLFSSL_SUCCESS) {
  62217. return -1;
  62218. }
  62219. }
  62220. }
  62221. if (ctx_c != NULL && ssl_c != NULL) {
  62222. *ssl_c = wolfSSL_new(*ctx_c);
  62223. if (*ssl_c == NULL) {
  62224. return -1;
  62225. }
  62226. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  62227. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  62228. }
  62229. if (ctx_s != NULL && ssl_s != NULL) {
  62230. *ssl_s = wolfSSL_new(*ctx_s);
  62231. if (*ssl_s == NULL) {
  62232. return -1;
  62233. }
  62234. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  62235. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  62236. #if !defined(NO_DH)
  62237. SetDH(*ssl_s);
  62238. #endif
  62239. }
  62240. return 0;
  62241. }
  62242. #endif /* HAVE_RPK */
  62243. static int test_rpk_set_xxx_cert_type(void)
  62244. {
  62245. EXPECT_DECLS;
  62246. #if defined(HAVE_RPK)
  62247. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  62248. WOLFSSL_CTX* ctx = NULL;
  62249. WOLFSSL* ssl = NULL;
  62250. int tp;
  62251. ctx = wolfSSL_CTX_new(wolfTLS_client_method());
  62252. ExpectNotNull(ctx);
  62253. ssl = wolfSSL_new(ctx);
  62254. ExpectNotNull(ssl);
  62255. /*--------------------------------------------*/
  62256. /* tests for wolfSSL_CTX_set_client_cert_type */
  62257. /*--------------------------------------------*/
  62258. /* illegal parameter test caces */
  62259. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  62260. MAX_CLIENT_CERT_TYPE_CNT),
  62261. BAD_FUNC_ARG);
  62262. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62263. sizeof(ctype)),
  62264. BAD_FUNC_ARG);
  62265. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  62266. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  62267. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62268. MAX_CLIENT_CERT_TYPE_CNT),
  62269. BAD_FUNC_ARG);
  62270. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  62271. ctype[1] = 10; /* set unknown cert type */
  62272. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62273. MAX_CLIENT_CERT_TYPE_CNT),
  62274. BAD_FUNC_ARG);
  62275. /* pass larger type count */
  62276. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  62277. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  62278. ctype[2] = 1; /* pass unacceptable type count */
  62279. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62280. MAX_CLIENT_CERT_TYPE_CNT + 1),
  62281. BAD_FUNC_ARG);
  62282. /* should accept NULL for type buffer */
  62283. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  62284. MAX_CLIENT_CERT_TYPE_CNT),
  62285. WOLFSSL_SUCCESS);
  62286. /* should accept zero for type count */
  62287. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62288. 0),
  62289. WOLFSSL_SUCCESS);
  62290. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62291. MAX_CLIENT_CERT_TYPE_CNT),
  62292. WOLFSSL_SUCCESS);
  62293. /*--------------------------------------------*/
  62294. /* tests for wolfSSL_CTX_set_server_cert_type */
  62295. /*--------------------------------------------*/
  62296. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  62297. MAX_SERVER_CERT_TYPE_CNT),
  62298. BAD_FUNC_ARG);
  62299. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62300. sizeof(ctype)),
  62301. BAD_FUNC_ARG);
  62302. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  62303. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  62304. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62305. MAX_SERVER_CERT_TYPE_CNT),
  62306. BAD_FUNC_ARG);
  62307. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  62308. ctype[1] = 10; /* set unknown cert type */
  62309. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62310. MAX_SERVER_CERT_TYPE_CNT),
  62311. BAD_FUNC_ARG);
  62312. /* pass larger type count */
  62313. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  62314. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  62315. ctype[2] = 1; /* pass unacceptable type count */
  62316. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62317. MAX_SERVER_CERT_TYPE_CNT + 1),
  62318. BAD_FUNC_ARG);
  62319. /* should accept NULL for type buffer */
  62320. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  62321. MAX_SERVER_CERT_TYPE_CNT),
  62322. WOLFSSL_SUCCESS);
  62323. /* should accept zero for type count */
  62324. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62325. 0),
  62326. WOLFSSL_SUCCESS);
  62327. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62328. MAX_CLIENT_CERT_TYPE_CNT),
  62329. WOLFSSL_SUCCESS);
  62330. /*--------------------------------------------*/
  62331. /* tests for wolfSSL_set_client_cert_type */
  62332. /*--------------------------------------------*/
  62333. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  62334. MAX_CLIENT_CERT_TYPE_CNT),
  62335. BAD_FUNC_ARG);
  62336. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62337. sizeof(ctype)),
  62338. BAD_FUNC_ARG);
  62339. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  62340. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  62341. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62342. MAX_CLIENT_CERT_TYPE_CNT),
  62343. BAD_FUNC_ARG);
  62344. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  62345. ctype[1] = 10; /* set unknown cert type */
  62346. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62347. MAX_CLIENT_CERT_TYPE_CNT),
  62348. BAD_FUNC_ARG);
  62349. /* pass larger type count */
  62350. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  62351. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  62352. ctype[2] = 1; /* pass unacceptable type count */
  62353. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62354. MAX_CLIENT_CERT_TYPE_CNT + 1),
  62355. BAD_FUNC_ARG);
  62356. /* should accept NULL for type buffer */
  62357. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  62358. MAX_CLIENT_CERT_TYPE_CNT),
  62359. WOLFSSL_SUCCESS);
  62360. /* should accept zero for type count */
  62361. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62362. 0),
  62363. WOLFSSL_SUCCESS);
  62364. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62365. MAX_CLIENT_CERT_TYPE_CNT),
  62366. WOLFSSL_SUCCESS);
  62367. /*--------------------------------------------*/
  62368. /* tests for wolfSSL_CTX_set_server_cert_type */
  62369. /*--------------------------------------------*/
  62370. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  62371. MAX_SERVER_CERT_TYPE_CNT),
  62372. BAD_FUNC_ARG);
  62373. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62374. sizeof(ctype)),
  62375. BAD_FUNC_ARG);
  62376. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  62377. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  62378. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62379. MAX_SERVER_CERT_TYPE_CNT),
  62380. BAD_FUNC_ARG);
  62381. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  62382. ctype[1] = 10; /* set unknown cert type */
  62383. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62384. MAX_SERVER_CERT_TYPE_CNT),
  62385. BAD_FUNC_ARG);
  62386. /* pass larger type count */
  62387. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  62388. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  62389. ctype[2] = 1; /* pass unacceptable type count */
  62390. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62391. MAX_SERVER_CERT_TYPE_CNT + 1),
  62392. BAD_FUNC_ARG);
  62393. /* should accept NULL for type buffer */
  62394. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  62395. MAX_SERVER_CERT_TYPE_CNT),
  62396. WOLFSSL_SUCCESS);
  62397. /* should accept zero for type count */
  62398. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62399. 0),
  62400. WOLFSSL_SUCCESS);
  62401. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62402. MAX_SERVER_CERT_TYPE_CNT),
  62403. WOLFSSL_SUCCESS);
  62404. /*------------------------------------------------*/
  62405. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  62406. /*------------------------------------------------*/
  62407. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  62408. BAD_FUNC_ARG);
  62409. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  62410. BAD_FUNC_ARG);
  62411. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  62412. BAD_FUNC_ARG);
  62413. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  62414. BAD_FUNC_ARG);
  62415. /* clean up */
  62416. wolfSSL_free(ssl);
  62417. wolfSSL_CTX_free(ctx);
  62418. #endif
  62419. return EXPECT_RESULT();
  62420. }
  62421. static int test_tls13_rpk_handshake(void)
  62422. {
  62423. EXPECT_DECLS;
  62424. #if defined(HAVE_RPK) && (!defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13))
  62425. #ifdef WOLFSSL_TLS13
  62426. int ret = 0;
  62427. #endif
  62428. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62429. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62430. struct test_memio_ctx test_ctx;
  62431. int err;
  62432. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  62433. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  62434. int typeCnt_c;
  62435. int typeCnt_s;
  62436. int tp = 0;
  62437. #if defined(WOLFSSL_ALWAYS_VERIFY_CB) && defined(WOLFSSL_TLS13)
  62438. int isServer;
  62439. #endif
  62440. (void)err;
  62441. (void)typeCnt_c;
  62442. (void)typeCnt_s;
  62443. (void)certType_c;
  62444. (void)certType_s;
  62445. #ifndef WOLFSSL_NO_TLS12
  62446. /* TLS1.2
  62447. * Both client and server load x509 cert and start handshaking.
  62448. * Check no negotiation occurred.
  62449. */
  62450. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62451. ExpectIntEQ(
  62452. test_rpk_memio_setup(
  62453. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62454. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62455. cliCertFile, WOLFSSL_FILETYPE_PEM,
  62456. svrCertFile, WOLFSSL_FILETYPE_PEM,
  62457. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62458. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  62459. , 0);
  62460. /* set client certificate type in client end */
  62461. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62462. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62463. typeCnt_c = 2;
  62464. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62465. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62466. typeCnt_s = 2;
  62467. /* both client and server do not call client/server_cert_type APIs,
  62468. * expecting default settings works and no negotiation performed.
  62469. */
  62470. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62471. return TEST_FAIL;
  62472. /* confirm no negotiation occurred */
  62473. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62474. WOLFSSL_SUCCESS);
  62475. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62476. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62477. WOLFSSL_SUCCESS);
  62478. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62479. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62480. WOLFSSL_SUCCESS);
  62481. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62482. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62483. WOLFSSL_SUCCESS);
  62484. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62485. (void)typeCnt_c;
  62486. (void)typeCnt_s;
  62487. wolfSSL_free(ssl_c);
  62488. wolfSSL_CTX_free(ctx_c);
  62489. wolfSSL_free(ssl_s);
  62490. wolfSSL_CTX_free(ctx_s);
  62491. ssl_c = ssl_s = NULL;
  62492. ctx_c = ctx_s = NULL;
  62493. #endif
  62494. #ifdef WOLFSSL_TLS13
  62495. /* Both client and server load x509 cert and start handshaking.
  62496. * Check no negotiation occurred.
  62497. */
  62498. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62499. ExpectIntEQ(
  62500. test_rpk_memio_setup(
  62501. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62502. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62503. cliCertFile, WOLFSSL_FILETYPE_PEM,
  62504. svrCertFile, WOLFSSL_FILETYPE_PEM,
  62505. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62506. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62507. , 0);
  62508. /* set client certificate type in client end */
  62509. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62510. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62511. typeCnt_c = 2;
  62512. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62513. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62514. typeCnt_s = 2;
  62515. /* both client and server do not call client/server_cert_type APIs,
  62516. * expecting default settings works and no negotiation performed.
  62517. */
  62518. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62519. return TEST_FAIL;
  62520. /* confirm no negotiation occurred */
  62521. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62522. WOLFSSL_SUCCESS);
  62523. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62524. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62525. WOLFSSL_SUCCESS);
  62526. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62527. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62528. WOLFSSL_SUCCESS);
  62529. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62530. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62531. WOLFSSL_SUCCESS);
  62532. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62533. (void)typeCnt_c;
  62534. (void)typeCnt_s;
  62535. wolfSSL_free(ssl_c);
  62536. wolfSSL_CTX_free(ctx_c);
  62537. wolfSSL_free(ssl_s);
  62538. wolfSSL_CTX_free(ctx_s);
  62539. ssl_c = ssl_s = NULL;
  62540. ctx_c = ctx_s = NULL;
  62541. /* Both client and server load RPK cert and start handshaking.
  62542. * Confirm negotiated cert types match as expected.
  62543. */
  62544. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62545. ExpectIntEQ(
  62546. test_rpk_memio_setup(
  62547. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62548. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62549. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62550. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62551. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62552. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62553. , 0);
  62554. /* set client certificate type in client end */
  62555. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62556. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62557. typeCnt_c = 2;
  62558. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62559. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62560. typeCnt_s = 2;
  62561. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62562. WOLFSSL_SUCCESS);
  62563. /* set server certificate type in client end */
  62564. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62565. WOLFSSL_SUCCESS);
  62566. /* set client certificate type in server end */
  62567. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62568. WOLFSSL_SUCCESS);
  62569. /* set server certificate type in server end */
  62570. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62571. WOLFSSL_SUCCESS);
  62572. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62573. return TEST_FAIL;
  62574. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62575. WOLFSSL_SUCCESS);
  62576. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62577. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62578. WOLFSSL_SUCCESS);
  62579. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62580. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62581. WOLFSSL_SUCCESS);
  62582. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62583. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62584. WOLFSSL_SUCCESS);
  62585. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62586. wolfSSL_free(ssl_c);
  62587. wolfSSL_CTX_free(ctx_c);
  62588. wolfSSL_free(ssl_s);
  62589. wolfSSL_CTX_free(ctx_s);
  62590. ssl_c = ssl_s = NULL;
  62591. ctx_c = ctx_s = NULL;
  62592. #endif
  62593. #ifndef WOLFSSL_NO_TLS12
  62594. /* TLS1.2
  62595. * Both client and server load RPK cert and start handshaking.
  62596. * Confirm negotiated cert types match as expected.
  62597. */
  62598. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62599. ExpectIntEQ(
  62600. test_rpk_memio_setup(
  62601. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62602. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62603. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62604. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62605. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62606. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62607. , 0);
  62608. /* set client certificate type in client end */
  62609. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62610. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62611. typeCnt_c = 2;
  62612. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62613. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62614. typeCnt_s = 2;
  62615. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62616. WOLFSSL_SUCCESS);
  62617. /* set server certificate type in client end */
  62618. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62619. WOLFSSL_SUCCESS);
  62620. /* set client certificate type in server end */
  62621. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62622. WOLFSSL_SUCCESS);
  62623. /* set server certificate type in server end */
  62624. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62625. WOLFSSL_SUCCESS);
  62626. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62627. return TEST_FAIL;
  62628. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62629. WOLFSSL_SUCCESS);
  62630. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62631. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62632. WOLFSSL_SUCCESS);
  62633. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62634. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62635. WOLFSSL_SUCCESS);
  62636. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62637. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62638. WOLFSSL_SUCCESS);
  62639. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62640. wolfSSL_free(ssl_c);
  62641. wolfSSL_CTX_free(ctx_c);
  62642. wolfSSL_free(ssl_s);
  62643. wolfSSL_CTX_free(ctx_s);
  62644. ssl_c = ssl_s = NULL;
  62645. ctx_c = ctx_s = NULL;
  62646. #endif
  62647. #ifdef WOLFSSL_TLS13
  62648. /* Both client and server load x509 cert.
  62649. * Have client call set_client_cert_type with both RPK and x509.
  62650. * This doesn't makes client add client cert type extension to ClientHello,
  62651. * since it does not load RPK cert actually.
  62652. */
  62653. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62654. ExpectIntEQ(
  62655. test_rpk_memio_setup(
  62656. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62657. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62658. cliCertFile, WOLFSSL_FILETYPE_PEM,
  62659. svrCertFile, WOLFSSL_FILETYPE_PEM,
  62660. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62661. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62662. , 0);
  62663. /* set client certificate type in client end
  62664. *
  62665. * client indicates both RPK and x509 certs are available but loaded RPK
  62666. * cert only. It does not have client add client-cert-type extension in CH.
  62667. */
  62668. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62669. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62670. typeCnt_c = 2;
  62671. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62672. WOLFSSL_SUCCESS);
  62673. /* client indicates both RPK and x509 certs are acceptable */
  62674. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62675. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62676. typeCnt_s = 2;
  62677. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62678. WOLFSSL_SUCCESS);
  62679. /* server indicates both RPK and x509 certs are acceptable */
  62680. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62681. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62682. typeCnt_c = 2;
  62683. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62684. WOLFSSL_SUCCESS);
  62685. /* server should indicate only RPK cert is available */
  62686. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62687. certType_s[1] = -1;
  62688. typeCnt_s = 1;
  62689. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62690. WOLFSSL_SUCCESS);
  62691. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62692. return TEST_FAIL;
  62693. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  62694. * be returned as cert type.
  62695. */
  62696. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62697. WOLFSSL_SUCCESS);
  62698. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62699. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62700. WOLFSSL_SUCCESS);
  62701. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62702. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62703. WOLFSSL_SUCCESS);
  62704. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62705. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62706. WOLFSSL_SUCCESS);
  62707. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62708. wolfSSL_free(ssl_c);
  62709. wolfSSL_CTX_free(ctx_c);
  62710. wolfSSL_free(ssl_s);
  62711. wolfSSL_CTX_free(ctx_s);
  62712. ssl_c = ssl_s = NULL;
  62713. ctx_c = ctx_s = NULL;
  62714. /* Have client load RPK cert and have server load x509 cert.
  62715. * Check the negotiation result from both ends.
  62716. */
  62717. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62718. ExpectIntEQ(
  62719. test_rpk_memio_setup(
  62720. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62721. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62722. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62723. svrCertFile, WOLFSSL_FILETYPE_PEM,
  62724. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62725. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62726. , 0);
  62727. /* have client tell to use RPK cert */
  62728. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62729. certType_c[1] = -1;
  62730. typeCnt_c = 1;
  62731. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62732. WOLFSSL_SUCCESS);
  62733. /* have client tell to accept both RPK and x509 cert */
  62734. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62735. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  62736. typeCnt_s = 2;
  62737. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62738. WOLFSSL_SUCCESS);
  62739. /* have server accept to both RPK and x509 cert */
  62740. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  62741. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  62742. typeCnt_c = 2;
  62743. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62744. WOLFSSL_SUCCESS);
  62745. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  62746. * end, expecting the default setting works.
  62747. */
  62748. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62749. return TEST_FAIL;
  62750. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62751. WOLFSSL_SUCCESS);
  62752. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62753. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62754. WOLFSSL_SUCCESS);
  62755. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62756. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62757. WOLFSSL_SUCCESS);
  62758. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62759. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62760. WOLFSSL_SUCCESS);
  62761. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62762. wolfSSL_free(ssl_c);
  62763. wolfSSL_CTX_free(ctx_c);
  62764. wolfSSL_free(ssl_s);
  62765. wolfSSL_CTX_free(ctx_s);
  62766. ssl_c = ssl_s = NULL;
  62767. ctx_c = ctx_s = NULL;
  62768. /* Have both client and server load RPK cert, however, have server
  62769. * indicate its cert type x509.
  62770. * Client is expected to detect the cert type mismatch then to send alert
  62771. * with "unsupported_certificate".
  62772. */
  62773. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62774. ExpectIntEQ(
  62775. test_rpk_memio_setup(
  62776. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62777. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62778. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62779. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  62780. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62781. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62782. , 0);
  62783. /* have client tell to use RPK cert */
  62784. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62785. certType_c[1] = -1;
  62786. typeCnt_c = 1;
  62787. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62788. WOLFSSL_SUCCESS);
  62789. /* have client tell to accept both RPK and x509 cert */
  62790. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62791. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  62792. typeCnt_s = 2;
  62793. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62794. WOLFSSL_SUCCESS);
  62795. /* have server accept to both RPK and x509 cert */
  62796. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  62797. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  62798. typeCnt_c = 2;
  62799. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62800. WOLFSSL_SUCCESS);
  62801. /* have server tell to use x509 cert intentionally. This will bring
  62802. * certificate type mismatch in client side.
  62803. */
  62804. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62805. certType_s[1] = -1;
  62806. typeCnt_s = 1;
  62807. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62808. WOLFSSL_SUCCESS);
  62809. /* expect client detect cert type mismatch then send Alert */
  62810. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  62811. if (ret != -1)
  62812. return TEST_FAIL;
  62813. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  62814. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62815. WOLFSSL_SUCCESS);
  62816. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62817. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62818. WOLFSSL_SUCCESS);
  62819. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62820. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62821. WOLFSSL_SUCCESS);
  62822. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62823. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62824. WOLFSSL_SUCCESS);
  62825. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62826. wolfSSL_free(ssl_c);
  62827. wolfSSL_CTX_free(ctx_c);
  62828. wolfSSL_free(ssl_s);
  62829. wolfSSL_CTX_free(ctx_s);
  62830. ssl_c = ssl_s = NULL;
  62831. ctx_c = ctx_s = NULL;
  62832. /* Have client load x509 cert and server load RPK cert,
  62833. * however, have client indicate its cert type RPK.
  62834. * Server is expected to detect the cert type mismatch then to send alert
  62835. * with "unsupported_certificate".
  62836. */
  62837. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62838. ExpectIntEQ(
  62839. test_rpk_memio_setup(
  62840. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62841. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62842. cliCertFile, WOLFSSL_FILETYPE_PEM,
  62843. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62844. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62845. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62846. , 0);
  62847. /* have client tell to use RPK cert intentionally */
  62848. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62849. certType_c[1] = -1;
  62850. typeCnt_c = 1;
  62851. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62852. WOLFSSL_SUCCESS);
  62853. /* have client tell to accept both RPK and x509 cert */
  62854. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62855. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  62856. typeCnt_s = 2;
  62857. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62858. WOLFSSL_SUCCESS);
  62859. /* have server accept to both RPK and x509 cert */
  62860. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  62861. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  62862. typeCnt_c = 2;
  62863. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62864. WOLFSSL_SUCCESS);
  62865. /* have server tell to use x509 cert intentionally. This will bring
  62866. * certificate type mismatch in client side.
  62867. */
  62868. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62869. certType_s[1] = -1;
  62870. typeCnt_s = 1;
  62871. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62872. WOLFSSL_SUCCESS);
  62873. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  62874. /* expect server detect cert type mismatch then send Alert */
  62875. ExpectIntNE(ret, 0);
  62876. err = wolfSSL_get_error(ssl_c, ret);
  62877. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  62878. /* client did not load RPK cert actually, so negotiation did not happen */
  62879. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62880. WOLFSSL_SUCCESS);
  62881. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62882. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62883. WOLFSSL_SUCCESS);
  62884. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62885. /* client did not load RPK cert actually, so negotiation did not happen */
  62886. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62887. WOLFSSL_SUCCESS);
  62888. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62889. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62890. WOLFSSL_SUCCESS);
  62891. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62892. wolfSSL_free(ssl_c);
  62893. wolfSSL_CTX_free(ctx_c);
  62894. wolfSSL_free(ssl_s);
  62895. wolfSSL_CTX_free(ctx_s);
  62896. ssl_c = ssl_s = NULL;
  62897. ctx_c = ctx_s = NULL;
  62898. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  62899. /* Both client and server load RPK cert and set certificate verify
  62900. * callbacks then start handshaking.
  62901. * Confirm both side can refer the peer's cert.
  62902. */
  62903. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62904. ExpectIntEQ(
  62905. test_rpk_memio_setup(
  62906. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62907. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62908. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62909. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62910. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62911. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62912. , 0);
  62913. /* set client certificate type in client end */
  62914. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62915. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62916. typeCnt_c = 2;
  62917. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62918. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62919. typeCnt_s = 2;
  62920. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62921. WOLFSSL_SUCCESS);
  62922. /* set server certificate type in client end */
  62923. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62924. WOLFSSL_SUCCESS);
  62925. /* set client certificate type in server end */
  62926. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62927. WOLFSSL_SUCCESS);
  62928. /* set server certificate type in server end */
  62929. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62930. WOLFSSL_SUCCESS);
  62931. /* set certificate verify callback to both client and server */
  62932. isServer = 0;
  62933. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  62934. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  62935. isServer = 1;
  62936. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  62937. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  62938. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  62939. if (ret != 0)
  62940. return TEST_FAIL;
  62941. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62942. WOLFSSL_SUCCESS);
  62943. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62944. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62945. WOLFSSL_SUCCESS);
  62946. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62947. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62948. WOLFSSL_SUCCESS);
  62949. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62950. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62951. WOLFSSL_SUCCESS);
  62952. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62953. wolfSSL_free(ssl_c);
  62954. wolfSSL_CTX_free(ctx_c);
  62955. wolfSSL_free(ssl_s);
  62956. wolfSSL_CTX_free(ctx_s);
  62957. ssl_c = ssl_s = NULL;
  62958. ctx_c = ctx_s = NULL;
  62959. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  62960. #endif /* WOLFSSL_TLS13 */
  62961. #endif /* HAVE_RPK && (!WOLFSSL_NO_TLS12 || WOLFSSL_TLS13) */
  62962. return EXPECT_RESULT();
  62963. }
  62964. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  62965. static int test_dtls13_bad_epoch_ch(void)
  62966. {
  62967. EXPECT_DECLS;
  62968. WOLFSSL_CTX *ctx_c = NULL;
  62969. WOLFSSL_CTX *ctx_s = NULL;
  62970. WOLFSSL *ssl_c = NULL;
  62971. WOLFSSL *ssl_s = NULL;
  62972. struct test_memio_ctx test_ctx;
  62973. const int EPOCH_OFF = 3;
  62974. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62975. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62976. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  62977. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  62978. * with just one message */
  62979. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  62980. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62981. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  62982. WOLFSSL_ERROR_WANT_READ);
  62983. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  62984. /* first CH should use epoch 0x0 */
  62985. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  62986. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  62987. /* change epoch to 2 */
  62988. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  62989. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  62990. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  62991. WOLFSSL_ERROR_WANT_READ);
  62992. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  62993. /* resend the CH */
  62994. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62995. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62996. wolfSSL_free(ssl_c);
  62997. wolfSSL_CTX_free(ctx_c);
  62998. wolfSSL_free(ssl_s);
  62999. wolfSSL_CTX_free(ctx_s);
  63000. return EXPECT_RESULT();
  63001. }
  63002. #else
  63003. static int test_dtls13_bad_epoch_ch(void)
  63004. {
  63005. return TEST_SKIPPED;
  63006. }
  63007. #endif
  63008. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  63009. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  63010. {
  63011. EXPECT_DECLS;
  63012. WOLFSSL_SESSION *sess = NULL;
  63013. /* Setup the session to avoid errors */
  63014. ssl->session->timeout = -1;
  63015. ssl->session->side = WOLFSSL_CLIENT_END;
  63016. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  63017. defined(HAVE_SESSION_TICKET))
  63018. ssl->session->version = ssl->version;
  63019. #endif
  63020. /* Force a short session ID to be sent */
  63021. ssl->session->sessionIDSz = 4;
  63022. #ifndef NO_SESSION_CACHE_REF
  63023. /* Allow the client cache to be used */
  63024. ssl->session->idLen = 4;
  63025. #endif
  63026. ssl->session->isSetup = 1;
  63027. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  63028. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  63029. return EXPECT_RESULT();
  63030. }
  63031. static int test_short_session_id(void)
  63032. {
  63033. EXPECT_DECLS;
  63034. test_ssl_cbf client_cbf;
  63035. test_ssl_cbf server_cbf;
  63036. size_t i;
  63037. struct {
  63038. method_provider client_meth;
  63039. method_provider server_meth;
  63040. const char* tls_version;
  63041. } params[] = {
  63042. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  63043. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  63044. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  63045. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  63046. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  63047. #ifdef WOLFSSL_DTLS13
  63048. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  63049. #endif
  63050. #endif
  63051. #ifndef WOLFSSL_NO_TLS12
  63052. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  63053. #ifdef WOLFSSL_DTLS
  63054. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  63055. #endif
  63056. #endif
  63057. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  63058. !defined(NO_DES3))
  63059. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  63060. #ifdef WOLFSSL_DTLS
  63061. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  63062. #endif
  63063. #endif
  63064. };
  63065. fprintf(stderr, "\n");
  63066. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  63067. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  63068. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  63069. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  63070. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  63071. client_cbf.method = params[i].client_meth;
  63072. server_cbf.method = params[i].server_meth;
  63073. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  63074. &server_cbf, NULL), TEST_SUCCESS);
  63075. }
  63076. return EXPECT_RESULT();
  63077. }
  63078. #else
  63079. static int test_short_session_id(void)
  63080. {
  63081. return TEST_SKIPPED;
  63082. }
  63083. #endif
  63084. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  63085. && defined(WOLFSSL_DTLS13)
  63086. static byte* test_find_string(const char *string,
  63087. byte *buf, int buf_size)
  63088. {
  63089. int string_size, i;
  63090. string_size = (int)XSTRLEN(string);
  63091. for (i = 0; i < buf_size - string_size - 1; i++) {
  63092. if (XSTRCMP((char*)&buf[i], string) == 0)
  63093. return &buf[i];
  63094. }
  63095. return NULL;
  63096. }
  63097. static int test_wolfSSL_dtls13_null_cipher(void)
  63098. {
  63099. EXPECT_DECLS;
  63100. WOLFSSL_CTX *ctx_c = NULL;
  63101. WOLFSSL_CTX *ctx_s = NULL;
  63102. WOLFSSL *ssl_c = NULL;
  63103. WOLFSSL *ssl_s = NULL;
  63104. struct test_memio_ctx test_ctx;
  63105. const char *test_str = "test";
  63106. int test_str_size;
  63107. byte buf[255], *ptr = NULL;
  63108. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63109. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  63110. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63111. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  63112. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63113. test_str_size = XSTRLEN("test") + 1;
  63114. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  63115. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  63116. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  63117. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  63118. /* check that the packet was sent cleartext */
  63119. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  63120. test_ctx.s_len));
  63121. if (ptr != NULL) {
  63122. /* modify the message */
  63123. *ptr = 'H';
  63124. /* bad messages should be ignored in DTLS */
  63125. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  63126. ExpectIntEQ(ssl_s->error, WANT_READ);
  63127. }
  63128. wolfSSL_free(ssl_c);
  63129. wolfSSL_free(ssl_s);
  63130. wolfSSL_CTX_free(ctx_c);
  63131. wolfSSL_CTX_free(ctx_s);
  63132. return TEST_SUCCESS;
  63133. }
  63134. #else
  63135. static int test_wolfSSL_dtls13_null_cipher(void)
  63136. {
  63137. return TEST_SKIPPED;
  63138. }
  63139. #endif
  63140. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63141. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63142. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  63143. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  63144. {
  63145. SOCKADDR_S peer;
  63146. XSOCKLENT len;
  63147. int ret;
  63148. XMEMSET((byte*)&peer, 0, sizeof(peer));
  63149. len = sizeof(peer);
  63150. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  63151. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  63152. return -1;
  63153. switch (peer.ss_family) {
  63154. #ifdef WOLFSSL_IPV6
  63155. case WOLFSSL_IP6: {
  63156. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  63157. break;
  63158. }
  63159. #endif /* WOLFSSL_IPV6 */
  63160. case WOLFSSL_IP4:
  63161. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  63162. break;
  63163. default:
  63164. return -1;
  63165. }
  63166. return 0;
  63167. }
  63168. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  63169. {
  63170. char buf[1] = {'t'};
  63171. SOCKADDR_IN_T addr;
  63172. int sock_fd;
  63173. word16 port;
  63174. int err;
  63175. (void)ssl;
  63176. (void)ctx;
  63177. if (ssl == NULL)
  63178. return -1;
  63179. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  63180. if (err != 0)
  63181. return -1;
  63182. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  63183. if (sock_fd == -1)
  63184. return -1;
  63185. build_addr(&addr, wolfSSLIP, port, 1, 0);
  63186. /* send a packet to the server. Being another socket, the kernel will ensure
  63187. * the source port will be different. */
  63188. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  63189. sizeof(addr));
  63190. close(sock_fd);
  63191. if (err == -1)
  63192. return -1;
  63193. return 0;
  63194. }
  63195. /* setup a SSL session but just after the handshake send a packet to the server
  63196. * with a source address different than the one of the connected client. The I/O
  63197. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  63198. * is done in test_dtls_msg_from_other_peer_cb */
  63199. static int test_dtls_msg_from_other_peer(void)
  63200. {
  63201. EXPECT_DECLS;
  63202. callback_functions client_cbs;
  63203. callback_functions server_cbs;
  63204. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  63205. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  63206. client_cbs.method = wolfDTLSv1_2_client_method;
  63207. server_cbs.method = wolfDTLSv1_2_server_method;
  63208. client_cbs.doUdp = 1;
  63209. server_cbs.doUdp = 1;
  63210. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  63211. test_dtls_msg_from_other_peer_cb);
  63212. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  63213. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  63214. return EXPECT_RESULT();
  63215. }
  63216. #else
  63217. static int test_dtls_msg_from_other_peer(void)
  63218. {
  63219. return TEST_SKIPPED;
  63220. }
  63221. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63222. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63223. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  63224. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  63225. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63226. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  63227. static int test_dtls_ipv6_check(void)
  63228. {
  63229. EXPECT_DECLS;
  63230. WOLFSSL_CTX *ctx_c = NULL;
  63231. WOLFSSL_CTX *ctx_s = NULL;
  63232. WOLFSSL *ssl_c = NULL;
  63233. WOLFSSL *ssl_s = NULL;
  63234. SOCKADDR_IN fake_addr6;
  63235. int sockfd = -1;
  63236. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  63237. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  63238. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  63239. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  63240. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  63241. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  63242. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  63243. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  63244. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  63245. /* mimic a sockaddr_in6 struct, this way we can't test without
  63246. * WOLFSSL_IPV6 */
  63247. fake_addr6.sin_family = WOLFSSL_IP6;
  63248. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  63249. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  63250. /* can't return error here, as the peer is opaque for wolfssl library at
  63251. * this point */
  63252. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  63253. WOLFSSL_SUCCESS);
  63254. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  63255. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  63256. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  63257. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  63258. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  63259. WOLFSSL_SUCCESS);
  63260. /* reuse the socket */
  63261. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  63262. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  63263. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  63264. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  63265. if (sockfd != -1)
  63266. close(sockfd);
  63267. wolfSSL_free(ssl_c);
  63268. wolfSSL_CTX_free(ctx_c);
  63269. wolfSSL_free(ssl_s);
  63270. wolfSSL_CTX_free(ctx_s);
  63271. return EXPECT_RESULT();
  63272. }
  63273. #else
  63274. static int test_dtls_ipv6_check(void)
  63275. {
  63276. return TEST_SKIPPED;
  63277. }
  63278. #endif
  63279. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63280. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  63281. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  63282. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  63283. {
  63284. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  63285. }
  63286. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  63287. {
  63288. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  63289. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  63290. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  63291. }
  63292. else {
  63293. char testMsg[] = "Message after SCR";
  63294. char msgBuf[sizeof(testMsg)];
  63295. int ret;
  63296. if (!wolfSSL_is_server(ssl)) {
  63297. AssertIntEQ(WOLFSSL_SUCCESS,
  63298. wolfSSL_set_session(ssl,
  63299. test_wolfSSL_SCR_after_resumption_session));
  63300. }
  63301. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  63302. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  63303. sizeof(testMsg));
  63304. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  63305. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  63306. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  63307. AssertIntEQ(ret, sizeof(msgBuf));
  63308. }
  63309. }
  63310. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  63311. {
  63312. AssertIntEQ(WOLFSSL_SUCCESS,
  63313. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  63314. }
  63315. static int test_wolfSSL_SCR_after_resumption(void)
  63316. {
  63317. EXPECT_DECLS;
  63318. callback_functions func_cb_client;
  63319. callback_functions func_cb_server;
  63320. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  63321. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  63322. func_cb_client.method = wolfTLSv1_2_client_method;
  63323. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  63324. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  63325. func_cb_server.method = wolfTLSv1_2_server_method;
  63326. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  63327. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63328. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63329. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63330. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  63331. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  63332. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63333. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63334. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63335. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  63336. return EXPECT_RESULT();
  63337. }
  63338. #else
  63339. static int test_wolfSSL_SCR_after_resumption(void)
  63340. {
  63341. return TEST_SKIPPED;
  63342. }
  63343. #endif
  63344. static int test_wolfSSL_configure_args(void)
  63345. {
  63346. EXPECT_DECLS;
  63347. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  63348. ExpectNotNull(wolfSSL_configure_args());
  63349. #endif
  63350. return EXPECT_RESULT();
  63351. }
  63352. static int test_dtls_no_extensions(void)
  63353. {
  63354. EXPECT_DECLS;
  63355. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  63356. !defined(WOLFSSL_NO_TLS12)
  63357. WOLFSSL *ssl_s = NULL;
  63358. WOLFSSL_CTX *ctx_s = NULL;
  63359. struct test_memio_ctx test_ctx;
  63360. const byte chNoExtensions[] = {
  63361. /* Handshake type */
  63362. 0x16,
  63363. /* Version */
  63364. 0xfe, 0xff,
  63365. /* Epoch */
  63366. 0x00, 0x00,
  63367. /* Seq number */
  63368. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63369. /* Length */
  63370. 0x00, 0x40,
  63371. /* CH type */
  63372. 0x01,
  63373. /* Length */
  63374. 0x00, 0x00, 0x34,
  63375. /* Msg Seq */
  63376. 0x00, 0x00,
  63377. /* Frag offset */
  63378. 0x00, 0x00, 0x00,
  63379. /* Frag length */
  63380. 0x00, 0x00, 0x34,
  63381. /* Version */
  63382. 0xfe, 0xff,
  63383. /* Random */
  63384. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  63385. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  63386. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  63387. /* Session Length */
  63388. 0x00,
  63389. /* Cookie Length */
  63390. 0x00,
  63391. /* CS Length */
  63392. 0x00, 0x0c,
  63393. /* CS */
  63394. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  63395. /* Comp Meths Length */
  63396. 0x01,
  63397. /* Comp Meths */
  63398. 0x00
  63399. /* And finally... no extensions */
  63400. };
  63401. int i;
  63402. #ifdef OPENSSL_EXTRA
  63403. int repeats = 2;
  63404. #else
  63405. int repeats = 1;
  63406. #endif
  63407. for (i = 0; i < repeats; i++) {
  63408. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63409. ssl_s = NULL;
  63410. ctx_s = NULL;
  63411. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  63412. NULL, wolfDTLS_server_method), 0);
  63413. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  63414. test_ctx.s_len = sizeof(chNoExtensions);
  63415. #ifdef OPENSSL_EXTRA
  63416. if (i > 0) {
  63417. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  63418. WOLFSSL_SUCCESS);
  63419. }
  63420. #endif
  63421. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  63422. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63423. /* Expecting a handshake msg. Either HVR or SH. */
  63424. ExpectIntGT(test_ctx.c_len, 0);
  63425. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  63426. wolfSSL_free(ssl_s);
  63427. wolfSSL_CTX_free(ctx_s);
  63428. }
  63429. #endif
  63430. return EXPECT_RESULT();
  63431. }
  63432. static int test_TLSX_CA_NAMES_bad_extension(void)
  63433. {
  63434. EXPECT_DECLS;
  63435. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  63436. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  63437. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  63438. defined(HAVE_NULL_CIPHER)
  63439. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  63440. * parse the CA Names extension. Otherwise it will return other non-related
  63441. * errors. If CA Names will be parsed in more configurations, that should
  63442. * be reflected in the macro guard above. */
  63443. WOLFSSL *ssl_c = NULL;
  63444. WOLFSSL_CTX *ctx_c = NULL;
  63445. struct test_memio_ctx test_ctx;
  63446. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  63447. const byte shBadCaNamesExt[] = {
  63448. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  63449. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  63450. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  63451. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  63452. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  63453. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  63454. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  63455. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  63456. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  63457. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  63458. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  63459. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  63460. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  63461. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  63462. };
  63463. const byte shBadCaNamesExt2[] = {
  63464. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  63465. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  63466. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  63467. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  63468. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  63469. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  63470. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  63471. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  63472. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  63473. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  63474. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  63475. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  63476. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  63477. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  63478. };
  63479. int i = 0;
  63480. for (i = 0; i < 2; i++) {
  63481. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63482. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  63483. wolfTLSv1_3_client_method, NULL), 0);
  63484. switch (i) {
  63485. case 0:
  63486. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  63487. sizeof(shBadCaNamesExt));
  63488. test_ctx.c_len = sizeof(shBadCaNamesExt);
  63489. break;
  63490. case 1:
  63491. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  63492. sizeof(shBadCaNamesExt2));
  63493. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  63494. break;
  63495. }
  63496. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63497. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  63498. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  63499. #else
  63500. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  63501. #endif
  63502. wolfSSL_free(ssl_c);
  63503. ssl_c = NULL;
  63504. wolfSSL_CTX_free(ctx_c);
  63505. ctx_c = NULL;
  63506. }
  63507. #endif
  63508. return EXPECT_RESULT();
  63509. }
  63510. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63511. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63512. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  63513. {
  63514. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  63515. WOLFSSL_SUCCESS);
  63516. }
  63517. static int test_dtls_1_0_hvr_downgrade(void)
  63518. {
  63519. EXPECT_DECLS;
  63520. callback_functions func_cb_client;
  63521. callback_functions func_cb_server;
  63522. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  63523. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  63524. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  63525. func_cb_client.method = wolfDTLS_client_method;
  63526. func_cb_server.method = wolfDTLSv1_2_server_method;
  63527. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  63528. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63529. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63530. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63531. return EXPECT_RESULT();
  63532. }
  63533. #else
  63534. static int test_dtls_1_0_hvr_downgrade(void)
  63535. {
  63536. EXPECT_DECLS;
  63537. return EXPECT_RESULT();
  63538. }
  63539. #endif
  63540. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  63541. defined(HAVE_SESSION_TICKET)
  63542. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  63543. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  63544. {
  63545. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  63546. AssertNotNull(test_session_ticket_no_id_session);
  63547. }
  63548. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  63549. {
  63550. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  63551. }
  63552. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  63553. {
  63554. test_session_ticket_no_id_session->sessionIDSz = 0;
  63555. AssertIntEQ(WOLFSSL_SUCCESS,
  63556. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  63557. }
  63558. static int test_session_ticket_no_id(void)
  63559. {
  63560. /* We are testing an expired (invalid crypto context in out case since the
  63561. * ctx changes) session ticket being sent with the session ID being 0
  63562. * length. */
  63563. EXPECT_DECLS;
  63564. callback_functions func_cb_client;
  63565. callback_functions func_cb_server;
  63566. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  63567. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  63568. func_cb_client.method = wolfTLSv1_2_client_method;
  63569. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  63570. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  63571. func_cb_server.method = wolfTLSv1_2_server_method;
  63572. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  63573. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63574. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63575. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63576. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  63577. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  63578. func_cb_client.method = wolfTLSv1_2_client_method;
  63579. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  63580. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  63581. func_cb_server.method = wolfTLSv1_2_server_method;
  63582. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  63583. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63584. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63585. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63586. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  63587. return EXPECT_RESULT();
  63588. }
  63589. #else
  63590. static int test_session_ticket_no_id(void)
  63591. {
  63592. EXPECT_DECLS;
  63593. return EXPECT_RESULT();
  63594. }
  63595. #endif
  63596. static int test_session_ticket_hs_update(void)
  63597. {
  63598. EXPECT_DECLS;
  63599. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  63600. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  63601. struct test_memio_ctx test_ctx;
  63602. struct test_memio_ctx test_ctx2;
  63603. struct test_memio_ctx test_ctx3;
  63604. WOLFSSL_CTX *ctx_c = NULL;
  63605. WOLFSSL_CTX *ctx_s = NULL;
  63606. WOLFSSL *ssl_c = NULL;
  63607. WOLFSSL *ssl_c2 = NULL;
  63608. WOLFSSL *ssl_c3 = NULL;
  63609. WOLFSSL *ssl_s = NULL;
  63610. WOLFSSL *ssl_s2 = NULL;
  63611. WOLFSSL *ssl_s3 = NULL;
  63612. WOLFSSL_SESSION *sess = NULL;
  63613. byte read_data[1];
  63614. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63615. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  63616. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  63617. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63618. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  63619. /* Generate tickets */
  63620. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63621. wolfSSL_SetLoggingPrefix("client");
  63622. /* Read the ticket msg */
  63623. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  63624. WOLFSSL_FATAL_ERROR);
  63625. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  63626. WOLFSSL_ERROR_WANT_READ);
  63627. wolfSSL_SetLoggingPrefix(NULL);
  63628. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  63629. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  63630. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  63631. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  63632. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  63633. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  63634. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  63635. wolfSSL_SetLoggingPrefix("client");
  63636. /* Exchange initial flights for the second connection */
  63637. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  63638. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  63639. WOLFSSL_ERROR_WANT_READ);
  63640. wolfSSL_SetLoggingPrefix(NULL);
  63641. wolfSSL_SetLoggingPrefix("server");
  63642. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  63643. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  63644. WOLFSSL_ERROR_WANT_READ);
  63645. wolfSSL_SetLoggingPrefix(NULL);
  63646. /* Complete third connection so that new tickets are exchanged */
  63647. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  63648. /* Read the ticket msg */
  63649. wolfSSL_SetLoggingPrefix("client");
  63650. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  63651. WOLFSSL_FATAL_ERROR);
  63652. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  63653. WOLFSSL_ERROR_WANT_READ);
  63654. wolfSSL_SetLoggingPrefix(NULL);
  63655. /* Complete second connection */
  63656. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  63657. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  63658. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  63659. wolfSSL_free(ssl_c);
  63660. wolfSSL_free(ssl_c2);
  63661. wolfSSL_free(ssl_c3);
  63662. wolfSSL_free(ssl_s);
  63663. wolfSSL_free(ssl_s2);
  63664. wolfSSL_free(ssl_s3);
  63665. wolfSSL_CTX_free(ctx_c);
  63666. wolfSSL_CTX_free(ctx_s);
  63667. wolfSSL_SESSION_free(sess);
  63668. #endif
  63669. return EXPECT_RESULT();
  63670. }
  63671. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63672. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  63673. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  63674. {
  63675. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  63676. WOLFSSL_SUCCESS);
  63677. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  63678. }
  63679. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  63680. {
  63681. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  63682. }
  63683. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  63684. {
  63685. char testMsg[] = "Message after SCR";
  63686. char msgBuf[sizeof(testMsg)];
  63687. if (wolfSSL_is_server(ssl)) {
  63688. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  63689. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  63690. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  63691. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  63692. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  63693. sizeof(testMsg));
  63694. }
  63695. else {
  63696. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  63697. sizeof(testMsg));
  63698. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  63699. }
  63700. }
  63701. static int test_dtls_downgrade_scr_server(void)
  63702. {
  63703. EXPECT_DECLS;
  63704. callback_functions func_cb_client;
  63705. callback_functions func_cb_server;
  63706. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  63707. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  63708. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  63709. func_cb_client.method = wolfDTLSv1_2_client_method;
  63710. func_cb_server.method = wolfDTLS_server_method;
  63711. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  63712. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  63713. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  63714. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  63715. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63716. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63717. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63718. return EXPECT_RESULT();
  63719. }
  63720. #else
  63721. static int test_dtls_downgrade_scr_server(void)
  63722. {
  63723. EXPECT_DECLS;
  63724. return EXPECT_RESULT();
  63725. }
  63726. #endif
  63727. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63728. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  63729. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  63730. {
  63731. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  63732. WOLFSSL_SUCCESS);
  63733. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  63734. }
  63735. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  63736. {
  63737. char testMsg[] = "Message after SCR";
  63738. char msgBuf[sizeof(testMsg)];
  63739. if (wolfSSL_is_server(ssl)) {
  63740. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  63741. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  63742. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  63743. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  63744. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  63745. sizeof(testMsg));
  63746. }
  63747. else {
  63748. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  63749. sizeof(testMsg));
  63750. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  63751. }
  63752. }
  63753. static int test_dtls_downgrade_scr(void)
  63754. {
  63755. EXPECT_DECLS;
  63756. callback_functions func_cb_client;
  63757. callback_functions func_cb_server;
  63758. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  63759. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  63760. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  63761. func_cb_client.method = wolfDTLS_client_method;
  63762. func_cb_server.method = wolfDTLSv1_2_server_method;
  63763. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  63764. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  63765. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  63766. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63767. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63768. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63769. return EXPECT_RESULT();
  63770. }
  63771. #else
  63772. static int test_dtls_downgrade_scr(void)
  63773. {
  63774. EXPECT_DECLS;
  63775. return EXPECT_RESULT();
  63776. }
  63777. #endif
  63778. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  63779. && !defined(WOLFSSL_NO_TLS12)
  63780. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  63781. char *data, int sz, void *ctx)
  63782. {
  63783. static int call_counter = 0;
  63784. call_counter++;
  63785. (void)ssl;
  63786. (void)data;
  63787. (void)sz;
  63788. (void)ctx;
  63789. switch (call_counter) {
  63790. case 1:
  63791. case 2:
  63792. return WOLFSSL_CBIO_ERR_TIMEOUT;
  63793. case 3:
  63794. return WOLFSSL_CBIO_ERR_WANT_READ;
  63795. default:
  63796. AssertIntLE(call_counter, 3);
  63797. return -1;
  63798. }
  63799. }
  63800. #endif
  63801. /* Make sure we don't send acks before getting a server hello */
  63802. static int test_dtls_client_hello_timeout_downgrade(void)
  63803. {
  63804. EXPECT_DECLS;
  63805. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  63806. && !defined(WOLFSSL_NO_TLS12)
  63807. WOLFSSL_CTX *ctx_c = NULL;
  63808. WOLFSSL_CTX *ctx_s = NULL;
  63809. WOLFSSL *ssl_c = NULL;
  63810. WOLFSSL *ssl_s = NULL;
  63811. struct test_memio_ctx test_ctx;
  63812. DtlsRecordLayerHeader* dtlsRH;
  63813. size_t len;
  63814. byte sequence_number[8];
  63815. int i;
  63816. for (i = 0; i < 2; i++) {
  63817. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63818. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63819. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  63820. if (i == 0) {
  63821. /* First time simulate timeout in IO layer */
  63822. /* CH1 */
  63823. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63824. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63825. /* HVR */
  63826. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63827. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63828. /* CH2 */
  63829. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63830. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63831. /* SH flight */
  63832. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63833. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63834. /* Drop the SH */
  63835. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  63836. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63837. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  63838. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  63839. (sizeof(DtlsRecordLayerHeader) + len));
  63840. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  63841. /* Read the remainder of the flight */
  63842. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63843. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63844. wolfSSL_SSLSetIORecv(ssl_c,
  63845. test_dtls_client_hello_timeout_downgrade_read_cb);
  63846. /* CH3 */
  63847. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63848. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63849. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  63850. }
  63851. else {
  63852. /* Second time call wolfSSL_dtls_got_timeout */
  63853. /* CH1 */
  63854. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63855. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63856. /* HVR */
  63857. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63858. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63859. /* CH2 */
  63860. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63861. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63862. /* SH flight */
  63863. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63864. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63865. /* Drop the SH */
  63866. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  63867. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63868. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  63869. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  63870. (sizeof(DtlsRecordLayerHeader) + len));
  63871. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  63872. /* Read the remainder of the flight */
  63873. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63874. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63875. /* Quick timeout should be set as we received at least one msg */
  63876. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  63877. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  63878. /* Quick timeout should be cleared after a quick timeout */
  63879. /* CH3 */
  63880. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  63881. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  63882. }
  63883. /* Parse out to make sure we got exactly one ClientHello message */
  63884. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  63885. /* Second ClientHello after HVR */
  63886. sequence_number[7] = 2;
  63887. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  63888. ExpectIntEQ(dtlsRH->type, handshake);
  63889. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  63890. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  63891. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  63892. sizeof(sequence_number)), 0);
  63893. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63894. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  63895. /* Connection should be able to continue */
  63896. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63897. wolfSSL_free(ssl_c);
  63898. wolfSSL_free(ssl_s);
  63899. wolfSSL_CTX_free(ctx_c);
  63900. wolfSSL_CTX_free(ctx_s);
  63901. ssl_c = NULL;
  63902. ssl_s = NULL;
  63903. ctx_c = NULL;
  63904. ctx_s = NULL;
  63905. if (!EXPECT_SUCCESS())
  63906. break;
  63907. }
  63908. #endif
  63909. return EXPECT_RESULT();
  63910. }
  63911. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  63912. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  63913. int sz, void *ctx)
  63914. {
  63915. static int call_counter = 0;
  63916. call_counter++;
  63917. (void)ssl;
  63918. (void)data;
  63919. (void)sz;
  63920. (void)ctx;
  63921. switch (call_counter) {
  63922. case 1:
  63923. return WOLFSSL_CBIO_ERR_TIMEOUT;
  63924. case 2:
  63925. return WOLFSSL_CBIO_ERR_WANT_READ;
  63926. default:
  63927. AssertIntLE(call_counter, 2);
  63928. return -1;
  63929. }
  63930. }
  63931. #endif
  63932. /* Make sure we don't send acks before getting a server hello */
  63933. static int test_dtls_client_hello_timeout(void)
  63934. {
  63935. EXPECT_DECLS;
  63936. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  63937. WOLFSSL *ssl_c = NULL;
  63938. WOLFSSL_CTX *ctx_c = NULL;
  63939. struct test_memio_ctx test_ctx;
  63940. DtlsRecordLayerHeader* dtlsRH;
  63941. size_t idx;
  63942. size_t len;
  63943. byte sequence_number[8];
  63944. int i;
  63945. for (i = 0; i < 2; i++) {
  63946. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63947. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  63948. wolfDTLSv1_3_client_method, NULL), 0);
  63949. if (i == 0) {
  63950. /* First time simulate timeout in IO layer */
  63951. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  63952. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63953. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63954. }
  63955. else {
  63956. /* Second time call wolfSSL_dtls_got_timeout */
  63957. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63958. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63959. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  63960. }
  63961. /* Parse out to make sure we got exactly two ClientHello messages */
  63962. idx = 0;
  63963. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  63964. /* First ClientHello */
  63965. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  63966. ExpectIntEQ(dtlsRH->type, handshake);
  63967. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  63968. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  63969. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  63970. sizeof(sequence_number)), 0);
  63971. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63972. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  63973. idx += sizeof(DtlsRecordLayerHeader) + len;
  63974. /* Second ClientHello */
  63975. sequence_number[7] = 1;
  63976. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  63977. ExpectIntEQ(dtlsRH->type, handshake);
  63978. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  63979. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  63980. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  63981. sizeof(sequence_number)), 0);
  63982. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63983. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  63984. wolfSSL_free(ssl_c);
  63985. wolfSSL_CTX_free(ctx_c);
  63986. ssl_c = NULL;
  63987. ctx_c = NULL;
  63988. if (!EXPECT_SUCCESS())
  63989. break;
  63990. }
  63991. #endif
  63992. return EXPECT_RESULT();
  63993. }
  63994. /* DTLS test when dropping the changed cipher spec message */
  63995. static int test_dtls_dropped_ccs(void)
  63996. {
  63997. EXPECT_DECLS;
  63998. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  63999. && !defined(WOLFSSL_NO_TLS12)
  64000. WOLFSSL_CTX *ctx_c = NULL;
  64001. WOLFSSL_CTX *ctx_s = NULL;
  64002. WOLFSSL *ssl_c = NULL;
  64003. WOLFSSL *ssl_s = NULL;
  64004. struct test_memio_ctx test_ctx;
  64005. DtlsRecordLayerHeader* dtlsRH;
  64006. size_t len;
  64007. byte data[1];
  64008. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64009. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64010. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  64011. /* CH1 */
  64012. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64013. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64014. /* HVR */
  64015. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64016. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64017. /* CH2 */
  64018. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64019. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64020. /* Server first flight */
  64021. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64022. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64023. /* Client flight */
  64024. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64025. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64026. /* Server ccs + finished */
  64027. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  64028. /* Drop the ccs */
  64029. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  64030. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  64031. ExpectIntEQ(len, 1);
  64032. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  64033. if (EXPECT_SUCCESS()) {
  64034. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  64035. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  64036. (sizeof(DtlsRecordLayerHeader) + len));
  64037. }
  64038. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  64039. /* Client rtx flight */
  64040. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64041. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64042. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  64043. /* Server ccs + finished rtx */
  64044. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  64045. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64046. /* Client processes finished */
  64047. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  64048. wolfSSL_free(ssl_c);
  64049. wolfSSL_free(ssl_s);
  64050. wolfSSL_CTX_free(ctx_c);
  64051. wolfSSL_CTX_free(ctx_s);
  64052. #endif
  64053. return EXPECT_RESULT();
  64054. }
  64055. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  64056. && !defined(WOLFSSL_NO_TLS12)
  64057. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  64058. byte seq_num)
  64059. {
  64060. EXPECT_DECLS;
  64061. DtlsRecordLayerHeader* dtlsRH;
  64062. byte sequence_number[8];
  64063. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  64064. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  64065. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  64066. ExpectIntEQ(dtlsRH->type, handshake);
  64067. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  64068. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  64069. sequence_number[7] = seq_num;
  64070. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  64071. sizeof(sequence_number)), 0);
  64072. return EXPECT_RESULT();
  64073. }
  64074. #endif
  64075. /*
  64076. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  64077. * and after a HelloRetryRequest. This is testing the server side as it is
  64078. * operating statelessly and should copy the sequence number of the ClientHello.
  64079. */
  64080. static int test_dtls_seq_num_downgrade(void)
  64081. {
  64082. EXPECT_DECLS;
  64083. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  64084. && !defined(WOLFSSL_NO_TLS12)
  64085. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  64086. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  64087. struct test_memio_ctx test_ctx;
  64088. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64089. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64090. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  64091. /* CH1 */
  64092. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64093. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64094. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  64095. test_ctx.s_len, 0), TEST_SUCCESS);
  64096. /* HVR */
  64097. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64098. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64099. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  64100. test_ctx.c_len, 0), TEST_SUCCESS);
  64101. /* CH2 */
  64102. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64103. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64104. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  64105. test_ctx.s_len, 1), TEST_SUCCESS);
  64106. /* Server first flight */
  64107. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64108. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64109. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  64110. test_ctx.c_len, 1), TEST_SUCCESS);
  64111. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64112. wolfSSL_free(ssl_c);
  64113. wolfSSL_CTX_free(ctx_c);
  64114. wolfSSL_free(ssl_s);
  64115. wolfSSL_CTX_free(ctx_s);
  64116. #endif
  64117. return EXPECT_RESULT();
  64118. }
  64119. /**
  64120. * Make sure we don't send RSA Signature Hash Algorithms in the
  64121. * CertificateRequest when we don't have any such ciphers set.
  64122. * @return EXPECT_RESULT()
  64123. */
  64124. static int test_certreq_sighash_algos(void)
  64125. {
  64126. EXPECT_DECLS;
  64127. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  64128. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  64129. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  64130. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  64131. WOLFSSL_CTX *ctx_c = NULL;
  64132. WOLFSSL_CTX *ctx_s = NULL;
  64133. WOLFSSL *ssl_c = NULL;
  64134. WOLFSSL *ssl_s = NULL;
  64135. struct test_memio_ctx test_ctx;
  64136. int idx = 0;
  64137. int maxIdx = 0;
  64138. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64139. test_ctx.c_ciphers = test_ctx.s_ciphers =
  64140. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  64141. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64142. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  64143. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  64144. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  64145. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  64146. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  64147. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64148. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  64149. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64150. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  64151. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  64152. WOLFSSL_ERROR_WANT_READ);
  64153. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  64154. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  64155. WOLFSSL_ERROR_WANT_READ);
  64156. /* Find the CertificateRequest message */
  64157. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  64158. word16 len;
  64159. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  64160. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  64161. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  64162. ato16(test_ctx.c_buff + idx, &len);
  64163. idx += OPAQUE16_LEN;
  64164. if (test_ctx.c_buff[idx] == certificate_request) {
  64165. idx++;
  64166. /* length */
  64167. idx += OPAQUE24_LEN;
  64168. /* cert types */
  64169. idx += 1 + test_ctx.c_buff[idx];
  64170. /* Sig algos */
  64171. ato16(test_ctx.c_buff + idx, &len);
  64172. idx += OPAQUE16_LEN;
  64173. maxIdx = idx + (int)len;
  64174. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  64175. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  64176. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  64177. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  64178. else
  64179. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  64180. }
  64181. break;
  64182. }
  64183. else {
  64184. idx += (int)len;
  64185. }
  64186. }
  64187. ExpectIntLT(idx, test_ctx.c_len);
  64188. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64189. wolfSSL_free(ssl_c);
  64190. wolfSSL_free(ssl_s);
  64191. wolfSSL_CTX_free(ctx_c);
  64192. wolfSSL_CTX_free(ctx_s);
  64193. #endif
  64194. return EXPECT_RESULT();
  64195. }
  64196. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  64197. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  64198. {
  64199. EXPECT_DECLS;
  64200. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  64201. myVerifyAction = VERIFY_USE_PREVERFIY;
  64202. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  64203. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  64204. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  64205. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  64206. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  64207. WOLFSSL_SUCCESS);
  64208. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  64209. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  64210. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64211. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  64212. "./certs/crl/ca-int.pem",
  64213. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64214. return EXPECT_RESULT();
  64215. }
  64216. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  64217. {
  64218. EXPECT_DECLS;
  64219. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  64220. myVerifyAction = VERIFY_USE_PREVERFIY;
  64221. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  64222. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  64223. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  64224. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  64225. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  64226. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  64227. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  64228. WOLFSSL_SUCCESS);
  64229. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  64230. "./certs/crl/ca-int2.pem",
  64231. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64232. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  64233. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  64234. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64235. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  64236. "./certs/crl/ca-int.pem",
  64237. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64238. return EXPECT_RESULT();
  64239. }
  64240. #endif
  64241. static int test_revoked_loaded_int_cert(void)
  64242. {
  64243. EXPECT_DECLS;
  64244. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  64245. test_ssl_cbf client_cbf;
  64246. test_ssl_cbf server_cbf;
  64247. struct {
  64248. const char* certPemFile;
  64249. const char* keyPemFile;
  64250. ctx_cb client_ctx_ready;
  64251. } test_params[] = {
  64252. {"./certs/intermediate/ca-int2-cert.pem",
  64253. "./certs/intermediate/ca-int2-key.pem",
  64254. test_revoked_loaded_int_cert_ctx_ready1},
  64255. {"./certs/intermediate/server-chain.pem",
  64256. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  64257. {"./certs/intermediate/server-chain-short.pem",
  64258. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  64259. };
  64260. size_t i;
  64261. printf("\n");
  64262. for (i = 0; i < XELEM_CNT(test_params); i++) {
  64263. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  64264. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  64265. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  64266. server_cbf.certPemFile = test_params[i].certPemFile;
  64267. server_cbf.keyPemFile = test_params[i].keyPemFile;
  64268. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  64269. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  64270. &server_cbf, NULL), TEST_FAIL);
  64271. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  64272. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  64273. if (!EXPECT_SUCCESS())
  64274. break;
  64275. printf("\t%s passed\n", test_params[i].certPemFile);
  64276. }
  64277. #endif
  64278. return EXPECT_RESULT();
  64279. }
  64280. static int test_dtls13_frag_ch_pq(void)
  64281. {
  64282. EXPECT_DECLS;
  64283. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  64284. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  64285. WOLFSSL_CTX *ctx_c = NULL;
  64286. WOLFSSL_CTX *ctx_s = NULL;
  64287. WOLFSSL *ssl_c = NULL;
  64288. WOLFSSL *ssl_s = NULL;
  64289. struct test_memio_ctx test_ctx;
  64290. const char *test_str = "test";
  64291. int test_str_size;
  64292. byte buf[255];
  64293. int group = WOLFSSL_KYBER_LEVEL5;
  64294. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64295. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64296. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  64297. /* Add in a large post-quantum key share to make the CH long. */
  64298. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  64299. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  64300. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  64301. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64302. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  64303. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  64304. test_str_size = XSTRLEN("test") + 1;
  64305. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  64306. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  64307. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  64308. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  64309. wolfSSL_free(ssl_c);
  64310. wolfSSL_free(ssl_s);
  64311. wolfSSL_CTX_free(ctx_c);
  64312. wolfSSL_CTX_free(ctx_s);
  64313. #endif
  64314. return EXPECT_RESULT();
  64315. }
  64316. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  64317. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  64318. static int test_dtls_frag_ch_count_records(byte* b, int len)
  64319. {
  64320. DtlsRecordLayerHeader* dtlsRH;
  64321. int records = 0;
  64322. size_t recordLen;
  64323. while (len > 0) {
  64324. records++;
  64325. dtlsRH = (DtlsRecordLayerHeader*)b;
  64326. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  64327. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  64328. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  64329. }
  64330. return records;
  64331. }
  64332. #endif
  64333. static int test_dtls_frag_ch(void)
  64334. {
  64335. EXPECT_DECLS;
  64336. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  64337. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  64338. WOLFSSL_CTX *ctx_c = NULL;
  64339. WOLFSSL_CTX *ctx_s = NULL;
  64340. WOLFSSL *ssl_c = NULL;
  64341. WOLFSSL *ssl_s = NULL;
  64342. struct test_memio_ctx test_ctx;
  64343. static unsigned int DUMMY_MTU = 256;
  64344. unsigned char four_frag_CH[] = {
  64345. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64346. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64347. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  64348. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  64349. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  64350. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  64351. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  64352. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  64353. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  64354. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  64355. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  64356. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  64357. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  64358. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  64359. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  64360. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  64361. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  64362. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  64363. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  64364. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64365. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  64366. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  64367. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  64368. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  64369. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  64370. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  64371. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  64372. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  64373. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  64374. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  64375. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  64376. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  64377. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  64378. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  64379. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  64380. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  64381. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  64382. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  64383. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  64384. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  64385. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  64386. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  64387. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  64388. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  64389. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  64390. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  64391. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  64392. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  64393. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  64394. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  64395. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  64396. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  64397. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  64398. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  64399. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  64400. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  64401. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  64402. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  64403. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  64404. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  64405. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  64406. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  64407. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  64408. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  64409. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  64410. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  64411. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  64412. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  64413. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  64414. 0xa5, 0xcb, 0x68, 0xf3
  64415. };
  64416. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64417. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64418. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  64419. /* Fragment msgs */
  64420. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  64421. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  64422. /* Add in some key shares to make the CH long */
  64423. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  64424. WOLFSSL_SUCCESS);
  64425. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  64426. WOLFSSL_SUCCESS);
  64427. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  64428. WOLFSSL_SUCCESS);
  64429. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  64430. WOLFSSL_SUCCESS);
  64431. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  64432. /* Reject fragmented first CH */
  64433. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  64434. sizeof(four_frag_CH)), 4);
  64435. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  64436. test_ctx.s_len = sizeof(four_frag_CH);
  64437. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  64438. int s_len = test_ctx.s_len;
  64439. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64440. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64441. /* Fail if we didn't advance the buffer to avoid infinite loops */
  64442. ExpectIntLT(test_ctx.s_len, s_len);
  64443. }
  64444. /* Expect all fragments read */
  64445. ExpectIntEQ(test_ctx.s_len, 0);
  64446. /* Expect quietly dropping fragmented first CH */
  64447. ExpectIntEQ(test_ctx.c_len, 0);
  64448. /* CH1 */
  64449. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64450. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64451. /* Count records. Expect 1 unfragmented CH */
  64452. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  64453. test_ctx.s_len), 1);
  64454. /* HRR */
  64455. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64456. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64457. /* CH2 */
  64458. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64459. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64460. /* Count records. Expect fragmented CH */
  64461. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  64462. test_ctx.s_len), 1);
  64463. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64464. wolfSSL_free(ssl_c);
  64465. wolfSSL_free(ssl_s);
  64466. wolfSSL_CTX_free(ctx_c);
  64467. wolfSSL_CTX_free(ctx_s);
  64468. ssl_c = ssl_s = NULL;
  64469. ctx_c = ctx_s = NULL;
  64470. #endif
  64471. return EXPECT_RESULT();
  64472. }
  64473. static int test_dtls_empty_keyshare_with_cookie(void)
  64474. {
  64475. EXPECT_DECLS;
  64476. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  64477. WOLFSSL_CTX *ctx_s = NULL;
  64478. WOLFSSL *ssl_s = NULL;
  64479. struct test_memio_ctx test_ctx;
  64480. unsigned char ch_empty_keyshare_with_cookie[] = {
  64481. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  64482. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  64483. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  64484. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  64485. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  64486. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  64487. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  64488. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  64489. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  64490. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  64491. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  64492. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  64493. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  64494. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  64495. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  64496. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  64497. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  64498. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  64499. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  64500. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  64501. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  64502. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  64503. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  64504. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  64505. };
  64506. DtlsRecordLayerHeader* dtlsRH;
  64507. byte sequence_number[8];
  64508. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  64509. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64510. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  64511. sizeof(ch_empty_keyshare_with_cookie));
  64512. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  64513. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  64514. NULL, wolfDTLSv1_3_server_method), 0);
  64515. /* CH1 */
  64516. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64517. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64518. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  64519. ExpectIntEQ(test_ctx.c_len, 15);
  64520. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  64521. ExpectIntEQ(dtlsRH->type, alert);
  64522. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  64523. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  64524. sequence_number[7] = 1;
  64525. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  64526. sizeof(sequence_number)), 0);
  64527. ExpectIntEQ(dtlsRH->length[0], 0);
  64528. ExpectIntEQ(dtlsRH->length[1], 2);
  64529. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  64530. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  64531. wolfSSL_free(ssl_s);
  64532. wolfSSL_CTX_free(ctx_s);
  64533. #endif
  64534. return EXPECT_RESULT();
  64535. }
  64536. static int test_dtls_old_seq_number(void)
  64537. {
  64538. EXPECT_DECLS;
  64539. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  64540. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  64541. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  64542. struct test_memio_ctx test_ctx;
  64543. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64544. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64545. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  64546. /* CH1 */
  64547. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64548. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64549. /* HVR */
  64550. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64551. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64552. /* CH2 */
  64553. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64554. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64555. /* Server first flight */
  64556. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64557. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64558. /* Client second flight */
  64559. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64560. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64561. /* Modify the sequence number */
  64562. {
  64563. DtlsRecordLayerHeader* dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  64564. XMEMSET(dtlsRH->sequence_number, 0, sizeof(dtlsRH->sequence_number));
  64565. }
  64566. /* Server second flight */
  64567. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64568. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64569. /* Server should not do anything as a pkt was dropped */
  64570. ExpectIntEQ(test_ctx.c_len, 0);
  64571. ExpectIntEQ(test_ctx.s_len, 0);
  64572. /* Trigger rtx */
  64573. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  64574. /* Complete connection */
  64575. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64576. wolfSSL_free(ssl_c);
  64577. wolfSSL_CTX_free(ctx_c);
  64578. wolfSSL_free(ssl_s);
  64579. wolfSSL_CTX_free(ctx_s);
  64580. #endif
  64581. return EXPECT_RESULT();
  64582. }
  64583. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  64584. defined(HAVE_LIBOQS)
  64585. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  64586. {
  64587. int group = WOLFSSL_KYBER_LEVEL5;
  64588. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  64589. }
  64590. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  64591. {
  64592. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  64593. }
  64594. #endif
  64595. static int test_tls13_pq_groups(void)
  64596. {
  64597. EXPECT_DECLS;
  64598. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  64599. defined(HAVE_LIBOQS)
  64600. callback_functions func_cb_client;
  64601. callback_functions func_cb_server;
  64602. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  64603. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  64604. func_cb_client.method = wolfTLSv1_3_client_method;
  64605. func_cb_server.method = wolfTLSv1_3_server_method;
  64606. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  64607. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  64608. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  64609. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  64610. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  64611. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  64612. #endif
  64613. return EXPECT_RESULT();
  64614. }
  64615. static int test_tls13_early_data(void)
  64616. {
  64617. EXPECT_DECLS;
  64618. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  64619. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  64620. int written = 0;
  64621. int read = 0;
  64622. size_t i;
  64623. int splitEarlyData;
  64624. char msg[] = "This is early data";
  64625. char msg2[] = "This is client data";
  64626. char msg3[] = "This is server data";
  64627. char msg4[] = "This is server immediate data";
  64628. char msgBuf[50];
  64629. struct {
  64630. method_provider client_meth;
  64631. method_provider server_meth;
  64632. const char* tls_version;
  64633. int isUdp;
  64634. } params[] = {
  64635. #ifdef WOLFSSL_TLS13
  64636. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  64637. "TLS 1.3", 0 },
  64638. #endif
  64639. #ifdef WOLFSSL_DTLS13
  64640. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  64641. "DTLS 1.3", 1 },
  64642. #endif
  64643. };
  64644. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  64645. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  64646. struct test_memio_ctx test_ctx;
  64647. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  64648. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  64649. WOLFSSL_SESSION *sess = NULL;
  64650. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64651. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  64652. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  64653. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  64654. /* Get a ticket so that we can do 0-RTT on the next connection */
  64655. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64656. /* Make sure we read the ticket */
  64657. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  64658. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64659. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  64660. wolfSSL_free(ssl_c);
  64661. ssl_c = NULL;
  64662. wolfSSL_free(ssl_s);
  64663. ssl_s = NULL;
  64664. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64665. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64666. params[i].client_meth, params[i].server_meth), 0);
  64667. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  64668. #ifdef WOLFSSL_DTLS13
  64669. if (params[i].isUdp) {
  64670. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  64671. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  64672. #else
  64673. /* Let's test this but we generally don't recommend turning off the
  64674. * cookie exchange */
  64675. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  64676. #endif
  64677. }
  64678. #endif
  64679. /* Test 0-RTT data */
  64680. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  64681. &written), sizeof(msg));
  64682. ExpectIntEQ(written, sizeof(msg));
  64683. if (splitEarlyData) {
  64684. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  64685. &written), sizeof(msg));
  64686. ExpectIntEQ(written, sizeof(msg));
  64687. }
  64688. /* Read first 0-RTT data (if split otherwise entire data) */
  64689. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  64690. &read), sizeof(msg));
  64691. ExpectIntEQ(read, sizeof(msg));
  64692. ExpectStrEQ(msg, msgBuf);
  64693. /* Test 0.5-RTT data */
  64694. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  64695. if (splitEarlyData) {
  64696. /* Read second 0-RTT data */
  64697. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  64698. &read), sizeof(msg));
  64699. ExpectIntEQ(read, sizeof(msg));
  64700. ExpectStrEQ(msg, msgBuf);
  64701. }
  64702. if (params[i].isUdp) {
  64703. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  64704. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  64705. /* Read server 0.5-RTT data */
  64706. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  64707. ExpectStrEQ(msg4, msgBuf);
  64708. /* Complete handshake */
  64709. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  64710. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64711. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  64712. * a user would loop until it is true but here we control both sides so we
  64713. * just assert the expected value. wolfSSL_read_early_data does not provide
  64714. * handshake status to us with non-blocking IO and we can't use
  64715. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  64716. * parsing logic. */
  64717. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  64718. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  64719. &read), 0);
  64720. ExpectIntEQ(read, 0);
  64721. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  64722. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  64723. }
  64724. else {
  64725. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  64726. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  64727. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  64728. &read), 0);
  64729. ExpectIntEQ(read, 0);
  64730. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  64731. /* Read server 0.5-RTT data */
  64732. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  64733. ExpectStrEQ(msg4, msgBuf);
  64734. }
  64735. /* Test bi-directional write */
  64736. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  64737. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  64738. ExpectStrEQ(msg2, msgBuf);
  64739. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  64740. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  64741. ExpectStrEQ(msg3, msgBuf);
  64742. ExpectTrue(wolfSSL_session_reused(ssl_c));
  64743. ExpectTrue(wolfSSL_session_reused(ssl_s));
  64744. wolfSSL_SESSION_free(sess);
  64745. wolfSSL_free(ssl_c);
  64746. wolfSSL_free(ssl_s);
  64747. wolfSSL_CTX_free(ctx_c);
  64748. wolfSSL_CTX_free(ctx_s);
  64749. }
  64750. }
  64751. #endif
  64752. return EXPECT_RESULT();
  64753. }
  64754. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  64755. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  64756. {
  64757. EXPECT_DECLS;
  64758. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  64759. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  64760. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  64761. return EXPECT_RESULT();
  64762. }
  64763. #endif
  64764. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  64765. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  64766. {
  64767. EXPECT_DECLS;
  64768. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  64769. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  64770. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  64771. return EXPECT_RESULT();
  64772. }
  64773. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  64774. {
  64775. EXPECT_DECLS;
  64776. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  64777. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  64778. 0), 1);
  64779. return EXPECT_RESULT();
  64780. }
  64781. #endif
  64782. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  64783. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  64784. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  64785. {
  64786. EXPECT_DECLS;
  64787. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  64788. return EXPECT_RESULT();
  64789. }
  64790. #endif
  64791. static int test_self_signed_stapling(void)
  64792. {
  64793. EXPECT_DECLS;
  64794. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  64795. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  64796. test_ssl_cbf client_cbf;
  64797. test_ssl_cbf server_cbf;
  64798. size_t i;
  64799. struct {
  64800. method_provider client_meth;
  64801. method_provider server_meth;
  64802. ctx_cb client_ctx;
  64803. const char* tls_version;
  64804. } params[] = {
  64805. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  64806. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  64807. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  64808. #endif
  64809. #ifndef WOLFSSL_NO_TLS12
  64810. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  64811. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  64812. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  64813. #endif
  64814. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  64815. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  64816. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  64817. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  64818. test_self_signed_stapling_client_v2_multi_ctx_ready,
  64819. "TLSv1_2 v2 multi" },
  64820. #endif
  64821. #endif
  64822. };
  64823. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  64824. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  64825. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  64826. printf("\nTesting self-signed cert with status request: %s\n",
  64827. params[i].tls_version);
  64828. client_cbf.method = params[i].client_meth;
  64829. client_cbf.ctx_ready = params[i].client_ctx;
  64830. server_cbf.method = params[i].server_meth;
  64831. server_cbf.certPemFile = "certs/ca-cert.pem";
  64832. server_cbf.keyPemFile = "certs/ca-key.pem";
  64833. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  64834. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  64835. &server_cbf, NULL), TEST_SUCCESS);
  64836. }
  64837. #endif
  64838. return EXPECT_RESULT();
  64839. }
  64840. static int test_tls_multi_handshakes_one_record(void)
  64841. {
  64842. EXPECT_DECLS;
  64843. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  64844. struct test_memio_ctx test_ctx;
  64845. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  64846. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  64847. RecordLayerHeader* rh = NULL;
  64848. byte *len ;
  64849. int newRecIdx = RECORD_HEADER_SZ;
  64850. int idx = 0;
  64851. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64852. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64853. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  64854. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  64855. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64856. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  64857. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64858. /* Combine server handshake msgs into one record */
  64859. while (idx < test_ctx.c_len) {
  64860. word16 recLen;
  64861. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  64862. len = &rh->length[0];
  64863. ato16((const byte*)len, &recLen);
  64864. idx += RECORD_HEADER_SZ;
  64865. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  64866. (size_t)recLen);
  64867. newRecIdx += recLen;
  64868. idx += recLen;
  64869. }
  64870. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  64871. len = &rh->length[0];
  64872. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  64873. test_ctx.c_len = newRecIdx;
  64874. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  64875. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64876. wolfSSL_free(ssl_c);
  64877. wolfSSL_free(ssl_s);
  64878. wolfSSL_CTX_free(ctx_c);
  64879. wolfSSL_CTX_free(ctx_s);
  64880. #endif
  64881. return EXPECT_RESULT();
  64882. }
  64883. static int test_write_dup(void)
  64884. {
  64885. EXPECT_DECLS;
  64886. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  64887. size_t i, j;
  64888. char hiWorld[] = "dup message";
  64889. char readData[sizeof(hiWorld) + 5];
  64890. struct {
  64891. method_provider client_meth;
  64892. method_provider server_meth;
  64893. const char* version_name;
  64894. int version;
  64895. } methods[] = {
  64896. #ifndef WOLFSSL_NO_TLS12
  64897. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  64898. #endif
  64899. #ifdef WOLFSSL_TLS13
  64900. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  64901. #endif
  64902. };
  64903. struct {
  64904. const char* cipher;
  64905. int version;
  64906. } ciphers[] = {
  64907. /* For simplicity the macros are copied from internal.h */
  64908. /* TLS 1.2 */
  64909. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  64910. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  64911. #ifndef NO_RSA
  64912. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  64913. #endif
  64914. #endif
  64915. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  64916. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  64917. #endif
  64918. #endif
  64919. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  64920. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  64921. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  64922. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  64923. #endif
  64924. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  64925. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  64926. #endif
  64927. #endif
  64928. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  64929. && !defined(NO_TLS) && !defined(NO_AES)
  64930. #ifdef HAVE_AESGCM
  64931. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  64932. #ifndef NO_RSA
  64933. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  64934. #endif
  64935. #endif
  64936. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  64937. #ifndef NO_RSA
  64938. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  64939. #endif
  64940. #endif
  64941. #endif
  64942. #endif
  64943. /* TLS 1.3 */
  64944. #ifdef WOLFSSL_TLS13
  64945. #ifdef HAVE_AESGCM
  64946. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  64947. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  64948. #endif
  64949. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  64950. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  64951. #endif
  64952. #endif
  64953. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  64954. #ifndef NO_SHA256
  64955. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  64956. #endif
  64957. #endif
  64958. #ifdef HAVE_AESCCM
  64959. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  64960. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  64961. #endif
  64962. #endif
  64963. #endif
  64964. };
  64965. for (i = 0; i < XELEM_CNT(methods); i++) {
  64966. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  64967. struct test_memio_ctx test_ctx;
  64968. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  64969. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  64970. WOLFSSL *ssl_c2 = NULL;
  64971. if (methods[i].version != ciphers[j].version)
  64972. continue;
  64973. if (i == 0 && j == 0)
  64974. printf("\n");
  64975. printf("Testing %s with %s... ", methods[i].version_name,
  64976. ciphers[j].cipher);
  64977. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64978. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  64979. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64980. methods[i].client_meth, methods[i].server_meth), 0);
  64981. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64982. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  64983. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  64984. WRITE_DUP_WRITE_E);
  64985. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  64986. sizeof(hiWorld));
  64987. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  64988. sizeof(hiWorld));
  64989. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  64990. sizeof(hiWorld));
  64991. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  64992. WRITE_DUP_READ_E);
  64993. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  64994. sizeof(hiWorld));
  64995. if (EXPECT_SUCCESS())
  64996. printf("ok\n");
  64997. else
  64998. printf("failed\n");
  64999. wolfSSL_free(ssl_c);
  65000. wolfSSL_free(ssl_c2);
  65001. wolfSSL_free(ssl_s);
  65002. wolfSSL_CTX_free(ctx_c);
  65003. wolfSSL_CTX_free(ctx_s);
  65004. }
  65005. }
  65006. #endif
  65007. return EXPECT_RESULT();
  65008. }
  65009. static int test_read_write_hs(void)
  65010. {
  65011. EXPECT_DECLS;
  65012. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  65013. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  65014. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  65015. struct test_memio_ctx test_ctx;
  65016. byte test_buffer[16];
  65017. unsigned int test;
  65018. /* test == 0 : client writes, server reads */
  65019. /* test == 1 : server writes, client reads */
  65020. for (test = 0; test < 2; test++) {
  65021. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  65022. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  65023. wolfTLSv1_2_client_method,
  65024. wolfTLSv1_2_server_method), 0);
  65025. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  65026. /* CH -> */
  65027. if (test == 0) {
  65028. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  65029. } else {
  65030. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  65031. sizeof(test_buffer)), -1);
  65032. }
  65033. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  65034. /* <- SH + SKE + SHD */
  65035. if (test == 0) {
  65036. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  65037. sizeof(test_buffer)), -1);
  65038. } else {
  65039. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  65040. }
  65041. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  65042. /* -> CKE + CLIENT FINISHED */
  65043. if (test == 0) {
  65044. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  65045. } else {
  65046. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  65047. sizeof(test_buffer)), -1);
  65048. }
  65049. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  65050. /* abide clang static analyzer */
  65051. if (ssl_s != NULL) {
  65052. /* disable group message to separate sending of ChangeCipherspec
  65053. * from Finished */
  65054. ssl_s->options.groupMessages = 0;
  65055. }
  65056. /* allow writing of CS, but not FINISHED */
  65057. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  65058. /* <- CS */
  65059. if (test == 0) {
  65060. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  65061. sizeof(test_buffer)), -1);
  65062. } else {
  65063. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  65064. }
  65065. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  65066. /* move CS message where the client can read it */
  65067. memmove(test_ctx.c_buff,
  65068. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  65069. test_ctx.c_len = 6;
  65070. /* read CS */
  65071. if (test == 0) {
  65072. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  65073. } else {
  65074. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  65075. sizeof(test_buffer)), -1);
  65076. }
  65077. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  65078. ExpectIntEQ(test_ctx.c_len, 0);
  65079. if (test == 0) {
  65080. /* send SERVER FINISHED */
  65081. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  65082. sizeof(test_buffer)), -1);
  65083. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  65084. WOLFSSL_ERROR_WANT_READ);
  65085. } else {
  65086. /* send SERVER FINISHED + App Data */
  65087. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  65088. }
  65089. ExpectIntGT(test_ctx.c_len, 0);
  65090. /* Send and receive the data */
  65091. if (test == 0) {
  65092. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  65093. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  65094. sizeof(test_buffer)), 5);
  65095. } else {
  65096. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  65097. sizeof(test_buffer)), 5);
  65098. }
  65099. ExpectBufEQ(test_buffer, "hello", 5);
  65100. wolfSSL_free(ssl_c);
  65101. wolfSSL_free(ssl_s);
  65102. wolfSSL_CTX_free(ctx_c);
  65103. wolfSSL_CTX_free(ctx_s);
  65104. ssl_c = ssl_s = NULL;
  65105. ctx_c = ctx_s = NULL;
  65106. }
  65107. #endif
  65108. return EXPECT_RESULT();
  65109. }
  65110. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  65111. static const char* test_get_signature_nid_siglag;
  65112. static int test_get_signature_nid_sig;
  65113. static int test_get_signature_nid_hash;
  65114. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  65115. {
  65116. EXPECT_DECLS;
  65117. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  65118. if (!wolfSSL_is_server(ssl)) {
  65119. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  65120. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  65121. }
  65122. return EXPECT_RESULT();
  65123. }
  65124. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  65125. {
  65126. EXPECT_DECLS;
  65127. int nid = 0;
  65128. (void)ctx;
  65129. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  65130. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  65131. ExpectIntEQ(nid, test_get_signature_nid_sig);
  65132. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  65133. ExpectIntEQ(nid, test_get_signature_nid_hash);
  65134. }
  65135. else /* No sigalg info on static ciphersuite */
  65136. return TEST_SUCCESS;
  65137. return EXPECT_RESULT();
  65138. }
  65139. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  65140. {
  65141. EXPECT_DECLS;
  65142. int nid = 0;
  65143. (void)ctx;
  65144. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  65145. ExpectIntEQ(nid, test_get_signature_nid_sig);
  65146. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  65147. ExpectIntEQ(nid, test_get_signature_nid_hash);
  65148. return EXPECT_RESULT();
  65149. }
  65150. #endif
  65151. static int test_get_signature_nid(void)
  65152. {
  65153. EXPECT_DECLS;
  65154. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  65155. test_ssl_cbf client_cbf;
  65156. test_ssl_cbf server_cbf;
  65157. size_t i;
  65158. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  65159. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  65160. caCertFile }
  65161. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  65162. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  65163. caEccCertFile }
  65164. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  65165. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  65166. caCertFile }
  65167. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  65168. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  65169. caEccCertFile }
  65170. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  65171. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  65172. caEdCertFile }
  65173. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  65174. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  65175. caEd448CertFile }
  65176. struct {
  65177. const char* siglag;
  65178. int sig_nid;
  65179. int hash_nid;
  65180. int tls_ver;
  65181. const char* server_cert;
  65182. const char* server_key;
  65183. const char* client_ca;
  65184. } params[] = {
  65185. #ifndef NO_RSA
  65186. #ifndef NO_SHA256
  65187. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  65188. #ifdef WC_RSA_PSS
  65189. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  65190. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  65191. #endif
  65192. #endif
  65193. #ifdef WOLFSSL_SHA512
  65194. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  65195. #ifdef WC_RSA_PSS
  65196. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  65197. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  65198. #endif
  65199. #endif
  65200. #endif
  65201. #ifdef HAVE_ECC
  65202. #ifndef NO_SHA256
  65203. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  65204. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  65205. #endif
  65206. #endif
  65207. #ifdef HAVE_ED25519
  65208. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  65209. #endif
  65210. #ifdef HAVE_ED448
  65211. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  65212. #endif
  65213. };
  65214. printf("\n");
  65215. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  65216. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  65217. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  65218. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  65219. params[i].siglag);
  65220. switch (params[i].tls_ver) {
  65221. #ifndef WOLFSSL_NO_TLS12
  65222. case WOLFSSL_TLSV1_2:
  65223. client_cbf.method = wolfTLSv1_2_client_method;
  65224. server_cbf.method = wolfTLSv1_2_server_method;
  65225. break;
  65226. #endif
  65227. #ifdef WOLFSSL_TLS13
  65228. case WOLFSSL_TLSV1_3:
  65229. client_cbf.method = wolfTLSv1_3_client_method;
  65230. server_cbf.method = wolfTLSv1_3_server_method;
  65231. break;
  65232. #endif
  65233. default:
  65234. printf("skipping\n");
  65235. continue;
  65236. }
  65237. test_get_signature_nid_siglag = params[i].siglag;
  65238. test_get_signature_nid_sig = params[i].sig_nid;
  65239. test_get_signature_nid_hash = params[i].hash_nid;
  65240. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  65241. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  65242. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  65243. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  65244. server_cbf.certPemFile = params[i].server_cert;
  65245. server_cbf.keyPemFile = params[i].server_key;
  65246. client_cbf.caPemFile = params[i].client_ca;
  65247. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  65248. &server_cbf, NULL), TEST_SUCCESS);
  65249. if (EXPECT_SUCCESS())
  65250. printf("passed\n");
  65251. }
  65252. #endif
  65253. return EXPECT_RESULT();
  65254. }
  65255. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  65256. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  65257. {
  65258. #ifndef NO_MD5
  65259. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  65260. #elif !defined(NO_SHA)
  65261. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  65262. #elif !defined(NO_SHA256)
  65263. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  65264. #else
  65265. #error "We need a digest to hash the Signer object"
  65266. #endif
  65267. byte hashBuf[WC_MAX_DIGEST_SIZE];
  65268. wc_HashAlg hash;
  65269. size_t i;
  65270. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  65271. for (i = 0; i < CA_TABLE_SIZE; i++) {
  65272. Signer* cur;
  65273. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  65274. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  65275. sizeof(*cur)), 0);
  65276. }
  65277. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  65278. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  65279. return MakeWordFromHash(hashBuf);
  65280. }
  65281. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  65282. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  65283. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  65284. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  65285. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  65286. {
  65287. EXPECT_DECLS;
  65288. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  65289. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  65290. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  65291. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  65292. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  65293. return EXPECT_RESULT();
  65294. }
  65295. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  65296. {
  65297. EXPECT_DECLS;
  65298. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  65299. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  65300. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  65301. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  65302. return EXPECT_RESULT();
  65303. }
  65304. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  65305. {
  65306. EXPECT_DECLS;
  65307. WOLFSSL_CERT_MANAGER* cm;
  65308. (void)ssl;
  65309. /* WARNING: this approach bypasses the reference counter check in
  65310. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  65311. * cause unexpected behaviour when other active connections try accessing
  65312. * the caTable. */
  65313. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  65314. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  65315. WOLFSSL_SUCCESS);
  65316. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  65317. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  65318. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  65319. return EXPECT_RESULT();
  65320. }
  65321. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  65322. {
  65323. EXPECT_DECLS;
  65324. WOLFSSL_CTX* ctx;
  65325. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  65326. return EXPECT_RESULT();
  65327. }
  65328. #endif
  65329. static int test_tls_cert_store_unchanged(void)
  65330. {
  65331. EXPECT_DECLS;
  65332. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  65333. test_ssl_cbf client_cbf;
  65334. test_ssl_cbf server_cbf;
  65335. int i;
  65336. for (i = 0; i < 2; i++) {
  65337. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  65338. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  65339. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  65340. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  65341. sizeof(test_tls_cert_store_unchanged_before_hashes));
  65342. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  65343. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  65344. sizeof(test_tls_cert_store_unchanged_after_hashes));
  65345. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  65346. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  65347. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  65348. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  65349. switch (i) {
  65350. case 0:
  65351. client_cbf.on_ctx_cleanup =
  65352. test_tls_cert_store_unchanged_ctx_cleanup;
  65353. server_cbf.on_ctx_cleanup =
  65354. test_tls_cert_store_unchanged_ctx_cleanup;
  65355. break;
  65356. case 1:
  65357. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  65358. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  65359. break;
  65360. default:
  65361. Fail(("Should not enter here"), ("Entered here"));
  65362. }
  65363. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  65364. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  65365. server_cbf.caPemFile = caCertFile;
  65366. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  65367. &server_cbf, NULL), TEST_SUCCESS);
  65368. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  65369. test_tls_cert_store_unchanged_after_hashes,
  65370. sizeof(test_tls_cert_store_unchanged_after_hashes));
  65371. }
  65372. #endif
  65373. return EXPECT_RESULT();
  65374. }
  65375. static int test_wolfSSL_SendUserCanceled(void)
  65376. {
  65377. EXPECT_DECLS;
  65378. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  65379. size_t i;
  65380. struct {
  65381. method_provider client_meth;
  65382. method_provider server_meth;
  65383. const char* tls_version;
  65384. } params[] = {
  65385. #if defined(WOLFSSL_TLS13)
  65386. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  65387. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  65388. #ifdef WOLFSSL_DTLS13
  65389. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  65390. #endif
  65391. #endif
  65392. #ifndef WOLFSSL_NO_TLS12
  65393. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  65394. #ifdef WOLFSSL_DTLS
  65395. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  65396. #endif
  65397. #endif
  65398. #if !defined(NO_OLD_TLS)
  65399. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  65400. #ifdef WOLFSSL_DTLS
  65401. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  65402. #endif
  65403. #endif
  65404. };
  65405. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  65406. WOLFSSL_CTX *ctx_c = NULL;
  65407. WOLFSSL_CTX *ctx_s = NULL;
  65408. WOLFSSL *ssl_c = NULL;
  65409. WOLFSSL *ssl_s = NULL;
  65410. struct test_memio_ctx test_ctx;
  65411. WOLFSSL_ALERT_HISTORY h;
  65412. printf("Testing %s\n", params[i].tls_version);
  65413. XMEMSET(&h, 0, sizeof(h));
  65414. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  65415. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  65416. params[i].client_meth, params[i].server_meth), 0);
  65417. /* CH1 */
  65418. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  65419. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  65420. ExpectIntEQ(wolfSSL_SendUserCanceled(ssl_s), WOLFSSL_SHUTDOWN_NOT_DONE);
  65421. /* Alert closed connection */
  65422. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  65423. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_ZERO_RETURN);
  65424. /* Last alert will be close notify because user_canceled should be
  65425. * followed by a close_notify */
  65426. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  65427. ExpectIntEQ(h.last_rx.code, close_notify);
  65428. ExpectIntEQ(h.last_rx.level, alert_warning);
  65429. wolfSSL_free(ssl_c);
  65430. wolfSSL_free(ssl_s);
  65431. wolfSSL_CTX_free(ctx_c);
  65432. wolfSSL_CTX_free(ctx_s);
  65433. }
  65434. #endif
  65435. return EXPECT_RESULT();
  65436. }
  65437. /*----------------------------------------------------------------------------*
  65438. | Main
  65439. *----------------------------------------------------------------------------*/
  65440. typedef int (*TEST_FUNC)(void);
  65441. typedef struct {
  65442. const char *name;
  65443. TEST_FUNC func;
  65444. byte run:1;
  65445. byte fail:1;
  65446. } TEST_CASE;
  65447. #define TEST_DECL(func) { #func, func, 0, 0 }
  65448. int testAll = 1;
  65449. TEST_CASE testCases[] = {
  65450. TEST_DECL(test_fileAccess),
  65451. /*********************************
  65452. * wolfcrypt
  65453. *********************************/
  65454. TEST_DECL(test_ForceZero),
  65455. TEST_DECL(test_wolfCrypt_Init),
  65456. TEST_DECL(test_wc_LoadStaticMemory_ex),
  65457. /* Locking with Compat Mutex */
  65458. TEST_DECL(test_wc_SetMutexCb),
  65459. TEST_DECL(test_wc_LockMutex_ex),
  65460. /* Digests */
  65461. TEST_DECL(test_wc_InitMd5),
  65462. TEST_DECL(test_wc_Md5Update),
  65463. TEST_DECL(test_wc_Md5Final),
  65464. TEST_DECL(test_wc_InitSha),
  65465. TEST_DECL(test_wc_ShaUpdate),
  65466. TEST_DECL(test_wc_ShaFinal),
  65467. TEST_DECL(test_wc_InitSha256),
  65468. TEST_DECL(test_wc_Sha256Update),
  65469. TEST_DECL(test_wc_Sha256Final),
  65470. TEST_DECL(test_wc_Sha256FinalRaw),
  65471. TEST_DECL(test_wc_Sha256GetFlags),
  65472. TEST_DECL(test_wc_Sha256Free),
  65473. TEST_DECL(test_wc_Sha256GetHash),
  65474. TEST_DECL(test_wc_Sha256Copy),
  65475. TEST_DECL(test_wc_InitSha224),
  65476. TEST_DECL(test_wc_Sha224Update),
  65477. TEST_DECL(test_wc_Sha224Final),
  65478. TEST_DECL(test_wc_Sha224SetFlags),
  65479. TEST_DECL(test_wc_Sha224GetFlags),
  65480. TEST_DECL(test_wc_Sha224Free),
  65481. TEST_DECL(test_wc_Sha224GetHash),
  65482. TEST_DECL(test_wc_Sha224Copy),
  65483. TEST_DECL(test_wc_InitSha512),
  65484. TEST_DECL(test_wc_Sha512Update),
  65485. TEST_DECL(test_wc_Sha512Final),
  65486. TEST_DECL(test_wc_Sha512GetFlags),
  65487. TEST_DECL(test_wc_Sha512FinalRaw),
  65488. TEST_DECL(test_wc_Sha512Free),
  65489. TEST_DECL(test_wc_Sha512GetHash),
  65490. TEST_DECL(test_wc_Sha512Copy),
  65491. TEST_DECL(test_wc_InitSha512_224),
  65492. TEST_DECL(test_wc_Sha512_224Update),
  65493. TEST_DECL(test_wc_Sha512_224Final),
  65494. TEST_DECL(test_wc_Sha512_224GetFlags),
  65495. TEST_DECL(test_wc_Sha512_224FinalRaw),
  65496. TEST_DECL(test_wc_Sha512_224Free),
  65497. TEST_DECL(test_wc_Sha512_224GetHash),
  65498. TEST_DECL(test_wc_Sha512_224Copy),
  65499. TEST_DECL(test_wc_InitSha512_256),
  65500. TEST_DECL(test_wc_Sha512_256Update),
  65501. TEST_DECL(test_wc_Sha512_256Final),
  65502. TEST_DECL(test_wc_Sha512_256GetFlags),
  65503. TEST_DECL(test_wc_Sha512_256FinalRaw),
  65504. TEST_DECL(test_wc_Sha512_256Free),
  65505. TEST_DECL(test_wc_Sha512_256GetHash),
  65506. TEST_DECL(test_wc_Sha512_256Copy),
  65507. TEST_DECL(test_wc_InitSha384),
  65508. TEST_DECL(test_wc_Sha384Update),
  65509. TEST_DECL(test_wc_Sha384Final),
  65510. TEST_DECL(test_wc_Sha384GetFlags),
  65511. TEST_DECL(test_wc_Sha384FinalRaw),
  65512. TEST_DECL(test_wc_Sha384Free),
  65513. TEST_DECL(test_wc_Sha384GetHash),
  65514. TEST_DECL(test_wc_Sha384Copy),
  65515. TEST_DECL(test_wc_InitBlake2b),
  65516. TEST_DECL(test_wc_InitBlake2b_WithKey),
  65517. TEST_DECL(test_wc_InitBlake2s_WithKey),
  65518. TEST_DECL(test_wc_InitRipeMd),
  65519. TEST_DECL(test_wc_RipeMdUpdate),
  65520. TEST_DECL(test_wc_RipeMdFinal),
  65521. TEST_DECL(test_wc_InitSha3),
  65522. TEST_DECL(testing_wc_Sha3_Update),
  65523. TEST_DECL(test_wc_Sha3_224_Final),
  65524. TEST_DECL(test_wc_Sha3_256_Final),
  65525. TEST_DECL(test_wc_Sha3_384_Final),
  65526. TEST_DECL(test_wc_Sha3_512_Final),
  65527. TEST_DECL(test_wc_Sha3_224_Copy),
  65528. TEST_DECL(test_wc_Sha3_256_Copy),
  65529. TEST_DECL(test_wc_Sha3_384_Copy),
  65530. TEST_DECL(test_wc_Sha3_512_Copy),
  65531. TEST_DECL(test_wc_Sha3_GetFlags),
  65532. TEST_DECL(test_wc_InitShake256),
  65533. TEST_DECL(testing_wc_Shake256_Update),
  65534. TEST_DECL(test_wc_Shake256_Final),
  65535. TEST_DECL(test_wc_Shake256_Copy),
  65536. TEST_DECL(test_wc_Shake256Hash),
  65537. /* SM3 Digest */
  65538. TEST_DECL(test_wc_InitSm3Free),
  65539. TEST_DECL(test_wc_Sm3UpdateFinal),
  65540. TEST_DECL(test_wc_Sm3GetHash),
  65541. TEST_DECL(test_wc_Sm3Copy),
  65542. TEST_DECL(test_wc_Sm3FinalRaw),
  65543. TEST_DECL(test_wc_Sm3GetSetFlags),
  65544. TEST_DECL(test_wc_Sm3Hash),
  65545. TEST_DECL(test_wc_HashInit),
  65546. TEST_DECL(test_wc_HashSetFlags),
  65547. TEST_DECL(test_wc_HashGetFlags),
  65548. /* HMAC */
  65549. TEST_DECL(test_wc_Md5HmacSetKey),
  65550. TEST_DECL(test_wc_Md5HmacUpdate),
  65551. TEST_DECL(test_wc_Md5HmacFinal),
  65552. TEST_DECL(test_wc_ShaHmacSetKey),
  65553. TEST_DECL(test_wc_ShaHmacUpdate),
  65554. TEST_DECL(test_wc_ShaHmacFinal),
  65555. TEST_DECL(test_wc_Sha224HmacSetKey),
  65556. TEST_DECL(test_wc_Sha224HmacUpdate),
  65557. TEST_DECL(test_wc_Sha224HmacFinal),
  65558. TEST_DECL(test_wc_Sha256HmacSetKey),
  65559. TEST_DECL(test_wc_Sha256HmacUpdate),
  65560. TEST_DECL(test_wc_Sha256HmacFinal),
  65561. TEST_DECL(test_wc_Sha384HmacSetKey),
  65562. TEST_DECL(test_wc_Sha384HmacUpdate),
  65563. TEST_DECL(test_wc_Sha384HmacFinal),
  65564. /* CMAC */
  65565. TEST_DECL(test_wc_InitCmac),
  65566. TEST_DECL(test_wc_CmacUpdate),
  65567. TEST_DECL(test_wc_CmacFinal),
  65568. TEST_DECL(test_wc_AesCmacGenerate),
  65569. /* Cipher */
  65570. TEST_DECL(test_wc_AesGcmStream),
  65571. TEST_DECL(test_wc_Des3_SetIV),
  65572. TEST_DECL(test_wc_Des3_SetKey),
  65573. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  65574. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  65575. TEST_DECL(test_wc_Des3_EcbEncrypt),
  65576. TEST_DECL(test_wc_Chacha_SetKey),
  65577. TEST_DECL(test_wc_Chacha_Process),
  65578. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  65579. TEST_DECL(test_wc_Poly1305SetKey),
  65580. TEST_DECL(test_wc_CamelliaSetKey),
  65581. TEST_DECL(test_wc_CamelliaSetIV),
  65582. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  65583. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  65584. TEST_DECL(test_wc_Arc4SetKey),
  65585. TEST_DECL(test_wc_Arc4Process),
  65586. TEST_DECL(test_wc_Rc2SetKey),
  65587. TEST_DECL(test_wc_Rc2SetIV),
  65588. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  65589. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  65590. /* AES cipher and GMAC. */
  65591. TEST_DECL(test_wc_AesSetKey),
  65592. TEST_DECL(test_wc_AesSetIV),
  65593. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  65594. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  65595. TEST_DECL(test_wc_AesGcmSetKey),
  65596. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  65597. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  65598. TEST_DECL(test_wc_GmacSetKey),
  65599. TEST_DECL(test_wc_GmacUpdate),
  65600. TEST_DECL(test_wc_AesCcmSetKey),
  65601. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  65602. #if defined(WOLFSSL_AES_EAX) && \
  65603. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  65604. TEST_DECL(test_wc_AesEaxVectors),
  65605. TEST_DECL(test_wc_AesEaxEncryptAuth),
  65606. TEST_DECL(test_wc_AesEaxDecryptAuth),
  65607. #endif /* WOLFSSL_AES_EAX */
  65608. /* SM4 cipher */
  65609. TEST_DECL(test_wc_Sm4),
  65610. TEST_DECL(test_wc_Sm4Ecb),
  65611. TEST_DECL(test_wc_Sm4Cbc),
  65612. TEST_DECL(test_wc_Sm4Ctr),
  65613. TEST_DECL(test_wc_Sm4Gcm),
  65614. TEST_DECL(test_wc_Sm4Ccm),
  65615. /* RNG tests */
  65616. #ifdef HAVE_HASHDRBG
  65617. #ifdef TEST_RESEED_INTERVAL
  65618. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  65619. #endif
  65620. TEST_DECL(test_wc_RNG_GenerateBlock),
  65621. #endif
  65622. TEST_DECL(test_get_rand_digit),
  65623. TEST_DECL(test_wc_InitRngNonce),
  65624. TEST_DECL(test_wc_InitRngNonce_ex),
  65625. /* MP API tests */
  65626. TEST_DECL(test_get_digit_count),
  65627. TEST_DECL(test_mp_cond_copy),
  65628. TEST_DECL(test_mp_rand),
  65629. TEST_DECL(test_get_digit),
  65630. TEST_DECL(test_wc_export_int),
  65631. /* RSA */
  65632. TEST_DECL(test_wc_InitRsaKey),
  65633. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  65634. TEST_DECL(test_wc_RsaPublicKeyDecode),
  65635. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  65636. TEST_DECL(test_wc_RsaPrivateKeyDecodeRaw),
  65637. TEST_DECL(test_wc_MakeRsaKey),
  65638. TEST_DECL(test_wc_CheckProbablePrime),
  65639. TEST_DECL(test_wc_RsaPSS_Verify),
  65640. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  65641. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  65642. TEST_DECL(test_wc_RsaKeyToDer),
  65643. TEST_DECL(test_wc_RsaKeyToPublicDer),
  65644. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  65645. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  65646. TEST_DECL(test_wc_RsaEncryptSize),
  65647. TEST_DECL(test_wc_RsaSSL_SignVerify),
  65648. TEST_DECL(test_wc_RsaFlattenPublicKey),
  65649. TEST_DECL(test_RsaDecryptBoundsCheck),
  65650. /* DSA */
  65651. TEST_DECL(test_wc_InitDsaKey),
  65652. TEST_DECL(test_wc_DsaSignVerify),
  65653. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  65654. TEST_DECL(test_wc_MakeDsaKey),
  65655. TEST_DECL(test_wc_DsaKeyToDer),
  65656. TEST_DECL(test_wc_DsaKeyToPublicDer),
  65657. TEST_DECL(test_wc_DsaImportParamsRaw),
  65658. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  65659. TEST_DECL(test_wc_DsaExportParamsRaw),
  65660. TEST_DECL(test_wc_DsaExportKeyRaw),
  65661. /* DH */
  65662. TEST_DECL(test_wc_DhPublicKeyDecode),
  65663. /* wolfCrypt ECC tests */
  65664. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  65665. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  65666. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  65667. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  65668. !defined(HAVE_SELFTEST) && \
  65669. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  65670. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  65671. #endif
  65672. TEST_DECL(test_wc_ecc_make_key),
  65673. TEST_DECL(test_wc_ecc_init),
  65674. TEST_DECL(test_wc_ecc_check_key),
  65675. TEST_DECL(test_wc_ecc_get_generator),
  65676. TEST_DECL(test_wc_ecc_size),
  65677. TEST_DECL(test_wc_ecc_params),
  65678. TEST_DECL(test_wc_ecc_signVerify_hash),
  65679. TEST_DECL(test_wc_ecc_shared_secret),
  65680. TEST_DECL(test_wc_ecc_export_x963),
  65681. TEST_DECL(test_wc_ecc_export_x963_ex),
  65682. TEST_DECL(test_wc_ecc_import_x963),
  65683. TEST_DECL(test_wc_ecc_import_private_key),
  65684. TEST_DECL(test_wc_ecc_export_private_only),
  65685. TEST_DECL(test_wc_ecc_rs_to_sig),
  65686. TEST_DECL(test_wc_ecc_import_raw),
  65687. TEST_DECL(test_wc_ecc_import_unsigned),
  65688. TEST_DECL(test_wc_ecc_sig_size),
  65689. TEST_DECL(test_wc_ecc_ctx_new),
  65690. TEST_DECL(test_wc_ecc_ctx_reset),
  65691. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  65692. TEST_DECL(test_wc_ecc_ctx_set_info),
  65693. TEST_DECL(test_wc_ecc_encryptDecrypt),
  65694. TEST_DECL(test_wc_ecc_del_point),
  65695. TEST_DECL(test_wc_ecc_pointFns),
  65696. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  65697. TEST_DECL(test_wc_ecc_verify_hash_ex),
  65698. TEST_DECL(test_wc_ecc_mulmod),
  65699. TEST_DECL(test_wc_ecc_is_valid_idx),
  65700. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  65701. TEST_DECL(test_wc_ecc_sig_size_calc),
  65702. TEST_DECL(test_wc_EccPrivateKeyToDer),
  65703. /* SM2 elliptic curve */
  65704. TEST_DECL(test_wc_ecc_sm2_make_key),
  65705. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  65706. TEST_DECL(test_wc_ecc_sm2_create_digest),
  65707. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  65708. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  65709. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  65710. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  65711. /* Curve25519 */
  65712. TEST_DECL(test_wc_curve25519_init),
  65713. TEST_DECL(test_wc_curve25519_size),
  65714. TEST_DECL(test_wc_curve25519_export_key_raw),
  65715. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  65716. TEST_DECL(test_wc_curve25519_make_key),
  65717. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  65718. TEST_DECL(test_wc_curve25519_make_pub),
  65719. TEST_DECL(test_wc_curve25519_export_public_ex),
  65720. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  65721. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  65722. TEST_DECL(test_wc_curve25519_import_private),
  65723. /* ED25519 */
  65724. TEST_DECL(test_wc_ed25519_make_key),
  65725. TEST_DECL(test_wc_ed25519_init),
  65726. TEST_DECL(test_wc_ed25519_sign_msg),
  65727. TEST_DECL(test_wc_ed25519_import_public),
  65728. TEST_DECL(test_wc_ed25519_import_private_key),
  65729. TEST_DECL(test_wc_ed25519_export),
  65730. TEST_DECL(test_wc_ed25519_size),
  65731. TEST_DECL(test_wc_ed25519_exportKey),
  65732. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  65733. TEST_DECL(test_wc_Ed25519KeyToDer),
  65734. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  65735. /* Curve448 */
  65736. TEST_DECL(test_wc_curve448_make_key),
  65737. TEST_DECL(test_wc_curve448_shared_secret_ex),
  65738. TEST_DECL(test_wc_curve448_export_public_ex),
  65739. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  65740. TEST_DECL(test_wc_curve448_export_key_raw),
  65741. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  65742. TEST_DECL(test_wc_curve448_import_private),
  65743. TEST_DECL(test_wc_curve448_init),
  65744. TEST_DECL(test_wc_curve448_size),
  65745. /* Ed448 */
  65746. TEST_DECL(test_wc_ed448_make_key),
  65747. TEST_DECL(test_wc_ed448_init),
  65748. TEST_DECL(test_wc_ed448_sign_msg),
  65749. TEST_DECL(test_wc_ed448_import_public),
  65750. TEST_DECL(test_wc_ed448_import_private_key),
  65751. TEST_DECL(test_wc_ed448_export),
  65752. TEST_DECL(test_wc_ed448_size),
  65753. TEST_DECL(test_wc_ed448_exportKey),
  65754. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  65755. TEST_DECL(test_wc_Ed448KeyToDer),
  65756. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  65757. TEST_DECL(test_wc_Curve448PrivateKeyToDer),
  65758. /* Dilithium */
  65759. TEST_DECL(test_wc_dilithium),
  65760. TEST_DECL(test_wc_dilithium_make_key),
  65761. TEST_DECL(test_wc_dilithium_sign),
  65762. TEST_DECL(test_wc_dilithium_verify),
  65763. TEST_DECL(test_wc_dilithium_check_key),
  65764. TEST_DECL(test_wc_dilithium_der),
  65765. /* Signature API */
  65766. TEST_DECL(test_wc_SignatureGetSize_ecc),
  65767. TEST_DECL(test_wc_SignatureGetSize_rsa),
  65768. /* PEM and DER APIs. */
  65769. TEST_DECL(test_wc_PemToDer),
  65770. TEST_DECL(test_wc_AllocDer),
  65771. TEST_DECL(test_wc_CertPemToDer),
  65772. TEST_DECL(test_wc_KeyPemToDer),
  65773. TEST_DECL(test_wc_PubKeyPemToDer),
  65774. TEST_DECL(test_wc_PemPubKeyToDer),
  65775. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  65776. TEST_DECL(test_wc_CheckCertSigPubKey),
  65777. /* wolfCrypt ASN tests */
  65778. TEST_DECL(test_ToTraditional),
  65779. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  65780. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  65781. /* Certificate */
  65782. TEST_DECL(test_wc_SetSubjectRaw),
  65783. TEST_DECL(test_wc_GetSubjectRaw),
  65784. TEST_DECL(test_wc_SetIssuerRaw),
  65785. TEST_DECL(test_wc_SetIssueBuffer),
  65786. TEST_DECL(test_wc_SetSubjectKeyId),
  65787. TEST_DECL(test_wc_SetSubject),
  65788. TEST_DECL(test_CheckCertSignature),
  65789. TEST_DECL(test_wc_ParseCert),
  65790. TEST_DECL(test_wc_ParseCert_Error),
  65791. TEST_DECL(test_MakeCertWithPathLen),
  65792. TEST_DECL(test_MakeCertWith0Ser),
  65793. TEST_DECL(test_MakeCertWithCaFalse),
  65794. TEST_DECL(test_wc_SetKeyUsage),
  65795. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  65796. TEST_DECL(test_wc_SetSubjectBuffer),
  65797. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  65798. /* wolfcrypt PKCS#7 */
  65799. TEST_DECL(test_wc_PKCS7_New),
  65800. TEST_DECL(test_wc_PKCS7_Init),
  65801. TEST_DECL(test_wc_PKCS7_InitWithCert),
  65802. TEST_DECL(test_wc_PKCS7_EncodeData),
  65803. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  65804. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  65805. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  65806. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  65807. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  65808. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  65809. TEST_DECL(test_wc_PKCS7_Degenerate),
  65810. TEST_DECL(test_wc_PKCS7_BER),
  65811. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  65812. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  65813. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  65814. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  65815. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  65816. /* wolfCrypt PKCS#12 */
  65817. TEST_DECL(test_wc_i2d_PKCS12),
  65818. /*
  65819. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  65820. * avoid memory leaks.
  65821. */
  65822. TEST_DECL(test_wolfCrypt_Cleanup),
  65823. TEST_DECL(test_wolfSSL_Init),
  65824. TEST_DECL(test_dual_alg_support),
  65825. /*********************************
  65826. * OpenSSL compatibility API tests
  65827. *********************************/
  65828. /* If at some point a stub get implemented this test should fail indicating
  65829. * a need to implement a new test case
  65830. */
  65831. TEST_DECL(test_stubs_are_stubs),
  65832. /* ASN.1 compatibility API tests */
  65833. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  65834. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  65835. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  65836. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  65837. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  65838. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  65839. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  65840. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  65841. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  65842. TEST_DECL(test_wolfSSL_ASN1_get_object),
  65843. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  65844. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  65845. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  65846. TEST_DECL(test_wolfSSL_ASN1_STRING),
  65847. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  65848. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  65849. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  65850. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  65851. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  65852. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  65853. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  65854. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  65855. TEST_DECL(test_wolfSSL_ASN1_TIME),
  65856. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  65857. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  65858. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  65859. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  65860. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  65861. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  65862. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  65863. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  65864. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  65865. TEST_DECL(test_wolfSSL_lhash),
  65866. TEST_DECL(test_wolfSSL_certs),
  65867. TEST_DECL(test_wolfSSL_private_keys),
  65868. TEST_DECL(test_wolfSSL_PEM_def_callback),
  65869. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  65870. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  65871. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  65872. TEST_DECL(test_wolfSSL_PEM_PrivateKey_rsa),
  65873. TEST_DECL(test_wolfSSL_PEM_PrivateKey_ecc),
  65874. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dsa),
  65875. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dh),
  65876. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  65877. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  65878. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  65879. #ifndef NO_BIO
  65880. TEST_DECL(test_wolfSSL_BIO),
  65881. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  65882. TEST_DECL(test_wolfSSL_PEM_read_bio),
  65883. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  65884. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  65885. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  65886. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  65887. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  65888. #endif
  65889. /* EVP API testing */
  65890. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  65891. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  65892. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  65893. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  65894. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  65895. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  65896. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  65897. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  65898. TEST_DECL(test_wolfSSL_EVP_shake128),
  65899. TEST_DECL(test_wolfSSL_EVP_shake256),
  65900. TEST_DECL(test_wolfSSL_EVP_sm3),
  65901. TEST_DECL(test_EVP_blake2),
  65902. #ifdef OPENSSL_ALL
  65903. TEST_DECL(test_wolfSSL_EVP_md4),
  65904. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  65905. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  65906. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  65907. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  65908. #endif
  65909. TEST_DECL(test_EVP_MD_do_all),
  65910. TEST_DECL(test_wolfSSL_EVP_MD_size),
  65911. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  65912. TEST_DECL(test_wolfSSL_EVP_Digest),
  65913. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  65914. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  65915. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  65916. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  65917. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  65918. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  65919. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  65920. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  65921. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  65922. TEST_DECL(test_wolfssl_EVP_chacha20),
  65923. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  65924. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  65925. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  65926. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  65927. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  65928. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  65929. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  65930. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  65931. #ifdef OPENSSL_ALL
  65932. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  65933. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  65934. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  65935. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  65936. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  65937. TEST_DECL(test_wolfSSL_EVP_rc4),
  65938. TEST_DECL(test_wolfSSL_EVP_enc_null),
  65939. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  65940. TEST_DECL(test_wolfSSL_EVP_mdc2),
  65941. TEST_DECL(test_evp_cipher_aes_gcm),
  65942. #endif
  65943. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  65944. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  65945. #ifdef OPENSSL_EXTRA
  65946. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  65947. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  65948. #endif
  65949. #ifdef OPENSSL_ALL
  65950. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  65951. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  65952. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  65953. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  65954. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  65955. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  65956. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  65957. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  65958. #endif
  65959. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  65960. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  65961. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  65962. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  65963. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  65964. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  65965. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  65966. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  65967. #ifndef NO_BIO
  65968. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  65969. #endif
  65970. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  65971. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  65972. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  65973. #ifndef NO_BIO
  65974. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  65975. #endif /* !NO_BIO */
  65976. #endif
  65977. #ifdef OPENSSL_ALL
  65978. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  65979. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  65980. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  65981. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  65982. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  65983. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  65984. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  65985. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  65986. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  65987. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  65988. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  65989. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  65990. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  65991. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  65992. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  65993. #endif
  65994. TEST_DECL(test_EVP_PKEY_rsa),
  65995. TEST_DECL(test_EVP_PKEY_ec),
  65996. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  65997. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  65998. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  65999. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  66000. TEST_DECL(test_EVP_PKEY_cmp),
  66001. #ifdef OPENSSL_ALL
  66002. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  66003. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  66004. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  66005. #endif
  66006. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  66007. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  66008. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  66009. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  66010. #endif
  66011. #ifndef NO_BIO
  66012. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  66013. TEST_DECL(test_wolfSSL_GetLoggingCb),
  66014. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  66015. TEST_DECL(test_wc_ERR_remove_state),
  66016. TEST_DECL(test_wc_ERR_print_errors_fp),
  66017. #endif
  66018. TEST_DECL(test_wolfSSL_configure_args),
  66019. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  66020. TEST_DECL(test_wolfSSL_set1_curves_list),
  66021. TEST_DECL(test_wolfSSL_curves_mismatch),
  66022. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  66023. TEST_DECL(test_wolfSSL_OtherName),
  66024. TEST_DECL(test_wolfSSL_FPKI),
  66025. TEST_DECL(test_wolfSSL_URI),
  66026. TEST_DECL(test_wolfSSL_TBS),
  66027. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  66028. TEST_DECL(test_X509_STORE_untrusted),
  66029. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  66030. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  66031. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  66032. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  66033. TEST_DECL(test_wolfSSL_X509_Name_canon),
  66034. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  66035. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  66036. TEST_DECL(test_wolfSSL_X509_NID),
  66037. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  66038. TEST_DECL(test_wolfSSL_get0_param),
  66039. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  66040. TEST_DECL(test_wolfSSL_set1_host),
  66041. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  66042. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  66043. TEST_DECL(test_wolfSSL_X509_STORE),
  66044. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  66045. TEST_DECL(test_X509_STORE_get0_objects),
  66046. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  66047. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  66048. TEST_DECL(test_wolfSSL_X509_STORE_set_get_crl),
  66049. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  66050. TEST_DECL(test_wolfSSL_X509_cmp_time),
  66051. TEST_DECL(test_wolfSSL_X509_time_adj),
  66052. /* X509 tests */
  66053. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  66054. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  66055. TEST_DECL(test_wolfSSL_X509_check_host),
  66056. TEST_DECL(test_wolfSSL_X509_check_email),
  66057. TEST_DECL(test_wolfSSL_X509_check_private_key),
  66058. TEST_DECL(test_wolfSSL_X509),
  66059. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  66060. TEST_DECL(test_wolfSSL_X509_sign),
  66061. TEST_DECL(test_wolfSSL_X509_sign2),
  66062. TEST_DECL(test_wolfSSL_X509_verify),
  66063. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  66064. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  66065. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  66066. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  66067. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  66068. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  66069. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  66070. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  66071. TEST_DECL(test_wolfSSL_X509_NAME),
  66072. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  66073. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  66074. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  66075. TEST_DECL(test_wolfSSL_X509_set_name),
  66076. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  66077. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  66078. TEST_DECL(test_wolfSSL_X509_set_version),
  66079. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  66080. TEST_DECL(test_wolfSSL_X509_CRL),
  66081. TEST_DECL(test_wolfSSL_i2d_X509),
  66082. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  66083. TEST_DECL(test_wolfSSL_PEM_read_X509),
  66084. TEST_DECL(test_wolfSSL_X509_check_ca),
  66085. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  66086. TEST_DECL(test_wolfSSL_X509_bad_altname),
  66087. TEST_DECL(test_wolfSSL_X509_name_match),
  66088. TEST_DECL(test_wolfSSL_X509_name_match2),
  66089. TEST_DECL(test_wolfSSL_X509_name_match3),
  66090. TEST_DECL(test_wolfSSL_X509_max_altnames),
  66091. TEST_DECL(test_wolfSSL_X509_max_name_constraints),
  66092. TEST_DECL(test_wolfSSL_make_cert),
  66093. #ifndef NO_BIO
  66094. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  66095. TEST_DECL(test_wolfSSL_X509_INFO),
  66096. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  66097. #endif
  66098. #ifdef OPENSSL_ALL
  66099. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  66100. #endif
  66101. TEST_DECL(test_wolfSSL_X509_CA_num),
  66102. TEST_DECL(test_wolfSSL_X509_get_version),
  66103. #ifndef NO_BIO
  66104. TEST_DECL(test_wolfSSL_X509_print),
  66105. TEST_DECL(test_wolfSSL_X509_CRL_print),
  66106. #endif
  66107. TEST_DECL(test_X509_get_signature_nid),
  66108. /* X509 extension testing. */
  66109. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  66110. TEST_DECL(test_wolfSSL_X509_get_ext),
  66111. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  66112. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  66113. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  66114. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  66115. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  66116. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  66117. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  66118. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  66119. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  66120. TEST_DECL(test_wolfSSL_X509V3_EXT),
  66121. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  66122. TEST_DECL(test_wolfSSL_X509_cmp),
  66123. TEST_DECL(test_GENERAL_NAME_set0_othername),
  66124. TEST_DECL(test_othername_and_SID_ext),
  66125. TEST_DECL(test_wolfSSL_dup_CA_list),
  66126. /* OpenSSL sk_X509 API test */
  66127. TEST_DECL(test_sk_X509),
  66128. /* OpenSSL sk_X509_CRL API test */
  66129. TEST_DECL(test_sk_X509_CRL),
  66130. /* OpenSSL X509 REQ API test */
  66131. TEST_DECL(test_X509_REQ),
  66132. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  66133. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  66134. TEST_DECL(test_X509_LOOKUP_add_dir),
  66135. /* RAND compatibility API */
  66136. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  66137. TEST_DECL(test_wolfSSL_RAND_bytes),
  66138. TEST_DECL(test_wolfSSL_RAND),
  66139. /* BN compatibility API */
  66140. TEST_DECL(test_wolfSSL_BN_CTX),
  66141. TEST_DECL(test_wolfSSL_BN),
  66142. TEST_DECL(test_wolfSSL_BN_init),
  66143. TEST_DECL(test_wolfSSL_BN_enc_dec),
  66144. TEST_DECL(test_wolfSSL_BN_word),
  66145. TEST_DECL(test_wolfSSL_BN_bits),
  66146. TEST_DECL(test_wolfSSL_BN_shift),
  66147. TEST_DECL(test_wolfSSL_BN_math),
  66148. TEST_DECL(test_wolfSSL_BN_math_mod),
  66149. TEST_DECL(test_wolfSSL_BN_math_other),
  66150. TEST_DECL(test_wolfSSL_BN_rand),
  66151. TEST_DECL(test_wolfSSL_BN_prime),
  66152. /* OpenSSL PKCS5 API test */
  66153. TEST_DECL(test_wolfSSL_PKCS5),
  66154. /* OpenSSL PKCS8 API test */
  66155. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  66156. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  66157. /* OpenSSL PKCS7 API test */
  66158. TEST_DECL(test_wolfssl_PKCS7),
  66159. TEST_DECL(test_wolfSSL_PKCS7_certs),
  66160. TEST_DECL(test_wolfSSL_PKCS7_sign),
  66161. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  66162. #ifndef NO_BIO
  66163. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  66164. #ifdef HAVE_SMIME
  66165. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  66166. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  66167. #endif /* HAVE_SMIME */
  66168. #endif /* !NO_BIO */
  66169. /* OpenSSL PKCS12 API test */
  66170. TEST_DECL(test_wolfSSL_PKCS12),
  66171. /* Can't memory test as callbacks use Assert. */
  66172. TEST_DECL(test_error_queue_per_thread),
  66173. TEST_DECL(test_wolfSSL_ERR_put_error),
  66174. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  66175. #ifndef NO_BIO
  66176. TEST_DECL(test_wolfSSL_ERR_print_errors),
  66177. #endif
  66178. TEST_DECL(test_OBJ_NAME_do_all),
  66179. TEST_DECL(test_wolfSSL_OBJ),
  66180. TEST_DECL(test_wolfSSL_OBJ_cmp),
  66181. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  66182. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  66183. #ifdef OPENSSL_ALL
  66184. TEST_DECL(test_wolfSSL_OBJ_ln),
  66185. TEST_DECL(test_wolfSSL_OBJ_sn),
  66186. #endif
  66187. #ifndef NO_BIO
  66188. TEST_DECL(test_wolfSSL_BIO_gets),
  66189. TEST_DECL(test_wolfSSL_BIO_puts),
  66190. TEST_DECL(test_wolfSSL_BIO_dump),
  66191. /* Can't memory test as server hangs. */
  66192. TEST_DECL(test_wolfSSL_BIO_should_retry),
  66193. TEST_DECL(test_wolfSSL_BIO_write),
  66194. TEST_DECL(test_wolfSSL_BIO_printf),
  66195. TEST_DECL(test_wolfSSL_BIO_f_md),
  66196. TEST_DECL(test_wolfSSL_BIO_up_ref),
  66197. TEST_DECL(test_wolfSSL_BIO_reset),
  66198. TEST_DECL(test_wolfSSL_BIO_get_len),
  66199. #endif
  66200. TEST_DECL(test_wolfSSL_check_domain),
  66201. TEST_DECL(test_wolfSSL_cert_cb),
  66202. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  66203. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  66204. TEST_DECL(test_wolfSSL_sigalg_info),
  66205. /* Can't memory test as tcp_connect aborts. */
  66206. TEST_DECL(test_wolfSSL_SESSION),
  66207. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  66208. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  66209. TEST_DECL(test_wolfSSL_ticket_keys),
  66210. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  66211. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  66212. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  66213. TEST_DECL(test_wolfSSL_verify_mode),
  66214. TEST_DECL(test_wolfSSL_verify_depth),
  66215. TEST_DECL(test_wolfSSL_verify_result),
  66216. TEST_DECL(test_wolfSSL_msg_callback),
  66217. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  66218. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  66219. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  66220. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  66221. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  66222. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  66223. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  66224. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  66225. TEST_DECL(test_wolfSSL_PEM_read),
  66226. TEST_DECL(test_wolfSSL_OpenSSL_version),
  66227. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  66228. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  66229. TEST_DECL(test_CONF_modules_xxx),
  66230. #ifdef OPENSSL_ALL
  66231. TEST_DECL(test_wolfSSL_TXT_DB),
  66232. TEST_DECL(test_wolfSSL_NCONF),
  66233. #endif
  66234. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  66235. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  66236. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  66237. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  66238. TEST_DECL(test_CRYPTO_THREADID_xxx),
  66239. TEST_DECL(test_ENGINE_cleanup),
  66240. /* test the no op functions for compatibility */
  66241. TEST_DECL(test_no_op_functions),
  66242. /* OpenSSL error API tests */
  66243. TEST_DECL(test_ERR_load_crypto_strings),
  66244. #ifdef OPENSSL_ALL
  66245. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  66246. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  66247. TEST_DECL(test_wolfSSL_CTX_ctrl),
  66248. #endif /* OPENSSL_ALL */
  66249. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  66250. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  66251. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  66252. /*********************************
  66253. * Crypto API tests
  66254. *********************************/
  66255. TEST_DECL(test_wolfSSL_MD4),
  66256. TEST_DECL(test_wolfSSL_MD5),
  66257. TEST_DECL(test_wolfSSL_MD5_Transform),
  66258. TEST_DECL(test_wolfSSL_SHA),
  66259. TEST_DECL(test_wolfSSL_SHA_Transform),
  66260. TEST_DECL(test_wolfSSL_SHA224),
  66261. TEST_DECL(test_wolfSSL_SHA256),
  66262. TEST_DECL(test_wolfSSL_SHA256_Transform),
  66263. TEST_DECL(test_wolfSSL_SHA512_Transform),
  66264. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  66265. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  66266. TEST_DECL(test_wolfSSL_HMAC_CTX),
  66267. TEST_DECL(test_wolfSSL_HMAC),
  66268. TEST_DECL(test_wolfSSL_CMAC),
  66269. TEST_DECL(test_wolfSSL_DES),
  66270. TEST_DECL(test_wolfSSL_DES_ncbc),
  66271. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  66272. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  66273. TEST_DECL(test_wolfSSL_AES_encrypt),
  66274. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  66275. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  66276. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  66277. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  66278. TEST_DECL(test_wolfSSL_RC4),
  66279. TEST_DECL(test_wolfSSL_RSA),
  66280. TEST_DECL(test_wolfSSL_RSA_DER),
  66281. TEST_DECL(test_wolfSSL_RSA_print),
  66282. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  66283. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  66284. TEST_DECL(test_wolfSSL_RSA_get0_key),
  66285. TEST_DECL(test_wolfSSL_RSA_meth),
  66286. TEST_DECL(test_wolfSSL_RSA_verify),
  66287. TEST_DECL(test_wolfSSL_RSA_sign),
  66288. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  66289. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  66290. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  66291. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  66292. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  66293. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  66294. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  66295. TEST_DECL(test_wolfSSL_RSA_ex_data),
  66296. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  66297. TEST_DECL(test_wolfSSL_RSA_To_Der),
  66298. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  66299. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  66300. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  66301. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  66302. TEST_DECL(test_wolfSSL_DH),
  66303. TEST_DECL(test_wolfSSL_DH_dup),
  66304. TEST_DECL(test_wolfSSL_DH_check),
  66305. TEST_DECL(test_wolfSSL_DH_prime),
  66306. TEST_DECL(test_wolfSSL_DH_1536_prime),
  66307. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  66308. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  66309. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  66310. TEST_DECL(test_wolfSSL_d2i_DHparams),
  66311. TEST_DECL(test_wolfSSL_DH_LoadDer),
  66312. TEST_DECL(test_wolfSSL_i2d_DHparams),
  66313. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  66314. TEST_DECL(test_wolfSSL_EC_GROUP),
  66315. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  66316. TEST_DECL(test_wolfSSL_EC_POINT),
  66317. TEST_DECL(test_wolfSSL_SPAKE),
  66318. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  66319. TEST_DECL(test_EC_i2d),
  66320. TEST_DECL(test_wolfSSL_EC_curve),
  66321. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  66322. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  66323. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  66324. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  66325. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  66326. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  66327. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  66328. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  66329. TEST_DECL(test_ECDSA_size_sign),
  66330. TEST_DECL(test_ECDH_compute_key),
  66331. #endif
  66332. #ifdef OPENSSL_EXTRA
  66333. TEST_DECL(test_EC25519),
  66334. TEST_DECL(test_ED25519),
  66335. TEST_DECL(test_EC448),
  66336. TEST_DECL(test_ED448),
  66337. #endif
  66338. TEST_DECL(test_DSA_do_sign_verify),
  66339. #ifdef OPENSSL_ALL
  66340. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  66341. TEST_DECL(test_wolfSSL_DSA_SIG),
  66342. #endif
  66343. TEST_DECL(test_openssl_generate_key_and_cert),
  66344. TEST_DECL(test_wolfSSL_FIPS_mode),
  66345. TEST_DECL(test_openssl_FIPS_drbg),
  66346. /*********************************
  66347. * CertManager API tests
  66348. *********************************/
  66349. TEST_DECL(test_wolfSSL_CertManagerAPI),
  66350. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  66351. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  66352. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  66353. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  66354. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  66355. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  66356. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  66357. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  66358. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  66359. TEST_DECL(test_wolfSSL_CertManagerCRL),
  66360. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  66361. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  66362. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  66363. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  66364. !defined(WOLFSSL_NO_CLIENT_AUTH))
  66365. TEST_DECL(test_various_pathlen_chains),
  66366. #endif
  66367. /*********************************
  66368. * SSL/TLS API tests
  66369. *********************************/
  66370. TEST_DECL(test_wolfSSL_Method_Allocators),
  66371. #ifndef NO_WOLFSSL_SERVER
  66372. TEST_DECL(test_wolfSSL_CTX_new),
  66373. #endif
  66374. TEST_DECL(test_server_wolfSSL_new),
  66375. TEST_DECL(test_client_wolfSSL_new),
  66376. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  66377. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  66378. TEST_DECL(test_for_double_Free),
  66379. #endif
  66380. TEST_DECL(test_wolfSSL_set_options),
  66381. #ifdef WOLFSSL_TLS13
  66382. /* TLS v1.3 API tests */
  66383. TEST_DECL(test_tls13_apis),
  66384. TEST_DECL(test_tls13_cipher_suites),
  66385. #endif
  66386. TEST_DECL(test_wolfSSL_tmp_dh),
  66387. TEST_DECL(test_wolfSSL_ctrl),
  66388. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  66389. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  66390. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  66391. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  66392. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  66393. #endif
  66394. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  66395. TEST_DECL(test_wolfSSL_security_level),
  66396. TEST_DECL(test_wolfSSL_SSL_in_init),
  66397. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  66398. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  66399. TEST_DECL(test_CONF_CTX_FILE),
  66400. TEST_DECL(test_CONF_CTX_CMDLINE),
  66401. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  66402. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  66403. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  66404. /* Bad certificate signature tests */
  66405. TEST_DECL(test_EccSigFailure_cm),
  66406. TEST_DECL(test_RsaSigFailure_cm),
  66407. #endif /* NO_CERTS */
  66408. /* PKCS8 testing */
  66409. TEST_DECL(test_wolfSSL_no_password_cb),
  66410. TEST_DECL(test_wolfSSL_PKCS8),
  66411. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  66412. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  66413. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  66414. TEST_DECL(test_wolfSSL_get_finished),
  66415. /* Uses Assert in handshake callback. */
  66416. TEST_DECL(test_wolfSSL_CTX_add_session),
  66417. /* Large number of memory allocations. */
  66418. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  66419. /* Large number of memory allocations. */
  66420. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  66421. /* Large number of memory allocations. */
  66422. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  66423. /* Large number of memory allocations. */
  66424. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  66425. /* Large number of memory allocations. */
  66426. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  66427. /* Large number of memory allocations. */
  66428. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  66429. #endif
  66430. TEST_DECL(test_SSL_CIPHER_get_xxx),
  66431. TEST_DECL(test_wolfSSL_ERR_strings),
  66432. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  66433. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  66434. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  66435. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  66436. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  66437. /* Large number of memory allocations. */
  66438. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  66439. TEST_DECL(test_wolfSSL_CertRsaPss),
  66440. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  66441. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  66442. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  66443. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  66444. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  66445. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  66446. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  66447. TEST_DECL(test_multiple_crls_same_issuer),
  66448. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  66449. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  66450. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  66451. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  66452. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  66453. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  66454. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  66455. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  66456. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  66457. TEST_DECL(test_SetTmpEC_DHE_Sz),
  66458. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  66459. #ifdef WOLFSSL_DTLS
  66460. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  66461. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  66462. #endif
  66463. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  66464. /* Uses Assert in handshake callback. */
  66465. TEST_DECL(test_wolfSSL_dtls_plaintext),
  66466. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  66467. defined(HAVE_IO_TESTS_DEPENDENCIES)
  66468. TEST_DECL(test_wolfSSL_read_write),
  66469. /* Can't memory test as server hangs if client fails before second connect.
  66470. */
  66471. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  66472. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  66473. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  66474. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  66475. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  66476. /* Can't memory test as server hangs. */
  66477. TEST_DECL(test_wolfSSL_dtls_export),
  66478. /* Uses Assert in handshake callback. */
  66479. TEST_DECL(test_wolfSSL_tls_export),
  66480. #endif
  66481. TEST_DECL(test_wolfSSL_dtls_export_peers),
  66482. TEST_DECL(test_wolfSSL_SetMinVersion),
  66483. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  66484. /* wolfSSL handshake APIs. */
  66485. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  66486. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  66487. TEST_DECL(test_wolfSSL_BUF),
  66488. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  66489. /* Can't memory test as server hangs. */
  66490. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  66491. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  66492. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  66493. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  66494. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  66495. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  66496. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  66497. /* Can't memory test as server hangs. */
  66498. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  66499. TEST_DECL(test_wolfSSL_Tls13_postauth),
  66500. TEST_DECL(test_wolfSSL_set_ecdh_auto),
  66501. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  66502. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  66503. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  66504. TEST_DECL(test_wolfSSL_THREADID_hash),
  66505. /* TLS extensions tests */
  66506. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  66507. #ifdef HAVE_SNI
  66508. TEST_DECL(test_wolfSSL_UseSNI_params),
  66509. /* Uses Assert in handshake callback. */
  66510. TEST_DECL(test_wolfSSL_UseSNI_connection),
  66511. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  66512. #endif /* HAVE_SNI */
  66513. #endif
  66514. TEST_DECL(test_wolfSSL_UseTrustedCA),
  66515. TEST_DECL(test_wolfSSL_UseMaxFragment),
  66516. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  66517. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  66518. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  66519. /* Uses Assert in handshake callback. */
  66520. TEST_DECL(test_wolfSSL_UseALPN_connection),
  66521. TEST_DECL(test_wolfSSL_UseALPN_params),
  66522. #endif
  66523. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  66524. /* Uses Assert in handshake callback. */
  66525. TEST_DECL(test_wolfSSL_set_alpn_protos),
  66526. #endif
  66527. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  66528. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  66529. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  66530. TEST_DECL(test_tls_ext_duplicate),
  66531. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  66532. defined(HAVE_IO_TESTS_DEPENDENCIES)
  66533. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  66534. /* Uses Assert in handshake callback. */
  66535. TEST_DECL(test_wolfSSL_Tls13_ECH),
  66536. #endif
  66537. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  66538. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  66539. /* OCSP Stapling */
  66540. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  66541. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  66542. TEST_DECL(test_self_signed_stapling),
  66543. /* Multicast */
  66544. TEST_DECL(test_wolfSSL_mcast),
  66545. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  66546. TEST_DECL(test_wolfSSL_msgCb),
  66547. TEST_DECL(test_wolfSSL_either_side),
  66548. TEST_DECL(test_wolfSSL_DTLS_either_side),
  66549. /* Uses Assert in handshake callback. */
  66550. TEST_DECL(test_wolfSSL_dtls_fragments),
  66551. /* Uses Assert in handshake callback. */
  66552. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  66553. /* Uses Assert in handshake callback. */
  66554. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  66555. /* Uses Assert in handshake callback. */
  66556. TEST_DECL(test_wolfSSL_dtls_bad_record),
  66557. /* Uses Assert in handshake callback. */
  66558. TEST_DECL(test_wolfSSL_dtls_stateless),
  66559. TEST_DECL(test_generate_cookie),
  66560. #ifndef NO_BIO
  66561. /* Can't memory test as server hangs. */
  66562. TEST_DECL(test_wolfSSL_BIO_connect),
  66563. /* Can't memory test as server Asserts in thread. */
  66564. TEST_DECL(test_wolfSSL_BIO_accept),
  66565. TEST_DECL(test_wolfSSL_BIO_tls),
  66566. #endif
  66567. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  66568. TEST_DECL(test_DhCallbacks),
  66569. #endif
  66570. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  66571. TEST_DECL(test_export_keying_material),
  66572. #endif
  66573. /* Can't memory test as client/server Asserts in thread. */
  66574. TEST_DECL(test_ticket_and_psk_mixing),
  66575. /* Can't memory test as client/server Asserts in thread. */
  66576. TEST_DECL(test_prioritize_psk),
  66577. /* Can't memory test as client/server hangs. */
  66578. TEST_DECL(test_wc_CryptoCb),
  66579. /* Can't memory test as client/server hangs. */
  66580. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  66581. #if !defined(NO_FILESYSTEM) && \
  66582. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  66583. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  66584. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  66585. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  66586. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  66587. #ifdef HAVE_MAX_FRAGMENT
  66588. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  66589. #endif /* HAVE_MAX_FRAGMENT */
  66590. #ifndef NO_RSA
  66591. TEST_DECL(test_wolfSSL_dtls_stateless2),
  66592. #if !defined(NO_OLD_TLS)
  66593. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  66594. #endif /* !defined(NO_OLD_TLS) */
  66595. #endif /* ! NO_RSA */
  66596. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  66597. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  66598. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  66599. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  66600. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  66601. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  66602. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  66603. && defined(WOLFSSL_TLS13) && \
  66604. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  66605. TEST_DECL(test_ticket_nonce_malloc),
  66606. #endif
  66607. TEST_DECL(test_ticket_ret_create),
  66608. TEST_DECL(test_wrong_cs_downgrade),
  66609. TEST_DECL(test_extra_alerts_wrong_cs),
  66610. TEST_DECL(test_extra_alerts_skip_hs),
  66611. TEST_DECL(test_extra_alerts_bad_psk),
  66612. TEST_DECL(test_tls13_bad_psk_binder),
  66613. /* Can't memory test as client/server Asserts. */
  66614. TEST_DECL(test_harden_no_secure_renegotiation),
  66615. TEST_DECL(test_override_alt_cert_chain),
  66616. TEST_DECL(test_rpk_set_xxx_cert_type),
  66617. TEST_DECL(test_tls13_rpk_handshake),
  66618. TEST_DECL(test_dtls13_bad_epoch_ch),
  66619. TEST_DECL(test_short_session_id),
  66620. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  66621. /* Can't memory test as client/server hangs. */
  66622. TEST_DECL(test_dtls_msg_from_other_peer),
  66623. TEST_DECL(test_dtls_ipv6_check),
  66624. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  66625. TEST_DECL(test_dtls_no_extensions),
  66626. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  66627. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  66628. TEST_DECL(test_session_ticket_no_id),
  66629. TEST_DECL(test_session_ticket_hs_update),
  66630. TEST_DECL(test_dtls_downgrade_scr_server),
  66631. TEST_DECL(test_dtls_downgrade_scr),
  66632. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  66633. TEST_DECL(test_dtls_client_hello_timeout),
  66634. TEST_DECL(test_dtls_dropped_ccs),
  66635. TEST_DECL(test_dtls_seq_num_downgrade),
  66636. TEST_DECL(test_certreq_sighash_algos),
  66637. TEST_DECL(test_revoked_loaded_int_cert),
  66638. TEST_DECL(test_dtls_frag_ch),
  66639. TEST_DECL(test_dtls13_frag_ch_pq),
  66640. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  66641. TEST_DECL(test_dtls_old_seq_number),
  66642. TEST_DECL(test_tls13_pq_groups),
  66643. TEST_DECL(test_tls13_early_data),
  66644. TEST_DECL(test_tls_multi_handshakes_one_record),
  66645. TEST_DECL(test_write_dup),
  66646. TEST_DECL(test_read_write_hs),
  66647. TEST_DECL(test_get_signature_nid),
  66648. TEST_DECL(test_tls_cert_store_unchanged),
  66649. TEST_DECL(test_wolfSSL_SendUserCanceled),
  66650. /* This test needs to stay at the end to clean up any caches allocated. */
  66651. TEST_DECL(test_wolfSSL_Cleanup)
  66652. };
  66653. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  66654. static void TestSetup(void)
  66655. {
  66656. /* Stub, for now. Add common test setup code here. */
  66657. }
  66658. static void TestCleanup(void)
  66659. {
  66660. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  66661. /* Clear any errors added to the error queue during the test run. */
  66662. wolfSSL_ERR_clear_error();
  66663. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  66664. }
  66665. /* Print out all API test cases with numeric identifier.
  66666. */
  66667. void ApiTest_PrintTestCases(void)
  66668. {
  66669. int i;
  66670. printf("All Test Cases:\n");
  66671. for (i = 0; i < TEST_CASE_CNT; i++) {
  66672. printf("%3d: %s\n", i + 1, testCases[i].name);
  66673. }
  66674. }
  66675. /* Add test case with index to the list to run.
  66676. *
  66677. * @param [in] idx Index of test case to run starting at 1.
  66678. * @return 0 on success.
  66679. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  66680. */
  66681. int ApiTest_RunIdx(int idx)
  66682. {
  66683. if (idx < 1 || idx > TEST_CASE_CNT) {
  66684. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  66685. return BAD_FUNC_ARG;
  66686. }
  66687. testAll = 0;
  66688. testCases[idx-1].run = 1;
  66689. return 0;
  66690. }
  66691. /* Add test case with name to the list to run.
  66692. *
  66693. * @param [in] name Name of test case to run.
  66694. * @return 0 on success.
  66695. * @return BAD_FUNC_ARG when name is not a known test case name.
  66696. */
  66697. int ApiTest_RunName(char* name)
  66698. {
  66699. int i;
  66700. for (i = 0; i < TEST_CASE_CNT; i++) {
  66701. if (XSTRCMP(testCases[i].name, name) == 0) {
  66702. testAll = 0;
  66703. testCases[i].run = 1;
  66704. return 0;
  66705. }
  66706. }
  66707. printf("Test case name not found: %s\n", name);
  66708. printf("Use --list to see all test case names.\n");
  66709. return BAD_FUNC_ARG;
  66710. }
  66711. /* Converts the result code to a string.
  66712. *
  66713. * @param [in] res Test result code.
  66714. * @return String describing test result.
  66715. */
  66716. static const char* apitest_res_string(int res)
  66717. {
  66718. const char* str = "invalid result";
  66719. switch (res) {
  66720. case TEST_SUCCESS:
  66721. str = "passed";
  66722. break;
  66723. case TEST_FAIL:
  66724. str = "failed";
  66725. break;
  66726. case TEST_SKIPPED:
  66727. str = "skipped";
  66728. break;
  66729. }
  66730. return str;
  66731. }
  66732. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  66733. static double gettime_secs(void)
  66734. #if defined(_MSC_VER) && defined(_WIN32)
  66735. {
  66736. /* there's no gettimeofday for Windows, so we'll use system time */
  66737. #define EPOCH_DIFF 11644473600LL
  66738. FILETIME currentFileTime;
  66739. GetSystemTimePreciseAsFileTime(&currentFileTime);
  66740. ULARGE_INTEGER uli = { 0, 0 };
  66741. uli.LowPart = currentFileTime.dwLowDateTime;
  66742. uli.HighPart = currentFileTime.dwHighDateTime;
  66743. /* Convert to seconds since Unix epoch */
  66744. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  66745. }
  66746. #else
  66747. {
  66748. struct timeval tv;
  66749. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  66750. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  66751. }
  66752. #endif
  66753. #endif
  66754. int ApiTest(void)
  66755. {
  66756. int i;
  66757. int ret;
  66758. int res = 0;
  66759. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  66760. double timeDiff;
  66761. #endif
  66762. printf(" Begin API Tests\n");
  66763. fflush(stdout);
  66764. /* we must perform init and cleanup if not all tests are running */
  66765. if (!testAll) {
  66766. #ifdef WOLFCRYPT_ONLY
  66767. if (wolfCrypt_Init() != 0) {
  66768. printf("wolfCrypt Initialization failed\n");
  66769. res = 1;
  66770. }
  66771. #else
  66772. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  66773. printf("wolfSSL Initialization failed\n");
  66774. res = 1;
  66775. }
  66776. #endif
  66777. }
  66778. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  66779. if (res == 0) {
  66780. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  66781. printf("failed to create tmp dir\n");
  66782. res = 1;
  66783. }
  66784. else {
  66785. tmpDirNameSet = 1;
  66786. }
  66787. }
  66788. #endif
  66789. if (res == 0) {
  66790. for (i = 0; i < TEST_CASE_CNT; ++i) {
  66791. EXPECT_DECLS;
  66792. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  66793. currentTestName = testCases[i].name;
  66794. #endif
  66795. /* When not testing all cases then skip if not marked for running.
  66796. */
  66797. if (!testAll && !testCases[i].run) {
  66798. continue;
  66799. }
  66800. TestSetup();
  66801. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  66802. fflush(stdout);
  66803. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  66804. timeDiff = gettime_secs();
  66805. #endif
  66806. ret = testCases[i].func();
  66807. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  66808. timeDiff = gettime_secs() - timeDiff;
  66809. #endif
  66810. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  66811. if (ret != TEST_SKIPPED) {
  66812. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  66813. }
  66814. else
  66815. #endif
  66816. {
  66817. printf(" %s\n", apitest_res_string(ret));
  66818. }
  66819. fflush(stdout);
  66820. /* if return code is < 0 and not skipped then assert error */
  66821. Expect((ret > 0 || ret == TEST_SKIPPED),
  66822. ("Test failed\n"),
  66823. ("ret %d", ret));
  66824. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  66825. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  66826. TestCleanup();
  66827. }
  66828. }
  66829. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  66830. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  66831. wc_ecc_fp_free(); /* free per thread cache */
  66832. #endif
  66833. if (!testAll) {
  66834. #ifdef WOLFCRYPT_ONLY
  66835. wolfCrypt_Cleanup();
  66836. #else
  66837. wolfSSL_Cleanup();
  66838. #endif
  66839. }
  66840. (void)testDevId;
  66841. if (res != 0) {
  66842. printf("\nFAILURES:\n");
  66843. for (i = 0; i < TEST_CASE_CNT; ++i) {
  66844. if (testCases[i].fail) {
  66845. printf(" %3d: %s\n", i + 1, testCases[i].name);
  66846. }
  66847. }
  66848. printf("\n");
  66849. fflush(stdout);
  66850. }
  66851. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  66852. if (tmpDirNameSet) {
  66853. printf("\nBinary dumps of the memio streams can be found in the\n"
  66854. "%s directory. This can be imported into\n"
  66855. "Wireshark by transforming the file with\n"
  66856. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  66857. "And then loading test_output.dump.hex into Wireshark using\n"
  66858. "the \"Import from Hex Dump...\" option and selecting the\n"
  66859. "TCP encapsulation option.\n", tmpDirName);
  66860. }
  66861. #endif
  66862. printf(" End API Tests\n");
  66863. fflush(stdout);
  66864. return res;
  66865. }