api.c 3.1 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048670496705067051670526705367054670556705667057670586705967060670616706267063670646706567066670676706867069670706707167072670736707467075670766707767078670796708067081670826708367084670856708667087670886708967090670916709267093670946709567096670976709867099671006710167102671036710467105671066710767108671096711067111671126711367114671156711667117671186711967120671216712267123671246712567126671276712867129671306713167132671336713467135671366713767138671396714067141671426714367144671456714667147671486714967150671516715267153671546715567156671576715867159671606716167162671636716467165671666716767168671696717067171671726717367174671756717667177671786717967180671816718267183671846718567186671876718867189671906719167192671936719467195671966719767198671996720067201672026720367204672056720667207672086720967210672116721267213672146721567216672176721867219672206722167222672236722467225672266722767228672296723067231672326723367234672356723667237672386723967240672416724267243672446724567246672476724867249672506725167252672536725467255672566725767258672596726067261672626726367264672656726667267672686726967270672716727267273672746727567276672776727867279672806728167282672836728467285672866728767288672896729067291672926729367294672956729667297672986729967300673016730267303673046730567306673076730867309673106731167312673136731467315673166731767318673196732067321673226732367324673256732667327673286732967330673316733267333673346733567336673376733867339673406734167342673436734467345673466734767348673496735067351673526735367354673556735667357673586735967360673616736267363673646736567366673676736867369673706737167372673736737467375673766737767378673796738067381673826738367384673856738667387673886738967390673916739267393673946739567396673976739867399674006740167402674036740467405674066740767408674096741067411674126741367414674156741667417674186741967420674216742267423674246742567426674276742867429674306743167432674336743467435674366743767438674396744067441674426744367444674456744667447674486744967450674516745267453674546745567456674576745867459674606746167462674636746467465674666746767468674696747067471674726747367474674756747667477674786747967480674816748267483674846748567486674876748867489674906749167492674936749467495674966749767498674996750067501675026750367504675056750667507675086750967510675116751267513675146751567516675176751867519675206752167522675236752467525675266752767528675296753067531675326753367534675356753667537675386753967540675416754267543675446754567546675476754867549675506755167552675536755467555675566755767558675596756067561675626756367564675656756667567675686756967570675716757267573675746757567576675776757867579675806758167582675836758467585675866758767588675896759067591675926759367594675956759667597675986759967600676016760267603676046760567606676076760867609676106761167612676136761467615676166761767618676196762067621676226762367624676256762667627676286762967630676316763267633676346763567636676376763867639676406764167642676436764467645676466764767648676496765067651676526765367654676556765667657676586765967660676616766267663676646766567666676676766867669676706767167672676736767467675676766767767678676796768067681676826768367684676856768667687676886768967690676916769267693676946769567696676976769867699677006770167702677036770467705677066770767708677096771067711677126771367714677156771667717677186771967720677216772267723677246772567726677276772867729677306773167732677336773467735677366773767738677396774067741677426774367744677456774667747677486774967750677516775267753677546775567756677576775867759677606776167762677636776467765677666776767768677696777067771677726777367774677756777667777677786777967780677816778267783677846778567786677876778867789677906779167792677936779467795677966779767798677996780067801678026780367804678056780667807678086780967810678116781267813678146781567816678176781867819678206782167822678236782467825678266782767828678296783067831678326783367834678356783667837678386783967840678416784267843678446784567846678476784867849678506785167852678536785467855678566785767858678596786067861678626786367864678656786667867678686786967870678716787267873678746787567876678776787867879678806788167882678836788467885678866788767888678896789067891678926789367894678956789667897678986789967900679016790267903679046790567906679076790867909679106791167912679136791467915679166791767918679196792067921679226792367924679256792667927679286792967930679316793267933679346793567936679376793867939679406794167942679436794467945679466794767948679496795067951679526795367954679556795667957679586795967960679616796267963679646796567966679676796867969679706797167972679736797467975679766797767978679796798067981679826798367984679856798667987679886798967990679916799267993679946799567996679976799867999680006800168002680036800468005680066800768008680096801068011680126801368014680156801668017680186801968020680216802268023680246802568026680276802868029680306803168032680336803468035680366803768038680396804068041680426804368044680456804668047680486804968050680516805268053680546805568056680576805868059680606806168062680636806468065680666806768068680696807068071680726807368074680756807668077680786807968080680816808268083680846808568086680876808868089680906809168092680936809468095680966809768098680996810068101681026810368104681056810668107681086810968110681116811268113681146811568116681176811868119681206812168122681236812468125681266812768128681296813068131681326813368134681356813668137681386813968140681416814268143681446814568146681476814868149681506815168152681536815468155681566815768158681596816068161681626816368164681656816668167681686816968170681716817268173681746817568176681776817868179681806818168182681836818468185681866818768188681896819068191681926819368194681956819668197681986819968200682016820268203682046820568206682076820868209682106821168212682136821468215682166821768218682196822068221682226822368224682256822668227682286822968230682316823268233682346823568236682376823868239682406824168242682436824468245682466824768248682496825068251682526825368254682556825668257682586825968260682616826268263682646826568266682676826868269682706827168272682736827468275682766827768278682796828068281682826828368284682856828668287682886828968290682916829268293682946829568296682976829868299683006830168302683036830468305683066830768308683096831068311683126831368314683156831668317683186831968320683216832268323683246832568326683276832868329683306833168332683336833468335683366833768338683396834068341683426834368344683456834668347683486834968350683516835268353683546835568356683576835868359683606836168362683636836468365683666836768368683696837068371683726837368374683756837668377683786837968380683816838268383683846838568386683876838868389683906839168392683936839468395683966839768398683996840068401684026840368404684056840668407684086840968410684116841268413684146841568416684176841868419684206842168422684236842468425684266842768428684296843068431684326843368434684356843668437684386843968440684416844268443684446844568446684476844868449684506845168452684536845468455684566845768458684596846068461684626846368464684656846668467684686846968470684716847268473684746847568476684776847868479684806848168482684836848468485684866848768488684896849068491684926849368494684956849668497684986849968500685016850268503685046850568506685076850868509685106851168512685136851468515685166851768518685196852068521685226852368524685256852668527685286852968530685316853268533685346853568536685376853868539685406854168542685436854468545685466854768548685496855068551685526855368554685556855668557685586855968560685616856268563685646856568566685676856868569685706857168572685736857468575685766857768578685796858068581685826858368584685856858668587685886858968590685916859268593685946859568596685976859868599686006860168602686036860468605686066860768608686096861068611686126861368614686156861668617686186861968620686216862268623686246862568626686276862868629686306863168632686336863468635686366863768638686396864068641686426864368644686456864668647686486864968650686516865268653686546865568656686576865868659686606866168662686636866468665686666866768668686696867068671686726867368674686756867668677686786867968680686816868268683686846868568686686876868868689686906869168692686936869468695686966869768698686996870068701687026870368704687056870668707687086870968710687116871268713687146871568716687176871868719687206872168722687236872468725687266872768728687296873068731687326873368734687356873668737687386873968740687416874268743687446874568746687476874868749687506875168752687536875468755687566875768758687596876068761687626876368764687656876668767687686876968770687716877268773687746877568776687776877868779687806878168782687836878468785687866878768788687896879068791687926879368794687956879668797687986879968800688016880268803688046880568806688076880868809688106881168812688136881468815688166881768818688196882068821688226882368824688256882668827688286882968830688316883268833688346883568836688376883868839688406884168842688436884468845688466884768848688496885068851688526885368854688556885668857688586885968860688616886268863688646886568866688676886868869688706887168872688736887468875688766887768878688796888068881688826888368884688856888668887688886888968890688916889268893688946889568896688976889868899689006890168902689036890468905689066890768908689096891068911689126891368914689156891668917689186891968920689216892268923689246892568926689276892868929689306893168932689336893468935689366893768938689396894068941689426894368944689456894668947689486894968950689516895268953689546895568956689576895868959689606896168962689636896468965689666896768968689696897068971689726897368974689756897668977689786897968980689816898268983689846898568986689876898868989689906899168992689936899468995689966899768998689996900069001690026900369004690056900669007690086900969010690116901269013690146901569016690176901869019690206902169022690236902469025690266902769028690296903069031690326903369034690356903669037690386903969040690416904269043690446904569046690476904869049690506905169052690536905469055690566905769058690596906069061690626906369064690656906669067690686906969070690716907269073690746907569076690776907869079690806908169082690836908469085690866908769088690896909069091690926909369094690956909669097690986909969100691016910269103691046910569106691076910869109691106911169112691136911469115691166911769118691196912069121691226912369124691256912669127691286912969130691316913269133691346913569136691376913869139691406914169142691436914469145691466914769148691496915069151691526915369154691556915669157691586915969160691616916269163691646916569166691676916869169691706917169172691736917469175691766917769178691796918069181691826918369184691856918669187691886918969190691916919269193691946919569196691976919869199692006920169202692036920469205692066920769208692096921069211692126921369214692156921669217692186921969220692216922269223692246922569226692276922869229692306923169232692336923469235692366923769238692396924069241692426924369244692456924669247692486924969250692516925269253692546925569256692576925869259692606926169262692636926469265692666926769268692696927069271692726927369274692756927669277692786927969280692816928269283692846928569286692876928869289692906929169292692936929469295692966929769298692996930069301693026930369304693056930669307693086930969310693116931269313693146931569316693176931869319693206932169322693236932469325693266932769328693296933069331693326933369334693356933669337693386933969340693416934269343693446934569346693476934869349693506935169352693536935469355693566935769358693596936069361693626936369364693656936669367693686936969370693716937269373693746937569376693776937869379693806938169382693836938469385693866938769388693896939069391693926939369394693956939669397693986939969400694016940269403694046940569406694076940869409694106941169412694136941469415694166941769418694196942069421694226942369424694256942669427694286942969430694316943269433694346943569436694376943869439694406944169442694436944469445694466944769448694496945069451694526945369454694556945669457694586945969460694616946269463694646946569466694676946869469694706947169472694736947469475694766947769478694796948069481694826948369484694856948669487694886948969490694916949269493694946949569496694976949869499695006950169502695036950469505695066950769508695096951069511695126951369514695156951669517695186951969520695216952269523695246952569526695276952869529695306953169532695336953469535695366953769538695396954069541695426954369544695456954669547695486954969550695516955269553695546955569556695576955869559695606956169562695636956469565695666956769568695696957069571695726957369574695756957669577695786957969580695816958269583695846958569586695876958869589695906959169592695936959469595695966959769598695996960069601696026960369604696056960669607696086960969610696116961269613696146961569616696176961869619696206962169622696236962469625696266962769628696296963069631696326963369634696356963669637696386963969640696416964269643696446964569646696476964869649696506965169652696536965469655696566965769658696596966069661696626966369664696656966669667696686966969670696716967269673696746967569676696776967869679696806968169682696836968469685696866968769688696896969069691696926969369694696956969669697696986969969700697016970269703697046970569706697076970869709697106971169712697136971469715697166971769718697196972069721697226972369724697256972669727697286972969730697316973269733697346973569736697376973869739697406974169742697436974469745697466974769748697496975069751697526975369754697556975669757697586975969760697616976269763697646976569766697676976869769697706977169772697736977469775697766977769778697796978069781697826978369784697856978669787697886978969790697916979269793697946979569796697976979869799698006980169802698036980469805698066980769808698096981069811698126981369814698156981669817698186981969820698216982269823698246982569826698276982869829698306983169832698336983469835698366983769838698396984069841698426984369844698456984669847698486984969850698516985269853698546985569856698576985869859698606986169862698636986469865698666986769868698696987069871698726987369874698756987669877698786987969880698816988269883698846988569886698876988869889698906989169892698936989469895698966989769898698996990069901699026990369904699056990669907699086990969910699116991269913699146991569916699176991869919699206992169922699236992469925699266992769928699296993069931699326993369934699356993669937699386993969940699416994269943699446994569946699476994869949699506995169952699536995469955699566995769958699596996069961699626996369964699656996669967699686996969970699716997269973699746997569976699776997869979699806998169982699836998469985699866998769988699896999069991699926999369994699956999669997699986999970000700017000270003700047000570006700077000870009700107001170012700137001470015700167001770018700197002070021700227002370024700257002670027700287002970030700317003270033700347003570036700377003870039700407004170042700437004470045700467004770048700497005070051700527005370054700557005670057700587005970060700617006270063700647006570066700677006870069700707007170072700737007470075700767007770078700797008070081700827008370084700857008670087700887008970090700917009270093700947009570096700977009870099701007010170102701037010470105701067010770108701097011070111701127011370114701157011670117701187011970120701217012270123701247012570126701277012870129701307013170132701337013470135701367013770138701397014070141701427014370144701457014670147701487014970150701517015270153701547015570156701577015870159701607016170162701637016470165701667016770168701697017070171701727017370174701757017670177701787017970180701817018270183701847018570186701877018870189701907019170192701937019470195701967019770198701997020070201702027020370204702057020670207702087020970210702117021270213702147021570216702177021870219702207022170222702237022470225702267022770228702297023070231702327023370234702357023670237702387023970240702417024270243702447024570246702477024870249702507025170252702537025470255702567025770258702597026070261702627026370264702657026670267702687026970270702717027270273702747027570276702777027870279702807028170282702837028470285702867028770288702897029070291702927029370294702957029670297702987029970300703017030270303703047030570306703077030870309703107031170312703137031470315703167031770318703197032070321703227032370324703257032670327703287032970330703317033270333703347033570336703377033870339703407034170342703437034470345703467034770348703497035070351703527035370354703557035670357703587035970360703617036270363703647036570366703677036870369703707037170372703737037470375703767037770378703797038070381703827038370384703857038670387703887038970390703917039270393703947039570396703977039870399704007040170402704037040470405704067040770408704097041070411704127041370414704157041670417704187041970420704217042270423704247042570426704277042870429704307043170432704337043470435704367043770438704397044070441704427044370444704457044670447704487044970450704517045270453704547045570456704577045870459704607046170462704637046470465704667046770468704697047070471704727047370474704757047670477704787047970480704817048270483704847048570486704877048870489704907049170492704937049470495704967049770498704997050070501705027050370504705057050670507705087050970510705117051270513705147051570516705177051870519705207052170522705237052470525705267052770528705297053070531705327053370534705357053670537705387053970540705417054270543705447054570546705477054870549705507055170552705537055470555705567055770558705597056070561705627056370564705657056670567705687056970570705717057270573705747057570576705777057870579705807058170582705837058470585705867058770588705897059070591705927059370594705957059670597705987059970600706017060270603706047060570606706077060870609706107061170612706137061470615706167061770618706197062070621706227062370624706257062670627706287062970630706317063270633706347063570636706377063870639706407064170642706437064470645706467064770648706497065070651706527065370654706557065670657706587065970660706617066270663706647066570666706677066870669706707067170672706737067470675706767067770678706797068070681706827068370684706857068670687706887068970690706917069270693706947069570696706977069870699707007070170702707037070470705707067070770708707097071070711707127071370714707157071670717707187071970720707217072270723707247072570726707277072870729707307073170732707337073470735707367073770738707397074070741707427074370744707457074670747707487074970750707517075270753707547075570756707577075870759707607076170762707637076470765707667076770768707697077070771707727077370774707757077670777707787077970780707817078270783707847078570786707877078870789707907079170792707937079470795707967079770798707997080070801708027080370804708057080670807708087080970810708117081270813708147081570816708177081870819708207082170822708237082470825708267082770828708297083070831708327083370834708357083670837708387083970840708417084270843708447084570846708477084870849708507085170852708537085470855708567085770858708597086070861708627086370864708657086670867708687086970870708717087270873708747087570876708777087870879708807088170882708837088470885708867088770888708897089070891708927089370894708957089670897708987089970900709017090270903709047090570906709077090870909709107091170912709137091470915709167091770918709197092070921709227092370924709257092670927709287092970930709317093270933709347093570936709377093870939709407094170942709437094470945709467094770948709497095070951709527095370954709557095670957709587095970960709617096270963709647096570966709677096870969709707097170972709737097470975709767097770978709797098070981709827098370984709857098670987709887098970990709917099270993709947099570996709977099870999710007100171002710037100471005710067100771008710097101071011710127101371014710157101671017710187101971020710217102271023710247102571026710277102871029710307103171032710337103471035710367103771038710397104071041710427104371044710457104671047710487104971050710517105271053710547105571056710577105871059710607106171062710637106471065710667106771068710697107071071710727107371074710757107671077710787107971080710817108271083710847108571086710877108871089710907109171092710937109471095710967109771098710997110071101711027110371104711057110671107711087110971110711117111271113711147111571116711177111871119711207112171122711237112471125711267112771128711297113071131711327113371134711357113671137711387113971140711417114271143711447114571146711477114871149711507115171152711537115471155711567115771158711597116071161711627116371164711657116671167711687116971170711717117271173711747117571176711777117871179711807118171182711837118471185711867118771188711897119071191711927119371194711957119671197711987119971200712017120271203712047120571206712077120871209712107121171212712137121471215712167121771218712197122071221712227122371224712257122671227712287122971230712317123271233712347123571236712377123871239712407124171242712437124471245712467124771248712497125071251712527125371254712557125671257712587125971260712617126271263712647126571266712677126871269712707127171272712737127471275712767127771278712797128071281712827128371284712857128671287712887128971290712917129271293712947129571296712977129871299713007130171302713037130471305713067130771308713097131071311713127131371314713157131671317713187131971320713217132271323713247132571326713277132871329713307133171332713337133471335713367133771338713397134071341713427134371344713457134671347713487134971350713517135271353713547135571356713577135871359713607136171362713637136471365713667136771368713697137071371713727137371374713757137671377713787137971380713817138271383713847138571386713877138871389713907139171392713937139471395713967139771398713997140071401714027140371404714057140671407714087140971410714117141271413714147141571416714177141871419714207142171422714237142471425714267142771428714297143071431714327143371434714357143671437714387143971440714417144271443714447144571446714477144871449714507145171452714537145471455714567145771458714597146071461714627146371464714657146671467714687146971470714717147271473714747147571476714777147871479714807148171482714837148471485714867148771488714897149071491714927149371494714957149671497714987149971500715017150271503715047150571506715077150871509715107151171512715137151471515715167151771518715197152071521715227152371524715257152671527715287152971530715317153271533715347153571536715377153871539715407154171542715437154471545715467154771548715497155071551715527155371554715557155671557715587155971560715617156271563715647156571566715677156871569715707157171572715737157471575715767157771578715797158071581715827158371584715857158671587715887158971590715917159271593715947159571596715977159871599716007160171602716037160471605716067160771608716097161071611716127161371614716157161671617716187161971620716217162271623716247162571626716277162871629716307163171632716337163471635716367163771638716397164071641716427164371644716457164671647716487164971650716517165271653716547165571656716577165871659716607166171662716637166471665716667166771668716697167071671716727167371674716757167671677716787167971680716817168271683716847168571686716877168871689716907169171692716937169471695716967169771698716997170071701717027170371704717057170671707717087170971710717117171271713717147171571716717177171871719717207172171722717237172471725717267172771728717297173071731717327173371734717357173671737717387173971740717417174271743717447174571746717477174871749717507175171752717537175471755717567175771758717597176071761717627176371764717657176671767717687176971770717717177271773717747177571776717777177871779717807178171782717837178471785717867178771788717897179071791717927179371794717957179671797717987179971800718017180271803718047180571806718077180871809718107181171812718137181471815718167181771818718197182071821718227182371824718257182671827718287182971830718317183271833718347183571836718377183871839718407184171842718437184471845718467184771848718497185071851718527185371854718557185671857718587185971860718617186271863718647186571866718677186871869718707187171872718737187471875718767187771878718797188071881718827188371884718857188671887718887188971890718917189271893718947189571896718977189871899719007190171902719037190471905719067190771908719097191071911719127191371914719157191671917719187191971920719217192271923719247192571926719277192871929719307193171932719337193471935719367193771938719397194071941719427194371944719457194671947719487194971950719517195271953719547195571956719577195871959719607196171962719637196471965719667196771968719697197071971719727197371974719757197671977719787197971980719817198271983719847198571986719877198871989719907199171992719937199471995719967199771998719997200072001720027200372004720057200672007720087200972010720117201272013720147201572016720177201872019720207202172022720237202472025720267202772028720297203072031720327203372034720357203672037720387203972040720417204272043720447204572046720477204872049720507205172052720537205472055720567205772058720597206072061720627206372064720657206672067720687206972070720717207272073720747207572076720777207872079720807208172082720837208472085720867208772088720897209072091720927209372094720957209672097720987209972100721017210272103721047210572106721077210872109721107211172112721137211472115721167211772118721197212072121721227212372124721257212672127721287212972130721317213272133721347213572136721377213872139721407214172142721437214472145721467214772148721497215072151721527215372154721557215672157721587215972160721617216272163721647216572166721677216872169721707217172172721737217472175721767217772178721797218072181721827218372184721857218672187721887218972190721917219272193721947219572196721977219872199722007220172202722037220472205722067220772208722097221072211722127221372214722157221672217722187221972220722217222272223722247222572226722277222872229722307223172232722337223472235722367223772238722397224072241722427224372244722457224672247722487224972250722517225272253722547225572256722577225872259722607226172262722637226472265722667226772268722697227072271722727227372274722757227672277722787227972280722817228272283722847228572286722877228872289722907229172292722937229472295722967229772298722997230072301723027230372304723057230672307723087230972310723117231272313723147231572316723177231872319723207232172322723237232472325723267232772328723297233072331723327233372334723357233672337723387233972340723417234272343723447234572346723477234872349723507235172352723537235472355723567235772358723597236072361723627236372364723657236672367723687236972370723717237272373723747237572376723777237872379723807238172382723837238472385723867238772388723897239072391723927239372394723957239672397723987239972400724017240272403724047240572406724077240872409724107241172412724137241472415724167241772418724197242072421724227242372424724257242672427724287242972430724317243272433724347243572436724377243872439724407244172442724437244472445724467244772448724497245072451724527245372454724557245672457724587245972460724617246272463724647246572466724677246872469724707247172472724737247472475724767247772478724797248072481724827248372484724857248672487724887248972490724917249272493724947249572496724977249872499725007250172502725037250472505725067250772508725097251072511725127251372514725157251672517725187251972520725217252272523725247252572526725277252872529725307253172532725337253472535725367253772538725397254072541725427254372544725457254672547725487254972550725517255272553725547255572556725577255872559725607256172562725637256472565725667256772568725697257072571725727257372574725757257672577725787257972580725817258272583725847258572586725877258872589725907259172592725937259472595725967259772598725997260072601726027260372604726057260672607726087260972610726117261272613726147261572616726177261872619726207262172622726237262472625726267262772628726297263072631726327263372634726357263672637726387263972640726417264272643726447264572646726477264872649726507265172652726537265472655726567265772658726597266072661726627266372664726657266672667726687266972670726717267272673726747267572676726777267872679726807268172682726837268472685726867268772688726897269072691726927269372694726957269672697726987269972700727017270272703727047270572706727077270872709727107271172712727137271472715727167271772718727197272072721727227272372724727257272672727727287272972730727317273272733727347273572736727377273872739727407274172742727437274472745727467274772748727497275072751727527275372754727557275672757727587275972760727617276272763727647276572766727677276872769727707277172772727737277472775727767277772778727797278072781727827278372784727857278672787727887278972790727917279272793727947279572796727977279872799728007280172802728037280472805728067280772808728097281072811728127281372814728157281672817728187281972820728217282272823728247282572826728277282872829728307283172832728337283472835728367283772838728397284072841728427284372844728457284672847728487284972850728517285272853728547285572856728577285872859728607286172862728637286472865728667286772868728697287072871728727287372874728757287672877728787287972880728817288272883728847288572886728877288872889728907289172892728937289472895728967289772898728997290072901729027290372904729057290672907729087290972910729117291272913729147291572916729177291872919729207292172922729237292472925729267292772928729297293072931729327293372934729357293672937729387293972940729417294272943729447294572946729477294872949729507295172952729537295472955729567295772958729597296072961729627296372964729657296672967729687296972970729717297272973729747297572976729777297872979729807298172982729837298472985729867298772988729897299072991729927299372994729957299672997729987299973000730017300273003730047300573006730077300873009730107301173012730137301473015730167301773018730197302073021730227302373024730257302673027730287302973030730317303273033730347303573036730377303873039730407304173042730437304473045730467304773048730497305073051730527305373054730557305673057730587305973060730617306273063730647306573066730677306873069730707307173072730737307473075730767307773078730797308073081730827308373084730857308673087730887308973090730917309273093730947309573096730977309873099731007310173102731037310473105731067310773108731097311073111731127311373114731157311673117731187311973120731217312273123731247312573126731277312873129731307313173132731337313473135731367313773138731397314073141731427314373144731457314673147731487314973150731517315273153731547315573156731577315873159731607316173162731637316473165731667316773168731697317073171731727317373174731757317673177731787317973180731817318273183731847318573186731877318873189731907319173192731937319473195731967319773198731997320073201732027320373204732057320673207732087320973210732117321273213732147321573216732177321873219732207322173222732237322473225732267322773228732297323073231732327323373234732357323673237732387323973240732417324273243732447324573246732477324873249732507325173252732537325473255732567325773258732597326073261732627326373264732657326673267732687326973270732717327273273732747327573276732777327873279732807328173282732837328473285732867328773288732897329073291732927329373294732957329673297732987329973300733017330273303733047330573306733077330873309733107331173312733137331473315733167331773318733197332073321733227332373324733257332673327733287332973330733317333273333733347333573336733377333873339733407334173342733437334473345733467334773348733497335073351733527335373354733557335673357733587335973360733617336273363733647336573366733677336873369733707337173372733737337473375733767337773378733797338073381733827338373384733857338673387733887338973390733917339273393733947339573396733977339873399734007340173402734037340473405734067340773408734097341073411734127341373414734157341673417734187341973420734217342273423734247342573426734277342873429734307343173432734337343473435734367343773438734397344073441734427344373444734457344673447734487344973450734517345273453734547345573456734577345873459734607346173462734637346473465734667346773468734697347073471734727347373474734757347673477734787347973480734817348273483734847348573486734877348873489734907349173492734937349473495734967349773498734997350073501735027350373504735057350673507735087350973510735117351273513735147351573516735177351873519735207352173522735237352473525735267352773528735297353073531735327353373534735357353673537735387353973540735417354273543735447354573546735477354873549735507355173552735537355473555735567355773558735597356073561735627356373564735657356673567735687356973570735717357273573735747357573576735777357873579735807358173582735837358473585735867358773588735897359073591735927359373594735957359673597735987359973600736017360273603736047360573606736077360873609736107361173612736137361473615736167361773618736197362073621736227362373624736257362673627736287362973630736317363273633736347363573636736377363873639736407364173642736437364473645736467364773648736497365073651736527365373654736557365673657736587365973660736617366273663736647366573666736677366873669736707367173672736737367473675736767367773678736797368073681736827368373684736857368673687736887368973690736917369273693736947369573696736977369873699737007370173702737037370473705737067370773708737097371073711737127371373714737157371673717737187371973720737217372273723737247372573726737277372873729737307373173732737337373473735737367373773738737397374073741737427374373744737457374673747737487374973750737517375273753737547375573756737577375873759737607376173762737637376473765737667376773768737697377073771737727377373774737757377673777737787377973780737817378273783737847378573786737877378873789737907379173792737937379473795737967379773798737997380073801738027380373804738057380673807738087380973810738117381273813738147381573816738177381873819738207382173822738237382473825738267382773828738297383073831738327383373834738357383673837738387383973840738417384273843738447384573846738477384873849738507385173852738537385473855738567385773858738597386073861738627386373864738657386673867738687386973870738717387273873738747387573876738777387873879738807388173882738837388473885738867388773888738897389073891738927389373894738957389673897738987389973900739017390273903739047390573906739077390873909739107391173912739137391473915739167391773918739197392073921739227392373924739257392673927739287392973930739317393273933739347393573936739377393873939739407394173942739437394473945739467394773948739497395073951739527395373954739557395673957739587395973960739617396273963739647396573966739677396873969739707397173972739737397473975739767397773978739797398073981739827398373984739857398673987739887398973990739917399273993739947399573996739977399873999740007400174002740037400474005740067400774008740097401074011740127401374014740157401674017740187401974020740217402274023740247402574026740277402874029740307403174032740337403474035740367403774038740397404074041740427404374044740457404674047740487404974050740517405274053740547405574056740577405874059740607406174062740637406474065740667406774068740697407074071740727407374074740757407674077740787407974080740817408274083740847408574086740877408874089740907409174092740937409474095740967409774098740997410074101741027410374104741057410674107741087410974110741117411274113741147411574116741177411874119741207412174122741237412474125741267412774128741297413074131741327413374134741357413674137741387413974140741417414274143741447414574146741477414874149741507415174152741537415474155741567415774158741597416074161741627416374164741657416674167741687416974170741717417274173741747417574176741777417874179741807418174182741837418474185741867418774188741897419074191741927419374194741957419674197741987419974200742017420274203742047420574206742077420874209742107421174212742137421474215742167421774218742197422074221742227422374224742257422674227742287422974230742317423274233742347423574236742377423874239742407424174242742437424474245742467424774248742497425074251742527425374254742557425674257742587425974260742617426274263742647426574266742677426874269742707427174272742737427474275742767427774278742797428074281742827428374284742857428674287742887428974290742917429274293742947429574296742977429874299743007430174302743037430474305743067430774308743097431074311743127431374314743157431674317743187431974320743217432274323743247432574326743277432874329743307433174332743337433474335743367433774338743397434074341743427434374344743457434674347743487434974350743517435274353743547435574356743577435874359743607436174362743637436474365743667436774368743697437074371743727437374374743757437674377743787437974380743817438274383743847438574386743877438874389743907439174392743937439474395743967439774398743997440074401744027440374404744057440674407744087440974410744117441274413744147441574416744177441874419744207442174422744237442474425744267442774428744297443074431744327443374434744357443674437744387443974440744417444274443744447444574446744477444874449744507445174452744537445474455744567445774458744597446074461744627446374464744657446674467744687446974470744717447274473744747447574476744777447874479744807448174482744837448474485744867448774488744897449074491744927449374494744957449674497744987449974500745017450274503745047450574506745077450874509745107451174512745137451474515745167451774518745197452074521745227452374524745257452674527745287452974530745317453274533745347453574536745377453874539745407454174542745437454474545745467454774548745497455074551745527455374554745557455674557745587455974560745617456274563745647456574566745677456874569745707457174572745737457474575745767457774578745797458074581745827458374584745857458674587745887458974590745917459274593745947459574596745977459874599746007460174602746037460474605746067460774608746097461074611746127461374614746157461674617746187461974620746217462274623746247462574626746277462874629746307463174632746337463474635746367463774638746397464074641746427464374644746457464674647746487464974650746517465274653746547465574656746577465874659746607466174662746637466474665746667466774668746697467074671746727467374674746757467674677746787467974680746817468274683746847468574686746877468874689746907469174692746937469474695746967469774698746997470074701747027470374704747057470674707747087470974710747117471274713747147471574716747177471874719747207472174722747237472474725747267472774728747297473074731747327473374734747357473674737747387473974740747417474274743747447474574746747477474874749747507475174752747537475474755747567475774758747597476074761747627476374764747657476674767747687476974770747717477274773747747477574776747777477874779747807478174782747837478474785747867478774788747897479074791747927479374794747957479674797747987479974800748017480274803748047480574806748077480874809748107481174812748137481474815748167481774818748197482074821748227482374824748257482674827748287482974830748317483274833748347483574836748377483874839748407484174842748437484474845748467484774848748497485074851748527485374854748557485674857748587485974860748617486274863748647486574866748677486874869748707487174872748737487474875748767487774878748797488074881748827488374884748857488674887748887488974890748917489274893748947489574896748977489874899749007490174902749037490474905749067490774908749097491074911749127491374914749157491674917749187491974920749217492274923749247492574926749277492874929749307493174932749337493474935749367493774938749397494074941749427494374944749457494674947749487494974950749517495274953749547495574956749577495874959749607496174962749637496474965749667496774968749697497074971749727497374974749757497674977749787497974980749817498274983749847498574986749877498874989749907499174992749937499474995749967499774998749997500075001750027500375004750057500675007750087500975010750117501275013750147501575016750177501875019750207502175022750237502475025750267502775028750297503075031750327503375034750357503675037750387503975040750417504275043750447504575046750477504875049750507505175052750537505475055750567505775058750597506075061750627506375064750657506675067750687506975070750717507275073750747507575076750777507875079750807508175082750837508475085750867508775088750897509075091750927509375094750957509675097750987509975100751017510275103751047510575106751077510875109751107511175112751137511475115751167511775118751197512075121751227512375124751257512675127751287512975130751317513275133751347513575136751377513875139751407514175142751437514475145751467514775148751497515075151751527515375154751557515675157751587515975160751617516275163751647516575166751677516875169751707517175172751737517475175751767517775178751797518075181751827518375184751857518675187751887518975190751917519275193751947519575196751977519875199752007520175202752037520475205752067520775208752097521075211752127521375214752157521675217752187521975220752217522275223752247522575226752277522875229752307523175232752337523475235752367523775238752397524075241752427524375244752457524675247752487524975250752517525275253752547525575256752577525875259752607526175262752637526475265752667526775268752697527075271752727527375274752757527675277752787527975280752817528275283752847528575286752877528875289752907529175292752937529475295752967529775298752997530075301753027530375304753057530675307753087530975310753117531275313753147531575316753177531875319753207532175322753237532475325753267532775328753297533075331753327533375334753357533675337753387533975340753417534275343753447534575346753477534875349753507535175352753537535475355753567535775358753597536075361753627536375364753657536675367753687536975370753717537275373753747537575376753777537875379753807538175382753837538475385753867538775388753897539075391753927539375394753957539675397753987539975400754017540275403754047540575406754077540875409754107541175412754137541475415754167541775418754197542075421754227542375424754257542675427754287542975430754317543275433754347543575436754377543875439754407544175442754437544475445754467544775448754497545075451754527545375454754557545675457754587545975460754617546275463754647546575466754677546875469754707547175472754737547475475754767547775478754797548075481754827548375484754857548675487754887548975490754917549275493754947549575496754977549875499755007550175502755037550475505755067550775508755097551075511755127551375514755157551675517755187551975520755217552275523755247552575526755277552875529755307553175532755337553475535755367553775538755397554075541755427554375544755457554675547755487554975550755517555275553755547555575556755577555875559755607556175562755637556475565755667556775568755697557075571755727557375574755757557675577755787557975580755817558275583755847558575586755877558875589755907559175592755937559475595755967559775598755997560075601756027560375604756057560675607756087560975610756117561275613756147561575616756177561875619756207562175622756237562475625756267562775628756297563075631756327563375634756357563675637756387563975640756417564275643756447564575646756477564875649756507565175652756537565475655756567565775658756597566075661756627566375664756657566675667756687566975670756717567275673756747567575676756777567875679756807568175682756837568475685756867568775688756897569075691756927569375694756957569675697756987569975700757017570275703757047570575706757077570875709757107571175712757137571475715757167571775718757197572075721757227572375724757257572675727757287572975730757317573275733757347573575736757377573875739757407574175742757437574475745757467574775748757497575075751757527575375754757557575675757757587575975760757617576275763757647576575766757677576875769757707577175772757737577475775757767577775778757797578075781757827578375784757857578675787757887578975790757917579275793757947579575796757977579875799758007580175802758037580475805758067580775808758097581075811758127581375814758157581675817758187581975820758217582275823758247582575826758277582875829758307583175832758337583475835758367583775838758397584075841758427584375844758457584675847758487584975850758517585275853758547585575856758577585875859758607586175862758637586475865758667586775868758697587075871758727587375874758757587675877758787587975880758817588275883758847588575886758877588875889758907589175892758937589475895758967589775898758997590075901759027590375904759057590675907759087590975910759117591275913759147591575916759177591875919759207592175922759237592475925759267592775928759297593075931759327593375934759357593675937759387593975940759417594275943759447594575946759477594875949759507595175952759537595475955759567595775958759597596075961759627596375964759657596675967759687596975970759717597275973759747597575976759777597875979759807598175982759837598475985759867598775988759897599075991759927599375994759957599675997759987599976000760017600276003760047600576006760077600876009760107601176012760137601476015760167601776018760197602076021760227602376024760257602676027760287602976030760317603276033760347603576036760377603876039760407604176042760437604476045760467604776048760497605076051760527605376054760557605676057760587605976060760617606276063760647606576066760677606876069760707607176072760737607476075760767607776078760797608076081760827608376084760857608676087760887608976090760917609276093760947609576096760977609876099761007610176102761037610476105761067610776108761097611076111761127611376114761157611676117761187611976120761217612276123761247612576126761277612876129761307613176132761337613476135761367613776138761397614076141761427614376144761457614676147761487614976150761517615276153761547615576156761577615876159761607616176162761637616476165761667616776168761697617076171761727617376174761757617676177761787617976180761817618276183761847618576186761877618876189761907619176192761937619476195761967619776198761997620076201762027620376204762057620676207762087620976210762117621276213762147621576216762177621876219762207622176222762237622476225762267622776228762297623076231762327623376234762357623676237762387623976240762417624276243762447624576246762477624876249762507625176252762537625476255762567625776258762597626076261762627626376264762657626676267762687626976270762717627276273762747627576276762777627876279762807628176282762837628476285762867628776288762897629076291762927629376294762957629676297762987629976300763017630276303763047630576306763077630876309763107631176312763137631476315763167631776318763197632076321763227632376324763257632676327763287632976330763317633276333763347633576336763377633876339763407634176342763437634476345763467634776348763497635076351763527635376354763557635676357763587635976360763617636276363763647636576366763677636876369763707637176372763737637476375763767637776378763797638076381763827638376384763857638676387763887638976390763917639276393763947639576396763977639876399764007640176402764037640476405764067640776408764097641076411764127641376414764157641676417764187641976420764217642276423764247642576426764277642876429764307643176432764337643476435764367643776438764397644076441764427644376444764457644676447764487644976450764517645276453764547645576456764577645876459764607646176462764637646476465764667646776468764697647076471764727647376474764757647676477764787647976480764817648276483764847648576486764877648876489764907649176492764937649476495764967649776498764997650076501765027650376504765057650676507765087650976510765117651276513765147651576516765177651876519765207652176522765237652476525765267652776528765297653076531765327653376534765357653676537765387653976540765417654276543765447654576546765477654876549765507655176552765537655476555765567655776558765597656076561765627656376564765657656676567765687656976570765717657276573765747657576576765777657876579765807658176582765837658476585765867658776588765897659076591765927659376594765957659676597765987659976600766017660276603766047660576606766077660876609766107661176612766137661476615766167661776618766197662076621766227662376624766257662676627766287662976630766317663276633766347663576636766377663876639766407664176642766437664476645766467664776648766497665076651766527665376654766557665676657766587665976660766617666276663766647666576666766677666876669766707667176672766737667476675766767667776678766797668076681766827668376684766857668676687766887668976690766917669276693766947669576696766977669876699767007670176702767037670476705767067670776708767097671076711767127671376714767157671676717767187671976720767217672276723767247672576726767277672876729767307673176732767337673476735767367673776738767397674076741767427674376744767457674676747767487674976750767517675276753767547675576756767577675876759767607676176762767637676476765767667676776768767697677076771767727677376774767757677676777767787677976780767817678276783767847678576786767877678876789767907679176792767937679476795767967679776798767997680076801768027680376804768057680676807768087680976810768117681276813768147681576816768177681876819768207682176822768237682476825768267682776828768297683076831768327683376834768357683676837768387683976840768417684276843768447684576846768477684876849768507685176852768537685476855768567685776858768597686076861768627686376864768657686676867768687686976870768717687276873768747687576876768777687876879768807688176882768837688476885768867688776888768897689076891768927689376894768957689676897768987689976900769017690276903769047690576906769077690876909769107691176912769137691476915769167691776918769197692076921769227692376924769257692676927769287692976930769317693276933769347693576936769377693876939769407694176942769437694476945769467694776948769497695076951769527695376954769557695676957769587695976960769617696276963769647696576966769677696876969769707697176972769737697476975769767697776978769797698076981769827698376984769857698676987769887698976990769917699276993769947699576996769977699876999770007700177002770037700477005770067700777008770097701077011770127701377014770157701677017770187701977020770217702277023770247702577026770277702877029770307703177032770337703477035770367703777038770397704077041770427704377044770457704677047770487704977050770517705277053770547705577056770577705877059770607706177062770637706477065770667706777068770697707077071770727707377074770757707677077770787707977080770817708277083770847708577086770877708877089770907709177092770937709477095770967709777098770997710077101771027710377104771057710677107771087710977110771117711277113771147711577116771177711877119771207712177122771237712477125771267712777128771297713077131771327713377134771357713677137771387713977140771417714277143771447714577146771477714877149771507715177152771537715477155771567715777158771597716077161771627716377164771657716677167771687716977170771717717277173771747717577176771777717877179771807718177182771837718477185771867718777188771897719077191771927719377194771957719677197771987719977200772017720277203772047720577206772077720877209772107721177212772137721477215772167721777218772197722077221772227722377224772257722677227772287722977230772317723277233772347723577236772377723877239772407724177242772437724477245772467724777248772497725077251772527725377254772557725677257772587725977260772617726277263772647726577266772677726877269772707727177272772737727477275772767727777278772797728077281772827728377284772857728677287772887728977290772917729277293772947729577296772977729877299773007730177302773037730477305773067730777308773097731077311773127731377314773157731677317773187731977320773217732277323773247732577326773277732877329773307733177332773337733477335773367733777338773397734077341773427734377344773457734677347773487734977350773517735277353773547735577356773577735877359773607736177362773637736477365773667736777368773697737077371773727737377374773757737677377773787737977380773817738277383773847738577386773877738877389773907739177392773937739477395773967739777398773997740077401774027740377404774057740677407774087740977410774117741277413774147741577416774177741877419774207742177422774237742477425774267742777428774297743077431774327743377434774357743677437774387743977440774417744277443774447744577446774477744877449774507745177452774537745477455774567745777458774597746077461774627746377464774657746677467774687746977470774717747277473774747747577476774777747877479774807748177482774837748477485774867748777488774897749077491774927749377494774957749677497774987749977500775017750277503775047750577506775077750877509775107751177512775137751477515775167751777518775197752077521775227752377524775257752677527775287752977530775317753277533775347753577536775377753877539775407754177542775437754477545775467754777548775497755077551775527755377554775557755677557775587755977560775617756277563775647756577566775677756877569775707757177572775737757477575775767757777578775797758077581775827758377584775857758677587775887758977590775917759277593775947759577596775977759877599776007760177602776037760477605776067760777608776097761077611776127761377614776157761677617776187761977620776217762277623776247762577626776277762877629776307763177632776337763477635776367763777638776397764077641776427764377644776457764677647776487764977650776517765277653776547765577656776577765877659776607766177662776637766477665776667766777668776697767077671776727767377674776757767677677776787767977680776817768277683776847768577686776877768877689776907769177692776937769477695776967769777698776997770077701777027770377704777057770677707777087770977710777117771277713777147771577716777177771877719777207772177722777237772477725777267772777728777297773077731777327773377734777357773677737777387773977740777417774277743777447774577746777477774877749777507775177752777537775477755777567775777758777597776077761777627776377764777657776677767777687776977770777717777277773777747777577776777777777877779777807778177782777837778477785777867778777788777897779077791777927779377794777957779677797777987779977800778017780277803778047780577806778077780877809778107781177812778137781477815778167781777818778197782077821778227782377824778257782677827778287782977830778317783277833778347783577836778377783877839778407784177842778437784477845778467784777848778497785077851778527785377854778557785677857778587785977860778617786277863778647786577866778677786877869778707787177872778737787477875778767787777878778797788077881778827788377884778857788677887778887788977890778917789277893778947789577896778977789877899779007790177902779037790477905779067790777908779097791077911779127791377914779157791677917779187791977920779217792277923779247792577926779277792877929779307793177932779337793477935779367793777938779397794077941779427794377944779457794677947779487794977950779517795277953779547795577956779577795877959779607796177962779637796477965779667796777968779697797077971779727797377974779757797677977779787797977980779817798277983779847798577986779877798877989779907799177992779937799477995779967799777998779997800078001780027800378004780057800678007780087800978010780117801278013780147801578016780177801878019780207802178022780237802478025780267802778028780297803078031780327803378034780357803678037780387803978040780417804278043780447804578046780477804878049780507805178052780537805478055780567805778058780597806078061780627806378064780657806678067780687806978070780717807278073780747807578076780777807878079780807808178082780837808478085780867808778088780897809078091780927809378094780957809678097780987809978100781017810278103781047810578106781077810878109781107811178112781137811478115781167811778118781197812078121781227812378124781257812678127781287812978130781317813278133781347813578136781377813878139781407814178142781437814478145781467814778148781497815078151781527815378154781557815678157781587815978160781617816278163781647816578166781677816878169781707817178172781737817478175781767817778178781797818078181781827818378184781857818678187781887818978190781917819278193781947819578196781977819878199782007820178202782037820478205782067820778208782097821078211782127821378214782157821678217782187821978220782217822278223782247822578226782277822878229782307823178232782337823478235782367823778238782397824078241782427824378244782457824678247782487824978250782517825278253782547825578256782577825878259782607826178262782637826478265782667826778268782697827078271782727827378274782757827678277782787827978280782817828278283782847828578286782877828878289782907829178292782937829478295782967829778298782997830078301783027830378304783057830678307783087830978310783117831278313783147831578316783177831878319783207832178322783237832478325783267832778328783297833078331783327833378334783357833678337783387833978340783417834278343783447834578346783477834878349783507835178352783537835478355783567835778358783597836078361783627836378364783657836678367783687836978370783717837278373783747837578376783777837878379783807838178382783837838478385783867838778388783897839078391783927839378394783957839678397783987839978400784017840278403784047840578406784077840878409784107841178412784137841478415784167841778418784197842078421784227842378424784257842678427784287842978430784317843278433784347843578436784377843878439784407844178442784437844478445784467844778448784497845078451784527845378454784557845678457784587845978460784617846278463784647846578466784677846878469784707847178472784737847478475784767847778478784797848078481784827848378484784857848678487784887848978490784917849278493784947849578496784977849878499785007850178502785037850478505785067850778508785097851078511785127851378514785157851678517785187851978520785217852278523785247852578526785277852878529785307853178532785337853478535785367853778538785397854078541785427854378544785457854678547785487854978550785517855278553785547855578556785577855878559785607856178562785637856478565785667856778568785697857078571785727857378574785757857678577785787857978580785817858278583785847858578586785877858878589785907859178592785937859478595785967859778598785997860078601786027860378604786057860678607786087860978610786117861278613786147861578616786177861878619786207862178622786237862478625786267862778628786297863078631786327863378634786357863678637786387863978640786417864278643786447864578646786477864878649786507865178652786537865478655786567865778658786597866078661786627866378664786657866678667786687866978670786717867278673786747867578676786777867878679786807868178682786837868478685786867868778688786897869078691786927869378694786957869678697786987869978700787017870278703787047870578706787077870878709787107871178712787137871478715787167871778718787197872078721787227872378724787257872678727787287872978730787317873278733787347873578736787377873878739787407874178742787437874478745787467874778748787497875078751787527875378754787557875678757787587875978760787617876278763787647876578766787677876878769787707877178772787737877478775787767877778778787797878078781787827878378784787857878678787787887878978790787917879278793787947879578796787977879878799788007880178802788037880478805788067880778808788097881078811788127881378814788157881678817788187881978820788217882278823788247882578826788277882878829788307883178832788337883478835788367883778838788397884078841788427884378844788457884678847788487884978850788517885278853788547885578856788577885878859788607886178862788637886478865788667886778868788697887078871788727887378874788757887678877788787887978880788817888278883788847888578886788877888878889788907889178892788937889478895788967889778898788997890078901789027890378904789057890678907789087890978910789117891278913789147891578916789177891878919789207892178922789237892478925789267892778928789297893078931789327893378934789357893678937789387893978940789417894278943789447894578946789477894878949789507895178952789537895478955789567895778958789597896078961789627896378964789657896678967789687896978970789717897278973789747897578976789777897878979789807898178982789837898478985789867898778988789897899078991789927899378994789957899678997789987899979000790017900279003790047900579006790077900879009790107901179012790137901479015790167901779018790197902079021790227902379024790257902679027790287902979030790317903279033790347903579036790377903879039790407904179042790437904479045790467904779048790497905079051790527905379054790557905679057790587905979060790617906279063790647906579066790677906879069790707907179072790737907479075790767907779078790797908079081790827908379084790857908679087790887908979090790917909279093790947909579096790977909879099791007910179102791037910479105791067910779108791097911079111791127911379114791157911679117791187911979120791217912279123791247912579126791277912879129791307913179132791337913479135791367913779138791397914079141791427914379144791457914679147791487914979150791517915279153791547915579156791577915879159791607916179162791637916479165791667916779168791697917079171791727917379174791757917679177791787917979180791817918279183791847918579186791877918879189791907919179192791937919479195791967919779198791997920079201792027920379204792057920679207792087920979210792117921279213792147921579216792177921879219792207922179222792237922479225792267922779228792297923079231792327923379234792357923679237792387923979240792417924279243792447924579246792477924879249792507925179252792537925479255792567925779258792597926079261792627926379264792657926679267792687926979270792717927279273792747927579276792777927879279792807928179282792837928479285792867928779288792897929079291792927929379294792957929679297792987929979300793017930279303793047930579306793077930879309793107931179312793137931479315793167931779318793197932079321793227932379324793257932679327793287932979330793317933279333793347933579336793377933879339793407934179342793437934479345793467934779348793497935079351793527935379354793557935679357793587935979360793617936279363793647936579366793677936879369793707937179372793737937479375793767937779378793797938079381793827938379384793857938679387793887938979390793917939279393793947939579396793977939879399794007940179402794037940479405794067940779408794097941079411794127941379414794157941679417794187941979420794217942279423794247942579426794277942879429794307943179432794337943479435794367943779438794397944079441794427944379444794457944679447794487944979450794517945279453794547945579456794577945879459794607946179462794637946479465794667946779468794697947079471794727947379474794757947679477794787947979480794817948279483794847948579486794877948879489794907949179492794937949479495794967949779498794997950079501795027950379504795057950679507795087950979510795117951279513795147951579516795177951879519795207952179522795237952479525795267952779528795297953079531795327953379534795357953679537795387953979540795417954279543795447954579546795477954879549795507955179552795537955479555795567955779558795597956079561795627956379564795657956679567795687956979570795717957279573795747957579576795777957879579795807958179582795837958479585795867958779588795897959079591795927959379594795957959679597795987959979600796017960279603796047960579606796077960879609796107961179612796137961479615796167961779618796197962079621796227962379624796257962679627796287962979630796317963279633796347963579636796377963879639796407964179642796437964479645796467964779648796497965079651796527965379654796557965679657796587965979660796617966279663796647966579666796677966879669796707967179672796737967479675796767967779678796797968079681796827968379684796857968679687796887968979690796917969279693796947969579696796977969879699797007970179702797037970479705797067970779708797097971079711797127971379714797157971679717797187971979720797217972279723797247972579726797277972879729797307973179732797337973479735797367973779738797397974079741797427974379744797457974679747797487974979750797517975279753797547975579756797577975879759797607976179762797637976479765797667976779768797697977079771797727977379774797757977679777797787977979780797817978279783797847978579786797877978879789797907979179792797937979479795797967979779798797997980079801798027980379804798057980679807798087980979810798117981279813798147981579816798177981879819798207982179822798237982479825798267982779828798297983079831798327983379834798357983679837798387983979840798417984279843798447984579846798477984879849798507985179852798537985479855798567985779858798597986079861798627986379864798657986679867798687986979870798717987279873798747987579876798777987879879798807988179882798837988479885798867988779888798897989079891798927989379894798957989679897798987989979900799017990279903799047990579906799077990879909799107991179912799137991479915799167991779918799197992079921799227992379924799257992679927799287992979930799317993279933799347993579936799377993879939799407994179942799437994479945799467994779948799497995079951799527995379954799557995679957799587995979960799617996279963799647996579966799677996879969799707997179972799737997479975799767997779978799797998079981799827998379984799857998679987799887998979990799917999279993799947999579996799977999879999800008000180002800038000480005800068000780008800098001080011800128001380014800158001680017800188001980020800218002280023800248002580026800278002880029800308003180032800338003480035800368003780038800398004080041800428004380044800458004680047800488004980050800518005280053800548005580056800578005880059800608006180062800638006480065800668006780068800698007080071800728007380074800758007680077800788007980080800818008280083800848008580086800878008880089800908009180092800938009480095800968009780098800998010080101801028010380104801058010680107801088010980110801118011280113801148011580116801178011880119801208012180122801238012480125801268012780128801298013080131801328013380134801358013680137801388013980140801418014280143801448014580146801478014880149801508015180152801538015480155801568015780158801598016080161801628016380164801658016680167801688016980170801718017280173801748017580176801778017880179801808018180182801838018480185801868018780188801898019080191801928019380194801958019680197801988019980200802018020280203802048020580206802078020880209802108021180212802138021480215802168021780218802198022080221802228022380224802258022680227802288022980230802318023280233802348023580236802378023880239802408024180242802438024480245802468024780248802498025080251802528025380254802558025680257802588025980260802618026280263802648026580266802678026880269802708027180272802738027480275802768027780278802798028080281802828028380284802858028680287802888028980290802918029280293802948029580296802978029880299803008030180302803038030480305803068030780308803098031080311803128031380314803158031680317803188031980320803218032280323803248032580326803278032880329803308033180332803338033480335803368033780338803398034080341803428034380344803458034680347803488034980350803518035280353803548035580356803578035880359803608036180362803638036480365803668036780368803698037080371803728037380374803758037680377803788037980380803818038280383803848038580386803878038880389803908039180392803938039480395803968039780398803998040080401804028040380404804058040680407804088040980410804118041280413804148041580416804178041880419804208042180422804238042480425804268042780428804298043080431804328043380434804358043680437804388043980440804418044280443804448044580446804478044880449804508045180452804538045480455804568045780458804598046080461804628046380464804658046680467804688046980470804718047280473804748047580476804778047880479804808048180482804838048480485804868048780488804898049080491804928049380494804958049680497804988049980500805018050280503805048050580506805078050880509805108051180512805138051480515805168051780518805198052080521805228052380524805258052680527805288052980530805318053280533805348053580536805378053880539805408054180542805438054480545805468054780548805498055080551805528055380554805558055680557805588055980560805618056280563805648056580566805678056880569805708057180572805738057480575805768057780578805798058080581805828058380584805858058680587805888058980590805918059280593805948059580596805978059880599806008060180602806038060480605806068060780608806098061080611806128061380614806158061680617806188061980620806218062280623806248062580626806278062880629806308063180632806338063480635806368063780638806398064080641806428064380644806458064680647806488064980650806518065280653806548065580656806578065880659806608066180662806638066480665806668066780668806698067080671806728067380674806758067680677806788067980680806818068280683806848068580686806878068880689806908069180692806938069480695806968069780698806998070080701807028070380704807058070680707807088070980710807118071280713807148071580716807178071880719807208072180722807238072480725807268072780728807298073080731807328073380734807358073680737807388073980740807418074280743807448074580746807478074880749807508075180752807538075480755807568075780758807598076080761807628076380764807658076680767807688076980770807718077280773807748077580776807778077880779807808078180782807838078480785807868078780788807898079080791807928079380794807958079680797807988079980800808018080280803808048080580806808078080880809808108081180812808138081480815808168081780818808198082080821808228082380824808258082680827808288082980830808318083280833808348083580836808378083880839808408084180842808438084480845808468084780848808498085080851808528085380854808558085680857808588085980860808618086280863808648086580866808678086880869808708087180872808738087480875808768087780878808798088080881808828088380884808858088680887808888088980890808918089280893808948089580896808978089880899809008090180902809038090480905809068090780908809098091080911809128091380914809158091680917809188091980920809218092280923809248092580926809278092880929809308093180932809338093480935809368093780938809398094080941809428094380944809458094680947809488094980950809518095280953809548095580956809578095880959809608096180962809638096480965809668096780968809698097080971809728097380974809758097680977809788097980980809818098280983809848098580986809878098880989809908099180992809938099480995809968099780998809998100081001810028100381004810058100681007810088100981010810118101281013810148101581016810178101881019810208102181022810238102481025810268102781028810298103081031810328103381034810358103681037810388103981040810418104281043810448104581046810478104881049810508105181052810538105481055810568105781058810598106081061810628106381064810658106681067810688106981070810718107281073810748107581076810778107881079810808108181082810838108481085810868108781088810898109081091810928109381094810958109681097810988109981100811018110281103811048110581106811078110881109811108111181112811138111481115811168111781118811198112081121811228112381124811258112681127811288112981130811318113281133811348113581136811378113881139811408114181142811438114481145811468114781148811498115081151811528115381154811558115681157811588115981160811618116281163811648116581166811678116881169811708117181172811738117481175811768117781178811798118081181811828118381184811858118681187811888118981190811918119281193811948119581196811978119881199812008120181202812038120481205812068120781208812098121081211812128121381214812158121681217812188121981220812218122281223812248122581226812278122881229812308123181232812338123481235812368123781238812398124081241812428124381244812458124681247812488124981250812518125281253812548125581256812578125881259812608126181262812638126481265812668126781268812698127081271812728127381274812758127681277812788127981280812818128281283812848128581286812878128881289812908129181292812938129481295812968129781298812998130081301813028130381304813058130681307813088130981310813118131281313813148131581316813178131881319813208132181322813238132481325813268132781328813298133081331813328133381334813358133681337813388133981340813418134281343813448134581346813478134881349813508135181352813538135481355813568135781358813598136081361813628136381364813658136681367813688136981370813718137281373813748137581376813778137881379813808138181382813838138481385813868138781388813898139081391813928139381394813958139681397813988139981400814018140281403814048140581406814078140881409814108141181412814138141481415814168141781418814198142081421814228142381424814258142681427814288142981430814318143281433814348143581436814378143881439814408144181442814438144481445814468144781448814498145081451814528145381454814558145681457814588145981460814618146281463814648146581466814678146881469814708147181472814738147481475814768147781478814798148081481814828148381484814858148681487814888148981490814918149281493814948149581496814978149881499815008150181502815038150481505815068150781508815098151081511815128151381514815158151681517815188151981520815218152281523815248152581526815278152881529815308153181532815338153481535815368153781538815398154081541815428154381544815458154681547815488154981550815518155281553815548155581556815578155881559815608156181562815638156481565815668156781568815698157081571815728157381574815758157681577815788157981580815818158281583815848158581586815878158881589815908159181592815938159481595815968159781598815998160081601816028160381604816058160681607816088160981610816118161281613816148161581616816178161881619816208162181622816238162481625816268162781628816298163081631816328163381634816358163681637816388163981640816418164281643816448164581646816478164881649816508165181652816538165481655816568165781658816598166081661816628166381664816658166681667816688166981670816718167281673816748167581676816778167881679816808168181682816838168481685816868168781688816898169081691816928169381694816958169681697816988169981700817018170281703817048170581706817078170881709817108171181712817138171481715817168171781718817198172081721817228172381724817258172681727817288172981730817318173281733817348173581736817378173881739817408174181742817438174481745817468174781748817498175081751817528175381754817558175681757817588175981760817618176281763817648176581766817678176881769817708177181772817738177481775817768177781778817798178081781817828178381784817858178681787817888178981790817918179281793817948179581796817978179881799818008180181802818038180481805818068180781808818098181081811818128181381814818158181681817818188181981820818218182281823818248182581826818278182881829818308183181832818338183481835818368183781838818398184081841818428184381844818458184681847818488184981850818518185281853818548185581856818578185881859818608186181862818638186481865818668186781868818698187081871818728187381874818758187681877818788187981880818818188281883818848188581886818878188881889818908189181892818938189481895818968189781898818998190081901819028190381904819058190681907819088190981910819118191281913819148191581916819178191881919819208192181922819238192481925819268192781928819298193081931819328193381934819358193681937819388193981940819418194281943819448194581946819478194881949819508195181952819538195481955819568195781958819598196081961819628196381964819658196681967819688196981970819718197281973819748197581976819778197881979819808198181982819838198481985819868198781988819898199081991819928199381994819958199681997819988199982000820018200282003820048200582006820078200882009820108201182012820138201482015820168201782018820198202082021820228202382024820258202682027820288202982030820318203282033820348203582036820378203882039820408204182042820438204482045820468204782048820498205082051820528205382054820558205682057820588205982060820618206282063820648206582066820678206882069820708207182072820738207482075820768207782078820798208082081820828208382084820858208682087820888208982090820918209282093820948209582096820978209882099821008210182102821038210482105821068210782108821098211082111821128211382114821158211682117821188211982120821218212282123821248212582126821278212882129821308213182132821338213482135821368213782138821398214082141821428214382144821458214682147821488214982150821518215282153821548215582156821578215882159821608216182162821638216482165821668216782168821698217082171821728217382174821758217682177821788217982180821818218282183821848218582186821878218882189821908219182192821938219482195821968219782198821998220082201822028220382204822058220682207822088220982210822118221282213822148221582216822178221882219822208222182222822238222482225822268222782228822298223082231822328223382234822358223682237822388223982240822418224282243822448224582246822478224882249822508225182252822538225482255822568225782258822598226082261822628226382264822658226682267822688226982270822718227282273822748227582276822778227882279822808228182282822838228482285822868228782288822898229082291822928229382294822958229682297822988229982300823018230282303823048230582306823078230882309823108231182312823138231482315823168231782318823198232082321823228232382324823258232682327823288232982330823318233282333823348233582336823378233882339823408234182342823438234482345823468234782348823498235082351823528235382354823558235682357823588235982360823618236282363823648236582366823678236882369823708237182372823738237482375823768237782378823798238082381823828238382384823858238682387823888238982390823918239282393823948239582396823978239882399824008240182402824038240482405824068240782408824098241082411824128241382414824158241682417824188241982420824218242282423824248242582426824278242882429824308243182432824338243482435824368243782438824398244082441824428244382444824458244682447824488244982450824518245282453824548245582456824578245882459824608246182462824638246482465824668246782468824698247082471824728247382474824758247682477824788247982480824818248282483824848248582486824878248882489824908249182492824938249482495824968249782498824998250082501825028250382504825058250682507825088250982510825118251282513825148251582516825178251882519825208252182522825238252482525825268252782528825298253082531825328253382534825358253682537825388253982540825418254282543825448254582546825478254882549825508255182552825538255482555825568255782558825598256082561825628256382564825658256682567825688256982570825718257282573825748257582576825778257882579825808258182582825838258482585825868258782588825898259082591825928259382594825958259682597825988259982600826018260282603826048260582606826078260882609826108261182612826138261482615826168261782618826198262082621826228262382624826258262682627826288262982630826318263282633826348263582636826378263882639826408264182642826438264482645826468264782648826498265082651826528265382654826558265682657826588265982660826618266282663826648266582666826678266882669826708267182672826738267482675826768267782678826798268082681826828268382684826858268682687826888268982690826918269282693826948269582696826978269882699827008270182702827038270482705827068270782708827098271082711827128271382714827158271682717827188271982720827218272282723827248272582726827278272882729827308273182732827338273482735827368273782738827398274082741827428274382744827458274682747827488274982750827518275282753827548275582756827578275882759827608276182762827638276482765827668276782768827698277082771827728277382774827758277682777827788277982780827818278282783827848278582786827878278882789827908279182792827938279482795827968279782798827998280082801828028280382804828058280682807828088280982810828118281282813828148281582816828178281882819828208282182822828238282482825828268282782828828298283082831828328283382834828358283682837828388283982840828418284282843828448284582846828478284882849828508285182852828538285482855828568285782858828598286082861828628286382864828658286682867828688286982870828718287282873828748287582876828778287882879828808288182882828838288482885828868288782888828898289082891828928289382894828958289682897828988289982900829018290282903829048290582906829078290882909829108291182912829138291482915829168291782918829198292082921829228292382924829258292682927829288292982930829318293282933829348293582936829378293882939829408294182942829438294482945829468294782948829498295082951829528295382954829558295682957829588295982960829618296282963829648296582966829678296882969829708297182972829738297482975829768297782978829798298082981829828298382984829858298682987829888298982990829918299282993829948299582996829978299882999830008300183002830038300483005830068300783008830098301083011830128301383014830158301683017830188301983020830218302283023830248302583026830278302883029830308303183032830338303483035830368303783038830398304083041830428304383044830458304683047830488304983050830518305283053830548305583056830578305883059830608306183062830638306483065830668306783068830698307083071830728307383074830758307683077830788307983080830818308283083830848308583086830878308883089830908309183092830938309483095830968309783098830998310083101831028310383104831058310683107831088310983110831118311283113831148311583116831178311883119831208312183122831238312483125831268312783128831298313083131831328313383134831358313683137831388313983140831418314283143831448314583146831478314883149831508315183152831538315483155831568315783158831598316083161831628316383164831658316683167831688316983170831718317283173831748317583176831778317883179831808318183182831838318483185831868318783188831898319083191831928319383194831958319683197831988319983200832018320283203832048320583206832078320883209832108321183212832138321483215832168321783218832198322083221832228322383224832258322683227832288322983230832318323283233832348323583236832378323883239832408324183242832438324483245832468324783248832498325083251832528325383254832558325683257832588325983260832618326283263832648326583266832678326883269832708327183272832738327483275832768327783278832798328083281832828328383284832858328683287832888328983290832918329283293832948329583296832978329883299833008330183302833038330483305833068330783308833098331083311833128331383314833158331683317833188331983320833218332283323833248332583326833278332883329833308333183332833338333483335833368333783338833398334083341833428334383344833458334683347833488334983350833518335283353833548335583356833578335883359833608336183362833638336483365833668336783368833698337083371833728337383374833758337683377833788337983380833818338283383833848338583386833878338883389833908339183392833938339483395833968339783398833998340083401834028340383404834058340683407834088340983410834118341283413834148341583416834178341883419834208342183422834238342483425834268342783428834298343083431834328343383434834358343683437834388343983440834418344283443834448344583446834478344883449834508345183452834538345483455834568345783458834598346083461834628346383464834658346683467834688346983470834718347283473834748347583476834778347883479834808348183482834838348483485834868348783488834898349083491834928349383494834958349683497834988349983500835018350283503835048350583506835078350883509835108351183512835138351483515835168351783518835198352083521835228352383524835258352683527835288352983530835318353283533835348353583536835378353883539835408354183542835438354483545835468354783548835498355083551835528355383554835558355683557835588355983560835618356283563835648356583566835678356883569835708357183572835738357483575835768357783578835798358083581835828358383584835858358683587835888358983590835918359283593835948359583596835978359883599836008360183602836038360483605836068360783608836098361083611836128361383614836158361683617836188361983620836218362283623836248362583626836278362883629836308363183632836338363483635836368363783638836398364083641836428364383644836458364683647836488364983650836518365283653836548365583656836578365883659836608366183662836638366483665836668366783668836698367083671836728367383674836758367683677836788367983680836818368283683836848368583686836878368883689836908369183692836938369483695836968369783698836998370083701837028370383704837058370683707837088370983710837118371283713837148371583716837178371883719837208372183722837238372483725837268372783728837298373083731837328373383734837358373683737837388373983740837418374283743837448374583746837478374883749837508375183752837538375483755837568375783758837598376083761837628376383764837658376683767837688376983770837718377283773837748377583776837778377883779837808378183782837838378483785837868378783788837898379083791837928379383794837958379683797837988379983800838018380283803838048380583806838078380883809838108381183812838138381483815838168381783818838198382083821838228382383824838258382683827838288382983830838318383283833838348383583836838378383883839838408384183842838438384483845838468384783848838498385083851838528385383854838558385683857838588385983860838618386283863838648386583866838678386883869838708387183872838738387483875838768387783878838798388083881838828388383884838858388683887838888388983890838918389283893838948389583896838978389883899839008390183902839038390483905839068390783908839098391083911839128391383914839158391683917839188391983920839218392283923839248392583926839278392883929839308393183932839338393483935839368393783938839398394083941839428394383944839458394683947839488394983950839518395283953839548395583956839578395883959839608396183962839638396483965839668396783968839698397083971839728397383974839758397683977839788397983980839818398283983839848398583986839878398883989839908399183992839938399483995839968399783998839998400084001840028400384004840058400684007840088400984010840118401284013840148401584016840178401884019840208402184022840238402484025840268402784028840298403084031840328403384034840358403684037840388403984040840418404284043840448404584046840478404884049840508405184052840538405484055840568405784058840598406084061840628406384064840658406684067840688406984070840718407284073840748407584076840778407884079840808408184082840838408484085840868408784088840898409084091840928409384094840958409684097840988409984100841018410284103841048410584106841078410884109841108411184112841138411484115841168411784118841198412084121841228412384124841258412684127841288412984130841318413284133841348413584136841378413884139841408414184142841438414484145841468414784148841498415084151841528415384154841558415684157841588415984160841618416284163841648416584166841678416884169841708417184172841738417484175841768417784178841798418084181841828418384184841858418684187841888418984190841918419284193841948419584196841978419884199842008420184202842038420484205842068420784208842098421084211842128421384214842158421684217842188421984220842218422284223842248422584226842278422884229842308423184232842338423484235842368423784238842398424084241842428424384244842458424684247842488424984250842518425284253842548425584256842578425884259842608426184262842638426484265842668426784268842698427084271842728427384274842758427684277842788427984280842818428284283842848428584286842878428884289842908429184292842938429484295842968429784298842998430084301843028430384304843058430684307843088430984310843118431284313843148431584316843178431884319843208432184322843238432484325843268432784328843298433084331843328433384334843358433684337843388433984340843418434284343843448434584346843478434884349843508435184352843538435484355843568435784358843598436084361843628436384364843658436684367843688436984370843718437284373843748437584376843778437884379843808438184382843838438484385843868438784388843898439084391843928439384394843958439684397843988439984400844018440284403844048440584406844078440884409844108441184412844138441484415844168441784418844198442084421844228442384424844258442684427844288442984430844318443284433844348443584436844378443884439844408444184442844438444484445844468444784448844498445084451844528445384454844558445684457844588445984460844618446284463844648446584466844678446884469844708447184472844738447484475844768447784478844798448084481844828448384484844858448684487844888448984490844918449284493844948449584496844978449884499845008450184502845038450484505845068450784508845098451084511845128451384514845158451684517845188451984520845218452284523845248452584526845278452884529845308453184532845338453484535845368453784538845398454084541845428454384544845458454684547845488454984550845518455284553845548455584556845578455884559845608456184562845638456484565845668456784568845698457084571845728457384574845758457684577845788457984580845818458284583845848458584586845878458884589845908459184592845938459484595845968459784598845998460084601846028460384604846058460684607846088460984610846118461284613846148461584616846178461884619846208462184622846238462484625846268462784628846298463084631846328463384634846358463684637846388463984640846418464284643846448464584646846478464884649846508465184652846538465484655846568465784658846598466084661846628466384664846658466684667846688466984670846718467284673846748467584676846778467884679846808468184682846838468484685846868468784688846898469084691846928469384694846958469684697846988469984700847018470284703847048470584706847078470884709847108471184712847138471484715847168471784718847198472084721847228472384724847258472684727847288472984730847318473284733847348473584736847378473884739847408474184742847438474484745847468474784748847498475084751847528475384754847558475684757847588475984760847618476284763847648476584766847678476884769847708477184772847738477484775847768477784778847798478084781847828478384784847858478684787847888478984790847918479284793847948479584796847978479884799848008480184802848038480484805848068480784808848098481084811848128481384814848158481684817848188481984820848218482284823848248482584826848278482884829848308483184832848338483484835848368483784838848398484084841848428484384844848458484684847848488484984850848518485284853848548485584856848578485884859848608486184862848638486484865848668486784868848698487084871848728487384874848758487684877848788487984880848818488284883848848488584886848878488884889848908489184892848938489484895848968489784898848998490084901849028490384904849058490684907849088490984910849118491284913849148491584916849178491884919849208492184922849238492484925849268492784928849298493084931849328493384934849358493684937849388493984940849418494284943849448494584946849478494884949849508495184952849538495484955849568495784958849598496084961849628496384964849658496684967849688496984970849718497284973849748497584976849778497884979849808498184982849838498484985849868498784988849898499084991849928499384994849958499684997849988499985000850018500285003850048500585006850078500885009850108501185012850138501485015850168501785018850198502085021850228502385024850258502685027850288502985030850318503285033850348503585036850378503885039850408504185042850438504485045850468504785048850498505085051850528505385054850558505685057850588505985060850618506285063850648506585066850678506885069850708507185072850738507485075850768507785078850798508085081850828508385084850858508685087850888508985090850918509285093850948509585096850978509885099851008510185102851038510485105851068510785108851098511085111851128511385114851158511685117851188511985120851218512285123851248512585126851278512885129851308513185132851338513485135851368513785138851398514085141851428514385144851458514685147851488514985150851518515285153851548515585156851578515885159851608516185162851638516485165851668516785168851698517085171851728517385174851758517685177851788517985180851818518285183851848518585186851878518885189851908519185192851938519485195851968519785198851998520085201852028520385204852058520685207852088520985210852118521285213852148521585216852178521885219852208522185222852238522485225852268522785228852298523085231852328523385234852358523685237852388523985240852418524285243852448524585246852478524885249852508525185252852538525485255852568525785258852598526085261852628526385264852658526685267852688526985270852718527285273852748527585276852778527885279852808528185282852838528485285852868528785288852898529085291852928529385294852958529685297852988529985300853018530285303853048530585306853078530885309853108531185312853138531485315853168531785318853198532085321853228532385324853258532685327853288532985330853318533285333853348533585336853378533885339853408534185342853438534485345853468534785348853498535085351853528535385354853558535685357853588535985360853618536285363853648536585366853678536885369853708537185372853738537485375853768537785378853798538085381853828538385384853858538685387853888538985390853918539285393853948539585396853978539885399854008540185402854038540485405854068540785408854098541085411854128541385414854158541685417854188541985420854218542285423854248542585426854278542885429854308543185432854338543485435854368543785438854398544085441854428544385444854458544685447854488544985450854518545285453854548545585456854578545885459854608546185462854638546485465854668546785468854698547085471854728547385474854758547685477854788547985480854818548285483854848548585486854878548885489854908549185492854938549485495854968549785498854998550085501855028550385504855058550685507855088550985510855118551285513855148551585516855178551885519855208552185522855238552485525855268552785528855298553085531855328553385534855358553685537855388553985540855418554285543855448554585546855478554885549855508555185552855538555485555855568555785558855598556085561855628556385564855658556685567855688556985570855718557285573855748557585576855778557885579855808558185582855838558485585855868558785588855898559085591855928559385594855958559685597855988559985600856018560285603856048560585606856078560885609856108561185612856138561485615856168561785618856198562085621856228562385624856258562685627856288562985630856318563285633856348563585636856378563885639856408564185642856438564485645856468564785648856498565085651856528565385654856558565685657856588565985660856618566285663856648566585666856678566885669856708567185672856738567485675856768567785678856798568085681856828568385684856858568685687856888568985690856918569285693856948569585696856978569885699857008570185702857038570485705857068570785708857098571085711857128571385714857158571685717857188571985720857218572285723857248572585726857278572885729857308573185732857338573485735857368573785738857398574085741857428574385744857458574685747857488574985750857518575285753857548575585756857578575885759857608576185762857638576485765857668576785768857698577085771857728577385774857758577685777857788577985780857818578285783857848578585786857878578885789857908579185792857938579485795857968579785798857998580085801858028580385804858058580685807858088580985810858118581285813858148581585816858178581885819858208582185822858238582485825858268582785828858298583085831858328583385834858358583685837858388583985840858418584285843858448584585846858478584885849858508585185852858538585485855858568585785858858598586085861858628586385864858658586685867858688586985870858718587285873858748587585876858778587885879858808588185882858838588485885858868588785888858898589085891858928589385894858958589685897858988589985900859018590285903859048590585906859078590885909859108591185912859138591485915859168591785918859198592085921
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2024 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 WOLFSSL_HAVE_KYBER
  265. #include <wolfssl/wolfcrypt/kyber.h>
  266. #ifdef WOLFSSL_WC_KYBER
  267. #include <wolfssl/wolfcrypt/wc_kyber.h>
  268. #endif
  269. #endif
  270. #ifdef HAVE_DILITHIUM
  271. #include <wolfssl/wolfcrypt/dilithium.h>
  272. #endif
  273. #ifdef HAVE_PKCS12
  274. #include <wolfssl/wolfcrypt/pkcs12.h>
  275. #endif
  276. #include <wolfssl/wolfcrypt/logging.h>
  277. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  278. #include <wolfssl/openssl/ssl.h>
  279. #ifndef NO_ASN
  280. /* for ASN_COMMON_NAME DN_tags enum */
  281. #include <wolfssl/wolfcrypt/asn.h>
  282. #endif
  283. #ifdef HAVE_OCSP
  284. #include <wolfssl/openssl/ocsp.h>
  285. #endif
  286. #endif
  287. #ifdef OPENSSL_EXTRA
  288. #include <wolfssl/openssl/cmac.h>
  289. #include <wolfssl/openssl/x509v3.h>
  290. #include <wolfssl/openssl/asn1.h>
  291. #include <wolfssl/openssl/crypto.h>
  292. #include <wolfssl/openssl/pkcs12.h>
  293. #include <wolfssl/openssl/evp.h>
  294. #include <wolfssl/openssl/dh.h>
  295. #include <wolfssl/openssl/bn.h>
  296. #include <wolfssl/openssl/buffer.h>
  297. #include <wolfssl/openssl/pem.h>
  298. #include <wolfssl/openssl/ec.h>
  299. #include <wolfssl/openssl/ecdh.h>
  300. #include <wolfssl/openssl/engine.h>
  301. #include <wolfssl/openssl/hmac.h>
  302. #include <wolfssl/openssl/objects.h>
  303. #include <wolfssl/openssl/rand.h>
  304. #include <wolfssl/openssl/modes.h>
  305. #include <wolfssl/openssl/fips_rand.h>
  306. #include <wolfssl/openssl/kdf.h>
  307. #ifdef OPENSSL_ALL
  308. #include <wolfssl/openssl/txt_db.h>
  309. #include <wolfssl/openssl/lhash.h>
  310. #endif
  311. #ifndef NO_AES
  312. #include <wolfssl/openssl/aes.h>
  313. #endif
  314. #ifndef NO_DES3
  315. #include <wolfssl/openssl/des.h>
  316. #endif
  317. #ifndef NO_RC4
  318. #include <wolfssl/openssl/rc4.h>
  319. #endif
  320. #ifdef HAVE_ECC
  321. #include <wolfssl/openssl/ecdsa.h>
  322. #endif
  323. #ifdef HAVE_PKCS7
  324. #include <wolfssl/openssl/pkcs7.h>
  325. #endif
  326. #ifdef HAVE_CURVE25519
  327. #include <wolfssl/openssl/ec25519.h>
  328. #endif
  329. #ifdef HAVE_ED25519
  330. #include <wolfssl/openssl/ed25519.h>
  331. #endif
  332. #ifdef HAVE_CURVE448
  333. #include <wolfssl/openssl/ec448.h>
  334. #endif
  335. #ifdef HAVE_ED448
  336. #include <wolfssl/openssl/ed448.h>
  337. #endif
  338. #endif /* OPENSSL_EXTRA */
  339. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  340. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  341. #include <wolfssl/wolfcrypt/srp.h>
  342. #endif
  343. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  344. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  345. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  346. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  347. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  348. || !defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13)
  349. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  350. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  351. * number tracking */
  352. #include "wolfssl/internal.h"
  353. #endif
  354. /* force enable test buffers */
  355. #ifndef USE_CERT_BUFFERS_2048
  356. #define USE_CERT_BUFFERS_2048
  357. #endif
  358. #ifndef USE_CERT_BUFFERS_256
  359. #define USE_CERT_BUFFERS_256
  360. #endif
  361. #include <wolfssl/certs_test.h>
  362. #include "tests/utils.h"
  363. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  364. * have default (hidden) visibility, and in the absence of visibility, it's
  365. * benign to mask out the library implementation.
  366. */
  367. #define WOLFSSL_MISC_INCLUDED
  368. #include <wolfcrypt/src/misc.c>
  369. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  370. /* FIPS build has replaced ecc.h. */
  371. #define wc_ecc_key_get_priv(key) (&((key)->k))
  372. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  373. #endif
  374. typedef struct testVector {
  375. const char* input;
  376. const char* output;
  377. size_t inLen;
  378. size_t outLen;
  379. } testVector;
  380. #if defined(HAVE_PKCS7)
  381. typedef struct {
  382. const byte* content;
  383. word32 contentSz;
  384. int contentOID;
  385. int encryptOID;
  386. int keyWrapOID;
  387. int keyAgreeOID;
  388. byte* cert;
  389. size_t certSz;
  390. byte* privateKey;
  391. word32 privateKeySz;
  392. } pkcs7EnvelopedVector;
  393. #ifndef NO_PKCS7_ENCRYPTED_DATA
  394. typedef struct {
  395. const byte* content;
  396. word32 contentSz;
  397. int contentOID;
  398. int encryptOID;
  399. byte* encryptionKey;
  400. word32 encryptionKeySz;
  401. } pkcs7EncryptedVector;
  402. #endif
  403. #endif /* HAVE_PKCS7 */
  404. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  405. typedef int (*ssl_cb)(WOLFSSL* ssl);
  406. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  407. typedef int (*hs_cb)(WOLFSSL_CTX **ctx, WOLFSSL **ssl);
  408. typedef struct test_ssl_cbf {
  409. method_provider method;
  410. ctx_cb ctx_ready;
  411. ssl_cb ssl_ready;
  412. ssl_cb on_result;
  413. ctx_cb on_ctx_cleanup;
  414. ssl_cb on_cleanup;
  415. hs_cb on_handshake;
  416. WOLFSSL_CTX* ctx;
  417. const char* caPemFile;
  418. const char* certPemFile;
  419. const char* keyPemFile;
  420. const char* crlPemFile;
  421. #ifdef WOLFSSL_STATIC_MEMORY
  422. byte* mem;
  423. word32 memSz;
  424. wolfSSL_method_func method_ex;
  425. #endif
  426. int devId;
  427. int return_code;
  428. int last_err;
  429. unsigned char isSharedCtx:1;
  430. unsigned char loadToSSL:1;
  431. unsigned char ticNoInit:1;
  432. unsigned char doUdp:1;
  433. } test_ssl_cbf;
  434. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  435. typedef struct test_ssl_memio_ctx {
  436. WOLFSSL_CTX* s_ctx;
  437. WOLFSSL_CTX* c_ctx;
  438. WOLFSSL* s_ssl;
  439. WOLFSSL* c_ssl;
  440. const char* c_ciphers;
  441. const char* s_ciphers;
  442. char* c_msg;
  443. int c_msglen;
  444. char* s_msg;
  445. int s_msglen;
  446. test_ssl_cbf s_cb;
  447. test_ssl_cbf c_cb;
  448. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  449. int c_len;
  450. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  451. int s_len;
  452. } test_ssl_memio_ctx;
  453. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  454. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  455. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  456. const char* currentTestName;
  457. char tmpDirName[16];
  458. int tmpDirNameSet = 0;
  459. #endif
  460. /*----------------------------------------------------------------------------*
  461. | Constants
  462. *----------------------------------------------------------------------------*/
  463. /* Test result constants and macros. */
  464. /* Test succeeded. */
  465. #define TEST_SUCCESS (1)
  466. /* Test failed. */
  467. #define TEST_FAIL (0)
  468. /* Test skipped - not run. */
  469. #define TEST_SKIPPED (-7777)
  470. /* Returns the result based on whether check is true.
  471. *
  472. * @param [in] check Condition for success.
  473. * @return When condition is true: TEST_SUCCESS.
  474. * @return When condition is false: TEST_FAIL.
  475. */
  476. #ifdef DEBUG_WOLFSSL_VERBOSE
  477. #define XSTRINGIFY(s) STRINGIFY(s)
  478. #define STRINGIFY(s) #s
  479. #define TEST_RES_CHECK(check) ({ \
  480. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  481. if (_ret == TEST_FAIL) { \
  482. fprintf(stderr, " check \"%s\" at %d ", \
  483. XSTRINGIFY(check), __LINE__); \
  484. } \
  485. _ret; })
  486. #else
  487. #define TEST_RES_CHECK(check) \
  488. ((check) ? TEST_SUCCESS : TEST_FAIL)
  489. #endif /* DEBUG_WOLFSSL_VERBOSE */
  490. #define TEST_STRING "Everyone gets Friday off."
  491. #define TEST_STRING_SZ 25
  492. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  493. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  494. #define TEST_RSA_BITS 1024
  495. #else
  496. #define TEST_RSA_BITS 2048
  497. #endif
  498. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  499. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  500. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  501. static const char* bogusFile =
  502. #ifdef _WIN32
  503. "NUL"
  504. #else
  505. "/dev/null"
  506. #endif
  507. ;
  508. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  509. enum {
  510. TESTING_RSA = 1,
  511. TESTING_ECC = 2
  512. };
  513. #ifdef WOLFSSL_QNX_CAAM
  514. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  515. static int testDevId = WOLFSSL_CAAM_DEVID;
  516. #else
  517. static int testDevId = INVALID_DEVID;
  518. #endif
  519. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  520. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  521. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  522. #define HAVE_IO_TESTS_DEPENDENCIES
  523. #endif
  524. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  525. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT) && \
  526. !defined(WOLFSSL_TIRTOS)
  527. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  528. #endif
  529. /*----------------------------------------------------------------------------*
  530. | BIO with fixed read/write size
  531. *----------------------------------------------------------------------------*/
  532. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  533. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  534. int len)
  535. {
  536. if ((bio == NULL) || (bio->ptr.mem_buf_data == NULL) || (data == NULL)) {
  537. len = 0;
  538. }
  539. else {
  540. if (bio->wrSz - bio->wrIdx < len) {
  541. len = bio->wrSz - bio->wrIdx;
  542. }
  543. XMEMCPY(bio->ptr.mem_buf_data + bio->wrIdx, data, len);
  544. bio->wrIdx += len;
  545. }
  546. return len;
  547. }
  548. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  549. {
  550. if ((bio == NULL) || (bio->ptr.mem_buf_data == NULL) || (data == NULL)) {
  551. len = 0;
  552. }
  553. else {
  554. if (bio->wrSz - bio->rdIdx < len) {
  555. len = bio->wrSz - bio->rdIdx;
  556. }
  557. XMEMCPY(data, bio->ptr.mem_buf_data + bio->rdIdx, len);
  558. bio->rdIdx += len;
  559. }
  560. return len;
  561. }
  562. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  563. {
  564. static WOLFSSL_BIO_METHOD meth;
  565. meth.type = WOLFSSL_BIO_BIO;
  566. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  567. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  568. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  569. return &meth;
  570. }
  571. #endif
  572. /*----------------------------------------------------------------------------*
  573. | Setup
  574. *----------------------------------------------------------------------------*/
  575. static int test_wolfSSL_Init(void)
  576. {
  577. EXPECT_DECLS;
  578. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  579. return EXPECT_RESULT();
  580. }
  581. static int test_wolfSSL_Cleanup(void)
  582. {
  583. EXPECT_DECLS;
  584. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  585. return EXPECT_RESULT();
  586. }
  587. /* Initialize the wolfCrypt state.
  588. * POST: 0 success.
  589. */
  590. static int test_wolfCrypt_Init(void)
  591. {
  592. EXPECT_DECLS;
  593. ExpectIntEQ(wolfCrypt_Init(), 0);
  594. return EXPECT_RESULT();
  595. } /* END test_wolfCrypt_Init */
  596. static int test_wolfCrypt_Cleanup(void)
  597. {
  598. EXPECT_DECLS;
  599. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  600. return EXPECT_RESULT();
  601. }
  602. #ifdef WOLFSSL_STATIC_MEMORY
  603. #define TEST_LSM_STATIC_SIZE 440000
  604. /* Create new bucket list, using the default list, adding
  605. * one dang large buffer size. */
  606. #define TEST_LSM_DEF_BUCKETS (WOLFMEM_DEF_BUCKETS+1)
  607. #define TEST_LSM_BUCKETS WOLFMEM_BUCKETS,(LARGEST_MEM_BUCKET*2)
  608. #define TEST_LSM_DIST WOLFMEM_DIST,1
  609. #endif
  610. static int test_wc_LoadStaticMemory_ex(void)
  611. {
  612. EXPECT_DECLS;
  613. #ifdef WOLFSSL_STATIC_MEMORY
  614. byte staticMemory[TEST_LSM_STATIC_SIZE];
  615. word32 sizeList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_BUCKETS };
  616. word32 distList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_DIST };
  617. WOLFSSL_HEAP_HINT* heap;
  618. /* For this test, the size and dist lists will be the ones configured
  619. * for the build, or default. The value of WOLFMEM_DEF_BUCKETS is 9,
  620. * so these lists are 10 long. For most tests, the value of
  621. * WOLFMEM_DEF_BUCKETS is used. There's a test case where one is added
  622. * to that, to make sure the list size is larger than
  623. * WOLFMEM_MAX_BUCKETS. */
  624. /* Pass in zero everything. */
  625. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL, 0, NULL, NULL, NULL, 0, 0, 0),
  626. BAD_FUNC_ARG);
  627. /* Set the heap pointer to NULL. */
  628. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL,
  629. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  630. staticMemory, (word32)sizeof(staticMemory),
  631. 0, 1),
  632. BAD_FUNC_ARG);
  633. /* Set other pointer values to NULL one at a time. */
  634. heap = NULL;
  635. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  636. WOLFMEM_DEF_BUCKETS, NULL, distList,
  637. staticMemory, (word32)sizeof(staticMemory),
  638. 0, 1),
  639. BAD_FUNC_ARG);
  640. heap = NULL;
  641. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  642. WOLFMEM_DEF_BUCKETS, sizeList, NULL,
  643. staticMemory, (word32)sizeof(staticMemory),
  644. 0, 1),
  645. BAD_FUNC_ARG);
  646. heap = NULL;
  647. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  648. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  649. NULL, (word32)sizeof(staticMemory),
  650. 0, 1),
  651. BAD_FUNC_ARG);
  652. /* Set the size of the static buffer to 0. */
  653. heap = NULL;
  654. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  655. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  656. staticMemory, 0,
  657. 0, 1),
  658. BUFFER_E);
  659. /* Set the size of the static buffer to one less than minimum allowed. */
  660. heap = NULL;
  661. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  662. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  663. staticMemory,
  664. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) - 1,
  665. 0, 1),
  666. BUFFER_E);
  667. /* Set the size of the static buffer to exactly the minimum size. */
  668. heap = NULL;
  669. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  670. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  671. staticMemory,
  672. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)),
  673. 0, 1),
  674. 0);
  675. wc_UnloadStaticMemory(heap);
  676. /* Use more buckets than able. Success case. */
  677. heap = NULL;
  678. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  679. WOLFMEM_DEF_BUCKETS*2, sizeList, distList,
  680. staticMemory, (word32)sizeof(staticMemory),
  681. 0, 1),
  682. 0);
  683. wc_UnloadStaticMemory(heap);
  684. /* Success case. */
  685. heap = NULL;
  686. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  687. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  688. staticMemory, (word32)sizeof(staticMemory),
  689. 0, 1),
  690. 0);
  691. wc_UnloadStaticMemory(heap);
  692. #endif /* WOLFSSL_STATIC_MEMORY */
  693. return EXPECT_RESULT();
  694. }
  695. /*----------------------------------------------------------------------------*
  696. | Platform dependent function test
  697. *----------------------------------------------------------------------------*/
  698. static int test_fileAccess(void)
  699. {
  700. EXPECT_DECLS;
  701. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  702. const char *fname[] = {
  703. svrCertFile, svrKeyFile, caCertFile,
  704. eccCertFile, eccKeyFile, eccRsaCertFile,
  705. cliCertFile, cliCertDerFile, cliKeyFile,
  706. dhParamFile,
  707. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  708. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  709. NULL
  710. };
  711. const char derfile[] = "./certs/server-cert.der";
  712. XFILE f = XBADFILE;
  713. size_t sz;
  714. byte *buff = NULL;
  715. int i;
  716. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  717. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  718. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  719. XFCLOSE(f);
  720. }
  721. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  722. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  723. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  724. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  725. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  726. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  727. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  728. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  729. XFCLOSE(f);
  730. #endif
  731. return EXPECT_RESULT();
  732. }
  733. /*----------------------------------------------------------------------------*
  734. | Method Allocators
  735. *----------------------------------------------------------------------------*/
  736. static int test_wolfSSL_Method_Allocators(void)
  737. {
  738. EXPECT_DECLS;
  739. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  740. do { \
  741. WOLFSSL_METHOD *method = NULL; \
  742. condition(method = allocator()); \
  743. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  744. } while (0)
  745. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  746. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  747. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  748. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  749. #ifndef NO_OLD_TLS
  750. #ifdef WOLFSSL_ALLOW_SSLV3
  751. #ifndef NO_WOLFSSL_SERVER
  752. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  753. #endif
  754. #ifndef NO_WOLFSSL_CLIENT
  755. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  756. #endif
  757. #endif
  758. #ifdef WOLFSSL_ALLOW_TLSV10
  759. #ifndef NO_WOLFSSL_SERVER
  760. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  761. #endif
  762. #ifndef NO_WOLFSSL_CLIENT
  763. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  764. #endif
  765. #endif
  766. #ifndef NO_WOLFSSL_SERVER
  767. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  768. #endif
  769. #ifndef NO_WOLFSSL_CLIENT
  770. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  771. #endif
  772. #endif /* !NO_OLD_TLS */
  773. #ifndef WOLFSSL_NO_TLS12
  774. #ifndef NO_WOLFSSL_SERVER
  775. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  776. #endif
  777. #ifndef NO_WOLFSSL_CLIENT
  778. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  779. #endif
  780. #endif /* !WOLFSSL_NO_TLS12 */
  781. #ifdef WOLFSSL_TLS13
  782. #ifndef NO_WOLFSSL_SERVER
  783. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  784. #endif
  785. #ifndef NO_WOLFSSL_CLIENT
  786. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  787. #endif
  788. #endif /* WOLFSSL_TLS13 */
  789. #ifndef NO_WOLFSSL_SERVER
  790. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  791. #endif
  792. #ifndef NO_WOLFSSL_CLIENT
  793. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  794. #endif
  795. #ifdef WOLFSSL_DTLS
  796. #ifndef NO_OLD_TLS
  797. #ifndef NO_WOLFSSL_SERVER
  798. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  799. #endif
  800. #ifndef NO_WOLFSSL_CLIENT
  801. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  802. #endif
  803. #endif
  804. #ifndef WOLFSSL_NO_TLS12
  805. #ifndef NO_WOLFSSL_SERVER
  806. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  807. #endif
  808. #ifndef NO_WOLFSSL_CLIENT
  809. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  810. #endif
  811. #endif
  812. #endif /* WOLFSSL_DTLS */
  813. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  814. /* Stubs */
  815. #ifndef NO_WOLFSSL_SERVER
  816. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  817. #endif
  818. #ifndef NO_WOLFSSL_CLIENT
  819. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  820. #endif
  821. #endif
  822. /* Test Either Method (client or server) */
  823. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  824. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  825. #ifndef NO_OLD_TLS
  826. #ifdef WOLFSSL_ALLOW_TLSV10
  827. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  828. #endif
  829. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  830. #endif /* !NO_OLD_TLS */
  831. #ifndef WOLFSSL_NO_TLS12
  832. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  833. #endif /* !WOLFSSL_NO_TLS12 */
  834. #ifdef WOLFSSL_TLS13
  835. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  836. #endif /* WOLFSSL_TLS13 */
  837. #ifdef WOLFSSL_DTLS
  838. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  839. #ifndef NO_OLD_TLS
  840. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  841. #endif /* !NO_OLD_TLS */
  842. #ifndef WOLFSSL_NO_TLS12
  843. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  844. #endif /* !WOLFSSL_NO_TLS12 */
  845. #endif /* WOLFSSL_DTLS */
  846. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  847. return EXPECT_RESULT();
  848. }
  849. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  850. /*----------------------------------------------------------------------------*
  851. | Dual algorithm Certificate Tests
  852. *----------------------------------------------------------------------------*/
  853. #define LARGE_TEMP_SZ 4096
  854. /* To better understand this, please see the X9.146 example in wolfssl-examples
  855. * repo. */
  856. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  857. char *sapkiFile, char *altPrivFile)
  858. {
  859. EXPECT_DECLS;
  860. FILE* file = NULL;
  861. Cert newCert;
  862. DecodedCert preTBS;
  863. byte caKeyBuf[LARGE_TEMP_SZ];
  864. word32 caKeySz = LARGE_TEMP_SZ;
  865. byte sapkiBuf[LARGE_TEMP_SZ];
  866. word32 sapkiSz = LARGE_TEMP_SZ;
  867. byte altPrivBuf[LARGE_TEMP_SZ];
  868. word32 altPrivSz = LARGE_TEMP_SZ;
  869. byte altSigAlgBuf[LARGE_TEMP_SZ];
  870. word32 altSigAlgSz = LARGE_TEMP_SZ;
  871. byte scratchBuf[LARGE_TEMP_SZ];
  872. word32 scratchSz = LARGE_TEMP_SZ;
  873. byte preTbsBuf[LARGE_TEMP_SZ];
  874. word32 preTbsSz = LARGE_TEMP_SZ;
  875. byte altSigValBuf[LARGE_TEMP_SZ];
  876. word32 altSigValSz = LARGE_TEMP_SZ;
  877. byte *outBuf = NULL;
  878. word32 outSz = LARGE_TEMP_SZ;
  879. WC_RNG rng;
  880. RsaKey caKey;
  881. ecc_key altCaKey;
  882. word32 idx = 0;
  883. XMEMSET(&rng, 0, sizeof(WC_RNG));
  884. XMEMSET(&caKey, 0, sizeof(RsaKey));
  885. XMEMSET(&altCaKey, 0, sizeof(ecc_key));
  886. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  887. DYNAMIC_TYPE_TMP_BUFFER));
  888. ExpectIntEQ(wc_InitRng(&rng), 0);
  889. XMEMSET(caKeyBuf, 0, caKeySz);
  890. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  891. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  892. if (file) {
  893. fclose(file);
  894. file = NULL;
  895. }
  896. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  897. idx = 0;
  898. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  899. 0);
  900. XMEMSET(sapkiBuf, 0, sapkiSz);
  901. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  902. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  903. if (file) {
  904. fclose(file);
  905. file = NULL;
  906. }
  907. XMEMSET(altPrivBuf, 0, altPrivSz);
  908. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  909. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  910. if (file) {
  911. fclose(file);
  912. file = NULL;
  913. }
  914. wc_ecc_init(&altCaKey);
  915. idx = 0;
  916. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  917. (word32)altPrivSz), 0);
  918. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  919. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  920. oidSigType, 0), 0);
  921. wc_InitCert(&newCert);
  922. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  923. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  924. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  925. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  926. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  927. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  928. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  929. newCert.sigType = CTC_SHA256wRSA;
  930. newCert.isCA = 1;
  931. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  932. (const byte *)"This is NOT a critical extension", 32), 0);
  933. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  934. sapkiSz), 0);
  935. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  936. altSigAlgSz), 0);
  937. XMEMSET(scratchBuf, 0, scratchSz);
  938. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  939. &caKey, &rng), 0);
  940. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  941. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  942. XMEMSET(preTbsBuf, 0, preTbsSz);
  943. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  944. XMEMSET(altSigValBuf, 0, altSigValSz);
  945. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  946. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  947. &rng), 0);
  948. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  949. altSigValSz), 0);
  950. /* Finally, generate the new certificate. */
  951. if (outBuf != NULL) {
  952. XMEMSET(outBuf, 0, outSz);
  953. }
  954. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  955. 0);
  956. *out = outBuf;
  957. wc_FreeRsaKey(&caKey);
  958. wc_FreeRng(&rng);
  959. wc_FreeDecodedCert(&preTBS);
  960. return outSz;
  961. }
  962. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  963. char *sapkiFile, char *altPrivFile,
  964. char *serverKeyFile,
  965. byte *caCertBuf, int caCertSz)
  966. {
  967. EXPECT_DECLS;
  968. FILE* file = NULL;
  969. Cert newCert;
  970. DecodedCert preTBS;
  971. byte serverKeyBuf[LARGE_TEMP_SZ];
  972. word32 serverKeySz = LARGE_TEMP_SZ;
  973. byte caKeyBuf[LARGE_TEMP_SZ];
  974. word32 caKeySz = LARGE_TEMP_SZ;
  975. byte sapkiBuf[LARGE_TEMP_SZ];
  976. word32 sapkiSz = LARGE_TEMP_SZ;
  977. byte altPrivBuf[LARGE_TEMP_SZ];
  978. word32 altPrivSz = LARGE_TEMP_SZ;
  979. byte altSigAlgBuf[LARGE_TEMP_SZ];
  980. word32 altSigAlgSz = LARGE_TEMP_SZ;
  981. byte scratchBuf[LARGE_TEMP_SZ];
  982. word32 scratchSz = LARGE_TEMP_SZ;
  983. byte preTbsBuf[LARGE_TEMP_SZ];
  984. word32 preTbsSz = LARGE_TEMP_SZ;
  985. byte altSigValBuf[LARGE_TEMP_SZ];
  986. word32 altSigValSz = LARGE_TEMP_SZ;
  987. byte *outBuf = NULL;
  988. word32 outSz = LARGE_TEMP_SZ;
  989. WC_RNG rng;
  990. RsaKey caKey;
  991. RsaKey serverKey;
  992. ecc_key altCaKey;
  993. word32 idx = 0;
  994. XMEMSET(&rng, 0, sizeof(WC_RNG));
  995. XMEMSET(&caKey, 0, sizeof(RsaKey));
  996. XMEMSET(&serverKey, 0, sizeof(RsaKey));
  997. XMEMSET(&altCaKey, 0, sizeof(ecc_key));
  998. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  999. DYNAMIC_TYPE_TMP_BUFFER));
  1000. ExpectIntEQ(wc_InitRng(&rng), 0);
  1001. XMEMSET(serverKeyBuf, 0, serverKeySz);
  1002. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  1003. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  1004. 0);
  1005. if (file) {
  1006. fclose(file);
  1007. file = NULL;
  1008. }
  1009. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  1010. idx = 0;
  1011. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  1012. (word32)serverKeySz), 0);
  1013. XMEMSET(caKeyBuf, 0, caKeySz);
  1014. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  1015. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  1016. if (file) {
  1017. fclose(file);
  1018. file = NULL;
  1019. }
  1020. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  1021. idx = 0;
  1022. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  1023. (word32)caKeySz), 0);
  1024. XMEMSET(sapkiBuf, 0, sapkiSz);
  1025. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  1026. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  1027. if (file) {
  1028. fclose(file);
  1029. file = NULL;
  1030. }
  1031. XMEMSET(altPrivBuf, 0, altPrivSz);
  1032. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  1033. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  1034. if (file) {
  1035. fclose(file);
  1036. file = NULL;
  1037. }
  1038. wc_ecc_init(&altCaKey);
  1039. idx = 0;
  1040. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  1041. (word32)altPrivSz), 0);
  1042. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  1043. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  1044. oidSigType, 0), 0);
  1045. wc_InitCert(&newCert);
  1046. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  1047. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  1048. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  1049. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  1050. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  1051. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  1052. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  1053. newCert.sigType = CTC_SHA256wRSA;
  1054. newCert.isCA = 0;
  1055. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  1056. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  1057. sapkiSz), 0);
  1058. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  1059. altSigAlgSz), 0);
  1060. XMEMSET(scratchBuf, 0, scratchSz);
  1061. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  1062. &rng), 0);
  1063. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  1064. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  1065. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  1066. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  1067. XMEMSET(preTbsBuf, 0, preTbsSz);
  1068. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  1069. XMEMSET(altSigValBuf, 0, altSigValSz);
  1070. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  1071. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  1072. &rng), 0);
  1073. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  1074. altSigValBuf, altSigValSz), 0);
  1075. /* Finally, generate the new certificate. */
  1076. if (outBuf != NULL) {
  1077. XMEMSET(outBuf, 0, outSz);
  1078. }
  1079. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  1080. 0);
  1081. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  1082. outSz, &caKey, NULL, &rng), 0);
  1083. *out = outBuf;
  1084. wc_FreeRsaKey(&caKey);
  1085. wc_FreeRsaKey(&serverKey);
  1086. wc_FreeRng(&rng);
  1087. wc_FreeDecodedCert(&preTBS);
  1088. return outSz;
  1089. }
  1090. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  1091. byte *serverCert, word32 serverCertSz,
  1092. byte *serverKey, word32 serverKeySz,
  1093. int negative_test)
  1094. {
  1095. EXPECT_DECLS;
  1096. WOLFSSL_CTX *ctx_c = NULL;
  1097. WOLFSSL_CTX *ctx_s = NULL;
  1098. WOLFSSL *ssl_c = NULL;
  1099. WOLFSSL *ssl_s = NULL;
  1100. struct test_memio_ctx test_ctx;
  1101. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  1102. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  1103. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  1104. caCert, caCertSz, serverCert, serverCertSz,
  1105. serverKey, serverKeySz), 0);
  1106. if (negative_test) {
  1107. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  1108. }
  1109. else {
  1110. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  1111. }
  1112. wolfSSL_free(ssl_c);
  1113. wolfSSL_free(ssl_s);
  1114. wolfSSL_CTX_free(ctx_c);
  1115. wolfSSL_CTX_free(ctx_s);
  1116. return EXPECT_RESULT();
  1117. }
  1118. static int test_dual_alg_support(void)
  1119. {
  1120. EXPECT_DECLS;
  1121. /* Root CA and server keys will be the same. This is only appropriate for
  1122. * testing. */
  1123. char keyFile[] = "./certs/ca-key.der";
  1124. char sapkiFile[] = "./certs/ecc-keyPub.der";
  1125. char altPrivFile[] = "./certs/ecc-key.der";
  1126. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1127. byte *serverKey = NULL;
  1128. size_t serverKeySz = 0;
  1129. byte *root = NULL;
  1130. int rootSz = 0;
  1131. byte *server = NULL;
  1132. int serverSz = 0;
  1133. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1134. /* Base normal case. */
  1135. if (EXPECT_SUCCESS()) {
  1136. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile,
  1137. altPrivFile);
  1138. }
  1139. ExpectNotNull(root);
  1140. ExpectIntGT(rootSz, 0);
  1141. if (EXPECT_SUCCESS()) {
  1142. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1143. altPrivFile, keyFile, root, rootSz);
  1144. }
  1145. ExpectNotNull(server);
  1146. ExpectIntGT(serverSz, 0);
  1147. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1148. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1149. TEST_SUCCESS);
  1150. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1151. root = NULL;
  1152. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1153. server = NULL;
  1154. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1155. * the alternative signature and then set negative_test to true for the
  1156. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1157. * because the signature won't verify. The exception is if
  1158. * WOLFSSL_TRUST_PEER_CERT is defined. In that case, no verification happens
  1159. * and this is no longer a negative test. */
  1160. if (EXPECT_SUCCESS()) {
  1161. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile,
  1162. wrongPrivFile);
  1163. }
  1164. ExpectNotNull(root);
  1165. ExpectIntGT(rootSz, 0);
  1166. if (EXPECT_SUCCESS()) {
  1167. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1168. wrongPrivFile, keyFile, root, rootSz);
  1169. }
  1170. ExpectNotNull(server);
  1171. ExpectIntGT(serverSz, 0);
  1172. #ifdef WOLFSSL_TRUST_PEER_CERT
  1173. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1174. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1175. TEST_SUCCESS);
  1176. #else
  1177. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1178. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1179. TEST_SUCCESS);
  1180. #endif
  1181. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1182. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1183. free(serverKey);
  1184. return EXPECT_RESULT();
  1185. }
  1186. #else
  1187. static int test_dual_alg_support(void)
  1188. {
  1189. return TEST_SKIPPED;
  1190. }
  1191. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1192. /*----------------------------------------------------------------------------*
  1193. | Context
  1194. *----------------------------------------------------------------------------*/
  1195. #ifndef NO_WOLFSSL_SERVER
  1196. static int test_wolfSSL_CTX_new(void)
  1197. {
  1198. EXPECT_DECLS;
  1199. WOLFSSL_CTX *ctx;
  1200. WOLFSSL_METHOD* method;
  1201. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1202. ExpectNotNull(method = wolfSSLv23_server_method());
  1203. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1204. wolfSSL_CTX_free(ctx);
  1205. return EXPECT_RESULT();
  1206. }
  1207. #endif
  1208. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1209. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1210. static int test_for_double_Free(void)
  1211. {
  1212. EXPECT_DECLS;
  1213. WOLFSSL_CTX* ctx = NULL;
  1214. WOLFSSL* ssl = NULL;
  1215. int skipTest = 0;
  1216. const char* testCertFile;
  1217. const char* testKeyFile;
  1218. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1219. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1220. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1221. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1222. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1223. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1224. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1225. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1226. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1227. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1228. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1229. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1230. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1231. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1232. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1233. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1234. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1235. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1236. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1237. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1238. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1239. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1240. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1241. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1242. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1243. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1244. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1245. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1246. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1247. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1248. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1249. #ifdef OPENSSL_EXTRA
  1250. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1251. "!SRP:!kRSA:!aNULL:!eNULL";
  1252. #endif
  1253. #ifndef NO_RSA
  1254. testCertFile = svrCertFile;
  1255. testKeyFile = svrKeyFile;
  1256. #elif defined(HAVE_ECC)
  1257. testCertFile = eccCertFile;
  1258. testKeyFile = eccKeyFile;
  1259. #else
  1260. skipTest = 1;
  1261. #endif
  1262. if (skipTest != 1) {
  1263. #ifndef NO_WOLFSSL_SERVER
  1264. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1265. #else
  1266. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1267. #endif
  1268. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1269. WOLFSSL_FILETYPE_PEM));
  1270. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1271. WOLFSSL_FILETYPE_PEM));
  1272. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1273. /* First test freeing SSL, then CTX */
  1274. wolfSSL_free(ssl);
  1275. ssl = NULL;
  1276. wolfSSL_CTX_free(ctx);
  1277. ctx = NULL;
  1278. #ifndef NO_WOLFSSL_CLIENT
  1279. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1280. #else
  1281. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1282. #endif
  1283. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1284. WOLFSSL_FILETYPE_PEM));
  1285. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1286. WOLFSSL_FILETYPE_PEM));
  1287. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1288. /* Next test freeing CTX then SSL */
  1289. wolfSSL_CTX_free(ctx);
  1290. ctx = NULL;
  1291. wolfSSL_free(ssl);
  1292. ssl = NULL;
  1293. #ifndef NO_WOLFSSL_SERVER
  1294. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1295. #else
  1296. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1297. #endif
  1298. /* Test setting ciphers at ctx level */
  1299. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1300. WOLFSSL_FILETYPE_PEM));
  1301. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1302. WOLFSSL_FILETYPE_PEM));
  1303. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1304. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1305. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1306. /* only update TLSv13 suites */
  1307. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1308. #endif
  1309. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1310. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1311. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1312. /* only update pre-TLSv13 suites */
  1313. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1314. "ECDHE-RSA-AES128-GCM-SHA256"));
  1315. #endif
  1316. #ifdef OPENSSL_EXTRA
  1317. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1318. #endif
  1319. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1320. wolfSSL_CTX_free(ctx);
  1321. ctx = NULL;
  1322. wolfSSL_free(ssl);
  1323. ssl = NULL;
  1324. #ifndef NO_WOLFSSL_CLIENT
  1325. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1326. #else
  1327. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1328. #endif
  1329. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1330. WOLFSSL_FILETYPE_PEM));
  1331. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1332. WOLFSSL_FILETYPE_PEM));
  1333. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1334. /* test setting ciphers at SSL level */
  1335. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1336. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1337. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1338. /* only update TLSv13 suites */
  1339. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1340. #endif
  1341. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1342. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1343. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1344. /* only update pre-TLSv13 suites */
  1345. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1346. #endif
  1347. wolfSSL_CTX_free(ctx);
  1348. ctx = NULL;
  1349. wolfSSL_free(ssl);
  1350. ssl = NULL;
  1351. }
  1352. return EXPECT_RESULT();
  1353. }
  1354. #endif
  1355. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1356. {
  1357. EXPECT_DECLS;
  1358. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1359. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1360. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1361. const char* testCertFile;
  1362. const char* testKeyFile;
  1363. WOLFSSL_CTX* ctx = NULL;
  1364. WOLFSSL* ssl = NULL;
  1365. const byte cipherList[] =
  1366. {
  1367. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0x00, 0x16,
  1368. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0x00, 0x39,
  1369. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0x00, 0x33,
  1370. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0x00, 0x34,
  1371. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0x00, 0x35,
  1372. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0x00, 0x2F,
  1373. /* TLS_RSA_WITH_NULL_MD5 */ 0x00, 0x01,
  1374. /* TLS_RSA_WITH_NULL_SHA */ 0x00, 0x02,
  1375. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0x00, 0x8d,
  1376. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xae,
  1377. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xaf,
  1378. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0x00, 0x8c,
  1379. /* TLS_PSK_WITH_NULL_SHA256 */ 0x00, 0xb0,
  1380. /* TLS_PSK_WITH_NULL_SHA384 */ 0x00, 0xb1,
  1381. /* TLS_PSK_WITH_NULL_SHA */ 0x00, 0x2c,
  1382. /* SSL_RSA_WITH_RC4_128_SHA */ 0x00, 0x05,
  1383. /* SSL_RSA_WITH_RC4_128_MD5 */ 0x00, 0x04,
  1384. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0x00, 0x0A,
  1385. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1386. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1387. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1388. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1389. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1390. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1391. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1392. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1393. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1394. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1395. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1396. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1397. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1398. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1399. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1400. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1401. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1402. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1403. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1404. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1405. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1406. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1407. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1408. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1409. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1410. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1411. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1412. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1413. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1414. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1415. /* SHA256 */
  1416. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1417. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1418. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1419. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1420. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1421. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1422. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1423. /* SHA384 */
  1424. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1425. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1426. /* AES-GCM */
  1427. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1428. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1429. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1430. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1431. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1432. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1433. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1434. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1435. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1436. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1437. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1438. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1439. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1440. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1441. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1442. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1443. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1444. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1445. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1446. * also, in some of the other AES-CCM suites
  1447. * there will be second byte number conflicts
  1448. * with non-ECC AES-GCM */
  1449. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1450. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1451. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1452. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1453. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1454. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1455. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1456. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1457. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1458. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1459. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1460. /* Camellia */
  1461. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1462. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1463. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1464. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1465. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1466. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1467. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1468. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1469. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1470. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1471. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1472. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1473. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1474. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1475. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1476. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1477. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1478. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1479. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1480. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1481. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1482. /* TLS v1.3 cipher suites */
  1483. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1484. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1485. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1486. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1487. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1488. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1489. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1490. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1491. };
  1492. #ifndef NO_RSA
  1493. testCertFile = svrCertFile;
  1494. testKeyFile = svrKeyFile;
  1495. #elif defined(HAVE_ECC)
  1496. testCertFile = eccCertFile;
  1497. testKeyFile = eccKeyFile;
  1498. #endif
  1499. #ifndef NO_WOLFSSL_SERVER
  1500. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1501. #else
  1502. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1503. #endif
  1504. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1505. sizeof(cipherList)));
  1506. wolfSSL_CTX_free(ctx);
  1507. ctx = NULL;
  1508. #ifndef NO_WOLFSSL_SERVER
  1509. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1510. #else
  1511. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1512. #endif
  1513. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1514. WOLFSSL_FILETYPE_PEM));
  1515. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1516. WOLFSSL_FILETYPE_PEM));
  1517. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1518. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1519. sizeof(cipherList)));
  1520. wolfSSL_free(ssl);
  1521. wolfSSL_CTX_free(ctx);
  1522. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1523. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1524. return EXPECT_RESULT();
  1525. }
  1526. static int test_wolfSSL_CTX_use_certificate(void)
  1527. {
  1528. EXPECT_DECLS;
  1529. #if defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || \
  1530. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL) || \
  1531. defined(WOLFSSL_NGINX) || defined(HAVE_POCO_LIB) || \
  1532. defined(WOLFSSL_HAPROXY)
  1533. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  1534. WOLFSSL_CTX* ctx = NULL;
  1535. X509* x509 = NULL;
  1536. #ifndef NO_WOLFSSL_SERVER
  1537. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1538. #else
  1539. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1540. #endif
  1541. ExpectNotNull(x509 = wolfSSL_X509_new());
  1542. /* Negative tests. */
  1543. ExpectIntEQ(SSL_CTX_use_certificate(NULL, NULL), 0);
  1544. ExpectIntEQ(SSL_CTX_use_certificate(ctx, NULL), 0);
  1545. ExpectIntEQ(SSL_CTX_use_certificate(NULL, x509), 0);
  1546. /* Empty certificate */
  1547. ExpectIntEQ(SSL_CTX_use_certificate(ctx, x509), 0);
  1548. wolfSSL_X509_free(x509);
  1549. wolfSSL_CTX_free(ctx);
  1550. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  1551. #endif
  1552. return EXPECT_RESULT();
  1553. }
  1554. static int test_wolfSSL_CTX_use_certificate_file(void)
  1555. {
  1556. EXPECT_DECLS;
  1557. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1558. WOLFSSL_CTX *ctx = NULL;
  1559. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1560. /* invalid context */
  1561. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1562. WOLFSSL_FILETYPE_PEM));
  1563. /* invalid cert file */
  1564. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1565. WOLFSSL_FILETYPE_PEM));
  1566. /* invalid cert type */
  1567. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1568. #ifdef NO_RSA
  1569. /* rsa needed */
  1570. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1571. WOLFSSL_FILETYPE_PEM));
  1572. #else
  1573. /* success */
  1574. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1575. WOLFSSL_FILETYPE_PEM));
  1576. #endif
  1577. wolfSSL_CTX_free(ctx);
  1578. #endif
  1579. return EXPECT_RESULT();
  1580. }
  1581. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1582. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1583. {
  1584. EXPECT_DECLS;
  1585. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1586. WOLFSSL_CTX* ctx = NULL;
  1587. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1588. /* Failure cases. */
  1589. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(NULL, 0, NULL ),
  1590. WOLFSSL_FAILURE);
  1591. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx , 0, NULL ),
  1592. WOLFSSL_FAILURE);
  1593. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(NULL, 0, server_cert_der_2048),
  1594. WOLFSSL_FAILURE);
  1595. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx , 0, server_cert_der_2048),
  1596. WOLFSSL_FAILURE);
  1597. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1598. server_cert_der_2048), WOLFSSL_SUCCESS);
  1599. wolfSSL_CTX_free(ctx);
  1600. #endif
  1601. return EXPECT_RESULT();
  1602. }
  1603. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1604. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1605. * context using buffer.
  1606. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1607. * --enable-testcert flag.
  1608. */
  1609. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1610. {
  1611. EXPECT_DECLS;
  1612. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1613. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1614. WOLFSSL_CTX* ctx = NULL;
  1615. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1616. /* Invalid parameters. */
  1617. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(NULL, NULL, 0,
  1618. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1619. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, NULL, 0,
  1620. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1621. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(NULL, server_cert_der_2048,
  1622. 0, WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1623. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048, 0,
  1624. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1625. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx,
  1626. server_cert_der_2048, sizeof_server_cert_der_2048,
  1627. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1628. wolfSSL_CTX_free(ctx);
  1629. #endif
  1630. return EXPECT_RESULT();
  1631. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1632. static int test_wolfSSL_use_certificate_buffer(void)
  1633. {
  1634. EXPECT_DECLS;
  1635. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1636. !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  1637. WOLFSSL_CTX* ctx = NULL;
  1638. WOLFSSL* ssl = NULL;
  1639. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1640. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1641. /* Invalid parameters. */
  1642. ExpectIntEQ(wolfSSL_use_certificate_buffer(NULL, NULL, 0,
  1643. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1644. ExpectIntEQ(wolfSSL_use_certificate_buffer(ssl, NULL, 0,
  1645. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1646. ExpectIntEQ(wolfSSL_use_certificate_buffer(NULL, client_cert_der_2048, 0,
  1647. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1648. ExpectIntEQ(wolfSSL_use_certificate_buffer(ssl, client_cert_der_2048, 0,
  1649. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1650. ExpectIntEQ(wolfSSL_use_certificate_buffer(ssl,
  1651. client_cert_der_2048, sizeof_client_cert_der_2048,
  1652. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1653. wolfSSL_free(ssl);
  1654. wolfSSL_CTX_free(ctx);
  1655. #endif
  1656. return EXPECT_RESULT();
  1657. }
  1658. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1659. {
  1660. EXPECT_DECLS;
  1661. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1662. WOLFSSL_CTX *ctx = NULL;
  1663. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1664. /* invalid context */
  1665. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1666. WOLFSSL_FILETYPE_PEM));
  1667. /* invalid key file */
  1668. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1669. WOLFSSL_FILETYPE_PEM));
  1670. /* invalid key type */
  1671. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1672. /* success */
  1673. #ifdef NO_RSA
  1674. /* rsa needed */
  1675. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1676. WOLFSSL_FILETYPE_PEM));
  1677. #else
  1678. /* success */
  1679. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1680. WOLFSSL_FILETYPE_PEM));
  1681. #endif
  1682. wolfSSL_CTX_free(ctx);
  1683. #endif
  1684. return EXPECT_RESULT();
  1685. }
  1686. static int test_wolfSSL_CTX_use_RSAPrivateKey_file(void)
  1687. {
  1688. EXPECT_DECLS;
  1689. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1690. !defined(NO_WOLFSSL_SERVER) && defined(OPENSSL_EXTRA)
  1691. WOLFSSL_CTX *ctx = NULL;
  1692. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1693. /* invalid context */
  1694. ExpectIntEQ(wolfSSL_CTX_use_RSAPrivateKey_file(NULL, svrKeyFile,
  1695. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  1696. /* invalid key file */
  1697. ExpectIntEQ(wolfSSL_CTX_use_RSAPrivateKey_file(ctx, bogusFile,
  1698. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  1699. /* invalid key type */
  1700. ExpectIntEQ(wolfSSL_CTX_use_RSAPrivateKey_file(ctx, svrKeyFile, 9999),
  1701. WOLFSSL_FAILURE);
  1702. /* success */
  1703. #ifdef NO_RSA
  1704. /* rsa needed */
  1705. ExpectIntEQ(wolfSSL_CTX_use_RSAPrivateKey_file(ctx, svrKeyFile,
  1706. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  1707. #else
  1708. /* success */
  1709. ExpectIntEQ(wolfSSL_CTX_use_RSAPrivateKey_file(ctx, svrKeyFile,
  1710. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1711. #endif
  1712. wolfSSL_CTX_free(ctx);
  1713. #endif
  1714. return EXPECT_RESULT();
  1715. }
  1716. static int test_wolfSSL_use_RSAPrivateKey_file(void)
  1717. {
  1718. EXPECT_DECLS;
  1719. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1720. !defined(NO_WOLFSSL_CLIENT) && defined(OPENSSL_EXTRA)
  1721. WOLFSSL_CTX* ctx = NULL;
  1722. WOLFSSL* ssl = NULL;
  1723. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1724. ExpectNotNull(ssl = SSL_new(ctx));
  1725. /* invalid context */
  1726. ExpectIntEQ(wolfSSL_use_RSAPrivateKey_file(NULL, svrKeyFile,
  1727. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  1728. /* invalid key file */
  1729. ExpectIntEQ(wolfSSL_use_RSAPrivateKey_file(ssl, bogusFile,
  1730. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  1731. /* invalid key type */
  1732. ExpectIntEQ(wolfSSL_use_RSAPrivateKey_file(ssl, svrKeyFile, 9999),
  1733. WOLFSSL_FAILURE);
  1734. /* success */
  1735. #ifdef NO_RSA
  1736. /* rsa needed */
  1737. ExpectIntEQ(wolfSSL_use_RSAPrivateKey_file(ssl, svrKeyFile,
  1738. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  1739. #else
  1740. /* success */
  1741. ExpectIntEQ(wolfSSL_use_RSAPrivateKey_file(ssl, svrKeyFile,
  1742. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1743. #endif
  1744. wolfSSL_free(ssl);
  1745. wolfSSL_CTX_free(ctx);
  1746. #endif
  1747. return EXPECT_RESULT();
  1748. }
  1749. static int test_wolfSSL_CTX_use_PrivateKey(void)
  1750. {
  1751. EXPECT_DECLS;
  1752. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1753. !defined(NO_WOLFSSL_SERVER) && defined(OPENSSL_EXTRA)
  1754. WOLFSSL_CTX *ctx = NULL;
  1755. WOLFSSL_EVP_PKEY* pkey = NULL;
  1756. const unsigned char* p;
  1757. (void)p;
  1758. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1759. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  1760. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(NULL, NULL), WOLFSSL_FAILURE);
  1761. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(ctx, NULL), WOLFSSL_FAILURE);
  1762. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(NULL, pkey), WOLFSSL_FAILURE);
  1763. /* No data. */
  1764. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_FAILURE);
  1765. wolfSSL_EVP_PKEY_free(pkey);
  1766. pkey = NULL;
  1767. #if defined(USE_CERT_BUFFERS_2048)
  1768. #if !defined(NO_RSA)
  1769. p = client_key_der_2048;
  1770. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p,
  1771. sizeof_client_key_der_2048));
  1772. #if defined(WOLFSSL_KEY_GEN)
  1773. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  1774. #else
  1775. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_FAILURE);
  1776. #endif
  1777. wolfSSL_EVP_PKEY_free(pkey);
  1778. pkey = NULL;
  1779. #endif
  1780. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  1781. #ifndef NO_DSA
  1782. p = dsa_key_der_2048;
  1783. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_DSA, NULL, &p,
  1784. sizeof_dsa_key_der_2048));
  1785. #if !defined(HAVE_SELFTEST) && (defined(WOLFSSL_KEY_GEN) || \
  1786. defined(WOLFSSL_CERT_GEN))
  1787. /* Not supported in ProcessBuffer. */
  1788. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_BAD_FILE);
  1789. #else
  1790. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_FAILURE);
  1791. #endif
  1792. wolfSSL_EVP_PKEY_free(pkey);
  1793. pkey = NULL;
  1794. #endif
  1795. #endif /* WOLFSSL_QT || OPENSSL_ALL || WOLFSSL_OPENSSH */
  1796. #if !defined(NO_DH) && defined(OPENSSL_ALL) && \
  1797. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  1798. p = dh_ffdhe_statickey_der_2048;
  1799. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &p,
  1800. sizeof_dh_ffdhe_statickey_der_2048));
  1801. /* Not supported. */
  1802. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_FAILURE);
  1803. wolfSSL_EVP_PKEY_free(pkey);
  1804. pkey = NULL;
  1805. #endif
  1806. #endif /* USE_CERT_BUFFERS_2048 */
  1807. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  1808. p = ecc_clikey_der_256;
  1809. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p,
  1810. sizeof_ecc_clikey_der_256));
  1811. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  1812. wolfSSL_EVP_PKEY_free(pkey);
  1813. pkey = NULL;
  1814. #endif
  1815. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  1816. (unsigned char*)"01234567012345670123456701234567", 32));
  1817. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_FAILURE);
  1818. wolfSSL_EVP_PKEY_free(pkey);
  1819. pkey = NULL;
  1820. wolfSSL_CTX_free(ctx);
  1821. #endif
  1822. return EXPECT_RESULT();
  1823. }
  1824. /* test both file and buffer versions along with unloading trusted peer certs */
  1825. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1826. {
  1827. EXPECT_DECLS;
  1828. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1829. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1830. WOLFSSL_CTX *ctx = NULL;
  1831. WOLFSSL* ssl = NULL;
  1832. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1833. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1834. #if !defined(NO_FILESYSTEM)
  1835. /* invalid file */
  1836. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1837. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1838. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1839. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1840. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1841. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1842. /* success */
  1843. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1844. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1845. /* unload cert */
  1846. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1847. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1848. /* invalid file */
  1849. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1850. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1851. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1852. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1853. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1854. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1855. /* success */
  1856. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1857. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1858. #ifdef WOLFSSL_LOCAL_X509_STORE
  1859. /* unload cert */
  1860. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1861. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1862. #endif
  1863. #endif
  1864. /* Test of loading certs from buffers */
  1865. /* invalid buffer */
  1866. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1867. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1868. /* success */
  1869. #ifdef USE_CERT_BUFFERS_1024
  1870. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1871. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1872. #endif
  1873. #ifdef USE_CERT_BUFFERS_2048
  1874. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1875. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1876. #endif
  1877. /* unload cert */
  1878. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1879. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1880. wolfSSL_free(ssl);
  1881. wolfSSL_CTX_free(ctx);
  1882. #endif
  1883. return EXPECT_RESULT();
  1884. }
  1885. static int test_wolfSSL_CTX_load_verify_locations(void)
  1886. {
  1887. EXPECT_DECLS;
  1888. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1889. WOLFSSL_CTX *ctx = NULL;
  1890. #ifndef NO_RSA
  1891. WOLFSSL_CERT_MANAGER* cm = NULL;
  1892. #ifdef PERSIST_CERT_CACHE
  1893. int cacheSz = 0;
  1894. unsigned char* cache = NULL;
  1895. int used = 0;
  1896. #ifndef NO_FILESYSTEM
  1897. const char* cacheFile = "./tests/cert_cache.tmp";
  1898. #endif
  1899. int i;
  1900. int t;
  1901. int* p;
  1902. #endif
  1903. #endif
  1904. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1905. const char* load_certs_path = "./certs/external";
  1906. const char* load_no_certs_path = "./examples";
  1907. const char* load_expired_path = "./certs/test/expired";
  1908. #endif
  1909. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1910. /* invalid arguments */
  1911. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1912. WOLFSSL_FAILURE);
  1913. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1914. WOLFSSL_FAILURE);
  1915. /* invalid ca file */
  1916. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1917. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1918. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1919. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1920. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1921. /* invalid path */
  1922. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1923. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1924. #endif
  1925. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1926. /* test ignoring the invalid path */
  1927. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1928. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1929. #endif
  1930. /* load ca cert */
  1931. #ifdef NO_RSA
  1932. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1933. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1934. #else /* Skip the following test without RSA certs. */
  1935. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1936. WOLFSSL_SUCCESS);
  1937. #ifdef PERSIST_CERT_CACHE
  1938. /* Get cert cache size */
  1939. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1940. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1941. DYNAMIC_TYPE_TMP_BUFFER));
  1942. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1943. BAD_FUNC_ARG);
  1944. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1945. BAD_FUNC_ARG);
  1946. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1947. BAD_FUNC_ARG);
  1948. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1949. BAD_FUNC_ARG);
  1950. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1951. BAD_FUNC_ARG);
  1952. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1953. BAD_FUNC_ARG);
  1954. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1955. BAD_FUNC_ARG);
  1956. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1957. BAD_FUNC_ARG);
  1958. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1959. BAD_FUNC_ARG);
  1960. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1961. BUFFER_E);
  1962. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1963. ExpectIntEQ(cacheSz, used);
  1964. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1965. BAD_FUNC_ARG);
  1966. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1967. BAD_FUNC_ARG);
  1968. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1969. BAD_FUNC_ARG);
  1970. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1971. BAD_FUNC_ARG);
  1972. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1973. BAD_FUNC_ARG);
  1974. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1975. BAD_FUNC_ARG);
  1976. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1977. BAD_FUNC_ARG);
  1978. /* Smaller than header. */
  1979. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1980. for (i = 1; i < cacheSz; i++) {
  1981. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1982. BUFFER_E);
  1983. }
  1984. if (EXPECT_SUCCESS()) {
  1985. /* Modify header for bad results! */
  1986. p = (int*)cache;
  1987. /* version */
  1988. t = p[0]; p[0] = 0xff;
  1989. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1990. CACHE_MATCH_ERROR);
  1991. p[0] = t; p++;
  1992. /* rows */
  1993. t = p[0]; p[0] = 0xff;
  1994. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1995. CACHE_MATCH_ERROR);
  1996. p[0] = t; p++;
  1997. /* columns[0] */
  1998. t = p[0]; p[0] = -1;
  1999. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  2000. PARSE_ERROR);
  2001. p[0] = t; p += CA_TABLE_SIZE;
  2002. /* signerSz*/
  2003. t = p[0]; p[0] = 0xff;
  2004. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  2005. CACHE_MATCH_ERROR);
  2006. p[0] = t;
  2007. }
  2008. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  2009. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  2010. #ifndef NO_FILESYSTEM
  2011. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  2012. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  2013. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  2014. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  2015. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  2016. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  2017. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  2018. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  2019. WOLFSSL_BAD_FILE);
  2020. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  2021. /* File contents is not a cache. */
  2022. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  2023. CACHE_MATCH_ERROR);
  2024. #endif
  2025. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2026. #endif
  2027. /* Test unloading CA's */
  2028. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  2029. #ifdef PERSIST_CERT_CACHE
  2030. /* Verify no certs (result is less than cacheSz) */
  2031. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  2032. #endif
  2033. /* load ca cert again */
  2034. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  2035. WOLFSSL_SUCCESS);
  2036. /* Test getting CERT_MANAGER */
  2037. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  2038. /* Test unloading CA's using CM */
  2039. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  2040. #ifdef PERSIST_CERT_CACHE
  2041. /* Verify no certs (result is less than cacheSz) */
  2042. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  2043. #endif
  2044. #endif
  2045. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  2046. /* Test loading CA certificates using a path */
  2047. #ifdef NO_RSA
  2048. /* failure here okay since certs in external directory are RSA */
  2049. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  2050. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  2051. #else
  2052. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  2053. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  2054. #endif
  2055. /* Test loading path with no files */
  2056. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  2057. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  2058. /* Test loading expired CA certificates */
  2059. #ifdef NO_RSA
  2060. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  2061. load_expired_path,
  2062. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  2063. WOLFSSL_SUCCESS);
  2064. #else
  2065. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  2066. load_expired_path,
  2067. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  2068. WOLFSSL_SUCCESS);
  2069. #endif
  2070. /* Test loading CA certificates and ignoring all errors */
  2071. #ifdef NO_RSA
  2072. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  2073. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  2074. #else
  2075. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  2076. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  2077. #endif
  2078. #endif
  2079. wolfSSL_CTX_free(ctx);
  2080. #endif
  2081. return EXPECT_RESULT();
  2082. }
  2083. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  2084. {
  2085. int res = TEST_SKIPPED;
  2086. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  2087. (!defined(NO_RSA) || defined(HAVE_ECC))
  2088. WOLFSSL_CTX* ctx;
  2089. byte dirValid = 0;
  2090. int ret = 0;
  2091. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2092. if (ctx == NULL) {
  2093. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  2094. ret = -1;
  2095. }
  2096. if (ret == 0) {
  2097. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  2098. dirValid = 1;
  2099. #else
  2100. word32 numDirs;
  2101. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  2102. if (caDirs == NULL || numDirs == 0) {
  2103. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  2104. ret = -1;
  2105. }
  2106. else {
  2107. ReadDirCtx dirCtx;
  2108. word32 i;
  2109. for (i = 0; i < numDirs; ++i) {
  2110. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  2111. /* Directory isn't empty. */
  2112. dirValid = 1;
  2113. wc_ReadDirClose(&dirCtx);
  2114. break;
  2115. }
  2116. }
  2117. }
  2118. #endif
  2119. }
  2120. /*
  2121. * If the directory isn't empty, we should be able to load CA
  2122. * certs from it. On Windows/Mac, we assume the CA cert stores are
  2123. * usable.
  2124. */
  2125. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  2126. WOLFSSL_SUCCESS) {
  2127. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  2128. ret = -1;
  2129. }
  2130. #ifdef OPENSSL_EXTRA
  2131. if (ret == 0 &&
  2132. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  2133. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  2134. ret = -1;
  2135. }
  2136. #endif /* OPENSSL_EXTRA */
  2137. wolfSSL_CTX_free(ctx);
  2138. res = TEST_RES_CHECK(ret == 0);
  2139. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  2140. return res;
  2141. }
  2142. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2143. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  2144. int file_type)
  2145. {
  2146. int ret;
  2147. WOLFSSL_CERT_MANAGER* cm;
  2148. cm = wolfSSL_CertManagerNew();
  2149. if (cm == NULL) {
  2150. fprintf(stderr, "test_cm_load_ca failed\n");
  2151. return -1;
  2152. }
  2153. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  2154. wolfSSL_CertManagerFree(cm);
  2155. return ret;
  2156. }
  2157. static int test_cm_load_ca_file(const char* ca_cert_file)
  2158. {
  2159. int ret = 0;
  2160. byte* cert_buf = NULL;
  2161. size_t cert_sz = 0;
  2162. #if defined(WOLFSSL_PEM_TO_DER)
  2163. DerBuffer* pDer = NULL;
  2164. #endif
  2165. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  2166. if (ret == 0) {
  2167. /* normal test */
  2168. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  2169. if (ret == WOLFSSL_SUCCESS) {
  2170. /* test including null terminator in length */
  2171. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  2172. if (tmp == NULL) {
  2173. ret = MEMORY_E;
  2174. }
  2175. else {
  2176. cert_buf = tmp;
  2177. cert_buf[cert_sz] = '\0';
  2178. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  2179. WOLFSSL_FILETYPE_PEM);
  2180. }
  2181. }
  2182. #if defined(WOLFSSL_PEM_TO_DER)
  2183. if (ret == WOLFSSL_SUCCESS) {
  2184. /* test loading DER */
  2185. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  2186. if (ret == 0 && pDer != NULL) {
  2187. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  2188. WOLFSSL_FILETYPE_ASN1);
  2189. wc_FreeDer(&pDer);
  2190. }
  2191. }
  2192. #endif
  2193. }
  2194. free(cert_buf);
  2195. return ret;
  2196. }
  2197. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  2198. int file_type, word32 flags)
  2199. {
  2200. int ret;
  2201. WOLFSSL_CERT_MANAGER* cm;
  2202. cm = wolfSSL_CertManagerNew();
  2203. if (cm == NULL) {
  2204. fprintf(stderr, "test_cm_load_ca failed\n");
  2205. return -1;
  2206. }
  2207. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  2208. 0, flags);
  2209. wolfSSL_CertManagerFree(cm);
  2210. return ret;
  2211. }
  2212. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  2213. {
  2214. int ret = 0;
  2215. byte* cert_buf = NULL;
  2216. size_t cert_sz = 0;
  2217. #if defined(WOLFSSL_PEM_TO_DER)
  2218. DerBuffer* pDer = NULL;
  2219. #endif
  2220. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  2221. if (ret == 0) {
  2222. /* normal test */
  2223. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  2224. WOLFSSL_FILETYPE_PEM, flags);
  2225. if (ret == WOLFSSL_SUCCESS) {
  2226. /* test including null terminator in length */
  2227. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  2228. if (tmp == NULL) {
  2229. ret = MEMORY_E;
  2230. }
  2231. else {
  2232. cert_buf = tmp;
  2233. cert_buf[cert_sz] = '\0';
  2234. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  2235. WOLFSSL_FILETYPE_PEM, flags);
  2236. }
  2237. }
  2238. #if defined(WOLFSSL_PEM_TO_DER)
  2239. if (ret == WOLFSSL_SUCCESS) {
  2240. /* test loading DER */
  2241. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  2242. if (ret == 0 && pDer != NULL) {
  2243. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  2244. WOLFSSL_FILETYPE_ASN1, flags);
  2245. wc_FreeDer(&pDer);
  2246. }
  2247. }
  2248. #endif
  2249. }
  2250. free(cert_buf);
  2251. return ret;
  2252. }
  2253. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  2254. static int test_wolfSSL_CertManagerAPI(void)
  2255. {
  2256. EXPECT_DECLS;
  2257. #ifndef NO_CERTS
  2258. WOLFSSL_CERT_MANAGER* cm = NULL;
  2259. unsigned char c;
  2260. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2261. wolfSSL_CertManagerFree(NULL);
  2262. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  2263. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  2264. #ifdef WOLFSSL_TRUST_PEER_CERT
  2265. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  2266. #endif
  2267. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  2268. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  2269. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2270. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  2271. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2272. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  2273. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2274. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  2275. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2276. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  2277. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2278. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  2279. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2280. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  2281. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2282. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  2283. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2284. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  2285. WOLFSSL_BAD_FILETYPE);
  2286. #endif
  2287. #if !defined(NO_FILESYSTEM)
  2288. {
  2289. const char* ca_cert = "./certs/ca-cert.pem";
  2290. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2291. const char* ca_cert_der = "./certs/ca-cert.der";
  2292. #endif
  2293. const char* ca_path = "./certs";
  2294. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2295. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  2296. BAD_FUNC_ARG);
  2297. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2298. BAD_FUNC_ARG);
  2299. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  2300. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  2301. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  2302. WOLFSSL_BAD_FILETYPE);
  2303. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  2304. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  2305. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  2306. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  2307. #endif
  2308. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  2309. WOLFSSL_FATAL_ERROR);
  2310. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  2311. WOLFSSL_FATAL_ERROR);
  2312. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  2313. WOLFSSL_FATAL_ERROR);
  2314. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  2315. WOLFSSL_FATAL_ERROR);
  2316. }
  2317. #endif
  2318. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2319. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  2320. #elif !defined(HAVE_CRL)
  2321. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  2322. #endif
  2323. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  2324. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  2325. #ifdef HAVE_CRL
  2326. /* Test APIs when CRL is disabled. */
  2327. #ifdef HAVE_CRL_IO
  2328. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2329. #endif
  2330. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2331. sizeof_server_cert_der_2048), 1);
  2332. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2333. #endif
  2334. /* OCSP */
  2335. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  2336. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  2337. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  2338. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  2339. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2340. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2341. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  2342. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2343. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2344. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2345. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2346. #endif
  2347. #ifdef HAVE_OCSP
  2348. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2349. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2350. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2351. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2352. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2353. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2354. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2355. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2356. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2357. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2358. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2359. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2360. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2361. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2362. BAD_FUNC_ARG);
  2363. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2364. BAD_FUNC_ARG);
  2365. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2366. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2367. BAD_FUNC_ARG);
  2368. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2369. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2370. /* Test APIs when OCSP is disabled. */
  2371. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2372. NULL, NULL, NULL, NULL), 1);
  2373. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2374. #endif
  2375. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2376. if (EXPECT_SUCCESS()) {
  2377. wolfSSL_CertManagerFree(cm);
  2378. }
  2379. wolfSSL_CertManagerFree(cm);
  2380. cm = NULL;
  2381. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2382. #ifdef HAVE_OCSP
  2383. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2384. WOLFSSL_OCSP_CHECKALL), 1);
  2385. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2386. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2387. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2388. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2389. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2390. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2391. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2392. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2393. #endif
  2394. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2395. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2396. #endif
  2397. #ifdef WOLFSSL_TRUST_PEER_CERT
  2398. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2399. #endif
  2400. wolfSSL_CertManagerFree(cm);
  2401. #endif
  2402. return EXPECT_RESULT();
  2403. }
  2404. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2405. {
  2406. EXPECT_DECLS;
  2407. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2408. const char* ca_cert = "./certs/ca-cert.pem";
  2409. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2410. int ret;
  2411. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2412. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2413. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2414. #elif defined(NO_RSA)
  2415. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2416. #else
  2417. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2418. #endif
  2419. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2420. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2421. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2422. #elif defined(NO_RSA)
  2423. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2424. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2425. !defined(NO_ASN_TIME)
  2426. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2427. #else
  2428. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2429. #endif
  2430. #endif
  2431. return EXPECT_RESULT();
  2432. }
  2433. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2434. {
  2435. EXPECT_DECLS;
  2436. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2437. const char* ca_cert = "./certs/ca-cert.pem";
  2438. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2439. int ret;
  2440. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2441. 1);
  2442. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2443. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2444. #elif defined(NO_RSA)
  2445. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2446. #else
  2447. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2448. #endif
  2449. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2450. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2451. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2452. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2453. #elif defined(NO_RSA)
  2454. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2455. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2456. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2457. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2458. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2459. #else
  2460. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2461. #endif
  2462. #endif
  2463. return EXPECT_RESULT();
  2464. }
  2465. static int test_wolfSSL_CertManagerGetCerts(void)
  2466. {
  2467. EXPECT_DECLS;
  2468. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2469. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2470. defined(WOLFSSL_SIGNER_DER_CERT)
  2471. WOLFSSL_CERT_MANAGER* cm = NULL;
  2472. WOLFSSL_STACK* sk = NULL;
  2473. X509* x509 = NULL;
  2474. X509* cert1 = NULL;
  2475. FILE* file1 = NULL;
  2476. #ifdef DEBUG_WOLFSSL_VERBOSE
  2477. WOLFSSL_BIO* bio = NULL;
  2478. #endif
  2479. int i = 0;
  2480. int ret = 0;
  2481. const byte* der = NULL;
  2482. int derSz = 0;
  2483. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2484. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2485. if (file1 != NULL) {
  2486. fclose(file1);
  2487. }
  2488. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2489. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2490. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2491. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2492. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2493. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2494. * and full OpenSSL compatibility */
  2495. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2496. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2497. #else
  2498. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2499. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2500. #endif
  2501. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2502. "./certs/ca-cert.pem", NULL));
  2503. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2504. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2505. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2506. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2507. #ifdef DEBUG_WOLFSSL_VERBOSE
  2508. bio = BIO_new(wolfSSL_BIO_s_file());
  2509. if (bio != NULL) {
  2510. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2511. X509_print(bio, x509);
  2512. BIO_free(bio);
  2513. }
  2514. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2515. }
  2516. wolfSSL_X509_free(cert1);
  2517. sk_X509_pop_free(sk, NULL);
  2518. wolfSSL_CertManagerFree(cm);
  2519. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2520. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2521. defined(WOLFSSL_SIGNER_DER_CERT) */
  2522. return EXPECT_RESULT();
  2523. }
  2524. static int test_wolfSSL_CertManagerSetVerify(void)
  2525. {
  2526. EXPECT_DECLS;
  2527. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2528. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2529. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2530. WOLFSSL_CERT_MANAGER* cm = NULL;
  2531. int tmp = myVerifyAction;
  2532. const char* ca_cert = "./certs/ca-cert.pem";
  2533. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2534. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2535. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2536. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2537. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2538. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2539. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2540. #else
  2541. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2542. WOLFSSL_SUCCESS);
  2543. #endif
  2544. /* Use the test CB that always accepts certs */
  2545. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2546. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2547. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2548. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2549. {
  2550. const char* verifyCert = "./certs/server-cert.der";
  2551. /* Use the test CB that always fails certs */
  2552. myVerifyAction = VERIFY_FORCE_FAIL;
  2553. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2554. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2555. }
  2556. #endif
  2557. wolfSSL_CertManagerFree(cm);
  2558. myVerifyAction = tmp;
  2559. #endif
  2560. return EXPECT_RESULT();
  2561. }
  2562. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2563. defined(DEBUG_UNIT_TEST_CERTS)
  2564. /* Used when debugging name constraint tests. Not static to allow use in
  2565. * multiple locations with complex define guards. */
  2566. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2567. {
  2568. BIO* out = BIO_new_file(fileName, "wb");
  2569. if (out != NULL) {
  2570. PEM_write_bio_X509(out, x509);
  2571. BIO_free(out);
  2572. }
  2573. }
  2574. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2575. {
  2576. BIO* out = BIO_new_file(fileName, "wb");
  2577. if (out != NULL) {
  2578. BIO_write(out, der, derSz);
  2579. BIO_free(out);
  2580. }
  2581. }
  2582. #else
  2583. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2584. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2585. #endif
  2586. static int test_wolfSSL_CertManagerNameConstraint(void)
  2587. {
  2588. EXPECT_DECLS;
  2589. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2590. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2591. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2592. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2593. !defined(NO_SHA256)
  2594. WOLFSSL_CERT_MANAGER* cm = NULL;
  2595. WOLFSSL_EVP_PKEY *priv = NULL;
  2596. WOLFSSL_X509_NAME* name = NULL;
  2597. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2598. const char* server_cert = "./certs/test/server-goodcn.pem";
  2599. int i = 0;
  2600. static const byte extNameConsOid[] = {85, 29, 30};
  2601. RsaKey key;
  2602. WC_RNG rng;
  2603. byte *der = NULL;
  2604. int derSz = 0;
  2605. word32 idx = 0;
  2606. byte *pt;
  2607. WOLFSSL_X509 *x509 = NULL;
  2608. WOLFSSL_X509 *ca = NULL;
  2609. wc_InitRng(&rng);
  2610. /* load in CA private key for signing */
  2611. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2612. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2613. sizeof_server_key_der_2048), 0);
  2614. /* get ca certificate then alter it */
  2615. ExpectNotNull(der =
  2616. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2617. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2618. WOLFSSL_FILETYPE_ASN1));
  2619. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2620. if (EXPECT_SUCCESS() && (der != NULL)) {
  2621. XMEMCPY(der, pt, derSz);
  2622. /* find the name constraint extension and alter it */
  2623. pt = der;
  2624. for (i = 0; i < derSz - 3; i++) {
  2625. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2626. pt += 3;
  2627. break;
  2628. }
  2629. pt++;
  2630. }
  2631. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2632. /* go to the length value and set it to 0 */
  2633. while (i < derSz && *pt != 0x81) {
  2634. pt++;
  2635. i++;
  2636. }
  2637. ExpectIntNE(i, derSz); /* did not place to alter */
  2638. pt++;
  2639. *pt = 0x00;
  2640. }
  2641. /* resign the altered certificate */
  2642. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2643. FOURK_BUF, &key, NULL, &rng)), 0);
  2644. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2645. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2646. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2647. wolfSSL_CertManagerFree(cm);
  2648. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2649. wolfSSL_X509_free(x509);
  2650. wc_FreeRsaKey(&key);
  2651. wc_FreeRng(&rng);
  2652. /* add email alt name to satisfy constraint */
  2653. pt = (byte*)server_key_der_2048;
  2654. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2655. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2656. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2657. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2658. WOLFSSL_FILETYPE_ASN1));
  2659. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2660. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2661. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2662. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2663. /* Good cert test with proper alt email name */
  2664. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2665. WOLFSSL_FILETYPE_PEM));
  2666. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2667. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2668. name = NULL;
  2669. ExpectNotNull(name = X509_NAME_new());
  2670. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2671. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2672. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2673. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2674. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2675. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2676. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2677. X509_NAME_free(name);
  2678. name = NULL;
  2679. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2680. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2681. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2682. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2683. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2684. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2685. wolfSSL_X509_free(x509);
  2686. x509 = NULL;
  2687. /* Cert with bad alt name list */
  2688. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2689. WOLFSSL_FILETYPE_PEM));
  2690. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2691. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2692. name = NULL;
  2693. ExpectNotNull(name = X509_NAME_new());
  2694. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2695. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2696. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2697. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2698. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2699. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2700. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2701. X509_NAME_free(name);
  2702. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2703. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2704. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2705. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2706. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2707. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2708. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2709. wolfSSL_CertManagerFree(cm);
  2710. wolfSSL_X509_free(x509);
  2711. wolfSSL_X509_free(ca);
  2712. wolfSSL_EVP_PKEY_free(priv);
  2713. #endif
  2714. return EXPECT_RESULT();
  2715. }
  2716. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2717. {
  2718. EXPECT_DECLS;
  2719. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2720. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2721. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2722. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2723. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2724. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2725. const char* server_cert = "./certs/server-cert.pem";
  2726. WOLFSSL_CERT_MANAGER* cm = NULL;
  2727. WOLFSSL_X509 *x509 = NULL;
  2728. WOLFSSL_X509 *ca = NULL;
  2729. const unsigned char *der = NULL;
  2730. const unsigned char *pt;
  2731. WOLFSSL_EVP_PKEY *priv = NULL;
  2732. WOLFSSL_X509_NAME* name = NULL;
  2733. int derSz = 0;
  2734. /* C=US*/
  2735. char altName[] = {
  2736. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2737. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2738. };
  2739. /* C=ID */
  2740. char altNameFail[] = {
  2741. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2742. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2743. };
  2744. /* C=US ST=California*/
  2745. char altNameExc[] = {
  2746. 0x30, 0x22,
  2747. 0x31, 0x0B,
  2748. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2749. 0x31, 0x13,
  2750. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2751. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2752. };
  2753. /* load in CA private key for signing */
  2754. pt = ca_key_der_2048;
  2755. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2756. sizeof_ca_key_der_2048));
  2757. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2758. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2759. WOLFSSL_FILETYPE_ASN1));
  2760. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2761. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2762. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2763. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2764. WOLFSSL_FILETYPE_PEM));
  2765. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2766. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2767. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2768. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2769. #else
  2770. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2771. #endif
  2772. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2773. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2774. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2775. /* add in matching DIR alt name and resign */
  2776. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2777. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2778. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2779. #else
  2780. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2781. #endif
  2782. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2783. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2784. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2785. wolfSSL_X509_free(x509);
  2786. x509 = NULL;
  2787. /* check verify fail */
  2788. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2789. WOLFSSL_FILETYPE_PEM));
  2790. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2791. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2792. /* add in miss matching DIR alt name and resign */
  2793. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2794. ASN_DIR_TYPE);
  2795. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2796. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2797. #else
  2798. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2799. #endif
  2800. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2801. #ifndef WOLFSSL_NO_ASN_STRICT
  2802. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2803. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2804. #else
  2805. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2806. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2807. #endif
  2808. /* check that it still fails if one bad altname and one good altname is in
  2809. * the certificate */
  2810. wolfSSL_X509_free(x509);
  2811. x509 = NULL;
  2812. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2813. WOLFSSL_FILETYPE_PEM));
  2814. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2815. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2816. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2817. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2818. ASN_DIR_TYPE);
  2819. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2820. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2821. #else
  2822. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2823. #endif
  2824. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2825. #ifndef WOLFSSL_NO_ASN_STRICT
  2826. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2827. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2828. #else
  2829. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2830. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2831. #endif
  2832. /* check it fails with switching position of bad altname */
  2833. wolfSSL_X509_free(x509);
  2834. x509 = NULL;
  2835. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2836. WOLFSSL_FILETYPE_PEM));
  2837. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2838. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2839. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2840. ASN_DIR_TYPE);
  2841. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2842. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2843. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2844. #else
  2845. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2846. #endif
  2847. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2848. #ifndef WOLFSSL_NO_ASN_STRICT
  2849. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2850. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2851. #else
  2852. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2853. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2854. #endif
  2855. wolfSSL_CertManagerFree(cm);
  2856. wolfSSL_X509_free(x509);
  2857. x509 = NULL;
  2858. wolfSSL_X509_free(ca);
  2859. ca = NULL;
  2860. /* now test with excluded name constraint */
  2861. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2862. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2863. WOLFSSL_FILETYPE_ASN1));
  2864. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2865. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2866. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2867. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2868. WOLFSSL_FILETYPE_PEM));
  2869. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2870. ASN_DIR_TYPE);
  2871. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2872. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2873. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2874. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2875. #else
  2876. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2877. #endif
  2878. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2879. #ifndef WOLFSSL_NO_ASN_STRICT
  2880. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2881. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2882. #else
  2883. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2884. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2885. #endif
  2886. wolfSSL_CertManagerFree(cm);
  2887. wolfSSL_X509_free(x509);
  2888. wolfSSL_X509_free(ca);
  2889. wolfSSL_EVP_PKEY_free(priv);
  2890. #endif
  2891. return EXPECT_RESULT();
  2892. }
  2893. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2894. {
  2895. EXPECT_DECLS;
  2896. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2897. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2898. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2899. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2900. !defined(NO_SHA256)
  2901. WOLFSSL_CERT_MANAGER* cm = NULL;
  2902. WOLFSSL_EVP_PKEY *priv = NULL;
  2903. WOLFSSL_X509_NAME* name = NULL;
  2904. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2905. const char* server_cert = "./certs/test/server-goodcn.pem";
  2906. byte *der = NULL;
  2907. int derSz = 0;
  2908. byte *pt;
  2909. WOLFSSL_X509 *x509 = NULL;
  2910. WOLFSSL_X509 *ca = NULL;
  2911. pt = (byte*)server_key_der_2048;
  2912. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2913. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2914. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2915. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2916. WOLFSSL_FILETYPE_ASN1));
  2917. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2918. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2919. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2920. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2921. /* check satisfying .wolfssl.com constraint passes */
  2922. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2923. WOLFSSL_FILETYPE_PEM));
  2924. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2925. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2926. name = NULL;
  2927. ExpectNotNull(name = X509_NAME_new());
  2928. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2929. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2930. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2931. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2932. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2933. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2934. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2935. X509_NAME_free(name);
  2936. name = NULL;
  2937. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2938. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2939. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2940. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2941. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2942. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2943. wolfSSL_X509_free(x509);
  2944. x509 = NULL;
  2945. /* check satisfying .random.com constraint passes */
  2946. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2947. WOLFSSL_FILETYPE_PEM));
  2948. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2949. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2950. name = NULL;
  2951. ExpectNotNull(name = X509_NAME_new());
  2952. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2953. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2954. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2955. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2956. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2957. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2958. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2959. X509_NAME_free(name);
  2960. name = NULL;
  2961. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2962. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2963. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2964. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2965. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2966. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2967. wolfSSL_X509_free(x509);
  2968. x509 = NULL;
  2969. /* check fail case when neither constraint is matched */
  2970. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2971. WOLFSSL_FILETYPE_PEM));
  2972. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2973. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2974. name = NULL;
  2975. ExpectNotNull(name = X509_NAME_new());
  2976. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2977. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2978. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2979. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2980. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2981. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2982. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2983. X509_NAME_free(name);
  2984. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2985. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2986. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2987. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2988. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2989. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2990. wolfSSL_CertManagerFree(cm);
  2991. wolfSSL_X509_free(x509);
  2992. wolfSSL_X509_free(ca);
  2993. wolfSSL_EVP_PKEY_free(priv);
  2994. #endif
  2995. return EXPECT_RESULT();
  2996. }
  2997. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2998. {
  2999. EXPECT_DECLS;
  3000. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  3001. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  3002. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  3003. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  3004. !defined(NO_SHA256)
  3005. WOLFSSL_CERT_MANAGER* cm = NULL;
  3006. WOLFSSL_EVP_PKEY *priv = NULL;
  3007. WOLFSSL_X509_NAME* name = NULL;
  3008. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  3009. const char* server_cert = "./certs/test/server-goodcn.pem";
  3010. byte *der = NULL;
  3011. int derSz;
  3012. byte *pt;
  3013. WOLFSSL_X509 *x509 = NULL;
  3014. WOLFSSL_X509 *ca = NULL;
  3015. pt = (byte*)server_key_der_2048;
  3016. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  3017. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  3018. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3019. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  3020. WOLFSSL_FILETYPE_ASN1));
  3021. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  3022. DEBUG_WRITE_DER(der, derSz, "ca.der");
  3023. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  3024. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3025. /* check satisfying wolfssl.com constraint passes */
  3026. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3027. WOLFSSL_FILETYPE_PEM));
  3028. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3029. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3030. name = NULL;
  3031. ExpectNotNull(name = X509_NAME_new());
  3032. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3033. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3034. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  3035. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  3036. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3037. X509_NAME_free(name);
  3038. name = NULL;
  3039. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  3040. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3041. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  3042. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3043. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3044. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3045. wolfSSL_X509_free(x509);
  3046. x509 = NULL;
  3047. /* check satisfying example.com constraint passes */
  3048. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3049. WOLFSSL_FILETYPE_PEM));
  3050. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3051. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3052. name = NULL;
  3053. ExpectNotNull(name = X509_NAME_new());
  3054. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3055. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3056. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  3057. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  3058. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3059. X509_NAME_free(name);
  3060. name = NULL;
  3061. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  3062. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3063. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  3064. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3065. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3066. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3067. wolfSSL_X509_free(x509);
  3068. x509 = NULL;
  3069. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  3070. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3071. WOLFSSL_FILETYPE_PEM));
  3072. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3073. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3074. name = NULL;
  3075. ExpectNotNull(name = X509_NAME_new());
  3076. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3077. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3078. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  3079. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  3080. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3081. X509_NAME_free(name);
  3082. name = NULL;
  3083. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  3084. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  3085. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  3086. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3087. DEBUG_WRITE_CERT_X509(x509, "good-multiple-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. x509 = NULL;
  3093. /* check fail when one DNS in the list is bad */
  3094. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3095. WOLFSSL_FILETYPE_PEM));
  3096. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3097. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3098. name = NULL;
  3099. ExpectNotNull(name = X509_NAME_new());
  3100. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3101. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3102. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  3103. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  3104. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3105. X509_NAME_free(name);
  3106. name = NULL;
  3107. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  3108. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  3109. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  3110. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3111. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  3112. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3113. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3114. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3115. wolfSSL_X509_free(x509);
  3116. x509 = NULL;
  3117. /* check fail case when neither constraint is matched */
  3118. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3119. WOLFSSL_FILETYPE_PEM));
  3120. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3121. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3122. name = NULL;
  3123. ExpectNotNull(name = X509_NAME_new());
  3124. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3125. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3126. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  3127. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  3128. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3129. X509_NAME_free(name);
  3130. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  3131. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3132. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  3133. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3134. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3135. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3136. wolfSSL_CertManagerFree(cm);
  3137. wolfSSL_X509_free(x509);
  3138. wolfSSL_X509_free(ca);
  3139. wolfSSL_EVP_PKEY_free(priv);
  3140. #endif
  3141. return EXPECT_RESULT();
  3142. }
  3143. static int test_wolfSSL_CertManagerNameConstraint5(void)
  3144. {
  3145. EXPECT_DECLS;
  3146. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  3147. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  3148. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  3149. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  3150. !defined(NO_SHA256)
  3151. WOLFSSL_CERT_MANAGER* cm = NULL;
  3152. WOLFSSL_EVP_PKEY *priv = NULL;
  3153. WOLFSSL_X509_NAME* name = NULL;
  3154. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  3155. const char* server_cert = "./certs/test/server-goodcn.pem";
  3156. byte *der = NULL;
  3157. int derSz;
  3158. byte *pt;
  3159. WOLFSSL_X509 *x509 = NULL;
  3160. WOLFSSL_X509 *ca = NULL;
  3161. pt = (byte*)server_key_der_2048;
  3162. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  3163. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  3164. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3165. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  3166. WOLFSSL_FILETYPE_ASN1));
  3167. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  3168. DEBUG_WRITE_DER(der, derSz, "ca.der");
  3169. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  3170. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3171. /* check satisfying wolfssl.com constraint passes */
  3172. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3173. WOLFSSL_FILETYPE_PEM));
  3174. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3175. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3176. name = NULL;
  3177. ExpectNotNull(name = X509_NAME_new());
  3178. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3179. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3180. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  3181. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  3182. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3183. X509_NAME_free(name);
  3184. name = NULL;
  3185. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  3186. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  3187. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3188. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  3189. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3190. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3191. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3192. wolfSSL_X509_free(x509);
  3193. x509 = NULL;
  3194. /* fail with DNS check because of common name */
  3195. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3196. WOLFSSL_FILETYPE_PEM));
  3197. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3198. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3199. name = NULL;
  3200. ExpectNotNull(name = X509_NAME_new());
  3201. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3202. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3203. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  3204. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  3205. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3206. X509_NAME_free(name);
  3207. name = NULL;
  3208. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3209. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  3210. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3211. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  3212. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3213. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3214. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3215. wolfSSL_X509_free(x509);
  3216. x509 = NULL;
  3217. /* fail on permitted DNS name constraint */
  3218. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3219. WOLFSSL_FILETYPE_PEM));
  3220. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3221. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3222. name = NULL;
  3223. ExpectNotNull(name = X509_NAME_new());
  3224. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3225. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3226. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3227. X509_NAME_free(name);
  3228. name = NULL;
  3229. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  3230. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  3231. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3232. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3233. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  3234. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3235. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3236. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3237. wolfSSL_X509_free(x509);
  3238. x509 = NULL;
  3239. /* fail on permitted email name constraint */
  3240. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3241. WOLFSSL_FILETYPE_PEM));
  3242. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3243. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3244. name = NULL;
  3245. ExpectNotNull(name = X509_NAME_new());
  3246. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3247. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3248. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3249. X509_NAME_free(name);
  3250. name = NULL;
  3251. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3252. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3253. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  3254. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3255. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  3256. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3257. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3258. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3259. wolfSSL_X509_free(x509);
  3260. x509 = NULL;
  3261. /* success with empty email name */
  3262. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3263. WOLFSSL_FILETYPE_PEM));
  3264. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3265. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3266. name = NULL;
  3267. ExpectNotNull(name = X509_NAME_new());
  3268. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3269. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3270. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3271. X509_NAME_free(name);
  3272. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3273. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3274. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  3275. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3276. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3277. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3278. wolfSSL_X509_free(x509);
  3279. wolfSSL_CertManagerFree(cm);
  3280. wolfSSL_X509_free(ca);
  3281. wolfSSL_EVP_PKEY_free(priv);
  3282. #endif
  3283. return EXPECT_RESULT();
  3284. }
  3285. static int test_wolfSSL_CertManagerCRL(void)
  3286. {
  3287. EXPECT_DECLS;
  3288. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  3289. !defined(NO_RSA)
  3290. const char* ca_cert = "./certs/ca-cert.pem";
  3291. const char* crl1 = "./certs/crl/crl.pem";
  3292. const char* crl2 = "./certs/crl/crl2.pem";
  3293. #ifdef WC_RSA_PSS
  3294. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  3295. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  3296. #endif
  3297. const unsigned char crl_buff[] = {
  3298. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  3299. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  3300. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  3301. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  3302. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  3303. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  3304. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  3305. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  3306. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  3307. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  3308. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  3309. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  3310. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  3311. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  3312. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  3313. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  3314. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  3315. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  3316. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  3317. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  3318. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  3319. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  3320. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  3321. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  3322. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  3323. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  3324. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  3325. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  3326. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  3327. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  3328. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  3329. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  3330. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  3331. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  3332. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  3333. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  3334. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  3335. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  3336. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  3337. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  3338. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  3339. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  3340. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  3341. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  3342. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  3343. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  3344. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3345. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3346. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3347. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3348. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3349. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3350. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3351. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3352. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3353. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3354. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3355. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3356. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3357. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3358. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3359. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3360. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3361. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3362. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3363. };
  3364. WOLFSSL_CERT_MANAGER* cm = NULL;
  3365. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3366. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3367. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3368. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3369. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3370. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3371. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3372. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3373. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3374. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3375. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3376. BAD_FUNC_ARG);
  3377. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3378. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3379. BAD_FUNC_ARG);
  3380. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3381. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3382. BAD_FUNC_ARG);
  3383. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3384. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3385. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3386. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3387. #ifdef HAVE_CRL_IO
  3388. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3389. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3390. #endif
  3391. #ifndef NO_FILESYSTEM
  3392. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3393. 0), BAD_FUNC_ARG);
  3394. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3395. 0), BAD_FUNC_ARG);
  3396. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3397. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3398. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3399. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3400. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3401. BAD_FUNC_ARG);
  3402. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3403. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3404. ASN_PARSE_E);
  3405. #endif
  3406. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3407. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3408. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3409. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3410. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3411. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3412. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3413. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3414. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3415. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3416. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3417. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3418. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3419. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3420. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3421. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3422. ExpectIntEQ(WOLFSSL_SUCCESS,
  3423. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3424. ExpectIntEQ(WOLFSSL_SUCCESS,
  3425. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3426. ExpectIntEQ(WOLFSSL_SUCCESS,
  3427. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3428. wolfSSL_CertManagerFreeCRL(cm);
  3429. #ifndef WOLFSSL_CRL_ALLOW_MISSING_CDP
  3430. ExpectIntEQ(WOLFSSL_SUCCESS,
  3431. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3432. ExpectIntEQ(WOLFSSL_SUCCESS,
  3433. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3434. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3435. sizeof_server_cert_der_2048), CRL_MISSING);
  3436. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3437. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3438. #endif /* !WOLFSSL_CRL_ALLOW_MISSING_CDP */
  3439. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3440. WOLFSSL_FILETYPE_ASN1), 1);
  3441. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3442. /* loading should fail without the CA set */
  3443. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3444. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3445. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3446. ExpectIntEQ(WOLFSSL_SUCCESS,
  3447. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3448. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3449. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3450. #endif
  3451. wolfSSL_CertManagerFree(cm);
  3452. #endif
  3453. return EXPECT_RESULT();
  3454. }
  3455. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3456. {
  3457. EXPECT_DECLS;
  3458. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3459. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3460. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3461. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3462. defined(HAVE_LIGHTY)
  3463. WOLFSSL_CERT_MANAGER* cm = NULL;
  3464. /* Raw OCSP response bytes captured using the following setup:
  3465. * - Run responder with
  3466. * openssl ocsp -port 9999 -ndays 9999
  3467. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3468. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3469. * -rkey certs/ocsp/ocsp-responder-key.pem
  3470. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3471. * - Run client with
  3472. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3473. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3474. * -cert certs/ocsp/server1-cert.pem
  3475. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3476. * - Select the response packet in Wireshark, and export it using
  3477. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3478. * packets only". After importing into the editor, remove the initial
  3479. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3480. */
  3481. static const byte response[] = {
  3482. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3483. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3484. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3485. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3486. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3487. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3488. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3489. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3490. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3491. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3492. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3493. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3494. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3495. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3496. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3497. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3498. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3499. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3500. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3501. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3502. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3503. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3504. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3505. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3506. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3507. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3508. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3509. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3510. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3511. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3512. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3513. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3514. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3515. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3516. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3517. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3518. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3519. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3520. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3521. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3522. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3523. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3524. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3525. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3526. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3527. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3528. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3529. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3530. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3531. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3532. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3533. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3534. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3535. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3536. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3537. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3538. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3539. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3540. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3541. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3542. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3543. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3544. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3545. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3546. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3547. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3548. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3549. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3550. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3551. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3552. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3553. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3554. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3555. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3556. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3557. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3558. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3559. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3560. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3561. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3562. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3563. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3564. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3565. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3566. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3567. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3568. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3569. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3570. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3571. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3572. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3573. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3574. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3575. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3576. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3577. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3578. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3579. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3580. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3581. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3582. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3583. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3584. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3585. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3586. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3587. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3588. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3589. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3590. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3591. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3592. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3593. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3594. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3595. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3596. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3597. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3598. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3599. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3600. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3601. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3602. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3603. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3604. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3605. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3606. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3607. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3608. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3609. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3610. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3611. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3612. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3613. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3614. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3615. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3616. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3617. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3618. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3619. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3620. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3621. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3622. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3623. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3624. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3625. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3626. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3627. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3628. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3629. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3630. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3631. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3632. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3633. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3634. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3635. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3636. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3637. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3638. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3639. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3640. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3641. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3642. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3643. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3644. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3645. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3646. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3647. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3648. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3649. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3650. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3651. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3652. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3653. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3654. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3655. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3656. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3657. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3658. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3659. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3660. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3661. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3662. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3663. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3664. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3665. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3666. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3667. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3668. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3669. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3670. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3671. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3672. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3673. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3674. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3675. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3676. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3677. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3678. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3679. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3680. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3681. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3682. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3683. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3684. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3685. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3686. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3687. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3688. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3689. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3690. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3691. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3692. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3693. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3694. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3695. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3696. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3697. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3698. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3699. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3700. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3701. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3702. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3703. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3704. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3705. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3706. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3707. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3708. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3709. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3710. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3711. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3712. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3713. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3714. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3715. };
  3716. OcspEntry entry[1];
  3717. CertStatus status[1];
  3718. OcspRequest* request = NULL;
  3719. #ifndef NO_FILESYSTEM
  3720. const char* ca_cert = "./certs/ca-cert.pem";
  3721. #endif
  3722. byte serial[] = {0x05};
  3723. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3724. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3725. XMEMSET(entry, 0, sizeof(OcspEntry));
  3726. XMEMSET(status, 0, sizeof(CertStatus));
  3727. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3728. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3729. DYNAMIC_TYPE_OCSP_REQUEST));
  3730. if ((request != NULL) && (request->serial != NULL)) {
  3731. request->serialSz = sizeof(serial);
  3732. XMEMCPY(request->serial, serial, sizeof(serial));
  3733. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3734. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3735. }
  3736. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3737. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3738. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3739. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3740. /* Response should be valid. */
  3741. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3742. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3743. /* Flip a byte in the request serial number, response should be invalid
  3744. * now. */
  3745. if ((request != NULL) && (request->serial != NULL))
  3746. request->serial[0] ^= request->serial[0];
  3747. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3748. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3749. #ifndef NO_FILESYSTEM
  3750. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3751. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3752. ExpectIntEQ(WOLFSSL_SUCCESS,
  3753. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3754. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3755. sizeof(server_cert_der_2048)), 1);
  3756. #endif
  3757. wolfSSL_OCSP_REQUEST_free(request);
  3758. wolfSSL_CertManagerFree(cm);
  3759. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3760. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3761. #endif /* HAVE_OCSP */
  3762. return EXPECT_RESULT();
  3763. }
  3764. static int test_wolfSSL_CheckOCSPResponse(void)
  3765. {
  3766. EXPECT_DECLS;
  3767. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3768. defined(OPENSSL_ALL)
  3769. const char* responseFile = "./certs/ocsp/test-response.der";
  3770. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3771. const char* responseNoInternFile =
  3772. "./certs/ocsp/test-response-nointern.der";
  3773. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3774. OcspResponse* res = NULL;
  3775. byte data[4096];
  3776. const unsigned char* pt;
  3777. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3778. * gcc sanitizer with --enable-heapmath.
  3779. */
  3780. XFILE f = XBADFILE;
  3781. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3782. WOLFSSL_X509_STORE* st = NULL;
  3783. WOLFSSL_X509* issuer = NULL;
  3784. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3785. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3786. if (f != XBADFILE) {
  3787. XFCLOSE(f);
  3788. f = XBADFILE;
  3789. }
  3790. pt = data;
  3791. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3792. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3793. SSL_FILETYPE_PEM));
  3794. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3795. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3796. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3797. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3798. wolfSSL_OCSP_BASICRESP_free(bs);
  3799. bs = NULL;
  3800. wolfSSL_OCSP_RESPONSE_free(res);
  3801. res = NULL;
  3802. wolfSSL_X509_STORE_free(st);
  3803. st = NULL;
  3804. wolfSSL_X509_free(issuer);
  3805. issuer = NULL;
  3806. /* check loading a response with optional certs */
  3807. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3808. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3809. if (f != XBADFILE)
  3810. XFCLOSE(f);
  3811. f = XBADFILE;
  3812. pt = data;
  3813. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3814. wolfSSL_OCSP_RESPONSE_free(res);
  3815. res = NULL;
  3816. /* check loading a response with multiple certs */
  3817. {
  3818. WOLFSSL_CERT_MANAGER* cm = NULL;
  3819. OcspEntry *entry = NULL;
  3820. CertStatus* status = NULL;
  3821. OcspRequest* request = NULL;
  3822. byte serial1[] = {0x01};
  3823. byte serial[] = {0x02};
  3824. byte issuerHash[] = {
  3825. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3826. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3827. 0x37, 0x15, 0x8A, 0x88
  3828. };
  3829. byte issuerKeyHash[] = {
  3830. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3831. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3832. 0x7E, 0x72, 0x15, 0x21
  3833. };
  3834. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3835. DYNAMIC_TYPE_OPENSSL));
  3836. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3837. DYNAMIC_TYPE_OPENSSL));
  3838. if (entry != NULL)
  3839. XMEMSET(entry, 0, sizeof(OcspEntry));
  3840. if (status != NULL)
  3841. XMEMSET(status, 0, sizeof(CertStatus));
  3842. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3843. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3844. DYNAMIC_TYPE_OCSP_REQUEST));
  3845. if (request != NULL && request->serial != NULL) {
  3846. request->serialSz = sizeof(serial);
  3847. XMEMCPY(request->serial, serial, sizeof(serial));
  3848. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3849. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3850. sizeof(issuerKeyHash));
  3851. }
  3852. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3853. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3854. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3855. WOLFSSL_SUCCESS);
  3856. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3857. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3858. if (f != XBADFILE)
  3859. XFCLOSE(f);
  3860. f = XBADFILE;
  3861. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3862. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3863. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3864. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3865. ExpectNotNull(entry->status);
  3866. if (request != NULL && request->serial != NULL)
  3867. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3868. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3869. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3870. /* store both status's in the entry to check that "next" is not
  3871. * overwritten */
  3872. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3873. status->next = entry->status;
  3874. entry->status = status;
  3875. }
  3876. if (request != NULL && request->serial != NULL)
  3877. XMEMCPY(request->serial, serial, sizeof(serial));
  3878. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3879. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3880. ExpectNotNull(entry->status->next);
  3881. /* compare the status found */
  3882. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3883. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3884. status->serialSz), 0);
  3885. if (status != NULL && entry != NULL && entry->status != status) {
  3886. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3887. }
  3888. wolfSSL_OCSP_CERTID_free(entry);
  3889. wolfSSL_OCSP_REQUEST_free(request);
  3890. wolfSSL_CertManagerFree(cm);
  3891. }
  3892. #if defined(WC_RSA_PSS)
  3893. {
  3894. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3895. /* check loading a response with RSA-PSS signature */
  3896. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3897. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3898. if (f != XBADFILE)
  3899. XFCLOSE(f);
  3900. pt = data;
  3901. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3902. /* try to verify the response */
  3903. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3904. SSL_FILETYPE_PEM));
  3905. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3906. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3907. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3908. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3909. WOLFSSL_SUCCESS);
  3910. wolfSSL_OCSP_BASICRESP_free(bs);
  3911. wolfSSL_OCSP_RESPONSE_free(res);
  3912. wolfSSL_X509_STORE_free(st);
  3913. wolfSSL_X509_free(issuer);
  3914. }
  3915. #endif
  3916. #endif /* HAVE_OCSP */
  3917. return EXPECT_RESULT();
  3918. }
  3919. static int test_wolfSSL_FPKI(void)
  3920. {
  3921. EXPECT_DECLS;
  3922. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3923. XFILE f = XBADFILE;
  3924. const char* fpkiCert = "./certs/fpki-cert.der";
  3925. DecodedCert cert;
  3926. byte buf[4096];
  3927. byte* uuid = NULL;
  3928. byte* fascn = NULL;
  3929. word32 fascnSz;
  3930. word32 uuidSz;
  3931. int bytes = 0;
  3932. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3933. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3934. if (f != XBADFILE)
  3935. XFCLOSE(f);
  3936. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3937. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3938. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3939. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3940. DYNAMIC_TYPE_TMP_BUFFER));
  3941. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3942. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3943. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3944. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3945. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3946. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3947. wc_FreeDecodedCert(&cert);
  3948. #endif
  3949. return EXPECT_RESULT();
  3950. }
  3951. /* use RID in confuncture with other names to test parsing of unknown other
  3952. * names */
  3953. static int test_wolfSSL_OtherName(void)
  3954. {
  3955. EXPECT_DECLS;
  3956. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3957. XFILE f = XBADFILE;
  3958. const char* ridCert = "./certs/rid-cert.der";
  3959. DecodedCert cert;
  3960. byte buf[4096];
  3961. int bytes = 0;
  3962. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3963. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3964. if (f != XBADFILE)
  3965. XFCLOSE(f);
  3966. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3967. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3968. wc_FreeDecodedCert(&cert);
  3969. #endif
  3970. return EXPECT_RESULT();
  3971. }
  3972. static int test_wolfSSL_CertRsaPss(void)
  3973. {
  3974. EXPECT_DECLS;
  3975. /* FIPS v2 and below don't support long salts. */
  3976. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3977. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3978. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3979. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3980. XFILE f = XBADFILE;
  3981. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3982. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3983. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3984. RSA_MAX_SIZE >= 3072
  3985. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3986. #endif
  3987. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3988. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3989. #endif
  3990. DecodedCert cert;
  3991. byte buf[4096];
  3992. int bytes = 0;
  3993. WOLFSSL_CERT_MANAGER* cm = NULL;
  3994. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3995. ExpectIntEQ(WOLFSSL_SUCCESS,
  3996. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3997. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3998. ExpectIntEQ(WOLFSSL_SUCCESS,
  3999. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  4000. #endif
  4001. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  4002. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  4003. if (f != XBADFILE) {
  4004. XFCLOSE(f);
  4005. f = XBADFILE;
  4006. }
  4007. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  4008. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  4009. wc_FreeDecodedCert(&cert);
  4010. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  4011. RSA_MAX_SIZE >= 3072
  4012. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  4013. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  4014. if (f != XBADFILE)
  4015. XFCLOSE(f);
  4016. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  4017. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  4018. wc_FreeDecodedCert(&cert);
  4019. #endif
  4020. wolfSSL_CertManagerFree(cm);
  4021. #endif
  4022. return EXPECT_RESULT();
  4023. }
  4024. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  4025. {
  4026. EXPECT_DECLS;
  4027. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4028. !defined(NO_WOLFSSL_CLIENT)
  4029. WOLFSSL_CTX* ctx = NULL;
  4030. const char* ca_cert = "./certs/ca-cert.pem";
  4031. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  4032. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4033. /* test good CA */
  4034. ExpectTrue(WOLFSSL_SUCCESS ==
  4035. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  4036. WOLFSSL_LOAD_FLAG_NONE));
  4037. /* test expired CA */
  4038. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  4039. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  4040. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  4041. #else
  4042. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  4043. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  4044. #endif
  4045. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  4046. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  4047. wolfSSL_CTX_free(ctx);
  4048. #endif
  4049. return EXPECT_RESULT();
  4050. }
  4051. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  4052. {
  4053. EXPECT_DECLS;
  4054. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  4055. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4056. WOLFSSL_CTX* ctx;
  4057. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  4058. byte ca_expired_cert[TWOK_BUF];
  4059. word32 sizeof_ca_expired_cert = 0;
  4060. XFILE fp = XBADFILE;
  4061. #ifndef NO_WOLFSSL_CLIENT
  4062. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4063. #else
  4064. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  4065. #endif
  4066. ExpectNotNull(ctx);
  4067. #if defined(USE_CERT_BUFFERS_2048)
  4068. /* test good CA */
  4069. ExpectTrue(WOLFSSL_SUCCESS ==
  4070. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  4071. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  4072. WOLFSSL_LOAD_FLAG_NONE));
  4073. #endif
  4074. /* load expired CA */
  4075. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  4076. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  4077. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  4078. sizeof(ca_expired_cert), fp), 0);
  4079. if (fp != XBADFILE)
  4080. XFCLOSE(fp);
  4081. /* test expired CA failure */
  4082. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  4083. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  4084. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  4085. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  4086. #else
  4087. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  4088. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  4089. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  4090. #endif
  4091. /* test expired CA success */
  4092. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  4093. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  4094. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  4095. /* Fail when ctx is NULL. */
  4096. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(NULL, ca_expired_cert,
  4097. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  4098. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), BAD_FUNC_ARG);
  4099. /* Load as modified cert - bad initial length. */
  4100. ca_expired_cert[2] = 0x7f;
  4101. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  4102. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 1,
  4103. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), ASN_PARSE_E);
  4104. wolfSSL_CTX_free(ctx);
  4105. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4106. #endif
  4107. return EXPECT_RESULT();
  4108. }
  4109. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  4110. {
  4111. EXPECT_DECLS;
  4112. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  4113. defined(USE_CERT_BUFFERS_2048) && (WOLFSSL_MIN_RSA_BITS <= 1024) && \
  4114. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4115. WOLFSSL_CTX* ctx = NULL;
  4116. #ifndef NO_WOLFSSL_CLIENT
  4117. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4118. #else
  4119. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4120. #endif
  4121. /* Public key 140 bytes??? */
  4122. ExpectIntEQ(wolfSSL_CTX_load_verify_chain_buffer_format(ctx,
  4123. ca_cert_chain_der, sizeof_ca_cert_chain_der, WOLFSSL_FILETYPE_ASN1),
  4124. WOLFSSL_SUCCESS);
  4125. wolfSSL_CTX_free(ctx);
  4126. #endif
  4127. return EXPECT_RESULT();
  4128. }
  4129. static int test_wolfSSL_CTX_add1_chain_cert(void)
  4130. {
  4131. EXPECT_DECLS;
  4132. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  4133. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  4134. WOLFSSL_CTX* ctx;
  4135. WOLFSSL* ssl = NULL;
  4136. const char *certChain[] = {
  4137. "./certs/intermediate/client-int-cert.pem",
  4138. "./certs/intermediate/ca-int2-cert.pem",
  4139. "./certs/intermediate/ca-int-cert.pem",
  4140. "./certs/ca-cert.pem",
  4141. NULL
  4142. };
  4143. const char** cert;
  4144. WOLFSSL_X509* x509 = NULL;
  4145. WOLF_STACK_OF(X509)* chain = NULL;
  4146. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4147. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4148. ExpectNotNull(x509 = wolfSSL_X509_new());
  4149. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 0);
  4150. ExpectIntEQ(SSL_CTX_add0_chain_cert(ctx, x509), 0);
  4151. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 0);
  4152. ExpectIntEQ(SSL_add0_chain_cert(ssl, x509), 0);
  4153. wolfSSL_X509_free(x509);
  4154. x509 = NULL;
  4155. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  4156. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  4157. WOLFSSL_FILETYPE_PEM));
  4158. /* Do negative tests once */
  4159. if (cert == certChain) {
  4160. /* Negative tests. */
  4161. ExpectIntEQ(SSL_CTX_add1_chain_cert(NULL, NULL), 0);
  4162. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, NULL), 0);
  4163. ExpectIntEQ(SSL_CTX_add1_chain_cert(NULL, x509), 0);
  4164. ExpectIntEQ(SSL_CTX_add0_chain_cert(NULL, NULL), 0);
  4165. ExpectIntEQ(SSL_CTX_add0_chain_cert(ctx, NULL), 0);
  4166. ExpectIntEQ(SSL_CTX_add0_chain_cert(NULL, x509), 0);
  4167. }
  4168. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  4169. X509_free(x509);
  4170. x509 = NULL;
  4171. }
  4172. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  4173. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  4174. WOLFSSL_FILETYPE_PEM));
  4175. /* Do negative tests once */
  4176. if (cert == certChain) {
  4177. /* Negative tests. */
  4178. ExpectIntEQ(SSL_add1_chain_cert(NULL, NULL), 0);
  4179. ExpectIntEQ(SSL_add1_chain_cert(ssl, NULL), 0);
  4180. ExpectIntEQ(SSL_add1_chain_cert(NULL, x509), 0);
  4181. ExpectIntEQ(SSL_add0_chain_cert(NULL, NULL), 0);
  4182. ExpectIntEQ(SSL_add0_chain_cert(ssl, NULL), 0);
  4183. ExpectIntEQ(SSL_add0_chain_cert(NULL, x509), 0);
  4184. }
  4185. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  4186. X509_free(x509);
  4187. x509 = NULL;
  4188. }
  4189. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  4190. ExpectIntEQ(sk_X509_num(chain), 3);
  4191. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  4192. ExpectIntEQ(sk_X509_num(chain), 3);
  4193. SSL_free(ssl);
  4194. SSL_CTX_free(ctx);
  4195. #endif
  4196. return EXPECT_RESULT();
  4197. }
  4198. static int test_wolfSSL_CTX_use_certificate_chain_buffer_format(void)
  4199. {
  4200. EXPECT_DECLS;
  4201. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4202. !defined(NO_WOLFSSL_CLIENT) && defined(USE_CERT_BUFFERS_2048)
  4203. WOLFSSL_CTX* ctx = NULL;
  4204. WOLFSSL* ssl = NULL;
  4205. const char* cert = "./certs/server-cert.pem";
  4206. unsigned char* buf = NULL;
  4207. size_t len;
  4208. ExpectIntEQ(load_file(cert, &buf, &len), 0);
  4209. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4210. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4211. /* Invalid parameters. */
  4212. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_buffer_format(NULL,
  4213. NULL, 0, WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  4214. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_buffer_format(ctx,
  4215. NULL, 0, WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  4216. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_buffer_format(NULL,
  4217. server_cert_der_2048, sizeof_server_cert_der_2048,
  4218. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  4219. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_buffer(NULL, NULL, 0),
  4220. BAD_FUNC_ARG);
  4221. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_buffer(ctx, NULL, 0),
  4222. ASN_NO_PEM_HEADER);
  4223. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_buffer(NULL, buf, (long)len),
  4224. BAD_FUNC_ARG);
  4225. ExpectIntEQ(wolfSSL_use_certificate_chain_buffer(NULL, NULL, 0),
  4226. BAD_FUNC_ARG);
  4227. ExpectIntEQ(wolfSSL_use_certificate_chain_buffer(ssl, NULL, 0),
  4228. ASN_NO_PEM_HEADER);
  4229. ExpectIntEQ(wolfSSL_use_certificate_chain_buffer(NULL, buf, (long)len),
  4230. BAD_FUNC_ARG);
  4231. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_buffer_format(ctx,
  4232. server_cert_der_2048, sizeof_server_cert_der_2048,
  4233. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4234. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, buf,
  4235. (long)len, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4236. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_buffer(ctx, buf, (long)len),
  4237. WOLFSSL_SUCCESS);
  4238. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_buffer(ctx,
  4239. server_cert_der_2048, sizeof_server_cert_der_2048), ASN_NO_PEM_HEADER);
  4240. ExpectIntEQ(wolfSSL_use_certificate_chain_buffer(ssl, buf, (long)len),
  4241. WOLFSSL_SUCCESS);
  4242. ExpectIntEQ(wolfSSL_use_certificate_chain_buffer(ssl, server_cert_der_2048,
  4243. sizeof_server_cert_der_2048), ASN_NO_PEM_HEADER);
  4244. wolfSSL_free(ssl);
  4245. wolfSSL_CTX_free(ctx);
  4246. if (buf != NULL) {
  4247. free(buf);
  4248. }
  4249. #endif
  4250. return EXPECT_RESULT();
  4251. }
  4252. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  4253. {
  4254. EXPECT_DECLS;
  4255. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4256. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4257. const char* server_chain_der = "./certs/server-cert-chain.der";
  4258. const char* client_single_pem = "./certs/client-cert.pem";
  4259. WOLFSSL_CTX* ctx = NULL;
  4260. (void)server_chain_der;
  4261. (void)client_single_pem;
  4262. (void)ctx;
  4263. #ifndef NO_WOLFSSL_CLIENT
  4264. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4265. #else
  4266. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4267. #endif
  4268. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  4269. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4270. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  4271. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4272. wolfSSL_CTX_free(ctx);
  4273. #endif
  4274. return EXPECT_RESULT();
  4275. }
  4276. static int test_wolfSSL_use_certificate_chain_file(void)
  4277. {
  4278. EXPECT_DECLS;
  4279. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4280. !defined(NO_WOLFSSL_CLIENT)
  4281. const char* server_chain_der = "./certs/server-cert-chain.der";
  4282. const char* client_single_pem = "./certs/client-cert.pem";
  4283. WOLFSSL_CTX* ctx = NULL;
  4284. WOLFSSL* ssl = NULL;
  4285. (void)server_chain_der;
  4286. (void)client_single_pem;
  4287. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4288. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4289. /* Invalid parameters. */
  4290. ExpectIntEQ(wolfSSL_use_certificate_chain_file_format(NULL, NULL,
  4291. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  4292. ExpectIntEQ(wolfSSL_use_certificate_chain_file_format(ssl, NULL,
  4293. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4294. ExpectIntEQ(wolfSSL_use_certificate_chain_file_format(NULL,
  4295. server_chain_der, WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  4296. ExpectIntEQ(wolfSSL_use_certificate_chain_file(NULL, NULL), BAD_FUNC_ARG);
  4297. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ssl, NULL), WOLFSSL_FAILURE);
  4298. ExpectIntEQ(wolfSSL_use_certificate_chain_file(NULL, client_single_pem),
  4299. BAD_FUNC_ARG);
  4300. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ssl, server_chain_der),
  4301. WOLFSSL_FAILURE);
  4302. ExpectIntEQ(wolfSSL_use_certificate_chain_file_format(ssl,
  4303. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4304. ExpectIntEQ(wolfSSL_use_certificate_chain_file_format(ssl,
  4305. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4306. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ssl, client_single_pem),
  4307. WOLFSSL_SUCCESS);
  4308. wolfSSL_free(ssl);
  4309. wolfSSL_CTX_free(ctx);
  4310. #endif
  4311. return EXPECT_RESULT();
  4312. }
  4313. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  4314. {
  4315. EXPECT_DECLS;
  4316. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4317. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4318. WOLFSSL_CTX *ctx = NULL;
  4319. #if defined(WOLFSSL_WPAS) && !defined(NO_DSA)
  4320. const char* dsaParamFile = "./certs/dsaparams.pem";
  4321. #endif
  4322. (void)ctx;
  4323. #ifndef NO_WOLFSSL_CLIENT
  4324. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4325. #else
  4326. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4327. #endif
  4328. /* invalid context */
  4329. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  4330. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4331. /* invalid dhParamFile file */
  4332. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  4333. NULL, WOLFSSL_FILETYPE_PEM));
  4334. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  4335. bogusFile, WOLFSSL_FILETYPE_PEM));
  4336. /* success */
  4337. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  4338. WOLFSSL_FILETYPE_PEM));
  4339. #if defined(WOLFSSL_WPAS) && !defined(NO_DSA)
  4340. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dsaParamFile,
  4341. WOLFSSL_FILETYPE_PEM));
  4342. #endif
  4343. wolfSSL_CTX_free(ctx);
  4344. #endif
  4345. return EXPECT_RESULT();
  4346. }
  4347. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  4348. {
  4349. EXPECT_DECLS;
  4350. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4351. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4352. WOLFSSL_CTX *ctx = NULL;
  4353. #ifndef NO_WOLFSSL_CLIENT
  4354. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4355. #else
  4356. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4357. #endif
  4358. /* invalid context */
  4359. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  4360. dh_key_der_2048, sizeof_dh_key_der_2048,
  4361. WOLFSSL_FILETYPE_ASN1));
  4362. /* invalid dhParamFile file */
  4363. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  4364. 0, WOLFSSL_FILETYPE_ASN1));
  4365. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, NULL,
  4366. 0, WOLFSSL_FILETYPE_ASN1));
  4367. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4368. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  4369. WOLFSSL_FILETYPE_ASN1));
  4370. /* invalid file format */
  4371. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4372. dh_key_der_2048, sizeof_dh_key_der_2048, -1));
  4373. /* success */
  4374. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4375. dh_key_der_2048, sizeof_dh_key_der_2048,
  4376. WOLFSSL_FILETYPE_ASN1));
  4377. wolfSSL_CTX_free(ctx);
  4378. #endif
  4379. return EXPECT_RESULT();
  4380. }
  4381. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  4382. {
  4383. EXPECT_DECLS;
  4384. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4385. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4386. WOLFSSL_CTX *ctx;
  4387. (void)ctx;
  4388. #ifndef NO_WOLFSSL_CLIENT
  4389. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4390. #else
  4391. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  4392. #endif
  4393. ExpectNotNull(ctx);
  4394. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4395. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  4396. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4397. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  4398. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4399. dh_key_der_2048, sizeof_dh_key_der_2048,
  4400. WOLFSSL_FILETYPE_ASN1));
  4401. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4402. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4403. dh_key_der_2048, sizeof_dh_key_der_2048,
  4404. WOLFSSL_FILETYPE_ASN1));
  4405. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  4406. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4407. dh_key_der_2048, sizeof_dh_key_der_2048,
  4408. WOLFSSL_FILETYPE_ASN1));
  4409. wolfSSL_CTX_free(ctx);
  4410. #endif
  4411. return EXPECT_RESULT();
  4412. }
  4413. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  4414. {
  4415. EXPECT_DECLS;
  4416. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  4417. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4418. WOLFSSL_CTX* ctx = NULL;
  4419. const char* derCert = "./certs/server-cert.der";
  4420. const char* nullPath = NULL;
  4421. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  4422. const char* emptyPath = "";
  4423. /* der load Case 1 ctx NULL */
  4424. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4425. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4426. #ifndef NO_WOLFSSL_CLIENT
  4427. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4428. #else
  4429. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4430. #endif
  4431. /* Case 2 filePath NULL */
  4432. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  4433. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4434. /* Case 3 invalid format */
  4435. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4436. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  4437. /* Case 4 filePath not valid */
  4438. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  4439. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4440. /* Case 5 filePath empty */
  4441. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  4442. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4443. #ifndef NO_RSA
  4444. /* Case 6 success case */
  4445. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4446. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4447. #endif
  4448. wolfSSL_CTX_free(ctx);
  4449. #endif
  4450. return EXPECT_RESULT();
  4451. }
  4452. static int test_wolfSSL_CTX_enable_disable(void)
  4453. {
  4454. EXPECT_DECLS;
  4455. #ifndef NO_CERTS
  4456. WOLFSSL_CTX* ctx = NULL;
  4457. #ifdef HAVE_CRL
  4458. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  4459. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  4460. #endif
  4461. #ifdef HAVE_OCSP
  4462. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  4463. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  4464. #endif
  4465. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4466. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4467. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  4468. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  4469. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4470. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4471. #endif
  4472. #ifndef NO_WOLFSSL_CLIENT
  4473. #ifdef HAVE_EXTENDED_MASTER
  4474. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  4475. #endif
  4476. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4477. #ifdef HAVE_EXTENDED_MASTER
  4478. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  4479. #endif
  4480. #elif !defined(NO_WOLFSSL_SERVER)
  4481. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4482. #endif
  4483. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4484. #ifdef HAVE_CRL
  4485. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  4486. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4487. #endif
  4488. #ifdef HAVE_OCSP
  4489. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4490. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4491. WOLFSSL_SUCCESS);
  4492. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4493. WOLFSSL_SUCCESS);
  4494. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4495. WOLFSSL_SUCCESS);
  4496. #endif
  4497. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4498. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4499. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4500. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4501. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4502. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4503. #endif
  4504. wolfSSL_CTX_free(ctx);
  4505. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4506. #endif /* NO_CERTS */
  4507. return EXPECT_RESULT();
  4508. }
  4509. static int test_wolfSSL_CTX_ticket_API(void)
  4510. {
  4511. EXPECT_DECLS;
  4512. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4513. WOLFSSL_CTX* ctx = NULL;
  4514. void *userCtx = (void*)"this is my ctx";
  4515. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4516. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4517. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4518. wolfSSL_CTX_free(ctx);
  4519. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4520. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4521. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4522. return EXPECT_RESULT();
  4523. }
  4524. static int test_wolfSSL_set_minmax_proto_version(void)
  4525. {
  4526. EXPECT_DECLS;
  4527. #ifdef OPENSSL_EXTRA
  4528. WOLFSSL_CTX *ctx = NULL;
  4529. WOLFSSL *ssl = NULL;
  4530. (void)ssl;
  4531. #ifndef NO_WOLFSSL_CLIENT
  4532. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4533. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4534. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4535. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4536. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4537. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4538. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4539. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4540. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4541. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4542. wolfSSL_free(ssl);
  4543. wolfSSL_CTX_free(ctx);
  4544. ctx = NULL;
  4545. #endif
  4546. #ifndef NO_WOLFSSL_SERVER
  4547. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4548. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4549. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4550. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4551. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4552. wolfSSL_CTX_free(ctx);
  4553. #endif
  4554. #endif
  4555. return EXPECT_RESULT();
  4556. }
  4557. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4558. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4559. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4560. {
  4561. EXPECT_DECLS;
  4562. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4563. return EXPECT_RESULT();
  4564. }
  4565. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4566. {
  4567. EXPECT_DECLS;
  4568. /* Set TLS 1.2 */
  4569. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4570. WOLFSSL_SUCCESS);
  4571. return EXPECT_RESULT();
  4572. }
  4573. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4574. * what was set at ctx creation. */
  4575. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4576. {
  4577. EXPECT_DECLS;
  4578. test_ssl_cbf client_cbs;
  4579. test_ssl_cbf server_cbs;
  4580. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4581. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4582. client_cbs.method = wolfTLS_client_method;
  4583. server_cbs.method = wolfTLS_server_method;
  4584. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4585. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4586. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4587. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4588. &server_cbs, NULL), TEST_SUCCESS);
  4589. return EXPECT_RESULT();
  4590. }
  4591. #else
  4592. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4593. {
  4594. return TEST_SKIPPED;
  4595. }
  4596. #endif
  4597. /*----------------------------------------------------------------------------*
  4598. | SSL
  4599. *----------------------------------------------------------------------------*/
  4600. static int test_server_wolfSSL_new(void)
  4601. {
  4602. EXPECT_DECLS;
  4603. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4604. !defined(NO_WOLFSSL_SERVER)
  4605. WOLFSSL_CTX *ctx = NULL;
  4606. WOLFSSL_CTX *ctx_nocert = NULL;
  4607. WOLFSSL *ssl = NULL;
  4608. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4609. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4610. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4611. WOLFSSL_FILETYPE_PEM));
  4612. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4613. WOLFSSL_FILETYPE_PEM));
  4614. /* invalid context */
  4615. ExpectNull(ssl = wolfSSL_new(NULL));
  4616. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4617. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4618. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4619. #endif
  4620. /* success */
  4621. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4622. wolfSSL_free(ssl);
  4623. wolfSSL_CTX_free(ctx);
  4624. wolfSSL_CTX_free(ctx_nocert);
  4625. #endif
  4626. return EXPECT_RESULT();
  4627. }
  4628. static int test_client_wolfSSL_new(void)
  4629. {
  4630. EXPECT_DECLS;
  4631. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4632. !defined(NO_WOLFSSL_CLIENT)
  4633. WOLFSSL_CTX *ctx = NULL;
  4634. WOLFSSL_CTX *ctx_nocert = NULL;
  4635. WOLFSSL *ssl = NULL;
  4636. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4637. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4638. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4639. /* invalid context */
  4640. ExpectNull(ssl = wolfSSL_new(NULL));
  4641. /* success */
  4642. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4643. wolfSSL_free(ssl);
  4644. ssl = NULL;
  4645. /* success */
  4646. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4647. wolfSSL_free(ssl);
  4648. wolfSSL_CTX_free(ctx);
  4649. wolfSSL_CTX_free(ctx_nocert);
  4650. #endif
  4651. return EXPECT_RESULT();
  4652. }
  4653. static int test_wolfSSL_SetTmpDH_file(void)
  4654. {
  4655. EXPECT_DECLS;
  4656. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4657. !defined(NO_WOLFSSL_SERVER)
  4658. WOLFSSL_CTX *ctx = NULL;
  4659. WOLFSSL *ssl = NULL;
  4660. const char* dhX942ParamFile = "./certs/x942dh2048.pem";
  4661. #if defined(WOLFSSL_WPAS) && !defined(NO_DSA)
  4662. const char* dsaParamFile = "./certs/dsaparams.pem";
  4663. #endif
  4664. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4665. #ifndef NO_RSA
  4666. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4667. WOLFSSL_FILETYPE_PEM));
  4668. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4669. WOLFSSL_FILETYPE_PEM));
  4670. #elif defined(HAVE_ECC)
  4671. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4672. WOLFSSL_FILETYPE_PEM));
  4673. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4674. WOLFSSL_FILETYPE_PEM));
  4675. #elif defined(HAVE_ED25519)
  4676. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4677. WOLFSSL_FILETYPE_PEM));
  4678. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4679. WOLFSSL_FILETYPE_PEM));
  4680. #elif defined(HAVE_ED448)
  4681. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4682. WOLFSSL_FILETYPE_PEM));
  4683. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4684. WOLFSSL_FILETYPE_PEM));
  4685. #endif
  4686. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4687. /* invalid ssl */
  4688. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4689. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4690. /* invalid dhParamFile file */
  4691. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4692. NULL, WOLFSSL_FILETYPE_PEM));
  4693. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4694. bogusFile, WOLFSSL_FILETYPE_PEM));
  4695. /* success */
  4696. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4697. WOLFSSL_FILETYPE_PEM));
  4698. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhX942ParamFile,
  4699. WOLFSSL_FILETYPE_PEM));
  4700. #if defined(WOLFSSL_WPAS) && !defined(NO_DSA)
  4701. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dsaParamFile,
  4702. WOLFSSL_FILETYPE_PEM));
  4703. #endif
  4704. wolfSSL_free(ssl);
  4705. wolfSSL_CTX_free(ctx);
  4706. #endif
  4707. return EXPECT_RESULT();
  4708. }
  4709. static int test_wolfSSL_SetTmpDH_buffer(void)
  4710. {
  4711. EXPECT_DECLS;
  4712. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4713. WOLFSSL_CTX *ctx = NULL;
  4714. WOLFSSL *ssl = NULL;
  4715. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4716. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4717. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4718. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4719. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4720. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4721. /* invalid ssl */
  4722. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4723. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4724. /* invalid dhParamFile file */
  4725. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4726. 0, WOLFSSL_FILETYPE_ASN1));
  4727. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, NULL, 0,
  4728. WOLFSSL_FILETYPE_ASN1));
  4729. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4730. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4731. /* success */
  4732. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4733. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4734. wolfSSL_free(ssl);
  4735. wolfSSL_CTX_free(ctx);
  4736. #endif
  4737. return EXPECT_RESULT();
  4738. }
  4739. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4740. {
  4741. EXPECT_DECLS;
  4742. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4743. WOLFSSL_CTX *ctx = NULL;
  4744. WOLFSSL_CTX *ctx2 = NULL;
  4745. WOLFSSL *ssl = NULL;
  4746. WOLFSSL *ssl2 = NULL;
  4747. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4748. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4749. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4750. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4751. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4752. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4753. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4754. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4755. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4756. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4757. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4758. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4759. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4760. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4761. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4762. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4763. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4764. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4765. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4766. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4767. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4768. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4769. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4770. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4771. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4772. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4773. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4774. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4775. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4776. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4777. wolfSSL_free(ssl2);
  4778. wolfSSL_CTX_free(ctx2);
  4779. wolfSSL_free(ssl);
  4780. wolfSSL_CTX_free(ctx);
  4781. #endif
  4782. return EXPECT_RESULT();
  4783. }
  4784. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4785. * allowed.
  4786. * POST: return 1 on success.
  4787. */
  4788. static int test_wolfSSL_SetMinVersion(void)
  4789. {
  4790. int res = TEST_SKIPPED;
  4791. #ifndef NO_WOLFSSL_CLIENT
  4792. int failFlag = WOLFSSL_SUCCESS;
  4793. WOLFSSL_CTX* ctx = NULL;
  4794. WOLFSSL* ssl = NULL;
  4795. int itr;
  4796. #ifndef NO_OLD_TLS
  4797. const int versions[] = {
  4798. #ifdef WOLFSSL_ALLOW_TLSV10
  4799. WOLFSSL_TLSV1,
  4800. #endif
  4801. WOLFSSL_TLSV1_1,
  4802. WOLFSSL_TLSV1_2};
  4803. #elif !defined(WOLFSSL_NO_TLS12)
  4804. const int versions[] = { WOLFSSL_TLSV1_2 };
  4805. #else
  4806. const int versions[] = { WOLFSSL_TLSV1_3 };
  4807. #endif
  4808. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4809. ssl = wolfSSL_new(ctx);
  4810. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4811. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4812. failFlag = WOLFSSL_FAILURE;
  4813. }
  4814. }
  4815. wolfSSL_free(ssl);
  4816. wolfSSL_CTX_free(ctx);
  4817. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4818. #endif
  4819. return res;
  4820. } /* END test_wolfSSL_SetMinVersion */
  4821. #ifdef OPENSSL_EXTRA
  4822. static int test_EC25519(void)
  4823. {
  4824. EXPECT_DECLS;
  4825. #if defined(HAVE_CURVE25519) && defined(WOLFSSL_KEY_GEN)
  4826. byte priv[CURVE25519_KEYSIZE];
  4827. unsigned int privSz = CURVE25519_KEYSIZE;
  4828. byte pub[CURVE25519_KEYSIZE];
  4829. unsigned int pubSz = CURVE25519_KEYSIZE;
  4830. byte priv2[CURVE25519_KEYSIZE];
  4831. unsigned int priv2Sz = CURVE25519_KEYSIZE;
  4832. byte pub2[CURVE25519_KEYSIZE];
  4833. unsigned int pub2Sz = CURVE25519_KEYSIZE;
  4834. byte shared[CURVE25519_KEYSIZE];
  4835. unsigned int sharedSz = CURVE25519_KEYSIZE;
  4836. byte shared2[CURVE25519_KEYSIZE];
  4837. unsigned int shared2Sz = CURVE25519_KEYSIZE;
  4838. /* Bad parameter testing of key generation. */
  4839. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4840. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4841. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4842. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4843. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4844. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, NULL), 0);
  4845. /* Bad length */
  4846. privSz = 1;
  4847. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4848. privSz = CURVE25519_KEYSIZE;
  4849. pubSz = 1;
  4850. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4851. pubSz = CURVE25519_KEYSIZE;
  4852. /* Good case of generating key. */
  4853. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 1);
  4854. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv2, &priv2Sz, pub2, &pub2Sz),
  4855. 1);
  4856. ExpectIntEQ(privSz, CURVE25519_KEYSIZE);
  4857. ExpectIntEQ(pubSz, CURVE25519_KEYSIZE);
  4858. /* Bad parameter testing of shared key. */
  4859. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, NULL, NULL, privSz,
  4860. NULL, pubSz), 0);
  4861. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, NULL, privSz,
  4862. NULL, pubSz), 0);
  4863. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4864. pub, pubSz), 0);
  4865. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4866. pub, pubSz), 0);
  4867. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4868. NULL, pubSz), 0);
  4869. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4870. pub, pubSz), 0);
  4871. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, NULL, priv, privSz,
  4872. pub, pubSz), 0);
  4873. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4874. pub, pubSz), 0);
  4875. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4876. NULL, pubSz), 0);
  4877. /* Bad length. */
  4878. sharedSz = 1;
  4879. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4880. pub, pubSz), 0);
  4881. sharedSz = CURVE25519_KEYSIZE;
  4882. privSz = 1;
  4883. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4884. pub, pubSz), 0);
  4885. privSz = CURVE25519_KEYSIZE;
  4886. pubSz = 1;
  4887. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4888. pub, pubSz), 0);
  4889. pubSz = CURVE25519_KEYSIZE;
  4890. /* Good case of shared key. */
  4891. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4892. pub2, pub2Sz), 1);
  4893. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4894. pub, pubSz), 1);
  4895. ExpectIntEQ(sharedSz, CURVE25519_KEYSIZE);
  4896. ExpectIntEQ(shared2Sz, CURVE25519_KEYSIZE);
  4897. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4898. #endif /* HAVE_CURVE25519 && WOLFSSL_KEY_GEN */
  4899. return EXPECT_RESULT();
  4900. }
  4901. static int test_ED25519(void)
  4902. {
  4903. EXPECT_DECLS;
  4904. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4905. defined(WOLFSSL_KEY_GEN)
  4906. byte priv[ED25519_PRV_KEY_SIZE];
  4907. unsigned int privSz = (unsigned int)sizeof(priv);
  4908. byte pub[ED25519_PUB_KEY_SIZE];
  4909. unsigned int pubSz = (unsigned int)sizeof(pub);
  4910. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4911. const char* msg = TEST_STRING;
  4912. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4913. byte sig[ED25519_SIG_SIZE];
  4914. unsigned int sigSz = (unsigned int)sizeof(sig);
  4915. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4916. /* Bad parameter testing of key generation. */
  4917. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4918. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, NULL, NULL), 0);
  4919. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, NULL, NULL), 0);
  4920. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, pub, NULL), 0);
  4921. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4922. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4923. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4924. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4925. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, NULL), 0);
  4926. /* Bad length. */
  4927. privSz = 1;
  4928. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4929. privSz = ED25519_PRV_KEY_SIZE;
  4930. pubSz = 1;
  4931. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4932. pubSz = ED25519_PUB_KEY_SIZE;
  4933. /* Good case of generating key. */
  4934. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4935. 1);
  4936. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4937. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4938. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4939. /* Bad parameter testing of signing. */
  4940. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4941. NULL), 0);
  4942. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4943. NULL), 0);
  4944. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, NULL,
  4945. NULL), 0);
  4946. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, sig,
  4947. NULL), 0);
  4948. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4949. &sigSz), 0);
  4950. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, sig,
  4951. &sigSz), 0);
  4952. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, sig,
  4953. &sigSz), 0);
  4954. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, NULL,
  4955. &sigSz), 0);
  4956. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4957. NULL), 0);
  4958. /* Bad length. */
  4959. privSz = 1;
  4960. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4961. &sigSz), 0);
  4962. privSz = ED25519_PRV_KEY_SIZE;
  4963. sigSz = 1;
  4964. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4965. &sigSz), 0);
  4966. sigSz = ED25519_SIG_SIZE;
  4967. /* Good case of signing. */
  4968. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4969. &sigSz), 1);
  4970. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4971. #ifdef HAVE_ED25519_VERIFY
  4972. /* Bad parameter testing of verification. */
  4973. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, NULL,
  4974. sigSz), 0);
  4975. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4976. sigSz), 0);
  4977. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, NULL,
  4978. sigSz), 0);
  4979. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, sig,
  4980. sigSz), 0);
  4981. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, sig,
  4982. sigSz), 0);
  4983. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4984. sigSz), 0);
  4985. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4986. sigSz), 0);
  4987. /* Bad length. */
  4988. pubSz = 1;
  4989. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4990. sigSz), 0);
  4991. pubSz = ED25519_PUB_KEY_SIZE;
  4992. sigSz = 1;
  4993. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4994. sigSz), 0);
  4995. sigSz = ED25519_SIG_SIZE;
  4996. /* Good case of verification. */
  4997. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4998. sigSz), 1);
  4999. /* Bad signature. */
  5000. if (EXPECT_SUCCESS()) {
  5001. sig[1] ^= 0x80;
  5002. }
  5003. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  5004. sigSz), 0);
  5005. #endif /* HAVE_ED25519_VERIFY */
  5006. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  5007. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  5008. return EXPECT_RESULT();
  5009. }
  5010. static int test_EC448(void)
  5011. {
  5012. EXPECT_DECLS;
  5013. #if defined(HAVE_CURVE448) && defined(WOLFSSL_KEY_GEN)
  5014. byte priv[CURVE448_KEY_SIZE];
  5015. unsigned int privSz = CURVE448_KEY_SIZE;
  5016. byte pub[CURVE448_KEY_SIZE];
  5017. unsigned int pubSz = CURVE448_KEY_SIZE;
  5018. byte priv2[CURVE448_KEY_SIZE];
  5019. unsigned int priv2Sz = CURVE448_KEY_SIZE;
  5020. byte pub2[CURVE448_KEY_SIZE];
  5021. unsigned int pub2Sz = CURVE448_KEY_SIZE;
  5022. byte shared[CURVE448_KEY_SIZE];
  5023. unsigned int sharedSz = CURVE448_KEY_SIZE;
  5024. byte shared2[CURVE448_KEY_SIZE];
  5025. unsigned int shared2Sz = CURVE448_KEY_SIZE;
  5026. /* Bad parameter testing of key generation. */
  5027. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, NULL, NULL, NULL), 0);
  5028. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  5029. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  5030. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, NULL, pub, &pubSz), 0);
  5031. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  5032. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, NULL), 0);
  5033. /* Bad length. */
  5034. privSz = 1;
  5035. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  5036. privSz = CURVE448_KEY_SIZE;
  5037. pubSz = 1;
  5038. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  5039. pubSz = CURVE448_KEY_SIZE;
  5040. /* Good case of generating key. */
  5041. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 1);
  5042. ExpectIntEQ(wolfSSL_EC448_generate_key(priv2, &priv2Sz, pub2, &pub2Sz), 1);
  5043. ExpectIntEQ(privSz, CURVE448_KEY_SIZE);
  5044. ExpectIntEQ(pubSz, CURVE448_KEY_SIZE);
  5045. /* Bad parameter testing of shared key. */
  5046. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, NULL, NULL, privSz,
  5047. NULL, pubSz), 0);
  5048. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, NULL, privSz,
  5049. NULL, pubSz), 0);
  5050. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  5051. pub, pubSz), 0);
  5052. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  5053. pub, pubSz), 0);
  5054. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  5055. NULL, pubSz), 0);
  5056. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  5057. pub, pubSz), 0);
  5058. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, NULL, priv, privSz,
  5059. pub, pubSz), 0);
  5060. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  5061. pub, pubSz), 0);
  5062. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  5063. NULL, pubSz), 0);
  5064. /* Bad length. */
  5065. sharedSz = 1;
  5066. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  5067. pub, pubSz), 0);
  5068. sharedSz = CURVE448_KEY_SIZE;
  5069. privSz = 1;
  5070. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  5071. pub, pubSz), 0);
  5072. privSz = CURVE448_KEY_SIZE;
  5073. pubSz = 1;
  5074. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  5075. pub, pubSz), 0);
  5076. pubSz = CURVE448_KEY_SIZE;
  5077. /* Good case of shared key. */
  5078. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  5079. pub2, pub2Sz), 1);
  5080. ExpectIntEQ(wolfSSL_EC448_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  5081. pub, pubSz), 1);
  5082. ExpectIntEQ(sharedSz, CURVE448_KEY_SIZE);
  5083. ExpectIntEQ(shared2Sz, CURVE448_KEY_SIZE);
  5084. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  5085. #endif /* HAVE_CURVE448 && WOLFSSL_KEY_GEN */
  5086. return EXPECT_RESULT();
  5087. }
  5088. static int test_ED448(void)
  5089. {
  5090. EXPECT_DECLS;
  5091. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  5092. defined(WOLFSSL_KEY_GEN)
  5093. byte priv[ED448_PRV_KEY_SIZE];
  5094. unsigned int privSz = (unsigned int)sizeof(priv);
  5095. byte pub[ED448_PUB_KEY_SIZE];
  5096. unsigned int pubSz = (unsigned int)sizeof(pub);
  5097. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  5098. const char* msg = TEST_STRING;
  5099. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  5100. byte sig[ED448_SIG_SIZE];
  5101. unsigned int sigSz = (unsigned int)sizeof(sig);
  5102. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  5103. /* Bad parameter testing of key generation. */
  5104. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, NULL), 0);
  5105. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, NULL, NULL), 0);
  5106. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, NULL, NULL), 0);
  5107. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, pub, NULL), 0);
  5108. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, &pubSz), 0);
  5109. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  5110. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, pub, &pubSz), 0);
  5111. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  5112. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, NULL), 0);
  5113. /* Bad length. */
  5114. privSz = 1;
  5115. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  5116. privSz = ED448_PRV_KEY_SIZE;
  5117. pubSz = 1;
  5118. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  5119. pubSz = ED448_PUB_KEY_SIZE;
  5120. /* Good case of generating key. */
  5121. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 1);
  5122. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  5123. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  5124. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  5125. /* Bad parameter testing of signing. */
  5126. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  5127. NULL), 0);
  5128. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, NULL,
  5129. NULL), 0);
  5130. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, NULL,
  5131. NULL), 0);
  5132. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, sig,
  5133. NULL), 0);
  5134. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  5135. &sigSz), 0);
  5136. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, sig,
  5137. &sigSz), 0);
  5138. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, sig,
  5139. &sigSz), 0);
  5140. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, NULL,
  5141. &sigSz), 0);
  5142. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  5143. NULL), 0);
  5144. /* Bad length. */
  5145. privSz = 1;
  5146. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  5147. &sigSz), 0);
  5148. privSz = ED448_PRV_KEY_SIZE;
  5149. sigSz = 1;
  5150. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  5151. &sigSz), 0);
  5152. sigSz = ED448_SIG_SIZE;
  5153. /* Good case of signing. */
  5154. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  5155. &sigSz), 1);
  5156. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  5157. #ifdef HAVE_ED448_VERIFY
  5158. /* Bad parameter testing of verification. */
  5159. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, NULL,
  5160. sigSz), 0);
  5161. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  5162. sigSz), 0);
  5163. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, NULL,
  5164. sigSz), 0);
  5165. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, sig,
  5166. sigSz), 0);
  5167. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, sig,
  5168. sigSz), 0);
  5169. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, sig,
  5170. sigSz), 0);
  5171. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, NULL,
  5172. sigSz), 0);
  5173. /* Bad length. */
  5174. pubSz = 1;
  5175. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  5176. sigSz), 0);
  5177. pubSz = ED448_PUB_KEY_SIZE;
  5178. sigSz = 1;
  5179. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  5180. sigSz), 0);
  5181. sigSz = ED448_SIG_SIZE;
  5182. /* Good case of verification. */
  5183. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  5184. sigSz), 1);
  5185. /* Bad signature. */
  5186. if (EXPECT_SUCCESS()) {
  5187. sig[1] ^= 0x80;
  5188. }
  5189. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  5190. sigSz), 0);
  5191. #endif /* HAVE_ED448_VERIFY */
  5192. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  5193. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  5194. return EXPECT_RESULT();
  5195. }
  5196. #endif /* OPENSSL_EXTRA */
  5197. #include <wolfssl/openssl/pem.h>
  5198. /*----------------------------------------------------------------------------*
  5199. | EVP
  5200. *----------------------------------------------------------------------------*/
  5201. static int test_wolfSSL_EVP_PKEY_print_public(void)
  5202. {
  5203. EXPECT_DECLS;
  5204. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  5205. WOLFSSL_BIO* rbio = NULL;
  5206. WOLFSSL_BIO* wbio = NULL;
  5207. WOLFSSL_EVP_PKEY* pkey = NULL;
  5208. char line[256] = { 0 };
  5209. char line1[256] = { 0 };
  5210. int i = 0;
  5211. /* test error cases */
  5212. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  5213. /*
  5214. * test RSA public key print
  5215. * in this test, pass '3' for indent
  5216. */
  5217. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  5218. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  5219. sizeof_client_keypub_der_1024));
  5220. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  5221. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  5222. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  5223. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5224. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  5225. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  5226. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5227. strcpy(line1, " Modulus:\n");
  5228. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5229. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5230. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  5231. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5232. /* skip to the end of modulus element*/
  5233. for (i = 0; i < 8 ;i++) {
  5234. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5235. }
  5236. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5237. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  5238. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5239. /* should reach EOF */
  5240. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  5241. EVP_PKEY_free(pkey);
  5242. pkey = NULL;
  5243. BIO_free(rbio);
  5244. BIO_free(wbio);
  5245. rbio = NULL;
  5246. wbio = NULL;
  5247. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  5248. /*
  5249. * test DSA public key print
  5250. */
  5251. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  5252. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  5253. sizeof_dsa_pub_key_der_2048));
  5254. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  5255. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  5256. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  5257. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5258. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  5259. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5260. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5261. strcpy(line1, "pub:\n");
  5262. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5263. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5264. strcpy(line1,
  5265. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  5266. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5267. /* skip to the end of pub element*/
  5268. for (i = 0; i < 17 ;i++) {
  5269. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5270. }
  5271. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5272. strcpy(line1, "P:\n");
  5273. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5274. /* skip to the end of P element*/
  5275. for (i = 0; i < 18 ;i++) {
  5276. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5277. }
  5278. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5279. strcpy(line1, "Q:\n");
  5280. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5281. /* skip to the end of Q element*/
  5282. for (i = 0; i < 3 ;i++) {
  5283. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5284. }
  5285. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5286. strcpy(line1, "G:\n");
  5287. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5288. /* skip to the end of G element*/
  5289. for (i = 0; i < 18 ;i++) {
  5290. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5291. }
  5292. /* should reach EOF */
  5293. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  5294. EVP_PKEY_free(pkey);
  5295. pkey = NULL;
  5296. BIO_free(rbio);
  5297. BIO_free(wbio);
  5298. rbio = NULL;
  5299. wbio = NULL;
  5300. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  5301. /*
  5302. * test ECC public key print
  5303. */
  5304. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  5305. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  5306. sizeof_ecc_clikeypub_der_256));
  5307. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  5308. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  5309. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  5310. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5311. strcpy(line1, "Public-Key: (256 bit)\n");
  5312. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5313. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5314. strcpy(line1, "pub:\n");
  5315. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5316. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5317. strcpy(line1,
  5318. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  5319. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5320. /* skip to the end of pub element*/
  5321. for (i = 0; i < 4 ;i++) {
  5322. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5323. }
  5324. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5325. strcpy(line1, "ASN1 OID: prime256v1\n");
  5326. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5327. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5328. strcpy(line1, "NIST CURVE: P-256\n");
  5329. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5330. /* should reach EOF */
  5331. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  5332. EVP_PKEY_free(pkey);
  5333. pkey = NULL;
  5334. BIO_free(rbio);
  5335. BIO_free(wbio);
  5336. rbio = NULL;
  5337. wbio = NULL;
  5338. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  5339. /*
  5340. * test DH public key print
  5341. */
  5342. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  5343. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  5344. sizeof_dh_pub_key_der_2048));
  5345. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  5346. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  5347. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  5348. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5349. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  5350. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5351. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5352. strcpy(line1, "public-key:\n");
  5353. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5354. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5355. strcpy(line1,
  5356. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  5357. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5358. /* skip to the end of public-key element*/
  5359. for (i = 0; i < 17 ;i++) {
  5360. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5361. }
  5362. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5363. strcpy(line1, "prime:\n");
  5364. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5365. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5366. strcpy(line1,
  5367. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  5368. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5369. /* skip to the end of prime element*/
  5370. for (i = 0; i < 17 ;i++) {
  5371. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5372. }
  5373. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5374. strcpy(line1, "generator: 2 (0x02)\n");
  5375. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5376. /* should reach EOF */
  5377. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  5378. EVP_PKEY_free(pkey);
  5379. pkey = NULL;
  5380. BIO_free(rbio);
  5381. BIO_free(wbio);
  5382. rbio = NULL;
  5383. wbio = NULL;
  5384. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  5385. /* to prevent "unused variable" warning */
  5386. (void)pkey;
  5387. (void)wbio;
  5388. (void)rbio;
  5389. (void)line;
  5390. (void)line1;
  5391. (void)i;
  5392. #endif /* OPENSSL_EXTRA */
  5393. return EXPECT_RESULT();
  5394. }
  5395. /* Test functions for base64 encode/decode */
  5396. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  5397. {
  5398. EXPECT_DECLS;
  5399. #if defined(OPENSSL_EXTRA) && \
  5400. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5401. EVP_ENCODE_CTX* ctx = NULL;
  5402. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5403. ExpectIntEQ(ctx->remaining,0);
  5404. ExpectIntEQ(ctx->data[0],0);
  5405. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  5406. EVP_ENCODE_CTX_free(ctx);
  5407. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5408. return EXPECT_RESULT();
  5409. }
  5410. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  5411. {
  5412. EXPECT_DECLS;
  5413. #if defined(OPENSSL_EXTRA) && \
  5414. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5415. EVP_ENCODE_CTX* ctx = NULL;
  5416. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5417. EVP_ENCODE_CTX_free(ctx);
  5418. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5419. return EXPECT_RESULT();
  5420. }
  5421. static int test_wolfSSL_EVP_EncodeInit(void)
  5422. {
  5423. EXPECT_DECLS;
  5424. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5425. EVP_ENCODE_CTX* ctx = NULL;
  5426. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5427. ExpectIntEQ(ctx->remaining, 0);
  5428. ExpectIntEQ(ctx->data[0], 0);
  5429. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5430. if (ctx != NULL) {
  5431. /* make ctx dirty */
  5432. ctx->remaining = 10;
  5433. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  5434. }
  5435. EVP_EncodeInit(ctx);
  5436. ExpectIntEQ(ctx->remaining, 0);
  5437. ExpectIntEQ(ctx->data[0], 0);
  5438. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5439. EVP_ENCODE_CTX_free(ctx);
  5440. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5441. return EXPECT_RESULT();
  5442. }
  5443. static int test_wolfSSL_EVP_EncodeUpdate(void)
  5444. {
  5445. EXPECT_DECLS;
  5446. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5447. int outl;
  5448. int total;
  5449. const unsigned char plain0[] = {"Th"};
  5450. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  5451. const unsigned char plain2[] = {"This is additional data."};
  5452. const unsigned char encBlock0[] = {"VGg="};
  5453. const unsigned char enc0[] = {"VGg=\n"};
  5454. /* expected encoded result for the first output 64 chars plus trailing LF*/
  5455. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  5456. const unsigned char enc2[] =
  5457. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  5458. unsigned char encOutBuff[300];
  5459. EVP_ENCODE_CTX* ctx = NULL;
  5460. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5461. EVP_EncodeInit(ctx);
  5462. /* illegal parameter test */
  5463. ExpectIntEQ(
  5464. EVP_EncodeUpdate(
  5465. NULL, /* pass NULL as ctx */
  5466. encOutBuff,
  5467. &outl,
  5468. plain1,
  5469. sizeof(plain1)-1),
  5470. 0 /* expected result code 0: fail */
  5471. );
  5472. ExpectIntEQ(
  5473. EVP_EncodeUpdate(
  5474. ctx,
  5475. NULL, /* pass NULL as out buff */
  5476. &outl,
  5477. plain1,
  5478. sizeof(plain1)-1),
  5479. 0 /* expected result code 0: fail */
  5480. );
  5481. ExpectIntEQ(
  5482. EVP_EncodeUpdate(
  5483. ctx,
  5484. encOutBuff,
  5485. NULL, /* pass NULL as outl */
  5486. plain1,
  5487. sizeof(plain1)-1),
  5488. 0 /* expected result code 0: fail */
  5489. );
  5490. ExpectIntEQ(
  5491. EVP_EncodeUpdate(
  5492. ctx,
  5493. encOutBuff,
  5494. &outl,
  5495. NULL, /* pass NULL as in */
  5496. sizeof(plain1)-1),
  5497. 0 /* expected result code 0: fail */
  5498. );
  5499. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  5500. /* meaningless parameter test */
  5501. ExpectIntEQ(
  5502. EVP_EncodeUpdate(
  5503. ctx,
  5504. encOutBuff,
  5505. &outl,
  5506. plain1,
  5507. 0), /* pass zero input */
  5508. 1 /* expected result code 1: success */
  5509. );
  5510. /* very small data encoding test */
  5511. EVP_EncodeInit(ctx);
  5512. ExpectIntEQ(
  5513. EVP_EncodeUpdate(
  5514. ctx,
  5515. encOutBuff,
  5516. &outl,
  5517. plain0,
  5518. sizeof(plain0)-1),
  5519. 1 /* expected result code 1: success */
  5520. );
  5521. ExpectIntEQ(outl,0);
  5522. if (EXPECT_SUCCESS()) {
  5523. EVP_EncodeFinal(
  5524. ctx,
  5525. encOutBuff + outl,
  5526. &outl);
  5527. }
  5528. ExpectIntEQ( outl, sizeof(enc0)-1);
  5529. ExpectIntEQ(
  5530. XSTRNCMP(
  5531. (const char*)encOutBuff,
  5532. (const char*)enc0,sizeof(enc0) ),
  5533. 0);
  5534. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5535. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  5536. sizeof(encBlock0)-1);
  5537. ExpectStrEQ(encOutBuff, encBlock0);
  5538. /* pass small size( < 48bytes ) input, then make sure they are not
  5539. * encoded and just stored in ctx
  5540. */
  5541. EVP_EncodeInit(ctx);
  5542. total = 0;
  5543. outl = 0;
  5544. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5545. ExpectIntEQ(
  5546. EVP_EncodeUpdate(
  5547. ctx,
  5548. encOutBuff, /* buffer for output */
  5549. &outl, /* size of output */
  5550. plain1, /* input */
  5551. sizeof(plain1)-1), /* size of input */
  5552. 1); /* expected result code 1:success */
  5553. total += outl;
  5554. ExpectIntEQ(outl, 0); /* no output expected */
  5555. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  5556. ExpectTrue(
  5557. XSTRNCMP((const char*)(ctx->data),
  5558. (const char*)plain1,
  5559. ctx->remaining) ==0 );
  5560. ExpectTrue(encOutBuff[0] == 0);
  5561. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  5562. * the stored data and the new input together
  5563. */
  5564. ExpectIntEQ(
  5565. EVP_EncodeUpdate(
  5566. ctx,
  5567. encOutBuff + outl, /* buffer for output */
  5568. &outl, /* size of output */
  5569. plain2, /* additional input */
  5570. sizeof(plain2) -1), /* size of additional input */
  5571. 1); /* expected result code 1:success */
  5572. total += outl;
  5573. ExpectIntNE(outl, 0); /* some output is expected this time*/
  5574. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  5575. ExpectIntEQ(
  5576. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  5577. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  5578. EVP_EncodeFinal(
  5579. ctx,
  5580. encOutBuff + outl,
  5581. &outl);
  5582. total += outl;
  5583. ExpectIntNE(total,0);
  5584. ExpectIntNE(outl,0);
  5585. ExpectIntEQ(XSTRNCMP(
  5586. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  5587. /* test with illeagal parameters */
  5588. outl = 1;
  5589. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  5590. ExpectIntEQ(outl, 0);
  5591. outl = 1;
  5592. EVP_EncodeFinal(ctx, NULL, &outl);
  5593. ExpectIntEQ(outl, 0);
  5594. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  5595. EVP_EncodeFinal(NULL, NULL, NULL);
  5596. EVP_ENCODE_CTX_free(ctx);
  5597. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5598. return EXPECT_RESULT();
  5599. }
  5600. static int test_wolfSSL_EVP_EncodeFinal(void)
  5601. {
  5602. int res = TEST_SKIPPED;
  5603. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5604. /* tests for wolfSSL_EVP_EncodeFinal are included in
  5605. * test_wolfSSL_EVP_EncodeUpdate
  5606. */
  5607. res = TEST_SUCCESS;
  5608. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5609. return res;
  5610. }
  5611. static int test_wolfSSL_EVP_DecodeInit(void)
  5612. {
  5613. EXPECT_DECLS;
  5614. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5615. EVP_ENCODE_CTX* ctx = NULL;
  5616. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  5617. ExpectIntEQ( ctx->remaining,0);
  5618. ExpectIntEQ( ctx->data[0],0);
  5619. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5620. if (ctx != NULL) {
  5621. /* make ctx dirty */
  5622. ctx->remaining = 10;
  5623. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  5624. }
  5625. EVP_DecodeInit(ctx);
  5626. ExpectIntEQ( ctx->remaining,0);
  5627. ExpectIntEQ( ctx->data[0],0);
  5628. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5629. EVP_ENCODE_CTX_free(ctx);
  5630. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5631. return EXPECT_RESULT();
  5632. }
  5633. static int test_wolfSSL_EVP_DecodeUpdate(void)
  5634. {
  5635. EXPECT_DECLS;
  5636. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5637. int outl;
  5638. unsigned char decOutBuff[300];
  5639. EVP_ENCODE_CTX* ctx = NULL;
  5640. static const unsigned char enc1[] =
  5641. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5642. /* const unsigned char plain1[] =
  5643. {"This is a base64 decoding test."} */
  5644. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5645. EVP_DecodeInit(ctx);
  5646. /* illegal parameter tests */
  5647. /* pass NULL as ctx */
  5648. ExpectIntEQ(
  5649. EVP_DecodeUpdate(
  5650. NULL, /* pass NULL as ctx */
  5651. decOutBuff,
  5652. &outl,
  5653. enc1,
  5654. sizeof(enc1)-1),
  5655. -1 /* expected result code -1: fail */
  5656. );
  5657. ExpectIntEQ( outl, 0);
  5658. /* pass NULL as output */
  5659. ExpectIntEQ(
  5660. EVP_DecodeUpdate(
  5661. ctx,
  5662. NULL, /* pass NULL as out buff */
  5663. &outl,
  5664. enc1,
  5665. sizeof(enc1)-1),
  5666. -1 /* expected result code -1: fail */
  5667. );
  5668. ExpectIntEQ( outl, 0);
  5669. /* pass NULL as outl */
  5670. ExpectIntEQ(
  5671. EVP_DecodeUpdate(
  5672. ctx,
  5673. decOutBuff,
  5674. NULL, /* pass NULL as outl */
  5675. enc1,
  5676. sizeof(enc1)-1),
  5677. -1 /* expected result code -1: fail */
  5678. );
  5679. /* pass NULL as input */
  5680. ExpectIntEQ(
  5681. EVP_DecodeUpdate(
  5682. ctx,
  5683. decOutBuff,
  5684. &outl,
  5685. NULL, /* pass NULL as in */
  5686. sizeof(enc1)-1),
  5687. -1 /* expected result code -1: fail */
  5688. );
  5689. ExpectIntEQ( outl, 0);
  5690. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  5691. /* pass zero length input */
  5692. ExpectIntEQ(
  5693. EVP_DecodeUpdate(
  5694. ctx,
  5695. decOutBuff,
  5696. &outl,
  5697. enc1,
  5698. 0), /* pass zero as input len */
  5699. 1 /* expected result code 1: success */
  5700. );
  5701. /* decode correct base64 string */
  5702. {
  5703. static const unsigned char enc2[] =
  5704. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5705. static const unsigned char plain2[] =
  5706. {"This is a base64 decoding test."};
  5707. EVP_EncodeInit(ctx);
  5708. ExpectIntEQ(
  5709. EVP_DecodeUpdate(
  5710. ctx,
  5711. decOutBuff,
  5712. &outl,
  5713. enc2,
  5714. sizeof(enc2)-1),
  5715. 0 /* expected result code 0: success */
  5716. );
  5717. ExpectIntEQ(outl,sizeof(plain2) -1);
  5718. ExpectIntEQ(
  5719. EVP_DecodeFinal(
  5720. ctx,
  5721. decOutBuff + outl,
  5722. &outl),
  5723. 1 /* expected result code 1: success */
  5724. );
  5725. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  5726. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5727. sizeof(plain2) -1 ),0);
  5728. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  5729. sizeof(plain2)-1);
  5730. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5731. sizeof(plain2) -1 ),0);
  5732. }
  5733. /* decode correct base64 string which does not have '\n' in its last*/
  5734. {
  5735. static const unsigned char enc3[] =
  5736. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  5737. static const unsigned char plain3[] =
  5738. {"This is a base64 decoding test."}; /* 31 chars */
  5739. EVP_EncodeInit(ctx);
  5740. ExpectIntEQ(
  5741. EVP_DecodeUpdate(
  5742. ctx,
  5743. decOutBuff,
  5744. &outl,
  5745. enc3,
  5746. sizeof(enc3)-1),
  5747. 0 /* expected result code 0: success */
  5748. );
  5749. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  5750. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5751. sizeof(plain3) -1 ),0);
  5752. ExpectIntEQ(
  5753. EVP_DecodeFinal(
  5754. ctx,
  5755. decOutBuff + outl,
  5756. &outl),
  5757. 1 /* expected result code 1: success */
  5758. );
  5759. ExpectIntEQ(outl,0 );
  5760. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  5761. sizeof(plain3)-1);
  5762. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5763. sizeof(plain3) -1 ),0);
  5764. }
  5765. /* decode string which has a padding char ('=') in the illegal position*/
  5766. {
  5767. static const unsigned char enc4[] =
  5768. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  5769. EVP_EncodeInit(ctx);
  5770. ExpectIntEQ(
  5771. EVP_DecodeUpdate(
  5772. ctx,
  5773. decOutBuff,
  5774. &outl,
  5775. enc4,
  5776. sizeof(enc4)-1),
  5777. -1 /* expected result code -1: error */
  5778. );
  5779. ExpectIntEQ(outl,0);
  5780. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  5781. }
  5782. /* small data decode test */
  5783. {
  5784. static const unsigned char enc00[] = {"VG"};
  5785. static const unsigned char enc01[] = {"g=\n"};
  5786. static const unsigned char plain4[] = {"Th"};
  5787. EVP_EncodeInit(ctx);
  5788. ExpectIntEQ(
  5789. EVP_DecodeUpdate(
  5790. ctx,
  5791. decOutBuff,
  5792. &outl,
  5793. enc00,
  5794. sizeof(enc00)-1),
  5795. 1 /* expected result code 1: success */
  5796. );
  5797. ExpectIntEQ(outl,0);
  5798. ExpectIntEQ(
  5799. EVP_DecodeUpdate(
  5800. ctx,
  5801. decOutBuff + outl,
  5802. &outl,
  5803. enc01,
  5804. sizeof(enc01)-1),
  5805. 0 /* expected result code 0: success */
  5806. );
  5807. ExpectIntEQ(outl,sizeof(plain4)-1);
  5808. /* test with illegal parameters */
  5809. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  5810. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5811. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5812. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5813. if (EXPECT_SUCCESS()) {
  5814. EVP_DecodeFinal(
  5815. ctx,
  5816. decOutBuff + outl,
  5817. &outl);
  5818. }
  5819. ExpectIntEQ( outl, 0);
  5820. ExpectIntEQ(
  5821. XSTRNCMP(
  5822. (const char*)decOutBuff,
  5823. (const char*)plain4,sizeof(plain4)-1 ),
  5824. 0);
  5825. }
  5826. EVP_ENCODE_CTX_free(ctx);
  5827. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5828. return EXPECT_RESULT();
  5829. }
  5830. static int test_wolfSSL_EVP_DecodeFinal(void)
  5831. {
  5832. int res = TEST_SKIPPED;
  5833. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5834. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5835. * test_wolfSSL_EVP_DecodeUpdate
  5836. */
  5837. res = TEST_SUCCESS;
  5838. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5839. return res;
  5840. }
  5841. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5842. */
  5843. #ifdef OPENSSL_EXTRA
  5844. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5845. {
  5846. EXPECT_DECLS;
  5847. #ifndef NO_AES
  5848. const WOLFSSL_EVP_CIPHER* c;
  5849. c = wolfSSL_EVP_get_cipherbynid(419);
  5850. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5851. defined(WOLFSSL_AES_128)
  5852. ExpectNotNull(c);
  5853. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5854. #else
  5855. ExpectNull(c);
  5856. #endif
  5857. c = wolfSSL_EVP_get_cipherbynid(423);
  5858. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5859. defined(WOLFSSL_AES_192)
  5860. ExpectNotNull(c);
  5861. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5862. #else
  5863. ExpectNull(c);
  5864. #endif
  5865. c = wolfSSL_EVP_get_cipherbynid(427);
  5866. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5867. defined(WOLFSSL_AES_256)
  5868. ExpectNotNull(c);
  5869. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5870. #else
  5871. ExpectNull(c);
  5872. #endif
  5873. c = wolfSSL_EVP_get_cipherbynid(904);
  5874. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5875. ExpectNotNull(c);
  5876. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5877. #else
  5878. ExpectNull(c);
  5879. #endif
  5880. c = wolfSSL_EVP_get_cipherbynid(905);
  5881. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5882. ExpectNotNull(c);
  5883. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5884. #else
  5885. ExpectNull(c);
  5886. #endif
  5887. c = wolfSSL_EVP_get_cipherbynid(906);
  5888. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5889. ExpectNotNull(c);
  5890. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5891. #else
  5892. ExpectNull(c);
  5893. #endif
  5894. c = wolfSSL_EVP_get_cipherbynid(418);
  5895. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5896. ExpectNotNull(c);
  5897. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5898. #else
  5899. ExpectNull(c);
  5900. #endif
  5901. c = wolfSSL_EVP_get_cipherbynid(422);
  5902. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5903. ExpectNotNull(c);
  5904. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5905. #else
  5906. ExpectNull(c);
  5907. #endif
  5908. c = wolfSSL_EVP_get_cipherbynid(426);
  5909. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5910. ExpectNotNull(c);
  5911. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5912. #else
  5913. ExpectNull(c);
  5914. #endif
  5915. #endif /* !NO_AES */
  5916. #ifndef NO_DES3
  5917. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5918. #ifdef WOLFSSL_DES_ECB
  5919. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5920. #endif
  5921. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5922. #ifdef WOLFSSL_DES_ECB
  5923. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5924. #endif
  5925. #endif /* !NO_DES3 */
  5926. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5927. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5928. #endif
  5929. /* test for nid is out of range */
  5930. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5931. return EXPECT_RESULT();
  5932. }
  5933. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5934. {
  5935. EXPECT_DECLS;
  5936. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5937. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5938. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5939. const EVP_CIPHER *test;
  5940. byte key[AES_BLOCK_SIZE] = {0};
  5941. byte iv[AES_BLOCK_SIZE] = {0};
  5942. ExpectNotNull(ctx);
  5943. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5944. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5945. test = EVP_CIPHER_CTX_cipher(ctx);
  5946. ExpectTrue(init == test);
  5947. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5948. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5949. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5950. EVP_CIPHER_CTX_free(ctx);
  5951. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5952. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5953. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5954. return EXPECT_RESULT();
  5955. }
  5956. #endif /* OPENSSL_EXTRA */
  5957. /*----------------------------------------------------------------------------*
  5958. | IO
  5959. *----------------------------------------------------------------------------*/
  5960. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5961. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5962. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5963. #ifdef WC_SHA512_DIGEST_SIZE
  5964. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5965. #else
  5966. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5967. #endif
  5968. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5969. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5970. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5971. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5972. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5973. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5974. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5975. defined(HAVE_AES_CBC)
  5976. typedef struct openssl_key_ctx {
  5977. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5978. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5979. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5980. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5981. } openssl_key_ctx;
  5982. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5983. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5984. static WC_INLINE int OpenSSLTicketInit(void)
  5985. {
  5986. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5987. if (ret != 0) return ret;
  5988. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5989. sizeof(myOpenSSLKey_ctx.name));
  5990. if (ret != 0) return ret;
  5991. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5992. sizeof(myOpenSSLKey_ctx.key));
  5993. if (ret != 0) return ret;
  5994. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5995. sizeof(myOpenSSLKey_ctx.hmacKey));
  5996. if (ret != 0) return ret;
  5997. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5998. sizeof(myOpenSSLKey_ctx.iv));
  5999. if (ret != 0) return ret;
  6000. return 0;
  6001. }
  6002. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  6003. byte name[WOLFSSL_TICKET_NAME_SZ],
  6004. byte iv[WOLFSSL_TICKET_IV_SZ],
  6005. WOLFSSL_EVP_CIPHER_CTX *ectx,
  6006. WOLFSSL_HMAC_CTX *hctx, int enc) {
  6007. (void)ssl;
  6008. if (enc) {
  6009. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  6010. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  6011. }
  6012. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  6013. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  6014. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  6015. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  6016. return 0;
  6017. }
  6018. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  6019. if (enc)
  6020. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  6021. else
  6022. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  6023. return 1;
  6024. }
  6025. static WC_INLINE void OpenSSLTicketCleanup(void)
  6026. {
  6027. wc_FreeRng(&myOpenSSLKey_rng);
  6028. }
  6029. #endif
  6030. #endif
  6031. /* helper functions */
  6032. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  6033. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  6034. void *ctx)
  6035. {
  6036. struct test_ssl_memio_ctx *test_ctx;
  6037. byte *buf;
  6038. int *len;
  6039. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  6040. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  6041. buf = test_ctx->c_buff;
  6042. len = &test_ctx->c_len;
  6043. }
  6044. else {
  6045. buf = test_ctx->s_buff;
  6046. len = &test_ctx->s_len;
  6047. }
  6048. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  6049. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  6050. XMEMCPY(buf + *len, data, sz);
  6051. *len += sz;
  6052. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  6053. {
  6054. /* This can be imported into Wireshark by transforming the file with
  6055. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  6056. * And then loading test_output.dump.hex into Wireshark using the
  6057. * "Import from Hex Dump..." option ion and selecting the TCP
  6058. * encapsulation option. */
  6059. char dump_file_name[64];
  6060. WOLFSSL_BIO *dump_file;
  6061. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  6062. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  6063. if (dump_file != NULL) {
  6064. (void)wolfSSL_BIO_write(dump_file, data, sz);
  6065. wolfSSL_BIO_free(dump_file);
  6066. }
  6067. }
  6068. #endif
  6069. return sz;
  6070. }
  6071. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  6072. void *ctx)
  6073. {
  6074. struct test_ssl_memio_ctx *test_ctx;
  6075. int read_sz;
  6076. byte *buf;
  6077. int *len;
  6078. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  6079. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  6080. buf = test_ctx->s_buff;
  6081. len = &test_ctx->s_len;
  6082. }
  6083. else {
  6084. buf = test_ctx->c_buff;
  6085. len = &test_ctx->c_len;
  6086. }
  6087. if (*len == 0)
  6088. return WOLFSSL_CBIO_ERR_WANT_READ;
  6089. read_sz = sz < *len ? sz : *len;
  6090. XMEMCPY(data, buf, read_sz);
  6091. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  6092. *len -= read_sz;
  6093. return read_sz;
  6094. }
  6095. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  6096. {
  6097. EXPECT_DECLS;
  6098. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6099. int c_sharedCtx = 0;
  6100. int s_sharedCtx = 0;
  6101. #endif
  6102. const char* clientCertFile = cliCertFile;
  6103. const char* clientKeyFile = cliKeyFile;
  6104. const char* serverCertFile = svrCertFile;
  6105. const char* serverKeyFile = svrKeyFile;
  6106. /********************************
  6107. * Create WOLFSSL_CTX for client.
  6108. ********************************/
  6109. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6110. if (ctx->c_ctx != NULL) {
  6111. c_sharedCtx = ctx->c_cb.isSharedCtx;
  6112. }
  6113. else
  6114. #endif
  6115. {
  6116. WOLFSSL_METHOD* method = NULL;
  6117. if (ctx->c_cb.method != NULL) {
  6118. method = ctx->c_cb.method();
  6119. }
  6120. else {
  6121. method = wolfSSLv23_client_method();
  6122. }
  6123. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  6124. }
  6125. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  6126. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  6127. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6128. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  6129. #endif
  6130. if (ctx->c_cb.caPemFile != NULL)
  6131. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  6132. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  6133. else
  6134. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  6135. caCertFile, 0), WOLFSSL_SUCCESS);
  6136. if (ctx->c_cb.certPemFile != NULL) {
  6137. clientCertFile = ctx->c_cb.certPemFile;
  6138. }
  6139. if (ctx->c_cb.keyPemFile != NULL) {
  6140. clientKeyFile = ctx->c_cb.keyPemFile;
  6141. }
  6142. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6143. if (!c_sharedCtx)
  6144. #endif
  6145. {
  6146. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  6147. clientCertFile), WOLFSSL_SUCCESS);
  6148. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  6149. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  6150. }
  6151. #ifdef HAVE_CRL
  6152. if (ctx->c_cb.crlPemFile != NULL) {
  6153. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  6154. WOLFSSL_SUCCESS);
  6155. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  6156. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  6157. }
  6158. #endif
  6159. if (ctx->c_ciphers != NULL) {
  6160. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  6161. WOLFSSL_SUCCESS);
  6162. }
  6163. if (ctx->c_cb.ctx_ready != NULL) {
  6164. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  6165. }
  6166. /********************************
  6167. * Create WOLFSSL_CTX for server.
  6168. ********************************/
  6169. if (ctx->s_ctx != NULL) {
  6170. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6171. s_sharedCtx = 1;
  6172. #endif
  6173. ctx->s_cb.isSharedCtx = 1;
  6174. }
  6175. else
  6176. {
  6177. WOLFSSL_METHOD* method = NULL;
  6178. if (ctx->s_cb.method != NULL) {
  6179. method = ctx->s_cb.method();
  6180. }
  6181. else {
  6182. method = wolfSSLv23_server_method();
  6183. }
  6184. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  6185. ctx->s_cb.isSharedCtx = 0;
  6186. }
  6187. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  6188. #if defined(HAVE_SESSION_TICKET) && \
  6189. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6190. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6191. OpenSSLTicketInit();
  6192. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  6193. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6194. TicketInit();
  6195. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  6196. #endif
  6197. #endif
  6198. }
  6199. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  6200. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  6201. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  6202. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6203. if (ctx->s_cb.caPemFile != NULL)
  6204. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  6205. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  6206. else
  6207. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  6208. cliCertFile, 0), WOLFSSL_SUCCESS);
  6209. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6210. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  6211. #endif
  6212. if (ctx->s_cb.certPemFile != NULL) {
  6213. serverCertFile = ctx->s_cb.certPemFile;
  6214. }
  6215. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6216. if (!s_sharedCtx)
  6217. #endif
  6218. {
  6219. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  6220. serverCertFile), WOLFSSL_SUCCESS);
  6221. }
  6222. if (ctx->s_cb.keyPemFile != NULL) {
  6223. serverKeyFile = ctx->s_cb.keyPemFile;
  6224. }
  6225. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6226. if (!s_sharedCtx)
  6227. #endif
  6228. {
  6229. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  6230. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  6231. }
  6232. if (ctx->s_ciphers != NULL) {
  6233. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  6234. WOLFSSL_SUCCESS);
  6235. }
  6236. if (ctx->s_cb.ctx_ready != NULL) {
  6237. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  6238. }
  6239. /****************************
  6240. * Create WOLFSSL for client.
  6241. ****************************/
  6242. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  6243. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  6244. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  6245. if (0
  6246. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6247. || c_sharedCtx
  6248. #endif
  6249. )
  6250. {
  6251. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  6252. clientCertFile), WOLFSSL_SUCCESS);
  6253. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  6254. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  6255. }
  6256. if (ctx->c_cb.ssl_ready != NULL) {
  6257. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  6258. }
  6259. /****************************
  6260. * Create WOLFSSL for server.
  6261. ****************************/
  6262. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  6263. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  6264. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  6265. if (0
  6266. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6267. || s_sharedCtx
  6268. #endif
  6269. )
  6270. {
  6271. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  6272. serverCertFile), WOLFSSL_SUCCESS);
  6273. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  6274. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  6275. }
  6276. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6277. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6278. #elif !defined(NO_DH)
  6279. /* will repick suites with DHE, higher priority than PSK */
  6280. SetDH(ctx->s_ssl);
  6281. #endif
  6282. if (ctx->s_cb.ssl_ready != NULL) {
  6283. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  6284. }
  6285. return EXPECT_RESULT();
  6286. }
  6287. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  6288. int* rounds)
  6289. {
  6290. int handshake_complete = 0;
  6291. int hs_c = 0;
  6292. int hs_s = 0;
  6293. int failing_s = 0;
  6294. int failing_c = 0;
  6295. int ret;
  6296. int err;
  6297. if (rounds != NULL) {
  6298. *rounds = 0;
  6299. }
  6300. while ((!handshake_complete) && (max_rounds > 0)) {
  6301. if (!hs_c) {
  6302. wolfSSL_SetLoggingPrefix("client");
  6303. ret = wolfSSL_connect(ctx->c_ssl);
  6304. wolfSSL_SetLoggingPrefix(NULL);
  6305. if (ret == WOLFSSL_SUCCESS) {
  6306. hs_c = 1;
  6307. }
  6308. else {
  6309. err = wolfSSL_get_error(ctx->c_ssl, ret);
  6310. if (err != WOLFSSL_ERROR_WANT_READ &&
  6311. err != WOLFSSL_ERROR_WANT_WRITE) {
  6312. char buff[WOLFSSL_MAX_ERROR_SZ];
  6313. fprintf(stderr, "error = %d, %s\n", err,
  6314. wolfSSL_ERR_error_string(err, buff));
  6315. failing_c = 1;
  6316. hs_c = 1;
  6317. if (failing_c && failing_s) {
  6318. break;
  6319. }
  6320. }
  6321. }
  6322. }
  6323. if (!hs_s) {
  6324. wolfSSL_SetLoggingPrefix("server");
  6325. ret = wolfSSL_accept(ctx->s_ssl);
  6326. wolfSSL_SetLoggingPrefix(NULL);
  6327. if (ret == WOLFSSL_SUCCESS) {
  6328. hs_s = 1;
  6329. }
  6330. else {
  6331. err = wolfSSL_get_error(ctx->s_ssl, ret);
  6332. if (err != WOLFSSL_ERROR_WANT_READ &&
  6333. err != WOLFSSL_ERROR_WANT_WRITE) {
  6334. char buff[WOLFSSL_MAX_ERROR_SZ];
  6335. fprintf(stderr, "error = %d, %s\n", err,
  6336. wolfSSL_ERR_error_string(err, buff));
  6337. failing_s = 1;
  6338. hs_s = 1;
  6339. if (failing_c && failing_s) {
  6340. break;
  6341. }
  6342. }
  6343. }
  6344. }
  6345. handshake_complete = hs_c && hs_s;
  6346. max_rounds--;
  6347. if (rounds != NULL) {
  6348. *rounds += 1;
  6349. }
  6350. }
  6351. if (!handshake_complete || failing_c || failing_s) {
  6352. return TEST_FAIL;
  6353. }
  6354. return TEST_SUCCESS;
  6355. }
  6356. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  6357. {
  6358. EXPECT_DECLS;
  6359. char input[1024];
  6360. int idx = 0;
  6361. const char* msg_c = "hello wolfssl!";
  6362. int msglen_c = (int)XSTRLEN(msg_c);
  6363. const char* msg_s = "I hear you fa shizzle!";
  6364. int msglen_s = (int)XSTRLEN(msg_s);
  6365. if (ctx->c_msg != NULL) {
  6366. msg_c = ctx->c_msg;
  6367. msglen_c = ctx->c_msglen;
  6368. }
  6369. if (ctx->s_msg != NULL) {
  6370. msg_s = ctx->s_msg;
  6371. msglen_s = ctx->s_msglen;
  6372. }
  6373. wolfSSL_SetLoggingPrefix("client");
  6374. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  6375. wolfSSL_SetLoggingPrefix("server");
  6376. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  6377. if (idx >= 0) {
  6378. input[idx] = '\0';
  6379. }
  6380. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  6381. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  6382. ctx->s_cb.return_code = EXPECT_RESULT();
  6383. wolfSSL_SetLoggingPrefix("client");
  6384. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  6385. wolfSSL_SetLoggingPrefix(NULL);
  6386. if (idx >= 0) {
  6387. input[idx] = '\0';
  6388. }
  6389. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  6390. ctx->c_cb.return_code = EXPECT_RESULT();
  6391. if (ctx->c_cb.on_result != NULL) {
  6392. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  6393. }
  6394. if (ctx->s_cb.on_result != NULL) {
  6395. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  6396. }
  6397. return EXPECT_RESULT();
  6398. }
  6399. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  6400. {
  6401. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  6402. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  6403. if (ctx->c_cb.on_cleanup != NULL) {
  6404. ctx->c_cb.on_cleanup(ctx->c_ssl);
  6405. }
  6406. if (ctx->s_cb.on_cleanup != NULL) {
  6407. ctx->s_cb.on_cleanup(ctx->s_ssl);
  6408. }
  6409. wolfSSL_shutdown(ctx->s_ssl);
  6410. wolfSSL_shutdown(ctx->c_ssl);
  6411. wolfSSL_free(ctx->s_ssl);
  6412. wolfSSL_free(ctx->c_ssl);
  6413. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  6414. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  6415. }
  6416. if (!ctx->c_cb.isSharedCtx) {
  6417. wolfSSL_CTX_free(ctx->c_ctx);
  6418. ctx->c_ctx = NULL;
  6419. }
  6420. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  6421. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  6422. }
  6423. if (!ctx->s_cb.isSharedCtx) {
  6424. wolfSSL_CTX_free(ctx->s_ctx);
  6425. ctx->s_ctx = NULL;
  6426. }
  6427. if (!ctx->s_cb.ticNoInit) {
  6428. #if defined(HAVE_SESSION_TICKET) && \
  6429. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6430. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6431. OpenSSLTicketCleanup();
  6432. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6433. TicketCleanup();
  6434. #endif
  6435. #endif
  6436. }
  6437. }
  6438. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  6439. test_ssl_cbf* server_cb, cbType client_on_handshake)
  6440. {
  6441. EXPECT_DECLS;
  6442. struct test_ssl_memio_ctx test_ctx;
  6443. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6444. size_t msg_len;
  6445. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6446. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  6447. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  6448. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  6449. test_ctx.c_ctx = client_cb->ctx;
  6450. test_ctx.s_ctx = server_cb->ctx;
  6451. test_ctx.c_cb.return_code = TEST_FAIL;
  6452. test_ctx.s_cb.return_code = TEST_FAIL;
  6453. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  6454. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  6455. if (client_on_handshake != NULL) {
  6456. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  6457. TEST_SUCCESS);
  6458. }
  6459. if (client_cb->on_handshake != NULL) {
  6460. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  6461. TEST_SUCCESS);
  6462. }
  6463. if (server_cb->on_handshake != NULL) {
  6464. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  6465. TEST_SUCCESS);
  6466. }
  6467. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6468. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6469. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  6470. MD_MAX_SIZE);
  6471. ExpectIntGE(msg_len, 0);
  6472. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6473. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  6474. MD_MAX_SIZE);
  6475. ExpectIntGE(msg_len, 0);
  6476. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6477. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  6478. test_ssl_memio_cleanup(&test_ctx);
  6479. client_cb->return_code = test_ctx.c_cb.return_code;
  6480. client_cb->last_err = test_ctx.c_cb.last_err;
  6481. server_cb->return_code = test_ctx.s_cb.return_code;
  6482. server_cb->last_err = test_ctx.s_cb.last_err;
  6483. return EXPECT_RESULT();
  6484. }
  6485. #endif
  6486. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  6487. #ifdef WOLFSSL_SESSION_EXPORT
  6488. #ifdef WOLFSSL_DTLS
  6489. /* set up function for sending session information */
  6490. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  6491. {
  6492. WOLFSSL_CTX* ctx = NULL;
  6493. WOLFSSL* ssl = NULL;
  6494. AssertNotNull(inSsl);
  6495. AssertNotNull(buf);
  6496. AssertIntNE(0, sz);
  6497. /* Set ctx to DTLS 1.2 */
  6498. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  6499. AssertNotNull(ctx);
  6500. ssl = wolfSSL_new(ctx);
  6501. AssertNotNull(ssl);
  6502. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  6503. wolfSSL_free(ssl);
  6504. wolfSSL_CTX_free(ctx);
  6505. (void)userCtx;
  6506. return 0;
  6507. }
  6508. #endif
  6509. /* returns negative value on fail and positive (including 0) on success */
  6510. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  6511. {
  6512. int ret, err, loop_count, count, timeout = 10;
  6513. char msg[] = "I hear you fa shizzle!";
  6514. char input[1024];
  6515. loop_count = ((func_args*)args)->argc;
  6516. #ifdef WOLFSSL_ASYNC_CRYPT
  6517. err = 0; /* Reset error */
  6518. #endif
  6519. do {
  6520. #ifdef WOLFSSL_ASYNC_CRYPT
  6521. if (err == WC_PENDING_E) {
  6522. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6523. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6524. }
  6525. #endif
  6526. ret = wolfSSL_accept(ssl);
  6527. err = wolfSSL_get_error(ssl, 0);
  6528. if (err == WOLFSSL_ERROR_WANT_READ ||
  6529. err == WOLFSSL_ERROR_WANT_WRITE) {
  6530. int select_ret;
  6531. err = WC_PENDING_E;
  6532. select_ret = tcp_select(*sockfd, timeout);
  6533. if (select_ret == TEST_TIMEOUT) {
  6534. return WOLFSSL_FATAL_ERROR;
  6535. }
  6536. }
  6537. } while (err == WC_PENDING_E);
  6538. if (ret != WOLFSSL_SUCCESS) {
  6539. char buff[WOLFSSL_MAX_ERROR_SZ];
  6540. fprintf(stderr, "error = %d, %s\n", err,
  6541. wolfSSL_ERR_error_string(err, buff));
  6542. return ret;
  6543. }
  6544. for (count = 0; count < loop_count; count++) {
  6545. int select_ret;
  6546. select_ret = tcp_select(*sockfd, timeout);
  6547. if (select_ret == TEST_TIMEOUT) {
  6548. ret = WOLFSSL_FATAL_ERROR;
  6549. break;
  6550. }
  6551. do {
  6552. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6553. if (ret > 0) {
  6554. input[ret] = '\0';
  6555. fprintf(stderr, "Client message: %s\n", input);
  6556. }
  6557. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6558. do {
  6559. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  6560. return WOLFSSL_FATAL_ERROR;
  6561. }
  6562. err = wolfSSL_get_error(ssl, ret);
  6563. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6564. }
  6565. return ret;
  6566. }
  6567. #endif /* WOLFSSL_SESSION_EXPORT */
  6568. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  6569. {
  6570. SOCKET_T sockfd = 0;
  6571. SOCKET_T clientfd = 0;
  6572. word16 port;
  6573. callback_functions* cbf;
  6574. WOLFSSL_CTX* ctx = NULL;
  6575. WOLFSSL* ssl = NULL;
  6576. func_args* opts = (func_args*)args;
  6577. char msg[] = "I hear you fa shizzle!";
  6578. char input[1024];
  6579. int idx;
  6580. int ret, err = 0;
  6581. int sharedCtx = 0;
  6582. int doUdp = 0;
  6583. SOCKADDR_IN_T cliAddr;
  6584. socklen_t cliLen;
  6585. const char* certFile = svrCertFile;
  6586. const char* keyFile = svrKeyFile;
  6587. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6588. size_t msg_len = 0;
  6589. #endif
  6590. wolfSSL_SetLoggingPrefix("server");
  6591. #ifdef WOLFSSL_TIRTOS
  6592. fdOpenSession(Task_self());
  6593. #endif
  6594. opts->return_code = TEST_FAIL;
  6595. cbf = opts->callbacks;
  6596. if (cbf != NULL && cbf->ctx) {
  6597. ctx = cbf->ctx;
  6598. sharedCtx = 1;
  6599. }
  6600. else
  6601. {
  6602. WOLFSSL_METHOD* method = NULL;
  6603. if (cbf != NULL && cbf->method != NULL) {
  6604. method = cbf->method();
  6605. }
  6606. else {
  6607. method = wolfSSLv23_server_method();
  6608. }
  6609. ctx = wolfSSL_CTX_new(method);
  6610. }
  6611. if (ctx == NULL) {
  6612. /* Release the wait for TCP ready. */
  6613. signal_ready(opts->signal);
  6614. goto done;
  6615. }
  6616. if (cbf == NULL || !cbf->ticNoInit) {
  6617. #if defined(HAVE_SESSION_TICKET) && \
  6618. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6619. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6620. OpenSSLTicketInit();
  6621. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  6622. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6623. TicketInit();
  6624. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  6625. #endif
  6626. #endif
  6627. }
  6628. #if defined(USE_WINDOWS_API)
  6629. port = opts->signal->port;
  6630. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6631. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6632. /* Let tcp_listen assign port */
  6633. port = 0;
  6634. #else
  6635. /* Use default port */
  6636. port = wolfSSLPort;
  6637. #endif
  6638. if (cbf != NULL)
  6639. doUdp = cbf->doUdp;
  6640. /* do it here to detect failure */
  6641. tcp_accept(
  6642. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  6643. if (doUdp) {
  6644. cliLen = sizeof(cliAddr);
  6645. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  6646. (struct sockaddr*)&cliAddr, &cliLen);
  6647. AssertIntGT(idx, 0);
  6648. }
  6649. else {
  6650. CloseSocket(sockfd);
  6651. }
  6652. wolfSSL_CTX_set_verify(ctx,
  6653. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6654. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6655. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6656. #endif
  6657. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6658. != WOLFSSL_SUCCESS) {
  6659. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6660. goto done;
  6661. }
  6662. if (cbf != NULL && cbf->certPemFile != NULL)
  6663. certFile = cbf->certPemFile;
  6664. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6665. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6666. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6667. #else
  6668. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6669. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6670. #endif
  6671. /*err_sys("can't load server cert chain file, "
  6672. "Please run from wolfSSL home dir");*/
  6673. goto done;
  6674. }
  6675. if (cbf != NULL && cbf->keyPemFile != NULL)
  6676. keyFile = cbf->keyPemFile;
  6677. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6678. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6679. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6680. #else
  6681. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6682. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6683. #endif
  6684. /*err_sys("can't load server key file, "
  6685. "Please run from wolfSSL home dir");*/
  6686. goto done;
  6687. }
  6688. #ifdef HAVE_CRL
  6689. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6690. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6691. goto done;
  6692. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6693. != WOLFSSL_SUCCESS)
  6694. goto done;
  6695. }
  6696. #endif
  6697. /* call ctx setup callback */
  6698. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6699. cbf->ctx_ready(ctx);
  6700. }
  6701. ssl = wolfSSL_new(ctx);
  6702. if (ssl == NULL) {
  6703. goto done;
  6704. }
  6705. if (doUdp) {
  6706. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6707. if (err != WOLFSSL_SUCCESS)
  6708. goto done;
  6709. }
  6710. #ifdef WOLFSSL_SESSION_EXPORT
  6711. /* only add in more complex nonblocking case with session export tests */
  6712. if (args && opts->argc > 0) {
  6713. /* set as nonblock and time out for waiting on read/write */
  6714. tcp_set_nonblocking(&clientfd);
  6715. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  6716. }
  6717. #endif
  6718. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6719. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  6720. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6721. #else
  6722. if (wolfSSL_use_certificate_file(ssl, certFile,
  6723. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6724. #endif
  6725. /*err_sys("can't load server cert chain file, "
  6726. "Please run from wolfSSL home dir");*/
  6727. goto done;
  6728. }
  6729. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6730. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6731. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6732. #else
  6733. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6734. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6735. #endif
  6736. /*err_sys("can't load server key file, "
  6737. "Please run from wolfSSL home dir");*/
  6738. goto done;
  6739. }
  6740. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6741. /*err_sys("SSL_set_fd failed");*/
  6742. goto done;
  6743. }
  6744. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6745. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6746. #elif !defined(NO_DH)
  6747. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6748. #endif
  6749. /* call ssl setup callback */
  6750. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6751. cbf->ssl_ready(ssl);
  6752. }
  6753. #ifdef WOLFSSL_SESSION_EXPORT
  6754. /* only add in more complex nonblocking case with session export tests */
  6755. if (opts->argc > 0) {
  6756. ret = nonblocking_accept_read(args, ssl, &clientfd);
  6757. if (ret >= 0) {
  6758. opts->return_code = TEST_SUCCESS;
  6759. }
  6760. #ifdef WOLFSSL_TIRTOS
  6761. Task_yield();
  6762. #endif
  6763. goto done;
  6764. }
  6765. #endif
  6766. #ifdef WOLFSSL_ASYNC_CRYPT
  6767. err = 0; /* Reset error */
  6768. #endif
  6769. do {
  6770. #ifdef WOLFSSL_ASYNC_CRYPT
  6771. if (err == WC_PENDING_E) {
  6772. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6773. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6774. }
  6775. #endif
  6776. ret = wolfSSL_negotiate(ssl);
  6777. err = wolfSSL_get_error(ssl, 0);
  6778. } while (err == WC_PENDING_E);
  6779. if (ret != WOLFSSL_SUCCESS) {
  6780. char buff[WOLFSSL_MAX_ERROR_SZ];
  6781. fprintf(stderr, "error = %d, %s\n", err,
  6782. wolfSSL_ERR_error_string(err, buff));
  6783. /*err_sys("SSL_accept failed");*/
  6784. goto done;
  6785. }
  6786. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6787. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6788. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  6789. AssertIntGE(msg_len, 0);
  6790. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6791. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  6792. AssertIntGE(msg_len, 0);
  6793. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6794. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6795. if (idx > 0) {
  6796. input[idx] = '\0';
  6797. fprintf(stderr, "Client message: %s\n", input);
  6798. }
  6799. else if (idx < 0) {
  6800. goto done;
  6801. }
  6802. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6803. /*err_sys("SSL_write failed");*/
  6804. goto done;
  6805. }
  6806. if (cbf != NULL && cbf->on_result != NULL)
  6807. cbf->on_result(ssl);
  6808. #ifdef WOLFSSL_TIRTOS
  6809. Task_yield();
  6810. #endif
  6811. opts->return_code = TEST_SUCCESS;
  6812. done:
  6813. if (cbf != NULL)
  6814. cbf->last_err = err;
  6815. if (cbf != NULL && cbf->on_cleanup != NULL)
  6816. cbf->on_cleanup(ssl);
  6817. wolfSSL_shutdown(ssl);
  6818. wolfSSL_free(ssl);
  6819. if (!sharedCtx)
  6820. wolfSSL_CTX_free(ctx);
  6821. CloseSocket(clientfd);
  6822. #ifdef WOLFSSL_TIRTOS
  6823. fdCloseSession(Task_self());
  6824. #endif
  6825. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6826. && defined(HAVE_THREAD_LS)
  6827. wc_ecc_fp_free(); /* free per thread cache */
  6828. #endif
  6829. if (cbf == NULL || !cbf->ticNoInit) {
  6830. #if defined(HAVE_SESSION_TICKET) && \
  6831. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6832. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6833. OpenSSLTicketCleanup();
  6834. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6835. TicketCleanup();
  6836. #endif
  6837. #endif
  6838. }
  6839. wolfSSL_SetLoggingPrefix(NULL);
  6840. WOLFSSL_RETURN_FROM_THREAD(0);
  6841. }
  6842. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6843. !defined(WOLFSSL_NO_TLS12)
  6844. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6845. {
  6846. SOCKET_T sockfd;
  6847. SOCKET_T clientfd = -1;
  6848. word16 port;
  6849. callback_functions* cbf;
  6850. WOLFSSL_CTX* ctx = 0;
  6851. WOLFSSL* ssl = 0;
  6852. char msg[] = "I hear you fa shizzle!";
  6853. char input[1024];
  6854. int idx;
  6855. int ret, err = 0;
  6856. int sharedCtx = 0;
  6857. func_args* opts = (func_args*)args;
  6858. int loop_count = opts->argc;
  6859. int count = 0;
  6860. #ifdef WOLFSSL_TIRTOS
  6861. fdOpenSession(Task_self());
  6862. #endif
  6863. opts->return_code = TEST_FAIL;
  6864. cbf = opts->callbacks;
  6865. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6866. if (cbf != NULL && cbf->ctx) {
  6867. ctx = cbf->ctx;
  6868. sharedCtx = 1;
  6869. }
  6870. else
  6871. #endif
  6872. {
  6873. WOLFSSL_METHOD* method = NULL;
  6874. if (cbf != NULL && cbf->method != NULL) {
  6875. method = cbf->method();
  6876. }
  6877. else {
  6878. method = wolfSSLv23_server_method();
  6879. }
  6880. ctx = wolfSSL_CTX_new(method);
  6881. }
  6882. #if defined(USE_WINDOWS_API)
  6883. port = opts->signal->port;
  6884. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6885. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6886. /* Let tcp_listen assign port */
  6887. port = 0;
  6888. #else
  6889. /* Use default port */
  6890. port = wolfSSLPort;
  6891. #endif
  6892. wolfSSL_CTX_set_verify(ctx,
  6893. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6894. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6895. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6896. #endif
  6897. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6898. != WOLFSSL_SUCCESS) {
  6899. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6900. /* Release the wait for TCP ready. */
  6901. signal_ready(opts->signal);
  6902. goto done;
  6903. }
  6904. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  6905. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6906. /*err_sys("can't load server cert chain file, "
  6907. "Please run from wolfSSL home dir");*/
  6908. /* Release the wait for TCP ready. */
  6909. signal_ready(opts->signal);
  6910. goto done;
  6911. }
  6912. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  6913. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6914. /*err_sys("can't load server key file, "
  6915. "Please run from wolfSSL home dir");*/
  6916. /* Release the wait for TCP ready. */
  6917. signal_ready(opts->signal);
  6918. goto done;
  6919. }
  6920. /* call ctx setup callback */
  6921. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6922. cbf->ctx_ready(ctx);
  6923. }
  6924. while (count != loop_count) {
  6925. ssl = wolfSSL_new(ctx);
  6926. if (ssl == NULL) {
  6927. signal_ready(opts->signal);
  6928. goto done;
  6929. }
  6930. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6931. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6932. /*err_sys("can't load server cert chain file, "
  6933. "Please run from wolfSSL home dir");*/
  6934. /* Release the wait for TCP ready. */
  6935. signal_ready(opts->signal);
  6936. goto done;
  6937. }
  6938. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6939. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6940. /*err_sys("can't load server key file, "
  6941. "Please run from wolfSSL home dir");*/
  6942. /* Release the wait for TCP ready. */
  6943. signal_ready(opts->signal);
  6944. goto done;
  6945. }
  6946. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6947. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6948. #elif !defined(NO_DH)
  6949. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6950. #endif
  6951. /* call ssl setup callback */
  6952. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6953. cbf->ssl_ready(ssl);
  6954. }
  6955. /* do it here to detect failure */
  6956. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6957. 0);
  6958. CloseSocket(sockfd);
  6959. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6960. /*err_sys("SSL_set_fd failed");*/
  6961. goto done;
  6962. }
  6963. #ifdef WOLFSSL_ASYNC_CRYPT
  6964. err = 0; /* Reset error */
  6965. #endif
  6966. do {
  6967. #ifdef WOLFSSL_ASYNC_CRYPT
  6968. if (err == WC_PENDING_E) {
  6969. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6970. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6971. }
  6972. #endif
  6973. ret = wolfSSL_accept(ssl);
  6974. err = wolfSSL_get_error(ssl, 0);
  6975. } while (err == WC_PENDING_E);
  6976. if (ret != WOLFSSL_SUCCESS) {
  6977. char buff[WOLFSSL_MAX_ERROR_SZ];
  6978. fprintf(stderr, "error = %d, %s\n", err,
  6979. wolfSSL_ERR_error_string(err, buff));
  6980. /*err_sys("SSL_accept failed");*/
  6981. goto done;
  6982. }
  6983. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6984. if (idx > 0) {
  6985. input[idx] = '\0';
  6986. fprintf(stderr, "Client message: %s\n", input);
  6987. }
  6988. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6989. /*err_sys("SSL_write failed");*/
  6990. goto done;
  6991. }
  6992. /* free ssl for this connection */
  6993. wolfSSL_shutdown(ssl);
  6994. wolfSSL_free(ssl); ssl = NULL;
  6995. CloseSocket(clientfd);
  6996. clientfd = -1;
  6997. count++;
  6998. }
  6999. #ifdef WOLFSSL_TIRTOS
  7000. Task_yield();
  7001. #endif
  7002. opts->return_code = TEST_SUCCESS;
  7003. done:
  7004. if (ssl != NULL) {
  7005. wolfSSL_shutdown(ssl);
  7006. wolfSSL_free(ssl);
  7007. }
  7008. if (!sharedCtx)
  7009. wolfSSL_CTX_free(ctx);
  7010. if (clientfd >= 0)
  7011. CloseSocket(clientfd);
  7012. #ifdef WOLFSSL_TIRTOS
  7013. fdCloseSession(Task_self());
  7014. #endif
  7015. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7016. && defined(HAVE_THREAD_LS)
  7017. wc_ecc_fp_free(); /* free per thread cache */
  7018. #endif
  7019. WOLFSSL_RETURN_FROM_THREAD(0);
  7020. }
  7021. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  7022. static int test_client_nofail(void* args, cbType cb)
  7023. {
  7024. #if !defined(NO_WOLFSSL_CLIENT)
  7025. SOCKET_T sockfd = 0;
  7026. callback_functions* cbf;
  7027. WOLFSSL_CTX* ctx = 0;
  7028. WOLFSSL* ssl = 0;
  7029. WOLFSSL_CIPHER* cipher;
  7030. char msg[64] = "hello wolfssl!";
  7031. char reply[1024];
  7032. int input;
  7033. int msgSz = (int)XSTRLEN(msg);
  7034. int ret, err = 0;
  7035. int cipherSuite;
  7036. int sharedCtx = 0;
  7037. int doUdp = 0;
  7038. const char* cipherName1, *cipherName2;
  7039. wolfSSL_SetLoggingPrefix("client");
  7040. #ifdef WOLFSSL_TIRTOS
  7041. fdOpenSession(Task_self());
  7042. #endif
  7043. ((func_args*)args)->return_code = TEST_FAIL;
  7044. cbf = ((func_args*)args)->callbacks;
  7045. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  7046. if (cbf != NULL && cbf->ctx) {
  7047. ctx = cbf->ctx;
  7048. sharedCtx = cbf->isSharedCtx;
  7049. }
  7050. else
  7051. #endif
  7052. {
  7053. WOLFSSL_METHOD* method = NULL;
  7054. if (cbf != NULL && cbf->method != NULL) {
  7055. method = cbf->method();
  7056. }
  7057. else {
  7058. method = wolfSSLv23_client_method();
  7059. }
  7060. ctx = wolfSSL_CTX_new(method);
  7061. }
  7062. if (cbf != NULL)
  7063. doUdp = cbf->doUdp;
  7064. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7065. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7066. #endif
  7067. /* Do connect here so server detects failures */
  7068. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7069. doUdp, 0, NULL);
  7070. /* Connect the socket so that we don't have to set the peer later on */
  7071. if (doUdp)
  7072. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  7073. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  7074. {
  7075. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  7076. goto done;
  7077. }
  7078. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  7079. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  7080. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7081. #else
  7082. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  7083. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7084. #endif
  7085. /*err_sys("can't load client cert file, "
  7086. "Please run from wolfSSL home dir");*/
  7087. goto done;
  7088. }
  7089. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  7090. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  7091. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7092. #else
  7093. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  7094. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7095. #endif
  7096. /*err_sys("can't load client key file, "
  7097. "Please run from wolfSSL home dir");*/
  7098. goto done;
  7099. }
  7100. #ifdef HAVE_CRL
  7101. if (cbf != NULL && cbf->crlPemFile != NULL) {
  7102. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  7103. goto done;
  7104. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  7105. != WOLFSSL_SUCCESS)
  7106. goto done;
  7107. }
  7108. #endif
  7109. /* call ctx setup callback */
  7110. if (cbf != NULL && cbf->ctx_ready != NULL) {
  7111. cbf->ctx_ready(ctx);
  7112. }
  7113. ssl = wolfSSL_new(ctx);
  7114. if (ssl == NULL) {
  7115. goto done;
  7116. }
  7117. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  7118. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  7119. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7120. #else
  7121. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  7122. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7123. #endif
  7124. /*err_sys("can't load client cert file, "
  7125. "Please run from wolfSSL home dir");*/
  7126. goto done;
  7127. }
  7128. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  7129. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  7130. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7131. #else
  7132. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  7133. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7134. #endif
  7135. /*err_sys("can't load client key file, "
  7136. "Please run from wolfSSL home dir");*/
  7137. goto done;
  7138. }
  7139. if (!doUdp) {
  7140. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7141. /*err_sys("SSL_set_fd failed");*/
  7142. goto done;
  7143. }
  7144. }
  7145. else {
  7146. #ifdef WOLFSSL_DTLS
  7147. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7148. /*err_sys("SSL_set_fd failed");*/
  7149. goto done;
  7150. }
  7151. #else
  7152. goto done;
  7153. #endif
  7154. }
  7155. /* call ssl setup callback */
  7156. if (cbf != NULL && cbf->ssl_ready != NULL) {
  7157. cbf->ssl_ready(ssl);
  7158. }
  7159. #ifdef WOLFSSL_ASYNC_CRYPT
  7160. err = 0; /* Reset error */
  7161. #endif
  7162. do {
  7163. #ifdef WOLFSSL_ASYNC_CRYPT
  7164. if (err == WC_PENDING_E) {
  7165. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7166. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7167. }
  7168. #endif
  7169. ret = wolfSSL_negotiate(ssl);
  7170. err = wolfSSL_get_error(ssl, 0);
  7171. } while (err == WC_PENDING_E);
  7172. if (ret != WOLFSSL_SUCCESS) {
  7173. char buff[WOLFSSL_MAX_ERROR_SZ];
  7174. fprintf(stderr, "error = %d, %s\n", err,
  7175. wolfSSL_ERR_error_string(err, buff));
  7176. /*err_sys("SSL_connect failed");*/
  7177. goto done;
  7178. }
  7179. /* test the various get cipher methods */
  7180. /* Internal cipher suite names */
  7181. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  7182. cipherName1 = wolfSSL_get_cipher_name(ssl);
  7183. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  7184. (cipherSuite >> 8), cipherSuite & 0xFF);
  7185. AssertStrEQ(cipherName1, cipherName2);
  7186. /* IANA Cipher Suites Names */
  7187. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  7188. then it's the internal cipher suite name */
  7189. cipher = wolfSSL_get_current_cipher(ssl);
  7190. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  7191. cipherName2 = wolfSSL_get_cipher(ssl);
  7192. AssertStrEQ(cipherName1, cipherName2);
  7193. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  7194. !defined(WOLFSSL_QT)
  7195. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  7196. (cipherSuite >> 8), cipherSuite & 0xFF);
  7197. AssertStrEQ(cipherName1, cipherName2);
  7198. #endif
  7199. if (cb != NULL)
  7200. (cb)(ctx, ssl);
  7201. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7202. /*err_sys("SSL_write failed");*/
  7203. goto done;
  7204. }
  7205. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7206. if (input > 0) {
  7207. reply[input] = '\0';
  7208. fprintf(stderr, "Server response: %s\n", reply);
  7209. }
  7210. if (cbf != NULL && cbf->on_result != NULL)
  7211. cbf->on_result(ssl);
  7212. ((func_args*)args)->return_code = TEST_SUCCESS;
  7213. done:
  7214. if (cbf != NULL)
  7215. cbf->last_err = err;
  7216. if (cbf != NULL && cbf->on_cleanup != NULL)
  7217. cbf->on_cleanup(ssl);
  7218. wolfSSL_free(ssl);
  7219. if (!sharedCtx)
  7220. wolfSSL_CTX_free(ctx);
  7221. CloseSocket(sockfd);
  7222. #ifdef WOLFSSL_TIRTOS
  7223. fdCloseSession(Task_self());
  7224. #endif
  7225. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7226. && defined(HAVE_THREAD_LS)
  7227. wc_ecc_fp_free(); /* free per thread cache */
  7228. #endif
  7229. #else
  7230. (void)args;
  7231. (void)cb;
  7232. #endif /* !NO_WOLFSSL_CLIENT */
  7233. wolfSSL_SetLoggingPrefix(NULL);
  7234. return 0;
  7235. }
  7236. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  7237. callback_functions* server_cb, cbType client_on_handshake)
  7238. {
  7239. func_args client_args;
  7240. func_args server_args;
  7241. tcp_ready ready;
  7242. THREAD_TYPE serverThread;
  7243. XMEMSET(&client_args, 0, sizeof(func_args));
  7244. XMEMSET(&server_args, 0, sizeof(func_args));
  7245. #ifdef WOLFSSL_TIRTOS
  7246. fdOpenSession(Task_self());
  7247. #endif
  7248. StartTCP();
  7249. InitTcpReady(&ready);
  7250. #if defined(USE_WINDOWS_API)
  7251. /* use RNG to get random port if using windows */
  7252. ready.port = GetRandomPort();
  7253. #endif
  7254. server_args.signal = &ready;
  7255. server_args.callbacks = server_cb;
  7256. client_args.signal = &ready;
  7257. client_args.callbacks = client_cb;
  7258. start_thread(test_server_nofail, &server_args, &serverThread);
  7259. wait_tcp_ready(&server_args);
  7260. test_client_nofail(&client_args, client_on_handshake);
  7261. join_thread(serverThread);
  7262. client_cb->return_code = client_args.return_code;
  7263. server_cb->return_code = server_args.return_code;
  7264. FreeTcpReady(&ready);
  7265. #ifdef WOLFSSL_TIRTOS
  7266. fdOpenSession(Task_self());
  7267. #endif
  7268. }
  7269. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  7270. callback_functions* server_cb)
  7271. {
  7272. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  7273. }
  7274. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7275. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  7276. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  7277. void* server_args)
  7278. {
  7279. SOCKET_T sockfd = 0;
  7280. callback_functions* cbf;
  7281. WOLFSSL_CTX* ctx = 0;
  7282. WOLFSSL* ssl = 0;
  7283. WOLFSSL_SESSION* session = NULL;
  7284. char msg[64] = "hello wolfssl!";
  7285. char reply[1024];
  7286. int input;
  7287. int msgSz = (int)XSTRLEN(msg);
  7288. int ret, err = 0;
  7289. int sharedCtx = 0;
  7290. #ifdef WOLFSSL_TIRTOS
  7291. fdOpenSession(Task_self());
  7292. #endif
  7293. ((func_args*)args)->return_code = TEST_FAIL;
  7294. cbf = ((func_args*)args)->callbacks;
  7295. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  7296. if (cbf != NULL && cbf->ctx) {
  7297. ctx = cbf->ctx;
  7298. sharedCtx = 1;
  7299. }
  7300. else
  7301. #endif
  7302. {
  7303. WOLFSSL_METHOD* method = NULL;
  7304. if (cbf != NULL && cbf->method != NULL) {
  7305. method = cbf->method();
  7306. }
  7307. else {
  7308. method = wolfSSLv23_client_method();
  7309. }
  7310. ctx = wolfSSL_CTX_new(method);
  7311. }
  7312. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7313. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7314. #endif
  7315. /* Do connect here so server detects failures */
  7316. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7317. 0, 0, NULL);
  7318. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  7319. WOLFSSL_SUCCESS) {
  7320. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  7321. goto done;
  7322. }
  7323. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  7324. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7325. /*err_sys("can't load client cert file, "
  7326. "Please run from wolfSSL home dir");*/
  7327. goto done;
  7328. }
  7329. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  7330. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7331. /*err_sys("can't load client key file, "
  7332. "Please run from wolfSSL home dir");*/
  7333. goto done;
  7334. }
  7335. /* call ctx setup callback */
  7336. if (cbf != NULL && cbf->ctx_ready != NULL) {
  7337. cbf->ctx_ready(ctx);
  7338. }
  7339. ssl = wolfSSL_new(ctx);
  7340. if (ssl == NULL) {
  7341. goto done;
  7342. }
  7343. /* keep handshake resources for re-using WOLFSSL obj */
  7344. wolfSSL_KeepArrays(ssl);
  7345. if (wolfSSL_KeepHandshakeResources(ssl)) {
  7346. /* err_sys("SSL_KeepHandshakeResources failed"); */
  7347. goto done;
  7348. }
  7349. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  7350. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7351. /*err_sys("can't load client cert file, "
  7352. "Please run from wolfSSL home dir");*/
  7353. goto done;
  7354. }
  7355. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  7356. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7357. /*err_sys("can't load client key file, "
  7358. "Please run from wolfSSL home dir");*/
  7359. goto done;
  7360. }
  7361. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7362. /*err_sys("SSL_set_fd failed");*/
  7363. goto done;
  7364. }
  7365. /* call ssl setup callback */
  7366. if (cbf != NULL && cbf->ssl_ready != NULL) {
  7367. cbf->ssl_ready(ssl);
  7368. }
  7369. #ifdef WOLFSSL_ASYNC_CRYPT
  7370. err = 0; /* Reset error */
  7371. #endif
  7372. do {
  7373. #ifdef WOLFSSL_ASYNC_CRYPT
  7374. if (err == WC_PENDING_E) {
  7375. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7376. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7377. }
  7378. #endif
  7379. ret = wolfSSL_connect(ssl);
  7380. err = wolfSSL_get_error(ssl, 0);
  7381. } while (err == WC_PENDING_E);
  7382. if (ret != WOLFSSL_SUCCESS) {
  7383. char buff[WOLFSSL_MAX_ERROR_SZ];
  7384. fprintf(stderr, "error = %d, %s\n", err,
  7385. wolfSSL_ERR_error_string(err, buff));
  7386. /*err_sys("SSL_connect failed");*/
  7387. goto done;
  7388. }
  7389. /* Build first session */
  7390. if (cb != NULL)
  7391. cb(ctx, ssl);
  7392. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7393. /*err_sys("SSL_write failed");*/
  7394. goto done;
  7395. }
  7396. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7397. if (input > 0) {
  7398. reply[input] = '\0';
  7399. fprintf(stderr, "Server response: %s\n", reply);
  7400. }
  7401. /* Session Resumption by re-using WOLFSSL object */
  7402. wolfSSL_set_quiet_shutdown(ssl, 1);
  7403. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  7404. /* err_sys ("SSL shutdown failed"); */
  7405. goto done;
  7406. }
  7407. session = wolfSSL_get1_session(ssl);
  7408. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  7409. wolfSSL_SESSION_free(session);
  7410. /* err_sys ("SSL_clear failed"); */
  7411. goto done;
  7412. }
  7413. wolfSSL_set_session(ssl, session);
  7414. wolfSSL_SESSION_free(session);
  7415. session = NULL;
  7416. /* close socket once */
  7417. CloseSocket(sockfd);
  7418. sockfd = 0;
  7419. /* wait until server ready */
  7420. wait_tcp_ready((func_args*)server_args);
  7421. fprintf(stderr, "session resumption\n");
  7422. /* Do re-connect */
  7423. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7424. 0, 0, NULL);
  7425. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7426. /*err_sys("SSL_set_fd failed");*/
  7427. goto done;
  7428. }
  7429. #ifdef WOLFSSL_ASYNC_CRYPT
  7430. err = 0; /* Reset error */
  7431. #endif
  7432. do {
  7433. #ifdef WOLFSSL_ASYNC_CRYPT
  7434. if (err == WC_PENDING_E) {
  7435. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7436. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7437. }
  7438. #endif
  7439. ret = wolfSSL_connect(ssl);
  7440. err = wolfSSL_get_error(ssl, 0);
  7441. } while (err == WC_PENDING_E);
  7442. if (ret != WOLFSSL_SUCCESS) {
  7443. char buff[WOLFSSL_MAX_ERROR_SZ];
  7444. fprintf(stderr, "error = %d, %s\n", err,
  7445. wolfSSL_ERR_error_string(err, buff));
  7446. /*err_sys("SSL_connect failed");*/
  7447. goto done;
  7448. }
  7449. /* Build first session */
  7450. if (cb != NULL)
  7451. cb(ctx, ssl);
  7452. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7453. /*err_sys("SSL_write failed");*/
  7454. goto done;
  7455. }
  7456. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7457. if (input > 0) {
  7458. reply[input] = '\0';
  7459. fprintf(stderr, "Server response: %s\n", reply);
  7460. }
  7461. ((func_args*)args)->return_code = TEST_SUCCESS;
  7462. done:
  7463. wolfSSL_free(ssl);
  7464. if (!sharedCtx)
  7465. wolfSSL_CTX_free(ctx);
  7466. CloseSocket(sockfd);
  7467. #ifdef WOLFSSL_TIRTOS
  7468. fdCloseSession(Task_self());
  7469. #endif
  7470. return;
  7471. }
  7472. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7473. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  7474. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  7475. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  7476. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  7477. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  7478. #define HAVE_ALPN_PROTOS_SUPPORT
  7479. #endif
  7480. /* Generic TLS client / server with callbacks for API unit tests
  7481. * Used by SNI / ALPN / crypto callback helper functions */
  7482. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  7483. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  7484. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  7485. #define ENABLE_TLS_CALLBACK_TEST
  7486. #endif
  7487. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  7488. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  7489. /* TLS server for API unit testing - generic */
  7490. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  7491. {
  7492. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7493. WOLFSSL_CTX* ctx = NULL;
  7494. WOLFSSL* ssl = NULL;
  7495. SOCKET_T sfd = 0;
  7496. SOCKET_T cfd = 0;
  7497. word16 port;
  7498. char msg[] = "I hear you fa shizzle!";
  7499. int len = (int) XSTRLEN(msg);
  7500. char input[1024];
  7501. int idx;
  7502. int ret, err = 0;
  7503. ((func_args*)args)->return_code = TEST_FAIL;
  7504. #if defined(USE_WINDOWS_API)
  7505. port = ((func_args*)args)->signal->port;
  7506. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7507. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7508. /* Let tcp_listen assign port */
  7509. port = 0;
  7510. #else
  7511. /* Use default port */
  7512. port = wolfSSLPort;
  7513. #endif
  7514. #ifdef WOLFSSL_DTLS
  7515. if (callbacks->method == wolfDTLS_server_method
  7516. #ifdef WOLFSSL_STATIC_MEMORY
  7517. || callbacks->method_ex == wolfDTLS_server_method_ex
  7518. #endif
  7519. #ifndef NO_OLD_TLS
  7520. || callbacks->method == wolfDTLSv1_server_method
  7521. #ifdef WOLFSSL_STATIC_MEMORY
  7522. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  7523. #endif
  7524. #endif
  7525. #ifndef WOLFSSL_NO_TLS12
  7526. || callbacks->method == wolfDTLSv1_2_server_method
  7527. #ifdef WOLFSSL_STATIC_MEMORY
  7528. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  7529. #endif
  7530. #endif
  7531. #ifdef WOLFSSL_DTLS13
  7532. || callbacks->method == wolfDTLSv1_3_server_method
  7533. #ifdef WOLFSSL_STATIC_MEMORY
  7534. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  7535. #endif
  7536. #endif
  7537. ) {
  7538. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  7539. }
  7540. else
  7541. #endif
  7542. {
  7543. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7544. }
  7545. #ifdef WOLFSSL_STATIC_MEMORY
  7546. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7547. callbacks->memSz > 0) {
  7548. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7549. callbacks->mem, callbacks->memSz, 0, 1);
  7550. if (ret != WOLFSSL_SUCCESS) {
  7551. fprintf(stderr, "CTX static new failed %d\n", ret);
  7552. goto cleanup;
  7553. }
  7554. }
  7555. #else
  7556. ctx = wolfSSL_CTX_new(callbacks->method());
  7557. #endif
  7558. if (ctx == NULL) {
  7559. fprintf(stderr, "CTX new failed\n");
  7560. goto cleanup;
  7561. }
  7562. /* set defaults */
  7563. if (callbacks->caPemFile == NULL)
  7564. callbacks->caPemFile = cliCertFile;
  7565. if (callbacks->certPemFile == NULL)
  7566. callbacks->certPemFile = svrCertFile;
  7567. if (callbacks->keyPemFile == NULL)
  7568. callbacks->keyPemFile = svrKeyFile;
  7569. #ifdef WOLFSSL_TIRTOS
  7570. fdOpenSession(Task_self());
  7571. #endif
  7572. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7573. wolfSSL_CTX_set_verify(ctx,
  7574. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  7575. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7576. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7577. #endif
  7578. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  7579. if (callbacks->method == wolfDTLSv1_2_server_method) {
  7580. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  7581. goto cleanup;
  7582. }
  7583. #endif
  7584. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7585. WOLFSSL_SUCCESS) {
  7586. goto cleanup;
  7587. }
  7588. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7589. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7590. goto cleanup;
  7591. }
  7592. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7593. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7594. goto cleanup;
  7595. }
  7596. #ifdef HAVE_CRL
  7597. if (callbacks->crlPemFile != NULL) {
  7598. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7599. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7600. goto cleanup;
  7601. }
  7602. }
  7603. #endif
  7604. if (callbacks->ctx_ready)
  7605. callbacks->ctx_ready(ctx);
  7606. ssl = wolfSSL_new(ctx);
  7607. if (ssl == NULL) {
  7608. fprintf(stderr, "SSL new failed\n");
  7609. goto cleanup;
  7610. }
  7611. if (wolfSSL_dtls(ssl)) {
  7612. SOCKADDR_IN_T cliAddr;
  7613. socklen_t cliLen;
  7614. cliLen = sizeof(cliAddr);
  7615. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  7616. (struct sockaddr*)&cliAddr, &cliLen);
  7617. if (idx <= 0) {
  7618. goto cleanup;
  7619. }
  7620. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  7621. }
  7622. else {
  7623. CloseSocket(sfd);
  7624. }
  7625. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  7626. goto cleanup;
  7627. }
  7628. if (callbacks->loadToSSL) {
  7629. wolfSSL_SetDevId(ssl, callbacks->devId);
  7630. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7631. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7632. goto cleanup;
  7633. }
  7634. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7635. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7636. goto cleanup;
  7637. }
  7638. }
  7639. #ifdef NO_PSK
  7640. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  7641. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  7642. #elif !defined(NO_DH)
  7643. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  7644. #endif
  7645. #endif
  7646. if (callbacks->ssl_ready)
  7647. callbacks->ssl_ready(ssl);
  7648. #ifdef WOLFSSL_ASYNC_CRYPT
  7649. err = 0; /* Reset error */
  7650. #endif
  7651. do {
  7652. #ifdef WOLFSSL_ASYNC_CRYPT
  7653. if (err == WC_PENDING_E) {
  7654. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7655. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7656. }
  7657. #endif
  7658. ret = wolfSSL_accept(ssl);
  7659. err = wolfSSL_get_error(ssl, ret);
  7660. } while (err == WC_PENDING_E);
  7661. if (ret != WOLFSSL_SUCCESS) {
  7662. char buff[WOLFSSL_MAX_ERROR_SZ];
  7663. fprintf(stderr, "accept error = %d, %s\n", err,
  7664. wolfSSL_ERR_error_string(err, buff));
  7665. /*err_sys("SSL_accept failed");*/
  7666. }
  7667. else {
  7668. #ifdef WOLFSSL_ASYNC_CRYPT
  7669. err = 0; /* Reset error */
  7670. #endif
  7671. do {
  7672. #ifdef WOLFSSL_ASYNC_CRYPT
  7673. if (err == WC_PENDING_E) {
  7674. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7675. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7676. }
  7677. #endif
  7678. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7679. err = wolfSSL_get_error(ssl, idx);
  7680. } while (err == WC_PENDING_E);
  7681. if (idx > 0) {
  7682. input[idx] = 0;
  7683. fprintf(stderr, "Client message: %s\n", input);
  7684. }
  7685. #ifdef WOLFSSL_ASYNC_CRYPT
  7686. err = 0; /* Reset error */
  7687. #endif
  7688. do {
  7689. #ifdef WOLFSSL_ASYNC_CRYPT
  7690. if (err == WC_PENDING_E) {
  7691. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7692. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7693. }
  7694. #endif
  7695. ret = wolfSSL_write(ssl, msg, len);
  7696. err = wolfSSL_get_error(ssl, ret);
  7697. } while (err == WC_PENDING_E);
  7698. if (len != ret) {
  7699. goto cleanup;
  7700. }
  7701. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  7702. defined(WOLFSSL_DTLS)
  7703. if (wolfSSL_dtls(ssl)) {
  7704. byte* import;
  7705. word32 sz;
  7706. wolfSSL_dtls_export(ssl, NULL, &sz);
  7707. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7708. if (import == NULL) {
  7709. goto cleanup;
  7710. }
  7711. idx = wolfSSL_dtls_export(ssl, import, &sz);
  7712. if (idx < 0) {
  7713. goto cleanup;
  7714. }
  7715. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  7716. goto cleanup;
  7717. }
  7718. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7719. }
  7720. #endif
  7721. #ifdef WOLFSSL_TIRTOS
  7722. Task_yield();
  7723. #endif
  7724. ((func_args*)args)->return_code = TEST_SUCCESS;
  7725. }
  7726. if (callbacks->on_result)
  7727. callbacks->on_result(ssl);
  7728. wolfSSL_shutdown(ssl);
  7729. cleanup:
  7730. wolfSSL_free(ssl);
  7731. wolfSSL_CTX_free(ctx);
  7732. CloseSocket(cfd);
  7733. #ifdef WOLFSSL_TIRTOS
  7734. fdCloseSession(Task_self());
  7735. #endif
  7736. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7737. && defined(HAVE_THREAD_LS)
  7738. wc_ecc_fp_free(); /* free per thread cache */
  7739. #endif
  7740. WOLFSSL_RETURN_FROM_THREAD(0);
  7741. }
  7742. /* TLS Client for API unit testing - generic */
  7743. static void run_wolfssl_client(void* args)
  7744. {
  7745. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7746. WOLFSSL_CTX* ctx = NULL;
  7747. WOLFSSL* ssl = NULL;
  7748. SOCKET_T sfd = 0;
  7749. char msg[] = "hello wolfssl server!";
  7750. int len = (int) XSTRLEN(msg);
  7751. char input[1024];
  7752. int ret, err = 0;
  7753. ((func_args*)args)->return_code = TEST_FAIL;
  7754. /* set defaults */
  7755. if (callbacks->caPemFile == NULL)
  7756. callbacks->caPemFile = caCertFile;
  7757. if (callbacks->certPemFile == NULL)
  7758. callbacks->certPemFile = cliCertFile;
  7759. if (callbacks->keyPemFile == NULL)
  7760. callbacks->keyPemFile = cliKeyFile;
  7761. #ifdef WOLFSSL_STATIC_MEMORY
  7762. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7763. callbacks->memSz > 0) {
  7764. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7765. callbacks->mem, callbacks->memSz, 0, 1);
  7766. if (ret != WOLFSSL_SUCCESS) {
  7767. fprintf(stderr, "CTX static new failed %d\n", ret);
  7768. goto cleanup;
  7769. }
  7770. }
  7771. #else
  7772. ctx = wolfSSL_CTX_new(callbacks->method());
  7773. #endif
  7774. if (ctx == NULL) {
  7775. fprintf(stderr, "CTX new failed\n");
  7776. goto cleanup;
  7777. }
  7778. #ifdef WOLFSSL_TIRTOS
  7779. fdOpenSession(Task_self());
  7780. #endif
  7781. if (!callbacks->loadToSSL) {
  7782. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7783. }
  7784. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7785. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7786. #endif
  7787. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7788. WOLFSSL_SUCCESS) {
  7789. goto cleanup;
  7790. }
  7791. if (!callbacks->loadToSSL) {
  7792. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7793. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7794. goto cleanup;
  7795. }
  7796. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7797. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7798. goto cleanup;
  7799. }
  7800. }
  7801. #ifdef HAVE_CRL
  7802. if (callbacks->crlPemFile != NULL) {
  7803. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7804. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7805. goto cleanup;
  7806. }
  7807. }
  7808. #endif
  7809. if (callbacks->ctx_ready)
  7810. callbacks->ctx_ready(ctx);
  7811. ssl = wolfSSL_new(ctx);
  7812. if (wolfSSL_dtls(ssl)) {
  7813. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7814. 1, 0, ssl);
  7815. }
  7816. else {
  7817. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7818. 0, 0, ssl);
  7819. }
  7820. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7821. goto cleanup;
  7822. }
  7823. if (callbacks->loadToSSL) {
  7824. wolfSSL_SetDevId(ssl, callbacks->devId);
  7825. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7826. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7827. goto cleanup;
  7828. }
  7829. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7830. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7831. goto cleanup;
  7832. }
  7833. }
  7834. if (callbacks->ssl_ready)
  7835. callbacks->ssl_ready(ssl);
  7836. #ifdef WOLFSSL_ASYNC_CRYPT
  7837. err = 0; /* Reset error */
  7838. #endif
  7839. do {
  7840. #ifdef WOLFSSL_ASYNC_CRYPT
  7841. if (err == WC_PENDING_E) {
  7842. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7843. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7844. }
  7845. #endif
  7846. ret = wolfSSL_connect(ssl);
  7847. err = wolfSSL_get_error(ssl, ret);
  7848. } while (err == WC_PENDING_E);
  7849. if (ret != WOLFSSL_SUCCESS) {
  7850. char buff[WOLFSSL_MAX_ERROR_SZ];
  7851. fprintf(stderr, "error = %d, %s\n", err,
  7852. wolfSSL_ERR_error_string(err, buff));
  7853. /*err_sys("SSL_connect failed");*/
  7854. }
  7855. else {
  7856. #ifdef WOLFSSL_ASYNC_CRYPT
  7857. err = 0; /* Reset error */
  7858. #endif
  7859. do {
  7860. #ifdef WOLFSSL_ASYNC_CRYPT
  7861. if (err == WC_PENDING_E) {
  7862. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7863. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7864. }
  7865. #endif
  7866. ret = wolfSSL_write(ssl, msg, len);
  7867. err = wolfSSL_get_error(ssl, ret);
  7868. } while (err == WC_PENDING_E);
  7869. if (len != ret)
  7870. goto cleanup;
  7871. #ifdef WOLFSSL_ASYNC_CRYPT
  7872. err = 0; /* Reset error */
  7873. #endif
  7874. do {
  7875. #ifdef WOLFSSL_ASYNC_CRYPT
  7876. if (err == WC_PENDING_E) {
  7877. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7878. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7879. }
  7880. #endif
  7881. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7882. err = wolfSSL_get_error(ssl, ret);
  7883. } while (err == WC_PENDING_E);
  7884. if (ret > 0) {
  7885. input[ret] = '\0'; /* null term */
  7886. fprintf(stderr, "Server response: %s\n", input);
  7887. }
  7888. ((func_args*)args)->return_code = TEST_SUCCESS;
  7889. }
  7890. if (callbacks->on_result)
  7891. callbacks->on_result(ssl);
  7892. cleanup:
  7893. wolfSSL_free(ssl);
  7894. wolfSSL_CTX_free(ctx);
  7895. CloseSocket(sfd);
  7896. #ifdef WOLFSSL_TIRTOS
  7897. fdCloseSession(Task_self());
  7898. #endif
  7899. }
  7900. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7901. static int test_wolfSSL_read_write(void)
  7902. {
  7903. /* The unit testing for read and write shall happen simultaneously, since
  7904. * one can't do anything with one without the other. (Except for a failure
  7905. * test case.) This function will call all the others that will set up,
  7906. * execute, and report their test findings.
  7907. *
  7908. * Set up the success case first. This function will become the template
  7909. * for the other tests. This should eventually be renamed
  7910. *
  7911. * The success case isn't interesting, how can this fail?
  7912. * - Do not give the client context a CA certificate. The connect should
  7913. * fail. Do not need server for this?
  7914. * - Using NULL for the ssl object on server. Do not need client for this.
  7915. * - Using NULL for the ssl object on client. Do not need server for this.
  7916. * - Good ssl objects for client and server. Client write() without server
  7917. * read().
  7918. * - Good ssl objects for client and server. Server write() without client
  7919. * read().
  7920. * - Forgetting the password callback?
  7921. */
  7922. tcp_ready ready;
  7923. func_args client_args;
  7924. func_args server_args;
  7925. THREAD_TYPE serverThread;
  7926. EXPECT_DECLS;
  7927. XMEMSET(&client_args, 0, sizeof(func_args));
  7928. XMEMSET(&server_args, 0, sizeof(func_args));
  7929. #ifdef WOLFSSL_TIRTOS
  7930. fdOpenSession(Task_self());
  7931. #endif
  7932. StartTCP();
  7933. InitTcpReady(&ready);
  7934. #if defined(USE_WINDOWS_API)
  7935. /* use RNG to get random port if using windows */
  7936. ready.port = GetRandomPort();
  7937. #endif
  7938. server_args.signal = &ready;
  7939. client_args.signal = &ready;
  7940. start_thread(test_server_nofail, &server_args, &serverThread);
  7941. wait_tcp_ready(&server_args);
  7942. test_client_nofail(&client_args, NULL);
  7943. join_thread(serverThread);
  7944. ExpectTrue(client_args.return_code);
  7945. ExpectTrue(server_args.return_code);
  7946. FreeTcpReady(&ready);
  7947. #ifdef WOLFSSL_TIRTOS
  7948. fdOpenSession(Task_self());
  7949. #endif
  7950. return EXPECT_RESULT();
  7951. }
  7952. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7953. {
  7954. EXPECT_DECLS;
  7955. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7956. !defined(WOLFSSL_NO_TLS12)
  7957. /* The unit test for session resumption by re-using WOLFSSL object.
  7958. * WOLFSSL object is not cleared after first session. It reuse the object
  7959. * for second connection.
  7960. */
  7961. tcp_ready ready;
  7962. func_args client_args;
  7963. func_args server_args;
  7964. THREAD_TYPE serverThread;
  7965. callback_functions client_cbf;
  7966. callback_functions server_cbf;
  7967. XMEMSET(&client_args, 0, sizeof(func_args));
  7968. XMEMSET(&server_args, 0, sizeof(func_args));
  7969. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7970. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7971. #ifdef WOLFSSL_TIRTOS
  7972. fdOpenSession(Task_self());
  7973. #endif
  7974. StartTCP();
  7975. InitTcpReady(&ready);
  7976. #if defined(USE_WINDOWS_API)
  7977. /* use RNG to get random port if using windows */
  7978. ready.port = GetRandomPort();
  7979. #endif
  7980. client_cbf.method = wolfTLSv1_2_client_method;
  7981. server_cbf.method = wolfTLSv1_2_server_method;
  7982. client_args.callbacks = &client_cbf;
  7983. server_args.callbacks = &server_cbf;
  7984. server_args.signal = &ready;
  7985. client_args.signal = &ready;
  7986. /* the var is used for loop number */
  7987. server_args.argc = 2;
  7988. start_thread(test_server_loop, &server_args, &serverThread);
  7989. wait_tcp_ready(&server_args);
  7990. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7991. join_thread(serverThread);
  7992. ExpectTrue(client_args.return_code);
  7993. ExpectTrue(server_args.return_code);
  7994. FreeTcpReady(&ready);
  7995. #ifdef WOLFSSL_TIRTOS
  7996. fdOpenSession(Task_self());
  7997. #endif
  7998. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7999. * !defined(WOLFSSL_TLS13) */
  8000. return EXPECT_RESULT();
  8001. }
  8002. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  8003. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  8004. WOLFSSL_CTX* ctx)
  8005. {
  8006. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  8007. myVerifyAction = VERIFY_USE_PREVERFIY;
  8008. wolfSSL_CTX_set_verify_depth(ctx, 2);
  8009. return TEST_SUCCESS;
  8010. }
  8011. #endif
  8012. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  8013. {
  8014. EXPECT_DECLS;
  8015. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  8016. test_ssl_cbf client_cbf;
  8017. test_ssl_cbf server_cbf;
  8018. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  8019. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  8020. #ifdef WOLFSSL_TLS13
  8021. client_cbf.method = wolfTLSv1_3_client_method;
  8022. #endif /* WOLFSSL_TLS13 */
  8023. client_cbf.ctx_ready =
  8024. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  8025. /* test case 1 verify depth is equal to peer chain */
  8026. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  8027. &server_cbf, NULL), TEST_SUCCESS);
  8028. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  8029. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  8030. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  8031. return EXPECT_RESULT();
  8032. }
  8033. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  8034. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  8035. WOLFSSL_CTX* ctx)
  8036. {
  8037. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  8038. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  8039. wolfSSL_CTX_set_verify_depth(ctx, 0);
  8040. return TEST_SUCCESS;
  8041. }
  8042. #endif
  8043. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  8044. {
  8045. EXPECT_DECLS;
  8046. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  8047. test_ssl_cbf client_cbf;
  8048. test_ssl_cbf server_cbf;
  8049. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  8050. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  8051. #ifdef WOLFSSL_TLS13
  8052. client_cbf.method = wolfTLSv1_3_client_method;
  8053. #endif /* WOLFSSL_TLS13 */
  8054. client_cbf.ctx_ready =
  8055. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  8056. /* test case 2
  8057. * verify depth is zero, number of peer's chain is 2.
  8058. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  8059. * callback.
  8060. */
  8061. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  8062. &server_cbf, NULL), TEST_SUCCESS);
  8063. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  8064. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  8065. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  8066. return EXPECT_RESULT();
  8067. }
  8068. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  8069. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  8070. WOLFSSL_CTX* ctx)
  8071. {
  8072. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  8073. myVerifyAction = VERIFY_USE_PREVERFIY;
  8074. wolfSSL_CTX_set_verify_depth(ctx, 0);
  8075. return TEST_SUCCESS;
  8076. }
  8077. #endif
  8078. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  8079. {
  8080. EXPECT_DECLS;
  8081. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  8082. test_ssl_cbf client_cbf;
  8083. test_ssl_cbf server_cbf;
  8084. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  8085. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  8086. #ifdef WOLFSSL_TLS13
  8087. client_cbf.method = wolfTLSv1_3_client_method;
  8088. #endif /* WOLFSSL_TLS13 */
  8089. client_cbf.ctx_ready =
  8090. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  8091. /* test case 3
  8092. * verify depth is zero, number of peer's chain is 2
  8093. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  8094. * therefore, handshake becomes failure.
  8095. */
  8096. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  8097. &server_cbf, NULL), TEST_FAIL);
  8098. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  8099. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  8100. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  8101. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  8102. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  8103. return EXPECT_RESULT();
  8104. }
  8105. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  8106. !defined(WOLFSSL_NO_TLS12) && \
  8107. defined(HAVE_ECC) && !defined(NO_AES) && !defined(NO_SHA256)
  8108. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  8109. {
  8110. EXPECT_DECLS;
  8111. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  8112. return EXPECT_RESULT();
  8113. }
  8114. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  8115. {
  8116. EXPECT_DECLS;
  8117. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  8118. return EXPECT_RESULT();
  8119. }
  8120. #endif
  8121. static int test_wolfSSL_CTX_set_cipher_list(void)
  8122. {
  8123. EXPECT_DECLS;
  8124. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  8125. defined(HAVE_ECC) && !defined(NO_AES) && !defined(NO_SHA256)
  8126. #if !defined(WOLFSSL_NO_TLS12)
  8127. WOLFSSL_CTX* ctxClient = NULL;
  8128. WOLFSSL* sslClient = NULL;
  8129. test_ssl_cbf client_cbf;
  8130. test_ssl_cbf server_cbf;
  8131. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  8132. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  8133. server_cbf.method = wolfTLSv1_2_server_method;
  8134. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  8135. client_cbf.method = wolfTLSv1_2_client_method;
  8136. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  8137. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  8138. &server_cbf, NULL), TEST_SUCCESS);
  8139. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  8140. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  8141. /* check with cipher string that has '+' */
  8142. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  8143. /* Use trailing : with nothing to test for ASAN */
  8144. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM:"));
  8145. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  8146. /* check for the existence of an ECDHE ECDSA cipher suite */
  8147. if (EXPECT_SUCCESS()) {
  8148. int i = 0;
  8149. int found = 0;
  8150. const char* suite;
  8151. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  8152. WOLFSSL_CIPHER* current;
  8153. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  8154. do {
  8155. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  8156. if (current) {
  8157. suite = wolfSSL_CIPHER_get_name(current);
  8158. if (suite && XSTRSTR(suite, "ECDSA")) {
  8159. found = 1;
  8160. break;
  8161. }
  8162. }
  8163. } while (current);
  8164. ExpectIntEQ(found, 1);
  8165. }
  8166. wolfSSL_free(sslClient);
  8167. wolfSSL_CTX_free(ctxClient);
  8168. #endif /* !WOLFSSL_NO_TLS12 */
  8169. #endif
  8170. return EXPECT_RESULT();
  8171. }
  8172. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  8173. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  8174. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  8175. WOLFSSL* ssl)
  8176. {
  8177. EXPECT_DECLS;
  8178. size_t msg_len;
  8179. (void)ctx;
  8180. /* get_finished test */
  8181. /* 1. get own sent message */
  8182. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  8183. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  8184. ExpectIntGE(msg_len, 0);
  8185. /* 2. get peer message */
  8186. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  8187. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  8188. ExpectIntGE(msg_len, 0);
  8189. return EXPECT_RESULT();
  8190. }
  8191. #endif
  8192. static int test_wolfSSL_get_finished(void)
  8193. {
  8194. EXPECT_DECLS;
  8195. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  8196. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  8197. test_ssl_cbf client_cbf;
  8198. test_ssl_cbf server_cbf;
  8199. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  8200. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  8201. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  8202. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  8203. TEST_SUCCESS);
  8204. /* test received msg vs sent msg */
  8205. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  8206. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  8207. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  8208. return EXPECT_RESULT();
  8209. }
  8210. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8211. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  8212. !defined(NO_SESSION_CACHE)
  8213. /* Sessions to restore/store */
  8214. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  8215. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  8216. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  8217. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  8218. {
  8219. /* Don't store sessions. Lookup is still enabled. */
  8220. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8221. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8222. #ifdef OPENSSL_EXTRA
  8223. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8224. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8225. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8226. #endif
  8227. /* Require both peers to provide certs */
  8228. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8229. }
  8230. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  8231. {
  8232. WOLFSSL_SESSION** sess;
  8233. if (wolfSSL_is_server(ssl))
  8234. sess = &test_wolfSSL_CTX_add_session_server_sess;
  8235. else
  8236. sess = &test_wolfSSL_CTX_add_session_client_sess;
  8237. if (*sess == NULL) {
  8238. #ifdef NO_SESSION_CACHE_REF
  8239. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  8240. #else
  8241. /* Test for backwards compatibility */
  8242. if (wolfSSL_is_server(ssl)) {
  8243. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  8244. }
  8245. else {
  8246. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  8247. }
  8248. #endif
  8249. /* Now save the session in the internal store to make it available
  8250. * for lookup. For TLS 1.3, we can't save the session without
  8251. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  8252. * session from cache. */
  8253. if (wolfSSL_is_server(ssl)
  8254. #ifndef WOLFSSL_TICKET_HAVE_ID
  8255. && wolfSSL_version(ssl) != TLS1_3_VERSION
  8256. #endif
  8257. )
  8258. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  8259. *sess), WOLFSSL_SUCCESS);
  8260. }
  8261. else {
  8262. /* If we have a session retrieved then remaining connections should be
  8263. * resuming on that session */
  8264. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  8265. }
  8266. /* Save CTX to be able to decrypt tickets */
  8267. if (wolfSSL_is_server(ssl) &&
  8268. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  8269. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  8270. = wolfSSL_get_SSL_CTX(ssl));
  8271. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  8272. WOLFSSL_SUCCESS);
  8273. }
  8274. #ifdef SESSION_CERTS
  8275. #ifndef WOLFSSL_TICKET_HAVE_ID
  8276. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8277. wolfSSL_session_reused(ssl))
  8278. #endif
  8279. {
  8280. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  8281. * for all connections. TLS 1.3 only has tickets so if we don't
  8282. * include the session id in the ticket then the certificates
  8283. * will not be available on resumption. */
  8284. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  8285. AssertNotNull(peer);
  8286. wolfSSL_X509_free(peer);
  8287. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8288. #ifdef OPENSSL_EXTRA
  8289. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  8290. #endif
  8291. }
  8292. #endif /* SESSION_CERTS */
  8293. }
  8294. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  8295. {
  8296. /* Set the session to reuse for the client */
  8297. AssertIntEQ(wolfSSL_set_session(ssl,
  8298. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  8299. }
  8300. #endif
  8301. static int test_wolfSSL_CTX_add_session(void)
  8302. {
  8303. EXPECT_DECLS;
  8304. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8305. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  8306. !defined(NO_SESSION_CACHE)
  8307. tcp_ready ready;
  8308. func_args client_args;
  8309. func_args server_args;
  8310. THREAD_TYPE serverThread;
  8311. callback_functions client_cb;
  8312. callback_functions server_cb;
  8313. method_provider methods[][2] = {
  8314. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8315. !defined(NO_DES3))
  8316. /* Without AES there are almost no ciphersuites available. This leads
  8317. * to no ciphersuites being available and an error. */
  8318. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  8319. #endif
  8320. #ifndef WOLFSSL_NO_TLS12
  8321. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  8322. #endif
  8323. /* Needs the default ticket callback since it is tied to the
  8324. * connection context and this makes it easy to carry over the ticket
  8325. * crypto context between connections */
  8326. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8327. defined(HAVE_SESSION_TICKET)
  8328. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  8329. #endif
  8330. };
  8331. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  8332. size_t i, j;
  8333. for (i = 0; i < methodsLen; i++) {
  8334. /* First run creates a connection while the second+ run will attempt
  8335. * to resume the connection. The trick is that the internal cache
  8336. * is turned off. wolfSSL_CTX_add_session should put the session in
  8337. * the cache anyway. */
  8338. test_wolfSSL_CTX_add_session_client_sess = NULL;
  8339. test_wolfSSL_CTX_add_session_server_sess = NULL;
  8340. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  8341. #ifdef NO_SESSION_CACHE_REF
  8342. for (j = 0; j < 4; j++) {
  8343. #else
  8344. /* The session may be overwritten in this case. Do only one resumption
  8345. * to stop this test from failing intermittently. */
  8346. for (j = 0; j < 2; j++) {
  8347. #endif
  8348. #ifdef WOLFSSL_TIRTOS
  8349. fdOpenSession(Task_self());
  8350. #endif
  8351. StartTCP();
  8352. InitTcpReady(&ready);
  8353. XMEMSET(&client_args, 0, sizeof(func_args));
  8354. XMEMSET(&server_args, 0, sizeof(func_args));
  8355. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8356. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8357. client_cb.method = methods[i][0];
  8358. server_cb.method = methods[i][1];
  8359. server_args.signal = &ready;
  8360. server_args.callbacks = &server_cb;
  8361. client_args.signal = &ready;
  8362. client_args.callbacks = &client_cb;
  8363. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  8364. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  8365. server_cb.isSharedCtx = 1;
  8366. }
  8367. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8368. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8369. if (j != 0)
  8370. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  8371. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8372. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8373. server_cb.ticNoInit = 1; /* Use default builtin */
  8374. start_thread(test_server_nofail, &server_args, &serverThread);
  8375. wait_tcp_ready(&server_args);
  8376. test_client_nofail(&client_args, NULL);
  8377. join_thread(serverThread);
  8378. ExpectTrue(client_args.return_code);
  8379. ExpectTrue(server_args.return_code);
  8380. FreeTcpReady(&ready);
  8381. if (EXPECT_FAIL())
  8382. break;
  8383. }
  8384. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  8385. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  8386. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  8387. if (EXPECT_FAIL())
  8388. break;
  8389. }
  8390. #endif
  8391. return EXPECT_RESULT();
  8392. }
  8393. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8394. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8395. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8396. defined(HAVE_SESSION_TICKET) && \
  8397. !defined(TITAN_SESSION_CACHE) && \
  8398. !defined(HUGE_SESSION_CACHE) && \
  8399. !defined(BIG_SESSION_CACHE) && \
  8400. !defined(MEDIUM_SESSION_CACHE)
  8401. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  8402. /* Sessions to restore/store */
  8403. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  8404. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  8405. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  8406. static int twcase_new_session_called = 0;
  8407. static int twcase_remove_session_called = 0;
  8408. static int twcase_get_session_called = 0;
  8409. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  8410. #define SESSION_CACHE_SIZE 33
  8411. typedef struct {
  8412. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  8413. WOLFSSL_SESSION* value;
  8414. } hashTable_entry;
  8415. typedef struct {
  8416. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  8417. size_t capacity; /* size of entries */
  8418. size_t length; /* number of items in the hash table */
  8419. wolfSSL_Mutex htLock; /* lock */
  8420. }hashTable;
  8421. static hashTable server_sessionCache;
  8422. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  8423. {
  8424. int i;
  8425. unsigned int len;
  8426. (void)ssl;
  8427. /*
  8428. * This example uses a hash table.
  8429. * Steps you should take for a non-demo code:
  8430. * - acquire a lock for the file named according to the session id
  8431. * - open the file
  8432. * - encrypt and write the SSL_SESSION object to the file
  8433. * - release the lock
  8434. *
  8435. * Return:
  8436. * 0: The callback does not wish to hold a reference of the sess
  8437. * 1: The callback wants to hold a reference of the sess. The callback is
  8438. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  8439. */
  8440. if (sess == NULL)
  8441. return 0;
  8442. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8443. return 0;
  8444. }
  8445. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8446. if (server_sessionCache.entries[i].value == NULL) {
  8447. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  8448. server_sessionCache.entries[i].value = sess;
  8449. server_sessionCache.length++;
  8450. break;
  8451. }
  8452. }
  8453. ++twcase_new_session_called;
  8454. wc_UnLockMutex(&server_sessionCache.htLock);
  8455. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  8456. twcase_new_session_called);
  8457. return 1;
  8458. }
  8459. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  8460. {
  8461. int i;
  8462. (void)ctx;
  8463. (void)sess;
  8464. if (sess == NULL)
  8465. return;
  8466. /*
  8467. * This example uses a hash table.
  8468. * Steps you should take for a non-demo code:
  8469. * - acquire a lock for the file named according to the session id
  8470. * - remove the file
  8471. * - release the lock
  8472. */
  8473. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8474. return;
  8475. }
  8476. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8477. if (server_sessionCache.entries[i].key != NULL &&
  8478. XMEMCMP(server_sessionCache.entries[i].key,
  8479. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8480. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8481. server_sessionCache.entries[i].value = NULL;
  8482. server_sessionCache.entries[i].key = NULL;
  8483. server_sessionCache.length--;
  8484. break;
  8485. }
  8486. }
  8487. ++twcase_remove_session_called;
  8488. wc_UnLockMutex(&server_sessionCache.htLock);
  8489. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  8490. twcase_remove_session_called);
  8491. }
  8492. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  8493. const unsigned char *id, int len, int *ref)
  8494. {
  8495. int i;
  8496. (void)ssl;
  8497. (void)id;
  8498. (void)len;
  8499. /*
  8500. * This example uses a hash table.
  8501. * Steps you should take for a non-demo code:
  8502. * - acquire a lock for the file named according to the session id in the
  8503. * 2nd arg
  8504. * - read and decrypt contents of file and create a new SSL_SESSION
  8505. * - object release the lock
  8506. * - return the new session object
  8507. */
  8508. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  8509. ++twcase_get_session_called);
  8510. /* This callback want to retain a copy of the object. If we want wolfSSL to
  8511. * be responsible for the pointer then set to 0. */
  8512. *ref = 1;
  8513. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8514. if (server_sessionCache.entries[i].key != NULL &&
  8515. XMEMCMP(server_sessionCache.entries[i].key, id,
  8516. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8517. return server_sessionCache.entries[i].value;
  8518. }
  8519. }
  8520. return NULL;
  8521. }
  8522. static int twcase_get_sessionCb_cleanup(void)
  8523. {
  8524. int i;
  8525. int cnt = 0;
  8526. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  8527. * for freeing sessions */
  8528. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8529. if (server_sessionCache.entries[i].value != NULL) {
  8530. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8531. cnt++;
  8532. }
  8533. }
  8534. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  8535. cnt);
  8536. return TEST_SUCCESS;
  8537. }
  8538. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  8539. {
  8540. EXPECT_DECLS;
  8541. /* off - Disable internal cache */
  8542. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8543. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8544. #ifdef OPENSSL_EXTRA
  8545. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8546. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8547. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8548. #endif
  8549. /* off - Do not setup external cache */
  8550. /* Require both peers to provide certs */
  8551. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8552. return EXPECT_RESULT();
  8553. }
  8554. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  8555. {
  8556. /* on - internal cache is on by default */
  8557. /* off - Do not setup external cache */
  8558. /* Require both peers to provide certs */
  8559. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8560. return TEST_SUCCESS;
  8561. }
  8562. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  8563. {
  8564. EXPECT_DECLS;
  8565. /* off - Disable internal cache */
  8566. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8567. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8568. #ifdef OPENSSL_EXTRA
  8569. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8570. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8571. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8572. #endif
  8573. /* on - Enable external cache */
  8574. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8575. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8576. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8577. /* Require both peers to provide certs */
  8578. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8579. return EXPECT_RESULT();
  8580. }
  8581. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  8582. {
  8583. /* on - internal cache is on by default */
  8584. /* on - Enable external cache */
  8585. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8586. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8587. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8588. /* Require both peers to provide certs */
  8589. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8590. return TEST_SUCCESS;
  8591. }
  8592. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  8593. {
  8594. /* on - internal cache is on by default */
  8595. /* on - Enable external cache */
  8596. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8597. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8598. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8599. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  8600. /* Require both peers to provide certs */
  8601. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8602. return TEST_SUCCESS;
  8603. }
  8604. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8605. {
  8606. EXPECT_DECLS;
  8607. WOLFSSL_SESSION** sess;
  8608. if (wolfSSL_is_server(ssl))
  8609. sess = &twcase_server_first_session_ptr;
  8610. else
  8611. return TEST_SUCCESS;
  8612. if (*sess == NULL) {
  8613. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8614. /* Now save the session in the internal store to make it available
  8615. * for lookup. For TLS 1.3, we can't save the session without
  8616. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  8617. * session from cache. */
  8618. if (wolfSSL_is_server(ssl)
  8619. #ifndef WOLFSSL_TICKET_HAVE_ID
  8620. && wolfSSL_version(ssl) != TLS1_3_VERSION
  8621. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  8622. #endif
  8623. ) {
  8624. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  8625. *sess), WOLFSSL_SUCCESS);
  8626. }
  8627. }
  8628. /* Save CTX to be able to decrypt tickets */
  8629. if (twcase_server_current_ctx_ptr == NULL) {
  8630. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  8631. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  8632. WOLFSSL_SUCCESS);
  8633. }
  8634. #ifdef SESSION_CERTS
  8635. #ifndef WOLFSSL_TICKET_HAVE_ID
  8636. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8637. wolfSSL_session_reused(ssl))
  8638. #endif
  8639. {
  8640. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  8641. * for all connections. TLS 1.3 only has tickets so if we don't
  8642. * include the session id in the ticket then the certificates
  8643. * will not be available on resumption. */
  8644. WOLFSSL_X509* peer = NULL;
  8645. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  8646. wolfSSL_X509_free(peer);
  8647. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8648. }
  8649. #endif
  8650. return EXPECT_RESULT();
  8651. }
  8652. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8653. {
  8654. EXPECT_DECLS;
  8655. WOLFSSL_SESSION** sess;
  8656. sess = &twcase_client_first_session_ptr;
  8657. if (*sess == NULL) {
  8658. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8659. }
  8660. else {
  8661. /* If we have a session retrieved then remaining connections should be
  8662. * resuming on that session */
  8663. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  8664. }
  8665. #ifdef SESSION_CERTS
  8666. #ifndef WOLFSSL_TICKET_HAVE_ID
  8667. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8668. wolfSSL_session_reused(ssl))
  8669. #endif
  8670. {
  8671. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  8672. ExpectNotNull(peer);
  8673. wolfSSL_X509_free(peer);
  8674. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8675. #ifdef OPENSSL_EXTRA
  8676. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  8677. #endif
  8678. }
  8679. #endif
  8680. return EXPECT_RESULT();
  8681. }
  8682. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  8683. {
  8684. EXPECT_DECLS;
  8685. /* Set the session to reuse for the client */
  8686. ExpectNotNull(ssl);
  8687. ExpectNotNull(twcase_client_first_session_ptr);
  8688. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  8689. WOLFSSL_SUCCESS);
  8690. return EXPECT_RESULT();
  8691. }
  8692. struct test_add_session_ext_params {
  8693. method_provider client_meth;
  8694. method_provider server_meth;
  8695. const char* tls_version;
  8696. };
  8697. static int test_wolfSSL_CTX_add_session_ext(
  8698. struct test_add_session_ext_params* param)
  8699. {
  8700. EXPECT_DECLS;
  8701. /* Test the default 33 sessions */
  8702. int j;
  8703. /* Clear cache before starting */
  8704. wolfSSL_CTX_flush_sessions(NULL, -1);
  8705. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  8706. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  8707. return BAD_MUTEX_E;
  8708. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  8709. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  8710. for (j = 0; j < 5; j++) {
  8711. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  8712. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  8713. test_ssl_cbf client_cb;
  8714. test_ssl_cbf server_cb;
  8715. (void)dtls;
  8716. /* Test five cache configurations */
  8717. twcase_client_first_session_ptr = NULL;
  8718. twcase_server_first_session_ptr = NULL;
  8719. twcase_server_current_ctx_ptr = NULL;
  8720. twcase_new_session_called = 0;
  8721. twcase_remove_session_called = 0;
  8722. twcase_get_session_called = 0;
  8723. /* connection 1 - first connection */
  8724. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  8725. XMEMSET(&client_cb, 0, sizeof(client_cb));
  8726. XMEMSET(&server_cb, 0, sizeof(server_cb));
  8727. client_cb.method = param->client_meth;
  8728. server_cb.method = param->server_meth;
  8729. if (dtls)
  8730. client_cb.doUdp = server_cb.doUdp = 1;
  8731. /* Setup internal and external cache */
  8732. switch (j) {
  8733. case 0:
  8734. /* SSL_OP_NO_TICKET stateful ticket case */
  8735. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  8736. break;
  8737. case 1:
  8738. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  8739. break;
  8740. case 2:
  8741. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  8742. break;
  8743. case 3:
  8744. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  8745. break;
  8746. case 4:
  8747. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  8748. break;
  8749. }
  8750. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  8751. /* Add session to internal cache and save SSL session for testing */
  8752. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  8753. /* Save client SSL session for testing */
  8754. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  8755. server_cb.ticNoInit = 1; /* Use default builtin */
  8756. /* Don't free/release ctx */
  8757. server_cb.ctx = twcase_server_current_ctx_ptr;
  8758. server_cb.isSharedCtx = 1;
  8759. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8760. &server_cb, NULL), TEST_SUCCESS);
  8761. ExpectIntEQ(twcase_get_session_called, 0);
  8762. if (EXPECT_FAIL()) {
  8763. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8764. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8765. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8766. break;
  8767. }
  8768. switch (j) {
  8769. case 0:
  8770. case 1:
  8771. case 2:
  8772. /* cache cannot be searched with out a connection */
  8773. /* Add a new session */
  8774. ExpectIntEQ(twcase_new_session_called, 1);
  8775. /* In twcase_server_sess_ctx_pre_shutdown
  8776. * wolfSSL_CTX_add_session which evicts the existing session
  8777. * in cache and adds it back in */
  8778. ExpectIntLE(twcase_remove_session_called, 1);
  8779. break;
  8780. case 3:
  8781. case 4:
  8782. /* no external cache */
  8783. ExpectIntEQ(twcase_new_session_called, 0);
  8784. ExpectIntEQ(twcase_remove_session_called, 0);
  8785. break;
  8786. }
  8787. /* connection 2 - session resume */
  8788. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  8789. twcase_new_session_called = 0;
  8790. twcase_remove_session_called = 0;
  8791. twcase_get_session_called = 0;
  8792. server_cb.on_result = 0;
  8793. client_cb.on_result = 0;
  8794. server_cb.ticNoInit = 1; /* Use default builtin */
  8795. server_cb.ctx = twcase_server_current_ctx_ptr;
  8796. /* try session resumption */
  8797. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  8798. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8799. &server_cb, NULL), TEST_SUCCESS);
  8800. /* Clear cache before checking */
  8801. wolfSSL_CTX_flush_sessions(NULL, -1);
  8802. switch (j) {
  8803. case 0:
  8804. if (tls13) {
  8805. /* (D)TLSv1.3 stateful case */
  8806. /* cache hit */
  8807. /* DTLS accesses cache once for stateless parsing and
  8808. * once for stateful parsing */
  8809. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8810. /* (D)TLSv1.3 creates a new ticket,
  8811. * updates both internal and external cache */
  8812. ExpectIntEQ(twcase_new_session_called, 1);
  8813. /* A new session ID is created for a new ticket */
  8814. ExpectIntEQ(twcase_remove_session_called, 2);
  8815. }
  8816. else {
  8817. /* non (D)TLSv1.3 case, no update */
  8818. /* DTLS accesses cache once for stateless parsing and
  8819. * once for stateful parsing */
  8820. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8821. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8822. #else
  8823. ExpectIntEQ(twcase_get_session_called, 1);
  8824. #endif
  8825. ExpectIntEQ(twcase_new_session_called, 0);
  8826. /* Called on session added in
  8827. * twcase_server_sess_ctx_pre_shutdown */
  8828. ExpectIntEQ(twcase_remove_session_called, 1);
  8829. }
  8830. break;
  8831. case 1:
  8832. if (tls13) {
  8833. /* (D)TLSv1.3 case */
  8834. /* cache hit */
  8835. ExpectIntEQ(twcase_get_session_called, 1);
  8836. /* (D)TLSv1.3 creates a new ticket,
  8837. * updates both internal and external cache */
  8838. ExpectIntEQ(twcase_new_session_called, 1);
  8839. /* Called on session added in
  8840. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8841. ExpectIntEQ(twcase_remove_session_called, 1);
  8842. }
  8843. else {
  8844. /* non (D)TLSv1.3 case */
  8845. /* cache hit */
  8846. /* DTLS accesses cache once for stateless parsing and
  8847. * once for stateful parsing */
  8848. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8849. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8850. #else
  8851. ExpectIntEQ(twcase_get_session_called, 1);
  8852. #endif
  8853. ExpectIntEQ(twcase_new_session_called, 0);
  8854. /* Called on session added in
  8855. * twcase_server_sess_ctx_pre_shutdown */
  8856. ExpectIntEQ(twcase_remove_session_called, 1);
  8857. }
  8858. break;
  8859. case 2:
  8860. if (tls13) {
  8861. /* (D)TLSv1.3 case */
  8862. /* cache hit */
  8863. ExpectIntEQ(twcase_get_session_called, 1);
  8864. /* (D)TLSv1.3 creates a new ticket,
  8865. * updates both internal and external cache */
  8866. ExpectIntEQ(twcase_new_session_called, 1);
  8867. /* Called on session added in
  8868. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8869. ExpectIntEQ(twcase_remove_session_called, 1);
  8870. }
  8871. else {
  8872. /* non (D)TLSv1.3 case */
  8873. /* cache hit */
  8874. /* DTLS accesses cache once for stateless parsing and
  8875. * once for stateful parsing */
  8876. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8877. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8878. #else
  8879. ExpectIntEQ(twcase_get_session_called, 1);
  8880. #endif
  8881. ExpectIntEQ(twcase_new_session_called, 0);
  8882. /* Called on session added in
  8883. * twcase_server_sess_ctx_pre_shutdown */
  8884. ExpectIntEQ(twcase_remove_session_called, 1);
  8885. }
  8886. break;
  8887. case 3:
  8888. case 4:
  8889. /* no external cache */
  8890. ExpectIntEQ(twcase_get_session_called, 0);
  8891. ExpectIntEQ(twcase_new_session_called, 0);
  8892. ExpectIntEQ(twcase_remove_session_called, 0);
  8893. break;
  8894. }
  8895. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8896. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8897. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8898. if (EXPECT_FAIL())
  8899. break;
  8900. }
  8901. twcase_get_sessionCb_cleanup();
  8902. XMEMSET(&server_sessionCache.entries, 0,
  8903. sizeof(server_sessionCache.entries));
  8904. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8905. wc_FreeMutex(&server_sessionCache.htLock);
  8906. return EXPECT_RESULT();
  8907. }
  8908. #endif
  8909. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8910. {
  8911. EXPECT_DECLS;
  8912. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8913. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8914. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8915. defined(HAVE_SESSION_TICKET) && \
  8916. !defined(TITAN_SESSION_CACHE) && \
  8917. !defined(HUGE_SESSION_CACHE) && \
  8918. !defined(BIG_SESSION_CACHE) && \
  8919. !defined(MEDIUM_SESSION_CACHE)
  8920. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8921. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8922. struct test_add_session_ext_params param[1] = {
  8923. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8924. };
  8925. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8926. #endif
  8927. #endif
  8928. return EXPECT_RESULT();
  8929. }
  8930. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8931. {
  8932. EXPECT_DECLS;
  8933. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8934. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8935. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8936. defined(HAVE_SESSION_TICKET) && \
  8937. !defined(TITAN_SESSION_CACHE) && \
  8938. !defined(HUGE_SESSION_CACHE) && \
  8939. !defined(BIG_SESSION_CACHE) && \
  8940. !defined(MEDIUM_SESSION_CACHE)
  8941. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8942. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8943. #ifdef WOLFSSL_DTLS13
  8944. struct test_add_session_ext_params param[1] = {
  8945. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8946. };
  8947. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8948. #endif
  8949. #endif
  8950. #endif
  8951. return EXPECT_RESULT();
  8952. }
  8953. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8954. {
  8955. EXPECT_DECLS;
  8956. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8957. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8958. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8959. defined(HAVE_SESSION_TICKET) && \
  8960. !defined(TITAN_SESSION_CACHE) && \
  8961. !defined(HUGE_SESSION_CACHE) && \
  8962. !defined(BIG_SESSION_CACHE) && \
  8963. !defined(MEDIUM_SESSION_CACHE)
  8964. #ifndef WOLFSSL_NO_TLS12
  8965. struct test_add_session_ext_params param[1] = {
  8966. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8967. };
  8968. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8969. #endif
  8970. #endif
  8971. return EXPECT_RESULT();
  8972. }
  8973. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8974. {
  8975. EXPECT_DECLS;
  8976. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8977. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8978. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8979. defined(HAVE_SESSION_TICKET) && \
  8980. !defined(TITAN_SESSION_CACHE) && \
  8981. !defined(HUGE_SESSION_CACHE) && \
  8982. !defined(BIG_SESSION_CACHE) && \
  8983. !defined(MEDIUM_SESSION_CACHE)
  8984. #ifndef WOLFSSL_NO_TLS12
  8985. #ifdef WOLFSSL_DTLS
  8986. struct test_add_session_ext_params param[1] = {
  8987. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8988. };
  8989. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8990. #endif
  8991. #endif
  8992. #endif
  8993. return EXPECT_RESULT();
  8994. }
  8995. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8996. {
  8997. EXPECT_DECLS;
  8998. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8999. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  9000. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  9001. defined(HAVE_SESSION_TICKET) && \
  9002. !defined(TITAN_SESSION_CACHE) && \
  9003. !defined(HUGE_SESSION_CACHE) && \
  9004. !defined(BIG_SESSION_CACHE) && \
  9005. !defined(MEDIUM_SESSION_CACHE)
  9006. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  9007. !defined(NO_DES3))
  9008. struct test_add_session_ext_params param[1] = {
  9009. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  9010. };
  9011. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  9012. #endif
  9013. #endif
  9014. return EXPECT_RESULT();
  9015. }
  9016. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  9017. {
  9018. EXPECT_DECLS;
  9019. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  9020. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  9021. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  9022. defined(HAVE_SESSION_TICKET) && \
  9023. !defined(TITAN_SESSION_CACHE) && \
  9024. !defined(HUGE_SESSION_CACHE) && \
  9025. !defined(BIG_SESSION_CACHE) && \
  9026. !defined(MEDIUM_SESSION_CACHE)
  9027. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  9028. !defined(NO_DES3))
  9029. #ifdef WOLFSSL_DTLS
  9030. struct test_add_session_ext_params param[1] = {
  9031. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  9032. };
  9033. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  9034. #endif
  9035. #endif
  9036. #endif
  9037. return EXPECT_RESULT();
  9038. }
  9039. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  9040. /* canned export of a session using older version 3 */
  9041. static unsigned char version_3[] = {
  9042. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  9043. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  9044. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  9045. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  9046. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  9047. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9048. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  9049. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  9050. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  9051. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9052. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  9053. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  9054. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9055. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  9056. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  9057. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  9058. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  9059. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  9060. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  9061. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  9062. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9063. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9064. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9065. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9066. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9067. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9068. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9069. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9070. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9071. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9072. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9073. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  9074. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  9075. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  9076. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  9077. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  9078. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  9079. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  9080. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  9081. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  9082. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  9083. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  9084. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  9085. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  9086. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9087. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  9088. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  9089. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  9090. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  9091. 0x2E, 0x31, 0xED, 0x4F
  9092. };
  9093. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  9094. static int test_wolfSSL_dtls_export(void)
  9095. {
  9096. EXPECT_DECLS;
  9097. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  9098. tcp_ready ready;
  9099. func_args client_args;
  9100. func_args server_args;
  9101. THREAD_TYPE serverThread;
  9102. callback_functions server_cbf;
  9103. callback_functions client_cbf;
  9104. #ifdef WOLFSSL_TIRTOS
  9105. fdOpenSession(Task_self());
  9106. #endif
  9107. InitTcpReady(&ready);
  9108. #if defined(USE_WINDOWS_API)
  9109. /* use RNG to get random port if using windows */
  9110. ready.port = GetRandomPort();
  9111. #endif
  9112. /* set using dtls */
  9113. XMEMSET(&client_args, 0, sizeof(func_args));
  9114. XMEMSET(&server_args, 0, sizeof(func_args));
  9115. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9116. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  9117. server_cbf.method = wolfDTLSv1_2_server_method;
  9118. client_cbf.method = wolfDTLSv1_2_client_method;
  9119. server_args.callbacks = &server_cbf;
  9120. client_args.callbacks = &client_cbf;
  9121. server_args.signal = &ready;
  9122. client_args.signal = &ready;
  9123. start_thread(run_wolfssl_server, &server_args, &serverThread);
  9124. wait_tcp_ready(&server_args);
  9125. run_wolfssl_client(&client_args);
  9126. join_thread(serverThread);
  9127. ExpectTrue(client_args.return_code);
  9128. ExpectTrue(server_args.return_code);
  9129. FreeTcpReady(&ready);
  9130. #ifdef WOLFSSL_TIRTOS
  9131. fdOpenSession(Task_self());
  9132. #endif
  9133. if (EXPECT_SUCCESS()) {
  9134. SOCKET_T sockfd = 0;
  9135. WOLFSSL_CTX* ctx = NULL;
  9136. WOLFSSL* ssl = NULL;
  9137. char msg[64] = "hello wolfssl!";
  9138. char reply[1024];
  9139. int msgSz = (int)XSTRLEN(msg);
  9140. byte *session, *window;
  9141. unsigned int sessionSz = 0;
  9142. unsigned int windowSz = 0;
  9143. #ifndef TEST_IPV6
  9144. struct sockaddr_in peerAddr;
  9145. #else
  9146. struct sockaddr_in6 peerAddr;
  9147. #endif /* TEST_IPV6 */
  9148. int i;
  9149. /* Set ctx to DTLS 1.2 */
  9150. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  9151. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9152. /* test importing version 3 */
  9153. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  9154. /* test importing bad length and bad version */
  9155. version_3[2] += 1;
  9156. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  9157. version_3[2] -= 1; version_3[1] = 0XA0;
  9158. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  9159. wolfSSL_free(ssl);
  9160. wolfSSL_CTX_free(ctx);
  9161. /* check storing client state after connection and storing window only */
  9162. #ifdef WOLFSSL_TIRTOS
  9163. fdOpenSession(Task_self());
  9164. #endif
  9165. InitTcpReady(&ready);
  9166. #if defined(USE_WINDOWS_API)
  9167. /* use RNG to get random port if using windows */
  9168. ready.port = GetRandomPort();
  9169. #endif
  9170. /* set using dtls */
  9171. XMEMSET(&server_args, 0, sizeof(func_args));
  9172. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9173. server_cbf.method = wolfDTLSv1_2_server_method;
  9174. server_cbf.doUdp = 1;
  9175. server_args.callbacks = &server_cbf;
  9176. server_args.argc = 3; /* set loop_count to 3 */
  9177. server_args.signal = &ready;
  9178. start_thread(test_server_nofail, &server_args, &serverThread);
  9179. wait_tcp_ready(&server_args);
  9180. /* create and connect with client */
  9181. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  9182. ExpectIntEQ(WOLFSSL_SUCCESS,
  9183. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  9184. ExpectIntEQ(WOLFSSL_SUCCESS,
  9185. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  9186. ExpectIntEQ(WOLFSSL_SUCCESS,
  9187. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  9188. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  9189. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9190. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  9191. /* store server information connected too */
  9192. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  9193. #ifndef TEST_IPV6
  9194. peerAddr.sin_family = AF_INET;
  9195. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  9196. peerAddr.sin_port = XHTONS(server_args.signal->port);
  9197. #else
  9198. peerAddr.sin6_family = AF_INET6;
  9199. ExpectIntEQ(
  9200. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  9201. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  9202. #endif
  9203. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  9204. WOLFSSL_SUCCESS);
  9205. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  9206. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  9207. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9208. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  9209. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9210. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  9211. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  9212. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9213. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  9214. wolfSSL_free(ssl);
  9215. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  9216. /* restore state */
  9217. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9218. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  9219. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  9220. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  9221. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  9222. WOLFSSL_SUCCESS);
  9223. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9224. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  9225. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  9226. wolfSSL_free(ssl);
  9227. }
  9228. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9229. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9230. wolfSSL_CTX_free(ctx);
  9231. fprintf(stderr, "done and waiting for server\n");
  9232. join_thread(serverThread);
  9233. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  9234. FreeTcpReady(&ready);
  9235. #ifdef WOLFSSL_TIRTOS
  9236. fdOpenSession(Task_self());
  9237. #endif
  9238. }
  9239. #endif
  9240. return EXPECT_RESULT();
  9241. }
  9242. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  9243. #ifdef WOLFSSL_TLS13
  9244. static const byte canned_client_tls13_session[] = {
  9245. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  9246. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  9247. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  9248. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  9249. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  9250. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  9251. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  9252. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  9253. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  9254. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9255. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9256. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  9257. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  9258. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  9259. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  9260. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  9261. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  9262. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  9263. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  9264. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  9265. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  9266. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  9267. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  9268. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  9269. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  9270. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  9271. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  9272. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  9273. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9274. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  9275. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  9276. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  9277. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  9278. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  9279. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9280. 0x00, 0x03
  9281. };
  9282. static const byte canned_server_tls13_session[] = {
  9283. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  9284. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  9285. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  9286. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  9287. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  9289. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  9290. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  9291. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  9292. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9293. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9294. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  9295. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  9296. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  9297. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  9298. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  9299. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  9300. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  9301. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  9302. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  9303. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  9304. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  9305. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  9306. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  9307. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  9308. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  9309. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  9310. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  9311. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9312. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  9313. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  9314. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  9315. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  9316. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  9317. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9318. 0x00, 0x04
  9319. };
  9320. #endif /* WOLFSSL_TLS13 */
  9321. static const byte canned_client_session[] = {
  9322. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  9323. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  9324. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  9325. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  9326. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  9327. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9328. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  9329. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  9330. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  9331. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9332. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9333. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  9334. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  9335. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  9336. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  9337. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  9338. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  9339. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  9340. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  9341. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  9342. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  9343. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  9344. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  9345. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  9346. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  9347. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  9348. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  9349. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  9350. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  9351. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9352. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9353. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9354. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9355. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  9356. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  9357. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  9358. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  9359. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9360. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9361. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9362. 0x00, 0x03
  9363. };
  9364. static const byte canned_server_session[] = {
  9365. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  9366. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  9367. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  9368. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  9369. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  9370. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9371. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  9372. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  9373. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  9374. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9375. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9376. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  9377. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  9378. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  9379. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  9380. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  9381. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  9382. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  9383. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  9384. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  9385. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  9386. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  9387. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  9388. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  9389. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  9390. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  9391. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  9392. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  9393. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  9394. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9395. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9396. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9397. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9398. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  9399. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  9400. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9401. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9402. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  9403. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  9404. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9405. 0x00, 0x04
  9406. };
  9407. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  9408. {
  9409. SOCKET_T sockfd = 0;
  9410. SOCKET_T clientfd = 0;
  9411. word16 port;
  9412. callback_functions* cbf;
  9413. WOLFSSL_CTX* ctx = 0;
  9414. WOLFSSL* ssl = 0;
  9415. char msg[] = "I hear you fa shizzle!";
  9416. char input[1024];
  9417. int idx;
  9418. #ifdef WOLFSSL_TIRTOS
  9419. fdOpenSession(Task_self());
  9420. #endif
  9421. ((func_args*)args)->return_code = TEST_FAIL;
  9422. cbf = ((func_args*)args)->callbacks;
  9423. #if defined(USE_WINDOWS_API)
  9424. port = ((func_args*)args)->signal->port;
  9425. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  9426. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  9427. /* Let tcp_listen assign port */
  9428. port = 0;
  9429. #else
  9430. /* Use default port */
  9431. port = wolfSSLPort;
  9432. #endif
  9433. /* do it here to detect failure */
  9434. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  9435. CloseSocket(sockfd);
  9436. {
  9437. WOLFSSL_METHOD* method = NULL;
  9438. if (cbf != NULL && cbf->method != NULL) {
  9439. method = cbf->method();
  9440. }
  9441. else {
  9442. method = wolfTLSv1_2_server_method();
  9443. }
  9444. ctx = wolfSSL_CTX_new(method);
  9445. }
  9446. if (ctx == NULL) {
  9447. goto done;
  9448. }
  9449. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9450. /* call ctx setup callback */
  9451. if (cbf != NULL && cbf->ctx_ready != NULL) {
  9452. cbf->ctx_ready(ctx);
  9453. }
  9454. ssl = wolfSSL_new(ctx);
  9455. if (ssl == NULL) {
  9456. goto done;
  9457. }
  9458. wolfSSL_set_fd(ssl, clientfd);
  9459. /* call ssl setup callback */
  9460. if (cbf != NULL && cbf->ssl_ready != NULL) {
  9461. cbf->ssl_ready(ssl);
  9462. }
  9463. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  9464. if (idx > 0) {
  9465. input[idx] = '\0';
  9466. fprintf(stderr, "Client message export/import: %s\n", input);
  9467. }
  9468. else {
  9469. fprintf(stderr, "ret = %d error = %d\n", idx,
  9470. wolfSSL_get_error(ssl, idx));
  9471. goto done;
  9472. }
  9473. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  9474. /*err_sys("SSL_write failed");*/
  9475. WOLFSSL_RETURN_FROM_THREAD(0);
  9476. }
  9477. #ifdef WOLFSSL_TIRTOS
  9478. Task_yield();
  9479. #endif
  9480. ((func_args*)args)->return_code = TEST_SUCCESS;
  9481. done:
  9482. wolfSSL_shutdown(ssl);
  9483. wolfSSL_free(ssl);
  9484. wolfSSL_CTX_free(ctx);
  9485. CloseSocket(clientfd);
  9486. #ifdef WOLFSSL_TIRTOS
  9487. fdCloseSession(Task_self());
  9488. #endif
  9489. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9490. && defined(HAVE_THREAD_LS)
  9491. wc_ecc_fp_free(); /* free per thread cache */
  9492. #endif
  9493. #if defined(HAVE_SESSION_TICKET) && \
  9494. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  9495. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  9496. OpenSSLTicketCleanup();
  9497. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  9498. TicketCleanup();
  9499. #endif
  9500. #endif
  9501. WOLFSSL_RETURN_FROM_THREAD(0);
  9502. }
  9503. static void load_tls12_canned_server(WOLFSSL* ssl)
  9504. {
  9505. int clientfd = wolfSSL_get_fd(ssl);
  9506. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  9507. sizeof(canned_server_session)), sizeof(canned_server_session));
  9508. wolfSSL_set_fd(ssl, clientfd);
  9509. }
  9510. #ifdef WOLFSSL_TLS13
  9511. static void load_tls13_canned_server(WOLFSSL* ssl)
  9512. {
  9513. int clientfd = wolfSSL_get_fd(ssl);
  9514. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  9515. sizeof(canned_server_tls13_session)),
  9516. sizeof(canned_server_tls13_session));
  9517. wolfSSL_set_fd(ssl, clientfd);
  9518. }
  9519. #endif
  9520. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  9521. static int test_wolfSSL_tls_export_run(int v)
  9522. {
  9523. EXPECT_DECLS;
  9524. SOCKET_T sockfd = 0;
  9525. WOLFSSL_CTX* ctx = 0;
  9526. WOLFSSL* ssl = 0;
  9527. char msg[64] = "hello wolfssl!";
  9528. char reply[1024];
  9529. word32 replySz;
  9530. int msgSz = (int)XSTRLEN(msg);
  9531. const byte* clientSession = NULL;
  9532. int clientSessionSz = 0;
  9533. tcp_ready ready;
  9534. func_args server_args;
  9535. THREAD_TYPE serverThread;
  9536. callback_functions server_cbf;
  9537. #ifdef WOLFSSL_TIRTOS
  9538. fdOpenSession(Task_self());
  9539. #endif
  9540. InitTcpReady(&ready);
  9541. #if defined(USE_WINDOWS_API)
  9542. /* use RNG to get random port if using windows */
  9543. ready.port = GetRandomPort();
  9544. #endif
  9545. XMEMSET(&server_args, 0, sizeof(func_args));
  9546. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9547. switch (v) {
  9548. case WOLFSSL_TLSV1_2:
  9549. server_cbf.method = wolfTLSv1_2_server_method;
  9550. server_cbf.ssl_ready = load_tls12_canned_server;
  9551. /* setup the client side */
  9552. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9553. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9554. clientSession = canned_client_session;
  9555. clientSessionSz = sizeof(canned_client_session);
  9556. break;
  9557. #ifdef WOLFSSL_TLS13
  9558. case WOLFSSL_TLSV1_3:
  9559. server_cbf.method = wolfTLSv1_3_server_method;
  9560. server_cbf.ssl_ready = load_tls13_canned_server;
  9561. /* setup the client side */
  9562. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9563. clientSession = canned_client_tls13_session;
  9564. clientSessionSz = sizeof(canned_client_tls13_session);
  9565. break;
  9566. #endif
  9567. }
  9568. server_args.callbacks = &server_cbf;
  9569. server_args.signal = &ready;
  9570. start_thread(tls_export_server, &server_args, &serverThread);
  9571. wait_tcp_ready(&server_args);
  9572. #ifdef WOLFSSL_TIRTOS
  9573. fdOpenSession(Task_self());
  9574. #endif
  9575. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9576. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  9577. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  9578. clientSessionSz);
  9579. replySz = sizeof(reply);
  9580. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  9581. #if !defined(NO_PSK) && defined(HAVE_ANON)
  9582. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  9583. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  9584. #endif
  9585. wolfSSL_set_fd(ssl, sockfd);
  9586. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9587. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  9588. wolfSSL_free(ssl);
  9589. wolfSSL_CTX_free(ctx);
  9590. CloseSocket(sockfd);
  9591. #ifdef WOLFSSL_TIRTOS
  9592. fdCloseSession(Task_self());
  9593. #endif
  9594. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9595. && defined(HAVE_THREAD_LS)
  9596. wc_ecc_fp_free(); /* free per thread cache */
  9597. #endif
  9598. join_thread(serverThread);
  9599. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  9600. FreeTcpReady(&ready);
  9601. #ifdef WOLFSSL_TIRTOS
  9602. fdOpenSession(Task_self());
  9603. #endif
  9604. return EXPECT_RESULT();
  9605. }
  9606. #endif
  9607. static int test_wolfSSL_tls_export(void)
  9608. {
  9609. int res = TEST_SKIPPED;
  9610. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  9611. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  9612. #ifdef WOLFSSL_TLS13
  9613. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  9614. #endif
  9615. res = TEST_RES_CHECK(1);
  9616. #endif
  9617. return res;
  9618. }
  9619. /*----------------------------------------------------------------------------*
  9620. | TLS extensions tests
  9621. *----------------------------------------------------------------------------*/
  9622. #ifdef ENABLE_TLS_CALLBACK_TEST
  9623. /* Connection test runner - generic */
  9624. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  9625. callback_functions* server_callbacks)
  9626. {
  9627. tcp_ready ready;
  9628. func_args client_args;
  9629. func_args server_args;
  9630. THREAD_TYPE serverThread;
  9631. XMEMSET(&client_args, 0, sizeof(func_args));
  9632. XMEMSET(&server_args, 0, sizeof(func_args));
  9633. StartTCP();
  9634. client_args.callbacks = client_callbacks;
  9635. server_args.callbacks = server_callbacks;
  9636. #ifdef WOLFSSL_TIRTOS
  9637. fdOpenSession(Task_self());
  9638. #endif
  9639. /* RUN Server side */
  9640. InitTcpReady(&ready);
  9641. #if defined(USE_WINDOWS_API)
  9642. /* use RNG to get random port if using windows */
  9643. ready.port = GetRandomPort();
  9644. #endif
  9645. server_args.signal = &ready;
  9646. client_args.signal = &ready;
  9647. start_thread(run_wolfssl_server, &server_args, &serverThread);
  9648. wait_tcp_ready(&server_args);
  9649. /* RUN Client side */
  9650. run_wolfssl_client(&client_args);
  9651. join_thread(serverThread);
  9652. FreeTcpReady(&ready);
  9653. #ifdef WOLFSSL_TIRTOS
  9654. fdCloseSession(Task_self());
  9655. #endif
  9656. client_callbacks->return_code = client_args.return_code;
  9657. server_callbacks->return_code = server_args.return_code;
  9658. }
  9659. #endif /* ENABLE_TLS_CALLBACK_TEST */
  9660. #ifdef HAVE_SNI
  9661. static int test_wolfSSL_UseSNI_params(void)
  9662. {
  9663. EXPECT_DECLS;
  9664. #if !defined(NO_WOLFSSL_CLIENT)
  9665. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9666. WOLFSSL *ssl = wolfSSL_new(ctx);
  9667. ExpectNotNull(ctx);
  9668. ExpectNotNull(ssl);
  9669. /* invalid [ctx|ssl] */
  9670. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  9671. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  9672. /* invalid type */
  9673. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  9674. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  9675. /* invalid data */
  9676. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  9677. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  9678. /* success case */
  9679. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  9680. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  9681. wolfSSL_free(ssl);
  9682. wolfSSL_CTX_free(ctx);
  9683. #endif /* !NO_WOLFSSL_CLIENT */
  9684. return EXPECT_RESULT();
  9685. }
  9686. /* BEGIN of connection tests callbacks */
  9687. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9688. {
  9689. AssertIntEQ(WOLFSSL_SUCCESS,
  9690. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9691. }
  9692. static void use_SNI_at_ssl(WOLFSSL* ssl)
  9693. {
  9694. AssertIntEQ(WOLFSSL_SUCCESS,
  9695. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9696. }
  9697. static void different_SNI_at_ssl(WOLFSSL* ssl)
  9698. {
  9699. AssertIntEQ(WOLFSSL_SUCCESS,
  9700. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  9701. }
  9702. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  9703. {
  9704. use_SNI_at_ssl(ssl);
  9705. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9706. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  9707. }
  9708. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  9709. {
  9710. use_SNI_at_ssl(ssl);
  9711. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9712. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  9713. }
  9714. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9715. {
  9716. use_SNI_at_ctx(ctx);
  9717. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9718. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9719. }
  9720. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  9721. {
  9722. use_SNI_at_ssl(ssl);
  9723. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9724. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9725. }
  9726. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9727. {
  9728. use_SNI_at_ctx(ctx);
  9729. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9730. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9731. }
  9732. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  9733. {
  9734. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  9735. }
  9736. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  9737. {
  9738. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  9739. }
  9740. static void verify_SNI_no_matching(WOLFSSL* ssl)
  9741. {
  9742. byte type = WOLFSSL_SNI_HOST_NAME;
  9743. void* request = (void*) &type; /* to be overwritten */
  9744. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  9745. AssertNotNull(request);
  9746. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9747. AssertNull(request);
  9748. }
  9749. static void verify_SNI_real_matching(WOLFSSL* ssl)
  9750. {
  9751. byte type = WOLFSSL_SNI_HOST_NAME;
  9752. void* request = NULL;
  9753. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  9754. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9755. AssertNotNull(request);
  9756. AssertStrEQ("www.wolfssl.com", (char*)request);
  9757. }
  9758. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  9759. {
  9760. byte type = WOLFSSL_SNI_HOST_NAME;
  9761. void* request = NULL;
  9762. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  9763. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9764. AssertNotNull(request);
  9765. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  9766. }
  9767. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9768. {
  9769. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  9770. }
  9771. /* END of connection tests callbacks */
  9772. static int test_wolfSSL_UseSNI_connection(void)
  9773. {
  9774. int res = TEST_SKIPPED;
  9775. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9776. callback_functions client_cb;
  9777. callback_functions server_cb;
  9778. size_t i;
  9779. #ifdef WOLFSSL_STATIC_MEMORY
  9780. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  9781. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  9782. #endif
  9783. struct {
  9784. method_provider client_meth;
  9785. method_provider server_meth;
  9786. #ifdef WOLFSSL_STATIC_MEMORY
  9787. wolfSSL_method_func client_meth_ex;
  9788. wolfSSL_method_func server_meth_ex;
  9789. #endif
  9790. } methods[] = {
  9791. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  9792. {wolfSSLv23_client_method, wolfSSLv23_server_method
  9793. #ifdef WOLFSSL_STATIC_MEMORY
  9794. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  9795. #endif
  9796. },
  9797. #endif
  9798. #ifndef WOLFSSL_NO_TLS12
  9799. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  9800. #ifdef WOLFSSL_STATIC_MEMORY
  9801. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  9802. #endif
  9803. },
  9804. #endif
  9805. #ifdef WOLFSSL_TLS13
  9806. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9807. #ifdef WOLFSSL_STATIC_MEMORY
  9808. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9809. #endif
  9810. },
  9811. #endif
  9812. };
  9813. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9814. for (i = 0; i < methodsSz; i++) {
  9815. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9816. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9817. client_cb.method = methods[i].client_meth;
  9818. server_cb.method = methods[i].server_meth;
  9819. client_cb.devId = testDevId;
  9820. server_cb.devId = testDevId;
  9821. #ifdef WOLFSSL_STATIC_MEMORY
  9822. client_cb.method_ex = methods[i].client_meth_ex;
  9823. server_cb.method_ex = methods[i].server_meth_ex;
  9824. client_cb.mem = cliMem;
  9825. client_cb.memSz = (word32)sizeof(cliMem);
  9826. server_cb.mem = svrMem;
  9827. server_cb.memSz = (word32)sizeof(svrMem);;
  9828. #endif
  9829. /* success case at ctx */
  9830. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9831. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9832. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9833. test_wolfSSL_client_server(&client_cb, &server_cb);
  9834. /* success case at ssl */
  9835. fprintf(stderr, "\tsuccess case at ssl\n");
  9836. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9837. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9838. test_wolfSSL_client_server(&client_cb, &server_cb);
  9839. /* default mismatch behavior */
  9840. fprintf(stderr, "\tdefault mismatch behavior\n");
  9841. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9842. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9843. test_wolfSSL_client_server(&client_cb, &server_cb);
  9844. /* continue on mismatch */
  9845. fprintf(stderr, "\tcontinue on mismatch\n");
  9846. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9847. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9848. test_wolfSSL_client_server(&client_cb, &server_cb);
  9849. /* fake answer on mismatch */
  9850. fprintf(stderr, "\tfake answer on mismatch\n");
  9851. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9852. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9853. test_wolfSSL_client_server(&client_cb, &server_cb);
  9854. /* sni abort - success */
  9855. fprintf(stderr, "\tsni abort - success\n");
  9856. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9857. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9858. test_wolfSSL_client_server(&client_cb, &server_cb);
  9859. /* sni abort - abort when absent (ctx) */
  9860. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9861. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9862. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9863. test_wolfSSL_client_server(&client_cb, &server_cb);
  9864. /* sni abort - abort when absent (ssl) */
  9865. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9866. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9867. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9868. test_wolfSSL_client_server(&client_cb, &server_cb);
  9869. /* sni abort - success when overwritten */
  9870. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9871. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9872. 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;
  9873. test_wolfSSL_client_server(&client_cb, &server_cb);
  9874. /* sni abort - success when allowing mismatches */
  9875. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9876. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9877. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9878. test_wolfSSL_client_server(&client_cb, &server_cb);
  9879. }
  9880. res = TEST_RES_CHECK(1);
  9881. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9882. return res;
  9883. }
  9884. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9885. {
  9886. EXPECT_DECLS;
  9887. byte buff[] = { /* www.paypal.com */
  9888. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9889. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9890. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9891. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9892. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9893. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9894. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9895. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9896. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9897. };
  9898. byte buff2[] = { /* api.textmate.org */
  9899. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9900. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9901. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9902. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9903. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9904. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9905. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9906. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9907. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9908. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9909. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9910. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9911. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9912. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9913. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9914. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9915. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9916. };
  9917. byte buff3[] = { /* no sni extension */
  9918. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9919. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9920. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9921. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9922. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9923. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9924. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9925. };
  9926. byte buff4[] = { /* last extension has zero size */
  9927. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9928. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9929. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9930. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9931. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9932. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9933. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9934. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9935. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9936. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9937. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9938. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9939. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9940. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9941. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9942. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9943. 0x12, 0x00, 0x00
  9944. };
  9945. byte buff5[] = { /* SSL v2.0 client hello */
  9946. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9947. /* dummy bytes below, just to pass size check */
  9948. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9949. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9950. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9951. };
  9952. byte result[32] = {0};
  9953. word32 length = 32;
  9954. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9955. 0, result, &length));
  9956. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9957. 0, result, &length));
  9958. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9959. 1, result, &length));
  9960. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9961. 0, result, &length));
  9962. buff[0] = 0x16;
  9963. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9964. 0, result, &length));
  9965. buff[1] = 0x03;
  9966. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9967. sizeof(buff), 0, result, &length));
  9968. buff[2] = 0x03;
  9969. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9970. sizeof(buff), 0, result, &length));
  9971. buff[4] = 0x64;
  9972. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9973. 0, result, &length));
  9974. if (EXPECT_SUCCESS())
  9975. result[length] = 0;
  9976. ExpectStrEQ("www.paypal.com", (const char*) result);
  9977. length = 32;
  9978. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9979. 0, result, &length));
  9980. if (EXPECT_SUCCESS())
  9981. result[length] = 0;
  9982. ExpectStrEQ("api.textmate.org", (const char*) result);
  9983. /* SSL v2.0 tests */
  9984. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9985. sizeof(buff5), 0, result, &length));
  9986. buff5[2] = 0x02;
  9987. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9988. sizeof(buff5), 0, result, &length));
  9989. buff5[2] = 0x01; buff5[6] = 0x08;
  9990. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9991. sizeof(buff5), 0, result, &length));
  9992. buff5[6] = 0x09; buff5[8] = 0x01;
  9993. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9994. sizeof(buff5), 0, result, &length));
  9995. return EXPECT_RESULT();
  9996. }
  9997. #endif /* HAVE_SNI */
  9998. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9999. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  10000. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  10001. /* Dummy peer functions to satisfy the exporter/importer */
  10002. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  10003. int* ipSz, unsigned short* port, int* fam)
  10004. {
  10005. (void)ssl;
  10006. ip[0] = -1;
  10007. *ipSz = 1;
  10008. *port = 1;
  10009. *fam = 2;
  10010. return 1;
  10011. }
  10012. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  10013. int ipSz, unsigned short port, int fam)
  10014. {
  10015. (void)ssl;
  10016. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  10017. return 0;
  10018. return 1;
  10019. }
  10020. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  10021. WOLFSSL **ssl)
  10022. {
  10023. EXPECT_DECLS;
  10024. unsigned char* sessionBuf = NULL;
  10025. unsigned int sessionSz = 0;
  10026. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  10027. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  10028. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  10029. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  10030. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  10031. ExpectNotNull(sessionBuf =
  10032. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  10033. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  10034. wolfSSL_free(*ssl);
  10035. *ssl = NULL;
  10036. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  10037. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  10038. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  10039. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  10040. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10041. return EXPECT_RESULT();
  10042. }
  10043. #endif
  10044. static int test_wolfSSL_dtls_export_peers(void)
  10045. {
  10046. EXPECT_DECLS;
  10047. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  10048. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  10049. test_ssl_cbf client_cbf;
  10050. test_ssl_cbf server_cbf;
  10051. size_t i, j;
  10052. struct test_params {
  10053. method_provider client_meth;
  10054. method_provider server_meth;
  10055. const char* dtls_version;
  10056. } params[] = {
  10057. #ifndef NO_OLD_TLS
  10058. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  10059. #endif
  10060. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  10061. /* TODO DTLS 1.3 exporting not supported
  10062. #ifdef WOLFSSL_DTLS13
  10063. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  10064. #endif
  10065. */
  10066. };
  10067. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  10068. for (j = 0; j <= 0b11; j++) {
  10069. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  10070. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  10071. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  10072. client_cbf.method = params[i].client_meth;
  10073. server_cbf.method = params[i].server_meth;
  10074. if (j & 0b01) {
  10075. client_cbf.on_handshake =
  10076. test_wolfSSL_dtls_export_peers_on_handshake;
  10077. printf(" With client export;");
  10078. }
  10079. if (j & 0b10) {
  10080. server_cbf.on_handshake =
  10081. test_wolfSSL_dtls_export_peers_on_handshake;
  10082. printf(" With server export;");
  10083. }
  10084. printf("\n");
  10085. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  10086. &server_cbf, NULL), TEST_SUCCESS);
  10087. if (!EXPECT_SUCCESS())
  10088. break;
  10089. }
  10090. }
  10091. #endif
  10092. return EXPECT_RESULT();
  10093. }
  10094. static int test_wolfSSL_UseTrustedCA(void)
  10095. {
  10096. EXPECT_DECLS;
  10097. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  10098. && !defined(NO_RSA)
  10099. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10100. WOLFSSL_CTX *ctx = NULL;
  10101. WOLFSSL *ssl = NULL;
  10102. byte id[20];
  10103. #ifndef NO_WOLFSSL_SERVER
  10104. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  10105. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  10106. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  10107. #else
  10108. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  10109. #endif
  10110. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  10111. XMEMSET(id, 0, sizeof(id));
  10112. /* error cases */
  10113. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  10114. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  10115. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  10116. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  10117. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  10118. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  10119. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  10120. #ifdef NO_SHA
  10121. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  10122. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  10123. #endif
  10124. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  10125. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  10126. /* success cases */
  10127. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  10128. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  10129. #ifndef NO_SHA
  10130. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  10131. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  10132. #endif
  10133. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  10134. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  10135. wolfSSL_free(ssl);
  10136. wolfSSL_CTX_free(ctx);
  10137. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10138. #endif /* HAVE_TRUSTED_CA */
  10139. return EXPECT_RESULT();
  10140. }
  10141. static int test_wolfSSL_UseMaxFragment(void)
  10142. {
  10143. EXPECT_DECLS;
  10144. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  10145. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  10146. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10147. #ifndef NO_WOLFSSL_SERVER
  10148. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  10149. #else
  10150. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10151. #endif
  10152. WOLFSSL *ssl = NULL;
  10153. #ifdef OPENSSL_EXTRA
  10154. int (*UseMaxFragment)(SSL *s, unsigned char mode);
  10155. int (*CTX_UseMaxFragment)(SSL_CTX *c, unsigned char mode);
  10156. #else
  10157. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  10158. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  10159. #endif
  10160. #ifndef NO_WOLFSSL_SERVER
  10161. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  10162. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  10163. #endif
  10164. ExpectNotNull(ctx);
  10165. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10166. #ifdef OPENSSL_EXTRA
  10167. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  10168. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  10169. #else
  10170. UseMaxFragment = wolfSSL_UseMaxFragment;
  10171. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  10172. #endif
  10173. /* error cases */
  10174. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  10175. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  10176. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  10177. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  10178. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  10179. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  10180. /* success case */
  10181. #ifdef OPENSSL_EXTRA
  10182. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  10183. #else
  10184. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  10185. #endif
  10186. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  10187. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  10188. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  10189. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  10190. #ifdef OPENSSL_EXTRA
  10191. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  10192. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  10193. #else
  10194. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  10195. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  10196. #endif
  10197. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  10198. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  10199. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  10200. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  10201. #ifdef OPENSSL_EXTRA
  10202. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  10203. #else
  10204. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  10205. #endif
  10206. wolfSSL_free(ssl);
  10207. wolfSSL_CTX_free(ctx);
  10208. #if defined(OPENSSL_EXTRA) && defined(HAVE_MAX_FRAGMENT) && \
  10209. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  10210. /* check negotiated max fragment size */
  10211. {
  10212. WOLFSSL *ssl_c = NULL;
  10213. WOLFSSL *ssl_s = NULL;
  10214. struct test_memio_ctx test_ctx;
  10215. WOLFSSL_CTX *ctx_c = NULL;
  10216. WOLFSSL_CTX *ctx_s = NULL;
  10217. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  10218. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10219. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10220. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c, WOLFSSL_MFL_2_8),
  10221. WOLFSSL_SUCCESS);
  10222. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10223. #ifndef NO_SESSION_CACHE
  10224. ExpectIntEQ(SSL_SESSION_get_max_fragment_length(
  10225. wolfSSL_get_session(ssl_c)), WOLFSSL_MFL_2_8);
  10226. #endif
  10227. wolfSSL_free(ssl_c);
  10228. wolfSSL_free(ssl_s);
  10229. wolfSSL_CTX_free(ctx_c);
  10230. wolfSSL_CTX_free(ctx_s);
  10231. }
  10232. #endif
  10233. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10234. #endif
  10235. return EXPECT_RESULT();
  10236. }
  10237. static int test_wolfSSL_UseTruncatedHMAC(void)
  10238. {
  10239. EXPECT_DECLS;
  10240. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  10241. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  10242. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10243. #ifndef NO_WOLFSSL_SERVER
  10244. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  10245. #else
  10246. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10247. #endif
  10248. WOLFSSL *ssl = NULL;
  10249. ExpectNotNull(ctx);
  10250. #ifndef NO_WOLFSSL_SERVER
  10251. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  10252. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  10253. #endif
  10254. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10255. /* error cases */
  10256. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  10257. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  10258. /* success case */
  10259. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  10260. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  10261. wolfSSL_free(ssl);
  10262. wolfSSL_CTX_free(ctx);
  10263. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10264. #endif
  10265. return EXPECT_RESULT();
  10266. }
  10267. static int test_wolfSSL_UseSupportedCurve(void)
  10268. {
  10269. EXPECT_DECLS;
  10270. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  10271. !defined(NO_TLS)
  10272. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10273. WOLFSSL *ssl = wolfSSL_new(ctx);
  10274. ExpectNotNull(ctx);
  10275. ExpectNotNull(ssl);
  10276. /* error cases */
  10277. ExpectIntNE(WOLFSSL_SUCCESS,
  10278. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  10279. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  10280. ExpectIntNE(WOLFSSL_SUCCESS,
  10281. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  10282. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  10283. /* success case */
  10284. ExpectIntEQ(WOLFSSL_SUCCESS,
  10285. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  10286. ExpectIntEQ(WOLFSSL_SUCCESS,
  10287. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  10288. wolfSSL_free(ssl);
  10289. wolfSSL_CTX_free(ctx);
  10290. #endif
  10291. return EXPECT_RESULT();
  10292. }
  10293. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  10294. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  10295. {
  10296. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  10297. }
  10298. static void use_ALPN_all(WOLFSSL* ssl)
  10299. {
  10300. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  10301. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  10302. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  10303. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  10304. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10305. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  10306. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10307. }
  10308. static void use_ALPN_all_continue(WOLFSSL* ssl)
  10309. {
  10310. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  10311. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  10312. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  10313. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  10314. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10315. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  10316. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10317. }
  10318. static void use_ALPN_one(WOLFSSL* ssl)
  10319. {
  10320. /* spdy/2 */
  10321. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10322. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  10323. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10324. }
  10325. static void use_ALPN_unknown(WOLFSSL* ssl)
  10326. {
  10327. /* http/2.0 */
  10328. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  10329. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  10330. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10331. }
  10332. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  10333. {
  10334. /* http/2.0 */
  10335. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  10336. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  10337. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10338. }
  10339. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  10340. {
  10341. /* spdy/3 */
  10342. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10343. char *proto = NULL;
  10344. word16 protoSz = 0;
  10345. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  10346. /* check value */
  10347. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  10348. if (proto) {
  10349. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  10350. }
  10351. }
  10352. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  10353. {
  10354. char *proto = NULL;
  10355. word16 protoSz = 0;
  10356. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  10357. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  10358. /* check value */
  10359. AssertIntEQ(1, (0 == protoSz));
  10360. AssertIntEQ(1, (NULL == proto));
  10361. }
  10362. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  10363. {
  10364. /* http/1.1 */
  10365. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10366. char *proto;
  10367. word16 protoSz = 0;
  10368. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  10369. /* check value */
  10370. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10371. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10372. }
  10373. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  10374. {
  10375. /* spdy/2 */
  10376. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10377. char *proto;
  10378. word16 protoSz = 0;
  10379. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  10380. /* check value */
  10381. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10382. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10383. }
  10384. static void verify_ALPN_client_list(WOLFSSL* ssl)
  10385. {
  10386. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  10387. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  10388. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  10389. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  10390. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10391. char *clist = NULL;
  10392. word16 clistSz = 0;
  10393. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  10394. &clistSz));
  10395. /* check value */
  10396. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  10397. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  10398. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  10399. }
  10400. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10401. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10402. /* ALPN select callback, success with spdy/2 */
  10403. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  10404. unsigned char *outlen, const unsigned char *in,
  10405. unsigned int inlen, void *arg)
  10406. {
  10407. /* spdy/2 */
  10408. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10409. (void)ssl;
  10410. (void)arg;
  10411. /* adding +1 since LEN byte comes first */
  10412. if (inlen < sizeof(proto) + 1) {
  10413. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10414. }
  10415. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  10416. *out = in + 1;
  10417. *outlen = (unsigned char)sizeof(proto);
  10418. return SSL_TLSEXT_ERR_OK;
  10419. }
  10420. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10421. }
  10422. /* ALPN select callback, force failure */
  10423. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  10424. unsigned char *outlen, const unsigned char *in,
  10425. unsigned int inlen, void *arg)
  10426. {
  10427. (void)ssl;
  10428. (void)out;
  10429. (void)outlen;
  10430. (void)in;
  10431. (void)inlen;
  10432. (void)arg;
  10433. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10434. }
  10435. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  10436. {
  10437. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  10438. }
  10439. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  10440. {
  10441. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  10442. }
  10443. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  10444. static int test_wolfSSL_UseALPN_connection(void)
  10445. {
  10446. int res = TEST_SKIPPED;
  10447. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10448. callback_functions client_cb;
  10449. callback_functions server_cb;
  10450. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10451. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10452. client_cb.method = wolfSSLv23_client_method;
  10453. server_cb.method = wolfSSLv23_server_method;
  10454. client_cb.devId = testDevId;
  10455. server_cb.devId = testDevId;
  10456. /* success case same list */
  10457. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10458. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  10459. test_wolfSSL_client_server(&client_cb, &server_cb);
  10460. /* success case only one for server */
  10461. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10462. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  10463. test_wolfSSL_client_server(&client_cb, &server_cb);
  10464. /* success case only one for client */
  10465. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  10466. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  10467. test_wolfSSL_client_server(&client_cb, &server_cb);
  10468. /* success case none for client */
  10469. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10470. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  10471. test_wolfSSL_client_server(&client_cb, &server_cb);
  10472. /* success case mismatch behavior but option 'continue' set */
  10473. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  10474. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  10475. test_wolfSSL_client_server(&client_cb, &server_cb);
  10476. /* success case read protocol send by client */
  10477. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10478. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  10479. test_wolfSSL_client_server(&client_cb, &server_cb);
  10480. /* mismatch behavior with same list
  10481. * the first and only this one must be taken */
  10482. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10483. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  10484. test_wolfSSL_client_server(&client_cb, &server_cb);
  10485. /* default mismatch behavior */
  10486. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10487. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10488. test_wolfSSL_client_server(&client_cb, &server_cb);
  10489. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10490. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10491. /* WOLFSSL-level ALPN select callback tests */
  10492. /* Callback: success (one protocol, spdy/2) */
  10493. client_cb.ctx_ready = NULL;
  10494. client_cb.ssl_ready = use_ALPN_one;
  10495. client_cb.on_result = verify_ALPN_matching_spdy2;
  10496. server_cb.ctx_ready = NULL;
  10497. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  10498. server_cb.on_result = verify_ALPN_matching_spdy2;
  10499. test_wolfSSL_client_server(&client_cb, &server_cb);
  10500. /* Callback: failure (one client protocol, spdy/2) */
  10501. client_cb.ctx_ready = NULL;
  10502. client_cb.ssl_ready = use_ALPN_one;
  10503. client_cb.on_result = NULL;
  10504. server_cb.ctx_ready = NULL;
  10505. server_cb.ssl_ready = use_ALPN_failure_callback;
  10506. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10507. test_wolfSSL_client_server(&client_cb, &server_cb);
  10508. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  10509. res = TEST_RES_CHECK(1);
  10510. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10511. return res;
  10512. }
  10513. static int test_wolfSSL_UseALPN_params(void)
  10514. {
  10515. EXPECT_DECLS;
  10516. #ifndef NO_WOLFSSL_CLIENT
  10517. /* "http/1.1" */
  10518. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10519. /* "spdy/1" */
  10520. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  10521. /* "spdy/2" */
  10522. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10523. /* "spdy/3" */
  10524. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10525. char buff[256];
  10526. word32 idx;
  10527. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10528. WOLFSSL *ssl = wolfSSL_new(ctx);
  10529. ExpectNotNull(ctx);
  10530. ExpectNotNull(ssl);
  10531. /* error cases */
  10532. ExpectIntNE(WOLFSSL_SUCCESS,
  10533. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  10534. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10535. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  10536. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10537. /* success case */
  10538. /* http1 only */
  10539. ExpectIntEQ(WOLFSSL_SUCCESS,
  10540. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  10541. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10542. /* http1, spdy1 */
  10543. XMEMCPY(buff, http1, sizeof(http1));
  10544. idx = sizeof(http1);
  10545. buff[idx++] = ',';
  10546. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10547. idx += sizeof(spdy1);
  10548. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10549. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10550. /* http1, spdy2, spdy1 */
  10551. XMEMCPY(buff, http1, sizeof(http1));
  10552. idx = sizeof(http1);
  10553. buff[idx++] = ',';
  10554. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10555. idx += sizeof(spdy2);
  10556. buff[idx++] = ',';
  10557. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10558. idx += sizeof(spdy1);
  10559. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10560. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10561. /* spdy3, http1, spdy2, spdy1 */
  10562. XMEMCPY(buff, spdy3, sizeof(spdy3));
  10563. idx = sizeof(spdy3);
  10564. buff[idx++] = ',';
  10565. XMEMCPY(buff+idx, http1, sizeof(http1));
  10566. idx += sizeof(http1);
  10567. buff[idx++] = ',';
  10568. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10569. idx += sizeof(spdy2);
  10570. buff[idx++] = ',';
  10571. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10572. idx += sizeof(spdy1);
  10573. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10574. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10575. wolfSSL_free(ssl);
  10576. wolfSSL_CTX_free(ctx);
  10577. #endif
  10578. return EXPECT_RESULT();
  10579. }
  10580. #endif /* HAVE_ALPN */
  10581. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  10582. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  10583. {
  10584. unsigned char p[] = {
  10585. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10586. 6, 's', 'p', 'd', 'y', '/', '2',
  10587. 6, 's', 'p', 'd', 'y', '/', '1',
  10588. };
  10589. unsigned char p_len = sizeof(p);
  10590. int ret;
  10591. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  10592. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10593. AssertIntEQ(ret, 0);
  10594. #else
  10595. AssertIntEQ(ret, SSL_SUCCESS);
  10596. #endif
  10597. }
  10598. static void set_alpn_protos(SSL* ssl)
  10599. {
  10600. unsigned char p[] = {
  10601. 6, 's', 'p', 'd', 'y', '/', '3',
  10602. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10603. 6, 's', 'p', 'd', 'y', '/', '2',
  10604. 6, 's', 'p', 'd', 'y', '/', '1',
  10605. };
  10606. unsigned char p_len = sizeof(p);
  10607. int ret;
  10608. ret = SSL_set_alpn_protos(ssl, p, p_len);
  10609. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10610. AssertIntEQ(ret, 0);
  10611. #else
  10612. AssertIntEQ(ret, SSL_SUCCESS);
  10613. #endif
  10614. }
  10615. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  10616. {
  10617. /* "spdy/3" */
  10618. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10619. const unsigned char *proto;
  10620. unsigned int protoSz = 0;
  10621. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10622. /* check value */
  10623. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10624. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10625. }
  10626. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  10627. {
  10628. /* "http/1.1" */
  10629. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10630. const unsigned char *proto;
  10631. unsigned int protoSz = 0;
  10632. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10633. /* check value */
  10634. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10635. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10636. }
  10637. static int test_wolfSSL_set_alpn_protos(void)
  10638. {
  10639. int res = TEST_SKIPPED;
  10640. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10641. callback_functions client_cb;
  10642. callback_functions server_cb;
  10643. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10644. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10645. client_cb.method = wolfSSLv23_client_method;
  10646. server_cb.method = wolfSSLv23_server_method;
  10647. client_cb.devId = testDevId;
  10648. server_cb.devId = testDevId;
  10649. /* use CTX_alpn_protos */
  10650. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10651. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  10652. test_wolfSSL_client_server(&client_cb, &server_cb);
  10653. /* use set_alpn_protos */
  10654. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  10655. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  10656. test_wolfSSL_client_server(&client_cb, &server_cb);
  10657. res = TEST_SUCCESS;
  10658. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10659. return res;
  10660. }
  10661. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  10662. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  10663. {
  10664. EXPECT_DECLS;
  10665. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  10666. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10667. WOLFSSL *ssl = wolfSSL_new(ctx);
  10668. ExpectNotNull(ctx);
  10669. ExpectNotNull(ssl);
  10670. /* error cases */
  10671. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  10672. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  10673. /* success cases */
  10674. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  10675. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  10676. wolfSSL_free(ssl);
  10677. wolfSSL_CTX_free(ctx);
  10678. #endif
  10679. return EXPECT_RESULT();
  10680. }
  10681. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  10682. {
  10683. EXPECT_DECLS;
  10684. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  10685. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10686. WOLFSSL *ssl = wolfSSL_new(ctx);
  10687. ExpectNotNull(ctx);
  10688. ExpectNotNull(ssl);
  10689. /* error cases */
  10690. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  10691. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  10692. /* success cases */
  10693. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  10694. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  10695. wolfSSL_free(ssl);
  10696. wolfSSL_CTX_free(ctx);
  10697. #endif
  10698. return EXPECT_RESULT();
  10699. }
  10700. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  10701. static int test_wolfSSL_SCR_Reconnect(void)
  10702. {
  10703. EXPECT_DECLS;
  10704. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  10705. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  10706. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  10707. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  10708. struct test_memio_ctx test_ctx;
  10709. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  10710. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  10711. byte data;
  10712. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  10713. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  10714. test_ctx.s_ciphers =
  10715. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  10716. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10717. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10718. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  10719. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  10720. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  10721. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  10722. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10723. /* WOLFSSL_FATAL_ERROR since it will block */
  10724. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  10725. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10726. WOLFSSL_ERROR_WANT_READ);
  10727. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  10728. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10729. WOLFSSL_ERROR_WANT_READ);
  10730. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10731. wolfSSL_free(ssl_c);
  10732. ssl_c = NULL;
  10733. wolfSSL_free(ssl_s);
  10734. ssl_s = NULL;
  10735. wolfSSL_CTX_free(ctx_c);
  10736. ctx_c = NULL;
  10737. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  10738. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10739. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10740. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10741. wolfSSL_free(ssl_s);
  10742. wolfSSL_free(ssl_c);
  10743. wolfSSL_CTX_free(ctx_s);
  10744. wolfSSL_CTX_free(ctx_c);
  10745. #endif
  10746. return EXPECT_RESULT();
  10747. }
  10748. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  10749. (!defined(NO_RSA) || defined(HAVE_ECC))
  10750. /* Called when writing. */
  10751. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10752. {
  10753. (void)ssl;
  10754. (void)buf;
  10755. (void)sz;
  10756. (void)ctx;
  10757. /* Force error return from wolfSSL_accept_TLSv13(). */
  10758. return WANT_WRITE;
  10759. }
  10760. /* Called when reading. */
  10761. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10762. {
  10763. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  10764. int len = (int)msg->length;
  10765. (void)ssl;
  10766. (void)sz;
  10767. /* Pass back as much of message as will fit in buffer. */
  10768. if (len > sz)
  10769. len = sz;
  10770. XMEMCPY(buf, msg->buffer, len);
  10771. /* Move over returned data. */
  10772. msg->buffer += len;
  10773. msg->length -= len;
  10774. /* Amount actually copied. */
  10775. return len;
  10776. }
  10777. #endif
  10778. /* Test the detection of duplicate known TLS extensions.
  10779. * Specifically in a ClientHello.
  10780. */
  10781. static int test_tls_ext_duplicate(void)
  10782. {
  10783. EXPECT_DECLS;
  10784. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  10785. !defined(NO_FILESYSTEM)
  10786. const unsigned char clientHelloDupTlsExt[] = {
  10787. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  10788. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  10789. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  10790. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  10791. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  10792. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  10793. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  10794. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  10795. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10796. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  10797. 0x00, 0x9e, 0x01, 0x00,
  10798. /* Extensions - duplicate signature algorithms. */
  10799. 0x00, 0x19, 0x00, 0x0d,
  10800. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  10801. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  10802. /* Supported Versions extension for TLS 1.3. */
  10803. 0x00, 0x2b,
  10804. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  10805. };
  10806. WOLFSSL_BUFFER_INFO msg;
  10807. const char* testCertFile;
  10808. const char* testKeyFile;
  10809. WOLFSSL_CTX *ctx = NULL;
  10810. WOLFSSL *ssl = NULL;
  10811. #ifndef NO_RSA
  10812. testCertFile = svrCertFile;
  10813. testKeyFile = svrKeyFile;
  10814. #elif defined(HAVE_ECC)
  10815. testCertFile = eccCertFile;
  10816. testKeyFile = eccKeyFile;
  10817. #endif
  10818. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10819. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  10820. WOLFSSL_FILETYPE_PEM));
  10821. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  10822. WOLFSSL_FILETYPE_PEM));
  10823. /* Read from 'msg'. */
  10824. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  10825. /* No where to send to - dummy sender. */
  10826. wolfSSL_SetIOSend(ctx, DummySend);
  10827. ssl = wolfSSL_new(ctx);
  10828. ExpectNotNull(ssl);
  10829. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  10830. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10831. wolfSSL_SetIOReadCtx(ssl, &msg);
  10832. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10833. /* can return duplicate ext error or socket error if the peer closed down
  10834. * while sending alert */
  10835. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10836. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10837. }
  10838. wolfSSL_free(ssl);
  10839. wolfSSL_CTX_free(ctx);
  10840. #endif
  10841. return EXPECT_RESULT();
  10842. }
  10843. /*----------------------------------------------------------------------------*
  10844. | X509 Tests
  10845. *----------------------------------------------------------------------------*/
  10846. static int test_wolfSSL_X509_NAME_get_entry(void)
  10847. {
  10848. EXPECT_DECLS;
  10849. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10850. #if defined(OPENSSL_ALL) || \
  10851. (defined(OPENSSL_EXTRA) && \
  10852. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10853. /* use openssl like name to test mapping */
  10854. X509_NAME_ENTRY* ne = NULL;
  10855. X509_NAME* name = NULL;
  10856. X509* x509 = NULL;
  10857. #ifndef NO_FILESYSTEM
  10858. ASN1_STRING* asn = NULL;
  10859. char* subCN = NULL;
  10860. #endif
  10861. int idx = 0;
  10862. ASN1_OBJECT *object = NULL;
  10863. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10864. defined(WOLFSSL_NGINX)
  10865. #ifndef NO_BIO
  10866. BIO* bio = NULL;
  10867. #endif
  10868. #endif
  10869. #ifndef NO_FILESYSTEM
  10870. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10871. WOLFSSL_FILETYPE_PEM));
  10872. ExpectNotNull(name = X509_get_subject_name(x509));
  10873. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10874. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10875. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10876. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10877. wolfSSL_FreeX509(x509);
  10878. x509 = NULL;
  10879. #endif
  10880. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10881. WOLFSSL_FILETYPE_PEM));
  10882. ExpectNotNull(name = X509_get_subject_name(x509));
  10883. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10884. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10885. defined(WOLFSSL_NGINX)
  10886. #ifndef NO_BIO
  10887. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10888. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10889. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10890. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10891. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10892. BIO_free(bio);
  10893. #endif
  10894. #endif
  10895. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10896. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10897. wolfSSL_FreeX509(x509);
  10898. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10899. #endif /* !NO_CERTS && !NO_RSA */
  10900. return EXPECT_RESULT();
  10901. }
  10902. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10903. static int test_wolfSSL_PKCS12(void)
  10904. {
  10905. EXPECT_DECLS;
  10906. /* .p12 file is encrypted with DES3 */
  10907. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10908. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10909. * Password Key
  10910. */
  10911. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10912. !defined(NO_STDIO_FILESYSTEM) && \
  10913. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10914. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10915. byte buf[6000];
  10916. char file[] = "./certs/test-servercert.p12";
  10917. char order[] = "./certs/ecc-rsa-server.p12";
  10918. #ifdef WC_RC2
  10919. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10920. #endif
  10921. char pass[] = "a password";
  10922. const char goodPsw[] = "wolfSSL test";
  10923. const char badPsw[] = "bad";
  10924. #ifdef HAVE_ECC
  10925. WOLFSSL_X509_NAME *subject = NULL;
  10926. WOLFSSL_X509 *x509 = NULL;
  10927. #endif
  10928. XFILE f = XBADFILE;
  10929. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10930. WOLFSSL_BIO *bio = NULL;
  10931. WOLFSSL_EVP_PKEY *pkey = NULL;
  10932. WC_PKCS12 *pkcs12 = NULL;
  10933. WC_PKCS12 *pkcs12_2 = NULL;
  10934. WOLFSSL_X509 *cert = NULL;
  10935. WOLFSSL_X509 *tmp = NULL;
  10936. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10937. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10938. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10939. WOLFSSL_CTX *ctx = NULL;
  10940. WOLFSSL *ssl = NULL;
  10941. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10942. #endif
  10943. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10944. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10945. if (f != XBADFILE) {
  10946. XFCLOSE(f);
  10947. f = XBADFILE;
  10948. }
  10949. goodPswLen = (int)XSTRLEN(goodPsw);
  10950. badPswLen = (int)XSTRLEN(badPsw);
  10951. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10952. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10953. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10954. ExpectNotNull(pkcs12);
  10955. BIO_free(bio);
  10956. bio = NULL;
  10957. /* check verify MAC directly */
  10958. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10959. /* check verify MAC fail case directly */
  10960. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10961. /* check verify MAC fail case */
  10962. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10963. ExpectNull(pkey);
  10964. ExpectNull(cert);
  10965. /* check parse with no extra certs kept */
  10966. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10967. 1);
  10968. ExpectNotNull(pkey);
  10969. ExpectNotNull(cert);
  10970. wolfSSL_EVP_PKEY_free(pkey);
  10971. pkey = NULL;
  10972. wolfSSL_X509_free(cert);
  10973. cert = NULL;
  10974. /* check parse with extra certs kept */
  10975. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10976. 1);
  10977. ExpectNotNull(pkey);
  10978. ExpectNotNull(cert);
  10979. ExpectNotNull(ca);
  10980. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10981. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10982. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10983. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10984. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10985. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10986. #else
  10987. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10988. #endif
  10989. /* Copy stack structure */
  10990. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10991. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10992. /* CTX now owns the tmp_ca stack structure */
  10993. tmp_ca = NULL;
  10994. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10995. ExpectNotNull(tmp_ca);
  10996. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10997. /* Check that the main cert is also set */
  10998. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10999. ExpectNotNull(ssl = SSL_new(ctx));
  11000. ExpectNotNull(SSL_get_certificate(ssl));
  11001. SSL_free(ssl);
  11002. SSL_CTX_free(ctx);
  11003. ctx = NULL;
  11004. #endif
  11005. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11006. /* should be 2 other certs on stack */
  11007. ExpectNotNull(tmp = sk_X509_pop(ca));
  11008. X509_free(tmp);
  11009. ExpectNotNull(tmp = sk_X509_pop(ca));
  11010. X509_free(tmp);
  11011. ExpectNull(sk_X509_pop(ca));
  11012. EVP_PKEY_free(pkey);
  11013. pkey = NULL;
  11014. X509_free(cert);
  11015. cert = NULL;
  11016. sk_X509_pop_free(ca, X509_free);
  11017. ca = NULL;
  11018. /* check PKCS12_create */
  11019. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  11020. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  11021. SSL_SUCCESS);
  11022. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  11023. -1, -1, 100, -1, 0)));
  11024. EVP_PKEY_free(pkey);
  11025. pkey = NULL;
  11026. X509_free(cert);
  11027. cert = NULL;
  11028. sk_X509_pop_free(ca, NULL);
  11029. ca = NULL;
  11030. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  11031. SSL_SUCCESS);
  11032. PKCS12_free(pkcs12_2);
  11033. pkcs12_2 = NULL;
  11034. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  11035. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  11036. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  11037. 2000, 1, 0)));
  11038. EVP_PKEY_free(pkey);
  11039. pkey = NULL;
  11040. X509_free(cert);
  11041. cert = NULL;
  11042. sk_X509_pop_free(ca, NULL);
  11043. ca = NULL;
  11044. /* convert to DER then back and parse */
  11045. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  11046. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  11047. PKCS12_free(pkcs12_2);
  11048. pkcs12_2 = NULL;
  11049. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  11050. BIO_free(bio);
  11051. bio = NULL;
  11052. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  11053. SSL_SUCCESS);
  11054. /* should be 2 other certs on stack */
  11055. ExpectNotNull(tmp = sk_X509_pop(ca));
  11056. X509_free(tmp);
  11057. ExpectNotNull(tmp = sk_X509_pop(ca));
  11058. X509_free(tmp);
  11059. ExpectNull(sk_X509_pop(ca));
  11060. #ifndef NO_RC4
  11061. PKCS12_free(pkcs12_2);
  11062. pkcs12_2 = NULL;
  11063. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  11064. NID_pbe_WithSHA1And128BitRC4,
  11065. NID_pbe_WithSHA1And128BitRC4,
  11066. 2000, 1, 0)));
  11067. EVP_PKEY_free(pkey);
  11068. pkey = NULL;
  11069. X509_free(cert);
  11070. cert = NULL;
  11071. sk_X509_pop_free(ca, NULL);
  11072. ca = NULL;
  11073. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  11074. SSL_SUCCESS);
  11075. #endif /* NO_RC4 */
  11076. EVP_PKEY_free(pkey);
  11077. pkey = NULL;
  11078. X509_free(cert);
  11079. cert = NULL;
  11080. PKCS12_free(pkcs12);
  11081. pkcs12 = NULL;
  11082. PKCS12_free(pkcs12_2);
  11083. pkcs12_2 = NULL;
  11084. sk_X509_pop_free(ca, NULL);
  11085. ca = NULL;
  11086. #ifdef HAVE_ECC
  11087. /* test order of parsing */
  11088. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  11089. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  11090. if (f != XBADFILE) {
  11091. XFCLOSE(f);
  11092. f = XBADFILE;
  11093. }
  11094. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  11095. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  11096. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  11097. WOLFSSL_SUCCESS);
  11098. /* check use of pkey after parse */
  11099. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  11100. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  11101. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11102. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  11103. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11104. #else
  11105. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11106. #endif
  11107. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  11108. SSL_CTX_free(ctx);
  11109. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11110. #endif
  11111. ExpectNotNull(pkey);
  11112. ExpectNotNull(cert);
  11113. ExpectNotNull(ca);
  11114. /* compare subject lines of certificates */
  11115. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  11116. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  11117. SSL_FILETYPE_PEM));
  11118. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  11119. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  11120. X509_free(x509);
  11121. x509 = NULL;
  11122. /* test expected fail case */
  11123. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  11124. SSL_FILETYPE_PEM));
  11125. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  11126. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  11127. X509_free(x509);
  11128. x509 = NULL;
  11129. X509_free(cert);
  11130. cert = NULL;
  11131. /* get subject line from ca stack */
  11132. ExpectNotNull(cert = sk_X509_pop(ca));
  11133. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  11134. /* compare subject from certificate in ca to expected */
  11135. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  11136. SSL_FILETYPE_PEM));
  11137. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  11138. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  11139. /* modify case and compare subject from certificate in ca to expected.
  11140. * The first bit of the name is:
  11141. * /C=US/ST=Washington
  11142. * So we'll change subject->name[1] to 'c' (lower case) */
  11143. if (subject != NULL) {
  11144. subject->name[1] = 'c';
  11145. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  11146. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  11147. }
  11148. EVP_PKEY_free(pkey);
  11149. pkey = NULL;
  11150. X509_free(x509);
  11151. x509 = NULL;
  11152. X509_free(cert);
  11153. cert = NULL;
  11154. BIO_free(bio);
  11155. bio = NULL;
  11156. PKCS12_free(pkcs12);
  11157. pkcs12 = NULL;
  11158. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  11159. ca = NULL;
  11160. /* test order of parsing */
  11161. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  11162. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  11163. if (f != XBADFILE) {
  11164. XFCLOSE(f);
  11165. f = XBADFILE;
  11166. }
  11167. /* check verify MAC fail case */
  11168. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  11169. ExpectNull(pkey);
  11170. ExpectNull(cert);
  11171. /* check parse with no extra certs kept */
  11172. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  11173. 1);
  11174. ExpectNotNull(pkey);
  11175. ExpectNotNull(cert);
  11176. wolfSSL_EVP_PKEY_free(pkey);
  11177. pkey = NULL;
  11178. wolfSSL_X509_free(cert);
  11179. cert = NULL;
  11180. /* check parse with extra certs kept */
  11181. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  11182. 1);
  11183. ExpectNotNull(pkey);
  11184. ExpectNotNull(cert);
  11185. ExpectNotNull(ca);
  11186. wolfSSL_EVP_PKEY_free(pkey);
  11187. pkey = NULL;
  11188. wolfSSL_X509_free(cert);
  11189. cert = NULL;
  11190. sk_X509_pop_free(ca, NULL);
  11191. ca = NULL;
  11192. PKCS12_free(pkcs12);
  11193. pkcs12 = NULL;
  11194. #endif /* HAVE_ECC */
  11195. #ifdef WC_RC2
  11196. /* test PKCS#12 with RC2 encryption */
  11197. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  11198. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  11199. if (f != XBADFILE) {
  11200. XFCLOSE(f);
  11201. f = XBADFILE;
  11202. }
  11203. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  11204. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  11205. /* check verify MAC fail case */
  11206. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  11207. ExpectNull(pkey);
  11208. ExpectNull(cert);
  11209. /* check parse with not extra certs kept */
  11210. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  11211. WOLFSSL_SUCCESS);
  11212. ExpectNotNull(pkey);
  11213. ExpectNotNull(cert);
  11214. wolfSSL_EVP_PKEY_free(pkey);
  11215. pkey = NULL;
  11216. wolfSSL_X509_free(cert);
  11217. cert = NULL;
  11218. /* check parse with extra certs kept */
  11219. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  11220. WOLFSSL_SUCCESS);
  11221. ExpectNotNull(pkey);
  11222. ExpectNotNull(cert);
  11223. ExpectNotNull(ca);
  11224. wolfSSL_EVP_PKEY_free(pkey);
  11225. wolfSSL_X509_free(cert);
  11226. sk_X509_pop_free(ca, NULL);
  11227. BIO_free(bio);
  11228. bio = NULL;
  11229. PKCS12_free(pkcs12);
  11230. pkcs12 = NULL;
  11231. #endif /* WC_RC2 */
  11232. /* Test i2d_PKCS12_bio */
  11233. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  11234. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  11235. if (f != XBADFILE)
  11236. XFCLOSE(f);
  11237. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  11238. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  11239. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  11240. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  11241. PKCS12_free(pkcs12);
  11242. BIO_free(bio);
  11243. (void)order;
  11244. #endif /* OPENSSL_EXTRA */
  11245. #endif /* HAVE_FIPS */
  11246. return EXPECT_RESULT();
  11247. }
  11248. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  11249. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  11250. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  11251. #define TEST_PKCS8_ENC
  11252. #endif
  11253. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  11254. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  11255. /* used to keep track if FailTestCallback was called */
  11256. static int failTestCallbackCalled = 0;
  11257. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  11258. {
  11259. (void)passwd;
  11260. (void)sz;
  11261. (void)rw;
  11262. (void)userdata;
  11263. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  11264. failTestCallbackCalled = 1;
  11265. return -1;
  11266. }
  11267. #endif
  11268. static int test_wolfSSL_no_password_cb(void)
  11269. {
  11270. EXPECT_DECLS;
  11271. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  11272. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  11273. WOLFSSL_CTX* ctx = NULL;
  11274. byte buff[FOURK_BUF];
  11275. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  11276. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  11277. XFILE f = XBADFILE;
  11278. int bytes = 0;
  11279. #ifndef NO_WOLFSSL_CLIENT
  11280. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  11281. #else
  11282. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  11283. #endif
  11284. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  11285. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11286. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11287. if (f != XBADFILE) {
  11288. XFCLOSE(f);
  11289. f = XBADFILE;
  11290. }
  11291. ExpectIntLE(bytes, sizeof(buff));
  11292. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11293. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11294. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  11295. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11296. if (f != XBADFILE)
  11297. XFCLOSE(f);
  11298. ExpectIntLE(bytes, sizeof(buff));
  11299. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11300. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11301. wolfSSL_CTX_free(ctx);
  11302. /* Password callback should not be called by default */
  11303. ExpectIntEQ(failTestCallbackCalled, 0);
  11304. #endif
  11305. return EXPECT_RESULT();
  11306. }
  11307. #ifdef TEST_PKCS8_ENC
  11308. /* for PKCS8 test case */
  11309. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  11310. {
  11311. int flag = 0;
  11312. (void)rw;
  11313. if (userdata != NULL) {
  11314. flag = *((int*)userdata); /* user set data */
  11315. }
  11316. switch (flag) {
  11317. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  11318. * can be anything the user wishes to be passed to the callback
  11319. * associated with the WOLFSSL_CTX */
  11320. XSTRNCPY(passwd, "yassl123", sz);
  11321. return 8;
  11322. default:
  11323. return BAD_FUNC_ARG;
  11324. }
  11325. }
  11326. #endif /* TEST_PKCS8_ENC */
  11327. /* Testing functions dealing with PKCS8 */
  11328. static int test_wolfSSL_PKCS8(void)
  11329. {
  11330. EXPECT_DECLS;
  11331. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  11332. !defined(WOLFCRYPT_ONLY)
  11333. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11334. byte buff[FOURK_BUF];
  11335. byte der[FOURK_BUF];
  11336. #ifndef NO_RSA
  11337. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  11338. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  11339. #endif
  11340. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  11341. #ifdef HAVE_ECC
  11342. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  11343. #endif
  11344. XFILE f = XBADFILE;
  11345. int bytes = 0;
  11346. WOLFSSL_CTX* ctx = NULL;
  11347. #if defined(HAVE_ECC) && !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  11348. int ret;
  11349. ecc_key key;
  11350. word32 x = 0;
  11351. #endif
  11352. #ifdef TEST_PKCS8_ENC
  11353. #if !defined(NO_RSA) && !defined(NO_SHA)
  11354. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  11355. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  11356. #endif
  11357. #if defined(HAVE_ECC) && !defined(NO_SHA)
  11358. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  11359. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  11360. #endif
  11361. int flag;
  11362. #endif
  11363. (void)der;
  11364. #ifndef NO_WOLFSSL_CLIENT
  11365. #ifndef WOLFSSL_NO_TLS12
  11366. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11367. #else
  11368. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11369. #endif
  11370. #else
  11371. #ifndef WOLFSSL_NO_TLS12
  11372. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11373. #else
  11374. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11375. #endif
  11376. #endif
  11377. #ifdef TEST_PKCS8_ENC
  11378. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  11379. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  11380. flag = 1; /* used by password callback as return code */
  11381. #if !defined(NO_RSA) && !defined(NO_SHA)
  11382. /* test loading PEM PKCS8 encrypted file */
  11383. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  11384. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11385. if (f != XBADFILE) {
  11386. XFCLOSE(f);
  11387. f = XBADFILE;
  11388. }
  11389. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11390. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11391. /* this next case should fail because of password callback return code */
  11392. flag = 0; /* used by password callback as return code */
  11393. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11394. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11395. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11396. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11397. "yassl123"), 0);
  11398. /* test that error value is returned with a bad password */
  11399. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11400. "bad"), 0);
  11401. /* test loading PEM PKCS8 encrypted file */
  11402. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  11403. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11404. if (f != XBADFILE) {
  11405. XFCLOSE(f);
  11406. f = XBADFILE;
  11407. }
  11408. flag = 1; /* used by password callback as return code */
  11409. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11410. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11411. /* this next case should fail because of password callback return code */
  11412. flag = 0; /* used by password callback as return code */
  11413. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11414. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11415. #endif /* !NO_RSA && !NO_SHA */
  11416. #if defined(HAVE_ECC) && !defined(NO_SHA)
  11417. /* test loading PEM PKCS8 encrypted ECC Key file */
  11418. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  11419. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11420. if (f != XBADFILE) {
  11421. XFCLOSE(f);
  11422. f = XBADFILE;
  11423. }
  11424. flag = 1; /* used by password callback as return code */
  11425. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11426. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11427. /* this next case should fail because of password callback return code */
  11428. flag = 0; /* used by password callback as return code */
  11429. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11430. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11431. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11432. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11433. "yassl123"), 0);
  11434. /* test that error value is returned with a bad password */
  11435. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11436. "bad"), 0);
  11437. /* test loading DER PKCS8 encrypted ECC Key file */
  11438. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  11439. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11440. if (f != XBADFILE) {
  11441. XFCLOSE(f);
  11442. f = XBADFILE;
  11443. }
  11444. flag = 1; /* used by password callback as return code */
  11445. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11446. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11447. /* this next case should fail because of password callback return code */
  11448. flag = 0; /* used by password callback as return code */
  11449. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11450. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11451. /* leave flag as "okay" */
  11452. flag = 1;
  11453. #endif /* HAVE_ECC && !NO_SHA */
  11454. #endif /* TEST_PKCS8_ENC */
  11455. #ifndef NO_RSA
  11456. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  11457. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  11458. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11459. if (f != XBADFILE) {
  11460. XFCLOSE(f);
  11461. f = XBADFILE;
  11462. }
  11463. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11464. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11465. /* test loading PEM PKCS8 private key file (not encrypted) */
  11466. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  11467. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11468. if (f != XBADFILE) {
  11469. XFCLOSE(f);
  11470. f = XBADFILE;
  11471. }
  11472. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11473. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11474. #endif /* !NO_RSA */
  11475. /* Test PKCS8 PEM ECC key no crypt */
  11476. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  11477. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11478. if (f != XBADFILE) {
  11479. XFCLOSE(f);
  11480. f = XBADFILE;
  11481. }
  11482. #ifdef HAVE_ECC
  11483. /* Test PKCS8 PEM ECC key no crypt */
  11484. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11485. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11486. #if !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  11487. /* decrypt PKCS8 PEM to key in DER format */
  11488. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  11489. (word32)sizeof(der), NULL)), 0);
  11490. ret = wc_ecc_init(&key);
  11491. if (ret == 0) {
  11492. ret = wc_EccPrivateKeyDecode(der, &x, &key, (word32)bytes);
  11493. wc_ecc_free(&key);
  11494. }
  11495. ExpectIntEQ(ret, 0);
  11496. #endif
  11497. /* Test PKCS8 DER ECC key no crypt */
  11498. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11499. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11500. if (f != XBADFILE)
  11501. XFCLOSE(f);
  11502. /* Test using a PKCS8 ECC PEM */
  11503. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11504. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11505. #else
  11506. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  11507. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  11508. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  11509. #endif /* HAVE_ECC */
  11510. wolfSSL_CTX_free(ctx);
  11511. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11512. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  11513. return EXPECT_RESULT();
  11514. }
  11515. static int test_wolfSSL_PKCS8_ED25519(void)
  11516. {
  11517. EXPECT_DECLS;
  11518. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11519. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  11520. defined(HAVE_ED25519_KEY_IMPORT)
  11521. const byte encPrivKey[] = \
  11522. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11523. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  11524. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  11525. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  11526. "2L9W4v7swXkV+X+a1ww=\n"
  11527. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11528. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11529. byte der[FOURK_BUF];
  11530. WOLFSSL_CTX* ctx = NULL;
  11531. int bytes;
  11532. XMEMSET(der, 0, sizeof(der));
  11533. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11534. (word32)sizeof(der), password)), 0);
  11535. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11536. #ifndef NO_WOLFSSL_SERVER
  11537. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11538. #else
  11539. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11540. #endif
  11541. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11542. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11543. wolfSSL_CTX_free(ctx);
  11544. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11545. #endif
  11546. return EXPECT_RESULT();
  11547. }
  11548. static int test_wolfSSL_PKCS8_ED448(void)
  11549. {
  11550. EXPECT_DECLS;
  11551. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11552. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  11553. defined(HAVE_ED448_KEY_IMPORT)
  11554. const byte encPrivKey[] = \
  11555. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11556. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  11557. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  11558. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  11559. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  11560. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11561. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11562. byte der[FOURK_BUF];
  11563. WOLFSSL_CTX* ctx = NULL;
  11564. int bytes;
  11565. XMEMSET(der, 0, sizeof(der));
  11566. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11567. (word32)sizeof(der), password)), 0);
  11568. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11569. #ifndef NO_WOLFSSL_SERVER
  11570. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11571. #else
  11572. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11573. #endif
  11574. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11575. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11576. wolfSSL_CTX_free(ctx);
  11577. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11578. #endif
  11579. return EXPECT_RESULT();
  11580. }
  11581. /* Testing functions dealing with PKCS5 */
  11582. static int test_wolfSSL_PKCS5(void)
  11583. {
  11584. EXPECT_DECLS;
  11585. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  11586. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  11587. const char* passwd = "myfipsPa$$W0rd";
  11588. #else
  11589. const char *passwd = "pass1234";
  11590. #endif
  11591. const unsigned char *salt = (unsigned char *)"salt1234";
  11592. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  11593. DYNAMIC_TYPE_TMP_BUFFER);
  11594. int ret = 0;
  11595. ExpectNotNull(out);
  11596. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  11597. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  11598. WOLFSSL_SUCCESS);
  11599. #ifdef WOLFSSL_SHA512
  11600. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  11601. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  11602. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  11603. #endif
  11604. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11605. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  11606. return EXPECT_RESULT();
  11607. }
  11608. /* test parsing URI from certificate */
  11609. static int test_wolfSSL_URI(void)
  11610. {
  11611. EXPECT_DECLS;
  11612. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11613. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  11614. defined(OPENSSL_EXTRA))
  11615. WOLFSSL_X509* x509 = NULL;
  11616. const char uri[] = "./certs/client-uri-cert.pem";
  11617. const char urn[] = "./certs/client-absolute-urn.pem";
  11618. const char badUri[] = "./certs/client-relative-uri.pem";
  11619. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  11620. WOLFSSL_FILETYPE_PEM));
  11621. wolfSSL_FreeX509(x509);
  11622. x509 = NULL;
  11623. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  11624. WOLFSSL_FILETYPE_PEM));
  11625. wolfSSL_FreeX509(x509);
  11626. x509 = NULL;
  11627. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  11628. && !defined(WOLFSSL_FPKI)
  11629. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11630. WOLFSSL_FILETYPE_PEM));
  11631. #else
  11632. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11633. WOLFSSL_FILETYPE_PEM));
  11634. #endif
  11635. wolfSSL_FreeX509(x509);
  11636. #endif
  11637. return EXPECT_RESULT();
  11638. }
  11639. static int test_wolfSSL_TBS(void)
  11640. {
  11641. EXPECT_DECLS;
  11642. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11643. && defined(OPENSSL_EXTRA)
  11644. WOLFSSL_X509* x509 = NULL;
  11645. const unsigned char* tbs;
  11646. int tbsSz;
  11647. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  11648. WOLFSSL_FILETYPE_PEM));
  11649. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  11650. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  11651. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  11652. ExpectIntEQ(tbsSz, 1003);
  11653. wolfSSL_FreeX509(x509);
  11654. #endif
  11655. return EXPECT_RESULT();
  11656. }
  11657. static int test_wolfSSL_X509_verify(void)
  11658. {
  11659. EXPECT_DECLS;
  11660. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  11661. defined(OPENSSL_EXTRA)
  11662. WOLFSSL_X509* ca = NULL;
  11663. WOLFSSL_X509* serv = NULL;
  11664. WOLFSSL_EVP_PKEY* pkey = NULL;
  11665. unsigned char buf[2048];
  11666. const unsigned char* pt = NULL;
  11667. int bufSz;
  11668. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  11669. WOLFSSL_FILETYPE_PEM));
  11670. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  11671. WOLFSSL_SUCCESS);
  11672. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  11673. WOLFSSL_SUCCESS);
  11674. ExpectIntEQ(bufSz, 294);
  11675. bufSz = 2048;
  11676. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  11677. WOLFSSL_SUCCESS);
  11678. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  11679. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  11680. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  11681. WOLFSSL_FILETYPE_PEM));
  11682. /* success case */
  11683. pt = buf;
  11684. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11685. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  11686. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  11687. wolfSSL_EVP_PKEY_free(pkey);
  11688. pkey = NULL;
  11689. /* fail case */
  11690. bufSz = 2048;
  11691. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  11692. WOLFSSL_SUCCESS);
  11693. pt = buf;
  11694. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11695. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  11696. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  11697. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  11698. wolfSSL_EVP_PKEY_free(pkey);
  11699. wolfSSL_FreeX509(ca);
  11700. wolfSSL_FreeX509(serv);
  11701. #endif
  11702. return EXPECT_RESULT();
  11703. }
  11704. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11705. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11706. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11707. /* create certificate with version 2 */
  11708. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  11709. {
  11710. EXPECT_DECLS;
  11711. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  11712. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  11713. unsigned char *der = NULL, *key = NULL, *pt;
  11714. char *header = NULL, *name = NULL;
  11715. int derSz;
  11716. long keySz;
  11717. XFILE fp = XBADFILE;
  11718. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  11719. time_t t;
  11720. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  11721. WOLFSSL_FILETYPE_PEM));
  11722. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  11723. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  11724. WOLFSSL_SUCCESS);
  11725. if (fp != XBADFILE)
  11726. XFCLOSE(fp);
  11727. pt = key;
  11728. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  11729. (const unsigned char**)&pt, keySz));
  11730. /* create the version 2 certificate */
  11731. ExpectNotNull(x509v2 = X509_new());
  11732. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  11733. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  11734. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  11735. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  11736. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  11737. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  11738. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  11739. t = time(NULL);
  11740. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  11741. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  11742. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  11743. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  11744. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  11745. derSz = wolfSSL_i2d_X509(x509v2, &der);
  11746. ExpectIntGT(derSz, 0);
  11747. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  11748. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11749. /* TODO: Replace with API call */
  11750. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  11751. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11752. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11753. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11754. wolfSSL_X509_free(x509);
  11755. wolfSSL_X509_free(x509v2);
  11756. wolfSSL_EVP_PKEY_free(priv);
  11757. wolfSSL_EVP_PKEY_free(pub);
  11758. wolfSSL_ASN1_TIME_free(notBefore);
  11759. wolfSSL_ASN1_TIME_free(notAfter);
  11760. return EXPECT_RESULT();
  11761. }
  11762. /* override certificate version error */
  11763. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  11764. {
  11765. EXPECT_DECLS;
  11766. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11767. ExpectIntEQ(store->error, ASN_VERSION_E);
  11768. #else
  11769. ExpectIntEQ(store->error, 0);
  11770. #endif
  11771. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  11772. (void)preverify;
  11773. return EXPECT_RESULT() == TEST_SUCCESS;
  11774. }
  11775. /* set verify callback that will override bad certificate version */
  11776. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  11777. {
  11778. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  11779. return TEST_SUCCESS;
  11780. }
  11781. #endif
  11782. static int test_wolfSSL_X509_TLS_version_test_1(void)
  11783. {
  11784. EXPECT_DECLS;
  11785. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11786. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11787. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11788. test_ssl_cbf func_cb_client;
  11789. test_ssl_cbf func_cb_server;
  11790. /* test server rejects a client certificate that is not version 3 */
  11791. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11792. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11793. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11794. #ifndef WOLFSSL_NO_TLS12
  11795. func_cb_client.method = wolfTLSv1_2_client_method;
  11796. #else
  11797. func_cb_client.method = wolfTLSv1_3_client_method;
  11798. #endif
  11799. #ifndef WOLFSSL_NO_TLS12
  11800. func_cb_server.method = wolfTLSv1_2_server_method;
  11801. #else
  11802. func_cb_server.method = wolfTLSv1_3_server_method;
  11803. #endif
  11804. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11805. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11806. &func_cb_server, NULL), TEST_FAIL);
  11807. #else
  11808. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11809. &func_cb_server, NULL), TEST_SUCCESS);
  11810. #endif
  11811. #endif
  11812. return EXPECT_RESULT();
  11813. }
  11814. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11815. {
  11816. EXPECT_DECLS;
  11817. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11818. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11819. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11820. test_ssl_cbf func_cb_client;
  11821. test_ssl_cbf func_cb_server;
  11822. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11823. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11824. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11825. func_cb_server.ctx_ready = &test_set_override_x509;
  11826. #ifndef WOLFSSL_NO_TLS12
  11827. func_cb_client.method = wolfTLSv1_2_client_method;
  11828. #else
  11829. func_cb_client.method = wolfTLSv1_3_client_method;
  11830. #endif
  11831. #ifndef WOLFSSL_NO_TLS12
  11832. func_cb_server.method = wolfTLSv1_2_server_method;
  11833. #else
  11834. func_cb_server.method = wolfTLSv1_3_server_method;
  11835. #endif
  11836. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11837. &func_cb_server, NULL), TEST_SUCCESS);
  11838. #endif
  11839. return EXPECT_RESULT();
  11840. }
  11841. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11842. * version allowed.
  11843. * POST: 1 on success.
  11844. */
  11845. static int test_wolfSSL_CTX_SetMinVersion(void)
  11846. {
  11847. int res = TEST_SKIPPED;
  11848. #ifndef NO_WOLFSSL_CLIENT
  11849. int failFlag = WOLFSSL_SUCCESS;
  11850. WOLFSSL_CTX* ctx;
  11851. int itr;
  11852. #ifndef NO_OLD_TLS
  11853. const int versions[] = {
  11854. #ifdef WOLFSSL_ALLOW_TLSV10
  11855. WOLFSSL_TLSV1,
  11856. #endif
  11857. WOLFSSL_TLSV1_1,
  11858. WOLFSSL_TLSV1_2 };
  11859. #elif !defined(WOLFSSL_NO_TLS12)
  11860. const int versions[] = { WOLFSSL_TLSV1_2 };
  11861. #elif defined(WOLFSSL_TLS13)
  11862. const int versions[] = { WOLFSSL_TLSV1_3 };
  11863. #else
  11864. const int versions[0];
  11865. #endif
  11866. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11867. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11868. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11869. != WOLFSSL_SUCCESS) {
  11870. failFlag = WOLFSSL_FAILURE;
  11871. }
  11872. }
  11873. wolfSSL_CTX_free(ctx);
  11874. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11875. #endif
  11876. return res;
  11877. } /* END test_wolfSSL_CTX_SetMinVersion */
  11878. /*----------------------------------------------------------------------------*
  11879. | OCSP Stapling
  11880. *----------------------------------------------------------------------------*/
  11881. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11882. * need to contact the CA, lowering the cost of cert revocation checking.
  11883. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11884. * POST: 1 returned for success.
  11885. */
  11886. static int test_wolfSSL_UseOCSPStapling(void)
  11887. {
  11888. EXPECT_DECLS;
  11889. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11890. !defined(NO_WOLFSSL_CLIENT)
  11891. WOLFSSL_CTX* ctx = NULL;
  11892. WOLFSSL* ssl = NULL;
  11893. #ifndef NO_WOLFSSL_CLIENT
  11894. #ifndef WOLFSSL_NO_TLS12
  11895. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11896. #else
  11897. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11898. #endif
  11899. #else
  11900. #ifndef WOLFSSL_NO_TLS12
  11901. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11902. #else
  11903. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11904. #endif
  11905. #endif
  11906. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11907. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11908. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11909. #ifndef NO_WOLFSSL_CLIENT
  11910. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11911. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11912. #else
  11913. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11914. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11915. #endif
  11916. wolfSSL_free(ssl);
  11917. wolfSSL_CTX_free(ctx);
  11918. #endif
  11919. return EXPECT_RESULT();
  11920. } /* END test_wolfSSL_UseOCSPStapling */
  11921. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11922. * stapling eliminates the need to contact the CA and lowers cert revocation
  11923. * check.
  11924. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11925. */
  11926. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11927. {
  11928. EXPECT_DECLS;
  11929. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11930. !defined(NO_WOLFSSL_CLIENT)
  11931. WOLFSSL_CTX* ctx = NULL;
  11932. WOLFSSL* ssl = NULL;
  11933. #ifndef NO_WOLFSSL_CLIENT
  11934. #ifndef WOLFSSL_NO_TLS12
  11935. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11936. #else
  11937. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11938. #endif
  11939. #else
  11940. #ifndef WOLFSSL_NO_TLS12
  11941. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11942. #else
  11943. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11944. #endif
  11945. #endif
  11946. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11947. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11948. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11949. #ifndef NO_WOLFSSL_CLIENT
  11950. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11951. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11952. #else
  11953. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11954. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11955. #endif
  11956. wolfSSL_free(ssl);
  11957. wolfSSL_CTX_free(ctx);
  11958. #endif
  11959. return EXPECT_RESULT();
  11960. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11961. /*----------------------------------------------------------------------------*
  11962. | Multicast Tests
  11963. *----------------------------------------------------------------------------*/
  11964. static int test_wolfSSL_mcast(void)
  11965. {
  11966. EXPECT_DECLS;
  11967. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11968. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11969. WOLFSSL_CTX* ctx = NULL;
  11970. WOLFSSL* ssl = NULL;
  11971. byte preMasterSecret[512];
  11972. byte clientRandom[32];
  11973. byte serverRandom[32];
  11974. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11975. byte buf[256];
  11976. word16 newId;
  11977. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11978. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11979. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11980. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11981. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11982. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11983. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11984. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11985. WOLFSSL_SUCCESS);
  11986. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11987. ExpectIntLE(newId, 100);
  11988. wolfSSL_free(ssl);
  11989. wolfSSL_CTX_free(ctx);
  11990. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11991. * WOLFSSL_SNIFFER) */
  11992. return EXPECT_RESULT();
  11993. }
  11994. /*----------------------------------------------------------------------------*
  11995. | Wolfcrypt
  11996. *----------------------------------------------------------------------------*/
  11997. /*
  11998. * Unit test for the wc_InitBlake2b()
  11999. */
  12000. static int test_wc_InitBlake2b(void)
  12001. {
  12002. EXPECT_DECLS;
  12003. #ifdef HAVE_BLAKE2
  12004. Blake2b blake;
  12005. /* Test good arg. */
  12006. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  12007. /* Test bad arg. */
  12008. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  12009. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  12010. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  12011. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  12012. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  12013. #endif
  12014. return EXPECT_RESULT();
  12015. } /* END test_wc_InitBlake2b*/
  12016. /*
  12017. * Unit test for the wc_InitBlake2b_WithKey()
  12018. */
  12019. static int test_wc_InitBlake2b_WithKey(void)
  12020. {
  12021. EXPECT_DECLS;
  12022. #ifdef HAVE_BLAKE2
  12023. Blake2b blake;
  12024. word32 digestSz = BLAKE2B_KEYBYTES;
  12025. byte key[BLAKE2B_KEYBYTES];
  12026. word32 keylen = BLAKE2B_KEYBYTES;
  12027. XMEMSET(key, 0, sizeof(key));
  12028. /* Test good arg. */
  12029. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  12030. /* Test bad args. */
  12031. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  12032. BAD_FUNC_ARG);
  12033. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  12034. BAD_FUNC_ARG);
  12035. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  12036. #endif
  12037. return EXPECT_RESULT();
  12038. } /* END wc_InitBlake2b_WithKey*/
  12039. /*
  12040. * Unit test for the wc_InitBlake2s_WithKey()
  12041. */
  12042. static int test_wc_InitBlake2s_WithKey(void)
  12043. {
  12044. EXPECT_DECLS;
  12045. #ifdef HAVE_BLAKE2S
  12046. Blake2s blake;
  12047. word32 digestSz = BLAKE2S_KEYBYTES;
  12048. byte *key = (byte*)"01234567890123456789012345678901";
  12049. word32 keylen = BLAKE2S_KEYBYTES;
  12050. /* Test good arg. */
  12051. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  12052. /* Test bad args. */
  12053. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  12054. BAD_FUNC_ARG);
  12055. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  12056. BAD_FUNC_ARG);
  12057. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  12058. #endif
  12059. return EXPECT_RESULT();
  12060. } /* END wc_InitBlake2s_WithKey*/
  12061. /*
  12062. * Unit test for the wc_InitMd5()
  12063. */
  12064. static int test_wc_InitMd5(void)
  12065. {
  12066. EXPECT_DECLS;
  12067. #ifndef NO_MD5
  12068. wc_Md5 md5;
  12069. /* Test good arg. */
  12070. ExpectIntEQ(wc_InitMd5(&md5), 0);
  12071. /* Test bad arg. */
  12072. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  12073. wc_Md5Free(&md5);
  12074. #endif
  12075. return EXPECT_RESULT();
  12076. } /* END test_wc_InitMd5 */
  12077. /*
  12078. * Testing wc_UpdateMd5()
  12079. */
  12080. static int test_wc_Md5Update(void)
  12081. {
  12082. EXPECT_DECLS;
  12083. #ifndef NO_MD5
  12084. wc_Md5 md5;
  12085. byte hash[WC_MD5_DIGEST_SIZE];
  12086. testVector a, b, c;
  12087. ExpectIntEQ(wc_InitMd5(&md5), 0);
  12088. /* Input */
  12089. a.input = "a";
  12090. a.inLen = XSTRLEN(a.input);
  12091. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  12092. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  12093. /* Update input. */
  12094. a.input = "abc";
  12095. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  12096. "\x72";
  12097. a.inLen = XSTRLEN(a.input);
  12098. a.outLen = XSTRLEN(a.output);
  12099. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  12100. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  12101. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  12102. /* Pass in bad values. */
  12103. b.input = NULL;
  12104. b.inLen = 0;
  12105. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  12106. c.input = NULL;
  12107. c.inLen = WC_MD5_DIGEST_SIZE;
  12108. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  12109. BAD_FUNC_ARG);
  12110. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  12111. BAD_FUNC_ARG);
  12112. wc_Md5Free(&md5);
  12113. #endif
  12114. return EXPECT_RESULT();
  12115. } /* END test_wc_Md5Update() */
  12116. /*
  12117. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  12118. */
  12119. static int test_wc_Md5Final(void)
  12120. {
  12121. EXPECT_DECLS;
  12122. #ifndef NO_MD5
  12123. /* Instantiate */
  12124. wc_Md5 md5;
  12125. byte* hash_test[3];
  12126. byte hash1[WC_MD5_DIGEST_SIZE];
  12127. byte hash2[2*WC_MD5_DIGEST_SIZE];
  12128. byte hash3[5*WC_MD5_DIGEST_SIZE];
  12129. int times, i;
  12130. /* Initialize */
  12131. ExpectIntEQ(wc_InitMd5(&md5), 0);
  12132. hash_test[0] = hash1;
  12133. hash_test[1] = hash2;
  12134. hash_test[2] = hash3;
  12135. times = sizeof(hash_test)/sizeof(byte*);
  12136. for (i = 0; i < times; i++) {
  12137. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  12138. }
  12139. /* Test bad args. */
  12140. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  12141. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  12142. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  12143. wc_Md5Free(&md5);
  12144. #endif
  12145. return EXPECT_RESULT();
  12146. }
  12147. /*
  12148. * Unit test for the wc_InitSha()
  12149. */
  12150. static int test_wc_InitSha(void)
  12151. {
  12152. EXPECT_DECLS;
  12153. #ifndef NO_SHA
  12154. wc_Sha sha;
  12155. /* Test good arg. */
  12156. ExpectIntEQ(wc_InitSha(&sha), 0);
  12157. /* Test bad arg. */
  12158. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  12159. wc_ShaFree(&sha);
  12160. #endif
  12161. return EXPECT_RESULT();
  12162. } /* END test_wc_InitSha */
  12163. /*
  12164. * Tesing wc_ShaUpdate()
  12165. */
  12166. static int test_wc_ShaUpdate(void)
  12167. {
  12168. EXPECT_DECLS;
  12169. #ifndef NO_SHA
  12170. wc_Sha sha;
  12171. byte hash[WC_SHA_DIGEST_SIZE];
  12172. testVector a, b, c;
  12173. ExpectIntEQ(wc_InitSha(&sha), 0);
  12174. /* Input. */
  12175. a.input = "a";
  12176. a.inLen = XSTRLEN(a.input);
  12177. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  12178. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  12179. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  12180. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  12181. /* Update input. */
  12182. a.input = "abc";
  12183. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  12184. "\x6C\x9C\xD0\xD8\x9D";
  12185. a.inLen = XSTRLEN(a.input);
  12186. a.outLen = XSTRLEN(a.output);
  12187. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  12188. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  12189. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  12190. /* Try passing in bad values. */
  12191. b.input = NULL;
  12192. b.inLen = 0;
  12193. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  12194. c.input = NULL;
  12195. c.inLen = WC_SHA_DIGEST_SIZE;
  12196. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  12197. BAD_FUNC_ARG);
  12198. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  12199. BAD_FUNC_ARG);
  12200. wc_ShaFree(&sha);
  12201. #endif
  12202. return EXPECT_RESULT();
  12203. } /* END test_wc_ShaUpdate() */
  12204. /*
  12205. * Unit test on wc_ShaFinal
  12206. */
  12207. static int test_wc_ShaFinal(void)
  12208. {
  12209. EXPECT_DECLS;
  12210. #ifndef NO_SHA
  12211. wc_Sha sha;
  12212. byte* hash_test[3];
  12213. byte hash1[WC_SHA_DIGEST_SIZE];
  12214. byte hash2[2*WC_SHA_DIGEST_SIZE];
  12215. byte hash3[5*WC_SHA_DIGEST_SIZE];
  12216. int times, i;
  12217. /* Initialize*/
  12218. ExpectIntEQ(wc_InitSha(&sha), 0);
  12219. hash_test[0] = hash1;
  12220. hash_test[1] = hash2;
  12221. hash_test[2] = hash3;
  12222. times = sizeof(hash_test)/sizeof(byte*);
  12223. for (i = 0; i < times; i++) {
  12224. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  12225. }
  12226. /* Test bad args. */
  12227. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  12228. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  12229. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  12230. wc_ShaFree(&sha);
  12231. #endif
  12232. return EXPECT_RESULT();
  12233. } /* END test_wc_ShaFinal */
  12234. /*
  12235. * Unit test for wc_InitSha256()
  12236. */
  12237. static int test_wc_InitSha256(void)
  12238. {
  12239. EXPECT_DECLS;
  12240. #ifndef NO_SHA256
  12241. wc_Sha256 sha256;
  12242. /* Test good arg. */
  12243. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12244. /* Test bad arg. */
  12245. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  12246. wc_Sha256Free(&sha256);
  12247. #endif
  12248. return EXPECT_RESULT();
  12249. } /* END test_wc_InitSha256 */
  12250. /*
  12251. * Unit test for wc_Sha256Update()
  12252. */
  12253. static int test_wc_Sha256Update(void)
  12254. {
  12255. EXPECT_DECLS;
  12256. #ifndef NO_SHA256
  12257. wc_Sha256 sha256;
  12258. byte hash[WC_SHA256_DIGEST_SIZE];
  12259. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  12260. testVector a, b, c;
  12261. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12262. /* Input. */
  12263. a.input = "a";
  12264. a.inLen = XSTRLEN(a.input);
  12265. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  12266. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  12267. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  12268. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  12269. /* Update input. */
  12270. a.input = "abc";
  12271. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  12272. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  12273. "\x15\xAD";
  12274. a.inLen = XSTRLEN(a.input);
  12275. a.outLen = XSTRLEN(a.output);
  12276. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  12277. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  12278. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  12279. /* Unaligned check. */
  12280. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  12281. 0);
  12282. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  12283. /* Try passing in bad values */
  12284. b.input = NULL;
  12285. b.inLen = 0;
  12286. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  12287. c.input = NULL;
  12288. c.inLen = WC_SHA256_DIGEST_SIZE;
  12289. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  12290. BAD_FUNC_ARG);
  12291. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12292. BAD_FUNC_ARG);
  12293. wc_Sha256Free(&sha256);
  12294. #endif
  12295. return EXPECT_RESULT();
  12296. } /* END test_wc_Sha256Update */
  12297. /*
  12298. * Unit test function for wc_Sha256Final()
  12299. */
  12300. static int test_wc_Sha256Final(void)
  12301. {
  12302. EXPECT_DECLS;
  12303. #ifndef NO_SHA256
  12304. wc_Sha256 sha256;
  12305. byte* hash_test[3];
  12306. byte hash1[WC_SHA256_DIGEST_SIZE];
  12307. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  12308. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  12309. int times, i;
  12310. /* Initialize */
  12311. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12312. hash_test[0] = hash1;
  12313. hash_test[1] = hash2;
  12314. hash_test[2] = hash3;
  12315. times = sizeof(hash_test) / sizeof(byte*);
  12316. for (i = 0; i < times; i++) {
  12317. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  12318. }
  12319. /* Test bad args. */
  12320. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  12321. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  12322. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  12323. wc_Sha256Free(&sha256);
  12324. #endif
  12325. return EXPECT_RESULT();
  12326. } /* END test_wc_Sha256Final */
  12327. /*
  12328. * Unit test function for wc_Sha256FinalRaw()
  12329. */
  12330. static int test_wc_Sha256FinalRaw(void)
  12331. {
  12332. EXPECT_DECLS;
  12333. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  12334. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  12335. !defined(WOLFSSL_NO_HASH_RAW)
  12336. wc_Sha256 sha256;
  12337. byte* hash_test[3];
  12338. byte hash1[WC_SHA256_DIGEST_SIZE];
  12339. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  12340. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  12341. int times, i;
  12342. /* Initialize */
  12343. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12344. hash_test[0] = hash1;
  12345. hash_test[1] = hash2;
  12346. hash_test[2] = hash3;
  12347. times = sizeof(hash_test) / sizeof(byte*);
  12348. for (i = 0; i < times; i++) {
  12349. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  12350. }
  12351. /* Test bad args. */
  12352. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12353. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12354. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  12355. wc_Sha256Free(&sha256);
  12356. #endif
  12357. return EXPECT_RESULT();
  12358. } /* END test_wc_Sha256FinalRaw */
  12359. /*
  12360. * Unit test function for wc_Sha256GetFlags()
  12361. */
  12362. static int test_wc_Sha256GetFlags(void)
  12363. {
  12364. EXPECT_DECLS;
  12365. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  12366. wc_Sha256 sha256;
  12367. word32 flags = 0;
  12368. /* Initialize */
  12369. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12370. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  12371. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12372. wc_Sha256Free(&sha256);
  12373. #endif
  12374. return EXPECT_RESULT();
  12375. } /* END test_wc_Sha256GetFlags */
  12376. /*
  12377. * Unit test function for wc_Sha256Free()
  12378. */
  12379. static int test_wc_Sha256Free(void)
  12380. {
  12381. EXPECT_DECLS;
  12382. #ifndef NO_SHA256
  12383. wc_Sha256Free(NULL);
  12384. /* Set result to SUCCESS. */
  12385. ExpectTrue(1);
  12386. #endif
  12387. return EXPECT_RESULT();
  12388. } /* END test_wc_Sha256Free */
  12389. /*
  12390. * Unit test function for wc_Sha256GetHash()
  12391. */
  12392. static int test_wc_Sha256GetHash(void)
  12393. {
  12394. EXPECT_DECLS;
  12395. #ifndef NO_SHA256
  12396. wc_Sha256 sha256;
  12397. byte hash1[WC_SHA256_DIGEST_SIZE];
  12398. /* Initialize */
  12399. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12400. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  12401. /* test bad arguments*/
  12402. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  12403. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  12404. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  12405. wc_Sha256Free(&sha256);
  12406. #endif
  12407. return EXPECT_RESULT();
  12408. } /* END test_wc_Sha256GetHash */
  12409. /*
  12410. * Unit test function for wc_Sha256Copy()
  12411. */
  12412. static int test_wc_Sha256Copy(void)
  12413. {
  12414. EXPECT_DECLS;
  12415. #ifndef NO_SHA256
  12416. wc_Sha256 sha256;
  12417. wc_Sha256 temp;
  12418. XMEMSET(&sha256, 0, sizeof(sha256));
  12419. XMEMSET(&temp, 0, sizeof(temp));
  12420. /* Initialize */
  12421. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12422. ExpectIntEQ(wc_InitSha256(&temp), 0);
  12423. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  12424. /* test bad arguments*/
  12425. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  12426. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  12427. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  12428. wc_Sha256Free(&sha256);
  12429. wc_Sha256Free(&temp);
  12430. #endif
  12431. return EXPECT_RESULT();
  12432. } /* END test_wc_Sha256Copy */
  12433. /*
  12434. * Testing wc_InitSha512()
  12435. */
  12436. static int test_wc_InitSha512(void)
  12437. {
  12438. EXPECT_DECLS;
  12439. #ifdef WOLFSSL_SHA512
  12440. wc_Sha512 sha512;
  12441. /* Test good arg. */
  12442. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12443. /* Test bad arg. */
  12444. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  12445. wc_Sha512Free(&sha512);
  12446. #endif
  12447. return EXPECT_RESULT();
  12448. } /* END test_wc_InitSha512 */
  12449. /*
  12450. * wc_Sha512Update() test.
  12451. */
  12452. static int test_wc_Sha512Update(void)
  12453. {
  12454. EXPECT_DECLS;
  12455. #ifdef WOLFSSL_SHA512
  12456. wc_Sha512 sha512;
  12457. byte hash[WC_SHA512_DIGEST_SIZE];
  12458. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  12459. testVector a, b, c;
  12460. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12461. /* Input. */
  12462. a.input = "a";
  12463. a.inLen = XSTRLEN(a.input);
  12464. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  12465. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  12466. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  12467. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12468. /* Update input. */
  12469. a.input = "abc";
  12470. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  12471. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  12472. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  12473. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  12474. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  12475. a.inLen = XSTRLEN(a.input);
  12476. a.outLen = XSTRLEN(a.output);
  12477. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  12478. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12479. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  12480. /* Unaligned check. */
  12481. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  12482. 0);
  12483. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  12484. /* Try passing in bad values */
  12485. b.input = NULL;
  12486. b.inLen = 0;
  12487. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  12488. c.input = NULL;
  12489. c.inLen = WC_SHA512_DIGEST_SIZE;
  12490. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12491. BAD_FUNC_ARG);
  12492. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  12493. BAD_FUNC_ARG);
  12494. wc_Sha512Free(&sha512);
  12495. #endif
  12496. return EXPECT_RESULT();
  12497. } /* END test_wc_Sha512Update */
  12498. #ifdef WOLFSSL_SHA512
  12499. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12500. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12501. /* Performs test for
  12502. * - wc_Sha512Final/wc_Sha512FinalRaw
  12503. * - wc_Sha512_224Final/wc_Sha512_224Final
  12504. * - wc_Sha512_256Final/wc_Sha512_256Final
  12505. * parameter:
  12506. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  12507. * WC_HASH_TYPE_SHA512_256
  12508. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  12509. *return 0 on success
  12510. */
  12511. static int test_Sha512_Family_Final(int type, int isRaw)
  12512. {
  12513. EXPECT_DECLS;
  12514. wc_Sha512 sha512;
  12515. byte* hash_test[3];
  12516. byte hash1[WC_SHA512_DIGEST_SIZE];
  12517. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12518. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12519. int times, i;
  12520. int(*initFp)(wc_Sha512*);
  12521. int(*finalFp)(wc_Sha512*, byte*);
  12522. void(*freeFp)(wc_Sha512*);
  12523. if (type == WC_HASH_TYPE_SHA512) {
  12524. initFp = wc_InitSha512;
  12525. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12526. !defined(WOLFSSL_NO_HASH_RAW)
  12527. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  12528. #else
  12529. finalFp = (isRaw)? NULL : wc_Sha512Final;
  12530. #endif
  12531. freeFp = wc_Sha512Free;
  12532. }
  12533. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12534. #if !defined(WOLFSSL_NOSHA512_224)
  12535. else if (type == WC_HASH_TYPE_SHA512_224) {
  12536. initFp = wc_InitSha512_224;
  12537. #if !defined(WOLFSSL_NO_HASH_RAW)
  12538. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  12539. #else
  12540. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  12541. #endif
  12542. freeFp = wc_Sha512_224Free;
  12543. }
  12544. #endif
  12545. #if !defined(WOLFSSL_NOSHA512_256)
  12546. else if (type == WC_HASH_TYPE_SHA512_256) {
  12547. initFp = wc_InitSha512_256;
  12548. #if !defined(WOLFSSL_NO_HASH_RAW)
  12549. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  12550. #else
  12551. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  12552. #endif
  12553. freeFp = wc_Sha512_256Free;
  12554. }
  12555. #endif
  12556. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12557. else
  12558. return TEST_FAIL;
  12559. /* Initialize */
  12560. ExpectIntEQ(initFp(&sha512), 0);
  12561. hash_test[0] = hash1;
  12562. hash_test[1] = hash2;
  12563. hash_test[2] = hash3;
  12564. times = sizeof(hash_test) / sizeof(byte *);
  12565. /* Good test args. */
  12566. for (i = 0; i < times; i++) {
  12567. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  12568. }
  12569. /* Test bad args. */
  12570. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  12571. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  12572. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  12573. freeFp(&sha512);
  12574. return EXPECT_RESULT();
  12575. }
  12576. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12577. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12578. #endif /* WOLFSSL_SHA512 */
  12579. /*
  12580. * Unit test function for wc_Sha512Final()
  12581. */
  12582. static int test_wc_Sha512Final(void)
  12583. {
  12584. EXPECT_DECLS;
  12585. #ifdef WOLFSSL_SHA512
  12586. wc_Sha512 sha512;
  12587. byte* hash_test[3];
  12588. byte hash1[WC_SHA512_DIGEST_SIZE];
  12589. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12590. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12591. int times, i;
  12592. /* Initialize */
  12593. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12594. hash_test[0] = hash1;
  12595. hash_test[1] = hash2;
  12596. hash_test[2] = hash3;
  12597. times = sizeof(hash_test) / sizeof(byte *);
  12598. for (i = 0; i < times; i++) {
  12599. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  12600. }
  12601. /* Test bad args. */
  12602. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  12603. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  12604. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  12605. wc_Sha512Free(&sha512);
  12606. #endif
  12607. return EXPECT_RESULT();
  12608. } /* END test_wc_Sha512Final */
  12609. /*
  12610. * Unit test function for wc_Sha512GetFlags()
  12611. */
  12612. static int test_wc_Sha512GetFlags(void)
  12613. {
  12614. EXPECT_DECLS;
  12615. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  12616. wc_Sha512 sha512;
  12617. word32 flags = 0;
  12618. /* Initialize */
  12619. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12620. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  12621. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  12622. wc_Sha512Free(&sha512);
  12623. #endif
  12624. return EXPECT_RESULT();
  12625. } /* END test_wc_Sha512GetFlags */
  12626. /*
  12627. * Unit test function for wc_Sha512FinalRaw()
  12628. */
  12629. static int test_wc_Sha512FinalRaw(void)
  12630. {
  12631. EXPECT_DECLS;
  12632. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12633. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12634. !defined(WOLFSSL_NO_HASH_RAW)
  12635. wc_Sha512 sha512;
  12636. byte* hash_test[3];
  12637. byte hash1[WC_SHA512_DIGEST_SIZE];
  12638. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12639. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12640. int times, i;
  12641. /* Initialize */
  12642. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12643. hash_test[0] = hash1;
  12644. hash_test[1] = hash2;
  12645. hash_test[2] = hash3;
  12646. times = sizeof(hash_test) / sizeof(byte*);
  12647. /* Good test args. */
  12648. for (i = 0; i < times; i++) {
  12649. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  12650. }
  12651. /* Test bad args. */
  12652. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12653. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12654. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  12655. wc_Sha512Free(&sha512);
  12656. #endif
  12657. return EXPECT_RESULT();
  12658. } /* END test_wc_Sha512FinalRaw */
  12659. /*
  12660. * Unit test function for wc_Sha512Free()
  12661. */
  12662. static int test_wc_Sha512Free(void)
  12663. {
  12664. EXPECT_DECLS;
  12665. #ifdef WOLFSSL_SHA512
  12666. wc_Sha512Free(NULL);
  12667. /* Set result to SUCCESS. */
  12668. ExpectTrue(1);
  12669. #endif
  12670. return EXPECT_RESULT();
  12671. } /* END test_wc_Sha512Free */
  12672. #ifdef WOLFSSL_SHA512
  12673. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12674. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12675. static int test_Sha512_Family_GetHash(int type )
  12676. {
  12677. EXPECT_DECLS;
  12678. int(*initFp)(wc_Sha512*);
  12679. int(*ghashFp)(wc_Sha512*, byte*);
  12680. wc_Sha512 sha512;
  12681. byte hash1[WC_SHA512_DIGEST_SIZE];
  12682. if (type == WC_HASH_TYPE_SHA512) {
  12683. initFp = wc_InitSha512;
  12684. ghashFp = wc_Sha512GetHash;
  12685. }
  12686. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12687. #if !defined(WOLFSSL_NOSHA512_224)
  12688. else if (type == WC_HASH_TYPE_SHA512_224) {
  12689. initFp = wc_InitSha512_224;
  12690. ghashFp = wc_Sha512_224GetHash;
  12691. }
  12692. #endif
  12693. #if !defined(WOLFSSL_NOSHA512_256)
  12694. else if (type == WC_HASH_TYPE_SHA512_256) {
  12695. initFp = wc_InitSha512_256;
  12696. ghashFp = wc_Sha512_256GetHash;
  12697. }
  12698. #endif
  12699. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12700. else {
  12701. initFp = NULL;
  12702. ghashFp = NULL;
  12703. }
  12704. if (initFp == NULL || ghashFp == NULL)
  12705. return TEST_FAIL;
  12706. ExpectIntEQ(initFp(&sha512), 0);
  12707. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  12708. /* test bad arguments*/
  12709. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  12710. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  12711. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  12712. wc_Sha512Free(&sha512);
  12713. return EXPECT_RESULT();
  12714. }
  12715. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12716. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12717. #endif /* WOLFSSL_SHA512 */
  12718. /*
  12719. * Unit test function for wc_Sha512GetHash()
  12720. */
  12721. static int test_wc_Sha512GetHash(void)
  12722. {
  12723. EXPECT_DECLS;
  12724. #ifdef WOLFSSL_SHA512
  12725. wc_Sha512 sha512;
  12726. byte hash1[WC_SHA512_DIGEST_SIZE];
  12727. /* Initialize */
  12728. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12729. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  12730. /* test bad arguments*/
  12731. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  12732. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  12733. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  12734. wc_Sha512Free(&sha512);
  12735. #endif
  12736. return EXPECT_RESULT();
  12737. } /* END test_wc_Sha512GetHash */
  12738. /*
  12739. * Unit test function for wc_Sha512Copy()
  12740. */
  12741. static int test_wc_Sha512Copy(void)
  12742. {
  12743. EXPECT_DECLS;
  12744. #ifdef WOLFSSL_SHA512
  12745. wc_Sha512 sha512;
  12746. wc_Sha512 temp;
  12747. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12748. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12749. /* Initialize */
  12750. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12751. ExpectIntEQ(wc_InitSha512(&temp), 0);
  12752. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  12753. /* test bad arguments*/
  12754. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  12755. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  12756. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  12757. wc_Sha512Free(&sha512);
  12758. wc_Sha512Free(&temp);
  12759. #endif
  12760. return EXPECT_RESULT();
  12761. } /* END test_wc_Sha512Copy */
  12762. static int test_wc_InitSha512_224(void)
  12763. {
  12764. EXPECT_DECLS;
  12765. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12766. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12767. wc_Sha512 sha512;
  12768. /* Test good arg. */
  12769. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12770. /* Test bad arg. */
  12771. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  12772. wc_Sha512_224Free(&sha512);
  12773. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12774. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12775. return EXPECT_RESULT();
  12776. }
  12777. static int test_wc_Sha512_224Update(void)
  12778. {
  12779. EXPECT_DECLS;
  12780. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12781. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12782. wc_Sha512 sha512;
  12783. byte hash[WC_SHA512_DIGEST_SIZE];
  12784. testVector a, c;
  12785. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12786. /* Input. */
  12787. a.input = "a";
  12788. a.inLen = XSTRLEN(a.input);
  12789. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  12790. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  12791. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12792. 0);
  12793. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12794. /* Update input. */
  12795. a.input = "abc";
  12796. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  12797. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  12798. a.inLen = XSTRLEN(a.input);
  12799. a.outLen = XSTRLEN(a.output);
  12800. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12801. 0);
  12802. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12803. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  12804. c.input = NULL;
  12805. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  12806. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12807. BAD_FUNC_ARG);
  12808. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12809. BAD_FUNC_ARG);
  12810. wc_Sha512_224Free(&sha512);
  12811. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12812. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12813. return EXPECT_RESULT();
  12814. }
  12815. static int test_wc_Sha512_224Final(void)
  12816. {
  12817. EXPECT_DECLS;
  12818. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12819. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12820. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12821. TEST_SUCCESS);
  12822. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12823. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12824. return EXPECT_RESULT();
  12825. }
  12826. static int test_wc_Sha512_224GetFlags(void)
  12827. {
  12828. EXPECT_DECLS;
  12829. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12830. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12831. wc_Sha512 sha512;
  12832. wc_Sha512 copy;
  12833. word32 flags = 0;
  12834. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12835. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12836. /* Initialize */
  12837. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12838. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12839. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12840. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12841. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12842. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12843. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12844. wc_Sha512_224Free(&copy);
  12845. wc_Sha512_224Free(&sha512);
  12846. #endif
  12847. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12848. return EXPECT_RESULT();
  12849. }
  12850. static int test_wc_Sha512_224FinalRaw(void)
  12851. {
  12852. EXPECT_DECLS;
  12853. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12854. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12855. !defined(WOLFSSL_NO_HASH_RAW)
  12856. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12857. TEST_SUCCESS);
  12858. #endif
  12859. return EXPECT_RESULT();
  12860. }
  12861. static int test_wc_Sha512_224Free(void)
  12862. {
  12863. EXPECT_DECLS;
  12864. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12865. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12866. wc_Sha512_224Free(NULL);
  12867. /* Set result to SUCCESS. */
  12868. ExpectTrue(1);
  12869. #endif
  12870. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12871. return EXPECT_RESULT();
  12872. }
  12873. static int test_wc_Sha512_224GetHash(void)
  12874. {
  12875. EXPECT_DECLS;
  12876. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12877. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12878. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12879. TEST_SUCCESS);
  12880. #endif
  12881. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12882. return EXPECT_RESULT();
  12883. }
  12884. static int test_wc_Sha512_224Copy(void)
  12885. {
  12886. EXPECT_DECLS;
  12887. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12888. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12889. wc_Sha512 sha512;
  12890. wc_Sha512 temp;
  12891. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12892. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12893. /* Initialize */
  12894. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12895. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12896. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12897. /* test bad arguments*/
  12898. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12899. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12900. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12901. wc_Sha512_224Free(&sha512);
  12902. wc_Sha512_224Free(&temp);
  12903. #endif
  12904. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12905. return EXPECT_RESULT();
  12906. }
  12907. static int test_wc_InitSha512_256(void)
  12908. {
  12909. EXPECT_DECLS;
  12910. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12911. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12912. wc_Sha512 sha512;
  12913. /* Test good arg. */
  12914. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12915. /* Test bad arg. */
  12916. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12917. wc_Sha512_256Free(&sha512);
  12918. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12919. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12920. return EXPECT_RESULT();
  12921. }
  12922. static int test_wc_Sha512_256Update(void)
  12923. {
  12924. EXPECT_DECLS;
  12925. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12926. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12927. wc_Sha512 sha512;
  12928. byte hash[WC_SHA512_DIGEST_SIZE];
  12929. testVector a, c;
  12930. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12931. /* Input. */
  12932. a.input = "a";
  12933. a.inLen = XSTRLEN(a.input);
  12934. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12935. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12936. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12937. 0);
  12938. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12939. /* Update input. */
  12940. a.input = "abc";
  12941. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12942. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12943. "\x07\xe7\xaf\x23";
  12944. a.inLen = XSTRLEN(a.input);
  12945. a.outLen = XSTRLEN(a.output);
  12946. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12947. 0);
  12948. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12949. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12950. c.input = NULL;
  12951. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12952. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12953. BAD_FUNC_ARG);
  12954. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12955. BAD_FUNC_ARG);
  12956. wc_Sha512_256Free(&sha512);
  12957. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12958. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12959. return EXPECT_RESULT();
  12960. }
  12961. static int test_wc_Sha512_256Final(void)
  12962. {
  12963. EXPECT_DECLS;
  12964. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12965. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12966. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12967. TEST_SUCCESS);
  12968. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12969. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12970. return EXPECT_RESULT();
  12971. }
  12972. static int test_wc_Sha512_256GetFlags(void)
  12973. {
  12974. EXPECT_DECLS;
  12975. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12976. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12977. wc_Sha512 sha512, copy;
  12978. word32 flags = 0;
  12979. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12980. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12981. /* Initialize */
  12982. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12983. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12984. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12985. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12986. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12987. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12988. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12989. wc_Sha512_256Free(&sha512);
  12990. #endif
  12991. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12992. return EXPECT_RESULT();
  12993. }
  12994. static int test_wc_Sha512_256FinalRaw(void)
  12995. {
  12996. EXPECT_DECLS;
  12997. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12998. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12999. !defined(WOLFSSL_NO_HASH_RAW)
  13000. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  13001. TEST_SUCCESS);
  13002. #endif
  13003. return EXPECT_RESULT();
  13004. }
  13005. static int test_wc_Sha512_256Free(void)
  13006. {
  13007. EXPECT_DECLS;
  13008. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  13009. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  13010. wc_Sha512_256Free(NULL);
  13011. /* Set result to SUCCESS. */
  13012. ExpectTrue(1);
  13013. #endif
  13014. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  13015. return EXPECT_RESULT();
  13016. }
  13017. static int test_wc_Sha512_256GetHash(void)
  13018. {
  13019. EXPECT_DECLS;
  13020. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  13021. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  13022. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  13023. TEST_SUCCESS);
  13024. #endif
  13025. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  13026. return EXPECT_RESULT();
  13027. }
  13028. static int test_wc_Sha512_256Copy(void)
  13029. {
  13030. EXPECT_DECLS;
  13031. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  13032. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  13033. wc_Sha512 sha512;
  13034. wc_Sha512 temp;
  13035. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  13036. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  13037. /* Initialize */
  13038. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  13039. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  13040. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  13041. /* test bad arguments*/
  13042. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  13043. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  13044. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  13045. wc_Sha512_256Free(&sha512);
  13046. wc_Sha512_256Free(&temp);
  13047. #endif
  13048. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  13049. return EXPECT_RESULT();
  13050. }
  13051. /*
  13052. * Testing wc_InitSha384()
  13053. */
  13054. static int test_wc_InitSha384(void)
  13055. {
  13056. EXPECT_DECLS;
  13057. #ifdef WOLFSSL_SHA384
  13058. wc_Sha384 sha384;
  13059. /* Test good arg. */
  13060. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  13061. /* Test bad arg. */
  13062. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  13063. wc_Sha384Free(&sha384);
  13064. #endif
  13065. return EXPECT_RESULT();
  13066. } /* END test_wc_InitSha384 */
  13067. /*
  13068. * test wc_Sha384Update()
  13069. */
  13070. static int test_wc_Sha384Update(void)
  13071. {
  13072. EXPECT_DECLS;
  13073. #ifdef WOLFSSL_SHA384
  13074. wc_Sha384 sha384;
  13075. byte hash[WC_SHA384_DIGEST_SIZE];
  13076. testVector a, b, c;
  13077. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  13078. /* Input */
  13079. a.input = "a";
  13080. a.inLen = XSTRLEN(a.input);
  13081. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  13082. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  13083. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  13084. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  13085. /* Update input. */
  13086. a.input = "abc";
  13087. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  13088. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  13089. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  13090. "\xc8\x25\xa7";
  13091. a.inLen = XSTRLEN(a.input);
  13092. a.outLen = XSTRLEN(a.output);
  13093. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  13094. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  13095. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  13096. /* Pass in bad values. */
  13097. b.input = NULL;
  13098. b.inLen = 0;
  13099. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  13100. c.input = NULL;
  13101. c.inLen = WC_SHA384_DIGEST_SIZE;
  13102. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  13103. BAD_FUNC_ARG);
  13104. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  13105. BAD_FUNC_ARG);
  13106. wc_Sha384Free(&sha384);
  13107. #endif
  13108. return EXPECT_RESULT();
  13109. } /* END test_wc_Sha384Update */
  13110. /*
  13111. * Unit test function for wc_Sha384Final();
  13112. */
  13113. static int test_wc_Sha384Final(void)
  13114. {
  13115. EXPECT_DECLS;
  13116. #ifdef WOLFSSL_SHA384
  13117. wc_Sha384 sha384;
  13118. byte* hash_test[3];
  13119. byte hash1[WC_SHA384_DIGEST_SIZE];
  13120. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  13121. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  13122. int times, i;
  13123. /* Initialize */
  13124. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  13125. hash_test[0] = hash1;
  13126. hash_test[1] = hash2;
  13127. hash_test[2] = hash3;
  13128. times = sizeof(hash_test) / sizeof(byte*);
  13129. /* Good test args. */
  13130. for (i = 0; i < times; i++) {
  13131. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  13132. }
  13133. /* Test bad args. */
  13134. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  13135. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  13136. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  13137. wc_Sha384Free(&sha384);
  13138. #endif
  13139. return EXPECT_RESULT();
  13140. } /* END test_wc_Sha384Final */
  13141. /*
  13142. * Unit test function for wc_Sha384GetFlags()
  13143. */
  13144. static int test_wc_Sha384GetFlags(void)
  13145. {
  13146. EXPECT_DECLS;
  13147. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  13148. wc_Sha384 sha384;
  13149. word32 flags = 0;
  13150. /* Initialize */
  13151. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  13152. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  13153. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13154. wc_Sha384Free(&sha384);
  13155. #endif
  13156. return EXPECT_RESULT();
  13157. } /* END test_wc_Sha384GetFlags */
  13158. /*
  13159. * Unit test function for wc_Sha384FinalRaw()
  13160. */
  13161. static int test_wc_Sha384FinalRaw(void)
  13162. {
  13163. EXPECT_DECLS;
  13164. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  13165. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  13166. !defined(WOLFSSL_NO_HASH_RAW)
  13167. wc_Sha384 sha384;
  13168. byte* hash_test[3];
  13169. byte hash1[WC_SHA384_DIGEST_SIZE];
  13170. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  13171. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  13172. int times, i;
  13173. /* Initialize */
  13174. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  13175. hash_test[0] = hash1;
  13176. hash_test[1] = hash2;
  13177. hash_test[2] = hash3;
  13178. times = sizeof(hash_test) / sizeof(byte*);
  13179. /* Good test args. */
  13180. for (i = 0; i < times; i++) {
  13181. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  13182. }
  13183. /* Test bad args. */
  13184. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13185. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13186. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  13187. wc_Sha384Free(&sha384);
  13188. #endif
  13189. return EXPECT_RESULT();
  13190. } /* END test_wc_Sha384FinalRaw */
  13191. /*
  13192. * Unit test function for wc_Sha384Free()
  13193. */
  13194. static int test_wc_Sha384Free(void)
  13195. {
  13196. EXPECT_DECLS;
  13197. #ifdef WOLFSSL_SHA384
  13198. wc_Sha384Free(NULL);
  13199. /* Set result to SUCCESS. */
  13200. ExpectTrue(1);
  13201. #endif
  13202. return EXPECT_RESULT();
  13203. } /* END test_wc_Sha384Free */
  13204. /*
  13205. * Unit test function for wc_Sha384GetHash()
  13206. */
  13207. static int test_wc_Sha384GetHash(void)
  13208. {
  13209. EXPECT_DECLS;
  13210. #ifdef WOLFSSL_SHA384
  13211. wc_Sha384 sha384;
  13212. byte hash1[WC_SHA384_DIGEST_SIZE];
  13213. /* Initialize */
  13214. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  13215. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  13216. /* test bad arguments*/
  13217. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  13218. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  13219. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  13220. wc_Sha384Free(&sha384);
  13221. #endif
  13222. return EXPECT_RESULT();
  13223. } /* END test_wc_Sha384GetHash */
  13224. /*
  13225. * Unit test function for wc_Sha384Copy()
  13226. */
  13227. static int test_wc_Sha384Copy(void)
  13228. {
  13229. EXPECT_DECLS;
  13230. #ifdef WOLFSSL_SHA384
  13231. wc_Sha384 sha384;
  13232. wc_Sha384 temp;
  13233. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  13234. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  13235. /* Initialize */
  13236. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  13237. ExpectIntEQ(wc_InitSha384(&temp), 0);
  13238. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  13239. /* test bad arguments*/
  13240. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  13241. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  13242. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  13243. wc_Sha384Free(&sha384);
  13244. wc_Sha384Free(&temp);
  13245. #endif
  13246. return EXPECT_RESULT();
  13247. } /* END test_wc_Sha384Copy */
  13248. /*
  13249. * Testing wc_InitSha224();
  13250. */
  13251. static int test_wc_InitSha224(void)
  13252. {
  13253. EXPECT_DECLS;
  13254. #ifdef WOLFSSL_SHA224
  13255. wc_Sha224 sha224;
  13256. /* Test good arg. */
  13257. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13258. /* Test bad arg. */
  13259. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  13260. wc_Sha224Free(&sha224);
  13261. #endif
  13262. return EXPECT_RESULT();
  13263. } /* END test_wc_InitSha224 */
  13264. /*
  13265. * Unit test on wc_Sha224Update
  13266. */
  13267. static int test_wc_Sha224Update(void)
  13268. {
  13269. EXPECT_DECLS;
  13270. #ifdef WOLFSSL_SHA224
  13271. wc_Sha224 sha224;
  13272. byte hash[WC_SHA224_DIGEST_SIZE];
  13273. testVector a, b, c;
  13274. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13275. /* Input. */
  13276. a.input = "a";
  13277. a.inLen = XSTRLEN(a.input);
  13278. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  13279. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  13280. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  13281. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  13282. /* Update input. */
  13283. a.input = "abc";
  13284. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  13285. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  13286. a.inLen = XSTRLEN(a.input);
  13287. a.outLen = XSTRLEN(a.output);
  13288. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  13289. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  13290. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  13291. /* Pass in bad values. */
  13292. b.input = NULL;
  13293. b.inLen = 0;
  13294. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  13295. c.input = NULL;
  13296. c.inLen = WC_SHA224_DIGEST_SIZE;
  13297. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  13298. BAD_FUNC_ARG);
  13299. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  13300. BAD_FUNC_ARG);
  13301. wc_Sha224Free(&sha224);
  13302. #endif
  13303. return EXPECT_RESULT();
  13304. } /* END test_wc_Sha224Update */
  13305. /*
  13306. * Unit test for wc_Sha224Final();
  13307. */
  13308. static int test_wc_Sha224Final(void)
  13309. {
  13310. EXPECT_DECLS;
  13311. #ifdef WOLFSSL_SHA224
  13312. wc_Sha224 sha224;
  13313. byte* hash_test[3];
  13314. byte hash1[WC_SHA224_DIGEST_SIZE];
  13315. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  13316. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  13317. int times, i;
  13318. /* Initialize */
  13319. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13320. hash_test[0] = hash1;
  13321. hash_test[1] = hash2;
  13322. hash_test[2] = hash3;
  13323. times = sizeof(hash_test) / sizeof(byte*);
  13324. /* Good test args. */
  13325. /* Testing oversized buffers. */
  13326. for (i = 0; i < times; i++) {
  13327. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  13328. }
  13329. /* Test bad args. */
  13330. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  13331. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  13332. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  13333. wc_Sha224Free(&sha224);
  13334. #endif
  13335. return EXPECT_RESULT();
  13336. } /* END test_wc_Sha224Final */
  13337. /*
  13338. * Unit test function for wc_Sha224SetFlags()
  13339. */
  13340. static int test_wc_Sha224SetFlags(void)
  13341. {
  13342. EXPECT_DECLS;
  13343. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  13344. wc_Sha224 sha224;
  13345. word32 flags = WC_HASH_FLAG_WILLCOPY;
  13346. /* Initialize */
  13347. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13348. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  13349. flags = 0;
  13350. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  13351. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  13352. wc_Sha224Free(&sha224);
  13353. #endif
  13354. return EXPECT_RESULT();
  13355. } /* END test_wc_Sha224SetFlags */
  13356. /*
  13357. * Unit test function for wc_Sha224GetFlags()
  13358. */
  13359. static int test_wc_Sha224GetFlags(void)
  13360. {
  13361. EXPECT_DECLS;
  13362. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  13363. wc_Sha224 sha224;
  13364. word32 flags = 0;
  13365. /* Initialize */
  13366. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13367. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  13368. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13369. wc_Sha224Free(&sha224);
  13370. #endif
  13371. return EXPECT_RESULT();
  13372. } /* END test_wc_Sha224GetFlags */
  13373. /*
  13374. * Unit test function for wc_Sha224Free()
  13375. */
  13376. static int test_wc_Sha224Free(void)
  13377. {
  13378. EXPECT_DECLS;
  13379. #ifdef WOLFSSL_SHA224
  13380. wc_Sha224Free(NULL);
  13381. /* Set result to SUCCESS. */
  13382. ExpectTrue(1);
  13383. #endif
  13384. return EXPECT_RESULT();
  13385. } /* END test_wc_Sha224Free */
  13386. /*
  13387. * Unit test function for wc_Sha224GetHash()
  13388. */
  13389. static int test_wc_Sha224GetHash(void)
  13390. {
  13391. EXPECT_DECLS;
  13392. #ifdef WOLFSSL_SHA224
  13393. wc_Sha224 sha224;
  13394. byte hash1[WC_SHA224_DIGEST_SIZE];
  13395. /* Initialize */
  13396. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13397. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  13398. /* test bad arguments*/
  13399. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  13400. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  13401. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  13402. wc_Sha224Free(&sha224);
  13403. #endif
  13404. return EXPECT_RESULT();
  13405. } /* END test_wc_Sha224GetHash */
  13406. /*
  13407. * Unit test function for wc_Sha224Copy()
  13408. */
  13409. static int test_wc_Sha224Copy(void)
  13410. {
  13411. EXPECT_DECLS;
  13412. #ifdef WOLFSSL_SHA224
  13413. wc_Sha224 sha224;
  13414. wc_Sha224 temp;
  13415. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  13416. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  13417. /* Initialize */
  13418. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13419. ExpectIntEQ(wc_InitSha224(&temp), 0);
  13420. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  13421. /* test bad arguments*/
  13422. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  13423. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  13424. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  13425. wc_Sha224Free(&sha224);
  13426. wc_Sha224Free(&temp);
  13427. #endif
  13428. return EXPECT_RESULT();
  13429. } /* END test_wc_Sha224Copy */
  13430. /*
  13431. * Testing wc_InitRipeMd()
  13432. */
  13433. static int test_wc_InitRipeMd(void)
  13434. {
  13435. EXPECT_DECLS;
  13436. #ifdef WOLFSSL_RIPEMD
  13437. RipeMd ripemd;
  13438. /* Test good arg. */
  13439. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13440. /* Test bad arg. */
  13441. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  13442. #endif
  13443. return EXPECT_RESULT();
  13444. } /* END test_wc_InitRipeMd */
  13445. /*
  13446. * Testing wc_RipeMdUpdate()
  13447. */
  13448. static int test_wc_RipeMdUpdate(void)
  13449. {
  13450. EXPECT_DECLS;
  13451. #ifdef WOLFSSL_RIPEMD
  13452. RipeMd ripemd;
  13453. byte hash[RIPEMD_DIGEST_SIZE];
  13454. testVector a, b, c;
  13455. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13456. /* Input */
  13457. a.input = "a";
  13458. a.inLen = XSTRLEN(a.input);
  13459. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13460. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13461. /* Update input. */
  13462. a.input = "abc";
  13463. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  13464. "\xb0\x87\xf1\x5a\x0b\xfc";
  13465. a.inLen = XSTRLEN(a.input);
  13466. a.outLen = XSTRLEN(a.output);
  13467. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13468. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13469. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  13470. /* Pass in bad values. */
  13471. b.input = NULL;
  13472. b.inLen = 0;
  13473. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  13474. c.input = NULL;
  13475. c.inLen = RIPEMD_DIGEST_SIZE;
  13476. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  13477. BAD_FUNC_ARG);
  13478. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13479. BAD_FUNC_ARG);
  13480. #endif
  13481. return EXPECT_RESULT();
  13482. } /* END test_wc_RipeMdUdpate */
  13483. /*
  13484. * Unit test function for wc_RipeMdFinal()
  13485. */
  13486. static int test_wc_RipeMdFinal(void)
  13487. {
  13488. EXPECT_DECLS;
  13489. #ifdef WOLFSSL_RIPEMD
  13490. RipeMd ripemd;
  13491. byte* hash_test[3];
  13492. byte hash1[RIPEMD_DIGEST_SIZE];
  13493. byte hash2[2*RIPEMD_DIGEST_SIZE];
  13494. byte hash3[5*RIPEMD_DIGEST_SIZE];
  13495. int times, i;
  13496. /* Initialize */
  13497. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13498. hash_test[0] = hash1;
  13499. hash_test[1] = hash2;
  13500. hash_test[2] = hash3;
  13501. times = sizeof(hash_test) / sizeof(byte*);
  13502. /* Testing oversized buffers. */
  13503. for (i = 0; i < times; i++) {
  13504. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  13505. }
  13506. /* Test bad args. */
  13507. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  13508. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  13509. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  13510. #endif
  13511. return EXPECT_RESULT();
  13512. } /* END test_wc_RipeMdFinal */
  13513. /*
  13514. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  13515. * wc_InitSha3_512
  13516. */
  13517. static int test_wc_InitSha3(void)
  13518. {
  13519. EXPECT_DECLS;
  13520. #if defined(WOLFSSL_SHA3)
  13521. wc_Sha3 sha3;
  13522. (void)sha3;
  13523. #if !defined(WOLFSSL_NOSHA3_224)
  13524. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13525. /* Test bad args. */
  13526. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13527. wc_Sha3_224_Free(&sha3);
  13528. #endif /* NOSHA3_224 */
  13529. #if !defined(WOLFSSL_NOSHA3_256)
  13530. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13531. /* Test bad args. */
  13532. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13533. wc_Sha3_256_Free(&sha3);
  13534. #endif /* NOSHA3_256 */
  13535. #if !defined(WOLFSSL_NOSHA3_384)
  13536. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13537. /* Test bad args. */
  13538. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13539. wc_Sha3_384_Free(&sha3);
  13540. #endif /* NOSHA3_384 */
  13541. #if !defined(WOLFSSL_NOSHA3_512)
  13542. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13543. /* Test bad args. */
  13544. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13545. wc_Sha3_512_Free(&sha3);
  13546. #endif /* NOSHA3_512 */
  13547. #endif
  13548. return EXPECT_RESULT();
  13549. } /* END test_wc_InitSha3 */
  13550. /*
  13551. * Testing wc_Sha3_Update()
  13552. */
  13553. static int testing_wc_Sha3_Update(void)
  13554. {
  13555. EXPECT_DECLS;
  13556. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  13557. !defined(WOLFSSL_AFALG_XILINX)
  13558. wc_Sha3 sha3;
  13559. byte msg[] = "Everybody's working for the weekend.";
  13560. byte msg2[] = "Everybody gets Friday off.";
  13561. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13562. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13563. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13564. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13565. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13566. word32 msglen = sizeof(msg) - 1;
  13567. word32 msg2len = sizeof(msg2);
  13568. word32 msgCmplen = sizeof(msgCmp);
  13569. #if !defined(WOLFSSL_NOSHA3_224)
  13570. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13571. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  13572. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13573. ExpectTrue(sha3.i == msglen);
  13574. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13575. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13576. /* Pass bad args. */
  13577. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13578. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13579. wc_Sha3_224_Free(&sha3);
  13580. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13581. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  13582. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13583. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13584. wc_Sha3_224_Free(&sha3);
  13585. #endif /* SHA3_224 */
  13586. #if !defined(WOLFSSL_NOSHA3_256)
  13587. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13588. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  13589. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13590. ExpectTrue(sha3.i == msglen);
  13591. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13592. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13593. /* Pass bad args. */
  13594. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13595. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13596. wc_Sha3_256_Free(&sha3);
  13597. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13598. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  13599. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13600. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13601. wc_Sha3_256_Free(&sha3);
  13602. #endif /* SHA3_256 */
  13603. #if !defined(WOLFSSL_NOSHA3_384)
  13604. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13605. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  13606. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13607. ExpectTrue(sha3.i == msglen);
  13608. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13609. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13610. /* Pass bad args. */
  13611. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13612. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13613. wc_Sha3_384_Free(&sha3);
  13614. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13615. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  13616. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13617. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13618. wc_Sha3_384_Free(&sha3);
  13619. #endif /* SHA3_384 */
  13620. #if !defined(WOLFSSL_NOSHA3_512)
  13621. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13622. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  13623. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13624. ExpectTrue(sha3.i == msglen);
  13625. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13626. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13627. /* Pass bad args. */
  13628. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13629. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13630. wc_Sha3_512_Free(&sha3);
  13631. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13632. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  13633. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13634. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13635. wc_Sha3_512_Free(&sha3);
  13636. #endif /* SHA3_512 */
  13637. #endif /* WOLFSSL_SHA3 */
  13638. return EXPECT_RESULT();
  13639. } /* END testing_wc_Sha3_Update */
  13640. /*
  13641. * Testing wc_Sha3_224_Final()
  13642. */
  13643. static int test_wc_Sha3_224_Final(void)
  13644. {
  13645. EXPECT_DECLS;
  13646. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13647. wc_Sha3 sha3;
  13648. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13649. "nopnopq";
  13650. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13651. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13652. "\x64\xea\xd0\xfc\xce\x33";
  13653. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13654. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13655. /* Init stack variables. */
  13656. XMEMSET(hash, 0, sizeof(hash));
  13657. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13658. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13659. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13660. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  13661. /* Test bad args. */
  13662. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  13663. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  13664. wc_Sha3_224_Free(&sha3);
  13665. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13666. /* Init stack variables. */
  13667. XMEMSET(hash, 0, sizeof(hash));
  13668. XMEMSET(hashRet, 0, sizeof(hashRet));
  13669. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13670. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  13671. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13672. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  13673. /* Test bad args. */
  13674. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13675. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13676. wc_Sha3_224_Free(&sha3);
  13677. #endif
  13678. return EXPECT_RESULT();
  13679. } /* END test_wc_Sha3_224_Final */
  13680. /*
  13681. * Testing wc_Sha3_256_Final()
  13682. */
  13683. static int test_wc_Sha3_256_Final(void)
  13684. {
  13685. EXPECT_DECLS;
  13686. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13687. wc_Sha3 sha3;
  13688. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13689. "nopnopq";
  13690. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13691. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13692. "\xdd\x97\x49\x6d\x33\x76";
  13693. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13694. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13695. /* Init stack variables. */
  13696. XMEMSET(hash, 0, sizeof(hash));
  13697. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13698. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13699. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13700. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  13701. /* Test bad args. */
  13702. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  13703. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  13704. wc_Sha3_256_Free(&sha3);
  13705. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13706. /* Init stack variables. */
  13707. XMEMSET(hash, 0, sizeof(hash));
  13708. XMEMSET(hashRet, 0, sizeof(hashRet));
  13709. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13710. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  13711. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13712. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  13713. /* Test bad args. */
  13714. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13715. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13716. wc_Sha3_256_Free(&sha3);
  13717. #endif
  13718. return EXPECT_RESULT();
  13719. } /* END test_wc_Sha3_256_Final */
  13720. /*
  13721. * Testing wc_Sha3_384_Final()
  13722. */
  13723. static int test_wc_Sha3_384_Final(void)
  13724. {
  13725. EXPECT_DECLS;
  13726. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13727. wc_Sha3 sha3;
  13728. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13729. "nopnopq";
  13730. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13731. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13732. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13733. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13734. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13735. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13736. /* Init stack variables. */
  13737. XMEMSET(hash, 0, sizeof(hash));
  13738. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13739. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13740. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13741. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  13742. /* Test bad args. */
  13743. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  13744. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  13745. wc_Sha3_384_Free(&sha3);
  13746. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13747. /* Init stack variables. */
  13748. XMEMSET(hash, 0, sizeof(hash));
  13749. XMEMSET(hashRet, 0, sizeof(hashRet));
  13750. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13751. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  13752. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13753. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  13754. /* Test bad args. */
  13755. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13756. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13757. wc_Sha3_384_Free(&sha3);
  13758. #endif
  13759. return EXPECT_RESULT();
  13760. } /* END test_wc_Sha3_384_Final */
  13761. /*
  13762. * Testing wc_Sha3_512_Final()
  13763. */
  13764. static int test_wc_Sha3_512_Final(void)
  13765. {
  13766. EXPECT_DECLS;
  13767. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13768. !defined(WOLFSSL_NOSHA3_384)
  13769. wc_Sha3 sha3;
  13770. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13771. "nopnopq";
  13772. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13773. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13774. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13775. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13776. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13777. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13778. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13779. /* Init stack variables. */
  13780. XMEMSET(hash, 0, sizeof(hash));
  13781. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13782. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13783. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13784. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  13785. /* Test bad args. */
  13786. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  13787. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  13788. wc_Sha3_512_Free(&sha3);
  13789. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13790. /* Init stack variables. */
  13791. XMEMSET(hash, 0, sizeof(hash));
  13792. XMEMSET(hashRet, 0, sizeof(hashRet));
  13793. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13794. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  13795. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13796. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  13797. /* Test bad args. */
  13798. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13799. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13800. wc_Sha3_512_Free(&sha3);
  13801. #endif
  13802. return EXPECT_RESULT();
  13803. } /* END test_wc_Sha3_512_Final */
  13804. /*
  13805. * Testing wc_Sha3_224_Copy()
  13806. */
  13807. static int test_wc_Sha3_224_Copy(void)
  13808. {
  13809. EXPECT_DECLS;
  13810. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13811. wc_Sha3 sha3, sha3Cpy;
  13812. const char* msg = TEST_STRING;
  13813. word32 msglen = (word32)TEST_STRING_SZ;
  13814. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13815. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13816. XMEMSET(hash, 0, sizeof(hash));
  13817. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13818. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13819. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13820. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13821. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13822. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13823. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13824. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13825. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13826. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13827. /* Test bad args. */
  13828. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13829. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13830. wc_Sha3_224_Free(&sha3);
  13831. wc_Sha3_224_Free(&sha3Cpy);
  13832. #endif
  13833. return EXPECT_RESULT();
  13834. } /* END test_wc_Sha3_224_Copy */
  13835. /*
  13836. * Testing wc_Sha3_256_Copy()
  13837. */
  13838. static int test_wc_Sha3_256_Copy(void)
  13839. {
  13840. EXPECT_DECLS;
  13841. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13842. wc_Sha3 sha3, sha3Cpy;
  13843. const char* msg = TEST_STRING;
  13844. word32 msglen = (word32)TEST_STRING_SZ;
  13845. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13846. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13847. XMEMSET(hash, 0, sizeof(hash));
  13848. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13849. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13850. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13851. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13852. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13853. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13854. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13855. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13856. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13857. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13858. /* Test bad args. */
  13859. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13860. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13861. wc_Sha3_256_Free(&sha3);
  13862. wc_Sha3_256_Free(&sha3Cpy);
  13863. #endif
  13864. return EXPECT_RESULT();
  13865. } /* END test_wc_Sha3_256_Copy */
  13866. /*
  13867. * Testing wc_Sha3_384_Copy()
  13868. */
  13869. static int test_wc_Sha3_384_Copy(void)
  13870. {
  13871. EXPECT_DECLS;
  13872. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13873. wc_Sha3 sha3, sha3Cpy;
  13874. const char* msg = TEST_STRING;
  13875. word32 msglen = (word32)TEST_STRING_SZ;
  13876. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13877. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13878. XMEMSET(hash, 0, sizeof(hash));
  13879. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13880. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13881. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13882. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13883. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13884. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13885. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13886. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13887. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13888. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13889. /* Test bad args. */
  13890. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13891. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13892. wc_Sha3_384_Free(&sha3);
  13893. wc_Sha3_384_Free(&sha3Cpy);
  13894. #endif
  13895. return EXPECT_RESULT();
  13896. } /* END test_wc_Sha3_384_Copy */
  13897. /*
  13898. * Testing wc_Sha3_512_Copy()
  13899. */
  13900. static int test_wc_Sha3_512_Copy(void)
  13901. {
  13902. EXPECT_DECLS;
  13903. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13904. wc_Sha3 sha3, sha3Cpy;
  13905. const char* msg = TEST_STRING;
  13906. word32 msglen = (word32)TEST_STRING_SZ;
  13907. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13908. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13909. XMEMSET(hash, 0, sizeof(hash));
  13910. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13911. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13912. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13913. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13914. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13915. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13916. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13917. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13918. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13919. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13920. /* Test bad args. */
  13921. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13922. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13923. wc_Sha3_512_Free(&sha3);
  13924. wc_Sha3_512_Free(&sha3Cpy);
  13925. #endif
  13926. return EXPECT_RESULT();
  13927. } /* END test_wc_Sha3_512_Copy */
  13928. /*
  13929. * Unit test function for wc_Sha3_GetFlags()
  13930. */
  13931. static int test_wc_Sha3_GetFlags(void)
  13932. {
  13933. EXPECT_DECLS;
  13934. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13935. wc_Sha3 sha3;
  13936. word32 flags = 0;
  13937. /* Initialize */
  13938. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13939. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13940. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13941. wc_Sha3_224_Free(&sha3);
  13942. #endif
  13943. return EXPECT_RESULT();
  13944. } /* END test_wc_Sha3_GetFlags */
  13945. static int test_wc_InitShake256(void)
  13946. {
  13947. EXPECT_DECLS;
  13948. #ifdef WOLFSSL_SHAKE256
  13949. wc_Shake shake;
  13950. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13951. /* Test bad args. */
  13952. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13953. wc_Shake256_Free(&shake);
  13954. #endif
  13955. return EXPECT_RESULT();
  13956. }
  13957. static int testing_wc_Shake256_Update(void)
  13958. {
  13959. EXPECT_DECLS;
  13960. #ifdef WOLFSSL_SHAKE256
  13961. wc_Shake shake;
  13962. byte msg[] = "Everybody's working for the weekend.";
  13963. byte msg2[] = "Everybody gets Friday off.";
  13964. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13965. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13966. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13967. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13968. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13969. word32 msglen = sizeof(msg) - 1;
  13970. word32 msg2len = sizeof(msg2);
  13971. word32 msgCmplen = sizeof(msgCmp);
  13972. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13973. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13974. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13975. ExpectTrue(shake.i == msglen);
  13976. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13977. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13978. /* Pass bad args. */
  13979. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13980. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13981. wc_Shake256_Free(&shake);
  13982. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13983. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13984. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13985. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13986. wc_Shake256_Free(&shake);
  13987. #endif /* WOLFSSL_SHAKE256 */
  13988. return EXPECT_RESULT();
  13989. }
  13990. static int test_wc_Shake256_Final(void)
  13991. {
  13992. EXPECT_DECLS;
  13993. #ifdef WOLFSSL_SHAKE256
  13994. wc_Shake shake;
  13995. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13996. "nopnopq";
  13997. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13998. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13999. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  14000. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  14001. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  14002. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  14003. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  14004. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  14005. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  14006. byte hash[114];
  14007. /* Init stack variables. */
  14008. XMEMSET(hash, 0, sizeof(hash));
  14009. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  14010. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  14011. 0);
  14012. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  14013. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  14014. /* Test bad args. */
  14015. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  14016. BAD_FUNC_ARG);
  14017. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  14018. BAD_FUNC_ARG);
  14019. wc_Shake256_Free(&shake);
  14020. #endif
  14021. return EXPECT_RESULT();
  14022. }
  14023. /*
  14024. * Testing wc_Shake256_Copy()
  14025. */
  14026. static int test_wc_Shake256_Copy(void)
  14027. {
  14028. EXPECT_DECLS;
  14029. #ifdef WOLFSSL_SHAKE256
  14030. wc_Shake shake, shakeCpy;
  14031. const char* msg = TEST_STRING;
  14032. word32 msglen = (word32)TEST_STRING_SZ;
  14033. byte hash[144];
  14034. byte hashCpy[144];
  14035. word32 hashLen = sizeof(hash);
  14036. word32 hashLenCpy = sizeof(hashCpy);
  14037. XMEMSET(hash, 0, sizeof(hash));
  14038. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  14039. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  14040. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  14041. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  14042. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  14043. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  14044. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  14045. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  14046. /* Test bad args. */
  14047. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  14048. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  14049. wc_Shake256_Free(&shake);
  14050. wc_Shake256_Free(&shakeCpy);
  14051. #endif
  14052. return EXPECT_RESULT();
  14053. } /* END test_wc_Shake256_Copy */
  14054. /*
  14055. * Unit test function for wc_Shake256Hash()
  14056. */
  14057. static int test_wc_Shake256Hash(void)
  14058. {
  14059. EXPECT_DECLS;
  14060. #ifdef WOLFSSL_SHAKE256
  14061. const byte data[] = { /* Hello World */
  14062. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  14063. 0x72,0x6c,0x64
  14064. };
  14065. word32 len = sizeof(data);
  14066. byte hash[144];
  14067. word32 hashLen = sizeof(hash);
  14068. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  14069. #endif
  14070. return EXPECT_RESULT();
  14071. } /* END test_wc_Shake256Hash */
  14072. /*
  14073. * Testing wc_InitSm3(), wc_Sm3Free()
  14074. */
  14075. static int test_wc_InitSm3Free(void)
  14076. {
  14077. EXPECT_DECLS;
  14078. #ifdef WOLFSSL_SM3
  14079. wc_Sm3 sm3;
  14080. /* Invalid Parameters */
  14081. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14082. /* Valid Parameters */
  14083. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  14084. wc_Sm3Free(NULL);
  14085. wc_Sm3Free(&sm3);
  14086. #endif
  14087. return EXPECT_RESULT();
  14088. } /* END test_wc_InitSm3 */
  14089. /*
  14090. * Testing wc_Sm3Update(), wc_Sm3Final()
  14091. */
  14092. static int test_wc_Sm3UpdateFinal(void)
  14093. {
  14094. EXPECT_DECLS;
  14095. #ifdef WOLFSSL_SM3
  14096. wc_Sm3 sm3;
  14097. byte data[WC_SM3_BLOCK_SIZE * 4];
  14098. byte hash[WC_SM3_DIGEST_SIZE];
  14099. byte calcHash[WC_SM3_DIGEST_SIZE];
  14100. byte expHash[WC_SM3_DIGEST_SIZE] = {
  14101. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  14102. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  14103. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  14104. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  14105. };
  14106. word32 chunk;
  14107. word32 i;
  14108. XMEMSET(data, 0, sizeof(data));
  14109. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  14110. /* Invalid Parameters */
  14111. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  14112. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  14113. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  14114. /* Valid Parameters */
  14115. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  14116. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  14117. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  14118. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  14119. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  14120. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  14121. /* Ensure too many bytes for lengths. */
  14122. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  14123. /* Invalid Parameters */
  14124. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  14125. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  14126. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  14127. /* Valid Parameters */
  14128. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  14129. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  14130. /* Chunk tests. */
  14131. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  14132. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  14133. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  14134. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  14135. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  14136. }
  14137. if (i < (word32)sizeof(data)) {
  14138. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  14139. 0);
  14140. }
  14141. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  14142. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  14143. }
  14144. /* Not testing when the low 32-bit length overflows. */
  14145. wc_Sm3Free(&sm3);
  14146. #endif
  14147. return EXPECT_RESULT();
  14148. } /* END test_wc_Sm3Update */
  14149. /*
  14150. * Testing wc_Sm3GetHash()
  14151. */
  14152. static int test_wc_Sm3GetHash(void)
  14153. {
  14154. EXPECT_DECLS;
  14155. #ifdef WOLFSSL_SM3
  14156. wc_Sm3 sm3;
  14157. byte hash[WC_SM3_DIGEST_SIZE];
  14158. byte calcHash[WC_SM3_DIGEST_SIZE];
  14159. byte data[WC_SM3_BLOCK_SIZE];
  14160. XMEMSET(data, 0, sizeof(data));
  14161. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  14162. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  14163. /* Invalid Parameters */
  14164. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  14165. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  14166. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  14167. /* Valid Parameters */
  14168. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  14169. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  14170. /* With update. */
  14171. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  14172. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  14173. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  14174. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  14175. wc_Sm3Free(&sm3);
  14176. #endif
  14177. return EXPECT_RESULT();
  14178. } /* END test_wc_Sm3Update */
  14179. /*
  14180. * Testing wc_Sm3Copy()
  14181. */
  14182. static int test_wc_Sm3Copy(void)
  14183. {
  14184. EXPECT_DECLS;
  14185. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  14186. wc_Sm3 sm3;
  14187. wc_Sm3 sm3Copy;
  14188. byte hash[WC_SM3_DIGEST_SIZE];
  14189. byte hashCopy[WC_SM3_DIGEST_SIZE];
  14190. byte data[WC_SM3_BLOCK_SIZE + 1];
  14191. int i;
  14192. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  14193. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  14194. /* Invalid Parameters */
  14195. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  14196. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  14197. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  14198. /* Valid Parameters */
  14199. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  14200. /* Ensure all parts of data updated during hashing are copied. */
  14201. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  14202. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  14203. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  14204. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  14205. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  14206. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  14207. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  14208. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  14209. }
  14210. wc_Sm3Free(&sm3Copy);
  14211. wc_Sm3Free(&sm3);
  14212. #endif
  14213. return EXPECT_RESULT();
  14214. } /* END test_wc_Sm3Copy */
  14215. /*
  14216. * Testing wc_Sm3FinalRaw()
  14217. */
  14218. static int test_wc_Sm3FinalRaw(void)
  14219. {
  14220. EXPECT_DECLS;
  14221. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  14222. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  14223. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  14224. !defined(WOLFSSL_NO_HASH_RAW)
  14225. wc_Sm3 sm3;
  14226. byte hash1[WC_SM3_DIGEST_SIZE];
  14227. byte hash2[WC_SM3_DIGEST_SIZE];
  14228. byte hash3[WC_SM3_DIGEST_SIZE];
  14229. byte* hash_test[3] = { hash1, hash2, hash3 };
  14230. int times;
  14231. int i;
  14232. XMEMSET(&sm3, 0, sizeof(sm3));
  14233. /* Initialize */
  14234. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  14235. /* Invalid Parameters */
  14236. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  14237. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  14238. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  14239. times = sizeof(hash_test) / sizeof(byte*);
  14240. for (i = 0; i < times; i++) {
  14241. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  14242. }
  14243. wc_Sm3Free(&sm3);
  14244. #endif
  14245. return EXPECT_RESULT();
  14246. } /* END test_wc_Sm3FinalRaw */
  14247. /*
  14248. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  14249. */
  14250. static int test_wc_Sm3GetSetFlags(void)
  14251. {
  14252. EXPECT_DECLS;
  14253. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  14254. wc_Sm3 sm3;
  14255. wc_Sm3 sm3Copy;
  14256. word32 flags = 0;
  14257. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  14258. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  14259. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  14260. ExpectIntEQ(flags, 0);
  14261. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  14262. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  14263. ExpectIntEQ(flags, 0);
  14264. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  14265. ExpectIntEQ(flags, 0);
  14266. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  14267. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  14268. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  14269. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  14270. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  14271. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  14272. wc_Sm3Free(&sm3Copy);
  14273. wc_Sm3Free(&sm3);
  14274. #endif
  14275. return EXPECT_RESULT();
  14276. } /* END test_wc_Sm3Update */
  14277. /*
  14278. * Testing wc_Sm3Hash()
  14279. */
  14280. static int test_wc_Sm3Hash(void)
  14281. {
  14282. EXPECT_DECLS;
  14283. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  14284. byte data[WC_SM3_BLOCK_SIZE];
  14285. byte hash[WC_SM3_DIGEST_SIZE];
  14286. /* Invalid parameters. */
  14287. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  14288. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  14289. /* Valid parameters. */
  14290. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  14291. #endif
  14292. return EXPECT_RESULT();
  14293. } /* END test_wc_Sm3Hash */
  14294. /*
  14295. * Test function for wc_HmacSetKey
  14296. */
  14297. static int test_wc_Md5HmacSetKey(void)
  14298. {
  14299. EXPECT_DECLS;
  14300. #if !defined(NO_HMAC) && !defined(NO_MD5)
  14301. Hmac hmac;
  14302. int ret, times, itr;
  14303. const char* keys[]=
  14304. {
  14305. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  14306. #ifndef HAVE_FIPS
  14307. "Jefe", /* smaller than minimum FIPS key size */
  14308. #endif
  14309. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14310. };
  14311. times = sizeof(keys) / sizeof(char*);
  14312. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14313. for (itr = 0; itr < times; itr++) {
  14314. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  14315. (word32)XSTRLEN(keys[itr]));
  14316. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  14317. wc_HmacFree(&hmac);
  14318. ExpectIntEQ(ret, BAD_FUNC_ARG);
  14319. #else
  14320. ExpectIntEQ(ret, 0);
  14321. #endif
  14322. }
  14323. /* Bad args. */
  14324. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  14325. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14326. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  14327. BAD_FUNC_ARG);
  14328. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14329. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14330. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  14331. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  14332. ExpectIntEQ(ret, BAD_FUNC_ARG);
  14333. #elif defined(HAVE_FIPS)
  14334. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14335. #else
  14336. ExpectIntEQ(ret, 0);
  14337. #endif
  14338. wc_HmacFree(&hmac);
  14339. #endif
  14340. return EXPECT_RESULT();
  14341. } /* END test_wc_Md5HmacSetKey */
  14342. /*
  14343. * testing wc_HmacSetKey() on wc_Sha hash.
  14344. */
  14345. static int test_wc_ShaHmacSetKey(void)
  14346. {
  14347. EXPECT_DECLS;
  14348. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14349. Hmac hmac;
  14350. int ret, times, itr;
  14351. const char* keys[]=
  14352. {
  14353. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14354. "\x0b\x0b\x0b",
  14355. #ifndef HAVE_FIPS
  14356. "Jefe", /* smaller than minimum FIPS key size */
  14357. #endif
  14358. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14359. "\xAA\xAA\xAA"
  14360. };
  14361. times = sizeof(keys) / sizeof(char*);
  14362. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14363. for (itr = 0; itr < times; itr++) {
  14364. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  14365. (word32)XSTRLEN(keys[itr])), 0);
  14366. }
  14367. /* Bad args. */
  14368. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  14369. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14370. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  14371. BAD_FUNC_ARG);
  14372. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14373. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14374. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  14375. #ifdef HAVE_FIPS
  14376. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14377. #else
  14378. ExpectIntEQ(ret, 0);
  14379. #endif
  14380. wc_HmacFree(&hmac);
  14381. #endif
  14382. return EXPECT_RESULT();
  14383. } /* END test_wc_ShaHmacSetKey() */
  14384. /*
  14385. * testing wc_HmacSetKey() on Sha224 hash.
  14386. */
  14387. static int test_wc_Sha224HmacSetKey(void)
  14388. {
  14389. EXPECT_DECLS;
  14390. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14391. Hmac hmac;
  14392. int ret, times, itr;
  14393. const char* keys[]=
  14394. {
  14395. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14396. "\x0b\x0b\x0b",
  14397. #ifndef HAVE_FIPS
  14398. "Jefe", /* smaller than minimum FIPS key size */
  14399. #endif
  14400. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14401. "\xAA\xAA\xAA"
  14402. };
  14403. times = sizeof(keys) / sizeof(char*);
  14404. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14405. for (itr = 0; itr < times; itr++) {
  14406. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  14407. (word32)XSTRLEN(keys[itr])), 0);
  14408. }
  14409. /* Bad args. */
  14410. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  14411. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14412. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  14413. BAD_FUNC_ARG);
  14414. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14415. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14416. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  14417. #ifdef HAVE_FIPS
  14418. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14419. #else
  14420. ExpectIntEQ(ret, 0);
  14421. #endif
  14422. wc_HmacFree(&hmac);
  14423. #endif
  14424. return EXPECT_RESULT();
  14425. } /* END test_wc_Sha224HmacSetKey() */
  14426. /*
  14427. * testing wc_HmacSetKey() on Sha256 hash
  14428. */
  14429. static int test_wc_Sha256HmacSetKey(void)
  14430. {
  14431. EXPECT_DECLS;
  14432. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14433. Hmac hmac;
  14434. int ret, times, itr;
  14435. const char* keys[]=
  14436. {
  14437. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14438. "\x0b\x0b\x0b",
  14439. #ifndef HAVE_FIPS
  14440. "Jefe", /* smaller than minimum FIPS key size */
  14441. #endif
  14442. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14443. "\xAA\xAA\xAA"
  14444. };
  14445. times = sizeof(keys) / sizeof(char*);
  14446. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14447. for (itr = 0; itr < times; itr++) {
  14448. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  14449. (word32)XSTRLEN(keys[itr])), 0);
  14450. }
  14451. /* Bad args. */
  14452. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  14453. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14454. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  14455. BAD_FUNC_ARG);
  14456. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14457. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14458. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  14459. #ifdef HAVE_FIPS
  14460. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14461. #else
  14462. ExpectIntEQ(ret, 0);
  14463. #endif
  14464. wc_HmacFree(&hmac);
  14465. #endif
  14466. return EXPECT_RESULT();
  14467. } /* END test_wc_Sha256HmacSetKey() */
  14468. /*
  14469. * testing wc_HmacSetKey on Sha384 hash.
  14470. */
  14471. static int test_wc_Sha384HmacSetKey(void)
  14472. {
  14473. EXPECT_DECLS;
  14474. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14475. Hmac hmac;
  14476. int ret, times, itr;
  14477. const char* keys[]=
  14478. {
  14479. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14480. "\x0b\x0b\x0b",
  14481. #ifndef HAVE_FIPS
  14482. "Jefe", /* smaller than minimum FIPS key size */
  14483. #endif
  14484. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14485. "\xAA\xAA\xAA"
  14486. };
  14487. times = sizeof(keys) / sizeof(char*);
  14488. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14489. for (itr = 0; itr < times; itr++) {
  14490. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14491. (word32)XSTRLEN(keys[itr])), 0);
  14492. }
  14493. /* Bad args. */
  14494. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14495. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14496. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  14497. BAD_FUNC_ARG);
  14498. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14499. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14500. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14501. #ifdef HAVE_FIPS
  14502. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14503. #else
  14504. ExpectIntEQ(ret, 0);
  14505. #endif
  14506. wc_HmacFree(&hmac);
  14507. #endif
  14508. return EXPECT_RESULT();
  14509. } /* END test_wc_Sha384HmacSetKey() */
  14510. /*
  14511. * testing wc_HmacUpdate on wc_Md5 hash.
  14512. */
  14513. static int test_wc_Md5HmacUpdate(void)
  14514. {
  14515. EXPECT_DECLS;
  14516. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14517. Hmac hmac;
  14518. testVector a, b;
  14519. #ifdef HAVE_FIPS
  14520. const char* keys =
  14521. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14522. #else
  14523. const char* keys = "Jefe";
  14524. #endif
  14525. a.input = "what do ya want for nothing?";
  14526. a.inLen = XSTRLEN(a.input);
  14527. b.input = "Hi There";
  14528. b.inLen = XSTRLEN(b.input);
  14529. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14530. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  14531. (word32)XSTRLEN(keys)), 0);
  14532. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14533. /* Update Hmac. */
  14534. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14535. /* Test bad args. */
  14536. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14537. BAD_FUNC_ARG);
  14538. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14539. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14540. wc_HmacFree(&hmac);
  14541. #endif
  14542. return EXPECT_RESULT();
  14543. } /* END test_wc_Md5HmacUpdate */
  14544. /*
  14545. * testing wc_HmacUpdate on SHA hash.
  14546. */
  14547. static int test_wc_ShaHmacUpdate(void)
  14548. {
  14549. EXPECT_DECLS;
  14550. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14551. Hmac hmac;
  14552. testVector a, b;
  14553. #ifdef HAVE_FIPS
  14554. const char* keys =
  14555. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14556. #else
  14557. const char* keys = "Jefe";
  14558. #endif
  14559. a.input = "what do ya want for nothing?";
  14560. a.inLen = XSTRLEN(a.input);
  14561. b.input = "Hi There";
  14562. b.inLen = XSTRLEN(b.input);
  14563. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14564. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  14565. (word32)XSTRLEN(keys)), 0);
  14566. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14567. /* Update Hmac. */
  14568. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14569. /* Test bad args. */
  14570. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14571. BAD_FUNC_ARG);
  14572. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14573. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14574. wc_HmacFree(&hmac);
  14575. #endif
  14576. return EXPECT_RESULT();
  14577. } /* END test_wc_ShaHmacUpdate */
  14578. /*
  14579. * testing wc_HmacUpdate on SHA224 hash.
  14580. */
  14581. static int test_wc_Sha224HmacUpdate(void)
  14582. {
  14583. EXPECT_DECLS;
  14584. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14585. Hmac hmac;
  14586. testVector a, b;
  14587. #ifdef HAVE_FIPS
  14588. const char* keys =
  14589. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14590. #else
  14591. const char* keys = "Jefe";
  14592. #endif
  14593. a.input = "what do ya want for nothing?";
  14594. a.inLen = XSTRLEN(a.input);
  14595. b.input = "Hi There";
  14596. b.inLen = XSTRLEN(b.input);
  14597. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14598. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  14599. (word32)XSTRLEN(keys)), 0);
  14600. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14601. /* Update Hmac. */
  14602. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14603. /* Test bad args. */
  14604. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14605. BAD_FUNC_ARG);
  14606. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14607. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14608. wc_HmacFree(&hmac);
  14609. #endif
  14610. return EXPECT_RESULT();
  14611. } /* END test_wc_Sha224HmacUpdate */
  14612. /*
  14613. * testing wc_HmacUpdate on SHA256 hash.
  14614. */
  14615. static int test_wc_Sha256HmacUpdate(void)
  14616. {
  14617. EXPECT_DECLS;
  14618. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14619. Hmac hmac;
  14620. testVector a, b;
  14621. #ifdef HAVE_FIPS
  14622. const char* keys =
  14623. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14624. #else
  14625. const char* keys = "Jefe";
  14626. #endif
  14627. a.input = "what do ya want for nothing?";
  14628. a.inLen = XSTRLEN(a.input);
  14629. b.input = "Hi There";
  14630. b.inLen = XSTRLEN(b.input);
  14631. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14632. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  14633. (word32)XSTRLEN(keys)), 0);
  14634. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14635. /* Update Hmac. */
  14636. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14637. /* Test bad args. */
  14638. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14639. BAD_FUNC_ARG);
  14640. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14641. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14642. wc_HmacFree(&hmac);
  14643. #endif
  14644. return EXPECT_RESULT();
  14645. } /* END test_wc_Sha256HmacUpdate */
  14646. /*
  14647. * testing wc_HmacUpdate on SHA384 hash.
  14648. */
  14649. static int test_wc_Sha384HmacUpdate(void)
  14650. {
  14651. EXPECT_DECLS;
  14652. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14653. Hmac hmac;
  14654. testVector a, b;
  14655. #ifdef HAVE_FIPS
  14656. const char* keys =
  14657. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14658. #else
  14659. const char* keys = "Jefe";
  14660. #endif
  14661. a.input = "what do ya want for nothing?";
  14662. a.inLen = XSTRLEN(a.input);
  14663. b.input = "Hi There";
  14664. b.inLen = XSTRLEN(b.input);
  14665. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14666. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  14667. (word32)XSTRLEN(keys)), 0);
  14668. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14669. /* Update Hmac. */
  14670. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14671. /* Test bad args. */
  14672. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14673. BAD_FUNC_ARG);
  14674. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14675. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14676. wc_HmacFree(&hmac);
  14677. #endif
  14678. return EXPECT_RESULT();
  14679. } /* END test_wc_Sha384HmacUpdate */
  14680. /*
  14681. * Testing wc_HmacFinal() with MD5
  14682. */
  14683. static int test_wc_Md5HmacFinal(void)
  14684. {
  14685. EXPECT_DECLS;
  14686. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14687. Hmac hmac;
  14688. byte hash[WC_MD5_DIGEST_SIZE];
  14689. testVector a;
  14690. const char* key;
  14691. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14692. a.input = "Hi There";
  14693. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14694. "\x9d";
  14695. a.inLen = XSTRLEN(a.input);
  14696. a.outLen = XSTRLEN(a.output);
  14697. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14698. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  14699. 0);
  14700. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14701. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14702. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  14703. /* Try bad parameters. */
  14704. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14705. #ifndef HAVE_FIPS
  14706. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14707. #endif
  14708. wc_HmacFree(&hmac);
  14709. #endif
  14710. return EXPECT_RESULT();
  14711. } /* END test_wc_Md5HmacFinal */
  14712. /*
  14713. * Testing wc_HmacFinal() with SHA
  14714. */
  14715. static int test_wc_ShaHmacFinal(void)
  14716. {
  14717. EXPECT_DECLS;
  14718. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14719. Hmac hmac;
  14720. byte hash[WC_SHA_DIGEST_SIZE];
  14721. testVector a;
  14722. const char* key;
  14723. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14724. "\x0b\x0b\x0b";
  14725. a.input = "Hi There";
  14726. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14727. "\x8e\xf1\x46\xbe\x00";
  14728. a.inLen = XSTRLEN(a.input);
  14729. a.outLen = XSTRLEN(a.output);
  14730. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14731. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  14732. 0);
  14733. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14734. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14735. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  14736. /* Try bad parameters. */
  14737. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14738. #ifndef HAVE_FIPS
  14739. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14740. #endif
  14741. wc_HmacFree(&hmac);
  14742. #endif
  14743. return EXPECT_RESULT();
  14744. } /* END test_wc_ShaHmacFinal */
  14745. /*
  14746. * Testing wc_HmacFinal() with SHA224
  14747. */
  14748. static int test_wc_Sha224HmacFinal(void)
  14749. {
  14750. EXPECT_DECLS;
  14751. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14752. Hmac hmac;
  14753. byte hash[WC_SHA224_DIGEST_SIZE];
  14754. testVector a;
  14755. const char* key;
  14756. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14757. "\x0b\x0b\x0b";
  14758. a.input = "Hi There";
  14759. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14760. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14761. a.inLen = XSTRLEN(a.input);
  14762. a.outLen = XSTRLEN(a.output);
  14763. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14764. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  14765. (word32)XSTRLEN(key)), 0);
  14766. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14767. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14768. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  14769. /* Try bad parameters. */
  14770. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14771. #ifndef HAVE_FIPS
  14772. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14773. #endif
  14774. wc_HmacFree(&hmac);
  14775. #endif
  14776. return EXPECT_RESULT();
  14777. } /* END test_wc_Sha224HmacFinal */
  14778. /*
  14779. * Testing wc_HmacFinal() with SHA256
  14780. */
  14781. static int test_wc_Sha256HmacFinal(void)
  14782. {
  14783. EXPECT_DECLS;
  14784. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14785. Hmac hmac;
  14786. byte hash[WC_SHA256_DIGEST_SIZE];
  14787. testVector a;
  14788. const char* key;
  14789. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14790. "\x0b\x0b\x0b";
  14791. a.input = "Hi There";
  14792. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14793. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14794. "\xcf\xf7";
  14795. a.inLen = XSTRLEN(a.input);
  14796. a.outLen = XSTRLEN(a.output);
  14797. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14798. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  14799. (word32)XSTRLEN(key)), 0);
  14800. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14801. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14802. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  14803. /* Try bad parameters. */
  14804. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14805. #ifndef HAVE_FIPS
  14806. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14807. #endif
  14808. wc_HmacFree(&hmac);
  14809. #endif
  14810. return EXPECT_RESULT();
  14811. } /* END test_wc_Sha256HmacFinal */
  14812. /*
  14813. * Testing wc_HmacFinal() with SHA384
  14814. */
  14815. static int test_wc_Sha384HmacFinal(void)
  14816. {
  14817. EXPECT_DECLS;
  14818. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14819. Hmac hmac;
  14820. byte hash[WC_SHA384_DIGEST_SIZE];
  14821. testVector a;
  14822. const char* key;
  14823. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14824. "\x0b\x0b\x0b";
  14825. a.input = "Hi There";
  14826. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14827. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14828. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14829. "\xfa\x9c\xb6";
  14830. a.inLen = XSTRLEN(a.input);
  14831. a.outLen = XSTRLEN(a.output);
  14832. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14833. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14834. (word32)XSTRLEN(key)), 0);
  14835. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14836. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14837. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14838. /* Try bad parameters. */
  14839. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14840. #ifndef HAVE_FIPS
  14841. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14842. #endif
  14843. wc_HmacFree(&hmac);
  14844. #endif
  14845. return EXPECT_RESULT();
  14846. } /* END test_wc_Sha384HmacFinal */
  14847. /*
  14848. * Testing wc_InitCmac()
  14849. */
  14850. static int test_wc_InitCmac(void)
  14851. {
  14852. EXPECT_DECLS;
  14853. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14854. Cmac cmac1;
  14855. Cmac cmac2;
  14856. Cmac cmac3;
  14857. /* AES 128 key. */
  14858. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14859. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14860. /* AES 192 key. */
  14861. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14862. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14863. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14864. /* AES 256 key. */
  14865. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14866. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14867. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14868. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14869. word32 key1Sz = (word32)sizeof(key1) - 1;
  14870. word32 key2Sz = (word32)sizeof(key2) - 1;
  14871. word32 key3Sz = (word32)sizeof(key3) - 1;
  14872. int type = WC_CMAC_AES;
  14873. (void)key1;
  14874. (void)key1Sz;
  14875. (void)key2;
  14876. (void)key2Sz;
  14877. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14878. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14879. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14880. #ifdef WOLFSSL_AES_128
  14881. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14882. #endif
  14883. #ifdef WOLFSSL_AES_192
  14884. wc_AesFree(&cmac1.aes);
  14885. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14886. #endif
  14887. #ifdef WOLFSSL_AES_256
  14888. wc_AesFree(&cmac2.aes);
  14889. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14890. #endif
  14891. wc_AesFree(&cmac3.aes);
  14892. /* Test bad args. */
  14893. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14894. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14895. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14896. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14897. #endif
  14898. return EXPECT_RESULT();
  14899. } /* END test_wc_InitCmac */
  14900. /*
  14901. * Testing wc_CmacUpdate()
  14902. */
  14903. static int test_wc_CmacUpdate(void)
  14904. {
  14905. EXPECT_DECLS;
  14906. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14907. Cmac cmac;
  14908. byte key[] = {
  14909. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14910. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14911. };
  14912. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14913. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14914. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14915. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14916. "\xf4";
  14917. word32 inSz = (word32)sizeof(in) - 1;
  14918. word32 keySz = (word32)sizeof(key);
  14919. int type = WC_CMAC_AES;
  14920. XMEMSET(&cmac, 0, sizeof(Cmac));
  14921. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14922. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14923. /* Test bad args. */
  14924. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14925. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14926. wc_AesFree(&cmac.aes);
  14927. #endif
  14928. return EXPECT_RESULT();
  14929. } /* END test_wc_CmacUpdate */
  14930. /*
  14931. * Testing wc_CmacFinal()
  14932. */
  14933. static int test_wc_CmacFinal(void)
  14934. {
  14935. EXPECT_DECLS;
  14936. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14937. Cmac cmac;
  14938. byte key[] = {
  14939. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14940. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14941. };
  14942. byte msg[] = {
  14943. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14944. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14945. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14946. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14947. 0xf4
  14948. };
  14949. /* Test vectors from CMACGenAES128.rsp from
  14950. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14951. * Per RFC4493 truncation of lsb is possible.
  14952. */
  14953. byte expMac[] = {
  14954. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14955. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14956. };
  14957. byte mac[AES_BLOCK_SIZE];
  14958. word32 msgSz = (word32)sizeof(msg);
  14959. word32 keySz = (word32)sizeof(key);
  14960. word32 macSz = sizeof(mac);
  14961. word32 badMacSz = 17;
  14962. int expMacSz = sizeof(expMac);
  14963. int type = WC_CMAC_AES;
  14964. XMEMSET(&cmac, 0, sizeof(Cmac));
  14965. XMEMSET(mac, 0, macSz);
  14966. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14967. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14968. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14969. /* Pass in bad args. */
  14970. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14971. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14972. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14973. /* For the last call, use the API with implicit wc_CmacFree(). */
  14974. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14975. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14976. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14977. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14978. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14979. /* Pass in bad args. */
  14980. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14981. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14982. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14983. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14984. #endif
  14985. return EXPECT_RESULT();
  14986. } /* END test_wc_CmacFinal */
  14987. /*
  14988. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14989. */
  14990. static int test_wc_AesCmacGenerate(void)
  14991. {
  14992. EXPECT_DECLS;
  14993. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14994. byte key[] = {
  14995. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14996. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14997. };
  14998. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14999. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  15000. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  15001. "\x3d\x32\x65\x4c\x66\x23\xc5";
  15002. byte mac[AES_BLOCK_SIZE];
  15003. word32 keySz = sizeof(key);
  15004. word32 macSz = sizeof(mac);
  15005. word32 msgSz = sizeof(msg) - 1;
  15006. word32 expMacSz = sizeof(expMac) - 1;
  15007. XMEMSET(mac, 0, macSz);
  15008. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  15009. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  15010. /* Pass in bad args. */
  15011. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  15012. BAD_FUNC_ARG);
  15013. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  15014. BAD_FUNC_ARG);
  15015. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  15016. BAD_FUNC_ARG);
  15017. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  15018. BAD_FUNC_ARG);
  15019. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  15020. /* Test bad args. */
  15021. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  15022. BAD_FUNC_ARG);
  15023. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  15024. BAD_FUNC_ARG);
  15025. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  15026. BAD_FUNC_ARG);
  15027. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  15028. BAD_FUNC_ARG);
  15029. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  15030. BAD_FUNC_ARG);
  15031. #endif
  15032. return EXPECT_RESULT();
  15033. } /* END test_wc_AesCmacGenerate */
  15034. /*
  15035. * Testing streaming AES-GCM API.
  15036. */
  15037. static int test_wc_AesGcmStream(void)
  15038. {
  15039. EXPECT_DECLS;
  15040. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  15041. defined(WOLFSSL_AESGCM_STREAM)
  15042. int i;
  15043. WC_RNG rng[1];
  15044. Aes aesEnc[1];
  15045. Aes aesDec[1];
  15046. byte tag[AES_BLOCK_SIZE];
  15047. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  15048. byte out[AES_BLOCK_SIZE * 3 + 2];
  15049. byte plain[AES_BLOCK_SIZE * 3 + 2];
  15050. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  15051. byte key[AES_128_KEY_SIZE] = { 0, };
  15052. byte iv[AES_IV_SIZE] = { 1, };
  15053. byte ivOut[AES_IV_SIZE];
  15054. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  15055. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  15056. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  15057. };
  15058. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  15059. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  15060. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  15061. };
  15062. static const byte expTag[AES_BLOCK_SIZE] = {
  15063. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  15064. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  15065. };
  15066. XMEMSET(&rng, 0, sizeof(WC_RNG));
  15067. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15068. XMEMSET(&aesDec, 0, sizeof(Aes));
  15069. /* Create a random for generating IV/nonce. */
  15070. ExpectIntEQ(wc_InitRng(rng), 0);
  15071. /* Initialize data structures. */
  15072. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  15073. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  15074. /* BadParameters to streaming init. */
  15075. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15076. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15077. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  15078. BAD_FUNC_ARG);
  15079. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  15080. BAD_FUNC_ARG);
  15081. /* Bad parameters to encrypt update. */
  15082. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  15083. BAD_FUNC_ARG);
  15084. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  15085. BAD_FUNC_ARG);
  15086. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  15087. BAD_FUNC_ARG);
  15088. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  15089. BAD_FUNC_ARG);
  15090. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  15091. BAD_FUNC_ARG);
  15092. /* Bad parameters to decrypt update. */
  15093. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  15094. BAD_FUNC_ARG);
  15095. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  15096. BAD_FUNC_ARG);
  15097. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  15098. BAD_FUNC_ARG);
  15099. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  15100. BAD_FUNC_ARG);
  15101. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  15102. BAD_FUNC_ARG);
  15103. /* Bad parameters to encrypt final. */
  15104. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  15105. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  15106. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  15107. BAD_FUNC_ARG);
  15108. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  15109. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  15110. BAD_FUNC_ARG);
  15111. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  15112. BAD_FUNC_ARG);
  15113. /* Bad parameters to decrypt final. */
  15114. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  15115. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  15116. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  15117. BAD_FUNC_ARG);
  15118. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  15119. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  15120. BAD_FUNC_ARG);
  15121. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  15122. BAD_FUNC_ARG);
  15123. /* Check calling final before setting key fails. */
  15124. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  15125. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  15126. /* Check calling update before setting key else fails. */
  15127. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  15128. MISSING_KEY);
  15129. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  15130. MISSING_KEY);
  15131. /* Set key but not IV. */
  15132. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  15133. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  15134. /* Check calling final before setting IV fails. */
  15135. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  15136. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  15137. /* Check calling update before setting IV else fails. */
  15138. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  15139. MISSING_IV);
  15140. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  15141. MISSING_IV);
  15142. /* Set IV using fixed part IV and external IV APIs. */
  15143. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  15144. rng), 0);
  15145. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  15146. GCM_NONCE_MID_SZ), 0);
  15147. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  15148. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  15149. /* Encrypt and decrypt data. */
  15150. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  15151. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  15152. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  15153. /* Finalize and check tag matches. */
  15154. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  15155. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  15156. /* Set key and IV through streaming init API. */
  15157. wc_AesFree(aesEnc);
  15158. wc_AesFree(aesDec);
  15159. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  15160. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  15161. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15162. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15163. /* Encrypt/decrypt one block and AAD of one block. */
  15164. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  15165. AES_BLOCK_SIZE), 0);
  15166. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  15167. AES_BLOCK_SIZE), 0);
  15168. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  15169. /* Finalize and check tag matches. */
  15170. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  15171. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  15172. /* Set key and IV through streaming init API. */
  15173. wc_AesFree(aesEnc);
  15174. wc_AesFree(aesDec);
  15175. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  15176. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  15177. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15178. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15179. /* No data to encrypt/decrypt one byte of AAD. */
  15180. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  15181. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  15182. /* Finalize and check tag matches. */
  15183. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  15184. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  15185. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  15186. /* Set key and IV through streaming init API. */
  15187. wc_AesFree(aesEnc);
  15188. wc_AesFree(aesDec);
  15189. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  15190. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  15191. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15192. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15193. /* Encrypt/decrypt one byte and no AAD. */
  15194. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  15195. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  15196. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  15197. /* Finalize and check tag matches. */
  15198. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  15199. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  15200. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  15201. /* Set key and IV through streaming init API. */
  15202. wc_AesFree(aesEnc);
  15203. wc_AesFree(aesDec);
  15204. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  15205. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  15206. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15207. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15208. /* Encryption AES is one byte at a time */
  15209. for (i = 0; i < (int)sizeof(aad); i++) {
  15210. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  15211. 0);
  15212. }
  15213. for (i = 0; i < (int)sizeof(in); i++) {
  15214. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  15215. 0);
  15216. }
  15217. /* Decryption AES is two bytes at a time */
  15218. for (i = 0; i < (int)sizeof(aad); i += 2) {
  15219. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  15220. 0);
  15221. }
  15222. for (i = 0; i < (int)sizeof(aad); i += 2) {
  15223. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  15224. 0), 0);
  15225. }
  15226. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15227. /* Finalize and check tag matches. */
  15228. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  15229. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  15230. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  15231. /* Check streaming encryption can be decrypted with one shot. */
  15232. wc_AesFree(aesDec);
  15233. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  15234. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15235. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  15236. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  15237. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15238. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15239. wc_AesFree(aesEnc);
  15240. wc_AesFree(aesDec);
  15241. wc_FreeRng(rng);
  15242. #endif
  15243. return EXPECT_RESULT();
  15244. } /* END test_wc_AesGcmStream */
  15245. /*
  15246. * Testing streaming SM4 API.
  15247. */
  15248. static int test_wc_Sm4(void)
  15249. {
  15250. int res = TEST_SKIPPED;
  15251. #ifdef WOLFSSL_SM4
  15252. EXPECT_DECLS;
  15253. wc_Sm4 sm4;
  15254. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  15255. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  15256. unsigned char key[SM4_KEY_SIZE];
  15257. #endif
  15258. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  15259. unsigned char iv[SM4_IV_SIZE];
  15260. #endif
  15261. /* Invalid parameters - wc_Sm4Init */
  15262. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  15263. /* Valid cases - wc_Sm4Init */
  15264. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15265. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  15266. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  15267. XMEMSET(key, 0, sizeof(key));
  15268. /* Invalid parameters - wc_Sm4SetKey. */
  15269. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15270. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15271. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  15272. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15273. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15274. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15275. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15276. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  15277. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  15278. /* Valid cases - wc_Sm4SetKey. */
  15279. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15280. #endif
  15281. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  15282. XMEMSET(iv, 0, sizeof(iv));
  15283. /* Invalid parameters - wc_Sm4SetIV. */
  15284. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  15285. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  15286. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  15287. /* Valid cases - wc_Sm4SetIV. */
  15288. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15289. #endif
  15290. /* Valid cases - wc_Sm4Free */
  15291. wc_Sm4Free(NULL);
  15292. wc_Sm4Free(&sm4);
  15293. res = EXPECT_RESULT();
  15294. #endif
  15295. return res;
  15296. } /* END test_wc_Sm4 */
  15297. /*
  15298. * Testing block based SM4-ECB API.
  15299. */
  15300. static int test_wc_Sm4Ecb(void)
  15301. {
  15302. int res = TEST_SKIPPED;
  15303. #ifdef WOLFSSL_SM4_ECB
  15304. EXPECT_DECLS;
  15305. wc_Sm4 sm4;
  15306. unsigned char key[SM4_KEY_SIZE];
  15307. unsigned char in[SM4_BLOCK_SIZE * 2];
  15308. unsigned char out[SM4_BLOCK_SIZE * 2];
  15309. unsigned char out2[SM4_BLOCK_SIZE];
  15310. XMEMSET(key, 0, sizeof(key));
  15311. XMEMSET(in, 0, sizeof(in));
  15312. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15313. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15314. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  15315. /* Tested in test_wc_Sm4. */
  15316. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15317. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  15318. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15319. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15320. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15321. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15322. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15323. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15324. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15325. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15326. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15327. /* Valid cases - wc_Sm4EcbEncrypt. */
  15328. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  15329. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15330. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15331. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15332. /* In and out are same pointer. */
  15333. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15334. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15335. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  15336. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15337. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15338. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15339. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15340. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15341. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15342. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15343. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15344. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15345. /* Valid cases - wc_Sm4EcbDecrypt. */
  15346. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  15347. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15348. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15349. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15350. /* In and out are same pointer. */
  15351. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15352. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15353. wc_Sm4Free(&sm4);
  15354. res = EXPECT_RESULT();
  15355. #endif
  15356. return res;
  15357. } /* END test_wc_Sm4Ecb */
  15358. /*
  15359. * Testing block based SM4-CBC API.
  15360. */
  15361. static int test_wc_Sm4Cbc(void)
  15362. {
  15363. int res = TEST_SKIPPED;
  15364. #ifdef WOLFSSL_SM4_CBC
  15365. EXPECT_DECLS;
  15366. wc_Sm4 sm4;
  15367. unsigned char key[SM4_KEY_SIZE];
  15368. unsigned char iv[SM4_IV_SIZE];
  15369. unsigned char in[SM4_BLOCK_SIZE * 2];
  15370. unsigned char out[SM4_BLOCK_SIZE * 2];
  15371. unsigned char out2[SM4_BLOCK_SIZE];
  15372. XMEMSET(key, 0, sizeof(key));
  15373. XMEMSET(iv, 0, sizeof(iv));
  15374. XMEMSET(in, 0, sizeof(in));
  15375. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15376. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15377. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  15378. /* Tested in test_wc_Sm4. */
  15379. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15380. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  15381. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  15382. /* Tested in test_wc_Sm4. */
  15383. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15384. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  15385. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15386. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15387. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15388. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15389. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15390. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15391. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15392. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15393. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15394. /* Valid cases - wc_Sm4CbcEncrypt. */
  15395. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  15396. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15397. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15398. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15399. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15400. /* In and out are same pointer. */
  15401. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15402. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15403. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15404. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  15405. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15406. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15407. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15408. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15409. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15410. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15411. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15412. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15413. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15414. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15415. /* Valid cases - wc_Sm4CbcDecrypt. */
  15416. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  15417. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15418. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15419. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15420. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15421. /* In and out are same pointer. */
  15422. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15423. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15424. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15425. wc_Sm4Free(&sm4);
  15426. res = EXPECT_RESULT();
  15427. #endif
  15428. return res;
  15429. } /* END test_wc_Sm4Cbc */
  15430. /*
  15431. * Testing streaming SM4-CTR API.
  15432. */
  15433. static int test_wc_Sm4Ctr(void)
  15434. {
  15435. int res = TEST_SKIPPED;
  15436. #ifdef WOLFSSL_SM4_CTR
  15437. EXPECT_DECLS;
  15438. wc_Sm4 sm4;
  15439. unsigned char key[SM4_KEY_SIZE];
  15440. unsigned char iv[SM4_IV_SIZE];
  15441. unsigned char in[SM4_BLOCK_SIZE * 4];
  15442. unsigned char out[SM4_BLOCK_SIZE * 4];
  15443. unsigned char out2[SM4_BLOCK_SIZE * 4];
  15444. word32 chunk;
  15445. word32 i;
  15446. XMEMSET(key, 0, sizeof(key));
  15447. XMEMSET(iv, 0, sizeof(iv));
  15448. XMEMSET(in, 0, sizeof(in));
  15449. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15450. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15451. /* Tested in test_wc_Sm4. */
  15452. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15453. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  15454. /* Tested in test_wc_Sm4. */
  15455. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15456. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  15457. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15458. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  15459. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  15460. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  15461. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15462. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15463. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15464. /* Valid cases - wc_Sm4CtrEncrypt. */
  15465. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  15466. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  15467. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15468. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  15469. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  15470. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15471. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15472. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  15473. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15474. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15475. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15476. /* In and out are same pointer. Also check encrypt of cipher text produces
  15477. * plaintext.
  15478. */
  15479. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15480. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  15481. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15482. /* Chunking tests. */
  15483. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15484. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  15485. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  15486. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15487. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  15488. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  15489. }
  15490. if (i < (word32)sizeof(in)) {
  15491. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  15492. (word32)sizeof(in) - i), 0);
  15493. }
  15494. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  15495. }
  15496. for (i = 0; i < (word32)sizeof(iv); i++) {
  15497. iv[i] = 0xff;
  15498. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15499. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15500. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15501. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  15502. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  15503. }
  15504. wc_Sm4Free(&sm4);
  15505. res = EXPECT_RESULT();
  15506. #endif
  15507. return res;
  15508. } /* END test_wc_Sm4Ctr */
  15509. /*
  15510. * Testing stream SM4-GCM API.
  15511. */
  15512. static int test_wc_Sm4Gcm(void)
  15513. {
  15514. int res = TEST_SKIPPED;
  15515. #ifdef WOLFSSL_SM4_GCM
  15516. EXPECT_DECLS;
  15517. wc_Sm4 sm4;
  15518. unsigned char key[SM4_KEY_SIZE];
  15519. unsigned char nonce[GCM_NONCE_MAX_SZ];
  15520. unsigned char in[SM4_BLOCK_SIZE * 2];
  15521. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15522. unsigned char out[SM4_BLOCK_SIZE * 2];
  15523. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15524. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15525. unsigned char tag[SM4_BLOCK_SIZE];
  15526. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15527. word32 i;
  15528. XMEMSET(key, 0, sizeof(key));
  15529. XMEMSET(nonce, 0, sizeof(nonce));
  15530. XMEMSET(in, 0, sizeof(in));
  15531. XMEMSET(in2, 0, sizeof(in2));
  15532. XMEMSET(aad, 0, sizeof(aad));
  15533. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15534. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15535. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15536. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15537. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15538. /* Invalid parameters - wc_Sm4GcmSetKey. */
  15539. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15540. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15541. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  15542. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15543. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15544. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15545. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15546. /* Valid parameters - wc_Sm4GcmSetKey. */
  15547. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15548. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  15549. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15550. 0), BAD_FUNC_ARG);
  15551. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15552. 0), BAD_FUNC_ARG);
  15553. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15554. 0), BAD_FUNC_ARG);
  15555. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15556. 0), BAD_FUNC_ARG);
  15557. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15558. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15559. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15560. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15561. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15562. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15563. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15564. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15565. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15566. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15567. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15568. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15569. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15570. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15571. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15572. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15573. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15574. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15575. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15576. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15577. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  15578. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15579. 0), BAD_FUNC_ARG);
  15580. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15581. 0), BAD_FUNC_ARG);
  15582. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15583. 0), BAD_FUNC_ARG);
  15584. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15585. 0), BAD_FUNC_ARG);
  15586. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15587. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15588. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15589. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15590. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15591. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15592. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15593. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15594. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15595. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15596. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15597. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15598. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15599. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15600. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15601. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15602. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15603. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15604. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15605. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15606. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15607. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15608. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15609. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15610. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15611. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15612. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15613. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15614. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15615. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15616. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15617. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15618. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15619. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15620. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15621. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15622. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15623. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15624. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15625. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15626. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15627. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15628. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15629. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15630. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15631. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15632. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15633. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15634. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15635. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15636. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15637. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15638. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15639. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15640. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15641. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15642. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15643. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15644. SM4_GCM_AUTH_E);
  15645. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15646. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  15647. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15648. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15649. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15650. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15651. }
  15652. /* Check different in/out sizes. */
  15653. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  15654. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15655. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  15656. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15657. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  15658. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15659. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15660. XMEMCPY(out2, out, i - 1);
  15661. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  15662. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15663. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15664. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  15665. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15666. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15667. }
  15668. /* Force the counter to roll over in first byte. */
  15669. {
  15670. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15671. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15672. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15673. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15674. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15675. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15676. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15677. }
  15678. wc_Sm4Free(&sm4);
  15679. res = EXPECT_RESULT();
  15680. #endif
  15681. return res;
  15682. } /* END test_wc_Sm4Gcm */
  15683. /*
  15684. * Testing stream SM4-CCM API.
  15685. */
  15686. static int test_wc_Sm4Ccm(void)
  15687. {
  15688. int res = TEST_SKIPPED;
  15689. #ifdef WOLFSSL_SM4_CCM
  15690. EXPECT_DECLS;
  15691. wc_Sm4 sm4;
  15692. unsigned char key[SM4_KEY_SIZE];
  15693. unsigned char nonce[CCM_NONCE_MAX_SZ];
  15694. unsigned char in[SM4_BLOCK_SIZE * 2];
  15695. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15696. unsigned char out[SM4_BLOCK_SIZE * 2];
  15697. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15698. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15699. unsigned char tag[SM4_BLOCK_SIZE];
  15700. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15701. word32 i;
  15702. XMEMSET(key, 0, sizeof(key));
  15703. XMEMSET(nonce, 0, sizeof(nonce));
  15704. XMEMSET(in, 0, sizeof(in));
  15705. XMEMSET(in2, 0, sizeof(in2));
  15706. XMEMSET(aad, 0, sizeof(aad));
  15707. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15708. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15709. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15710. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15711. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15712. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15713. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  15714. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15715. 0), BAD_FUNC_ARG);
  15716. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15717. 0), BAD_FUNC_ARG);
  15718. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15719. 0), BAD_FUNC_ARG);
  15720. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15721. 0), BAD_FUNC_ARG);
  15722. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15723. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15724. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15725. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15726. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15727. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15728. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15729. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15730. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15731. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15732. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15733. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15734. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15735. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15736. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15737. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15738. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15739. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15740. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15741. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15742. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  15743. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15744. 0), BAD_FUNC_ARG);
  15745. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15746. 0), BAD_FUNC_ARG);
  15747. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15748. 0), BAD_FUNC_ARG);
  15749. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15750. 0), BAD_FUNC_ARG);
  15751. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15752. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15753. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15754. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15755. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15756. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15757. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15758. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15759. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15760. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15761. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15762. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15763. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15764. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15765. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15766. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15767. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15768. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15769. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15770. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15771. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15772. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15773. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15774. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15775. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15776. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15777. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15778. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15779. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15780. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15781. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15782. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15783. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15784. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15785. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15786. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15787. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15788. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15789. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15790. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15791. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15792. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15793. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15794. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15795. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15796. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15797. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15798. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15799. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  15800. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15801. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15802. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15803. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15804. }
  15805. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15806. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15807. SM4_CCM_AUTH_E);
  15808. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15809. for (i = 0; i < 4; i++) {
  15810. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15811. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15812. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15813. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15814. }
  15815. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  15816. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15817. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15818. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15819. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15820. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15821. }
  15822. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15823. * Even values in range 4..SM4_BLOCK_SIZE.
  15824. */
  15825. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15826. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15827. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15828. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15829. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15830. }
  15831. /* Check different in/out sizes. */
  15832. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15833. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15834. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15835. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15836. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15837. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15838. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15839. XMEMCPY(out2, out, i - 1);
  15840. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15841. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15842. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15843. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15844. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15845. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15846. }
  15847. /* Force the counter to roll over in first byte. */
  15848. {
  15849. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15850. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15851. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15852. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15853. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15854. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15855. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15856. }
  15857. wc_Sm4Free(&sm4);
  15858. res = EXPECT_RESULT();
  15859. #endif
  15860. return res;
  15861. } /* END test_wc_Sm4Ccm */
  15862. /*
  15863. * unit test for wc_Des3_SetIV()
  15864. */
  15865. static int test_wc_Des3_SetIV(void)
  15866. {
  15867. EXPECT_DECLS;
  15868. #ifndef NO_DES3
  15869. Des3 des;
  15870. const byte key[] = {
  15871. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15872. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15873. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15874. };
  15875. const byte iv[] = {
  15876. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15877. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15878. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15879. };
  15880. XMEMSET(&des, 0, sizeof(Des3));
  15881. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15882. /* DES_ENCRYPTION or DES_DECRYPTION */
  15883. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15884. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15885. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15886. /* Test explicitly wc_Des3_SetIV() */
  15887. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15888. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15889. #endif
  15890. wc_Des3Free(&des);
  15891. #endif
  15892. return EXPECT_RESULT();
  15893. } /* END test_wc_Des3_SetIV */
  15894. /*
  15895. * unit test for wc_Des3_SetKey()
  15896. */
  15897. static int test_wc_Des3_SetKey(void)
  15898. {
  15899. EXPECT_DECLS;
  15900. #ifndef NO_DES3
  15901. Des3 des;
  15902. const byte key[] = {
  15903. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15904. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15905. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15906. };
  15907. const byte iv[] = {
  15908. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15909. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15910. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15911. };
  15912. XMEMSET(&des, 0, sizeof(Des3));
  15913. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15914. /* DES_ENCRYPTION or DES_DECRYPTION */
  15915. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15916. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15917. /* Test bad args. */
  15918. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15919. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15920. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15921. /* Default case. Should return 0. */
  15922. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15923. wc_Des3Free(&des);
  15924. #endif
  15925. return EXPECT_RESULT();
  15926. } /* END test_wc_Des3_SetKey */
  15927. /*
  15928. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15929. */
  15930. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15931. {
  15932. EXPECT_DECLS;
  15933. #ifndef NO_DES3
  15934. Des3 des;
  15935. byte cipher[24];
  15936. byte plain[24];
  15937. const byte key[] = {
  15938. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15939. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15940. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15941. };
  15942. const byte iv[] = {
  15943. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15944. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15945. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15946. };
  15947. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15948. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15949. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15950. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15951. };
  15952. XMEMSET(&des, 0, sizeof(Des3));
  15953. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15954. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15955. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15956. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15957. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15958. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15959. /* Pass in bad args. */
  15960. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15961. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15962. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15963. BAD_FUNC_ARG);
  15964. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15965. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15966. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15967. wc_Des3Free(&des);
  15968. #endif
  15969. return EXPECT_RESULT();
  15970. } /* END wc_Des3_CbcEncrypt */
  15971. /*
  15972. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15973. */
  15974. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15975. {
  15976. EXPECT_DECLS;
  15977. #ifndef NO_DES3
  15978. word32 vectorSz, cipherSz;
  15979. byte cipher[24];
  15980. byte plain[24];
  15981. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15982. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15983. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15984. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15985. };
  15986. byte key[] = {
  15987. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15988. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15989. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15990. };
  15991. byte iv[] = {
  15992. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15993. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15994. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15995. };
  15996. vectorSz = sizeof(byte) * 24;
  15997. cipherSz = sizeof(byte) * 24;
  15998. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15999. 0);
  16000. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  16001. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  16002. /* pass in bad args. */
  16003. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  16004. BAD_FUNC_ARG);
  16005. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  16006. BAD_FUNC_ARG);
  16007. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  16008. BAD_FUNC_ARG);
  16009. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  16010. 0);
  16011. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  16012. BAD_FUNC_ARG);
  16013. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  16014. BAD_FUNC_ARG);
  16015. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  16016. BAD_FUNC_ARG);
  16017. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  16018. 0);
  16019. #endif
  16020. return EXPECT_RESULT();
  16021. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  16022. /*
  16023. * Unit test for wc_Des3_EcbEncrypt
  16024. */
  16025. static int test_wc_Des3_EcbEncrypt(void)
  16026. {
  16027. EXPECT_DECLS;
  16028. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  16029. Des3 des;
  16030. byte cipher[24];
  16031. word32 cipherSz = sizeof(cipher);
  16032. const byte key[] = {
  16033. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  16034. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  16035. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  16036. };
  16037. const byte iv[] = {
  16038. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  16039. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  16040. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  16041. };
  16042. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  16043. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16044. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16045. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16046. };
  16047. XMEMSET(&des, 0, sizeof(Des3));
  16048. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  16049. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  16050. /* Bad Cases */
  16051. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  16052. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  16053. BAD_FUNC_ARG);
  16054. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  16055. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  16056. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  16057. /* Good Cases */
  16058. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  16059. wc_Des3Free(&des);
  16060. #endif
  16061. return EXPECT_RESULT();
  16062. } /* END test_wc_Des3_EcbEncrypt */
  16063. /*
  16064. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  16065. */
  16066. static int test_wc_Chacha_SetKey(void)
  16067. {
  16068. EXPECT_DECLS;
  16069. #ifdef HAVE_CHACHA
  16070. ChaCha ctx;
  16071. const byte key[] = {
  16072. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  16073. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  16074. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  16075. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  16076. };
  16077. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  16078. byte cipher[128];
  16079. XMEMSET(cipher, 0, sizeof(cipher));
  16080. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  16081. /* Test bad args. */
  16082. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  16083. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  16084. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  16085. /* Test bad args. */
  16086. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  16087. #endif
  16088. return EXPECT_RESULT();
  16089. } /* END test_wc_Chacha_SetKey */
  16090. /*
  16091. * unit test for wc_Poly1305SetKey()
  16092. */
  16093. static int test_wc_Poly1305SetKey(void)
  16094. {
  16095. EXPECT_DECLS;
  16096. #ifdef HAVE_POLY1305
  16097. Poly1305 ctx;
  16098. const byte key[] =
  16099. {
  16100. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  16101. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  16102. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  16103. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  16104. };
  16105. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  16106. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  16107. /* Test bad args. */
  16108. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  16109. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  16110. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  16111. #endif
  16112. return EXPECT_RESULT();
  16113. } /* END test_wc_Poly1305_SetKey() */
  16114. /*
  16115. * Testing wc_Chacha_Process()
  16116. */
  16117. static int test_wc_Chacha_Process(void)
  16118. {
  16119. EXPECT_DECLS;
  16120. #ifdef HAVE_CHACHA
  16121. ChaCha enc, dec;
  16122. byte cipher[128];
  16123. byte plain[128];
  16124. const byte key[] =
  16125. {
  16126. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  16127. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  16128. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  16129. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  16130. };
  16131. const char* input = "Everybody gets Friday off.";
  16132. word32 keySz = sizeof(key)/sizeof(byte);
  16133. unsigned long int inlen = XSTRLEN(input);
  16134. /* Initialize stack variables. */
  16135. XMEMSET(cipher, 0, 128);
  16136. XMEMSET(plain, 0, 128);
  16137. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  16138. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  16139. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  16140. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  16141. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  16142. 0);
  16143. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  16144. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  16145. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  16146. /* test checking and using leftovers, currently just in C code */
  16147. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  16148. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  16149. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  16150. (word32)inlen - 2), 0);
  16151. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  16152. (byte*)input + (inlen - 2), 2), 0);
  16153. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  16154. (word32)inlen - 2), 0);
  16155. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  16156. (byte*)input + (inlen - 2), 2), 0);
  16157. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  16158. /* check edge cases with counter increment */
  16159. {
  16160. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  16161. const byte expected[] = {
  16162. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  16163. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  16164. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  16165. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  16166. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  16167. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  16168. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  16169. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  16170. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  16171. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  16172. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  16173. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  16174. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  16175. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  16176. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  16177. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  16178. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  16179. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  16180. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  16181. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  16182. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  16183. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  16184. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  16185. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  16186. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  16187. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  16188. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  16189. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  16190. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  16191. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  16192. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  16193. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  16194. };
  16195. const byte iv2[] = {
  16196. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  16197. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  16198. };
  16199. byte input2[256];
  16200. int i;
  16201. for (i = 0; i < 256; i++)
  16202. input2[i] = i;
  16203. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  16204. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  16205. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  16206. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  16207. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  16208. /* partial */
  16209. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  16210. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  16211. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  16212. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  16213. }
  16214. #endif
  16215. /* Test bad args. */
  16216. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  16217. BAD_FUNC_ARG);
  16218. #endif
  16219. return EXPECT_RESULT();
  16220. } /* END test_wc_Chacha_Process */
  16221. /*
  16222. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  16223. */
  16224. static int test_wc_ChaCha20Poly1305_aead(void)
  16225. {
  16226. EXPECT_DECLS;
  16227. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  16228. const byte key[] = {
  16229. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  16230. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  16231. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  16232. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  16233. };
  16234. const byte plaintext[] = {
  16235. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  16236. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  16237. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  16238. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  16239. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  16240. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  16241. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  16242. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  16243. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  16244. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  16245. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  16246. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  16247. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  16248. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  16249. 0x74, 0x2e
  16250. };
  16251. const byte iv[] = {
  16252. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  16253. 0x44, 0x45, 0x46, 0x47
  16254. };
  16255. const byte aad[] = { /* additional data */
  16256. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  16257. 0xc4, 0xc5, 0xc6, 0xc7
  16258. };
  16259. const byte cipher[] = { /* expected output from operation */
  16260. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  16261. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  16262. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  16263. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  16264. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  16265. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  16266. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  16267. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  16268. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  16269. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  16270. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  16271. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  16272. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  16273. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  16274. 0x61, 0x16
  16275. };
  16276. const byte authTag[] = { /* expected output from operation */
  16277. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  16278. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  16279. };
  16280. byte generatedCiphertext[272];
  16281. byte generatedPlaintext[272];
  16282. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  16283. /* Initialize stack variables. */
  16284. XMEMSET(generatedCiphertext, 0, 272);
  16285. XMEMSET(generatedPlaintext, 0, 272);
  16286. /* Test Encrypt */
  16287. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  16288. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  16289. 0);
  16290. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  16291. sizeof(cipher)/sizeof(byte)), 0);
  16292. /* Test bad args. */
  16293. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  16294. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  16295. BAD_FUNC_ARG);
  16296. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  16297. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  16298. BAD_FUNC_ARG);
  16299. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  16300. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  16301. BAD_FUNC_ARG);
  16302. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  16303. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  16304. BAD_FUNC_ARG);
  16305. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  16306. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  16307. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  16308. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  16309. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  16310. sizeof(cipher), authTag, generatedPlaintext), 0);
  16311. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  16312. sizeof(plaintext)/sizeof(byte)), 0);
  16313. /* Test bad args. */
  16314. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  16315. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  16316. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  16317. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  16318. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  16319. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  16320. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  16321. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  16322. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  16323. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  16324. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  16325. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  16326. #endif
  16327. return EXPECT_RESULT();
  16328. } /* END test_wc_ChaCha20Poly1305_aead */
  16329. /*
  16330. * Testing function for wc_Rc2SetKey().
  16331. */
  16332. static int test_wc_Rc2SetKey(void)
  16333. {
  16334. EXPECT_DECLS;
  16335. #ifdef WC_RC2
  16336. Rc2 rc2;
  16337. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  16338. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  16339. /* valid key and IV */
  16340. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  16341. iv, 40), 0);
  16342. /* valid key, no IV */
  16343. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  16344. NULL, 40), 0);
  16345. /* bad arguments */
  16346. /* null Rc2 struct */
  16347. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  16348. iv, 40), BAD_FUNC_ARG);
  16349. /* null key */
  16350. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  16351. iv, 40), BAD_FUNC_ARG);
  16352. /* key size == 0 */
  16353. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  16354. /* key size > 128 */
  16355. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  16356. /* effective bits == 0 */
  16357. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  16358. iv, 0), WC_KEY_SIZE_E);
  16359. /* effective bits > 1024 */
  16360. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  16361. iv, 1025), WC_KEY_SIZE_E);
  16362. #endif
  16363. return EXPECT_RESULT();
  16364. } /* END test_wc_Rc2SetKey */
  16365. /*
  16366. * Testing function for wc_Rc2SetIV().
  16367. */
  16368. static int test_wc_Rc2SetIV(void)
  16369. {
  16370. EXPECT_DECLS;
  16371. #ifdef WC_RC2
  16372. Rc2 rc2;
  16373. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  16374. /* valid IV */
  16375. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16376. /* valid NULL IV */
  16377. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  16378. /* bad arguments */
  16379. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  16380. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  16381. #endif
  16382. return EXPECT_RESULT();
  16383. } /* END test_wc_Rc2SetIV */
  16384. /*
  16385. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  16386. */
  16387. static int test_wc_Rc2EcbEncryptDecrypt(void)
  16388. {
  16389. EXPECT_DECLS;
  16390. #ifdef WC_RC2
  16391. Rc2 rc2;
  16392. int effectiveKeyBits = 63;
  16393. byte cipher[RC2_BLOCK_SIZE];
  16394. byte plain[RC2_BLOCK_SIZE];
  16395. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16396. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16397. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  16398. XMEMSET(cipher, 0, sizeof(cipher));
  16399. XMEMSET(plain, 0, sizeof(plain));
  16400. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16401. NULL, effectiveKeyBits), 0);
  16402. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  16403. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  16404. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  16405. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  16406. /* Rc2EcbEncrypt bad arguments */
  16407. /* null Rc2 struct */
  16408. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  16409. BAD_FUNC_ARG);
  16410. /* null out buffer */
  16411. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  16412. BAD_FUNC_ARG);
  16413. /* null input buffer */
  16414. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  16415. BAD_FUNC_ARG);
  16416. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16417. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  16418. /* Rc2EcbDecrypt bad arguments */
  16419. /* null Rc2 struct */
  16420. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  16421. BAD_FUNC_ARG);
  16422. /* null out buffer */
  16423. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  16424. BAD_FUNC_ARG);
  16425. /* null input buffer */
  16426. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  16427. BAD_FUNC_ARG);
  16428. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16429. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  16430. #endif
  16431. return EXPECT_RESULT();
  16432. } /* END test_wc_Rc2EcbEncryptDecrypt */
  16433. /*
  16434. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  16435. */
  16436. static int test_wc_Rc2CbcEncryptDecrypt(void)
  16437. {
  16438. EXPECT_DECLS;
  16439. #ifdef WC_RC2
  16440. Rc2 rc2;
  16441. int effectiveKeyBits = 63;
  16442. byte cipher[RC2_BLOCK_SIZE*2];
  16443. byte plain[RC2_BLOCK_SIZE*2];
  16444. /* vector taken from test.c */
  16445. byte key[] = {
  16446. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16447. };
  16448. byte iv[] = {
  16449. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16450. };
  16451. byte input[] = {
  16452. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16453. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16454. };
  16455. byte output[] = {
  16456. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  16457. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  16458. };
  16459. XMEMSET(cipher, 0, sizeof(cipher));
  16460. XMEMSET(plain, 0, sizeof(plain));
  16461. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16462. iv, effectiveKeyBits), 0);
  16463. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  16464. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  16465. /* reset IV for decrypt */
  16466. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16467. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  16468. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  16469. /* Rc2CbcEncrypt bad arguments */
  16470. /* null Rc2 struct */
  16471. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  16472. BAD_FUNC_ARG);
  16473. /* null out buffer */
  16474. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  16475. BAD_FUNC_ARG);
  16476. /* null input buffer */
  16477. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  16478. BAD_FUNC_ARG);
  16479. /* Rc2CbcDecrypt bad arguments */
  16480. /* in size is 0 */
  16481. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  16482. /* null Rc2 struct */
  16483. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  16484. BAD_FUNC_ARG);
  16485. /* null out buffer */
  16486. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  16487. BAD_FUNC_ARG);
  16488. /* null input buffer */
  16489. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  16490. BAD_FUNC_ARG);
  16491. #endif
  16492. return EXPECT_RESULT();
  16493. } /* END test_wc_Rc2CbcEncryptDecrypt */
  16494. /*
  16495. * Testing function for wc_AesSetIV
  16496. */
  16497. static int test_wc_AesSetIV(void)
  16498. {
  16499. int res = TEST_SKIPPED;
  16500. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16501. Aes aes;
  16502. int ret = 0;
  16503. byte key16[] =
  16504. {
  16505. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16506. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16507. };
  16508. byte iv1[] = "1234567890abcdef";
  16509. byte iv2[] = "0987654321fedcba";
  16510. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16511. if (ret != 0)
  16512. return ret;
  16513. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16514. iv1, AES_ENCRYPTION);
  16515. if (ret == 0) {
  16516. ret = wc_AesSetIV(&aes, iv2);
  16517. }
  16518. /* Test bad args. */
  16519. if (ret == 0) {
  16520. ret = wc_AesSetIV(NULL, iv1);
  16521. if (ret == BAD_FUNC_ARG) {
  16522. /* NULL iv should return 0. */
  16523. ret = wc_AesSetIV(&aes, NULL);
  16524. }
  16525. else {
  16526. ret = WOLFSSL_FATAL_ERROR;
  16527. }
  16528. }
  16529. wc_AesFree(&aes);
  16530. res = TEST_RES_CHECK(ret == 0);
  16531. #endif
  16532. return res;
  16533. } /* test_wc_AesSetIV */
  16534. /*
  16535. * Testing function for wc_AesSetKey().
  16536. */
  16537. static int test_wc_AesSetKey(void)
  16538. {
  16539. EXPECT_DECLS;
  16540. #ifndef NO_AES
  16541. Aes aes;
  16542. byte key16[] = {
  16543. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16544. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16545. };
  16546. #ifdef WOLFSSL_AES_192
  16547. byte key24[] = {
  16548. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16549. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16550. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16551. };
  16552. #endif
  16553. #ifdef WOLFSSL_AES_256
  16554. byte key32[] = {
  16555. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16556. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16557. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16558. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16559. };
  16560. #endif
  16561. byte badKey16[] = {
  16562. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16563. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16564. };
  16565. byte iv[] = "1234567890abcdef";
  16566. XMEMSET(&aes, 0, sizeof(Aes));
  16567. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16568. #ifdef WOLFSSL_AES_128
  16569. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  16570. iv, AES_ENCRYPTION), 0);
  16571. #endif
  16572. #ifdef WOLFSSL_AES_192
  16573. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  16574. iv, AES_ENCRYPTION), 0);
  16575. #endif
  16576. #ifdef WOLFSSL_AES_256
  16577. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  16578. iv, AES_ENCRYPTION), 0);
  16579. #endif
  16580. /* Pass in bad args. */
  16581. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  16582. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  16583. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  16584. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  16585. BAD_FUNC_ARG);
  16586. wc_AesFree(&aes);
  16587. #endif
  16588. return EXPECT_RESULT();
  16589. } /* END test_wc_AesSetKey */
  16590. /*
  16591. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16592. * and wc_AesCbcDecryptWithKey()
  16593. */
  16594. static int test_wc_AesCbcEncryptDecrypt(void)
  16595. {
  16596. EXPECT_DECLS;
  16597. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16598. defined(WOLFSSL_AES_256)
  16599. Aes aes;
  16600. byte key32[] = {
  16601. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16602. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16603. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16604. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16605. };
  16606. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  16607. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  16608. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  16609. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  16610. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  16611. };
  16612. byte iv[] = "1234567890abcdef";
  16613. byte enc[sizeof(vector)];
  16614. byte dec[sizeof(vector)];
  16615. byte dec2[sizeof(vector)];
  16616. /* Init stack variables. */
  16617. XMEMSET(&aes, 0, sizeof(Aes));
  16618. XMEMSET(enc, 0, sizeof(enc));
  16619. XMEMSET(dec, 0, sizeof(vector));
  16620. XMEMSET(dec2, 0, sizeof(vector));
  16621. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16622. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16623. AES_ENCRYPTION), 0);
  16624. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  16625. /* Re init for decrypt and set flag. */
  16626. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16627. AES_DECRYPTION), 0);
  16628. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  16629. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16630. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  16631. sizeof(key32)/sizeof(byte), iv), 0);
  16632. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  16633. /* Pass in bad args */
  16634. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  16635. BAD_FUNC_ARG);
  16636. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  16637. BAD_FUNC_ARG);
  16638. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  16639. BAD_FUNC_ARG);
  16640. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16641. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  16642. BAD_LENGTH_E);
  16643. #endif
  16644. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16645. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16646. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16647. "mode (v2), skip test");
  16648. #else
  16649. /* Test passing in size of 0 */
  16650. XMEMSET(enc, 0, sizeof(enc));
  16651. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  16652. /* Check enc was not modified */
  16653. {
  16654. int i;
  16655. for (i = 0; i < (int)sizeof(enc); i++)
  16656. ExpectIntEQ(enc[i], 0);
  16657. }
  16658. #endif
  16659. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  16660. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  16661. BAD_FUNC_ARG);
  16662. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  16663. BAD_FUNC_ARG);
  16664. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16665. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16666. BAD_LENGTH_E);
  16667. #else
  16668. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16669. BAD_FUNC_ARG);
  16670. #endif
  16671. /* Test passing in size of 0 */
  16672. XMEMSET(dec, 0, sizeof(dec));
  16673. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  16674. /* Check dec was not modified */
  16675. {
  16676. int i;
  16677. for (i = 0; i < (int)sizeof(dec); i++)
  16678. ExpectIntEQ(dec[i], 0);
  16679. }
  16680. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16681. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16682. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16683. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16684. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16685. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16686. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16687. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  16688. wc_AesFree(&aes);
  16689. #endif
  16690. return EXPECT_RESULT();
  16691. } /* END test_wc_AesCbcEncryptDecrypt */
  16692. /*
  16693. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16694. */
  16695. static int test_wc_AesCtrEncryptDecrypt(void)
  16696. {
  16697. EXPECT_DECLS;
  16698. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16699. Aes aesEnc;
  16700. Aes aesDec;
  16701. byte key32[] = {
  16702. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16703. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16704. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16705. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16706. };
  16707. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16708. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16709. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16710. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16711. };
  16712. byte iv[] = "1234567890abcdef";
  16713. byte enc[AES_BLOCK_SIZE * 2];
  16714. byte dec[AES_BLOCK_SIZE * 2];
  16715. /* Init stack variables. */
  16716. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16717. XMEMSET(&aesDec, 0, sizeof(Aes));
  16718. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16719. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16720. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16721. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16722. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  16723. AES_ENCRYPTION), 0);
  16724. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16725. sizeof(vector)/sizeof(byte)), 0);
  16726. /* Decrypt with wc_AesCtrEncrypt() */
  16727. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  16728. AES_ENCRYPTION), 0);
  16729. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  16730. 0);
  16731. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16732. /* Test bad args. */
  16733. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  16734. BAD_FUNC_ARG);
  16735. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  16736. BAD_FUNC_ARG);
  16737. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  16738. BAD_FUNC_ARG);
  16739. wc_AesFree(&aesEnc);
  16740. wc_AesFree(&aesDec);
  16741. #endif
  16742. return EXPECT_RESULT();
  16743. } /* END test_wc_AesCtrEncryptDecrypt */
  16744. /*
  16745. * test function for wc_AesGcmSetKey()
  16746. */
  16747. static int test_wc_AesGcmSetKey(void)
  16748. {
  16749. EXPECT_DECLS;
  16750. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16751. Aes aes;
  16752. #ifdef WOLFSSL_AES_128
  16753. byte key16[] = {
  16754. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16755. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16756. };
  16757. #endif
  16758. #ifdef WOLFSSL_AES_192
  16759. byte key24[] = {
  16760. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16761. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16762. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16763. };
  16764. #endif
  16765. #ifdef WOLFSSL_AES_256
  16766. byte key32[] = {
  16767. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16768. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16769. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16770. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16771. };
  16772. #endif
  16773. byte badKey16[] = {
  16774. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16775. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16776. };
  16777. byte badKey24[] = {
  16778. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16779. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16780. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16781. };
  16782. byte badKey32[] = {
  16783. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16784. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16785. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16786. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16787. };
  16788. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16789. #ifdef WOLFSSL_AES_128
  16790. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  16791. #endif
  16792. #ifdef WOLFSSL_AES_192
  16793. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  16794. #endif
  16795. #ifdef WOLFSSL_AES_256
  16796. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16797. #endif
  16798. /* Pass in bad args. */
  16799. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  16800. BAD_FUNC_ARG);
  16801. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  16802. BAD_FUNC_ARG);
  16803. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  16804. BAD_FUNC_ARG);
  16805. wc_AesFree(&aes);
  16806. #endif
  16807. return EXPECT_RESULT();
  16808. } /* END test_wc_AesGcmSetKey */
  16809. /*
  16810. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16811. */
  16812. static int test_wc_AesGcmEncryptDecrypt(void)
  16813. {
  16814. EXPECT_DECLS;
  16815. /* WOLFSSL_AFALG requires 12 byte IV */
  16816. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16817. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16818. Aes aes;
  16819. byte key32[] = {
  16820. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16821. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16822. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16823. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16824. };
  16825. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16826. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16827. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16828. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16829. };
  16830. const byte a[] = {
  16831. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16832. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16833. 0xab, 0xad, 0xda, 0xd2
  16834. };
  16835. byte iv[] = "1234567890a";
  16836. byte longIV[] = "1234567890abcdefghij";
  16837. byte enc[sizeof(vector)];
  16838. byte resultT[AES_BLOCK_SIZE];
  16839. byte dec[sizeof(vector)];
  16840. /* Init stack variables. */
  16841. XMEMSET(&aes, 0, sizeof(Aes));
  16842. XMEMSET(enc, 0, sizeof(vector));
  16843. XMEMSET(dec, 0, sizeof(vector));
  16844. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16845. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16846. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16847. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16848. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16849. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16850. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16851. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16852. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16853. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16854. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16855. BAD_FUNC_ARG);
  16856. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16857. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16858. BAD_FUNC_ARG);
  16859. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16860. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16861. BAD_FUNC_ARG);
  16862. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16863. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16864. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16865. /* FIPS does not check the lower bound of ivSz */
  16866. #else
  16867. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16868. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16869. #endif
  16870. /* This case is now considered good. Long IVs are now allowed.
  16871. * Except for the original FIPS release, it still has an upper
  16872. * bound on the IV length. */
  16873. #if (!defined(HAVE_FIPS) || \
  16874. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16875. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16876. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16877. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16878. 0);
  16879. #else
  16880. (void)longIV;
  16881. #endif /* Old FIPS */
  16882. /* END wc_AesGcmEncrypt */
  16883. #ifdef HAVE_AES_DECRYPT
  16884. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16885. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16886. BAD_FUNC_ARG);
  16887. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16888. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16889. BAD_FUNC_ARG);
  16890. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16891. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16892. BAD_FUNC_ARG);
  16893. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16894. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16895. BAD_FUNC_ARG);
  16896. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16897. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16898. BAD_FUNC_ARG);
  16899. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16900. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16901. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16902. AES_GCM_AUTH_E);
  16903. #else
  16904. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16905. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16906. BAD_FUNC_ARG);
  16907. #endif
  16908. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16909. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16910. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16911. /* FIPS does not check the lower bound of ivSz */
  16912. #else
  16913. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16914. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16915. #endif
  16916. #endif /* HAVE_AES_DECRYPT */
  16917. wc_AesFree(&aes);
  16918. #endif
  16919. return EXPECT_RESULT();
  16920. } /* END test_wc_AesGcmEncryptDecrypt */
  16921. /*
  16922. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16923. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16924. */
  16925. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16926. {
  16927. EXPECT_DECLS;
  16928. #if (!defined(HAVE_FIPS) || \
  16929. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16930. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16931. const byte key[] = {
  16932. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16933. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16934. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16935. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16936. };
  16937. const byte in[] = {
  16938. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16939. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16940. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16941. };
  16942. const byte aad[] = {
  16943. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16944. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16945. 0xab, 0xad, 0xda, 0xd2
  16946. };
  16947. Aes aesEnc;
  16948. Aes aesDec;
  16949. byte iv[] = "1234567890abcdefghij";
  16950. byte out[sizeof(in)];
  16951. byte plain[sizeof(in)];
  16952. byte tag[AES_BLOCK_SIZE];
  16953. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16954. XMEMSET(&aesDec, 0, sizeof(Aes));
  16955. XMEMSET(out, 0, sizeof(out));
  16956. XMEMSET(plain, 0, sizeof(plain));
  16957. XMEMSET(tag, 0, sizeof(tag));
  16958. /* Perform one-shot encryption using long IV */
  16959. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16960. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16961. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16962. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16963. /* Perform streaming decryption using long IV */
  16964. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16965. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16966. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16967. sizeof(aad)), 0);
  16968. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16969. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16970. /* Free resources */
  16971. wc_AesFree(&aesEnc);
  16972. wc_AesFree(&aesDec);
  16973. #endif
  16974. return EXPECT_RESULT();
  16975. } /* END wc_AesGcmMixedEncDecLongIV */
  16976. /*
  16977. * unit test for wc_GmacSetKey()
  16978. */
  16979. static int test_wc_GmacSetKey(void)
  16980. {
  16981. EXPECT_DECLS;
  16982. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16983. Gmac gmac;
  16984. byte key16[] = {
  16985. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16986. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16987. };
  16988. #ifdef WOLFSSL_AES_192
  16989. byte key24[] = {
  16990. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16991. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16992. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16993. };
  16994. #endif
  16995. #ifdef WOLFSSL_AES_256
  16996. byte key32[] = {
  16997. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16998. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16999. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17000. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17001. };
  17002. #endif
  17003. byte badKey16[] = {
  17004. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17005. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  17006. };
  17007. byte badKey24[] = {
  17008. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  17009. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17010. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17011. };
  17012. byte badKey32[] = {
  17013. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17014. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  17015. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17016. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17017. };
  17018. XMEMSET(&gmac, 0, sizeof(Gmac));
  17019. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  17020. #ifdef WOLFSSL_AES_128
  17021. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  17022. #endif
  17023. #ifdef WOLFSSL_AES_192
  17024. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  17025. #endif
  17026. #ifdef WOLFSSL_AES_256
  17027. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  17028. #endif
  17029. /* Pass in bad args. */
  17030. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  17031. BAD_FUNC_ARG);
  17032. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  17033. BAD_FUNC_ARG);
  17034. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  17035. BAD_FUNC_ARG);
  17036. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  17037. BAD_FUNC_ARG);
  17038. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  17039. BAD_FUNC_ARG);
  17040. wc_AesFree(&gmac.aes);
  17041. #endif
  17042. return EXPECT_RESULT();
  17043. } /* END test_wc_GmacSetKey */
  17044. /*
  17045. * unit test for wc_GmacUpdate
  17046. */
  17047. static int test_wc_GmacUpdate(void)
  17048. {
  17049. EXPECT_DECLS;
  17050. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  17051. Gmac gmac;
  17052. #ifdef WOLFSSL_AES_128
  17053. const byte key16[] = {
  17054. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  17055. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  17056. };
  17057. #endif
  17058. #ifdef WOLFSSL_AES_192
  17059. byte key24[] = {
  17060. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  17061. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  17062. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  17063. };
  17064. #endif
  17065. #ifdef WOLFSSL_AES_256
  17066. byte key32[] = {
  17067. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  17068. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  17069. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  17070. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  17071. };
  17072. #endif
  17073. #ifdef WOLFSSL_AES_128
  17074. const byte authIn[] = {
  17075. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  17076. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  17077. };
  17078. #endif
  17079. #ifdef WOLFSSL_AES_192
  17080. const byte authIn2[] = {
  17081. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  17082. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  17083. };
  17084. #endif
  17085. const byte authIn3[] = {
  17086. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  17087. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  17088. };
  17089. #ifdef WOLFSSL_AES_128
  17090. const byte tag1[] = { /* Known. */
  17091. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  17092. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  17093. };
  17094. #endif
  17095. #ifdef WOLFSSL_AES_192
  17096. const byte tag2[] = { /* Known */
  17097. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  17098. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  17099. };
  17100. #endif
  17101. const byte tag3[] = { /* Known */
  17102. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  17103. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  17104. };
  17105. #ifdef WOLFSSL_AES_128
  17106. const byte iv[] = {
  17107. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  17108. 0xe2, 0x8c, 0x8f, 0x16
  17109. };
  17110. #endif
  17111. #ifdef WOLFSSL_AES_192
  17112. const byte iv2[] = {
  17113. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  17114. 0x7e, 0x1a, 0x6f, 0xbc
  17115. };
  17116. #endif
  17117. const byte iv3[] = {
  17118. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  17119. 0xc3, 0xfb, 0x6c, 0x8a
  17120. };
  17121. byte tagOut[16];
  17122. byte tagOut2[24];
  17123. byte tagOut3[32];
  17124. /* Init stack variables. */
  17125. XMEMSET(&gmac, 0, sizeof(Gmac));
  17126. XMEMSET(tagOut, 0, sizeof(tagOut));
  17127. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  17128. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  17129. #ifdef WOLFSSL_AES_128
  17130. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  17131. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  17132. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  17133. tagOut, sizeof(tag1)), 0);
  17134. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  17135. wc_AesFree(&gmac.aes);
  17136. #endif
  17137. #ifdef WOLFSSL_AES_192
  17138. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  17139. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  17140. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  17141. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  17142. tagOut2, sizeof(tag2)), 0);
  17143. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  17144. wc_AesFree(&gmac.aes);
  17145. #endif
  17146. #ifdef WOLFSSL_AES_256
  17147. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  17148. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  17149. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  17150. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  17151. tagOut3, sizeof(tag3)), 0);
  17152. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  17153. wc_AesFree(&gmac.aes);
  17154. #endif
  17155. /* Pass bad args. */
  17156. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  17157. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  17158. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  17159. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  17160. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  17161. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  17162. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  17163. wc_AesFree(&gmac.aes);
  17164. #endif
  17165. return EXPECT_RESULT();
  17166. } /* END test_wc_GmacUpdate */
  17167. /*
  17168. * testing wc_CamelliaSetKey
  17169. */
  17170. static int test_wc_CamelliaSetKey(void)
  17171. {
  17172. EXPECT_DECLS;
  17173. #ifdef HAVE_CAMELLIA
  17174. Camellia camellia;
  17175. /*128-bit key*/
  17176. static const byte key16[] = {
  17177. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  17178. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  17179. };
  17180. /* 192-bit key */
  17181. static const byte key24[] = {
  17182. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  17183. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  17184. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  17185. };
  17186. /* 256-bit key */
  17187. static const byte key32[] = {
  17188. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  17189. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  17190. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  17191. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  17192. };
  17193. static const byte iv[] = {
  17194. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17195. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  17196. };
  17197. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  17198. 0);
  17199. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  17200. NULL), 0);
  17201. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  17202. 0);
  17203. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  17204. NULL), 0);
  17205. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  17206. 0);
  17207. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  17208. NULL), 0);
  17209. /* Bad args. */
  17210. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  17211. BAD_FUNC_ARG);
  17212. #endif
  17213. return EXPECT_RESULT();
  17214. } /* END test_wc_CammeliaSetKey */
  17215. /*
  17216. * Testing wc_CamelliaSetIV()
  17217. */
  17218. static int test_wc_CamelliaSetIV(void)
  17219. {
  17220. EXPECT_DECLS;
  17221. #ifdef HAVE_CAMELLIA
  17222. Camellia camellia;
  17223. static const byte iv[] = {
  17224. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17225. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  17226. };
  17227. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  17228. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  17229. /* Bad args. */
  17230. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  17231. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  17232. #endif
  17233. return EXPECT_RESULT();
  17234. } /* END test_wc_CamelliaSetIV*/
  17235. /*
  17236. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  17237. */
  17238. static int test_wc_CamelliaEncryptDecryptDirect(void)
  17239. {
  17240. EXPECT_DECLS;
  17241. #ifdef HAVE_CAMELLIA
  17242. Camellia camellia;
  17243. static const byte key24[] = {
  17244. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  17245. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  17246. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  17247. };
  17248. static const byte iv[] = {
  17249. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17250. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  17251. };
  17252. static const byte plainT[] = {
  17253. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  17254. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  17255. };
  17256. byte enc[sizeof(plainT)];
  17257. byte dec[sizeof(enc)];
  17258. /* Init stack variables.*/
  17259. XMEMSET(enc, 0, 16);
  17260. XMEMSET(enc, 0, 16);
  17261. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  17262. 0);
  17263. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  17264. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  17265. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  17266. /* Pass bad args. */
  17267. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  17268. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  17269. BAD_FUNC_ARG);
  17270. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  17271. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  17272. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  17273. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  17274. #endif
  17275. return EXPECT_RESULT();
  17276. } /* END test-wc_CamelliaEncryptDecryptDirect */
  17277. /*
  17278. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  17279. */
  17280. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  17281. {
  17282. EXPECT_DECLS;
  17283. #ifdef HAVE_CAMELLIA
  17284. Camellia camellia;
  17285. static const byte key24[] = {
  17286. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  17287. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  17288. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  17289. };
  17290. static const byte plainT[] = {
  17291. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  17292. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  17293. };
  17294. byte enc[CAMELLIA_BLOCK_SIZE];
  17295. byte dec[CAMELLIA_BLOCK_SIZE];
  17296. /* Init stack variables. */
  17297. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  17298. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  17299. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  17300. NULL), 0);
  17301. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  17302. CAMELLIA_BLOCK_SIZE), 0);
  17303. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  17304. NULL), 0);
  17305. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  17306. 0);
  17307. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  17308. /* Pass in bad args. */
  17309. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  17310. BAD_FUNC_ARG);
  17311. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  17312. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  17313. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  17314. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  17315. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  17316. BAD_FUNC_ARG);
  17317. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  17318. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  17319. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  17320. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  17321. #endif
  17322. return EXPECT_RESULT();
  17323. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  17324. /*
  17325. * Testing wc_Arc4SetKey()
  17326. */
  17327. static int test_wc_Arc4SetKey(void)
  17328. {
  17329. EXPECT_DECLS;
  17330. #ifndef NO_RC4
  17331. Arc4 arc;
  17332. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  17333. int keyLen = 8;
  17334. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, (word32)keyLen), 0);
  17335. /* Test bad args. */
  17336. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, (word32)keyLen), BAD_FUNC_ARG);
  17337. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , (word32)keyLen), BAD_FUNC_ARG);
  17338. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  17339. #endif
  17340. return EXPECT_RESULT();
  17341. } /* END test_wc_Arc4SetKey */
  17342. /*
  17343. * Testing wc_Arc4Process for ENC/DEC.
  17344. */
  17345. static int test_wc_Arc4Process(void)
  17346. {
  17347. EXPECT_DECLS;
  17348. #ifndef NO_RC4
  17349. Arc4 enc;
  17350. Arc4 dec;
  17351. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  17352. int keyLen = 8;
  17353. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  17354. byte cipher[8];
  17355. byte plain[8];
  17356. /* Init stack variables */
  17357. XMEMSET(&enc, 0, sizeof(Arc4));
  17358. XMEMSET(&dec, 0, sizeof(Arc4));
  17359. XMEMSET(cipher, 0, sizeof(cipher));
  17360. XMEMSET(plain, 0, sizeof(plain));
  17361. /* Use for async. */
  17362. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  17363. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  17364. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, (word32)keyLen), 0);
  17365. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, (word32)keyLen), 0);
  17366. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, (word32)keyLen), 0);
  17367. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, (word32)keyLen), 0);
  17368. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  17369. /* Bad args. */
  17370. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, (word32)keyLen), BAD_FUNC_ARG);
  17371. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, (word32)keyLen), BAD_FUNC_ARG);
  17372. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, (word32)keyLen), BAD_FUNC_ARG);
  17373. wc_Arc4Free(&enc);
  17374. wc_Arc4Free(&dec);
  17375. #endif
  17376. return EXPECT_RESULT();
  17377. } /* END test_wc_Arc4Process */
  17378. /*
  17379. * Testing wc_Init RsaKey()
  17380. */
  17381. static int test_wc_InitRsaKey(void)
  17382. {
  17383. EXPECT_DECLS;
  17384. #ifndef NO_RSA
  17385. RsaKey key;
  17386. XMEMSET(&key, 0, sizeof(RsaKey));
  17387. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17388. /* Test bad args. */
  17389. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  17390. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17391. #endif
  17392. return EXPECT_RESULT();
  17393. } /* END test_wc_InitRsaKey */
  17394. /*
  17395. * Testing wc_RsaPrivateKeyDecode()
  17396. */
  17397. static int test_wc_RsaPrivateKeyDecode(void)
  17398. {
  17399. EXPECT_DECLS;
  17400. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17401. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17402. RsaKey key;
  17403. byte* tmp = NULL;
  17404. word32 idx = 0;
  17405. int bytes = 0;
  17406. XMEMSET(&key, 0, sizeof(RsaKey));
  17407. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  17408. DYNAMIC_TYPE_TMP_BUFFER));
  17409. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17410. if (tmp != NULL) {
  17411. #ifdef USE_CERT_BUFFERS_1024
  17412. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17413. bytes = sizeof_client_key_der_1024;
  17414. #else
  17415. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17416. bytes = sizeof_client_key_der_2048;
  17417. #endif /* Use cert buffers. */
  17418. }
  17419. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  17420. /* Test bad args. */
  17421. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  17422. BAD_FUNC_ARG);
  17423. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  17424. BAD_FUNC_ARG);
  17425. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17426. BAD_FUNC_ARG);
  17427. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17428. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17429. #endif
  17430. return EXPECT_RESULT();
  17431. } /* END test_wc_RsaPrivateKeyDecode */
  17432. /*
  17433. * Testing wc_RsaPublicKeyDecode()
  17434. */
  17435. static int test_wc_RsaPublicKeyDecode(void)
  17436. {
  17437. EXPECT_DECLS;
  17438. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17439. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17440. RsaKey keyPub;
  17441. byte* tmp = NULL;
  17442. word32 idx = 0;
  17443. int bytes = 0;
  17444. word32 keySz = 0;
  17445. word32 tstKeySz = 0;
  17446. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17447. XFILE f = XBADFILE;
  17448. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17449. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17450. byte buf[4096];
  17451. #endif
  17452. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  17453. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17454. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  17455. if (tmp != NULL) {
  17456. #ifdef USE_CERT_BUFFERS_1024
  17457. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17458. bytes = sizeof_client_keypub_der_1024;
  17459. keySz = 1024;
  17460. #else
  17461. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17462. bytes = sizeof_client_keypub_der_2048;
  17463. keySz = 2048;
  17464. #endif
  17465. }
  17466. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  17467. /* Pass in bad args. */
  17468. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  17469. BAD_FUNC_ARG);
  17470. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  17471. BAD_FUNC_ARG);
  17472. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17473. BAD_FUNC_ARG);
  17474. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  17475. /* Test for getting modulus key size */
  17476. idx = 0;
  17477. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17478. &tstKeySz, NULL, NULL), 0);
  17479. ExpectIntEQ(tstKeySz, keySz/8);
  17480. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17481. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  17482. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17483. if (f != XBADFILE) {
  17484. XFCLOSE(f);
  17485. f = XBADFILE;
  17486. }
  17487. idx = 0;
  17488. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17489. NULL), 0);
  17490. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  17491. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17492. if (f != XBADFILE)
  17493. XFCLOSE(f);
  17494. idx = 0;
  17495. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17496. NULL), 0);
  17497. #endif
  17498. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17499. #endif
  17500. return EXPECT_RESULT();
  17501. } /* END test_wc_RsaPublicKeyDecode */
  17502. /*
  17503. * Testing wc_RsaPublicKeyDecodeRaw()
  17504. */
  17505. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17506. {
  17507. EXPECT_DECLS;
  17508. #if !defined(NO_RSA)
  17509. RsaKey key;
  17510. const byte n = 0x23;
  17511. const byte e = 0x03;
  17512. int nSz = sizeof(n);
  17513. int eSz = sizeof(e);
  17514. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17515. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  17516. /* Pass in bad args. */
  17517. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  17518. BAD_FUNC_ARG);
  17519. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  17520. BAD_FUNC_ARG);
  17521. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  17522. BAD_FUNC_ARG);
  17523. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17524. #endif
  17525. return EXPECT_RESULT();
  17526. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17527. /*
  17528. * Testing wc_RsaPrivateKeyDecodeRaw()
  17529. */
  17530. static int test_wc_RsaPrivateKeyDecodeRaw(void)
  17531. {
  17532. EXPECT_DECLS;
  17533. #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) \
  17534. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  17535. RsaKey key;
  17536. const byte n = 33;
  17537. const byte e = 3;
  17538. const byte d = 7;
  17539. const byte u = 2;
  17540. const byte p = 3;
  17541. const byte q = 11;
  17542. const byte dp = 1;
  17543. const byte dq = 7;
  17544. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17545. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17546. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17547. &p, sizeof(p), &q, sizeof(q), NULL, 0,
  17548. NULL, 0, &key), 0);
  17549. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17550. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17551. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17552. NULL, 0, &key), 0);
  17553. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17554. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17555. &p, sizeof(p), &q, sizeof(q), NULL, 0,
  17556. &dq, sizeof(dq), &key), 0);
  17557. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17558. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17559. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17560. &dq, sizeof(dq), &key), 0);
  17561. /* Pass in bad args. */
  17562. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(NULL, sizeof(n),
  17563. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17564. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17565. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17566. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, 0,
  17567. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17568. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17569. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17570. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17571. NULL, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17572. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17573. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17574. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17575. &e, 0, &d, sizeof(d), &u, sizeof(u),
  17576. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17577. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17578. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17579. &e, sizeof(e), NULL, sizeof(d), &u, sizeof(u),
  17580. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17581. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17582. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17583. &e, sizeof(e), &d, 0, &u, sizeof(u),
  17584. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17585. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17586. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17587. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17588. NULL, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17589. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17590. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17591. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17592. &p, 0, &q, sizeof(q), &dp, sizeof(dp),
  17593. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17594. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17595. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17596. &p, sizeof(p), NULL, sizeof(q), &dp, sizeof(dp),
  17597. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17598. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17599. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17600. &p, sizeof(p), &q, 0, &dp, sizeof(dp),
  17601. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17602. #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
  17603. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17604. &e, sizeof(e), &d, sizeof(d), &u, 0,
  17605. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17606. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17607. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17608. &e, sizeof(e), &d, sizeof(d), NULL, sizeof(u),
  17609. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17610. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17611. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17612. &e, sizeof(e), &d, sizeof(d), &u, 0,
  17613. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17614. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17615. #endif
  17616. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17617. #endif
  17618. return EXPECT_RESULT();
  17619. } /* END test_wc_RsaPrivateKeyDecodeRaw */
  17620. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17621. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17622. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17623. * trying until it gets a probable prime. */
  17624. #ifdef HAVE_FIPS
  17625. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17626. {
  17627. int ret;
  17628. for (;;) {
  17629. ret = wc_MakeRsaKey(key, size, e, rng);
  17630. if (ret != PRIME_GEN_E) break;
  17631. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17632. "trying again.\n");
  17633. }
  17634. return ret;
  17635. }
  17636. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17637. #else
  17638. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17639. #endif
  17640. #endif
  17641. /*
  17642. * Testing wc_MakeRsaKey()
  17643. */
  17644. static int test_wc_MakeRsaKey(void)
  17645. {
  17646. EXPECT_DECLS;
  17647. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17648. RsaKey genKey;
  17649. WC_RNG rng;
  17650. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17651. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17652. int bits = 1024;
  17653. #else
  17654. int bits = 2048;
  17655. #endif
  17656. XMEMSET(&genKey, 0, sizeof(RsaKey));
  17657. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17658. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17659. ExpectIntEQ(wc_InitRng(&rng), 0);
  17660. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17661. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17662. /* Test bad args. */
  17663. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  17664. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  17665. BAD_FUNC_ARG);
  17666. /* e < 3 */
  17667. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  17668. /* e & 1 == 0 */
  17669. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  17670. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17671. #endif
  17672. return EXPECT_RESULT();
  17673. } /* END test_wc_MakeRsaKey */
  17674. /*
  17675. * Test the bounds checking on the cipher text versus the key modulus.
  17676. * 1. Make a new RSA key.
  17677. * 2. Set c to 1.
  17678. * 3. Decrypt c into k. (error)
  17679. * 4. Copy the key modulus to c and sub 1 from the copy.
  17680. * 5. Decrypt c into k. (error)
  17681. * Valid bounds test cases are covered by all the other RSA tests.
  17682. */
  17683. static int test_RsaDecryptBoundsCheck(void)
  17684. {
  17685. EXPECT_DECLS;
  17686. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17687. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17688. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17689. WC_RNG rng;
  17690. RsaKey key;
  17691. byte flatC[256];
  17692. word32 flatCSz;
  17693. byte out[256];
  17694. word32 outSz = sizeof(out);
  17695. XMEMSET(&key, 0, sizeof(RsaKey));
  17696. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17697. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17698. ExpectIntEQ(wc_InitRng(&rng), 0);
  17699. if (EXPECT_SUCCESS()) {
  17700. const byte* derKey;
  17701. word32 derKeySz;
  17702. word32 idx = 0;
  17703. #ifdef USE_CERT_BUFFERS_1024
  17704. derKey = server_key_der_1024;
  17705. derKeySz = (word32)sizeof_server_key_der_1024;
  17706. flatCSz = 128;
  17707. #else
  17708. derKey = server_key_der_2048;
  17709. derKeySz = (word32)sizeof_server_key_der_2048;
  17710. flatCSz = 256;
  17711. #endif
  17712. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  17713. }
  17714. if (EXPECT_SUCCESS()) {
  17715. XMEMSET(flatC, 0, flatCSz);
  17716. flatC[flatCSz-1] = 1;
  17717. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17718. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  17719. if (EXPECT_SUCCESS()) {
  17720. mp_int c;
  17721. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  17722. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  17723. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  17724. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17725. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  17726. mp_clear(&c);
  17727. }
  17728. }
  17729. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17730. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17731. #endif
  17732. return EXPECT_RESULT();
  17733. } /* END test_wc_RsaDecryptBoundsCheck */
  17734. /*
  17735. * Testing wc_SetKeyUsage()
  17736. */
  17737. static int test_wc_SetKeyUsage(void)
  17738. {
  17739. EXPECT_DECLS;
  17740. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17741. Cert myCert;
  17742. ExpectIntEQ(wc_InitCert(&myCert), 0);
  17743. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  17744. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  17745. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  17746. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  17747. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  17748. /* Test bad args. */
  17749. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  17750. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  17751. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  17752. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  17753. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  17754. KEYUSAGE_E);
  17755. #endif
  17756. return EXPECT_RESULT();
  17757. } /* END test_wc_SetKeyUsage */
  17758. /*
  17759. * Testing wc_CheckProbablePrime()
  17760. */
  17761. static int test_wc_CheckProbablePrime(void)
  17762. {
  17763. EXPECT_DECLS;
  17764. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17765. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17766. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17767. RsaKey key;
  17768. WC_RNG rng;
  17769. byte e[3];
  17770. word32 eSz = (word32)sizeof(e);
  17771. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17772. word32 nSz = (word32)sizeof(n);
  17773. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17774. word32 dSz = (word32)sizeof(d);
  17775. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17776. word32 pSz = (word32)sizeof(p);
  17777. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17778. word32 qSz = (word32)sizeof(q);
  17779. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17780. int* isPrime;
  17781. int test[5];
  17782. isPrime = test;
  17783. XMEMSET(&key, 0, sizeof(RsaKey));
  17784. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17785. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17786. ExpectIntEQ(wc_InitRng(&rng), 0);
  17787. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17788. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  17789. WC_RSA_EXPONENT, &rng), 0);
  17790. PRIVATE_KEY_UNLOCK();
  17791. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  17792. &qSz), 0);
  17793. PRIVATE_KEY_LOCK();
  17794. /* Bad cases */
  17795. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  17796. BAD_FUNC_ARG);
  17797. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  17798. BAD_FUNC_ARG);
  17799. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  17800. BAD_FUNC_ARG);
  17801. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  17802. BAD_FUNC_ARG);
  17803. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  17804. BAD_FUNC_ARG);
  17805. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  17806. BAD_FUNC_ARG);
  17807. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  17808. BAD_FUNC_ARG);
  17809. /* Good case */
  17810. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  17811. 0);
  17812. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17813. wc_FreeRng(&rng);
  17814. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17815. #endif
  17816. return EXPECT_RESULT();
  17817. } /* END test_wc_CheckProbablePrime */
  17818. /*
  17819. * Testing wc_RsaPSS_Verify()
  17820. */
  17821. static int test_wc_RsaPSS_Verify(void)
  17822. {
  17823. EXPECT_DECLS;
  17824. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17825. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17826. RsaKey key;
  17827. WC_RNG rng;
  17828. int sz = 256;
  17829. const char* szMessage = "This is the string to be signed";
  17830. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17831. unsigned char pDecrypted[2048/8];
  17832. byte* pt = pDecrypted;
  17833. word32 outLen = sizeof(pDecrypted);
  17834. XMEMSET(&key, 0, sizeof(RsaKey));
  17835. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17836. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17837. ExpectIntEQ(wc_InitRng(&rng), 0);
  17838. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17839. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17840. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  17841. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  17842. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17843. /* Bad cases */
  17844. ExpectIntEQ(wc_RsaPSS_Verify(NULL, (word32)sz, pt, outLen,
  17845. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17846. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17847. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17848. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, (word32)sz, NULL, outLen,
  17849. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17850. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17851. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17852. /* Good case */
  17853. ExpectIntGT(wc_RsaPSS_Verify(pSignature, (word32)sz, pt, outLen,
  17854. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17855. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17856. wc_FreeRng(&rng);
  17857. #endif
  17858. return EXPECT_RESULT();
  17859. } /* END test_wc_RsaPSS_Verify */
  17860. /*
  17861. * Testing wc_RsaPSS_VerifyCheck()
  17862. */
  17863. static int test_wc_RsaPSS_VerifyCheck(void)
  17864. {
  17865. EXPECT_DECLS;
  17866. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17867. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17868. RsaKey key;
  17869. WC_RNG rng;
  17870. int sz = 256; /* 2048/8 */
  17871. byte digest[32];
  17872. word32 digestSz = sizeof(digest);
  17873. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17874. word32 pSignatureSz = sizeof(pSignature);
  17875. unsigned char pDecrypted[2048/8];
  17876. byte* pt = pDecrypted;
  17877. word32 outLen = sizeof(pDecrypted);
  17878. XMEMSET(&key, 0, sizeof(RsaKey));
  17879. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17880. XMEMSET(digest, 0, sizeof(digest));
  17881. XMEMSET(pSignature, 0, sizeof(pSignature));
  17882. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17883. ExpectIntEQ(wc_InitRng(&rng), 0);
  17884. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17885. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17886. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17887. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17888. 0);
  17889. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17890. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17891. /* Bad cases */
  17892. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, (word32)sz, pt, outLen, digest,
  17893. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17894. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  17895. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17896. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, NULL, outLen, digest,
  17897. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17898. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  17899. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17900. /* Good case */
  17901. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, pt, outLen, digest,
  17902. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17903. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17904. wc_FreeRng(&rng);
  17905. #endif
  17906. return EXPECT_RESULT();
  17907. } /* END test_wc_RsaPSS_VerifyCheck */
  17908. /*
  17909. * Testing wc_RsaPSS_VerifyCheckInline()
  17910. */
  17911. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17912. {
  17913. EXPECT_DECLS;
  17914. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17915. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17916. RsaKey key;
  17917. WC_RNG rng;
  17918. int sz = 256;
  17919. byte digest[32];
  17920. word32 digestSz = sizeof(digest);
  17921. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17922. unsigned char pDecrypted[2048/8];
  17923. byte* pt = pDecrypted;
  17924. XMEMSET(&key, 0, sizeof(RsaKey));
  17925. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17926. XMEMSET(digest, 0, sizeof(digest));
  17927. XMEMSET(pSignature, 0, sizeof(pSignature));
  17928. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17929. ExpectIntEQ(wc_InitRng(&rng), 0);
  17930. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17931. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17932. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17933. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17934. 0);
  17935. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17936. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17937. /* Bad Cases */
  17938. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, (word32)sz, &pt, digest,
  17939. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17940. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17941. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17942. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17943. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17944. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17945. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17946. /* Good case */
  17947. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17948. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17949. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17950. wc_FreeRng(&rng);
  17951. #endif
  17952. return EXPECT_RESULT();
  17953. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17954. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17955. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17956. {
  17957. (void)flag;
  17958. (void)type;
  17959. (void)file;
  17960. (void)line;
  17961. }
  17962. #endif
  17963. /*
  17964. * Testing wc_LockMutex_ex
  17965. */
  17966. static int test_wc_LockMutex_ex(void)
  17967. {
  17968. EXPECT_DECLS;
  17969. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17970. int flag = CRYPTO_LOCK;
  17971. int type = 0;
  17972. const char* file = "./test-LockMutex_ex.txt";
  17973. int line = 0;
  17974. /* without SetMutexCb */
  17975. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17976. /* with SetMutexCb */
  17977. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17978. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17979. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17980. #endif
  17981. return EXPECT_RESULT();
  17982. } /* End test_wc_LockMutex_ex*/
  17983. /*
  17984. * Testing wc_SetMutexCb
  17985. */
  17986. static int test_wc_SetMutexCb(void)
  17987. {
  17988. EXPECT_DECLS;
  17989. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17990. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17991. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17992. #endif
  17993. return EXPECT_RESULT();
  17994. } /* End test_wc_SetMutexCb*/
  17995. /*
  17996. * Testing wc_RsaKeyToDer()
  17997. */
  17998. static int test_wc_RsaKeyToDer(void)
  17999. {
  18000. EXPECT_DECLS;
  18001. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18002. RsaKey genKey;
  18003. WC_RNG rng;
  18004. byte* der = NULL;
  18005. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18006. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18007. int bits = 1024;
  18008. word32 derSz = 611;
  18009. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  18010. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  18011. #else
  18012. int bits = 2048;
  18013. word32 derSz = 1196;
  18014. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  18015. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  18016. #endif
  18017. XMEMSET(&rng, 0, sizeof(rng));
  18018. XMEMSET(&genKey, 0, sizeof(genKey));
  18019. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  18020. /* Init structures. */
  18021. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  18022. ExpectIntEQ(wc_InitRng(&rng), 0);
  18023. /* Make key. */
  18024. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  18025. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  18026. /* Pass good/bad args. */
  18027. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  18028. /* Get just the output length */
  18029. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  18030. /* Try Public Key. */
  18031. genKey.type = 0;
  18032. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  18033. #ifdef WOLFSSL_CHECK_MEM_ZERO
  18034. /* Put back to Private Key */
  18035. genKey.type = 1;
  18036. #endif
  18037. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18038. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  18039. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18040. #endif
  18041. return EXPECT_RESULT();
  18042. } /* END test_wc_RsaKeyToDer */
  18043. /*
  18044. * Testing wc_RsaKeyToPublicDer()
  18045. */
  18046. static int test_wc_RsaKeyToPublicDer(void)
  18047. {
  18048. EXPECT_DECLS;
  18049. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18050. RsaKey key;
  18051. WC_RNG rng;
  18052. byte* der = NULL;
  18053. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18054. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18055. int bits = 1024;
  18056. word32 derLen = 162;
  18057. #else
  18058. int bits = 2048;
  18059. word32 derLen = 294;
  18060. #endif
  18061. int ret;
  18062. XMEMSET(&rng, 0, sizeof(rng));
  18063. XMEMSET(&key, 0, sizeof(key));
  18064. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  18065. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  18066. ExpectIntEQ(wc_InitRng(&rng), 0);
  18067. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  18068. /* test getting size only */
  18069. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  18070. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  18071. /* test getting size only */
  18072. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  18073. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  18074. /* Pass in bad args. */
  18075. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  18076. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  18077. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  18078. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18079. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  18080. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18081. #endif
  18082. return EXPECT_RESULT();
  18083. } /* END test_wc_RsaKeyToPublicDer */
  18084. /*
  18085. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  18086. */
  18087. static int test_wc_RsaPublicEncryptDecrypt(void)
  18088. {
  18089. EXPECT_DECLS;
  18090. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18091. RsaKey key;
  18092. WC_RNG rng;
  18093. const char inStr[] = TEST_STRING;
  18094. const word32 plainLen = (word32)TEST_STRING_SZ;
  18095. const word32 inLen = (word32)TEST_STRING_SZ;
  18096. int bits = TEST_RSA_BITS;
  18097. const word32 cipherLen = TEST_RSA_BYTES;
  18098. word32 cipherLenResult = cipherLen;
  18099. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18100. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18101. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  18102. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  18103. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18104. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  18105. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18106. ExpectNotNull(in);
  18107. ExpectNotNull(plain);
  18108. ExpectNotNull(cipher);
  18109. #endif
  18110. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  18111. /* Initialize stack structures. */
  18112. XMEMSET(&key, 0, sizeof(RsaKey));
  18113. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18114. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  18115. ExpectIntEQ(wc_InitRng(&rng), 0);
  18116. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  18117. /* Encrypt. */
  18118. ExpectIntGT(cipherLenResult = (word32)wc_RsaPublicEncrypt(in, inLen, cipher,
  18119. cipherLen, &key, &rng), 0);
  18120. /* Pass bad args - tested in another testing function.*/
  18121. /* Decrypt */
  18122. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  18123. /* Bind rng */
  18124. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  18125. #endif
  18126. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  18127. &key), 0);
  18128. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  18129. /* Pass bad args - tested in another testing function.*/
  18130. WC_FREE_VAR(in, NULL);
  18131. WC_FREE_VAR(plain, NULL);
  18132. WC_FREE_VAR(cipher, NULL);
  18133. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  18134. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18135. #endif
  18136. return EXPECT_RESULT();
  18137. } /* END test_wc_RsaPublicEncryptDecrypt */
  18138. /*
  18139. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  18140. */
  18141. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  18142. {
  18143. EXPECT_DECLS;
  18144. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  18145. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  18146. RsaKey key;
  18147. WC_RNG rng;
  18148. const char inStr[] = TEST_STRING;
  18149. const word32 inLen = (word32)TEST_STRING_SZ;
  18150. const word32 plainSz = (word32)TEST_STRING_SZ;
  18151. byte* res = NULL;
  18152. int idx = 0;
  18153. int bits = TEST_RSA_BITS;
  18154. const word32 cipherSz = TEST_RSA_BYTES;
  18155. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18156. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18157. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  18158. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  18159. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18160. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  18161. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18162. ExpectNotNull(in);
  18163. ExpectNotNull(plain);
  18164. ExpectNotNull(cipher);
  18165. #endif
  18166. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  18167. /* Initialize stack structures. */
  18168. XMEMSET(&key, 0, sizeof(RsaKey));
  18169. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18170. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  18171. ExpectIntEQ(wc_InitRng(&rng), 0);
  18172. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  18173. /* Encrypt */
  18174. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  18175. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  18176. /* Pass bad args - tested in another testing function.*/
  18177. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18178. /* Decrypt */
  18179. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  18180. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  18181. #endif
  18182. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  18183. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  18184. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  18185. /* Pass bad args - tested in another testing function.*/
  18186. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  18187. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  18188. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  18189. #endif
  18190. WC_FREE_VAR(in, NULL);
  18191. WC_FREE_VAR(plain, NULL);
  18192. WC_FREE_VAR(cipher, NULL);
  18193. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  18194. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18195. #endif
  18196. return EXPECT_RESULT();
  18197. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  18198. /*
  18199. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  18200. */
  18201. static int test_wc_RsaSSL_SignVerify(void)
  18202. {
  18203. EXPECT_DECLS;
  18204. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18205. RsaKey key;
  18206. WC_RNG rng;
  18207. const char inStr[] = TEST_STRING;
  18208. const word32 plainSz = (word32)TEST_STRING_SZ;
  18209. const word32 inLen = (word32)TEST_STRING_SZ;
  18210. word32 idx = 0;
  18211. int bits = TEST_RSA_BITS;
  18212. const word32 outSz = TEST_RSA_BYTES;
  18213. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18214. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  18215. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18216. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  18217. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  18218. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18219. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18220. ExpectNotNull(in);
  18221. ExpectNotNull(out);
  18222. ExpectNotNull(plain);
  18223. #endif
  18224. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  18225. XMEMSET(&key, 0, sizeof(RsaKey));
  18226. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18227. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  18228. ExpectIntEQ(wc_InitRng(&rng), 0);
  18229. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  18230. /* Sign. */
  18231. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  18232. idx = (int)outSz;
  18233. /* Test bad args. */
  18234. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  18235. BAD_FUNC_ARG);
  18236. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  18237. BAD_FUNC_ARG);
  18238. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  18239. BAD_FUNC_ARG);
  18240. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  18241. BAD_FUNC_ARG);
  18242. /* Verify. */
  18243. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  18244. /* Pass bad args. */
  18245. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  18246. BAD_FUNC_ARG);
  18247. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  18248. BAD_FUNC_ARG);
  18249. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  18250. BAD_FUNC_ARG);
  18251. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  18252. BAD_FUNC_ARG);
  18253. WC_FREE_VAR(in, NULL);
  18254. WC_FREE_VAR(out, NULL);
  18255. WC_FREE_VAR(plain, NULL);
  18256. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  18257. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18258. #endif
  18259. return EXPECT_RESULT();
  18260. } /* END test_wc_RsaSSL_SignVerify */
  18261. /*
  18262. * Testing wc_RsaEncryptSize()
  18263. */
  18264. static int test_wc_RsaEncryptSize(void)
  18265. {
  18266. EXPECT_DECLS;
  18267. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18268. RsaKey key;
  18269. WC_RNG rng;
  18270. XMEMSET(&key, 0, sizeof(RsaKey));
  18271. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18272. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  18273. ExpectIntEQ(wc_InitRng(&rng), 0);
  18274. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18275. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18276. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  18277. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  18278. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  18279. #endif
  18280. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  18281. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  18282. /* Pass in bad arg. */
  18283. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  18284. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  18285. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18286. #endif
  18287. return EXPECT_RESULT();
  18288. } /* END test_wc_RsaEncryptSize*/
  18289. /*
  18290. * Testing wc_RsaFlattenPublicKey()
  18291. */
  18292. static int test_wc_RsaFlattenPublicKey(void)
  18293. {
  18294. EXPECT_DECLS;
  18295. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18296. RsaKey key;
  18297. WC_RNG rng;
  18298. byte e[256];
  18299. byte n[256];
  18300. word32 eSz = sizeof(e);
  18301. word32 nSz = sizeof(n);
  18302. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18303. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18304. int bits = 1024;
  18305. #else
  18306. int bits = 2048;
  18307. #endif
  18308. XMEMSET(&key, 0, sizeof(RsaKey));
  18309. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18310. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  18311. ExpectIntEQ(wc_InitRng(&rng), 0);
  18312. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  18313. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  18314. /* Pass bad args. */
  18315. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  18316. BAD_FUNC_ARG);
  18317. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  18318. BAD_FUNC_ARG);
  18319. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  18320. BAD_FUNC_ARG);
  18321. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  18322. BAD_FUNC_ARG);
  18323. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  18324. BAD_FUNC_ARG);
  18325. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  18326. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18327. #endif
  18328. return EXPECT_RESULT();
  18329. } /* END test_wc_RsaFlattenPublicKey */
  18330. /*
  18331. * unit test for wc_AesCcmSetKey
  18332. */
  18333. static int test_wc_AesCcmSetKey(void)
  18334. {
  18335. EXPECT_DECLS;
  18336. #ifdef HAVE_AESCCM
  18337. Aes aes;
  18338. const byte key16[] = {
  18339. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18340. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18341. };
  18342. const byte key24[] = {
  18343. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18344. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  18345. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  18346. };
  18347. const byte key32[] = {
  18348. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18349. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  18350. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18351. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  18352. };
  18353. XMEMSET(&aes, 0, sizeof(Aes));
  18354. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  18355. #ifdef WOLFSSL_AES_128
  18356. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  18357. #endif
  18358. #ifdef WOLFSSL_AES_192
  18359. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  18360. #endif
  18361. #ifdef WOLFSSL_AES_256
  18362. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  18363. #endif
  18364. /* Test bad args. */
  18365. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  18366. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  18367. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  18368. wc_AesFree(&aes);
  18369. #endif
  18370. return EXPECT_RESULT();
  18371. } /* END test_wc_AesCcmSetKey */
  18372. /*
  18373. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  18374. */
  18375. static int test_wc_AesCcmEncryptDecrypt(void)
  18376. {
  18377. EXPECT_DECLS;
  18378. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  18379. Aes aes;
  18380. const byte key16[] = {
  18381. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18382. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18383. };
  18384. /* plaintext */
  18385. const byte plainT[] = {
  18386. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  18387. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18388. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  18389. };
  18390. /* nonce */
  18391. const byte iv[] = {
  18392. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  18393. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  18394. };
  18395. const byte c[] = { /* cipher text. */
  18396. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  18397. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  18398. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  18399. };
  18400. const byte t[] = { /* Auth tag */
  18401. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  18402. };
  18403. const byte authIn[] = {
  18404. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  18405. };
  18406. byte cipherOut[sizeof(plainT)];
  18407. byte authTag[sizeof(t)];
  18408. #ifdef HAVE_AES_DECRYPT
  18409. byte plainOut[sizeof(cipherOut)];
  18410. #endif
  18411. XMEMSET(&aes, 0, sizeof(Aes));
  18412. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  18413. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  18414. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18415. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  18416. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  18417. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  18418. #ifdef HAVE_AES_DECRYPT
  18419. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18420. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  18421. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  18422. #endif
  18423. /* Pass in bad args. Encrypt*/
  18424. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  18425. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18426. BAD_FUNC_ARG);
  18427. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  18428. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18429. BAD_FUNC_ARG);
  18430. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  18431. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18432. BAD_FUNC_ARG);
  18433. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18434. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18435. BAD_FUNC_ARG);
  18436. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18437. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  18438. BAD_FUNC_ARG);
  18439. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18440. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18441. BAD_FUNC_ARG);
  18442. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18443. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18444. BAD_FUNC_ARG);
  18445. #ifdef HAVE_AES_DECRYPT
  18446. /* Pass in bad args. Decrypt*/
  18447. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  18448. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18449. BAD_FUNC_ARG);
  18450. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  18451. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18452. BAD_FUNC_ARG);
  18453. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  18454. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18455. BAD_FUNC_ARG);
  18456. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18457. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18458. BAD_FUNC_ARG);
  18459. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18460. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  18461. BAD_FUNC_ARG);
  18462. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18463. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18464. BAD_FUNC_ARG);
  18465. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18466. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18467. BAD_FUNC_ARG);
  18468. #endif
  18469. wc_AesFree(&aes);
  18470. #endif /* HAVE_AESCCM */
  18471. return EXPECT_RESULT();
  18472. } /* END test_wc_AesCcmEncryptDecrypt */
  18473. #if defined(WOLFSSL_AES_EAX) && \
  18474. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  18475. /*
  18476. * Testing test_wc_AesEaxVectors()
  18477. */
  18478. static int test_wc_AesEaxVectors(void)
  18479. {
  18480. EXPECT_DECLS;
  18481. typedef struct {
  18482. byte key[AES_256_KEY_SIZE];
  18483. int key_length;
  18484. byte iv[AES_BLOCK_SIZE];
  18485. int iv_length;
  18486. byte aad[AES_BLOCK_SIZE * 2];
  18487. int aad_length;
  18488. byte msg[AES_BLOCK_SIZE * 5];
  18489. int msg_length;
  18490. byte ct[AES_BLOCK_SIZE * 5];
  18491. int ct_length;
  18492. byte tag[AES_BLOCK_SIZE];
  18493. int tag_length;
  18494. int valid;
  18495. } AadVector;
  18496. /* Test vectors obtained from Google wycheproof project
  18497. * https://github.com/google/wycheproof
  18498. * from testvectors/aes_eax_test.json
  18499. */
  18500. const AadVector vectors[] = {
  18501. {
  18502. /* key, key length */
  18503. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  18504. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  18505. /* iv, iv length */
  18506. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  18507. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  18508. /* aad, aad length */
  18509. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  18510. /* msg, msg length */
  18511. {0x00}, 0,
  18512. /* ct, ct length */
  18513. {0x00}, 0,
  18514. /* tag, tag length */
  18515. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  18516. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  18517. /* valid */
  18518. 1,
  18519. },
  18520. {
  18521. /* key, key length */
  18522. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  18523. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  18524. /* iv, iv length */
  18525. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  18526. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  18527. /* aad, aad length */
  18528. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  18529. /* msg, msg length */
  18530. {0xf7, 0xfb}, 2,
  18531. /* ct, ct length */
  18532. {0x19, 0xdd}, 2,
  18533. /* tag, tag length */
  18534. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  18535. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  18536. /* valid */
  18537. 1,
  18538. },
  18539. {
  18540. /* key, key length */
  18541. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  18542. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  18543. /* iv, iv length */
  18544. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  18545. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  18546. /* aad, aad length */
  18547. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  18548. /* msg, msg length */
  18549. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  18550. /* ct, ct length */
  18551. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  18552. /* tag, tag length */
  18553. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  18554. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  18555. /* valid */
  18556. 1,
  18557. },
  18558. {
  18559. /* key, key length */
  18560. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  18561. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  18562. /* iv, iv length */
  18563. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  18564. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  18565. /* aad, aad length */
  18566. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  18567. /* msg, msg length */
  18568. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  18569. /* ct, ct length */
  18570. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  18571. /* tag, tag length */
  18572. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  18573. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  18574. /* valid */
  18575. 1,
  18576. },
  18577. {
  18578. /* key, key length */
  18579. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  18580. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  18581. /* iv, iv length */
  18582. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  18583. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  18584. /* aad, aad length */
  18585. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  18586. /* msg, msg length */
  18587. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  18588. /* ct, ct length */
  18589. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  18590. /* tag, tag length */
  18591. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  18592. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  18593. /* valid */
  18594. 1,
  18595. },
  18596. {
  18597. /* key, key length */
  18598. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  18599. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  18600. /* iv, iv length */
  18601. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  18602. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  18603. /* aad, aad length */
  18604. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  18605. /* msg, msg length */
  18606. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  18607. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  18608. /* ct, ct length */
  18609. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  18610. 0x0e, 0x72, 0x84, 0x14}, 12,
  18611. /* tag, tag length */
  18612. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  18613. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  18614. /* valid */
  18615. 1,
  18616. },
  18617. {
  18618. /* key, key length */
  18619. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  18620. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  18621. /* iv, iv length */
  18622. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  18623. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  18624. /* aad, aad length */
  18625. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  18626. /* msg, msg length */
  18627. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  18628. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  18629. 0x36}, 17,
  18630. /* ct, ct length */
  18631. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  18632. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  18633. 0x30}, 17,
  18634. /* tag, tag length */
  18635. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  18636. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  18637. /* valid */
  18638. 1,
  18639. },
  18640. {
  18641. /* key, key length */
  18642. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  18643. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  18644. /* iv, iv length */
  18645. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  18646. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  18647. /* aad, aad length */
  18648. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  18649. /* msg, msg length */
  18650. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  18651. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  18652. 0x2d, 0x56}, 18,
  18653. /* ct, ct length */
  18654. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  18655. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  18656. 0x8c, 0xc3}, 18,
  18657. /* tag, tag length */
  18658. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  18659. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  18660. /* valid */
  18661. 1,
  18662. },
  18663. {
  18664. /* key, key length */
  18665. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  18666. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  18667. /* iv, iv length */
  18668. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  18669. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  18670. /* aad, aad length */
  18671. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  18672. /* msg, msg length */
  18673. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  18674. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  18675. 0xef, 0x11}, 18,
  18676. /* ct, ct length */
  18677. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  18678. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  18679. 0xb1, 0xe8}, 18,
  18680. /* tag, tag length */
  18681. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  18682. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  18683. /* valid */
  18684. 1,
  18685. },
  18686. {
  18687. /* key, key length */
  18688. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  18689. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  18690. /* iv, iv length */
  18691. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  18692. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  18693. /* aad, aad length */
  18694. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  18695. /* msg, msg length */
  18696. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  18697. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  18698. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  18699. /* ct, ct length */
  18700. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  18701. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  18702. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  18703. /* tag, tag length */
  18704. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  18705. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  18706. /* valid */
  18707. 1,
  18708. },
  18709. {
  18710. /* key, key length */
  18711. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18712. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18713. /* iv, iv length */
  18714. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  18715. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  18716. /* aad, aad length */
  18717. {0x00}, 0,
  18718. /* msg, msg length */
  18719. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18720. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18721. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18722. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18723. /* ct, ct length */
  18724. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  18725. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  18726. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  18727. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  18728. /* tag, tag length */
  18729. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  18730. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  18731. /* valid */
  18732. 1,
  18733. },
  18734. {
  18735. /* key, key length */
  18736. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18737. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18738. /* iv, iv length */
  18739. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  18740. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  18741. /* aad, aad length */
  18742. {0x00}, 0,
  18743. /* msg, msg length */
  18744. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18745. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18746. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18747. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18748. /* ct, ct length */
  18749. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  18750. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  18751. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  18752. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  18753. /* tag, tag length */
  18754. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  18755. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  18756. /* valid */
  18757. 1,
  18758. },
  18759. {
  18760. /* key, key length */
  18761. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18762. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18763. /* iv, iv length */
  18764. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  18765. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  18766. /* aad, aad length */
  18767. {0x00}, 0,
  18768. /* msg, msg length */
  18769. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18770. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18771. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18772. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18773. /* ct, ct length */
  18774. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  18775. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  18776. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  18777. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  18778. /* tag, tag length */
  18779. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  18780. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  18781. /* valid */
  18782. 1,
  18783. },
  18784. {
  18785. /* key, key length */
  18786. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18787. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18788. /* iv, iv length */
  18789. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  18790. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  18791. /* aad, aad length */
  18792. {0x00}, 0,
  18793. /* msg, msg length */
  18794. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18795. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18796. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18797. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18798. /* ct, ct length */
  18799. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  18800. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  18801. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  18802. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  18803. /* tag, tag length */
  18804. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  18805. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  18806. /* valid */
  18807. 1,
  18808. },
  18809. {
  18810. /* key, key length */
  18811. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18812. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18813. /* iv, iv length */
  18814. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  18815. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  18816. /* aad, aad length */
  18817. {0x00}, 0,
  18818. /* msg, msg length */
  18819. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18820. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18821. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18822. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18823. /* ct, ct length */
  18824. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  18825. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  18826. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  18827. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  18828. /* tag, tag length */
  18829. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  18830. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  18831. /* valid */
  18832. 1,
  18833. },
  18834. {
  18835. /* key, key length */
  18836. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18837. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18838. /* iv, iv length */
  18839. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  18840. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  18841. /* aad, aad length */
  18842. {0x00}, 0,
  18843. /* msg, msg length */
  18844. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18845. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18846. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18847. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18848. /* ct, ct length */
  18849. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  18850. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  18851. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  18852. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  18853. /* tag, tag length */
  18854. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  18855. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  18856. /* valid */
  18857. 1,
  18858. },
  18859. {
  18860. /* key, key length */
  18861. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18862. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18863. /* iv, iv length */
  18864. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  18865. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  18866. /* aad, aad length */
  18867. {0x00}, 0,
  18868. /* msg, msg length */
  18869. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18870. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18871. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18872. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18873. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18874. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18875. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18876. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  18877. /* ct, ct length */
  18878. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  18879. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  18880. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  18881. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  18882. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  18883. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  18884. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  18885. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  18886. /* tag, tag length */
  18887. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  18888. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  18889. /* valid */
  18890. 1,
  18891. },
  18892. {
  18893. /* key, key length */
  18894. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18895. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18896. /* iv, iv length */
  18897. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  18898. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  18899. /* aad, aad length */
  18900. {0x00}, 0,
  18901. /* msg, msg length */
  18902. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18903. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18904. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18905. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18906. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18907. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18908. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18909. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18910. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18911. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18912. /* ct, ct length */
  18913. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18914. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18915. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18916. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18917. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18918. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18919. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18920. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18921. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18922. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18923. /* tag, tag length */
  18924. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18925. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18926. /* valid */
  18927. 1,
  18928. },
  18929. {
  18930. /* key, key length */
  18931. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18932. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18933. /* iv, iv length */
  18934. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18935. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18936. /* aad, aad length */
  18937. {0x00}, 0,
  18938. /* msg, msg length */
  18939. {0x00}, 0,
  18940. /* ct, ct length */
  18941. {0x00}, 0,
  18942. /* tag, tag length */
  18943. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18944. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18945. /* valid */
  18946. 1,
  18947. },
  18948. {
  18949. /* key, key length */
  18950. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18951. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18952. /* iv, iv length */
  18953. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18954. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18955. /* aad, aad length */
  18956. {0x00}, 0,
  18957. /* msg, msg length */
  18958. {0x01}, 1,
  18959. /* ct, ct length */
  18960. {0x1d}, 1,
  18961. /* tag, tag length */
  18962. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18963. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18964. /* valid */
  18965. 1,
  18966. },
  18967. {
  18968. /* key, key length */
  18969. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18970. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18971. /* iv, iv length */
  18972. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18973. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18974. /* aad, aad length */
  18975. {0x00}, 0,
  18976. /* msg, msg length */
  18977. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18978. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18979. /* ct, ct length */
  18980. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18981. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18982. /* tag, tag length */
  18983. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18984. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18985. /* valid */
  18986. 1,
  18987. },
  18988. {
  18989. /* key, key length */
  18990. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18991. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18992. /* iv, iv length */
  18993. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18994. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18995. /* aad, aad length */
  18996. {0x00}, 0,
  18997. /* msg, msg length */
  18998. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18999. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  19000. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  19001. /* ct, ct length */
  19002. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  19003. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  19004. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  19005. /* tag, tag length */
  19006. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  19007. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  19008. /* valid */
  19009. 1,
  19010. },
  19011. {
  19012. /* key, key length */
  19013. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  19014. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  19015. /* iv, iv length */
  19016. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  19017. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  19018. /* aad, aad length */
  19019. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  19020. /* msg, msg length */
  19021. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  19022. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  19023. /* ct, ct length */
  19024. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  19025. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  19026. /* tag, tag length */
  19027. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  19028. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  19029. /* valid */
  19030. 1,
  19031. },
  19032. {
  19033. /* key, key length */
  19034. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  19035. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  19036. /* iv, iv length */
  19037. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  19038. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  19039. /* aad, aad length */
  19040. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  19041. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  19042. /* msg, msg length */
  19043. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  19044. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  19045. /* ct, ct length */
  19046. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  19047. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  19048. /* tag, tag length */
  19049. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  19050. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  19051. /* valid */
  19052. 1,
  19053. },
  19054. {
  19055. /* key, key length */
  19056. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  19057. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  19058. /* iv, iv length */
  19059. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  19060. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  19061. /* aad, aad length */
  19062. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  19063. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  19064. /* msg, msg length */
  19065. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  19066. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  19067. /* ct, ct length */
  19068. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  19069. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  19070. /* tag, tag length */
  19071. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  19072. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  19073. /* valid */
  19074. 1,
  19075. },
  19076. {
  19077. /* key, key length */
  19078. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  19079. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  19080. /* iv, iv length */
  19081. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  19082. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  19083. /* aad, aad length */
  19084. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  19085. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  19086. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  19087. /* msg, msg length */
  19088. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  19089. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  19090. /* ct, ct length */
  19091. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  19092. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  19093. /* tag, tag length */
  19094. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  19095. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  19096. /* valid */
  19097. 1,
  19098. },
  19099. {
  19100. /* key, key length */
  19101. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19102. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19103. /* iv, iv length */
  19104. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19105. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19106. /* aad, aad length */
  19107. {0x00}, 0,
  19108. /* msg, msg length */
  19109. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19110. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19111. /* ct, ct length */
  19112. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19113. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19114. /* tag, tag length */
  19115. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19116. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19117. /* valid */
  19118. 0,
  19119. },
  19120. {
  19121. /* key, key length */
  19122. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19123. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19124. /* iv, iv length */
  19125. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19126. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19127. /* aad, aad length */
  19128. {0x00}, 0,
  19129. /* msg, msg length */
  19130. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19131. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19132. /* ct, ct length */
  19133. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19134. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19135. /* tag, tag length */
  19136. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19137. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19138. /* valid */
  19139. 0,
  19140. },
  19141. {
  19142. /* key, key length */
  19143. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19144. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19145. /* iv, iv length */
  19146. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19147. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19148. /* aad, aad length */
  19149. {0x00}, 0,
  19150. /* msg, msg length */
  19151. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19152. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19153. /* ct, ct length */
  19154. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19155. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19156. /* tag, tag length */
  19157. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19158. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19159. /* valid */
  19160. 0,
  19161. },
  19162. {
  19163. /* key, key length */
  19164. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19165. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19166. /* iv, iv length */
  19167. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19168. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19169. /* aad, aad length */
  19170. {0x00}, 0,
  19171. /* msg, msg length */
  19172. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19173. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19174. /* ct, ct length */
  19175. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19176. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19177. /* tag, tag length */
  19178. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19179. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19180. /* valid */
  19181. 0,
  19182. },
  19183. {
  19184. /* key, key length */
  19185. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19186. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19187. /* iv, iv length */
  19188. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19189. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19190. /* aad, aad length */
  19191. {0x00}, 0,
  19192. /* msg, msg length */
  19193. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19194. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19195. /* ct, ct length */
  19196. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19197. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19198. /* tag, tag length */
  19199. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  19200. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19201. /* valid */
  19202. 0,
  19203. },
  19204. {
  19205. /* key, key length */
  19206. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19207. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19208. /* iv, iv length */
  19209. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19210. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19211. /* aad, aad length */
  19212. {0x00}, 0,
  19213. /* msg, msg length */
  19214. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19215. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19216. /* ct, ct length */
  19217. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19218. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19219. /* tag, tag length */
  19220. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  19221. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19222. /* valid */
  19223. 0,
  19224. },
  19225. {
  19226. /* key, key length */
  19227. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19228. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19229. /* iv, iv length */
  19230. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19231. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19232. /* aad, aad length */
  19233. {0x00}, 0,
  19234. /* msg, msg length */
  19235. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19236. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19237. /* ct, ct length */
  19238. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19239. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19240. /* tag, tag length */
  19241. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  19242. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19243. /* valid */
  19244. 0,
  19245. },
  19246. {
  19247. /* key, key length */
  19248. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19249. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19250. /* iv, iv length */
  19251. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19252. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19253. /* aad, aad length */
  19254. {0x00}, 0,
  19255. /* msg, msg length */
  19256. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19257. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19258. /* ct, ct length */
  19259. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19260. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19261. /* tag, tag length */
  19262. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  19263. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19264. /* valid */
  19265. 0,
  19266. },
  19267. {
  19268. /* key, key length */
  19269. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19270. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19271. /* iv, iv length */
  19272. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19273. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19274. /* aad, aad length */
  19275. {0x00}, 0,
  19276. /* msg, msg length */
  19277. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19278. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19279. /* ct, ct length */
  19280. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19281. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19282. /* tag, tag length */
  19283. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19284. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19285. /* valid */
  19286. 0,
  19287. },
  19288. {
  19289. /* key, key length */
  19290. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19291. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19292. /* iv, iv length */
  19293. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19294. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19295. /* aad, aad length */
  19296. {0x00}, 0,
  19297. /* msg, msg length */
  19298. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19299. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19300. /* ct, ct length */
  19301. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19302. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19303. /* tag, tag length */
  19304. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19305. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19306. /* valid */
  19307. 0,
  19308. },
  19309. {
  19310. /* key, key length */
  19311. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19312. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19313. /* iv, iv length */
  19314. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19315. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19316. /* aad, aad length */
  19317. {0x00}, 0,
  19318. /* msg, msg length */
  19319. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19320. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19321. /* ct, ct length */
  19322. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19323. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19324. /* tag, tag length */
  19325. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19326. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19327. /* valid */
  19328. 0,
  19329. },
  19330. {
  19331. /* key, key length */
  19332. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19333. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19334. /* iv, iv length */
  19335. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19336. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19337. /* aad, aad length */
  19338. {0x00}, 0,
  19339. /* msg, msg length */
  19340. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19341. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19342. /* ct, ct length */
  19343. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19344. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19345. /* tag, tag length */
  19346. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19347. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19348. /* valid */
  19349. 0,
  19350. },
  19351. {
  19352. /* key, key length */
  19353. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19354. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19355. /* iv, iv length */
  19356. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19357. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19358. /* aad, aad length */
  19359. {0x00}, 0,
  19360. /* msg, msg length */
  19361. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19362. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19363. /* ct, ct length */
  19364. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19365. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19366. /* tag, tag length */
  19367. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19368. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  19369. /* valid */
  19370. 0,
  19371. },
  19372. {
  19373. /* key, key length */
  19374. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19375. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19376. /* iv, iv length */
  19377. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19378. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19379. /* aad, aad length */
  19380. {0x00}, 0,
  19381. /* msg, msg length */
  19382. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19383. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19384. /* ct, ct length */
  19385. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19386. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19387. /* tag, tag length */
  19388. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19389. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  19390. /* valid */
  19391. 0,
  19392. },
  19393. {
  19394. /* key, key length */
  19395. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19396. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19397. /* iv, iv length */
  19398. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19399. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19400. /* aad, aad length */
  19401. {0x00}, 0,
  19402. /* msg, msg length */
  19403. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19404. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19405. /* ct, ct length */
  19406. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19407. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19408. /* tag, tag length */
  19409. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19410. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  19411. /* valid */
  19412. 0,
  19413. },
  19414. {
  19415. /* key, key length */
  19416. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19417. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19418. /* iv, iv length */
  19419. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19420. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19421. /* aad, aad length */
  19422. {0x00}, 0,
  19423. /* msg, msg length */
  19424. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19425. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19426. /* ct, ct length */
  19427. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19428. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19429. /* tag, tag length */
  19430. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19431. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  19432. /* valid */
  19433. 0,
  19434. },
  19435. {
  19436. /* key, key length */
  19437. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19438. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19439. /* iv, iv length */
  19440. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19441. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19442. /* aad, aad length */
  19443. {0x00}, 0,
  19444. /* msg, msg length */
  19445. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19446. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19447. /* ct, ct length */
  19448. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19449. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19450. /* tag, tag length */
  19451. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19452. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  19453. /* valid */
  19454. 0,
  19455. },
  19456. {
  19457. /* key, key length */
  19458. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19459. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19460. /* iv, iv length */
  19461. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19462. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19463. /* aad, aad length */
  19464. {0x00}, 0,
  19465. /* msg, msg length */
  19466. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19467. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19468. /* ct, ct length */
  19469. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19470. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19471. /* tag, tag length */
  19472. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19473. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  19474. /* valid */
  19475. 0,
  19476. },
  19477. {
  19478. /* key, key length */
  19479. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19480. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19481. /* iv, iv length */
  19482. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19483. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19484. /* aad, aad length */
  19485. {0x00}, 0,
  19486. /* msg, msg length */
  19487. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19488. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19489. /* ct, ct length */
  19490. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19491. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19492. /* tag, tag length */
  19493. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19494. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19495. /* valid */
  19496. 0,
  19497. },
  19498. {
  19499. /* key, key length */
  19500. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19501. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19502. /* iv, iv length */
  19503. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19504. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19505. /* aad, aad length */
  19506. {0x00}, 0,
  19507. /* msg, msg length */
  19508. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19509. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19510. /* ct, ct length */
  19511. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19512. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19513. /* tag, tag length */
  19514. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19515. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19516. /* valid */
  19517. 0,
  19518. },
  19519. {
  19520. /* key, key length */
  19521. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19522. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19523. /* iv, iv length */
  19524. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19525. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19526. /* aad, aad length */
  19527. {0x00}, 0,
  19528. /* msg, msg length */
  19529. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19530. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19531. /* ct, ct length */
  19532. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19533. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19534. /* tag, tag length */
  19535. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  19536. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19537. /* valid */
  19538. 0,
  19539. },
  19540. {
  19541. /* key, key length */
  19542. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19543. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19544. /* iv, iv length */
  19545. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19546. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19547. /* aad, aad length */
  19548. {0x00}, 0,
  19549. /* msg, msg length */
  19550. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19551. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19552. /* ct, ct length */
  19553. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19554. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19555. /* tag, tag length */
  19556. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  19557. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19558. /* valid */
  19559. 0,
  19560. },
  19561. {
  19562. /* key, key length */
  19563. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19564. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19565. /* iv, iv length */
  19566. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19567. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19568. /* aad, aad length */
  19569. {0x00}, 0,
  19570. /* msg, msg length */
  19571. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19572. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19573. /* ct, ct length */
  19574. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19575. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19576. /* tag, tag length */
  19577. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  19578. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  19579. /* valid */
  19580. 0,
  19581. },
  19582. {
  19583. /* key, key length */
  19584. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19585. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19586. /* iv, iv length */
  19587. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19588. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19589. /* aad, aad length */
  19590. {0x00}, 0,
  19591. /* msg, msg length */
  19592. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19593. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19594. /* ct, ct length */
  19595. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19596. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19597. /* tag, tag length */
  19598. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19599. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  19600. /* valid */
  19601. 0,
  19602. },
  19603. {
  19604. /* key, key length */
  19605. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19606. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19607. /* iv, iv length */
  19608. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19609. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19610. /* aad, aad length */
  19611. {0x00}, 0,
  19612. /* msg, msg length */
  19613. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19614. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19615. /* ct, ct length */
  19616. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19617. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19618. /* tag, tag length */
  19619. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19620. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  19621. /* valid */
  19622. 0,
  19623. },
  19624. {
  19625. /* key, key length */
  19626. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19627. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19628. /* iv, iv length */
  19629. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19630. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19631. /* aad, aad length */
  19632. {0x00}, 0,
  19633. /* msg, msg length */
  19634. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19635. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19636. /* ct, ct length */
  19637. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19638. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19639. /* tag, tag length */
  19640. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  19641. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  19642. /* valid */
  19643. 0,
  19644. },
  19645. {
  19646. /* key, key length */
  19647. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19648. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19649. /* iv, iv length */
  19650. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19651. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19652. /* aad, aad length */
  19653. {0x00}, 0,
  19654. /* msg, msg length */
  19655. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19656. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19657. /* ct, ct length */
  19658. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19659. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19660. /* tag, tag length */
  19661. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  19662. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  19663. /* valid */
  19664. 0,
  19665. },
  19666. };
  19667. byte ciphertext[sizeof(vectors[0].ct)];
  19668. byte authtag[sizeof(vectors[0].tag)];
  19669. int i;
  19670. int len;
  19671. int ret;
  19672. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  19673. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19674. len = sizeof(authtag);
  19675. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  19676. ciphertext,
  19677. vectors[i].msg, vectors[i].msg_length,
  19678. vectors[i].iv, vectors[i].iv_length,
  19679. authtag, len,
  19680. vectors[i].aad, vectors[i].aad_length),
  19681. 0);
  19682. /* check ciphertext matches vector */
  19683. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  19684. 0);
  19685. /* check that computed tag matches vector only for vectors marked as valid */
  19686. ret = XMEMCMP(authtag, vectors[i].tag, len);
  19687. if (vectors[i].valid) {
  19688. ExpectIntEQ(ret, 0);
  19689. }
  19690. else {
  19691. ExpectIntNE(ret, 0);
  19692. }
  19693. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19694. /* Decrypt, checking that the computed auth tags match */
  19695. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  19696. ciphertext,
  19697. vectors[i].ct, vectors[i].ct_length,
  19698. vectors[i].iv, vectors[i].iv_length,
  19699. authtag, len,
  19700. vectors[i].aad, vectors[i].aad_length),
  19701. 0);
  19702. /* check decrypted ciphertext matches vector plaintext */
  19703. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  19704. 0);
  19705. }
  19706. return EXPECT_RESULT();
  19707. } /* END test_wc_AesEaxVectors */
  19708. /*
  19709. * Testing test_wc_AesEaxEncryptAuth()
  19710. */
  19711. static int test_wc_AesEaxEncryptAuth(void)
  19712. {
  19713. EXPECT_DECLS;
  19714. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19715. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19716. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19717. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19718. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19719. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19720. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19721. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19722. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19723. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19724. byte ciphertext[sizeof(msg)];
  19725. byte authtag[AES_BLOCK_SIZE];
  19726. int i;
  19727. int len;
  19728. len = sizeof(authtag);
  19729. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19730. ciphertext,
  19731. msg, sizeof(msg),
  19732. iv, sizeof(iv),
  19733. authtag, (word32)len,
  19734. aad, sizeof(aad)),
  19735. 0);
  19736. /* Test null checking */
  19737. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  19738. ciphertext,
  19739. msg, sizeof(msg),
  19740. iv, sizeof(iv),
  19741. authtag, (word32)len,
  19742. aad, sizeof(aad)),
  19743. BAD_FUNC_ARG);
  19744. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19745. NULL,
  19746. msg, sizeof(msg),
  19747. iv, sizeof(iv),
  19748. authtag, (word32)len,
  19749. aad, sizeof(aad)),
  19750. BAD_FUNC_ARG);
  19751. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19752. ciphertext,
  19753. NULL, sizeof(msg),
  19754. iv, sizeof(iv),
  19755. authtag, (word32)len,
  19756. aad, sizeof(aad)),
  19757. BAD_FUNC_ARG);
  19758. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19759. ciphertext,
  19760. msg, sizeof(msg),
  19761. NULL, sizeof(iv),
  19762. authtag, (word32)len,
  19763. aad, sizeof(aad)),
  19764. BAD_FUNC_ARG);
  19765. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19766. ciphertext,
  19767. msg, sizeof(msg),
  19768. iv, sizeof(iv),
  19769. NULL, (word32)len,
  19770. aad, sizeof(aad)),
  19771. BAD_FUNC_ARG);
  19772. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19773. ciphertext,
  19774. msg, sizeof(msg),
  19775. iv, sizeof(iv),
  19776. authtag, (word32)len,
  19777. NULL, sizeof(aad)),
  19778. BAD_FUNC_ARG);
  19779. /* Test bad key lengths */
  19780. for (i = 0; i <= 32; i++) {
  19781. int exp_ret;
  19782. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19783. || i == AES_256_KEY_SIZE) {
  19784. exp_ret = 0;
  19785. }
  19786. else {
  19787. exp_ret = BAD_FUNC_ARG;
  19788. }
  19789. ExpectIntEQ(wc_AesEaxEncryptAuth(key, (word32)i,
  19790. ciphertext,
  19791. msg, sizeof(msg),
  19792. iv, sizeof(iv),
  19793. authtag, (word32)len,
  19794. aad, sizeof(aad)),
  19795. exp_ret);
  19796. }
  19797. /* Test auth tag size out of range */
  19798. len = AES_BLOCK_SIZE + 1;
  19799. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19800. ciphertext,
  19801. msg, sizeof(msg),
  19802. iv, sizeof(iv),
  19803. authtag, (word32)len,
  19804. aad, sizeof(aad)),
  19805. BAD_FUNC_ARG);
  19806. return EXPECT_RESULT();
  19807. } /* END test_wc_AesEaxEncryptAuth() */
  19808. /*
  19809. * Testing test_wc_AesEaxDecryptAuth()
  19810. */
  19811. static int test_wc_AesEaxDecryptAuth(void)
  19812. {
  19813. EXPECT_DECLS;
  19814. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19815. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19816. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19817. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19818. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19819. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19820. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19821. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19822. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19823. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19824. /* Garbage tag that should always fail for above aad */
  19825. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  19826. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  19827. byte plaintext[sizeof(ct)];
  19828. int i;
  19829. int len;
  19830. len = sizeof(tag);
  19831. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19832. plaintext,
  19833. ct, sizeof(ct),
  19834. iv, sizeof(iv),
  19835. tag, (word32)len,
  19836. aad, sizeof(aad)),
  19837. AES_EAX_AUTH_E);
  19838. /* Test null checking */
  19839. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  19840. plaintext,
  19841. ct, sizeof(ct),
  19842. iv, sizeof(iv),
  19843. tag, (word32)len,
  19844. aad, sizeof(aad)),
  19845. BAD_FUNC_ARG);
  19846. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19847. NULL,
  19848. ct, sizeof(ct),
  19849. iv, sizeof(iv),
  19850. tag, (word32)len,
  19851. aad, sizeof(aad)),
  19852. BAD_FUNC_ARG);
  19853. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19854. plaintext,
  19855. NULL, sizeof(ct),
  19856. iv, sizeof(iv),
  19857. tag, (word32)len,
  19858. aad, sizeof(aad)),
  19859. BAD_FUNC_ARG);
  19860. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19861. plaintext,
  19862. ct, sizeof(ct),
  19863. NULL, sizeof(iv),
  19864. tag, (word32)len,
  19865. aad, sizeof(aad)),
  19866. BAD_FUNC_ARG);
  19867. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19868. plaintext,
  19869. ct, sizeof(ct),
  19870. iv, sizeof(iv),
  19871. NULL, (word32)len,
  19872. aad, sizeof(aad)),
  19873. BAD_FUNC_ARG);
  19874. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19875. plaintext,
  19876. ct, sizeof(ct),
  19877. iv, sizeof(iv),
  19878. tag, (word32)len,
  19879. NULL, sizeof(aad)),
  19880. BAD_FUNC_ARG);
  19881. /* Test bad key lengths */
  19882. for (i = 0; i <= 32; i++) {
  19883. int exp_ret;
  19884. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19885. || i == AES_256_KEY_SIZE) {
  19886. exp_ret = AES_EAX_AUTH_E;
  19887. }
  19888. else {
  19889. exp_ret = BAD_FUNC_ARG;
  19890. }
  19891. ExpectIntEQ(wc_AesEaxDecryptAuth(key, (word32)i,
  19892. plaintext,
  19893. ct, sizeof(ct),
  19894. iv, sizeof(iv),
  19895. tag, (word32)len,
  19896. aad, sizeof(aad)),
  19897. exp_ret);
  19898. }
  19899. /* Test auth tag size out of range */
  19900. len = AES_BLOCK_SIZE + 1;
  19901. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19902. plaintext,
  19903. ct, sizeof(ct),
  19904. iv, sizeof(iv),
  19905. tag, (word32)len,
  19906. aad, sizeof(aad)),
  19907. BAD_FUNC_ARG);
  19908. return EXPECT_RESULT();
  19909. } /* END test_wc_AesEaxDecryptAuth() */
  19910. #endif /* WOLFSSL_AES_EAX &&
  19911. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19912. */
  19913. /*
  19914. * Testing wc_InitDsaKey()
  19915. */
  19916. static int test_wc_InitDsaKey(void)
  19917. {
  19918. EXPECT_DECLS;
  19919. #ifndef NO_DSA
  19920. DsaKey key;
  19921. XMEMSET(&key, 0, sizeof(DsaKey));
  19922. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19923. /* Pass in bad args. */
  19924. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19925. wc_FreeDsaKey(&key);
  19926. #endif
  19927. return EXPECT_RESULT();
  19928. } /* END test_wc_InitDsaKey */
  19929. /*
  19930. * Testing wc_DsaSign() and wc_DsaVerify()
  19931. */
  19932. static int test_wc_DsaSignVerify(void)
  19933. {
  19934. EXPECT_DECLS;
  19935. #if !defined(NO_DSA)
  19936. DsaKey key;
  19937. WC_RNG rng;
  19938. wc_Sha sha;
  19939. byte signature[DSA_SIG_SIZE];
  19940. byte hash[WC_SHA_DIGEST_SIZE];
  19941. word32 idx = 0;
  19942. word32 bytes;
  19943. int answer;
  19944. #ifdef USE_CERT_BUFFERS_1024
  19945. byte tmp[ONEK_BUF];
  19946. XMEMSET(tmp, 0, sizeof(tmp));
  19947. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19948. bytes = sizeof_dsa_key_der_1024;
  19949. #elif defined(USE_CERT_BUFFERS_2048)
  19950. byte tmp[TWOK_BUF];
  19951. XMEMSET(tmp, 0, sizeof(tmp));
  19952. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19953. bytes = sizeof_dsa_key_der_2048;
  19954. #else
  19955. byte tmp[TWOK_BUF];
  19956. XFILE fp = XBADFILE;
  19957. XMEMSET(tmp, 0, sizeof(tmp));
  19958. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19959. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19960. if (fp != XBADFILE)
  19961. XFCLOSE(fp);
  19962. #endif /* END USE_CERT_BUFFERS_1024 */
  19963. ExpectIntEQ(wc_InitSha(&sha), 0);
  19964. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19965. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19966. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19967. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19968. ExpectIntEQ(wc_InitRng(&rng), 0);
  19969. /* Sign. */
  19970. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19971. /* Test bad args. */
  19972. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19973. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19974. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19975. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19976. /* Verify. */
  19977. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19978. ExpectIntEQ(answer, 1);
  19979. /* Pass in bad args. */
  19980. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19981. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19982. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19983. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19984. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19985. /* hard set q to 0 and test fail case */
  19986. mp_free(&key.q);
  19987. mp_init(&key.q);
  19988. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19989. mp_set(&key.q, 1);
  19990. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19991. #endif
  19992. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19993. wc_FreeDsaKey(&key);
  19994. wc_ShaFree(&sha);
  19995. #endif
  19996. return EXPECT_RESULT();
  19997. } /* END test_wc_DsaSign */
  19998. /*
  19999. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  20000. */
  20001. static int test_wc_DsaPublicPrivateKeyDecode(void)
  20002. {
  20003. EXPECT_DECLS;
  20004. #if !defined(NO_DSA)
  20005. DsaKey key;
  20006. word32 bytes = 0;
  20007. word32 idx = 0;
  20008. int ret = 0;
  20009. #ifdef USE_CERT_BUFFERS_1024
  20010. byte tmp[ONEK_BUF];
  20011. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  20012. bytes = sizeof_dsa_key_der_1024;
  20013. #elif defined(USE_CERT_BUFFERS_2048)
  20014. byte tmp[TWOK_BUF];
  20015. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  20016. bytes = sizeof_dsa_key_der_2048;
  20017. #else
  20018. byte tmp[TWOK_BUF];
  20019. XFILE fp = XBADFILE;
  20020. XMEMSET(tmp, 0, sizeof(tmp));
  20021. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  20022. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  20023. if (fp != XBADFILE)
  20024. XFCLOSE(fp);
  20025. #endif /* END USE_CERT_BUFFERS_1024 */
  20026. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  20027. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  20028. /* Test bad args. */
  20029. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  20030. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  20031. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  20032. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  20033. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  20034. wc_FreeDsaKey(&key);
  20035. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  20036. idx = 0; /* Reset */
  20037. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  20038. /* Test bad args. */
  20039. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  20040. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  20041. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  20042. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  20043. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  20044. wc_FreeDsaKey(&key);
  20045. #endif /* !NO_DSA */
  20046. return EXPECT_RESULT();
  20047. } /* END test_wc_DsaPublicPrivateKeyDecode */
  20048. /*
  20049. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  20050. */
  20051. static int test_wc_MakeDsaKey(void)
  20052. {
  20053. EXPECT_DECLS;
  20054. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  20055. DsaKey genKey;
  20056. WC_RNG rng;
  20057. XMEMSET(&genKey, 0, sizeof(genKey));
  20058. XMEMSET(&rng, 0, sizeof(rng));
  20059. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  20060. ExpectIntEQ(wc_InitRng(&rng), 0);
  20061. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  20062. /* Test bad args. */
  20063. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  20064. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  20065. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  20066. BAD_FUNC_ARG);
  20067. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  20068. /* Test bad args. */
  20069. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  20070. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  20071. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20072. wc_FreeDsaKey(&genKey);
  20073. #endif
  20074. return EXPECT_RESULT();
  20075. } /* END test_wc_MakeDsaKey */
  20076. /*
  20077. * Testing wc_DsaKeyToDer()
  20078. */
  20079. static int test_wc_DsaKeyToDer(void)
  20080. {
  20081. EXPECT_DECLS;
  20082. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  20083. DsaKey key;
  20084. word32 bytes;
  20085. word32 idx = 0;
  20086. #ifdef USE_CERT_BUFFERS_1024
  20087. byte tmp[ONEK_BUF];
  20088. byte der[ONEK_BUF];
  20089. XMEMSET(tmp, 0, sizeof(tmp));
  20090. XMEMSET(der, 0, sizeof(der));
  20091. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  20092. bytes = sizeof_dsa_key_der_1024;
  20093. #elif defined(USE_CERT_BUFFERS_2048)
  20094. byte tmp[TWOK_BUF];
  20095. byte der[TWOK_BUF];
  20096. XMEMSET(tmp, 0, sizeof(tmp));
  20097. XMEMSET(der, 0, sizeof(der));
  20098. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  20099. bytes = sizeof_dsa_key_der_2048;
  20100. #else
  20101. byte tmp[TWOK_BUF];
  20102. byte der[TWOK_BUF];
  20103. XFILE fp = XBADFILE;
  20104. XMEMSET(tmp, 0, sizeof(tmp));
  20105. XMEMSET(der, 0, sizeof(der));
  20106. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  20107. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  20108. if (fp != XBADFILE)
  20109. XFCLOSE(fp);
  20110. #endif /* END USE_CERT_BUFFERS_1024 */
  20111. XMEMSET(&key, 0, sizeof(DsaKey));
  20112. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  20113. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  20114. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  20115. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  20116. /* Test bad args. */
  20117. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  20118. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  20119. wc_FreeDsaKey(&key);
  20120. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  20121. return EXPECT_RESULT();
  20122. } /* END test_wc_DsaKeyToDer */
  20123. /*
  20124. * Testing wc_DsaKeyToPublicDer()
  20125. * (indirectly testing setDsaPublicKey())
  20126. */
  20127. static int test_wc_DsaKeyToPublicDer(void)
  20128. {
  20129. EXPECT_DECLS;
  20130. #ifndef HAVE_SELFTEST
  20131. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  20132. DsaKey key;
  20133. WC_RNG rng;
  20134. byte* der = NULL;
  20135. word32 sz = 0;
  20136. word32 idx = 0;
  20137. XMEMSET(&key, 0, sizeof(DsaKey));
  20138. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20139. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  20140. DYNAMIC_TYPE_TMP_BUFFER));
  20141. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  20142. ExpectIntEQ(wc_InitRng(&rng), 0);
  20143. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  20144. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  20145. ExpectIntGE(sz = (word32)wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  20146. wc_FreeDsaKey(&key);
  20147. idx = 0;
  20148. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  20149. /* Test without the SubjectPublicKeyInfo header */
  20150. ExpectIntGE(sz = (word32)wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  20151. wc_FreeDsaKey(&key);
  20152. idx = 0;
  20153. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  20154. /* Test bad args. */
  20155. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  20156. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  20157. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20158. wc_FreeDsaKey(&key);
  20159. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  20160. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  20161. #endif /* !HAVE_SELFTEST */
  20162. return EXPECT_RESULT();
  20163. } /* END test_wc_DsaKeyToPublicDer */
  20164. /*
  20165. * Testing wc_DsaImportParamsRaw()
  20166. */
  20167. static int test_wc_DsaImportParamsRaw(void)
  20168. {
  20169. EXPECT_DECLS;
  20170. #if !defined(NO_DSA)
  20171. DsaKey key;
  20172. /* [mod = L=1024, N=160], from CAVP KeyPair */
  20173. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  20174. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  20175. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  20176. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  20177. "47123188f8dc551054ee162b634d60f097f719076640e209"
  20178. "80a0093113a8bd73";
  20179. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  20180. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  20181. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  20182. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  20183. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  20184. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  20185. "76341a7e7d9";
  20186. /* invalid p and q parameters */
  20187. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  20188. const char* invalidQ = "96c5390a";
  20189. XMEMSET(&key, 0, sizeof(DsaKey));
  20190. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  20191. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  20192. /* test bad args */
  20193. /* null key struct */
  20194. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  20195. /* null param pointers */
  20196. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  20197. /* illegal p length */
  20198. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  20199. /* illegal q length */
  20200. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  20201. wc_FreeDsaKey(&key);
  20202. #endif
  20203. return EXPECT_RESULT();
  20204. } /* END test_wc_DsaImportParamsRaw */
  20205. /*
  20206. * Testing wc_DsaImportParamsRawCheck()
  20207. */
  20208. static int test_wc_DsaImportParamsRawCheck(void)
  20209. {
  20210. EXPECT_DECLS;
  20211. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20212. DsaKey key;
  20213. int trusted = 0;
  20214. /* [mod = L=1024, N=160], from CAVP KeyPair */
  20215. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  20216. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  20217. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  20218. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  20219. "47123188f8dc551054ee162b634d60f097f719076640e209"
  20220. "80a0093113a8bd73";
  20221. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  20222. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  20223. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  20224. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  20225. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  20226. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  20227. "76341a7e7d9";
  20228. /* invalid p and q parameters */
  20229. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  20230. const char* invalidQ = "96c5390a";
  20231. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  20232. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  20233. /* test bad args */
  20234. /* null key struct */
  20235. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  20236. BAD_FUNC_ARG);
  20237. /* null param pointers */
  20238. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  20239. NULL), BAD_FUNC_ARG);
  20240. /* illegal p length */
  20241. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  20242. BAD_FUNC_ARG);
  20243. /* illegal q length */
  20244. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  20245. BAD_FUNC_ARG);
  20246. wc_FreeDsaKey(&key);
  20247. #endif
  20248. return EXPECT_RESULT();
  20249. } /* END test_wc_DsaImportParamsRawCheck */
  20250. /*
  20251. * Testing wc_DsaExportParamsRaw()
  20252. */
  20253. static int test_wc_DsaExportParamsRaw(void)
  20254. {
  20255. EXPECT_DECLS;
  20256. #if !defined(NO_DSA)
  20257. DsaKey key;
  20258. /* [mod = L=1024, N=160], from CAVP KeyPair */
  20259. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  20260. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  20261. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  20262. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  20263. "47123188f8dc551054ee162b634d60f097f719076640e209"
  20264. "80a0093113a8bd73";
  20265. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  20266. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  20267. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  20268. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  20269. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  20270. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  20271. "76341a7e7d9";
  20272. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  20273. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  20274. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  20275. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  20276. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  20277. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  20278. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  20279. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  20280. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  20281. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  20282. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  20283. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  20284. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  20285. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  20286. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  20287. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  20288. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  20289. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  20290. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  20291. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  20292. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  20293. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  20294. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  20295. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  20296. byte pOut[MAX_DSA_PARAM_SIZE];
  20297. byte qOut[MAX_DSA_PARAM_SIZE];
  20298. byte gOut[MAX_DSA_PARAM_SIZE];
  20299. word32 pOutSz;
  20300. word32 qOutSz;
  20301. word32 gOutSz;
  20302. XMEMSET(&key, 0, sizeof(DsaKey));
  20303. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  20304. /* first test using imported raw parameters, for expected */
  20305. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  20306. pOutSz = sizeof(pOut);
  20307. qOutSz = sizeof(qOut);
  20308. gOutSz = sizeof(gOut);
  20309. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  20310. &gOutSz), 0);
  20311. /* validate exported parameters are correct */
  20312. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  20313. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  20314. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  20315. /* test bad args */
  20316. /* null key struct */
  20317. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  20318. &gOutSz), BAD_FUNC_ARG);
  20319. /* null output pointers */
  20320. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  20321. &gOutSz), LENGTH_ONLY_E);
  20322. /* null output size pointers */
  20323. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  20324. NULL), BAD_FUNC_ARG);
  20325. /* p output buffer size too small */
  20326. pOutSz = 1;
  20327. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  20328. &gOutSz), BUFFER_E);
  20329. pOutSz = sizeof(pOut);
  20330. /* q output buffer size too small */
  20331. qOutSz = 1;
  20332. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  20333. &gOutSz), BUFFER_E);
  20334. qOutSz = sizeof(qOut);
  20335. /* g output buffer size too small */
  20336. gOutSz = 1;
  20337. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  20338. &gOutSz), BUFFER_E);
  20339. wc_FreeDsaKey(&key);
  20340. #endif
  20341. return EXPECT_RESULT();
  20342. } /* END test_wc_DsaExportParamsRaw */
  20343. /*
  20344. * Testing wc_DsaExportKeyRaw()
  20345. */
  20346. static int test_wc_DsaExportKeyRaw(void)
  20347. {
  20348. EXPECT_DECLS;
  20349. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  20350. DsaKey key;
  20351. WC_RNG rng;
  20352. byte xOut[MAX_DSA_PARAM_SIZE];
  20353. byte yOut[MAX_DSA_PARAM_SIZE];
  20354. word32 xOutSz, yOutSz;
  20355. XMEMSET(&key, 0, sizeof(key));
  20356. XMEMSET(&rng, 0, sizeof(rng));
  20357. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  20358. ExpectIntEQ(wc_InitRng(&rng), 0);
  20359. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  20360. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  20361. /* try successful export */
  20362. xOutSz = sizeof(xOut);
  20363. yOutSz = sizeof(yOut);
  20364. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  20365. /* test bad args */
  20366. /* null key struct */
  20367. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  20368. BAD_FUNC_ARG);
  20369. /* null output pointers */
  20370. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  20371. LENGTH_ONLY_E);
  20372. /* null output size pointers */
  20373. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  20374. BAD_FUNC_ARG);
  20375. /* x output buffer size too small */
  20376. xOutSz = 1;
  20377. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  20378. BUFFER_E);
  20379. xOutSz = sizeof(xOut);
  20380. /* y output buffer size too small */
  20381. yOutSz = 1;
  20382. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  20383. BUFFER_E);
  20384. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20385. wc_FreeDsaKey(&key);
  20386. #endif
  20387. return EXPECT_RESULT();
  20388. } /* END test_wc_DsaExportParamsRaw */
  20389. /*
  20390. * Testing wc_ed25519_make_key().
  20391. */
  20392. static int test_wc_ed25519_make_key(void)
  20393. {
  20394. EXPECT_DECLS;
  20395. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  20396. ed25519_key key;
  20397. WC_RNG rng;
  20398. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  20399. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  20400. XMEMSET(&key, 0, sizeof(ed25519_key));
  20401. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20402. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20403. ExpectIntEQ(wc_InitRng(&rng), 0);
  20404. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, (word32)pubkey_sz),
  20405. ECC_PRIV_KEY_E);
  20406. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, (word32)pubkey_sz),
  20407. ECC_PRIV_KEY_E);
  20408. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20409. /* Test bad args. */
  20410. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  20411. BAD_FUNC_ARG);
  20412. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  20413. BAD_FUNC_ARG);
  20414. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  20415. BAD_FUNC_ARG);
  20416. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  20417. BAD_FUNC_ARG);
  20418. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20419. wc_ed25519_free(&key);
  20420. #endif
  20421. return EXPECT_RESULT();
  20422. } /* END test_wc_ed25519_make_key */
  20423. /*
  20424. * Testing wc_ed25519_init()
  20425. */
  20426. static int test_wc_ed25519_init(void)
  20427. {
  20428. EXPECT_DECLS;
  20429. #if defined(HAVE_ED25519)
  20430. ed25519_key key;
  20431. XMEMSET(&key, 0, sizeof(ed25519_key));
  20432. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20433. /* Test bad args. */
  20434. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  20435. wc_ed25519_free(&key);
  20436. #endif
  20437. return EXPECT_RESULT();
  20438. } /* END test_wc_ed25519_init */
  20439. /*
  20440. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  20441. */
  20442. static int test_wc_ed25519_sign_msg(void)
  20443. {
  20444. EXPECT_DECLS;
  20445. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  20446. WC_RNG rng;
  20447. ed25519_key key;
  20448. byte msg[] = "Everybody gets Friday off.\n";
  20449. byte sig[ED25519_SIG_SIZE+1];
  20450. word32 msglen = sizeof(msg);
  20451. word32 siglen = ED25519_SIG_SIZE;
  20452. word32 badSigLen = ED25519_SIG_SIZE - 1;
  20453. #ifdef HAVE_ED25519_VERIFY
  20454. int verify_ok = 0; /*1 = Verify success.*/
  20455. #endif
  20456. /* Initialize stack variables. */
  20457. XMEMSET(&key, 0, sizeof(ed25519_key));
  20458. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20459. XMEMSET(sig, 0, sizeof(sig));
  20460. /* Initialize key. */
  20461. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20462. ExpectIntEQ(wc_InitRng(&rng), 0);
  20463. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20464. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  20465. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  20466. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  20467. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  20468. /* Test bad args. */
  20469. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  20470. BAD_FUNC_ARG);
  20471. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  20472. BAD_FUNC_ARG);
  20473. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  20474. BAD_FUNC_ARG);
  20475. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  20476. BAD_FUNC_ARG);
  20477. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  20478. BUFFER_E);
  20479. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  20480. badSigLen -= 1;
  20481. #ifdef HAVE_ED25519_VERIFY
  20482. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20483. &key), 0);
  20484. ExpectIntEQ(verify_ok, 1);
  20485. /* Test bad args. */
  20486. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  20487. &verify_ok, &key), BAD_FUNC_ARG);
  20488. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  20489. &verify_ok, &key), BAD_FUNC_ARG);
  20490. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20491. &key), BAD_FUNC_ARG);
  20492. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  20493. &key), BAD_FUNC_ARG);
  20494. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  20495. BAD_FUNC_ARG);
  20496. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20497. NULL), BAD_FUNC_ARG);
  20498. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  20499. &key), BAD_FUNC_ARG);
  20500. #endif /* Verify. */
  20501. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20502. wc_ed25519_free(&key);
  20503. #endif
  20504. return EXPECT_RESULT();
  20505. } /* END test_wc_ed25519_sign_msg */
  20506. /*
  20507. * Testing wc_ed25519_import_public()
  20508. */
  20509. static int test_wc_ed25519_import_public(void)
  20510. {
  20511. EXPECT_DECLS;
  20512. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20513. ed25519_key pubKey;
  20514. WC_RNG rng;
  20515. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  20516. word32 inlen = sizeof(in);
  20517. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  20518. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20519. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  20520. ExpectIntEQ(wc_InitRng(&rng), 0);
  20521. #ifdef HAVE_ED25519_MAKE_KEY
  20522. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  20523. #endif
  20524. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  20525. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20526. /* Test bad args. */
  20527. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20528. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20529. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20530. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20531. wc_ed25519_free(&pubKey);
  20532. #endif
  20533. return EXPECT_RESULT();
  20534. } /* END wc_ed25519_import_public */
  20535. /*
  20536. * Testing wc_ed25519_import_private_key()
  20537. */
  20538. static int test_wc_ed25519_import_private_key(void)
  20539. {
  20540. EXPECT_DECLS;
  20541. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20542. ed25519_key key;
  20543. WC_RNG rng;
  20544. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  20545. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  20546. word32 privKeySz = sizeof(privKey);
  20547. word32 pubKeySz = sizeof(pubKey);
  20548. #ifdef HAVE_ED25519_KEY_EXPORT
  20549. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20550. word32 bothKeysSz = sizeof(bothKeys);
  20551. #endif
  20552. XMEMSET(&key, 0, sizeof(ed25519_key));
  20553. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20554. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20555. ExpectIntEQ(wc_InitRng(&rng), 0);
  20556. #ifdef HAVE_ED25519_MAKE_KEY
  20557. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20558. #endif
  20559. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  20560. pubKeySz, &key, 1), 0);
  20561. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20562. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20563. #ifdef HAVE_ED25519_KEY_EXPORT
  20564. PRIVATE_KEY_UNLOCK();
  20565. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  20566. PRIVATE_KEY_LOCK();
  20567. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20568. &key, 1), 0);
  20569. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20570. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20571. #endif
  20572. /* Test bad args. */
  20573. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20574. &key), BAD_FUNC_ARG);
  20575. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  20576. pubKeySz, &key), BAD_FUNC_ARG);
  20577. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20578. pubKeySz, NULL), BAD_FUNC_ARG);
  20579. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  20580. pubKeySz, &key), BAD_FUNC_ARG);
  20581. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20582. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20583. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  20584. &key), BAD_FUNC_ARG);
  20585. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20586. wc_ed25519_free(&key);
  20587. #endif
  20588. return EXPECT_RESULT();
  20589. } /* END test_wc_ed25519_import_private_key */
  20590. /*
  20591. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20592. */
  20593. static int test_wc_ed25519_export(void)
  20594. {
  20595. EXPECT_DECLS;
  20596. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20597. ed25519_key key;
  20598. WC_RNG rng;
  20599. byte priv[ED25519_PRV_KEY_SIZE];
  20600. byte pub[ED25519_PUB_KEY_SIZE];
  20601. word32 privSz = sizeof(priv);
  20602. word32 pubSz = sizeof(pub);
  20603. #ifndef HAVE_ED25519_MAKE_KEY
  20604. const byte privKey[] = {
  20605. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20606. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20607. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20608. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20609. };
  20610. const byte pubKey[] = {
  20611. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20612. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20613. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20614. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20615. };
  20616. #endif
  20617. XMEMSET(&key, 0, sizeof(ed25519_key));
  20618. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20619. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20620. ExpectIntEQ(wc_InitRng(&rng), 0);
  20621. #ifdef HAVE_ED25519_MAKE_KEY
  20622. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20623. #else
  20624. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20625. pubKey, sizeof(pubKey), &key, 1), 0);
  20626. #endif
  20627. PRIVATE_KEY_UNLOCK();
  20628. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  20629. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  20630. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20631. /* Test bad args. */
  20632. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20633. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20634. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20635. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  20636. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  20637. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20638. /* Test bad args. */
  20639. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  20640. BAD_FUNC_ARG);
  20641. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  20642. BAD_FUNC_ARG);
  20643. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  20644. BAD_FUNC_ARG);
  20645. PRIVATE_KEY_LOCK();
  20646. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20647. wc_ed25519_free(&key);
  20648. #endif
  20649. return EXPECT_RESULT();
  20650. } /* END test_wc_ed25519_export */
  20651. /*
  20652. * Testing wc_ed25519_size()
  20653. */
  20654. static int test_wc_ed25519_size(void)
  20655. {
  20656. EXPECT_DECLS;
  20657. #if defined(HAVE_ED25519)
  20658. ed25519_key key;
  20659. WC_RNG rng;
  20660. #ifndef HAVE_ED25519_MAKE_KEY
  20661. const byte privKey[] = {
  20662. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20663. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20664. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20665. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20666. };
  20667. const byte pubKey[] = {
  20668. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20669. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20670. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20671. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20672. };
  20673. #endif
  20674. XMEMSET(&key, 0, sizeof(ed25519_key));
  20675. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20676. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20677. ExpectIntEQ(wc_InitRng(&rng), 0);
  20678. #ifdef HAVE_ED25519_MAKE_KEY
  20679. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20680. #else
  20681. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20682. pubKey, sizeof(pubKey), &key, 1), 0);
  20683. #endif
  20684. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  20685. /* Test bad args. */
  20686. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  20687. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  20688. /* Test bad args. */
  20689. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  20690. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  20691. /* Test bad args. */
  20692. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  20693. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  20694. /* Test bad args. */
  20695. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  20696. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20697. wc_ed25519_free(&key);
  20698. #endif
  20699. return EXPECT_RESULT();
  20700. } /* END test_wc_ed25519_size */
  20701. /*
  20702. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20703. */
  20704. static int test_wc_ed25519_exportKey(void)
  20705. {
  20706. EXPECT_DECLS;
  20707. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20708. WC_RNG rng;
  20709. ed25519_key key;
  20710. byte priv[ED25519_PRV_KEY_SIZE];
  20711. byte pub[ED25519_PUB_KEY_SIZE];
  20712. byte privOnly[ED25519_PRV_KEY_SIZE];
  20713. word32 privSz = sizeof(priv);
  20714. word32 pubSz = sizeof(pub);
  20715. word32 privOnlySz = sizeof(privOnly);
  20716. #ifndef HAVE_ED25519_MAKE_KEY
  20717. const byte privKey[] = {
  20718. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20719. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20720. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20721. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20722. };
  20723. const byte pubKey[] = {
  20724. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20725. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20726. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20727. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20728. };
  20729. #endif
  20730. XMEMSET(&key, 0, sizeof(ed25519_key));
  20731. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20732. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20733. ExpectIntEQ(wc_InitRng(&rng), 0);
  20734. #ifdef HAVE_ED25519_MAKE_KEY
  20735. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20736. #else
  20737. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20738. pubKey, sizeof(pubKey), &key, 1), 0);
  20739. #endif
  20740. PRIVATE_KEY_UNLOCK();
  20741. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  20742. /* Test bad args. */
  20743. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  20744. BAD_FUNC_ARG);
  20745. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  20746. BAD_FUNC_ARG);
  20747. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20748. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20749. /* Test bad args. */
  20750. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  20751. BAD_FUNC_ARG);
  20752. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  20753. BAD_FUNC_ARG);
  20754. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  20755. BAD_FUNC_ARG);
  20756. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  20757. BAD_FUNC_ARG);
  20758. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  20759. BAD_FUNC_ARG);
  20760. PRIVATE_KEY_LOCK();
  20761. /* Cross check output. */
  20762. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20763. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20764. wc_ed25519_free(&key);
  20765. #endif
  20766. return EXPECT_RESULT();
  20767. } /* END test_wc_ed25519_exportKey */
  20768. /*
  20769. * Testing wc_Ed25519PublicKeyToDer
  20770. */
  20771. static int test_wc_Ed25519PublicKeyToDer(void)
  20772. {
  20773. EXPECT_DECLS;
  20774. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20775. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20776. ed25519_key key;
  20777. byte derBuf[1024];
  20778. XMEMSET(&key, 0, sizeof(ed25519_key));
  20779. /* Test bad args */
  20780. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20781. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20782. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20783. wc_ed25519_free(&key);
  20784. /* Test good args */
  20785. if (EXPECT_SUCCESS()) {
  20786. WC_RNG rng;
  20787. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20788. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20789. ExpectIntEQ(wc_InitRng(&rng), 0);
  20790. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20791. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20792. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20793. wc_ed25519_free(&key);
  20794. }
  20795. #endif
  20796. return EXPECT_RESULT();
  20797. } /* END testing wc_Ed25519PublicKeyToDer */
  20798. /*
  20799. * Testing wc_curve25519_init and wc_curve25519_free.
  20800. */
  20801. static int test_wc_curve25519_init(void)
  20802. {
  20803. EXPECT_DECLS;
  20804. #if defined(HAVE_CURVE25519)
  20805. curve25519_key key;
  20806. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20807. /* Test bad args for wc_curve25519_init */
  20808. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  20809. /* Test good args for wc_curve_25519_free */
  20810. wc_curve25519_free(&key);
  20811. /* Test bad args for wc_curve25519 free. */
  20812. wc_curve25519_free(NULL);
  20813. #endif
  20814. return EXPECT_RESULT();
  20815. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20816. /*
  20817. * Testing test_wc_curve25519_size.
  20818. */
  20819. static int test_wc_curve25519_size(void)
  20820. {
  20821. EXPECT_DECLS;
  20822. #if defined(HAVE_CURVE25519)
  20823. curve25519_key key;
  20824. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20825. /* Test good args for wc_curve25519_size */
  20826. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20827. /* Test bad args for wc_curve25519_size */
  20828. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  20829. wc_curve25519_free(&key);
  20830. #endif
  20831. return EXPECT_RESULT();
  20832. } /* END test_wc_curve25519_size*/
  20833. /*
  20834. * Testing test_wc_curve25519_export_key_raw().
  20835. */
  20836. static int test_wc_curve25519_export_key_raw(void)
  20837. {
  20838. EXPECT_DECLS;
  20839. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20840. curve25519_key key;
  20841. WC_RNG rng;
  20842. byte privateKey[CURVE25519_KEYSIZE];
  20843. byte publicKey[CURVE25519_KEYSIZE];
  20844. word32 prvkSz;
  20845. word32 pubkSz;
  20846. byte prik[CURVE25519_KEYSIZE];
  20847. byte pubk[CURVE25519_KEYSIZE];
  20848. word32 prksz;
  20849. word32 pbksz;
  20850. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20851. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20852. ExpectIntEQ(wc_InitRng(&rng), 0);
  20853. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20854. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20855. prvkSz = CURVE25519_KEYSIZE;
  20856. pubkSz = CURVE25519_KEYSIZE;
  20857. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  20858. publicKey, &pubkSz), BAD_FUNC_ARG);
  20859. prvkSz = CURVE25519_KEYSIZE;
  20860. pubkSz = CURVE25519_KEYSIZE;
  20861. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  20862. &pubkSz), BAD_FUNC_ARG);
  20863. prvkSz = CURVE25519_KEYSIZE;
  20864. pubkSz = CURVE25519_KEYSIZE;
  20865. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  20866. publicKey, &pubkSz), BAD_FUNC_ARG);
  20867. /* prvkSz = CURVE25519_KEYSIZE; */
  20868. pubkSz = CURVE25519_KEYSIZE;
  20869. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20870. NULL, &pubkSz), BAD_FUNC_ARG);
  20871. prvkSz = CURVE25519_KEYSIZE;
  20872. pubkSz = CURVE25519_KEYSIZE;
  20873. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20874. publicKey, NULL), BAD_FUNC_ARG);
  20875. /* cross-testing */
  20876. prksz = CURVE25519_KEYSIZE;
  20877. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  20878. pbksz = CURVE25519_KEYSIZE;
  20879. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  20880. prvkSz = CURVE25519_KEYSIZE;
  20881. /* pubkSz = CURVE25519_KEYSIZE; */
  20882. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20883. publicKey, &pubkSz), 0);
  20884. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20885. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20886. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20887. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20888. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20889. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20890. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20891. wc_curve25519_free(&key);
  20892. #endif
  20893. return EXPECT_RESULT();
  20894. } /* end of test_wc_curve25519_export_key_raw */
  20895. /*
  20896. * Testing test_wc_curve25519_export_key_raw_ex().
  20897. */
  20898. static int test_wc_curve25519_export_key_raw_ex(void)
  20899. {
  20900. EXPECT_DECLS;
  20901. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20902. curve25519_key key;
  20903. WC_RNG rng;
  20904. byte privateKey[CURVE25519_KEYSIZE];
  20905. byte publicKey[CURVE25519_KEYSIZE];
  20906. word32 prvkSz;
  20907. word32 pubkSz;
  20908. byte prik[CURVE25519_KEYSIZE];
  20909. byte pubk[CURVE25519_KEYSIZE];
  20910. word32 prksz;
  20911. word32 pbksz;
  20912. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20913. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20914. ExpectIntEQ(wc_InitRng(&rng), 0);
  20915. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20916. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20917. prvkSz = CURVE25519_KEYSIZE;
  20918. pubkSz = CURVE25519_KEYSIZE;
  20919. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20920. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20921. prvkSz = CURVE25519_KEYSIZE;
  20922. pubkSz = CURVE25519_KEYSIZE;
  20923. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20924. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20925. prvkSz = CURVE25519_KEYSIZE;
  20926. pubkSz = CURVE25519_KEYSIZE;
  20927. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20928. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20929. /* prvkSz = CURVE25519_KEYSIZE; */
  20930. pubkSz = CURVE25519_KEYSIZE;
  20931. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20932. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20933. prvkSz = CURVE25519_KEYSIZE;
  20934. pubkSz = CURVE25519_KEYSIZE;
  20935. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20936. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20937. prvkSz = CURVE25519_KEYSIZE;
  20938. /* pubkSz = CURVE25519_KEYSIZE; */
  20939. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20940. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20941. prvkSz = CURVE25519_KEYSIZE;
  20942. pubkSz = CURVE25519_KEYSIZE;
  20943. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20944. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20945. prvkSz = CURVE25519_KEYSIZE;
  20946. pubkSz = CURVE25519_KEYSIZE;
  20947. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20948. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20949. /* prvkSz = CURVE25519_KEYSIZE; */
  20950. pubkSz = CURVE25519_KEYSIZE;
  20951. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20952. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20953. prvkSz = CURVE25519_KEYSIZE;
  20954. pubkSz = CURVE25519_KEYSIZE;
  20955. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20956. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20957. /* illegal value for endian */
  20958. prvkSz = CURVE25519_KEYSIZE;
  20959. /* pubkSz = CURVE25519_KEYSIZE; */
  20960. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20961. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20962. /* cross-testing */
  20963. prksz = CURVE25519_KEYSIZE;
  20964. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20965. pbksz = CURVE25519_KEYSIZE;
  20966. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20967. prvkSz = CURVE25519_KEYSIZE;
  20968. /* pubkSz = CURVE25519_KEYSIZE; */
  20969. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20970. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20971. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20972. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20973. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20974. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20975. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20976. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20977. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20978. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20979. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20980. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20981. /* try once with another endian */
  20982. prvkSz = CURVE25519_KEYSIZE;
  20983. pubkSz = CURVE25519_KEYSIZE;
  20984. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20985. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20986. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20987. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20988. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20989. wc_curve25519_free(&key);
  20990. #endif
  20991. return EXPECT_RESULT();
  20992. } /* end of test_wc_curve25519_export_key_raw_ex */
  20993. /*
  20994. * Testing wc_curve25519_make_key
  20995. */
  20996. static int test_wc_curve25519_make_key(void)
  20997. {
  20998. EXPECT_DECLS;
  20999. #if defined(HAVE_CURVE25519)
  21000. curve25519_key key;
  21001. WC_RNG rng;
  21002. int keysize;
  21003. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21004. ExpectIntEQ(wc_curve25519_init(&key), 0);
  21005. ExpectIntEQ(wc_InitRng(&rng), 0);
  21006. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  21007. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  21008. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  21009. /* test bad cases*/
  21010. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21011. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21012. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21013. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21014. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21015. wc_curve25519_free(&key);
  21016. #endif
  21017. return EXPECT_RESULT();
  21018. } /* END test_wc_curve25519_make_key*/
  21019. /*
  21020. * Testing wc_curve25519_shared_secret_ex
  21021. */
  21022. static int test_wc_curve25519_shared_secret_ex(void)
  21023. {
  21024. EXPECT_DECLS;
  21025. #if defined(HAVE_CURVE25519)
  21026. curve25519_key private_key;
  21027. curve25519_key public_key;
  21028. WC_RNG rng;
  21029. byte out[CURVE25519_KEYSIZE];
  21030. word32 outLen = sizeof(out);
  21031. int endian = EC25519_BIG_ENDIAN;
  21032. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  21033. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  21034. ExpectIntEQ(wc_InitRng(&rng), 0);
  21035. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  21036. 0);
  21037. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  21038. 0);
  21039. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  21040. &outLen, endian), 0);
  21041. /* test bad cases*/
  21042. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21043. BAD_FUNC_ARG);
  21044. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  21045. endian), BAD_FUNC_ARG);
  21046. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  21047. endian), BAD_FUNC_ARG);
  21048. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  21049. &outLen, endian), BAD_FUNC_ARG);
  21050. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  21051. NULL, endian), BAD_FUNC_ARG);
  21052. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  21053. * increasing to 0x8f checks for error being returned*/
  21054. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  21055. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  21056. &outLen, endian), ECC_BAD_ARG_E);
  21057. outLen = outLen - 2;
  21058. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  21059. &outLen, endian), BAD_FUNC_ARG);
  21060. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21061. wc_curve25519_free(&private_key);
  21062. wc_curve25519_free(&public_key);
  21063. #endif
  21064. return EXPECT_RESULT();
  21065. } /* END test_wc_curve25519_shared_secret_ex*/
  21066. /*
  21067. * Testing wc_curve25519_make_pub
  21068. */
  21069. static int test_wc_curve25519_make_pub(void)
  21070. {
  21071. EXPECT_DECLS;
  21072. #ifdef HAVE_CURVE25519
  21073. curve25519_key key;
  21074. WC_RNG rng;
  21075. byte out[CURVE25519_KEYSIZE];
  21076. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21077. ExpectIntEQ(wc_curve25519_init(&key), 0);
  21078. ExpectIntEQ(wc_InitRng(&rng), 0);
  21079. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  21080. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  21081. (int)sizeof(key.k), key.k), 0);
  21082. /* test bad cases*/
  21083. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  21084. (int)sizeof out, out), ECC_BAD_ARG_E);
  21085. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  21086. NULL), ECC_BAD_ARG_E);
  21087. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  21088. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  21089. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  21090. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  21091. /* verify clamping test */
  21092. key.k[0] |= ~248;
  21093. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  21094. key.k), ECC_BAD_ARG_E);
  21095. key.k[0] &= 248;
  21096. /* repeat the expected-to-succeed test. */
  21097. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  21098. key.k), 0);
  21099. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21100. wc_curve25519_free(&key);
  21101. #endif
  21102. return EXPECT_RESULT();
  21103. } /* END test_wc_curve25519_make_pub */
  21104. /*
  21105. * Testing test_wc_curve25519_export_public_ex
  21106. */
  21107. static int test_wc_curve25519_export_public_ex(void)
  21108. {
  21109. EXPECT_DECLS;
  21110. #if defined(HAVE_CURVE25519)
  21111. curve25519_key key;
  21112. WC_RNG rng;
  21113. byte out[CURVE25519_KEYSIZE];
  21114. word32 outLen = sizeof(out);
  21115. int endian = EC25519_BIG_ENDIAN;
  21116. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21117. ExpectIntEQ(wc_curve25519_init(&key), 0);
  21118. ExpectIntEQ(wc_InitRng(&rng), 0);
  21119. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  21120. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  21121. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  21122. /* test bad cases*/
  21123. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  21124. BAD_FUNC_ARG);
  21125. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  21126. BAD_FUNC_ARG);
  21127. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  21128. BAD_FUNC_ARG);
  21129. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  21130. BAD_FUNC_ARG);
  21131. outLen = outLen - 2;
  21132. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  21133. ECC_BAD_ARG_E);
  21134. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21135. wc_curve25519_free(&key);
  21136. #endif
  21137. return EXPECT_RESULT();
  21138. } /* END test_wc_curve25519_export_public_ex*/
  21139. /*
  21140. * Testing test_wc_curve25519_import_private_raw_ex
  21141. */
  21142. static int test_wc_curve25519_import_private_raw_ex(void)
  21143. {
  21144. EXPECT_DECLS;
  21145. #if defined(HAVE_CURVE25519)
  21146. curve25519_key key;
  21147. WC_RNG rng;
  21148. byte priv[CURVE25519_KEYSIZE];
  21149. byte pub[CURVE25519_KEYSIZE];
  21150. word32 privSz = sizeof(priv);
  21151. word32 pubSz = sizeof(pub);
  21152. int endian = EC25519_BIG_ENDIAN;
  21153. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21154. ExpectIntEQ(wc_curve25519_init(&key), 0);
  21155. ExpectIntEQ(wc_InitRng(&rng), 0);
  21156. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  21157. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  21158. endian), 0);
  21159. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  21160. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  21161. &key, endian), 0);
  21162. /* test bad cases*/
  21163. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  21164. endian), BAD_FUNC_ARG);
  21165. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21166. &key, endian), BAD_FUNC_ARG);
  21167. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21168. &key, endian), BAD_FUNC_ARG);
  21169. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  21170. NULL, endian), BAD_FUNC_ARG);
  21171. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  21172. &key, endian), ECC_BAD_ARG_E);
  21173. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  21174. &key, endian), ECC_BAD_ARG_E);
  21175. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  21176. &key, EC25519_LITTLE_ENDIAN), 0);
  21177. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21178. wc_curve25519_free(&key);
  21179. #endif
  21180. return EXPECT_RESULT();
  21181. } /* END test_wc_curve25519_import_private_raw_ex*/
  21182. /*
  21183. * Testing test_wc_curve25519_import_private
  21184. */
  21185. static int test_wc_curve25519_import_private(void)
  21186. {
  21187. EXPECT_DECLS;
  21188. #if defined(HAVE_CURVE25519)
  21189. curve25519_key key;
  21190. WC_RNG rng;
  21191. byte priv[CURVE25519_KEYSIZE];
  21192. word32 privSz = sizeof(priv);
  21193. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21194. ExpectIntEQ(wc_curve25519_init(&key), 0);
  21195. ExpectIntEQ(wc_InitRng(&rng), 0);
  21196. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  21197. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  21198. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  21199. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21200. wc_curve25519_free(&key);
  21201. #endif
  21202. return EXPECT_RESULT();
  21203. } /* END test_wc_curve25519_import*/
  21204. /*
  21205. * Testing test_wc_curve25519_export_private_raw_ex
  21206. */
  21207. static int test_wc_curve25519_export_private_raw_ex(void)
  21208. {
  21209. EXPECT_DECLS;
  21210. #if defined(HAVE_CURVE25519)
  21211. curve25519_key key;
  21212. byte out[CURVE25519_KEYSIZE];
  21213. word32 outLen = sizeof(out);
  21214. int endian = EC25519_BIG_ENDIAN;
  21215. ExpectIntEQ(wc_curve25519_init(&key), 0);
  21216. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  21217. 0);
  21218. /* test bad cases*/
  21219. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  21220. BAD_FUNC_ARG);
  21221. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  21222. BAD_FUNC_ARG);
  21223. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  21224. endian), BAD_FUNC_ARG);
  21225. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  21226. BAD_FUNC_ARG);
  21227. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  21228. EC25519_LITTLE_ENDIAN), 0);
  21229. outLen = outLen - 2;
  21230. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  21231. ECC_BAD_ARG_E);
  21232. wc_curve25519_free(&key);
  21233. #endif
  21234. return EXPECT_RESULT();
  21235. } /* END test_wc_curve25519_export_private_raw_ex*/
  21236. /*
  21237. * Testing wc_ed448_make_key().
  21238. */
  21239. static int test_wc_ed448_make_key(void)
  21240. {
  21241. EXPECT_DECLS;
  21242. #if defined(HAVE_ED448)
  21243. ed448_key key;
  21244. WC_RNG rng;
  21245. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  21246. XMEMSET(&key, 0, sizeof(ed448_key));
  21247. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21248. ExpectIntEQ(wc_ed448_init(&key), 0);
  21249. ExpectIntEQ(wc_InitRng(&rng), 0);
  21250. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  21251. ECC_PRIV_KEY_E);
  21252. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21253. /* Test bad args. */
  21254. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  21255. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  21256. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  21257. BAD_FUNC_ARG);
  21258. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  21259. BAD_FUNC_ARG);
  21260. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21261. wc_ed448_free(&key);
  21262. #endif
  21263. return EXPECT_RESULT();
  21264. } /* END test_wc_ed448_make_key */
  21265. /*
  21266. * Testing wc_ed448_init()
  21267. */
  21268. static int test_wc_ed448_init(void)
  21269. {
  21270. EXPECT_DECLS;
  21271. #if defined(HAVE_ED448)
  21272. ed448_key key;
  21273. XMEMSET(&key, 0, sizeof(ed448_key));
  21274. ExpectIntEQ(wc_ed448_init(&key), 0);
  21275. /* Test bad args. */
  21276. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  21277. wc_ed448_free(&key);
  21278. #endif
  21279. return EXPECT_RESULT();
  21280. } /* END test_wc_ed448_init */
  21281. /*
  21282. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  21283. */
  21284. static int test_wc_ed448_sign_msg(void)
  21285. {
  21286. EXPECT_DECLS;
  21287. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  21288. ed448_key key;
  21289. WC_RNG rng;
  21290. byte msg[] = "Everybody gets Friday off.\n";
  21291. byte sig[ED448_SIG_SIZE];
  21292. word32 msglen = sizeof(msg);
  21293. word32 siglen = sizeof(sig);
  21294. word32 badSigLen = sizeof(sig) - 1;
  21295. #ifdef HAVE_ED448_VERIFY
  21296. int verify_ok = 0; /*1 = Verify success.*/
  21297. #endif
  21298. /* Initialize stack variables. */
  21299. XMEMSET(&key, 0, sizeof(ed448_key));
  21300. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21301. XMEMSET(sig, 0, siglen);
  21302. /* Initialize key. */
  21303. ExpectIntEQ(wc_ed448_init(&key), 0);
  21304. ExpectIntEQ(wc_InitRng(&rng), 0);
  21305. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21306. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  21307. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  21308. /* Test bad args. */
  21309. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  21310. BAD_FUNC_ARG);
  21311. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  21312. BAD_FUNC_ARG);
  21313. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  21314. BAD_FUNC_ARG);
  21315. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  21316. BAD_FUNC_ARG);
  21317. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  21318. BUFFER_E);
  21319. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  21320. badSigLen -= 1;
  21321. #ifdef HAVE_ED448_VERIFY
  21322. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  21323. NULL, 0), 0);
  21324. ExpectIntEQ(verify_ok, 1);
  21325. /* Test bad args. */
  21326. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  21327. &key, NULL, 0), BAD_FUNC_ARG);
  21328. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  21329. &key, NULL, 0), BAD_FUNC_ARG);
  21330. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  21331. &key, NULL, 0), BAD_FUNC_ARG);
  21332. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  21333. &key, NULL, 0), BAD_FUNC_ARG);
  21334. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  21335. &key, NULL, 0), BAD_FUNC_ARG);
  21336. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  21337. NULL, NULL, 0), BAD_FUNC_ARG);
  21338. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  21339. &key, NULL, 0), BAD_FUNC_ARG);
  21340. #endif /* Verify. */
  21341. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21342. wc_ed448_free(&key);
  21343. #endif
  21344. return EXPECT_RESULT();
  21345. } /* END test_wc_ed448_sign_msg */
  21346. /*
  21347. * Testing wc_ed448_import_public()
  21348. */
  21349. static int test_wc_ed448_import_public(void)
  21350. {
  21351. EXPECT_DECLS;
  21352. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21353. ed448_key pubKey;
  21354. WC_RNG rng;
  21355. const byte in[] =
  21356. "Ed448PublicKeyUnitTest.................................\n";
  21357. word32 inlen = sizeof(in);
  21358. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  21359. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21360. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  21361. ExpectIntEQ(wc_InitRng(&rng), 0);
  21362. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  21363. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  21364. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  21365. /* Test bad args. */
  21366. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  21367. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  21368. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  21369. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21370. wc_ed448_free(&pubKey);
  21371. #endif
  21372. return EXPECT_RESULT();
  21373. } /* END wc_ed448_import_public */
  21374. /*
  21375. * Testing wc_ed448_import_private_key()
  21376. */
  21377. static int test_wc_ed448_import_private_key(void)
  21378. {
  21379. EXPECT_DECLS;
  21380. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21381. ed448_key key;
  21382. WC_RNG rng;
  21383. const byte privKey[] =
  21384. "Ed448PrivateKeyUnitTest................................\n";
  21385. const byte pubKey[] =
  21386. "Ed448PublicKeyUnitTest.................................\n";
  21387. word32 privKeySz = sizeof(privKey);
  21388. word32 pubKeySz = sizeof(pubKey);
  21389. #ifdef HAVE_ED448_KEY_EXPORT
  21390. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  21391. word32 bothKeysSz = sizeof(bothKeys);
  21392. #endif
  21393. XMEMSET(&key, 0, sizeof(ed448_key));
  21394. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21395. ExpectIntEQ(wc_ed448_init(&key), 0);
  21396. ExpectIntEQ(wc_InitRng(&rng), 0);
  21397. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21398. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  21399. pubKeySz, &key, 1), 0);
  21400. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  21401. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  21402. #ifdef HAVE_ED448_KEY_EXPORT
  21403. PRIVATE_KEY_UNLOCK();
  21404. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  21405. PRIVATE_KEY_LOCK();
  21406. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  21407. &key, 1), 0);
  21408. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  21409. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  21410. #endif
  21411. /* Test bad args. */
  21412. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  21413. &key), BAD_FUNC_ARG);
  21414. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  21415. &key), BAD_FUNC_ARG);
  21416. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21417. pubKeySz, NULL), BAD_FUNC_ARG);
  21418. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  21419. pubKeySz, &key), BAD_FUNC_ARG);
  21420. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21421. pubKeySz - 1, &key), BAD_FUNC_ARG);
  21422. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  21423. BAD_FUNC_ARG);
  21424. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21425. wc_ed448_free(&key);
  21426. #endif
  21427. return EXPECT_RESULT();
  21428. } /* END test_wc_ed448_import_private_key */
  21429. /*
  21430. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  21431. */
  21432. static int test_wc_ed448_export(void)
  21433. {
  21434. EXPECT_DECLS;
  21435. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21436. ed448_key key;
  21437. WC_RNG rng;
  21438. byte priv[ED448_PRV_KEY_SIZE];
  21439. byte pub[ED448_PUB_KEY_SIZE];
  21440. word32 privSz = sizeof(priv);
  21441. word32 pubSz = sizeof(pub);
  21442. XMEMSET(&key, 0, sizeof(ed448_key));
  21443. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21444. ExpectIntEQ(wc_ed448_init(&key), 0);
  21445. ExpectIntEQ(wc_InitRng(&rng), 0);
  21446. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21447. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  21448. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  21449. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  21450. /* Test bad args. */
  21451. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  21452. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  21453. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  21454. PRIVATE_KEY_UNLOCK();
  21455. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  21456. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  21457. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  21458. /* Test bad args. */
  21459. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  21460. BAD_FUNC_ARG);
  21461. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  21462. BAD_FUNC_ARG);
  21463. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  21464. PRIVATE_KEY_LOCK();
  21465. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21466. wc_ed448_free(&key);
  21467. #endif
  21468. return EXPECT_RESULT();
  21469. } /* END test_wc_ed448_export */
  21470. /*
  21471. * Testing wc_ed448_size()
  21472. */
  21473. static int test_wc_ed448_size(void)
  21474. {
  21475. EXPECT_DECLS;
  21476. #if defined(HAVE_ED448)
  21477. ed448_key key;
  21478. WC_RNG rng;
  21479. XMEMSET(&key, 0, sizeof(ed448_key));
  21480. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21481. ExpectIntEQ(wc_ed448_init(&key), 0);
  21482. ExpectIntEQ(wc_InitRng(&rng), 0);
  21483. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21484. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  21485. /* Test bad args. */
  21486. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  21487. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  21488. /* Test bad args. */
  21489. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  21490. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  21491. /* Test bad args. */
  21492. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  21493. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  21494. /* Test bad args. */
  21495. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  21496. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21497. wc_ed448_free(&key);
  21498. #endif
  21499. return EXPECT_RESULT();
  21500. } /* END test_wc_ed448_size */
  21501. /*
  21502. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21503. */
  21504. static int test_wc_ed448_exportKey(void)
  21505. {
  21506. EXPECT_DECLS;
  21507. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21508. ed448_key key;
  21509. WC_RNG rng;
  21510. byte priv[ED448_PRV_KEY_SIZE];
  21511. byte pub[ED448_PUB_KEY_SIZE];
  21512. byte privOnly[ED448_PRV_KEY_SIZE];
  21513. word32 privSz = sizeof(priv);
  21514. word32 pubSz = sizeof(pub);
  21515. word32 privOnlySz = sizeof(privOnly);
  21516. XMEMSET(&key, 0, sizeof(ed448_key));
  21517. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21518. ExpectIntEQ(wc_ed448_init(&key), 0);
  21519. ExpectIntEQ(wc_InitRng(&rng), 0);
  21520. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21521. PRIVATE_KEY_UNLOCK();
  21522. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  21523. /* Test bad args. */
  21524. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  21525. BAD_FUNC_ARG);
  21526. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  21527. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  21528. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  21529. /* Test bad args. */
  21530. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  21531. BAD_FUNC_ARG);
  21532. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  21533. BAD_FUNC_ARG);
  21534. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  21535. BAD_FUNC_ARG);
  21536. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  21537. BAD_FUNC_ARG);
  21538. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  21539. BAD_FUNC_ARG);
  21540. PRIVATE_KEY_LOCK();
  21541. /* Cross check output. */
  21542. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  21543. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21544. wc_ed448_free(&key);
  21545. #endif
  21546. return EXPECT_RESULT();
  21547. } /* END test_wc_ed448_exportKey */
  21548. /*
  21549. * Testing wc_Ed448PublicKeyToDer
  21550. */
  21551. static int test_wc_Ed448PublicKeyToDer(void)
  21552. {
  21553. EXPECT_DECLS;
  21554. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21555. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21556. ed448_key key;
  21557. byte derBuf[1024];
  21558. XMEMSET(&key, 0, sizeof(ed448_key));
  21559. /* Test bad args */
  21560. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  21561. ExpectIntEQ(wc_ed448_init(&key), 0);
  21562. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  21563. wc_ed448_free(&key);
  21564. /* Test good args */
  21565. if (EXPECT_SUCCESS()) {
  21566. WC_RNG rng;
  21567. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21568. ExpectIntEQ(wc_ed448_init(&key), 0);
  21569. ExpectIntEQ(wc_InitRng(&rng), 0);
  21570. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21571. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  21572. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21573. wc_ed448_free(&key);
  21574. }
  21575. #endif
  21576. return EXPECT_RESULT();
  21577. } /* END testing wc_Ed448PublicKeyToDer */
  21578. /*
  21579. * Testing wc_curve448_init and wc_curve448_free.
  21580. */
  21581. static int test_wc_curve448_init(void)
  21582. {
  21583. EXPECT_DECLS;
  21584. #if defined(HAVE_CURVE448)
  21585. curve448_key key;
  21586. /* Test bad args for wc_curve448_init */
  21587. ExpectIntEQ(wc_curve448_init(&key), 0);
  21588. /* Test bad args for wc_curve448_init */
  21589. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  21590. /* Test good args for wc_curve_448_free */
  21591. wc_curve448_free(&key);
  21592. /* Test bad args for wc_curve448_free */
  21593. wc_curve448_free(NULL);
  21594. #endif
  21595. return EXPECT_RESULT();
  21596. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21597. /*
  21598. * Testing wc_curve448_make_key
  21599. */
  21600. static int test_wc_curve448_make_key(void)
  21601. {
  21602. EXPECT_DECLS;
  21603. #if defined(HAVE_CURVE448)
  21604. curve448_key key;
  21605. WC_RNG rng;
  21606. int keysize;
  21607. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21608. ExpectIntEQ(wc_curve448_init(&key), 0);
  21609. ExpectIntEQ(wc_InitRng(&rng), 0);
  21610. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21611. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21612. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  21613. /* test bad cases */
  21614. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21615. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21616. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21617. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21618. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21619. wc_curve448_free(&key);
  21620. #endif
  21621. return EXPECT_RESULT();
  21622. } /* END test_wc_curve448_make_key*/
  21623. /*
  21624. * Testing test_wc_curve448_shared_secret_ex
  21625. */
  21626. static int test_wc_curve448_shared_secret_ex(void)
  21627. {
  21628. EXPECT_DECLS;
  21629. #if defined(HAVE_CURVE448)
  21630. curve448_key private_key;
  21631. curve448_key public_key;
  21632. WC_RNG rng;
  21633. byte out[CURVE448_KEY_SIZE];
  21634. word32 outLen = sizeof(out);
  21635. int endian = EC448_BIG_ENDIAN;
  21636. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21637. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  21638. ExpectIntEQ(wc_InitRng(&rng), 0);
  21639. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  21640. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  21641. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  21642. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21643. &outLen, endian), 0);
  21644. /* test bad cases */
  21645. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21646. BAD_FUNC_ARG);
  21647. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  21648. endian), BAD_FUNC_ARG);
  21649. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  21650. endian), BAD_FUNC_ARG);
  21651. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21652. &outLen, endian), BAD_FUNC_ARG);
  21653. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21654. NULL, endian), BAD_FUNC_ARG);
  21655. outLen = outLen - 2;
  21656. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21657. &outLen, endian), BAD_FUNC_ARG);
  21658. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21659. wc_curve448_free(&private_key);
  21660. wc_curve448_free(&public_key);
  21661. #endif
  21662. return EXPECT_RESULT();
  21663. } /* END test_wc_curve448_shared_secret_ex*/
  21664. /*
  21665. * Testing test_wc_curve448_export_public_ex
  21666. */
  21667. static int test_wc_curve448_export_public_ex(void)
  21668. {
  21669. EXPECT_DECLS;
  21670. #if defined(HAVE_CURVE448)
  21671. WC_RNG rng;
  21672. curve448_key key;
  21673. byte out[CURVE448_KEY_SIZE];
  21674. word32 outLen = sizeof(out);
  21675. int endian = EC448_BIG_ENDIAN;
  21676. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21677. ExpectIntEQ(wc_curve448_init(&key), 0);
  21678. ExpectIntEQ(wc_InitRng(&rng), 0);
  21679. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21680. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  21681. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  21682. /* test bad cases*/
  21683. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  21684. BAD_FUNC_ARG);
  21685. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  21686. BAD_FUNC_ARG);
  21687. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  21688. BAD_FUNC_ARG);
  21689. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  21690. BAD_FUNC_ARG);
  21691. outLen = outLen - 2;
  21692. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  21693. ECC_BAD_ARG_E);
  21694. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21695. wc_curve448_free(&key);
  21696. #endif
  21697. return EXPECT_RESULT();
  21698. } /* END test_wc_curve448_export_public_ex*/
  21699. /*
  21700. * Testing test_wc_curve448_export_private_raw_ex
  21701. */
  21702. static int test_wc_curve448_export_private_raw_ex(void)
  21703. {
  21704. EXPECT_DECLS;
  21705. #if defined(HAVE_CURVE448)
  21706. curve448_key key;
  21707. byte out[CURVE448_KEY_SIZE];
  21708. word32 outLen = sizeof(out);
  21709. int endian = EC448_BIG_ENDIAN;
  21710. ExpectIntEQ(wc_curve448_init(&key), 0);
  21711. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21712. 0);
  21713. /* test bad cases*/
  21714. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  21715. BAD_FUNC_ARG);
  21716. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  21717. BAD_FUNC_ARG);
  21718. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  21719. BAD_FUNC_ARG);
  21720. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  21721. BAD_FUNC_ARG);
  21722. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21723. EC448_LITTLE_ENDIAN), 0);
  21724. outLen = outLen - 2;
  21725. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21726. ECC_BAD_ARG_E);
  21727. wc_curve448_free(&key);
  21728. #endif
  21729. return EXPECT_RESULT();
  21730. } /* END test_wc_curve448_export_private_raw_ex*/
  21731. /*
  21732. * Testing test_wc_curve448_import_private_raw_ex
  21733. */
  21734. static int test_wc_curve448_import_private_raw_ex(void)
  21735. {
  21736. EXPECT_DECLS;
  21737. #if defined(HAVE_CURVE448)
  21738. curve448_key key;
  21739. WC_RNG rng;
  21740. byte priv[CURVE448_KEY_SIZE];
  21741. byte pub[CURVE448_KEY_SIZE];
  21742. word32 privSz = sizeof(priv);
  21743. word32 pubSz = sizeof(pub);
  21744. int endian = EC448_BIG_ENDIAN;
  21745. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21746. ExpectIntEQ(wc_curve448_init(&key), 0);
  21747. ExpectIntEQ(wc_InitRng(&rng), 0);
  21748. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21749. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21750. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21751. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21752. &key, endian), 0);
  21753. /* test bad cases */
  21754. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  21755. BAD_FUNC_ARG);
  21756. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21757. &key, endian), BAD_FUNC_ARG);
  21758. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21759. &key, endian), BAD_FUNC_ARG);
  21760. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21761. NULL, endian), BAD_FUNC_ARG);
  21762. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21763. &key, endian), ECC_BAD_ARG_E);
  21764. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21765. &key, endian), ECC_BAD_ARG_E);
  21766. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21767. &key, EC448_LITTLE_ENDIAN), 0);
  21768. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21769. wc_curve448_free(&key);
  21770. #endif
  21771. return EXPECT_RESULT();
  21772. } /* END test_wc_curve448_import_private_raw_ex*/
  21773. /*
  21774. * Testing test_curve448_export_key_raw
  21775. */
  21776. static int test_wc_curve448_export_key_raw(void)
  21777. {
  21778. EXPECT_DECLS;
  21779. #if defined(HAVE_CURVE448)
  21780. curve448_key key;
  21781. WC_RNG rng;
  21782. byte priv[CURVE448_KEY_SIZE];
  21783. byte pub[CURVE448_KEY_SIZE];
  21784. word32 privSz = sizeof(priv);
  21785. word32 pubSz = sizeof(pub);
  21786. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21787. ExpectIntEQ(wc_curve448_init(&key), 0);
  21788. ExpectIntEQ(wc_InitRng(&rng), 0);
  21789. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21790. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21791. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21792. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  21793. 0);
  21794. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21795. wc_curve448_free(&key);
  21796. #endif
  21797. return EXPECT_RESULT();
  21798. } /* END test_wc_curve448_import_private_raw_ex*/
  21799. /*
  21800. * Testing test_wc_curve448_import_private
  21801. */
  21802. static int test_wc_curve448_import_private(void)
  21803. {
  21804. EXPECT_DECLS;
  21805. #if defined(HAVE_CURVE448)
  21806. curve448_key key;
  21807. WC_RNG rng;
  21808. byte priv[CURVE448_KEY_SIZE];
  21809. word32 privSz = sizeof(priv);
  21810. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21811. ExpectIntEQ(wc_curve448_init(&key), 0);
  21812. ExpectIntEQ(wc_InitRng(&rng), 0);
  21813. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21814. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21815. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  21816. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21817. wc_curve448_free(&key);
  21818. #endif
  21819. return EXPECT_RESULT();
  21820. } /* END test_wc_curve448_import*/
  21821. /*
  21822. * Testing test_wc_curve448_size.
  21823. */
  21824. static int test_wc_curve448_size(void)
  21825. {
  21826. EXPECT_DECLS;
  21827. #if defined(HAVE_CURVE448)
  21828. curve448_key key;
  21829. ExpectIntEQ(wc_curve448_init(&key), 0);
  21830. /* Test good args for wc_curve448_size */
  21831. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21832. /* Test bad args for wc_curve448_size */
  21833. ExpectIntEQ(wc_curve448_size(NULL), 0);
  21834. wc_curve448_free(&key);
  21835. #endif
  21836. return EXPECT_RESULT();
  21837. } /* END test_wc_curve448_size*/
  21838. /*
  21839. * Testing wc_ecc_make_key.
  21840. */
  21841. static int test_wc_ecc_make_key(void)
  21842. {
  21843. EXPECT_DECLS;
  21844. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21845. ecc_key key;
  21846. WC_RNG rng;
  21847. int ret;
  21848. XMEMSET(&key, 0, sizeof(ecc_key));
  21849. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21850. ExpectIntEQ(wc_ecc_init(&key), 0);
  21851. ExpectIntEQ(wc_InitRng(&rng), 0);
  21852. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21853. #if defined(WOLFSSL_ASYNC_CRYPT)
  21854. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21855. #endif
  21856. ExpectIntEQ(ret, 0);
  21857. /* Pass in bad args. */
  21858. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  21859. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  21860. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21861. wc_ecc_free(&key);
  21862. #ifdef FP_ECC
  21863. wc_ecc_fp_free();
  21864. #endif
  21865. #endif
  21866. return EXPECT_RESULT();
  21867. } /* END test_wc_ecc_make_key */
  21868. /*
  21869. * Testing wc_ecc_init()
  21870. */
  21871. static int test_wc_ecc_init(void)
  21872. {
  21873. EXPECT_DECLS;
  21874. #ifdef HAVE_ECC
  21875. ecc_key key;
  21876. XMEMSET(&key, 0, sizeof(ecc_key));
  21877. ExpectIntEQ(wc_ecc_init(&key), 0);
  21878. /* Pass in bad args. */
  21879. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  21880. wc_ecc_free(&key);
  21881. #endif
  21882. return EXPECT_RESULT();
  21883. } /* END test_wc_ecc_init */
  21884. /*
  21885. * Testing wc_ecc_check_key()
  21886. */
  21887. static int test_wc_ecc_check_key(void)
  21888. {
  21889. EXPECT_DECLS;
  21890. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21891. ecc_key key;
  21892. WC_RNG rng;
  21893. int ret;
  21894. XMEMSET(&rng, 0, sizeof(rng));
  21895. XMEMSET(&key, 0, sizeof(key));
  21896. ExpectIntEQ(wc_ecc_init(&key), 0);
  21897. ExpectIntEQ(wc_InitRng(&rng), 0);
  21898. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21899. #if defined(WOLFSSL_ASYNC_CRYPT)
  21900. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21901. #endif
  21902. ExpectIntEQ(ret, 0);
  21903. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  21904. /* Pass in bad args. */
  21905. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  21906. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21907. wc_ecc_free(&key);
  21908. #ifdef FP_ECC
  21909. wc_ecc_fp_free();
  21910. #endif
  21911. #endif
  21912. return EXPECT_RESULT();
  21913. } /* END test_wc_ecc_check_key */
  21914. /*
  21915. * Testing wc_ecc_get_generator()
  21916. */
  21917. static int test_wc_ecc_get_generator(void)
  21918. {
  21919. EXPECT_DECLS;
  21920. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21921. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21922. ecc_point* pt = NULL;
  21923. ExpectNotNull(pt = wc_ecc_new_point());
  21924. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21925. MP_OKAY);
  21926. /* Test bad args. */
  21927. /* Returns Zero for bad arg. */
  21928. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21929. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21930. MP_OKAY);
  21931. /* If we ever get to 1000 curves increase this number */
  21932. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21933. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21934. wc_ecc_del_point(pt);
  21935. #endif
  21936. return EXPECT_RESULT();
  21937. } /* END test_wc_ecc_get_generator */
  21938. /*
  21939. * Testing wc_ecc_size()
  21940. */
  21941. static int test_wc_ecc_size(void)
  21942. {
  21943. EXPECT_DECLS;
  21944. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21945. WC_RNG rng;
  21946. ecc_key key;
  21947. int ret;
  21948. XMEMSET(&key, 0, sizeof(ecc_key));
  21949. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21950. ExpectIntEQ(wc_ecc_init(&key), 0);
  21951. ExpectIntEQ(wc_InitRng(&rng), 0);
  21952. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21953. #if defined(WOLFSSL_ASYNC_CRYPT)
  21954. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21955. #endif
  21956. ExpectIntEQ(ret, 0);
  21957. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21958. /* Test bad args. */
  21959. /* Returns Zero for bad arg. */
  21960. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21961. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21962. wc_ecc_free(&key);
  21963. #endif
  21964. return EXPECT_RESULT();
  21965. } /* END test_wc_ecc_size */
  21966. static int test_wc_ecc_params(void)
  21967. {
  21968. EXPECT_DECLS;
  21969. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21970. It was added after certifications */
  21971. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21972. const ecc_set_type* ecc_set;
  21973. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21974. /* Test for SECP256R1 curve */
  21975. int curve_id = ECC_SECP256R1;
  21976. int curve_idx;
  21977. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21978. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21979. ExpectIntEQ(ecc_set->id, curve_id);
  21980. #endif
  21981. /* Test case when SECP256R1 is not enabled */
  21982. /* Test that we get curve params for index 0 */
  21983. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21984. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21985. return EXPECT_RESULT();
  21986. }
  21987. /*
  21988. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21989. */
  21990. static int test_wc_ecc_signVerify_hash(void)
  21991. {
  21992. EXPECT_DECLS;
  21993. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21994. ecc_key key;
  21995. WC_RNG rng;
  21996. int ret;
  21997. #ifdef HAVE_ECC_VERIFY
  21998. int verify = 0;
  21999. #endif
  22000. word32 siglen = ECC_BUFSIZE;
  22001. byte sig[ECC_BUFSIZE];
  22002. byte adjustedSig[ECC_BUFSIZE+1];
  22003. byte digest[] = TEST_STRING;
  22004. word32 digestlen = (word32)TEST_STRING_SZ;
  22005. /* Init stack var */
  22006. XMEMSET(&key, 0, sizeof(ecc_key));
  22007. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22008. XMEMSET(sig, 0, siglen);
  22009. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  22010. /* Init structs. */
  22011. ExpectIntEQ(wc_ecc_init(&key), 0);
  22012. ExpectIntEQ(wc_InitRng(&rng), 0);
  22013. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22014. #if defined(WOLFSSL_ASYNC_CRYPT)
  22015. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22016. #endif
  22017. ExpectIntEQ(ret, 0);
  22018. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  22019. 0);
  22020. /* Check bad args. */
  22021. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  22022. ECC_BAD_ARG_E);
  22023. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  22024. ECC_BAD_ARG_E);
  22025. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  22026. ECC_BAD_ARG_E);
  22027. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  22028. ECC_BAD_ARG_E);
  22029. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  22030. ECC_BAD_ARG_E);
  22031. #ifdef HAVE_ECC_VERIFY
  22032. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  22033. &key), 0);
  22034. ExpectIntEQ(verify, 1);
  22035. /* test check on length of signature passed in */
  22036. XMEMCPY(adjustedSig, sig, siglen);
  22037. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  22038. #ifndef NO_STRICT_ECDSA_LEN
  22039. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  22040. &verify, &key), 0);
  22041. #else
  22042. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  22043. * is allowed */
  22044. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  22045. &verify, &key), 0);
  22046. #endif
  22047. /* Test bad args. */
  22048. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  22049. &key), ECC_BAD_ARG_E);
  22050. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  22051. ECC_BAD_ARG_E);
  22052. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  22053. ECC_BAD_ARG_E);
  22054. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  22055. NULL), ECC_BAD_ARG_E);
  22056. #endif /* HAVE_ECC_VERIFY */
  22057. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22058. wc_ecc_free(&key);
  22059. #ifdef FP_ECC
  22060. wc_ecc_fp_free();
  22061. #endif
  22062. #endif
  22063. return EXPECT_RESULT();
  22064. } /* END test_wc_ecc_sign_hash */
  22065. /*
  22066. * Testing wc_ecc_shared_secret()
  22067. */
  22068. static int test_wc_ecc_shared_secret(void)
  22069. {
  22070. EXPECT_DECLS;
  22071. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  22072. ecc_key key;
  22073. ecc_key pubKey;
  22074. WC_RNG rng;
  22075. #if defined(NO_ECC256)
  22076. int ret;
  22077. #endif
  22078. byte out[KEY32];
  22079. int keySz = sizeof(out);
  22080. word32 outlen = (word32)sizeof(out);
  22081. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22082. const char* qx =
  22083. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22084. const char* qy =
  22085. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22086. const char* d =
  22087. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22088. const char* curveName = "SECP256R1";
  22089. const byte expected_shared_secret[] =
  22090. {
  22091. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  22092. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  22093. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  22094. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  22095. };
  22096. #endif
  22097. PRIVATE_KEY_UNLOCK();
  22098. /* Initialize variables. */
  22099. XMEMSET(&key, 0, sizeof(ecc_key));
  22100. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  22101. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22102. XMEMSET(out, 0, keySz);
  22103. ExpectIntEQ(wc_ecc_init(&key), 0);
  22104. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  22105. ExpectIntEQ(wc_InitRng(&rng), 0);
  22106. #if !defined(NO_ECC256)
  22107. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  22108. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  22109. #else
  22110. ret = wc_ecc_make_key(&rng, keySz, &key);
  22111. #if defined(WOLFSSL_ASYNC_CRYPT)
  22112. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22113. #endif
  22114. ExpectIntEQ(ret, 0);
  22115. ret = wc_ecc_make_key(&rng, keySz, &key);
  22116. #if defined(WOLFSSL_ASYNC_CRYPT)
  22117. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22118. #endif
  22119. ExpectIntEQ(ret, 0);
  22120. #endif
  22121. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22122. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22123. !defined(HAVE_SELFTEST)
  22124. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22125. #endif
  22126. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  22127. #if !defined(NO_ECC256)
  22128. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  22129. #endif
  22130. /* Test bad args. */
  22131. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  22132. BAD_FUNC_ARG);
  22133. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  22134. BAD_FUNC_ARG);
  22135. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  22136. BAD_FUNC_ARG);
  22137. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  22138. BAD_FUNC_ARG);
  22139. /* Invalid length */
  22140. outlen = 1;
  22141. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  22142. BUFFER_E);
  22143. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22144. wc_ecc_free(&pubKey);
  22145. wc_ecc_free(&key);
  22146. #ifdef FP_ECC
  22147. wc_ecc_fp_free();
  22148. #endif
  22149. PRIVATE_KEY_LOCK();
  22150. #endif
  22151. return EXPECT_RESULT();
  22152. } /* END tests_wc_ecc_shared_secret */
  22153. /*
  22154. * testint wc_ecc_export_x963()
  22155. */
  22156. static int test_wc_ecc_export_x963(void)
  22157. {
  22158. EXPECT_DECLS;
  22159. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22160. ecc_key key;
  22161. WC_RNG rng;
  22162. byte out[ECC_ASN963_MAX_BUF_SZ];
  22163. word32 outlen = sizeof(out);
  22164. int ret;
  22165. PRIVATE_KEY_UNLOCK();
  22166. /* Initialize variables. */
  22167. XMEMSET(&key, 0, sizeof(ecc_key));
  22168. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22169. XMEMSET(out, 0, outlen);
  22170. ExpectIntEQ(wc_ecc_init(&key), 0);
  22171. ExpectIntEQ(wc_InitRng(&rng), 0);
  22172. ret = wc_ecc_make_key(&rng, KEY20, &key);
  22173. #if defined(WOLFSSL_ASYNC_CRYPT)
  22174. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22175. #endif
  22176. ExpectIntEQ(ret, 0);
  22177. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  22178. /* Test bad args. */
  22179. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  22180. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  22181. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  22182. key.idx = -4;
  22183. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  22184. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22185. wc_ecc_free(&key);
  22186. #ifdef FP_ECC
  22187. wc_ecc_fp_free();
  22188. #endif
  22189. PRIVATE_KEY_LOCK();
  22190. #endif
  22191. return EXPECT_RESULT();
  22192. } /* END test_wc_ecc_export_x963 */
  22193. /*
  22194. * Testing wc_ecc_export_x963_ex()
  22195. * compile with --enable-compkey will use compression.
  22196. */
  22197. static int test_wc_ecc_export_x963_ex(void)
  22198. {
  22199. EXPECT_DECLS;
  22200. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22201. ecc_key key;
  22202. WC_RNG rng;
  22203. int ret;
  22204. byte out[ECC_ASN963_MAX_BUF_SZ];
  22205. word32 outlen = sizeof(out);
  22206. #ifdef HAVE_COMP_KEY
  22207. word32 badOutLen = 5;
  22208. #endif
  22209. /* Init stack variables. */
  22210. XMEMSET(&key, 0, sizeof(ecc_key));
  22211. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22212. XMEMSET(out, 0, outlen);
  22213. PRIVATE_KEY_UNLOCK();
  22214. ExpectIntEQ(wc_ecc_init(&key), 0);
  22215. ExpectIntEQ(wc_InitRng(&rng), 0);
  22216. ret = wc_ecc_make_key(&rng, KEY64, &key);
  22217. #if defined(WOLFSSL_ASYNC_CRYPT)
  22218. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22219. #endif
  22220. ExpectIntEQ(ret, 0);
  22221. #ifdef HAVE_COMP_KEY
  22222. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  22223. #else
  22224. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  22225. #endif
  22226. /* Test bad args. */
  22227. #ifdef HAVE_COMP_KEY
  22228. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  22229. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  22230. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  22231. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  22232. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  22233. #else
  22234. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  22235. LENGTH_ONLY_E);
  22236. #endif
  22237. key.idx = -4;
  22238. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  22239. #else
  22240. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  22241. ECC_BAD_ARG_E);
  22242. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  22243. LENGTH_ONLY_E);
  22244. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  22245. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  22246. ECC_BAD_ARG_E);
  22247. key.idx = -4;
  22248. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  22249. ECC_BAD_ARG_E);
  22250. #endif
  22251. PRIVATE_KEY_LOCK();
  22252. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22253. wc_ecc_free(&key);
  22254. #ifdef FP_ECC
  22255. wc_ecc_fp_free();
  22256. #endif
  22257. #endif
  22258. return EXPECT_RESULT();
  22259. } /* END test_wc_ecc_export_x963_ex */
  22260. /*
  22261. * testing wc_ecc_import_x963()
  22262. */
  22263. static int test_wc_ecc_import_x963(void)
  22264. {
  22265. EXPECT_DECLS;
  22266. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22267. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22268. ecc_key pubKey;
  22269. ecc_key key;
  22270. WC_RNG rng;
  22271. byte x963[ECC_ASN963_MAX_BUF_SZ];
  22272. word32 x963Len = (word32)sizeof(x963);
  22273. int ret;
  22274. /* Init stack variables. */
  22275. XMEMSET(&key, 0, sizeof(ecc_key));
  22276. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  22277. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22278. XMEMSET(x963, 0, x963Len);
  22279. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  22280. ExpectIntEQ(wc_ecc_init(&key), 0);
  22281. ExpectIntEQ(wc_InitRng(&rng), 0);
  22282. #if FIPS_VERSION3_GE(6,0,0)
  22283. ret = wc_ecc_make_key(&rng, KEY32, &key);
  22284. #else
  22285. ret = wc_ecc_make_key(&rng, KEY24, &key);
  22286. #endif
  22287. #if defined(WOLFSSL_ASYNC_CRYPT)
  22288. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22289. #endif
  22290. ExpectIntEQ(ret, 0);
  22291. PRIVATE_KEY_UNLOCK();
  22292. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  22293. PRIVATE_KEY_LOCK();
  22294. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  22295. /* Test bad args. */
  22296. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  22297. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  22298. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  22299. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22300. wc_ecc_free(&key);
  22301. wc_ecc_free(&pubKey);
  22302. #ifdef FP_ECC
  22303. wc_ecc_fp_free();
  22304. #endif
  22305. #endif
  22306. return EXPECT_RESULT();
  22307. } /* END wc_ecc_import_x963 */
  22308. /*
  22309. * testing wc_ecc_import_private_key()
  22310. */
  22311. static int test_wc_ecc_import_private_key(void)
  22312. {
  22313. EXPECT_DECLS;
  22314. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22315. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22316. ecc_key key;
  22317. ecc_key keyImp;
  22318. WC_RNG rng;
  22319. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  22320. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  22321. word32 privKeySz = (word32)sizeof(privKey);
  22322. word32 x963KeySz = (word32)sizeof(x963Key);
  22323. int ret;
  22324. /* Init stack variables. */
  22325. XMEMSET(&key, 0, sizeof(ecc_key));
  22326. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  22327. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22328. XMEMSET(privKey, 0, privKeySz);
  22329. XMEMSET(x963Key, 0, x963KeySz);
  22330. PRIVATE_KEY_UNLOCK();
  22331. ExpectIntEQ(wc_ecc_init(&key), 0);
  22332. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  22333. ExpectIntEQ(wc_InitRng(&rng), 0);
  22334. ret = wc_ecc_make_key(&rng, KEY48, &key);
  22335. #if defined(WOLFSSL_ASYNC_CRYPT)
  22336. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22337. #endif
  22338. ExpectIntEQ(ret, 0);
  22339. PRIVATE_KEY_UNLOCK();
  22340. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  22341. PRIVATE_KEY_LOCK();
  22342. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  22343. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22344. x963KeySz, &keyImp), 0);
  22345. /* Pass in bad args. */
  22346. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22347. x963KeySz, NULL), BAD_FUNC_ARG);
  22348. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  22349. &keyImp), BAD_FUNC_ARG);
  22350. PRIVATE_KEY_LOCK();
  22351. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22352. wc_ecc_free(&keyImp);
  22353. wc_ecc_free(&key);
  22354. #ifdef FP_ECC
  22355. wc_ecc_fp_free();
  22356. #endif
  22357. #endif
  22358. return EXPECT_RESULT();
  22359. } /* END test_wc_ecc_import_private_key */
  22360. /*
  22361. * Testing wc_ecc_export_private_only()
  22362. */
  22363. static int test_wc_ecc_export_private_only(void)
  22364. {
  22365. EXPECT_DECLS;
  22366. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22367. ecc_key key;
  22368. WC_RNG rng;
  22369. byte out[ECC_PRIV_KEY_BUF];
  22370. word32 outlen = sizeof(out);
  22371. int ret;
  22372. /* Init stack variables. */
  22373. XMEMSET(&key, 0, sizeof(ecc_key));
  22374. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22375. XMEMSET(out, 0, outlen);
  22376. PRIVATE_KEY_UNLOCK();
  22377. ExpectIntEQ(wc_ecc_init(&key), 0);
  22378. ExpectIntEQ(wc_InitRng(&rng), 0);
  22379. ret = wc_ecc_make_key(&rng, KEY32, &key);
  22380. #if defined(WOLFSSL_ASYNC_CRYPT)
  22381. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22382. #endif
  22383. ExpectIntEQ(ret, 0);
  22384. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  22385. /* Pass in bad args. */
  22386. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  22387. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  22388. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  22389. PRIVATE_KEY_LOCK();
  22390. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22391. wc_ecc_free(&key);
  22392. #ifdef FP_ECC
  22393. wc_ecc_fp_free();
  22394. #endif
  22395. #endif
  22396. return EXPECT_RESULT();
  22397. } /* END test_wc_ecc_export_private_only */
  22398. /*
  22399. * Testing wc_ecc_rs_to_sig()
  22400. */
  22401. static int test_wc_ecc_rs_to_sig(void)
  22402. {
  22403. EXPECT_DECLS;
  22404. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22405. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22406. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22407. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22408. const char* zeroStr = "0";
  22409. byte sig[ECC_MAX_SIG_SIZE];
  22410. word32 siglen = (word32)sizeof(sig);
  22411. /* R and S max size is the order of curve. 2^192.*/
  22412. int keySz = KEY24;
  22413. byte r[KEY24];
  22414. byte s[KEY24];
  22415. word32 rlen = (word32)sizeof(r);
  22416. word32 slen = (word32)sizeof(s);
  22417. /* Init stack variables. */
  22418. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22419. XMEMSET(r, 0, keySz);
  22420. XMEMSET(s, 0, keySz);
  22421. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  22422. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  22423. /* Test bad args. */
  22424. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  22425. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  22426. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  22427. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  22428. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  22429. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  22430. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  22431. ECC_BAD_ARG_E);
  22432. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  22433. ECC_BAD_ARG_E);
  22434. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  22435. ECC_BAD_ARG_E);
  22436. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  22437. ECC_BAD_ARG_E);
  22438. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  22439. ECC_BAD_ARG_E);
  22440. #endif
  22441. return EXPECT_RESULT();
  22442. } /* END test_wc_ecc_rs_to_sig */
  22443. static int test_wc_ecc_import_raw(void)
  22444. {
  22445. EXPECT_DECLS;
  22446. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22447. ecc_key key;
  22448. const char* qx =
  22449. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22450. const char* qy =
  22451. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22452. const char* d =
  22453. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22454. const char* curveName = "SECP256R1";
  22455. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22456. const char* kNullStr = "";
  22457. int ret;
  22458. #endif
  22459. XMEMSET(&key, 0, sizeof(ecc_key));
  22460. ExpectIntEQ(wc_ecc_init(&key), 0);
  22461. /* Test good import */
  22462. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  22463. /* Test bad args. */
  22464. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  22465. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  22466. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  22467. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  22468. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22469. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22470. wc_ecc_free(&key);
  22471. #endif
  22472. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  22473. curveName), 0);
  22474. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22475. #endif
  22476. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22477. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22478. wc_ecc_free(&key);
  22479. #endif
  22480. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22481. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22482. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22483. #else
  22484. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22485. #endif
  22486. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22487. wc_ecc_free(&key);
  22488. #endif
  22489. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22490. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22491. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22492. #else
  22493. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22494. #endif
  22495. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22496. wc_ecc_free(&key);
  22497. #endif
  22498. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  22499. #endif
  22500. wc_ecc_free(&key);
  22501. #endif
  22502. return EXPECT_RESULT();
  22503. } /* END test_wc_ecc_import_raw */
  22504. static int test_wc_ecc_import_unsigned(void)
  22505. {
  22506. EXPECT_DECLS;
  22507. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22508. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  22509. HAVE_FIPS_VERSION >= 2))
  22510. ecc_key key;
  22511. const byte qx[] = {
  22512. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22513. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22514. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22515. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22516. };
  22517. const byte qy[] = {
  22518. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22519. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22520. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22521. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22522. };
  22523. const byte d[] = {
  22524. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22525. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22526. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22527. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22528. };
  22529. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22530. const byte nullBytes[32] = {0};
  22531. int ret;
  22532. #endif
  22533. int curveId = ECC_SECP256R1;
  22534. XMEMSET(&key, 0, sizeof(ecc_key));
  22535. ExpectIntEQ(wc_ecc_init(&key), 0);
  22536. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22537. curveId), 0);
  22538. /* Test bad args. */
  22539. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22540. curveId), BAD_FUNC_ARG);
  22541. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22542. curveId), BAD_FUNC_ARG);
  22543. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22544. curveId), BAD_FUNC_ARG);
  22545. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22546. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  22547. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22548. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22549. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  22550. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22551. #endif
  22552. wc_ecc_free(&key);
  22553. #endif
  22554. return EXPECT_RESULT();
  22555. } /* END test_wc_ecc_import_unsigned */
  22556. /*
  22557. * Testing wc_ecc_sig_size()
  22558. */
  22559. static int test_wc_ecc_sig_size(void)
  22560. {
  22561. EXPECT_DECLS;
  22562. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22563. ecc_key key;
  22564. WC_RNG rng;
  22565. int keySz = KEY16;
  22566. int ret;
  22567. XMEMSET(&rng, 0, sizeof(rng));
  22568. XMEMSET(&key, 0, sizeof(key));
  22569. ExpectIntEQ(wc_ecc_init(&key), 0);
  22570. ExpectIntEQ(wc_InitRng(&rng), 0);
  22571. ret = wc_ecc_make_key(&rng, keySz, &key);
  22572. #if defined(WOLFSSL_ASYNC_CRYPT)
  22573. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22574. #endif
  22575. ExpectIntEQ(ret, 0);
  22576. ExpectIntLE(wc_ecc_sig_size(&key),
  22577. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  22578. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22579. wc_ecc_free(&key);
  22580. #endif
  22581. return EXPECT_RESULT();
  22582. } /* END test_wc_ecc_sig_size */
  22583. /*
  22584. * Testing wc_ecc_ctx_new()
  22585. */
  22586. static int test_wc_ecc_ctx_new(void)
  22587. {
  22588. EXPECT_DECLS;
  22589. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22590. WC_RNG rng;
  22591. ecEncCtx* cli = NULL;
  22592. ecEncCtx* srv = NULL;
  22593. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22594. ExpectIntEQ(wc_InitRng(&rng), 0);
  22595. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22596. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22597. wc_ecc_ctx_free(cli);
  22598. cli = NULL;
  22599. wc_ecc_ctx_free(srv);
  22600. /* Test bad args. */
  22601. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22602. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  22603. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  22604. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22605. wc_ecc_ctx_free(cli);
  22606. #endif
  22607. return EXPECT_RESULT();
  22608. } /* END test_wc_ecc_ctx_new */
  22609. /*
  22610. * Tesing wc_ecc_reset()
  22611. */
  22612. static int test_wc_ecc_ctx_reset(void)
  22613. {
  22614. EXPECT_DECLS;
  22615. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22616. ecEncCtx* ctx = NULL;
  22617. WC_RNG rng;
  22618. XMEMSET(&rng, 0, sizeof(rng));
  22619. ExpectIntEQ(wc_InitRng(&rng), 0);
  22620. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22621. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  22622. /* Pass in bad args. */
  22623. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  22624. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  22625. wc_ecc_ctx_free(ctx);
  22626. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22627. #endif
  22628. return EXPECT_RESULT();
  22629. } /* END test_wc_ecc_ctx_reset */
  22630. /*
  22631. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22632. */
  22633. static int test_wc_ecc_ctx_set_peer_salt(void)
  22634. {
  22635. EXPECT_DECLS;
  22636. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22637. WC_RNG rng;
  22638. ecEncCtx* cliCtx = NULL;
  22639. ecEncCtx* servCtx = NULL;
  22640. const byte* cliSalt = NULL;
  22641. const byte* servSalt = NULL;
  22642. XMEMSET(&rng, 0, sizeof(rng));
  22643. ExpectIntEQ(wc_InitRng(&rng), 0);
  22644. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22645. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22646. /* Test bad args. */
  22647. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  22648. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  22649. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  22650. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  22651. /* Test bad args. */
  22652. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  22653. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  22654. wc_ecc_ctx_free(cliCtx);
  22655. wc_ecc_ctx_free(servCtx);
  22656. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22657. #endif
  22658. return EXPECT_RESULT();
  22659. } /* END test_wc_ecc_ctx_set_peer_salt */
  22660. /*
  22661. * Testing wc_ecc_ctx_set_info()
  22662. */
  22663. static int test_wc_ecc_ctx_set_info(void)
  22664. {
  22665. EXPECT_DECLS;
  22666. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22667. ecEncCtx* ctx = NULL;
  22668. WC_RNG rng;
  22669. const char* optInfo = "Optional Test Info.";
  22670. int optInfoSz = (int)XSTRLEN(optInfo);
  22671. const char* badOptInfo = NULL;
  22672. XMEMSET(&rng, 0, sizeof(rng));
  22673. ExpectIntEQ(wc_InitRng(&rng), 0);
  22674. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22675. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  22676. /* Test bad args. */
  22677. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  22678. BAD_FUNC_ARG);
  22679. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  22680. BAD_FUNC_ARG);
  22681. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  22682. wc_ecc_ctx_free(ctx);
  22683. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22684. #endif
  22685. return EXPECT_RESULT();
  22686. } /* END test_wc_ecc_ctx_set_info */
  22687. /*
  22688. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22689. */
  22690. static int test_wc_ecc_encryptDecrypt(void)
  22691. {
  22692. EXPECT_DECLS;
  22693. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22694. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22695. ecc_key srvKey;
  22696. ecc_key cliKey;
  22697. ecc_key tmpKey;
  22698. WC_RNG rng;
  22699. int ret;
  22700. const char* msg = "EccBlock Size 16";
  22701. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22702. #ifdef WOLFSSL_ECIES_OLD
  22703. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22704. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22705. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22706. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22707. #else
  22708. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  22709. WC_SHA256_DIGEST_SIZE];
  22710. #endif
  22711. word32 outSz = (word32)sizeof(out);
  22712. byte plain[sizeof("EccBlock Size 16")];
  22713. word32 plainSz = (word32)sizeof(plain);
  22714. int keySz = KEY20;
  22715. /* Init stack variables. */
  22716. XMEMSET(out, 0, outSz);
  22717. XMEMSET(plain, 0, plainSz);
  22718. XMEMSET(&rng, 0, sizeof(rng));
  22719. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  22720. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  22721. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  22722. ExpectIntEQ(wc_InitRng(&rng), 0);
  22723. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  22724. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22725. #if defined(WOLFSSL_ASYNC_CRYPT)
  22726. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22727. #endif
  22728. ExpectIntEQ(ret, 0);
  22729. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  22730. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22731. #if defined(WOLFSSL_ASYNC_CRYPT)
  22732. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22733. #endif
  22734. ExpectIntEQ(ret, 0);
  22735. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  22736. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22737. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22738. !defined(HAVE_SELFTEST)
  22739. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  22740. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  22741. #endif
  22742. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22743. &outSz, NULL), 0);
  22744. /* Test bad args. */
  22745. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  22746. NULL), BAD_FUNC_ARG);
  22747. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  22748. NULL), BAD_FUNC_ARG);
  22749. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  22750. NULL), BAD_FUNC_ARG);
  22751. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22752. &outSz, NULL), BAD_FUNC_ARG);
  22753. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  22754. NULL), BAD_FUNC_ARG);
  22755. #ifdef WOLFSSL_ECIES_OLD
  22756. tmpKey.dp = cliKey.dp;
  22757. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  22758. #endif
  22759. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  22760. NULL), 0);
  22761. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  22762. NULL), BAD_FUNC_ARG);
  22763. #ifdef WOLFSSL_ECIES_OLD
  22764. /* NULL parameter allowed in new implementations - public key comes from
  22765. * the message. */
  22766. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  22767. NULL), BAD_FUNC_ARG);
  22768. #endif
  22769. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  22770. NULL), BAD_FUNC_ARG);
  22771. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  22772. NULL), BAD_FUNC_ARG);
  22773. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  22774. BAD_FUNC_ARG);
  22775. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  22776. wc_ecc_free(&tmpKey);
  22777. wc_ecc_free(&srvKey);
  22778. wc_ecc_free(&cliKey);
  22779. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22780. #endif
  22781. return EXPECT_RESULT();
  22782. } /* END test_wc_ecc_encryptDecrypt */
  22783. /*
  22784. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22785. */
  22786. static int test_wc_ecc_del_point(void)
  22787. {
  22788. EXPECT_DECLS;
  22789. #if defined(HAVE_ECC)
  22790. ecc_point* pt = NULL;
  22791. ExpectNotNull(pt = wc_ecc_new_point());
  22792. wc_ecc_del_point(pt);
  22793. #endif
  22794. return EXPECT_RESULT();
  22795. } /* END test_wc_ecc_del_point */
  22796. /*
  22797. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22798. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22799. * and wc_ecc_cmp_point()
  22800. */
  22801. static int test_wc_ecc_pointFns(void)
  22802. {
  22803. EXPECT_DECLS;
  22804. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22805. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22806. !defined(WOLFSSL_ATECC608A)
  22807. ecc_key key;
  22808. WC_RNG rng;
  22809. int ret;
  22810. ecc_point* point = NULL;
  22811. ecc_point* cpypt = NULL;
  22812. int idx = 0;
  22813. int keySz = KEY32;
  22814. byte der[DER_SZ(KEY32)];
  22815. word32 derlenChk = 0;
  22816. word32 derSz = DER_SZ(KEY32);
  22817. /* Init stack variables. */
  22818. XMEMSET(der, 0, derSz);
  22819. XMEMSET(&key, 0, sizeof(ecc_key));
  22820. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22821. ExpectIntEQ(wc_InitRng(&rng), 0);
  22822. ExpectIntEQ(wc_ecc_init(&key), 0);
  22823. ret = wc_ecc_make_key(&rng, keySz, &key);
  22824. #if defined(WOLFSSL_ASYNC_CRYPT)
  22825. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22826. #endif
  22827. ExpectIntEQ(ret, 0);
  22828. ExpectNotNull(point = wc_ecc_new_point());
  22829. ExpectNotNull(cpypt = wc_ecc_new_point());
  22830. /* Export */
  22831. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  22832. &derlenChk), LENGTH_ONLY_E);
  22833. /* Check length value. */
  22834. ExpectIntEQ(derSz, derlenChk);
  22835. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22836. &derSz), 0);
  22837. /* Test bad args. */
  22838. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  22839. ECC_BAD_ARG_E);
  22840. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  22841. ECC_BAD_ARG_E);
  22842. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22843. NULL), ECC_BAD_ARG_E);
  22844. /* Import */
  22845. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  22846. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  22847. /* Test bad args. */
  22848. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  22849. ECC_BAD_ARG_E);
  22850. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  22851. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  22852. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  22853. ECC_BAD_ARG_E);
  22854. /* Copy */
  22855. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  22856. /* Test bad args. */
  22857. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  22858. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  22859. /* Compare point */
  22860. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  22861. /* Test bad args. */
  22862. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  22863. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  22864. /* At infinity if return == 1, otherwise return == 0. */
  22865. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  22866. /* Test bad args. */
  22867. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  22868. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  22869. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  22870. #ifdef USE_ECC_B_PARAM
  22871. /* On curve if ret == 0 */
  22872. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  22873. /* Test bad args. */
  22874. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  22875. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  22876. #endif /* USE_ECC_B_PARAM */
  22877. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  22878. /* Free */
  22879. wc_ecc_del_point(point);
  22880. wc_ecc_del_point(cpypt);
  22881. wc_ecc_free(&key);
  22882. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22883. #endif
  22884. return EXPECT_RESULT();
  22885. } /* END test_wc_ecc_pointFns */
  22886. /*
  22887. * Testing wc_ecc_shared_secret_ssh()
  22888. */
  22889. static int test_wc_ecc_shared_secret_ssh(void)
  22890. {
  22891. EXPECT_DECLS;
  22892. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22893. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22894. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  22895. !defined(WOLFSSL_CRYPTOCELL)
  22896. ecc_key key;
  22897. ecc_key key2;
  22898. WC_RNG rng;
  22899. int ret;
  22900. int keySz = KEY32;
  22901. #if FIPS_VERSION3_GE(6,0,0)
  22902. int key2Sz = KEY28;
  22903. #else
  22904. int key2Sz = KEY24;
  22905. #endif
  22906. byte secret[KEY32];
  22907. word32 secretLen = (word32)keySz;
  22908. /* Init stack variables. */
  22909. XMEMSET(&key, 0, sizeof(ecc_key));
  22910. XMEMSET(&key2, 0, sizeof(ecc_key));
  22911. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22912. XMEMSET(secret, 0, secretLen);
  22913. PRIVATE_KEY_UNLOCK();
  22914. /* Make keys */
  22915. ExpectIntEQ(wc_ecc_init(&key), 0);
  22916. ExpectIntEQ(wc_InitRng(&rng), 0);
  22917. ret = wc_ecc_make_key(&rng, keySz, &key);
  22918. #if defined(WOLFSSL_ASYNC_CRYPT)
  22919. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22920. #endif
  22921. ExpectIntEQ(ret, 0);
  22922. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22923. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22924. ExpectIntEQ(wc_InitRng(&rng), 0);
  22925. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22926. #if defined(WOLFSSL_ASYNC_CRYPT)
  22927. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22928. #endif
  22929. ExpectIntEQ(ret, 0);
  22930. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22931. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22932. !defined(HAVE_SELFTEST)
  22933. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22934. #endif
  22935. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22936. &secretLen), 0);
  22937. /* Pass in bad args. */
  22938. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22939. &secretLen), BAD_FUNC_ARG);
  22940. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22941. BAD_FUNC_ARG);
  22942. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22943. BAD_FUNC_ARG);
  22944. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22945. BAD_FUNC_ARG);
  22946. key.type = ECC_PUBLICKEY;
  22947. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22948. &secretLen), ECC_BAD_ARG_E);
  22949. PRIVATE_KEY_LOCK();
  22950. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22951. wc_ecc_free(&key);
  22952. wc_ecc_free(&key2);
  22953. #ifdef FP_ECC
  22954. wc_ecc_fp_free();
  22955. #endif
  22956. #endif
  22957. return EXPECT_RESULT();
  22958. } /* END test_wc_ecc_shared_secret_ssh */
  22959. /*
  22960. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22961. */
  22962. static int test_wc_ecc_verify_hash_ex(void)
  22963. {
  22964. EXPECT_DECLS;
  22965. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22966. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22967. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22968. ecc_key key;
  22969. WC_RNG rng;
  22970. int ret;
  22971. mp_int r;
  22972. mp_int s;
  22973. mp_int z;
  22974. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22975. unsigned char iHash[] = "Everyone gets Friday off.......";
  22976. unsigned char shortHash[] = TEST_STRING;
  22977. word32 hashlen = sizeof(hash);
  22978. word32 iHashLen = sizeof(iHash);
  22979. word32 shortHashLen = sizeof(shortHash);
  22980. int keySz = KEY32;
  22981. int verify_ok = 0;
  22982. XMEMSET(&key, 0, sizeof(ecc_key));
  22983. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22984. XMEMSET(&r, 0, sizeof(mp_int));
  22985. XMEMSET(&s, 0, sizeof(mp_int));
  22986. XMEMSET(&z, 0, sizeof(mp_int));
  22987. /* Initialize r, s and z. */
  22988. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22989. ExpectIntEQ(wc_ecc_init(&key), 0);
  22990. ExpectIntEQ(wc_InitRng(&rng), 0);
  22991. ret = wc_ecc_make_key(&rng, keySz, &key);
  22992. #if defined(WOLFSSL_ASYNC_CRYPT)
  22993. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22994. #endif
  22995. ExpectIntEQ(ret, 0);
  22996. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22997. /* verify_ok should be 1. */
  22998. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22999. 0);
  23000. ExpectIntEQ(verify_ok, 1);
  23001. /* verify_ok should be 0 */
  23002. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  23003. &key), 0);
  23004. ExpectIntEQ(verify_ok, 0);
  23005. /* verify_ok should be 0. */
  23006. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23007. &verify_ok, &key), 0);
  23008. ExpectIntEQ(verify_ok, 0);
  23009. /* Test bad args. */
  23010. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  23011. ECC_BAD_ARG_E);
  23012. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  23013. ECC_BAD_ARG_E);
  23014. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  23015. ECC_BAD_ARG_E);
  23016. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  23017. ECC_BAD_ARG_E);
  23018. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  23019. ECC_BAD_ARG_E);
  23020. /* Test bad args. */
  23021. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  23022. &verify_ok, &key), ECC_BAD_ARG_E);
  23023. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  23024. &verify_ok, &key), ECC_BAD_ARG_E);
  23025. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  23026. &verify_ok, &key), MP_ZERO_E);
  23027. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  23028. &verify_ok, &key), MP_ZERO_E);
  23029. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  23030. &verify_ok, &key), MP_ZERO_E);
  23031. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  23032. &key), ECC_BAD_ARG_E);
  23033. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  23034. &key), ECC_BAD_ARG_E);
  23035. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23036. &verify_ok, NULL), ECC_BAD_ARG_E);
  23037. wc_ecc_free(&key);
  23038. mp_free(&r);
  23039. mp_free(&s);
  23040. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23041. #endif
  23042. return EXPECT_RESULT();
  23043. } /* END test_wc_ecc_verify_hash_ex */
  23044. /*
  23045. * Testing wc_ecc_mulmod()
  23046. */
  23047. static int test_wc_ecc_mulmod(void)
  23048. {
  23049. EXPECT_DECLS;
  23050. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  23051. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  23052. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23053. ecc_key key1;
  23054. ecc_key key2;
  23055. ecc_key key3;
  23056. WC_RNG rng;
  23057. int ret;
  23058. XMEMSET(&key1, 0, sizeof(ecc_key));
  23059. XMEMSET(&key2, 0, sizeof(ecc_key));
  23060. XMEMSET(&key3, 0, sizeof(ecc_key));
  23061. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23062. ExpectIntEQ(wc_ecc_init(&key1), 0);
  23063. ExpectIntEQ(wc_ecc_init(&key2), 0);
  23064. ExpectIntEQ(wc_ecc_init(&key3), 0);
  23065. ExpectIntEQ(wc_InitRng(&rng), 0);
  23066. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  23067. #if defined(WOLFSSL_ASYNC_CRYPT)
  23068. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  23069. #endif
  23070. ExpectIntEQ(ret, 0);
  23071. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23072. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  23073. key1.dp->Af, ECC_SECP256R1), 0);
  23074. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  23075. key1.dp->prime, ECC_SECP256R1), 0);
  23076. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  23077. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  23078. 1), 0);
  23079. /* Test bad args. */
  23080. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  23081. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  23082. ECC_BAD_ARG_E);
  23083. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  23084. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  23085. ECC_BAD_ARG_E);
  23086. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  23087. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  23088. ECC_BAD_ARG_E);
  23089. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  23090. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  23091. wc_ecc_free(&key1);
  23092. wc_ecc_free(&key2);
  23093. wc_ecc_free(&key3);
  23094. #ifdef FP_ECC
  23095. wc_ecc_fp_free();
  23096. #endif
  23097. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  23098. return EXPECT_RESULT();
  23099. } /* END test_wc_ecc_mulmod */
  23100. /*
  23101. * Testing wc_ecc_is_valid_idx()
  23102. */
  23103. static int test_wc_ecc_is_valid_idx(void)
  23104. {
  23105. EXPECT_DECLS;
  23106. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  23107. ecc_key key;
  23108. WC_RNG rng;
  23109. int ret;
  23110. int iVal = -2;
  23111. int iVal2 = 3000;
  23112. XMEMSET(&key, 0, sizeof(ecc_key));
  23113. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23114. ExpectIntEQ(wc_ecc_init(&key), 0);
  23115. ExpectIntEQ(wc_InitRng(&rng), 0);
  23116. ret = wc_ecc_make_key(&rng, 32, &key);
  23117. #if defined(WOLFSSL_ASYNC_CRYPT)
  23118. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23119. #endif
  23120. ExpectIntEQ(ret, 0);
  23121. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  23122. /* Test bad args. */
  23123. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  23124. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  23125. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23126. wc_ecc_free(&key);
  23127. #ifdef FP_ECC
  23128. wc_ecc_fp_free();
  23129. #endif
  23130. #endif
  23131. return EXPECT_RESULT();
  23132. } /* END test_wc_ecc_is_valid_idx */
  23133. /*
  23134. * Testing wc_ecc_get_curve_id_from_oid()
  23135. */
  23136. static int test_wc_ecc_get_curve_id_from_oid(void)
  23137. {
  23138. EXPECT_DECLS;
  23139. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  23140. !defined(HAVE_FIPS)
  23141. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  23142. word32 len = sizeof(oid);
  23143. /* Bad Cases */
  23144. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  23145. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  23146. /* Good Case */
  23147. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  23148. #endif
  23149. return EXPECT_RESULT();
  23150. } /* END test_wc_ecc_get_curve_id_from_oid */
  23151. /*
  23152. * Testing wc_ecc_sig_size_calc()
  23153. */
  23154. static int test_wc_ecc_sig_size_calc(void)
  23155. {
  23156. EXPECT_DECLS;
  23157. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  23158. ecc_key key;
  23159. WC_RNG rng;
  23160. int sz = 0;
  23161. int ret;
  23162. XMEMSET(&key, 0, sizeof(ecc_key));
  23163. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23164. ExpectIntEQ(wc_ecc_init(&key), 0);
  23165. ExpectIntEQ(wc_InitRng(&rng), 0);
  23166. ret = wc_ecc_make_key(&rng, 16, &key);
  23167. #if defined(WOLFSSL_ASYNC_CRYPT)
  23168. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23169. #endif
  23170. #if FIPS_VERSION3_GE(6,0,0)
  23171. ExpectIntEQ(ret, BAD_FUNC_ARG);
  23172. #else
  23173. ExpectIntEQ(ret, 0);
  23174. #endif
  23175. #if FIPS_VERSION3_LT(6,0,0)
  23176. sz = key.dp->size;
  23177. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  23178. #else
  23179. (void) sz;
  23180. #endif
  23181. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23182. wc_ecc_free(&key);
  23183. #endif
  23184. return EXPECT_RESULT();
  23185. } /* END test_wc_ecc_sig_size_calc */
  23186. /*
  23187. * Testing wc_ecc_sm2_make_key()
  23188. */
  23189. static int test_wc_ecc_sm2_make_key(void)
  23190. {
  23191. int res = TEST_SKIPPED;
  23192. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  23193. EXPECT_DECLS;
  23194. WC_RNG rng[1];
  23195. ecc_key key[1];
  23196. XMEMSET(rng, 0, sizeof(*rng));
  23197. XMEMSET(key, 0, sizeof(*key));
  23198. ExpectIntEQ(wc_InitRng(rng), 0);
  23199. ExpectIntEQ(wc_ecc_init(key), 0);
  23200. /* Test invalid parameters. */
  23201. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  23202. BAD_FUNC_ARG);
  23203. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  23204. BAD_FUNC_ARG);
  23205. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  23206. BAD_FUNC_ARG);
  23207. /* Test valid parameters. */
  23208. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23209. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  23210. wc_ecc_free(key);
  23211. wc_FreeRng(rng);
  23212. #ifdef FP_ECC
  23213. wc_ecc_fp_free();
  23214. #endif
  23215. res = EXPECT_RESULT();
  23216. #endif
  23217. return res;
  23218. }
  23219. /*
  23220. * Testing wc_ecc_sm2_shared_secret()
  23221. */
  23222. static int test_wc_ecc_sm2_shared_secret(void)
  23223. {
  23224. int res = TEST_SKIPPED;
  23225. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  23226. EXPECT_DECLS;
  23227. WC_RNG rng[1];
  23228. ecc_key keyA[1];
  23229. ecc_key keyB[1];
  23230. byte outA[32];
  23231. byte outB[32];
  23232. word32 outALen = 32;
  23233. word32 outBLen = 32;
  23234. XMEMSET(rng, 0, sizeof(*rng));
  23235. XMEMSET(keyA, 0, sizeof(*keyA));
  23236. XMEMSET(keyB, 0, sizeof(*keyB));
  23237. ExpectIntEQ(wc_InitRng(rng), 0);
  23238. ExpectIntEQ(wc_ecc_init(keyA), 0);
  23239. ExpectIntEQ(wc_ecc_init(keyB), 0);
  23240. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  23241. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  23242. #ifdef ECC_TIMING_RESISTANT
  23243. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  23244. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  23245. #endif
  23246. /* Test invalid parameters. */
  23247. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  23248. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  23249. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  23250. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  23251. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  23252. BAD_FUNC_ARG);
  23253. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  23254. BAD_FUNC_ARG);
  23255. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  23256. BAD_FUNC_ARG);
  23257. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  23258. BAD_FUNC_ARG);
  23259. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  23260. /* Test valid parameters. */
  23261. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  23262. ExpectIntLE(outALen, 32);
  23263. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  23264. ExpectIntLE(outBLen, 32);
  23265. ExpectIntEQ(outALen, outBLen);
  23266. ExpectBufEQ(outA, outB, outALen);
  23267. wc_ecc_free(keyB);
  23268. wc_ecc_free(keyA);
  23269. wc_FreeRng(rng);
  23270. #ifdef FP_ECC
  23271. wc_ecc_fp_free();
  23272. #endif
  23273. res = EXPECT_RESULT();
  23274. #endif
  23275. return res;
  23276. }
  23277. /*
  23278. * Testing wc_ecc_sm2_create_digest()
  23279. */
  23280. static int test_wc_ecc_sm2_create_digest(void)
  23281. {
  23282. int res = TEST_SKIPPED;
  23283. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  23284. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  23285. EXPECT_DECLS;
  23286. ecc_key key[1];
  23287. enum wc_HashType hashType;
  23288. unsigned char pub[] = {
  23289. 0x04,
  23290. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  23291. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23292. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23293. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23294. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23295. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23296. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23297. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23298. };
  23299. unsigned char id[] = {
  23300. 0x01, 0x02, 0x03,
  23301. };
  23302. unsigned char msg[] = {
  23303. 0x01, 0x02, 0x03,
  23304. };
  23305. unsigned char hash[32];
  23306. #ifdef WOLFSSL_SM3
  23307. unsigned char expHash[32] = {
  23308. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  23309. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  23310. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  23311. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  23312. };
  23313. #else
  23314. unsigned char expHash[32] = {
  23315. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  23316. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  23317. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  23318. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  23319. };
  23320. #endif
  23321. #ifdef WOLFSSL_SM3
  23322. hashType = WC_HASH_TYPE_SM3;
  23323. #else
  23324. hashType = WC_HASH_TYPE_SHA256;
  23325. #endif
  23326. XMEMSET(key, 0, sizeof(*key));
  23327. ExpectIntEQ(wc_ecc_init(key), 0);
  23328. /* Test with no curve set. */
  23329. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23330. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  23331. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23332. /* Test invalid parameters. */
  23333. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  23334. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  23335. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  23336. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  23337. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  23338. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  23339. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  23340. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  23341. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  23342. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  23343. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  23344. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  23345. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  23346. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  23347. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23348. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  23349. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23350. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  23351. /* Bad hash type. */
  23352. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  23353. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23354. -1, hash, 0, key), BAD_FUNC_ARG);
  23355. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  23356. /* Bad hash size. */
  23357. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23358. hashType, hash, 0, key), BUFFER_E);
  23359. /* Test valid parameters. */
  23360. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23361. hashType, hash, sizeof(hash), key), 0);
  23362. ExpectBufEQ(hash, expHash, sizeof(expHash));
  23363. wc_ecc_free(key);
  23364. res = EXPECT_RESULT();
  23365. #endif
  23366. return res;
  23367. }
  23368. /*
  23369. * Testing wc_ecc_sm2_verify_hash_ex()
  23370. */
  23371. static int test_wc_ecc_sm2_verify_hash_ex(void)
  23372. {
  23373. int res = TEST_SKIPPED;
  23374. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  23375. defined(WOLFSSL_PUBLIC_MP)
  23376. EXPECT_DECLS;
  23377. ecc_key key[1];
  23378. mp_int r[1];
  23379. mp_int s[1];
  23380. int verified;
  23381. unsigned char pub[] = {
  23382. 0x04,
  23383. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  23384. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23385. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23386. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23387. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23388. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23389. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23390. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23391. };
  23392. unsigned char hash[] = {
  23393. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23394. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23395. 0x50, 0x69, 0x5B, 0x20
  23396. };
  23397. unsigned char rData[] = {
  23398. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  23399. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  23400. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  23401. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  23402. };
  23403. unsigned char sData[] = {
  23404. 0x1D,
  23405. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23406. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23407. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23408. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23409. };
  23410. unsigned char rBadData[] = {
  23411. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  23412. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  23413. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  23414. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  23415. };
  23416. XMEMSET(key, 0, sizeof(*key));
  23417. XMEMSET(r, 0, sizeof(*r));
  23418. XMEMSET(s, 0, sizeof(*s));
  23419. ExpectIntEQ(mp_init(r), 0);
  23420. ExpectIntEQ(mp_init(s), 0);
  23421. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  23422. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  23423. ExpectIntEQ(wc_ecc_init(key), 0);
  23424. /* Test with no curve set. */
  23425. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23426. &verified, key), BAD_FUNC_ARG);
  23427. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23428. /* Test invalid parameters. */
  23429. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23430. NULL, NULL), BAD_FUNC_ARG);
  23431. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  23432. NULL, NULL), BAD_FUNC_ARG);
  23433. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  23434. NULL, NULL), BAD_FUNC_ARG);
  23435. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  23436. NULL, NULL), BAD_FUNC_ARG);
  23437. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23438. &verified, NULL), BAD_FUNC_ARG);
  23439. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23440. NULL, key), BAD_FUNC_ARG);
  23441. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  23442. &verified, key), BAD_FUNC_ARG);
  23443. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  23444. &verified, key), BAD_FUNC_ARG);
  23445. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  23446. &verified, key), BAD_FUNC_ARG);
  23447. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23448. NULL, key), BAD_FUNC_ARG);
  23449. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23450. &verified, NULL), BAD_FUNC_ARG);
  23451. /* Make key not on the SM2 curve. */
  23452. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23453. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23454. &verified, key), BAD_FUNC_ARG);
  23455. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23456. /* Test valid parameters. */
  23457. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23458. &verified, key), 0);
  23459. ExpectIntEQ(verified, 1);
  23460. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  23461. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23462. &verified, key), 0);
  23463. ExpectIntEQ(verified, 0);
  23464. mp_free(s);
  23465. mp_free(r);
  23466. wc_ecc_free(key);
  23467. #ifdef FP_ECC
  23468. wc_ecc_fp_free();
  23469. #endif
  23470. res = EXPECT_RESULT();
  23471. #endif
  23472. return res;
  23473. }
  23474. /*
  23475. * Testing wc_ecc_sm2_verify_hash()
  23476. */
  23477. static int test_wc_ecc_sm2_verify_hash(void)
  23478. {
  23479. int res = TEST_SKIPPED;
  23480. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  23481. EXPECT_DECLS;
  23482. ecc_key key[1];
  23483. int verified;
  23484. unsigned char pub[] = {
  23485. 0x04,
  23486. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  23487. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23488. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23489. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23490. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23491. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23492. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23493. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23494. };
  23495. unsigned char hash[] = {
  23496. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23497. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23498. 0x50, 0x69, 0x5B, 0x20
  23499. };
  23500. unsigned char sig[] = {
  23501. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23502. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23503. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23504. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23505. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23506. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23507. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23508. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23509. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23510. };
  23511. unsigned char sigBad[] = {
  23512. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23513. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23514. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23515. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23516. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23517. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23518. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23519. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23520. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  23521. };
  23522. XMEMSET(key, 0, sizeof(*key));
  23523. ExpectIntEQ(wc_ecc_init(key), 0);
  23524. /* Test with no curve set. */
  23525. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23526. &verified, key), BAD_FUNC_ARG);
  23527. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23528. /* Test invalid parameters. */
  23529. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23530. NULL, NULL), BAD_FUNC_ARG);
  23531. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23532. NULL, NULL), BAD_FUNC_ARG);
  23533. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23534. NULL, NULL), BAD_FUNC_ARG);
  23535. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23536. &verified, NULL), BAD_FUNC_ARG);
  23537. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23538. NULL, key), BAD_FUNC_ARG);
  23539. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23540. &verified, key), BAD_FUNC_ARG);
  23541. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23542. &verified, key), BAD_FUNC_ARG);
  23543. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23544. NULL, key), BAD_FUNC_ARG);
  23545. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23546. &verified, NULL), BAD_FUNC_ARG);
  23547. /* Make key not on the SM2 curve. */
  23548. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23549. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23550. &verified, key), BAD_FUNC_ARG);
  23551. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23552. /* Test valid parameters. */
  23553. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23554. &verified, key), 0);
  23555. ExpectIntEQ(verified, 1);
  23556. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  23557. sizeof(hash), &verified, key), 0);
  23558. ExpectIntEQ(verified, 0);
  23559. wc_ecc_free(key);
  23560. #ifdef FP_ECC
  23561. wc_ecc_fp_free();
  23562. #endif
  23563. res = EXPECT_RESULT();
  23564. #endif
  23565. return res;
  23566. }
  23567. /*
  23568. * Testing wc_ecc_sm2_verify_hash_ex()
  23569. */
  23570. static int test_wc_ecc_sm2_sign_hash_ex(void)
  23571. {
  23572. int res = TEST_SKIPPED;
  23573. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  23574. defined(WOLFSSL_PUBLIC_MP)
  23575. EXPECT_DECLS;
  23576. WC_RNG rng[1];
  23577. ecc_key key[1];
  23578. mp_int r[1];
  23579. mp_int s[1];
  23580. unsigned char hash[32];
  23581. #ifdef HAVE_ECC_VERIFY
  23582. int verified;
  23583. #endif
  23584. XMEMSET(rng, 0, sizeof(*rng));
  23585. XMEMSET(key, 0, sizeof(*key));
  23586. XMEMSET(r, 0, sizeof(*r));
  23587. XMEMSET(s, 0, sizeof(*s));
  23588. ExpectIntEQ(wc_InitRng(rng), 0);
  23589. ExpectIntEQ(mp_init(r), 0);
  23590. ExpectIntEQ(mp_init(s), 0);
  23591. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23592. ExpectIntEQ(wc_ecc_init(key), 0);
  23593. /* Test with no curve set. */
  23594. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23595. BAD_FUNC_ARG);
  23596. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23597. /* Test invalid parameters. */
  23598. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23599. NULL), BAD_FUNC_ARG);
  23600. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  23601. NULL), BAD_FUNC_ARG);
  23602. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  23603. NULL), BAD_FUNC_ARG);
  23604. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  23605. NULL), BAD_FUNC_ARG);
  23606. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  23607. NULL), BAD_FUNC_ARG);
  23608. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23609. s), BAD_FUNC_ARG);
  23610. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  23611. BAD_FUNC_ARG);
  23612. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  23613. BAD_FUNC_ARG);
  23614. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  23615. BAD_FUNC_ARG);
  23616. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  23617. BAD_FUNC_ARG);
  23618. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  23619. BAD_FUNC_ARG);
  23620. /* Make key not on the SM2 curve. */
  23621. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23622. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23623. BAD_FUNC_ARG);
  23624. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23625. #ifdef WOLFSSL_SP_MATH_ALL
  23626. {
  23627. mp_int smallR[1];
  23628. sp_init_size(smallR, 1);
  23629. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  23630. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  23631. smallR, s), 0);
  23632. }
  23633. #endif
  23634. /* Test valid parameters. */
  23635. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23636. 0);
  23637. #ifdef HAVE_ECC_VERIFY
  23638. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  23639. key), 0);
  23640. ExpectIntEQ(verified, 1);
  23641. #endif
  23642. mp_free(s);
  23643. mp_free(r);
  23644. wc_ecc_free(key);
  23645. wc_FreeRng(rng);
  23646. #ifdef FP_ECC
  23647. wc_ecc_fp_free();
  23648. #endif
  23649. res = EXPECT_RESULT();
  23650. #endif
  23651. return res;
  23652. }
  23653. /*
  23654. * Testing wc_ecc_sm2_verify_hash()
  23655. */
  23656. static int test_wc_ecc_sm2_sign_hash(void)
  23657. {
  23658. int res = TEST_SKIPPED;
  23659. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  23660. EXPECT_DECLS;
  23661. WC_RNG rng[1];
  23662. ecc_key key[1];
  23663. unsigned char hash[32];
  23664. unsigned char sig[72];
  23665. word32 sigSz = sizeof(sig);
  23666. #ifdef HAVE_ECC_VERIFY
  23667. int verified;
  23668. #endif
  23669. XMEMSET(rng, 0, sizeof(*rng));
  23670. XMEMSET(key, 0, sizeof(*key));
  23671. ExpectIntEQ(wc_InitRng(rng), 0);
  23672. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23673. ExpectIntEQ(wc_ecc_init(key), 0);
  23674. /* Test with no curve set. */
  23675. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23676. BAD_FUNC_ARG);
  23677. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23678. /* Test invalid parameters. */
  23679. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23680. NULL), BAD_FUNC_ARG);
  23681. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  23682. NULL), BAD_FUNC_ARG);
  23683. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  23684. NULL), BAD_FUNC_ARG);
  23685. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  23686. NULL), BAD_FUNC_ARG);
  23687. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  23688. NULL), BAD_FUNC_ARG);
  23689. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23690. key), BAD_FUNC_ARG);
  23691. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  23692. key), BAD_FUNC_ARG);
  23693. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  23694. key), BAD_FUNC_ARG);
  23695. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  23696. key), BAD_FUNC_ARG);
  23697. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  23698. key), BAD_FUNC_ARG);
  23699. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  23700. NULL), BAD_FUNC_ARG);
  23701. /* Make key not on the SM2 curve. */
  23702. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23703. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23704. BAD_FUNC_ARG);
  23705. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23706. /* Test valid parameters. */
  23707. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23708. 0);
  23709. #ifdef HAVE_ECC_VERIFY
  23710. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  23711. &verified, key), 0);
  23712. ExpectIntEQ(verified, 1);
  23713. #endif
  23714. wc_ecc_free(key);
  23715. wc_FreeRng(rng);
  23716. #ifdef FP_ECC
  23717. wc_ecc_fp_free();
  23718. #endif
  23719. res = EXPECT_RESULT();
  23720. #endif
  23721. return res;
  23722. }
  23723. /*
  23724. * Testing ToTraditional
  23725. */
  23726. static int test_ToTraditional(void)
  23727. {
  23728. EXPECT_DECLS;
  23729. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23730. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23731. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  23732. XFILE f = XBADFILE;
  23733. byte input[TWOK_BUF];
  23734. word32 sz = 0;
  23735. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  23736. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  23737. if (f != XBADFILE)
  23738. XFCLOSE(f);
  23739. /* Good case */
  23740. ExpectIntGT(ToTraditional(input, sz), 0);
  23741. /* Bad cases */
  23742. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  23743. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  23744. #ifdef WOLFSSL_ASN_TEMPLATE
  23745. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  23746. #else
  23747. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  23748. #endif
  23749. #endif
  23750. return EXPECT_RESULT();
  23751. } /* End test_ToTraditional*/
  23752. /*
  23753. * Testing wc_EccPrivateKeyToDer
  23754. */
  23755. static int test_wc_EccPrivateKeyToDer(void)
  23756. {
  23757. EXPECT_DECLS;
  23758. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23759. byte output[ONEK_BUF];
  23760. ecc_key eccKey;
  23761. WC_RNG rng;
  23762. word32 inLen;
  23763. word32 outLen = 0;
  23764. int ret;
  23765. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23766. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23767. PRIVATE_KEY_UNLOCK();
  23768. ExpectIntEQ(wc_InitRng(&rng), 0);
  23769. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23770. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23771. #if defined(WOLFSSL_ASYNC_CRYPT)
  23772. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23773. #endif
  23774. ExpectIntEQ(ret, 0);
  23775. inLen = (word32)sizeof(output);
  23776. /* Bad Cases */
  23777. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23778. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23779. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  23780. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  23781. /* Good Case */
  23782. ExpectIntGT(outLen = (word32)wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  23783. wc_ecc_free(&eccKey);
  23784. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23785. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23786. {
  23787. /* test importing private only into a PKEY struct */
  23788. EC_KEY* ec = NULL;
  23789. EVP_PKEY* pkey = NULL;
  23790. const unsigned char* der;
  23791. der = output;
  23792. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  23793. der = output;
  23794. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  23795. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23796. if (EXPECT_FAIL()) {
  23797. EC_KEY_free(ec);
  23798. }
  23799. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23800. }
  23801. #endif
  23802. PRIVATE_KEY_LOCK();
  23803. #endif
  23804. return EXPECT_RESULT();
  23805. } /* End test_wc_EccPrivateKeyToDer*/
  23806. /*
  23807. * Testing wc_DhPublicKeyDecode
  23808. */
  23809. static int test_wc_DhPublicKeyDecode(void)
  23810. {
  23811. EXPECT_DECLS;
  23812. #ifndef NO_DH
  23813. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23814. DhKey key;
  23815. word32 inOutIdx;
  23816. XMEMSET(&key, 0, sizeof(DhKey));
  23817. ExpectIntEQ(wc_InitDhKey(&key), 0);
  23818. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  23819. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23820. BAD_FUNC_ARG);
  23821. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23822. BAD_FUNC_ARG);
  23823. inOutIdx = 0;
  23824. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23825. BAD_FUNC_ARG);
  23826. inOutIdx = 0;
  23827. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23828. BAD_FUNC_ARG);
  23829. inOutIdx = 0;
  23830. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23831. sizeof_dh_pub_key_der_2048), 0);
  23832. ExpectIntNE(key.p.used, 0);
  23833. ExpectIntNE(key.g.used, 0);
  23834. ExpectIntEQ(key.q.used, 0);
  23835. ExpectIntNE(key.pub.used, 0);
  23836. ExpectIntEQ(key.priv.used, 0);
  23837. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  23838. #endif
  23839. #endif /* !NO_DH */
  23840. return EXPECT_RESULT();
  23841. }
  23842. /*
  23843. * Testing wc_Ed25519KeyToDer
  23844. */
  23845. static int test_wc_Ed25519KeyToDer(void)
  23846. {
  23847. EXPECT_DECLS;
  23848. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23849. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23850. byte output[ONEK_BUF];
  23851. ed25519_key ed25519Key;
  23852. WC_RNG rng;
  23853. word32 inLen;
  23854. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23855. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23856. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23857. ExpectIntEQ(wc_InitRng(&rng), 0);
  23858. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23859. inLen = (word32)sizeof(output);
  23860. /* Bad Cases */
  23861. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23862. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23863. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  23864. /* Good Cases */
  23865. /* length only */
  23866. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  23867. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  23868. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23869. wc_ed25519_free(&ed25519Key);
  23870. #endif
  23871. return EXPECT_RESULT();
  23872. } /* End test_wc_Ed25519KeyToDer*/
  23873. /*
  23874. * Testing wc_Ed25519PrivateKeyToDer
  23875. */
  23876. static int test_wc_Ed25519PrivateKeyToDer(void)
  23877. {
  23878. EXPECT_DECLS;
  23879. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23880. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23881. byte output[ONEK_BUF];
  23882. ed25519_key ed25519PrivKey;
  23883. WC_RNG rng;
  23884. word32 inLen;
  23885. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  23886. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23887. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  23888. ExpectIntEQ(wc_InitRng(&rng), 0);
  23889. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  23890. 0);
  23891. inLen = (word32)sizeof(output);
  23892. /* Bad Cases */
  23893. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23894. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23895. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  23896. BAD_FUNC_ARG);
  23897. /* Good Cases */
  23898. /* length only */
  23899. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  23900. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  23901. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23902. wc_ed25519_free(&ed25519PrivKey);
  23903. #endif
  23904. return EXPECT_RESULT();
  23905. } /* End test_wc_Ed25519PrivateKeyToDer*/
  23906. /*
  23907. * Testing wc_Ed448KeyToDer
  23908. */
  23909. static int test_wc_Ed448KeyToDer(void)
  23910. {
  23911. EXPECT_DECLS;
  23912. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23913. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23914. byte output[ONEK_BUF];
  23915. ed448_key ed448Key;
  23916. WC_RNG rng;
  23917. word32 inLen;
  23918. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23919. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23920. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23921. ExpectIntEQ(wc_InitRng(&rng), 0);
  23922. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23923. inLen = (word32)sizeof(output);
  23924. /* Bad Cases */
  23925. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23926. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23927. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  23928. /* Good Cases */
  23929. /* length only */
  23930. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  23931. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  23932. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23933. wc_ed448_free(&ed448Key);
  23934. #endif
  23935. return EXPECT_RESULT();
  23936. } /* End test_wc_Ed448KeyToDer*/
  23937. /*
  23938. * Testing wc_Ed448PrivateKeyToDer
  23939. */
  23940. static int test_wc_Ed448PrivateKeyToDer(void)
  23941. {
  23942. EXPECT_DECLS;
  23943. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23944. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23945. byte output[ONEK_BUF];
  23946. ed448_key ed448PrivKey;
  23947. WC_RNG rng;
  23948. word32 inLen;
  23949. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23950. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23951. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23952. ExpectIntEQ(wc_InitRng(&rng), 0);
  23953. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23954. 0);
  23955. inLen = (word32)sizeof(output);
  23956. /* Bad Cases */
  23957. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23958. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23959. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23960. BAD_FUNC_ARG);
  23961. /* Good cases */
  23962. /* length only */
  23963. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23964. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23965. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23966. wc_ed448_free(&ed448PrivKey);
  23967. #endif
  23968. return EXPECT_RESULT();
  23969. } /* End test_wc_Ed448PrivateKeyToDer*/
  23970. /*
  23971. * Testing wc_Curve448PrivateKeyToDer
  23972. */
  23973. static int test_wc_Curve448PrivateKeyToDer(void)
  23974. {
  23975. EXPECT_DECLS;
  23976. #if defined(HAVE_CURVE448) && defined(HAVE_CURVE448_KEY_EXPORT) && \
  23977. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23978. byte output[ONEK_BUF];
  23979. curve448_key curve448PrivKey;
  23980. WC_RNG rng;
  23981. word32 inLen;
  23982. XMEMSET(&curve448PrivKey, 0, sizeof(curve448PrivKey));
  23983. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23984. ExpectIntEQ(wc_curve448_init(&curve448PrivKey), 0);
  23985. ExpectIntEQ(wc_InitRng(&rng), 0);
  23986. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &curve448PrivKey),
  23987. 0);
  23988. inLen = (word32)sizeof(output);
  23989. /* Bad Cases */
  23990. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23991. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23992. ExpectIntEQ(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, 0),
  23993. BAD_FUNC_ARG);
  23994. /* Good cases */
  23995. /* length only */
  23996. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, inLen), 0);
  23997. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, inLen), 0);
  23998. /* Bad Cases */
  23999. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  24000. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, output, inLen, 0), BAD_FUNC_ARG);
  24001. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 0),
  24002. BUFFER_E);
  24003. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 1),
  24004. BUFFER_E);
  24005. /* Good cases */
  24006. /* length only */
  24007. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 0), 0);
  24008. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 1), 0);
  24009. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 0), 0);
  24010. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 1), 0);
  24011. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24012. wc_curve448_free(&curve448PrivKey);
  24013. #endif
  24014. return EXPECT_RESULT();
  24015. } /* End wc_Curve448PrivateKeyToDer*/
  24016. static int test_wc_kyber_make_key_kats(void)
  24017. {
  24018. EXPECT_DECLS;
  24019. #if defined(WOLFSSL_HAVE_KYBER) && defined(WOLFSSL_WC_KYBER) && \
  24020. defined(WOLFSSL_ML_KEM)
  24021. KyberKey* key;
  24022. #ifndef WOLFSSL_NO_KYBER512
  24023. static const byte seed_512[KYBER_MAKEKEY_RAND_SZ] = {
  24024. 0xCD, 0x11, 0x9A, 0xFD, 0xC8, 0x55, 0x94, 0x42,
  24025. 0x42, 0x4A, 0x87, 0xC1, 0x3E, 0xA1, 0x01, 0xE2,
  24026. 0x9F, 0xCA, 0x11, 0x88, 0x18, 0x69, 0x07, 0x7E,
  24027. 0x40, 0x92, 0xE7, 0x51, 0xBE, 0xDC, 0xA8, 0xBC,
  24028. 0xCD, 0x11, 0x9A, 0xFD, 0xC8, 0x55, 0x94, 0x42,
  24029. 0x42, 0x4A, 0x87, 0xC1, 0x3E, 0xA1, 0x01, 0xE2,
  24030. 0x9F, 0xCA, 0x11, 0x88, 0x18, 0x69, 0x07, 0x7E,
  24031. 0x40, 0x92, 0xE7, 0x51, 0xBE, 0xDC, 0xA8, 0xBC
  24032. };
  24033. static const byte ek_512[KYBER512_PUBLIC_KEY_SIZE] = {
  24034. 0xC6, 0x5A, 0x1D, 0x9D, 0x47, 0x97, 0x77, 0xE6,
  24035. 0x90, 0x5A, 0x91, 0xA5, 0xCB, 0x24, 0x55, 0x1C,
  24036. 0x8B, 0x1E, 0x52, 0xA3, 0xC7, 0x7B, 0x63, 0x31,
  24037. 0x3F, 0xFC, 0x8B, 0x58, 0x17, 0x81, 0x52, 0x59,
  24038. 0xA6, 0xAD, 0xB5, 0x96, 0x45, 0xDC, 0x4B, 0xB1,
  24039. 0x43, 0x6D, 0x51, 0xE6, 0x2A, 0x09, 0x68, 0x34,
  24040. 0xAF, 0x43, 0x77, 0x25, 0x10, 0xC4, 0xED, 0xF3,
  24041. 0x4C, 0xDE, 0x0A, 0x5B, 0x57, 0xC1, 0x45, 0xE6,
  24042. 0x87, 0xCB, 0x87, 0x16, 0x2F, 0x00, 0x1C, 0x21,
  24043. 0xC9, 0xE1, 0x93, 0x4A, 0xC1, 0x1A, 0xAF, 0xA7,
  24044. 0x0F, 0xF8, 0x10, 0x73, 0x26, 0x50, 0xB3, 0x2A,
  24045. 0x30, 0x18, 0xA7, 0xC5, 0x0C, 0xD7, 0x36, 0x79,
  24046. 0x62, 0x22, 0xC8, 0xAB, 0x82, 0x1A, 0x92, 0x83,
  24047. 0xBE, 0x1C, 0xC2, 0x04, 0xC3, 0xF1, 0x63, 0x0D,
  24048. 0x3C, 0xCC, 0xDB, 0x0A, 0x9A, 0x3D, 0x17, 0x55,
  24049. 0x2B, 0x91, 0x58, 0xC0, 0x66, 0x4E, 0x5D, 0x6A,
  24050. 0x04, 0xB0, 0xFA, 0x36, 0xDE, 0x45, 0x86, 0x2A,
  24051. 0x46, 0xA3, 0x9E, 0xC5, 0x97, 0xAE, 0x42, 0xC3,
  24052. 0x11, 0xC4, 0xAC, 0x22, 0x4A, 0x72, 0xD6, 0xF2,
  24053. 0x53, 0xBB, 0x52, 0x35, 0xF7, 0xA2, 0xB8, 0xB0,
  24054. 0xF2, 0x4D, 0x13, 0x76, 0xAF, 0x58, 0x87, 0x46,
  24055. 0xF3, 0xBB, 0x8E, 0x03, 0x65, 0x07, 0x87, 0x61,
  24056. 0xCA, 0xB9, 0x83, 0xA4, 0xA6, 0xA9, 0x40, 0xA3,
  24057. 0xD9, 0x97, 0x04, 0x7A, 0x8F, 0x36, 0xA7, 0x31,
  24058. 0xE8, 0x96, 0x52, 0x36, 0xC3, 0x7B, 0xF2, 0x00,
  24059. 0x08, 0x2F, 0x82, 0x1D, 0xCA, 0x77, 0x16, 0xC4,
  24060. 0x44, 0xA9, 0x0B, 0xEC, 0x53, 0x07, 0x4B, 0xBA,
  24061. 0x58, 0xC1, 0x32, 0xBF, 0xB9, 0xA2, 0xAC, 0xE2,
  24062. 0xCE, 0xC9, 0xAA, 0x65, 0x8E, 0xAC, 0x12, 0x32,
  24063. 0xCC, 0xCA, 0x3C, 0x81, 0x7A, 0x92, 0xC1, 0x19,
  24064. 0x5C, 0x05, 0xC0, 0xE1, 0xD6, 0x63, 0x9F, 0xD2,
  24065. 0xAD, 0xE5, 0x31, 0x60, 0x7D, 0x48, 0x8B, 0x74,
  24066. 0xA7, 0x47, 0xCF, 0xF4, 0x7F, 0xCA, 0x5C, 0x8B,
  24067. 0x21, 0x63, 0xCA, 0x03, 0xC5, 0x45, 0xED, 0x10,
  24068. 0x32, 0x78, 0x43, 0x0C, 0x60, 0xB2, 0x38, 0x1A,
  24069. 0x09, 0x42, 0x7F, 0xD1, 0x30, 0xF8, 0x59, 0xBF,
  24070. 0x5D, 0xB7, 0x76, 0xDA, 0x09, 0x5D, 0xCA, 0x58,
  24071. 0x04, 0xFA, 0x63, 0xB0, 0xD7, 0xD8, 0x7F, 0xA9,
  24072. 0x41, 0x5C, 0x72, 0xFB, 0x51, 0x87, 0x2A, 0x98,
  24073. 0x9F, 0x46, 0x6C, 0x98, 0x4B, 0xC7, 0x4C, 0x29,
  24074. 0xB8, 0x63, 0x20, 0x19, 0xCA, 0x04, 0x0C, 0x9C,
  24075. 0xA3, 0x5E, 0x22, 0x60, 0x8D, 0xAA, 0x70, 0x35,
  24076. 0x7A, 0xE2, 0xC3, 0xAD, 0x83, 0x63, 0x1F, 0xAA,
  24077. 0x17, 0x4E, 0x0A, 0xCD, 0xF5, 0xDB, 0xBF, 0x3C,
  24078. 0xF6, 0x8A, 0x05, 0xB6, 0x54, 0x3A, 0xB6, 0x26,
  24079. 0x8E, 0x1A, 0x51, 0xB0, 0x93, 0x2C, 0x17, 0xB0,
  24080. 0x0A, 0x13, 0x71, 0xB2, 0xDA, 0xB2, 0x41, 0xF9,
  24081. 0x2A, 0x43, 0xFF, 0xB4, 0x56, 0xD0, 0xA8, 0xC8,
  24082. 0x86, 0x0A, 0x8E, 0x28, 0xA6, 0x1A, 0x21, 0x30,
  24083. 0x7C, 0xC0, 0x45, 0x6D, 0xA4, 0x24, 0x29, 0x05,
  24084. 0xCB, 0x1D, 0x3D, 0x0B, 0xBD, 0x81, 0xBB, 0x8E,
  24085. 0xE2, 0x74, 0xA4, 0x3C, 0x76, 0xC3, 0x10, 0x01,
  24086. 0x95, 0x15, 0xFC, 0xC1, 0x40, 0x46, 0x7C, 0x33,
  24087. 0x37, 0x0C, 0x86, 0x80, 0x8E, 0xCA, 0xA5, 0x8E,
  24088. 0x3B, 0xA9, 0x3A, 0x2C, 0x11, 0x90, 0x46, 0x1C,
  24089. 0x1D, 0xFA, 0x11, 0x30, 0x20, 0x01, 0xBB, 0xAB,
  24090. 0x4C, 0xB1, 0xE3, 0x64, 0x2E, 0xF8, 0xCB, 0x26,
  24091. 0x30, 0x9B, 0x60, 0x52, 0x3B, 0xC2, 0x18, 0x87,
  24092. 0xB0, 0x7F, 0x89, 0x8C, 0xE5, 0x62, 0xA6, 0xCA,
  24093. 0x77, 0x8E, 0xA0, 0x15, 0x05, 0x85, 0x13, 0x78,
  24094. 0xCE, 0xA8, 0xBB, 0x7F, 0xC0, 0x9D, 0x11, 0x96,
  24095. 0x1B, 0x6C, 0x59, 0x6F, 0x93, 0x54, 0x2A, 0x99,
  24096. 0x04, 0x86, 0x4E, 0xB1, 0x0C, 0xD0, 0xA7, 0x03,
  24097. 0xDB, 0xA9, 0x89, 0x21, 0x86, 0x1A, 0x87, 0xB0,
  24098. 0x56, 0x52, 0x5C, 0x71, 0xA8, 0x43, 0x55, 0x3E,
  24099. 0x64, 0x00, 0x77, 0x74, 0x37, 0xC9, 0x5C, 0xCC,
  24100. 0x80, 0x85, 0xCC, 0x0C, 0x47, 0x7D, 0x66, 0x5A,
  24101. 0x44, 0x79, 0x01, 0x9D, 0x4C, 0xD4, 0x42, 0xF7,
  24102. 0x4A, 0x3C, 0xD8, 0x16, 0x9F, 0x42, 0x62, 0xB8,
  24103. 0x27, 0x1B, 0x5D, 0x5A, 0x67, 0xC8, 0xC1, 0x61,
  24104. 0x1A, 0xAE, 0x7B, 0x3D, 0x05, 0x34, 0xC0, 0x85,
  24105. 0x97, 0x16, 0xFD, 0xF0, 0xBB, 0x68, 0x94, 0x90,
  24106. 0x94, 0xC0, 0x6A, 0x1B, 0x73, 0xC9, 0xAA, 0x1C,
  24107. 0xBD, 0xF3, 0x31, 0x54, 0x3D, 0xE0, 0x02, 0xA8,
  24108. 0xC0, 0x6F, 0x94, 0xE8, 0x81, 0x0A, 0x5C, 0xB3,
  24109. 0x73, 0x83, 0x27, 0x45, 0xD7, 0x20, 0x68, 0x3B,
  24110. 0x57, 0x48, 0x75, 0xA6, 0x66, 0x94, 0x6D, 0x02,
  24111. 0x96, 0x89, 0x3F, 0x2B, 0x59, 0xE9, 0x07, 0x48,
  24112. 0x8D, 0x8C, 0x84, 0x89, 0xD4, 0x74, 0xD9, 0x29,
  24113. 0xA0, 0x5A, 0x57, 0x3E, 0xD6, 0x67, 0x49, 0x03,
  24114. 0x71, 0xA4, 0x6D, 0x45, 0x56, 0xCB, 0xB6, 0x8A,
  24115. 0xAA, 0x79, 0xCC, 0x3E, 0xC6, 0x65, 0x34, 0x13,
  24116. 0x57, 0x6C, 0x22, 0x8E, 0x37, 0x9A, 0x14, 0xCB,
  24117. 0x90, 0xB7, 0xB7, 0x59, 0x1B, 0x19, 0xA7, 0xBD,
  24118. 0x37, 0xA1, 0xC4, 0xD3, 0x78, 0x59, 0x89, 0x22,
  24119. 0x19, 0x44, 0x2B, 0xB0, 0xB9, 0xB9, 0xBA, 0x67,
  24120. 0xBA, 0x3B, 0xC0, 0xD0, 0x95, 0xC8, 0x80, 0x3C,
  24121. 0xEB, 0xE9, 0x7A, 0xFF, 0x0B, 0x1C, 0x15, 0x35,
  24122. 0x78, 0xA1, 0x30, 0xCD, 0x81, 0x57, 0xCF, 0x74,
  24123. 0x59, 0x46, 0xC2, 0xF5, 0x72, 0x6D, 0x9C, 0x11,
  24124. 0x27, 0x35, 0x75, 0x50, 0x52, 0x91, 0x34, 0x65,
  24125. 0x28, 0xEE, 0x0B, 0xAC, 0x04, 0x7C, 0xC9, 0x84,
  24126. 0x53, 0x8B, 0x97, 0xBB, 0xAB, 0xFC, 0xC3, 0x57,
  24127. 0xDC, 0xB8, 0xA9, 0x8F, 0xB8, 0x57, 0xC9, 0xC5,
  24128. 0x2D, 0x1B, 0x78, 0x67, 0x49, 0xCA, 0x61, 0x89,
  24129. 0x2B, 0x09, 0x75, 0x99, 0x80, 0x52, 0x00, 0x91,
  24130. 0xB9, 0xB4, 0x77, 0xC7, 0x0E, 0x6C, 0x46, 0x58,
  24131. 0x6B, 0x1C, 0xCE, 0xBE, 0x87, 0xBC, 0xF6, 0xDF,
  24132. 0x03, 0xC2, 0xB2, 0x7C, 0xB0, 0x9F, 0xA0, 0x3F,
  24133. 0x63, 0x16, 0x09, 0x58, 0x38, 0x3B, 0xE6, 0x36
  24134. };
  24135. static const byte dk_512[KYBER512_PRIVATE_KEY_SIZE] = {
  24136. 0x37, 0xEC, 0x47, 0x7E, 0x21, 0x7B, 0xFB, 0x40,
  24137. 0x38, 0x4C, 0x85, 0x0E, 0x51, 0xC1, 0x83, 0x71,
  24138. 0x58, 0xBD, 0xBC, 0x23, 0xA3, 0x18, 0x32, 0xBC,
  24139. 0x25, 0xC9, 0x1B, 0x31, 0x21, 0x44, 0x4A, 0xD4,
  24140. 0x53, 0x37, 0x33, 0xBA, 0xFF, 0x07, 0xCA, 0x81,
  24141. 0x7B, 0x64, 0xB2, 0xCA, 0x42, 0x99, 0xAA, 0x26,
  24142. 0x45, 0x4C, 0xBA, 0xFB, 0x35, 0xB6, 0xAB, 0xE1,
  24143. 0x18, 0x5C, 0xB4, 0x7C, 0x4C, 0xD6, 0x1A, 0xF9,
  24144. 0x83, 0x83, 0xC4, 0x81, 0x4B, 0x20, 0xAB, 0x87,
  24145. 0x54, 0xFC, 0x51, 0x4F, 0x23, 0x07, 0x41, 0x14,
  24146. 0xC3, 0xE5, 0xA8, 0x10, 0xA4, 0x53, 0xB8, 0x55,
  24147. 0xAA, 0x7F, 0x13, 0x10, 0xC7, 0x4B, 0x0B, 0x01,
  24148. 0xE5, 0xAA, 0xB2, 0xE8, 0x71, 0x73, 0x8F, 0xAC,
  24149. 0x27, 0x86, 0xC7, 0xA0, 0x5D, 0x6B, 0x3B, 0x32,
  24150. 0xA0, 0x50, 0xD0, 0xFB, 0x22, 0x39, 0x56, 0xC9,
  24151. 0x5C, 0xA0, 0xC2, 0xC1, 0xD5, 0x41, 0x54, 0xA7,
  24152. 0x7B, 0xD3, 0x37, 0x37, 0xA4, 0x9A, 0x00, 0x65,
  24153. 0xD1, 0x42, 0x4A, 0x2A, 0xBA, 0xFD, 0x52, 0xAA,
  24154. 0x93, 0x4C, 0x98, 0x04, 0x93, 0x92, 0x08, 0xF0,
  24155. 0x5C, 0xCF, 0x8B, 0x8B, 0x80, 0x86, 0x31, 0x6E,
  24156. 0x09, 0x43, 0xA0, 0x87, 0x10, 0x50, 0x0C, 0x91,
  24157. 0x8A, 0x2B, 0x21, 0x8D, 0x37, 0xB8, 0x5A, 0xE2,
  24158. 0x80, 0x22, 0xCB, 0x01, 0x34, 0xFB, 0x49, 0xF5,
  24159. 0xC4, 0x5D, 0x98, 0xD3, 0xC0, 0x4B, 0x75, 0x5A,
  24160. 0x60, 0x88, 0x04, 0x22, 0x66, 0x8E, 0x2B, 0x30,
  24161. 0x1B, 0x18, 0xD5, 0x19, 0x4D, 0xE9, 0x91, 0xB2,
  24162. 0x65, 0xBF, 0x94, 0x69, 0x7E, 0x6A, 0x4B, 0x81,
  24163. 0x50, 0xC8, 0xB8, 0x52, 0x03, 0x39, 0x15, 0x63,
  24164. 0x5E, 0x30, 0x66, 0x5B, 0xDA, 0x21, 0x91, 0xDA,
  24165. 0xA5, 0x05, 0xD4, 0x33, 0x44, 0xFD, 0x29, 0xC9,
  24166. 0xFC, 0xC1, 0xC5, 0x07, 0x69, 0x1D, 0x47, 0x5B,
  24167. 0x61, 0x7C, 0x94, 0x8F, 0xCC, 0x84, 0xB1, 0xB0,
  24168. 0x8A, 0x1C, 0x63, 0x8C, 0x3E, 0x13, 0x58, 0x0C,
  24169. 0xE3, 0x59, 0x78, 0x9A, 0x98, 0x60, 0xE5, 0x46,
  24170. 0x9C, 0xC7, 0x54, 0xB0, 0x8E, 0xE3, 0x3F, 0x09,
  24171. 0x21, 0xBD, 0xEF, 0x15, 0xA9, 0x06, 0x96, 0x9F,
  24172. 0x2D, 0xC5, 0x7A, 0x25, 0xE8, 0x0C, 0xE4, 0xC4,
  24173. 0x5F, 0x11, 0xE0, 0x4A, 0x51, 0x9A, 0xB0, 0x8B,
  24174. 0x9B, 0x92, 0x7C, 0x3A, 0x13, 0xA0, 0x81, 0xCF,
  24175. 0xFA, 0x11, 0x0F, 0xAC, 0xCC, 0x5E, 0x8D, 0xC2,
  24176. 0x94, 0x95, 0x97, 0x8B, 0x55, 0x53, 0x10, 0x4D,
  24177. 0x47, 0x3A, 0x17, 0x59, 0x18, 0xAD, 0x5B, 0x54,
  24178. 0x87, 0xBB, 0xA6, 0x97, 0x12, 0xAE, 0x93, 0xF6,
  24179. 0x15, 0xC6, 0x0A, 0x8D, 0x38, 0x7B, 0xCE, 0x3F,
  24180. 0x65, 0x1E, 0x56, 0x88, 0x0A, 0x52, 0x2B, 0x2D,
  24181. 0xB8, 0x63, 0x51, 0xCA, 0xB6, 0x5D, 0x13, 0xB4,
  24182. 0x69, 0x3D, 0xB0, 0xB2, 0xC8, 0x09, 0x36, 0xFA,
  24183. 0xD1, 0xCE, 0x67, 0x92, 0x5E, 0x6B, 0xB7, 0xC1,
  24184. 0x10, 0xC4, 0x3E, 0x83, 0x24, 0x7D, 0x22, 0x60,
  24185. 0x8D, 0x8C, 0x10, 0x23, 0x43, 0x1C, 0xB6, 0x92,
  24186. 0x90, 0xA4, 0xF8, 0xA9, 0x59, 0x3B, 0xF1, 0x24,
  24187. 0x1D, 0x73, 0x7C, 0x0C, 0xD1, 0x6D, 0x75, 0xEB,
  24188. 0x50, 0xC6, 0x84, 0x2C, 0xE0, 0xA2, 0x1D, 0xCE,
  24189. 0x49, 0x40, 0x36, 0x82, 0x4C, 0xE6, 0x32, 0x52,
  24190. 0xE9, 0x32, 0x5F, 0x05, 0xB7, 0x34, 0x45, 0x2B,
  24191. 0x12, 0x91, 0x32, 0xB1, 0x96, 0x08, 0x4A, 0x37,
  24192. 0x88, 0xBB, 0xB1, 0xF2, 0x0A, 0x37, 0xD2, 0xC2,
  24193. 0xB3, 0xF9, 0x0E, 0x0D, 0xD7, 0xA2, 0x74, 0xC9,
  24194. 0xB1, 0xA9, 0xF0, 0x2E, 0xC7, 0xE7, 0x21, 0xF4,
  24195. 0xA4, 0x3D, 0x40, 0x9A, 0x25, 0xFB, 0xC9, 0x9A,
  24196. 0x44, 0xD4, 0x76, 0x31, 0x07, 0xC7, 0x87, 0x62,
  24197. 0x09, 0x41, 0x76, 0x1E, 0xD4, 0x8C, 0x93, 0x29,
  24198. 0x24, 0xBA, 0x62, 0x09, 0x86, 0xCF, 0x27, 0x7A,
  24199. 0x23, 0x47, 0x1C, 0x7B, 0x13, 0x33, 0x3D, 0x93,
  24200. 0x6C, 0x0D, 0xD4, 0x9E, 0x0F, 0xF3, 0x4C, 0xA3,
  24201. 0xAB, 0x82, 0x34, 0xC4, 0x2A, 0xEB, 0xE4, 0x59,
  24202. 0xC6, 0x12, 0x05, 0x2B, 0x97, 0x16, 0xE9, 0x6B,
  24203. 0x20, 0xBE, 0xC7, 0x18, 0x12, 0x60, 0x40, 0xA9,
  24204. 0x09, 0x1F, 0x6B, 0xA9, 0x44, 0x5F, 0x45, 0x80,
  24205. 0x6A, 0xEB, 0x6E, 0x38, 0x16, 0x71, 0x0F, 0x7C,
  24206. 0xBF, 0xED, 0x11, 0x01, 0x46, 0x12, 0x84, 0xDD,
  24207. 0x96, 0x2B, 0x7B, 0x12, 0x04, 0x7C, 0x0A, 0x0A,
  24208. 0x90, 0x6A, 0x05, 0x89, 0xB4, 0xA9, 0xA4, 0x26,
  24209. 0x46, 0x9B, 0xDA, 0x39, 0x46, 0x09, 0x1A, 0x37,
  24210. 0x5B, 0x19, 0x52, 0xA9, 0x1C, 0x23, 0x1C, 0x0F,
  24211. 0xE6, 0xB5, 0x7F, 0x7C, 0xC9, 0x7E, 0xFE, 0xD0,
  24212. 0xBC, 0x10, 0x01, 0x36, 0x78, 0x23, 0xBE, 0x18,
  24213. 0x86, 0x30, 0x8B, 0x3A, 0x21, 0x45, 0x2B, 0x7E,
  24214. 0x45, 0x50, 0x66, 0x71, 0x9C, 0xCC, 0xEA, 0xF6,
  24215. 0xA7, 0x26, 0xFC, 0x22, 0xBC, 0x83, 0x99, 0xF5,
  24216. 0x4B, 0xBF, 0xCA, 0xF7, 0xCA, 0x63, 0xBA, 0x73,
  24217. 0x17, 0x3C, 0x7A, 0xA8, 0x61, 0x9A, 0x3F, 0x48,
  24218. 0x5C, 0x3E, 0x33, 0x04, 0x21, 0x00, 0x67, 0x66,
  24219. 0x74, 0x6F, 0x4E, 0xF6, 0x65, 0x3E, 0x44, 0x0E,
  24220. 0x5C, 0xDC, 0x59, 0x53, 0x40, 0x18, 0xC3, 0x52,
  24221. 0xC0, 0x23, 0x58, 0x4C, 0xBB, 0x37, 0x4E, 0xB7,
  24222. 0xA9, 0xB7, 0x83, 0x68, 0x32, 0xBE, 0x53, 0xAF,
  24223. 0x27, 0x2A, 0x06, 0x97, 0x55, 0xCE, 0x2F, 0xF2,
  24224. 0x9C, 0xD8, 0xB3, 0x94, 0xC5, 0x24, 0x22, 0xB3,
  24225. 0x47, 0x0E, 0x27, 0x41, 0x5F, 0x41, 0xB3, 0x97,
  24226. 0x53, 0x59, 0x59, 0xF1, 0x60, 0x00, 0x3B, 0x45,
  24227. 0x2C, 0xF4, 0x96, 0x97, 0xB7, 0xA5, 0x36, 0x89,
  24228. 0x85, 0x2B, 0xBE, 0x6C, 0xCF, 0xDF, 0xB4, 0x0B,
  24229. 0x48, 0xE9, 0x32, 0x8D, 0xE1, 0x15, 0x22, 0xD0,
  24230. 0xA4, 0x31, 0xB1, 0x15, 0xA5, 0xC0, 0xC2, 0xF4,
  24231. 0x30, 0x7D, 0x98, 0x62, 0xC0, 0xDD, 0x1B, 0x40,
  24232. 0xC6, 0x5A, 0x1D, 0x9D, 0x47, 0x97, 0x77, 0xE6,
  24233. 0x90, 0x5A, 0x91, 0xA5, 0xCB, 0x24, 0x55, 0x1C,
  24234. 0x8B, 0x1E, 0x52, 0xA3, 0xC7, 0x7B, 0x63, 0x31,
  24235. 0x3F, 0xFC, 0x8B, 0x58, 0x17, 0x81, 0x52, 0x59,
  24236. 0xA6, 0xAD, 0xB5, 0x96, 0x45, 0xDC, 0x4B, 0xB1,
  24237. 0x43, 0x6D, 0x51, 0xE6, 0x2A, 0x09, 0x68, 0x34,
  24238. 0xAF, 0x43, 0x77, 0x25, 0x10, 0xC4, 0xED, 0xF3,
  24239. 0x4C, 0xDE, 0x0A, 0x5B, 0x57, 0xC1, 0x45, 0xE6,
  24240. 0x87, 0xCB, 0x87, 0x16, 0x2F, 0x00, 0x1C, 0x21,
  24241. 0xC9, 0xE1, 0x93, 0x4A, 0xC1, 0x1A, 0xAF, 0xA7,
  24242. 0x0F, 0xF8, 0x10, 0x73, 0x26, 0x50, 0xB3, 0x2A,
  24243. 0x30, 0x18, 0xA7, 0xC5, 0x0C, 0xD7, 0x36, 0x79,
  24244. 0x62, 0x22, 0xC8, 0xAB, 0x82, 0x1A, 0x92, 0x83,
  24245. 0xBE, 0x1C, 0xC2, 0x04, 0xC3, 0xF1, 0x63, 0x0D,
  24246. 0x3C, 0xCC, 0xDB, 0x0A, 0x9A, 0x3D, 0x17, 0x55,
  24247. 0x2B, 0x91, 0x58, 0xC0, 0x66, 0x4E, 0x5D, 0x6A,
  24248. 0x04, 0xB0, 0xFA, 0x36, 0xDE, 0x45, 0x86, 0x2A,
  24249. 0x46, 0xA3, 0x9E, 0xC5, 0x97, 0xAE, 0x42, 0xC3,
  24250. 0x11, 0xC4, 0xAC, 0x22, 0x4A, 0x72, 0xD6, 0xF2,
  24251. 0x53, 0xBB, 0x52, 0x35, 0xF7, 0xA2, 0xB8, 0xB0,
  24252. 0xF2, 0x4D, 0x13, 0x76, 0xAF, 0x58, 0x87, 0x46,
  24253. 0xF3, 0xBB, 0x8E, 0x03, 0x65, 0x07, 0x87, 0x61,
  24254. 0xCA, 0xB9, 0x83, 0xA4, 0xA6, 0xA9, 0x40, 0xA3,
  24255. 0xD9, 0x97, 0x04, 0x7A, 0x8F, 0x36, 0xA7, 0x31,
  24256. 0xE8, 0x96, 0x52, 0x36, 0xC3, 0x7B, 0xF2, 0x00,
  24257. 0x08, 0x2F, 0x82, 0x1D, 0xCA, 0x77, 0x16, 0xC4,
  24258. 0x44, 0xA9, 0x0B, 0xEC, 0x53, 0x07, 0x4B, 0xBA,
  24259. 0x58, 0xC1, 0x32, 0xBF, 0xB9, 0xA2, 0xAC, 0xE2,
  24260. 0xCE, 0xC9, 0xAA, 0x65, 0x8E, 0xAC, 0x12, 0x32,
  24261. 0xCC, 0xCA, 0x3C, 0x81, 0x7A, 0x92, 0xC1, 0x19,
  24262. 0x5C, 0x05, 0xC0, 0xE1, 0xD6, 0x63, 0x9F, 0xD2,
  24263. 0xAD, 0xE5, 0x31, 0x60, 0x7D, 0x48, 0x8B, 0x74,
  24264. 0xA7, 0x47, 0xCF, 0xF4, 0x7F, 0xCA, 0x5C, 0x8B,
  24265. 0x21, 0x63, 0xCA, 0x03, 0xC5, 0x45, 0xED, 0x10,
  24266. 0x32, 0x78, 0x43, 0x0C, 0x60, 0xB2, 0x38, 0x1A,
  24267. 0x09, 0x42, 0x7F, 0xD1, 0x30, 0xF8, 0x59, 0xBF,
  24268. 0x5D, 0xB7, 0x76, 0xDA, 0x09, 0x5D, 0xCA, 0x58,
  24269. 0x04, 0xFA, 0x63, 0xB0, 0xD7, 0xD8, 0x7F, 0xA9,
  24270. 0x41, 0x5C, 0x72, 0xFB, 0x51, 0x87, 0x2A, 0x98,
  24271. 0x9F, 0x46, 0x6C, 0x98, 0x4B, 0xC7, 0x4C, 0x29,
  24272. 0xB8, 0x63, 0x20, 0x19, 0xCA, 0x04, 0x0C, 0x9C,
  24273. 0xA3, 0x5E, 0x22, 0x60, 0x8D, 0xAA, 0x70, 0x35,
  24274. 0x7A, 0xE2, 0xC3, 0xAD, 0x83, 0x63, 0x1F, 0xAA,
  24275. 0x17, 0x4E, 0x0A, 0xCD, 0xF5, 0xDB, 0xBF, 0x3C,
  24276. 0xF6, 0x8A, 0x05, 0xB6, 0x54, 0x3A, 0xB6, 0x26,
  24277. 0x8E, 0x1A, 0x51, 0xB0, 0x93, 0x2C, 0x17, 0xB0,
  24278. 0x0A, 0x13, 0x71, 0xB2, 0xDA, 0xB2, 0x41, 0xF9,
  24279. 0x2A, 0x43, 0xFF, 0xB4, 0x56, 0xD0, 0xA8, 0xC8,
  24280. 0x86, 0x0A, 0x8E, 0x28, 0xA6, 0x1A, 0x21, 0x30,
  24281. 0x7C, 0xC0, 0x45, 0x6D, 0xA4, 0x24, 0x29, 0x05,
  24282. 0xCB, 0x1D, 0x3D, 0x0B, 0xBD, 0x81, 0xBB, 0x8E,
  24283. 0xE2, 0x74, 0xA4, 0x3C, 0x76, 0xC3, 0x10, 0x01,
  24284. 0x95, 0x15, 0xFC, 0xC1, 0x40, 0x46, 0x7C, 0x33,
  24285. 0x37, 0x0C, 0x86, 0x80, 0x8E, 0xCA, 0xA5, 0x8E,
  24286. 0x3B, 0xA9, 0x3A, 0x2C, 0x11, 0x90, 0x46, 0x1C,
  24287. 0x1D, 0xFA, 0x11, 0x30, 0x20, 0x01, 0xBB, 0xAB,
  24288. 0x4C, 0xB1, 0xE3, 0x64, 0x2E, 0xF8, 0xCB, 0x26,
  24289. 0x30, 0x9B, 0x60, 0x52, 0x3B, 0xC2, 0x18, 0x87,
  24290. 0xB0, 0x7F, 0x89, 0x8C, 0xE5, 0x62, 0xA6, 0xCA,
  24291. 0x77, 0x8E, 0xA0, 0x15, 0x05, 0x85, 0x13, 0x78,
  24292. 0xCE, 0xA8, 0xBB, 0x7F, 0xC0, 0x9D, 0x11, 0x96,
  24293. 0x1B, 0x6C, 0x59, 0x6F, 0x93, 0x54, 0x2A, 0x99,
  24294. 0x04, 0x86, 0x4E, 0xB1, 0x0C, 0xD0, 0xA7, 0x03,
  24295. 0xDB, 0xA9, 0x89, 0x21, 0x86, 0x1A, 0x87, 0xB0,
  24296. 0x56, 0x52, 0x5C, 0x71, 0xA8, 0x43, 0x55, 0x3E,
  24297. 0x64, 0x00, 0x77, 0x74, 0x37, 0xC9, 0x5C, 0xCC,
  24298. 0x80, 0x85, 0xCC, 0x0C, 0x47, 0x7D, 0x66, 0x5A,
  24299. 0x44, 0x79, 0x01, 0x9D, 0x4C, 0xD4, 0x42, 0xF7,
  24300. 0x4A, 0x3C, 0xD8, 0x16, 0x9F, 0x42, 0x62, 0xB8,
  24301. 0x27, 0x1B, 0x5D, 0x5A, 0x67, 0xC8, 0xC1, 0x61,
  24302. 0x1A, 0xAE, 0x7B, 0x3D, 0x05, 0x34, 0xC0, 0x85,
  24303. 0x97, 0x16, 0xFD, 0xF0, 0xBB, 0x68, 0x94, 0x90,
  24304. 0x94, 0xC0, 0x6A, 0x1B, 0x73, 0xC9, 0xAA, 0x1C,
  24305. 0xBD, 0xF3, 0x31, 0x54, 0x3D, 0xE0, 0x02, 0xA8,
  24306. 0xC0, 0x6F, 0x94, 0xE8, 0x81, 0x0A, 0x5C, 0xB3,
  24307. 0x73, 0x83, 0x27, 0x45, 0xD7, 0x20, 0x68, 0x3B,
  24308. 0x57, 0x48, 0x75, 0xA6, 0x66, 0x94, 0x6D, 0x02,
  24309. 0x96, 0x89, 0x3F, 0x2B, 0x59, 0xE9, 0x07, 0x48,
  24310. 0x8D, 0x8C, 0x84, 0x89, 0xD4, 0x74, 0xD9, 0x29,
  24311. 0xA0, 0x5A, 0x57, 0x3E, 0xD6, 0x67, 0x49, 0x03,
  24312. 0x71, 0xA4, 0x6D, 0x45, 0x56, 0xCB, 0xB6, 0x8A,
  24313. 0xAA, 0x79, 0xCC, 0x3E, 0xC6, 0x65, 0x34, 0x13,
  24314. 0x57, 0x6C, 0x22, 0x8E, 0x37, 0x9A, 0x14, 0xCB,
  24315. 0x90, 0xB7, 0xB7, 0x59, 0x1B, 0x19, 0xA7, 0xBD,
  24316. 0x37, 0xA1, 0xC4, 0xD3, 0x78, 0x59, 0x89, 0x22,
  24317. 0x19, 0x44, 0x2B, 0xB0, 0xB9, 0xB9, 0xBA, 0x67,
  24318. 0xBA, 0x3B, 0xC0, 0xD0, 0x95, 0xC8, 0x80, 0x3C,
  24319. 0xEB, 0xE9, 0x7A, 0xFF, 0x0B, 0x1C, 0x15, 0x35,
  24320. 0x78, 0xA1, 0x30, 0xCD, 0x81, 0x57, 0xCF, 0x74,
  24321. 0x59, 0x46, 0xC2, 0xF5, 0x72, 0x6D, 0x9C, 0x11,
  24322. 0x27, 0x35, 0x75, 0x50, 0x52, 0x91, 0x34, 0x65,
  24323. 0x28, 0xEE, 0x0B, 0xAC, 0x04, 0x7C, 0xC9, 0x84,
  24324. 0x53, 0x8B, 0x97, 0xBB, 0xAB, 0xFC, 0xC3, 0x57,
  24325. 0xDC, 0xB8, 0xA9, 0x8F, 0xB8, 0x57, 0xC9, 0xC5,
  24326. 0x2D, 0x1B, 0x78, 0x67, 0x49, 0xCA, 0x61, 0x89,
  24327. 0x2B, 0x09, 0x75, 0x99, 0x80, 0x52, 0x00, 0x91,
  24328. 0xB9, 0xB4, 0x77, 0xC7, 0x0E, 0x6C, 0x46, 0x58,
  24329. 0x6B, 0x1C, 0xCE, 0xBE, 0x87, 0xBC, 0xF6, 0xDF,
  24330. 0x03, 0xC2, 0xB2, 0x7C, 0xB0, 0x9F, 0xA0, 0x3F,
  24331. 0x63, 0x16, 0x09, 0x58, 0x38, 0x3B, 0xE6, 0x36,
  24332. 0xC0, 0xEC, 0xC8, 0xDD, 0xAE, 0x8B, 0x59, 0x4A,
  24333. 0x14, 0x03, 0x78, 0x68, 0xBE, 0xC0, 0xB2, 0x23,
  24334. 0x00, 0xDE, 0xFD, 0xFA, 0xA1, 0xD9, 0x73, 0xAC,
  24335. 0x5C, 0xEC, 0x84, 0xAE, 0x43, 0x86, 0xB8, 0xFB,
  24336. 0xCD, 0x11, 0x9A, 0xFD, 0xC8, 0x55, 0x94, 0x42,
  24337. 0x42, 0x4A, 0x87, 0xC1, 0x3E, 0xA1, 0x01, 0xE2,
  24338. 0x9F, 0xCA, 0x11, 0x88, 0x18, 0x69, 0x07, 0x7E,
  24339. 0x40, 0x92, 0xE7, 0x51, 0xBE, 0xDC, 0xA8, 0xBC
  24340. };
  24341. #endif
  24342. #ifndef WOLFSSL_NO_KYBER768
  24343. static const byte seed_768[KYBER_MAKEKEY_RAND_SZ] = {
  24344. 0x92, 0xAC, 0x7D, 0x1F, 0x83, 0xBA, 0xFA, 0xE6,
  24345. 0xEE, 0x86, 0xFE, 0x00, 0xF9, 0x5D, 0x81, 0x33,
  24346. 0x75, 0x77, 0x24, 0x34, 0x86, 0x0F, 0x5F, 0xF7,
  24347. 0xD5, 0x4F, 0xFC, 0x37, 0x39, 0x9B, 0xC4, 0xCC,
  24348. 0x92, 0xAC, 0x7D, 0x1F, 0x83, 0xBA, 0xFA, 0xE6,
  24349. 0xEE, 0x86, 0xFE, 0x00, 0xF9, 0x5D, 0x81, 0x33,
  24350. 0x75, 0x77, 0x24, 0x34, 0x86, 0x0F, 0x5F, 0xF7,
  24351. 0xD5, 0x4F, 0xFC, 0x37, 0x39, 0x9B, 0xC4, 0xCC
  24352. };
  24353. static const byte ek_768[KYBER768_PUBLIC_KEY_SIZE] = {
  24354. 0xD2, 0xE6, 0x9A, 0x05, 0x53, 0x4A, 0x72, 0x32,
  24355. 0xC5, 0xF1, 0xB7, 0x66, 0xE9, 0x3A, 0x5E, 0xE2,
  24356. 0xEA, 0x1B, 0x26, 0xE8, 0x60, 0xA3, 0x44, 0x1A,
  24357. 0xDE, 0xA9, 0x1E, 0xDB, 0x78, 0x2C, 0xAB, 0xC8,
  24358. 0xA5, 0xD0, 0x11, 0xA2, 0x1B, 0xC3, 0x88, 0xE7,
  24359. 0xF4, 0x86, 0xF0, 0xB7, 0x99, 0x30, 0x79, 0xAE,
  24360. 0x3F, 0x1A, 0x7C, 0x85, 0xD2, 0x7D, 0x0F, 0x49,
  24361. 0x21, 0x84, 0xD5, 0x90, 0x62, 0x14, 0x2B, 0x76,
  24362. 0xA4, 0x37, 0x34, 0xA9, 0x0D, 0x55, 0x6A, 0x95,
  24363. 0xDC, 0x48, 0x3D, 0xD8, 0x21, 0x04, 0xED, 0x58,
  24364. 0xCA, 0x15, 0x71, 0xC3, 0x96, 0x85, 0x82, 0x79,
  24365. 0x51, 0x43, 0x4C, 0xC1, 0x00, 0x1A, 0xA4, 0xC8,
  24366. 0x13, 0x26, 0x1E, 0x4F, 0x93, 0x02, 0x8E, 0x14,
  24367. 0xCD, 0x08, 0xF7, 0x68, 0xA4, 0x54, 0x31, 0x0C,
  24368. 0x3B, 0x01, 0x0C, 0x83, 0xB7, 0x4D, 0x04, 0xA5,
  24369. 0x7B, 0xB9, 0x77, 0xB3, 0xD8, 0xBC, 0xF3, 0xAA,
  24370. 0xA7, 0x8C, 0xA1, 0x2B, 0x78, 0xF0, 0x10, 0xD9,
  24371. 0x51, 0x34, 0x92, 0x8A, 0x5E, 0x5D, 0x96, 0xA0,
  24372. 0x29, 0xB4, 0x42, 0xA4, 0x18, 0x88, 0x03, 0x8B,
  24373. 0x29, 0xC2, 0xF1, 0x22, 0xB0, 0xB6, 0xB3, 0xAF,
  24374. 0x12, 0x1A, 0xEA, 0x29, 0xA0, 0x55, 0x53, 0xBD,
  24375. 0xF1, 0xDB, 0x60, 0x7A, 0xFB, 0x17, 0x00, 0x18,
  24376. 0x60, 0xAF, 0x18, 0x23, 0xBC, 0xF0, 0x3D, 0xB3,
  24377. 0xB4, 0x41, 0xDA, 0x16, 0x3A, 0x28, 0xC5, 0x23,
  24378. 0xA5, 0xFB, 0x46, 0x69, 0xA6, 0x42, 0x34, 0xA4,
  24379. 0xBC, 0xD1, 0x21, 0x7F, 0xF2, 0x63, 0x5B, 0xD9,
  24380. 0x76, 0x80, 0xFF, 0x93, 0x8D, 0xBC, 0xF1, 0x0E,
  24381. 0x95, 0x32, 0xA9, 0xA7, 0x9A, 0x5B, 0x07, 0x3A,
  24382. 0x9E, 0x8D, 0xB2, 0x12, 0x3D, 0x21, 0x0F, 0xAE,
  24383. 0xA2, 0x00, 0xB6, 0x64, 0x83, 0x8E, 0x80, 0x07,
  24384. 0x1F, 0x2B, 0xA2, 0x54, 0xAA, 0xC8, 0x90, 0xA4,
  24385. 0x6E, 0x28, 0xEC, 0x34, 0x2D, 0x92, 0x81, 0x2B,
  24386. 0x01, 0x59, 0x30, 0x71, 0x65, 0x7E, 0x7A, 0x3A,
  24387. 0x4A, 0x75, 0xCB, 0x3D, 0x52, 0x79, 0xCE, 0x88,
  24388. 0x40, 0x5A, 0xC5, 0xAD, 0xAC, 0xB2, 0x05, 0x1E,
  24389. 0x02, 0x2E, 0xE0, 0xAC, 0x9B, 0xBF, 0xE3, 0x2D,
  24390. 0xEF, 0x98, 0x66, 0x7E, 0xD3, 0x47, 0xAD, 0xCB,
  24391. 0x39, 0x30, 0xF3, 0xCA, 0xD0, 0x31, 0x39, 0x1B,
  24392. 0x70, 0x9A, 0x4E, 0x61, 0xB8, 0xDD, 0x4B, 0x3F,
  24393. 0xB7, 0x41, 0xB5, 0xBD, 0x60, 0xBF, 0x30, 0x40,
  24394. 0x15, 0xEE, 0x75, 0x46, 0xA2, 0x4B, 0x59, 0xEA,
  24395. 0xDC, 0xA1, 0x37, 0xC7, 0x12, 0x50, 0x74, 0x72,
  24396. 0x6B, 0x76, 0x86, 0xEC, 0x55, 0x1B, 0x7B, 0xC2,
  24397. 0x6B, 0xBD, 0xB2, 0x0F, 0xC3, 0x78, 0x35, 0x34,
  24398. 0xE3, 0x4E, 0xE1, 0xF1, 0xBC, 0x6B, 0x77, 0xAB,
  24399. 0x49, 0xA6, 0x66, 0x78, 0x46, 0x97, 0x57, 0x78,
  24400. 0xC3, 0xC5, 0x36, 0x83, 0x04, 0x50, 0xA3, 0xFA,
  24401. 0x91, 0x02, 0x59, 0x72, 0x2F, 0x3F, 0x80, 0x6E,
  24402. 0x6E, 0xB4, 0xB9, 0x34, 0x67, 0x63, 0xFE, 0xF0,
  24403. 0x92, 0x2B, 0xC4, 0xB6, 0xEB, 0x38, 0x26, 0xAF,
  24404. 0xF2, 0x4E, 0xAD, 0xC6, 0xCF, 0x6E, 0x47, 0x7C,
  24405. 0x2E, 0x05, 0x5C, 0xFB, 0x7A, 0x90, 0xA5, 0x5C,
  24406. 0x06, 0xD0, 0xB2, 0xA2, 0xF5, 0x11, 0x60, 0x69,
  24407. 0xE6, 0x4A, 0x5B, 0x50, 0x78, 0xC0, 0x57, 0x7B,
  24408. 0xC8, 0xE7, 0x90, 0x0E, 0xA7, 0x1C, 0x34, 0x1C,
  24409. 0x02, 0xAD, 0x85, 0x4E, 0xA5, 0xA0, 0x1A, 0xF2,
  24410. 0xA6, 0x05, 0xCB, 0x20, 0x68, 0xD5, 0x24, 0x38,
  24411. 0xCD, 0xDC, 0x60, 0xB0, 0x38, 0x82, 0xCC, 0x02,
  24412. 0x4D, 0x13, 0x04, 0x5F, 0x2B, 0xA6, 0xB0, 0xF4,
  24413. 0x46, 0xAA, 0xA5, 0x95, 0x87, 0x60, 0x61, 0x79,
  24414. 0x45, 0x37, 0x1F, 0xD7, 0x8C, 0x28, 0xA4, 0x06,
  24415. 0x77, 0xA6, 0xE7, 0x2F, 0x51, 0x3B, 0x9E, 0x06,
  24416. 0x67, 0xA9, 0xBA, 0xF4, 0x46, 0xC1, 0xBA, 0x93,
  24417. 0x1B, 0xA8, 0x18, 0x34, 0x23, 0x47, 0x92, 0xA2,
  24418. 0xA2, 0xB2, 0xB3, 0x70, 0x1F, 0x31, 0xB7, 0xCF,
  24419. 0x46, 0x7C, 0x80, 0xF1, 0x98, 0x11, 0x41, 0xBB,
  24420. 0x45, 0x77, 0x93, 0xE1, 0x30, 0x70, 0x91, 0xC4,
  24421. 0x8B, 0x59, 0x14, 0x64, 0x6A, 0x60, 0xCE, 0x1A,
  24422. 0x30, 0x15, 0x43, 0x77, 0x9D, 0x7C, 0x33, 0x42,
  24423. 0xAD, 0x17, 0x97, 0x96, 0xC2, 0xC4, 0x40, 0xD9,
  24424. 0x9D, 0xF9, 0xD4, 0x1B, 0x52, 0xE3, 0x26, 0x25,
  24425. 0xA8, 0x2A, 0xA5, 0xF5, 0x79, 0xA9, 0x92, 0x0B,
  24426. 0xFF, 0xBA, 0x96, 0x4F, 0xA7, 0x0D, 0xB2, 0x59,
  24427. 0xC8, 0x5E, 0x68, 0xC8, 0x13, 0x81, 0x7B, 0x13,
  24428. 0x47, 0xBF, 0x19, 0x81, 0x4D, 0xA5, 0xE9, 0x36,
  24429. 0x4A, 0x46, 0x45, 0xE6, 0x21, 0x92, 0x3D, 0x95,
  24430. 0x5C, 0x21, 0x1A, 0x55, 0xD3, 0x55, 0xC8, 0x16,
  24431. 0xDA, 0x04, 0x73, 0x0A, 0xA3, 0x24, 0x08, 0x5E,
  24432. 0x62, 0x2B, 0x51, 0xD6, 0x10, 0x9B, 0x49, 0xF6,
  24433. 0x73, 0xAD, 0xD0, 0x0E, 0x41, 0x47, 0x55, 0xC8,
  24434. 0x02, 0x4A, 0xA0, 0x16, 0x4F, 0x24, 0x55, 0x6D,
  24435. 0xED, 0x96, 0x3D, 0x61, 0x14, 0x38, 0x56, 0xCB,
  24436. 0x4F, 0xF0, 0x56, 0x7E, 0x33, 0x20, 0x73, 0x0D,
  24437. 0xBC, 0xBF, 0x12, 0xF6, 0x6E, 0x2B, 0x70, 0xB2,
  24438. 0x00, 0x54, 0xA6, 0xDE, 0xA4, 0x26, 0x14, 0xB5,
  24439. 0x0E, 0xF7, 0x2B, 0x15, 0x6F, 0x51, 0x49, 0xFC,
  24440. 0x26, 0x3D, 0xD7, 0xE0, 0x39, 0xC5, 0x5A, 0x3E,
  24441. 0xE9, 0x82, 0x7D, 0xF9, 0x2C, 0x56, 0x5D, 0x24,
  24442. 0xC5, 0x5E, 0x0A, 0x81, 0xC6, 0x49, 0x46, 0x95,
  24443. 0x34, 0x4D, 0x94, 0x87, 0x48, 0xAF, 0xBA, 0x9F,
  24444. 0x76, 0x2C, 0x0E, 0xA9, 0x0B, 0xB7, 0x24, 0x89,
  24445. 0x79, 0x02, 0x00, 0x07, 0x75, 0x61, 0x39, 0x49,
  24446. 0x60, 0x2C, 0x48, 0xC7, 0x8A, 0x94, 0x40, 0x67,
  24447. 0x8C, 0x24, 0x08, 0x6D, 0x32, 0x6D, 0x79, 0x64,
  24448. 0x3B, 0xAF, 0x70, 0x36, 0xC6, 0x6C, 0x7E, 0x02,
  24449. 0x6A, 0xAE, 0xFD, 0xA2, 0x80, 0x7A, 0x60, 0xBD,
  24450. 0x7F, 0xC9, 0x13, 0x63, 0xBB, 0x02, 0x34, 0xA5,
  24451. 0x90, 0x98, 0x4A, 0xA0, 0x11, 0xF1, 0x1D, 0x40,
  24452. 0x26, 0x82, 0x18, 0xA1, 0x58, 0x83, 0x77, 0xB3,
  24453. 0xD7, 0x67, 0x1B, 0x8B, 0x99, 0x78, 0x99, 0x19,
  24454. 0xB8, 0x6E, 0xE8, 0x2B, 0x18, 0xEC, 0x22, 0xD4,
  24455. 0xE8, 0x0A, 0x1F, 0x27, 0x85, 0x3D, 0x88, 0x94,
  24456. 0x19, 0xD4, 0x60, 0xDE, 0xF7, 0x56, 0x7A, 0xA4,
  24457. 0x56, 0x79, 0x69, 0xC4, 0x30, 0x48, 0xC3, 0x2B,
  24458. 0x84, 0x62, 0xA9, 0xC9, 0x38, 0x6E, 0xB3, 0x15,
  24459. 0x2A, 0x69, 0x76, 0xAA, 0x78, 0x3C, 0xDD, 0x1A,
  24460. 0x8C, 0x57, 0xA9, 0xB6, 0xBB, 0xD8, 0x37, 0xA0,
  24461. 0x06, 0x24, 0xB5, 0x8B, 0x4B, 0xA3, 0xDB, 0xB6,
  24462. 0x3B, 0xB8, 0x20, 0x0E, 0x7B, 0xC8, 0x88, 0x81,
  24463. 0xBE, 0xBD, 0xA9, 0x25, 0xBC, 0xA0, 0x28, 0xE2,
  24464. 0x91, 0xAA, 0x1C, 0x22, 0x53, 0x9C, 0xD0, 0x4F,
  24465. 0x90, 0x09, 0x0D, 0x7F, 0x74, 0x10, 0x8C, 0x32,
  24466. 0xB8, 0x02, 0x2C, 0x15, 0x91, 0xC8, 0x81, 0xE7,
  24467. 0x63, 0x04, 0xE2, 0x40, 0x81, 0x90, 0xE2, 0x0F,
  24468. 0x09, 0xA5, 0x4F, 0xC2, 0x34, 0x20, 0xE2, 0x62,
  24469. 0x0E, 0x9D, 0x87, 0xA3, 0x10, 0x8A, 0x94, 0xFE,
  24470. 0xEA, 0x72, 0xD5, 0xAB, 0x7F, 0xCF, 0xB9, 0x72,
  24471. 0xE6, 0x56, 0x1B, 0x1A, 0x7B, 0x06, 0x2F, 0x1A,
  24472. 0x68, 0x2E, 0x02, 0x0A, 0xA2, 0x56, 0x28, 0x12,
  24473. 0xB2, 0x96, 0x54, 0x7B, 0x91, 0x78, 0x24, 0xCD,
  24474. 0xB8, 0x8C, 0x58, 0x2B, 0x5A, 0x68, 0x90, 0x17,
  24475. 0x7B, 0xC7, 0x0C, 0x91, 0xAC, 0xAC, 0x9A, 0xBE,
  24476. 0x29, 0x0A, 0xEB, 0x2C, 0x34, 0xA7, 0xE2, 0x36,
  24477. 0x89, 0x55, 0xCB, 0x45, 0x6A, 0x34, 0x53, 0x68,
  24478. 0xAB, 0xE3, 0xB9, 0x1B, 0x47, 0xFC, 0x30, 0xB0,
  24479. 0x23, 0x3A, 0x09, 0xBA, 0x79, 0xFB, 0x11, 0x23,
  24480. 0x8A, 0xC5, 0x08, 0xCC, 0xE6, 0x10, 0x95, 0xF8,
  24481. 0x54, 0xC2, 0x32, 0x04, 0xA8, 0xD3, 0x6B, 0xFC,
  24482. 0x2C, 0x6E, 0x05, 0xA7, 0x2A, 0xF5, 0x24, 0x4B,
  24483. 0x17, 0xC1, 0x21, 0x01, 0xE0, 0x14, 0x51, 0x57,
  24484. 0x0E, 0xB1, 0x10, 0x56, 0x7E, 0x85, 0x0E, 0x79,
  24485. 0xC0, 0x00, 0x14, 0x24, 0x41, 0xFE, 0x41, 0x60,
  24486. 0x02, 0x75, 0x45, 0xF6, 0x29, 0x0E, 0x85, 0x45,
  24487. 0x1B, 0x80, 0x23, 0x4A, 0x94, 0x06, 0xC3, 0x90,
  24488. 0xB0, 0xCE, 0xA3, 0xC8, 0x33, 0x5D, 0x4C, 0x6F,
  24489. 0x85, 0x50, 0xB5, 0x44, 0xC9, 0x34, 0x3E, 0x61,
  24490. 0xBA, 0x1C, 0x84, 0x89, 0xD1, 0xB0, 0x39, 0x97,
  24491. 0x39, 0x16, 0x8A, 0xF7, 0x40, 0xA4, 0x81, 0xB0,
  24492. 0xF5, 0xC3, 0x37, 0x25, 0x30, 0xCA, 0x06, 0xB5,
  24493. 0x08, 0xEC, 0xE8, 0x38, 0xAB, 0x78, 0xBE, 0xE1,
  24494. 0xE5, 0x97, 0xA9, 0xB1, 0x4F, 0x6A, 0xEC, 0x7A,
  24495. 0x3B, 0xD1, 0xAA, 0x8D, 0x10, 0xBA, 0xC2, 0x3B,
  24496. 0x98, 0x02, 0x90, 0x2C, 0xD5, 0x29, 0xAB, 0x6E,
  24497. 0xF5, 0x4D, 0xB3, 0x11, 0x0C, 0xFB, 0x56, 0x1E,
  24498. 0x7E, 0x69, 0x48, 0xE6, 0x52, 0x81, 0x25, 0x04,
  24499. 0x16, 0xC3, 0x49, 0xC8, 0x10, 0x0B, 0x3B, 0x4D,
  24500. 0x3D, 0x0F, 0x62, 0xAC, 0xAD, 0x8D, 0x16, 0x11,
  24501. 0x75, 0xB1, 0x34, 0xF7, 0x56, 0x49, 0x37, 0xCD
  24502. };
  24503. static const byte dk_768[KYBER768_PRIVATE_KEY_SIZE] = {
  24504. 0x19, 0xD7, 0x4A, 0xD5, 0x47, 0x2A, 0x8B, 0x2B,
  24505. 0xAA, 0xD2, 0xA5, 0x67, 0x02, 0xC9, 0xB3, 0xB5,
  24506. 0x51, 0x0E, 0xF3, 0x92, 0x48, 0x58, 0x06, 0x1D,
  24507. 0x57, 0xF9, 0x0D, 0xD9, 0xA1, 0xA0, 0x1F, 0xEC,
  24508. 0x2F, 0x57, 0xC5, 0x1A, 0x88, 0x88, 0x05, 0x34,
  24509. 0x1B, 0x61, 0x7C, 0x51, 0x55, 0x39, 0x59, 0x77,
  24510. 0x50, 0x83, 0x5C, 0x3E, 0xD7, 0xA0, 0x33, 0xB0,
  24511. 0x39, 0xD7, 0x24, 0x91, 0x33, 0x2C, 0x5D, 0xF4,
  24512. 0xA6, 0x9B, 0x6D, 0xF2, 0x61, 0x71, 0x87, 0x7A,
  24513. 0xD1, 0xE5, 0x0A, 0xC5, 0x01, 0x00, 0xBE, 0x47,
  24514. 0x28, 0x78, 0x66, 0x85, 0xDA, 0x7A, 0x73, 0x9E,
  24515. 0x84, 0x3F, 0xF0, 0xD4, 0x59, 0x22, 0xD7, 0x28,
  24516. 0x1E, 0x21, 0x0D, 0x5E, 0x82, 0xB9, 0x44, 0x65,
  24517. 0x2F, 0x48, 0x62, 0xCF, 0xB3, 0xD9, 0x02, 0xDE,
  24518. 0x60, 0xAF, 0xD0, 0xA1, 0x64, 0x47, 0x1B, 0x26,
  24519. 0x14, 0x4A, 0x1D, 0x7A, 0x38, 0x09, 0x65, 0x03,
  24520. 0x09, 0x59, 0x11, 0x76, 0x2E, 0xBA, 0x79, 0x62,
  24521. 0xC4, 0x51, 0x1D, 0x05, 0xA1, 0x28, 0xF2, 0x78,
  24522. 0x1E, 0xCB, 0x3D, 0x1F, 0x5B, 0xB1, 0x24, 0x42,
  24523. 0x37, 0x61, 0x1A, 0xBA, 0xB9, 0x24, 0x99, 0x1F,
  24524. 0x8A, 0x27, 0x32, 0xE2, 0x70, 0x32, 0x35, 0x79,
  24525. 0x20, 0xF1, 0x97, 0xC7, 0x69, 0x2D, 0x60, 0xA9,
  24526. 0x44, 0x44, 0x72, 0x25, 0x8C, 0xB4, 0x57, 0xC1,
  24527. 0xB7, 0x1B, 0x77, 0x99, 0x54, 0x69, 0xF3, 0xA9,
  24528. 0x62, 0xF3, 0xAB, 0xA6, 0x69, 0x96, 0x14, 0xFC,
  24529. 0xCC, 0xEA, 0x74, 0x1E, 0x21, 0xC6, 0x00, 0xC4,
  24530. 0x35, 0x7B, 0xBF, 0xAB, 0x45, 0x29, 0x27, 0xC3,
  24531. 0xD4, 0x41, 0xBF, 0x8E, 0xD7, 0x31, 0x52, 0xF7,
  24532. 0x5C, 0x08, 0xF5, 0x40, 0xE1, 0x86, 0xAC, 0xCA,
  24533. 0x33, 0x26, 0xF4, 0x22, 0xC8, 0x4B, 0x98, 0x8D,
  24534. 0x77, 0xE6, 0x1A, 0xE6, 0x18, 0x59, 0xCF, 0x85,
  24535. 0x41, 0xF8, 0x92, 0x09, 0xE4, 0x98, 0x30, 0x40,
  24536. 0xC5, 0x61, 0x76, 0x54, 0x80, 0x88, 0x52, 0xB6,
  24537. 0x49, 0xB8, 0x99, 0xA3, 0x99, 0xAE, 0xC2, 0xC8,
  24538. 0xBB, 0xA8, 0xA5, 0x42, 0xF3, 0x45, 0xAB, 0xF2,
  24539. 0x81, 0x3F, 0x65, 0xE9, 0xA7, 0x91, 0xD3, 0x2C,
  24540. 0xC2, 0xD7, 0x60, 0x26, 0xFB, 0x8D, 0x0C, 0x94,
  24541. 0xB6, 0x57, 0x48, 0x9A, 0xBB, 0x48, 0x7D, 0xA4,
  24542. 0xA2, 0xC0, 0xE3, 0x86, 0x8D, 0x3C, 0xF4, 0x7F,
  24543. 0x1C, 0xBB, 0x2F, 0xA7, 0x9C, 0x53, 0xCF, 0xF6,
  24544. 0x26, 0x47, 0x77, 0xC0, 0x9B, 0x17, 0x7C, 0x91,
  24545. 0x31, 0x54, 0x84, 0xD2, 0xB3, 0x0B, 0x0C, 0xA2,
  24546. 0x1F, 0x55, 0xAD, 0xD2, 0x3C, 0x57, 0xE1, 0x91,
  24547. 0x1C, 0x3F, 0x08, 0x6B, 0xCA, 0xD2, 0x17, 0x98,
  24548. 0x48, 0x6E, 0xB4, 0x7B, 0x7C, 0x58, 0x57, 0x73,
  24549. 0x81, 0xC0, 0x9F, 0x52, 0x52, 0x58, 0x2D, 0x1B,
  24550. 0x27, 0xA7, 0xD5, 0xB8, 0xE0, 0x60, 0xCE, 0x78,
  24551. 0x20, 0x9C, 0xC8, 0x2B, 0xAE, 0x4D, 0xA6, 0x06,
  24552. 0x80, 0x0C, 0x8D, 0xB1, 0x26, 0x8F, 0x7A, 0xD2,
  24553. 0xB7, 0x93, 0xA4, 0x4F, 0x34, 0x61, 0x2C, 0xCE,
  24554. 0xA3, 0x1C, 0xE7, 0xD7, 0x96, 0xA6, 0x5A, 0x26,
  24555. 0x91, 0xD6, 0x15, 0x00, 0x62, 0x5F, 0x83, 0xE7,
  24556. 0xBE, 0x57, 0x07, 0x7E, 0xE9, 0xC1, 0xB8, 0xC1,
  24557. 0xCA, 0xA1, 0x37, 0xCC, 0x4B, 0x65, 0x73, 0x30,
  24558. 0x8C, 0x19, 0x66, 0x8B, 0x24, 0xB0, 0x1E, 0x96,
  24559. 0x69, 0x03, 0xAB, 0xBC, 0xB7, 0x9B, 0x67, 0xBE,
  24560. 0x0A, 0x3E, 0x3E, 0x05, 0x8A, 0xAD, 0xA1, 0x89,
  24561. 0xB9, 0xEA, 0x80, 0x35, 0x9A, 0xC2, 0x6F, 0x4C,
  24562. 0x5C, 0x53, 0x73, 0x5F, 0xE4, 0xFC, 0x35, 0x24,
  24563. 0x73, 0x37, 0x76, 0x0C, 0xCA, 0x35, 0x29, 0xB8,
  24564. 0xD2, 0x66, 0xBB, 0x6C, 0x48, 0x01, 0x06, 0x54,
  24565. 0xCD, 0xBC, 0x5A, 0x3E, 0x97, 0x57, 0x52, 0x46,
  24566. 0x75, 0xAB, 0xC4, 0x13, 0x13, 0x0C, 0xC2, 0x70,
  24567. 0x1F, 0x28, 0x93, 0x3E, 0xAB, 0xB8, 0x39, 0x2B,
  24568. 0x0D, 0x6D, 0x05, 0x9C, 0xFC, 0x3A, 0x30, 0x32,
  24569. 0x6C, 0x4F, 0xCC, 0x81, 0x0B, 0x37, 0xA4, 0x74,
  24570. 0x8C, 0x1C, 0x53, 0x92, 0x8A, 0x49, 0x13, 0xE4,
  24571. 0x8B, 0x18, 0x66, 0x97, 0x16, 0x2C, 0x33, 0xFF,
  24572. 0xFB, 0x06, 0xDD, 0x51, 0x61, 0xC8, 0x63, 0x9D,
  24573. 0xB1, 0x95, 0xC6, 0xCA, 0x64, 0x82, 0x9B, 0x2B,
  24574. 0x3A, 0x2E, 0x4C, 0x96, 0x83, 0xB6, 0x6D, 0xF7,
  24575. 0xFB, 0x19, 0x09, 0x90, 0x4E, 0x00, 0x02, 0x0D,
  24576. 0xBA, 0x13, 0x4E, 0x02, 0xA1, 0x68, 0xD7, 0x6A,
  24577. 0xC0, 0x76, 0xBB, 0x77, 0xD4, 0xDC, 0x84, 0x96,
  24578. 0xB4, 0xBB, 0xE7, 0xB4, 0x69, 0x0B, 0xA2, 0x9B,
  24579. 0x62, 0xA9, 0x1A, 0xBE, 0x72, 0xBE, 0xF3, 0x23,
  24580. 0xA4, 0x4C, 0x89, 0x03, 0xE4, 0x82, 0xB6, 0x0D,
  24581. 0x99, 0xBA, 0x61, 0xD1, 0xBB, 0xCF, 0x9C, 0xB9,
  24582. 0x67, 0x35, 0x34, 0xC1, 0xD6, 0x47, 0x66, 0x23,
  24583. 0x74, 0xEE, 0x2C, 0x7C, 0x5F, 0x00, 0x81, 0xBA,
  24584. 0xD1, 0x49, 0xF4, 0x42, 0x06, 0x71, 0x76, 0x84,
  24585. 0xD9, 0x74, 0x6B, 0x20, 0x48, 0x63, 0x3A, 0xF7,
  24586. 0xA6, 0x8C, 0x68, 0x65, 0xFB, 0x59, 0x03, 0x58,
  24587. 0xD8, 0xCF, 0x82, 0x14, 0x58, 0x36, 0x9B, 0x0C,
  24588. 0x31, 0xEB, 0x59, 0x7C, 0xF5, 0xBE, 0x78, 0xEB,
  24589. 0x48, 0x0E, 0xA0, 0x4E, 0x35, 0xFA, 0xCC, 0x38,
  24590. 0x03, 0x72, 0xC8, 0xC0, 0xA0, 0x4D, 0xE2, 0x76,
  24591. 0xB1, 0xA7, 0x21, 0x21, 0xE5, 0x96, 0xCB, 0xB2,
  24592. 0x5E, 0xF7, 0x53, 0x6A, 0xD3, 0x80, 0x41, 0x84,
  24593. 0xA8, 0x7B, 0xDF, 0xB5, 0xA7, 0x69, 0x16, 0x0B,
  24594. 0xFB, 0xB0, 0xCA, 0x3C, 0x36, 0x07, 0x90, 0xE5,
  24595. 0x56, 0x2B, 0xB7, 0x8E, 0xFE, 0x00, 0x69, 0xC7,
  24596. 0x74, 0x83, 0xAD, 0x35, 0xCA, 0xC2, 0x37, 0xC6,
  24597. 0x1D, 0xE7, 0x8A, 0x7D, 0xB4, 0x6F, 0xC9, 0x17,
  24598. 0x12, 0x4C, 0xA1, 0x75, 0x10, 0xDB, 0x7D, 0xA2,
  24599. 0x18, 0x89, 0x0F, 0x44, 0x8E, 0xF6, 0x31, 0x86,
  24600. 0x13, 0xA1, 0xC9, 0x7C, 0x92, 0x8E, 0x2B, 0x7B,
  24601. 0x6A, 0x54, 0x61, 0x7B, 0xCC, 0xB6, 0xCD, 0xF2,
  24602. 0x78, 0xAE, 0x54, 0x2B, 0x56, 0xAD, 0x7B, 0xB5,
  24603. 0xEC, 0xD8, 0xC4, 0x6A, 0x66, 0xC4, 0xFA, 0x09,
  24604. 0x50, 0xCE, 0x41, 0x35, 0x2C, 0xB8, 0x57, 0x11,
  24605. 0x89, 0x04, 0x58, 0xF2, 0x99, 0xBF, 0x40, 0xBA,
  24606. 0x6F, 0xF2, 0xC0, 0x71, 0x38, 0x62, 0x26, 0x8B,
  24607. 0x5F, 0x08, 0xE4, 0x98, 0x45, 0xB0, 0x94, 0x43,
  24608. 0x99, 0x7A, 0xB2, 0x9A, 0x62, 0x07, 0x3C, 0x0D,
  24609. 0x98, 0x18, 0xC0, 0x20, 0x16, 0x7D, 0x47, 0x49,
  24610. 0x23, 0x1C, 0x05, 0x9E, 0x6F, 0x48, 0x3F, 0x97,
  24611. 0x68, 0x17, 0xC9, 0x0C, 0x20, 0xA9, 0xC9, 0x37,
  24612. 0x07, 0x9C, 0x2D, 0x4B, 0xE3, 0x0D, 0xA9, 0x74,
  24613. 0xA9, 0x7E, 0x4B, 0xC5, 0x3E, 0xD9, 0x6A, 0x55,
  24614. 0x16, 0x9F, 0x4A, 0x23, 0xA3, 0xEA, 0x24, 0xBD,
  24615. 0x8E, 0x01, 0xB8, 0xFA, 0xEB, 0x95, 0xD4, 0xE5,
  24616. 0x3F, 0xFF, 0xEC, 0xB6, 0x08, 0x02, 0xC3, 0x88,
  24617. 0xA4, 0x0F, 0x46, 0x60, 0x54, 0x0B, 0x1B, 0x1F,
  24618. 0x81, 0x76, 0xC9, 0x81, 0x1B, 0xB2, 0x6A, 0x68,
  24619. 0x3C, 0xA7, 0x89, 0x56, 0x4A, 0x29, 0x40, 0xFC,
  24620. 0xEB, 0x2C, 0xE6, 0xA9, 0x2A, 0x1E, 0xE4, 0x5E,
  24621. 0xE4, 0xC3, 0x18, 0x57, 0xC9, 0xB9, 0xB8, 0xB5,
  24622. 0x6A, 0x79, 0xD9, 0x5A, 0x46, 0xCB, 0x39, 0x3A,
  24623. 0x31, 0xA2, 0x73, 0x7B, 0xAF, 0xEA, 0x6C, 0x81,
  24624. 0x06, 0x6A, 0x67, 0x2B, 0x34, 0xC1, 0x0A, 0xA9,
  24625. 0x89, 0x57, 0xC9, 0x17, 0x66, 0xB7, 0x30, 0x03,
  24626. 0x6A, 0x56, 0xD9, 0x40, 0xAA, 0x4E, 0xBC, 0xB7,
  24627. 0x58, 0xB0, 0x83, 0x51, 0xE2, 0xC4, 0xFD, 0x19,
  24628. 0x45, 0x3B, 0xF3, 0xA6, 0x29, 0x2A, 0x99, 0x3D,
  24629. 0x67, 0xC7, 0xEC, 0xC7, 0x2F, 0x42, 0xF7, 0x82,
  24630. 0xE9, 0xEB, 0xAA, 0x1A, 0x8B, 0x3B, 0x0F, 0x56,
  24631. 0x7A, 0xB3, 0x94, 0x21, 0xF6, 0xA6, 0x7A, 0x6B,
  24632. 0x84, 0x10, 0xFD, 0x94, 0xA7, 0x21, 0xD3, 0x65,
  24633. 0xF1, 0x63, 0x9E, 0x9D, 0xDA, 0xBF, 0xD0, 0xA6,
  24634. 0xCE, 0x1A, 0x46, 0x05, 0xBD, 0x2B, 0x1C, 0x9B,
  24635. 0x97, 0x7B, 0xD1, 0xEA, 0x32, 0x86, 0x73, 0x68,
  24636. 0xD6, 0xE6, 0x39, 0xD0, 0x19, 0xAC, 0x10, 0x18,
  24637. 0x53, 0xBC, 0x15, 0x3C, 0x86, 0xF8, 0x52, 0x80,
  24638. 0xFC, 0x76, 0x3B, 0xA2, 0x4F, 0xB5, 0x7A, 0x29,
  24639. 0x6C, 0xB1, 0x2D, 0x32, 0xE0, 0x8A, 0xB3, 0x2C,
  24640. 0x55, 0x1D, 0x5A, 0x45, 0xA4, 0xA2, 0x8F, 0x9A,
  24641. 0xDC, 0x28, 0xF7, 0xA2, 0x90, 0x0E, 0x25, 0xA4,
  24642. 0x0B, 0x51, 0x90, 0xB2, 0x2A, 0xB1, 0x9D, 0xFB,
  24643. 0x24, 0x6F, 0x42, 0xB2, 0x4F, 0x97, 0xCC, 0xA9,
  24644. 0xB0, 0x9B, 0xEA, 0xD2, 0x46, 0xE1, 0x73, 0x4F,
  24645. 0x44, 0x66, 0x77, 0xB3, 0x8B, 0x75, 0x22, 0xB7,
  24646. 0x80, 0x72, 0x7C, 0x11, 0x74, 0x40, 0xC9, 0xF1,
  24647. 0xA0, 0x24, 0x52, 0x0C, 0x14, 0x1A, 0x69, 0xCD,
  24648. 0xD2, 0xE6, 0x9A, 0x05, 0x53, 0x4A, 0x72, 0x32,
  24649. 0xC5, 0xF1, 0xB7, 0x66, 0xE9, 0x3A, 0x5E, 0xE2,
  24650. 0xEA, 0x1B, 0x26, 0xE8, 0x60, 0xA3, 0x44, 0x1A,
  24651. 0xDE, 0xA9, 0x1E, 0xDB, 0x78, 0x2C, 0xAB, 0xC8,
  24652. 0xA5, 0xD0, 0x11, 0xA2, 0x1B, 0xC3, 0x88, 0xE7,
  24653. 0xF4, 0x86, 0xF0, 0xB7, 0x99, 0x30, 0x79, 0xAE,
  24654. 0x3F, 0x1A, 0x7C, 0x85, 0xD2, 0x7D, 0x0F, 0x49,
  24655. 0x21, 0x84, 0xD5, 0x90, 0x62, 0x14, 0x2B, 0x76,
  24656. 0xA4, 0x37, 0x34, 0xA9, 0x0D, 0x55, 0x6A, 0x95,
  24657. 0xDC, 0x48, 0x3D, 0xD8, 0x21, 0x04, 0xED, 0x58,
  24658. 0xCA, 0x15, 0x71, 0xC3, 0x96, 0x85, 0x82, 0x79,
  24659. 0x51, 0x43, 0x4C, 0xC1, 0x00, 0x1A, 0xA4, 0xC8,
  24660. 0x13, 0x26, 0x1E, 0x4F, 0x93, 0x02, 0x8E, 0x14,
  24661. 0xCD, 0x08, 0xF7, 0x68, 0xA4, 0x54, 0x31, 0x0C,
  24662. 0x3B, 0x01, 0x0C, 0x83, 0xB7, 0x4D, 0x04, 0xA5,
  24663. 0x7B, 0xB9, 0x77, 0xB3, 0xD8, 0xBC, 0xF3, 0xAA,
  24664. 0xA7, 0x8C, 0xA1, 0x2B, 0x78, 0xF0, 0x10, 0xD9,
  24665. 0x51, 0x34, 0x92, 0x8A, 0x5E, 0x5D, 0x96, 0xA0,
  24666. 0x29, 0xB4, 0x42, 0xA4, 0x18, 0x88, 0x03, 0x8B,
  24667. 0x29, 0xC2, 0xF1, 0x22, 0xB0, 0xB6, 0xB3, 0xAF,
  24668. 0x12, 0x1A, 0xEA, 0x29, 0xA0, 0x55, 0x53, 0xBD,
  24669. 0xF1, 0xDB, 0x60, 0x7A, 0xFB, 0x17, 0x00, 0x18,
  24670. 0x60, 0xAF, 0x18, 0x23, 0xBC, 0xF0, 0x3D, 0xB3,
  24671. 0xB4, 0x41, 0xDA, 0x16, 0x3A, 0x28, 0xC5, 0x23,
  24672. 0xA5, 0xFB, 0x46, 0x69, 0xA6, 0x42, 0x34, 0xA4,
  24673. 0xBC, 0xD1, 0x21, 0x7F, 0xF2, 0x63, 0x5B, 0xD9,
  24674. 0x76, 0x80, 0xFF, 0x93, 0x8D, 0xBC, 0xF1, 0x0E,
  24675. 0x95, 0x32, 0xA9, 0xA7, 0x9A, 0x5B, 0x07, 0x3A,
  24676. 0x9E, 0x8D, 0xB2, 0x12, 0x3D, 0x21, 0x0F, 0xAE,
  24677. 0xA2, 0x00, 0xB6, 0x64, 0x83, 0x8E, 0x80, 0x07,
  24678. 0x1F, 0x2B, 0xA2, 0x54, 0xAA, 0xC8, 0x90, 0xA4,
  24679. 0x6E, 0x28, 0xEC, 0x34, 0x2D, 0x92, 0x81, 0x2B,
  24680. 0x01, 0x59, 0x30, 0x71, 0x65, 0x7E, 0x7A, 0x3A,
  24681. 0x4A, 0x75, 0xCB, 0x3D, 0x52, 0x79, 0xCE, 0x88,
  24682. 0x40, 0x5A, 0xC5, 0xAD, 0xAC, 0xB2, 0x05, 0x1E,
  24683. 0x02, 0x2E, 0xE0, 0xAC, 0x9B, 0xBF, 0xE3, 0x2D,
  24684. 0xEF, 0x98, 0x66, 0x7E, 0xD3, 0x47, 0xAD, 0xCB,
  24685. 0x39, 0x30, 0xF3, 0xCA, 0xD0, 0x31, 0x39, 0x1B,
  24686. 0x70, 0x9A, 0x4E, 0x61, 0xB8, 0xDD, 0x4B, 0x3F,
  24687. 0xB7, 0x41, 0xB5, 0xBD, 0x60, 0xBF, 0x30, 0x40,
  24688. 0x15, 0xEE, 0x75, 0x46, 0xA2, 0x4B, 0x59, 0xEA,
  24689. 0xDC, 0xA1, 0x37, 0xC7, 0x12, 0x50, 0x74, 0x72,
  24690. 0x6B, 0x76, 0x86, 0xEC, 0x55, 0x1B, 0x7B, 0xC2,
  24691. 0x6B, 0xBD, 0xB2, 0x0F, 0xC3, 0x78, 0x35, 0x34,
  24692. 0xE3, 0x4E, 0xE1, 0xF1, 0xBC, 0x6B, 0x77, 0xAB,
  24693. 0x49, 0xA6, 0x66, 0x78, 0x46, 0x97, 0x57, 0x78,
  24694. 0xC3, 0xC5, 0x36, 0x83, 0x04, 0x50, 0xA3, 0xFA,
  24695. 0x91, 0x02, 0x59, 0x72, 0x2F, 0x3F, 0x80, 0x6E,
  24696. 0x6E, 0xB4, 0xB9, 0x34, 0x67, 0x63, 0xFE, 0xF0,
  24697. 0x92, 0x2B, 0xC4, 0xB6, 0xEB, 0x38, 0x26, 0xAF,
  24698. 0xF2, 0x4E, 0xAD, 0xC6, 0xCF, 0x6E, 0x47, 0x7C,
  24699. 0x2E, 0x05, 0x5C, 0xFB, 0x7A, 0x90, 0xA5, 0x5C,
  24700. 0x06, 0xD0, 0xB2, 0xA2, 0xF5, 0x11, 0x60, 0x69,
  24701. 0xE6, 0x4A, 0x5B, 0x50, 0x78, 0xC0, 0x57, 0x7B,
  24702. 0xC8, 0xE7, 0x90, 0x0E, 0xA7, 0x1C, 0x34, 0x1C,
  24703. 0x02, 0xAD, 0x85, 0x4E, 0xA5, 0xA0, 0x1A, 0xF2,
  24704. 0xA6, 0x05, 0xCB, 0x20, 0x68, 0xD5, 0x24, 0x38,
  24705. 0xCD, 0xDC, 0x60, 0xB0, 0x38, 0x82, 0xCC, 0x02,
  24706. 0x4D, 0x13, 0x04, 0x5F, 0x2B, 0xA6, 0xB0, 0xF4,
  24707. 0x46, 0xAA, 0xA5, 0x95, 0x87, 0x60, 0x61, 0x79,
  24708. 0x45, 0x37, 0x1F, 0xD7, 0x8C, 0x28, 0xA4, 0x06,
  24709. 0x77, 0xA6, 0xE7, 0x2F, 0x51, 0x3B, 0x9E, 0x06,
  24710. 0x67, 0xA9, 0xBA, 0xF4, 0x46, 0xC1, 0xBA, 0x93,
  24711. 0x1B, 0xA8, 0x18, 0x34, 0x23, 0x47, 0x92, 0xA2,
  24712. 0xA2, 0xB2, 0xB3, 0x70, 0x1F, 0x31, 0xB7, 0xCF,
  24713. 0x46, 0x7C, 0x80, 0xF1, 0x98, 0x11, 0x41, 0xBB,
  24714. 0x45, 0x77, 0x93, 0xE1, 0x30, 0x70, 0x91, 0xC4,
  24715. 0x8B, 0x59, 0x14, 0x64, 0x6A, 0x60, 0xCE, 0x1A,
  24716. 0x30, 0x15, 0x43, 0x77, 0x9D, 0x7C, 0x33, 0x42,
  24717. 0xAD, 0x17, 0x97, 0x96, 0xC2, 0xC4, 0x40, 0xD9,
  24718. 0x9D, 0xF9, 0xD4, 0x1B, 0x52, 0xE3, 0x26, 0x25,
  24719. 0xA8, 0x2A, 0xA5, 0xF5, 0x79, 0xA9, 0x92, 0x0B,
  24720. 0xFF, 0xBA, 0x96, 0x4F, 0xA7, 0x0D, 0xB2, 0x59,
  24721. 0xC8, 0x5E, 0x68, 0xC8, 0x13, 0x81, 0x7B, 0x13,
  24722. 0x47, 0xBF, 0x19, 0x81, 0x4D, 0xA5, 0xE9, 0x36,
  24723. 0x4A, 0x46, 0x45, 0xE6, 0x21, 0x92, 0x3D, 0x95,
  24724. 0x5C, 0x21, 0x1A, 0x55, 0xD3, 0x55, 0xC8, 0x16,
  24725. 0xDA, 0x04, 0x73, 0x0A, 0xA3, 0x24, 0x08, 0x5E,
  24726. 0x62, 0x2B, 0x51, 0xD6, 0x10, 0x9B, 0x49, 0xF6,
  24727. 0x73, 0xAD, 0xD0, 0x0E, 0x41, 0x47, 0x55, 0xC8,
  24728. 0x02, 0x4A, 0xA0, 0x16, 0x4F, 0x24, 0x55, 0x6D,
  24729. 0xED, 0x96, 0x3D, 0x61, 0x14, 0x38, 0x56, 0xCB,
  24730. 0x4F, 0xF0, 0x56, 0x7E, 0x33, 0x20, 0x73, 0x0D,
  24731. 0xBC, 0xBF, 0x12, 0xF6, 0x6E, 0x2B, 0x70, 0xB2,
  24732. 0x00, 0x54, 0xA6, 0xDE, 0xA4, 0x26, 0x14, 0xB5,
  24733. 0x0E, 0xF7, 0x2B, 0x15, 0x6F, 0x51, 0x49, 0xFC,
  24734. 0x26, 0x3D, 0xD7, 0xE0, 0x39, 0xC5, 0x5A, 0x3E,
  24735. 0xE9, 0x82, 0x7D, 0xF9, 0x2C, 0x56, 0x5D, 0x24,
  24736. 0xC5, 0x5E, 0x0A, 0x81, 0xC6, 0x49, 0x46, 0x95,
  24737. 0x34, 0x4D, 0x94, 0x87, 0x48, 0xAF, 0xBA, 0x9F,
  24738. 0x76, 0x2C, 0x0E, 0xA9, 0x0B, 0xB7, 0x24, 0x89,
  24739. 0x79, 0x02, 0x00, 0x07, 0x75, 0x61, 0x39, 0x49,
  24740. 0x60, 0x2C, 0x48, 0xC7, 0x8A, 0x94, 0x40, 0x67,
  24741. 0x8C, 0x24, 0x08, 0x6D, 0x32, 0x6D, 0x79, 0x64,
  24742. 0x3B, 0xAF, 0x70, 0x36, 0xC6, 0x6C, 0x7E, 0x02,
  24743. 0x6A, 0xAE, 0xFD, 0xA2, 0x80, 0x7A, 0x60, 0xBD,
  24744. 0x7F, 0xC9, 0x13, 0x63, 0xBB, 0x02, 0x34, 0xA5,
  24745. 0x90, 0x98, 0x4A, 0xA0, 0x11, 0xF1, 0x1D, 0x40,
  24746. 0x26, 0x82, 0x18, 0xA1, 0x58, 0x83, 0x77, 0xB3,
  24747. 0xD7, 0x67, 0x1B, 0x8B, 0x99, 0x78, 0x99, 0x19,
  24748. 0xB8, 0x6E, 0xE8, 0x2B, 0x18, 0xEC, 0x22, 0xD4,
  24749. 0xE8, 0x0A, 0x1F, 0x27, 0x85, 0x3D, 0x88, 0x94,
  24750. 0x19, 0xD4, 0x60, 0xDE, 0xF7, 0x56, 0x7A, 0xA4,
  24751. 0x56, 0x79, 0x69, 0xC4, 0x30, 0x48, 0xC3, 0x2B,
  24752. 0x84, 0x62, 0xA9, 0xC9, 0x38, 0x6E, 0xB3, 0x15,
  24753. 0x2A, 0x69, 0x76, 0xAA, 0x78, 0x3C, 0xDD, 0x1A,
  24754. 0x8C, 0x57, 0xA9, 0xB6, 0xBB, 0xD8, 0x37, 0xA0,
  24755. 0x06, 0x24, 0xB5, 0x8B, 0x4B, 0xA3, 0xDB, 0xB6,
  24756. 0x3B, 0xB8, 0x20, 0x0E, 0x7B, 0xC8, 0x88, 0x81,
  24757. 0xBE, 0xBD, 0xA9, 0x25, 0xBC, 0xA0, 0x28, 0xE2,
  24758. 0x91, 0xAA, 0x1C, 0x22, 0x53, 0x9C, 0xD0, 0x4F,
  24759. 0x90, 0x09, 0x0D, 0x7F, 0x74, 0x10, 0x8C, 0x32,
  24760. 0xB8, 0x02, 0x2C, 0x15, 0x91, 0xC8, 0x81, 0xE7,
  24761. 0x63, 0x04, 0xE2, 0x40, 0x81, 0x90, 0xE2, 0x0F,
  24762. 0x09, 0xA5, 0x4F, 0xC2, 0x34, 0x20, 0xE2, 0x62,
  24763. 0x0E, 0x9D, 0x87, 0xA3, 0x10, 0x8A, 0x94, 0xFE,
  24764. 0xEA, 0x72, 0xD5, 0xAB, 0x7F, 0xCF, 0xB9, 0x72,
  24765. 0xE6, 0x56, 0x1B, 0x1A, 0x7B, 0x06, 0x2F, 0x1A,
  24766. 0x68, 0x2E, 0x02, 0x0A, 0xA2, 0x56, 0x28, 0x12,
  24767. 0xB2, 0x96, 0x54, 0x7B, 0x91, 0x78, 0x24, 0xCD,
  24768. 0xB8, 0x8C, 0x58, 0x2B, 0x5A, 0x68, 0x90, 0x17,
  24769. 0x7B, 0xC7, 0x0C, 0x91, 0xAC, 0xAC, 0x9A, 0xBE,
  24770. 0x29, 0x0A, 0xEB, 0x2C, 0x34, 0xA7, 0xE2, 0x36,
  24771. 0x89, 0x55, 0xCB, 0x45, 0x6A, 0x34, 0x53, 0x68,
  24772. 0xAB, 0xE3, 0xB9, 0x1B, 0x47, 0xFC, 0x30, 0xB0,
  24773. 0x23, 0x3A, 0x09, 0xBA, 0x79, 0xFB, 0x11, 0x23,
  24774. 0x8A, 0xC5, 0x08, 0xCC, 0xE6, 0x10, 0x95, 0xF8,
  24775. 0x54, 0xC2, 0x32, 0x04, 0xA8, 0xD3, 0x6B, 0xFC,
  24776. 0x2C, 0x6E, 0x05, 0xA7, 0x2A, 0xF5, 0x24, 0x4B,
  24777. 0x17, 0xC1, 0x21, 0x01, 0xE0, 0x14, 0x51, 0x57,
  24778. 0x0E, 0xB1, 0x10, 0x56, 0x7E, 0x85, 0x0E, 0x79,
  24779. 0xC0, 0x00, 0x14, 0x24, 0x41, 0xFE, 0x41, 0x60,
  24780. 0x02, 0x75, 0x45, 0xF6, 0x29, 0x0E, 0x85, 0x45,
  24781. 0x1B, 0x80, 0x23, 0x4A, 0x94, 0x06, 0xC3, 0x90,
  24782. 0xB0, 0xCE, 0xA3, 0xC8, 0x33, 0x5D, 0x4C, 0x6F,
  24783. 0x85, 0x50, 0xB5, 0x44, 0xC9, 0x34, 0x3E, 0x61,
  24784. 0xBA, 0x1C, 0x84, 0x89, 0xD1, 0xB0, 0x39, 0x97,
  24785. 0x39, 0x16, 0x8A, 0xF7, 0x40, 0xA4, 0x81, 0xB0,
  24786. 0xF5, 0xC3, 0x37, 0x25, 0x30, 0xCA, 0x06, 0xB5,
  24787. 0x08, 0xEC, 0xE8, 0x38, 0xAB, 0x78, 0xBE, 0xE1,
  24788. 0xE5, 0x97, 0xA9, 0xB1, 0x4F, 0x6A, 0xEC, 0x7A,
  24789. 0x3B, 0xD1, 0xAA, 0x8D, 0x10, 0xBA, 0xC2, 0x3B,
  24790. 0x98, 0x02, 0x90, 0x2C, 0xD5, 0x29, 0xAB, 0x6E,
  24791. 0xF5, 0x4D, 0xB3, 0x11, 0x0C, 0xFB, 0x56, 0x1E,
  24792. 0x7E, 0x69, 0x48, 0xE6, 0x52, 0x81, 0x25, 0x04,
  24793. 0x16, 0xC3, 0x49, 0xC8, 0x10, 0x0B, 0x3B, 0x4D,
  24794. 0x3D, 0x0F, 0x62, 0xAC, 0xAD, 0x8D, 0x16, 0x11,
  24795. 0x75, 0xB1, 0x34, 0xF7, 0x56, 0x49, 0x37, 0xCD,
  24796. 0xEC, 0xE9, 0xE2, 0x46, 0xAA, 0xD1, 0x10, 0x21,
  24797. 0xA6, 0x7B, 0x20, 0xEB, 0x8F, 0x77, 0x65, 0xAC,
  24798. 0x28, 0x23, 0xA9, 0xD1, 0x8C, 0x93, 0xEC, 0x28,
  24799. 0x2D, 0x6D, 0xBC, 0x53, 0xCD, 0x6D, 0xF5, 0x75,
  24800. 0x92, 0xAC, 0x7D, 0x1F, 0x83, 0xBA, 0xFA, 0xE6,
  24801. 0xEE, 0x86, 0xFE, 0x00, 0xF9, 0x5D, 0x81, 0x33,
  24802. 0x75, 0x77, 0x24, 0x34, 0x86, 0x0F, 0x5F, 0xF7,
  24803. 0xD5, 0x4F, 0xFC, 0x37, 0x39, 0x9B, 0xC4, 0xCC
  24804. };
  24805. #endif
  24806. #ifndef WOLFSSL_NO_KYBER1024
  24807. static const byte seed_1024[KYBER_MAKEKEY_RAND_SZ] = {
  24808. 0x7A, 0xF6, 0x50, 0x22, 0xE0, 0xA4, 0x72, 0xED,
  24809. 0x63, 0x88, 0x63, 0x8E, 0xA2, 0x9D, 0x82, 0xDA,
  24810. 0x68, 0xB4, 0xCF, 0x9F, 0xFD, 0xF2, 0xB6, 0x7C,
  24811. 0xD7, 0x08, 0xEA, 0x5A, 0x37, 0x0C, 0x6A, 0x7C,
  24812. 0x7A, 0xF6, 0x50, 0x22, 0xE0, 0xA4, 0x72, 0xED,
  24813. 0x63, 0x88, 0x63, 0x8E, 0xA2, 0x9D, 0x82, 0xDA,
  24814. 0x68, 0xB4, 0xCF, 0x9F, 0xFD, 0xF2, 0xB6, 0x7C,
  24815. 0xD7, 0x08, 0xEA, 0x5A, 0x37, 0x0C, 0x6A, 0x7C
  24816. };
  24817. static const byte ek_1024[KYBER1024_PUBLIC_KEY_SIZE] = {
  24818. 0x70, 0xE1, 0x3F, 0x30, 0x15, 0x17, 0xB5, 0xA4,
  24819. 0x0D, 0x70, 0x36, 0x1F, 0x63, 0x09, 0x41, 0x60,
  24820. 0x67, 0x64, 0x6D, 0x2B, 0x71, 0x36, 0x62, 0x6B,
  24821. 0xCC, 0xCC, 0x17, 0x0C, 0x66, 0xCE, 0xD4, 0x90,
  24822. 0xC7, 0x35, 0x34, 0x4B, 0x62, 0x77, 0x09, 0x7C,
  24823. 0xA9, 0x14, 0x21, 0x2A, 0x29, 0x2D, 0xD1, 0x22,
  24824. 0xFB, 0xB6, 0x9F, 0xDE, 0xCA, 0x47, 0xFA, 0xB4,
  24825. 0x53, 0x2B, 0x8C, 0x80, 0xCE, 0xB7, 0x7F, 0x9C,
  24826. 0x54, 0x3E, 0x0B, 0xF1, 0x53, 0x6D, 0x1C, 0x0C,
  24827. 0xAE, 0x07, 0x7E, 0x2C, 0xA7, 0x86, 0x2B, 0x45,
  24828. 0xA4, 0x10, 0x46, 0x9C, 0xC5, 0xB7, 0x06, 0xBA,
  24829. 0xE0, 0x05, 0x1C, 0xB2, 0x96, 0x1D, 0xB7, 0x27,
  24830. 0x0B, 0x75, 0xB7, 0x11, 0x69, 0x8D, 0x2B, 0x80,
  24831. 0x70, 0x40, 0xD5, 0x62, 0x81, 0x29, 0x43, 0x6F,
  24832. 0xBB, 0x58, 0xF1, 0x20, 0x3F, 0x75, 0x56, 0x14,
  24833. 0x65, 0xF5, 0x42, 0x57, 0xE4, 0x4D, 0x33, 0xF5,
  24834. 0x12, 0xD6, 0x33, 0x43, 0x1D, 0x00, 0xA2, 0xFB,
  24835. 0x02, 0x30, 0xC9, 0xBB, 0x9C, 0xDD, 0xFC, 0x83,
  24836. 0xBD, 0x65, 0xC9, 0x74, 0x45, 0x30, 0x21, 0x86,
  24837. 0xA1, 0x72, 0x23, 0xAD, 0x21, 0x33, 0x28, 0x03,
  24838. 0xB9, 0x09, 0xE5, 0xE5, 0x67, 0x19, 0x70, 0xBB,
  24839. 0xB0, 0xF1, 0xC4, 0x83, 0x7B, 0xB8, 0x42, 0x73,
  24840. 0xBA, 0x67, 0x5A, 0xC0, 0x74, 0xC5, 0x29, 0x0B,
  24841. 0x41, 0x1C, 0x25, 0x00, 0x65, 0x70, 0x59, 0x33,
  24842. 0x9D, 0xE3, 0x92, 0xF9, 0xCA, 0x30, 0x89, 0x52,
  24843. 0xA2, 0x20, 0x1A, 0x58, 0x87, 0x67, 0xAD, 0xC0,
  24844. 0x35, 0xBD, 0xF3, 0x30, 0x24, 0xEA, 0x3B, 0x9A,
  24845. 0x83, 0xC5, 0xA0, 0xB9, 0xC5, 0x42, 0x5D, 0x14,
  24846. 0x07, 0x0C, 0x81, 0xAA, 0xDA, 0x26, 0xBA, 0xC3,
  24847. 0xFB, 0xB8, 0xD4, 0xB7, 0xCF, 0xEE, 0x03, 0x92,
  24848. 0x37, 0x5C, 0x68, 0x42, 0x73, 0x51, 0xDF, 0xEC,
  24849. 0x63, 0x60, 0x9B, 0xBB, 0x50, 0xB4, 0x63, 0xE0,
  24850. 0x40, 0x92, 0x85, 0x70, 0x09, 0xD1, 0xE5, 0xB8,
  24851. 0x1D, 0x70, 0x7D, 0x14, 0xB8, 0x33, 0xCD, 0x4A,
  24852. 0x0B, 0x55, 0x1B, 0xAA, 0x13, 0xEC, 0x48, 0x8A,
  24853. 0x15, 0x03, 0xB0, 0x46, 0x7E, 0xE4, 0x02, 0x3C,
  24854. 0x3F, 0xE0, 0x32, 0xC7, 0x82, 0x25, 0x06, 0x38,
  24855. 0x86, 0xE2, 0x46, 0x8E, 0x00, 0xF7, 0x00, 0x07,
  24856. 0x2A, 0x2E, 0xC8, 0xDA, 0x6A, 0xFB, 0x20, 0x6C,
  24857. 0x91, 0x90, 0x44, 0x33, 0xBB, 0xCC, 0xB0, 0xE7,
  24858. 0x6F, 0x42, 0x46, 0x8C, 0x40, 0xEB, 0x5F, 0x59,
  24859. 0xCB, 0x9A, 0xE1, 0xB0, 0x35, 0xE5, 0x21, 0x51,
  24860. 0x0B, 0xF2, 0x16, 0xA1, 0xAB, 0xCB, 0x19, 0x03,
  24861. 0x3B, 0x7A, 0x65, 0x88, 0x97, 0xC6, 0x58, 0x74,
  24862. 0xD5, 0x13, 0x51, 0x83, 0x14, 0x9F, 0x97, 0x9E,
  24863. 0x55, 0x3C, 0xCF, 0xBF, 0xA3, 0x90, 0x0C, 0xDA,
  24864. 0x6F, 0x01, 0x96, 0x0B, 0x75, 0x15, 0x7F, 0x54,
  24865. 0x53, 0xAA, 0x6E, 0x73, 0xB3, 0xED, 0x90, 0x2F,
  24866. 0x7D, 0x7C, 0x93, 0x05, 0x97, 0x1B, 0xDF, 0x72,
  24867. 0x2E, 0x29, 0x37, 0x16, 0x9A, 0x1B, 0xC0, 0xFA,
  24868. 0xEB, 0x6C, 0x92, 0xF7, 0x15, 0x0D, 0x23, 0x30,
  24869. 0x87, 0x7C, 0x5D, 0xC5, 0x24, 0x9A, 0xAE, 0x20,
  24870. 0x30, 0x26, 0x34, 0xC5, 0xC5, 0xB2, 0x30, 0x53,
  24871. 0x52, 0x10, 0x28, 0x12, 0x25, 0x42, 0xF4, 0x85,
  24872. 0xA0, 0xEA, 0xC8, 0x69, 0x22, 0x37, 0x20, 0x63,
  24873. 0x36, 0x51, 0xF5, 0xB2, 0x47, 0xC6, 0x62, 0xB3,
  24874. 0x1A, 0x10, 0x53, 0x8C, 0xA7, 0x49, 0x1B, 0x14,
  24875. 0x37, 0xAA, 0x74, 0xF4, 0x28, 0x2D, 0x12, 0x97,
  24876. 0x4D, 0x9C, 0x93, 0x4D, 0xF2, 0x14, 0x78, 0x5B,
  24877. 0x64, 0x18, 0x46, 0x8B, 0x92, 0xE5, 0x25, 0x28,
  24878. 0xC8, 0x44, 0x7A, 0x1C, 0xA4, 0x22, 0xFA, 0x6C,
  24879. 0xC8, 0x8E, 0x28, 0xB0, 0x59, 0xF0, 0x4B, 0x23,
  24880. 0x59, 0x73, 0x23, 0xF7, 0x2F, 0x3E, 0x23, 0x36,
  24881. 0xF8, 0x7C, 0x47, 0x90, 0x5C, 0xBA, 0x65, 0x5B,
  24882. 0xB7, 0x3F, 0xC3, 0x2E, 0x18, 0xD4, 0xB7, 0x87,
  24883. 0x05, 0xC7, 0x82, 0xEB, 0xCB, 0x43, 0xE2, 0x78,
  24884. 0x5C, 0x82, 0xC5, 0xAF, 0x24, 0xB0, 0xE1, 0x69,
  24885. 0x9C, 0xFB, 0xC0, 0x25, 0x74, 0x75, 0x79, 0x9A,
  24886. 0x53, 0x9B, 0x11, 0xA5, 0x0F, 0x4D, 0xF2, 0xB7,
  24887. 0xFA, 0xA2, 0x0B, 0xD8, 0x82, 0x75, 0x15, 0xCA,
  24888. 0x37, 0x0F, 0x89, 0xC0, 0xD4, 0xC6, 0x09, 0x02,
  24889. 0xF6, 0x56, 0x7C, 0xD6, 0x0B, 0x08, 0x60, 0xA5,
  24890. 0x5B, 0xC8, 0x57, 0x2C, 0x43, 0x6C, 0x24, 0x6A,
  24891. 0xC2, 0x76, 0x64, 0x4E, 0x7D, 0x60, 0x2A, 0xA5,
  24892. 0x7C, 0x01, 0x66, 0x20, 0x18, 0x14, 0x99, 0x1C,
  24893. 0x1B, 0xD7, 0x5C, 0x7C, 0x47, 0xC3, 0x48, 0xB6,
  24894. 0x7D, 0x77, 0x61, 0x33, 0x86, 0x90, 0x81, 0x44,
  24895. 0xEA, 0x83, 0xFF, 0x72, 0x1F, 0x9A, 0x50, 0x07,
  24896. 0x6C, 0x51, 0x01, 0x64, 0xD1, 0x8E, 0x05, 0xD0,
  24897. 0x5D, 0x98, 0x84, 0xC4, 0x41, 0x46, 0xA0, 0x7C,
  24898. 0xCA, 0xCF, 0x89, 0x04, 0x98, 0xED, 0x1A, 0x19,
  24899. 0xB2, 0xA1, 0x54, 0x31, 0x72, 0x9D, 0xC1, 0xF1,
  24900. 0x2B, 0x7E, 0xA1, 0x0F, 0x9F, 0x92, 0x80, 0x62,
  24901. 0xD1, 0x45, 0x4B, 0x4B, 0x9F, 0x68, 0xE5, 0x99,
  24902. 0x90, 0x29, 0x0B, 0xE3, 0x72, 0x8B, 0x32, 0x89,
  24903. 0x56, 0x93, 0x63, 0xAB, 0x10, 0x05, 0x13, 0x1B,
  24904. 0x23, 0x81, 0xA0, 0x8C, 0xC2, 0xBF, 0x94, 0x3E,
  24905. 0x95, 0xD5, 0xB2, 0x1B, 0xC6, 0xAA, 0xBC, 0x22,
  24906. 0x73, 0x34, 0x8B, 0xC7, 0x2B, 0xD0, 0x93, 0xB7,
  24907. 0xB5, 0x61, 0x7A, 0xE8, 0x7F, 0x60, 0x2B, 0xB9,
  24908. 0x89, 0xE6, 0xAF, 0xC4, 0x4B, 0x81, 0x51, 0x20,
  24909. 0x76, 0xA3, 0xA8, 0x76, 0xE0, 0xE2, 0x5F, 0x97,
  24910. 0x62, 0xB4, 0x62, 0x08, 0x19, 0x85, 0x50, 0x2F,
  24911. 0x26, 0xB2, 0x87, 0xA2, 0x93, 0x6D, 0x5B, 0x1A,
  24912. 0xCF, 0xFC, 0xEC, 0x4E, 0xEE, 0x77, 0xA9, 0xCB,
  24913. 0xA9, 0x80, 0xEB, 0x9B, 0x5F, 0xDE, 0x75, 0x53,
  24914. 0x9F, 0x65, 0x09, 0x04, 0x67, 0x7D, 0xBE, 0x29,
  24915. 0xAB, 0x8B, 0xB9, 0x18, 0xA3, 0x49, 0x48, 0x03,
  24916. 0xEC, 0xA5, 0x9A, 0x2C, 0x32, 0xE5, 0xB5, 0xC8,
  24917. 0x3B, 0x0B, 0x80, 0xB1, 0x10, 0x2C, 0xD7, 0xD9,
  24918. 0x48, 0x2B, 0x45, 0x9B, 0x6B, 0x74, 0x49, 0x1E,
  24919. 0xC3, 0x0C, 0x4B, 0xE7, 0x7C, 0x2B, 0x52, 0x4A,
  24920. 0xF7, 0xB3, 0xAD, 0x1F, 0x71, 0x34, 0x1D, 0xF0,
  24921. 0xA7, 0x6F, 0x25, 0x5C, 0x29, 0x03, 0xC8, 0x82,
  24922. 0x08, 0x07, 0x93, 0x79, 0x93, 0x0A, 0x95, 0x13,
  24923. 0xF3, 0x90, 0x12, 0x6E, 0x73, 0x2A, 0x2B, 0xB0,
  24924. 0x94, 0xBF, 0xA6, 0xBF, 0x0A, 0x43, 0x2B, 0xCD,
  24925. 0x65, 0x7D, 0xAF, 0xCB, 0x25, 0xC8, 0xBB, 0x15,
  24926. 0xE0, 0x95, 0x5D, 0x09, 0x9B, 0x74, 0xFF, 0x1A,
  24927. 0x4D, 0xE6, 0x55, 0x9C, 0xD6, 0x79, 0x7C, 0x38,
  24928. 0xC4, 0x8C, 0x11, 0x34, 0xCA, 0x2C, 0x97, 0x92,
  24929. 0x43, 0xF3, 0x15, 0x2A, 0xF4, 0xBB, 0xE4, 0xD7,
  24930. 0xA6, 0xBC, 0x09, 0x87, 0x21, 0x33, 0x92, 0x0C,
  24931. 0xD2, 0x3B, 0x3E, 0xF9, 0x84, 0x8C, 0xCC, 0x68,
  24932. 0x45, 0xD6, 0x47, 0xB5, 0x38, 0x75, 0x57, 0x73,
  24933. 0x65, 0x13, 0xD5, 0x85, 0x60, 0x84, 0x51, 0x92,
  24934. 0xF9, 0x26, 0x51, 0x59, 0x93, 0x2E, 0x57, 0x2A,
  24935. 0x88, 0xC4, 0x4E, 0x65, 0x66, 0x76, 0x0C, 0x06,
  24936. 0x1C, 0x67, 0xFC, 0xB5, 0xBF, 0x21, 0x00, 0x95,
  24937. 0xE2, 0x14, 0xDA, 0x74, 0x53, 0x57, 0xE3, 0x69,
  24938. 0x96, 0xD8, 0xC0, 0x66, 0x31, 0x1B, 0xBC, 0x76,
  24939. 0x1A, 0x1F, 0xD2, 0x52, 0x73, 0xD2, 0x1E, 0xAB,
  24940. 0x50, 0x01, 0x05, 0x63, 0xCD, 0x64, 0x68, 0xA4,
  24941. 0xEA, 0x83, 0x6B, 0x6D, 0x64, 0xBD, 0x2B, 0xD7,
  24942. 0x6D, 0xBE, 0x35, 0x82, 0xD5, 0x73, 0x6A, 0x60,
  24943. 0x5A, 0x55, 0x09, 0xFC, 0x28, 0x78, 0x9B, 0x56,
  24944. 0xB8, 0x84, 0xAE, 0x9A, 0x60, 0x41, 0x5F, 0x55,
  24945. 0x67, 0x4B, 0xE6, 0x01, 0x57, 0x6C, 0x7C, 0xEE,
  24946. 0x58, 0x14, 0x3B, 0xF0, 0x54, 0x80, 0x6A, 0xBC,
  24947. 0xB3, 0x45, 0xA2, 0x56, 0xCB, 0xC4, 0x54, 0xE3,
  24948. 0x43, 0xF3, 0xCC, 0x7A, 0xDE, 0x65, 0x56, 0x2F,
  24949. 0xD2, 0x9E, 0xB2, 0x59, 0x73, 0x7B, 0xB3, 0xCF,
  24950. 0x96, 0x49, 0xBD, 0xEA, 0x28, 0x3F, 0xB0, 0x72,
  24951. 0x65, 0x67, 0x7C, 0x98, 0x08, 0xD1, 0x31, 0x19,
  24952. 0xC0, 0xA2, 0xAD, 0xF7, 0x45, 0xDE, 0x69, 0x75,
  24953. 0xF4, 0x56, 0x2C, 0xD6, 0x15, 0x57, 0xB3, 0x96,
  24954. 0x5D, 0x2B, 0x07, 0x2F, 0x00, 0x0A, 0xA7, 0xE0,
  24955. 0xA3, 0x57, 0xE1, 0x25, 0x3E, 0xAF, 0xEA, 0x7F,
  24956. 0xDF, 0xCC, 0x92, 0xFA, 0x87, 0x63, 0x0D, 0xD2,
  24957. 0x27, 0x6C, 0xE4, 0x2E, 0x82, 0x0B, 0x69, 0xD1,
  24958. 0xFC, 0x2E, 0x47, 0xD5, 0xC4, 0x98, 0xA5, 0x5B,
  24959. 0x3B, 0x29, 0xC3, 0x4E, 0x64, 0x90, 0x3D, 0x04,
  24960. 0x7A, 0xB1, 0xC0, 0x40, 0x24, 0x95, 0x8F, 0x70,
  24961. 0x11, 0x95, 0xF5, 0xD1, 0x3E, 0xC6, 0x70, 0x6B,
  24962. 0x84, 0x48, 0x50, 0x3A, 0x54, 0x99, 0x22, 0xA5,
  24963. 0x8A, 0x24, 0xB6, 0x7C, 0x93, 0x63, 0x27, 0x56,
  24964. 0xB7, 0x7D, 0x22, 0x54, 0x07, 0x31, 0x61, 0x71,
  24965. 0xDE, 0xEC, 0x56, 0x71, 0x44, 0x35, 0xCF, 0x94,
  24966. 0xCC, 0xF4, 0x59, 0x9E, 0x00, 0xD1, 0x0E, 0x56,
  24967. 0x96, 0x22, 0xBA, 0xDA, 0x82, 0x0C, 0x45, 0x2F,
  24968. 0x25, 0x42, 0xAD, 0xF0, 0x87, 0x65, 0xCA, 0x93,
  24969. 0xAE, 0x38, 0xEB, 0x02, 0x5D, 0xE3, 0x1C, 0xFF,
  24970. 0x79, 0x74, 0x54, 0x9A, 0x78, 0x25, 0xA8, 0x31,
  24971. 0xDD, 0x05, 0x4E, 0x87, 0xB8, 0x4C, 0x5F, 0x25,
  24972. 0x47, 0xFF, 0x47, 0xB4, 0x6F, 0x88, 0xC9, 0x9F,
  24973. 0x15, 0x48, 0xE9, 0x33, 0xA6, 0xF4, 0xD8, 0x7F,
  24974. 0x1A, 0x4A, 0x1B, 0x00, 0xE3, 0x9E, 0x02, 0xD6,
  24975. 0x0E, 0x51, 0xEB, 0x60, 0x3C, 0x1C, 0x0D, 0x80,
  24976. 0x7A, 0xCD, 0xAB, 0x08, 0xBA, 0xA2, 0xB9, 0x98,
  24977. 0x69, 0xB7, 0x5C, 0xA2, 0xC4, 0xB9, 0x63, 0x68,
  24978. 0xB5, 0x17, 0x80, 0xBD, 0x1E, 0xC7, 0x5B, 0x11,
  24979. 0x0B, 0x9F, 0xA6, 0x65, 0x56, 0x87, 0x6C, 0x5F,
  24980. 0x48, 0x79, 0x7D, 0x09, 0x01, 0x38, 0xF7, 0x54,
  24981. 0xAE, 0x30, 0x53, 0x3D, 0x36, 0xAA, 0x44, 0xB9,
  24982. 0xB1, 0x70, 0x2A, 0x6A, 0x8A, 0x56, 0x62, 0x6B,
  24983. 0xF0, 0x45, 0x1A, 0x37, 0xA7, 0xAC, 0x1A, 0x33,
  24984. 0x70, 0x76, 0xE5, 0x1E, 0x0A, 0x6B, 0x03, 0x00,
  24985. 0xC2, 0xC7, 0x90, 0xA4, 0x43, 0x7E, 0xA2, 0x8D,
  24986. 0x7E, 0xC9, 0x8C, 0x41, 0x9B, 0x37, 0xD6, 0xAA,
  24987. 0x97, 0x04, 0x17, 0x43, 0x5F, 0x91, 0xBE, 0xDC,
  24988. 0x2B, 0x1F, 0x4B, 0xC8, 0x15, 0x8A, 0x51, 0xB1,
  24989. 0xF4, 0x71, 0x51, 0x6F, 0xE8, 0x24, 0x28, 0x7C,
  24990. 0x89, 0x6B, 0x89, 0x1B, 0x49, 0xF2, 0x54, 0xDD,
  24991. 0x36, 0x35, 0x9B, 0x89, 0xC8, 0x24, 0xEB, 0x3F,
  24992. 0x62, 0x48, 0x02, 0x7F, 0xBB, 0xAD, 0x4C, 0xF2,
  24993. 0x91, 0x18, 0xCB, 0x50, 0xEB, 0xB6, 0x25, 0xA3,
  24994. 0x7C, 0x53, 0x7A, 0x02, 0x23, 0xF0, 0xEB, 0x70,
  24995. 0x85, 0xB5, 0xC7, 0xEC, 0x60, 0x75, 0x70, 0xDB,
  24996. 0x91, 0x85, 0xD5, 0x99, 0x02, 0xBC, 0x26, 0xC6,
  24997. 0x54, 0xA2, 0x80, 0x4C, 0x0D, 0x94, 0x67, 0x93,
  24998. 0xD8, 0xA2, 0x14, 0x82, 0xAC, 0x4F, 0x05, 0xE9,
  24999. 0x01, 0x62, 0x60, 0x33, 0x1D, 0xCC, 0x58, 0xBC,
  25000. 0x66, 0xAF, 0x3C, 0xA7, 0x58, 0x54, 0x40, 0x21,
  25001. 0x6A, 0xA0, 0x26, 0x3B, 0x2A, 0x72, 0x5E, 0x08,
  25002. 0x0F, 0x6F, 0x9C, 0x5B, 0x6A, 0x9C, 0x9D, 0xA2,
  25003. 0x93, 0x55, 0x18, 0x9B, 0x4B, 0x95, 0xB1, 0x37,
  25004. 0xD1, 0x22, 0x5F, 0x25, 0x2A, 0xC7, 0x97, 0xB0,
  25005. 0x64, 0x6C, 0xAC, 0x52, 0x16, 0x4B, 0x59, 0x72,
  25006. 0xA9, 0x92, 0x65, 0xD3, 0x47, 0xFC, 0x7C, 0x35,
  25007. 0x91, 0xD1, 0x5F, 0xFE, 0x68, 0x1C, 0x06, 0xD4,
  25008. 0x38, 0xCC, 0xEB, 0x60, 0xBB, 0x63, 0x10, 0xB7,
  25009. 0x95, 0x32, 0x89, 0x72, 0x0E, 0x2C, 0x72, 0x87,
  25010. 0x30, 0x05, 0x23, 0x37, 0xAC, 0xA7, 0xC8, 0x52,
  25011. 0x1A, 0xB4, 0x4F, 0x1E, 0x2A, 0x04, 0x9B, 0x83,
  25012. 0xE0, 0x77, 0x4C, 0x96, 0xCD, 0x8C, 0x87, 0x6F,
  25013. 0xA6, 0x75, 0xD0, 0x92, 0x39, 0x77, 0x27, 0x1B
  25014. };
  25015. static const byte dk_1024[KYBER1024_PRIVATE_KEY_SIZE] = {
  25016. 0x8A, 0xD0, 0xB5, 0xF0, 0x9A, 0x25, 0xAA, 0x93,
  25017. 0x5D, 0xD9, 0xDA, 0x34, 0xAB, 0x82, 0xCA, 0x75,
  25018. 0xA1, 0x2D, 0x66, 0xE9, 0x9C, 0xF4, 0x8B, 0xCA,
  25019. 0x45, 0xB9, 0xB2, 0xDB, 0x44, 0x1B, 0xC2, 0x97,
  25020. 0x1B, 0xDC, 0x99, 0x22, 0xB5, 0xF8, 0xBC, 0x3C,
  25021. 0x06, 0x78, 0x54, 0x67, 0x59, 0x07, 0x3C, 0xB8,
  25022. 0x8E, 0x26, 0xBA, 0xD1, 0xB1, 0xB3, 0xA4, 0x64,
  25023. 0x6A, 0x65, 0x29, 0xC6, 0x32, 0xEA, 0xA3, 0x47,
  25024. 0x73, 0x4A, 0x3B, 0xE5, 0x83, 0xD4, 0x71, 0x78,
  25025. 0x09, 0x4C, 0x4A, 0x67, 0x0C, 0xBC, 0x41, 0xEC,
  25026. 0x06, 0x89, 0x76, 0x56, 0x68, 0x54, 0x2E, 0x6F,
  25027. 0x15, 0xA7, 0xD5, 0x86, 0xC9, 0xE2, 0x6A, 0x6A,
  25028. 0x03, 0xC7, 0x14, 0x69, 0xC2, 0xC5, 0x3F, 0x7B,
  25029. 0x14, 0x1B, 0x23, 0x2D, 0x86, 0x21, 0x6A, 0x25,
  25030. 0xC7, 0xA8, 0xF3, 0x68, 0x52, 0x85, 0x8C, 0x07,
  25031. 0xA9, 0x52, 0x4E, 0xE1, 0x7B, 0xA6, 0x34, 0x0A,
  25032. 0xA2, 0xA2, 0x15, 0xC1, 0xEA, 0x85, 0x21, 0x67,
  25033. 0xB6, 0x89, 0x1C, 0xC1, 0x66, 0xC2, 0xFA, 0x13,
  25034. 0xA0, 0x27, 0x0A, 0x22, 0x98, 0x34, 0x13, 0xE0,
  25035. 0xAC, 0xC4, 0x44, 0xBF, 0x40, 0xE2, 0x8C, 0x45,
  25036. 0xE1, 0x4E, 0x07, 0x40, 0x4F, 0x62, 0x99, 0x63,
  25037. 0x69, 0x59, 0x7F, 0x10, 0xFC, 0xC1, 0x80, 0xEC,
  25038. 0xAC, 0xAD, 0x1A, 0x67, 0x19, 0xAB, 0x9F, 0x1B,
  25039. 0x44, 0x7A, 0xE1, 0x9A, 0x2C, 0xB0, 0x2A, 0x7D,
  25040. 0x04, 0x20, 0x61, 0x72, 0x16, 0x8C, 0x4F, 0x0A,
  25041. 0x99, 0xBA, 0xFA, 0x93, 0x2D, 0x66, 0x49, 0xE8,
  25042. 0x94, 0xA8, 0xF0, 0x57, 0x7B, 0x81, 0xC6, 0x64,
  25043. 0x83, 0xC5, 0xB5, 0xCF, 0x60, 0xAE, 0x75, 0xA4,
  25044. 0x44, 0x52, 0x6A, 0x9B, 0x36, 0x74, 0x32, 0x5F,
  25045. 0xBA, 0x38, 0xF5, 0x32, 0x96, 0x42, 0x1A, 0x78,
  25046. 0x50, 0x11, 0xC1, 0xDD, 0xB3, 0xA6, 0x99, 0x77,
  25047. 0x45, 0xDB, 0x83, 0xCD, 0x58, 0x3C, 0x0C, 0x41,
  25048. 0x77, 0xC7, 0x97, 0xD4, 0x0A, 0x4F, 0x69, 0x9F,
  25049. 0x1F, 0x40, 0xC5, 0x41, 0x3A, 0xC4, 0xE4, 0x23,
  25050. 0x73, 0x49, 0x2B, 0x6A, 0x2C, 0x6A, 0x40, 0x6D,
  25051. 0x43, 0x7F, 0x42, 0x57, 0x0B, 0x5E, 0x94, 0x9E,
  25052. 0xF4, 0x35, 0x0D, 0xEA, 0x79, 0x0C, 0xFE, 0xB7,
  25053. 0x2D, 0x12, 0x87, 0x51, 0x7F, 0xE3, 0x27, 0x3D,
  25054. 0x3C, 0xA6, 0x5A, 0x13, 0xCA, 0x6E, 0x23, 0xC5,
  25055. 0x7B, 0xF0, 0x7D, 0xA0, 0x4B, 0x85, 0x1C, 0xF3,
  25056. 0xAF, 0xA1, 0x8B, 0xAF, 0x5E, 0xF0, 0x20, 0x79,
  25057. 0x28, 0x57, 0xA9, 0xE7, 0x21, 0xF0, 0x1B, 0x9F,
  25058. 0xEA, 0x7B, 0x61, 0x2E, 0x4C, 0x6E, 0x29, 0x07,
  25059. 0x93, 0x66, 0xB0, 0x22, 0x86, 0x88, 0xBE, 0x2A,
  25060. 0x06, 0x7F, 0xBE, 0x92, 0x84, 0x2D, 0xD2, 0x80,
  25061. 0xB3, 0xC7, 0x4D, 0xFA, 0xB7, 0x61, 0xE6, 0x13,
  25062. 0xA8, 0x60, 0x4C, 0x47, 0x6E, 0x15, 0x46, 0x66,
  25063. 0x85, 0xC6, 0x95, 0xAC, 0x35, 0x79, 0x1A, 0x91,
  25064. 0x59, 0x94, 0x2F, 0x60, 0x17, 0x0C, 0xA2, 0x14,
  25065. 0xC7, 0xC0, 0x9B, 0x1A, 0x4B, 0x1B, 0xCC, 0x4F,
  25066. 0x4C, 0xC6, 0x0D, 0xF0, 0x1A, 0x10, 0x19, 0x15,
  25067. 0xA9, 0xA2, 0xBC, 0x55, 0x31, 0x19, 0x66, 0x50,
  25068. 0x32, 0xDC, 0xD9, 0x47, 0x6F, 0xBA, 0x7B, 0xB0,
  25069. 0x71, 0x57, 0xD3, 0x3C, 0x9C, 0x8E, 0xFA, 0x6B,
  25070. 0xD0, 0xAC, 0x38, 0xC1, 0xAC, 0x26, 0x5F, 0xB5,
  25071. 0x18, 0x57, 0xD0, 0x15, 0x17, 0x61, 0x53, 0x26,
  25072. 0xCA, 0x0E, 0x08, 0x65, 0x0B, 0xA6, 0xFA, 0x40,
  25073. 0x83, 0x2C, 0x7B, 0x4C, 0x41, 0xB6, 0x44, 0x71,
  25074. 0x60, 0x22, 0xB6, 0x52, 0xB1, 0x92, 0x7D, 0x55,
  25075. 0xC9, 0xB3, 0x7F, 0xE2, 0x5F, 0x1A, 0xB6, 0x7A,
  25076. 0x9A, 0x03, 0xC7, 0x00, 0x8C, 0x84, 0xB0, 0x7C,
  25077. 0x49, 0x26, 0xB6, 0x38, 0x1E, 0x40, 0xCF, 0xD4,
  25078. 0x41, 0x04, 0x12, 0x35, 0x18, 0x74, 0x16, 0xCE,
  25079. 0xC3, 0x66, 0xCA, 0x6F, 0xB7, 0x6F, 0xA0, 0xAB,
  25080. 0x6E, 0x32, 0x8A, 0x26, 0x41, 0xFC, 0x47, 0xDC,
  25081. 0xD7, 0x6E, 0x91, 0xCA, 0x94, 0x31, 0xE1, 0x9B,
  25082. 0xFF, 0x02, 0xCE, 0x62, 0x28, 0xC2, 0x33, 0x63,
  25083. 0x82, 0xF8, 0xA1, 0x0E, 0x9E, 0xE2, 0xC8, 0xF1,
  25084. 0x75, 0x93, 0x90, 0xA2, 0x00, 0x24, 0xA1, 0x5B,
  25085. 0x3B, 0x09, 0x0C, 0x13, 0x90, 0xCA, 0x03, 0x43,
  25086. 0x79, 0x72, 0x84, 0x24, 0x6B, 0xD8, 0x94, 0x35,
  25087. 0x07, 0xB7, 0xA6, 0xB7, 0x1F, 0xC3, 0x3A, 0x03,
  25088. 0xB7, 0xA8, 0x83, 0x66, 0xE4, 0xAF, 0xED, 0x51,
  25089. 0x57, 0x39, 0xE5, 0xC6, 0x9F, 0x8A, 0x26, 0x6E,
  25090. 0x4A, 0x1F, 0x53, 0xD7, 0x39, 0x30, 0xE9, 0x87,
  25091. 0x55, 0x69, 0x31, 0x2B, 0x27, 0x03, 0x7E, 0x5C,
  25092. 0x7F, 0x85, 0x21, 0x00, 0xC2, 0xBA, 0x36, 0x48,
  25093. 0xB1, 0xB9, 0xC1, 0xB1, 0x49, 0xF6, 0x25, 0x0E,
  25094. 0x0A, 0x6B, 0x06, 0x52, 0x13, 0x13, 0x4F, 0x30,
  25095. 0x25, 0x69, 0x75, 0x5B, 0x8C, 0x5C, 0x4F, 0xFC,
  25096. 0x68, 0x0B, 0xF7, 0x81, 0x18, 0x45, 0x34, 0x00,
  25097. 0x35, 0xF1, 0x70, 0xB0, 0x68, 0xBA, 0x67, 0xA4,
  25098. 0xC3, 0xB0, 0x16, 0x6D, 0x03, 0xCC, 0x82, 0x61,
  25099. 0x84, 0x01, 0x90, 0xA2, 0x0F, 0x9A, 0x3B, 0x1E,
  25100. 0xF4, 0x65, 0xC2, 0xF2, 0x18, 0x2D, 0xA8, 0xDA,
  25101. 0x8D, 0x3B, 0x3C, 0x8C, 0xB1, 0x29, 0x15, 0xF7,
  25102. 0xD9, 0x3E, 0x04, 0xD8, 0x84, 0x0C, 0x35, 0x67,
  25103. 0x25, 0x5A, 0x7B, 0xD6, 0xD4, 0x33, 0xCF, 0x10,
  25104. 0x68, 0xD8, 0x84, 0x52, 0xCF, 0xC1, 0x1F, 0x99,
  25105. 0x1B, 0x7C, 0xE3, 0x79, 0x27, 0xD6, 0xCA, 0xAE,
  25106. 0x88, 0x10, 0x74, 0x2F, 0x42, 0x14, 0x8B, 0x89,
  25107. 0x6E, 0xC4, 0xEB, 0xB5, 0x34, 0x03, 0x86, 0x31,
  25108. 0x5B, 0x2C, 0x1E, 0x2B, 0x43, 0x91, 0x5C, 0x04,
  25109. 0x54, 0x9C, 0xC8, 0xC1, 0x9A, 0xB4, 0x0E, 0x3B,
  25110. 0x7C, 0x31, 0x1B, 0x42, 0x61, 0x10, 0xA9, 0xBB,
  25111. 0xB1, 0x8D, 0x3B, 0x99, 0x2A, 0x42, 0xC0, 0x18,
  25112. 0x92, 0x90, 0xBE, 0x67, 0x3A, 0x39, 0x7C, 0x40,
  25113. 0x90, 0x44, 0x3B, 0x88, 0xC5, 0xD5, 0xC5, 0x65,
  25114. 0xA1, 0x0F, 0xEA, 0x05, 0x60, 0x3D, 0x36, 0x24,
  25115. 0x4A, 0x4A, 0xA8, 0xE9, 0x25, 0x5C, 0xF1, 0x84,
  25116. 0xAE, 0x69, 0x53, 0x5A, 0x83, 0x99, 0xC1, 0xC6,
  25117. 0xF7, 0x6C, 0xF2, 0x34, 0x2A, 0xDF, 0xEA, 0x6A,
  25118. 0x44, 0x7B, 0xB4, 0x50, 0x1B, 0x9A, 0x6C, 0x44,
  25119. 0x59, 0x3E, 0xB0, 0x43, 0xE7, 0xA5, 0x50, 0x2F,
  25120. 0x58, 0x6C, 0xF3, 0x40, 0x7D, 0xEB, 0x7A, 0x0F,
  25121. 0xC3, 0x2B, 0x3F, 0x46, 0xF1, 0x24, 0x5C, 0x55,
  25122. 0x96, 0xE0, 0xF1, 0xBE, 0xD9, 0x37, 0x20, 0x7C,
  25123. 0x45, 0x09, 0xE1, 0xD8, 0x98, 0x5B, 0xE7, 0x45,
  25124. 0xFD, 0x69, 0xBF, 0x44, 0x80, 0x92, 0x43, 0x30,
  25125. 0x28, 0xBE, 0x25, 0x95, 0x90, 0x33, 0x11, 0x47,
  25126. 0x95, 0x86, 0xA3, 0x4B, 0x2D, 0x49, 0x10, 0x74,
  25127. 0x10, 0xBC, 0x4B, 0xD2, 0x96, 0x53, 0x17, 0xFC,
  25128. 0x76, 0x35, 0x2B, 0x63, 0x8D, 0xF3, 0xB3, 0xA3,
  25129. 0x15, 0x32, 0x50, 0x26, 0x80, 0x9E, 0x3B, 0xC4,
  25130. 0x60, 0x8C, 0x0B, 0x2C, 0xB8, 0x4D, 0xF0, 0xC9,
  25131. 0x5B, 0xC0, 0x52, 0x70, 0x7F, 0xC1, 0xA3, 0x77,
  25132. 0xB2, 0xB4, 0x65, 0xEB, 0x7A, 0x5D, 0x64, 0x4A,
  25133. 0xB4, 0x27, 0x8D, 0xDC, 0xE5, 0xB6, 0x1E, 0x2B,
  25134. 0xB3, 0xA7, 0x10, 0x52, 0x55, 0x5C, 0xB3, 0xBA,
  25135. 0xC6, 0x93, 0xEF, 0x02, 0x5F, 0xF0, 0x03, 0x53,
  25136. 0xFB, 0x76, 0x94, 0x5B, 0x8A, 0xA3, 0xE9, 0x95,
  25137. 0x0F, 0x92, 0x73, 0x81, 0x87, 0x91, 0xCC, 0xAD,
  25138. 0x56, 0x88, 0x46, 0x58, 0x14, 0x2A, 0x2B, 0x4D,
  25139. 0xF3, 0xC5, 0x7E, 0xCA, 0x13, 0xAD, 0x44, 0xB4,
  25140. 0x9B, 0x63, 0x46, 0xC6, 0x3E, 0xE8, 0x90, 0x78,
  25141. 0x58, 0x9E, 0x9E, 0xB9, 0xA9, 0x80, 0x4A, 0x03,
  25142. 0xBF, 0x7A, 0x27, 0x6F, 0x86, 0xB9, 0x67, 0x6C,
  25143. 0x58, 0xD3, 0xE7, 0x1D, 0x2C, 0x87, 0x70, 0x80,
  25144. 0x4A, 0x61, 0x59, 0x21, 0x78, 0xB4, 0x49, 0xC7,
  25145. 0x95, 0x5B, 0xBE, 0x8C, 0xF4, 0x2F, 0x31, 0x67,
  25146. 0x25, 0xE3, 0xB1, 0x6D, 0x55, 0xB5, 0x27, 0xCF,
  25147. 0xB2, 0x32, 0x68, 0x1B, 0x21, 0xB2, 0xCB, 0x2F,
  25148. 0x30, 0xAC, 0x76, 0x01, 0x5B, 0xB5, 0x41, 0x6A,
  25149. 0x04, 0x11, 0xC1, 0x74, 0x58, 0x92, 0x41, 0x2E,
  25150. 0x68, 0x3A, 0x98, 0xD7, 0x36, 0xED, 0x1A, 0x4C,
  25151. 0xD9, 0x80, 0x61, 0x7D, 0x08, 0x21, 0xC2, 0xAB,
  25152. 0x02, 0x82, 0x07, 0x0A, 0x61, 0x1A, 0x11, 0xD1,
  25153. 0x97, 0x01, 0xFB, 0xD5, 0x5A, 0x21, 0x27, 0xB3,
  25154. 0x24, 0xE6, 0x90, 0x1D, 0x84, 0x98, 0x6C, 0x04,
  25155. 0x64, 0xDE, 0x71, 0x20, 0xAF, 0x45, 0x10, 0xAF,
  25156. 0x59, 0x1D, 0xD9, 0xBB, 0x79, 0x47, 0x9C, 0x5F,
  25157. 0xA8, 0x87, 0x14, 0xC6, 0xA9, 0x97, 0x14, 0xF7,
  25158. 0x6A, 0x1E, 0x40, 0x2C, 0x8F, 0x38, 0x4A, 0x4E,
  25159. 0xE6, 0xBC, 0xD4, 0x15, 0x00, 0x72, 0x4C, 0xC1,
  25160. 0x79, 0x3A, 0xBF, 0xD8, 0xD3, 0xC2, 0xF3, 0x20,
  25161. 0x39, 0x71, 0x34, 0xB0, 0x0F, 0x76, 0x2D, 0xBA,
  25162. 0x85, 0xA2, 0x3A, 0xF1, 0x55, 0xE6, 0xCC, 0x03,
  25163. 0x73, 0x80, 0xC1, 0xDD, 0x64, 0xA9, 0x73, 0xDB,
  25164. 0x35, 0xB7, 0x44, 0x70, 0x44, 0x8B, 0x24, 0x21,
  25165. 0x20, 0x62, 0x76, 0x47, 0x87, 0xE5, 0xA9, 0x3A,
  25166. 0x48, 0x80, 0x71, 0x71, 0xA7, 0x71, 0x5C, 0xFC,
  25167. 0x89, 0xBC, 0xBC, 0x9E, 0x14, 0x18, 0x86, 0xF8,
  25168. 0x07, 0xA1, 0xF9, 0xD6, 0x84, 0xC8, 0x42, 0x6F,
  25169. 0x01, 0x22, 0x88, 0x7D, 0x9C, 0x4C, 0x27, 0xEA,
  25170. 0x69, 0xCC, 0x15, 0x1B, 0x4D, 0x49, 0xB5, 0x1E,
  25171. 0x5A, 0x4E, 0xAA, 0xA5, 0xAD, 0x06, 0xAB, 0xA8,
  25172. 0x6D, 0xF9, 0x42, 0xE9, 0x86, 0xA5, 0xD5, 0x79,
  25173. 0x20, 0x80, 0xFC, 0x48, 0x03, 0x96, 0xB3, 0x94,
  25174. 0x86, 0x68, 0xFB, 0x38, 0x2C, 0xC8, 0xFC, 0x15,
  25175. 0x47, 0x48, 0xCB, 0x30, 0xB7, 0x64, 0x1F, 0x02,
  25176. 0x70, 0xC8, 0x34, 0x38, 0xB4, 0xFC, 0x3D, 0x19,
  25177. 0x01, 0x26, 0x58, 0x80, 0x40, 0x51, 0x77, 0xBC,
  25178. 0x7F, 0x44, 0x78, 0x82, 0x51, 0xAB, 0xC4, 0x74,
  25179. 0x27, 0x35, 0x31, 0x21, 0x2A, 0x66, 0x27, 0x9E,
  25180. 0x70, 0x33, 0x7A, 0x23, 0x09, 0xFD, 0xF4, 0x9E,
  25181. 0x05, 0x9B, 0xBD, 0xAF, 0x49, 0x73, 0xA5, 0x37,
  25182. 0x7A, 0x4D, 0x51, 0x7B, 0xA7, 0x55, 0x70, 0x2C,
  25183. 0x37, 0xCC, 0x35, 0x56, 0x85, 0x40, 0x4C, 0x95,
  25184. 0x2F, 0xB6, 0x7E, 0x04, 0x19, 0xC7, 0x8D, 0x15,
  25185. 0x84, 0xD0, 0x94, 0x92, 0x54, 0xD0, 0x49, 0x52,
  25186. 0xF7, 0x24, 0x3B, 0xF1, 0x40, 0x28, 0x03, 0xC9,
  25187. 0xFC, 0x73, 0x73, 0x25, 0x88, 0x13, 0x78, 0xCA,
  25188. 0x77, 0xEE, 0xF5, 0xC4, 0x15, 0xFB, 0x03, 0x7D,
  25189. 0x68, 0x9A, 0x58, 0x54, 0xA1, 0xD2, 0x4B, 0x65,
  25190. 0x27, 0xA5, 0x9B, 0x9B, 0x16, 0x95, 0x93, 0x84,
  25191. 0x35, 0x8C, 0x42, 0x3C, 0x79, 0x64, 0x5C, 0xCF,
  25192. 0x31, 0x33, 0xE2, 0x1B, 0x4B, 0x64, 0x95, 0x7B,
  25193. 0x14, 0xF6, 0x3F, 0x2A, 0xA2, 0x63, 0x57, 0xB1,
  25194. 0xC2, 0x62, 0xF2, 0xA9, 0x0F, 0x7C, 0xCC, 0x2A,
  25195. 0x15, 0x93, 0x69, 0x99, 0xB0, 0xA1, 0xB4, 0x98,
  25196. 0xAB, 0x3B, 0x32, 0x43, 0x30, 0x32, 0xC9, 0xCA,
  25197. 0x23, 0x08, 0x1C, 0x55, 0xD3, 0x1C, 0xAD, 0x36,
  25198. 0xE9, 0x0C, 0x1C, 0xE0, 0xB5, 0xFC, 0x24, 0x7C,
  25199. 0xE8, 0xC8, 0x43, 0xF2, 0x88, 0x35, 0x24, 0xB6,
  25200. 0x64, 0xFA, 0xC1, 0xB2, 0x0B, 0xE6, 0x02, 0xA1,
  25201. 0x0A, 0xAF, 0x65, 0x73, 0x86, 0x80, 0xBB, 0x10,
  25202. 0x25, 0x44, 0x26, 0xF9, 0xCB, 0x09, 0xA1, 0x95,
  25203. 0x4D, 0xB7, 0x65, 0x56, 0x22, 0x30, 0x8F, 0xAE,
  25204. 0xF5, 0x05, 0xAC, 0xB4, 0x97, 0x55, 0x4A, 0x8F,
  25205. 0xCF, 0xA9, 0x6A, 0x85, 0x25, 0x5A, 0xD8, 0x46,
  25206. 0x54, 0x20, 0x13, 0xB8, 0x41, 0x59, 0x51, 0xBD,
  25207. 0xD4, 0x5C, 0x01, 0x93, 0x1E, 0xBE, 0x58, 0x3E,
  25208. 0x70, 0xE1, 0x3F, 0x30, 0x15, 0x17, 0xB5, 0xA4,
  25209. 0x0D, 0x70, 0x36, 0x1F, 0x63, 0x09, 0x41, 0x60,
  25210. 0x67, 0x64, 0x6D, 0x2B, 0x71, 0x36, 0x62, 0x6B,
  25211. 0xCC, 0xCC, 0x17, 0x0C, 0x66, 0xCE, 0xD4, 0x90,
  25212. 0xC7, 0x35, 0x34, 0x4B, 0x62, 0x77, 0x09, 0x7C,
  25213. 0xA9, 0x14, 0x21, 0x2A, 0x29, 0x2D, 0xD1, 0x22,
  25214. 0xFB, 0xB6, 0x9F, 0xDE, 0xCA, 0x47, 0xFA, 0xB4,
  25215. 0x53, 0x2B, 0x8C, 0x80, 0xCE, 0xB7, 0x7F, 0x9C,
  25216. 0x54, 0x3E, 0x0B, 0xF1, 0x53, 0x6D, 0x1C, 0x0C,
  25217. 0xAE, 0x07, 0x7E, 0x2C, 0xA7, 0x86, 0x2B, 0x45,
  25218. 0xA4, 0x10, 0x46, 0x9C, 0xC5, 0xB7, 0x06, 0xBA,
  25219. 0xE0, 0x05, 0x1C, 0xB2, 0x96, 0x1D, 0xB7, 0x27,
  25220. 0x0B, 0x75, 0xB7, 0x11, 0x69, 0x8D, 0x2B, 0x80,
  25221. 0x70, 0x40, 0xD5, 0x62, 0x81, 0x29, 0x43, 0x6F,
  25222. 0xBB, 0x58, 0xF1, 0x20, 0x3F, 0x75, 0x56, 0x14,
  25223. 0x65, 0xF5, 0x42, 0x57, 0xE4, 0x4D, 0x33, 0xF5,
  25224. 0x12, 0xD6, 0x33, 0x43, 0x1D, 0x00, 0xA2, 0xFB,
  25225. 0x02, 0x30, 0xC9, 0xBB, 0x9C, 0xDD, 0xFC, 0x83,
  25226. 0xBD, 0x65, 0xC9, 0x74, 0x45, 0x30, 0x21, 0x86,
  25227. 0xA1, 0x72, 0x23, 0xAD, 0x21, 0x33, 0x28, 0x03,
  25228. 0xB9, 0x09, 0xE5, 0xE5, 0x67, 0x19, 0x70, 0xBB,
  25229. 0xB0, 0xF1, 0xC4, 0x83, 0x7B, 0xB8, 0x42, 0x73,
  25230. 0xBA, 0x67, 0x5A, 0xC0, 0x74, 0xC5, 0x29, 0x0B,
  25231. 0x41, 0x1C, 0x25, 0x00, 0x65, 0x70, 0x59, 0x33,
  25232. 0x9D, 0xE3, 0x92, 0xF9, 0xCA, 0x30, 0x89, 0x52,
  25233. 0xA2, 0x20, 0x1A, 0x58, 0x87, 0x67, 0xAD, 0xC0,
  25234. 0x35, 0xBD, 0xF3, 0x30, 0x24, 0xEA, 0x3B, 0x9A,
  25235. 0x83, 0xC5, 0xA0, 0xB9, 0xC5, 0x42, 0x5D, 0x14,
  25236. 0x07, 0x0C, 0x81, 0xAA, 0xDA, 0x26, 0xBA, 0xC3,
  25237. 0xFB, 0xB8, 0xD4, 0xB7, 0xCF, 0xEE, 0x03, 0x92,
  25238. 0x37, 0x5C, 0x68, 0x42, 0x73, 0x51, 0xDF, 0xEC,
  25239. 0x63, 0x60, 0x9B, 0xBB, 0x50, 0xB4, 0x63, 0xE0,
  25240. 0x40, 0x92, 0x85, 0x70, 0x09, 0xD1, 0xE5, 0xB8,
  25241. 0x1D, 0x70, 0x7D, 0x14, 0xB8, 0x33, 0xCD, 0x4A,
  25242. 0x0B, 0x55, 0x1B, 0xAA, 0x13, 0xEC, 0x48, 0x8A,
  25243. 0x15, 0x03, 0xB0, 0x46, 0x7E, 0xE4, 0x02, 0x3C,
  25244. 0x3F, 0xE0, 0x32, 0xC7, 0x82, 0x25, 0x06, 0x38,
  25245. 0x86, 0xE2, 0x46, 0x8E, 0x00, 0xF7, 0x00, 0x07,
  25246. 0x2A, 0x2E, 0xC8, 0xDA, 0x6A, 0xFB, 0x20, 0x6C,
  25247. 0x91, 0x90, 0x44, 0x33, 0xBB, 0xCC, 0xB0, 0xE7,
  25248. 0x6F, 0x42, 0x46, 0x8C, 0x40, 0xEB, 0x5F, 0x59,
  25249. 0xCB, 0x9A, 0xE1, 0xB0, 0x35, 0xE5, 0x21, 0x51,
  25250. 0x0B, 0xF2, 0x16, 0xA1, 0xAB, 0xCB, 0x19, 0x03,
  25251. 0x3B, 0x7A, 0x65, 0x88, 0x97, 0xC6, 0x58, 0x74,
  25252. 0xD5, 0x13, 0x51, 0x83, 0x14, 0x9F, 0x97, 0x9E,
  25253. 0x55, 0x3C, 0xCF, 0xBF, 0xA3, 0x90, 0x0C, 0xDA,
  25254. 0x6F, 0x01, 0x96, 0x0B, 0x75, 0x15, 0x7F, 0x54,
  25255. 0x53, 0xAA, 0x6E, 0x73, 0xB3, 0xED, 0x90, 0x2F,
  25256. 0x7D, 0x7C, 0x93, 0x05, 0x97, 0x1B, 0xDF, 0x72,
  25257. 0x2E, 0x29, 0x37, 0x16, 0x9A, 0x1B, 0xC0, 0xFA,
  25258. 0xEB, 0x6C, 0x92, 0xF7, 0x15, 0x0D, 0x23, 0x30,
  25259. 0x87, 0x7C, 0x5D, 0xC5, 0x24, 0x9A, 0xAE, 0x20,
  25260. 0x30, 0x26, 0x34, 0xC5, 0xC5, 0xB2, 0x30, 0x53,
  25261. 0x52, 0x10, 0x28, 0x12, 0x25, 0x42, 0xF4, 0x85,
  25262. 0xA0, 0xEA, 0xC8, 0x69, 0x22, 0x37, 0x20, 0x63,
  25263. 0x36, 0x51, 0xF5, 0xB2, 0x47, 0xC6, 0x62, 0xB3,
  25264. 0x1A, 0x10, 0x53, 0x8C, 0xA7, 0x49, 0x1B, 0x14,
  25265. 0x37, 0xAA, 0x74, 0xF4, 0x28, 0x2D, 0x12, 0x97,
  25266. 0x4D, 0x9C, 0x93, 0x4D, 0xF2, 0x14, 0x78, 0x5B,
  25267. 0x64, 0x18, 0x46, 0x8B, 0x92, 0xE5, 0x25, 0x28,
  25268. 0xC8, 0x44, 0x7A, 0x1C, 0xA4, 0x22, 0xFA, 0x6C,
  25269. 0xC8, 0x8E, 0x28, 0xB0, 0x59, 0xF0, 0x4B, 0x23,
  25270. 0x59, 0x73, 0x23, 0xF7, 0x2F, 0x3E, 0x23, 0x36,
  25271. 0xF8, 0x7C, 0x47, 0x90, 0x5C, 0xBA, 0x65, 0x5B,
  25272. 0xB7, 0x3F, 0xC3, 0x2E, 0x18, 0xD4, 0xB7, 0x87,
  25273. 0x05, 0xC7, 0x82, 0xEB, 0xCB, 0x43, 0xE2, 0x78,
  25274. 0x5C, 0x82, 0xC5, 0xAF, 0x24, 0xB0, 0xE1, 0x69,
  25275. 0x9C, 0xFB, 0xC0, 0x25, 0x74, 0x75, 0x79, 0x9A,
  25276. 0x53, 0x9B, 0x11, 0xA5, 0x0F, 0x4D, 0xF2, 0xB7,
  25277. 0xFA, 0xA2, 0x0B, 0xD8, 0x82, 0x75, 0x15, 0xCA,
  25278. 0x37, 0x0F, 0x89, 0xC0, 0xD4, 0xC6, 0x09, 0x02,
  25279. 0xF6, 0x56, 0x7C, 0xD6, 0x0B, 0x08, 0x60, 0xA5,
  25280. 0x5B, 0xC8, 0x57, 0x2C, 0x43, 0x6C, 0x24, 0x6A,
  25281. 0xC2, 0x76, 0x64, 0x4E, 0x7D, 0x60, 0x2A, 0xA5,
  25282. 0x7C, 0x01, 0x66, 0x20, 0x18, 0x14, 0x99, 0x1C,
  25283. 0x1B, 0xD7, 0x5C, 0x7C, 0x47, 0xC3, 0x48, 0xB6,
  25284. 0x7D, 0x77, 0x61, 0x33, 0x86, 0x90, 0x81, 0x44,
  25285. 0xEA, 0x83, 0xFF, 0x72, 0x1F, 0x9A, 0x50, 0x07,
  25286. 0x6C, 0x51, 0x01, 0x64, 0xD1, 0x8E, 0x05, 0xD0,
  25287. 0x5D, 0x98, 0x84, 0xC4, 0x41, 0x46, 0xA0, 0x7C,
  25288. 0xCA, 0xCF, 0x89, 0x04, 0x98, 0xED, 0x1A, 0x19,
  25289. 0xB2, 0xA1, 0x54, 0x31, 0x72, 0x9D, 0xC1, 0xF1,
  25290. 0x2B, 0x7E, 0xA1, 0x0F, 0x9F, 0x92, 0x80, 0x62,
  25291. 0xD1, 0x45, 0x4B, 0x4B, 0x9F, 0x68, 0xE5, 0x99,
  25292. 0x90, 0x29, 0x0B, 0xE3, 0x72, 0x8B, 0x32, 0x89,
  25293. 0x56, 0x93, 0x63, 0xAB, 0x10, 0x05, 0x13, 0x1B,
  25294. 0x23, 0x81, 0xA0, 0x8C, 0xC2, 0xBF, 0x94, 0x3E,
  25295. 0x95, 0xD5, 0xB2, 0x1B, 0xC6, 0xAA, 0xBC, 0x22,
  25296. 0x73, 0x34, 0x8B, 0xC7, 0x2B, 0xD0, 0x93, 0xB7,
  25297. 0xB5, 0x61, 0x7A, 0xE8, 0x7F, 0x60, 0x2B, 0xB9,
  25298. 0x89, 0xE6, 0xAF, 0xC4, 0x4B, 0x81, 0x51, 0x20,
  25299. 0x76, 0xA3, 0xA8, 0x76, 0xE0, 0xE2, 0x5F, 0x97,
  25300. 0x62, 0xB4, 0x62, 0x08, 0x19, 0x85, 0x50, 0x2F,
  25301. 0x26, 0xB2, 0x87, 0xA2, 0x93, 0x6D, 0x5B, 0x1A,
  25302. 0xCF, 0xFC, 0xEC, 0x4E, 0xEE, 0x77, 0xA9, 0xCB,
  25303. 0xA9, 0x80, 0xEB, 0x9B, 0x5F, 0xDE, 0x75, 0x53,
  25304. 0x9F, 0x65, 0x09, 0x04, 0x67, 0x7D, 0xBE, 0x29,
  25305. 0xAB, 0x8B, 0xB9, 0x18, 0xA3, 0x49, 0x48, 0x03,
  25306. 0xEC, 0xA5, 0x9A, 0x2C, 0x32, 0xE5, 0xB5, 0xC8,
  25307. 0x3B, 0x0B, 0x80, 0xB1, 0x10, 0x2C, 0xD7, 0xD9,
  25308. 0x48, 0x2B, 0x45, 0x9B, 0x6B, 0x74, 0x49, 0x1E,
  25309. 0xC3, 0x0C, 0x4B, 0xE7, 0x7C, 0x2B, 0x52, 0x4A,
  25310. 0xF7, 0xB3, 0xAD, 0x1F, 0x71, 0x34, 0x1D, 0xF0,
  25311. 0xA7, 0x6F, 0x25, 0x5C, 0x29, 0x03, 0xC8, 0x82,
  25312. 0x08, 0x07, 0x93, 0x79, 0x93, 0x0A, 0x95, 0x13,
  25313. 0xF3, 0x90, 0x12, 0x6E, 0x73, 0x2A, 0x2B, 0xB0,
  25314. 0x94, 0xBF, 0xA6, 0xBF, 0x0A, 0x43, 0x2B, 0xCD,
  25315. 0x65, 0x7D, 0xAF, 0xCB, 0x25, 0xC8, 0xBB, 0x15,
  25316. 0xE0, 0x95, 0x5D, 0x09, 0x9B, 0x74, 0xFF, 0x1A,
  25317. 0x4D, 0xE6, 0x55, 0x9C, 0xD6, 0x79, 0x7C, 0x38,
  25318. 0xC4, 0x8C, 0x11, 0x34, 0xCA, 0x2C, 0x97, 0x92,
  25319. 0x43, 0xF3, 0x15, 0x2A, 0xF4, 0xBB, 0xE4, 0xD7,
  25320. 0xA6, 0xBC, 0x09, 0x87, 0x21, 0x33, 0x92, 0x0C,
  25321. 0xD2, 0x3B, 0x3E, 0xF9, 0x84, 0x8C, 0xCC, 0x68,
  25322. 0x45, 0xD6, 0x47, 0xB5, 0x38, 0x75, 0x57, 0x73,
  25323. 0x65, 0x13, 0xD5, 0x85, 0x60, 0x84, 0x51, 0x92,
  25324. 0xF9, 0x26, 0x51, 0x59, 0x93, 0x2E, 0x57, 0x2A,
  25325. 0x88, 0xC4, 0x4E, 0x65, 0x66, 0x76, 0x0C, 0x06,
  25326. 0x1C, 0x67, 0xFC, 0xB5, 0xBF, 0x21, 0x00, 0x95,
  25327. 0xE2, 0x14, 0xDA, 0x74, 0x53, 0x57, 0xE3, 0x69,
  25328. 0x96, 0xD8, 0xC0, 0x66, 0x31, 0x1B, 0xBC, 0x76,
  25329. 0x1A, 0x1F, 0xD2, 0x52, 0x73, 0xD2, 0x1E, 0xAB,
  25330. 0x50, 0x01, 0x05, 0x63, 0xCD, 0x64, 0x68, 0xA4,
  25331. 0xEA, 0x83, 0x6B, 0x6D, 0x64, 0xBD, 0x2B, 0xD7,
  25332. 0x6D, 0xBE, 0x35, 0x82, 0xD5, 0x73, 0x6A, 0x60,
  25333. 0x5A, 0x55, 0x09, 0xFC, 0x28, 0x78, 0x9B, 0x56,
  25334. 0xB8, 0x84, 0xAE, 0x9A, 0x60, 0x41, 0x5F, 0x55,
  25335. 0x67, 0x4B, 0xE6, 0x01, 0x57, 0x6C, 0x7C, 0xEE,
  25336. 0x58, 0x14, 0x3B, 0xF0, 0x54, 0x80, 0x6A, 0xBC,
  25337. 0xB3, 0x45, 0xA2, 0x56, 0xCB, 0xC4, 0x54, 0xE3,
  25338. 0x43, 0xF3, 0xCC, 0x7A, 0xDE, 0x65, 0x56, 0x2F,
  25339. 0xD2, 0x9E, 0xB2, 0x59, 0x73, 0x7B, 0xB3, 0xCF,
  25340. 0x96, 0x49, 0xBD, 0xEA, 0x28, 0x3F, 0xB0, 0x72,
  25341. 0x65, 0x67, 0x7C, 0x98, 0x08, 0xD1, 0x31, 0x19,
  25342. 0xC0, 0xA2, 0xAD, 0xF7, 0x45, 0xDE, 0x69, 0x75,
  25343. 0xF4, 0x56, 0x2C, 0xD6, 0x15, 0x57, 0xB3, 0x96,
  25344. 0x5D, 0x2B, 0x07, 0x2F, 0x00, 0x0A, 0xA7, 0xE0,
  25345. 0xA3, 0x57, 0xE1, 0x25, 0x3E, 0xAF, 0xEA, 0x7F,
  25346. 0xDF, 0xCC, 0x92, 0xFA, 0x87, 0x63, 0x0D, 0xD2,
  25347. 0x27, 0x6C, 0xE4, 0x2E, 0x82, 0x0B, 0x69, 0xD1,
  25348. 0xFC, 0x2E, 0x47, 0xD5, 0xC4, 0x98, 0xA5, 0x5B,
  25349. 0x3B, 0x29, 0xC3, 0x4E, 0x64, 0x90, 0x3D, 0x04,
  25350. 0x7A, 0xB1, 0xC0, 0x40, 0x24, 0x95, 0x8F, 0x70,
  25351. 0x11, 0x95, 0xF5, 0xD1, 0x3E, 0xC6, 0x70, 0x6B,
  25352. 0x84, 0x48, 0x50, 0x3A, 0x54, 0x99, 0x22, 0xA5,
  25353. 0x8A, 0x24, 0xB6, 0x7C, 0x93, 0x63, 0x27, 0x56,
  25354. 0xB7, 0x7D, 0x22, 0x54, 0x07, 0x31, 0x61, 0x71,
  25355. 0xDE, 0xEC, 0x56, 0x71, 0x44, 0x35, 0xCF, 0x94,
  25356. 0xCC, 0xF4, 0x59, 0x9E, 0x00, 0xD1, 0x0E, 0x56,
  25357. 0x96, 0x22, 0xBA, 0xDA, 0x82, 0x0C, 0x45, 0x2F,
  25358. 0x25, 0x42, 0xAD, 0xF0, 0x87, 0x65, 0xCA, 0x93,
  25359. 0xAE, 0x38, 0xEB, 0x02, 0x5D, 0xE3, 0x1C, 0xFF,
  25360. 0x79, 0x74, 0x54, 0x9A, 0x78, 0x25, 0xA8, 0x31,
  25361. 0xDD, 0x05, 0x4E, 0x87, 0xB8, 0x4C, 0x5F, 0x25,
  25362. 0x47, 0xFF, 0x47, 0xB4, 0x6F, 0x88, 0xC9, 0x9F,
  25363. 0x15, 0x48, 0xE9, 0x33, 0xA6, 0xF4, 0xD8, 0x7F,
  25364. 0x1A, 0x4A, 0x1B, 0x00, 0xE3, 0x9E, 0x02, 0xD6,
  25365. 0x0E, 0x51, 0xEB, 0x60, 0x3C, 0x1C, 0x0D, 0x80,
  25366. 0x7A, 0xCD, 0xAB, 0x08, 0xBA, 0xA2, 0xB9, 0x98,
  25367. 0x69, 0xB7, 0x5C, 0xA2, 0xC4, 0xB9, 0x63, 0x68,
  25368. 0xB5, 0x17, 0x80, 0xBD, 0x1E, 0xC7, 0x5B, 0x11,
  25369. 0x0B, 0x9F, 0xA6, 0x65, 0x56, 0x87, 0x6C, 0x5F,
  25370. 0x48, 0x79, 0x7D, 0x09, 0x01, 0x38, 0xF7, 0x54,
  25371. 0xAE, 0x30, 0x53, 0x3D, 0x36, 0xAA, 0x44, 0xB9,
  25372. 0xB1, 0x70, 0x2A, 0x6A, 0x8A, 0x56, 0x62, 0x6B,
  25373. 0xF0, 0x45, 0x1A, 0x37, 0xA7, 0xAC, 0x1A, 0x33,
  25374. 0x70, 0x76, 0xE5, 0x1E, 0x0A, 0x6B, 0x03, 0x00,
  25375. 0xC2, 0xC7, 0x90, 0xA4, 0x43, 0x7E, 0xA2, 0x8D,
  25376. 0x7E, 0xC9, 0x8C, 0x41, 0x9B, 0x37, 0xD6, 0xAA,
  25377. 0x97, 0x04, 0x17, 0x43, 0x5F, 0x91, 0xBE, 0xDC,
  25378. 0x2B, 0x1F, 0x4B, 0xC8, 0x15, 0x8A, 0x51, 0xB1,
  25379. 0xF4, 0x71, 0x51, 0x6F, 0xE8, 0x24, 0x28, 0x7C,
  25380. 0x89, 0x6B, 0x89, 0x1B, 0x49, 0xF2, 0x54, 0xDD,
  25381. 0x36, 0x35, 0x9B, 0x89, 0xC8, 0x24, 0xEB, 0x3F,
  25382. 0x62, 0x48, 0x02, 0x7F, 0xBB, 0xAD, 0x4C, 0xF2,
  25383. 0x91, 0x18, 0xCB, 0x50, 0xEB, 0xB6, 0x25, 0xA3,
  25384. 0x7C, 0x53, 0x7A, 0x02, 0x23, 0xF0, 0xEB, 0x70,
  25385. 0x85, 0xB5, 0xC7, 0xEC, 0x60, 0x75, 0x70, 0xDB,
  25386. 0x91, 0x85, 0xD5, 0x99, 0x02, 0xBC, 0x26, 0xC6,
  25387. 0x54, 0xA2, 0x80, 0x4C, 0x0D, 0x94, 0x67, 0x93,
  25388. 0xD8, 0xA2, 0x14, 0x82, 0xAC, 0x4F, 0x05, 0xE9,
  25389. 0x01, 0x62, 0x60, 0x33, 0x1D, 0xCC, 0x58, 0xBC,
  25390. 0x66, 0xAF, 0x3C, 0xA7, 0x58, 0x54, 0x40, 0x21,
  25391. 0x6A, 0xA0, 0x26, 0x3B, 0x2A, 0x72, 0x5E, 0x08,
  25392. 0x0F, 0x6F, 0x9C, 0x5B, 0x6A, 0x9C, 0x9D, 0xA2,
  25393. 0x93, 0x55, 0x18, 0x9B, 0x4B, 0x95, 0xB1, 0x37,
  25394. 0xD1, 0x22, 0x5F, 0x25, 0x2A, 0xC7, 0x97, 0xB0,
  25395. 0x64, 0x6C, 0xAC, 0x52, 0x16, 0x4B, 0x59, 0x72,
  25396. 0xA9, 0x92, 0x65, 0xD3, 0x47, 0xFC, 0x7C, 0x35,
  25397. 0x91, 0xD1, 0x5F, 0xFE, 0x68, 0x1C, 0x06, 0xD4,
  25398. 0x38, 0xCC, 0xEB, 0x60, 0xBB, 0x63, 0x10, 0xB7,
  25399. 0x95, 0x32, 0x89, 0x72, 0x0E, 0x2C, 0x72, 0x87,
  25400. 0x30, 0x05, 0x23, 0x37, 0xAC, 0xA7, 0xC8, 0x52,
  25401. 0x1A, 0xB4, 0x4F, 0x1E, 0x2A, 0x04, 0x9B, 0x83,
  25402. 0xE0, 0x77, 0x4C, 0x96, 0xCD, 0x8C, 0x87, 0x6F,
  25403. 0xA6, 0x75, 0xD0, 0x92, 0x39, 0x77, 0x27, 0x1B,
  25404. 0xE6, 0xE8, 0x32, 0xF2, 0x49, 0x8C, 0xA5, 0xA3,
  25405. 0x43, 0x1F, 0x40, 0xD3, 0x18, 0x7B, 0x1E, 0xD9,
  25406. 0x65, 0xFD, 0xD6, 0x69, 0x3B, 0x37, 0xF6, 0xEB,
  25407. 0x40, 0x8A, 0x99, 0x97, 0x7A, 0xE4, 0x96, 0x44,
  25408. 0x7A, 0xF6, 0x50, 0x22, 0xE0, 0xA4, 0x72, 0xED,
  25409. 0x63, 0x88, 0x63, 0x8E, 0xA2, 0x9D, 0x82, 0xDA,
  25410. 0x68, 0xB4, 0xCF, 0x9F, 0xFD, 0xF2, 0xB6, 0x7C,
  25411. 0xD7, 0x08, 0xEA, 0x5A, 0x37, 0x0C, 0x6A, 0x7C
  25412. };
  25413. #endif
  25414. static byte pubKey[KYBER_MAX_PUBLIC_KEY_SIZE];
  25415. static byte privKey[KYBER_MAX_PRIVATE_KEY_SIZE];
  25416. key = (KyberKey*)XMALLOC(sizeof(KyberKey), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25417. ExpectNotNull(key);
  25418. if (key != NULL) {
  25419. XMEMSET(key, 0, sizeof(KyberKey));
  25420. }
  25421. #ifndef WOLFSSL_NO_KYBER512
  25422. ExpectIntEQ(wc_KyberKey_Init(KYBER512, key, NULL, INVALID_DEVID), 0);
  25423. ExpectIntEQ(wc_KyberKey_MakeKeyWithRandom(key, seed_512, sizeof(seed_512)),
  25424. 0);
  25425. ExpectIntEQ(wc_KyberKey_EncodePublicKey(key, pubKey,
  25426. KYBER512_PUBLIC_KEY_SIZE), 0);
  25427. ExpectIntEQ(wc_KyberKey_EncodePrivateKey(key, privKey,
  25428. KYBER512_PRIVATE_KEY_SIZE), 0);
  25429. ExpectIntEQ(XMEMCMP(pubKey, ek_512, KYBER512_PUBLIC_KEY_SIZE), 0);
  25430. ExpectIntEQ(XMEMCMP(privKey, dk_512, KYBER512_PRIVATE_KEY_SIZE), 0);
  25431. wc_KyberKey_Free(key);
  25432. #endif
  25433. #ifndef WOLFSSL_NO_KYBER768
  25434. ExpectIntEQ(wc_KyberKey_Init(KYBER768, key, NULL, INVALID_DEVID), 0);
  25435. ExpectIntEQ(wc_KyberKey_MakeKeyWithRandom(key, seed_768, sizeof(seed_768)),
  25436. 0);
  25437. ExpectIntEQ(wc_KyberKey_EncodePublicKey(key, pubKey,
  25438. KYBER768_PUBLIC_KEY_SIZE), 0);
  25439. ExpectIntEQ(wc_KyberKey_EncodePrivateKey(key, privKey,
  25440. KYBER768_PRIVATE_KEY_SIZE), 0);
  25441. ExpectIntEQ(XMEMCMP(pubKey, ek_768, KYBER768_PUBLIC_KEY_SIZE), 0);
  25442. ExpectIntEQ(XMEMCMP(privKey, dk_768, KYBER768_PRIVATE_KEY_SIZE), 0);
  25443. wc_KyberKey_Free(key);
  25444. #endif
  25445. #ifndef WOLFSSL_NO_KYBER1024
  25446. ExpectIntEQ(wc_KyberKey_Init(KYBER1024, key, NULL, INVALID_DEVID), 0);
  25447. ExpectIntEQ(wc_KyberKey_MakeKeyWithRandom(key, seed_1024,
  25448. sizeof(seed_1024)), 0);
  25449. ExpectIntEQ(wc_KyberKey_EncodePublicKey(key, pubKey,
  25450. KYBER1024_PUBLIC_KEY_SIZE), 0);
  25451. ExpectIntEQ(wc_KyberKey_EncodePrivateKey(key, privKey,
  25452. KYBER1024_PRIVATE_KEY_SIZE), 0);
  25453. ExpectIntEQ(XMEMCMP(pubKey, ek_1024, KYBER1024_PUBLIC_KEY_SIZE), 0);
  25454. ExpectIntEQ(XMEMCMP(privKey, dk_1024, KYBER1024_PRIVATE_KEY_SIZE), 0);
  25455. wc_KyberKey_Free(key);
  25456. #endif
  25457. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25458. #endif
  25459. return EXPECT_RESULT();
  25460. }
  25461. static int test_wc_kyber_encapsulate_kats(void)
  25462. {
  25463. EXPECT_DECLS;
  25464. #if defined(WOLFSSL_HAVE_KYBER) && defined(WOLFSSL_WC_KYBER) && \
  25465. defined(WOLFSSL_ML_KEM)
  25466. KyberKey* key;
  25467. #ifndef WOLFSSL_NO_KYBER512
  25468. static const byte ek_512[KYBER512_PUBLIC_KEY_SIZE] = {
  25469. 0xA5, 0x40, 0x97, 0x18, 0xCB, 0x72, 0xF2, 0x43,
  25470. 0x8A, 0x35, 0x55, 0xA3, 0xC8, 0xF1, 0x8F, 0x26,
  25471. 0x71, 0xA1, 0xF8, 0x14, 0x03, 0xDF, 0x7B, 0x5A,
  25472. 0x46, 0x59, 0xA5, 0x1F, 0x50, 0x82, 0x7B, 0xA6,
  25473. 0x57, 0x7A, 0xA7, 0x08, 0x00, 0xD7, 0x8D, 0x8B,
  25474. 0xC5, 0xAA, 0x86, 0xB8, 0x9E, 0x08, 0xB5, 0x8F,
  25475. 0x34, 0x80, 0xA8, 0x9E, 0x10, 0x4D, 0xC6, 0x92,
  25476. 0x2E, 0xDB, 0xC1, 0x2D, 0x06, 0xF8, 0x91, 0x02,
  25477. 0x7C, 0x65, 0x4E, 0x99, 0x4A, 0x22, 0xF9, 0x1A,
  25478. 0x2A, 0xF6, 0x34, 0x04, 0xCA, 0x98, 0xD7, 0xB6,
  25479. 0x7E, 0xEA, 0x25, 0x91, 0x1B, 0x24, 0xC7, 0x0D,
  25480. 0xEB, 0x81, 0x46, 0xA0, 0x82, 0x1F, 0x34, 0xA3,
  25481. 0x02, 0x55, 0x1F, 0x2D, 0x51, 0x0C, 0x05, 0x88,
  25482. 0xC8, 0xBC, 0xA7, 0x4E, 0xB4, 0xDC, 0x0C, 0xFA,
  25483. 0x46, 0x03, 0xC1, 0xC5, 0xA3, 0xC5, 0x53, 0x70,
  25484. 0x61, 0x78, 0x90, 0x68, 0x68, 0x2C, 0x4C, 0xC3,
  25485. 0x14, 0x3F, 0xBA, 0x9B, 0xB5, 0x54, 0x2F, 0x97,
  25486. 0x78, 0xBD, 0xF2, 0x3B, 0x36, 0x52, 0xF2, 0xA7,
  25487. 0x52, 0x47, 0x56, 0xFA, 0x73, 0x90, 0x9D, 0xDA,
  25488. 0xC7, 0xE5, 0x32, 0x52, 0x26, 0x59, 0x21, 0x8C,
  25489. 0xBA, 0x25, 0xF3, 0x3B, 0x6B, 0x04, 0x58, 0xCB,
  25490. 0x03, 0xDA, 0x79, 0x35, 0xBA, 0x59, 0x11, 0x19,
  25491. 0x55, 0x31, 0x2B, 0x15, 0xCC, 0xE2, 0xC0, 0xF7,
  25492. 0x34, 0x66, 0xA8, 0x00, 0x62, 0x83, 0xA2, 0xAA,
  25493. 0x7C, 0xBB, 0x61, 0x02, 0x2A, 0xBB, 0xC2, 0xD1,
  25494. 0x9F, 0x29, 0x20, 0xBC, 0x30, 0x24, 0x72, 0xDC,
  25495. 0x97, 0xC4, 0xA1, 0x78, 0x8C, 0x9B, 0xD3, 0xBB,
  25496. 0xED, 0xC9, 0x12, 0x2B, 0x82, 0x7B, 0x27, 0x9C,
  25497. 0x07, 0x4C, 0x80, 0x44, 0x31, 0x41, 0x11, 0x9F,
  25498. 0x4B, 0x16, 0x29, 0xF6, 0x2F, 0x10, 0xD4, 0xCE,
  25499. 0x2B, 0xE3, 0xBB, 0x34, 0x38, 0x16, 0xCA, 0xD1,
  25500. 0x6A, 0x1C, 0x87, 0x58, 0x2F, 0x2B, 0x70, 0xE2,
  25501. 0x66, 0x35, 0xB0, 0x8B, 0xB3, 0x90, 0xC1, 0x33,
  25502. 0x98, 0xFC, 0xCD, 0xA7, 0xE9, 0xBB, 0x3D, 0x9B,
  25503. 0x0B, 0x78, 0x03, 0x75, 0x0C, 0x95, 0x5C, 0x57,
  25504. 0xA0, 0x28, 0xA5, 0xD2, 0x6C, 0x27, 0x03, 0x16,
  25505. 0xBB, 0x2B, 0x81, 0x5C, 0x3B, 0x97, 0x2B, 0xA6,
  25506. 0x78, 0x2D, 0xAB, 0x02, 0xF3, 0x06, 0x82, 0x1E,
  25507. 0x61, 0x28, 0x5B, 0xB0, 0x72, 0xBF, 0x79, 0x78,
  25508. 0x1C, 0xAB, 0xC3, 0x86, 0x14, 0x2A, 0x50, 0xC7,
  25509. 0xAA, 0xAE, 0x66, 0xA9, 0x47, 0x58, 0x5B, 0xB0,
  25510. 0xD8, 0x28, 0x8D, 0xBC, 0xAF, 0x4B, 0x3B, 0x85,
  25511. 0xBB, 0x79, 0x26, 0x98, 0x7B, 0xAF, 0x76, 0x43,
  25512. 0xAA, 0xB5, 0xFB, 0x02, 0x21, 0x05, 0x80, 0xA0,
  25513. 0x26, 0x43, 0x52, 0xE6, 0x9C, 0x60, 0x98, 0x98,
  25514. 0x9C, 0xFB, 0x87, 0x48, 0x33, 0x95, 0x96, 0x0A,
  25515. 0x3A, 0x4F, 0x31, 0xBE, 0xFD, 0xA8, 0x0B, 0x5F,
  25516. 0x28, 0x6E, 0xCF, 0xDA, 0xA5, 0x55, 0xD4, 0x39,
  25517. 0x0A, 0xF6, 0xB5, 0x5D, 0x31, 0x39, 0x20, 0x92,
  25518. 0x90, 0x93, 0x44, 0x9C, 0xD6, 0x72, 0x9D, 0x00,
  25519. 0x21, 0x8E, 0x2D, 0x86, 0x57, 0x0A, 0xDC, 0x0C,
  25520. 0x4F, 0x65, 0x45, 0xFF, 0xB5, 0x63, 0x2E, 0xFB,
  25521. 0x3A, 0xAE, 0x26, 0x25, 0xA6, 0x98, 0x26, 0x70,
  25522. 0xFA, 0xCE, 0x8D, 0x16, 0x12, 0x6F, 0xA6, 0x07,
  25523. 0xE6, 0xD0, 0xA1, 0xFF, 0x61, 0x6A, 0x46, 0xEC,
  25524. 0xA6, 0x42, 0xCC, 0x6A, 0xAC, 0x55, 0x4D, 0xBB,
  25525. 0xC4, 0x3D, 0xFC, 0xF5, 0x7F, 0x36, 0x4C, 0x19,
  25526. 0x0C, 0xEA, 0x57, 0x76, 0xC1, 0xCE, 0xB5, 0x8B,
  25527. 0x70, 0x07, 0x50, 0x5F, 0xD7, 0x9C, 0x5F, 0x00,
  25528. 0x5A, 0x4B, 0xA2, 0x18, 0xCF, 0x06, 0x93, 0xB0,
  25529. 0x58, 0xB5, 0x10, 0xA4, 0xCA, 0x20, 0x43, 0x24,
  25530. 0x60, 0x2F, 0x59, 0xBB, 0x8F, 0x22, 0x81, 0xC4,
  25531. 0xD7, 0xB0, 0xBC, 0x86, 0x25, 0xE7, 0x88, 0x16,
  25532. 0x50, 0xF5, 0x7C, 0x89, 0xE3, 0x2C, 0xF4, 0x80,
  25533. 0x91, 0x44, 0x77, 0x5C, 0x90, 0x73, 0xB6, 0x73,
  25534. 0xE3, 0x94, 0x12, 0xA2, 0x7C, 0x91, 0x43, 0x21,
  25535. 0xCC, 0xB6, 0xA7, 0xCF, 0x7C, 0x37, 0xC5, 0xBC,
  25536. 0xBE, 0x7C, 0xA5, 0x1B, 0xE0, 0xC9, 0x28, 0x46,
  25537. 0x6A, 0x45, 0x8E, 0xB7, 0x78, 0xD6, 0x46, 0x6A,
  25538. 0x89, 0x2A, 0x0A, 0xCB, 0xC0, 0x96, 0x38, 0x78,
  25539. 0x4A, 0x27, 0x73, 0x9C, 0x97, 0x0C, 0xA5, 0x8B,
  25540. 0xC2, 0x59, 0x5A, 0xD6, 0xBF, 0xA4, 0xE5, 0x2E,
  25541. 0xB4, 0x38, 0xAC, 0x97, 0xC4, 0x16, 0x23, 0x80,
  25542. 0x22, 0x48, 0xE1, 0x10, 0xB0, 0x74, 0x83, 0x8F,
  25543. 0x31, 0xA6, 0xE7, 0x50, 0x37, 0x37, 0x70, 0x4E,
  25544. 0x7A, 0xE4, 0xAD, 0x91, 0x29, 0x95, 0x72, 0xA8,
  25545. 0xC1, 0x36, 0x03, 0x50, 0x0F, 0x36, 0x09, 0xB6,
  25546. 0x25, 0xB4, 0xE2, 0x4C, 0xAE, 0x33, 0x2B, 0x0D,
  25547. 0x7A, 0x5B, 0xB4, 0x7A, 0x03, 0x85, 0x12, 0xA0,
  25548. 0x81, 0xBC, 0x27, 0xCD, 0xF0, 0xF2, 0x92, 0x3C,
  25549. 0xD3, 0x47, 0x9F, 0x53, 0x07, 0x02, 0x0B, 0x77,
  25550. 0xF1, 0x49, 0x58, 0x45, 0x64, 0x06, 0x0E, 0x50,
  25551. 0x83, 0xCE, 0xD5, 0x53, 0x12, 0xB6, 0xA6, 0xA4,
  25552. 0x65, 0xA8, 0x2B, 0x45, 0x77, 0xD6, 0x3A, 0x4B,
  25553. 0x49, 0xC8, 0x0B, 0x07, 0xA9, 0x36, 0x7E, 0x39,
  25554. 0x77, 0x8A, 0xF7, 0x6F, 0xA8, 0xEC, 0x2C, 0xF5,
  25555. 0x28, 0x72, 0x28, 0x56, 0xCE, 0x78, 0x13, 0x40,
  25556. 0x1A, 0x83, 0x83, 0xBD, 0xB7, 0x15, 0x1B, 0x9B,
  25557. 0x6D, 0x2D, 0xD6, 0xBF, 0xF5, 0x54, 0x01, 0xD2,
  25558. 0x8A, 0xC6, 0x12, 0x81, 0x8C, 0x88, 0xC9, 0x28,
  25559. 0x73, 0x47, 0xB0, 0x98, 0xA9, 0x66, 0xEB, 0x9C,
  25560. 0x0A, 0x2D, 0xB7, 0x1F, 0x0A, 0x75, 0x55, 0x5E,
  25561. 0x17, 0x57, 0xD3, 0xAC, 0x4E, 0x3D, 0x80, 0x2C,
  25562. 0x8D, 0xC6, 0xA2, 0x61, 0x52, 0x12, 0x55, 0x18,
  25563. 0x6A, 0xBB, 0x98, 0xC2, 0x48, 0x03, 0x01, 0xB8,
  25564. 0xC6, 0xB3, 0x12, 0x28, 0xB5, 0x44, 0x61, 0xBC,
  25565. 0x44, 0xEA, 0x3C, 0x2C, 0xF9, 0x4B, 0x86, 0xC7,
  25566. 0xA5, 0xB8, 0x2C, 0x55, 0x16, 0x7A, 0x76, 0x06,
  25567. 0xCA, 0x9D, 0xC8, 0x25, 0x3B, 0x76, 0x04, 0xE4,
  25568. 0x4A, 0x07, 0xF3, 0xED, 0x55, 0xCD, 0x5B, 0x5E
  25569. };
  25570. static const byte seed_512[KYBER_ENC_RAND_SZ] = {
  25571. 0x10, 0x9A, 0x24, 0x8F, 0xE8, 0x05, 0x2F, 0x84,
  25572. 0x27, 0x1F, 0xF5, 0x7B, 0xAC, 0x15, 0x6B, 0x1B,
  25573. 0xA6, 0xA5, 0x09, 0xCD, 0xCD, 0xBC, 0xC9, 0x6C,
  25574. 0xCD, 0xB1, 0xCC, 0xB8, 0x5C, 0xA4, 0x93, 0x15
  25575. };
  25576. static const byte c_512[KYBER512_CIPHER_TEXT_SIZE] = {
  25577. 0x59, 0x7A, 0x06, 0xDE, 0xB8, 0x81, 0x72, 0xBA,
  25578. 0x8D, 0x7C, 0xDE, 0x8D, 0x82, 0xCA, 0xA2, 0x34,
  25579. 0xB8, 0x11, 0x2A, 0xF8, 0xA7, 0x2F, 0x1A, 0xB4,
  25580. 0xCE, 0xA1, 0xEF, 0xCB, 0x2D, 0x86, 0x8D, 0x53,
  25581. 0xD2, 0x12, 0xE3, 0x03, 0xB7, 0x0E, 0x7E, 0x52,
  25582. 0x1A, 0xB0, 0xF4, 0xB5, 0xDB, 0x4F, 0x51, 0x15,
  25583. 0x92, 0x48, 0xBF, 0xB2, 0x75, 0x36, 0x1B, 0xEF,
  25584. 0x88, 0x37, 0x52, 0xC7, 0x8B, 0x8D, 0x47, 0x12,
  25585. 0x27, 0x53, 0x85, 0x53, 0x6A, 0x4B, 0x0A, 0x96,
  25586. 0xE3, 0xC2, 0x3E, 0xA6, 0xC1, 0x7E, 0xA9, 0x2B,
  25587. 0x60, 0x26, 0x16, 0xE5, 0x82, 0x1E, 0x57, 0x53,
  25588. 0xA4, 0x73, 0x6C, 0x40, 0x39, 0xC2, 0x0C, 0x92,
  25589. 0x3C, 0xCE, 0xCB, 0x57, 0x98, 0x05, 0x58, 0x7C,
  25590. 0x0C, 0xE7, 0x22, 0x18, 0xBB, 0x1A, 0xB1, 0x24,
  25591. 0x52, 0xF8, 0xE1, 0x54, 0xCB, 0x86, 0x43, 0x32,
  25592. 0x81, 0x42, 0xF9, 0xB3, 0x40, 0xA6, 0x41, 0xC6,
  25593. 0xF2, 0x95, 0xE5, 0xEC, 0xF2, 0xE0, 0x48, 0xBC,
  25594. 0x7F, 0xC7, 0x9B, 0xC5, 0xB9, 0x42, 0x77, 0xC8,
  25595. 0x68, 0xD8, 0xE5, 0x36, 0xB5, 0x04, 0x25, 0x80,
  25596. 0x9D, 0xCF, 0xA0, 0x24, 0xA3, 0x90, 0x5C, 0xBA,
  25597. 0x55, 0x0A, 0xD3, 0xBB, 0x52, 0xB4, 0x59, 0xAC,
  25598. 0x38, 0xFA, 0xBC, 0x9B, 0xC0, 0x0E, 0xBA, 0x03,
  25599. 0xEC, 0x09, 0x06, 0x72, 0x5B, 0x4F, 0xE4, 0xE9,
  25600. 0x76, 0xF1, 0x74, 0x32, 0x00, 0x47, 0xB3, 0x1D,
  25601. 0x15, 0x89, 0x13, 0x65, 0xBA, 0x48, 0x23, 0x88,
  25602. 0xF0, 0xFB, 0x97, 0x3B, 0x85, 0x22, 0x4F, 0xB0,
  25603. 0x0B, 0xA8, 0x65, 0xAF, 0xAB, 0x3C, 0x9A, 0x1B,
  25604. 0x7D, 0x48, 0x9F, 0x7B, 0x98, 0x2D, 0x0B, 0xD4,
  25605. 0x70, 0xEF, 0x94, 0x8E, 0xCB, 0x5B, 0x39, 0x20,
  25606. 0xAF, 0x89, 0x03, 0x59, 0x60, 0x12, 0x3B, 0x1F,
  25607. 0x86, 0x30, 0xD7, 0x63, 0x68, 0x1B, 0xFD, 0x67,
  25608. 0x15, 0x67, 0xEF, 0xBB, 0x1E, 0x62, 0x76, 0xAA,
  25609. 0x4F, 0xB2, 0xDF, 0xA9, 0xC3, 0x94, 0x8D, 0xB7,
  25610. 0xF0, 0x83, 0xF2, 0x83, 0x83, 0xB7, 0x7B, 0xC5,
  25611. 0x14, 0xAF, 0x9D, 0x68, 0xD2, 0x2E, 0x24, 0x87,
  25612. 0xC2, 0x01, 0x63, 0xC0, 0x2B, 0x0B, 0xBF, 0x23,
  25613. 0xBB, 0xCE, 0x06, 0x50, 0xF8, 0x4F, 0xF8, 0xCE,
  25614. 0x02, 0xC7, 0x4E, 0x9E, 0x11, 0xD6, 0xF3, 0x0E,
  25615. 0xC5, 0xFA, 0x8A, 0x01, 0x2A, 0xDC, 0x3B, 0x89,
  25616. 0x62, 0x7C, 0x7D, 0xE8, 0x55, 0xC1, 0xFB, 0xBE,
  25617. 0xB5, 0xDC, 0xDE, 0x84, 0xD0, 0x5E, 0x36, 0xC5,
  25618. 0x56, 0x6E, 0x55, 0x51, 0xB5, 0x87, 0x50, 0xA4,
  25619. 0x11, 0x64, 0x26, 0x39, 0xB2, 0x78, 0x64, 0xF7,
  25620. 0xE0, 0x05, 0x97, 0x8F, 0xFE, 0x25, 0x6B, 0x75,
  25621. 0x7D, 0x13, 0xDA, 0x66, 0x3F, 0xC3, 0xBB, 0x07,
  25622. 0x94, 0xA2, 0x7C, 0xF7, 0x58, 0x5D, 0x12, 0xF2,
  25623. 0x2D, 0x95, 0x3B, 0x28, 0x54, 0x59, 0xFD, 0xC9,
  25624. 0xBC, 0xDF, 0xCD, 0xCC, 0xB7, 0xBF, 0x3E, 0x4E,
  25625. 0x36, 0x2D, 0x28, 0x91, 0xD5, 0x83, 0x85, 0x5F,
  25626. 0x5D, 0x94, 0x87, 0xE6, 0xFB, 0x21, 0x7E, 0x2E,
  25627. 0x45, 0xEE, 0x0B, 0xD9, 0xAF, 0xC2, 0x89, 0xF4,
  25628. 0xD5, 0x64, 0x58, 0x12, 0x09, 0xA3, 0xAC, 0xA3,
  25629. 0x17, 0x95, 0xA1, 0x24, 0xBD, 0x1B, 0xBA, 0xEA,
  25630. 0x84, 0x67, 0x55, 0xC8, 0xEA, 0x78, 0x10, 0xEA,
  25631. 0xA7, 0x30, 0x60, 0xE8, 0x6F, 0xB5, 0xFD, 0xF3,
  25632. 0xFB, 0xE7, 0x2F, 0x80, 0x6B, 0xB1, 0xBF, 0xBF,
  25633. 0xBA, 0xC0, 0xC7, 0xB1, 0x6B, 0xFE, 0x74, 0x25,
  25634. 0x02, 0x77, 0xEC, 0xF5, 0xF5, 0x41, 0x57, 0x1B,
  25635. 0x8A, 0x97, 0x50, 0x50, 0x91, 0x7F, 0xDF, 0x78,
  25636. 0x1F, 0xEA, 0x17, 0xB5, 0x85, 0xE3, 0xC6, 0xDB,
  25637. 0xFE, 0x77, 0xB1, 0xE4, 0x8A, 0x16, 0x50, 0x4C,
  25638. 0x3A, 0x38, 0x90, 0x11, 0x56, 0x10, 0x0C, 0xAF,
  25639. 0xEC, 0x2E, 0xD9, 0x39, 0xAE, 0x9A, 0x9E, 0xDF,
  25640. 0xC9, 0xC0, 0xF8, 0xC7, 0xF5, 0x5C, 0xC9, 0x3E,
  25641. 0x5D, 0xDD, 0x0B, 0x3D, 0xE1, 0xC6, 0xED, 0xAE,
  25642. 0x2B, 0x7E, 0xE3, 0x4C, 0x61, 0x01, 0xF0, 0x11,
  25643. 0xB5, 0x90, 0x4F, 0x69, 0x3D, 0x28, 0x63, 0x56,
  25644. 0xB5, 0x4C, 0x86, 0xCE, 0x8B, 0xCF, 0xEA, 0x9D,
  25645. 0xBF, 0xEC, 0x21, 0xC1, 0xEF, 0x0E, 0xCC, 0x91,
  25646. 0x05, 0x00, 0x5B, 0xAA, 0x37, 0x7D, 0x82, 0x9D,
  25647. 0xCA, 0x2C, 0xBF, 0x5E, 0xA5, 0xF3, 0x1B, 0x71,
  25648. 0xD4, 0x46, 0xB8, 0x33, 0xE0, 0x06, 0x19, 0x81,
  25649. 0x9D, 0x7F, 0xC6, 0x02, 0x40, 0x52, 0x49, 0x97,
  25650. 0x57, 0xA2, 0x76, 0x5F, 0x19, 0xCD, 0x2B, 0x36,
  25651. 0xC2, 0x48, 0x85, 0x99, 0xDC, 0x52, 0x47, 0x49,
  25652. 0x4F, 0xAB, 0xE8, 0x1E, 0xEB, 0xEF, 0xD3, 0xBE,
  25653. 0x75, 0xC4, 0x78, 0x0E, 0x43, 0xA5, 0x04, 0x18,
  25654. 0xC5, 0xDB, 0x2F, 0xF3, 0x59, 0xC5, 0xA6, 0xDE,
  25655. 0x28, 0x6E, 0xF5, 0x95, 0x1E, 0x27, 0x09, 0x48,
  25656. 0x6E, 0xDC, 0x9C, 0xC4, 0x9D, 0x07, 0x24, 0xEC,
  25657. 0xA3, 0xF2, 0xC0, 0xB7, 0x5F, 0x8A, 0x36, 0xCE,
  25658. 0x86, 0x23, 0x88, 0xF0, 0x0B, 0x3C, 0x59, 0x3D,
  25659. 0x1C, 0x8C, 0x6A, 0xC4, 0x5D, 0x73, 0xA7, 0x2F,
  25660. 0xF6, 0xB4, 0xF8, 0x05, 0xB1, 0x31, 0xED, 0x4E,
  25661. 0xAF, 0x56, 0x01, 0xD7, 0xB7, 0x3B, 0x0E, 0x37,
  25662. 0x24, 0xE7, 0x5D, 0x58, 0xDD, 0x50, 0xF5, 0x87,
  25663. 0x1C, 0x54, 0xA3, 0x7C, 0x14, 0x81, 0x33, 0x17,
  25664. 0x59, 0xF4, 0xBE, 0x86, 0xFB, 0x58, 0xA2, 0xEE,
  25665. 0x00, 0x31, 0x30, 0xF6, 0x6E, 0x18, 0x7C, 0x8B,
  25666. 0xA5, 0x01, 0x5B, 0xE7, 0x13, 0x29, 0x65, 0x89,
  25667. 0xAC, 0xAF, 0xBF, 0x65, 0x96, 0x89, 0x7E, 0x03,
  25668. 0xD4, 0x92, 0x0C, 0x91, 0xF2, 0x63, 0x33, 0xB7,
  25669. 0xBF, 0x17, 0x98, 0xAF, 0x81, 0x5C, 0x93, 0xD4,
  25670. 0xDF, 0x55, 0xBD, 0x47, 0xA0, 0x82, 0x49, 0xBF,
  25671. 0x11, 0x30, 0x63, 0xFB, 0xB3, 0x95, 0x03, 0xE9,
  25672. 0xB6, 0xD4, 0x3E, 0xAC, 0x7B, 0x0C, 0x30, 0x5A
  25673. };
  25674. static const byte k_512[KYBER_SS_SZ] = {
  25675. 0x4D, 0xDD, 0x30, 0x4E, 0x27, 0x48, 0x99, 0xBD,
  25676. 0x82, 0x97, 0x18, 0x56, 0x82, 0x4B, 0x58, 0x71,
  25677. 0x30, 0x92, 0x79, 0x52, 0x06, 0x01, 0x21, 0x85,
  25678. 0x8F, 0x9A, 0xDE, 0xB9, 0x6A, 0xB7, 0xF5, 0x71
  25679. };
  25680. #endif
  25681. #ifndef WOLFSSL_NO_KYBER768
  25682. static const byte ek_768[KYBER768_PUBLIC_KEY_SIZE] = {
  25683. 0x14, 0x56, 0xA2, 0xEE, 0x8C, 0x35, 0x56, 0x05,
  25684. 0x4A, 0xBC, 0x79, 0xB4, 0x88, 0x2C, 0x31, 0x90,
  25685. 0xE5, 0xCA, 0x72, 0x6A, 0xB4, 0x02, 0xE5, 0xB0,
  25686. 0x97, 0x28, 0xC0, 0xF4, 0xF7, 0x9C, 0x9F, 0xC2,
  25687. 0xAD, 0xD8, 0x28, 0xAB, 0xE4, 0x32, 0xB1, 0x50,
  25688. 0x1B, 0x60, 0xF4, 0x6C, 0xCB, 0xC8, 0x6A, 0x33,
  25689. 0x78, 0xC3, 0x48, 0x95, 0x70, 0x8A, 0x13, 0x67,
  25690. 0x1B, 0x20, 0xB3, 0x89, 0x47, 0x9A, 0xAA, 0x01,
  25691. 0xC6, 0x9D, 0x6B, 0x3B, 0x7D, 0x07, 0xD1, 0xC3,
  25692. 0xAB, 0x54, 0xB9, 0x1C, 0x58, 0x0F, 0x5A, 0x33,
  25693. 0x6B, 0x30, 0x06, 0x9A, 0x4F, 0x13, 0x4F, 0xFD,
  25694. 0x37, 0x64, 0xCE, 0x73, 0xA0, 0x47, 0xE2, 0x84,
  25695. 0x47, 0x71, 0x74, 0x2B, 0xF4, 0x71, 0x0B, 0x97,
  25696. 0x2D, 0x4F, 0x65, 0x90, 0xA1, 0xC5, 0x3A, 0x97,
  25697. 0x53, 0x68, 0xC2, 0x71, 0xB6, 0x70, 0xF1, 0xA4,
  25698. 0x03, 0x64, 0x41, 0x05, 0x4A, 0x66, 0xE8, 0x81,
  25699. 0x59, 0x97, 0x51, 0x22, 0x88, 0x55, 0x2F, 0xD7,
  25700. 0x14, 0x9F, 0xFB, 0x70, 0x5A, 0xAE, 0x13, 0x3F,
  25701. 0x84, 0x14, 0x06, 0x0D, 0x00, 0x92, 0xFA, 0x8A,
  25702. 0x16, 0x27, 0xD7, 0x8A, 0xB2, 0xAB, 0xC6, 0x69,
  25703. 0x62, 0x88, 0xBA, 0xF5, 0xC6, 0x0E, 0xF3, 0x70,
  25704. 0x82, 0x7A, 0x7E, 0xFA, 0x72, 0xAE, 0x5C, 0x67,
  25705. 0x41, 0xA5, 0xDA, 0x04, 0x3D, 0x59, 0x40, 0xF1,
  25706. 0x21, 0x48, 0x53, 0x72, 0xA9, 0x8F, 0x47, 0x2D,
  25707. 0x60, 0xF0, 0x5F, 0x74, 0xD9, 0x5F, 0x01, 0xA1,
  25708. 0x99, 0x1E, 0x73, 0xA3, 0xE0, 0xA9, 0x53, 0x64,
  25709. 0x67, 0xA4, 0x73, 0x8A, 0xB4, 0xCF, 0x38, 0x5B,
  25710. 0xA7, 0x72, 0x82, 0x7E, 0xB8, 0xCC, 0x05, 0x8B,
  25711. 0x35, 0x72, 0xE4, 0x0B, 0x59, 0x84, 0x44, 0xC1,
  25712. 0x81, 0xC7, 0xF6, 0xD9, 0xB7, 0x60, 0xA7, 0xB9,
  25713. 0x07, 0x09, 0x2E, 0x9C, 0x33, 0x51, 0xEA, 0x23,
  25714. 0x4E, 0x44, 0x49, 0xBD, 0x9B, 0x61, 0xA1, 0x34,
  25715. 0x65, 0x4E, 0x2D, 0xA1, 0x91, 0xFF, 0x07, 0x93,
  25716. 0x96, 0x15, 0x69, 0xD3, 0x59, 0x44, 0x48, 0xBB,
  25717. 0xC2, 0x58, 0x69, 0x99, 0xA6, 0x67, 0x1E, 0xFC,
  25718. 0xA9, 0x57, 0xF3, 0xA6, 0x69, 0x9A, 0x4A, 0x1B,
  25719. 0x2F, 0x47, 0x07, 0xAB, 0xA0, 0xB2, 0xDB, 0x20,
  25720. 0x11, 0x4F, 0xE6, 0x8A, 0x4E, 0x28, 0x15, 0xAF,
  25721. 0x3A, 0xAC, 0x4B, 0x8C, 0x6B, 0xE5, 0x64, 0x8C,
  25722. 0x50, 0xCC, 0x35, 0xC2, 0x7C, 0x57, 0x28, 0x80,
  25723. 0x28, 0xD3, 0x61, 0x70, 0x8D, 0x30, 0x2E, 0xEB,
  25724. 0xB8, 0x60, 0xBE, 0xE6, 0x91, 0xF6, 0x56, 0xA2,
  25725. 0x55, 0x0C, 0xB3, 0x21, 0xE9, 0x29, 0x3D, 0x75,
  25726. 0x16, 0xC5, 0x99, 0x81, 0x7B, 0x76, 0x6B, 0xA9,
  25727. 0x28, 0xB1, 0x08, 0x77, 0x9A, 0x1C, 0x87, 0x12,
  25728. 0xE7, 0x4C, 0x76, 0x84, 0x1A, 0xC5, 0x8B, 0x8C,
  25729. 0x51, 0x5B, 0xF4, 0x74, 0x9B, 0xF7, 0x15, 0x98,
  25730. 0x44, 0x45, 0xB2, 0xB5, 0x30, 0x63, 0x38, 0x40,
  25731. 0x01, 0xE5, 0x5F, 0x68, 0x86, 0x7B, 0x1A, 0xF4,
  25732. 0x6C, 0xA7, 0x0C, 0xA8, 0xEA, 0x74, 0x17, 0x2D,
  25733. 0xB8, 0x0B, 0x52, 0x18, 0xBD, 0xE4, 0xF0, 0x0A,
  25734. 0x0E, 0x65, 0x8D, 0xB5, 0xA1, 0x8D, 0x94, 0xE1,
  25735. 0x42, 0x7A, 0xF7, 0xAE, 0x35, 0x8C, 0xCE, 0xB2,
  25736. 0x38, 0x77, 0x2F, 0xCC, 0x83, 0xF1, 0x08, 0x28,
  25737. 0xA4, 0xA3, 0x67, 0xD4, 0x2C, 0x4C, 0xB6, 0x93,
  25738. 0x3F, 0xDD, 0x1C, 0x1C, 0x7B, 0x86, 0xAD, 0x8B,
  25739. 0x00, 0x96, 0x57, 0xA9, 0x62, 0x22, 0xD7, 0xBA,
  25740. 0x92, 0xF5, 0x27, 0xAF, 0x87, 0x79, 0x70, 0xA8,
  25741. 0x32, 0x47, 0xF4, 0x7A, 0x23, 0xFC, 0x22, 0x85,
  25742. 0x11, 0x8B, 0x57, 0x71, 0x77, 0x15, 0x20, 0x46,
  25743. 0x74, 0xDA, 0x9C, 0x94, 0xB6, 0x2B, 0xC7, 0x83,
  25744. 0x8C, 0xF8, 0x72, 0x00, 0x15, 0x6B, 0x26, 0xBA,
  25745. 0x46, 0x71, 0x15, 0x99, 0x31, 0xC4, 0x93, 0x22,
  25746. 0xD8, 0x06, 0x71, 0xA0, 0xF3, 0x32, 0xEA, 0xA2,
  25747. 0xBB, 0xF8, 0x93, 0xBE, 0x40, 0x8B, 0x9E, 0xAC,
  25748. 0x6A, 0x50, 0x54, 0x83, 0xAA, 0x90, 0x75, 0xBD,
  25749. 0x13, 0x68, 0xB5, 0x1F, 0x99, 0x21, 0x1F, 0x48,
  25750. 0x0A, 0x9C, 0x54, 0x2A, 0x75, 0xB5, 0xBE, 0x08,
  25751. 0xE4, 0x3A, 0xDA, 0xF3, 0x01, 0xDD, 0x72, 0x9A,
  25752. 0x85, 0x95, 0x40, 0x10, 0xE6, 0x48, 0x92, 0xA2,
  25753. 0xAA, 0x4F, 0x15, 0xC0, 0xBD, 0x70, 0xB3, 0xD8,
  25754. 0x56, 0x49, 0x4F, 0xF9, 0xBA, 0x0F, 0xE4, 0xCE,
  25755. 0x12, 0x99, 0x1C, 0xA0, 0x6B, 0x5E, 0x3D, 0x0B,
  25756. 0x2A, 0xF1, 0xF7, 0x97, 0xB7, 0xA2, 0xB7, 0x60,
  25757. 0x91, 0x0A, 0xE9, 0xF8, 0x33, 0xD0, 0xD4, 0x26,
  25758. 0x7A, 0x58, 0x05, 0x2C, 0x29, 0x90, 0xF1, 0x61,
  25759. 0xB8, 0x86, 0xE2, 0x51, 0x71, 0x1C, 0x09, 0xD0,
  25760. 0x85, 0xC3, 0xD9, 0x58, 0xB1, 0x44, 0x19, 0x2C,
  25761. 0x9C, 0xC3, 0x22, 0x4A, 0x46, 0x07, 0x15, 0xB6,
  25762. 0x78, 0x4E, 0xB0, 0xB2, 0x6F, 0x23, 0x71, 0x87,
  25763. 0x50, 0x7D, 0x85, 0xC5, 0x11, 0x0A, 0xCC, 0x71,
  25764. 0xCE, 0x47, 0x19, 0x8F, 0x25, 0x45, 0x53, 0x35,
  25765. 0x6D, 0xAB, 0x44, 0x8C, 0x38, 0xD2, 0x43, 0xA7,
  25766. 0xC0, 0x2B, 0xE4, 0x0C, 0x90, 0x8C, 0x82, 0x8D,
  25767. 0x05, 0xC0, 0x81, 0xDF, 0xAB, 0x8F, 0xC6, 0xB5,
  25768. 0xCF, 0xE7, 0xD5, 0x6E, 0x73, 0x17, 0x15, 0x7D,
  25769. 0xC0, 0x53, 0xB2, 0xB3, 0x48, 0x99, 0x86, 0xB0,
  25770. 0x81, 0x28, 0x88, 0x71, 0x81, 0x85, 0x85, 0xE0,
  25771. 0x99, 0x31, 0x09, 0x5E, 0x32, 0x74, 0xA0, 0x84,
  25772. 0x11, 0x5B, 0xE2, 0x76, 0x43, 0x82, 0x54, 0xA7,
  25773. 0x96, 0x27, 0x0A, 0x7B, 0x43, 0x06, 0xF0, 0x8B,
  25774. 0x98, 0xD9, 0xC2, 0xAA, 0xEC, 0xF7, 0x06, 0x5E,
  25775. 0x74, 0x44, 0x6B, 0x7C, 0x69, 0x6D, 0xBA, 0xAF,
  25776. 0x8B, 0x46, 0x25, 0xA1, 0x0B, 0x07, 0x82, 0x7B,
  25777. 0x4A, 0x8B, 0xAB, 0xAB, 0x09, 0xB6, 0x4A, 0xE1,
  25778. 0xC3, 0x75, 0xBB, 0x78, 0x54, 0x41, 0xF3, 0x19,
  25779. 0xFB, 0x9A, 0xC2, 0xF1, 0x4C, 0x95, 0xFF, 0xB2,
  25780. 0x52, 0xAB, 0xBB, 0x80, 0x9C, 0x69, 0x09, 0xCD,
  25781. 0x97, 0x70, 0x6E, 0x40, 0x69, 0x1C, 0xBA, 0x61,
  25782. 0xC9, 0x25, 0x2B, 0xD3, 0x8A, 0x04, 0x31, 0x1C,
  25783. 0xA5, 0xBB, 0x2C, 0xA7, 0x95, 0x78, 0x34, 0x75,
  25784. 0x05, 0xD0, 0x88, 0x88, 0x51, 0xE0, 0x82, 0x64,
  25785. 0x8B, 0xD0, 0x03, 0xBE, 0x97, 0xC0, 0xF8, 0xF6,
  25786. 0x67, 0x59, 0xEC, 0x96, 0xA9, 0x6A, 0x08, 0x1C,
  25787. 0x68, 0x22, 0xC4, 0x51, 0x05, 0x59, 0x53, 0x70,
  25788. 0x42, 0xFC, 0x15, 0xF0, 0x69, 0xA6, 0x49, 0xB7,
  25789. 0x4A, 0x10, 0x96, 0x1B, 0x35, 0x4A, 0x1F, 0x62,
  25790. 0x5B, 0x04, 0xE2, 0x5B, 0x29, 0x3C, 0xF6, 0x5F,
  25791. 0xB4, 0xF5, 0x3A, 0x80, 0xCC, 0x73, 0x3D, 0x7A,
  25792. 0x17, 0x57, 0x75, 0xBF, 0x8A, 0x9A, 0xBB, 0x92,
  25793. 0x01, 0x62, 0x0E, 0x83, 0xA7, 0xF3, 0xE7, 0x24,
  25794. 0xD1, 0x28, 0x7D, 0xBC, 0x44, 0xBD, 0xD5, 0xD8,
  25795. 0x5F, 0xC7, 0x15, 0x45, 0xA9, 0x27, 0xBE, 0xED,
  25796. 0xE5, 0x37, 0xA7, 0x76, 0x87, 0x35, 0xCC, 0x14,
  25797. 0x86, 0xC7, 0xC3, 0xF3, 0x11, 0x04, 0xDB, 0x67,
  25798. 0x34, 0x3F, 0x43, 0x5D, 0x2D, 0x45, 0x55, 0x4B,
  25799. 0xAA, 0xC9, 0xCD, 0xB5, 0x82, 0x2E, 0x84, 0x22,
  25800. 0xAE, 0x83, 0x21, 0xC7, 0x8A, 0xBE, 0x9F, 0x26,
  25801. 0x1F, 0xD4, 0x81, 0x0A, 0x79, 0xE3, 0x3E, 0x94,
  25802. 0xE6, 0x3B, 0x33, 0x41, 0x87, 0x2C, 0x92, 0x25,
  25803. 0x35, 0x21, 0x99, 0x7C, 0x08, 0x4F, 0xBC, 0x06,
  25804. 0x0B, 0x8B, 0x12, 0x5C, 0xCC, 0x88, 0xAC, 0x85,
  25805. 0xAC, 0x5F, 0xE3, 0x16, 0x8A, 0xCB, 0x05, 0x9B,
  25806. 0x3F, 0x11, 0x9C, 0x4E, 0x05, 0x0A, 0x20, 0x73,
  25807. 0x2F, 0x50, 0x1B, 0xB9, 0xB3, 0xE6, 0x87, 0xC8,
  25808. 0x46, 0xB5, 0xC2, 0x65, 0x3F, 0x88, 0x86, 0x37,
  25809. 0x3E, 0x10, 0x04, 0xA2, 0xAB, 0x8D, 0x1B, 0xB9,
  25810. 0x70, 0xA7, 0xE5, 0x71, 0xD8, 0xA4, 0x6E, 0xE8,
  25811. 0x1B, 0x78, 0x2F, 0x26, 0x94, 0x2D, 0xD3, 0x94,
  25812. 0xFD, 0xD9, 0xA5, 0xE4, 0xC5, 0x63, 0x1D, 0x98,
  25813. 0x55, 0x28, 0x60, 0x4B, 0x1C, 0xC9, 0x76, 0x27,
  25814. 0x5B, 0x6A, 0xC8, 0xA6, 0x7C, 0xEE, 0xC1, 0x0F,
  25815. 0xFA, 0xCB, 0xBA, 0x3D, 0x3B, 0xB1, 0x41, 0x32,
  25816. 0x1D, 0xFC, 0x3C, 0x92, 0x31, 0xFC, 0x96, 0xE4,
  25817. 0x48, 0xB9, 0xAB, 0x84, 0x70, 0x21, 0xE2, 0xC8,
  25818. 0xD9, 0x0C, 0x6B, 0xCA, 0xF2, 0xB1, 0x24, 0x07,
  25819. 0x83, 0xB6, 0x2C, 0x79, 0xDE, 0xDC, 0x07, 0x2A,
  25820. 0x57, 0x63, 0xE6, 0x60, 0xAF, 0x2C, 0x27, 0xC3,
  25821. 0xF0, 0xC3, 0xC0, 0x92, 0x07, 0xCA, 0xD9, 0x90,
  25822. 0xBB, 0x41, 0xA7, 0xBF, 0xCE, 0xC9, 0x9F, 0x51,
  25823. 0x59, 0x6A, 0x0E, 0x83, 0x77, 0x8F, 0x85, 0xC0,
  25824. 0x06, 0xAC, 0x6D, 0x1F, 0xE9, 0x81, 0xB4, 0xC4,
  25825. 0xBA, 0x1C, 0xB5, 0x75, 0xA7, 0xD0, 0x7A, 0xE2,
  25826. 0xD3, 0x1B, 0xA7, 0x60, 0x09, 0x5F, 0x74, 0xBC,
  25827. 0x16, 0x38, 0x41, 0xCF, 0x8F, 0xF7, 0x7F, 0x89,
  25828. 0x4A, 0xBC, 0x6D, 0x26, 0x1E, 0xD8, 0x7A, 0x45,
  25829. 0x30, 0x36, 0x3B, 0x94, 0x9C, 0x4A, 0xD2, 0x4E,
  25830. 0xFB, 0x3A, 0x56, 0x80, 0x94, 0x78, 0xDD, 0xA2
  25831. };
  25832. static const byte seed_768[KYBER_ENC_RAND_SZ] = {
  25833. 0x40, 0xBE, 0x9D, 0xCA, 0xC1, 0x6E, 0x9C, 0xA7,
  25834. 0x3D, 0x49, 0xD0, 0xC8, 0x3F, 0x9D, 0x3D, 0x89,
  25835. 0xBB, 0x71, 0x57, 0x4A, 0x42, 0x19, 0xA0, 0xF3,
  25836. 0x93, 0xDF, 0xEC, 0xE2, 0x98, 0x83, 0x94, 0xC4
  25837. };
  25838. static const byte c_768[KYBER768_CIPHER_TEXT_SIZE] = {
  25839. 0x77, 0x8D, 0x6B, 0x03, 0x79, 0x1A, 0xCA, 0xF5,
  25840. 0x6C, 0xAA, 0xFC, 0xC7, 0x8C, 0xEE, 0x5C, 0xBC,
  25841. 0xA1, 0xDE, 0x87, 0x37, 0xE9, 0xC7, 0xFF, 0x4A,
  25842. 0xE5, 0xF3, 0x84, 0xD3, 0x44, 0xE0, 0x82, 0x23,
  25843. 0xC7, 0x4C, 0x82, 0x4C, 0xB5, 0x84, 0x85, 0x20,
  25844. 0x51, 0x7C, 0x7F, 0x0E, 0xA0, 0x64, 0x5E, 0xB6,
  25845. 0xF8, 0x89, 0x51, 0x7A, 0xE5, 0x21, 0x6B, 0x0C,
  25846. 0xF4, 0x1D, 0xDC, 0x3F, 0x0D, 0x1D, 0xF9, 0xBC,
  25847. 0x6E, 0x4D, 0xEC, 0xB2, 0x36, 0xA5, 0xEA, 0x8B,
  25848. 0x21, 0x4F, 0x64, 0x26, 0x6D, 0x3C, 0xDE, 0x08,
  25849. 0xE0, 0xCB, 0x00, 0xE5, 0xD9, 0x1F, 0x58, 0x67,
  25850. 0x06, 0xB1, 0xEE, 0x53, 0x3D, 0x20, 0x47, 0x6F,
  25851. 0x44, 0x23, 0xB7, 0x8F, 0x91, 0x6B, 0x17, 0x26,
  25852. 0xEE, 0xEA, 0x95, 0x9F, 0xFB, 0x9A, 0xC6, 0x34,
  25853. 0xD0, 0x4A, 0x94, 0xD0, 0x99, 0x23, 0xCB, 0x0D,
  25854. 0x4E, 0x73, 0x0C, 0xCA, 0x41, 0x44, 0xE7, 0xC4,
  25855. 0x88, 0x49, 0x21, 0x65, 0x2D, 0xA4, 0x92, 0x8C,
  25856. 0x68, 0xE6, 0x44, 0xF6, 0x73, 0xCF, 0xC5, 0x7D,
  25857. 0x3E, 0x87, 0xCF, 0x5B, 0xE5, 0x81, 0xA8, 0x9F,
  25858. 0x9C, 0xB8, 0xF0, 0xFC, 0xE2, 0x78, 0x2D, 0x68,
  25859. 0x1E, 0x5C, 0xE8, 0x8A, 0xF5, 0x84, 0x58, 0xC3,
  25860. 0xD6, 0x3D, 0x80, 0x75, 0x72, 0xDE, 0x5A, 0xA8,
  25861. 0xE1, 0xFA, 0xF2, 0xDC, 0xD1, 0x4E, 0xDB, 0x73,
  25862. 0x49, 0x56, 0x5B, 0x7D, 0x32, 0x71, 0xDD, 0xBE,
  25863. 0xB0, 0xB6, 0xCC, 0x7A, 0xFE, 0x08, 0x63, 0x57,
  25864. 0x84, 0x31, 0x11, 0x59, 0x73, 0x3C, 0x46, 0xE5,
  25865. 0xFD, 0xC5, 0xE0, 0xCD, 0x36, 0xCE, 0x56, 0x85,
  25866. 0xAC, 0xFB, 0x1A, 0xFE, 0x50, 0xAB, 0xB4, 0x6F,
  25867. 0x44, 0x75, 0x21, 0xE6, 0x0D, 0x9C, 0x8F, 0x0E,
  25868. 0x4C, 0xA2, 0x8C, 0x19, 0x0A, 0xBB, 0x40, 0xC3,
  25869. 0x65, 0xF4, 0x12, 0x47, 0x1E, 0x95, 0xA8, 0xEA,
  25870. 0x39, 0x6D, 0x4B, 0xD8, 0x07, 0x0E, 0xEB, 0x1F,
  25871. 0x02, 0xB0, 0x7C, 0x82, 0x53, 0x67, 0xAA, 0x1E,
  25872. 0xC0, 0xF1, 0x0C, 0x38, 0x62, 0x41, 0x6B, 0xB2,
  25873. 0x1A, 0xD6, 0xCA, 0x74, 0x8A, 0x86, 0xE9, 0x82,
  25874. 0x9E, 0xFC, 0x1A, 0x04, 0x99, 0x09, 0x3C, 0x85,
  25875. 0x17, 0x6D, 0x37, 0xF5, 0x74, 0xC7, 0x5C, 0xF5,
  25876. 0xED, 0xFA, 0x8D, 0x92, 0x0D, 0x32, 0x68, 0xCB,
  25877. 0x34, 0xC6, 0xA4, 0xBB, 0x00, 0x02, 0x86, 0x9B,
  25878. 0xC0, 0x5D, 0x7C, 0x8F, 0xCC, 0x06, 0x58, 0xD4,
  25879. 0xA0, 0x1E, 0xAC, 0xD7, 0x45, 0x57, 0xA3, 0x7D,
  25880. 0x98, 0xA7, 0x63, 0x07, 0x47, 0x52, 0xDF, 0xDD,
  25881. 0x64, 0x29, 0x88, 0x1C, 0xAF, 0xF5, 0x77, 0xD3,
  25882. 0xA0, 0x48, 0x03, 0x1B, 0xD5, 0x2C, 0x4E, 0x97,
  25883. 0x26, 0x39, 0x85, 0x90, 0xF9, 0x51, 0x9F, 0xD5,
  25884. 0x94, 0x05, 0xD6, 0xB3, 0xC3, 0x07, 0xAF, 0xCB,
  25885. 0x16, 0x8A, 0x98, 0x57, 0x85, 0xD9, 0x54, 0xA6,
  25886. 0xD1, 0xDC, 0x1E, 0xA9, 0x2E, 0x1E, 0xB6, 0xF9,
  25887. 0x46, 0xA4, 0xD9, 0x9D, 0xD6, 0xCA, 0x30, 0x7A,
  25888. 0xBF, 0xD8, 0x36, 0x2F, 0xAB, 0xA9, 0x8B, 0xB2,
  25889. 0x64, 0xC6, 0x9C, 0x5F, 0x55, 0x5D, 0x60, 0x88,
  25890. 0x3C, 0xC5, 0x60, 0x19, 0xFE, 0xB4, 0xE8, 0x00,
  25891. 0x0C, 0x48, 0xB7, 0xE6, 0x8C, 0xD6, 0x67, 0xF0,
  25892. 0x0B, 0x52, 0x50, 0xCE, 0xF2, 0x93, 0xA4, 0xA9,
  25893. 0xE7, 0x78, 0x72, 0x6E, 0x62, 0xF1, 0x20, 0x36,
  25894. 0x1E, 0x21, 0xAB, 0x31, 0x40, 0x46, 0x4C, 0xDC,
  25895. 0x6A, 0xBD, 0xE9, 0xEA, 0x05, 0x19, 0x8D, 0x8B,
  25896. 0x3B, 0xB6, 0x71, 0xB9, 0x11, 0x1A, 0x2F, 0x31,
  25897. 0x75, 0x82, 0x84, 0x7C, 0xA5, 0x01, 0x56, 0x64,
  25898. 0xF2, 0x2C, 0xDB, 0x08, 0xC1, 0x43, 0x18, 0x7B,
  25899. 0xDE, 0x21, 0x29, 0xB5, 0x4F, 0x34, 0x16, 0x02,
  25900. 0x95, 0xD7, 0x5F, 0xE9, 0xA4, 0x94, 0xFD, 0x7E,
  25901. 0x67, 0xAA, 0xA7, 0x6B, 0x57, 0xAA, 0xFF, 0xD8,
  25902. 0x9D, 0x01, 0xA7, 0x1D, 0xF5, 0xC8, 0x15, 0x86,
  25903. 0x20, 0x29, 0x8D, 0x58, 0x2B, 0xBE, 0xFA, 0x6D,
  25904. 0x09, 0xAC, 0x41, 0x2A, 0x99, 0xAA, 0x3B, 0xE9,
  25905. 0xC3, 0x83, 0x50, 0x49, 0x48, 0xC4, 0x3D, 0xD5,
  25906. 0xAF, 0x41, 0x27, 0xB1, 0x43, 0x58, 0x04, 0xF4,
  25907. 0x4B, 0xAF, 0xA1, 0x42, 0xBF, 0xC2, 0xA9, 0x5D,
  25908. 0x95, 0xFB, 0x2E, 0xF0, 0x64, 0x1A, 0xBE, 0x71,
  25909. 0x06, 0x4D, 0xE5, 0x1D, 0x6B, 0x9E, 0xC5, 0x08,
  25910. 0x57, 0xB8, 0xEE, 0xF7, 0xF4, 0x80, 0x36, 0x31,
  25911. 0x3D, 0x0E, 0x93, 0x67, 0x63, 0xB8, 0xF7, 0xBD,
  25912. 0xE6, 0x9B, 0x06, 0x4D, 0xD5, 0x76, 0x1D, 0x80,
  25913. 0xEA, 0x6F, 0x1A, 0x8B, 0x37, 0x56, 0x57, 0x53,
  25914. 0xC5, 0x79, 0xBB, 0xB8, 0x95, 0xEF, 0xB9, 0xFC,
  25915. 0xB3, 0xFC, 0x5F, 0xA3, 0x36, 0x2E, 0x37, 0x74,
  25916. 0xF0, 0xF7, 0x71, 0x40, 0xB9, 0x73, 0xCA, 0xE5,
  25917. 0x87, 0xBA, 0xD2, 0xF3, 0xB5, 0x66, 0xA9, 0xC2,
  25918. 0x5A, 0x96, 0x93, 0x47, 0xE5, 0xC5, 0x4F, 0x87,
  25919. 0xF1, 0x10, 0x5E, 0x9C, 0x07, 0x48, 0x67, 0xD9,
  25920. 0x40, 0x77, 0xCC, 0xAE, 0x3A, 0xBE, 0xA5, 0x45,
  25921. 0x20, 0xED, 0xB5, 0x1D, 0x9D, 0xAA, 0xBE, 0x78,
  25922. 0x48, 0xE7, 0x8F, 0xDF, 0x66, 0xE0, 0x7E, 0x2E,
  25923. 0x22, 0xB3, 0x02, 0x51, 0x93, 0x1E, 0x89, 0x0B,
  25924. 0xAF, 0x1F, 0x5E, 0x17, 0x7D, 0x4D, 0x9C, 0xEC,
  25925. 0x9E, 0x49, 0x69, 0x48, 0x1F, 0xD7, 0xC1, 0x33,
  25926. 0x5A, 0x0E, 0xD5, 0x87, 0x9F, 0x34, 0xEF, 0x4B,
  25927. 0xB4, 0xF6, 0x6C, 0x28, 0x80, 0x3C, 0xEA, 0x16,
  25928. 0x2B, 0xA4, 0x61, 0x50, 0x6D, 0x52, 0xEB, 0x3A,
  25929. 0xE1, 0x69, 0x51, 0x92, 0x2B, 0x06, 0x82, 0x51,
  25930. 0x86, 0xC3, 0xD4, 0xCE, 0x1B, 0x51, 0xF3, 0xC9,
  25931. 0x2F, 0x3C, 0x52, 0xF2, 0xD0, 0x4D, 0x1F, 0x13,
  25932. 0xB2, 0xB1, 0x7C, 0x9E, 0xEB, 0x88, 0x2C, 0xCE,
  25933. 0x0E, 0xB8, 0x8B, 0x7E, 0xA9, 0xA1, 0xCE, 0x4E,
  25934. 0x37, 0x41, 0x5C, 0xC8, 0x4C, 0x7B, 0xC4, 0x36,
  25935. 0xA4, 0x62, 0x83, 0x86, 0xCC, 0x77, 0xD9, 0xAF,
  25936. 0xD2, 0x07, 0x91, 0x1B, 0xD9, 0xBF, 0xD8, 0xA7,
  25937. 0xFA, 0x05, 0xC2, 0x75, 0xBE, 0x0C, 0x4C, 0x6A,
  25938. 0x8F, 0xC0, 0xA6, 0x1B, 0xDA, 0x1D, 0x67, 0xAE,
  25939. 0x33, 0xB5, 0x31, 0x0B, 0xE1, 0x29, 0x0D, 0xC7,
  25940. 0x1C, 0x14, 0x18, 0xEB, 0x57, 0x44, 0xBF, 0x28,
  25941. 0x42, 0xC1, 0x65, 0x21, 0x73, 0xA4, 0x9A, 0x69,
  25942. 0x2E, 0x71, 0xFE, 0x43, 0x25, 0x8A, 0x20, 0x5B,
  25943. 0x3C, 0xAA, 0xB9, 0x0C, 0x03, 0x04, 0xA5, 0x1E,
  25944. 0x77, 0xD0, 0x1B, 0x40, 0x4A, 0x01, 0xFA, 0xE2,
  25945. 0xF8, 0x3A, 0xB8, 0x0C, 0x5D, 0xBF, 0x6C, 0xF5,
  25946. 0x18, 0xC0, 0x01, 0xF4, 0x6A, 0x63, 0x3F, 0xA1,
  25947. 0x69, 0xB1, 0xBD, 0xB7, 0x7A, 0x9D, 0x0B, 0x1E,
  25948. 0x0C, 0x00, 0x78, 0x35, 0xC0, 0x9F, 0x6A, 0xBB,
  25949. 0xA9, 0x6F, 0x3F, 0x53, 0x56, 0x4D, 0xA5, 0x08,
  25950. 0xEE, 0x88, 0x61, 0xA4, 0x83, 0xA8, 0x17, 0x49,
  25951. 0xD4, 0xA4, 0x46, 0x72, 0xB1, 0xEF, 0x16, 0x05,
  25952. 0xF2, 0x9D, 0x16, 0x8B, 0x74, 0xB7, 0x36, 0xB4,
  25953. 0xF1, 0x35, 0x01, 0xD7, 0xAD, 0x12, 0x13, 0x11,
  25954. 0x8A, 0x78, 0x32, 0xE6, 0x66, 0xA5, 0x0B, 0xE8,
  25955. 0x01, 0x0D, 0x54, 0x32, 0x2A, 0x52, 0x6C, 0xF7,
  25956. 0xA4, 0xE5, 0x43, 0xA7, 0x9D, 0x0D, 0x98, 0xE0,
  25957. 0x04, 0xFB, 0xEC, 0x76, 0xEA, 0x3F, 0x7E, 0x88,
  25958. 0x7B, 0xDB, 0xAF, 0x50, 0xDA, 0xDF, 0xDD, 0xDF,
  25959. 0x3F, 0xFE, 0xCF, 0x6D, 0x3F, 0x77, 0xEA, 0x4B,
  25960. 0x9B, 0x16, 0xDC, 0x75, 0x4F, 0x4A, 0x68, 0xE5,
  25961. 0xEF, 0x32, 0xF6, 0xA1, 0x37, 0xE7, 0xC9, 0xE3,
  25962. 0xC3, 0xE8, 0xC2, 0xE2, 0x36, 0xC7, 0xEB, 0xC4,
  25963. 0x5D, 0x46, 0xEC, 0x16, 0x77, 0xA5, 0xA8, 0xBB,
  25964. 0x26, 0x68, 0x44, 0x3B, 0x0B, 0xE8, 0x69, 0x3D,
  25965. 0xC2, 0x57, 0xF1, 0x3D, 0x8B, 0x9A, 0x90, 0x10,
  25966. 0x0B, 0x92, 0xB4, 0xD1, 0x76, 0x1B, 0x81, 0x96,
  25967. 0x73, 0x83, 0x2C, 0x32, 0x02, 0x06, 0x71, 0xBF,
  25968. 0xB3, 0xD0, 0x22, 0x0A, 0x36, 0x3E, 0x4B, 0xED,
  25969. 0x6D, 0x64, 0x9D, 0x3F, 0x73, 0x68, 0xCF, 0xE0,
  25970. 0x81, 0xE1, 0x96, 0xA4, 0x3D, 0x47, 0x08, 0x79,
  25971. 0x8E, 0x31, 0xBB, 0x2A, 0x2F, 0x61, 0x82, 0x46,
  25972. 0x74, 0xAB, 0xA2, 0xFC, 0x9D, 0xCD, 0x05, 0xDB,
  25973. 0x84, 0xB8, 0x62, 0x7A, 0xE1, 0x14, 0x88, 0x88,
  25974. 0x6F, 0x92, 0x1B, 0xC7, 0x9A, 0xE1, 0xFD, 0x03
  25975. };
  25976. static const byte k_768[KYBER_SS_SZ] = {
  25977. 0x61, 0x6E, 0x0B, 0x75, 0x3A, 0x3B, 0x7F, 0x40,
  25978. 0xFE, 0xF9, 0xA3, 0x89, 0xF5, 0x8F, 0x16, 0xBF,
  25979. 0xBB, 0x04, 0x62, 0x29, 0x41, 0xD2, 0x46, 0x4B,
  25980. 0xDA, 0xE7, 0x67, 0x82, 0x0D, 0xFA, 0xC3, 0x8E
  25981. };
  25982. #endif
  25983. #ifndef WOLFSSL_NO_KYBER1024
  25984. static const byte ek_1024[KYBER1024_PUBLIC_KEY_SIZE] = {
  25985. 0x27, 0x66, 0x9A, 0x66, 0x76, 0x67, 0xB8, 0xD5,
  25986. 0x46, 0x68, 0x58, 0x60, 0x22, 0x60, 0x11, 0x5B,
  25987. 0x62, 0x09, 0xBC, 0x2C, 0x45, 0xDF, 0x7A, 0x4E,
  25988. 0x64, 0x93, 0x2B, 0x75, 0xC7, 0x8B, 0x9F, 0x70,
  25989. 0x83, 0xF1, 0x31, 0xBC, 0xD4, 0xE2, 0x0E, 0xFF,
  25990. 0x8C, 0xCF, 0x69, 0x73, 0x6B, 0xDB, 0xC8, 0x84,
  25991. 0x06, 0xF9, 0xB6, 0x9A, 0xD3, 0xCE, 0x35, 0x6A,
  25992. 0x0F, 0x5E, 0x67, 0x6D, 0xD0, 0xA7, 0xC4, 0xAB,
  25993. 0xB1, 0xA1, 0xC9, 0xD6, 0x20, 0x21, 0xBB, 0x38,
  25994. 0x4A, 0x40, 0x14, 0xFB, 0x04, 0xCD, 0x2F, 0x82,
  25995. 0x18, 0x90, 0xD9, 0x04, 0x27, 0xC4, 0x9F, 0x4A,
  25996. 0x62, 0x8E, 0xCE, 0xC2, 0x73, 0x1F, 0xAC, 0x02,
  25997. 0x52, 0x37, 0x36, 0x0D, 0x58, 0x2C, 0xD0, 0x66,
  25998. 0x47, 0xB1, 0x10, 0x9A, 0xA6, 0xC2, 0xAC, 0x5D,
  25999. 0x43, 0x37, 0x58, 0xC1, 0xCA, 0xA5, 0x35, 0x55,
  26000. 0xFF, 0xF5, 0x77, 0xEB, 0xB5, 0x21, 0xFB, 0xE3,
  26001. 0x2D, 0x10, 0xF7, 0x90, 0x60, 0x4C, 0x53, 0xC2,
  26002. 0xF8, 0x2C, 0x17, 0xB0, 0x8E, 0xF3, 0x62, 0x56,
  26003. 0x74, 0x21, 0x48, 0x44, 0x90, 0x6D, 0xB3, 0xFB,
  26004. 0x95, 0x20, 0x03, 0x14, 0x22, 0xA1, 0x3B, 0xD7,
  26005. 0x61, 0x2D, 0x42, 0x01, 0xC2, 0x7D, 0x15, 0xB9,
  26006. 0xD1, 0x94, 0x83, 0x0C, 0xC3, 0x66, 0x9B, 0xB8,
  26007. 0xBA, 0x34, 0xC2, 0x52, 0x37, 0x64, 0x41, 0x39,
  26008. 0x71, 0xC4, 0x0D, 0x84, 0xAE, 0xE6, 0x56, 0x75,
  26009. 0xD5, 0x21, 0x53, 0x09, 0xDA, 0x83, 0x67, 0xF0,
  26010. 0x01, 0x49, 0x75, 0x46, 0xEC, 0xE0, 0x7C, 0xBF,
  26011. 0x00, 0x2D, 0x78, 0x1B, 0x83, 0x06, 0x82, 0x48,
  26012. 0x40, 0x80, 0xAD, 0x6F, 0x95, 0x58, 0xB3, 0x6B,
  26013. 0x6B, 0xF6, 0x10, 0x91, 0x71, 0x30, 0xB7, 0x41,
  26014. 0x9B, 0x39, 0xF8, 0x50, 0x29, 0x62, 0x12, 0x64,
  26015. 0xCF, 0x2C, 0x8A, 0xE4, 0xD8, 0x08, 0x38, 0x7B,
  26016. 0x20, 0xCC, 0x5A, 0xA0, 0xB9, 0x69, 0xC3, 0x9B,
  26017. 0xC8, 0x0E, 0x6C, 0xB9, 0xCA, 0x03, 0x51, 0xA3,
  26018. 0xF6, 0x0A, 0xCE, 0xAF, 0x12, 0xBD, 0x41, 0xFA,
  26019. 0x09, 0x96, 0xE3, 0x99, 0x06, 0xA9, 0xB6, 0x16,
  26020. 0x97, 0xB7, 0x47, 0xC2, 0x03, 0x1C, 0x76, 0x02,
  26021. 0x88, 0x36, 0x44, 0x57, 0x42, 0x5B, 0xBB, 0xB4,
  26022. 0x0F, 0x48, 0x98, 0xAD, 0x08, 0x58, 0x76, 0x60,
  26023. 0x8A, 0x77, 0xA5, 0xEB, 0x9D, 0x12, 0x4B, 0xC9,
  26024. 0x92, 0x26, 0x51, 0xB7, 0x63, 0x95, 0x88, 0x15,
  26025. 0x58, 0xCA, 0xD0, 0x6F, 0x3C, 0x4B, 0xCF, 0x08,
  26026. 0xE4, 0x5B, 0x67, 0xBA, 0x51, 0x60, 0x38, 0xA3,
  26027. 0x64, 0xB7, 0x74, 0x0E, 0x97, 0x40, 0xEE, 0x2B,
  26028. 0x93, 0xC5, 0xC6, 0x5F, 0x49, 0x02, 0x0A, 0xD4,
  26029. 0x2B, 0x3C, 0x0A, 0xEA, 0x5B, 0xF2, 0x42, 0xA4,
  26030. 0xF1, 0xB0, 0x89, 0xB5, 0xA3, 0x45, 0x8B, 0xE8,
  26031. 0xA3, 0x71, 0xCA, 0x1F, 0x29, 0x3C, 0x53, 0xF2,
  26032. 0x78, 0x0E, 0xCE, 0x28, 0x12, 0x93, 0xD9, 0x91,
  26033. 0xE6, 0xE5, 0x79, 0x04, 0x2B, 0xAB, 0xC1, 0x69,
  26034. 0x72, 0x4F, 0x10, 0x68, 0x1F, 0xD1, 0xC7, 0xD2,
  26035. 0xFB, 0x16, 0x48, 0xB0, 0xBF, 0x80, 0x81, 0x8A,
  26036. 0x7D, 0xD3, 0xB7, 0x09, 0x73, 0x4D, 0x38, 0x97,
  26037. 0x2E, 0x3E, 0x44, 0x87, 0x5A, 0xF0, 0x92, 0x7A,
  26038. 0x9A, 0xAD, 0xE8, 0x26, 0x13, 0xFC, 0xA0, 0x5E,
  26039. 0xE5, 0xB3, 0x21, 0x06, 0x47, 0xA5, 0x63, 0x2A,
  26040. 0xA1, 0x70, 0xD0, 0x9E, 0x70, 0xB5, 0x6A, 0x2F,
  26041. 0x04, 0x33, 0x7A, 0x33, 0x7E, 0xE9, 0x52, 0x38,
  26042. 0x3A, 0x1A, 0x8A, 0xEE, 0xA6, 0xCD, 0xB9, 0x0C,
  26043. 0xCD, 0x86, 0xA8, 0x18, 0xD1, 0xBB, 0x39, 0x46,
  26044. 0x5B, 0xA3, 0x13, 0xD2, 0x66, 0xBB, 0xB1, 0x05,
  26045. 0x81, 0xFA, 0x18, 0x7D, 0x92, 0x6A, 0xC3, 0xA8,
  26046. 0xB7, 0x49, 0xF6, 0x44, 0x45, 0xFA, 0xB5, 0x6C,
  26047. 0x99, 0x27, 0x55, 0x57, 0x93, 0xFB, 0x4A, 0xCF,
  26048. 0xB0, 0x39, 0xB1, 0xAA, 0x54, 0x3B, 0x1B, 0x87,
  26049. 0xAE, 0x6A, 0x49, 0xAB, 0x56, 0x29, 0x33, 0xC4,
  26050. 0xC9, 0x7B, 0xD7, 0x4C, 0x07, 0xBF, 0x29, 0x85,
  26051. 0x1A, 0x46, 0x98, 0x51, 0xA9, 0x82, 0x59, 0x55,
  26052. 0x96, 0xFE, 0x7A, 0xCA, 0xE0, 0xDB, 0x23, 0x53,
  26053. 0x30, 0x28, 0xAA, 0x34, 0x67, 0x6F, 0x7A, 0x9B,
  26054. 0x29, 0x26, 0x3E, 0x7A, 0xA2, 0x79, 0x00, 0x10,
  26055. 0x4B, 0x1B, 0xA1, 0xB5, 0x67, 0x47, 0x39, 0xB2,
  26056. 0xFC, 0x4E, 0xD8, 0xA3, 0x30, 0xBB, 0xA5, 0xA0,
  26057. 0xB6, 0x24, 0x7C, 0x63, 0xF1, 0x15, 0x3D, 0xA0,
  26058. 0x1D, 0xC8, 0xF6, 0x16, 0xF1, 0x04, 0x83, 0xA6,
  26059. 0x93, 0xA6, 0x34, 0xC1, 0xBA, 0x6A, 0xE1, 0xAB,
  26060. 0x2F, 0x16, 0x34, 0x00, 0xBB, 0x57, 0x71, 0xE7,
  26061. 0x01, 0x71, 0xFC, 0xB5, 0x41, 0x55, 0xAB, 0xFC,
  26062. 0xB2, 0x04, 0x4F, 0xCB, 0x30, 0xBA, 0xD6, 0x7F,
  26063. 0x74, 0x21, 0x83, 0x86, 0x18, 0x19, 0xED, 0xB1,
  26064. 0xAA, 0x6C, 0x77, 0x1F, 0xC8, 0xE1, 0x1A, 0x92,
  26065. 0xE0, 0x8B, 0x71, 0xF4, 0x0D, 0x03, 0x6C, 0x15,
  26066. 0xD2, 0x89, 0x6A, 0x20, 0x47, 0x25, 0xBA, 0x90,
  26067. 0xA0, 0x3B, 0x47, 0x8D, 0x98, 0xC4, 0x90, 0x84,
  26068. 0x38, 0x2F, 0x1D, 0x22, 0x3F, 0xE1, 0x29, 0x80,
  26069. 0xE9, 0x47, 0xA4, 0x15, 0xE5, 0x5F, 0xE6, 0x7B,
  26070. 0x85, 0xDA, 0x40, 0x44, 0x13, 0x42, 0x44, 0x5B,
  26071. 0x46, 0xC2, 0xFC, 0x42, 0x02, 0x0D, 0x04, 0x76,
  26072. 0x9A, 0x2A, 0x1C, 0x64, 0x64, 0x1F, 0x0C, 0x36,
  26073. 0x63, 0x6B, 0xA6, 0xC4, 0x65, 0x2B, 0x26, 0x7A,
  26074. 0x4B, 0x92, 0x19, 0xE3, 0x33, 0xA0, 0x68, 0x17,
  26075. 0xB5, 0x81, 0x7B, 0x6E, 0x6C, 0xC4, 0x85, 0xE3,
  26076. 0x52, 0x61, 0x41, 0x69, 0xAB, 0xC2, 0x0E, 0x18,
  26077. 0x91, 0xB7, 0xA0, 0x00, 0xC5, 0x2A, 0xF1, 0x5A,
  26078. 0x7B, 0x90, 0x4C, 0x97, 0x6C, 0x1B, 0xFD, 0x3A,
  26079. 0x23, 0x77, 0xEB, 0x76, 0xB5, 0x50, 0x33, 0xC7,
  26080. 0xC4, 0xC6, 0x9E, 0x71, 0x74, 0xAA, 0xF2, 0x77,
  26081. 0x15, 0x75, 0x63, 0x16, 0xCA, 0xCC, 0xCE, 0x63,
  26082. 0xA5, 0xA2, 0x24, 0x35, 0xC7, 0xD1, 0x02, 0x04,
  26083. 0x43, 0xAA, 0x71, 0x69, 0x3B, 0xF0, 0x62, 0x30,
  26084. 0x3D, 0x13, 0x33, 0x1F, 0x79, 0x54, 0x24, 0xC2,
  26085. 0x0D, 0x26, 0x6C, 0x1D, 0x90, 0x30, 0x5F, 0xC8,
  26086. 0xC2, 0x53, 0x66, 0x84, 0xA9, 0x3D, 0x50, 0x6D,
  26087. 0xE6, 0x32, 0x9B, 0x61, 0x62, 0x40, 0x59, 0x99,
  26088. 0xBD, 0x5C, 0xAA, 0x7D, 0xDB, 0x96, 0x13, 0xC8,
  26089. 0x23, 0x8C, 0xC6, 0xD3, 0x35, 0xA1, 0xEB, 0x40,
  26090. 0x82, 0xE7, 0x71, 0x0D, 0x07, 0x9F, 0x87, 0xA4,
  26091. 0xBF, 0xF6, 0x47, 0x8B, 0x5F, 0x0C, 0x58, 0x77,
  26092. 0x86, 0xAF, 0x42, 0x71, 0x92, 0xD9, 0xA3, 0x4A,
  26093. 0x4F, 0xA3, 0x3B, 0xF0, 0xD3, 0xCC, 0x58, 0xFB,
  26094. 0x46, 0x3B, 0x48, 0x38, 0xCA, 0x2C, 0x33, 0x7E,
  26095. 0x65, 0x39, 0x7D, 0xA1, 0x56, 0x90, 0xC5, 0x2A,
  26096. 0xC0, 0xE5, 0x46, 0x8B, 0xDC, 0x03, 0xDF, 0x5A,
  26097. 0x62, 0xF7, 0x02, 0x09, 0x34, 0xE2, 0x67, 0xE0,
  26098. 0xF7, 0xCF, 0x95, 0x59, 0x94, 0x35, 0xF9, 0x52,
  26099. 0xFA, 0xB7, 0x4C, 0xFE, 0xB4, 0x30, 0x8B, 0x17,
  26100. 0x3F, 0x12, 0xE0, 0x73, 0xF7, 0xF0, 0x40, 0xDB,
  26101. 0x4C, 0x63, 0xC1, 0xC4, 0x8A, 0x7B, 0x7A, 0x41,
  26102. 0xF4, 0x77, 0x9A, 0x6B, 0x57, 0xA9, 0x22, 0xC9,
  26103. 0x70, 0x77, 0x11, 0x80, 0x00, 0x84, 0x93, 0xD4,
  26104. 0xC7, 0x68, 0x05, 0x40, 0x0B, 0x7C, 0x66, 0x4D,
  26105. 0x0B, 0x92, 0xB2, 0x2C, 0x49, 0x55, 0x1B, 0x12,
  26106. 0x47, 0xE6, 0x2C, 0x85, 0xE1, 0xE5, 0x40, 0xC8,
  26107. 0x20, 0x93, 0x37, 0x10, 0x13, 0xC4, 0x67, 0x6C,
  26108. 0xEA, 0xD7, 0x7C, 0x5F, 0x30, 0x64, 0xA3, 0x73,
  26109. 0x49, 0xC7, 0x16, 0x5E, 0xB3, 0xAA, 0x7D, 0xEF,
  26110. 0x87, 0x31, 0xE9, 0xD6, 0x6A, 0x56, 0x36, 0x8F,
  26111. 0x19, 0x5C, 0x04, 0x5B, 0x2A, 0x50, 0xE5, 0x97,
  26112. 0x86, 0x16, 0x1A, 0x63, 0x0D, 0x28, 0x00, 0x89,
  26113. 0x80, 0x12, 0x98, 0xC1, 0x30, 0xE4, 0x48, 0x31,
  26114. 0x50, 0xCA, 0x91, 0x52, 0xC2, 0xA0, 0xF2, 0x47,
  26115. 0x75, 0x0C, 0x06, 0x22, 0x59, 0xB8, 0x4C, 0x28,
  26116. 0x23, 0x6C, 0x3F, 0xB5, 0x46, 0x25, 0xD5, 0xCD,
  26117. 0xBE, 0xCC, 0x68, 0xDB, 0xA2, 0x2F, 0xB1, 0x55,
  26118. 0x80, 0x55, 0xFB, 0x9B, 0x24, 0x35, 0x01, 0xC7,
  26119. 0x58, 0x51, 0xE7, 0x6A, 0xBE, 0x48, 0x47, 0xB9,
  26120. 0xB9, 0x72, 0xA7, 0x34, 0x11, 0xA6, 0xB4, 0x28,
  26121. 0x2B, 0xF5, 0x98, 0x3A, 0x82, 0xDA, 0x74, 0x13,
  26122. 0xE5, 0x4B, 0xA3, 0x5B, 0xAB, 0x37, 0xA9, 0xB3,
  26123. 0xC6, 0x28, 0x84, 0xB6, 0x43, 0xC1, 0x34, 0x16,
  26124. 0x5C, 0x98, 0x70, 0xC6, 0xBB, 0x39, 0x0F, 0x6B,
  26125. 0x7A, 0x1E, 0x57, 0x45, 0x15, 0x8F, 0xB2, 0x51,
  26126. 0xD6, 0x90, 0x94, 0x33, 0x55, 0x1F, 0xEB, 0xD3,
  26127. 0x0B, 0xA5, 0x75, 0xA1, 0xE2, 0xF1, 0x09, 0x58,
  26128. 0x49, 0x8D, 0x9F, 0x14, 0x7E, 0xD9, 0x53, 0x13,
  26129. 0x22, 0xA1, 0x60, 0x97, 0xF5, 0x5D, 0x81, 0x17,
  26130. 0x95, 0x45, 0x79, 0x12, 0x91, 0x2B, 0x1C, 0x65,
  26131. 0xF3, 0x80, 0x25, 0x42, 0x9B, 0x3E, 0x76, 0x4A,
  26132. 0x2E, 0x1A, 0xBC, 0x4E, 0x30, 0xC2, 0x88, 0x08,
  26133. 0x27, 0x42, 0x99, 0x55, 0x90, 0x98, 0x1C, 0x43,
  26134. 0xDB, 0xB3, 0x65, 0x96, 0x6B, 0xCB, 0x97, 0x20,
  26135. 0xB1, 0x78, 0xC5, 0xEB, 0x96, 0x3B, 0x82, 0x93,
  26136. 0x4C, 0x02, 0x81, 0x4B, 0x75, 0x25, 0x54, 0x6D,
  26137. 0xB7, 0xC9, 0x6D, 0x65, 0x82, 0x2E, 0x49, 0x42,
  26138. 0xE4, 0xA4, 0xAC, 0x13, 0xC9, 0x94, 0x90, 0xE7,
  26139. 0xAB, 0x4A, 0x70, 0x23, 0x71, 0xF2, 0x13, 0x16,
  26140. 0xA5, 0x79, 0x06, 0xB1, 0x92, 0x58, 0x42, 0x88,
  26141. 0x01, 0x19, 0x25, 0x67, 0xC2, 0x04, 0x5B, 0xF8,
  26142. 0x77, 0x5C, 0xF5, 0x8C, 0x5D, 0xB2, 0x8B, 0xA1,
  26143. 0xB0, 0x5E, 0x04, 0x2A, 0x18, 0x59, 0xE6, 0x42,
  26144. 0x86, 0xB5, 0xB1, 0x14, 0xF3, 0x9F, 0xCA, 0xCC,
  26145. 0x12, 0x7B, 0xE6, 0x3D, 0xFF, 0x59, 0x0B, 0xC1,
  26146. 0x84, 0xB8, 0x3B, 0x16, 0x8C, 0x30, 0x19, 0x98,
  26147. 0x90, 0x37, 0x41, 0x00, 0xE4, 0x0D, 0x2F, 0xC7,
  26148. 0x75, 0x2B, 0x14, 0x30, 0x35, 0x50, 0x22, 0xF3,
  26149. 0xD5, 0x89, 0x25, 0xD1, 0x99, 0x1B, 0xF3, 0xB9,
  26150. 0x8A, 0x90, 0x39, 0x5F, 0x85, 0x79, 0x64, 0x6C,
  26151. 0x84, 0x13, 0xBA, 0xB3, 0xC0, 0xC0, 0x70, 0x7A,
  26152. 0x23, 0x8A, 0x27, 0xD0, 0x9F, 0xA5, 0x7A, 0x32,
  26153. 0xFF, 0x85, 0x39, 0x2F, 0xD0, 0x8C, 0x2F, 0x22,
  26154. 0x86, 0xAB, 0xDB, 0x2B, 0x69, 0x36, 0xB9, 0xD3,
  26155. 0x50, 0x38, 0x02, 0xC6, 0xB5, 0x1E, 0x41, 0x5B,
  26156. 0x81, 0x67, 0x3C, 0xC7, 0x80, 0x54, 0xF1, 0xB2,
  26157. 0xC4, 0xBD, 0xFA, 0x73, 0x3E, 0x52, 0x64, 0xC5,
  26158. 0x5A, 0x7C, 0x4D, 0xA5, 0xB7, 0x39, 0x44, 0x40,
  26159. 0x24, 0x62, 0x03, 0x3D, 0x08, 0xAE, 0x62, 0x0B,
  26160. 0xD0, 0x56, 0x44, 0xB4, 0x77, 0xAB, 0x31, 0x5E,
  26161. 0x93, 0x6D, 0x3F, 0x25, 0xB5, 0xBA, 0x7A, 0xC1,
  26162. 0x9E, 0xB5, 0x59, 0xA5, 0xC1, 0x19, 0x5F, 0x56,
  26163. 0x8B, 0x31, 0x3C, 0x26, 0x75, 0x09, 0x2E, 0x6D,
  26164. 0xF5, 0x8F, 0xF3, 0x99, 0xC4, 0x2C, 0xAB, 0x63,
  26165. 0x63, 0xAA, 0x03, 0x36, 0x91, 0xCB, 0x8C, 0xE0,
  26166. 0x66, 0x99, 0xE7, 0x01, 0xF2, 0xB9, 0x25, 0x97,
  26167. 0xCB, 0x8F, 0xC2, 0x35, 0x16, 0xE9, 0xF4, 0x0C,
  26168. 0xE7, 0x5B, 0x7B, 0xC1, 0xE0, 0x52, 0x0A, 0x5A,
  26169. 0x38, 0x95, 0xEB, 0x7D, 0x8D, 0x47, 0x40, 0x09,
  26170. 0xA0, 0xCB, 0x0A, 0xDC, 0x2D, 0xF4, 0x76, 0xB5,
  26171. 0x16, 0x41, 0x12, 0xC3, 0xB6, 0x00, 0xB6, 0x77,
  26172. 0x6D, 0xAB, 0x49, 0xB2, 0x03, 0x81, 0xA4, 0x01,
  26173. 0x46, 0x91, 0x65, 0x2A, 0x3C, 0x31, 0x61, 0xAA,
  26174. 0xC6, 0x61, 0x6C, 0xFA, 0xA2, 0x65, 0x63, 0x8C,
  26175. 0x6C, 0x66, 0x5A, 0x84, 0x54, 0xF3, 0x67, 0x80,
  26176. 0xB7, 0x89, 0xCF, 0xA3, 0x5D, 0x2A, 0xF4, 0x9E,
  26177. 0x6D, 0x5F, 0x48, 0x2B, 0xFA, 0x3C, 0x86, 0x4B,
  26178. 0x0E, 0xF2, 0x9E, 0x18, 0xD2, 0xEF, 0xFF, 0x92,
  26179. 0xDB, 0x18, 0x76, 0xA2, 0x20, 0x76, 0xAB, 0x1A,
  26180. 0xAC, 0x0A, 0x73, 0x93, 0xED, 0x9E, 0x5A, 0x48
  26181. };
  26182. static const byte seed_1024[KYBER_ENC_RAND_SZ] = {
  26183. 0x03, 0x4F, 0xF1, 0x4A, 0x56, 0x24, 0x9C, 0x25,
  26184. 0x21, 0xD4, 0x27, 0x9E, 0xBA, 0x3D, 0x04, 0x93,
  26185. 0x1C, 0xC8, 0x92, 0xBB, 0xC4, 0x50, 0x02, 0xB5,
  26186. 0xB3, 0x3D, 0x9F, 0x01, 0x88, 0xAC, 0xBA, 0xF6
  26187. };
  26188. static const byte c_1024[KYBER1024_CIPHER_TEXT_SIZE] = {
  26189. 0x8D, 0x4E, 0x2C, 0xB3, 0x9F, 0xFD, 0xE4, 0x31,
  26190. 0x1A, 0xEE, 0xDB, 0x23, 0x38, 0xBF, 0x58, 0xCE,
  26191. 0x11, 0xFA, 0xDA, 0xBD, 0xC9, 0x81, 0x3A, 0x32,
  26192. 0x19, 0x30, 0xF4, 0x67, 0x56, 0xDD, 0x13, 0xA8,
  26193. 0xE7, 0x91, 0x9F, 0xAC, 0x4F, 0x59, 0xCC, 0x9F,
  26194. 0x8B, 0x91, 0xC8, 0x33, 0xB3, 0xB3, 0xF9, 0x1A,
  26195. 0xDC, 0x6F, 0x9F, 0xBD, 0xBD, 0xE2, 0xF7, 0xDA,
  26196. 0xE8, 0x84, 0x1B, 0xE5, 0x23, 0x8B, 0x98, 0x50,
  26197. 0xA5, 0xEE, 0xBE, 0x67, 0x5D, 0xDE, 0xF4, 0x2A,
  26198. 0x93, 0x14, 0xF6, 0x90, 0x59, 0x5D, 0x51, 0x52,
  26199. 0x3E, 0x81, 0x17, 0xF2, 0x22, 0x66, 0x03, 0x4F,
  26200. 0x09, 0xB7, 0x7D, 0x99, 0x1E, 0xE5, 0x75, 0x80,
  26201. 0x2A, 0xFE, 0x44, 0x63, 0x74, 0xEB, 0x3D, 0x9E,
  26202. 0x1B, 0xEB, 0x8F, 0x25, 0x04, 0x9C, 0x6E, 0xFA,
  26203. 0x96, 0x32, 0x73, 0x66, 0xC0, 0x24, 0xCD, 0xFB,
  26204. 0xE8, 0xDC, 0x27, 0xEF, 0x56, 0x49, 0x2C, 0x90,
  26205. 0x40, 0x9E, 0x87, 0x13, 0x9C, 0x60, 0x88, 0x48,
  26206. 0x8E, 0x17, 0xB8, 0x2D, 0x15, 0x56, 0xC2, 0x51,
  26207. 0x31, 0xAC, 0xEE, 0x7D, 0xAF, 0xFE, 0x2D, 0x43,
  26208. 0x7C, 0xEC, 0x34, 0x41, 0xBB, 0xBB, 0xAB, 0x80,
  26209. 0xC4, 0xBF, 0x17, 0x7E, 0x65, 0x3A, 0xE0, 0x83,
  26210. 0x1C, 0x9B, 0x4C, 0xEB, 0x70, 0x50, 0x57, 0x27,
  26211. 0xD6, 0x3C, 0x4D, 0x47, 0x4F, 0xED, 0xC5, 0x20,
  26212. 0x19, 0xBE, 0x41, 0x1C, 0x9A, 0x43, 0xB8, 0x71,
  26213. 0x70, 0xF5, 0x89, 0x3F, 0x06, 0xEC, 0xD8, 0xD7,
  26214. 0x82, 0x06, 0x3D, 0xF8, 0x93, 0xA1, 0xB6, 0x82,
  26215. 0x24, 0x6D, 0x1C, 0x64, 0xF8, 0xF5, 0xA8, 0xC6,
  26216. 0xFC, 0xDF, 0x07, 0x92, 0x7F, 0x4D, 0x5B, 0x7A,
  26217. 0x39, 0x7F, 0xBC, 0xBD, 0x07, 0x50, 0x45, 0xDF,
  26218. 0x2C, 0x4A, 0x36, 0xF5, 0x30, 0x4C, 0x95, 0xF4,
  26219. 0x4A, 0xF9, 0x27, 0xAE, 0x91, 0x66, 0x42, 0x0B,
  26220. 0x39, 0x44, 0x87, 0x94, 0xF5, 0xB3, 0xC3, 0x52,
  26221. 0x27, 0xC3, 0xC9, 0xDF, 0x92, 0x56, 0x02, 0xA1,
  26222. 0xAC, 0x98, 0xF8, 0x51, 0xAA, 0xDB, 0x65, 0xC9,
  26223. 0x3F, 0xDD, 0x63, 0x27, 0xAE, 0xD8, 0xAE, 0x41,
  26224. 0x29, 0x72, 0x44, 0x36, 0xA3, 0x3A, 0xA0, 0x8A,
  26225. 0xA5, 0x66, 0x08, 0x85, 0x5F, 0xF8, 0x0A, 0xAA,
  26226. 0x42, 0xAC, 0xA4, 0x56, 0x2B, 0x2D, 0x78, 0xDB,
  26227. 0xBD, 0x2F, 0x91, 0xAE, 0xF2, 0x51, 0x56, 0x6B,
  26228. 0x8C, 0x6F, 0x98, 0x21, 0x37, 0x84, 0xC9, 0x9D,
  26229. 0xD7, 0xD7, 0x1F, 0x49, 0x55, 0x64, 0xC9, 0x08,
  26230. 0x50, 0x1E, 0x35, 0xE3, 0xBF, 0xBB, 0x67, 0x5C,
  26231. 0xCB, 0x66, 0x63, 0x52, 0x87, 0xCB, 0x64, 0x66,
  26232. 0xE6, 0xE3, 0x8E, 0xA8, 0xAB, 0x11, 0xCE, 0x7E,
  26233. 0xC6, 0x0B, 0xED, 0x86, 0x20, 0xB3, 0xDC, 0xD6,
  26234. 0x94, 0x3D, 0x12, 0x79, 0xA4, 0x1F, 0x93, 0xA8,
  26235. 0x7F, 0xA3, 0x59, 0xE5, 0x13, 0xC8, 0x1D, 0xE9,
  26236. 0x18, 0xDA, 0x88, 0x32, 0x2B, 0x1B, 0x08, 0x81,
  26237. 0x40, 0xE0, 0x74, 0xBE, 0x39, 0xBC, 0x17, 0xE3,
  26238. 0xC5, 0x1A, 0xB7, 0x19, 0xDF, 0x6E, 0x42, 0x6D,
  26239. 0x64, 0xFF, 0x94, 0xB8, 0x66, 0x2B, 0x9D, 0xD2,
  26240. 0x6A, 0x32, 0xA3, 0xC3, 0x68, 0x7B, 0xF9, 0x29,
  26241. 0x4C, 0x53, 0x7A, 0x22, 0x68, 0xF9, 0xDE, 0xD3,
  26242. 0x80, 0xCC, 0x8A, 0x0F, 0x11, 0x27, 0xEE, 0x5A,
  26243. 0x32, 0x2B, 0x4D, 0xF2, 0x4D, 0x87, 0xFB, 0xCE,
  26244. 0x76, 0xF5, 0x60, 0xB0, 0x37, 0xC6, 0x59, 0xB6,
  26245. 0xFB, 0x15, 0xC1, 0x56, 0x07, 0x1A, 0xED, 0xC2,
  26246. 0x6E, 0xF1, 0x11, 0x40, 0xDE, 0x88, 0xD0, 0x8D,
  26247. 0x46, 0x3E, 0xA0, 0xEA, 0xF0, 0x80, 0xA0, 0xB2,
  26248. 0xE6, 0x27, 0xD9, 0xFF, 0x1D, 0x56, 0xC5, 0x02,
  26249. 0x33, 0x55, 0x24, 0x26, 0x97, 0x27, 0xA0, 0x32,
  26250. 0xDA, 0xCD, 0x16, 0x54, 0x3A, 0xDA, 0x83, 0x42,
  26251. 0xCD, 0x6C, 0xB4, 0x0E, 0x72, 0x28, 0x59, 0x2C,
  26252. 0x35, 0x74, 0xD9, 0x82, 0xE0, 0xB9, 0x14, 0x5E,
  26253. 0xB8, 0x65, 0xDB, 0x2E, 0xE7, 0x81, 0x07, 0x26,
  26254. 0xA9, 0x16, 0xB8, 0x37, 0xCA, 0x4F, 0x14, 0xC2,
  26255. 0xCB, 0x9E, 0x95, 0x1B, 0xDE, 0x76, 0xBE, 0x16,
  26256. 0xB8, 0xB1, 0xCD, 0xC2, 0xEE, 0xCD, 0xC0, 0x69,
  26257. 0x49, 0xB8, 0xBE, 0xB1, 0x17, 0x86, 0xB8, 0xF2,
  26258. 0x5F, 0x4C, 0x9A, 0xFA, 0x55, 0x97, 0xCE, 0xB1,
  26259. 0xD8, 0x5F, 0xC9, 0xB9, 0xC9, 0x1D, 0xC6, 0x19,
  26260. 0x66, 0xF3, 0x96, 0x09, 0x1E, 0x54, 0xC9, 0x6C,
  26261. 0x97, 0xA4, 0x30, 0x0E, 0x99, 0xFD, 0x9F, 0x75,
  26262. 0x2C, 0x0B, 0xEF, 0x5D, 0x88, 0xCA, 0xFB, 0xDC,
  26263. 0xB3, 0x99, 0x3F, 0xCF, 0x6C, 0x7A, 0x8C, 0x55,
  26264. 0x19, 0xFC, 0xEC, 0xB6, 0xA7, 0x91, 0x17, 0xE9,
  26265. 0xB5, 0x21, 0x68, 0x01, 0x97, 0xD8, 0xA9, 0x1A,
  26266. 0xB7, 0x5F, 0x18, 0x14, 0xDB, 0xC5, 0x80, 0x75,
  26267. 0xEF, 0x4F, 0x07, 0x98, 0x7A, 0xBC, 0x56, 0xA7,
  26268. 0x5D, 0xA4, 0x41, 0x6E, 0xDB, 0x9D, 0x6F, 0x3D,
  26269. 0x77, 0x1A, 0xD3, 0x40, 0xD5, 0xCB, 0xCF, 0xC0,
  26270. 0xE5, 0x71, 0xFA, 0x70, 0xAA, 0xC1, 0xC7, 0xDB,
  26271. 0xBB, 0x5F, 0x5C, 0x5E, 0x1D, 0x8B, 0x10, 0x36,
  26272. 0xF5, 0xA6, 0xFC, 0xFD, 0x06, 0x25, 0xAB, 0x5B,
  26273. 0xBD, 0xA5, 0x71, 0x83, 0x9C, 0x58, 0x35, 0xDD,
  26274. 0x69, 0x79, 0x77, 0x8F, 0x59, 0xD3, 0x48, 0x68,
  26275. 0x4F, 0xA6, 0xCF, 0xC2, 0xA6, 0x25, 0x35, 0xB4,
  26276. 0x7F, 0xAD, 0x7F, 0x97, 0xB5, 0x21, 0x88, 0x72,
  26277. 0xD5, 0x2D, 0xCA, 0xCE, 0x9D, 0x3C, 0x1B, 0x11,
  26278. 0x62, 0x8D, 0x35, 0x2A, 0xD8, 0x21, 0x90, 0x0F,
  26279. 0x44, 0xE1, 0x4B, 0x64, 0x7F, 0x6B, 0xFA, 0x70,
  26280. 0xF6, 0x46, 0xB5, 0xC7, 0xAF, 0x53, 0x13, 0x17,
  26281. 0x7A, 0x10, 0x95, 0x49, 0x44, 0x22, 0x91, 0x53,
  26282. 0xA4, 0x49, 0xFC, 0xF8, 0x9A, 0x62, 0x63, 0xBD,
  26283. 0xBF, 0x85, 0x56, 0xE9, 0x81, 0xE5, 0xD6, 0x25,
  26284. 0x13, 0x40, 0xF9, 0xF4, 0x3C, 0x66, 0x92, 0x03,
  26285. 0x0F, 0xB9, 0x60, 0x5B, 0xB9, 0x9F, 0x33, 0xE9,
  26286. 0x6F, 0x06, 0xD1, 0xE4, 0xE6, 0xAB, 0xBE, 0x65,
  26287. 0xE1, 0x46, 0x96, 0xD5, 0x30, 0xF1, 0xB5, 0x25,
  26288. 0xFF, 0xF8, 0x7D, 0x54, 0xC1, 0xAC, 0x2F, 0x5E,
  26289. 0x96, 0x4D, 0x46, 0xEE, 0x37, 0xF4, 0x04, 0x5B,
  26290. 0x54, 0xE6, 0x09, 0x8F, 0x76, 0xB2, 0x8E, 0xAF,
  26291. 0x69, 0xE9, 0x98, 0x88, 0x8D, 0x25, 0xE0, 0x21,
  26292. 0xA5, 0x38, 0xFD, 0x19, 0x56, 0xA7, 0xFC, 0x30,
  26293. 0xAE, 0x83, 0xF8, 0xBA, 0x99, 0x47, 0xF8, 0x64,
  26294. 0xFD, 0x59, 0x73, 0x1A, 0x6F, 0xBB, 0x40, 0x2A,
  26295. 0xF2, 0x99, 0x0E, 0x1E, 0xD2, 0xD5, 0x6B, 0xF6,
  26296. 0x2A, 0xA6, 0xCE, 0xAE, 0x6F, 0x76, 0x9D, 0x2D,
  26297. 0x0C, 0x6C, 0x31, 0x3D, 0x7A, 0xAF, 0x97, 0x4E,
  26298. 0x69, 0xDC, 0x02, 0xCC, 0x43, 0x18, 0xB9, 0x45,
  26299. 0x7B, 0x8C, 0xC4, 0x06, 0x56, 0xAB, 0x7B, 0x61,
  26300. 0x34, 0xDE, 0x3F, 0x98, 0x01, 0xCE, 0x01, 0x96,
  26301. 0x99, 0xCE, 0x85, 0x5E, 0xBE, 0x9C, 0x6C, 0x02,
  26302. 0xFD, 0x08, 0x50, 0x6F, 0x00, 0x4A, 0x4E, 0xED,
  26303. 0x2C, 0xA1, 0x66, 0xC9, 0x54, 0xC7, 0xDB, 0x88,
  26304. 0x10, 0x70, 0x0C, 0xA6, 0x71, 0xEF, 0x37, 0x2A,
  26305. 0x29, 0x0B, 0x00, 0xE1, 0xBF, 0xBB, 0x97, 0xE3,
  26306. 0xE6, 0x74, 0xD3, 0xDC, 0xCC, 0x57, 0xCE, 0x59,
  26307. 0xF4, 0x65, 0xB1, 0x48, 0x8F, 0xF7, 0x6F, 0x62,
  26308. 0x39, 0x00, 0x8B, 0xE3, 0xE7, 0x61, 0xEF, 0x9C,
  26309. 0x11, 0x3D, 0xF0, 0x10, 0x7B, 0x8E, 0xEA, 0xE3,
  26310. 0xFE, 0xBA, 0x55, 0xB3, 0x5E, 0x4C, 0x1D, 0xA3,
  26311. 0xB6, 0xC8, 0x7A, 0x8D, 0x20, 0x11, 0x0E, 0x1C,
  26312. 0xD7, 0x71, 0xCC, 0xBC, 0x30, 0xDF, 0xF7, 0x61,
  26313. 0xE6, 0x03, 0xD4, 0x88, 0xE5, 0x5B, 0x85, 0x3A,
  26314. 0xAE, 0x7D, 0xAA, 0xDF, 0x2A, 0x00, 0x7B, 0x83,
  26315. 0x93, 0xDF, 0x08, 0xAF, 0x53, 0x4F, 0x9F, 0x53,
  26316. 0xA7, 0x37, 0x57, 0xBA, 0xBE, 0x21, 0xC8, 0x64,
  26317. 0x26, 0xCF, 0x05, 0x8E, 0xCA, 0x81, 0x7E, 0xF2,
  26318. 0x37, 0xBF, 0xC5, 0x8A, 0xC2, 0x98, 0xFB, 0xF2,
  26319. 0xA1, 0x48, 0x1C, 0x4D, 0x12, 0xDC, 0xF1, 0xB7,
  26320. 0x37, 0xFD, 0x63, 0x97, 0x69, 0xA2, 0x53, 0x1E,
  26321. 0xF9, 0x31, 0xA3, 0x62, 0xA4, 0x44, 0x56, 0xEE,
  26322. 0x2C, 0xA4, 0x85, 0x98, 0xB4, 0x62, 0x59, 0xFC,
  26323. 0xC9, 0x77, 0x07, 0x6C, 0x59, 0xFA, 0x4E, 0x29,
  26324. 0x54, 0xE9, 0x96, 0x7D, 0xA4, 0x5D, 0xA7, 0xCB,
  26325. 0xF7, 0x86, 0x33, 0xEC, 0x59, 0xC4, 0x63, 0xFE,
  26326. 0x48, 0xA8, 0x3B, 0x80, 0x1A, 0x54, 0xDB, 0x3F,
  26327. 0xEA, 0xB4, 0x45, 0xA3, 0x57, 0xE4, 0x18, 0xB0,
  26328. 0x65, 0x3F, 0x29, 0x40, 0xB2, 0xB7, 0x13, 0x81,
  26329. 0xB2, 0xDF, 0x9E, 0xCF, 0x81, 0x00, 0x84, 0x8E,
  26330. 0x29, 0x12, 0xF4, 0xBD, 0x50, 0x3A, 0xF0, 0x75,
  26331. 0xAA, 0xAF, 0x36, 0xC1, 0x36, 0xA4, 0x13, 0xC9,
  26332. 0x5B, 0xE2, 0xF2, 0x5A, 0x6D, 0x29, 0x19, 0x76,
  26333. 0xCD, 0x66, 0xA2, 0x76, 0x43, 0x53, 0x7E, 0x35,
  26334. 0xE1, 0xDF, 0x89, 0xB1, 0xE4, 0x94, 0xB3, 0x6B,
  26335. 0x08, 0xF3, 0xD0, 0x19, 0x6C, 0xD7, 0xE9, 0x0B,
  26336. 0xA5, 0xBB, 0x21, 0x00, 0x9F, 0x37, 0xA8, 0x43,
  26337. 0x19, 0x9E, 0x08, 0xDD, 0x95, 0xCA, 0x49, 0x48,
  26338. 0xC5, 0x33, 0xCB, 0x26, 0x3B, 0x5D, 0x40, 0x5A,
  26339. 0xF2, 0xFA, 0x11, 0x99, 0x81, 0xA8, 0x53, 0x6E,
  26340. 0xB7, 0x1C, 0x88, 0x22, 0x6C, 0x41, 0x53, 0x4C,
  26341. 0x26, 0x87, 0xBF, 0x1E, 0xED, 0x34, 0x75, 0xE8,
  26342. 0x48, 0x8B, 0xDE, 0x90, 0x9A, 0x93, 0xD4, 0xDB,
  26343. 0x55, 0xB6, 0xE8, 0x34, 0xB5, 0xE7, 0x86, 0x0A,
  26344. 0xA9, 0x8F, 0xD8, 0xBC, 0xB1, 0x3A, 0xB0, 0x77,
  26345. 0xB7, 0xBF, 0xD7, 0x5B, 0x35, 0xFA, 0x39, 0x3E,
  26346. 0x93, 0xE3, 0xBF, 0xB4, 0xB9, 0xBA, 0x1D, 0xAA,
  26347. 0x74, 0x65, 0xFD, 0x5B, 0x23, 0xA5, 0xB4, 0xCD,
  26348. 0x17, 0x16, 0xD4, 0xBD, 0xF7, 0xB8, 0xD5, 0x57,
  26349. 0x4B, 0x15, 0x6D, 0xB8, 0x7D, 0x8D, 0xE1, 0xE5,
  26350. 0x26, 0xC9, 0x7F, 0x8E, 0xB2, 0x87, 0xBD, 0x97,
  26351. 0xEE, 0xEE, 0xEF, 0x07, 0x4D, 0xBC, 0xB2, 0xC4,
  26352. 0xDB, 0x51, 0xA4, 0xEF, 0xF1, 0xFA, 0x7F, 0xFF,
  26353. 0x32, 0x8A, 0x57, 0x2D, 0x72, 0x70, 0x01, 0x71,
  26354. 0x08, 0xAC, 0xE2, 0xED, 0x25, 0x09, 0x3D, 0xA5,
  26355. 0x35, 0xC7, 0xA2, 0x6D, 0x3B, 0x91, 0x2A, 0xA5,
  26356. 0x7F, 0xB3, 0x22, 0xE5, 0x3B, 0xB2, 0x22, 0xE9,
  26357. 0x4E, 0x7C, 0xF6, 0x8C, 0xD8, 0xA2, 0x1A, 0xD7,
  26358. 0xC0, 0x6A, 0x4A, 0xF9, 0x78, 0xED, 0x1D, 0xEB,
  26359. 0x10, 0xE3, 0xF2, 0x41, 0x2A, 0xC6, 0x54, 0x3C,
  26360. 0x18, 0x20, 0x68, 0xEF, 0xFB, 0xD8, 0x7F, 0x31,
  26361. 0x76, 0x5F, 0x5A, 0xE6, 0x81, 0xEE, 0x8B, 0x2E,
  26362. 0x9A, 0xEB, 0x5B, 0xC9, 0x40, 0xA9, 0x4E, 0xC0,
  26363. 0xEE, 0xF5, 0xBE, 0xF7, 0x48, 0x74, 0x16, 0x9E,
  26364. 0xAB, 0xEC, 0xF1, 0x51, 0x25, 0x65, 0xC5, 0x1E,
  26365. 0xA5, 0x87, 0x21, 0xDD, 0x3A, 0xF1, 0x69, 0x03,
  26366. 0x65, 0xDB, 0x22, 0xE1, 0x87, 0x7F, 0x2A, 0x5C,
  26367. 0x01, 0x72, 0x3F, 0x69, 0xB7, 0x72, 0x52, 0x77,
  26368. 0xAE, 0x4E, 0x9E, 0xFA, 0xCD, 0x3A, 0xFA, 0x5A,
  26369. 0xDC, 0xAF, 0x38, 0x57, 0x77, 0xE7, 0xCE, 0x10,
  26370. 0xF9, 0x56, 0xB4, 0x64, 0x2C, 0x6F, 0xC1, 0xC9,
  26371. 0x78, 0x08, 0x99, 0x3E, 0xFD, 0x99, 0x4C, 0xA6,
  26372. 0x5C, 0x75, 0xF4, 0x59, 0xAC, 0x58, 0x72, 0xF8,
  26373. 0x24, 0x88, 0xC5, 0x7F, 0xB7, 0xAF, 0x9A, 0xB9,
  26374. 0x69, 0xD5, 0xE3, 0x69, 0xC1, 0x6D, 0x0B, 0x2B,
  26375. 0xF7, 0x80, 0x0B, 0x93, 0x8D, 0x67, 0x84, 0xC7,
  26376. 0xF6, 0x4D, 0x0C, 0x55, 0xCA, 0x77, 0x94, 0x65,
  26377. 0x49, 0x38, 0x94, 0x9E, 0x14, 0x21, 0x70, 0x55,
  26378. 0xD3, 0x41, 0x01, 0xF9, 0x41, 0x7D, 0x37, 0x0A,
  26379. 0x8A, 0xDD, 0x72, 0xFC, 0x0B, 0x57, 0x66, 0xEC,
  26380. 0x1D, 0x8A, 0xDD, 0xD7, 0x02, 0x33, 0x4A, 0x2A,
  26381. 0xC2, 0x77, 0x09, 0xC5, 0xAC, 0x5A, 0xE5, 0x60,
  26382. 0x1D, 0xBA, 0x95, 0x2B, 0xE2, 0x58, 0xD9, 0x33,
  26383. 0x6D, 0xF3, 0xE0, 0xF6, 0x58, 0x78, 0xA8, 0x58,
  26384. 0x61, 0x32, 0x58, 0xFB, 0x5E, 0x47, 0x94, 0x1B
  26385. };
  26386. static const byte k_1024[KYBER_SS_SZ] = {
  26387. 0x46, 0xC2, 0x00, 0xF3, 0xF6, 0xEE, 0x8E, 0x11,
  26388. 0xD4, 0x76, 0x53, 0x80, 0x1E, 0x34, 0x82, 0x24,
  26389. 0x1C, 0xB7, 0x83, 0xB9, 0xD7, 0x94, 0xEB, 0x11,
  26390. 0x6A, 0x4B, 0xDA, 0x08, 0x5A, 0xEB, 0x6B, 0xB7
  26391. };
  26392. #endif
  26393. static byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  26394. static byte ss[KYBER_SS_SZ];
  26395. key = (KyberKey*)XMALLOC(sizeof(KyberKey), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26396. ExpectNotNull(key);
  26397. if (key != NULL) {
  26398. XMEMSET(key, 0, sizeof(KyberKey));
  26399. }
  26400. #ifndef WOLFSSL_NO_KYBER512
  26401. ExpectIntEQ(wc_KyberKey_Init(KYBER512, key, NULL, INVALID_DEVID), 0);
  26402. ExpectIntEQ(wc_KyberKey_DecodePublicKey(key, ek_512, sizeof(ek_512)), 0);
  26403. ExpectIntEQ(wc_KyberKey_EncapsulateWithRandom(key, ct, ss, seed_512,
  26404. sizeof(seed_512)), 0);
  26405. ExpectIntEQ(XMEMCMP(ct, c_512, KYBER512_CIPHER_TEXT_SIZE), 0);
  26406. ExpectIntEQ(XMEMCMP(ss, k_512, KYBER_SS_SZ), 0);
  26407. wc_KyberKey_Free(key);
  26408. #endif
  26409. #ifndef WOLFSSL_NO_KYBER768
  26410. ExpectIntEQ(wc_KyberKey_Init(KYBER768, key, NULL, INVALID_DEVID), 0);
  26411. ExpectIntEQ(wc_KyberKey_DecodePublicKey(key, ek_768, sizeof(ek_768)), 0);
  26412. ExpectIntEQ(wc_KyberKey_EncapsulateWithRandom(key, ct, ss, seed_768,
  26413. sizeof(seed_768)), 0);
  26414. ExpectIntEQ(XMEMCMP(ct, c_768, KYBER768_CIPHER_TEXT_SIZE), 0);
  26415. ExpectIntEQ(XMEMCMP(ss, k_768, KYBER_SS_SZ), 0);
  26416. wc_KyberKey_Free(key);
  26417. #endif
  26418. #ifndef WOLFSSL_NO_KYBER1024
  26419. ExpectIntEQ(wc_KyberKey_Init(KYBER1024, key, NULL, INVALID_DEVID), 0);
  26420. ExpectIntEQ(wc_KyberKey_DecodePublicKey(key, ek_1024, sizeof(ek_1024)), 0);
  26421. ExpectIntEQ(wc_KyberKey_EncapsulateWithRandom(key, ct, ss, seed_1024,
  26422. sizeof(seed_1024)), 0);
  26423. ExpectIntEQ(XMEMCMP(ct, c_1024, KYBER1024_CIPHER_TEXT_SIZE), 0);
  26424. ExpectIntEQ(XMEMCMP(ss, k_1024, KYBER_SS_SZ), 0);
  26425. wc_KyberKey_Free(key);
  26426. #endif
  26427. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26428. #endif
  26429. return EXPECT_RESULT();
  26430. }
  26431. static int test_wc_kyber_decapsulate_kats(void)
  26432. {
  26433. EXPECT_DECLS;
  26434. #if defined(WOLFSSL_HAVE_KYBER) && defined(WOLFSSL_WC_KYBER) && \
  26435. defined(WOLFSSL_ML_KEM)
  26436. KyberKey* key;
  26437. #ifndef WOLFSSL_NO_KYBER512
  26438. static const byte dk_512[KYBER512_PRIVATE_KEY_SIZE] = {
  26439. 0x17, 0x43, 0x13, 0xEF, 0xA9, 0x35, 0x20, 0xE2,
  26440. 0x8A, 0x70, 0x76, 0xC8, 0x88, 0x09, 0x6E, 0x02,
  26441. 0xB0, 0xBD, 0xD8, 0x68, 0x30, 0x49, 0x7B, 0x61,
  26442. 0xFD, 0xEA, 0xB6, 0x20, 0x9C, 0x6C, 0xF7, 0x1C,
  26443. 0x62, 0x5C, 0x46, 0x80, 0x77, 0x5C, 0x34, 0x77,
  26444. 0x58, 0x1C, 0x42, 0x7A, 0x6F, 0xE1, 0xB0, 0x35,
  26445. 0x6E, 0xAB, 0x04, 0x8B, 0xCA, 0x43, 0x4F, 0x83,
  26446. 0xB5, 0x42, 0xC8, 0xB8, 0x60, 0x01, 0x06, 0x96,
  26447. 0xA5, 0x72, 0x99, 0xBB, 0x26, 0x22, 0x68, 0x89,
  26448. 0x1F, 0xFC, 0x72, 0x14, 0x2C, 0xA1, 0xA8, 0x66,
  26449. 0x18, 0x5C, 0xA8, 0x2D, 0x05, 0x40, 0x66, 0x95,
  26450. 0xBA, 0x57, 0xD4, 0xC9, 0x30, 0xF9, 0xC1, 0x7D,
  26451. 0x62, 0x23, 0x52, 0x3C, 0xF5, 0xA4, 0xF2, 0xA4,
  26452. 0x33, 0xA3, 0x64, 0x45, 0x9A, 0xC0, 0xAC, 0xDE,
  26453. 0x72, 0x54, 0x48, 0x13, 0x29, 0x28, 0x8B, 0x1B,
  26454. 0xE1, 0x87, 0xCC, 0x25, 0x21, 0x9F, 0x48, 0xC2,
  26455. 0x44, 0x3C, 0x53, 0x21, 0x99, 0x85, 0x93, 0x55,
  26456. 0x32, 0x0D, 0x04, 0xF0, 0xB8, 0x0D, 0xE9, 0x69,
  26457. 0xF1, 0x69, 0xA3, 0xD2, 0xBA, 0x34, 0x11, 0xB4,
  26458. 0xAD, 0xBC, 0x01, 0xB6, 0x62, 0x71, 0x82, 0x4C,
  26459. 0xD9, 0x54, 0x3C, 0x78, 0xBA, 0x48, 0x04, 0xAE,
  26460. 0x81, 0xF3, 0xAF, 0x00, 0x33, 0x6C, 0x5C, 0xC3,
  26461. 0x69, 0x83, 0x54, 0xC0, 0xE0, 0x18, 0x73, 0xA2,
  26462. 0xA1, 0x7D, 0x6A, 0x95, 0xA3, 0x12, 0x68, 0x9A,
  26463. 0x99, 0xDC, 0x89, 0x08, 0x41, 0x50, 0xA8, 0xD5,
  26464. 0x2B, 0xB3, 0x1C, 0x3F, 0xF3, 0xD4, 0x21, 0x5F,
  26465. 0xA3, 0xC4, 0x11, 0x1B, 0x40, 0x19, 0x92, 0x86,
  26466. 0x6E, 0x51, 0x3E, 0x51, 0x28, 0xA2, 0x0E, 0xD9,
  26467. 0x5F, 0xDE, 0xE6, 0x14, 0x85, 0xDC, 0x93, 0x7E,
  26468. 0x09, 0x9D, 0x76, 0xF7, 0x9B, 0x92, 0x73, 0x4D,
  26469. 0xC4, 0xCB, 0xB9, 0xA7, 0xA4, 0x13, 0xFE, 0xA6,
  26470. 0x28, 0x5B, 0xC0, 0xC2, 0x7C, 0x96, 0x1E, 0x47,
  26471. 0xD1, 0x98, 0x36, 0x44, 0xC4, 0xBF, 0x91, 0x3D,
  26472. 0x72, 0xF4, 0xB0, 0x30, 0xD3, 0x47, 0x38, 0x42,
  26473. 0x72, 0x63, 0xE8, 0x7A, 0xB4, 0xC0, 0xB7, 0xDF,
  26474. 0x0B, 0x72, 0xCA, 0x8A, 0xA0, 0xBA, 0xA6, 0x7B,
  26475. 0x07, 0x99, 0x39, 0xD5, 0x87, 0x80, 0x1D, 0x60,
  26476. 0xC8, 0x7A, 0x20, 0x40, 0x5E, 0x5C, 0x52, 0x60,
  26477. 0x3C, 0x07, 0x2F, 0xDB, 0x63, 0xE2, 0xE1, 0xC2,
  26478. 0xA9, 0x5C, 0xC2, 0x6F, 0x5A, 0xBE, 0xF6, 0x08,
  26479. 0x83, 0x33, 0x80, 0x08, 0x86, 0xD0, 0x93, 0xCA,
  26480. 0x01, 0xA7, 0x6F, 0x57, 0x00, 0x5E, 0x05, 0x35,
  26481. 0x69, 0x54, 0x2E, 0x0A, 0x07, 0x6B, 0x98, 0x73,
  26482. 0x6D, 0x4D, 0x39, 0xB0, 0x0F, 0xC1, 0x65, 0x3F,
  26483. 0xBC, 0x2D, 0x12, 0xEA, 0x32, 0xA9, 0x4B, 0x9B,
  26484. 0x92, 0xC6, 0x8B, 0xA4, 0xB6, 0x8A, 0x4E, 0x7B,
  26485. 0x37, 0x0A, 0x23, 0xB0, 0x3F, 0xE8, 0x22, 0x16,
  26486. 0x39, 0xB0, 0x12, 0x44, 0x80, 0x6C, 0x27, 0x06,
  26487. 0x7A, 0x58, 0x03, 0x1D, 0xB8, 0x0D, 0x2D, 0x03,
  26488. 0x66, 0x1A, 0x01, 0x7B, 0xB4, 0x6B, 0xB3, 0x71,
  26489. 0x1A, 0xCB, 0x56, 0x8A, 0x4F, 0xAB, 0xEB, 0xAF,
  26490. 0xC5, 0xFA, 0x06, 0xF7, 0xCA, 0x0E, 0x4D, 0x96,
  26491. 0x2E, 0x31, 0x70, 0xCB, 0x11, 0xC0, 0xA8, 0xD1,
  26492. 0x8A, 0x09, 0xCE, 0x27, 0xA6, 0xA9, 0x76, 0x3E,
  26493. 0x12, 0x38, 0x85, 0x45, 0x02, 0x24, 0xDE, 0x07,
  26494. 0xCC, 0x17, 0x54, 0x6C, 0x17, 0x95, 0x1F, 0xDE,
  26495. 0x47, 0x6E, 0x08, 0x35, 0x83, 0xEF, 0x10, 0xBF,
  26496. 0x76, 0xA9, 0x8A, 0xFF, 0xF9, 0xB1, 0x2D, 0xB5,
  26497. 0x40, 0x1C, 0xD3, 0x67, 0x34, 0x95, 0x39, 0x2D,
  26498. 0x74, 0x12, 0x91, 0xC3, 0xAA, 0x78, 0x42, 0x0C,
  26499. 0x8A, 0x7C, 0xB5, 0xFF, 0xE6, 0x50, 0x12, 0x99,
  26500. 0x7C, 0x4D, 0xA4, 0x32, 0x2E, 0xA9, 0x0B, 0x50,
  26501. 0x14, 0xB5, 0xB4, 0xD0, 0x18, 0x01, 0x00, 0x24,
  26502. 0x70, 0x47, 0x34, 0x1E, 0x4C, 0x24, 0xB9, 0x6B,
  26503. 0x8D, 0x7C, 0x00, 0x20, 0x52, 0x4B, 0x7C, 0x1D,
  26504. 0x66, 0xC3, 0xE0, 0x8C, 0xB2, 0x99, 0xEB, 0x4E,
  26505. 0xC6, 0xFA, 0x0E, 0xE8, 0xEA, 0x05, 0xFD, 0x43,
  26506. 0x0F, 0x57, 0x60, 0x5E, 0x89, 0x2B, 0x23, 0x2D,
  26507. 0x20, 0x47, 0xCA, 0x9B, 0x4E, 0xCA, 0xD9, 0xBD,
  26508. 0xD0, 0x9C, 0x99, 0x51, 0x19, 0x69, 0x16, 0x52,
  26509. 0x5D, 0x1E, 0xC9, 0x21, 0xB6, 0xE3, 0xCE, 0x0E,
  26510. 0xE6, 0x92, 0xEB, 0xA7, 0x28, 0xB4, 0xDB, 0x10,
  26511. 0xF3, 0x38, 0x1F, 0xBF, 0x58, 0x4A, 0xBB, 0x7B,
  26512. 0x6A, 0x92, 0x10, 0xC7, 0xC4, 0x24, 0xCE, 0x4A,
  26513. 0x36, 0x93, 0x70, 0xCB, 0x48, 0xD6, 0x08, 0x63,
  26514. 0x4A, 0xBA, 0x0B, 0xFF, 0x91, 0xC5, 0x62, 0x0A,
  26515. 0x11, 0x89, 0xD0, 0xCA, 0x97, 0x42, 0x1D, 0x42,
  26516. 0x34, 0x29, 0xFB, 0x66, 0x39, 0x52, 0xDC, 0x12,
  26517. 0x31, 0xB4, 0x36, 0x2B, 0x71, 0x62, 0xFE, 0x3A,
  26518. 0x42, 0x11, 0x1C, 0x91, 0xD7, 0x6A, 0x96, 0x4C,
  26519. 0xB4, 0x15, 0x41, 0x94, 0x20, 0x9E, 0xDB, 0xAA,
  26520. 0x1F, 0x48, 0x1B, 0xD1, 0x26, 0xC3, 0x25, 0xD1,
  26521. 0x56, 0x78, 0xE3, 0x9B, 0xCC, 0xE4, 0xC7, 0x04,
  26522. 0xEA, 0x48, 0x72, 0x46, 0x64, 0x8A, 0x6C, 0x6C,
  26523. 0x25, 0x40, 0xB5, 0xF6, 0x80, 0xA3, 0x5E, 0xE2,
  26524. 0x82, 0x42, 0x46, 0x45, 0x0A, 0x72, 0x93, 0xF2,
  26525. 0x1A, 0x90, 0xCF, 0xD1, 0x4E, 0xFA, 0xF7, 0x8F,
  26526. 0xA3, 0xD7, 0x32, 0x22, 0x51, 0xC6, 0x41, 0xA5,
  26527. 0x0E, 0x95, 0xBB, 0x5E, 0xC5, 0xCA, 0x0B, 0x60,
  26528. 0xE8, 0x9D, 0x7C, 0x18, 0xB7, 0xA4, 0x4A, 0x0F,
  26529. 0xAF, 0xB4, 0xBC, 0xAD, 0xE9, 0xB5, 0x88, 0xD1,
  26530. 0xB7, 0xFC, 0xF1, 0x2B, 0xA1, 0xE1, 0x08, 0x4D,
  26531. 0x56, 0xB1, 0x97, 0xEA, 0x90, 0xA7, 0x9A, 0x3D,
  26532. 0x83, 0x92, 0x7A, 0x23, 0x07, 0x60, 0x3B, 0xC2,
  26533. 0x11, 0xC0, 0x83, 0x0C, 0xB7, 0x06, 0x2C, 0x04,
  26534. 0x25, 0x48, 0x24, 0x57, 0x5B, 0x22, 0x6C, 0xAD,
  26535. 0x9A, 0x27, 0xC2, 0xA4, 0x55, 0x19, 0xAE, 0x39,
  26536. 0x54, 0x64, 0x67, 0x69, 0x04, 0x85, 0x49, 0x8A,
  26537. 0x32, 0x0A, 0xD5, 0x69, 0x93, 0xB1, 0x5A, 0x9D,
  26538. 0x22, 0xC6, 0x19, 0x14, 0x46, 0xCB, 0x40, 0xAA,
  26539. 0x75, 0x47, 0x40, 0x16, 0x81, 0xDC, 0xC7, 0xE3,
  26540. 0x65, 0x96, 0xB1, 0x0C, 0x07, 0xFA, 0x2A, 0x20,
  26541. 0xB4, 0x3C, 0x4B, 0x01, 0x24, 0x40, 0x1F, 0x8A,
  26542. 0x0E, 0x74, 0x48, 0x78, 0xC7, 0x29, 0x66, 0x23,
  26543. 0xC7, 0x39, 0x5B, 0x69, 0x94, 0xD1, 0x8C, 0x47,
  26544. 0x87, 0xA2, 0x89, 0xDB, 0xB0, 0x5C, 0xB1, 0x82,
  26545. 0x74, 0x51, 0xD8, 0x3F, 0x07, 0x29, 0x04, 0x53,
  26546. 0x75, 0x94, 0xF5, 0x15, 0xCA, 0x10, 0x17, 0x99,
  26547. 0x16, 0x20, 0xA3, 0x3E, 0x09, 0x6E, 0xE0, 0xDC,
  26548. 0x09, 0x1A, 0xE4, 0xCA, 0x96, 0x06, 0x03, 0xB1,
  26549. 0x01, 0xB5, 0xB4, 0xE2, 0x3E, 0x9A, 0x5B, 0x65,
  26550. 0xE1, 0xF6, 0xC2, 0xA8, 0xCC, 0x89, 0x34, 0x13,
  26551. 0x83, 0xB7, 0x06, 0x72, 0x5E, 0xD5, 0xB3, 0x48,
  26552. 0x57, 0x69, 0x18, 0x1B, 0x8F, 0x76, 0x43, 0x9C,
  26553. 0x05, 0x63, 0x6A, 0x0C, 0x34, 0x36, 0xFF, 0xBA,
  26554. 0x8B, 0x86, 0xA5, 0x30, 0x6F, 0xA1, 0x11, 0xF6,
  26555. 0xFC, 0x71, 0xEB, 0x77, 0x9B, 0x25, 0x70, 0x7C,
  26556. 0xFA, 0xE0, 0xA6, 0xDA, 0x7B, 0x0A, 0xD5, 0xD9,
  26557. 0x4B, 0x10, 0xF2, 0x1E, 0x4F, 0xCA, 0x92, 0x89,
  26558. 0x3B, 0x9F, 0xFE, 0x73, 0x21, 0x07, 0x63, 0x40,
  26559. 0x13, 0x77, 0x83, 0x7A, 0x10, 0xCA, 0x96, 0x25,
  26560. 0x34, 0x6C, 0x42, 0xAD, 0xC7, 0x05, 0xBD, 0x92,
  26561. 0xDB, 0x34, 0x26, 0xD9, 0x26, 0xCE, 0x4B, 0x5E,
  26562. 0xC2, 0x4A, 0x5C, 0xDF, 0x27, 0xCB, 0x91, 0xE5,
  26563. 0xA7, 0xE7, 0x16, 0x4D, 0x1B, 0xDC, 0x99, 0xD7,
  26564. 0x56, 0x79, 0xFB, 0xC9, 0x3A, 0x58, 0xF6, 0x47,
  26565. 0xDA, 0xC1, 0x08, 0x6C, 0xE9, 0x31, 0xBC, 0x08,
  26566. 0x92, 0x33, 0xE9, 0x48, 0x7E, 0x08, 0x67, 0xBC,
  26567. 0x58, 0x47, 0x2B, 0x01, 0xBF, 0x28, 0x95, 0xC3,
  26568. 0x23, 0xB6, 0x4D, 0xBE, 0x4A, 0x17, 0xA9, 0xE8,
  26569. 0x41, 0xB0, 0x53, 0xCA, 0xDB, 0x5C, 0x76, 0xD0,
  26570. 0x35, 0x72, 0x4C, 0x32, 0x1B, 0xBC, 0x13, 0x66,
  26571. 0x6F, 0x0A, 0x35, 0xDF, 0xDA, 0x07, 0x21, 0xE8,
  26572. 0x98, 0x76, 0x23, 0x25, 0x6A, 0x99, 0x4D, 0x95,
  26573. 0xFA, 0x1C, 0x05, 0xF5, 0x7C, 0x1E, 0x15, 0xA3,
  26574. 0x0C, 0x4A, 0x0C, 0x83, 0x18, 0xA0, 0xD8, 0x3C,
  26575. 0x41, 0x0C, 0x36, 0x28, 0x62, 0xE8, 0x17, 0xDD,
  26576. 0x6A, 0xBB, 0xAA, 0x4B, 0xBE, 0x75, 0xB7, 0x36,
  26577. 0xCC, 0xCB, 0xB4, 0xAF, 0x2A, 0x18, 0x84, 0x02,
  26578. 0xBD, 0x4C, 0xE5, 0x97, 0x93, 0x20, 0x08, 0x86,
  26579. 0x28, 0x65, 0x33, 0x25, 0x62, 0xF3, 0x24, 0xC7,
  26580. 0xA4, 0x24, 0x15, 0x1F, 0xB5, 0x9D, 0x0A, 0xE1,
  26581. 0x82, 0x1F, 0x28, 0x64, 0xC7, 0xE6, 0x98, 0x12,
  26582. 0x7A, 0xAD, 0x92, 0xC3, 0x3B, 0x31, 0x39, 0x88,
  26583. 0xC2, 0x9A, 0x09, 0xE2, 0x60, 0x44, 0x9B, 0xCA,
  26584. 0x7B, 0xEE, 0x36, 0x08, 0x62, 0x31, 0x4E, 0x47,
  26585. 0x51, 0x9E, 0xF3, 0x91, 0x8D, 0xDD, 0xE4, 0x03,
  26586. 0xE7, 0xB9, 0x2A, 0xC9, 0x90, 0x8F, 0x93, 0xC6,
  26587. 0x36, 0x9C, 0xC5, 0xC4, 0x7B, 0x8C, 0xB1, 0xDC,
  26588. 0x3A, 0x34, 0x79, 0xC7, 0x62, 0xF6, 0x2A, 0x18,
  26589. 0xFE, 0x05, 0xA9, 0xB0, 0x64, 0x5A, 0x53, 0x11,
  26590. 0xA0, 0x18, 0x28, 0x72, 0x3A, 0xEB, 0x51, 0xFA,
  26591. 0x50, 0x5E, 0x96, 0xB2, 0x9E, 0x3D, 0x2B, 0x6E,
  26592. 0x5B, 0x13, 0x27, 0xDE, 0x3A, 0x61, 0xAB, 0x0C,
  26593. 0x50, 0xBE, 0x01, 0x24, 0xB6, 0x4B, 0x33, 0x31,
  26594. 0x4B, 0x32, 0xD6, 0x12, 0x25, 0x10, 0xE4, 0x64,
  26595. 0x45, 0x85, 0x7A, 0xA0, 0xE2, 0xC4, 0xB0, 0xD2,
  26596. 0x56, 0x95, 0x56, 0x20, 0xA8, 0x68, 0x1D, 0x1E,
  26597. 0x55, 0x51, 0x26, 0xD0, 0x05, 0x09, 0xE3, 0x5B,
  26598. 0xF5, 0x96, 0x83, 0xDD, 0xAA, 0x40, 0xE8, 0x2C,
  26599. 0x51, 0x9B, 0x85, 0x58, 0x52, 0xC3, 0x66, 0xCB,
  26600. 0x54, 0x45, 0x2B, 0xF9, 0x10, 0xB0, 0x01, 0x69,
  26601. 0x23, 0x30, 0x34, 0x57, 0x08, 0x65, 0x3F, 0x51,
  26602. 0x18, 0x00, 0xB1, 0x0E, 0x00, 0x9D, 0x9F, 0x7D,
  26603. 0x10, 0xA5, 0x3B, 0x8B, 0x30, 0xBF, 0x13, 0xB0,
  26604. 0x6F, 0x25, 0x4E, 0xC8, 0xA6, 0xBA, 0x53, 0x97,
  26605. 0x00, 0xF6, 0x35, 0x8D, 0xE0, 0x46, 0x3A, 0x01,
  26606. 0x95, 0x40, 0xC9, 0x87, 0x3F, 0x3F, 0x46, 0x80,
  26607. 0xE2, 0x11, 0x3A, 0x7C, 0xCC, 0x55, 0xFF, 0x75,
  26608. 0x4D, 0x85, 0xAA, 0x67, 0xE9, 0xE5, 0x5F, 0x88,
  26609. 0x74, 0x24, 0xE0, 0xB2, 0x62, 0x56, 0x82, 0xA5,
  26610. 0xDD, 0xA2, 0x18, 0xF0, 0x3C, 0x3C, 0x10, 0xA2,
  26611. 0x46, 0xCD, 0xB0, 0xCC, 0x91, 0xD1, 0x9D, 0x8F,
  26612. 0x02, 0x4D, 0xB9, 0xB1, 0x41, 0x5F, 0x50, 0xAC,
  26613. 0xD8, 0xF6, 0x5D, 0xE2, 0x78, 0x7B, 0x91, 0x03,
  26614. 0xC5, 0x75, 0xB6, 0x87, 0x76, 0x55, 0x72, 0xCF,
  26615. 0xFA, 0x59, 0x02, 0x6C, 0x2B, 0xCE, 0xE7, 0x74,
  26616. 0x23, 0xBC, 0xAF, 0xD3, 0x05, 0x4B, 0xF8, 0xE2,
  26617. 0x71, 0x3F, 0xB8, 0x5B, 0x0B, 0xF6, 0xA4, 0x6E,
  26618. 0x71, 0x61, 0x52, 0xF5, 0xC9, 0xA3, 0x01, 0x1E,
  26619. 0xC9, 0x01, 0x14, 0xC7, 0x6B, 0x01, 0x51, 0x67,
  26620. 0x99, 0xBD, 0x59, 0x11, 0x41, 0x5B, 0x70, 0x45,
  26621. 0x44, 0x07, 0x7F, 0x18, 0x88, 0x06, 0x75, 0x5E,
  26622. 0xEC, 0x41, 0x31, 0xE5, 0x55, 0x56, 0xDB, 0x90,
  26623. 0x3F, 0x42, 0x84, 0xC1, 0xF9, 0x00, 0x86, 0xFF,
  26624. 0x43, 0x1B, 0x68, 0xF5, 0x1F, 0x62, 0x98, 0x12,
  26625. 0xF3, 0x20, 0xB5, 0x5F, 0x21, 0x9D, 0x72, 0xA1,
  26626. 0x92, 0x8F, 0x38, 0xC9, 0xA1, 0xEC, 0x82, 0x3B,
  26627. 0xA1, 0x98, 0xBA, 0x9A, 0xBB, 0xAC, 0xF6, 0x29,
  26628. 0x02, 0xB3, 0xCA, 0x0A, 0xFC, 0x95, 0xEA, 0x8A,
  26629. 0xC3, 0x03, 0xFB, 0x8B, 0xDD, 0x29, 0xBB, 0x9D,
  26630. 0x18, 0xA0, 0x3B, 0xA4, 0x4E, 0x58, 0xB1, 0xB0,
  26631. 0xB8, 0x5A, 0x2A, 0x16, 0x62, 0xE6, 0xA3, 0x1D,
  26632. 0xA7, 0x54, 0x55, 0x11, 0xA4, 0x78, 0xA1, 0x81,
  26633. 0x77, 0x88, 0x90, 0x61, 0xEF, 0x76, 0x63, 0x12,
  26634. 0x64, 0x23, 0x9A, 0xDE, 0xBD, 0x04, 0xA8, 0xC5,
  26635. 0x2B, 0x72, 0xE2, 0xB1, 0xF3, 0xA2, 0xDF, 0xBB,
  26636. 0xD8, 0xC0, 0x54, 0xE7, 0x0C, 0xC2, 0xA7, 0x42,
  26637. 0xE7, 0xB7, 0xD4, 0x17, 0xDF, 0xED, 0x31, 0x44,
  26638. 0x22, 0x18, 0x7D, 0xE1, 0xB2, 0x95, 0x44, 0x81,
  26639. 0x19, 0x57, 0x55, 0xEC, 0x04, 0xBB, 0x76, 0x71,
  26640. 0xC4, 0x33, 0x14, 0x46, 0xBB, 0xE8, 0x95, 0x25,
  26641. 0x14, 0x90, 0x53, 0x21, 0xA2, 0x17, 0x6E, 0x93,
  26642. 0x5B, 0x54, 0x20, 0xC0, 0xD5, 0xEA, 0x44, 0x65
  26643. };
  26644. static const byte c_512[KYBER512_CIPHER_TEXT_SIZE] = {
  26645. 0x84, 0xA1, 0x88, 0xA0, 0x72, 0xE4, 0xD4, 0xF4,
  26646. 0x49, 0xA4, 0xBE, 0x17, 0x02, 0x74, 0xDD, 0x2A,
  26647. 0x5F, 0x3E, 0x35, 0x6E, 0x95, 0xB9, 0x6E, 0x40,
  26648. 0xAD, 0x3F, 0xF1, 0x45, 0x5E, 0x36, 0xC6, 0xA7,
  26649. 0x1E, 0x90, 0x9D, 0xD2, 0xC0, 0xDF, 0xF8, 0xAD,
  26650. 0x2C, 0x9F, 0x50, 0x3B, 0xAC, 0x90, 0x65, 0x71,
  26651. 0x62, 0x48, 0x08, 0x3B, 0xDA, 0x40, 0xCE, 0xCB,
  26652. 0x38, 0xE3, 0xB3, 0x05, 0x8B, 0xAF, 0x51, 0xA7,
  26653. 0x57, 0x23, 0x84, 0xFF, 0x84, 0x06, 0xA8, 0x13,
  26654. 0x6A, 0x4F, 0xC6, 0xD9, 0x12, 0xA5, 0x4B, 0x2E,
  26655. 0xB5, 0xB9, 0xD5, 0x98, 0xFB, 0x68, 0x9E, 0x72,
  26656. 0xED, 0x3D, 0xEF, 0xD2, 0xFF, 0x83, 0x55, 0xED,
  26657. 0x9E, 0x9C, 0xCA, 0x53, 0xE8, 0x2C, 0x08, 0x86,
  26658. 0xE0, 0x94, 0xC5, 0x92, 0xC3, 0x92, 0x31, 0x1F,
  26659. 0x04, 0xFE, 0xC6, 0x8F, 0x9A, 0x1C, 0x53, 0x1C,
  26660. 0xF3, 0x41, 0x90, 0x30, 0x89, 0x2B, 0x5B, 0xDC,
  26661. 0xAC, 0xEE, 0xF6, 0xA0, 0xE7, 0xF1, 0xBD, 0x44,
  26662. 0x90, 0x3F, 0x49, 0xDE, 0x8E, 0x37, 0xB0, 0x2B,
  26663. 0xA3, 0xFC, 0x51, 0x21, 0xD9, 0x9F, 0x8C, 0xC3,
  26664. 0x04, 0x0F, 0x66, 0x83, 0x2F, 0x77, 0x02, 0x1B,
  26665. 0x4C, 0xA3, 0x5F, 0x7A, 0x48, 0x25, 0x03, 0x89,
  26666. 0x36, 0x56, 0x4C, 0xA2, 0xE6, 0x73, 0xFF, 0x9C,
  26667. 0xC0, 0x51, 0x9C, 0x25, 0xF6, 0xA5, 0x2D, 0x87,
  26668. 0xED, 0xD9, 0x65, 0xB2, 0x46, 0x4A, 0xA3, 0x65,
  26669. 0xD2, 0xBF, 0x06, 0x8B, 0x72, 0xFC, 0x68, 0xB6,
  26670. 0x5E, 0x88, 0x51, 0x5E, 0x2C, 0x83, 0x2B, 0xBD,
  26671. 0xB2, 0x7D, 0x61, 0xBF, 0x51, 0x2B, 0x5F, 0xC2,
  26672. 0xD8, 0x59, 0x0F, 0xB3, 0x5F, 0x49, 0x50, 0x0C,
  26673. 0xAF, 0xE7, 0x0E, 0x7D, 0x07, 0x76, 0xB5, 0xC4,
  26674. 0xE4, 0x50, 0x3A, 0x71, 0x89, 0xAD, 0xBA, 0xFF,
  26675. 0x5D, 0x5B, 0x51, 0x5C, 0xC6, 0x8B, 0x2F, 0x81,
  26676. 0xD9, 0x93, 0xC6, 0xD7, 0xFA, 0x7D, 0x3D, 0x1D,
  26677. 0x90, 0xEB, 0xFF, 0x51, 0xDA, 0x3F, 0xBB, 0xB4,
  26678. 0x43, 0x0E, 0x5B, 0xBE, 0xDB, 0xCA, 0x8D, 0xA0,
  26679. 0x78, 0xDC, 0xE8, 0xEC, 0x81, 0x5B, 0x16, 0x8B,
  26680. 0xFC, 0x09, 0xAB, 0x4A, 0x20, 0x67, 0x88, 0x70,
  26681. 0xF4, 0x86, 0x8B, 0x1F, 0xAE, 0x28, 0xD2, 0x09,
  26682. 0xC7, 0x53, 0x68, 0xA7, 0x99, 0x31, 0x7D, 0xFA,
  26683. 0x08, 0xC2, 0xB6, 0x51, 0xFA, 0xC7, 0x2D, 0xCA,
  26684. 0x2A, 0x1B, 0x4C, 0xBB, 0x75, 0xE8, 0x73, 0xF1,
  26685. 0x5C, 0x51, 0xB6, 0xD0, 0xB5, 0xE6, 0xF5, 0xE6,
  26686. 0x0E, 0x2A, 0xF6, 0xC4, 0x0D, 0x2C, 0xAB, 0xCB,
  26687. 0xF3, 0x58, 0x8F, 0x44, 0xBC, 0xEA, 0x6D, 0x72,
  26688. 0xD3, 0x59, 0xF4, 0x0F, 0x9C, 0xF5, 0xE0, 0xEC,
  26689. 0x40, 0xA5, 0x21, 0x5E, 0x5A, 0xCE, 0xEA, 0xF0,
  26690. 0xDA, 0x00, 0xD9, 0x23, 0xD4, 0xCE, 0xFF, 0x5C,
  26691. 0x3A, 0x3A, 0xB1, 0xE4, 0x6C, 0x75, 0x4F, 0x4A,
  26692. 0xE0, 0x52, 0xC2, 0xBC, 0x49, 0xFD, 0xB4, 0x52,
  26693. 0x1A, 0xE4, 0x4D, 0xF6, 0x34, 0xD5, 0x6E, 0x43,
  26694. 0x3D, 0xAD, 0x3D, 0xF3, 0xC0, 0x71, 0x15, 0x40,
  26695. 0x6F, 0xF8, 0xBF, 0xD0, 0xD7, 0xC9, 0x3B, 0x49,
  26696. 0x41, 0xD0, 0xF0, 0x92, 0x13, 0xC1, 0x68, 0x1C,
  26697. 0xFD, 0x5C, 0x86, 0x63, 0xDF, 0x02, 0x04, 0x1A,
  26698. 0x3C, 0xBD, 0x16, 0x2F, 0x5C, 0x4D, 0x80, 0xCB,
  26699. 0x1D, 0xC7, 0xD4, 0xA5, 0x01, 0xAD, 0x06, 0xFE,
  26700. 0x96, 0xEB, 0x34, 0x8B, 0x6E, 0x33, 0x1C, 0x82,
  26701. 0x96, 0xFE, 0x90, 0x4E, 0xB9, 0x7C, 0x08, 0x74,
  26702. 0x56, 0x32, 0x8D, 0x70, 0x3B, 0x85, 0xBD, 0xAC,
  26703. 0x2F, 0xB4, 0x3C, 0x72, 0x8D, 0x0B, 0x05, 0xFC,
  26704. 0x54, 0xB8, 0xC1, 0x55, 0xC0, 0x10, 0xEF, 0x0D,
  26705. 0xB1, 0x4C, 0xC6, 0x68, 0xD1, 0xB1, 0xBC, 0x72,
  26706. 0x7A, 0xF8, 0x86, 0x40, 0x76, 0x73, 0x6B, 0x89,
  26707. 0x8B, 0xAB, 0xA1, 0xC8, 0x1D, 0xCA, 0x20, 0x53,
  26708. 0xF5, 0x85, 0x87, 0xD3, 0xC4, 0xE3, 0x3C, 0x69,
  26709. 0x4A, 0x26, 0x4B, 0xE2, 0x89, 0x7E, 0x7D, 0x2E,
  26710. 0xEF, 0xAD, 0xDA, 0x9F, 0xF8, 0x8D, 0x70, 0xBF,
  26711. 0x37, 0x31, 0xF1, 0x22, 0x8C, 0xB3, 0xE1, 0x31,
  26712. 0xEB, 0x0C, 0xB7, 0x6F, 0xDB, 0xD2, 0xCC, 0xB1,
  26713. 0xCB, 0xC1, 0x8D, 0x14, 0x50, 0xAC, 0x7A, 0x16,
  26714. 0x34, 0x9E, 0x71, 0x29, 0xCA, 0xB7, 0x20, 0xD5,
  26715. 0xCB, 0x70, 0xB5, 0x6E, 0x85, 0x5E, 0x83, 0x05,
  26716. 0xDC, 0xDA, 0x73, 0x0B, 0xBD, 0x0E, 0xA3, 0x3E,
  26717. 0xF0, 0x81, 0x5D, 0x02, 0x19, 0x0B, 0xB9, 0x8E,
  26718. 0x30, 0xF7, 0x3B, 0xF7, 0x78, 0x9C, 0xDD, 0x67,
  26719. 0x3C, 0x61, 0x3B, 0x0C, 0x57, 0xCB, 0x2E, 0xF3,
  26720. 0x2E, 0x67, 0x0A, 0x98, 0xD2, 0xD6, 0x30, 0x67,
  26721. 0x07, 0x73, 0xC5, 0x9D, 0x8A, 0x6A, 0x2C, 0xFC,
  26722. 0xFF, 0x1C, 0x7C, 0xA1, 0xBB, 0x55, 0xC1, 0x7A,
  26723. 0x32, 0xCB, 0x65, 0xA2, 0xEA, 0x19, 0xC7, 0xB8,
  26724. 0xE2, 0x95, 0xC6, 0x89, 0x8C, 0xF3, 0x2F, 0xEE,
  26725. 0x1D, 0xEB, 0x01, 0x47, 0x2B, 0xE7, 0x6C, 0x3A,
  26726. 0x78, 0xCB, 0x24, 0x2E, 0xDF, 0xE2, 0x1D, 0x96,
  26727. 0x1F, 0xCB, 0x85, 0xC3, 0xCF, 0x6C, 0xEE, 0x21,
  26728. 0x89, 0x86, 0xC1, 0xBD, 0x93, 0x2B, 0xF9, 0x7B,
  26729. 0xC6, 0xDE, 0xCA, 0xAB, 0xF8, 0xC6, 0x29, 0x40,
  26730. 0xC0, 0xA5, 0x8E, 0x87, 0xC6, 0xED, 0xDC, 0xD7,
  26731. 0x4B, 0x7F, 0x71, 0x5D, 0x8C, 0x22, 0x52, 0x05,
  26732. 0x46, 0x23, 0x9F, 0x3A, 0xAA, 0x10, 0xA4, 0x35,
  26733. 0x82, 0x01, 0x03, 0xB4, 0xE3, 0x29, 0x53, 0x11,
  26734. 0xD9, 0x92, 0xC9, 0xC8, 0x77, 0x1A, 0x3C, 0xE8,
  26735. 0x49, 0x86, 0x8F, 0x36, 0xF3, 0x12, 0x14, 0xF9,
  26736. 0x63, 0x9C, 0x02, 0x8F, 0x4A, 0x5F, 0x49, 0x45,
  26737. 0xF2, 0xBE, 0xC9, 0x58, 0x50, 0x77, 0xBF, 0x2F,
  26738. 0x63, 0x7D, 0x25, 0x49, 0xF8, 0x34, 0x8C, 0x00,
  26739. 0xEC, 0xBF, 0x19, 0xC4, 0x70, 0xDF, 0x25, 0x5E,
  26740. 0xFF, 0x62, 0x32, 0x81, 0x34, 0x29, 0xF8, 0x53
  26741. };
  26742. static const byte kprime_512[KYBER_SS_SZ] = {
  26743. 0x22, 0x4B, 0x9C, 0x05, 0x12, 0x13, 0xEF, 0x46,
  26744. 0x54, 0x92, 0x43, 0x79, 0x65, 0x32, 0x28, 0x29,
  26745. 0x73, 0xFA, 0x7C, 0xF9, 0x7E, 0x89, 0x13, 0xC3,
  26746. 0x39, 0xC1, 0x94, 0x0A, 0xC1, 0x7E, 0x05, 0xE0
  26747. };
  26748. #endif
  26749. #ifndef WOLFSSL_NO_KYBER768
  26750. static const byte dk_768[KYBER768_PRIVATE_KEY_SIZE] = {
  26751. 0x34, 0x56, 0x85, 0x9B, 0xF7, 0x07, 0xE6, 0x72,
  26752. 0xAC, 0x71, 0x2B, 0x7E, 0x70, 0xF5, 0x42, 0x75,
  26753. 0x74, 0x59, 0x75, 0x02, 0xB8, 0x1D, 0xE8, 0x93,
  26754. 0x1C, 0x92, 0xA9, 0xC0, 0xD2, 0x2A, 0x8E, 0x17,
  26755. 0x73, 0xCB, 0x87, 0x47, 0x22, 0x05, 0xA3, 0x1C,
  26756. 0x32, 0x20, 0x6B, 0xA4, 0xBC, 0xF4, 0x22, 0x59,
  26757. 0x53, 0x3C, 0xB3, 0xA1, 0x9C, 0x02, 0x00, 0x86,
  26758. 0x02, 0x44, 0xA6, 0xC3, 0xF6, 0x92, 0x18, 0x45,
  26759. 0xB0, 0xA0, 0x58, 0x50, 0x18, 0x7A, 0x43, 0x10,
  26760. 0xB3, 0xD5, 0x22, 0x3A, 0xAA, 0xA0, 0xC7, 0x9B,
  26761. 0x9B, 0xBC, 0xFC, 0xCB, 0x3F, 0x75, 0x12, 0x14,
  26762. 0xEB, 0x0C, 0xFA, 0xC1, 0xA2, 0x9E, 0xD8, 0x84,
  26763. 0x8A, 0x5A, 0x49, 0xBA, 0x84, 0xBA, 0x68, 0xE6,
  26764. 0xB6, 0xF5, 0x05, 0x7D, 0x49, 0x31, 0x05, 0xFF,
  26765. 0x38, 0xA9, 0xF4, 0x4B, 0x4E, 0x7F, 0x6C, 0xBE,
  26766. 0x7D, 0x21, 0x64, 0x08, 0xF7, 0xB4, 0x86, 0x05,
  26767. 0xB2, 0x70, 0xB2, 0x53, 0xB0, 0x01, 0xA5, 0x40,
  26768. 0x1C, 0x0C, 0x91, 0x27, 0xCC, 0x18, 0x5B, 0x1B,
  26769. 0x0C, 0xF9, 0x2B, 0x99, 0xFB, 0xA0, 0xD9, 0x5A,
  26770. 0x29, 0x5F, 0x87, 0x35, 0x15, 0x52, 0x0C, 0x86,
  26771. 0x32, 0x1B, 0x8C, 0x96, 0x6C, 0x83, 0x7A, 0xAB,
  26772. 0x34, 0xB2, 0xBF, 0xFA, 0xB2, 0xA2, 0xA4, 0x30,
  26773. 0x1B, 0x35, 0x6B, 0x26, 0xCD, 0xC4, 0x56, 0x38,
  26774. 0x02, 0x90, 0x1B, 0x47, 0x62, 0xF2, 0x84, 0x28,
  26775. 0x1A, 0x38, 0x2E, 0x5F, 0x76, 0x2B, 0xEF, 0x47,
  26776. 0xB5, 0x19, 0xA8, 0x1A, 0x10, 0x86, 0x57, 0xEB,
  26777. 0xE9, 0x62, 0xBE, 0x12, 0x0B, 0x5F, 0xB3, 0xB9,
  26778. 0xED, 0x33, 0x8C, 0xCF, 0x47, 0xB3, 0xA0, 0x39,
  26779. 0x52, 0xA1, 0x66, 0x33, 0xF6, 0xE6, 0xB5, 0x34,
  26780. 0xE6, 0xB6, 0x3D, 0x05, 0x70, 0x6E, 0xFA, 0x0F,
  26781. 0x94, 0xC0, 0x3A, 0x2B, 0x85, 0x6A, 0xE5, 0x51,
  26782. 0x42, 0x2F, 0x90, 0x11, 0xF2, 0x58, 0x9A, 0x41,
  26783. 0xB9, 0x6A, 0x2C, 0xD2, 0x13, 0xC6, 0x99, 0x9B,
  26784. 0x09, 0xE9, 0x1F, 0xF4, 0x23, 0xCB, 0x10, 0x6A,
  26785. 0x1A, 0x92, 0x0B, 0x84, 0xB8, 0x11, 0x46, 0x94,
  26786. 0x97, 0x15, 0x42, 0x23, 0x98, 0x7F, 0x00, 0x5C,
  26787. 0x72, 0xF8, 0xAF, 0x38, 0x8B, 0x09, 0x0C, 0x63,
  26788. 0x9F, 0x8C, 0x77, 0x4F, 0xC5, 0xA2, 0x94, 0xC7,
  26789. 0x4A, 0x21, 0x2C, 0x91, 0xA8, 0x6C, 0x32, 0x8A,
  26790. 0xEB, 0xEA, 0x55, 0x8A, 0xB4, 0x3F, 0x8B, 0x87,
  26791. 0x35, 0x34, 0xFA, 0x2E, 0xF9, 0xE6, 0x6C, 0xEF,
  26792. 0x3C, 0x52, 0xCD, 0x47, 0x1A, 0xB7, 0x83, 0x75,
  26793. 0xE7, 0x45, 0xB9, 0xD0, 0xAA, 0x65, 0xD2, 0x27,
  26794. 0x8B, 0x92, 0x75, 0xAE, 0x53, 0x48, 0xB1, 0x6C,
  26795. 0xF6, 0x2A, 0xC8, 0x06, 0x57, 0x34, 0xE4, 0xBD,
  26796. 0x77, 0xB8, 0x0C, 0xCF, 0x89, 0x76, 0x05, 0xEB,
  26797. 0x76, 0xF4, 0x85, 0xAF, 0x8A, 0x0B, 0x46, 0x65,
  26798. 0x57, 0xA8, 0x3C, 0x02, 0x92, 0xCC, 0xF9, 0x03,
  26799. 0xEE, 0x7A, 0xA5, 0x7C, 0x3B, 0x51, 0xAD, 0x66,
  26800. 0x01, 0x89, 0xB8, 0x61, 0x39, 0xE3, 0x80, 0x42,
  26801. 0x5B, 0x31, 0xA9, 0x26, 0x89, 0xDF, 0x24, 0x31,
  26802. 0xBF, 0xA7, 0xB6, 0x9E, 0xAB, 0x17, 0x27, 0x45,
  26803. 0x1B, 0x29, 0xDA, 0x8B, 0x8B, 0xF8, 0x51, 0xE1,
  26804. 0xBC, 0x2D, 0x3A, 0x63, 0x13, 0x4C, 0xA9, 0x66,
  26805. 0x3C, 0x57, 0xAE, 0xC6, 0x98, 0x5C, 0xEB, 0xD5,
  26806. 0x6D, 0xB0, 0x44, 0x7B, 0x13, 0x6B, 0x01, 0x7A,
  26807. 0x97, 0x47, 0x61, 0xC3, 0xC6, 0x7D, 0x33, 0x77,
  26808. 0x2F, 0x99, 0x64, 0xE5, 0x43, 0x4D, 0x64, 0x35,
  26809. 0x04, 0x33, 0x2A, 0x30, 0x27, 0x29, 0x4A, 0x07,
  26810. 0x8C, 0x59, 0x9C, 0xB2, 0x91, 0x63, 0x10, 0x9C,
  26811. 0xE3, 0xB5, 0x6C, 0xE6, 0x98, 0xB4, 0xD3, 0xF5,
  26812. 0x9E, 0x29, 0x56, 0xA1, 0xF0, 0x3A, 0x4B, 0x95,
  26813. 0x55, 0x93, 0xF2, 0xD2, 0x45, 0x7F, 0xFA, 0xAE,
  26814. 0x96, 0x24, 0xA0, 0x71, 0x10, 0x45, 0xB3, 0xF5,
  26815. 0x52, 0x92, 0xF2, 0x0C, 0xC9, 0xD0, 0xCD, 0x79,
  26816. 0x1A, 0x21, 0x59, 0x7B, 0x0F, 0x2C, 0xD9, 0x80,
  26817. 0xF3, 0x51, 0x0F, 0x0B, 0x02, 0x39, 0x02, 0x20,
  26818. 0x00, 0xD7, 0x35, 0x58, 0x6E, 0xE6, 0xA7, 0x3F,
  26819. 0x3A, 0x3D, 0xCB, 0xD6, 0xBD, 0x1A, 0x85, 0xC8,
  26820. 0x65, 0x12, 0xAB, 0xF3, 0xC5, 0x1C, 0xE0, 0x0A,
  26821. 0x03, 0x31, 0xF6, 0x53, 0x60, 0x46, 0x2C, 0x02,
  26822. 0x23, 0x29, 0x59, 0x7A, 0x81, 0xC3, 0xF9, 0x2F,
  26823. 0xC1, 0x79, 0x38, 0xC9, 0x13, 0x8F, 0x41, 0x11,
  26824. 0x38, 0x79, 0x79, 0xC2, 0x8F, 0x03, 0x34, 0xF9,
  26825. 0x01, 0x19, 0x22, 0x13, 0x74, 0xDA, 0xB0, 0x45,
  26826. 0x92, 0x9B, 0x49, 0xE4, 0x3A, 0x96, 0x46, 0xA2,
  26827. 0x43, 0xF4, 0x46, 0x4D, 0xAF, 0x81, 0x1A, 0xB0,
  26828. 0x06, 0x30, 0xC7, 0x59, 0x61, 0xBC, 0xD4, 0xAF,
  26829. 0x5D, 0x99, 0x11, 0x5A, 0x37, 0x49, 0x19, 0x1B,
  26830. 0xA8, 0xFD, 0x41, 0xCE, 0x0B, 0x3C, 0x89, 0xA6,
  26831. 0x95, 0xB4, 0xBB, 0x85, 0x06, 0x4F, 0xD3, 0xAF,
  26832. 0x95, 0xC9, 0xB4, 0xAE, 0xE0, 0x9A, 0xC7, 0xB0,
  26833. 0xCC, 0x69, 0xEC, 0xA3, 0x6A, 0x00, 0x4B, 0x6C,
  26834. 0xD6, 0x62, 0xA6, 0xD3, 0x27, 0x95, 0x05, 0x3E,
  26835. 0xF0, 0xA0, 0x3A, 0xDA, 0x3B, 0x98, 0xBF, 0xE3,
  26836. 0xB4, 0x6A, 0x79, 0x72, 0x3E, 0x3A, 0x45, 0xAB,
  26837. 0x3C, 0x31, 0x95, 0x06, 0x69, 0xAD, 0x77, 0x07,
  26838. 0x20, 0x62, 0xCC, 0x3B, 0x50, 0x4D, 0xF1, 0x33,
  26839. 0x4F, 0xD6, 0x90, 0x9E, 0xAC, 0x79, 0x15, 0xF1,
  26840. 0xD5, 0xAD, 0x16, 0x63, 0x9F, 0x5F, 0xB5, 0x64,
  26841. 0x41, 0x64, 0x54, 0x25, 0x91, 0x34, 0xD5, 0x65,
  26842. 0x88, 0x2C, 0xB3, 0x81, 0xCB, 0xA5, 0x8B, 0x76,
  26843. 0x88, 0x07, 0x67, 0xB5, 0x0A, 0xC1, 0xB8, 0x57,
  26844. 0x95, 0xD7, 0x26, 0x84, 0x33, 0xB3, 0x71, 0x23,
  26845. 0x0E, 0xD4, 0xC7, 0x2F, 0x99, 0xAB, 0x1A, 0xD1,
  26846. 0xE5, 0x95, 0xA4, 0x59, 0xCF, 0x0A, 0x23, 0x34,
  26847. 0xAA, 0x14, 0x63, 0xAD, 0xE4, 0xBD, 0xC9, 0x24,
  26848. 0x96, 0x05, 0x38, 0x18, 0x57, 0xBB, 0x98, 0x09,
  26849. 0x5B, 0x41, 0x13, 0x29, 0x46, 0xCA, 0x24, 0x57,
  26850. 0xDF, 0xAA, 0x91, 0x49, 0x58, 0x2A, 0xA1, 0x99,
  26851. 0x27, 0xB6, 0x36, 0x89, 0xE2, 0x92, 0x9A, 0xA4,
  26852. 0x10, 0x27, 0xBE, 0xF4, 0x92, 0x19, 0x70, 0xBA,
  26853. 0xD4, 0xA5, 0x54, 0x90, 0xD9, 0x1A, 0xBE, 0x25,
  26854. 0x1D, 0xEF, 0x45, 0x52, 0xCA, 0x88, 0x03, 0x41,
  26855. 0x06, 0xA0, 0x2C, 0xE4, 0xB0, 0x58, 0xF8, 0xB5,
  26856. 0x96, 0x24, 0xB6, 0x7E, 0x06, 0x3B, 0xF1, 0x78,
  26857. 0xB0, 0x15, 0xE4, 0x28, 0x1E, 0xB1, 0x14, 0xA2,
  26858. 0xBC, 0x24, 0x54, 0x94, 0x3A, 0x4B, 0x46, 0x47,
  26859. 0x12, 0x2C, 0x42, 0xCB, 0xEA, 0x4E, 0x94, 0x15,
  26860. 0x4F, 0xD3, 0xE4, 0xB7, 0x91, 0xF6, 0x29, 0x0B,
  26861. 0x78, 0x29, 0x94, 0x20, 0x68, 0x53, 0xD6, 0x70,
  26862. 0x00, 0xA6, 0x33, 0xF3, 0x20, 0xA8, 0xA3, 0x74,
  26863. 0xCA, 0x5D, 0x40, 0x38, 0xF9, 0xCA, 0x42, 0x44,
  26864. 0xDC, 0xB0, 0x2E, 0x9A, 0x84, 0xE1, 0xF7, 0xC8,
  26865. 0xA8, 0x21, 0x13, 0x2B, 0x32, 0xB9, 0xA8, 0x40,
  26866. 0x55, 0x7B, 0x34, 0x78, 0x06, 0x65, 0x30, 0x17,
  26867. 0x24, 0xBA, 0x26, 0x06, 0x68, 0x1D, 0x94, 0x5E,
  26868. 0x34, 0xD7, 0xCF, 0x94, 0x1B, 0x89, 0x63, 0xCA,
  26869. 0xA1, 0x00, 0x1A, 0x49, 0x1B, 0x8B, 0x2E, 0x43,
  26870. 0x57, 0x0E, 0x9A, 0xB9, 0x5C, 0x0A, 0x57, 0xC5,
  26871. 0x03, 0xF0, 0xAB, 0x96, 0x0B, 0x48, 0x56, 0xD0,
  26872. 0x25, 0x15, 0x74, 0x71, 0x0F, 0xE5, 0xCB, 0x47,
  26873. 0x42, 0x84, 0xFC, 0x10, 0x49, 0xAA, 0x2A, 0x7B,
  26874. 0x03, 0x69, 0x4A, 0x1C, 0x76, 0x3E, 0x99, 0xDA,
  26875. 0xC6, 0xAD, 0x0B, 0xA8, 0x03, 0x8B, 0x13, 0x8A,
  26876. 0x64, 0x43, 0x2E, 0x34, 0x91, 0x16, 0xA0, 0x31,
  26877. 0xE8, 0xC7, 0x92, 0x78, 0x17, 0x51, 0xBA, 0x47,
  26878. 0x3C, 0xBD, 0xF5, 0x57, 0x20, 0x00, 0x5A, 0xBD,
  26879. 0xAA, 0x13, 0xD5, 0x01, 0x82, 0xF0, 0xE6, 0x33,
  26880. 0x77, 0x6B, 0xB0, 0x67, 0x5C, 0x40, 0x47, 0x2B,
  26881. 0xAD, 0x1F, 0x96, 0x72, 0x76, 0x91, 0x83, 0xD0,
  26882. 0xCC, 0xC8, 0x10, 0xBC, 0x25, 0xA8, 0x57, 0x32,
  26883. 0x20, 0x56, 0x9F, 0x6A, 0xC4, 0xBA, 0xC2, 0x2A,
  26884. 0x13, 0x54, 0xD8, 0xB3, 0x6C, 0x05, 0x80, 0xD0,
  26885. 0xE5, 0x29, 0x9E, 0x62, 0x9C, 0x50, 0x6C, 0xC7,
  26886. 0x65, 0x55, 0x46, 0xFF, 0x27, 0x81, 0x0C, 0x97,
  26887. 0xB5, 0x1B, 0xA0, 0x56, 0xBB, 0xF8, 0x6E, 0xD9,
  26888. 0xCB, 0x7C, 0x0A, 0x53, 0x7F, 0x72, 0xD0, 0xCF,
  26889. 0x9A, 0xD2, 0xC2, 0x31, 0xE2, 0x9E, 0xBF, 0x55,
  26890. 0x3F, 0x61, 0x3C, 0xBB, 0x15, 0xB3, 0x72, 0x1A,
  26891. 0x20, 0x07, 0x7E, 0x50, 0x5F, 0xD3, 0x90, 0xCB,
  26892. 0x19, 0xF6, 0x48, 0x8A, 0x10, 0x7D, 0xEE, 0x1C,
  26893. 0xAC, 0x58, 0xAB, 0x70, 0x34, 0xBA, 0x69, 0x03,
  26894. 0x00, 0x21, 0x95, 0x95, 0xB3, 0x69, 0x5C, 0x12,
  26895. 0x34, 0xE8, 0xB5, 0x7E, 0x33, 0xC8, 0xD3, 0xA0,
  26896. 0x48, 0x45, 0x4A, 0x61, 0x6D, 0xF3, 0xC9, 0xB5,
  26897. 0x6A, 0x6F, 0xF2, 0x02, 0x6A, 0xF9, 0x97, 0x72,
  26898. 0x5F, 0xC9, 0x55, 0x79, 0x04, 0x3B, 0xAE, 0x93,
  26899. 0x99, 0xB6, 0x79, 0x0D, 0x63, 0x7B, 0x4F, 0xA8,
  26900. 0x20, 0xB0, 0xB2, 0xD2, 0xCA, 0xB6, 0x07, 0xBA,
  26901. 0xF6, 0xA3, 0x72, 0x73, 0x4C, 0x31, 0xEE, 0x00,
  26902. 0x26, 0xF3, 0xC0, 0x76, 0xD1, 0x4A, 0x8E, 0x3E,
  26903. 0xE6, 0x6A, 0xAD, 0x8B, 0xBB, 0xCC, 0xEB, 0x9D,
  26904. 0xC7, 0x0C, 0x7B, 0x6B, 0xB0, 0xBB, 0x76, 0xC2,
  26905. 0x00, 0xC2, 0x31, 0x60, 0x1C, 0xA0, 0x87, 0x3E,
  26906. 0xC8, 0x71, 0x0F, 0x4B, 0x18, 0xD5, 0x72, 0x90,
  26907. 0xB0, 0x33, 0x72, 0x7C, 0x60, 0x1E, 0xDB, 0x71,
  26908. 0xC2, 0xB0, 0xF0, 0xC2, 0x1D, 0x55, 0x3E, 0x0E,
  26909. 0x7A, 0x4F, 0x77, 0x71, 0x68, 0x39, 0xC7, 0xC8,
  26910. 0x44, 0x8A, 0xBB, 0x9F, 0x66, 0xA5, 0x4E, 0x8A,
  26911. 0x4B, 0x08, 0xA7, 0x9D, 0x9A, 0x39, 0x2C, 0xA1,
  26912. 0x27, 0x00, 0x31, 0x38, 0x8B, 0xAD, 0x56, 0x21,
  26913. 0x7E, 0x32, 0xAE, 0xF5, 0x54, 0x11, 0x97, 0x49,
  26914. 0x06, 0xA2, 0x45, 0xC0, 0x07, 0x12, 0xB3, 0xCB,
  26915. 0xB1, 0x17, 0x06, 0x85, 0x19, 0x3F, 0xE2, 0x5A,
  26916. 0xCD, 0x7A, 0xC1, 0x3D, 0x32, 0x07, 0x3F, 0x38,
  26917. 0x79, 0xA5, 0xD7, 0x83, 0x75, 0xF0, 0x05, 0x2C,
  26918. 0xF7, 0x91, 0x75, 0xBA, 0xB4, 0x6D, 0x22, 0x37,
  26919. 0x05, 0x97, 0xBD, 0x06, 0x78, 0x9E, 0xDD, 0x07,
  26920. 0x11, 0xCC, 0x42, 0x43, 0x50, 0x7A, 0x02, 0xB4,
  26921. 0xFA, 0xAD, 0xBB, 0x62, 0x25, 0x0C, 0xC9, 0x97,
  26922. 0xAE, 0x03, 0x27, 0xAE, 0xB0, 0x0D, 0xEB, 0x52,
  26923. 0x91, 0x92, 0xA6, 0x4B, 0x10, 0x96, 0xA8, 0x6B,
  26924. 0x19, 0x67, 0x4D, 0x0B, 0x0A, 0xF0, 0x5C, 0x4A,
  26925. 0xAE, 0x17, 0x8C, 0x2C, 0x9A, 0x64, 0x42, 0xE9,
  26926. 0x4E, 0xD0, 0xA5, 0x60, 0x33, 0xA1, 0x1E, 0xE4,
  26927. 0x26, 0x32, 0xC0, 0xB4, 0xAA, 0x51, 0xD4, 0x21,
  26928. 0x50, 0x79, 0x0F, 0x41, 0x06, 0x2B, 0x77, 0x25,
  26929. 0x3C, 0x25, 0xBA, 0x4D, 0xE5, 0x59, 0x76, 0x1F,
  26930. 0x0A, 0x90, 0x06, 0x83, 0x89, 0x72, 0x8B, 0xC9,
  26931. 0x77, 0xF7, 0x0C, 0xF7, 0xBC, 0xCF, 0xBD, 0x88,
  26932. 0x3D, 0xF1, 0x3C, 0x79, 0xF5, 0xF2, 0xC3, 0x43,
  26933. 0x12, 0xCB, 0x1D, 0x5A, 0x55, 0xD7, 0x8C, 0x1B,
  26934. 0x24, 0x20, 0x96, 0xA8, 0xC0, 0x59, 0x3C, 0xFB,
  26935. 0x27, 0x53, 0x46, 0x0B, 0xD3, 0x0A, 0xBA, 0x30,
  26936. 0x6C, 0x74, 0x17, 0x39, 0x95, 0x74, 0x83, 0x85,
  26937. 0xD0, 0x0B, 0x36, 0x70, 0xE6, 0x13, 0x24, 0xD8,
  26938. 0x7D, 0xE8, 0xA1, 0x44, 0x50, 0xDC, 0x49, 0x37,
  26939. 0x68, 0x77, 0x7F, 0xF0, 0xCE, 0x68, 0x10, 0x93,
  26940. 0x7A, 0x71, 0x12, 0x29, 0x56, 0x1A, 0x5E, 0xF2,
  26941. 0xBB, 0x69, 0x86, 0x10, 0x74, 0xE0, 0x0B, 0xD9,
  26942. 0x32, 0x66, 0xE4, 0xB8, 0x62, 0x69, 0xE1, 0x8E,
  26943. 0xEA, 0x2C, 0xAA, 0xCB, 0x60, 0xA1, 0x35, 0x86,
  26944. 0x36, 0xCD, 0x7A, 0x7C, 0xA6, 0xBB, 0x68, 0x21,
  26945. 0x30, 0x24, 0x17, 0x84, 0xB1, 0x01, 0xEA, 0x5B,
  26946. 0xFD, 0x6C, 0x3A, 0x07, 0x15, 0x86, 0x21, 0x61,
  26947. 0x47, 0x36, 0xF6, 0x99, 0x6D, 0x5A, 0x4E, 0x14,
  26948. 0x96, 0x3A, 0x12, 0xD8, 0x36, 0xE5, 0x33, 0xA0,
  26949. 0xC8, 0x91, 0x2D, 0xB7, 0xE1, 0x16, 0x85, 0xA4,
  26950. 0xA5, 0x3D, 0x82, 0x85, 0xF0, 0x87, 0x50, 0xDF,
  26951. 0xF6, 0x6D, 0xA2, 0x7C, 0x23, 0xB9, 0x75, 0x42,
  26952. 0xDE, 0xFB, 0x99, 0xE4, 0x70, 0xAC, 0xD5, 0xE6,
  26953. 0x47, 0xC9, 0x40, 0xCB, 0x57, 0x30, 0x1B, 0x43,
  26954. 0xCC, 0x3E, 0x68, 0xE6, 0x4E, 0x28, 0xB0, 0x67,
  26955. 0x70, 0x69, 0x5E, 0xF6, 0x09, 0x26, 0x5E, 0x06,
  26956. 0xC6, 0x0F, 0x22, 0xCB, 0x87, 0x58, 0x49, 0xE6,
  26957. 0x2B, 0xAB, 0x88, 0xCC, 0x10, 0xEC, 0xF6, 0x22,
  26958. 0xC3, 0x79, 0xCB, 0x54, 0xF1, 0x3D, 0x8B, 0x2B,
  26959. 0xAC, 0x90, 0x2B, 0x9A, 0xB0, 0x2B, 0xB3, 0x30,
  26960. 0xB4, 0x5A, 0xC8, 0xB7, 0x41, 0xC2, 0x64, 0x7A,
  26961. 0xC4, 0x5B, 0x5B, 0xF4, 0x8A, 0x6D, 0x3F, 0xE0,
  26962. 0x39, 0x98, 0x6C, 0xC9, 0x40, 0xC6, 0x0A, 0x94,
  26963. 0xE6, 0x6C, 0xF6, 0x44, 0x53, 0x10, 0x16, 0xA5,
  26964. 0x27, 0x24, 0x50, 0x82, 0x43, 0x14, 0xB5, 0x66,
  26965. 0x2A, 0x0A, 0x90, 0x9A, 0xBF, 0xB4, 0x6F, 0xD2,
  26966. 0x7B, 0xAE, 0xD3, 0xAB, 0xA8, 0x25, 0x93, 0x61,
  26967. 0x59, 0x68, 0x82, 0xB0, 0x8B, 0x2A, 0xC7, 0x23,
  26968. 0x39, 0x30, 0xFC, 0x37, 0x86, 0x73, 0x8E, 0xD2,
  26969. 0xF8, 0x1E, 0xE6, 0x38, 0xC4, 0x5C, 0x3B, 0x9C,
  26970. 0xFD, 0x19, 0x51, 0xDB, 0x5B, 0xCC, 0x14, 0x45,
  26971. 0xC2, 0xC1, 0x62, 0x5D, 0x57, 0xD5, 0x7B, 0x53,
  26972. 0x90, 0x4B, 0x6A, 0x1A, 0xB6, 0x81, 0x58, 0x07,
  26973. 0x55, 0xE8, 0x9F, 0xA7, 0x97, 0x75, 0xA6, 0x57,
  26974. 0xCD, 0x62, 0xB4, 0x42, 0x63, 0x04, 0xBC, 0x0C,
  26975. 0x71, 0x1E, 0x28, 0x07, 0xA2, 0xC9, 0xE8, 0x52,
  26976. 0xD4, 0xB4, 0x35, 0x9E, 0xE6, 0xB5, 0x3E, 0x46,
  26977. 0x75, 0xF5, 0x23, 0xC9, 0x07, 0x82, 0x57, 0x2D,
  26978. 0xC7, 0x36, 0x8F, 0xB4, 0x00, 0xC3, 0x28, 0xC7,
  26979. 0x0F, 0xC8, 0x46, 0xB5, 0xE9, 0x8A, 0x43, 0x30,
  26980. 0xBB, 0xB6, 0x27, 0xBD, 0xD7, 0x84, 0xB4, 0xDA,
  26981. 0xF0, 0xB1, 0xF6, 0x45, 0x94, 0x49, 0x42, 0xB4,
  26982. 0xC2, 0xB6, 0x22, 0x5C, 0x8B, 0x31, 0xE9, 0x89,
  26983. 0x54, 0x55, 0x22, 0xBA, 0x6F, 0x10, 0x39, 0x60,
  26984. 0x34, 0xCB, 0x1C, 0xA7, 0x45, 0x97, 0x78, 0x44,
  26985. 0xD5, 0x70, 0x89, 0x4C, 0x61, 0x1A, 0x56, 0x08,
  26986. 0xA7, 0x57, 0x41, 0x6D, 0x6D, 0xE5, 0x99, 0x63,
  26987. 0xC3, 0x27, 0x98, 0xC4, 0x93, 0xEF, 0xD2, 0x26,
  26988. 0x4C, 0x23, 0x19, 0x10, 0xE9, 0xA3, 0x00, 0x90,
  26989. 0xCA, 0x7B, 0x53, 0x84, 0xF2, 0x31, 0xB8, 0x9B,
  26990. 0xA6, 0x8A, 0x23, 0x81, 0x90, 0xEF, 0x1A, 0x2A,
  26991. 0x43, 0xCB, 0x01, 0x70, 0x34, 0x70, 0xA0, 0xF0,
  26992. 0x61, 0xA7, 0x07, 0x38, 0x94, 0x4B, 0xCD, 0x9B,
  26993. 0x70, 0x04, 0xF2, 0x47, 0x97, 0xAE, 0xCB, 0x88,
  26994. 0xB1, 0x09, 0x1C, 0xFE, 0xD0, 0x59, 0x0B, 0x04,
  26995. 0x15, 0x45, 0x3C, 0x39, 0xB6, 0xEC, 0x45, 0xB6,
  26996. 0x63, 0x05, 0xFA, 0xEA, 0x6B, 0x55, 0xA4, 0xB7,
  26997. 0x96, 0x75, 0x05, 0xFE, 0x38, 0x62, 0xA2, 0x67,
  26998. 0xAD, 0xBF, 0xE0, 0x5B, 0x91, 0x81, 0xA0, 0x65,
  26999. 0x01, 0x89, 0x33, 0x91, 0x65, 0x0E, 0xAA, 0xA4,
  27000. 0xA6, 0xD1, 0x68, 0x53, 0x34, 0x92, 0x76, 0xF9,
  27001. 0x8E, 0x0F, 0x44, 0xCD, 0x72, 0x66, 0x15, 0xC6,
  27002. 0x1C, 0x16, 0x71, 0x30, 0x94, 0xD8, 0xAB, 0x09,
  27003. 0x3C, 0xAC, 0x71, 0xF2, 0x80, 0x3E, 0x7D, 0x39,
  27004. 0x10, 0x9E, 0xF5, 0x00, 0x9C, 0x9C, 0x2C, 0xDA,
  27005. 0xF7, 0xB7, 0xA6, 0xB3, 0x7A, 0x33, 0xA4, 0x98,
  27006. 0x81, 0xF4, 0xBB, 0x5D, 0x72, 0x45, 0xA1, 0x4C,
  27007. 0x50, 0x42, 0x28, 0x0C, 0x76, 0xA8, 0x4E, 0x63,
  27008. 0xF4, 0x9D, 0x0D, 0x61, 0x9D, 0x46, 0xD7, 0x23,
  27009. 0xBA, 0xA7, 0x47, 0xA3, 0xBA, 0x90, 0xA6, 0xFB,
  27010. 0x63, 0x7A, 0x9A, 0x1D, 0xC0, 0x22, 0x68, 0xFD,
  27011. 0x5C, 0x04, 0x3D, 0x18, 0xCB, 0xA1, 0x52, 0x8A,
  27012. 0xC8, 0xE2, 0x25, 0xC1, 0xF9, 0x23, 0xD1, 0xCC,
  27013. 0x84, 0xF2, 0xE7, 0x8E, 0x25, 0xDC, 0x3C, 0xCE,
  27014. 0x93, 0x53, 0xC9, 0xDA, 0xC2, 0xAD, 0x72, 0x6A,
  27015. 0x79, 0xF6, 0x49, 0x40, 0x80, 0x1D, 0xD5, 0x70,
  27016. 0x1E, 0xFB, 0xDC, 0xB8, 0x0A, 0x98, 0xA2, 0x59,
  27017. 0x93, 0xCD, 0x7F, 0x80, 0x59, 0x13, 0x20, 0xB6,
  27018. 0x31, 0x72, 0x71, 0x86, 0x47, 0xB9, 0x76, 0xA9,
  27019. 0x8A, 0x77, 0x16, 0x86, 0xF0, 0x12, 0x0A, 0x05,
  27020. 0x3B, 0x0C, 0x44, 0x74, 0x60, 0x43, 0x05, 0x89,
  27021. 0x0F, 0xEC, 0xAF, 0x23, 0x47, 0x5D, 0xDC, 0xC1,
  27022. 0x1B, 0xC0, 0x8A, 0x9C, 0x5F, 0x59, 0x2A, 0xBB,
  27023. 0x1A, 0x15, 0x3D, 0xB1, 0xB8, 0x83, 0xC0, 0x50,
  27024. 0x7E, 0xB6, 0x8F, 0x78, 0xE0, 0xA1, 0x4D, 0xEB,
  27025. 0xBF, 0xEE, 0xC6, 0x21, 0xE1, 0x0A, 0x69, 0xB6,
  27026. 0xDA, 0xAF, 0xAA, 0x91, 0x6B, 0x53, 0x95, 0x33,
  27027. 0xE5, 0x08, 0x00, 0x7C, 0x41, 0x88, 0xCE, 0x05,
  27028. 0xC8, 0x62, 0xD1, 0x01, 0xD4, 0xDB, 0x1D, 0xF3,
  27029. 0xC4, 0x50, 0x2B, 0x8C, 0x8A, 0xE1, 0x45, 0x74,
  27030. 0x88, 0xA3, 0x6E, 0xAD, 0x26, 0x65, 0xBF, 0xAC,
  27031. 0xB3, 0x21, 0x76, 0x02, 0x81, 0xDB, 0x9C, 0xA7,
  27032. 0x2C, 0x76, 0x14, 0x36, 0x34, 0x04, 0xA0, 0xA8,
  27033. 0xEA, 0xBC, 0x05, 0x8A, 0x23, 0xA3, 0x46, 0x87,
  27034. 0x5F, 0xA9, 0x6B, 0xB1, 0x8A, 0xC2, 0xCC, 0xF0,
  27035. 0x93, 0xB8, 0xA8, 0x55, 0x67, 0x38, 0x11, 0xCE,
  27036. 0xD4, 0x7C, 0xBE, 0x1E, 0xE8, 0x1D, 0x2C, 0xF0,
  27037. 0x7E, 0x43, 0xFC, 0x48, 0x72, 0x09, 0x08, 0x53,
  27038. 0x74, 0x31, 0x08, 0x86, 0x5F, 0x02, 0xC5, 0x61,
  27039. 0x2A, 0xA8, 0x71, 0x66, 0x70, 0x7E, 0xE9, 0x0F,
  27040. 0xFD, 0x5B, 0x80, 0x21, 0xF0, 0xAA, 0x01, 0x6E,
  27041. 0x5D, 0xBC, 0xD9, 0x1F, 0x57, 0xB3, 0x56, 0x2D,
  27042. 0x3A, 0x2B, 0xCF, 0xA2, 0x0A, 0x4C, 0x03, 0x01,
  27043. 0x0B, 0x8A, 0xA1, 0x44, 0xE6, 0x48, 0x28, 0x04,
  27044. 0xB4, 0x74, 0xFE, 0xC1, 0xF5, 0xE1, 0x38, 0xBE,
  27045. 0x63, 0x2A, 0x3B, 0x9C, 0x82, 0x48, 0x3D, 0xC6,
  27046. 0x89, 0x0A, 0x13, 0xB1, 0xE8, 0xEE, 0x6A, 0xF7,
  27047. 0x14, 0xEC, 0x5E, 0xFA, 0xC3, 0xB1, 0x97, 0x6B,
  27048. 0x29, 0xDA, 0xDB, 0x60, 0x5B, 0x14, 0xD3, 0x73,
  27049. 0x2B, 0x5D, 0xE1, 0x18, 0x59, 0x65, 0x16, 0x85,
  27050. 0x81, 0x17, 0xE2, 0x63, 0x4C, 0x4E, 0xA0, 0xCC
  27051. };
  27052. static const byte c_768[KYBER768_CIPHER_TEXT_SIZE] = {
  27053. 0xDF, 0xA6, 0xB9, 0xD7, 0x2A, 0x63, 0xB4, 0x20,
  27054. 0xB8, 0x9D, 0xDE, 0x50, 0xF7, 0xE0, 0xD5, 0x6E,
  27055. 0xCF, 0x87, 0x6B, 0xFE, 0xF9, 0x91, 0xFC, 0xE9,
  27056. 0x1C, 0x8D, 0x28, 0x6F, 0xA6, 0xEA, 0xBA, 0xC1,
  27057. 0x73, 0x0F, 0xD8, 0x77, 0x41, 0xFE, 0x4A, 0xD7,
  27058. 0x17, 0xB2, 0x82, 0xA2, 0x1E, 0x23, 0x5A, 0x55,
  27059. 0xC3, 0x75, 0x7D, 0x88, 0xD4, 0xCE, 0x62, 0xF4,
  27060. 0x14, 0xEB, 0x77, 0xEB, 0x9D, 0x35, 0x7E, 0xE2,
  27061. 0x9D, 0x00, 0x08, 0x7B, 0xF8, 0x11, 0x0E, 0x5B,
  27062. 0xBB, 0xC7, 0xC9, 0x04, 0x19, 0x07, 0x2E, 0xAE,
  27063. 0x04, 0x4B, 0xF7, 0xE1, 0x83, 0xD4, 0x3A, 0x94,
  27064. 0xB2, 0x63, 0x2A, 0xA1, 0x46, 0x49, 0x61, 0x9B,
  27065. 0x70, 0x64, 0x95, 0x21, 0xBC, 0x19, 0x37, 0x09,
  27066. 0x42, 0xEF, 0x70, 0xF3, 0x6C, 0x34, 0xC8, 0xC2,
  27067. 0x35, 0x91, 0xEE, 0x0C, 0xA7, 0x1A, 0x12, 0xD2,
  27068. 0x79, 0xE0, 0xF5, 0x2D, 0x39, 0xED, 0x0F, 0x91,
  27069. 0x3F, 0x8C, 0x26, 0x26, 0x21, 0xFB, 0x24, 0x2E,
  27070. 0x68, 0x0D, 0xEB, 0x30, 0x7B, 0x07, 0x49, 0xC6,
  27071. 0xB3, 0x93, 0xA8, 0xEF, 0x66, 0xF8, 0xB0, 0x4A,
  27072. 0xAF, 0xA8, 0x77, 0xB9, 0x51, 0xAB, 0x93, 0xF5,
  27073. 0x98, 0xB4, 0xB2, 0xFA, 0xB0, 0x4F, 0x88, 0xAC,
  27074. 0x80, 0x39, 0x84, 0xFF, 0x37, 0xE3, 0xFE, 0x74,
  27075. 0xF3, 0xA6, 0x16, 0xD5, 0x31, 0x4E, 0xB3, 0xA8,
  27076. 0x26, 0xF8, 0x74, 0xF8, 0xEC, 0xD3, 0xA5, 0x64,
  27077. 0x7D, 0x04, 0x94, 0x2A, 0x57, 0xEF, 0xC0, 0x96,
  27078. 0x38, 0x47, 0x0D, 0xC0, 0xA9, 0xDF, 0x40, 0xB3,
  27079. 0x17, 0x57, 0x1D, 0x39, 0x84, 0xA7, 0x8C, 0xF7,
  27080. 0xD1, 0x17, 0x51, 0x09, 0x07, 0x22, 0xB3, 0x05,
  27081. 0x9E, 0x07, 0x59, 0x1C, 0xC4, 0xA2, 0xED, 0x9B,
  27082. 0xA0, 0xDC, 0xE9, 0x9B, 0xE9, 0xE5, 0xEE, 0x5D,
  27083. 0xB8, 0xD6, 0x98, 0xCD, 0xEB, 0x58, 0x14, 0x75,
  27084. 0x9B, 0xA9, 0x77, 0xC9, 0x00, 0x79, 0xCF, 0x2A,
  27085. 0xFD, 0xE4, 0x78, 0x06, 0x9C, 0x51, 0x3A, 0x60,
  27086. 0x09, 0x1A, 0x3A, 0x5D, 0x01, 0x11, 0xE2, 0x2D,
  27087. 0xE0, 0x6C, 0xB1, 0x45, 0xC1, 0x4E, 0x22, 0xA2,
  27088. 0x14, 0xCB, 0x27, 0x8C, 0x81, 0x52, 0xB0, 0x68,
  27089. 0x1B, 0xCA, 0xFF, 0x54, 0xD5, 0x52, 0xB5, 0x4A,
  27090. 0x67, 0x1C, 0x0D, 0xFE, 0xF7, 0x75, 0xE7, 0xC5,
  27091. 0x4F, 0xEF, 0xC4, 0x85, 0x38, 0x68, 0xC9, 0x55,
  27092. 0x97, 0x1A, 0xBD, 0xAC, 0x2A, 0x76, 0x29, 0x2C,
  27093. 0xCC, 0xD4, 0xFD, 0x1C, 0x70, 0x6B, 0x7D, 0x36,
  27094. 0x14, 0x15, 0x96, 0x73, 0xE9, 0xD7, 0xB2, 0x9A,
  27095. 0x2D, 0x3F, 0x63, 0x36, 0x31, 0x29, 0xE7, 0xA2,
  27096. 0x1E, 0x80, 0x3A, 0x46, 0x0F, 0x27, 0x14, 0xE3,
  27097. 0xE2, 0x59, 0x22, 0x78, 0x0A, 0xF3, 0x82, 0x57,
  27098. 0xCD, 0x14, 0x95, 0xAC, 0xD1, 0xE0, 0x19, 0x80,
  27099. 0x63, 0x8D, 0xF5, 0x8A, 0x15, 0x3D, 0xAB, 0x07,
  27100. 0xEF, 0xB5, 0xC7, 0xE7, 0x8A, 0xDA, 0xCF, 0x63,
  27101. 0x19, 0x56, 0xD6, 0x9C, 0xCD, 0xA0, 0x70, 0x45,
  27102. 0x95, 0x68, 0xBD, 0x9D, 0x11, 0xA2, 0x93, 0x4B,
  27103. 0xCF, 0x16, 0x43, 0xBC, 0x99, 0x46, 0x82, 0x38,
  27104. 0x91, 0x0B, 0x1F, 0x74, 0x2E, 0xBB, 0x3C, 0x03,
  27105. 0xD3, 0x9F, 0xD4, 0x5C, 0xFB, 0x85, 0xBA, 0x30,
  27106. 0x9E, 0x29, 0xDD, 0x9B, 0x5C, 0xD5, 0x60, 0x81,
  27107. 0x9E, 0xC7, 0x29, 0xFC, 0xAC, 0x8B, 0x9D, 0x72,
  27108. 0x5E, 0x3E, 0x8A, 0xBE, 0xDE, 0x4B, 0x52, 0x98,
  27109. 0xA8, 0x65, 0x8E, 0xE3, 0xF7, 0x81, 0xB0, 0xCE,
  27110. 0x68, 0x3C, 0xBB, 0x73, 0x35, 0xCD, 0x57, 0xEF,
  27111. 0xE2, 0x20, 0x4A, 0x8F, 0x19, 0x74, 0x46, 0xD7,
  27112. 0x31, 0x4C, 0xDB, 0xF4, 0xC5, 0xD0, 0x8C, 0xCC,
  27113. 0x41, 0xF8, 0x08, 0x57, 0xCC, 0x95, 0x71, 0xFB,
  27114. 0xFB, 0x90, 0x60, 0x60, 0xF7, 0xE1, 0x7C, 0x8C,
  27115. 0xEF, 0x0F, 0x27, 0x4A, 0xFF, 0x83, 0xE3, 0x93,
  27116. 0xB1, 0x5F, 0x2F, 0x95, 0x89, 0xA1, 0x3A, 0xF4,
  27117. 0xBC, 0x78, 0xE1, 0x6C, 0xDD, 0xE6, 0x23, 0x61,
  27118. 0xD6, 0x3B, 0x8D, 0xC9, 0x03, 0xB7, 0x0C, 0x01,
  27119. 0xA4, 0x34, 0x19, 0xCD, 0x20, 0x52, 0x15, 0x0B,
  27120. 0xD2, 0x87, 0x19, 0xF6, 0x1F, 0xF3, 0x1F, 0x4A,
  27121. 0x9B, 0xEC, 0x4D, 0xDB, 0xCE, 0xC1, 0xF8, 0xFB,
  27122. 0x2E, 0xFB, 0xF3, 0x7D, 0xFF, 0xFA, 0x4C, 0x7F,
  27123. 0xEC, 0xA8, 0xCE, 0x6D, 0x62, 0x6B, 0xFD, 0xA1,
  27124. 0x6E, 0xE7, 0x08, 0xD9, 0x20, 0x68, 0x14, 0xA2,
  27125. 0xEF, 0x98, 0x85, 0x25, 0x61, 0x5D, 0x4A, 0xC9,
  27126. 0xBE, 0x60, 0x8C, 0x4B, 0x03, 0xAB, 0xEE, 0x95,
  27127. 0xB3, 0x2A, 0x5D, 0xB7, 0x4A, 0x96, 0x11, 0x9A,
  27128. 0x7E, 0x15, 0x9A, 0xF9, 0x9C, 0xD9, 0x8E, 0x88,
  27129. 0xEA, 0xF0, 0x9F, 0x0D, 0x78, 0x0E, 0x7C, 0x7E,
  27130. 0x81, 0x4B, 0x8E, 0x88, 0xB4, 0xF4, 0xE1, 0x5F,
  27131. 0xA5, 0x49, 0x95, 0xD0, 0xEC, 0xBA, 0xD3, 0xEF,
  27132. 0x04, 0x6A, 0x49, 0x47, 0xF3, 0xE8, 0xB9, 0xE7,
  27133. 0x44, 0x24, 0x14, 0x89, 0xB8, 0x06, 0xFE, 0x94,
  27134. 0x01, 0xE7, 0x8B, 0xAF, 0xC8, 0xE8, 0x82, 0xE9,
  27135. 0xD6, 0xD0, 0x70, 0x0F, 0x72, 0x0C, 0x00, 0x24,
  27136. 0xE7, 0xDA, 0x49, 0x06, 0x1C, 0x5D, 0x18, 0xA6,
  27137. 0x20, 0x74, 0x04, 0x0A, 0xBC, 0x00, 0x03, 0x20,
  27138. 0x0E, 0xD4, 0x65, 0x23, 0x17, 0x97, 0x93, 0x0A,
  27139. 0x2E, 0x2A, 0xA5, 0x01, 0xF6, 0x48, 0x62, 0xDD,
  27140. 0xA1, 0x30, 0x14, 0xA9, 0x9F, 0x9D, 0x32, 0x70,
  27141. 0xAA, 0x90, 0x7E, 0xEB, 0x3F, 0xDB, 0xFF, 0x29,
  27142. 0x16, 0x00, 0xDF, 0x1F, 0x6B, 0x39, 0x68, 0x4B,
  27143. 0x11, 0xE3, 0x96, 0xB7, 0x0D, 0x86, 0xF9, 0x04,
  27144. 0x92, 0xE8, 0x2B, 0x09, 0xBA, 0x25, 0x60, 0x7B,
  27145. 0x0C, 0x28, 0x6F, 0xBC, 0x07, 0x01, 0x82, 0xAC,
  27146. 0x76, 0xFA, 0x7C, 0x85, 0x9A, 0xAF, 0xEA, 0x87,
  27147. 0x01, 0x6A, 0xED, 0x22, 0xC3, 0x60, 0x5A, 0x27,
  27148. 0x89, 0xA1, 0xD4, 0x39, 0xFD, 0x8D, 0x93, 0x33,
  27149. 0x42, 0xDA, 0xB7, 0x45, 0xA3, 0xE5, 0x50, 0xE7,
  27150. 0xD7, 0x7C, 0x01, 0xA6, 0x23, 0x4B, 0xDA, 0x7D,
  27151. 0x6B, 0xB1, 0x9D, 0x49, 0x5E, 0x65, 0x60, 0xFC,
  27152. 0xE8, 0x39, 0x6F, 0xC3, 0xC6, 0xE0, 0x88, 0xED,
  27153. 0x60, 0xF5, 0xF2, 0x77, 0x14, 0x16, 0xEA, 0x3B,
  27154. 0xE5, 0xBE, 0x47, 0x2B, 0x64, 0x04, 0x90, 0x6C,
  27155. 0x91, 0xE7, 0x1D, 0x9A, 0x86, 0x72, 0xF3, 0x90,
  27156. 0x08, 0x36, 0x55, 0xAB, 0x7D, 0x0E, 0xC6, 0xED,
  27157. 0xFE, 0x86, 0x78, 0x9C, 0xE2, 0x0B, 0xE2, 0xEA,
  27158. 0x90, 0xCA, 0x5C, 0xC3, 0x14, 0x16, 0xFB, 0x24,
  27159. 0xCB, 0xAF, 0x94, 0xDA, 0x14, 0x68, 0xFE, 0x69,
  27160. 0x6B, 0xCD, 0xF5, 0x24, 0x7C, 0xF1, 0x17, 0xCB,
  27161. 0xE9, 0x33, 0x40, 0x76, 0xCA, 0x68, 0x96, 0xB2,
  27162. 0xF6, 0xA0, 0x16, 0xB1, 0xF7, 0xC7, 0x37, 0x28,
  27163. 0x80, 0x78, 0x98, 0xD8, 0xB1, 0x99, 0x75, 0x6C,
  27164. 0x2B, 0x0A, 0xA2, 0x45, 0x7E, 0x1B, 0x4F, 0x77,
  27165. 0x54, 0xC4, 0x57, 0x6C, 0xE5, 0x64, 0x56, 0x14,
  27166. 0xEA, 0x15, 0xC1, 0xAE, 0x28, 0xB0, 0x94, 0xEB,
  27167. 0x21, 0x7C, 0x7A, 0x7A, 0x41, 0x23, 0x95, 0x76,
  27168. 0xCB, 0xDA, 0x38, 0x0E, 0xE6, 0x87, 0x83, 0x43,
  27169. 0x27, 0x30, 0xAD, 0x5E, 0xBE, 0x7F, 0x51, 0xD6,
  27170. 0xBE, 0x7F, 0xB0, 0x2A, 0xB3, 0x7B, 0xE0, 0xC9,
  27171. 0x6A, 0xAC, 0x9F, 0x3C, 0x79, 0x0A, 0x18, 0xD1,
  27172. 0x59, 0xE6, 0xBA, 0xBA, 0x71, 0xEC, 0x88, 0xC1,
  27173. 0x10, 0xFD, 0x84, 0xC3, 0x36, 0xDF, 0x63, 0x0F,
  27174. 0x27, 0x1C, 0xF7, 0x93, 0x28, 0xB6, 0xC8, 0x79,
  27175. 0xDF, 0x7C, 0xDE, 0x0F, 0x70, 0x71, 0x22, 0x20,
  27176. 0xB1, 0xFB, 0xB9, 0xAC, 0xB4, 0x82, 0x48, 0xD9,
  27177. 0x1F, 0x0E, 0x2B, 0x6E, 0x3B, 0xE4, 0x0C, 0x2B,
  27178. 0x22, 0x1E, 0x62, 0x6E, 0x7E, 0x33, 0x0D, 0x9D,
  27179. 0x83, 0xCC, 0x06, 0x68, 0xF7, 0x30, 0x85, 0x91,
  27180. 0xE1, 0x4C, 0x7D, 0x72, 0xB8, 0x41, 0xA6, 0xF0,
  27181. 0x5F, 0x3F, 0xDC, 0x13, 0x9E, 0xEC, 0xC1, 0x53,
  27182. 0x67, 0x65, 0x65, 0x0B, 0x55, 0xA9, 0xCE, 0xC6,
  27183. 0xBB, 0xF5, 0x4C, 0xCE, 0xC5, 0xC3, 0xAC, 0x9A,
  27184. 0x0E, 0x39, 0xF4, 0x8F, 0x23, 0x7B, 0xD4, 0xC6,
  27185. 0x60, 0xCB, 0x1A, 0x8D, 0x25, 0x0B, 0xB6, 0xC8,
  27186. 0xC0, 0x10, 0xFE, 0xC3, 0x4C, 0xC3, 0xD9, 0x15,
  27187. 0x99, 0x27, 0x1C, 0x75, 0x31, 0x33, 0x0F, 0x12,
  27188. 0xA3, 0xE4, 0x4F, 0xAF, 0xD9, 0x05, 0xD2, 0xC6
  27189. };
  27190. static const byte kprime_768[KYBER_SS_SZ] = {
  27191. 0xBD, 0x72, 0x56, 0xB2, 0x42, 0xF4, 0x04, 0x86,
  27192. 0x9D, 0x66, 0x2F, 0x80, 0xBF, 0x67, 0x7A, 0x16,
  27193. 0xC0, 0xC6, 0xFC, 0x15, 0x68, 0xCC, 0xA5, 0xB6,
  27194. 0x45, 0x82, 0xA0, 0x1A, 0x6A, 0x14, 0x2D, 0x71
  27195. };
  27196. #endif
  27197. #ifndef WOLFSSL_NO_KYBER1024
  27198. static const byte dk_1024[KYBER1024_PRIVATE_KEY_SIZE] = {
  27199. 0x0F, 0xEA, 0x26, 0xC4, 0xA5, 0x44, 0xA5, 0x14,
  27200. 0x44, 0x4A, 0x97, 0x1B, 0x5C, 0x5A, 0x82, 0x58,
  27201. 0x27, 0xC0, 0x9D, 0x42, 0x46, 0x9E, 0x59, 0x34,
  27202. 0x4C, 0xF2, 0xAC, 0x06, 0xA2, 0x8D, 0x33, 0xE9,
  27203. 0xA0, 0x12, 0xCA, 0xA3, 0x71, 0x7B, 0x2C, 0x3B,
  27204. 0x29, 0x0A, 0x07, 0x15, 0x82, 0x11, 0x09, 0xC4,
  27205. 0xCC, 0xEA, 0xC4, 0x9F, 0x34, 0x1D, 0xAD, 0xD3,
  27206. 0x77, 0xD4, 0x2A, 0x37, 0x26, 0x19, 0x16, 0xAC,
  27207. 0x7B, 0xB9, 0xE4, 0x1C, 0x09, 0x6C, 0xA8, 0x18,
  27208. 0x1C, 0xF5, 0x83, 0x50, 0x57, 0x3F, 0x60, 0x56,
  27209. 0x84, 0xA1, 0xBC, 0xA5, 0x3D, 0x88, 0x25, 0x74,
  27210. 0x53, 0xC5, 0x35, 0x16, 0x5C, 0x4E, 0xD7, 0x2A,
  27211. 0x9F, 0xF0, 0x56, 0x45, 0x71, 0x29, 0x01, 0xF6,
  27212. 0x6C, 0x10, 0xD0, 0x4F, 0x5E, 0xB4, 0xA2, 0xEC,
  27213. 0x37, 0x72, 0xE9, 0x49, 0x8E, 0x9D, 0xC4, 0x4B,
  27214. 0xBD, 0xAB, 0x71, 0xBB, 0xDB, 0xBC, 0xFC, 0x85,
  27215. 0xB8, 0x01, 0x36, 0x30, 0x89, 0xEA, 0x60, 0xEF,
  27216. 0xE5, 0x86, 0xE1, 0xE2, 0x18, 0x0C, 0x38, 0xB2,
  27217. 0xE7, 0xB4, 0xA6, 0x3E, 0xD6, 0x07, 0x49, 0x0B,
  27218. 0xC5, 0xBA, 0x7A, 0x58, 0xAC, 0x3B, 0x1C, 0x0E,
  27219. 0x43, 0x96, 0x72, 0x00, 0xC7, 0x98, 0x02, 0x90,
  27220. 0xEB, 0xF4, 0x11, 0x82, 0x84, 0x39, 0xEE, 0x8C,
  27221. 0x8E, 0x61, 0x29, 0xB2, 0x58, 0xE1, 0x3D, 0x12,
  27222. 0x7C, 0xB1, 0x5A, 0x00, 0xCB, 0x7B, 0x46, 0x8D,
  27223. 0x40, 0x23, 0xB5, 0x09, 0x7B, 0x9B, 0x2E, 0x50,
  27224. 0x9B, 0x50, 0xE8, 0x90, 0xB6, 0x3B, 0x47, 0x07,
  27225. 0x48, 0x79, 0x61, 0xA2, 0x9E, 0x18, 0x65, 0x6D,
  27226. 0xD2, 0xD0, 0x9E, 0x6A, 0x3B, 0x88, 0x43, 0xE2,
  27227. 0x84, 0x3C, 0xB4, 0x85, 0x4F, 0x18, 0x11, 0x6E,
  27228. 0x71, 0x7D, 0xDB, 0x03, 0x55, 0xA7, 0x51, 0x35,
  27229. 0xB2, 0x02, 0x6A, 0x75, 0x2C, 0x8E, 0x7F, 0xF1,
  27230. 0x8E, 0x0F, 0x4A, 0x39, 0x1C, 0xA3, 0x7F, 0x5B,
  27231. 0x2B, 0xCC, 0x88, 0xC9, 0x99, 0xB4, 0xE4, 0x77,
  27232. 0x50, 0xC4, 0x65, 0x47, 0xEC, 0x07, 0x6A, 0xC2,
  27233. 0x15, 0x30, 0x72, 0x2C, 0xFA, 0xF9, 0x67, 0x99,
  27234. 0x61, 0xC9, 0x86, 0x88, 0xC3, 0x56, 0x2B, 0x17,
  27235. 0xCC, 0x80, 0x81, 0x46, 0xA1, 0x25, 0x72, 0xC9,
  27236. 0xB5, 0xFF, 0x15, 0x1A, 0xAB, 0x54, 0x41, 0x09,
  27237. 0x01, 0x84, 0x0E, 0x26, 0x42, 0x39, 0x87, 0xC5,
  27238. 0xE0, 0xD2, 0x8E, 0xF2, 0xEA, 0x53, 0xEA, 0xE5,
  27239. 0x95, 0x1E, 0x62, 0xAC, 0x7B, 0xD5, 0x18, 0xB9,
  27240. 0x83, 0x0A, 0x4D, 0xBC, 0xCE, 0x6A, 0x93, 0x65,
  27241. 0x91, 0xEA, 0x8E, 0xF2, 0x75, 0x07, 0x8A, 0x09,
  27242. 0x73, 0x85, 0x2A, 0x4D, 0x13, 0x04, 0x95, 0xD0,
  27243. 0x0B, 0x3F, 0x21, 0x85, 0x15, 0x99, 0x90, 0x1C,
  27244. 0xFD, 0xF9, 0x36, 0x83, 0x44, 0xC8, 0x10, 0x42,
  27245. 0x2F, 0xFE, 0xA0, 0x8A, 0xED, 0xCB, 0x1A, 0x7F,
  27246. 0xD3, 0x62, 0x5F, 0x26, 0xB0, 0x34, 0x81, 0x2F,
  27247. 0xA3, 0x07, 0xAB, 0x2C, 0x20, 0x94, 0x54, 0x65,
  27248. 0x54, 0x6D, 0x31, 0xA3, 0x41, 0xA4, 0x01, 0x3D,
  27249. 0x81, 0x89, 0xB4, 0xF5, 0x0F, 0xE8, 0x60, 0xA6,
  27250. 0x68, 0xDA, 0xC7, 0xB1, 0x03, 0x44, 0x1E, 0x96,
  27251. 0x1F, 0xCE, 0xB0, 0xC5, 0xB1, 0xF3, 0x4D, 0xF2,
  27252. 0xE5, 0x98, 0xC6, 0xD8, 0xCF, 0x60, 0xB8, 0x64,
  27253. 0x15, 0x0C, 0x70, 0x3D, 0x2B, 0xBE, 0xAC, 0x9B,
  27254. 0x00, 0x1A, 0xA2, 0x10, 0x81, 0x47, 0xAE, 0x6B,
  27255. 0x8A, 0xAE, 0x2C, 0x77, 0x91, 0xDB, 0xE9, 0x56,
  27256. 0xC1, 0xF9, 0xB2, 0x04, 0x7A, 0x15, 0x76, 0x09,
  27257. 0x43, 0x87, 0x06, 0x4C, 0x3A, 0x80, 0x1B, 0x0D,
  27258. 0x89, 0xC9, 0x96, 0xA5, 0xCF, 0xA3, 0xB0, 0x12,
  27259. 0xC1, 0x44, 0x38, 0xB9, 0xF3, 0x53, 0x0C, 0x0C,
  27260. 0x5F, 0xA9, 0x38, 0x9F, 0x10, 0xFB, 0x3E, 0xF1,
  27261. 0xE2, 0x01, 0x33, 0x38, 0x41, 0x5F, 0x7B, 0x1D,
  27262. 0xB4, 0x11, 0xAD, 0xF9, 0x1C, 0x73, 0xB6, 0x45,
  27263. 0x6B, 0x68, 0xAB, 0x7C, 0xFC, 0x7B, 0xC9, 0x29,
  27264. 0xE4, 0x4E, 0x58, 0xEB, 0x34, 0xCA, 0x10, 0xAE,
  27265. 0x31, 0xF0, 0x3B, 0x2C, 0x3B, 0xA6, 0xCC, 0xA2,
  27266. 0x7E, 0xB3, 0x5C, 0xB1, 0x37, 0x9A, 0x13, 0x0A,
  27267. 0xAC, 0x87, 0xE3, 0xB8, 0x75, 0xCF, 0xE2, 0x53,
  27268. 0xAF, 0x03, 0xC4, 0xBD, 0x78, 0x3F, 0x18, 0xC5,
  27269. 0xA2, 0xF8, 0x49, 0x2B, 0xBF, 0x7C, 0x56, 0x87,
  27270. 0x55, 0x98, 0xB1, 0xB6, 0x3F, 0xE6, 0xCB, 0x06,
  27271. 0x94, 0xD0, 0x48, 0x0C, 0xA1, 0xC8, 0xF8, 0x86,
  27272. 0x7C, 0x11, 0xB8, 0xBF, 0x33, 0xA3, 0x2C, 0x20,
  27273. 0xB7, 0x9F, 0x9C, 0xA4, 0x86, 0x85, 0x86, 0x10,
  27274. 0xB1, 0x97, 0x83, 0xBE, 0xF7, 0x84, 0xBF, 0x6B,
  27275. 0x0F, 0x85, 0x8C, 0x1A, 0x79, 0x11, 0x30, 0xDA,
  27276. 0x69, 0x57, 0xF2, 0x12, 0x23, 0x4E, 0xC9, 0x86,
  27277. 0x79, 0x81, 0x4B, 0xE8, 0x39, 0xBF, 0x11, 0x0B,
  27278. 0x45, 0xC1, 0xC8, 0x83, 0xEC, 0xDC, 0x3D, 0xB3,
  27279. 0xF8, 0x22, 0xA4, 0xF7, 0xC1, 0x25, 0x56, 0x6E,
  27280. 0xD1, 0x66, 0x35, 0x68, 0xC8, 0x41, 0x3C, 0xD0,
  27281. 0x1C, 0x22, 0x46, 0x7A, 0xD5, 0x20, 0x1A, 0x0A,
  27282. 0xDC, 0x76, 0x34, 0x35, 0xA2, 0xCB, 0x05, 0xCD,
  27283. 0xC4, 0x70, 0x72, 0xA9, 0x43, 0x70, 0xF5, 0xB4,
  27284. 0x34, 0xF7, 0x5C, 0x07, 0x8B, 0x41, 0x59, 0x93,
  27285. 0xE8, 0x54, 0xDD, 0xE1, 0x7B, 0xBF, 0x86, 0xC0,
  27286. 0xC6, 0xC9, 0xA3, 0x24, 0x85, 0x32, 0xD9, 0xC2,
  27287. 0x13, 0x9E, 0xF3, 0xC7, 0x5A, 0x9B, 0xC6, 0x93,
  27288. 0x78, 0x10, 0x60, 0xDC, 0xAE, 0x2F, 0xFA, 0x58,
  27289. 0xD9, 0xCC, 0x54, 0x8F, 0x19, 0xC1, 0xCE, 0x53,
  27290. 0x64, 0x88, 0x0C, 0x7F, 0xB5, 0x0C, 0xC7, 0xBE,
  27291. 0x40, 0x53, 0x12, 0xD6, 0xCC, 0x94, 0x03, 0x76,
  27292. 0x18, 0xF3, 0x88, 0xC4, 0x90, 0xAF, 0x8F, 0x61,
  27293. 0xB9, 0xB4, 0x04, 0x4C, 0xF7, 0x5A, 0x5C, 0xD7,
  27294. 0x1A, 0x15, 0x85, 0x3B, 0x5F, 0xD6, 0x22, 0x4C,
  27295. 0x6B, 0x95, 0x90, 0xE5, 0x85, 0x01, 0xD2, 0x81,
  27296. 0x42, 0x00, 0xC9, 0x19, 0xF2, 0x83, 0xCC, 0x2B,
  27297. 0x49, 0xAD, 0x8B, 0xFA, 0x5B, 0xAA, 0xA2, 0x97,
  27298. 0x7F, 0x03, 0x82, 0x3F, 0x60, 0x9E, 0xFB, 0x24,
  27299. 0x26, 0xF9, 0x36, 0xC3, 0x02, 0x87, 0x09, 0x7B,
  27300. 0xD6, 0xB7, 0xBD, 0xC6, 0x78, 0x62, 0x85, 0x88,
  27301. 0x83, 0xDB, 0x59, 0x54, 0x08, 0x04, 0x29, 0xB9,
  27302. 0xCD, 0x02, 0xCA, 0x96, 0xBC, 0x1C, 0xCB, 0xDB,
  27303. 0x51, 0x21, 0xDF, 0xF8, 0x05, 0xB0, 0x82, 0x4A,
  27304. 0xEE, 0x99, 0x9E, 0x2B, 0xBB, 0x2D, 0x82, 0x35,
  27305. 0x3E, 0x6D, 0x3A, 0x30, 0x07, 0x92, 0x78, 0x10,
  27306. 0x58, 0xC5, 0x6E, 0xF7, 0x09, 0x8A, 0xB3, 0x58,
  27307. 0x4E, 0xA0, 0x62, 0x1E, 0x20, 0x33, 0x7D, 0x3A,
  27308. 0x97, 0x5D, 0x93, 0xCF, 0x32, 0x58, 0x6D, 0x6A,
  27309. 0x71, 0xA2, 0xC4, 0xBB, 0xB2, 0x02, 0xB8, 0x53,
  27310. 0xFF, 0x09, 0xC4, 0x07, 0xB4, 0x3B, 0x1C, 0x19,
  27311. 0xB1, 0xC4, 0xCC, 0xB8, 0x21, 0x48, 0x2D, 0xDD,
  27312. 0x27, 0x37, 0x81, 0x77, 0xAA, 0x7F, 0x61, 0x78,
  27313. 0x49, 0x7C, 0x3F, 0xBA, 0x79, 0x71, 0x53, 0x84,
  27314. 0x8C, 0x5D, 0x0B, 0x1F, 0x40, 0xB5, 0x4E, 0x9D,
  27315. 0x51, 0x93, 0x90, 0x4A, 0x30, 0x3F, 0x72, 0x5F,
  27316. 0x0C, 0xCC, 0x66, 0xC6, 0xCC, 0xB1, 0x58, 0x85,
  27317. 0x06, 0x05, 0x34, 0x6D, 0xB4, 0x2B, 0x87, 0x7D,
  27318. 0xD9, 0xCE, 0xA5, 0xF6, 0x9C, 0x12, 0xB2, 0x21,
  27319. 0xC7, 0xEC, 0x51, 0x00, 0xF7, 0x65, 0x87, 0xB9,
  27320. 0x83, 0x4B, 0xC0, 0xC6, 0x41, 0x53, 0x8F, 0x83,
  27321. 0xE8, 0x5B, 0xB3, 0x09, 0x0D, 0xBA, 0xFB, 0xCB,
  27322. 0x0B, 0x71, 0x18, 0xFF, 0x7C, 0x97, 0xE9, 0x52,
  27323. 0x63, 0x15, 0x70, 0x41, 0xF8, 0xAC, 0x40, 0x52,
  27324. 0xD0, 0x40, 0x35, 0x00, 0xCC, 0x4F, 0x68, 0x94,
  27325. 0x55, 0x97, 0x4C, 0xEB, 0x5B, 0x07, 0x67, 0x90,
  27326. 0xA0, 0x50, 0xE0, 0xB3, 0xF6, 0x77, 0x2A, 0x77,
  27327. 0x67, 0x54, 0x1F, 0xF6, 0xB6, 0x7B, 0x2A, 0x1D,
  27328. 0x54, 0x07, 0x82, 0x06, 0x47, 0x68, 0x8F, 0x36,
  27329. 0x0A, 0x2B, 0x01, 0x47, 0x37, 0x67, 0x71, 0x29,
  27330. 0x09, 0xB2, 0x27, 0x65, 0x8B, 0xE6, 0x45, 0x78,
  27331. 0x48, 0xC4, 0x40, 0x75, 0x71, 0x68, 0x06, 0x18,
  27332. 0x88, 0x58, 0x9C, 0xB0, 0x5A, 0x99, 0x9E, 0x55,
  27333. 0x49, 0x67, 0x91, 0xB1, 0x1A, 0xF2, 0x06, 0x6B,
  27334. 0xB8, 0xCA, 0x74, 0x60, 0x51, 0xC4, 0x68, 0x0A,
  27335. 0x0B, 0xC0, 0x73, 0x82, 0x41, 0x2A, 0xB8, 0xB8,
  27336. 0xA3, 0x19, 0xDB, 0xC7, 0x94, 0xDD, 0xC6, 0x94,
  27337. 0xBF, 0xDB, 0x81, 0x3F, 0x80, 0xB5, 0x8B, 0x72,
  27338. 0x21, 0x8D, 0xD6, 0x4D, 0xFC, 0xDB, 0xA1, 0xAB,
  27339. 0x48, 0xA9, 0x4F, 0x7A, 0x8D, 0xCA, 0x92, 0x66,
  27340. 0xCD, 0x15, 0xA4, 0x2D, 0x9B, 0xA5, 0xFB, 0x67,
  27341. 0x67, 0xA9, 0x55, 0x52, 0x6C, 0x05, 0x0D, 0xE2,
  27342. 0x59, 0x8B, 0x11, 0x2A, 0x2B, 0x10, 0x3A, 0xA2,
  27343. 0xD1, 0xF0, 0x60, 0x6F, 0xE6, 0x8A, 0x55, 0x19,
  27344. 0x1E, 0xF5, 0x3B, 0x30, 0x2F, 0x7C, 0x19, 0x22,
  27345. 0xC3, 0x01, 0xCE, 0xEA, 0x98, 0x9A, 0x62, 0x13,
  27346. 0x40, 0x90, 0xA8, 0x60, 0x76, 0x77, 0x6F, 0xA4,
  27347. 0x46, 0x27, 0xB7, 0x31, 0x63, 0x86, 0x57, 0x6A,
  27348. 0x67, 0x81, 0x75, 0xB2, 0x18, 0xE6, 0xF4, 0x82,
  27349. 0xB5, 0x2B, 0xC6, 0x02, 0x7B, 0xBE, 0xB3, 0x46,
  27350. 0x98, 0xB9, 0x80, 0x2F, 0xD6, 0x76, 0x34, 0xC1,
  27351. 0xA9, 0x4D, 0xD4, 0xC5, 0xCD, 0x49, 0xEC, 0x6E,
  27352. 0x2D, 0x66, 0x5F, 0x72, 0x77, 0x81, 0xD1, 0xEC,
  27353. 0x10, 0xAA, 0xF6, 0x6A, 0xD8, 0x27, 0x9B, 0x9B,
  27354. 0xF2, 0x4C, 0x99, 0xE8, 0x75, 0xEC, 0x94, 0x35,
  27355. 0x2D, 0x96, 0x05, 0xFA, 0x30, 0xCB, 0x3D, 0x8B,
  27356. 0x26, 0x86, 0xB0, 0x39, 0x71, 0xA7, 0x60, 0xB3,
  27357. 0x05, 0x3B, 0x34, 0x34, 0x6D, 0x0D, 0x71, 0xB4,
  27358. 0x4D, 0x8B, 0x7D, 0x2E, 0xA6, 0x1A, 0x5C, 0x10,
  27359. 0xA9, 0x33, 0xD3, 0x8B, 0xA4, 0x83, 0x36, 0x71,
  27360. 0x11, 0x74, 0x54, 0x61, 0x47, 0xD4, 0x4B, 0x29,
  27361. 0x14, 0xF8, 0x56, 0x89, 0xD9, 0xC1, 0xBF, 0x00,
  27362. 0x37, 0xC7, 0xF7, 0x37, 0x7C, 0xD9, 0x30, 0xCF,
  27363. 0xF6, 0x0F, 0x84, 0xB0, 0xA2, 0x00, 0x5D, 0x3E,
  27364. 0xFE, 0x55, 0xC7, 0x31, 0x1B, 0x1B, 0x61, 0x32,
  27365. 0x76, 0x8B, 0x52, 0x90, 0xD8, 0x36, 0xB8, 0x2B,
  27366. 0xC4, 0x43, 0xC3, 0x2B, 0x4F, 0xEC, 0x96, 0x02,
  27367. 0x19, 0xDB, 0x21, 0x32, 0xF7, 0x99, 0x0A, 0xD6,
  27368. 0x84, 0xA3, 0x72, 0x9F, 0x3D, 0x1A, 0x2C, 0xEA,
  27369. 0x3A, 0x1F, 0xE4, 0xB1, 0x26, 0x75, 0xC4, 0x89,
  27370. 0xEF, 0x33, 0x19, 0x8F, 0x01, 0xA1, 0x06, 0x80,
  27371. 0x6E, 0xFC, 0xE8, 0x92, 0x1D, 0xC4, 0x6E, 0x97,
  27372. 0x1C, 0x0A, 0x0A, 0x56, 0x4A, 0xF9, 0xE5, 0x6C,
  27373. 0xA7, 0x27, 0xA7, 0x64, 0x1C, 0x56, 0x8C, 0x95,
  27374. 0xAA, 0x59, 0x56, 0x91, 0x0B, 0x28, 0x84, 0x29,
  27375. 0xF8, 0x0E, 0xE7, 0x22, 0x6E, 0x9D, 0xC4, 0x06,
  27376. 0x7E, 0x34, 0x94, 0x4F, 0x06, 0x92, 0x6D, 0x44,
  27377. 0xB2, 0xCF, 0x87, 0x64, 0xF7, 0x13, 0x59, 0x3B,
  27378. 0x44, 0x29, 0xF8, 0x2B, 0x8F, 0xCC, 0x60, 0x77,
  27379. 0x98, 0x91, 0x6B, 0x81, 0x5B, 0x90, 0x98, 0x33,
  27380. 0x0E, 0xC3, 0x34, 0x29, 0x0D, 0xB8, 0xC0, 0x4B,
  27381. 0x08, 0x3D, 0xF3, 0xCA, 0x10, 0xCE, 0x35, 0x75,
  27382. 0x07, 0x30, 0x28, 0xE9, 0x94, 0xA2, 0x5B, 0xE7,
  27383. 0x28, 0x78, 0x49, 0x2F, 0xE1, 0xB6, 0x96, 0xBA,
  27384. 0x5C, 0xB1, 0xA7, 0x73, 0x19, 0x3A, 0x3B, 0x28,
  27385. 0xA4, 0xF4, 0x40, 0xAE, 0x58, 0x2D, 0xC7, 0xC2,
  27386. 0x4F, 0xE7, 0x45, 0x1D, 0x66, 0x76, 0x23, 0x2B,
  27387. 0xB9, 0x61, 0xC5, 0x04, 0x0C, 0x9E, 0x52, 0x01,
  27388. 0xAA, 0xF3, 0xCD, 0x4D, 0xE4, 0x0A, 0xD5, 0xA9,
  27389. 0x57, 0x8A, 0xF5, 0x28, 0x10, 0xB5, 0x93, 0xE9,
  27390. 0x81, 0x5E, 0x23, 0xF6, 0x3F, 0x56, 0x40, 0x61,
  27391. 0xA4, 0x84, 0x07, 0x21, 0x3A, 0xA1, 0xB0, 0x90,
  27392. 0x8F, 0x4B, 0x17, 0x4F, 0x86, 0xD5, 0x73, 0xFA,
  27393. 0x04, 0x38, 0x64, 0x98, 0xBE, 0x68, 0x39, 0x8E,
  27394. 0x8D, 0x72, 0x0D, 0x27, 0x81, 0x11, 0xD8, 0xB1,
  27395. 0x73, 0x03, 0x60, 0x2A, 0x96, 0xE3, 0x5F, 0x56,
  27396. 0xFB, 0x25, 0x17, 0x3C, 0x4F, 0x4A, 0x03, 0xCA,
  27397. 0x2A, 0xC9, 0xBF, 0x79, 0xDC, 0xAB, 0x76, 0x4B,
  27398. 0xCE, 0x44, 0x10, 0x40, 0x1E, 0x10, 0x13, 0xE6,
  27399. 0x52, 0x8C, 0xCC, 0x51, 0x13, 0x35, 0x85, 0x77,
  27400. 0xDA, 0x83, 0x75, 0xE0, 0x23, 0x43, 0x10, 0x8C,
  27401. 0x29, 0x24, 0xD2, 0x55, 0x1E, 0x5C, 0xC5, 0xA1,
  27402. 0xB0, 0x4D, 0xEF, 0x88, 0x32, 0x4D, 0x85, 0x4F,
  27403. 0xC9, 0x2C, 0x4A, 0xDF, 0x7C, 0x23, 0x01, 0x33,
  27404. 0x7E, 0x45, 0x20, 0xBF, 0xC3, 0x65, 0x56, 0x6F,
  27405. 0x66, 0x09, 0x2E, 0x36, 0x7A, 0xE6, 0x06, 0x12,
  27406. 0x74, 0x46, 0x53, 0xC1, 0xEB, 0x47, 0xF0, 0x82,
  27407. 0x09, 0x51, 0xA2, 0xA1, 0x4C, 0x42, 0x59, 0x09,
  27408. 0x34, 0x0D, 0x87, 0x27, 0x18, 0x8E, 0xAA, 0x08,
  27409. 0xE4, 0x86, 0x78, 0x98, 0x48, 0x76, 0xD0, 0x00,
  27410. 0x8D, 0xAE, 0x99, 0x01, 0x5B, 0x36, 0x63, 0xFD,
  27411. 0xCB, 0x72, 0x57, 0x41, 0x53, 0x0B, 0xC3, 0x89,
  27412. 0x5B, 0x11, 0x62, 0x0C, 0xE3, 0xB4, 0x17, 0xA3,
  27413. 0x20, 0xE1, 0x88, 0x13, 0xB9, 0x9C, 0x23, 0x5A,
  27414. 0xC0, 0x6F, 0x55, 0x60, 0x0F, 0x98, 0x38, 0x82,
  27415. 0xBF, 0xF0, 0x02, 0x36, 0x10, 0x7B, 0x50, 0x42,
  27416. 0x54, 0x5B, 0x6B, 0x77, 0x58, 0x68, 0xAE, 0xFB,
  27417. 0x79, 0xB5, 0x95, 0x59, 0x69, 0x02, 0xC6, 0x9B,
  27418. 0x9E, 0xCA, 0x3D, 0x35, 0x8C, 0x61, 0xFE, 0xE0,
  27419. 0x36, 0xD2, 0x18, 0xAC, 0x43, 0xBA, 0x3F, 0x52,
  27420. 0xC0, 0x6A, 0x8F, 0x88, 0x1A, 0x7E, 0xD7, 0x03,
  27421. 0x86, 0x14, 0x2C, 0xBA, 0xC5, 0xCC, 0x04, 0xFC,
  27422. 0xC3, 0x1E, 0x16, 0x27, 0x76, 0x51, 0xCE, 0x2D,
  27423. 0xCC, 0x50, 0x14, 0xF6, 0xBA, 0x5A, 0x91, 0x5C,
  27424. 0x13, 0x38, 0x83, 0x4E, 0xF4, 0x74, 0xB6, 0x71,
  27425. 0x59, 0x13, 0xBC, 0x7A, 0x4E, 0x59, 0x3C, 0x68,
  27426. 0x87, 0x66, 0xAD, 0xD7, 0x06, 0x98, 0xB3, 0x7E,
  27427. 0x06, 0xE5, 0x39, 0x15, 0xF3, 0x85, 0x38, 0x8C,
  27428. 0x25, 0xC4, 0x26, 0x5E, 0x1C, 0xB4, 0x4F, 0xE3,
  27429. 0xD0, 0x19, 0xD1, 0x21, 0xAE, 0x4C, 0x32, 0x43,
  27430. 0x4F, 0x37, 0xB0, 0xA4, 0xCB, 0x69, 0xC7, 0xCC,
  27431. 0x95, 0x70, 0x73, 0x50, 0xC3, 0x49, 0x3D, 0x0F,
  27432. 0xB1, 0x1C, 0xD4, 0xD0, 0x9F, 0x29, 0xDC, 0x56,
  27433. 0xC0, 0x7B, 0xC8, 0xEB, 0x0B, 0xD0, 0x08, 0x2B,
  27434. 0x41, 0x44, 0x21, 0x45, 0x66, 0x3C, 0x21, 0xAB,
  27435. 0x43, 0x34, 0x67, 0xB9, 0x5E, 0xC2, 0x47, 0x84,
  27436. 0x23, 0xC1, 0x8B, 0xF2, 0xEC, 0x70, 0x3E, 0xFB,
  27437. 0xA2, 0x8C, 0xDA, 0xBD, 0x42, 0xB7, 0xB8, 0x33,
  27438. 0x15, 0x0D, 0x6D, 0xA2, 0x5E, 0xB0, 0x0A, 0x83,
  27439. 0x28, 0x90, 0x2E, 0x2D, 0x08, 0x9B, 0x55, 0xD6,
  27440. 0x9A, 0xAD, 0x9A, 0x94, 0xD8, 0x18, 0x26, 0x4C,
  27441. 0x54, 0xB0, 0x4D, 0x61, 0x4D, 0x14, 0x7A, 0x30,
  27442. 0xAB, 0xFC, 0x03, 0xD9, 0x92, 0x9D, 0x96, 0xBA,
  27443. 0x7F, 0x81, 0x86, 0x5D, 0xA3, 0x53, 0xC4, 0x54,
  27444. 0xBA, 0x7A, 0xA7, 0x88, 0x1A, 0xB9, 0x74, 0xC1,
  27445. 0xB8, 0xF0, 0x83, 0x1E, 0x79, 0xC4, 0x41, 0x86,
  27446. 0x64, 0xE9, 0x53, 0xA5, 0x4D, 0xE9, 0x32, 0x13,
  27447. 0x69, 0x72, 0x81, 0x34, 0x1D, 0x37, 0xF5, 0x08,
  27448. 0xE8, 0xCB, 0xAE, 0x3D, 0x81, 0x85, 0x05, 0x45,
  27449. 0x67, 0xDE, 0xFC, 0x8E, 0x3B, 0xBC, 0xAA, 0x42,
  27450. 0x47, 0x90, 0x7C, 0x48, 0x3B, 0x8F, 0x1B, 0x84,
  27451. 0xB3, 0x24, 0xC1, 0xA7, 0xCA, 0x84, 0x42, 0xDB,
  27452. 0x6B, 0x7B, 0x12, 0x8C, 0x83, 0x13, 0xBE, 0x1F,
  27453. 0xE2, 0x57, 0x91, 0x20, 0x9B, 0x86, 0x4A, 0x3E,
  27454. 0x1A, 0x61, 0x8D, 0x56, 0xD7, 0x10, 0xD6, 0xF3,
  27455. 0xBF, 0x55, 0x95, 0x10, 0x16, 0x7C, 0x46, 0x4C,
  27456. 0x6B, 0x9B, 0x8B, 0xC4, 0x90, 0xB8, 0xE0, 0x39,
  27457. 0x25, 0xD0, 0x3D, 0x0E, 0xEB, 0x5D, 0x78, 0x17,
  27458. 0x94, 0x28, 0xBB, 0x80, 0xD3, 0xFB, 0x14, 0x88,
  27459. 0x40, 0x70, 0x9C, 0x41, 0x14, 0x7A, 0x68, 0x6F,
  27460. 0xC9, 0xBC, 0xBD, 0xCD, 0xF7, 0xC7, 0xEA, 0x7C,
  27461. 0x30, 0xFB, 0x64, 0x0F, 0xF0, 0x5B, 0x75, 0x39,
  27462. 0xAB, 0xAB, 0x70, 0x89, 0x29, 0x08, 0xE9, 0x3C,
  27463. 0xC9, 0xC3, 0x47, 0xF8, 0xAC, 0x88, 0x9E, 0x56,
  27464. 0x46, 0x8A, 0x13, 0x5B, 0x99, 0x75, 0x47, 0x38,
  27465. 0xE1, 0x5F, 0x4E, 0x67, 0x7D, 0xF3, 0x75, 0xBF,
  27466. 0x1B, 0x43, 0x60, 0x6A, 0x2C, 0x47, 0x38, 0x0B,
  27467. 0x10, 0xA0, 0xC1, 0x4C, 0x28, 0x58, 0x3C, 0x83,
  27468. 0x31, 0x1A, 0x28, 0x54, 0xB2, 0xA9, 0x93, 0x1F,
  27469. 0xD6, 0x60, 0x86, 0xC1, 0x07, 0x49, 0xF3, 0x34,
  27470. 0x57, 0x7F, 0xD7, 0x0B, 0x51, 0xB9, 0x50, 0x60,
  27471. 0x07, 0x51, 0x99, 0x31, 0x9B, 0x3F, 0x7C, 0xB5,
  27472. 0xB2, 0x37, 0x30, 0x2C, 0x37, 0x0A, 0x23, 0x17,
  27473. 0x5E, 0x4E, 0x01, 0x3C, 0x56, 0x28, 0x1B, 0xAF,
  27474. 0xE2, 0xBE, 0x9F, 0x82, 0x5A, 0x30, 0x66, 0xAB,
  27475. 0x8B, 0xBA, 0x57, 0x93, 0xE2, 0x1E, 0x7A, 0x48,
  27476. 0x97, 0x8C, 0xF6, 0x0C, 0x09, 0x1B, 0x1F, 0x80,
  27477. 0xC0, 0xC2, 0x38, 0x14, 0xA3, 0x0F, 0x77, 0x60,
  27478. 0x60, 0x1A, 0xCE, 0xAB, 0xB1, 0x21, 0x52, 0x00,
  27479. 0x94, 0x0F, 0xFA, 0x15, 0x22, 0x72, 0x09, 0x6D,
  27480. 0x45, 0x8D, 0x00, 0xDD, 0x03, 0x9F, 0x23, 0x6B,
  27481. 0x27, 0x27, 0xB5, 0x88, 0xC6, 0x22, 0x04, 0xE7,
  27482. 0x9C, 0x45, 0x16, 0x81, 0xDF, 0xE4, 0x10, 0xEE,
  27483. 0xC4, 0x2B, 0x74, 0x94, 0x5A, 0xEC, 0x03, 0x13,
  27484. 0xA3, 0x91, 0x94, 0x2A, 0xE1, 0xB1, 0x22, 0x17,
  27485. 0x4D, 0xBE, 0x59, 0xAB, 0x1E, 0x39, 0x0C, 0xD6,
  27486. 0x49, 0x41, 0x43, 0x6C, 0x75, 0xA9, 0x32, 0x3C,
  27487. 0x69, 0xA6, 0x41, 0x88, 0x08, 0x70, 0xFB, 0xB2,
  27488. 0x80, 0xB3, 0xB3, 0x7B, 0x3B, 0xD9, 0x82, 0xB8,
  27489. 0x29, 0x55, 0x62, 0x0B, 0x07, 0x83, 0xB8, 0x2E,
  27490. 0x89, 0x61, 0xA4, 0x04, 0x3B, 0xC7, 0xF6, 0x6C,
  27491. 0x0E, 0xF2, 0x5A, 0x5E, 0xD1, 0x53, 0x26, 0xF8,
  27492. 0x81, 0x6E, 0x5E, 0xA4, 0x16, 0x7E, 0xE8, 0xBF,
  27493. 0x66, 0x66, 0x45, 0x1D, 0x31, 0x5B, 0x2C, 0x75,
  27494. 0x14, 0x41, 0x17, 0x2C, 0x27, 0x83, 0x00, 0x26,
  27495. 0x82, 0x61, 0xC7, 0x8C, 0x6F, 0x0C, 0x46, 0x56,
  27496. 0x27, 0x79, 0xB3, 0xA1, 0x19, 0x6F, 0x87, 0x83,
  27497. 0x5F, 0x79, 0xFC, 0xB7, 0xE0, 0xCB, 0xA1, 0x53,
  27498. 0x36, 0xCC, 0x83, 0xE1, 0x56, 0xC5, 0x02, 0x28,
  27499. 0x87, 0xA8, 0x09, 0x86, 0xB4, 0x9C, 0x1B, 0x57,
  27500. 0x65, 0x94, 0xA2, 0x31, 0x42, 0x62, 0x4A, 0xBF,
  27501. 0x52, 0x48, 0x22, 0x41, 0x8C, 0x61, 0x01, 0x90,
  27502. 0x52, 0x62, 0x80, 0x65, 0x72, 0x49, 0x4D, 0x37,
  27503. 0x53, 0xC0, 0x62, 0x81, 0xE7, 0xF1, 0x7E, 0x0D,
  27504. 0x79, 0x6C, 0xD7, 0x76, 0x7F, 0xDC, 0xE9, 0x01,
  27505. 0xFE, 0x17, 0x12, 0xA0, 0x0A, 0x3D, 0x36, 0xEB,
  27506. 0x42, 0x3E, 0x29, 0x86, 0x88, 0x46, 0x93, 0x2A,
  27507. 0x94, 0x31, 0xB8, 0xCA, 0x66, 0x0F, 0xC1, 0x97,
  27508. 0x5E, 0x23, 0xA7, 0x5B, 0x4A, 0x51, 0xDE, 0x10,
  27509. 0x69, 0xD3, 0xA5, 0x9F, 0x6E, 0xEB, 0x2A, 0x5C,
  27510. 0xE7, 0x2A, 0x89, 0x16, 0xB5, 0xE8, 0x63, 0x47,
  27511. 0x6E, 0x6A, 0xC5, 0x72, 0x92, 0x9F, 0x2C, 0x29,
  27512. 0xBC, 0x56, 0x27, 0xBA, 0x99, 0x41, 0x63, 0xCE,
  27513. 0xD3, 0x5A, 0xB7, 0x03, 0x1C, 0x00, 0x49, 0x07,
  27514. 0x24, 0x55, 0x5A, 0xCD, 0xE6, 0x13, 0xAE, 0xB4,
  27515. 0xC3, 0xE9, 0x99, 0x81, 0xC6, 0x2B, 0x5D, 0xC6,
  27516. 0xA9, 0xB3, 0x5B, 0xA7, 0x92, 0x20, 0x24, 0x36,
  27517. 0x89, 0xE0, 0x59, 0x49, 0x96, 0x85, 0x7C, 0x04,
  27518. 0x5D, 0x67, 0x19, 0x3D, 0x9E, 0x41, 0x1B, 0x4F,
  27519. 0xF3, 0x9D, 0x0F, 0x8C, 0x3C, 0x0A, 0x70, 0xAD,
  27520. 0xB7, 0x2A, 0x70, 0x21, 0xE3, 0x6D, 0x64, 0xFB,
  27521. 0x29, 0x4D, 0x93, 0x2B, 0x24, 0xE1, 0xA2, 0xBC,
  27522. 0x0B, 0xC4, 0x1C, 0x4A, 0xA3, 0xB5, 0xEC, 0x3C,
  27523. 0xF0, 0xE6, 0x72, 0xDE, 0x14, 0x0F, 0x48, 0x47,
  27524. 0x33, 0xFD, 0x82, 0xBF, 0x08, 0x29, 0x34, 0xB5,
  27525. 0x40, 0xA6, 0x35, 0xC4, 0x48, 0x98, 0xE8, 0xAB,
  27526. 0x8E, 0x06, 0x45, 0x70, 0x5A, 0xA5, 0x81, 0x71,
  27527. 0x8B, 0x41, 0x32, 0xC4, 0x27, 0x92, 0x7F, 0xAE,
  27528. 0x75, 0xBF, 0x96, 0x16, 0xA5, 0x42, 0x4C, 0x20,
  27529. 0x20, 0xEB, 0xC5, 0xCF, 0xC1, 0xBC, 0x0E, 0xD1,
  27530. 0x65, 0x3A, 0xE5, 0x00, 0x5A, 0x17, 0x54, 0x18,
  27531. 0x16, 0x20, 0xB7, 0xF0, 0x6D, 0x71, 0x63, 0x13,
  27532. 0x03, 0x3B, 0xB7, 0x2A, 0x40, 0x64, 0x7A, 0xDB,
  27533. 0x2E, 0x66, 0x73, 0x70, 0xF2, 0xC7, 0x4F, 0xDB,
  27534. 0x94, 0x42, 0x0D, 0xA4, 0x8D, 0xD1, 0x37, 0x9D,
  27535. 0xBA, 0x59, 0xAA, 0x22, 0xF8, 0x57, 0xE2, 0x31,
  27536. 0xC5, 0xC0, 0x83, 0x29, 0x00, 0x66, 0xC5, 0x48,
  27537. 0x76, 0x1B, 0xDF, 0x38, 0x5F, 0x2F, 0x85, 0x81,
  27538. 0x7B, 0x21, 0x20, 0x66, 0xD3, 0x9F, 0x03, 0xB7,
  27539. 0x7F, 0x8E, 0xF4, 0x12, 0x19, 0xE4, 0xBF, 0xB9,
  27540. 0xC1, 0x2E, 0x4F, 0xC9, 0x88, 0x00, 0x57, 0x1D,
  27541. 0x22, 0x3A, 0xA9, 0x2A, 0x32, 0xC7, 0xA3, 0xC2,
  27542. 0xA7, 0xCF, 0x9C, 0x99, 0x5A, 0xE0, 0xA7, 0xB5,
  27543. 0x93, 0x91, 0xFE, 0x9A, 0x4F, 0x0D, 0x63, 0x3B,
  27544. 0xFB, 0x79, 0x8C, 0x34, 0xB7, 0x2B, 0xBA, 0x6A,
  27545. 0x9F, 0x16, 0xC4, 0x13, 0x2E, 0x88, 0xB5, 0x70,
  27546. 0x75, 0x8B, 0xD5, 0x51, 0xC9, 0x1B, 0xD2, 0xAD,
  27547. 0xEB, 0x53, 0xA7, 0x2A, 0xC6, 0xAA, 0x03, 0x68,
  27548. 0x9D, 0xD6, 0x4B, 0x03, 0x57, 0x09, 0xA8, 0xAF,
  27549. 0x46, 0x85, 0x43, 0xCB, 0x17, 0x36, 0xDB, 0xC9,
  27550. 0xC7, 0x2B, 0x52, 0x9E, 0x70, 0x59, 0x6D, 0x18,
  27551. 0xB1, 0x9C, 0xA6, 0x8E, 0x61, 0x7A, 0x14, 0x7C,
  27552. 0x18, 0x9D, 0x28, 0x3A, 0x77, 0x68, 0x8C, 0xAF,
  27553. 0x94, 0xDA, 0x5A, 0x0E, 0x9B, 0x63, 0x18, 0x1A,
  27554. 0x40, 0xBB, 0xE7, 0xBD, 0x41, 0x68, 0xA2, 0x4D,
  27555. 0x27, 0x43, 0x19, 0xA9, 0x93, 0xBC, 0xEA, 0x8A,
  27556. 0xBF, 0x50, 0x5F, 0xE8, 0x62, 0x12, 0x96, 0x92,
  27557. 0xB5, 0xBD, 0xE8, 0x49, 0xF3, 0x6A, 0xC9, 0x2F,
  27558. 0x71, 0x71, 0xE5, 0x38, 0x59, 0x31, 0x36, 0x04,
  27559. 0xEA, 0xC1, 0x0B, 0xE2, 0x78, 0x6F, 0xF3, 0x85,
  27560. 0xB9, 0xC7, 0x18, 0x15, 0x48, 0x18, 0x77, 0x2F,
  27561. 0xA7, 0xB8, 0x99, 0xC0, 0x4E, 0xFD, 0x18, 0xA8,
  27562. 0x01, 0x9A, 0x79, 0xB6, 0xF6, 0x4D, 0x5B, 0x9A,
  27563. 0x2C, 0x55, 0xE7, 0x84, 0xCB, 0x47, 0xCA, 0x29,
  27564. 0x48, 0x56, 0x68, 0x9A, 0xA6, 0xA7, 0x0C, 0xC2,
  27565. 0x7B, 0x6C, 0x20, 0xD4, 0xD1, 0xC7, 0x29, 0xC4,
  27566. 0x09, 0xD0, 0xB9, 0x25, 0xC4, 0x0C, 0x30, 0xC0,
  27567. 0x77, 0x78, 0x15, 0x07, 0x77, 0x49, 0x48, 0x8B,
  27568. 0x8D, 0xF0, 0x39, 0x06, 0x95, 0xAB, 0xDB, 0x04,
  27569. 0x8C, 0x7C, 0xE1, 0x85, 0x36, 0x02, 0xA5, 0x4D,
  27570. 0x15, 0x3C, 0xF2, 0xA5, 0x16, 0x17, 0x84, 0x7B,
  27571. 0x11, 0xE6, 0x3C, 0x4C, 0x76, 0x19, 0x66, 0xD5,
  27572. 0xAD, 0x93, 0x35, 0x0D, 0xBA, 0xDA, 0x4A, 0x15,
  27573. 0xC1, 0x24, 0xBD, 0x80, 0x88, 0x71, 0x99, 0x3F,
  27574. 0xC7, 0x75, 0xB6, 0xE4, 0x10, 0xC3, 0x86, 0x59,
  27575. 0x0F, 0x73, 0x0A, 0x8E, 0xC9, 0x47, 0x5E, 0xEE,
  27576. 0x91, 0x50, 0x39, 0xE9, 0x1B, 0x6F, 0xE4, 0x25,
  27577. 0xB9, 0x06, 0x68, 0xC6, 0xAC, 0x52, 0x58, 0xB7,
  27578. 0xAF, 0x10, 0x3B, 0x9F, 0x5E, 0x23, 0x0B, 0x71,
  27579. 0x9B, 0xBB, 0x09, 0x87, 0x1D, 0xC1, 0x62, 0x15,
  27580. 0x17, 0xBA, 0x2A, 0x83, 0x9C, 0x96, 0xAA, 0xA6,
  27581. 0x44, 0x0A, 0x87, 0x5E, 0xAC, 0x90, 0xB2, 0x98,
  27582. 0xD6, 0x1B, 0xD3, 0xF3, 0xAC, 0x89, 0xB4, 0x05,
  27583. 0xDB, 0x39, 0x42, 0x32, 0x68, 0x6A, 0x2B, 0xE0,
  27584. 0xF3, 0xC7, 0x5F, 0x15, 0xE6, 0x4E, 0x61, 0xF0,
  27585. 0x70, 0x79, 0x1E, 0xB4, 0xBB, 0x97, 0xB7, 0x01,
  27586. 0x98, 0x25, 0xF1, 0x17, 0xC7, 0xD7, 0x3A, 0x12,
  27587. 0xFD, 0x3D, 0xCC, 0x22, 0xD5, 0x81, 0xB0, 0xE4,
  27588. 0x1B, 0x78, 0x63, 0x74, 0xA4, 0x61, 0xEA, 0x0D,
  27589. 0x88, 0xDA, 0xA8, 0x9B, 0x65, 0x9F, 0x0D, 0xC8,
  27590. 0x24, 0x43, 0x42, 0x35, 0x15, 0xB6, 0x33, 0xB0,
  27591. 0x05, 0xC9, 0x58, 0xEC, 0x26, 0x56, 0x1B, 0x6D,
  27592. 0xB8, 0x18, 0xF4, 0xB8, 0xCB, 0x2E, 0x28, 0x99,
  27593. 0x0E, 0x74, 0x84, 0x17, 0x58, 0x7F, 0xEC, 0x38,
  27594. 0xA1, 0x28, 0x4B, 0xBB, 0x4F, 0xF9, 0xE4, 0x78
  27595. };
  27596. static const byte c_1024[KYBER1024_CIPHER_TEXT_SIZE] = {
  27597. 0x61, 0xFF, 0x1A, 0x8B, 0x61, 0x17, 0xEF, 0x11,
  27598. 0x83, 0x28, 0xE8, 0x8B, 0x32, 0x27, 0x99, 0x30,
  27599. 0x14, 0xDC, 0xD0, 0x75, 0xB8, 0xA1, 0xA7, 0xF9,
  27600. 0x80, 0x18, 0x93, 0xEE, 0xE6, 0x40, 0x5B, 0xB9,
  27601. 0x60, 0xB6, 0xB7, 0xF6, 0xA1, 0xA2, 0x75, 0x18,
  27602. 0xA3, 0x40, 0x91, 0x39, 0xA4, 0x8B, 0x85, 0x96,
  27603. 0x81, 0xCC, 0x75, 0x8F, 0x2B, 0xCC, 0x3E, 0xEF,
  27604. 0xB0, 0x43, 0x94, 0xA3, 0x75, 0xA5, 0xCD, 0x71,
  27605. 0x31, 0x64, 0x90, 0x93, 0x8A, 0xBF, 0xD1, 0x94,
  27606. 0xB2, 0x0B, 0xCD, 0x31, 0xB3, 0x98, 0x02, 0x61,
  27607. 0xC9, 0xED, 0x69, 0xBF, 0x9B, 0x1D, 0x7D, 0x76,
  27608. 0x59, 0xA8, 0x04, 0x0D, 0xB1, 0xE2, 0x5D, 0x2B,
  27609. 0xA6, 0xF7, 0x03, 0x48, 0x66, 0x24, 0xB7, 0x3C,
  27610. 0xAC, 0xDC, 0xA2, 0x7D, 0xB0, 0xF7, 0xE2, 0x40,
  27611. 0x8C, 0x94, 0x48, 0xE3, 0x88, 0x73, 0x28, 0x0F,
  27612. 0x5E, 0x99, 0x50, 0xD7, 0xCC, 0xE2, 0x52, 0xA6,
  27613. 0x47, 0x58, 0x0C, 0x19, 0x90, 0x4F, 0xAD, 0x62,
  27614. 0xAE, 0xC3, 0x00, 0xBC, 0x8E, 0x38, 0xF0, 0x59,
  27615. 0x48, 0xB6, 0x3B, 0xAD, 0x5C, 0xE7, 0xC9, 0x0E,
  27616. 0x40, 0xC4, 0xBC, 0x65, 0x11, 0x77, 0x61, 0xF5,
  27617. 0xF8, 0x86, 0x8F, 0x80, 0x25, 0xD6, 0xCE, 0xB2,
  27618. 0xC5, 0xDF, 0x60, 0xDE, 0x38, 0xC3, 0x23, 0x29,
  27619. 0x22, 0x08, 0x7E, 0xFC, 0xF2, 0xCD, 0x95, 0xDE,
  27620. 0x5E, 0x87, 0xB6, 0x88, 0x8B, 0x88, 0xC8, 0x6C,
  27621. 0xC7, 0x83, 0x15, 0x58, 0x5B, 0x2C, 0xC6, 0x88,
  27622. 0xA7, 0x1B, 0x47, 0x7B, 0xFA, 0x38, 0x8D, 0xC2,
  27623. 0x33, 0x4D, 0xFA, 0x8A, 0xA9, 0x55, 0x03, 0xD5,
  27624. 0x39, 0x7E, 0x2A, 0xE0, 0x35, 0x29, 0x03, 0xEA,
  27625. 0x6A, 0x0A, 0xE8, 0xB6, 0x49, 0xA9, 0x14, 0xB3,
  27626. 0x52, 0x5F, 0xE5, 0x8F, 0x56, 0x4B, 0xF1, 0x9C,
  27627. 0xC0, 0x9F, 0x54, 0xE1, 0x05, 0xD1, 0x9B, 0xD8,
  27628. 0x10, 0x54, 0xE5, 0x70, 0x01, 0xF7, 0x0B, 0xBD,
  27629. 0xD7, 0x71, 0x94, 0x49, 0x68, 0x7E, 0x9A, 0x53,
  27630. 0xB1, 0x6C, 0xA5, 0x36, 0x6A, 0x19, 0x10, 0x5A,
  27631. 0x8B, 0xA0, 0x85, 0x89, 0xAD, 0x08, 0xDF, 0x13,
  27632. 0x00, 0xEF, 0x4F, 0x92, 0x3B, 0xA9, 0xE7, 0x62,
  27633. 0xA8, 0x2F, 0xB0, 0x9B, 0x76, 0xE1, 0x25, 0xF2,
  27634. 0xF2, 0x74, 0xD6, 0x17, 0xBF, 0x30, 0xEA, 0xB4,
  27635. 0x65, 0xEC, 0xF2, 0x4D, 0x37, 0x07, 0xAD, 0x30,
  27636. 0x0D, 0x9A, 0xFC, 0x1C, 0xF1, 0xDC, 0x40, 0xEE,
  27637. 0x7D, 0x4E, 0xEA, 0x6D, 0x15, 0x0E, 0x6F, 0x0A,
  27638. 0x31, 0xDB, 0x9F, 0x8F, 0x92, 0xBA, 0x8E, 0xEE,
  27639. 0xB3, 0x5D, 0x74, 0x45, 0x58, 0x9B, 0x04, 0x6B,
  27640. 0xA7, 0x9E, 0xFE, 0x23, 0x11, 0x06, 0xCF, 0x0A,
  27641. 0x75, 0x71, 0x2A, 0xB3, 0x92, 0x72, 0x4C, 0x53,
  27642. 0xEF, 0xF9, 0xF5, 0x73, 0x3B, 0xEE, 0x0D, 0x6A,
  27643. 0x44, 0xD0, 0xB6, 0xF5, 0x15, 0xD0, 0xF5, 0xE4,
  27644. 0x0B, 0x1B, 0x1E, 0x17, 0xE6, 0x7A, 0xED, 0x3C,
  27645. 0x81, 0xD0, 0x0A, 0xC4, 0x68, 0xA2, 0x8F, 0x84,
  27646. 0x53, 0xD4, 0xB0, 0xDA, 0x80, 0x9E, 0x57, 0xD8,
  27647. 0x23, 0xF2, 0x8D, 0x61, 0xED, 0x0B, 0x59, 0xA0,
  27648. 0x8C, 0x62, 0x29, 0x72, 0xD9, 0x91, 0x79, 0xDA,
  27649. 0x86, 0x36, 0xC4, 0x5F, 0x1C, 0xE8, 0xF6, 0x25,
  27650. 0x2A, 0xC8, 0x6D, 0x91, 0xB5, 0xE9, 0x29, 0x97,
  27651. 0x01, 0x4E, 0x3F, 0x50, 0x89, 0xE6, 0x8B, 0xC5,
  27652. 0x2C, 0xED, 0x5D, 0xAE, 0x6D, 0x5B, 0x17, 0x5F,
  27653. 0xE2, 0xD6, 0x19, 0x28, 0x46, 0x50, 0x59, 0x72,
  27654. 0x4C, 0x83, 0x59, 0x02, 0xD7, 0x61, 0x2C, 0xDB,
  27655. 0x69, 0xCD, 0xAC, 0x66, 0x4F, 0xC1, 0xC9, 0xCB,
  27656. 0x11, 0x20, 0x3A, 0x8C, 0x7B, 0x71, 0x48, 0x6E,
  27657. 0x97, 0xB7, 0xD1, 0xBC, 0x6A, 0x98, 0xF4, 0x93,
  27658. 0xDC, 0xBE, 0xC8, 0xE6, 0x29, 0x55, 0x8E, 0xD3,
  27659. 0x61, 0x09, 0x12, 0x93, 0xD1, 0xB5, 0xD2, 0x09,
  27660. 0x6C, 0xEB, 0x9F, 0xC7, 0xAF, 0xEE, 0x71, 0xDB,
  27661. 0x7C, 0xCF, 0xE4, 0x82, 0xB6, 0x8A, 0x19, 0x64,
  27662. 0x29, 0xFF, 0x04, 0xD1, 0x59, 0x03, 0xE7, 0xA7,
  27663. 0x5C, 0x7B, 0xB5, 0xF6, 0x22, 0xC3, 0x69, 0x71,
  27664. 0x69, 0x45, 0x59, 0xFF, 0x07, 0xDF, 0xAA, 0x79,
  27665. 0xE4, 0x1C, 0x36, 0x2B, 0x22, 0x64, 0x3C, 0xD3,
  27666. 0x9B, 0xD9, 0xE1, 0xD3, 0xD6, 0xC2, 0xA3, 0x06,
  27667. 0xB5, 0xF1, 0x10, 0x2C, 0x26, 0x6E, 0xEE, 0x67,
  27668. 0xDC, 0xDA, 0xCF, 0x36, 0x69, 0x7A, 0x83, 0x6F,
  27669. 0x20, 0x38, 0x38, 0xEC, 0x11, 0x03, 0x08, 0xC9,
  27670. 0x0A, 0x3D, 0x01, 0x57, 0x0C, 0xB3, 0x66, 0x8A,
  27671. 0xBA, 0x50, 0x34, 0x0E, 0x40, 0xF5, 0x4C, 0xFA,
  27672. 0x6A, 0x9E, 0x88, 0x62, 0x53, 0x2F, 0x5F, 0x19,
  27673. 0x84, 0x8A, 0xA1, 0x1F, 0xD3, 0x4F, 0xC8, 0x6B,
  27674. 0x7F, 0xCB, 0x16, 0x37, 0xF4, 0xE5, 0xA1, 0xD0,
  27675. 0x3A, 0xFC, 0xE4, 0x41, 0x24, 0xE4, 0xE4, 0x60,
  27676. 0xB8, 0x4C, 0x63, 0x49, 0x6A, 0xDE, 0xD5, 0x58,
  27677. 0x01, 0xDF, 0x25, 0x17, 0xA9, 0x0A, 0xB0, 0x61,
  27678. 0xC8, 0xE6, 0x3A, 0xB6, 0xB1, 0x4B, 0xE1, 0x69,
  27679. 0x4D, 0x6F, 0x38, 0x9D, 0xD8, 0x5F, 0x56, 0x39,
  27680. 0xC5, 0x78, 0x3A, 0xFC, 0xA0, 0x14, 0x6E, 0x6A,
  27681. 0x1E, 0xB0, 0xC4, 0x05, 0x63, 0xC1, 0x37, 0x01,
  27682. 0x0D, 0xB6, 0x0B, 0xBC, 0x3D, 0x63, 0x74, 0xD6,
  27683. 0xF3, 0xA8, 0x92, 0xDE, 0xBC, 0x06, 0x47, 0x01,
  27684. 0xC6, 0x4B, 0xEC, 0xCB, 0x8E, 0x2C, 0x33, 0xB7,
  27685. 0x40, 0xCC, 0x7E, 0xD4, 0x9D, 0x10, 0x8A, 0x8C,
  27686. 0x46, 0x56, 0x81, 0x8D, 0xF5, 0xF7, 0xD9, 0x1E,
  27687. 0xAA, 0xA4, 0x46, 0xAC, 0x6C, 0xCD, 0xE3, 0x0C,
  27688. 0x6D, 0x3D, 0x1B, 0xF6, 0x6E, 0x4E, 0x3B, 0x7B,
  27689. 0x6B, 0x81, 0xE3, 0xCB, 0x17, 0x22, 0x7F, 0x80,
  27690. 0xDB, 0x00, 0x96, 0xE6, 0xBE, 0x7D, 0x85, 0x9C,
  27691. 0x09, 0x71, 0x37, 0x49, 0xFC, 0xA2, 0x15, 0x30,
  27692. 0xFE, 0x1A, 0x71, 0x6E, 0xBE, 0x32, 0x55, 0x04,
  27693. 0x31, 0x9B, 0xD0, 0xEA, 0x2A, 0x7D, 0x77, 0x13,
  27694. 0x60, 0x7C, 0xB6, 0x79, 0xB0, 0xA0, 0xB2, 0x26,
  27695. 0x8D, 0x49, 0x3B, 0x67, 0xC0, 0x48, 0x18, 0x72,
  27696. 0x17, 0x7F, 0xFD, 0x25, 0x93, 0xF3, 0xAC, 0xF6,
  27697. 0x91, 0xCE, 0xE9, 0x9A, 0x36, 0xEC, 0xA7, 0x22,
  27698. 0x57, 0x9E, 0xFA, 0xA5, 0x9A, 0xCC, 0x59, 0xEF,
  27699. 0x8C, 0xEA, 0x91, 0x08, 0xE6, 0x20, 0xB0, 0x60,
  27700. 0x56, 0xC1, 0x9D, 0x3C, 0x1E, 0xB9, 0x1E, 0x86,
  27701. 0x34, 0xDE, 0x49, 0x57, 0x70, 0x6D, 0xFA, 0x8F,
  27702. 0x9D, 0x0A, 0x9E, 0x0C, 0xD4, 0x09, 0x4F, 0x6B,
  27703. 0x95, 0xA8, 0x3F, 0x11, 0x8A, 0x51, 0x3E, 0xBF,
  27704. 0xE5, 0xE9, 0x9A, 0xEB, 0x88, 0xA2, 0x68, 0xE0,
  27705. 0x09, 0x7F, 0xCC, 0x3C, 0x7A, 0xE2, 0x50, 0xB6,
  27706. 0x81, 0x93, 0x3B, 0xBC, 0x2A, 0x8F, 0x53, 0x81,
  27707. 0xF9, 0x4D, 0x15, 0x64, 0x34, 0xA8, 0x7E, 0x9E,
  27708. 0xE3, 0x7E, 0x78, 0xC2, 0x7A, 0x0C, 0xDA, 0xEE,
  27709. 0xA9, 0x81, 0x4B, 0xCB, 0x43, 0xDF, 0x53, 0x8D,
  27710. 0xBE, 0x62, 0x8C, 0x80, 0x2C, 0x1A, 0x94, 0xE0,
  27711. 0xCD, 0xDC, 0xD0, 0xCD, 0x5A, 0x0F, 0x82, 0x20,
  27712. 0xDA, 0x97, 0xC2, 0x38, 0x39, 0x36, 0xA3, 0x39,
  27713. 0x19, 0xFC, 0xDC, 0x11, 0xD7, 0x0E, 0xD4, 0x43,
  27714. 0x7D, 0xD2, 0xD7, 0xC7, 0x3C, 0xD0, 0xC3, 0xBB,
  27715. 0x90, 0xCA, 0x70, 0x70, 0x22, 0x8F, 0xE8, 0xD6,
  27716. 0x4A, 0x1C, 0x9D, 0x56, 0xE6, 0xB3, 0x48, 0x30,
  27717. 0xEF, 0x30, 0x0B, 0x5A, 0xA6, 0xEC, 0x6C, 0x78,
  27718. 0xA5, 0x42, 0x5A, 0xE6, 0xF7, 0xAD, 0x0E, 0xFD,
  27719. 0xD5, 0x27, 0xCF, 0x0A, 0xF8, 0xE0, 0x9B, 0x56,
  27720. 0xE4, 0x95, 0xBE, 0x66, 0xF6, 0x65, 0xC6, 0x4B,
  27721. 0x0A, 0x42, 0xC5, 0xC4, 0xB2, 0x46, 0x80, 0x48,
  27722. 0x0A, 0xD2, 0xE5, 0xC1, 0x1D, 0x99, 0x1F, 0x7E,
  27723. 0x3D, 0xA7, 0x59, 0xAE, 0xC8, 0x02, 0xF1, 0x76,
  27724. 0xDD, 0xF1, 0x1E, 0xF7, 0x14, 0x69, 0xDC, 0x13,
  27725. 0xB3, 0xA3, 0xE0, 0x36, 0x99, 0x51, 0x98, 0x58,
  27726. 0xAC, 0x6F, 0xC6, 0x5C, 0x27, 0xFA, 0x4C, 0xEF,
  27727. 0xDA, 0x09, 0xC8, 0x2E, 0x8F, 0x95, 0x8E, 0x01,
  27728. 0x8D, 0xD5, 0x25, 0x5C, 0xA2, 0xF6, 0x28, 0xE0,
  27729. 0xDA, 0x73, 0x91, 0xAB, 0xED, 0x6D, 0x37, 0x70,
  27730. 0x55, 0x28, 0xAB, 0x22, 0xEC, 0x71, 0xDC, 0x88,
  27731. 0x36, 0xD7, 0xFD, 0x46, 0x45, 0x94, 0x47, 0x03,
  27732. 0xA5, 0x1C, 0xC7, 0x4D, 0x29, 0x70, 0x92, 0xFC,
  27733. 0xE1, 0x39, 0xE8, 0x97, 0x6F, 0x8B, 0xE9, 0xC5,
  27734. 0xF8, 0x63, 0x90, 0xB7, 0x4D, 0x40, 0x1A, 0x8C,
  27735. 0x81, 0x53, 0x11, 0x22, 0x01, 0x13, 0x3D, 0x0C,
  27736. 0x51, 0x7C, 0x6C, 0xE7, 0xA3, 0x8C, 0x08, 0x60,
  27737. 0x69, 0xCE, 0x39, 0x71, 0xF1, 0xAD, 0x28, 0xF3,
  27738. 0xE5, 0xD0, 0x1B, 0x56, 0xA4, 0x80, 0xB4, 0x17,
  27739. 0xA0, 0x16, 0xAE, 0xA4, 0x63, 0x94, 0xCD, 0xF7,
  27740. 0x64, 0x81, 0x29, 0x18, 0xD8, 0xAB, 0x05, 0x01,
  27741. 0xD5, 0xD1, 0x8C, 0xE1, 0x3F, 0xBD, 0x3D, 0xE9,
  27742. 0x1F, 0x50, 0x42, 0x15, 0xCC, 0xD0, 0xE2, 0xD1,
  27743. 0x7B, 0x7E, 0x96, 0x3C, 0x86, 0x7F, 0x6F, 0x13,
  27744. 0x21, 0x14, 0xE3, 0x64, 0x59, 0xFC, 0x5A, 0xF7,
  27745. 0xCE, 0xE9, 0x9B, 0x78, 0x96, 0x73, 0xE5, 0x24,
  27746. 0x13, 0x1F, 0x7D, 0xC7, 0x13, 0x60, 0x95, 0x1A,
  27747. 0x99, 0x7A, 0x9C, 0xE5, 0x0D, 0xD5, 0xFA, 0xFC,
  27748. 0x45, 0x21, 0x14, 0x44, 0x41, 0xC0, 0x6B, 0xB4,
  27749. 0x1C, 0x79, 0xE8, 0xED, 0x53, 0x28, 0x5D, 0x13,
  27750. 0x7D, 0x54, 0xF3, 0x25, 0xA6, 0xC2, 0xF2, 0xEF,
  27751. 0x74, 0xE3, 0x4C, 0x0F, 0x87, 0x7A, 0x61, 0x4C,
  27752. 0xE4, 0x5D, 0xC0, 0xAE, 0xDD, 0xF9, 0x5A, 0x0E,
  27753. 0x2E, 0x4E, 0xDA, 0xE2, 0x9A, 0xF4, 0x11, 0xC9,
  27754. 0xCC, 0x2A, 0xF9, 0x5C, 0x9E, 0xA9, 0xA9, 0x4A,
  27755. 0x79, 0x61, 0xC8, 0x24, 0x6E, 0x65, 0x4F, 0xA2,
  27756. 0x8F, 0x3D, 0x56, 0x8D, 0x5F, 0xEE, 0x93, 0x35,
  27757. 0x2C, 0x2E, 0x0D, 0x60, 0xCC, 0xAF, 0x5B, 0x00,
  27758. 0x09, 0x0A, 0xB6, 0xE7, 0xA5, 0x3A, 0xA0, 0x6A,
  27759. 0x8C, 0xD3, 0x73, 0x7E, 0xBF, 0x1B, 0x65, 0xD6,
  27760. 0x25, 0xBC, 0xF2, 0x20, 0xF7, 0x4D, 0xE2, 0x2D,
  27761. 0x98, 0x71, 0xEF, 0xC3, 0x76, 0xBF, 0x08, 0x2D,
  27762. 0x4B, 0x87, 0x2A, 0x30, 0x3C, 0x32, 0x42, 0x7A,
  27763. 0x0C, 0x98, 0xBE, 0xCF, 0x58, 0x95, 0x9C, 0x9F,
  27764. 0x9E, 0x2E, 0x88, 0x7D, 0xBC, 0x42, 0xAA, 0xB1,
  27765. 0x65, 0x6A, 0xD1, 0x56, 0x37, 0xA6, 0xA8, 0xF4,
  27766. 0xBF, 0x96, 0x34, 0x09, 0x54, 0x91, 0xF8, 0xC9,
  27767. 0x92, 0x42, 0x91, 0x38, 0x91, 0x43, 0x7E, 0x6C,
  27768. 0x5B, 0x50, 0xA2, 0x13, 0xDD, 0xE8, 0x0D, 0x21,
  27769. 0x96, 0xBE, 0x12, 0xC3, 0x93, 0x7F, 0xE3, 0x23,
  27770. 0x9B, 0xF6, 0x75, 0x9A, 0xBB, 0x8C, 0x1C, 0x94,
  27771. 0x66, 0xF4, 0x2F, 0xBD, 0x53, 0x89, 0x4A, 0xE5,
  27772. 0x2F, 0xB5, 0x33, 0x32, 0x14, 0x29, 0xFC, 0xE4,
  27773. 0xFE, 0xC1, 0xDB, 0x35, 0x2C, 0x49, 0x58, 0x3A,
  27774. 0x7D, 0x81, 0x7E, 0xAF, 0x62, 0x00, 0x08, 0x88,
  27775. 0xEC, 0xB0, 0xEB, 0xFF, 0xEF, 0x69, 0xFF, 0x8E,
  27776. 0x59, 0x0C, 0xFA, 0x25, 0xBE, 0xAB, 0x21, 0x60,
  27777. 0x5B, 0x63, 0x5A, 0xBC, 0x2C, 0xA2, 0x36, 0x80,
  27778. 0x78, 0x97, 0x25, 0xCF, 0x70, 0x0F, 0x55, 0x3C,
  27779. 0x88, 0x35, 0x2F, 0x31, 0x61, 0x61, 0x54, 0x87,
  27780. 0x3D, 0x18, 0xB6, 0xC6, 0xEB, 0x51, 0x9F, 0xC6,
  27781. 0x39, 0xB0, 0x70, 0xFD, 0x67, 0xF8, 0x6A, 0xAB,
  27782. 0x62, 0x34, 0x9D, 0xBF, 0xFA, 0x89, 0xF9, 0x30,
  27783. 0x51, 0xA7, 0xC7, 0xB7, 0xBD, 0x16, 0x1F, 0xCD,
  27784. 0x73, 0x67, 0x2C, 0xEE, 0xF5, 0x9A, 0x9B, 0xB7,
  27785. 0xF5, 0x71, 0xEA, 0xBE, 0x25, 0x70, 0xC5, 0xBF,
  27786. 0x31, 0xEC, 0xAA, 0x1F, 0x9C, 0xA7, 0xA9, 0xC6,
  27787. 0xD3, 0x1E, 0xA5, 0xFB, 0x7C, 0x97, 0x9C, 0xDD,
  27788. 0x26, 0x13, 0x89, 0x7E, 0x7D, 0x15, 0x03, 0xFB,
  27789. 0x0C, 0x19, 0xAD, 0xDC, 0xFB, 0x3A, 0x63, 0xE2,
  27790. 0x18, 0x5F, 0xC4, 0x10, 0x18, 0x38, 0xDA, 0x66,
  27791. 0xCC, 0xE2, 0xD3, 0xD9, 0xFF, 0xB4, 0x77, 0x46,
  27792. 0xC2, 0x00, 0x3E, 0xDD, 0x86, 0xC2, 0xF8, 0xC3
  27793. };
  27794. static const byte kprime_1024[KYBER_SS_SZ] = {
  27795. 0xC6, 0x1F, 0x73, 0xD2, 0xBF, 0xB1, 0x85, 0x94,
  27796. 0xE1, 0xBA, 0x5D, 0x3B, 0x58, 0xB4, 0xC9, 0x34,
  27797. 0x20, 0x6D, 0x3A, 0x6F, 0x8E, 0xC9, 0x13, 0x95,
  27798. 0xAB, 0x77, 0x79, 0xC6, 0x1F, 0xA1, 0xDD, 0x6F
  27799. };
  27800. #endif
  27801. static byte ss[KYBER_SS_SZ];
  27802. key = (KyberKey*)XMALLOC(sizeof(KyberKey), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27803. ExpectNotNull(key);
  27804. if (key != NULL) {
  27805. XMEMSET(key, 0, sizeof(KyberKey));
  27806. }
  27807. #ifndef WOLFSSL_NO_KYBER512
  27808. ExpectIntEQ(wc_KyberKey_Init(KYBER512, key, NULL, INVALID_DEVID), 0);
  27809. ExpectIntEQ(wc_KyberKey_DecodePrivateKey(key, dk_512, sizeof(dk_512)), 0);
  27810. ExpectIntEQ(wc_KyberKey_Decapsulate(key, ss, c_512, sizeof(c_512)), 0);
  27811. ExpectIntEQ(XMEMCMP(ss, kprime_512, KYBER_SS_SZ), 0);
  27812. wc_KyberKey_Free(key);
  27813. #endif
  27814. #ifndef WOLFSSL_NO_KYBER768
  27815. ExpectIntEQ(wc_KyberKey_Init(KYBER768, key, NULL, INVALID_DEVID), 0);
  27816. ExpectIntEQ(wc_KyberKey_DecodePrivateKey(key, dk_768, sizeof(dk_768)), 0);
  27817. ExpectIntEQ(wc_KyberKey_Decapsulate(key, ss, c_768, sizeof(c_768)), 0);
  27818. ExpectIntEQ(XMEMCMP(ss, kprime_768, KYBER_SS_SZ), 0);
  27819. wc_KyberKey_Free(key);
  27820. #endif
  27821. #ifndef WOLFSSL_NO_KYBER1024
  27822. ExpectIntEQ(wc_KyberKey_Init(KYBER1024, key, NULL, INVALID_DEVID), 0);
  27823. ExpectIntEQ(wc_KyberKey_DecodePrivateKey(key, dk_1024, sizeof(dk_1024)), 0);
  27824. ExpectIntEQ(wc_KyberKey_Decapsulate(key, ss, c_1024, sizeof(c_1024)), 0);
  27825. ExpectIntEQ(XMEMCMP(ss, kprime_1024, KYBER_SS_SZ), 0);
  27826. wc_KyberKey_Free(key);
  27827. #endif
  27828. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27829. #endif
  27830. return EXPECT_RESULT();
  27831. }
  27832. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  27833. !defined(WOLFSSL_DILITHIUM_NO_VERIFY) && !defined(WOLFSSL_NO_ML_DSA_44)
  27834. static const byte ml_dsa_44_pub_key[] = {
  27835. 0xf1, 0xdf, 0x1e, 0xfc, 0x6b, 0x41, 0xe7,
  27836. 0x5e, 0xcb, 0xb5, 0xb5, 0xd2, 0x3c, 0xc8,
  27837. 0xd3, 0x99, 0x73, 0x36, 0x9a, 0x0b, 0x32,
  27838. 0x71, 0x7a, 0x9f, 0x6d, 0x66, 0x07, 0xb8,
  27839. 0x31, 0x5f, 0x25, 0xb6, 0x2e, 0xee, 0x4f,
  27840. 0x63, 0x13, 0x02, 0x45, 0x3c, 0xd1, 0x3d,
  27841. 0x79, 0x6b, 0x3c, 0xfe, 0xd9, 0x2f, 0x39,
  27842. 0xe8, 0x62, 0x60, 0xf0, 0x04, 0x83, 0x28,
  27843. 0xaa, 0xdc, 0x15, 0x90, 0xef, 0x55, 0x48,
  27844. 0xf9, 0xd2, 0xcd, 0x53, 0x87, 0x0e, 0x42,
  27845. 0xba, 0x16, 0x87, 0x7b, 0x32, 0xa8, 0xbf,
  27846. 0xed, 0x32, 0xa1, 0x19, 0x66, 0x44, 0xfe,
  27847. 0x57, 0xec, 0x26, 0xed, 0x9e, 0x73, 0xa0,
  27848. 0x87, 0xe8, 0x8a, 0x93, 0x3c, 0xec, 0x1d,
  27849. 0xa0, 0xcc, 0x2e, 0x0d, 0x37, 0x5b, 0xb1,
  27850. 0x74, 0x77, 0x18, 0x4b, 0xde, 0x4b, 0xc9,
  27851. 0xac, 0xf8, 0xda, 0x23, 0x7a, 0x2a, 0x39,
  27852. 0xfa, 0x96, 0x01, 0xff, 0xf0, 0xc7, 0xa7,
  27853. 0x34, 0xca, 0x9d, 0xe9, 0xda, 0x4d, 0x85,
  27854. 0x00, 0xc9, 0xe9, 0xcf, 0xb0, 0x3e, 0x21,
  27855. 0xe6, 0xae, 0x52, 0x67, 0x4f, 0xe3, 0x93,
  27856. 0x2f, 0x50, 0x47, 0xdd, 0x89, 0xa2, 0x48,
  27857. 0xf8, 0xfe, 0x93, 0xfe, 0xce, 0x68, 0x9c,
  27858. 0xe9, 0x4d, 0xdd, 0xbd, 0x9f, 0xeb, 0x14,
  27859. 0x8d, 0x38, 0x7a, 0xc6, 0xf2, 0x50, 0x00,
  27860. 0x91, 0x65, 0xd0, 0xd1, 0xeb, 0x51, 0xab,
  27861. 0x3a, 0x0e, 0x45, 0x5c, 0xbd, 0x65, 0xf5,
  27862. 0x78, 0xc6, 0xa0, 0xaa, 0xae, 0x50, 0xf2,
  27863. 0x19, 0x1f, 0x90, 0x1a, 0x9f, 0x34, 0xa0,
  27864. 0xa1, 0x95, 0x94, 0x86, 0x30, 0xc2, 0xb2,
  27865. 0x95, 0x82, 0x13, 0xf6, 0x73, 0xe2, 0x03,
  27866. 0xe3, 0x7c, 0x09, 0x8e, 0x5d, 0x07, 0xd6,
  27867. 0x33, 0x93, 0x8a, 0x1b, 0x67, 0xc9, 0xb1,
  27868. 0x76, 0x74, 0x1c, 0x22, 0x58, 0x05, 0x5a,
  27869. 0xa8, 0x83, 0x68, 0xce, 0x64, 0xfc, 0x52,
  27870. 0x7f, 0x35, 0x80, 0x6e, 0xdf, 0xf5, 0x2d,
  27871. 0xd2, 0xd1, 0x17, 0xdc, 0xce, 0x95, 0xe8,
  27872. 0xe6, 0x42, 0xb1, 0xb1, 0x61, 0xc1, 0x24,
  27873. 0x79, 0x1c, 0x51, 0xfc, 0x3c, 0xba, 0x40,
  27874. 0xf7, 0x70, 0x35, 0x22, 0x73, 0x31, 0x53,
  27875. 0x21, 0xea, 0x09, 0xf7, 0xaa, 0x07, 0xb8,
  27876. 0xfa, 0x0b, 0xa0, 0xa9, 0xb4, 0x8c, 0x83,
  27877. 0xbb, 0x25, 0xfe, 0x39, 0x29, 0xef, 0x34,
  27878. 0xd5, 0xe2, 0xc7, 0x9e, 0x87, 0xbd, 0x50,
  27879. 0x86, 0x71, 0x12, 0x3e, 0x8a, 0x78, 0xe2,
  27880. 0xb3, 0xe1, 0xfa, 0x5b, 0x73, 0x3b, 0x34,
  27881. 0x9f, 0x4e, 0x7d, 0xd5, 0x1b, 0xb9, 0x8e,
  27882. 0x43, 0x76, 0xef, 0x3e, 0x37, 0x70, 0x33,
  27883. 0x36, 0xd1, 0xa1, 0xba, 0x1b, 0xb1, 0x79,
  27884. 0xfb, 0x2c, 0xb1, 0x9b, 0xc3, 0x1b, 0x26,
  27885. 0x83, 0x89, 0x4d, 0x53, 0x40, 0xa5, 0xf9,
  27886. 0x8b, 0xe2, 0xec, 0x30, 0x1f, 0xf6, 0x16,
  27887. 0xd6, 0x55, 0xce, 0x0e, 0x1b, 0xed, 0xe0,
  27888. 0xeb, 0xc9, 0x7a, 0x2e, 0x1a, 0x85, 0x81,
  27889. 0xa4, 0xe2, 0xa8, 0xbe, 0x9f, 0xac, 0x0b,
  27890. 0x23, 0xb4, 0xbb, 0xc2, 0x0f, 0x66, 0x43,
  27891. 0x45, 0x93, 0x20, 0x37, 0x4d, 0x47, 0x23,
  27892. 0x7f, 0x4a, 0x5e, 0x8b, 0x19, 0xec, 0xd9,
  27893. 0x57, 0x69, 0xc4, 0x91, 0xb0, 0xcd, 0x25,
  27894. 0x2a, 0x7d, 0x52, 0xdb, 0x59, 0x18, 0x8b,
  27895. 0x96, 0xad, 0x75, 0x21, 0x81, 0x1a, 0x2c,
  27896. 0xb3, 0x26, 0x30, 0x78, 0x19, 0x2b, 0x22,
  27897. 0x74, 0x6e, 0x92, 0x57, 0xec, 0x3c, 0x75,
  27898. 0x8b, 0xd8, 0x4b, 0x7c, 0xd1, 0x72, 0x1b,
  27899. 0x1f, 0xed, 0xae, 0x15, 0x82, 0xd3, 0xf6,
  27900. 0xaf, 0x01, 0x31, 0xec, 0x1b, 0xca, 0xa5,
  27901. 0xf8, 0x78, 0x7f, 0x8a, 0x8a, 0x03, 0xbd,
  27902. 0x03, 0x0a, 0xc5, 0x4e, 0x15, 0xab, 0xa4,
  27903. 0x76, 0x56, 0x5b, 0xf8, 0x50, 0xa9, 0xee,
  27904. 0x61, 0xbd, 0x05, 0xe0, 0xdf, 0xc6, 0xbe,
  27905. 0x4a, 0xaf, 0xdb, 0x96, 0x0a, 0x7e, 0xcb,
  27906. 0x2e, 0xb0, 0x68, 0x4e, 0x2d, 0x88, 0x32,
  27907. 0x1e, 0xe1, 0xbc, 0x08, 0x15, 0x15, 0x71,
  27908. 0xe6, 0x77, 0x2b, 0xeb, 0x47, 0x81, 0xb7,
  27909. 0xe8, 0x82, 0x9f, 0x5f, 0x94, 0xd2, 0xac,
  27910. 0xa5, 0x89, 0x52, 0xe1, 0x3c, 0x59, 0xe0,
  27911. 0x06, 0xe6, 0x66, 0xe1, 0xf9, 0x9d, 0x32,
  27912. 0x42, 0x9d, 0x77, 0xfe, 0x6a, 0x12, 0x4a,
  27913. 0xa3, 0xd2, 0x49, 0xbb, 0x39, 0xad, 0x42,
  27914. 0xb7, 0x37, 0xfb, 0xde, 0x9d, 0xaf, 0x1b,
  27915. 0xd5, 0x5a, 0x3b, 0x06, 0xa6, 0x51, 0x7d,
  27916. 0x6a, 0x5c, 0x32, 0xdb, 0xde, 0x5d, 0x0d,
  27917. 0x20, 0x88, 0xee, 0x8b, 0xa8, 0x49, 0x5b,
  27918. 0x6c, 0x50, 0x72, 0xdb, 0x68, 0x44, 0x17,
  27919. 0x28, 0xd4, 0xbb, 0x43, 0x8e, 0x00, 0xa5,
  27920. 0xc8, 0x27, 0x00, 0xaa, 0x2b, 0xa4, 0xc2,
  27921. 0x16, 0xcd, 0x2d, 0x59, 0xdc, 0x1a, 0xa2,
  27922. 0x66, 0xe2, 0x96, 0x6b, 0xcc, 0x39, 0xc6,
  27923. 0xe9, 0x2b, 0x14, 0xa7, 0x7d, 0x67, 0x5d,
  27924. 0x54, 0xfc, 0x93, 0x73, 0x52, 0x47, 0xc7,
  27925. 0x24, 0x1e, 0x7e, 0xc9, 0x2d, 0x87, 0x60,
  27926. 0xd3, 0xd8, 0x76, 0xf0, 0x51, 0x04, 0xc7,
  27927. 0xcb, 0x68, 0x0f, 0xd8, 0x4b, 0x22, 0xb2,
  27928. 0x51, 0x87, 0xe9, 0x1e, 0x05, 0x3d, 0xe2,
  27929. 0x8a, 0x6b, 0xb8, 0x96, 0xd6, 0xe0, 0x6e,
  27930. 0x38, 0x74, 0x96, 0xad, 0x7e, 0x4f, 0x52,
  27931. 0x35, 0xcf, 0x4a, 0x50, 0xe7, 0x60, 0x2e,
  27932. 0x58, 0xcf, 0xdc, 0x7a, 0x9a, 0x21, 0x76,
  27933. 0x1d, 0x2c, 0xd1, 0x98, 0xab, 0xab, 0xed,
  27934. 0xf9, 0xec, 0xd5, 0x7b, 0x09, 0xad, 0x2e,
  27935. 0xad, 0x5a, 0xdc, 0xad, 0xd6, 0x46, 0xba,
  27936. 0x2d, 0x55, 0xf7, 0x0c, 0x9a, 0x23, 0x10,
  27937. 0x50, 0x3e, 0x4f, 0xe1, 0xeb, 0x58, 0x8a,
  27938. 0xc0, 0x17, 0x48, 0x41, 0x40, 0x65, 0x0b,
  27939. 0xfb, 0x43, 0x9e, 0xf0, 0x37, 0x4a, 0x89,
  27940. 0x4e, 0x71, 0xad, 0x44, 0x19, 0x13, 0xbb,
  27941. 0x4a, 0x63, 0x83, 0x9e, 0x6a, 0x49, 0x1b,
  27942. 0x28, 0xb0, 0x8e, 0x9c, 0x7b, 0xaf, 0xf9,
  27943. 0x57, 0x5d, 0x35, 0x16, 0x5c, 0xa7, 0x5e,
  27944. 0xd1, 0x0d, 0x83, 0xdc, 0x49, 0xdd, 0x40,
  27945. 0x58, 0x9c, 0x97, 0x91, 0xa6, 0xb0, 0x68,
  27946. 0xb0, 0xfa, 0x9e, 0xc0, 0x3f, 0x81, 0xc6,
  27947. 0xce, 0x58, 0xc5, 0x87, 0xc6, 0xf4, 0x06,
  27948. 0xec, 0x91, 0x57, 0x81, 0xce, 0x3a, 0xe8,
  27949. 0xf1, 0x29, 0x3f, 0x01, 0x93, 0xf0, 0x74,
  27950. 0x22, 0xea, 0x6b, 0x06, 0xd8, 0x65, 0xdb,
  27951. 0xd7, 0x41, 0xd9, 0x60, 0x23, 0xe7, 0x83,
  27952. 0xc8, 0x69, 0x6b, 0x90, 0xc5, 0xc7, 0xb9,
  27953. 0xd5, 0xba, 0x79, 0xc9, 0x4a, 0x87, 0x23,
  27954. 0x1c, 0x95, 0x78, 0xf3, 0x73, 0x10, 0xbe,
  27955. 0xb2, 0x0f, 0x32, 0xec, 0xff, 0x15, 0x51,
  27956. 0x4d, 0xb5, 0x48, 0x3c, 0xca, 0x4c, 0x5b,
  27957. 0x32, 0x29, 0x47, 0x21, 0xba, 0x2a, 0x5d,
  27958. 0xc9, 0x59, 0xfa, 0x8f, 0x33, 0x10, 0x83,
  27959. 0x40, 0x80, 0xf3, 0xce, 0xee, 0x6d, 0xcd,
  27960. 0x9c, 0xbb, 0x23, 0x0b, 0x45, 0xba, 0x7a,
  27961. 0x07, 0xdc, 0x4d, 0x57, 0x97, 0xb4, 0xa4,
  27962. 0xef, 0x94, 0xe8, 0x43, 0xfe, 0x18, 0x47,
  27963. 0x1a, 0xb0, 0xf6, 0xb6, 0x0b, 0x55, 0x05,
  27964. 0xbd, 0x67, 0x2d, 0x37, 0x27, 0x17, 0x13,
  27965. 0x65, 0x22, 0xf2, 0x7c, 0xf7, 0x47, 0xd2,
  27966. 0x85, 0x63, 0x98, 0x83, 0xd2, 0xc1, 0xbf,
  27967. 0x8f, 0x4c, 0xda, 0xbf, 0xa4, 0x10, 0x6b,
  27968. 0x4e, 0x6b, 0x78, 0x5e, 0x3f, 0x7a, 0xec,
  27969. 0x15, 0x84, 0xbe, 0x1a, 0x94, 0xa2, 0x2b,
  27970. 0xb5, 0x3e, 0x55, 0x86, 0x51, 0xec, 0x2e,
  27971. 0x62, 0xcb, 0xd6, 0x9f, 0xe5, 0xa4, 0xb8,
  27972. 0xc0, 0xaa, 0x4e, 0x6d, 0x8a, 0xb1, 0xd6,
  27973. 0xf7, 0x8d, 0x1c, 0x04, 0x32, 0x8b, 0x20,
  27974. 0xf5, 0x80, 0x33, 0xbd, 0xcc, 0x3e, 0x4c,
  27975. 0x16, 0x04, 0xab, 0xd8, 0x64, 0x6d, 0xf9,
  27976. 0xc9, 0x15, 0x7d, 0x4b, 0x00, 0x86, 0xb2,
  27977. 0x70, 0x1d, 0x20, 0xcb, 0x7a, 0xed, 0x7e,
  27978. 0x81, 0x7f, 0x41, 0x33, 0xb8, 0x7b, 0xc0,
  27979. 0xa3, 0xbd, 0x12, 0xd1, 0x67, 0x48, 0xa0,
  27980. 0xb9, 0xeb, 0xd5, 0x29, 0xab, 0x91, 0x9c,
  27981. 0xa2, 0x2f, 0x8e, 0x01, 0x1c, 0x88, 0xc1,
  27982. 0x3e, 0x34, 0x47, 0x36, 0x8a, 0x35, 0x6c,
  27983. 0x2f, 0xc1, 0x8a, 0xb6, 0xd0, 0xa5, 0x01,
  27984. 0x82, 0xee, 0x4f, 0x44, 0xb9, 0xcd, 0x16,
  27985. 0x9c, 0x3a, 0xf8, 0xe9, 0x2a, 0xd2, 0xb6,
  27986. 0x1d, 0xfd, 0x3c, 0x06, 0xdc, 0x42, 0xdd,
  27987. 0x2d, 0x60, 0x6a, 0x44, 0x21, 0xc3, 0x37,
  27988. 0x75, 0x79, 0xc5, 0x29, 0x5c, 0x7e, 0xf5,
  27989. 0x86, 0xbb, 0x56, 0x05, 0x21, 0x46, 0xaf,
  27990. 0x6d, 0x3a, 0xa2, 0x9e, 0x11, 0x6d, 0x9e,
  27991. 0x05, 0x74, 0x8a, 0xfe, 0x84, 0x88, 0x3e,
  27992. 0x76, 0xb4, 0xef, 0x2f, 0xeb, 0x52, 0xcd,
  27993. 0x97, 0x82, 0xba, 0x0c, 0xcc, 0xcb, 0x72,
  27994. 0x8d, 0x8d, 0xd2, 0x32, 0x7c, 0x41, 0x39,
  27995. 0xa6, 0x22, 0xb7, 0xdc, 0x3f, 0x39, 0x43,
  27996. 0xf5, 0xee, 0x0c, 0xfc, 0xbb, 0x2b, 0x43,
  27997. 0xe8, 0xce, 0xae, 0x0c, 0xd9, 0x15, 0x22,
  27998. 0x32, 0xbd, 0x69, 0xad, 0x76, 0xd9, 0xdf,
  27999. 0x81, 0xdf, 0x24, 0x76, 0x7b, 0x53, 0x0b,
  28000. 0xe6, 0xc7, 0x6c, 0x38, 0x2c, 0xbf, 0x28,
  28001. 0x95, 0x03, 0x18, 0xef, 0x98, 0x88, 0xc2,
  28002. 0x6b, 0x1a, 0xf5, 0xb4, 0xf9, 0x19, 0x76,
  28003. 0x25, 0x1d, 0xcf, 0x9b, 0xcd, 0x4c, 0x00,
  28004. 0x06, 0xde, 0x55, 0x58, 0x95, 0x9a, 0x06,
  28005. 0xfb, 0xf9, 0x88, 0x20, 0x85, 0x80, 0xe3,
  28006. 0x27, 0xdf, 0xc5, 0x20, 0x29, 0x7c, 0x58,
  28007. 0x02, 0x07, 0x2e, 0xd2, 0xeb, 0xdc, 0x68,
  28008. 0x58, 0x91, 0x08, 0x71, 0x16, 0xb3, 0x82,
  28009. 0x2f, 0x6c, 0x45, 0xcd, 0xbe, 0xe5, 0x0c,
  28010. 0x07, 0x77, 0x95, 0x3b, 0x2c, 0x59, 0x8e,
  28011. 0xba, 0x07, 0xa8, 0xa1, 0xc6, 0xe5, 0x6a,
  28012. 0x49, 0xb5, 0x85, 0xf2, 0x70, 0x05, 0x22,
  28013. 0xc4, 0x2f, 0x8d, 0xdd, 0x48, 0x8d, 0x87,
  28014. 0xfa, 0xb6, 0xf8, 0x59, 0xc8, 0xb1, 0x18,
  28015. 0x03, 0x5f, 0xce, 0x53, 0x28, 0x96, 0x15,
  28016. 0xd4, 0xb4, 0x10, 0x2c, 0xe2, 0x22, 0x9e,
  28017. 0x88, 0xe5, 0xcd, 0xda, 0xfc, 0xf9, 0x64,
  28018. 0xa4, 0x7b, 0xfb, 0xeb, 0xa8, 0x6a, 0xb6,
  28019. 0xf6, 0x17, 0x84, 0x26, 0x3d, 0xe4, 0x66,
  28020. 0x7e, 0x5c, 0x85, 0x01, 0xaf, 0xdc, 0xdb,
  28021. 0x48, 0x33, 0x4a, 0x20, 0x7c, 0x22, 0x1b,
  28022. 0xd5, 0xeb, 0x2d,
  28023. };
  28024. static const byte ml_dsa_44_good_sig[] = {
  28025. 0xfc, 0x2d, 0xa0, 0x06, 0x85, 0xc2, 0xfc,
  28026. 0x92, 0x47, 0x77, 0x0b, 0x39, 0xbf, 0xe5,
  28027. 0xba, 0xd4, 0x44, 0xbf, 0xde, 0xce, 0x1f,
  28028. 0x04, 0xa2, 0x87, 0xed, 0x4a, 0xce, 0x0e,
  28029. 0xf3, 0x95, 0x61, 0x1e, 0x66, 0x4e, 0x9a,
  28030. 0x5d, 0x00, 0x31, 0x32, 0xf0, 0x90, 0x3d,
  28031. 0x7e, 0xf2, 0x9d, 0xe2, 0x93, 0xa1, 0xc8,
  28032. 0x64, 0x36, 0xf2, 0x59, 0xc7, 0x9e, 0xb6,
  28033. 0xb3, 0x6f, 0xe5, 0x80, 0x8d, 0x92, 0x77,
  28034. 0xd6, 0xb6, 0xe4, 0xc5, 0x5e, 0x79, 0x45,
  28035. 0x4b, 0xd0, 0xfe, 0x53, 0x55, 0xb6, 0x66,
  28036. 0x88, 0xfe, 0x95, 0x40, 0x07, 0xfd, 0xdb,
  28037. 0x40, 0x33, 0x39, 0x67, 0x03, 0x30, 0x8e,
  28038. 0x80, 0x4e, 0xa7, 0x0e, 0xe4, 0x05, 0x04,
  28039. 0xc5, 0x33, 0x72, 0x47, 0x5b, 0x85, 0x0f,
  28040. 0xe1, 0xeb, 0x98, 0x1a, 0x76, 0x79, 0x84,
  28041. 0xce, 0x26, 0x66, 0xe8, 0x92, 0xc9, 0x1f,
  28042. 0x40, 0x96, 0x72, 0xfe, 0x61, 0xae, 0xba,
  28043. 0x84, 0x70, 0xb7, 0x92, 0x2f, 0x7e, 0xc8,
  28044. 0xe8, 0xe4, 0x34, 0x73, 0xd4, 0x69, 0x57,
  28045. 0x3e, 0x28, 0x2b, 0x18, 0x0f, 0xef, 0xb1,
  28046. 0x06, 0xe2, 0xf8, 0x79, 0x70, 0x5a, 0x84,
  28047. 0x84, 0x6c, 0xb3, 0x57, 0x5b, 0x18, 0x42,
  28048. 0xdf, 0xd5, 0xdb, 0xf8, 0x35, 0x5f, 0x7b,
  28049. 0x23, 0x25, 0x2f, 0x0f, 0x17, 0x0b, 0x9a,
  28050. 0xb6, 0xe8, 0x31, 0x30, 0x6b, 0x90, 0x06,
  28051. 0x2c, 0xfd, 0xca, 0xaa, 0xa6, 0xc3, 0xdc,
  28052. 0x88, 0xa7, 0x31, 0x74, 0x67, 0xe2, 0x64,
  28053. 0x8f, 0x5c, 0xc4, 0xc0, 0x4d, 0x34, 0x15,
  28054. 0x0d, 0xd2, 0x23, 0x69, 0xfc, 0x6e, 0xbb,
  28055. 0x82, 0xca, 0xc4, 0xee, 0xf1, 0x14, 0xc1,
  28056. 0xd4, 0x5a, 0x71, 0x78, 0x9b, 0x40, 0x01,
  28057. 0xb9, 0xe4, 0x6e, 0x68, 0xf6, 0x13, 0xca,
  28058. 0xc1, 0xea, 0x70, 0x71, 0x3d, 0xc9, 0x1a,
  28059. 0x62, 0xb9, 0xa9, 0xe0, 0x1e, 0xe2, 0x34,
  28060. 0xf2, 0x9a, 0xf7, 0x23, 0xb3, 0xc1, 0xca,
  28061. 0x35, 0x0e, 0x5e, 0xa7, 0xd1, 0x3d, 0xea,
  28062. 0x51, 0xdc, 0xe2, 0x0e, 0xfc, 0x7d, 0x26,
  28063. 0x75, 0xec, 0x9a, 0x6e, 0x40, 0x1f, 0x60,
  28064. 0x06, 0xd7, 0x56, 0xf8, 0xa4, 0x2a, 0x82,
  28065. 0x9c, 0xef, 0x51, 0x4a, 0xe1, 0x01, 0x2b,
  28066. 0xb0, 0x8b, 0x34, 0x7b, 0xe1, 0x63, 0xa4,
  28067. 0xcc, 0x72, 0x81, 0xd9, 0xb4, 0x20, 0xcc,
  28068. 0x60, 0xe2, 0x15, 0x6d, 0xc7, 0x6c, 0x75,
  28069. 0x65, 0x4d, 0xb6, 0xc0, 0x36, 0x49, 0x87,
  28070. 0x06, 0x3e, 0xca, 0x1c, 0x32, 0x36, 0x2f,
  28071. 0xe3, 0xf6, 0x06, 0x0a, 0xb1, 0xd2, 0xfb,
  28072. 0xee, 0x4e, 0xd7, 0xce, 0x65, 0xcc, 0x89,
  28073. 0xf2, 0x77, 0x14, 0x27, 0x27, 0x84, 0x52,
  28074. 0x97, 0x1b, 0x89, 0x17, 0x31, 0x8d, 0xc4,
  28075. 0x0f, 0xc7, 0xc6, 0x45, 0x44, 0x9a, 0x97,
  28076. 0xd3, 0x88, 0x71, 0x73, 0x97, 0x64, 0xa6,
  28077. 0xe6, 0x3d, 0xf2, 0xd3, 0x7f, 0x7f, 0xfa,
  28078. 0x4f, 0xf4, 0xe9, 0x76, 0x8a, 0x2a, 0xfe,
  28079. 0x28, 0x8e, 0xa5, 0xb3, 0x46, 0x2b, 0xad,
  28080. 0x50, 0x5e, 0x12, 0xcd, 0xf8, 0x46, 0xe4,
  28081. 0x06, 0x12, 0xc2, 0xb8, 0xcd, 0x04, 0x76,
  28082. 0x07, 0x7c, 0xed, 0x2f, 0x0f, 0xd6, 0x97,
  28083. 0x31, 0xa4, 0x0c, 0x18, 0x85, 0x75, 0xd3,
  28084. 0x55, 0xfd, 0xe3, 0x1a, 0xbf, 0x43, 0xde,
  28085. 0x20, 0xa9, 0x19, 0xcd, 0x03, 0x01, 0xdf,
  28086. 0x04, 0x71, 0x09, 0x94, 0x99, 0x51, 0xb0,
  28087. 0x8e, 0x32, 0x80, 0xe6, 0x64, 0x4b, 0xdf,
  28088. 0xa5, 0xec, 0xfa, 0xce, 0xf6, 0xf3, 0xce,
  28089. 0x51, 0xe8, 0x6d, 0x03, 0x1e, 0x69, 0x59,
  28090. 0xef, 0x20, 0x98, 0x71, 0xe2, 0xc3, 0xec,
  28091. 0x19, 0x03, 0xa9, 0x2d, 0x12, 0x21, 0x79,
  28092. 0x7e, 0xb0, 0xcb, 0x76, 0x68, 0x2f, 0x11,
  28093. 0x01, 0x2b, 0x11, 0xef, 0xd6, 0xb7, 0x8b,
  28094. 0x5e, 0x31, 0x78, 0x7b, 0x2d, 0xe6, 0x4a,
  28095. 0xfb, 0xc1, 0xbb, 0x78, 0x92, 0x11, 0xcb,
  28096. 0x91, 0x97, 0x52, 0x1b, 0x8e, 0xfb, 0x59,
  28097. 0x02, 0x22, 0xbe, 0xf7, 0x33, 0xaa, 0x7b,
  28098. 0xfd, 0x93, 0xf7, 0xa8, 0x9b, 0xfc, 0x99,
  28099. 0x36, 0x22, 0x04, 0x1e, 0xdc, 0xa3, 0x2b,
  28100. 0xe6, 0xac, 0x2e, 0x4b, 0x38, 0x0a, 0x25,
  28101. 0xde, 0x3d, 0x8e, 0x0b, 0x95, 0x04, 0x48,
  28102. 0x3d, 0x66, 0x52, 0x99, 0x79, 0xe1, 0x8d,
  28103. 0xe0, 0xa7, 0xd5, 0x23, 0x0d, 0x45, 0x89,
  28104. 0x88, 0xa9, 0x59, 0x4e, 0xc7, 0x64, 0x39,
  28105. 0x93, 0xdd, 0xcb, 0xfe, 0x97, 0xe7, 0x7d,
  28106. 0xab, 0x61, 0x08, 0xf7, 0x7a, 0xff, 0x10,
  28107. 0x1d, 0x8d, 0x11, 0xa9, 0x97, 0xbd, 0x16,
  28108. 0xb5, 0x6c, 0x84, 0x71, 0x61, 0x72, 0x36,
  28109. 0x51, 0xe7, 0x43, 0x8b, 0x15, 0xb2, 0x48,
  28110. 0x6a, 0x14, 0x8a, 0xbe, 0x92, 0xa7, 0xfa,
  28111. 0xce, 0x02, 0x1a, 0x7f, 0xc5, 0xdb, 0x76,
  28112. 0x0a, 0x4c, 0xc7, 0x20, 0x2a, 0x34, 0xf4,
  28113. 0x92, 0x3b, 0x34, 0x69, 0x71, 0x3d, 0xe1,
  28114. 0xf2, 0x2f, 0x52, 0xe1, 0x48, 0xbe, 0x27,
  28115. 0x47, 0x1d, 0x55, 0x96, 0x6e, 0xa3, 0x39,
  28116. 0xc6, 0xd8, 0x12, 0xe0, 0xb2, 0x93, 0x56,
  28117. 0xef, 0x10, 0xf4, 0xa6, 0xf4, 0x5f, 0xa9,
  28118. 0xfd, 0x5d, 0x01, 0x87, 0xb8, 0xe5, 0x4f,
  28119. 0x86, 0x2c, 0xa5, 0x09, 0xfc, 0x29, 0x84,
  28120. 0x5b, 0x44, 0xf9, 0x8d, 0x9c, 0xbb, 0x19,
  28121. 0x97, 0x52, 0xbb, 0xac, 0x19, 0x57, 0x68,
  28122. 0x7d, 0x74, 0xb5, 0x4f, 0xda, 0x8a, 0x9c,
  28123. 0xcf, 0x5d, 0x7b, 0xd9, 0xf0, 0xb3, 0x11,
  28124. 0x76, 0x50, 0x03, 0x06, 0x44, 0xe7, 0x68,
  28125. 0x35, 0xe9, 0x14, 0x20, 0xbd, 0x0d, 0x90,
  28126. 0x96, 0x21, 0xa1, 0x17, 0x8f, 0xf7, 0x50,
  28127. 0x6c, 0xc0, 0x76, 0x3d, 0x34, 0x8d, 0xf2,
  28128. 0x75, 0xf0, 0xa2, 0x6c, 0x8a, 0xc1, 0x56,
  28129. 0x95, 0xb2, 0xd9, 0x87, 0xf2, 0xe4, 0x80,
  28130. 0x25, 0xc7, 0x97, 0xd2, 0xd2, 0xf8, 0x1c,
  28131. 0x7a, 0x48, 0x70, 0x99, 0x6b, 0xf2, 0x50,
  28132. 0x83, 0xf3, 0x10, 0xb1, 0x9b, 0x6d, 0x75,
  28133. 0x53, 0x86, 0x23, 0xc9, 0x60, 0x4d, 0x73,
  28134. 0xc7, 0x52, 0x90, 0x12, 0x6b, 0x92, 0x2d,
  28135. 0x35, 0xbc, 0x4d, 0x86, 0x67, 0xfe, 0x35,
  28136. 0x11, 0x6c, 0xbb, 0x9b, 0x76, 0xaf, 0x26,
  28137. 0xae, 0x50, 0x23, 0x76, 0x68, 0x16, 0x80,
  28138. 0xf0, 0xa4, 0xcc, 0x76, 0x6b, 0xf3, 0x99,
  28139. 0x04, 0x8b, 0x39, 0xf2, 0xa6, 0xa9, 0x72,
  28140. 0x6f, 0xbe, 0xa8, 0xdb, 0x53, 0xf3, 0x93,
  28141. 0x00, 0xac, 0x3e, 0x8f, 0xdd, 0x68, 0x9e,
  28142. 0x2f, 0xe3, 0x48, 0x0b, 0x11, 0xe2, 0x9a,
  28143. 0xfa, 0x98, 0x32, 0x40, 0x26, 0xf8, 0x83,
  28144. 0xc6, 0x00, 0x02, 0x7d, 0xb5, 0xd2, 0xd4,
  28145. 0xdd, 0xc6, 0x02, 0xec, 0xb3, 0x98, 0xd6,
  28146. 0x8e, 0xab, 0x75, 0x06, 0x37, 0x97, 0x4c,
  28147. 0x50, 0xc5, 0xe1, 0x43, 0x34, 0xd6, 0xbd,
  28148. 0xb6, 0xfc, 0xb7, 0x04, 0x0d, 0xd8, 0x35,
  28149. 0xb4, 0x3e, 0x0e, 0x43, 0x22, 0x83, 0xf5,
  28150. 0x5d, 0x2e, 0x0a, 0x8f, 0xa0, 0xec, 0x17,
  28151. 0xd9, 0xa1, 0x84, 0x98, 0x32, 0x5c, 0x99,
  28152. 0x66, 0x05, 0x70, 0x9a, 0xa4, 0x9b, 0xbe,
  28153. 0xd3, 0x3d, 0x2a, 0x13, 0xb1, 0x96, 0x37,
  28154. 0x4f, 0xe7, 0x6f, 0x6b, 0x5e, 0x80, 0xe4,
  28155. 0xb6, 0x98, 0x56, 0xed, 0xff, 0x5b, 0x21,
  28156. 0x5f, 0x79, 0x9a, 0x0f, 0x53, 0x69, 0x76,
  28157. 0xdb, 0xc0, 0x12, 0x9c, 0xed, 0xd4, 0x00,
  28158. 0x64, 0xca, 0xf4, 0xc3, 0x17, 0x49, 0xbb,
  28159. 0xef, 0xbc, 0x7b, 0x73, 0x24, 0x4f, 0x6e,
  28160. 0xcf, 0x25, 0x49, 0x30, 0x4f, 0x9a, 0xb7,
  28161. 0x2a, 0x2f, 0xc2, 0x69, 0x74, 0xe5, 0xa6,
  28162. 0xd7, 0x29, 0x4d, 0x80, 0xe6, 0xf6, 0x66,
  28163. 0x4b, 0xdf, 0xef, 0xfd, 0xb5, 0xaa, 0x53,
  28164. 0x75, 0x0e, 0xf5, 0x92, 0xb3, 0x30, 0x3d,
  28165. 0x5d, 0xa8, 0x49, 0x74, 0xa2, 0x13, 0xb3,
  28166. 0x99, 0x7e, 0xf9, 0x34, 0x08, 0xc2, 0xa6,
  28167. 0xc9, 0xd1, 0xb7, 0x65, 0xf4, 0xa9, 0xda,
  28168. 0x11, 0x07, 0x89, 0x08, 0x92, 0xdf, 0x1a,
  28169. 0x8d, 0xd7, 0x1e, 0xe9, 0xa3, 0x5c, 0x66,
  28170. 0x79, 0xa0, 0x2d, 0xd1, 0xd9, 0x65, 0xa2,
  28171. 0xd3, 0x47, 0xb1, 0xa1, 0xf4, 0xa1, 0x18,
  28172. 0x7f, 0xb0, 0xd1, 0x20, 0x05, 0x06, 0x6f,
  28173. 0xda, 0xe3, 0xef, 0xee, 0x47, 0xdf, 0x80,
  28174. 0x22, 0x14, 0x4b, 0xc2, 0xe4, 0xea, 0x02,
  28175. 0xe7, 0x06, 0xc8, 0x2d, 0x2d, 0xbd, 0xd2,
  28176. 0xef, 0xd3, 0x3b, 0xf0, 0xc8, 0xc1, 0x04,
  28177. 0x53, 0x81, 0x27, 0xb2, 0xf2, 0xf8, 0x5d,
  28178. 0xe1, 0x27, 0xd6, 0xd8, 0x2e, 0x0d, 0x43,
  28179. 0xf4, 0xf0, 0x7a, 0x8c, 0x78, 0x3f, 0x23,
  28180. 0x4d, 0x05, 0xf6, 0xc6, 0x9a, 0xc1, 0x19,
  28181. 0x58, 0x48, 0x02, 0x71, 0xc4, 0xf1, 0x16,
  28182. 0xc2, 0xbf, 0xc2, 0xf5, 0xa6, 0x70, 0x25,
  28183. 0x8b, 0x6c, 0x31, 0xa6, 0x2b, 0x6a, 0x1f,
  28184. 0x26, 0x4a, 0x6b, 0x05, 0x67, 0xa9, 0x5e,
  28185. 0xa6, 0xc7, 0x19, 0xf0, 0x6f, 0xc2, 0xa4,
  28186. 0x07, 0xe1, 0xf8, 0xb6, 0x70, 0x79, 0x85,
  28187. 0x97, 0x1f, 0xc2, 0x97, 0xf3, 0x57, 0x3d,
  28188. 0xd1, 0x70, 0xb1, 0xe8, 0x43, 0x11, 0xe6,
  28189. 0x2f, 0x8a, 0x12, 0x97, 0xe1, 0x1f, 0x86,
  28190. 0x33, 0xa1, 0x30, 0xb3, 0x8e, 0xd7, 0x43,
  28191. 0x99, 0x35, 0xd3, 0x48, 0x04, 0x29, 0xa3,
  28192. 0xf3, 0x2c, 0x0e, 0xda, 0x66, 0xc8, 0xa9,
  28193. 0xd3, 0x28, 0x7f, 0xf0, 0xaa, 0x53, 0xc8,
  28194. 0x4b, 0xa3, 0xde, 0xcc, 0x5f, 0xae, 0x9b,
  28195. 0x2f, 0x20, 0xf7, 0x9a, 0x41, 0xb7, 0xdb,
  28196. 0x7a, 0x92, 0xa1, 0x45, 0x2c, 0x49, 0xb7,
  28197. 0x2d, 0xe5, 0x6e, 0x84, 0xb1, 0xa4, 0x6e,
  28198. 0xea, 0xf9, 0xca, 0xc6, 0x0f, 0xd3, 0xdc,
  28199. 0xad, 0x79, 0xf4, 0x3b, 0xc0, 0x8c, 0x8c,
  28200. 0x6f, 0xdf, 0x2c, 0xbb, 0x2a, 0x9f, 0x74,
  28201. 0x2f, 0x31, 0x7b, 0x05, 0xc5, 0xc4, 0x22,
  28202. 0xfd, 0xfc, 0xdf, 0x95, 0x3b, 0x21, 0x41,
  28203. 0xab, 0x5b, 0xe9, 0x37, 0xdf, 0xd3, 0x4c,
  28204. 0x97, 0x73, 0xac, 0xbd, 0x82, 0x97, 0xd0,
  28205. 0x00, 0x55, 0x47, 0xa4, 0x67, 0x12, 0xcd,
  28206. 0xe3, 0x01, 0x31, 0xe6, 0x1c, 0x61, 0xf8,
  28207. 0xa4, 0xce, 0x23, 0xed, 0xeb, 0x8f, 0x57,
  28208. 0xbf, 0x97, 0x4b, 0x26, 0x75, 0xa0, 0x4b,
  28209. 0xf6, 0x7e, 0xda, 0x1c, 0x90, 0x8a, 0xc4,
  28210. 0xf4, 0x60, 0xfc, 0x65, 0x45, 0x8c, 0x1b,
  28211. 0x5c, 0x65, 0xc4, 0x5d, 0x99, 0x9a, 0xed,
  28212. 0x62, 0xd6, 0x74, 0x43, 0xac, 0x5e, 0x0c,
  28213. 0xfd, 0x7d, 0xf2, 0x4f, 0xc3, 0x02, 0xc8,
  28214. 0x24, 0xa7, 0x17, 0x49, 0xf9, 0xdd, 0xc9,
  28215. 0x64, 0x71, 0x08, 0xb9, 0xac, 0x47, 0x3d,
  28216. 0x6a, 0x4f, 0xdc, 0xa6, 0x56, 0x00, 0x01,
  28217. 0x53, 0x2d, 0xca, 0x32, 0x4e, 0x40, 0xec,
  28218. 0x14, 0x03, 0x69, 0x75, 0xfa, 0x93, 0x49,
  28219. 0x98, 0x04, 0x25, 0x29, 0xe5, 0x78, 0x6e,
  28220. 0x3e, 0x99, 0x58, 0x18, 0x48, 0xf3, 0x29,
  28221. 0x56, 0x8d, 0x23, 0xd1, 0x5d, 0xb2, 0x2a,
  28222. 0x74, 0xa7, 0x53, 0xc5, 0xc6, 0xc4, 0x12,
  28223. 0xfe, 0x65, 0x6c, 0xde, 0xc3, 0x6c, 0x18,
  28224. 0xde, 0xc3, 0x97, 0xaa, 0xed, 0x69, 0x3c,
  28225. 0x4d, 0xc9, 0xa9, 0x63, 0xa9, 0x4d, 0x91,
  28226. 0x63, 0xa3, 0x1c, 0x87, 0x36, 0x19, 0x4a,
  28227. 0xc5, 0xd1, 0xcb, 0xf4, 0x88, 0xfd, 0xa0,
  28228. 0x9b, 0x37, 0x9a, 0x7e, 0xcc, 0x09, 0xac,
  28229. 0x3b, 0xf0, 0xf5, 0xb5, 0x15, 0x72, 0x47,
  28230. 0xb0, 0x42, 0x0d, 0xed, 0x19, 0x42, 0x93,
  28231. 0x5a, 0x56, 0xbf, 0x2c, 0x4b, 0xec, 0xf4,
  28232. 0x13, 0x30, 0x0b, 0xdf, 0x0e, 0xc1, 0x22,
  28233. 0xa5, 0x6c, 0xf4, 0xcf, 0x09, 0x83, 0xe2,
  28234. 0xd0, 0x05, 0x62, 0x8d, 0xda, 0xea, 0x79,
  28235. 0xa9, 0x6c, 0xe1, 0x90, 0xc0, 0xe7, 0x6d,
  28236. 0x63, 0x8e, 0xe5, 0xe0, 0xa9, 0x67, 0x80,
  28237. 0xb7, 0x80, 0x43, 0xfc, 0xa1, 0x11, 0x6d,
  28238. 0xc9, 0x91, 0xa3, 0xcb, 0x1a, 0x6e, 0xf8,
  28239. 0x6e, 0xdb, 0xf0, 0x7d, 0xeb, 0x45, 0x6a,
  28240. 0xee, 0xd8, 0x3e, 0x6b, 0x9a, 0xc4, 0xcd,
  28241. 0x7b, 0x35, 0x19, 0x73, 0x14, 0x22, 0x47,
  28242. 0x86, 0x54, 0x34, 0xc9, 0x02, 0x49, 0xe6,
  28243. 0xb9, 0x45, 0xf7, 0x2c, 0xf7, 0xa4, 0x12,
  28244. 0xc4, 0x7a, 0x95, 0x41, 0x54, 0x8d, 0x51,
  28245. 0xc5, 0x1a, 0x19, 0x75, 0x0c, 0x11, 0x03,
  28246. 0xd0, 0x6f, 0x56, 0x04, 0xb2, 0x87, 0x21,
  28247. 0xdb, 0x47, 0xed, 0x9b, 0xe8, 0xec, 0xa3,
  28248. 0xe1, 0x74, 0x3e, 0x21, 0x50, 0xf2, 0x09,
  28249. 0x2c, 0x5e, 0x3d, 0xca, 0xa7, 0x31, 0x7c,
  28250. 0xbd, 0xe4, 0xf1, 0x15, 0x0d, 0xe6, 0x3d,
  28251. 0x4c, 0x8e, 0x36, 0x45, 0xf7, 0x08, 0x3d,
  28252. 0x56, 0x63, 0xb6, 0x99, 0x46, 0x34, 0x48,
  28253. 0xfa, 0xcf, 0x88, 0xe5, 0x43, 0xf7, 0x88,
  28254. 0xd5, 0x55, 0x13, 0xfe, 0x79, 0x02, 0x49,
  28255. 0x1e, 0x82, 0x45, 0x45, 0x29, 0x8e, 0x0a,
  28256. 0x7c, 0x77, 0x51, 0x1b, 0x8e, 0x75, 0xd5,
  28257. 0xd6, 0x61, 0xff, 0xa5, 0xfe, 0x59, 0x48,
  28258. 0xbf, 0xfc, 0xf2, 0xcd, 0x37, 0x09, 0x4c,
  28259. 0xb6, 0xfc, 0xb0, 0x20, 0x5f, 0x12, 0x7a,
  28260. 0x3f, 0x64, 0x96, 0xdb, 0xc8, 0xe8, 0xd0,
  28261. 0x11, 0x19, 0x47, 0x95, 0x2d, 0x3d, 0xbf,
  28262. 0x56, 0x9c, 0x23, 0x30, 0x07, 0x1f, 0x2c,
  28263. 0x04, 0x5c, 0x7e, 0x4e, 0x2e, 0xa6, 0x20,
  28264. 0x38, 0xa0, 0x88, 0x47, 0x8a, 0x3f, 0x8f,
  28265. 0x8a, 0xe7, 0x6d, 0x0a, 0xf1, 0x2d, 0xd6,
  28266. 0x10, 0x23, 0x01, 0x62, 0x71, 0x5a, 0xbb,
  28267. 0x51, 0x98, 0xa1, 0x27, 0x7d, 0xba, 0x5f,
  28268. 0xe4, 0xdc, 0xd6, 0xd7, 0x4c, 0x1a, 0xe0,
  28269. 0x4d, 0xe1, 0xee, 0x61, 0xb7, 0xc5, 0x50,
  28270. 0x92, 0x99, 0xc7, 0x7b, 0x18, 0xf9, 0x89,
  28271. 0x2b, 0x57, 0xf9, 0xe9, 0xac, 0x23, 0x6e,
  28272. 0xff, 0xbd, 0x5a, 0x93, 0xdf, 0x15, 0x74,
  28273. 0x92, 0x0b, 0x76, 0x84, 0x96, 0x94, 0x1b,
  28274. 0x8f, 0xe4, 0x6e, 0x2b, 0xd7, 0x47, 0xfd,
  28275. 0x24, 0x3b, 0xe4, 0xe4, 0x99, 0xf9, 0x0b,
  28276. 0xf9, 0x29, 0x25, 0x25, 0x6a, 0xc0, 0x1e,
  28277. 0xb8, 0x8c, 0xd6, 0xd0, 0x6e, 0x13, 0x86,
  28278. 0xa8, 0x7c, 0xc3, 0x31, 0x30, 0x2e, 0x9b,
  28279. 0x51, 0xc1, 0x21, 0xea, 0x15, 0x8e, 0xd8,
  28280. 0x06, 0xa2, 0xe9, 0x12, 0x9f, 0xcb, 0x6b,
  28281. 0x24, 0xef, 0x4e, 0x19, 0x6c, 0xa5, 0x98,
  28282. 0x47, 0x8b, 0x4d, 0xbe, 0x00, 0x0c, 0x04,
  28283. 0xe3, 0x4d, 0x84, 0x64, 0x36, 0x20, 0x9f,
  28284. 0xde, 0xe2, 0x55, 0x89, 0x3e, 0x40, 0xe1,
  28285. 0xe3, 0x63, 0x0a, 0xe7, 0x15, 0x4c, 0xcd,
  28286. 0x4b, 0x63, 0x6f, 0x70, 0xc2, 0x84, 0x30,
  28287. 0x5d, 0x22, 0xd0, 0xe4, 0x65, 0xfb, 0x8a,
  28288. 0x07, 0x1a, 0x54, 0xf5, 0x4b, 0x65, 0xad,
  28289. 0x64, 0x91, 0x6e, 0x23, 0x98, 0x31, 0x26,
  28290. 0x79, 0x70, 0x94, 0xff, 0xc0, 0x65, 0x70,
  28291. 0xb4, 0x9d, 0x74, 0x8e, 0x76, 0x6b, 0x9a,
  28292. 0x24, 0x28, 0x0d, 0x8a, 0x93, 0x87, 0x18,
  28293. 0x04, 0x10, 0xfd, 0x0b, 0xaf, 0xd3, 0x92,
  28294. 0xbb, 0xb5, 0x41, 0xd2, 0x87, 0xc7, 0x69,
  28295. 0x89, 0x0c, 0x52, 0xf8, 0x46, 0x70, 0x8e,
  28296. 0xf0, 0x99, 0x96, 0x57, 0x40, 0x9c, 0xef,
  28297. 0x9a, 0xc2, 0x63, 0x47, 0x13, 0x11, 0x68,
  28298. 0x40, 0xab, 0x36, 0x16, 0x53, 0xd6, 0x8f,
  28299. 0x58, 0x5a, 0xdd, 0x0c, 0xd7, 0x17, 0x6a,
  28300. 0x39, 0x34, 0xf1, 0xe7, 0x75, 0x3b, 0x41,
  28301. 0x7e, 0x5a, 0x70, 0xfe, 0x5b, 0x08, 0x30,
  28302. 0xf4, 0x7b, 0x1d, 0xd0, 0x70, 0xae, 0x18,
  28303. 0xad, 0xd4, 0xff, 0xbb, 0xa4, 0x31, 0xec,
  28304. 0x28, 0x72, 0x64, 0x9a, 0x24, 0x16, 0x30,
  28305. 0xe4, 0xc5, 0x3c, 0xb0, 0x3c, 0x81, 0x4f,
  28306. 0xb9, 0xfc, 0xe1, 0x3a, 0x05, 0x17, 0xb2,
  28307. 0x18, 0x4c, 0x98, 0x3b, 0xfc, 0x93, 0xf0,
  28308. 0x0d, 0xb6, 0x3c, 0x54, 0x7b, 0x10, 0xfd,
  28309. 0x7f, 0x63, 0xfe, 0xa5, 0xc6, 0xb5, 0x24,
  28310. 0xb4, 0xf2, 0x2f, 0xb2, 0x6f, 0x7f, 0xdb,
  28311. 0x01, 0xdb, 0xaf, 0x57, 0xdc, 0xdb, 0xf4,
  28312. 0xc8, 0x31, 0xb0, 0xdd, 0x05, 0x8b, 0x9b,
  28313. 0x6e, 0x7c, 0x5e, 0x5e, 0x31, 0x2c, 0x7f,
  28314. 0xbb, 0xa4, 0x26, 0x88, 0xe1, 0x55, 0x3d,
  28315. 0x8d, 0x36, 0x69, 0xb7, 0xc8, 0xcc, 0x05,
  28316. 0xfe, 0x6e, 0xcc, 0xcb, 0xfd, 0x81, 0x14,
  28317. 0x8b, 0xbc, 0x0c, 0xd4, 0x7e, 0xb4, 0x13,
  28318. 0xc0, 0xe2, 0x51, 0xf4, 0x07, 0xde, 0xc4,
  28319. 0x1c, 0xf0, 0xc8, 0x92, 0xd4, 0x38, 0xd7,
  28320. 0x1e, 0x57, 0xa7, 0x4b, 0xc7, 0xf8, 0xca,
  28321. 0xcc, 0x61, 0x86, 0x96, 0x50, 0x4e, 0x6a,
  28322. 0x71, 0xbd, 0x5f, 0xc6, 0x48, 0x8b, 0x6f,
  28323. 0xeb, 0x53, 0x50, 0xea, 0x35, 0x39, 0x79,
  28324. 0xcc, 0xee, 0xae, 0x81, 0x0f, 0xe2, 0xd2,
  28325. 0xbb, 0x81, 0x03, 0x8a, 0xeb, 0x98, 0xc2,
  28326. 0xad, 0xb7, 0xc0, 0x14, 0x68, 0xb9, 0x7e,
  28327. 0x8e, 0x30, 0x11, 0x43, 0x59, 0x8f, 0x04,
  28328. 0x2f, 0x7a, 0x99, 0x36, 0xd0, 0x75, 0x07,
  28329. 0x6b, 0x8e, 0xc2, 0x10, 0xac, 0xc6, 0x4d,
  28330. 0x30, 0x91, 0x9f, 0xde, 0x33, 0x0e, 0xe7,
  28331. 0xbc, 0xe5, 0x94, 0xbf, 0x80, 0xdb, 0xb7,
  28332. 0xe0, 0x8f, 0xb7, 0x74, 0xc3, 0x77, 0x18,
  28333. 0x76, 0x33, 0x34, 0xb8, 0xe4, 0x81, 0xa5,
  28334. 0xd9, 0xf7, 0x78, 0xba, 0xec, 0x62, 0x34,
  28335. 0xcb, 0x54, 0xbe, 0x90, 0xc1, 0x56, 0x59,
  28336. 0x22, 0x94, 0x5b, 0x1a, 0x8f, 0xc6, 0x8f,
  28337. 0xf2, 0x84, 0x1f, 0x61, 0x4e, 0xeb, 0x98,
  28338. 0x72, 0xdd, 0xde, 0xc9, 0xc6, 0xab, 0xd1,
  28339. 0xad, 0xd1, 0x02, 0x60, 0x82, 0x89, 0xba,
  28340. 0xf5, 0x1a, 0x6b, 0xde, 0x96, 0x19, 0x64,
  28341. 0x44, 0x0a, 0xd6, 0x27, 0x9f, 0x67, 0x96,
  28342. 0x44, 0xcc, 0x4b, 0xfd, 0x8f, 0x9e, 0xa1,
  28343. 0x1f, 0x06, 0xac, 0x2e, 0xcf, 0xdb, 0xc7,
  28344. 0x08, 0x5e, 0xe3, 0xa2, 0x59, 0xa2, 0x22,
  28345. 0xf1, 0x7a, 0xce, 0xf5, 0x30, 0x53, 0xc0,
  28346. 0xbb, 0x36, 0x13, 0x95, 0x69, 0xe2, 0x28,
  28347. 0x47, 0xad, 0xb1, 0x82, 0xfd, 0x14, 0xf6,
  28348. 0x6e, 0xed, 0x5b, 0xe0, 0xeb, 0x13, 0x5c,
  28349. 0xc6, 0x72, 0xd5, 0x2c, 0xd0, 0xae, 0xc3,
  28350. 0xad, 0xa9, 0x60, 0x2a, 0x68, 0x7b, 0x03,
  28351. 0x54, 0xc5, 0xd1, 0x71, 0xc4, 0x99, 0x48,
  28352. 0x75, 0x18, 0x1e, 0xda, 0x0a, 0x8b, 0xe1,
  28353. 0x2d, 0x67, 0x1f, 0xae, 0x91, 0xc0, 0x37,
  28354. 0x39, 0x8c, 0x7c, 0x9e, 0x42, 0xd3, 0x6e,
  28355. 0xaf, 0x7e, 0x8e, 0xa0, 0x01, 0x45, 0xc1,
  28356. 0xc6, 0xef, 0xc6, 0x2e, 0x87, 0x7b, 0x5a,
  28357. 0x60, 0xe0, 0xec, 0x3b, 0x76, 0x8e, 0xb3,
  28358. 0x0d, 0x57, 0x86, 0xa2, 0xc9, 0x1a, 0x1b,
  28359. 0x38, 0x6b, 0x75, 0x7a, 0x81, 0x8b, 0x97,
  28360. 0x99, 0xa1, 0xbb, 0xd8, 0xda, 0xdf, 0xea,
  28361. 0xef, 0xf0, 0xf9, 0x01, 0x08, 0x11, 0x18,
  28362. 0x26, 0x36, 0x4b, 0x52, 0x56, 0x68, 0x7b,
  28363. 0x98, 0xca, 0xd3, 0xd6, 0xef, 0xf6, 0x05,
  28364. 0x07, 0x09, 0x10, 0x1f, 0x21, 0x28, 0x32,
  28365. 0x39, 0x3a, 0x3c, 0x54, 0x77, 0x7b, 0x81,
  28366. 0x99, 0xa9, 0xb0, 0xb2, 0xce, 0xe5, 0xe7,
  28367. 0x1f, 0x23, 0x3f, 0x4e, 0x50, 0x5d, 0x71,
  28368. 0x7a, 0x7c, 0x91, 0xa7, 0xab, 0xae, 0xd0,
  28369. 0xd6, 0xe1, 0xe6, 0xf1, 0xf3, 0xfa, 0x00,
  28370. 0x00, 0x13, 0x24, 0x3a, 0x4e,
  28371. };
  28372. #endif
  28373. static int test_wc_dilithium(void)
  28374. {
  28375. EXPECT_DECLS;
  28376. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM)
  28377. dilithium_key* key;
  28378. byte level;
  28379. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  28380. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  28381. WC_RNG rng;
  28382. #endif
  28383. byte* privKey = NULL;
  28384. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  28385. word32 privKeyLen = DILITHIUM_MAX_KEY_SIZE;
  28386. #endif
  28387. byte* pubKey = NULL;
  28388. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  28389. word32 pubKeyLen = DILITHIUM_MAX_PUB_KEY_SIZE;
  28390. #endif
  28391. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28392. ExpectNotNull(key);
  28393. privKey = (byte*)XMALLOC(DILITHIUM_MAX_KEY_SIZE, NULL,
  28394. DYNAMIC_TYPE_TMP_BUFFER);
  28395. ExpectNotNull(privKey);
  28396. pubKey = (byte*)XMALLOC(DILITHIUM_MAX_PUB_KEY_SIZE, NULL,
  28397. DYNAMIC_TYPE_TMP_BUFFER);
  28398. ExpectNotNull(pubKey);
  28399. if (key != NULL) {
  28400. XMEMSET(key, 0, sizeof(*key));
  28401. }
  28402. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  28403. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  28404. XMEMSET(&rng, 0, sizeof(WC_RNG));
  28405. #endif
  28406. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  28407. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  28408. ExpectIntEQ(wc_InitRng(&rng), 0);
  28409. #endif
  28410. ExpectIntEQ(wc_dilithium_init(NULL), BAD_FUNC_ARG);
  28411. ExpectIntEQ(wc_dilithium_init_ex(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  28412. wc_dilithium_free(NULL);
  28413. ExpectIntEQ(wc_dilithium_init(key), 0);
  28414. wc_dilithium_free(key);
  28415. ExpectIntEQ(wc_dilithium_init_ex(key, NULL, INVALID_DEVID), 0);
  28416. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  28417. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &pubKeyLen),
  28418. BAD_FUNC_ARG);
  28419. #endif
  28420. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  28421. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &privKeyLen),
  28422. BAD_FUNC_ARG);
  28423. #endif
  28424. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  28425. ExpectIntEQ(wc_dilithium_size(NULL), BAD_FUNC_ARG);
  28426. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  28427. ExpectIntEQ(wc_dilithium_priv_size(NULL), BAD_FUNC_ARG);
  28428. #endif
  28429. #endif
  28430. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  28431. ExpectIntEQ(wc_dilithium_pub_size(NULL), BAD_FUNC_ARG);
  28432. #endif
  28433. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  28434. ExpectIntEQ(wc_dilithium_sig_size(NULL), BAD_FUNC_ARG);
  28435. #endif
  28436. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  28437. ExpectIntEQ(wc_dilithium_size(key), BAD_FUNC_ARG);
  28438. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  28439. ExpectIntEQ(wc_dilithium_priv_size(key), BAD_FUNC_ARG);
  28440. #endif
  28441. #endif
  28442. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  28443. ExpectIntEQ(wc_dilithium_pub_size(key), BAD_FUNC_ARG);
  28444. #endif
  28445. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  28446. ExpectIntEQ(wc_dilithium_sig_size(key), BAD_FUNC_ARG);
  28447. #endif
  28448. ExpectIntEQ(wc_dilithium_set_level(NULL, 0), BAD_FUNC_ARG);
  28449. ExpectIntEQ(wc_dilithium_set_level(key, 0), BAD_FUNC_ARG);
  28450. ExpectIntEQ(wc_dilithium_set_level(NULL, WC_ML_DSA_44), BAD_FUNC_ARG);
  28451. ExpectIntEQ(wc_dilithium_set_level(key, 1), BAD_FUNC_ARG);
  28452. ExpectIntEQ(wc_dilithium_set_level(key, 4), BAD_FUNC_ARG);
  28453. ExpectIntEQ(wc_dilithium_get_level(NULL, NULL), BAD_FUNC_ARG);
  28454. ExpectIntEQ(wc_dilithium_get_level(key, NULL), BAD_FUNC_ARG);
  28455. ExpectIntEQ(wc_dilithium_get_level(NULL, &level), BAD_FUNC_ARG);
  28456. ExpectIntEQ(wc_dilithium_get_level(key, &level), BAD_FUNC_ARG);
  28457. #ifndef WOLFSSL_NO_ML_DSA_87
  28458. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  28459. ExpectIntEQ(wc_dilithium_get_level(key, &level), 0);
  28460. ExpectIntEQ(level, WC_ML_DSA_87);
  28461. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  28462. ExpectIntEQ(wc_dilithium_size(key), DILITHIUM_LEVEL5_KEY_SIZE);
  28463. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  28464. ExpectIntEQ(wc_dilithium_priv_size(key), DILITHIUM_LEVEL5_PRV_KEY_SIZE);
  28465. #endif
  28466. #endif
  28467. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  28468. ExpectIntEQ(wc_dilithium_pub_size(key), DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  28469. #endif
  28470. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  28471. ExpectIntEQ(wc_dilithium_sig_size(key), DILITHIUM_LEVEL5_SIG_SIZE);
  28472. #endif
  28473. #else
  28474. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), NOT_COMPILED_IN);
  28475. #endif
  28476. #ifndef WOLFSSL_NO_ML_DSA_65
  28477. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  28478. ExpectIntEQ(wc_dilithium_get_level(key, &level), 0);
  28479. ExpectIntEQ(level, WC_ML_DSA_65);
  28480. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  28481. ExpectIntEQ(wc_dilithium_size(key), DILITHIUM_LEVEL3_KEY_SIZE);
  28482. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  28483. ExpectIntEQ(wc_dilithium_priv_size(key), DILITHIUM_LEVEL3_PRV_KEY_SIZE);
  28484. #endif
  28485. #endif
  28486. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  28487. ExpectIntEQ(wc_dilithium_pub_size(key), DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  28488. #endif
  28489. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  28490. ExpectIntEQ(wc_dilithium_sig_size(key), DILITHIUM_LEVEL3_SIG_SIZE);
  28491. #endif
  28492. #else
  28493. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), NOT_COMPILED_IN);
  28494. #endif
  28495. #ifndef WOLFSSL_NO_ML_DSA_44
  28496. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  28497. ExpectIntEQ(wc_dilithium_get_level(key, &level), 0);
  28498. ExpectIntEQ(level, WC_ML_DSA_44);
  28499. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  28500. ExpectIntEQ(wc_dilithium_size(key), DILITHIUM_LEVEL2_KEY_SIZE);
  28501. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  28502. ExpectIntEQ(wc_dilithium_priv_size(key), DILITHIUM_LEVEL2_PRV_KEY_SIZE);
  28503. #endif
  28504. #endif
  28505. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  28506. ExpectIntEQ(wc_dilithium_pub_size(key), DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  28507. #endif
  28508. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  28509. ExpectIntEQ(wc_dilithium_sig_size(key), DILITHIUM_LEVEL2_SIG_SIZE);
  28510. #endif
  28511. #else
  28512. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), NOT_COMPILED_IN);
  28513. #endif
  28514. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  28515. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &pubKeyLen),
  28516. BAD_FUNC_ARG);
  28517. #endif
  28518. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  28519. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &privKeyLen),
  28520. BAD_FUNC_ARG);
  28521. #endif
  28522. wc_dilithium_free(key);
  28523. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  28524. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  28525. wc_FreeRng(&rng);
  28526. #endif
  28527. XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28528. XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28529. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28530. #endif
  28531. return EXPECT_RESULT();
  28532. }
  28533. static int test_wc_dilithium_make_key(void)
  28534. {
  28535. EXPECT_DECLS;
  28536. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  28537. !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
  28538. dilithium_key* key;
  28539. WC_RNG rng;
  28540. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28541. ExpectNotNull(key);
  28542. if (key != NULL) {
  28543. XMEMSET(key, 0, sizeof(*key));
  28544. }
  28545. XMEMSET(&rng, 0, sizeof(WC_RNG));
  28546. ExpectIntEQ(wc_InitRng(&rng), 0);
  28547. ExpectIntEQ(wc_dilithium_init(key), 0);
  28548. ExpectIntEQ(wc_dilithium_make_key(key, &rng), BAD_STATE_E);
  28549. #ifndef WOLFSSL_NO_ML_DSA_44
  28550. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  28551. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28552. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  28553. #else
  28554. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  28555. #endif
  28556. ExpectIntEQ(wc_dilithium_make_key(NULL, NULL), BAD_FUNC_ARG);
  28557. ExpectIntEQ(wc_dilithium_make_key(key, NULL), BAD_FUNC_ARG);
  28558. ExpectIntEQ(wc_dilithium_make_key(NULL, &rng), BAD_FUNC_ARG);
  28559. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  28560. wc_dilithium_free(key);
  28561. wc_FreeRng(&rng);
  28562. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28563. #endif
  28564. return EXPECT_RESULT();
  28565. }
  28566. static int test_wc_dilithium_sign(void)
  28567. {
  28568. EXPECT_DECLS;
  28569. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  28570. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  28571. dilithium_key* key;
  28572. dilithium_key* importKey = NULL;
  28573. WC_RNG rng;
  28574. byte* privKey = NULL;
  28575. word32 privKeyLen = DILITHIUM_MAX_KEY_SIZE;
  28576. word32 badKeyLen;
  28577. byte msg[32];
  28578. byte* sig = NULL;
  28579. word32 sigLen = DILITHIUM_MAX_SIG_SIZE;
  28580. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28581. ExpectNotNull(key);
  28582. importKey = (dilithium_key*)XMALLOC(sizeof(*key), NULL,
  28583. DYNAMIC_TYPE_TMP_BUFFER);
  28584. ExpectNotNull(importKey);
  28585. privKey = (byte*)XMALLOC(DILITHIUM_MAX_KEY_SIZE, NULL,
  28586. DYNAMIC_TYPE_TMP_BUFFER);
  28587. ExpectNotNull(privKey);
  28588. sig = (byte*)XMALLOC(DILITHIUM_MAX_SIG_SIZE, NULL,
  28589. DYNAMIC_TYPE_TMP_BUFFER);
  28590. ExpectNotNull(sig);
  28591. if (key != NULL) {
  28592. XMEMSET(key, 0, sizeof(*key));
  28593. }
  28594. if (importKey != NULL) {
  28595. XMEMSET(importKey, 0, sizeof(*importKey));
  28596. }
  28597. XMEMSET(&rng, 0, sizeof(WC_RNG));
  28598. XMEMSET(msg, 0x55, sizeof(msg));
  28599. ExpectIntEQ(wc_InitRng(&rng), 0);
  28600. ExpectIntEQ(wc_dilithium_init(key), 0);
  28601. #ifndef WOLFSSL_NO_ML_DSA_44
  28602. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  28603. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28604. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  28605. #else
  28606. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  28607. #endif
  28608. #ifdef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  28609. #ifndef WOLFSSL_NO_ML_DSA_44
  28610. ExpectIntEQ(wc_dilithium_import_private(bench_dilithium_level2_key,
  28611. sizeof_bench_dilithium_level2_key, key), 0);
  28612. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28613. ExpectIntEQ(wc_dilithium_import_private(bench_dilithium_level3_key,
  28614. sizeof_bench_dilithium_level3_key, key), 0);
  28615. #else
  28616. ExpectIntEQ(wc_dilithium_import_private(bench_dilithium_level5_key,
  28617. sizeof_bench_dilithium_level5_key, key), 0);
  28618. #endif
  28619. #else
  28620. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  28621. #endif
  28622. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, NULL, NULL, NULL),
  28623. BAD_FUNC_ARG);
  28624. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, NULL, NULL, NULL, NULL),
  28625. BAD_FUNC_ARG);
  28626. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, sig, NULL, NULL, NULL),
  28627. BAD_FUNC_ARG);
  28628. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, &sigLen, NULL, NULL),
  28629. BAD_FUNC_ARG);
  28630. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, NULL, key, NULL),
  28631. BAD_FUNC_ARG);
  28632. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, NULL, NULL, &rng),
  28633. BAD_FUNC_ARG);
  28634. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, sig, &sigLen, key, &rng),
  28635. BAD_FUNC_ARG);
  28636. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, NULL, &sigLen, key, &rng),
  28637. BAD_FUNC_ARG);
  28638. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, NULL, key, &rng),
  28639. BAD_FUNC_ARG);
  28640. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, NULL, &rng),
  28641. BAD_FUNC_ARG);
  28642. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, NULL),
  28643. BAD_FUNC_ARG);
  28644. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, &rng), 0);
  28645. ExpectIntEQ(wc_dilithium_export_private(NULL, NULL, NULL),
  28646. BAD_FUNC_ARG);
  28647. ExpectIntEQ(wc_dilithium_export_private(key, NULL, NULL),
  28648. BAD_FUNC_ARG);
  28649. ExpectIntEQ(wc_dilithium_export_private(NULL, privKey, NULL),
  28650. BAD_FUNC_ARG);
  28651. ExpectIntEQ(wc_dilithium_export_private(NULL, NULL, &privKeyLen),
  28652. BAD_FUNC_ARG);
  28653. ExpectIntEQ(wc_dilithium_export_private(NULL, privKey, &privKeyLen),
  28654. BAD_FUNC_ARG);
  28655. ExpectIntEQ(wc_dilithium_export_private(key, NULL, &privKeyLen),
  28656. BAD_FUNC_ARG);
  28657. ExpectIntEQ(wc_dilithium_export_private(key, privKey, NULL),
  28658. BAD_FUNC_ARG);
  28659. badKeyLen = 0;
  28660. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &badKeyLen),
  28661. BUFFER_E);
  28662. #ifndef WOLFSSL_NO_ML_DSA_44
  28663. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL2_KEY_SIZE);
  28664. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28665. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL3_KEY_SIZE);
  28666. #else
  28667. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL5_KEY_SIZE);
  28668. #endif
  28669. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &privKeyLen),
  28670. 0);
  28671. #ifndef WOLFSSL_NO_ML_DSA_44
  28672. ExpectIntEQ(privKeyLen, DILITHIUM_LEVEL2_KEY_SIZE);
  28673. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28674. ExpectIntEQ(privKeyLen, DILITHIUM_LEVEL3_KEY_SIZE);
  28675. #else
  28676. ExpectIntEQ(privKeyLen, DILITHIUM_LEVEL5_KEY_SIZE);
  28677. #endif
  28678. ExpectIntEQ(wc_dilithium_init(importKey), 0);
  28679. ExpectIntEQ(wc_dilithium_import_private(privKey, privKeyLen, importKey),
  28680. BAD_FUNC_ARG);
  28681. #ifndef WOLFSSL_NO_ML_DSA_44
  28682. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_44), 0);
  28683. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28684. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_65), 0);
  28685. #else
  28686. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_87), 0);
  28687. #endif
  28688. ExpectIntEQ(wc_dilithium_import_private(NULL, 0, NULL),
  28689. BAD_FUNC_ARG);
  28690. ExpectIntEQ(wc_dilithium_import_private(privKey, 0, NULL),
  28691. BAD_FUNC_ARG);
  28692. ExpectIntEQ(wc_dilithium_import_private(NULL, privKeyLen, NULL),
  28693. BAD_FUNC_ARG);
  28694. ExpectIntEQ(wc_dilithium_import_private(NULL, 0, importKey),
  28695. BAD_FUNC_ARG);
  28696. ExpectIntEQ(wc_dilithium_import_private(NULL, privKeyLen, importKey),
  28697. BAD_FUNC_ARG);
  28698. ExpectIntEQ(wc_dilithium_import_private(privKey, 0, importKey),
  28699. BAD_FUNC_ARG);
  28700. ExpectIntEQ(wc_dilithium_import_private(privKey, privKeyLen, NULL),
  28701. BAD_FUNC_ARG);
  28702. ExpectIntEQ(wc_dilithium_import_private(privKey, privKeyLen, importKey),
  28703. 0);
  28704. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, &rng), 0);
  28705. #ifdef WOLFSSL_DILITHIUM_CHECK_KEY
  28706. ExpectIntEQ(wc_dilithium_check_key(importKey), PUBLIC_KEY_E);
  28707. #endif
  28708. wc_dilithium_free(importKey);
  28709. wc_dilithium_free(key);
  28710. wc_FreeRng(&rng);
  28711. XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28712. XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28713. XFREE(importKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28714. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28715. #endif
  28716. return EXPECT_RESULT();
  28717. }
  28718. static int test_wc_dilithium_verify(void)
  28719. {
  28720. EXPECT_DECLS;
  28721. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  28722. !defined(WOLFSSL_DILITHIUM_NO_VERIFY) && \
  28723. (!defined(WOLFSSL_NO_ML_DSA_44) || !defined(WOLFSSL_DILITHIUM_NO_SIGN))
  28724. dilithium_key* key;
  28725. dilithium_key* importKey = NULL;
  28726. WC_RNG rng;
  28727. byte* pubKey = NULL;
  28728. word32 pubKeyLen = DILITHIUM_MAX_PUB_KEY_SIZE;
  28729. word32 badKeyLen;
  28730. byte msg[32];
  28731. byte* sig = NULL;
  28732. word32 sigLen = DILITHIUM_MAX_SIG_SIZE;
  28733. int res;
  28734. #ifndef WOLFSSL_NO_ML_DSA_44
  28735. byte b;
  28736. #endif
  28737. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28738. ExpectNotNull(key);
  28739. importKey = (dilithium_key*)XMALLOC(sizeof(*key), NULL,
  28740. DYNAMIC_TYPE_TMP_BUFFER);
  28741. ExpectNotNull(importKey);
  28742. pubKey = (byte*)XMALLOC(DILITHIUM_MAX_PUB_KEY_SIZE, NULL,
  28743. DYNAMIC_TYPE_TMP_BUFFER);
  28744. ExpectNotNull(pubKey);
  28745. sig = (byte*)XMALLOC(DILITHIUM_MAX_SIG_SIZE, NULL,
  28746. DYNAMIC_TYPE_TMP_BUFFER);
  28747. ExpectNotNull(sig);
  28748. if (key != NULL) {
  28749. XMEMSET(key, 0, sizeof(*key));
  28750. }
  28751. if (importKey != NULL) {
  28752. XMEMSET(importKey, 0, sizeof(*importKey));
  28753. }
  28754. XMEMSET(&rng, 0, sizeof(WC_RNG));
  28755. XMEMSET(msg, 0x55, sizeof(msg));
  28756. ExpectIntEQ(wc_InitRng(&rng), 0);
  28757. ExpectIntEQ(wc_dilithium_init(key), 0);
  28758. #ifndef WOLFSSL_NO_ML_DSA_44
  28759. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  28760. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28761. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  28762. #else
  28763. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  28764. #endif
  28765. #if !defined(WOLFSSL_NO_ML_DSA_44)
  28766. ExpectIntEQ(wc_dilithium_import_public(ml_dsa_44_pub_key,
  28767. (word32)sizeof(ml_dsa_44_pub_key), key), 0);
  28768. if (sig != NULL) {
  28769. XMEMCPY(sig, ml_dsa_44_good_sig, sizeof(ml_dsa_44_good_sig));
  28770. }
  28771. sigLen = (word32)sizeof(ml_dsa_44_good_sig);
  28772. #else
  28773. #ifdef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  28774. #ifndef WOLFSSL_NO_ML_DSA_65
  28775. ExpectIntEQ(wc_dilithium_import_public(bench_dilithium_level3_pub_key,
  28776. sizeof_bench_dilithium_level3_pub_key, key), 0);
  28777. #else
  28778. ExpectIntEQ(wc_dilithium_import_public(bench_dilithium_level5_pub_key,
  28779. sizeof_bench_dilithium_level5_pub_key, key), 0);
  28780. #endif /* !WOLFSSL_NO_ML_DSA_65 */
  28781. #else
  28782. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  28783. #endif /* WOLFSSL_DILITHIUM_NO_MAKE_KEY */
  28784. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, &rng), 0);
  28785. #endif /* !WOLFSSL_NO_ML_DSA_44 */
  28786. ExpectIntEQ(wc_dilithium_export_public(NULL, NULL, NULL),
  28787. BAD_FUNC_ARG);
  28788. ExpectIntEQ(wc_dilithium_export_public(key, NULL, NULL),
  28789. BAD_FUNC_ARG);
  28790. ExpectIntEQ(wc_dilithium_export_public(NULL, pubKey, NULL),
  28791. BAD_FUNC_ARG);
  28792. ExpectIntEQ(wc_dilithium_export_public(NULL, NULL, &pubKeyLen),
  28793. BAD_FUNC_ARG);
  28794. ExpectIntEQ(wc_dilithium_export_public(NULL, pubKey, &pubKeyLen),
  28795. BAD_FUNC_ARG);
  28796. ExpectIntEQ(wc_dilithium_export_public(key, NULL, &pubKeyLen),
  28797. BAD_FUNC_ARG);
  28798. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, NULL),
  28799. BAD_FUNC_ARG);
  28800. badKeyLen = 0;
  28801. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &badKeyLen),
  28802. BUFFER_E);
  28803. #ifndef WOLFSSL_NO_ML_DSA_44
  28804. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  28805. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28806. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  28807. #else
  28808. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  28809. #endif
  28810. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &pubKeyLen), 0);
  28811. #ifndef WOLFSSL_NO_ML_DSA_44
  28812. ExpectIntEQ(pubKeyLen, DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  28813. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28814. ExpectIntEQ(pubKeyLen, DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  28815. #else
  28816. ExpectIntEQ(pubKeyLen, DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  28817. #endif
  28818. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, NULL, 32, NULL, NULL),
  28819. BAD_FUNC_ARG);
  28820. ExpectIntEQ(wc_dilithium_verify_msg(sig, 0, NULL, 32, NULL, NULL),
  28821. BAD_FUNC_ARG);
  28822. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, msg, 32, NULL, NULL),
  28823. BAD_FUNC_ARG);
  28824. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, NULL, 32, &res, NULL),
  28825. BAD_FUNC_ARG);
  28826. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, NULL, 32, NULL, key),
  28827. BAD_FUNC_ARG);
  28828. ExpectIntEQ(wc_dilithium_verify_msg(NULL, sigLen, msg, 32, &res, key),
  28829. BAD_FUNC_ARG);
  28830. ExpectIntEQ(wc_dilithium_verify_msg(sig, 0, msg, 32, &res, key),
  28831. BUFFER_E);
  28832. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, NULL, 32, &res, key),
  28833. BAD_FUNC_ARG);
  28834. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, NULL, key),
  28835. BAD_FUNC_ARG);
  28836. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, NULL),
  28837. BAD_FUNC_ARG);
  28838. res = 0;
  28839. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key), 0);
  28840. ExpectIntEQ(res, 1);
  28841. ExpectIntEQ(wc_dilithium_init(importKey), 0);
  28842. ExpectIntEQ(wc_dilithium_import_public(pubKey, pubKeyLen, importKey),
  28843. BAD_FUNC_ARG);
  28844. #ifndef WOLFSSL_NO_ML_DSA_44
  28845. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_44), 0);
  28846. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28847. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_65), 0);
  28848. #else
  28849. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_87), 0);
  28850. #endif
  28851. ExpectIntEQ(wc_dilithium_import_public(NULL, 0, NULL),
  28852. BAD_FUNC_ARG);
  28853. ExpectIntEQ(wc_dilithium_import_public(pubKey, 0, NULL),
  28854. BAD_FUNC_ARG);
  28855. ExpectIntEQ(wc_dilithium_import_public(NULL, pubKeyLen, NULL),
  28856. BAD_FUNC_ARG);
  28857. ExpectIntEQ(wc_dilithium_import_public(NULL, 0, importKey),
  28858. BAD_FUNC_ARG);
  28859. ExpectIntEQ(wc_dilithium_import_public(NULL, pubKeyLen, importKey),
  28860. BAD_FUNC_ARG);
  28861. ExpectIntEQ(wc_dilithium_import_public(pubKey, 0, importKey),
  28862. BAD_FUNC_ARG);
  28863. ExpectIntEQ(wc_dilithium_import_public(pubKey, pubKeyLen, NULL),
  28864. BAD_FUNC_ARG);
  28865. ExpectIntEQ(wc_dilithium_import_public(pubKey, pubKeyLen, importKey), 0);
  28866. res = 0;
  28867. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, importKey),
  28868. 0);
  28869. ExpectIntEQ(res, 1);
  28870. #ifdef WOLFSSL_DILITHIUM_CHECK_KEY
  28871. ExpectIntEQ(wc_dilithium_check_key(importKey), BAD_FUNC_ARG);
  28872. #endif
  28873. wc_dilithium_free(importKey);
  28874. #ifndef WOLFSSL_NO_ML_DSA_44
  28875. if (sig != NULL) {
  28876. if (sig[sigLen - 5] == 0) {
  28877. /* Unused hints meant to be 0. */
  28878. sig[sigLen - 5] = 0xff;
  28879. res = 1;
  28880. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res,
  28881. key), SIG_VERIFY_E);
  28882. ExpectIntEQ(res, 0);
  28883. sig[sigLen - 5] = 0x00;
  28884. }
  28885. /* Last count of hints must be less than PARAMS_ML_DSA_44_OMEGA == 80 */
  28886. b = sig[sigLen - 1];
  28887. sig[sigLen - 1] = 0xff;
  28888. res = 1;
  28889. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key),
  28890. SIG_VERIFY_E);
  28891. ExpectIntEQ(res, 0);
  28892. sig[sigLen - 1] = b;
  28893. if (sig[sigLen - 4] > 1) {
  28894. /* Index must be less than previous. */
  28895. b = sig[sigLen - 84];
  28896. sig[sigLen - 84] = 0xff;
  28897. res = 1;
  28898. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res,
  28899. key), SIG_VERIFY_E);
  28900. ExpectIntEQ(res, 0);
  28901. sig[sigLen - 84] = b;
  28902. }
  28903. /* Mess up commit hash. */
  28904. sig[0] ^= 0x80;
  28905. res = 1;
  28906. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key),
  28907. 0);
  28908. ExpectIntEQ(res, 0);
  28909. sig[0] ^= 0x80;
  28910. /* Mess up z. */
  28911. sig[100] ^= 0x80;
  28912. res = 1;
  28913. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key),
  28914. 0);
  28915. ExpectIntEQ(res, 0);
  28916. sig[100] ^= 0x80;
  28917. }
  28918. #endif
  28919. wc_dilithium_free(key);
  28920. wc_FreeRng(&rng);
  28921. XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28922. XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28923. XFREE(importKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28924. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28925. #endif
  28926. return EXPECT_RESULT();
  28927. }
  28928. static int test_wc_dilithium_check_key(void)
  28929. {
  28930. EXPECT_DECLS;
  28931. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  28932. defined(WOLFSSL_DILITHIUM_CHECK_KEY) && \
  28933. !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
  28934. dilithium_key* checkKey;
  28935. WC_RNG rng;
  28936. byte* privCheckKey = NULL;
  28937. word32 privCheckKeyLen = DILITHIUM_MAX_KEY_SIZE;
  28938. byte* pubCheckKey = NULL;
  28939. word32 pubCheckKeyLen = DILITHIUM_MAX_PUB_KEY_SIZE;
  28940. checkKey = (dilithium_key*)XMALLOC(sizeof(*checkKey), NULL,
  28941. DYNAMIC_TYPE_TMP_BUFFER);
  28942. ExpectNotNull(checkKey);
  28943. privCheckKey = (byte*)XMALLOC(DILITHIUM_MAX_KEY_SIZE, NULL,
  28944. DYNAMIC_TYPE_TMP_BUFFER);
  28945. ExpectNotNull(privCheckKey);
  28946. pubCheckKey = (byte*)XMALLOC(DILITHIUM_MAX_PUB_KEY_SIZE, NULL,
  28947. DYNAMIC_TYPE_TMP_BUFFER);
  28948. ExpectNotNull(pubCheckKey);
  28949. if (checkKey != NULL) {
  28950. XMEMSET(checkKey, 0, sizeof(*checkKey));
  28951. }
  28952. XMEMSET(&rng, 0, sizeof(WC_RNG));
  28953. ExpectIntEQ(wc_InitRng(&rng), 0);
  28954. ExpectIntEQ(wc_dilithium_check_key(NULL), BAD_FUNC_ARG);
  28955. ExpectIntEQ(wc_dilithium_init(checkKey), 0);
  28956. ExpectIntEQ(wc_dilithium_export_key(NULL, privCheckKey,
  28957. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  28958. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  28959. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, NULL), BAD_FUNC_ARG);
  28960. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  28961. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  28962. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  28963. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, checkKey), BAD_FUNC_ARG);
  28964. #ifndef WOLFSSL_NO_ML_DSA_44
  28965. ExpectIntEQ(wc_dilithium_set_level(checkKey, WC_ML_DSA_44), 0);
  28966. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  28967. ExpectIntEQ(wc_dilithium_set_level(checkKey, WC_ML_DSA_65), 0);
  28968. #else
  28969. ExpectIntEQ(wc_dilithium_set_level(checkKey, WC_ML_DSA_87), 0);
  28970. #endif
  28971. ExpectIntEQ(wc_dilithium_make_key(checkKey, &rng), 0);
  28972. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, NULL, NULL, NULL),
  28973. BAD_FUNC_ARG);
  28974. ExpectIntEQ(wc_dilithium_export_key(checkKey, NULL, NULL, NULL, NULL),
  28975. BAD_FUNC_ARG);
  28976. ExpectIntEQ(wc_dilithium_export_key(NULL, privCheckKey, NULL, NULL, NULL),
  28977. BAD_FUNC_ARG);
  28978. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, &privCheckKeyLen, NULL,
  28979. NULL), BAD_FUNC_ARG);
  28980. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, NULL, pubCheckKey, NULL),
  28981. BAD_FUNC_ARG);
  28982. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, NULL, NULL,
  28983. &pubCheckKeyLen), BAD_FUNC_ARG);
  28984. ExpectIntEQ(wc_dilithium_export_key(NULL , privCheckKey,
  28985. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  28986. ExpectIntEQ(wc_dilithium_export_key(checkKey, NULL ,
  28987. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  28988. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  28989. NULL , pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  28990. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  28991. &privCheckKeyLen, NULL , &pubCheckKeyLen), BAD_FUNC_ARG);
  28992. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  28993. &privCheckKeyLen, pubCheckKey, NULL ), BAD_FUNC_ARG);
  28994. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  28995. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), 0);
  28996. /* Modify hash. */
  28997. if (pubCheckKey != NULL) {
  28998. pubCheckKey[0] ^= 0x80;
  28999. ExpectIntEQ(wc_dilithium_import_key(NULL, 0, NULL, 0, NULL),
  29000. BAD_FUNC_ARG);
  29001. ExpectIntEQ(wc_dilithium_import_key(privCheckKey, 0, NULL, 0, NULL),
  29002. BAD_FUNC_ARG);
  29003. ExpectIntEQ(wc_dilithium_import_key(NULL, 0, pubCheckKey, 0, NULL),
  29004. BAD_FUNC_ARG);
  29005. ExpectIntEQ(wc_dilithium_import_key(NULL, 0, NULL, 0, checkKey),
  29006. BAD_FUNC_ARG);
  29007. ExpectIntEQ(wc_dilithium_import_key(NULL ,
  29008. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, checkKey),
  29009. BAD_FUNC_ARG);
  29010. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  29011. 0 , pubCheckKey, pubCheckKeyLen, checkKey),
  29012. BAD_FUNC_ARG);
  29013. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  29014. privCheckKeyLen, NULL , pubCheckKeyLen, checkKey),
  29015. BAD_FUNC_ARG);
  29016. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  29017. privCheckKeyLen, pubCheckKey, 0 , checkKey),
  29018. BAD_FUNC_ARG);
  29019. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  29020. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, NULL ),
  29021. BAD_FUNC_ARG);
  29022. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  29023. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, checkKey), 0);
  29024. ExpectIntEQ(wc_dilithium_check_key(checkKey), PUBLIC_KEY_E);
  29025. pubCheckKey[0] ^= 0x80;
  29026. /* Modify encoded t1. */
  29027. pubCheckKey[48] ^= 0x80;
  29028. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  29029. privCheckKeyLen,pubCheckKey, pubCheckKeyLen, checkKey), 0);
  29030. ExpectIntEQ(wc_dilithium_check_key(checkKey), PUBLIC_KEY_E);
  29031. pubCheckKey[48] ^= 0x80;
  29032. }
  29033. wc_dilithium_free(checkKey);
  29034. wc_FreeRng(&rng);
  29035. XFREE(pubCheckKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29036. XFREE(privCheckKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29037. XFREE(checkKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29038. #endif
  29039. return EXPECT_RESULT();
  29040. }
  29041. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  29042. defined(WOLFSSL_DILITHIUM_PUBLIC_KEY)
  29043. static const unsigned char dilithium_public_der[] = {
  29044. #ifndef WOLFSSL_NO_ML_DSA_44
  29045. 0x30, 0x82, 0x05, 0x34, 0x30, 0x0d, 0x06, 0x0b,
  29046. 0x2b, 0x06, 0x01, 0x04, 0x01, 0x02, 0x82, 0x0b,
  29047. 0x0c, 0x04, 0x04, 0x03, 0x82, 0x05, 0x21, 0x00,
  29048. 0x0a, 0xf7, 0xc8, 0xa4, 0x96, 0x01, 0xa7, 0xb2,
  29049. 0x2e, 0x4d, 0xc9, 0xd9, 0x1c, 0xa1, 0x86, 0x09,
  29050. 0xce, 0x14, 0x6f, 0xe8, 0x33, 0x3c, 0x7b, 0xdb,
  29051. 0x19, 0x9c, 0x56, 0x39, 0x6a, 0x6c, 0x5d, 0x1f,
  29052. 0xe4, 0x26, 0xcb, 0x16, 0x91, 0x4d, 0xeb, 0x5a,
  29053. 0x36, 0x22, 0xee, 0xda, 0xdf, 0x46, 0x3e, 0xa1,
  29054. 0x4f, 0x9a, 0x30, 0xb5, 0x3f, 0x60, 0xf7, 0x75,
  29055. 0x47, 0xdc, 0x55, 0xf1, 0xbe, 0xbc, 0x87, 0x6c,
  29056. 0x50, 0x7c, 0x21, 0x55, 0x35, 0xad, 0xa7, 0xf9,
  29057. 0x1c, 0xf8, 0xa1, 0x92, 0x79, 0x10, 0x52, 0x7a,
  29058. 0xc3, 0xba, 0xd3, 0x9d, 0xc6, 0x9b, 0xf4, 0xcb,
  29059. 0x1b, 0xa2, 0xde, 0x83, 0x86, 0xa6, 0x35, 0xea,
  29060. 0xf2, 0x8c, 0xdc, 0xba, 0x3e, 0xef, 0x9c, 0xf5,
  29061. 0x8e, 0xc3, 0xb0, 0xc0, 0x5b, 0xcc, 0x35, 0x6a,
  29062. 0x81, 0xe5, 0x17, 0xb3, 0x9a, 0x57, 0xa6, 0x4a,
  29063. 0x87, 0xb1, 0xa7, 0xf5, 0xa2, 0x96, 0x40, 0x8b,
  29064. 0xc1, 0x62, 0xb2, 0xd9, 0x76, 0xe8, 0x51, 0x33,
  29065. 0x44, 0x3d, 0xeb, 0x14, 0x86, 0x88, 0x2c, 0xc1,
  29066. 0x47, 0xba, 0x2b, 0x85, 0x3b, 0x72, 0xcb, 0x9f,
  29067. 0x40, 0xba, 0x19, 0x58, 0xa4, 0x34, 0x0a, 0xd2,
  29068. 0x8c, 0x97, 0xbd, 0x3d, 0x09, 0xb0, 0x4a, 0xeb,
  29069. 0xaa, 0xee, 0x58, 0x1e, 0xc1, 0x19, 0x26, 0x70,
  29070. 0x15, 0xa5, 0x17, 0x7e, 0xd0, 0xa1, 0x08, 0xf9,
  29071. 0x6d, 0xcf, 0x20, 0x62, 0x95, 0x8e, 0x61, 0xf4,
  29072. 0x29, 0x96, 0x6f, 0x38, 0x1c, 0x67, 0xd5, 0xa6,
  29073. 0x4c, 0xf5, 0x1f, 0xda, 0x12, 0x22, 0x24, 0x6b,
  29074. 0x0d, 0xb7, 0x6a, 0xe5, 0xaf, 0x6c, 0x89, 0x52,
  29075. 0xc2, 0x85, 0x85, 0x5f, 0x16, 0x33, 0x0c, 0xc6,
  29076. 0x7a, 0xe0, 0xa8, 0xed, 0x13, 0x58, 0xf3, 0xa0,
  29077. 0x80, 0x42, 0x3c, 0xe3, 0x57, 0xd1, 0xe2, 0x66,
  29078. 0xc4, 0xe0, 0x3d, 0x49, 0x32, 0x21, 0xd9, 0xa1,
  29079. 0x3c, 0x93, 0x0a, 0xf7, 0x5f, 0x34, 0x65, 0xa4,
  29080. 0x30, 0xf9, 0xe7, 0x8a, 0x96, 0x04, 0xdb, 0xc5,
  29081. 0x16, 0x15, 0x10, 0x74, 0x4f, 0xc9, 0x6b, 0x4b,
  29082. 0x66, 0x29, 0xb0, 0xd1, 0x3b, 0xdd, 0x41, 0x0a,
  29083. 0xfe, 0xdf, 0x5f, 0x72, 0x91, 0xbc, 0x99, 0x2f,
  29084. 0x8d, 0x72, 0x3a, 0x4a, 0xde, 0x11, 0x3a, 0x20,
  29085. 0xb2, 0x56, 0xb5, 0x73, 0x89, 0xb4, 0x63, 0x37,
  29086. 0x86, 0xbd, 0x99, 0x8b, 0x03, 0x56, 0x50, 0x21,
  29087. 0x11, 0x78, 0x8c, 0xd5, 0xc1, 0x92, 0x33, 0x72,
  29088. 0x6e, 0x8d, 0x88, 0x2d, 0x10, 0x8f, 0x31, 0xd3,
  29089. 0x23, 0xe5, 0xaa, 0x1f, 0xe1, 0x37, 0xec, 0x34,
  29090. 0x42, 0x30, 0x75, 0xff, 0xb2, 0x1a, 0x8e, 0x29,
  29091. 0x03, 0x4c, 0xfd, 0xdf, 0x53, 0xf2, 0x0b, 0x2d,
  29092. 0xf9, 0x1c, 0x9e, 0xb6, 0x5a, 0x6c, 0x5e, 0x88,
  29093. 0x48, 0x29, 0x89, 0x42, 0xfc, 0x97, 0xfb, 0x27,
  29094. 0x1c, 0x99, 0x2a, 0xbf, 0x7f, 0x04, 0xb2, 0xcd,
  29095. 0xc9, 0x3a, 0x39, 0xfe, 0x4f, 0x47, 0x92, 0x0b,
  29096. 0x85, 0xfc, 0x92, 0x57, 0xc5, 0x0b, 0x23, 0x1f,
  29097. 0x0b, 0x72, 0xb4, 0xde, 0xfe, 0xbe, 0xb7, 0x39,
  29098. 0xb3, 0xd7, 0x48, 0x03, 0xed, 0x76, 0xac, 0x63,
  29099. 0xf7, 0x2a, 0x58, 0xef, 0xdb, 0x63, 0x5a, 0x56,
  29100. 0x68, 0xcc, 0xb2, 0x8b, 0x22, 0xac, 0xdf, 0xc4,
  29101. 0xad, 0x6f, 0xad, 0x24, 0xfd, 0x30, 0xfb, 0xed,
  29102. 0x6e, 0xde, 0x65, 0x2b, 0xb4, 0x57, 0x35, 0x49,
  29103. 0xc1, 0xc9, 0x82, 0xf4, 0x72, 0x69, 0xef, 0x34,
  29104. 0xc0, 0x37, 0x8b, 0x8b, 0xd3, 0xd3, 0x25, 0xcc,
  29105. 0xe5, 0xf5, 0xf6, 0x9c, 0xa3, 0xe7, 0x88, 0xd7,
  29106. 0x55, 0x73, 0x31, 0x4c, 0xb1, 0x7b, 0x64, 0xb3,
  29107. 0x38, 0xde, 0x47, 0x9a, 0xfc, 0xf1, 0xfa, 0xf8,
  29108. 0x6e, 0xc5, 0x95, 0xb9, 0xaf, 0x6a, 0x7a, 0x94,
  29109. 0x80, 0x0d, 0x29, 0x62, 0x99, 0x0a, 0x34, 0xa2,
  29110. 0x8f, 0xa1, 0x5e, 0x98, 0x7c, 0x4e, 0x18, 0xcd,
  29111. 0x63, 0x68, 0x0e, 0xfa, 0x6f, 0x49, 0x01, 0x02,
  29112. 0xcd, 0xf1, 0xc1, 0x09, 0x57, 0xa3, 0x03, 0xec,
  29113. 0x94, 0x36, 0xab, 0xc6, 0x1c, 0xc0, 0x98, 0x22,
  29114. 0x15, 0x5b, 0x5b, 0x61, 0x3c, 0xc2, 0x5b, 0x6f,
  29115. 0x1c, 0x82, 0x41, 0x39, 0x87, 0xde, 0x92, 0xa9,
  29116. 0xe4, 0x12, 0x74, 0x3b, 0x31, 0x36, 0xac, 0x92,
  29117. 0xb0, 0x23, 0x26, 0xfa, 0xd8, 0xa3, 0xe8, 0x84,
  29118. 0xfc, 0x52, 0xc5, 0x7b, 0xd1, 0x4b, 0xe2, 0x1a,
  29119. 0x33, 0xdd, 0x3c, 0xdf, 0x27, 0x50, 0x6f, 0x12,
  29120. 0xd3, 0x17, 0x66, 0xd7, 0x54, 0x33, 0x30, 0x2b,
  29121. 0xe8, 0xd1, 0x1f, 0x2d, 0xf3, 0x37, 0x81, 0xa0,
  29122. 0x3c, 0x21, 0x8c, 0xea, 0x95, 0xa5, 0x5b, 0x3a,
  29123. 0x24, 0xed, 0xf7, 0x67, 0x7b, 0x72, 0x3a, 0xda,
  29124. 0x31, 0xbd, 0xa7, 0x63, 0xa6, 0x6f, 0xf9, 0xdf,
  29125. 0x06, 0x36, 0xb4, 0xe2, 0x35, 0x4b, 0xa5, 0x8e,
  29126. 0x29, 0x8e, 0x6c, 0x02, 0xc5, 0x06, 0x9b, 0x98,
  29127. 0x6e, 0x5e, 0x00, 0x6a, 0x42, 0x09, 0x4b, 0xc3,
  29128. 0x09, 0x37, 0x67, 0x19, 0x58, 0x6d, 0x40, 0x50,
  29129. 0xb0, 0x62, 0x5b, 0xd6, 0x63, 0x7f, 0xed, 0xb0,
  29130. 0x97, 0x80, 0x9e, 0x91, 0x3f, 0x82, 0xfd, 0x83,
  29131. 0x36, 0xce, 0x06, 0xc4, 0xdc, 0xa4, 0x1e, 0x70,
  29132. 0xd4, 0x94, 0xfc, 0x6e, 0x46, 0xa3, 0xc8, 0xed,
  29133. 0x34, 0x0a, 0xb1, 0x9a, 0x66, 0x5d, 0xc0, 0xce,
  29134. 0x73, 0xd3, 0x65, 0xcb, 0xfb, 0x79, 0xdd, 0xf6,
  29135. 0x19, 0xf6, 0xd8, 0xa9, 0xe6, 0x34, 0x15, 0x86,
  29136. 0x7a, 0x30, 0x79, 0xde, 0x2b, 0x06, 0xa4, 0xc0,
  29137. 0xc8, 0xa2, 0xc1, 0x41, 0xb3, 0x4c, 0xf6, 0xdb,
  29138. 0x16, 0xcd, 0xd2, 0x8b, 0xf1, 0x18, 0x5a, 0xc8,
  29139. 0x3e, 0xd9, 0x54, 0x40, 0xd4, 0xce, 0x88, 0xbb,
  29140. 0x66, 0xf1, 0x74, 0x20, 0xa2, 0x3c, 0x31, 0x09,
  29141. 0xba, 0xac, 0x61, 0x15, 0x9f, 0x73, 0x5f, 0xa7,
  29142. 0xe5, 0x0d, 0xb3, 0xab, 0xa2, 0x72, 0x25, 0xc9,
  29143. 0x87, 0x9b, 0x18, 0xdb, 0xff, 0xfb, 0x39, 0x84,
  29144. 0x8d, 0xf8, 0x97, 0x47, 0xab, 0xc4, 0xfb, 0xc2,
  29145. 0xd8, 0xe8, 0xce, 0x6e, 0x65, 0x76, 0x88, 0x4a,
  29146. 0x22, 0x2f, 0xdd, 0x43, 0xa7, 0xc4, 0x8d, 0x32,
  29147. 0x12, 0x75, 0x0b, 0x72, 0xd6, 0xb7, 0x43, 0x84,
  29148. 0xc8, 0x59, 0xa8, 0xb7, 0x8b, 0x84, 0x33, 0x92,
  29149. 0x8f, 0x94, 0xe8, 0xd0, 0xaf, 0x11, 0x35, 0xde,
  29150. 0xb7, 0x63, 0xb8, 0x91, 0x4c, 0x96, 0x4e, 0x9c,
  29151. 0x62, 0x28, 0xa2, 0xbc, 0x0b, 0x90, 0xae, 0x94,
  29152. 0x90, 0xe9, 0x32, 0xeb, 0xe3, 0x77, 0x60, 0x5f,
  29153. 0x87, 0x48, 0x4b, 0xb0, 0x78, 0x0e, 0xe2, 0x85,
  29154. 0x47, 0x06, 0xa4, 0xc9, 0x26, 0xac, 0x8f, 0xe7,
  29155. 0xc2, 0xc7, 0xce, 0xf5, 0xd1, 0x20, 0xa8, 0x56,
  29156. 0xe1, 0x4f, 0x50, 0x90, 0xb3, 0xc1, 0x03, 0x57,
  29157. 0xd3, 0x62, 0x0e, 0x2a, 0xe8, 0x86, 0xf4, 0x94,
  29158. 0x0e, 0xa5, 0x8b, 0x4e, 0x73, 0xa2, 0x76, 0xac,
  29159. 0x00, 0x29, 0xe5, 0x80, 0x26, 0x02, 0x13, 0xd1,
  29160. 0xb2, 0x68, 0x72, 0x23, 0x38, 0x55, 0xfc, 0x4d,
  29161. 0x05, 0x60, 0x49, 0x7b, 0xfb, 0xaa, 0x17, 0x8f,
  29162. 0x26, 0x0a, 0x08, 0x33, 0x8d, 0x7f, 0x4e, 0xe5,
  29163. 0x6e, 0xf8, 0x84, 0x9b, 0x9f, 0xcb, 0xa2, 0x2b,
  29164. 0xfb, 0xaf, 0xad, 0x21, 0xe2, 0x4f, 0x6f, 0x55,
  29165. 0xc1, 0x78, 0x46, 0xe3, 0xb5, 0x63, 0x06, 0x9b,
  29166. 0x93, 0x7d, 0xac, 0xd4, 0xe0, 0x64, 0x01, 0x8d,
  29167. 0xac, 0x30, 0x8b, 0x8b, 0x55, 0xb7, 0x8a, 0x16,
  29168. 0x3f, 0xc9, 0x82, 0x7f, 0xb5, 0x3b, 0x0d, 0xc0,
  29169. 0x46, 0x89, 0x5c, 0x6c, 0x45, 0x21, 0x78, 0xda,
  29170. 0x84, 0x1f, 0xc8, 0xcf, 0xf1, 0x1e, 0x79, 0x71,
  29171. 0x3b, 0xc8, 0xe2, 0x8b, 0x41, 0xfe, 0xaf, 0x2f,
  29172. 0x3b, 0x23, 0x13, 0xc5, 0x46, 0x87, 0xc6, 0x24,
  29173. 0x37, 0x21, 0x68, 0x8a, 0x3e, 0x45, 0x61, 0xf4,
  29174. 0xad, 0xf5, 0x1c, 0x23, 0x45, 0xa3, 0x42, 0xf2,
  29175. 0xa9, 0xac, 0x94, 0x50, 0xc9, 0x3d, 0x5e, 0x70,
  29176. 0x33, 0x2b, 0x78, 0xd1, 0x5c, 0x13, 0x35, 0xe6,
  29177. 0x13, 0x80, 0x5e, 0x55, 0xa7, 0xcc, 0x67, 0xb0,
  29178. 0x6c, 0xfe, 0xa2, 0x24, 0x02, 0x6d, 0xb3, 0xcb,
  29179. 0x9e, 0x94, 0xb3, 0xc6, 0x01, 0xf3, 0x01, 0x3a,
  29180. 0xe4, 0xa7, 0xa3, 0xdf, 0x56, 0x4c, 0x30, 0xce,
  29181. 0xb1, 0xd5, 0x1b, 0x68, 0x9b, 0x75, 0xae, 0xf4,
  29182. 0xb9, 0x2a, 0xe5, 0x8b, 0x7b, 0xe5, 0x99, 0x46,
  29183. 0x5f, 0x29, 0xf6, 0x82, 0xd0, 0x42, 0xb1, 0x45,
  29184. 0x09, 0x16, 0x5b, 0x32, 0x11, 0xca, 0x48, 0xea,
  29185. 0x51, 0x12, 0x0a, 0x9f, 0x6e, 0x3f, 0x74, 0xe6,
  29186. 0xe0, 0xfe, 0xf8, 0xa5, 0xc0, 0xfd, 0x15, 0x6e,
  29187. 0x2b, 0x4a, 0xd5, 0x76, 0xa8, 0x3d, 0xe3, 0x0d,
  29188. 0xfe, 0x44, 0x11, 0x5e, 0x7a, 0xde, 0x12, 0x29,
  29189. 0x5a, 0x5a, 0x25, 0xc0, 0x8e, 0x98, 0xd1, 0x11,
  29190. 0xc8, 0x00, 0x65, 0xb2, 0xf4, 0xd7, 0x56, 0x32,
  29191. 0x46, 0x2b, 0x4f, 0x7e, 0xc3, 0x4e, 0xf1, 0x17,
  29192. 0xff, 0x03, 0x32, 0xae, 0xe3, 0xbe, 0x0b, 0xab,
  29193. 0xfb, 0x43, 0x0f, 0x6d, 0xa5, 0xc6, 0x44, 0xba,
  29194. 0xc9, 0xe3, 0x3d, 0x40, 0xe7, 0x6c, 0xe8, 0x21,
  29195. 0xb2, 0x46, 0x7b, 0x3b, 0x3d, 0xde, 0x80, 0xc8,
  29196. 0xea, 0xf4, 0x6b, 0xf3, 0x53, 0xca, 0x51, 0x84,
  29197. 0xcf, 0xad, 0x7e, 0xce, 0xce, 0xc2, 0x65, 0xfc,
  29198. 0x03, 0x8c, 0xcb, 0xfa, 0xcb, 0x37, 0x89, 0x82,
  29199. 0x59, 0x5e, 0x36, 0x52, 0xe4, 0xbc, 0x8d, 0x47,
  29200. 0x7c, 0xb8, 0x3f, 0x63, 0x59, 0xdc, 0xd3, 0x74,
  29201. 0x11, 0x33, 0xb4, 0x69, 0x74, 0x40, 0x0d, 0x42,
  29202. 0x63, 0x1d, 0xe6, 0x5c, 0x1b, 0xca, 0x41, 0xff,
  29203. 0x23, 0x4e, 0xe8, 0x3d, 0x14, 0xa8, 0x17, 0x18,
  29204. 0xd0, 0x78, 0x08, 0x87, 0x7d, 0x5e, 0xdc, 0x3a,
  29205. 0x07, 0xba, 0x12, 0x8e, 0x8e, 0x56, 0x0a, 0xcb,
  29206. 0x37, 0xf6, 0x54, 0xeb, 0x55, 0x16, 0x8f, 0x06,
  29207. 0x15, 0x28, 0x6b, 0xfb, 0xed, 0x38, 0x9e, 0x9b,
  29208. 0x98, 0x5b, 0xdc, 0x67, 0x33, 0x0e, 0x02, 0x36,
  29209. 0x1b, 0x7a, 0x9a, 0x43, 0xcd, 0xf2, 0x65, 0xef,
  29210. 0x37, 0x19, 0x24, 0x6f, 0x4b, 0xb9, 0x4d, 0x3e,
  29211. 0x0b, 0x47, 0xd1, 0x67, 0x50, 0x6a, 0x7f, 0x07
  29212. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  29213. 0x30, 0x82, 0x07, 0xb4, 0x30, 0x0d, 0x06, 0x0b,
  29214. 0x2b, 0x06, 0x01, 0x04, 0x01, 0x02, 0x82, 0x0b,
  29215. 0x0c, 0x06, 0x05, 0x03, 0x82, 0x07, 0xa1, 0x00,
  29216. 0xff, 0x89, 0xee, 0xad, 0x20, 0x8f, 0x61, 0xa4,
  29217. 0x07, 0x1c, 0x54, 0x98, 0x8c, 0xf4, 0x2e, 0xd9,
  29218. 0xe6, 0x0f, 0xcb, 0x0e, 0xab, 0xa1, 0x37, 0x4d,
  29219. 0xc0, 0x48, 0x24, 0x78, 0xd6, 0x2d, 0x9b, 0x6f,
  29220. 0x0f, 0x17, 0x08, 0x71, 0xc3, 0xd1, 0xc8, 0x7a,
  29221. 0xe7, 0x32, 0xcb, 0xcd, 0xd6, 0xb5, 0x90, 0x08,
  29222. 0xe1, 0xda, 0xaa, 0x89, 0x3e, 0x4a, 0x62, 0x98,
  29223. 0x3d, 0xc6, 0x71, 0x30, 0xb4, 0x63, 0xa5, 0x3b,
  29224. 0xb3, 0x69, 0x75, 0x10, 0xaf, 0x5e, 0x72, 0x78,
  29225. 0xa2, 0xef, 0x63, 0x63, 0x21, 0xe7, 0xf4, 0xa7,
  29226. 0x9c, 0x50, 0x74, 0x14, 0x3e, 0xdd, 0x73, 0x9e,
  29227. 0x97, 0x65, 0xdd, 0xdf, 0x3c, 0x40, 0x4d, 0x03,
  29228. 0x49, 0xe4, 0xbf, 0x65, 0xe7, 0x44, 0x8f, 0x59,
  29229. 0x00, 0xe2, 0x98, 0xb5, 0x66, 0xa3, 0x3b, 0x11,
  29230. 0x9f, 0xc7, 0xc2, 0x16, 0x61, 0xf0, 0x1e, 0x89,
  29231. 0xc8, 0x96, 0x8d, 0x18, 0xac, 0x86, 0xa0, 0xe2,
  29232. 0xd9, 0x8c, 0xef, 0x53, 0x6d, 0x4e, 0x74, 0xc9,
  29233. 0x66, 0x28, 0x16, 0xf3, 0x62, 0xc4, 0x6f, 0x2b,
  29234. 0x6e, 0x36, 0x03, 0xad, 0xc5, 0xe4, 0x8f, 0x0b,
  29235. 0x90, 0x8c, 0x8f, 0xff, 0x5d, 0xdf, 0x7a, 0xe6,
  29236. 0xaf, 0x9a, 0x43, 0xbc, 0xd4, 0x73, 0x22, 0xdc,
  29237. 0x5f, 0x08, 0xa1, 0x17, 0x97, 0x89, 0x79, 0xf5,
  29238. 0xdc, 0xed, 0x4f, 0x85, 0x8e, 0x0c, 0x23, 0x35,
  29239. 0x3c, 0x34, 0x19, 0x65, 0xf5, 0xd6, 0xc9, 0x2d,
  29240. 0x7a, 0x2e, 0x67, 0xd5, 0xf1, 0x82, 0x97, 0xaa,
  29241. 0x05, 0x26, 0x84, 0x25, 0x47, 0x58, 0x2c, 0xe6,
  29242. 0x59, 0xc7, 0x98, 0x7a, 0xdb, 0x40, 0x45, 0x1c,
  29243. 0x71, 0x55, 0x2e, 0xea, 0x3f, 0x6e, 0x7c, 0x82,
  29244. 0x52, 0x6a, 0x19, 0x3a, 0xd3, 0xa1, 0x3c, 0xce,
  29245. 0x00, 0x06, 0xec, 0xed, 0x97, 0xce, 0xd8, 0xdf,
  29246. 0xde, 0xa3, 0xed, 0xe7, 0x81, 0x62, 0x02, 0x9c,
  29247. 0x1b, 0x51, 0xa1, 0xf4, 0x9d, 0x1b, 0x28, 0x76,
  29248. 0x93, 0x96, 0x20, 0x55, 0x60, 0x1f, 0xaf, 0x52,
  29249. 0xc3, 0xce, 0xb9, 0x12, 0x66, 0xf5, 0x64, 0x22,
  29250. 0x87, 0x86, 0x29, 0x80, 0x8f, 0x18, 0x33, 0xba,
  29251. 0x48, 0x71, 0x1d, 0x00, 0xfe, 0xa5, 0xfc, 0xc6,
  29252. 0x87, 0xbe, 0x44, 0x3c, 0xc9, 0x49, 0xfb, 0x68,
  29253. 0x3c, 0xdf, 0xca, 0xef, 0xa7, 0xdc, 0x67, 0xb8,
  29254. 0x28, 0xd6, 0xad, 0x18, 0xaf, 0xad, 0x1f, 0x4c,
  29255. 0x85, 0xa3, 0x64, 0xac, 0x3f, 0xa9, 0x39, 0x28,
  29256. 0xef, 0x8a, 0x45, 0x7e, 0xb0, 0xf4, 0x89, 0x72,
  29257. 0xf7, 0xb1, 0xef, 0x9d, 0x1c, 0x3c, 0x93, 0xcb,
  29258. 0xa0, 0xfb, 0x2a, 0x90, 0xe2, 0x1d, 0x49, 0x8e,
  29259. 0x36, 0xb8, 0x07, 0xf4, 0xb3, 0x09, 0xf0, 0x6f,
  29260. 0x3c, 0xd9, 0x37, 0x19, 0x57, 0xd4, 0x1e, 0x2a,
  29261. 0xa2, 0xa7, 0x2e, 0xc1, 0xcd, 0x8d, 0x48, 0x47,
  29262. 0xb5, 0x8a, 0x12, 0x93, 0x34, 0xb8, 0xec, 0x32,
  29263. 0x07, 0x49, 0xb6, 0x8d, 0x73, 0xd4, 0x2c, 0x6a,
  29264. 0xa0, 0x33, 0x29, 0x21, 0x5d, 0x37, 0xa9, 0x39,
  29265. 0x40, 0xbe, 0x71, 0x29, 0xbe, 0xd1, 0x4b, 0xbc,
  29266. 0x9a, 0x17, 0x93, 0x52, 0xb8, 0x81, 0xee, 0xc5,
  29267. 0xff, 0x25, 0x78, 0x2f, 0x52, 0x0a, 0x8f, 0xb2,
  29268. 0xef, 0xf3, 0x1d, 0x68, 0x56, 0x31, 0x29, 0x84,
  29269. 0x55, 0x47, 0x32, 0x34, 0x0f, 0x60, 0x07, 0xd6,
  29270. 0x2b, 0xb9, 0x29, 0xaf, 0x0f, 0xcd, 0x1c, 0xc0,
  29271. 0x77, 0x4c, 0xc6, 0x31, 0xdb, 0xf4, 0x17, 0xbe,
  29272. 0x3d, 0xf8, 0x8c, 0xf1, 0x02, 0x7c, 0x6b, 0xd4,
  29273. 0xaf, 0x03, 0xb2, 0xf4, 0x78, 0x8d, 0xd3, 0x4e,
  29274. 0x5c, 0x04, 0xb9, 0x01, 0xe3, 0x73, 0xb4, 0x67,
  29275. 0xe9, 0xa8, 0x77, 0x6f, 0x87, 0x2b, 0xe2, 0x00,
  29276. 0x98, 0x5f, 0x02, 0x43, 0x85, 0x03, 0x4c, 0x71,
  29277. 0xd2, 0xe7, 0x61, 0x03, 0x22, 0x9e, 0xe5, 0xc2,
  29278. 0xa7, 0x66, 0x42, 0x7c, 0x9f, 0xf4, 0xb8, 0x6b,
  29279. 0x2d, 0xe4, 0xaa, 0x51, 0xda, 0x08, 0x73, 0x75,
  29280. 0x26, 0x45, 0xdc, 0xa6, 0x20, 0xd7, 0xcb, 0x00,
  29281. 0xfc, 0xe4, 0xdb, 0x28, 0x92, 0xf8, 0xb0, 0xc7,
  29282. 0xf0, 0x4b, 0x6d, 0xe8, 0xc1, 0x84, 0x38, 0xed,
  29283. 0x1a, 0xd4, 0x66, 0x69, 0xc4, 0x96, 0x40, 0xc4,
  29284. 0x7d, 0xfa, 0x58, 0x70, 0x7e, 0x70, 0x40, 0xba,
  29285. 0xfc, 0x95, 0xb6, 0x4c, 0x7c, 0x58, 0xbc, 0xb3,
  29286. 0x59, 0x08, 0x14, 0x03, 0x35, 0xf3, 0xf1, 0xaa,
  29287. 0xd5, 0xa2, 0x57, 0x70, 0xb6, 0x20, 0x75, 0x0a,
  29288. 0x58, 0x66, 0x74, 0xf7, 0x1c, 0xfd, 0x99, 0x7c,
  29289. 0x20, 0xda, 0xe7, 0x76, 0xcb, 0xf4, 0xa3, 0x9b,
  29290. 0xbc, 0x8f, 0x74, 0xef, 0xe2, 0x46, 0x5a, 0x72,
  29291. 0x33, 0x06, 0x32, 0x1e, 0xbd, 0x4e, 0x4c, 0xf6,
  29292. 0x16, 0x43, 0xa5, 0xa5, 0xa5, 0x6c, 0x76, 0x33,
  29293. 0x35, 0x63, 0xdc, 0xe4, 0xec, 0x7f, 0x8a, 0xfa,
  29294. 0xc3, 0x53, 0x69, 0x28, 0xf7, 0xd6, 0x97, 0xb9,
  29295. 0x3a, 0xf4, 0x15, 0x90, 0x50, 0xd3, 0xdf, 0xf5,
  29296. 0xd3, 0xcf, 0x15, 0x76, 0xe3, 0x3d, 0x24, 0x14,
  29297. 0xfd, 0xd3, 0x01, 0x25, 0x82, 0xb4, 0xe3, 0xd8,
  29298. 0x68, 0x89, 0x86, 0xa8, 0x26, 0x02, 0x5f, 0xc6,
  29299. 0xf4, 0x99, 0x3b, 0x97, 0xa8, 0x65, 0xed, 0x18,
  29300. 0xbb, 0x3c, 0x43, 0x4a, 0x6e, 0xaa, 0xbc, 0x83,
  29301. 0x85, 0x19, 0x9f, 0x9b, 0xb8, 0xa4, 0xa3, 0xb2,
  29302. 0xb7, 0x56, 0x07, 0x6c, 0xbf, 0x7d, 0xff, 0x5d,
  29303. 0xb5, 0x1e, 0x83, 0xc8, 0x74, 0x70, 0x98, 0x17,
  29304. 0x40, 0xe0, 0x2d, 0xad, 0x31, 0x00, 0x8e, 0x42,
  29305. 0xd5, 0xb2, 0x25, 0xaa, 0x82, 0xaf, 0x33, 0xd8,
  29306. 0x5b, 0xe2, 0x07, 0xed, 0xda, 0x84, 0xe9, 0xa2,
  29307. 0xff, 0xbb, 0xa5, 0x47, 0x95, 0x6e, 0xa1, 0x8d,
  29308. 0x59, 0x52, 0xeb, 0xf3, 0x3c, 0x18, 0x29, 0x92,
  29309. 0x72, 0x27, 0x18, 0xfc, 0x95, 0xb9, 0xde, 0x46,
  29310. 0xda, 0xcc, 0x4c, 0x31, 0x1d, 0x78, 0x86, 0xd2,
  29311. 0x8c, 0x38, 0x9c, 0x32, 0xab, 0xf7, 0xca, 0x73,
  29312. 0x85, 0xa5, 0xf1, 0xe0, 0x25, 0x06, 0xf9, 0x18,
  29313. 0x14, 0xab, 0x3b, 0x73, 0x26, 0xee, 0xa0, 0xfd,
  29314. 0x15, 0xac, 0xd6, 0x4e, 0x6b, 0xdb, 0x01, 0xa1,
  29315. 0xdc, 0xd1, 0x2f, 0xd2, 0xb7, 0x5e, 0x12, 0x4f,
  29316. 0x4b, 0x59, 0xd8, 0x03, 0x12, 0x60, 0xc9, 0x81,
  29317. 0xb7, 0x06, 0x23, 0x09, 0xc4, 0xd9, 0xa8, 0x93,
  29318. 0x6e, 0x96, 0xf4, 0x93, 0x53, 0xf0, 0x3d, 0xde,
  29319. 0x10, 0x88, 0xb1, 0xd0, 0xcc, 0xad, 0x2c, 0xbf,
  29320. 0x88, 0x98, 0x8f, 0x25, 0x76, 0xd7, 0x65, 0x77,
  29321. 0xcc, 0x36, 0x1d, 0x1b, 0x6b, 0x60, 0x58, 0xc4,
  29322. 0xfe, 0xe6, 0xca, 0xa8, 0x29, 0x33, 0x69, 0x36,
  29323. 0xb8, 0x12, 0x95, 0x38, 0xd9, 0xd4, 0x16, 0xe9,
  29324. 0x3e, 0x40, 0x8c, 0xc7, 0xae, 0x04, 0x11, 0xdf,
  29325. 0x51, 0xd3, 0xdd, 0xbf, 0xa9, 0x41, 0x43, 0x4c,
  29326. 0xff, 0x87, 0x2f, 0xea, 0x0f, 0x13, 0x66, 0x2a,
  29327. 0x2b, 0x18, 0xe8, 0xc4, 0xff, 0xa0, 0x1c, 0x78,
  29328. 0x79, 0x21, 0xf8, 0xaa, 0x8a, 0xf8, 0x92, 0xdf,
  29329. 0x7b, 0x5f, 0x6a, 0x71, 0x60, 0x67, 0x5d, 0x94,
  29330. 0xf6, 0xbb, 0x1d, 0x90, 0x7c, 0x51, 0x70, 0x1d,
  29331. 0x87, 0xde, 0xf8, 0x91, 0xcb, 0x42, 0x9f, 0xc7,
  29332. 0x4b, 0xa0, 0x16, 0xee, 0xb4, 0x73, 0xe8, 0xe0,
  29333. 0x0b, 0xa5, 0xd3, 0x26, 0x9e, 0x52, 0xda, 0x4a,
  29334. 0x1f, 0xae, 0x76, 0xbf, 0xbb, 0x4d, 0x74, 0x98,
  29335. 0xa6, 0xae, 0xc0, 0x60, 0x96, 0xc5, 0xad, 0x9b,
  29336. 0x91, 0x31, 0xb9, 0x50, 0x3d, 0x9a, 0x0f, 0xe1,
  29337. 0x93, 0xef, 0x08, 0x72, 0xb2, 0x66, 0xe5, 0x5d,
  29338. 0xe4, 0x15, 0x53, 0x8e, 0xb0, 0xb3, 0xf8, 0x78,
  29339. 0xfc, 0x5d, 0x44, 0xc5, 0xbf, 0xf5, 0x01, 0x54,
  29340. 0xc5, 0x45, 0xa9, 0x30, 0xa4, 0xf1, 0x49, 0x79,
  29341. 0x4e, 0xab, 0xfc, 0xb2, 0x93, 0xe7, 0x3a, 0xe1,
  29342. 0x7f, 0x1f, 0x2f, 0x45, 0x3a, 0x53, 0x2b, 0x68,
  29343. 0xb3, 0xa4, 0xac, 0x23, 0x54, 0xb7, 0x5d, 0x25,
  29344. 0xa3, 0xe3, 0x90, 0x8a, 0xb0, 0x02, 0xfb, 0x7f,
  29345. 0x2d, 0xeb, 0x80, 0xc2, 0x5c, 0x62, 0xe1, 0x36,
  29346. 0x5a, 0x82, 0x8f, 0x4e, 0x74, 0xeb, 0x7d, 0x70,
  29347. 0xaf, 0x23, 0x92, 0x65, 0x3a, 0x11, 0xc0, 0x29,
  29348. 0xdb, 0xf7, 0x9a, 0xdc, 0x81, 0x45, 0x25, 0x0c,
  29349. 0x2e, 0x4f, 0x88, 0x41, 0x34, 0x53, 0xc6, 0x08,
  29350. 0x21, 0x77, 0xc1, 0xbb, 0x61, 0x48, 0x20, 0x69,
  29351. 0x1a, 0xbb, 0x71, 0x1b, 0x56, 0x18, 0x79, 0x75,
  29352. 0x16, 0x9a, 0xb3, 0x79, 0x31, 0x11, 0xa2, 0x89,
  29353. 0x8d, 0xea, 0x10, 0xb0, 0x04, 0x7f, 0xf8, 0x6e,
  29354. 0xdc, 0x08, 0x9b, 0x51, 0xa7, 0x64, 0xbd, 0x8d,
  29355. 0xd4, 0xd0, 0x1e, 0x38, 0x50, 0x1a, 0xa8, 0x7e,
  29356. 0x20, 0xae, 0xee, 0x8c, 0xa7, 0x72, 0x94, 0xc9,
  29357. 0xba, 0xf0, 0x67, 0xbd, 0x25, 0x1a, 0x3a, 0xdf,
  29358. 0x75, 0x39, 0xb7, 0xd3, 0x83, 0x3b, 0x89, 0xdf,
  29359. 0xb5, 0x2d, 0xd3, 0x12, 0x24, 0x21, 0x7c, 0x9e,
  29360. 0x92, 0x1c, 0x19, 0xae, 0x28, 0xcb, 0x2e, 0x2e,
  29361. 0x3c, 0xa9, 0x9b, 0xbd, 0xf9, 0x33, 0x30, 0xb2,
  29362. 0xbd, 0x8b, 0xbf, 0xc1, 0x8b, 0x32, 0xf1, 0x20,
  29363. 0xa1, 0x00, 0xfd, 0x11, 0x7d, 0x9a, 0xa8, 0x14,
  29364. 0x2c, 0xce, 0x16, 0x16, 0x4b, 0xdd, 0x56, 0x91,
  29365. 0x15, 0x36, 0x83, 0xcb, 0x01, 0x58, 0x35, 0xe1,
  29366. 0xdc, 0x22, 0x3d, 0xf8, 0xc2, 0x06, 0x54, 0x68,
  29367. 0x77, 0xd1, 0x47, 0x28, 0xdc, 0x09, 0x2a, 0x86,
  29368. 0x13, 0x80, 0xa6, 0xe9, 0xd0, 0xb4, 0xa3, 0x41,
  29369. 0x47, 0xf4, 0x71, 0x24, 0x10, 0x4c, 0x9f, 0xb7,
  29370. 0x57, 0x34, 0x48, 0x1b, 0xb4, 0xed, 0x0e, 0x89,
  29371. 0x4c, 0xf1, 0x73, 0x44, 0xff, 0x35, 0xb6, 0xe0,
  29372. 0x8f, 0x02, 0xa3, 0xa3, 0x81, 0x55, 0x38, 0xb5,
  29373. 0xc1, 0x99, 0xb3, 0x88, 0x84, 0x0d, 0xd9, 0x73,
  29374. 0x77, 0x65, 0x0b, 0xd7, 0xf8, 0x03, 0x88, 0xcb,
  29375. 0xdf, 0x25, 0xaf, 0xc6, 0xf1, 0xfa, 0x5c, 0x4d,
  29376. 0xfa, 0xc3, 0x7b, 0x8f, 0xb8, 0x38, 0x5d, 0x29,
  29377. 0xbb, 0x3d, 0x3e, 0x62, 0x1c, 0xdd, 0xe6, 0x97,
  29378. 0xe6, 0xe9, 0xbe, 0x6e, 0xd2, 0xb7, 0x7a, 0x9a,
  29379. 0x8e, 0xaf, 0xb3, 0xc8, 0x9e, 0x19, 0xee, 0x3d,
  29380. 0x5b, 0x1f, 0xec, 0x34, 0x3a, 0x1c, 0x27, 0x90,
  29381. 0xbd, 0x1e, 0x49, 0x72, 0x25, 0x2e, 0x38, 0x48,
  29382. 0x7d, 0xe1, 0x85, 0x46, 0xa7, 0x1b, 0x4a, 0xd5,
  29383. 0x23, 0x75, 0x6d, 0x8b, 0xc3, 0xf1, 0x87, 0xec,
  29384. 0x8b, 0x45, 0xf0, 0x9b, 0xb2, 0x14, 0x7a, 0x7c,
  29385. 0x8d, 0x78, 0x9c, 0x82, 0x64, 0x14, 0xfe, 0x01,
  29386. 0xfa, 0x04, 0x33, 0x96, 0xdd, 0x5f, 0x56, 0xbc,
  29387. 0xb2, 0x03, 0xe3, 0x0c, 0xa1, 0x09, 0x66, 0xa0,
  29388. 0x5e, 0x44, 0xde, 0x21, 0xae, 0x7d, 0x7a, 0x0e,
  29389. 0x81, 0x27, 0xd2, 0xfb, 0x85, 0xed, 0x27, 0x27,
  29390. 0xac, 0x11, 0x1c, 0xa1, 0x6d, 0xe9, 0xc1, 0xca,
  29391. 0xf6, 0x40, 0x7c, 0x95, 0x01, 0xb7, 0xa8, 0x29,
  29392. 0x9a, 0xd2, 0xcc, 0x62, 0x70, 0x1c, 0x7d, 0x0e,
  29393. 0xe5, 0x60, 0xcb, 0x79, 0xa3, 0xd7, 0x5d, 0x48,
  29394. 0x4b, 0x3c, 0xf8, 0x12, 0xe8, 0x7a, 0x7e, 0x83,
  29395. 0xab, 0x24, 0x33, 0x0f, 0x7b, 0x0a, 0x38, 0xae,
  29396. 0xb1, 0xfc, 0xc3, 0x50, 0x5c, 0x83, 0x53, 0xfd,
  29397. 0x15, 0xd6, 0x49, 0x54, 0xb6, 0x40, 0xe5, 0xe8,
  29398. 0x55, 0xba, 0x08, 0x2f, 0x21, 0xd7, 0x0e, 0x71,
  29399. 0x8a, 0xb2, 0xe1, 0x6b, 0xc6, 0x7e, 0x0f, 0x1c,
  29400. 0x4d, 0x41, 0x9f, 0x38, 0xc2, 0xce, 0x41, 0x41,
  29401. 0x48, 0xcd, 0xec, 0x16, 0x1d, 0x23, 0x8e, 0x41,
  29402. 0xcd, 0x5e, 0xf9, 0x5f, 0x01, 0x5e, 0x73, 0xa2,
  29403. 0xa1, 0xef, 0xe9, 0x57, 0xe0, 0xba, 0xe6, 0xbb,
  29404. 0x2b, 0xff, 0x3e, 0xb8, 0xad, 0xd5, 0x12, 0xc1,
  29405. 0x54, 0x49, 0xca, 0x93, 0xb0, 0x7d, 0x7b, 0xcf,
  29406. 0xf0, 0xc5, 0x94, 0x43, 0x30, 0x94, 0x11, 0x8d,
  29407. 0x15, 0x79, 0x2e, 0x57, 0xb8, 0x24, 0xcd, 0x2e,
  29408. 0xc2, 0x49, 0x3d, 0x92, 0x44, 0x23, 0x0c, 0x3e,
  29409. 0xa0, 0xf9, 0xa5, 0xad, 0x2a, 0x56, 0xec, 0xf4,
  29410. 0x6d, 0x0f, 0x5b, 0xb5, 0xd4, 0x2a, 0x3f, 0x2b,
  29411. 0x17, 0x9f, 0x5d, 0x33, 0x97, 0x42, 0xd4, 0x1e,
  29412. 0x14, 0x49, 0x01, 0xfb, 0xb6, 0x72, 0xbc, 0x14,
  29413. 0x5b, 0x79, 0xf4, 0x0a, 0xc5, 0x49, 0xe1, 0x76,
  29414. 0x44, 0x78, 0x87, 0xd1, 0x8e, 0x5b, 0xd5, 0x95,
  29415. 0xad, 0x19, 0x7c, 0x0d, 0x39, 0x7f, 0x41, 0x2e,
  29416. 0xd7, 0x9e, 0xbc, 0xfd, 0x2c, 0xde, 0xfa, 0x01,
  29417. 0x7d, 0x2b, 0x04, 0xef, 0x4d, 0xf9, 0xf4, 0x5b,
  29418. 0xed, 0x05, 0x9a, 0x50, 0x35, 0xe7, 0xb0, 0xba,
  29419. 0x24, 0xea, 0x16, 0x51, 0xe1, 0x6f, 0x32, 0x08,
  29420. 0x94, 0xd6, 0x19, 0x9d, 0x0e, 0x4c, 0xc1, 0xbb,
  29421. 0x01, 0x87, 0xa5, 0x90, 0x5f, 0x6f, 0xc4, 0xed,
  29422. 0xa1, 0x4c, 0x06, 0x4d, 0x2c, 0x47, 0x24, 0xda,
  29423. 0xae, 0xd2, 0x41, 0x92, 0x1f, 0x46, 0xce, 0xec,
  29424. 0xb1, 0xcc, 0x80, 0x1e, 0xb2, 0xcb, 0x66, 0x48,
  29425. 0x22, 0xec, 0x0e, 0x47, 0xfc, 0xad, 0x17, 0xfe,
  29426. 0x7b, 0xc5, 0x4d, 0x34, 0x95, 0x40, 0xd0, 0x02,
  29427. 0x7e, 0x90, 0xaa, 0x92, 0xaf, 0x48, 0x64, 0xc5,
  29428. 0xc1, 0x56, 0xd8, 0x9b, 0x6c, 0x5f, 0x2e, 0xfa,
  29429. 0xd7, 0x84, 0xdc, 0x71, 0x65, 0x1b, 0xfb, 0xbc,
  29430. 0x21, 0xc7, 0x57, 0xf4, 0x71, 0x2e, 0x6f, 0x34,
  29431. 0x85, 0x99, 0xa8, 0x5c, 0x6f, 0x34, 0x22, 0x44,
  29432. 0x89, 0x01, 0xf9, 0x48, 0xd2, 0xe2, 0xe4, 0x71,
  29433. 0x9d, 0x48, 0x07, 0x97, 0xd4, 0x66, 0xe4, 0x4d,
  29434. 0x48, 0xa3, 0x08, 0x7f, 0x6e, 0xaa, 0x7b, 0xe9,
  29435. 0x93, 0x81, 0x03, 0x0c, 0xd2, 0x48, 0xcf, 0x3f,
  29436. 0x5f, 0xbe, 0x03, 0xfb, 0x0f, 0xad, 0xc3, 0x81,
  29437. 0xd9, 0xce, 0x88, 0x0b, 0xfa, 0xed, 0x29, 0x7e,
  29438. 0x0b, 0xa1, 0x6f, 0x4c, 0x7d, 0xe4, 0x36, 0xff,
  29439. 0xdf, 0x94, 0x1a, 0x24, 0xb3, 0x7b, 0xca, 0x24,
  29440. 0x7e, 0x3a, 0x19, 0x53, 0x13, 0x4a, 0x17, 0x58,
  29441. 0xe7, 0x16, 0x9b, 0x50, 0xd8, 0xda, 0xcc, 0x6e,
  29442. 0x05, 0x25, 0xfe, 0x16, 0xcb, 0x5b, 0xd5, 0x35,
  29443. 0x76, 0x40, 0x44, 0x96, 0x23, 0x97, 0xe2, 0x4a,
  29444. 0x72, 0x0c, 0x54, 0x43, 0xc0, 0x09, 0x85, 0x8e,
  29445. 0x15, 0x85, 0xaf, 0x3c, 0x5e, 0x5f, 0x3c, 0x2d,
  29446. 0x21, 0x42, 0x75, 0xb7, 0xe4, 0x50, 0xf9, 0x00,
  29447. 0xa3, 0x4f, 0xb1, 0x7c, 0xfe, 0x62, 0xd0, 0xe9,
  29448. 0x6d, 0x51, 0xcc, 0x83, 0xc1, 0xdc, 0x37, 0x10,
  29449. 0x90, 0x0a, 0x15, 0xd8, 0xd5, 0x02, 0xf7, 0x74,
  29450. 0xb8, 0x46, 0x84, 0xc3, 0x61, 0x17, 0x26, 0x0f,
  29451. 0xe4, 0xde, 0x1a, 0xcf, 0x42, 0x53, 0x63, 0x2f,
  29452. 0x8d, 0xf7, 0x06, 0x07, 0xc3, 0x33, 0x39, 0x59,
  29453. 0xe9, 0x17, 0xc8, 0x05, 0xd2, 0xa2, 0xae, 0x53,
  29454. 0x2c, 0x7e, 0xd0, 0x9d, 0x5c, 0xb5, 0x42, 0x9f,
  29455. 0x84, 0xd7, 0xfe, 0x93, 0x74, 0xfb, 0xbb, 0xd2,
  29456. 0x1e, 0x57, 0x4e, 0x7f, 0x79, 0xaf, 0xd2, 0xf9,
  29457. 0x5e, 0x41, 0x9e, 0x63, 0x54, 0x61, 0x47, 0x0c,
  29458. 0x92, 0x4c, 0xc9, 0xfe, 0x4f, 0xcb, 0xe5, 0x8e,
  29459. 0x65, 0xb3, 0x97, 0x1b, 0xd8, 0xd1, 0x62, 0xfd
  29460. #else
  29461. 0x30, 0x82, 0x0a, 0x34, 0x30, 0x0d, 0x06, 0x0b,
  29462. 0x2b, 0x06, 0x01, 0x04, 0x01, 0x02, 0x82, 0x0b,
  29463. 0x0c, 0x08, 0x07, 0x03, 0x82, 0x0a, 0x21, 0x00,
  29464. 0x7f, 0x5f, 0x63, 0x81, 0x6f, 0x04, 0x4c, 0xec,
  29465. 0xa8, 0xaf, 0x7b, 0x99, 0x41, 0xc6, 0xff, 0xdf,
  29466. 0x77, 0x66, 0x28, 0xc0, 0xe2, 0x58, 0xea, 0x9c,
  29467. 0x60, 0xbb, 0x03, 0x3e, 0xca, 0xa8, 0x38, 0x64,
  29468. 0xfb, 0xf7, 0x1b, 0x3f, 0xec, 0xfd, 0x0f, 0xf1,
  29469. 0x9c, 0xe4, 0xfd, 0xad, 0x83, 0xf7, 0x03, 0x66,
  29470. 0x6e, 0x7f, 0x4d, 0x42, 0xab, 0x6b, 0x73, 0x26,
  29471. 0xde, 0x6f, 0x8c, 0xc4, 0xca, 0x21, 0x66, 0x31,
  29472. 0x79, 0x57, 0x88, 0xcb, 0x1e, 0xab, 0xda, 0x1d,
  29473. 0x56, 0x70, 0xd9, 0x83, 0xa1, 0xb4, 0x83, 0xce,
  29474. 0xcc, 0x0f, 0xeb, 0xd6, 0x63, 0xbd, 0xf6, 0x02,
  29475. 0x5d, 0x5b, 0x0c, 0x17, 0x3c, 0x3e, 0x15, 0x02,
  29476. 0x22, 0xa1, 0x5d, 0xb5, 0xc5, 0x81, 0x28, 0x95,
  29477. 0x0b, 0x34, 0x2b, 0x96, 0x0a, 0xae, 0x6a, 0xa8,
  29478. 0xb5, 0x1d, 0x56, 0xbb, 0x7d, 0x83, 0x9a, 0x15,
  29479. 0xad, 0x63, 0x9e, 0x86, 0x8c, 0x6e, 0x6a, 0xa8,
  29480. 0xde, 0x55, 0xd0, 0xce, 0xc0, 0x2e, 0x05, 0xfe,
  29481. 0x1f, 0x4d, 0xd7, 0x12, 0xa4, 0x5a, 0xe9, 0x04,
  29482. 0x0d, 0x20, 0x84, 0x90, 0xb9, 0xca, 0x64, 0xe4,
  29483. 0xad, 0x2e, 0x74, 0x4b, 0x1d, 0x2f, 0xcc, 0xac,
  29484. 0xd8, 0x1a, 0x5e, 0xb2, 0x78, 0xbe, 0x61, 0xf7,
  29485. 0x36, 0xa3, 0xd1, 0x93, 0x86, 0xb5, 0x15, 0xf1,
  29486. 0x74, 0xf8, 0x9f, 0x6d, 0x6a, 0x8f, 0x6d, 0x86,
  29487. 0x8b, 0x36, 0x61, 0x10, 0xc9, 0x1a, 0x31, 0x39,
  29488. 0x09, 0xe6, 0x15, 0xa0, 0xb1, 0xfa, 0x69, 0xd4,
  29489. 0xc2, 0xb2, 0x56, 0x4c, 0x06, 0x33, 0x13, 0xc4,
  29490. 0x78, 0x53, 0x16, 0xfc, 0x52, 0x99, 0xe6, 0x27,
  29491. 0xc9, 0x3b, 0x24, 0x5c, 0x3e, 0x85, 0x73, 0x76,
  29492. 0x61, 0xa3, 0x61, 0xf0, 0x95, 0xd5, 0xb2, 0xf5,
  29493. 0x21, 0xe7, 0x09, 0xc3, 0x0c, 0x5c, 0xb0, 0x36,
  29494. 0xce, 0x45, 0x68, 0x41, 0x45, 0xcb, 0x1c, 0x36,
  29495. 0x2f, 0x3a, 0x00, 0x07, 0x56, 0xbe, 0x61, 0xd2,
  29496. 0x77, 0x37, 0x63, 0xa4, 0xdb, 0xfa, 0xa9, 0x6b,
  29497. 0x37, 0x90, 0x35, 0xd1, 0x1e, 0x27, 0x5b, 0x3e,
  29498. 0xc0, 0x0a, 0x02, 0x64, 0xe4, 0x58, 0x49, 0xab,
  29499. 0x2d, 0xc1, 0x38, 0x29, 0x3d, 0x44, 0xf9, 0xac,
  29500. 0xb7, 0x65, 0xd1, 0x5f, 0xf8, 0xce, 0x52, 0x76,
  29501. 0x22, 0x15, 0x61, 0x02, 0x1f, 0xa7, 0xcd, 0xff,
  29502. 0xeb, 0xa6, 0x7f, 0x6b, 0xba, 0x75, 0xe3, 0x09,
  29503. 0x01, 0x06, 0x41, 0x20, 0x88, 0x75, 0x64, 0x6b,
  29504. 0x97, 0x38, 0x13, 0xab, 0x4c, 0x0a, 0xd4, 0x7e,
  29505. 0xd2, 0xfa, 0x78, 0xe8, 0x9f, 0x5d, 0xf9, 0x53,
  29506. 0x30, 0x17, 0xf1, 0x10, 0x9e, 0x4a, 0x32, 0x17,
  29507. 0x3a, 0x9b, 0xb9, 0x25, 0x8e, 0xeb, 0xd9, 0x41,
  29508. 0x01, 0xa2, 0xc6, 0x58, 0x4a, 0x9f, 0xc3, 0x73,
  29509. 0xfd, 0xe2, 0xe4, 0x2c, 0x92, 0xb4, 0xa2, 0x3d,
  29510. 0x0f, 0x1f, 0x37, 0x64, 0xf1, 0x17, 0x2a, 0x8c,
  29511. 0xc6, 0xb5, 0xb0, 0x69, 0x7d, 0xfe, 0x08, 0xe0,
  29512. 0x8e, 0xaa, 0xe0, 0x08, 0xd5, 0x28, 0x92, 0x51,
  29513. 0x73, 0x8a, 0x2f, 0x7a, 0x4a, 0xbf, 0x52, 0x8d,
  29514. 0x3e, 0x9b, 0x36, 0x6a, 0xfb, 0x19, 0xf0, 0xea,
  29515. 0xfe, 0x05, 0xbd, 0x2d, 0xa9, 0x58, 0x48, 0x02,
  29516. 0xa8, 0x20, 0x9e, 0xdc, 0x04, 0x57, 0xc2, 0x0c,
  29517. 0xae, 0xc1, 0x03, 0xe7, 0x17, 0x48, 0x80, 0x00,
  29518. 0x8d, 0x1b, 0xd0, 0xc5, 0xdc, 0x2a, 0x02, 0x6e,
  29519. 0x8e, 0x54, 0xf3, 0x79, 0x31, 0x02, 0x93, 0xc5,
  29520. 0xf2, 0x55, 0xea, 0x61, 0xd0, 0xb2, 0x8e, 0xc9,
  29521. 0x74, 0x17, 0x0d, 0x38, 0xf8, 0xab, 0xf4, 0x42,
  29522. 0xd4, 0xc2, 0xdc, 0xf7, 0x1b, 0xdb, 0x65, 0x36,
  29523. 0x9f, 0x56, 0xe2, 0xeb, 0xf7, 0xe5, 0x2d, 0x45,
  29524. 0xae, 0xc0, 0x95, 0xbc, 0xe4, 0x1f, 0x22, 0xdc,
  29525. 0x0f, 0x54, 0xed, 0x14, 0xb8, 0xf1, 0x2f, 0x5d,
  29526. 0xd1, 0x79, 0xa0, 0x81, 0x17, 0x71, 0xa1, 0xd6,
  29527. 0xf0, 0x88, 0x9c, 0x1c, 0xc7, 0x95, 0x07, 0xb0,
  29528. 0xea, 0xf7, 0xd3, 0xa2, 0x55, 0xfe, 0x85, 0x65,
  29529. 0x42, 0x06, 0xec, 0xd2, 0xbe, 0x03, 0x8f, 0x63,
  29530. 0x84, 0x4b, 0xb1, 0x47, 0x48, 0x20, 0x71, 0xd2,
  29531. 0xdf, 0xc9, 0x59, 0xb0, 0x24, 0x8a, 0x6e, 0xf9,
  29532. 0x4a, 0xa1, 0x7b, 0xed, 0x11, 0xb6, 0xf9, 0x9b,
  29533. 0xf7, 0x93, 0x0e, 0xcb, 0x7a, 0x32, 0x22, 0x23,
  29534. 0x4e, 0x86, 0xce, 0xad, 0x9d, 0x1b, 0x84, 0x57,
  29535. 0xaf, 0xa5, 0x04, 0x03, 0x0a, 0xc9, 0x04, 0x97,
  29536. 0xd0, 0xce, 0x8e, 0x2a, 0x9a, 0x00, 0x15, 0xeb,
  29537. 0xac, 0x96, 0x57, 0xde, 0xe6, 0xc1, 0x2d, 0xbd,
  29538. 0xfc, 0xd6, 0x95, 0x0f, 0x5f, 0x19, 0xac, 0xaf,
  29539. 0x6c, 0xd8, 0xa6, 0x1e, 0xd8, 0xdb, 0x14, 0xfd,
  29540. 0xba, 0x0f, 0xd0, 0x3f, 0x61, 0xe3, 0x76, 0xfc,
  29541. 0x47, 0x61, 0x07, 0x24, 0x49, 0x17, 0xca, 0x24,
  29542. 0x31, 0x16, 0x26, 0x4f, 0xdc, 0x2b, 0x39, 0xae,
  29543. 0x5f, 0xfa, 0x4f, 0x82, 0xef, 0xe1, 0x41, 0x8c,
  29544. 0x3e, 0x8e, 0xa7, 0x6c, 0xf2, 0x51, 0xf7, 0x85,
  29545. 0x35, 0x6c, 0xad, 0xea, 0x32, 0x35, 0xf3, 0xc0,
  29546. 0x14, 0x17, 0xe2, 0x98, 0x27, 0x36, 0x7e, 0x60,
  29547. 0x2f, 0x01, 0x60, 0x3e, 0x18, 0xf4, 0x4e, 0xe0,
  29548. 0xf5, 0x14, 0x21, 0x81, 0x05, 0x78, 0x1c, 0x5f,
  29549. 0x4e, 0x89, 0xbb, 0x23, 0x60, 0xb1, 0x8f, 0x07,
  29550. 0x53, 0x16, 0x6e, 0xfb, 0x86, 0x07, 0x90, 0xff,
  29551. 0xa6, 0x27, 0x60, 0xe6, 0x3e, 0x92, 0x2a, 0x3c,
  29552. 0xa3, 0x57, 0xec, 0x97, 0x23, 0xaf, 0xd2, 0x44,
  29553. 0xac, 0x09, 0x87, 0xb0, 0x54, 0xe9, 0x5b, 0x50,
  29554. 0x37, 0xfa, 0x12, 0xa4, 0xcb, 0x6f, 0xed, 0x9f,
  29555. 0x29, 0x73, 0xa7, 0x09, 0x29, 0x91, 0x93, 0x5c,
  29556. 0x54, 0xf4, 0x44, 0xc2, 0x04, 0x64, 0xfc, 0xd2,
  29557. 0xf2, 0x0a, 0x0b, 0x45, 0x1f, 0xc5, 0x18, 0xf0,
  29558. 0xff, 0x10, 0x1f, 0x3a, 0x97, 0xf8, 0xb1, 0x83,
  29559. 0x0e, 0x08, 0xe2, 0x55, 0x75, 0x6a, 0x45, 0x96,
  29560. 0xf8, 0x1b, 0xdc, 0xb6, 0x57, 0x83, 0x8c, 0x28,
  29561. 0xc0, 0x4a, 0x57, 0xc6, 0xfb, 0x27, 0x3d, 0xfa,
  29562. 0x5a, 0x0d, 0x69, 0x56, 0x23, 0x66, 0x02, 0x78,
  29563. 0xca, 0xf1, 0xfa, 0xcb, 0xc1, 0xf6, 0x92, 0x1c,
  29564. 0xa0, 0xe3, 0x09, 0x7d, 0x48, 0x5e, 0x86, 0xa0,
  29565. 0x82, 0xa8, 0xf1, 0x1e, 0xe1, 0xfe, 0xc6, 0x9d,
  29566. 0x4f, 0x2e, 0xf4, 0xfc, 0xc6, 0x48, 0x1d, 0xc1,
  29567. 0x2a, 0x6a, 0xb7, 0xea, 0x46, 0x89, 0x04, 0xe9,
  29568. 0xbd, 0xf1, 0xed, 0x16, 0x76, 0xd8, 0x4b, 0x42,
  29569. 0xd5, 0x43, 0xa4, 0xfb, 0x02, 0x01, 0x54, 0x00,
  29570. 0xaf, 0x55, 0x52, 0x27, 0xff, 0x00, 0xe2, 0xbb,
  29571. 0x4a, 0xf2, 0x69, 0xb4, 0x4e, 0x6c, 0x6b, 0xa3,
  29572. 0x96, 0x4f, 0xf4, 0x65, 0x90, 0x2d, 0xc8, 0x57,
  29573. 0x1f, 0xb2, 0xf0, 0x86, 0x7b, 0x93, 0x09, 0x49,
  29574. 0x31, 0xc4, 0xf4, 0x8f, 0xc8, 0x2d, 0xac, 0x1d,
  29575. 0xfc, 0xba, 0xa4, 0xa5, 0x41, 0x90, 0x76, 0x7d,
  29576. 0x9e, 0x47, 0xdc, 0x10, 0xe6, 0x0c, 0xf7, 0x0f,
  29577. 0xa4, 0xba, 0x4f, 0xe2, 0x46, 0x38, 0x4c, 0x28,
  29578. 0xa0, 0x57, 0xb5, 0x3c, 0xb3, 0x4b, 0x8f, 0x03,
  29579. 0x04, 0xff, 0xf6, 0xec, 0x60, 0x90, 0x62, 0xfe,
  29580. 0x74, 0x76, 0x48, 0xb3, 0xf4, 0x0a, 0x6a, 0x5a,
  29581. 0x5b, 0xad, 0xc8, 0x54, 0x62, 0x11, 0x52, 0xd9,
  29582. 0x84, 0x1a, 0x09, 0x4b, 0xca, 0x66, 0xaa, 0x3c,
  29583. 0x36, 0x08, 0x9d, 0x58, 0xd0, 0x4a, 0x3a, 0x8b,
  29584. 0x24, 0xe0, 0x80, 0x9f, 0xe3, 0x76, 0xb6, 0x07,
  29585. 0xb1, 0xbc, 0x00, 0x98, 0xb0, 0xc1, 0xe0, 0xf6,
  29586. 0x1f, 0x4d, 0xa8, 0xd1, 0x69, 0x44, 0x9c, 0x33,
  29587. 0xb0, 0x0f, 0x9c, 0xc9, 0x0c, 0x8c, 0xbc, 0x03,
  29588. 0x58, 0x81, 0x76, 0xab, 0x0d, 0xef, 0x25, 0x5a,
  29589. 0xf6, 0xab, 0x3b, 0xf1, 0x1f, 0x97, 0x12, 0x8e,
  29590. 0x7f, 0x28, 0x77, 0x26, 0x18, 0xc4, 0xc4, 0xda,
  29591. 0x2c, 0x43, 0x57, 0xd2, 0x1f, 0x67, 0x95, 0x40,
  29592. 0x2c, 0x94, 0x41, 0x69, 0x22, 0x8a, 0x24, 0xd9,
  29593. 0xc7, 0xfc, 0xea, 0x49, 0x83, 0x8f, 0x5d, 0x2e,
  29594. 0x9d, 0xac, 0x17, 0xb6, 0xe0, 0xc4, 0xe7, 0xe6,
  29595. 0xd5, 0xc2, 0x73, 0xa1, 0x8f, 0x33, 0x14, 0x02,
  29596. 0xae, 0x01, 0x9f, 0x6f, 0x40, 0x92, 0x4e, 0x03,
  29597. 0xc2, 0xa9, 0xf1, 0x36, 0x78, 0xe4, 0xde, 0x39,
  29598. 0x4d, 0x29, 0x2e, 0xc2, 0x00, 0x93, 0x79, 0xe4,
  29599. 0xb2, 0x29, 0x4b, 0x81, 0x5c, 0x06, 0x06, 0xbc,
  29600. 0xc1, 0x01, 0x1c, 0xa7, 0x08, 0xf7, 0x47, 0x1f,
  29601. 0x52, 0x4f, 0xdf, 0x94, 0x1e, 0xe6, 0x89, 0xe6,
  29602. 0x26, 0x71, 0x2e, 0xa2, 0xd2, 0xfe, 0x04, 0xf2,
  29603. 0x12, 0x4c, 0x06, 0x78, 0x34, 0xc0, 0xb9, 0x76,
  29604. 0x62, 0x3b, 0x72, 0x25, 0x8c, 0x0d, 0x73, 0x24,
  29605. 0xcf, 0x4b, 0x4c, 0x47, 0x20, 0x9d, 0x04, 0x7f,
  29606. 0x86, 0x2c, 0x45, 0xb8, 0xfe, 0xb2, 0xaa, 0x36,
  29607. 0xf8, 0xe0, 0x24, 0x25, 0x05, 0x23, 0x12, 0x16,
  29608. 0xbf, 0x64, 0x10, 0xdd, 0xe4, 0xc0, 0xb0, 0x85,
  29609. 0xa7, 0xd3, 0xd1, 0x18, 0x1b, 0x81, 0x6b, 0x94,
  29610. 0xfd, 0x07, 0x43, 0xdd, 0x12, 0x37, 0x78, 0x69,
  29611. 0xec, 0x8c, 0xd0, 0x41, 0x2c, 0x42, 0x94, 0x3e,
  29612. 0x9f, 0xe3, 0x49, 0xb3, 0xb8, 0x45, 0x0b, 0x1d,
  29613. 0xc1, 0x9b, 0x4d, 0x21, 0x85, 0x62, 0xea, 0xd1,
  29614. 0xc9, 0x12, 0x30, 0x8c, 0x4b, 0x63, 0xeb, 0x7d,
  29615. 0x02, 0x52, 0x15, 0xa1, 0x95, 0x48, 0x9f, 0xc2,
  29616. 0xce, 0xf3, 0x4b, 0xff, 0x5a, 0xb6, 0x8f, 0xce,
  29617. 0xcd, 0x42, 0x21, 0x40, 0x82, 0xad, 0x08, 0x99,
  29618. 0x4d, 0x24, 0x58, 0x25, 0xf3, 0x7e, 0x42, 0x86,
  29619. 0x06, 0x33, 0x1f, 0x53, 0xbb, 0x07, 0x33, 0xca,
  29620. 0xc0, 0x02, 0x18, 0x30, 0x3c, 0xc5, 0x67, 0x1c,
  29621. 0x32, 0x3f, 0x2d, 0x58, 0x4c, 0x24, 0x6e, 0x60,
  29622. 0x96, 0x1a, 0xf4, 0xd0, 0x55, 0xb8, 0x84, 0xf0,
  29623. 0xb9, 0x83, 0xbf, 0x3d, 0x37, 0xe4, 0xa6, 0x06,
  29624. 0x1c, 0xd1, 0xd7, 0x91, 0x24, 0xdc, 0x3f, 0xcc,
  29625. 0x71, 0xf3, 0x0c, 0x90, 0x2c, 0x1d, 0x2f, 0x90,
  29626. 0xc8, 0x3c, 0x6f, 0x2c, 0x5d, 0xad, 0x8c, 0xdf,
  29627. 0xbb, 0x0d, 0x2a, 0x7f, 0x4a, 0x34, 0x5a, 0xd9,
  29628. 0x83, 0xfd, 0x61, 0x36, 0xe0, 0x0a, 0xb3, 0xf6,
  29629. 0x69, 0xb1, 0xaf, 0x81, 0x22, 0xd6, 0x9e, 0x9a,
  29630. 0xf8, 0xa6, 0x24, 0x8e, 0x0c, 0xcb, 0x25, 0xc2,
  29631. 0xfc, 0xc5, 0x94, 0xbd, 0x23, 0x9c, 0xa9, 0xbd,
  29632. 0x76, 0x28, 0xa4, 0x55, 0x92, 0x7c, 0xe6, 0x76,
  29633. 0xf7, 0x30, 0xf8, 0x7d, 0xdc, 0x0a, 0x93, 0x9e,
  29634. 0x7c, 0x39, 0x0a, 0x70, 0xa0, 0xb2, 0x77, 0xe0,
  29635. 0x7a, 0x89, 0x50, 0xce, 0x75, 0xca, 0x2f, 0xa4,
  29636. 0x12, 0x0e, 0xcb, 0x75, 0x1f, 0x0a, 0x83, 0xe8,
  29637. 0x14, 0x80, 0xa7, 0xb0, 0xe8, 0x11, 0xca, 0x12,
  29638. 0x5e, 0xf7, 0x31, 0x65, 0xbd, 0x20, 0x3d, 0x8c,
  29639. 0xa6, 0x89, 0x83, 0x68, 0x66, 0x03, 0x28, 0x49,
  29640. 0x17, 0xc4, 0x3f, 0x43, 0x02, 0x9b, 0xf8, 0xed,
  29641. 0xae, 0x8e, 0x68, 0xbc, 0x8e, 0x39, 0xe7, 0x15,
  29642. 0x32, 0x45, 0x66, 0x2c, 0x1f, 0xce, 0x56, 0xc7,
  29643. 0xc0, 0x15, 0x52, 0x19, 0x40, 0xcf, 0x87, 0x20,
  29644. 0xcd, 0x3d, 0xec, 0x90, 0x8d, 0x04, 0x01, 0x31,
  29645. 0x0b, 0x74, 0x80, 0x6e, 0x61, 0xa7, 0xf3, 0x4c,
  29646. 0xb2, 0x16, 0x00, 0xd5, 0xdb, 0xcc, 0xbb, 0x2c,
  29647. 0x9f, 0xb6, 0x02, 0x4a, 0xcf, 0x71, 0x06, 0xfd,
  29648. 0x60, 0xe0, 0x00, 0xbe, 0x22, 0xba, 0x39, 0x36,
  29649. 0xa8, 0x7e, 0xe5, 0xcb, 0xea, 0x87, 0xb1, 0xee,
  29650. 0xa2, 0x6c, 0x85, 0x94, 0x18, 0x6c, 0xab, 0x9a,
  29651. 0x93, 0xa7, 0xab, 0x4e, 0x3b, 0x85, 0xf3, 0xef,
  29652. 0x8f, 0x15, 0x74, 0x21, 0x9f, 0x5d, 0x9c, 0x22,
  29653. 0x32, 0x71, 0xb5, 0x4d, 0x7f, 0xaa, 0x85, 0xe0,
  29654. 0x05, 0x2a, 0x53, 0xbb, 0x3c, 0xab, 0xc3, 0xd2,
  29655. 0x73, 0x6e, 0x97, 0xa3, 0xfd, 0x05, 0x58, 0xaa,
  29656. 0x49, 0xc8, 0x69, 0xa9, 0x0b, 0x73, 0xd4, 0xe9,
  29657. 0x1d, 0x84, 0x60, 0x34, 0x2a, 0x09, 0xb3, 0x0f,
  29658. 0x08, 0x13, 0x67, 0x77, 0xb3, 0x24, 0xdf, 0xad,
  29659. 0xbf, 0x51, 0x71, 0x2b, 0xbe, 0x4f, 0x5d, 0xf4,
  29660. 0xe7, 0x25, 0x4c, 0x24, 0xa2, 0x4a, 0x22, 0xec,
  29661. 0xcc, 0x7c, 0x6c, 0x62, 0xee, 0x47, 0x12, 0x43,
  29662. 0x88, 0xe4, 0x71, 0xaa, 0x63, 0xaa, 0x2b, 0xed,
  29663. 0x70, 0xbf, 0x26, 0x37, 0xcc, 0xa4, 0xff, 0xe9,
  29664. 0xb6, 0x65, 0x31, 0x4d, 0x0d, 0x32, 0xd6, 0x84,
  29665. 0xb8, 0xab, 0x98, 0xa7, 0x10, 0x44, 0x77, 0xc7,
  29666. 0x2a, 0x60, 0xf0, 0xf5, 0xd5, 0xd4, 0x3a, 0x73,
  29667. 0x11, 0xa5, 0x1b, 0x18, 0x3c, 0x13, 0xfb, 0xda,
  29668. 0x76, 0x9d, 0xeb, 0x3e, 0xb9, 0x7a, 0xce, 0x02,
  29669. 0xa7, 0x5e, 0x25, 0x96, 0xd2, 0xbc, 0x85, 0x1a,
  29670. 0xd1, 0xa4, 0xe2, 0x02, 0x15, 0x08, 0x49, 0x16,
  29671. 0x7c, 0xaf, 0xc6, 0x38, 0x7b, 0x95, 0xf9, 0x37,
  29672. 0xc0, 0x87, 0x73, 0x6f, 0x01, 0xcd, 0x2b, 0xf1,
  29673. 0xe7, 0x6e, 0x47, 0x18, 0x30, 0xb8, 0x16, 0x87,
  29674. 0x1d, 0x23, 0x62, 0x22, 0x85, 0x92, 0x69, 0x46,
  29675. 0x9c, 0x65, 0xd8, 0xf1, 0x27, 0x32, 0xe4, 0x16,
  29676. 0x7f, 0x9a, 0xba, 0x46, 0x61, 0x60, 0x34, 0xe5,
  29677. 0xc0, 0x14, 0xb5, 0xde, 0x4d, 0xd1, 0x71, 0x39,
  29678. 0x26, 0xdc, 0x0c, 0x0a, 0x53, 0x9e, 0x31, 0x10,
  29679. 0x45, 0x7a, 0xf9, 0xc8, 0xfa, 0x1d, 0x69, 0x5e,
  29680. 0x25, 0xc1, 0xe2, 0x00, 0xbf, 0x94, 0xa3, 0xa2,
  29681. 0x97, 0xca, 0xb4, 0x6a, 0x89, 0x68, 0xdd, 0xed,
  29682. 0x6b, 0x99, 0x5a, 0x87, 0x9e, 0xe9, 0x68, 0xe4,
  29683. 0xf2, 0xc2, 0x7e, 0x37, 0x02, 0xdf, 0x96, 0x1a,
  29684. 0x5b, 0xed, 0xa1, 0xe8, 0xdf, 0x3c, 0xf7, 0xd2,
  29685. 0x25, 0xac, 0xf7, 0x4a, 0x7f, 0x10, 0x27, 0x2b,
  29686. 0x02, 0xc7, 0x95, 0x10, 0x5a, 0xb5, 0xb0, 0xcd,
  29687. 0xa9, 0xe1, 0x36, 0xe2, 0x1c, 0x87, 0x99, 0x0e,
  29688. 0x0a, 0x44, 0xec, 0x97, 0x75, 0xa7, 0x03, 0x27,
  29689. 0x38, 0x3b, 0x16, 0x30, 0x00, 0x98, 0xbe, 0x77,
  29690. 0xfe, 0x3a, 0xac, 0x6f, 0x8f, 0x4d, 0xe1, 0xa9,
  29691. 0x9c, 0xba, 0x39, 0x52, 0xe8, 0xf7, 0xe4, 0xe6,
  29692. 0xf9, 0xe9, 0xb3, 0x57, 0x82, 0xb2, 0x23, 0xd6,
  29693. 0xa5, 0x14, 0xc0, 0x78, 0xb4, 0xa0, 0xf9, 0x96,
  29694. 0xe4, 0x03, 0xe8, 0x6c, 0x27, 0xd8, 0x37, 0x7c,
  29695. 0x8f, 0xf4, 0x80, 0x09, 0x09, 0xc9, 0x32, 0x15,
  29696. 0xe0, 0x3f, 0x37, 0xa7, 0x1a, 0x5f, 0x8c, 0xfb,
  29697. 0xdd, 0xfe, 0x6b, 0x34, 0x28, 0x53, 0x03, 0x4b,
  29698. 0x39, 0x91, 0xf2, 0x48, 0x4c, 0x2a, 0x45, 0xfe,
  29699. 0x66, 0xf7, 0x23, 0x74, 0xb8, 0x30, 0x70, 0xb4,
  29700. 0x0c, 0x2c, 0x65, 0xb1, 0x4e, 0x32, 0x0f, 0x50,
  29701. 0xbb, 0x46, 0x9b, 0x03, 0x34, 0x38, 0xfb, 0xe4,
  29702. 0x25, 0x37, 0x8d, 0x0f, 0xa1, 0x41, 0x50, 0x85,
  29703. 0x92, 0x07, 0x71, 0xff, 0x3c, 0xe6, 0xd9, 0x1d,
  29704. 0x55, 0xb7, 0x10, 0x9c, 0xea, 0x70, 0x5f, 0xa3,
  29705. 0xba, 0x84, 0x99, 0x91, 0x30, 0x3d, 0x4c, 0x98,
  29706. 0x0b, 0x1f, 0x1f, 0xcc, 0x17, 0x94, 0xdd, 0x78,
  29707. 0x7d, 0x50, 0xe5, 0xf5, 0x21, 0x88, 0x5a, 0x52,
  29708. 0x76, 0x5a, 0x97, 0xbe, 0xba, 0xa9, 0xfe, 0x82,
  29709. 0x8a, 0xb5, 0x46, 0xcf, 0x9c, 0xbe, 0xe8, 0x2f,
  29710. 0x01, 0x2f, 0x6a, 0x03, 0x8a, 0xfa, 0x4b, 0x0b,
  29711. 0xdc, 0x78, 0x79, 0x9c, 0x49, 0xc4, 0x01, 0x26,
  29712. 0x16, 0x58, 0xc6, 0xb8, 0xee, 0x6c, 0xc9, 0xa9,
  29713. 0x38, 0x7c, 0xcf, 0xf3, 0xf8, 0xd0, 0x6b, 0x99,
  29714. 0x43, 0x13, 0xe0, 0x43, 0x8e, 0xfb, 0xb2, 0xdb,
  29715. 0x61, 0x67, 0xf4, 0xfc, 0x01, 0x21, 0xd9, 0xb1,
  29716. 0x1e, 0x6c, 0x6f, 0x2a, 0x9a, 0x4b, 0x86, 0x3c,
  29717. 0x62, 0x03, 0x53, 0x83, 0x11, 0x18, 0x1a, 0x59,
  29718. 0x9e, 0x25, 0xfe, 0xdb, 0x85, 0xd0, 0xee, 0x7c,
  29719. 0x97, 0x72, 0xca, 0xf3, 0x0d, 0xd4, 0x19, 0x66,
  29720. 0x14, 0xaf, 0x46, 0x68, 0x75, 0xdb, 0x8f, 0x5f,
  29721. 0x77, 0x7f, 0xfe, 0xa9, 0xe6, 0xa1, 0x9e, 0x46,
  29722. 0x5e, 0x92, 0xda, 0xea, 0xdd, 0x89, 0x01, 0xd9,
  29723. 0xab, 0x25, 0x7d, 0xb4, 0x64, 0x50, 0x8f, 0xa3,
  29724. 0xbe, 0xe2, 0x03, 0xd5, 0xc6, 0x9c, 0xc2, 0xf8,
  29725. 0xac, 0xa4, 0x36, 0xa9, 0x37, 0x10, 0x59, 0x00,
  29726. 0x45, 0xbb, 0x55, 0x33, 0xb9, 0x6f, 0xbc, 0xa2,
  29727. 0x02, 0x9e, 0xa3, 0x1d, 0xf4, 0x17, 0x78, 0x9b,
  29728. 0xbc, 0x42, 0x4e, 0x21, 0xc3, 0xde, 0xb5, 0x70,
  29729. 0x4a, 0x23, 0x1e, 0xd4, 0x36, 0x5d, 0x7a, 0x08,
  29730. 0x37, 0x55, 0x98, 0x07, 0xa0, 0x16, 0xa3, 0x4e,
  29731. 0xa1, 0x2b, 0x96, 0x8b, 0x51, 0x63, 0x48, 0xab,
  29732. 0xc9, 0x19, 0x6f, 0x5f, 0x25, 0x9d, 0xe7, 0x25,
  29733. 0x63, 0xf0, 0x8e, 0xdb, 0x06, 0x2d, 0x42, 0x31,
  29734. 0xfd, 0x14, 0x2b, 0x7a, 0x31, 0x43, 0x04, 0xd5,
  29735. 0xe2, 0x89, 0x2e, 0xa8, 0xe4, 0x6e, 0xd5, 0xa5,
  29736. 0x21, 0x67, 0x9b, 0x92, 0x61, 0x79, 0xdd, 0xe5,
  29737. 0x44, 0x43, 0x45, 0x57, 0x13, 0xec, 0x04, 0xc1,
  29738. 0x41, 0xa3, 0x14, 0x70, 0x86, 0xda, 0x76, 0x5d,
  29739. 0xe8, 0x61, 0xd2, 0xfb, 0x7b, 0xe4, 0x71, 0x46,
  29740. 0xa3, 0x52, 0xbf, 0xf2, 0xa0, 0x3c, 0xc1, 0x90,
  29741. 0x0c, 0x2e, 0xeb, 0xb3, 0x38, 0xae, 0x13, 0x27,
  29742. 0x84, 0xe9, 0x7a, 0xd6, 0x02, 0x40, 0x84, 0xff,
  29743. 0x87, 0x1f, 0x37, 0x44, 0xd8, 0x2e, 0x93, 0xf7,
  29744. 0x0a, 0xff, 0x5b, 0x4d, 0x07, 0x82, 0xfd, 0x6e,
  29745. 0x44, 0xcc, 0x19, 0xc3, 0x7d, 0x7c, 0x31, 0xf9,
  29746. 0x0e, 0xa8, 0x1c, 0x0d, 0xcb, 0x8e, 0xe8, 0x33,
  29747. 0xb2, 0xff, 0x9e, 0x1d, 0x99, 0x7c, 0x46, 0x5b,
  29748. 0xc7, 0x28, 0xec, 0x01, 0x62, 0x82, 0xfe, 0x2a,
  29749. 0x22, 0xa3, 0x86, 0x4e, 0x47, 0xe2, 0x57, 0xf1,
  29750. 0xb4, 0x58, 0x94, 0x89, 0xe5, 0xf1, 0xcd, 0x4d,
  29751. 0x90, 0xd1, 0xa4, 0x4c, 0x34, 0x5d, 0xde, 0xdc,
  29752. 0x39, 0x63, 0x8b, 0x85, 0xfd, 0x02, 0x21, 0xf1,
  29753. 0x12, 0xa3, 0x6d, 0x65, 0x0f, 0x8d, 0xe5, 0xcd,
  29754. 0x70, 0xd5, 0x1d, 0xf8, 0x65, 0x99, 0xfb, 0xe8,
  29755. 0xb5, 0x5a, 0x09, 0x39, 0x9e, 0x09, 0x45, 0x62,
  29756. 0x22, 0x1d, 0xa2, 0x46, 0xbf, 0x75, 0x20, 0xd1,
  29757. 0xe7, 0xb0, 0x06, 0x68, 0xc3, 0x50, 0x48, 0xfc,
  29758. 0xf8, 0x5c, 0x67, 0x69, 0x68, 0x66, 0xb6, 0x81,
  29759. 0x95, 0x91, 0x81, 0x3d, 0xf6, 0x34, 0xd9, 0x4b,
  29760. 0x06, 0x35, 0x17, 0x59, 0x89, 0x18, 0x74, 0x32,
  29761. 0x50, 0xcf, 0x81, 0x16, 0x8e, 0x53, 0x9d, 0x1c,
  29762. 0xad, 0x2d, 0x8e, 0x16, 0x41, 0xda, 0xca, 0xab,
  29763. 0x78, 0x0d, 0xc9, 0x49, 0x61, 0xaa, 0x18, 0xf4,
  29764. 0x56, 0x48, 0x29, 0x8c, 0xe3, 0x9a, 0x7d, 0x58,
  29765. 0xf8, 0x99, 0x72, 0xf1, 0x78, 0xa8, 0x5a, 0x97,
  29766. 0xe3, 0x2a, 0xc6, 0xa9, 0x59, 0xde, 0xcc, 0x62,
  29767. 0xfb, 0xab, 0xc5, 0x9a, 0x0b, 0xc7, 0x16, 0x8f,
  29768. 0x18, 0x20, 0x6e, 0x01, 0x7e, 0x04, 0xef, 0x72,
  29769. 0x83, 0x61, 0xb8, 0x1a, 0x77, 0x0f, 0xd1, 0xa9,
  29770. 0x75, 0xe0, 0x4a, 0x11, 0x69, 0x9d, 0xb6, 0xc9,
  29771. 0x2e, 0xd3, 0xbf, 0xe2, 0x5b, 0x24, 0x77, 0x30,
  29772. 0x85, 0x91, 0xef, 0xa8, 0x93, 0x4e, 0xad, 0x99,
  29773. 0xad, 0xcb, 0x6d, 0x9d, 0x8f, 0xd8, 0x0f, 0xe5,
  29774. 0x41, 0xd9, 0x9e, 0x0b, 0xce, 0x33, 0xd9, 0xbb,
  29775. 0x87, 0x66, 0x2c, 0xa3, 0x0b, 0x68, 0x1b, 0xb0,
  29776. 0x71, 0x30, 0xfa, 0x15, 0x2e, 0xe8, 0xc1, 0x99,
  29777. 0x71, 0x01, 0xcc, 0xdb, 0x6f, 0x9f, 0x8a, 0xfd,
  29778. 0xb4, 0x0f, 0x35, 0xa1, 0x36, 0xf4, 0x3a, 0xc4,
  29779. 0x17, 0x77, 0x43, 0x60, 0x10, 0x18, 0xb4, 0xc2,
  29780. 0xe5, 0xc0, 0x64, 0xd8, 0x38, 0x7c, 0x05, 0x9a,
  29781. 0xfb, 0x2b, 0xb3, 0x9b, 0x9e, 0x34, 0x6b, 0x4b,
  29782. 0xc8, 0x3b, 0x77, 0xe0, 0x6f, 0x08, 0xa1, 0x7b,
  29783. 0x66, 0x69, 0x2f, 0xdb, 0x34, 0x9e, 0x98, 0x90,
  29784. 0x5b, 0x4d, 0x7b, 0xa2, 0x32, 0x8e, 0x64, 0xe6,
  29785. 0x0d, 0x75, 0xc9, 0x96, 0xe3, 0x57, 0xba, 0xad,
  29786. 0x3e, 0x3b, 0x23, 0xfb, 0x9e, 0x7f, 0xc0, 0x3c,
  29787. 0xd5, 0x41, 0x9c, 0xfb, 0xbc, 0xb3, 0x52, 0x49
  29788. #endif
  29789. };
  29790. #endif
  29791. static int test_wc_dilithium_public_der_decode(void)
  29792. {
  29793. EXPECT_DECLS;
  29794. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  29795. defined(WOLFSSL_DILITHIUM_PUBLIC_KEY)
  29796. dilithium_key* key;
  29797. word32 idx = 0;
  29798. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29799. ExpectNotNull(key);
  29800. if (key != NULL) {
  29801. XMEMSET(key, 0, sizeof(*key));
  29802. }
  29803. ExpectIntEQ(wc_dilithium_init(key), 0);
  29804. #ifndef WOLFSSL_NO_ML_DSA_44
  29805. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  29806. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  29807. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  29808. #else
  29809. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  29810. #endif
  29811. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(dilithium_public_der, &idx, key,
  29812. (word32)sizeof(dilithium_public_der)), 0);
  29813. wc_dilithium_free(key);
  29814. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29815. #endif
  29816. return EXPECT_RESULT();
  29817. }
  29818. static int test_wc_dilithium_der(void)
  29819. {
  29820. EXPECT_DECLS;
  29821. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  29822. !defined(WOLFSSL_DILITHIUM_NO_ASN1) && \
  29823. !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
  29824. #define DILITHIUM_MAX_DER_SIZE 8192
  29825. dilithium_key* key;
  29826. WC_RNG rng;
  29827. byte* der = NULL;
  29828. int len;
  29829. int pubLen;
  29830. int pubDerLen;
  29831. int privDerLen;
  29832. int keyDerLen;
  29833. word32 idx;
  29834. #ifndef WOLFSSL_NO_ML_DSA_44
  29835. pubLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE;
  29836. pubDerLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE + 24;
  29837. privDerLen = DILITHIUM_LEVEL2_KEY_SIZE + 30;
  29838. keyDerLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE + DILITHIUM_LEVEL2_KEY_SIZE + 34;
  29839. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  29840. pubLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE;
  29841. pubDerLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE + 24;
  29842. privDerLen = DILITHIUM_LEVEL3_KEY_SIZE + 30;
  29843. keyDerLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE + DILITHIUM_LEVEL3_KEY_SIZE + 34;
  29844. #else
  29845. pubLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE;
  29846. pubDerLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE + 24;
  29847. privDerLen = DILITHIUM_LEVEL5_KEY_SIZE + 30;
  29848. keyDerLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE + DILITHIUM_LEVEL5_KEY_SIZE + 34;
  29849. #endif
  29850. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29851. ExpectNotNull(key);
  29852. der = (byte*)XMALLOC(DILITHIUM_MAX_DER_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29853. ExpectNotNull(der);
  29854. if (key != NULL) {
  29855. XMEMSET(key, 0, sizeof(*key));
  29856. }
  29857. XMEMSET(&rng, 0, sizeof(WC_RNG));
  29858. ExpectIntEQ(wc_InitRng(&rng), 0);
  29859. ExpectIntEQ(wc_dilithium_init(key), 0);
  29860. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  29861. 0), BAD_FUNC_ARG);
  29862. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  29863. 1), BAD_FUNC_ARG);
  29864. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
  29865. BAD_FUNC_ARG);
  29866. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der, &idx, key, pubDerLen),
  29867. BAD_FUNC_ARG);
  29868. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, privDerLen),
  29869. BAD_FUNC_ARG);
  29870. #ifndef WOLFSSL_NO_ML_DSA_44
  29871. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  29872. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  29873. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  29874. #else
  29875. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  29876. #endif
  29877. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  29878. 0), BAD_FUNC_ARG);
  29879. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  29880. 1), BAD_FUNC_ARG);
  29881. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
  29882. BAD_FUNC_ARG);
  29883. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  29884. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, NULL, 0 ,
  29885. 0), BAD_FUNC_ARG);
  29886. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, der , 0 ,
  29887. 0), BAD_FUNC_ARG);
  29888. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, NULL, DILITHIUM_MAX_DER_SIZE,
  29889. 0), BAD_FUNC_ARG);
  29890. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, der , DILITHIUM_MAX_DER_SIZE,
  29891. 0), BAD_FUNC_ARG);
  29892. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , der , 0 ,
  29893. 0), BUFFER_E);
  29894. /* Get length only. */
  29895. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, 0 ,
  29896. 0), pubLen);
  29897. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE,
  29898. 0), pubLen);
  29899. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, 0 ,
  29900. 1), pubDerLen);
  29901. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE,
  29902. 1), pubDerLen);
  29903. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, NULL,
  29904. 0 ), BAD_FUNC_ARG);
  29905. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , NULL,
  29906. 0 ), BAD_FUNC_ARG);
  29907. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, der ,
  29908. 0 ), BAD_FUNC_ARG);
  29909. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, NULL,
  29910. DILITHIUM_MAX_DER_SIZE), BAD_FUNC_ARG);
  29911. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, der ,
  29912. DILITHIUM_MAX_DER_SIZE), BAD_FUNC_ARG);
  29913. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , der ,
  29914. 0 ), BAD_FUNC_ARG);
  29915. /* Get length only. */
  29916. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , NULL,
  29917. DILITHIUM_MAX_DER_SIZE), privDerLen);
  29918. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, NULL, 0 ),
  29919. BAD_FUNC_ARG);
  29920. ExpectIntEQ(wc_Dilithium_KeyToDer(key , NULL, 0 ),
  29921. BAD_FUNC_ARG);
  29922. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, der , 0 ),
  29923. BAD_FUNC_ARG);
  29924. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, NULL, DILITHIUM_MAX_DER_SIZE),
  29925. BAD_FUNC_ARG);
  29926. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, der , DILITHIUM_MAX_DER_SIZE),
  29927. BAD_FUNC_ARG);
  29928. ExpectIntEQ(wc_Dilithium_KeyToDer(key , der , 0 ),
  29929. BAD_FUNC_ARG);
  29930. /* Get length only. */
  29931. ExpectIntEQ(wc_Dilithium_KeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE),
  29932. keyDerLen);
  29933. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, NULL, NULL, 0 ),
  29934. BAD_FUNC_ARG);
  29935. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , NULL, NULL, 0 ),
  29936. BAD_FUNC_ARG);
  29937. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, &idx, NULL, 0 ),
  29938. BAD_FUNC_ARG);
  29939. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, NULL, key , 0 ),
  29940. BAD_FUNC_ARG);
  29941. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, NULL, NULL, pubDerLen),
  29942. BAD_FUNC_ARG);
  29943. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, &idx, key , pubDerLen),
  29944. BAD_FUNC_ARG);
  29945. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , NULL, key , pubDerLen),
  29946. BAD_FUNC_ARG);
  29947. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , &idx, NULL, pubDerLen),
  29948. BAD_FUNC_ARG);
  29949. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , &idx, key , 0 ),
  29950. BAD_FUNC_ARG);
  29951. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, NULL, NULL, 0 ),
  29952. BAD_FUNC_ARG);
  29953. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , NULL, NULL, 0 ),
  29954. BAD_FUNC_ARG);
  29955. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, &idx, NULL, 0 ),
  29956. BAD_FUNC_ARG);
  29957. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, NULL, key , 0 ),
  29958. BAD_FUNC_ARG);
  29959. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, NULL, NULL, privDerLen),
  29960. BAD_FUNC_ARG);
  29961. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, &idx, key , privDerLen),
  29962. BAD_FUNC_ARG);
  29963. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , NULL, key , privDerLen),
  29964. BAD_FUNC_ARG);
  29965. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , &idx, NULL, privDerLen),
  29966. BAD_FUNC_ARG);
  29967. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , &idx, key , 0 ),
  29968. BAD_FUNC_ARG);
  29969. ExpectIntEQ(len = wc_Dilithium_PublicKeyToDer(key, der,
  29970. DILITHIUM_MAX_DER_SIZE, 0), pubLen);
  29971. ExpectIntEQ(wc_dilithium_import_public(der, len, key), 0);
  29972. ExpectIntEQ(len = wc_Dilithium_PublicKeyToDer(key, der,
  29973. DILITHIUM_MAX_DER_SIZE, 1), pubDerLen);
  29974. idx = 0;
  29975. {
  29976. fprintf(stderr, "\n");
  29977. for (int ii = 0; ii < pubDerLen; ii++) {
  29978. if ((ii % 8) == 0) fprintf(stderr, " ");
  29979. fprintf(stderr, "0x%02x,", der[ii]);
  29980. if ((ii % 8) == 7) fprintf(stderr, "\n");
  29981. else fprintf(stderr, " ");
  29982. }
  29983. }
  29984. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der, &idx, key, len), 0);
  29985. ExpectIntEQ(len = wc_Dilithium_PrivateKeyToDer(key, der,
  29986. DILITHIUM_MAX_DER_SIZE), privDerLen);
  29987. idx = 0;
  29988. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, len), 0);
  29989. ExpectIntEQ(len = wc_Dilithium_KeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
  29990. keyDerLen);
  29991. idx = 0;
  29992. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, len), 0);
  29993. wc_dilithium_free(key);
  29994. wc_FreeRng(&rng);
  29995. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29996. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29997. #endif
  29998. return EXPECT_RESULT();
  29999. }
  30000. static int test_wc_dilithium_make_key_from_seed(void)
  30001. {
  30002. EXPECT_DECLS;
  30003. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  30004. !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
  30005. dilithium_key* key;
  30006. #ifndef WOLFSSL_NO_ML_DSA_44
  30007. static const byte seed_44[] = {
  30008. 0xBA, 0xC0, 0x59, 0x52, 0x75, 0x5B, 0x26, 0x47,
  30009. 0x01, 0xCA, 0x7D, 0x80, 0x6D, 0xFA, 0x08, 0x35,
  30010. 0x10, 0x28, 0xF6, 0x7B, 0x0E, 0x83, 0xC4, 0x24,
  30011. 0x01, 0x6F, 0x66, 0xCC, 0x83, 0x87, 0xD4, 0x69
  30012. };
  30013. static const byte pk_44[] = {
  30014. 0x86, 0xF0, 0x0C, 0x20, 0xE0, 0xDA, 0xEE, 0x5E,
  30015. 0x1E, 0xDE, 0x71, 0x39, 0x49, 0x0C, 0xC8, 0xCF,
  30016. 0xEF, 0xC9, 0xAB, 0x62, 0x3B, 0x8D, 0xEF, 0x0B,
  30017. 0xD8, 0x03, 0x12, 0x5B, 0x4A, 0xB2, 0x83, 0x61,
  30018. 0xED, 0x7E, 0xA9, 0xED, 0x2D, 0xED, 0x5A, 0x71,
  30019. 0xDD, 0xAE, 0x4A, 0x06, 0xE0, 0x2A, 0x5A, 0xAF,
  30020. 0x99, 0x69, 0x89, 0xC6, 0xAF, 0xE3, 0x2A, 0xFE,
  30021. 0x3D, 0x6E, 0x0A, 0x46, 0x71, 0x48, 0xD7, 0x17,
  30022. 0x99, 0x20, 0x01, 0x78, 0xD5, 0x8B, 0x40, 0xCB,
  30023. 0x81, 0xA0, 0x33, 0x38, 0xAE, 0x2B, 0x83, 0x4A,
  30024. 0xFD, 0x5F, 0xE0, 0xB7, 0xEE, 0xA0, 0xC4, 0x3D,
  30025. 0xB6, 0xA4, 0xD5, 0x59, 0x4B, 0xDD, 0x87, 0x1A,
  30026. 0xFC, 0x03, 0x30, 0xA0, 0xB3, 0xAD, 0x75, 0x3C,
  30027. 0xD4, 0x47, 0x72, 0x59, 0xCE, 0xB7, 0x80, 0xFD,
  30028. 0x34, 0x35, 0x5E, 0x96, 0xC8, 0x42, 0xD9, 0xDD,
  30029. 0x6C, 0xF1, 0xAB, 0xEF, 0x48, 0xD1, 0xA8, 0x02,
  30030. 0x02, 0x0F, 0x5B, 0x71, 0x4D, 0x36, 0x1E, 0x0D,
  30031. 0xC2, 0x09, 0x46, 0x7B, 0xF9, 0xEA, 0x24, 0x8F,
  30032. 0x7C, 0xCF, 0xB8, 0x9C, 0xF7, 0x49, 0x15, 0x8E,
  30033. 0x16, 0x49, 0x7E, 0xC5, 0x54, 0xF5, 0x03, 0x1D,
  30034. 0x16, 0x12, 0x02, 0x72, 0x1B, 0x38, 0x2D, 0x58,
  30035. 0x53, 0x15, 0x5E, 0xB6, 0x72, 0xCC, 0xA1, 0x09,
  30036. 0xB0, 0x2F, 0x10, 0xFA, 0x21, 0x45, 0x46, 0x37,
  30037. 0xD4, 0xFA, 0x7F, 0xFB, 0xB0, 0xD9, 0x20, 0xE2,
  30038. 0xCB, 0x56, 0xB3, 0x1E, 0xDF, 0x82, 0x67, 0x25,
  30039. 0x09, 0xD1, 0x8F, 0xFF, 0xE0, 0x43, 0xBD, 0x37,
  30040. 0x2B, 0x73, 0x0E, 0x13, 0x08, 0xC9, 0x49, 0x88,
  30041. 0x69, 0x69, 0xD9, 0x8C, 0x86, 0xE4, 0x7E, 0x63,
  30042. 0x35, 0xC5, 0xE1, 0xD0, 0x14, 0x9A, 0x89, 0x27,
  30043. 0x28, 0x17, 0xB0, 0x5B, 0x7A, 0x8F, 0xDD, 0x72,
  30044. 0x8B, 0x0A, 0x0D, 0x49, 0x58, 0x59, 0x2F, 0x0D,
  30045. 0x8F, 0x3D, 0x16, 0xCE, 0x7B, 0x11, 0xC7, 0x06,
  30046. 0x5D, 0xD5, 0x6D, 0x7B, 0x96, 0xED, 0x1E, 0x1A,
  30047. 0xF4, 0x10, 0x85, 0xDA, 0xDE, 0x84, 0x2F, 0x2B,
  30048. 0xBA, 0xFB, 0xA2, 0x5F, 0x33, 0x7D, 0x7C, 0x18,
  30049. 0x6B, 0xDF, 0x43, 0x3C, 0xE9, 0xEB, 0xB4, 0xC5,
  30050. 0x8E, 0x52, 0xF5, 0x7E, 0x4C, 0x3E, 0x6A, 0x33,
  30051. 0x41, 0x4C, 0x14, 0x05, 0x8E, 0x2C, 0x19, 0x0E,
  30052. 0x86, 0x91, 0x66, 0xDE, 0xF6, 0x4B, 0x35, 0xC2,
  30053. 0xDF, 0x3D, 0x4C, 0x7B, 0xC5, 0x58, 0x5E, 0x86,
  30054. 0x89, 0x6A, 0xFC, 0x86, 0x48, 0x75, 0xD1, 0x18,
  30055. 0xD1, 0xCB, 0x41, 0xC0, 0xF6, 0xD8, 0x87, 0x79,
  30056. 0xD9, 0xA2, 0x56, 0x2E, 0x83, 0x26, 0x11, 0xC1,
  30057. 0x4B, 0x53, 0x37, 0x85, 0x62, 0xFF, 0x6A, 0x67,
  30058. 0xFD, 0x18, 0x79, 0xD7, 0x55, 0x9B, 0xF7, 0x64,
  30059. 0xA9, 0x21, 0xB6, 0x1B, 0xF6, 0x11, 0x85, 0xF8,
  30060. 0xC0, 0x68, 0xDE, 0x61, 0x0C, 0x61, 0x7E, 0x8E,
  30061. 0xED, 0x9E, 0x58, 0x84, 0x16, 0x1A, 0x28, 0xC5,
  30062. 0x41, 0x63, 0xB3, 0xF0, 0x82, 0xAA, 0xE8, 0x36,
  30063. 0x81, 0x5C, 0xD3, 0xB7, 0xFB, 0x92, 0xF4, 0x7A,
  30064. 0x1E, 0x85, 0xA2, 0xB7, 0x21, 0xD5, 0xFA, 0xC8,
  30065. 0xE8, 0x02, 0x43, 0x5B, 0x56, 0x42, 0x03, 0x17,
  30066. 0x67, 0xEE, 0x3E, 0x31, 0x23, 0x63, 0xC7, 0x33,
  30067. 0x95, 0xDE, 0x07, 0xF6, 0x11, 0x3A, 0x2C, 0x3F,
  30068. 0x7B, 0xBB, 0x2D, 0x5C, 0x23, 0xF9, 0x2F, 0x9C,
  30069. 0x51, 0x19, 0x9F, 0x35, 0xC3, 0x18, 0x9F, 0x83,
  30070. 0x6E, 0xA8, 0x03, 0xF1, 0x79, 0x1F, 0xB0, 0xC8,
  30071. 0x2F, 0xF4, 0x2E, 0x9A, 0x26, 0xF3, 0x44, 0x02,
  30072. 0x8F, 0x45, 0x8B, 0xB0, 0x25, 0x1D, 0xF2, 0xD4,
  30073. 0x55, 0xB7, 0x65, 0xEF, 0xDB, 0x3D, 0x8E, 0x92,
  30074. 0xC8, 0xA0, 0x63, 0x4C, 0x38, 0xA3, 0x54, 0xD3,
  30075. 0xC2, 0x5A, 0x2A, 0x6A, 0x15, 0x27, 0x2A, 0xE2,
  30076. 0xFC, 0x25, 0xB6, 0xC8, 0x68, 0xEB, 0xED, 0x2D,
  30077. 0x23, 0xE8, 0x6D, 0x5C, 0xDD, 0x3F, 0x18, 0xB4,
  30078. 0x6E, 0x79, 0x36, 0xC9, 0x1C, 0xB4, 0x92, 0x41,
  30079. 0xAD, 0x35, 0xD4, 0x15, 0xE4, 0x64, 0x1C, 0x51,
  30080. 0xCB, 0x0C, 0x41, 0xB7, 0xFD, 0xC1, 0x09, 0x3E,
  30081. 0xD2, 0x4D, 0x38, 0x88, 0x77, 0x1C, 0x71, 0x91,
  30082. 0x74, 0xD3, 0x28, 0xE0, 0xCE, 0x9A, 0x11, 0x8D,
  30083. 0xBF, 0x4D, 0x8D, 0xF0, 0x44, 0xF6, 0x79, 0xFC,
  30084. 0x4C, 0xAD, 0x17, 0x88, 0xC0, 0x8C, 0x0B, 0x7A,
  30085. 0x90, 0x01, 0x53, 0x6C, 0x6B, 0x44, 0xF6, 0xE5,
  30086. 0x2E, 0xEC, 0x44, 0x4F, 0xB8, 0x9B, 0x10, 0xBE,
  30087. 0xCF, 0x55, 0x55, 0x29, 0x83, 0xB8, 0xD0, 0x25,
  30088. 0x5B, 0xCE, 0x8F, 0xA5, 0xB7, 0x6C, 0xA7, 0x47,
  30089. 0x65, 0xA9, 0xE9, 0x9B, 0xA5, 0xBC, 0x28, 0x1D,
  30090. 0x9F, 0x1F, 0x5E, 0x97, 0x42, 0x10, 0x84, 0x92,
  30091. 0xFB, 0x38, 0x0B, 0x2E, 0xAC, 0x79, 0x0A, 0x7D,
  30092. 0x00, 0x2C, 0x35, 0xD0, 0x54, 0x0D, 0x28, 0xE7,
  30093. 0xAB, 0x06, 0x02, 0xDA, 0x89, 0xA3, 0x06, 0x8E,
  30094. 0x13, 0x9A, 0xA7, 0xCA, 0x48, 0x09, 0xB0, 0x48,
  30095. 0x37, 0x08, 0xA7, 0x7D, 0xDA, 0xEB, 0x58, 0x64,
  30096. 0x39, 0xB3, 0xF3, 0xB2, 0x4C, 0x00, 0x4B, 0xCB,
  30097. 0x94, 0x36, 0xD4, 0x7C, 0x73, 0x45, 0xC8, 0x93,
  30098. 0xE5, 0x2A, 0x11, 0xF0, 0xEF, 0x0C, 0xED, 0x5F,
  30099. 0x8B, 0x0C, 0x86, 0xAD, 0x3A, 0x01, 0x07, 0x1A,
  30100. 0xC0, 0x34, 0xE8, 0x74, 0x21, 0x27, 0x73, 0x56,
  30101. 0x93, 0x76, 0x5D, 0x80, 0x59, 0xB4, 0xA4, 0xDC,
  30102. 0x80, 0xE7, 0xCE, 0x70, 0x0E, 0x0F, 0xEC, 0x56,
  30103. 0x42, 0x6E, 0x9C, 0x76, 0x3D, 0xF6, 0xB4, 0x41,
  30104. 0xE2, 0x3E, 0xAC, 0x25, 0xE7, 0x86, 0xA7, 0xA7,
  30105. 0x0A, 0x0D, 0x5D, 0x04, 0x1F, 0x45, 0xD4, 0x5B,
  30106. 0x42, 0x38, 0x4C, 0x60, 0xE7, 0xB7, 0x0D, 0xC7,
  30107. 0x28, 0x4F, 0xA5, 0x4E, 0x0C, 0x1B, 0xC4, 0xDA,
  30108. 0x50, 0x1A, 0xA0, 0x93, 0xAE, 0x10, 0x9A, 0x1A,
  30109. 0xC8, 0xC6, 0x56, 0xFC, 0x0A, 0xEA, 0x89, 0x3A,
  30110. 0x28, 0x21, 0xE9, 0x52, 0x9D, 0xEB, 0x07, 0x68,
  30111. 0xC1, 0x57, 0x32, 0x25, 0x1F, 0x93, 0x5D, 0x35,
  30112. 0xB2, 0x4B, 0x58, 0x30, 0xAF, 0x51, 0xC6, 0x7D,
  30113. 0x47, 0xD1, 0xA2, 0xAD, 0xDE, 0x75, 0x48, 0x84,
  30114. 0x74, 0x19, 0x74, 0x18, 0xA0, 0x2C, 0xD8, 0xB2,
  30115. 0xFE, 0x44, 0x78, 0x95, 0x6A, 0xBF, 0x56, 0x4D,
  30116. 0x20, 0x79, 0xE7, 0xE2, 0xE3, 0x56, 0x69, 0xB3,
  30117. 0xFA, 0xE1, 0xEB, 0xE6, 0x11, 0xAC, 0x18, 0xB3,
  30118. 0x98, 0xC1, 0x04, 0x20, 0x96, 0x4B, 0xAD, 0xDE,
  30119. 0x5B, 0x18, 0xEB, 0x7B, 0xBC, 0x15, 0x11, 0x57,
  30120. 0x29, 0x10, 0xE5, 0x80, 0x78, 0x4A, 0xF0, 0x87,
  30121. 0xF6, 0xD1, 0x3C, 0x23, 0xC5, 0xF4, 0x2D, 0xD7,
  30122. 0xAB, 0xA4, 0xD7, 0xB8, 0x45, 0x8E, 0x04, 0x1B,
  30123. 0x78, 0x59, 0x9F, 0x81, 0xE6, 0x04, 0xDF, 0x70,
  30124. 0x2B, 0x14, 0x74, 0x16, 0x49, 0xDA, 0xF0, 0xE1,
  30125. 0xC8, 0x29, 0xCC, 0x87, 0x8C, 0x2F, 0xFB, 0x18,
  30126. 0x3B, 0x47, 0xFC, 0x79, 0x04, 0x84, 0xCB, 0x0A,
  30127. 0xD2, 0x64, 0xBF, 0x86, 0xEA, 0x01, 0xAC, 0xE0,
  30128. 0xBD, 0xEC, 0x3B, 0xE1, 0xA7, 0x6C, 0xDE, 0x1D,
  30129. 0x58, 0x76, 0xCC, 0x53, 0x9E, 0xF6, 0xC6, 0xD4,
  30130. 0x2C, 0x87, 0x92, 0xA2, 0x89, 0x27, 0x31, 0x33,
  30131. 0x01, 0xA5, 0xA2, 0xE8, 0x8F, 0x13, 0x19, 0x0F,
  30132. 0xFD, 0x73, 0xB9, 0x91, 0xBD, 0xB8, 0x80, 0x9A,
  30133. 0xA3, 0xB1, 0x21, 0x6C, 0x91, 0x13, 0x8A, 0xAE,
  30134. 0xC7, 0xCB, 0x67, 0x14, 0xD1, 0xC0, 0x28, 0x89,
  30135. 0x04, 0x8C, 0x9F, 0xDE, 0xA0, 0x9A, 0x99, 0xA8,
  30136. 0x61, 0xE6, 0x8F, 0x8E, 0x39, 0xEF, 0x6B, 0x5E,
  30137. 0x84, 0x5F, 0x5D, 0x24, 0x37, 0x73, 0x9D, 0x75,
  30138. 0xC4, 0xEF, 0xE2, 0xA1, 0xF2, 0xBC, 0x0D, 0xE1,
  30139. 0x0D, 0xEC, 0xFA, 0xEE, 0xC1, 0x63, 0xC8, 0x2E,
  30140. 0x7D, 0x85, 0x65, 0xC3, 0xF2, 0x0D, 0x8B, 0x73,
  30141. 0xF9, 0x3B, 0x0B, 0x3D, 0x49, 0x8B, 0xFB, 0x16,
  30142. 0x5B, 0x75, 0x48, 0x9B, 0x56, 0x0A, 0x83, 0x4C,
  30143. 0x0D, 0x13, 0xB2, 0xB4, 0x25, 0xC7, 0x2C, 0xCB,
  30144. 0xA7, 0x9E, 0xCA, 0x41, 0x44, 0x14, 0x9A, 0x03,
  30145. 0xD3, 0x01, 0x8C, 0xB0, 0xD5, 0xA9, 0x36, 0xA4,
  30146. 0x16, 0x21, 0x49, 0x0A, 0x99, 0xA1, 0x89, 0xA5,
  30147. 0x91, 0x10, 0xA2, 0x1B, 0x3F, 0x98, 0x1E, 0x1C,
  30148. 0x43, 0xAA, 0x9C, 0x16, 0x5A, 0xF0, 0x18, 0x64,
  30149. 0x0F, 0x6A, 0xE3, 0x97, 0x83, 0x31, 0x4E, 0x84,
  30150. 0xC9, 0xEA, 0xD8, 0x9F, 0xEA, 0x9E, 0xD6, 0xF2,
  30151. 0x0E, 0x15, 0xA5, 0x48, 0x15, 0x8B, 0x10, 0x1D,
  30152. 0x77, 0x78, 0x1B, 0x54, 0x03, 0xC1, 0x2C, 0xB1,
  30153. 0xC8, 0x22, 0x11, 0x9D, 0xB8, 0x82, 0x94, 0x26,
  30154. 0xA0, 0xED, 0x6C, 0xAD, 0xA8, 0x03, 0xC2, 0xED,
  30155. 0x02, 0x74, 0x3E, 0x54, 0xBD, 0x77, 0xA6, 0x0B,
  30156. 0x37, 0xFE, 0x04, 0xCD, 0x25, 0x10, 0x2D, 0x52,
  30157. 0xC2, 0xD4, 0x5B, 0x9B, 0xAE, 0xFE, 0x35, 0x73,
  30158. 0x16, 0x61, 0x84, 0x25, 0x1D, 0xBE, 0x95, 0x34,
  30159. 0xA4, 0xF6, 0xB9, 0xA4, 0xF9, 0xAA, 0x5D, 0x1E,
  30160. 0x49, 0xBB, 0x19, 0xD9, 0x64, 0xD7, 0x48, 0x1A,
  30161. 0x0A, 0x93, 0xC3, 0x69, 0x13, 0x12, 0x68, 0xBB,
  30162. 0x97, 0x97, 0xBD, 0x99, 0x69, 0xCE, 0xE6, 0xF5,
  30163. 0x84, 0x7B, 0xCC, 0xE4, 0x7D, 0xD3, 0xCD, 0x8A,
  30164. 0x7A, 0x4B, 0x98, 0xF4, 0x09, 0x9D, 0xEA, 0x5D,
  30165. 0x4E, 0x1F, 0xE1, 0x1E, 0x6C, 0x48, 0xD3, 0x5E,
  30166. 0x67, 0xD9, 0xFF, 0x64, 0x4D, 0xA7, 0x64, 0x7A,
  30167. 0x01, 0xB2, 0xE9, 0x63, 0x14, 0x10, 0xB7, 0x08,
  30168. 0x0C, 0xF9, 0x4D, 0x66, 0x48, 0x46, 0xE3, 0xC2,
  30169. 0x48, 0x6B, 0x47, 0xCE, 0x00, 0x98, 0x92, 0x83,
  30170. 0xF7, 0xE0, 0x1F, 0x96, 0xFA, 0x53, 0xD5, 0x49,
  30171. 0x1C, 0xC7, 0x89, 0xB4, 0xA5, 0x4B, 0x63, 0xBF,
  30172. 0xD2, 0x00, 0x79, 0xDD, 0xC1, 0x60, 0xAA, 0xF2,
  30173. 0x0F, 0x47, 0xB9, 0x4F, 0x8A, 0x66, 0x05, 0x3D,
  30174. 0x96, 0x36, 0x64, 0x48, 0x5F, 0x7E, 0x56, 0x2B,
  30175. 0xB3, 0x47, 0xE2, 0x76, 0x64, 0x21, 0x65, 0x34,
  30176. 0xFC, 0xDD, 0x2D, 0x4C, 0xE2, 0x99, 0x33, 0x04,
  30177. 0xE4, 0x26, 0x15, 0x37, 0x6C, 0x32, 0xB9, 0x17
  30178. };
  30179. static const byte sk_44[] = {
  30180. 0x86, 0xF0, 0x0C, 0x20, 0xE0, 0xDA, 0xEE, 0x5E,
  30181. 0x1E, 0xDE, 0x71, 0x39, 0x49, 0x0C, 0xC8, 0xCF,
  30182. 0xEF, 0xC9, 0xAB, 0x62, 0x3B, 0x8D, 0xEF, 0x0B,
  30183. 0xD8, 0x03, 0x12, 0x5B, 0x4A, 0xB2, 0x83, 0x61,
  30184. 0x73, 0x61, 0x49, 0x01, 0x0F, 0x94, 0x08, 0x30,
  30185. 0x26, 0x02, 0x12, 0x63, 0x64, 0x15, 0x7A, 0x4D,
  30186. 0xBA, 0xF5, 0x25, 0xA7, 0xAA, 0x0B, 0x7C, 0x3D,
  30187. 0xCE, 0x05, 0x91, 0x95, 0xEF, 0x17, 0x2F, 0xE2,
  30188. 0x5A, 0x03, 0x5E, 0x2E, 0x4D, 0xFA, 0xE7, 0x5F,
  30189. 0xCD, 0x61, 0x34, 0xFB, 0x3D, 0x3C, 0x5C, 0x60,
  30190. 0x1A, 0x6F, 0x09, 0xB5, 0x9D, 0xDD, 0x90, 0x53,
  30191. 0xF6, 0x89, 0x50, 0xC2, 0xE1, 0xED, 0x0A, 0x61,
  30192. 0x8F, 0xFA, 0xDC, 0x2D, 0xB2, 0x8B, 0xA1, 0x56,
  30193. 0xAC, 0x5E, 0x0E, 0xF1, 0x3B, 0x1E, 0x22, 0x9F,
  30194. 0xAA, 0x05, 0x96, 0xA3, 0x5E, 0x44, 0x86, 0xA8,
  30195. 0xBA, 0x15, 0xD1, 0x11, 0x7D, 0xAA, 0xD0, 0xAA,
  30196. 0x01, 0x27, 0x25, 0x04, 0x82, 0x89, 0xA4, 0x22,
  30197. 0x2E, 0xDB, 0x80, 0x45, 0xD2, 0x30, 0x45, 0x59,
  30198. 0x16, 0x64, 0xE2, 0x08, 0x86, 0x50, 0x00, 0x8E,
  30199. 0xCA, 0x08, 0x51, 0x5A, 0x06, 0x01, 0x54, 0x82,
  30200. 0x20, 0xC4, 0x92, 0x30, 0x02, 0x21, 0x4E, 0x0A,
  30201. 0x93, 0x89, 0x84, 0xB8, 0x70, 0x24, 0x40, 0x51,
  30202. 0x24, 0xB3, 0x44, 0xDB, 0x08, 0x40, 0x1B, 0x37,
  30203. 0x44, 0x21, 0x22, 0x8E, 0x8C, 0x16, 0x42, 0x10,
  30204. 0x22, 0x0E, 0xA1, 0xB2, 0x8D, 0x18, 0x49, 0x30,
  30205. 0xC1, 0x32, 0x69, 0x21, 0x03, 0x8E, 0x49, 0x44,
  30206. 0x08, 0xD3, 0x16, 0x89, 0x10, 0xA4, 0x25, 0x5C,
  30207. 0x22, 0x8A, 0xC0, 0xC8, 0x08, 0xC1, 0x04, 0x6A,
  30208. 0xD2, 0xA0, 0x50, 0x8A, 0x02, 0x52, 0x92, 0x16,
  30209. 0x44, 0x54, 0x30, 0x4A, 0x92, 0x32, 0x0C, 0x4C,
  30210. 0x44, 0x2D, 0x04, 0x15, 0x2A, 0x99, 0x24, 0x42,
  30211. 0x52, 0xA8, 0x30, 0x53, 0x24, 0x85, 0x9A, 0xB8,
  30212. 0x01, 0xE2, 0x08, 0x09, 0x23, 0x28, 0x08, 0xC4,
  30213. 0x98, 0x85, 0x0B, 0xB9, 0x40, 0x60, 0x26, 0x28,
  30214. 0x0A, 0x45, 0x12, 0x0C, 0x43, 0x84, 0x82, 0x16,
  30215. 0x89, 0xC4, 0x48, 0x28, 0x58, 0x18, 0x2A, 0x20,
  30216. 0x07, 0x02, 0xD3, 0x82, 0x45, 0x50, 0xB0, 0x50,
  30217. 0x64, 0x36, 0x91, 0x52, 0x02, 0x45, 0x5A, 0x42,
  30218. 0x26, 0x01, 0x28, 0x71, 0xD4, 0x86, 0x10, 0x19,
  30219. 0xC4, 0x68, 0xC4, 0x30, 0x66, 0xE0, 0x02, 0x49,
  30220. 0x18, 0x34, 0x05, 0x04, 0x02, 0x04, 0x90, 0x94,
  30221. 0x4C, 0x01, 0xA7, 0x80, 0x12, 0x97, 0x08, 0x19,
  30222. 0xC5, 0x41, 0x24, 0xC1, 0x61, 0x08, 0xB0, 0x0C,
  30223. 0x21, 0xC1, 0x49, 0x9B, 0x42, 0x51, 0x62, 0x18,
  30224. 0x22, 0x54, 0x06, 0x06, 0x93, 0x26, 0x70, 0x49,
  30225. 0x86, 0x91, 0x00, 0x28, 0x6C, 0x02, 0xC9, 0x60,
  30226. 0x13, 0xC3, 0x09, 0xCB, 0x14, 0x66, 0x09, 0x17,
  30227. 0x25, 0x1C, 0x16, 0x89, 0x01, 0xB6, 0x01, 0x60,
  30228. 0x86, 0x71, 0x58, 0x96, 0x04, 0x82, 0x38, 0x61,
  30229. 0x43, 0x40, 0x72, 0xCC, 0x46, 0x71, 0x81, 0x20,
  30230. 0x2C, 0x18, 0x20, 0x6E, 0x03, 0x91, 0x11, 0x9A,
  30231. 0x08, 0x89, 0x43, 0x06, 0x48, 0x64, 0x02, 0x6C,
  30232. 0x21, 0x33, 0x8D, 0x48, 0x16, 0x66, 0x9B, 0xA4,
  30233. 0x2D, 0x01, 0x10, 0x66, 0xDC, 0xB0, 0x25, 0x40,
  30234. 0xA0, 0x24, 0xA2, 0xB4, 0x44, 0xC8, 0x26, 0x61,
  30235. 0x0A, 0x10, 0x4E, 0xD0, 0x04, 0x11, 0x4A, 0x82,
  30236. 0x51, 0x03, 0x04, 0x6C, 0x18, 0x88, 0x6C, 0xE0,
  30237. 0x98, 0x41, 0x11, 0x29, 0x06, 0x62, 0x12, 0x8E,
  30238. 0xDA, 0x42, 0x91, 0x09, 0x48, 0x60, 0xD1, 0xB4,
  30239. 0x80, 0x10, 0x30, 0x30, 0x80, 0x38, 0x82, 0xD0,
  30240. 0x84, 0x0D, 0x08, 0x14, 0x92, 0x24, 0x41, 0x40,
  30241. 0x0C, 0x10, 0x89, 0xCC, 0x38, 0x8A, 0x13, 0xB6,
  30242. 0x89, 0x1A, 0xA7, 0x24, 0x5C, 0x10, 0x12, 0x1B,
  30243. 0x21, 0x50, 0x91, 0xB4, 0x29, 0x99, 0xB6, 0x51,
  30244. 0x04, 0xB1, 0x91, 0x59, 0xA6, 0x05, 0x19, 0x08,
  30245. 0x4A, 0x4A, 0x84, 0x6C, 0x1C, 0x49, 0x02, 0x44,
  30246. 0x20, 0x85, 0x14, 0xB1, 0x89, 0x09, 0x44, 0x2C,
  30247. 0x10, 0x02, 0x22, 0xE1, 0xB4, 0x25, 0x01, 0x21,
  30248. 0x71, 0x53, 0xC2, 0x85, 0x82, 0x88, 0x28, 0xC0,
  30249. 0x02, 0x52, 0x19, 0xC5, 0x51, 0x19, 0xA5, 0x09,
  30250. 0xC0, 0x82, 0x91, 0x21, 0x47, 0x0D, 0x1C, 0x30,
  30251. 0x69, 0xDC, 0xB8, 0x6C, 0x04, 0x41, 0x6A, 0x91,
  30252. 0x16, 0x40, 0xA0, 0xC8, 0x24, 0x1A, 0x10, 0x01,
  30253. 0x04, 0x39, 0x25, 0x80, 0x16, 0x02, 0x63, 0x36,
  30254. 0x71, 0x90, 0xB0, 0x8D, 0x44, 0x16, 0x8E, 0xDA,
  30255. 0x16, 0x2E, 0xCB, 0x44, 0x20, 0x54, 0x38, 0x06,
  30256. 0x54, 0xC4, 0x01, 0x51, 0x40, 0x86, 0x52, 0x44,
  30257. 0x0E, 0x82, 0x02, 0x32, 0x21, 0x38, 0x89, 0x19,
  30258. 0x04, 0x40, 0xD8, 0x12, 0x68, 0x21, 0x98, 0x11,
  30259. 0x03, 0x33, 0x8A, 0x18, 0x00, 0x45, 0xCB, 0x22,
  30260. 0x32, 0xC3, 0x04, 0x46, 0x09, 0x18, 0x51, 0x22,
  30261. 0x44, 0x89, 0x13, 0x16, 0x6E, 0xDA, 0x46, 0x45,
  30262. 0x09, 0x19, 0x41, 0x81, 0x10, 0x01, 0xDC, 0x18,
  30263. 0x8E, 0xC8, 0x44, 0x4C, 0x00, 0x17, 0x82, 0x9C,
  30264. 0xA6, 0x4D, 0xC8, 0x08, 0x10, 0x24, 0x42, 0x6D,
  30265. 0x91, 0x38, 0x89, 0x8C, 0x40, 0x6E, 0x00, 0x35,
  30266. 0x11, 0xD3, 0x24, 0x09, 0x1A, 0x01, 0x65, 0x88,
  30267. 0x48, 0x45, 0x09, 0x01, 0x71, 0x43, 0xB8, 0x80,
  30268. 0x11, 0x82, 0x2C, 0x84, 0xB8, 0x49, 0x58, 0x14,
  30269. 0x28, 0x92, 0x20, 0x32, 0x09, 0x12, 0x05, 0x20,
  30270. 0x81, 0x2D, 0x5B, 0x86, 0x11, 0x04, 0x90, 0x45,
  30271. 0x49, 0x80, 0x40, 0xD1, 0xC8, 0x24, 0x98, 0xC2,
  30272. 0x2C, 0x99, 0xA2, 0x30, 0x04, 0x98, 0x8C, 0x53,
  30273. 0x24, 0x02, 0x8A, 0x04, 0x01, 0x4C, 0x28, 0x71,
  30274. 0xC3, 0x86, 0x6C, 0x24, 0x49, 0x81, 0x04, 0x02,
  30275. 0x28, 0x62, 0x44, 0x32, 0x61, 0x20, 0x28, 0x01,
  30276. 0x04, 0x11, 0x0C, 0x09, 0x08, 0x90, 0x98, 0x84,
  30277. 0x63, 0xB2, 0x45, 0x63, 0x38, 0x2E, 0x04, 0xA4,
  30278. 0x0C, 0x18, 0x05, 0x4E, 0xCC, 0x86, 0x90, 0x43,
  30279. 0x40, 0x91, 0x54, 0x02, 0x21, 0x43, 0x28, 0x42,
  30280. 0x23, 0x94, 0x29, 0xC8, 0xA6, 0x91, 0x02, 0x09,
  30281. 0x80, 0xE3, 0x82, 0x00, 0xC1, 0x34, 0x08, 0xD1,
  30282. 0x34, 0x84, 0x12, 0x45, 0x8C, 0x02, 0xC6, 0x81,
  30283. 0x41, 0xC6, 0x6C, 0x1B, 0x12, 0x24, 0x04, 0x08,
  30284. 0x0D, 0x02, 0x00, 0x0C, 0x9C, 0xA2, 0x05, 0x49,
  30285. 0x34, 0x65, 0x00, 0x06, 0x89, 0x88, 0x34, 0x00,
  30286. 0xD8, 0x82, 0x29, 0x92, 0x12, 0x91, 0xE3, 0x36,
  30287. 0x86, 0xD1, 0x80, 0x71, 0x98, 0xB0, 0x50, 0x48,
  30288. 0xC6, 0x11, 0x14, 0x80, 0x0D, 0xA0, 0x12, 0x4D,
  30289. 0x9B, 0xB2, 0x40, 0x21, 0x41, 0x50, 0x4B, 0x36,
  30290. 0x05, 0x52, 0x10, 0x26, 0x19, 0xB2, 0x60, 0x92,
  30291. 0xA2, 0x24, 0xCB, 0x08, 0x00, 0x14, 0x22, 0x49,
  30292. 0x5A, 0xD0, 0x55, 0xBD, 0x2B, 0x45, 0xE4, 0x31,
  30293. 0x41, 0xA8, 0xC3, 0xA3, 0xAD, 0xBD, 0xB6, 0x37,
  30294. 0x92, 0x06, 0x95, 0x6B, 0x3D, 0xD8, 0xE5, 0x33,
  30295. 0x71, 0xB6, 0x62, 0xB7, 0x67, 0x6C, 0x77, 0x84,
  30296. 0x63, 0x2F, 0x41, 0x1D, 0xBA, 0x51, 0x27, 0xE1,
  30297. 0x24, 0x5D, 0xC2, 0x38, 0x71, 0x65, 0x9E, 0x8E,
  30298. 0xE4, 0xEB, 0xBB, 0x1D, 0x89, 0xEB, 0x18, 0xCA,
  30299. 0x0C, 0xA6, 0x86, 0xA3, 0x4D, 0x7C, 0x7A, 0x02,
  30300. 0xAC, 0xDD, 0x34, 0xCE, 0x05, 0x3B, 0x1B, 0x49,
  30301. 0xF4, 0x6D, 0x12, 0x33, 0xBC, 0x52, 0x70, 0x59,
  30302. 0xDF, 0xBC, 0x5D, 0x49, 0x42, 0x6A, 0xED, 0xC7,
  30303. 0xF1, 0x8C, 0xF5, 0x6D, 0x1F, 0xBC, 0xE4, 0xBD,
  30304. 0x45, 0x5D, 0x59, 0xF8, 0xCE, 0x9A, 0x39, 0xB5,
  30305. 0x96, 0x32, 0xFD, 0x93, 0x65, 0x8E, 0x92, 0xF1,
  30306. 0x8F, 0xB0, 0x99, 0xF3, 0x80, 0x0F, 0x66, 0x14,
  30307. 0xFE, 0xEB, 0x23, 0x17, 0x2D, 0x4C, 0x8F, 0x41,
  30308. 0x9A, 0x9B, 0xD1, 0x5B, 0x5B, 0xC0, 0x3D, 0xA6,
  30309. 0x0E, 0xF3, 0xE0, 0xA1, 0x04, 0xDC, 0x24, 0x18,
  30310. 0x9D, 0x90, 0xC6, 0x89, 0x5A, 0x7F, 0x10, 0x1E,
  30311. 0x4B, 0x21, 0xEC, 0x91, 0xD8, 0x5D, 0x65, 0xDB,
  30312. 0xCF, 0x90, 0x62, 0x85, 0xE9, 0x58, 0xA3, 0x47,
  30313. 0x92, 0x1C, 0xD0, 0x0C, 0xA3, 0xF3, 0x3E, 0x36,
  30314. 0xDB, 0x24, 0xA6, 0x98, 0xAB, 0xA7, 0x89, 0x2B,
  30315. 0x71, 0x6C, 0x4D, 0x00, 0xB0, 0xD5, 0xA0, 0xCA,
  30316. 0x1A, 0x76, 0x8E, 0x80, 0xB7, 0xAE, 0x83, 0x89,
  30317. 0x50, 0xF8, 0xA7, 0x52, 0x8B, 0x94, 0xD2, 0x2B,
  30318. 0x9F, 0x49, 0x92, 0x3D, 0x54, 0x0D, 0xB8, 0xD1,
  30319. 0x19, 0x49, 0xAC, 0x91, 0xAF, 0xDB, 0xE9, 0x24,
  30320. 0x4D, 0xD8, 0xE1, 0xD5, 0x16, 0x0E, 0xB1, 0x39,
  30321. 0x40, 0x7D, 0x5F, 0xF5, 0x92, 0xB4, 0xAF, 0xC3,
  30322. 0x76, 0x2B, 0xDB, 0x7D, 0x52, 0x97, 0x62, 0x9F,
  30323. 0xCF, 0x32, 0x19, 0x5F, 0xE6, 0x32, 0xFB, 0x8E,
  30324. 0x39, 0x24, 0xB4, 0xEB, 0xE9, 0x17, 0x9E, 0x47,
  30325. 0x69, 0x4D, 0x92, 0x82, 0x96, 0x88, 0x38, 0x11,
  30326. 0xCE, 0xD6, 0xBF, 0x18, 0xE3, 0x51, 0x40, 0x81,
  30327. 0x11, 0xA0, 0x74, 0xDA, 0x0D, 0x5E, 0xEC, 0xD8,
  30328. 0x5D, 0x33, 0x22, 0x1E, 0xB9, 0x5D, 0xBF, 0x79,
  30329. 0xB0, 0xA1, 0xEF, 0xD1, 0x2D, 0xA0, 0x5F, 0xA1,
  30330. 0xC7, 0x6E, 0xD5, 0x08, 0xB8, 0xD0, 0xC1, 0x95,
  30331. 0x51, 0x9B, 0x07, 0xC3, 0x4A, 0x0A, 0xB5, 0xA1,
  30332. 0x28, 0xFE, 0x95, 0x95, 0x0A, 0xCF, 0x83, 0xA8,
  30333. 0xEB, 0x8F, 0xFB, 0x18, 0xD5, 0xBD, 0x69, 0x50,
  30334. 0xF1, 0xDF, 0x06, 0xFA, 0x9A, 0x65, 0x47, 0xBB,
  30335. 0x56, 0xE9, 0xCB, 0x8F, 0x69, 0x5F, 0xE0, 0xAD,
  30336. 0x19, 0x3A, 0x70, 0xE5, 0x66, 0x42, 0xD7, 0x1C,
  30337. 0x0C, 0xB4, 0x03, 0x89, 0x7D, 0x47, 0x4D, 0x29,
  30338. 0x67, 0x8C, 0x41, 0x73, 0xAB, 0x7D, 0xFD, 0x69,
  30339. 0x15, 0xAD, 0xE3, 0xB7, 0xF8, 0x98, 0x3B, 0xCA,
  30340. 0x8F, 0x27, 0x37, 0x7B, 0x72, 0x2C, 0x5F, 0x23,
  30341. 0x73, 0x15, 0xE2, 0xB6, 0xBD, 0xDE, 0x84, 0xF8,
  30342. 0x7E, 0x22, 0xB9, 0xFD, 0xD3, 0x4D, 0x62, 0x80,
  30343. 0xBA, 0xC5, 0x57, 0x29, 0x30, 0x1B, 0x06, 0x4D,
  30344. 0x20, 0xB1, 0x53, 0x86, 0xCB, 0x6A, 0x4A, 0xE3,
  30345. 0xC1, 0xA9, 0x88, 0xCF, 0xEB, 0x15, 0x2F, 0xA8,
  30346. 0xA8, 0x6F, 0xFC, 0x2A, 0xA8, 0x0E, 0xD9, 0xFA,
  30347. 0xEA, 0xD7, 0x3B, 0xCE, 0xF8, 0x5B, 0xD8, 0x92,
  30348. 0x22, 0x6A, 0x1A, 0x8E, 0x5E, 0x91, 0x37, 0x2C,
  30349. 0x21, 0x05, 0xC4, 0xAC, 0xF7, 0x62, 0x83, 0xBA,
  30350. 0x55, 0xD5, 0x2C, 0xCE, 0xA1, 0x19, 0x93, 0x0E,
  30351. 0xDE, 0xB6, 0xB8, 0x78, 0x0F, 0xBF, 0x4C, 0xA4,
  30352. 0x66, 0xAD, 0x97, 0x2F, 0xEE, 0x34, 0xE9, 0xA2,
  30353. 0xB6, 0x1D, 0x3C, 0x60, 0xFB, 0xB8, 0x7F, 0xF8,
  30354. 0xFD, 0x34, 0x8C, 0xC5, 0xC7, 0x38, 0x72, 0x74,
  30355. 0x19, 0xA9, 0xCF, 0x54, 0x49, 0x5B, 0xBA, 0x70,
  30356. 0x12, 0xC1, 0x61, 0xDC, 0x32, 0x61, 0x49, 0x66,
  30357. 0xF3, 0x57, 0xAA, 0x0F, 0xE6, 0x44, 0x9E, 0x8A,
  30358. 0x19, 0x9C, 0x6B, 0x63, 0x2C, 0x14, 0x1E, 0xDD,
  30359. 0x00, 0x27, 0xE3, 0x95, 0xE3, 0xE7, 0xD9, 0xFF,
  30360. 0x30, 0x2D, 0x14, 0x19, 0x4F, 0x49, 0x20, 0x0B,
  30361. 0x58, 0x2A, 0x23, 0x1C, 0xE2, 0xAD, 0x6B, 0x9C,
  30362. 0x7B, 0xB6, 0x20, 0x63, 0x08, 0x24, 0x55, 0x04,
  30363. 0x58, 0x1F, 0x0E, 0xBE, 0x2A, 0x6F, 0x79, 0x90,
  30364. 0x9E, 0x15, 0x8F, 0x4B, 0xDB, 0xE2, 0xBE, 0xBC,
  30365. 0x28, 0xB1, 0xC8, 0xFE, 0x00, 0x6D, 0x71, 0xCC,
  30366. 0x91, 0x6A, 0xCC, 0xF8, 0x12, 0x8B, 0xEC, 0xF3,
  30367. 0x46, 0x53, 0xB1, 0x7F, 0xB3, 0x79, 0xF0, 0xC7,
  30368. 0xD7, 0xA5, 0xCF, 0x2C, 0xC3, 0x09, 0x66, 0x82,
  30369. 0x53, 0x43, 0xFD, 0xAC, 0xDE, 0xD5, 0x85, 0xB3,
  30370. 0x79, 0x74, 0x55, 0xE8, 0xF6, 0xE5, 0xFB, 0xF0,
  30371. 0x63, 0x0C, 0x36, 0x63, 0x65, 0x10, 0x43, 0xC9,
  30372. 0x60, 0x99, 0xD6, 0x0C, 0xB9, 0x66, 0x1C, 0xA9,
  30373. 0x97, 0x4D, 0xDB, 0xA8, 0x13, 0x9E, 0xAE, 0xCA,
  30374. 0x7A, 0x5F, 0xE3, 0x24, 0xA0, 0xEE, 0x8A, 0x9D,
  30375. 0x7F, 0x03, 0x53, 0x21, 0x6B, 0xAF, 0x3D, 0xF9,
  30376. 0x38, 0xF3, 0x7A, 0x1D, 0xDA, 0xE2, 0xEF, 0xBA,
  30377. 0x86, 0x21, 0x85, 0x1F, 0x36, 0x08, 0x0B, 0xDA,
  30378. 0x37, 0x5A, 0x0A, 0xD7, 0x55, 0x41, 0xD5, 0x84,
  30379. 0x1B, 0x36, 0xA2, 0x50, 0x65, 0xD7, 0xF3, 0xA3,
  30380. 0xEB, 0xE1, 0xDE, 0x0F, 0x85, 0xAA, 0xF6, 0x2F,
  30381. 0xAB, 0xBB, 0xC8, 0xF1, 0x2A, 0xD1, 0x0A, 0x9B,
  30382. 0xE4, 0x7B, 0xBC, 0x4D, 0x42, 0xD8, 0xA3, 0x4C,
  30383. 0x07, 0x6A, 0x60, 0x3E, 0xE2, 0xDA, 0xE7, 0x00,
  30384. 0xDF, 0x27, 0x94, 0xEF, 0x90, 0x99, 0x88, 0x2C,
  30385. 0xCF, 0xAA, 0xE1, 0x71, 0x2D, 0xFD, 0x00, 0x9C,
  30386. 0x55, 0xBF, 0xC4, 0x7A, 0x55, 0xE9, 0xE0, 0xB4,
  30387. 0x7F, 0x3D, 0xE9, 0xB0, 0x01, 0xA7, 0x27, 0x23,
  30388. 0x27, 0x58, 0x31, 0x0E, 0x8E, 0x80, 0xD8, 0xEB,
  30389. 0x64, 0xA0, 0xC3, 0xC9, 0xEA, 0x69, 0x9C, 0x74,
  30390. 0x5E, 0xAF, 0xD5, 0xEF, 0x5C, 0x4E, 0x40, 0x71,
  30391. 0xD6, 0x57, 0x77, 0xE2, 0xAF, 0x0E, 0x1D, 0xB8,
  30392. 0x5A, 0x91, 0x20, 0x4C, 0x33, 0x4D, 0xD8, 0x4F,
  30393. 0x98, 0xE0, 0x86, 0x1D, 0x02, 0xA0, 0xDA, 0x06,
  30394. 0x17, 0xC4, 0x5D, 0x2E, 0x49, 0x31, 0xE6, 0xE4,
  30395. 0xDC, 0x18, 0x23, 0x26, 0xF3, 0x61, 0xF5, 0x8D,
  30396. 0x26, 0x2C, 0x18, 0x4C, 0xDF, 0x71, 0x90, 0x24,
  30397. 0x96, 0xD3, 0xD4, 0x1A, 0x6F, 0x08, 0xAB, 0x29,
  30398. 0x7D, 0xFF, 0x4E, 0x27, 0x6D, 0x39, 0x83, 0x17,
  30399. 0x90, 0xA4, 0x07, 0x8A, 0xDE, 0x79, 0x53, 0xF6,
  30400. 0x99, 0x2E, 0xA6, 0x39, 0x47, 0xC3, 0xBE, 0x12,
  30401. 0xC7, 0xA5, 0x7E, 0xA2, 0x19, 0x57, 0x04, 0x45,
  30402. 0xBE, 0x44, 0x62, 0x92, 0xCA, 0x56, 0xE1, 0xF0,
  30403. 0x45, 0x3B, 0xA4, 0xF8, 0xF5, 0xCD, 0xC7, 0xD2,
  30404. 0xB2, 0x46, 0x57, 0x51, 0x0B, 0x06, 0xDA, 0x54,
  30405. 0x03, 0x9E, 0x52, 0xA2, 0x78, 0x69, 0x25, 0x2E,
  30406. 0x75, 0x83, 0x25, 0x3F, 0xA3, 0x62, 0x27, 0xB9,
  30407. 0xA6, 0x59, 0x7A, 0xB1, 0xB6, 0xE9, 0xC1, 0xDD,
  30408. 0x2F, 0x22, 0x2D, 0x3B, 0xA3, 0x22, 0xD6, 0x11,
  30409. 0x7B, 0x08, 0x27, 0x92, 0x83, 0x7A, 0x5D, 0x0D,
  30410. 0x6B, 0x9D, 0x5B, 0xEB, 0xE9, 0xC0, 0x88, 0xDE,
  30411. 0x44, 0x55, 0xBA, 0x69, 0xC1, 0x7A, 0x4D, 0xE6,
  30412. 0x35, 0x67, 0x6F, 0x99, 0x9B, 0x07, 0xD8, 0x04,
  30413. 0xAA, 0xEA, 0x7D, 0xFF, 0x8E, 0xB8, 0xAA, 0x4C,
  30414. 0x79, 0xE2, 0x88, 0xA8, 0x1D, 0xE8, 0xA6, 0x77,
  30415. 0xCA, 0x06, 0xC0, 0xDF, 0x0E, 0x2B, 0xCB, 0xFF,
  30416. 0x9F, 0x64, 0x67, 0x11, 0xF1, 0xB9, 0x38, 0x83,
  30417. 0x19, 0x05, 0x30, 0x9B, 0x01, 0x11, 0x55, 0x03,
  30418. 0xAD, 0x44, 0x7D, 0x3C, 0x07, 0xEF, 0x88, 0x19,
  30419. 0x92, 0xC0, 0xFE, 0xE1, 0xAB, 0xDB, 0x24, 0x18,
  30420. 0x17, 0xD0, 0x03, 0x5C, 0x91, 0xD4, 0xA6, 0x2A,
  30421. 0xF1, 0xE9, 0x72, 0x62, 0x58, 0x22, 0x7D, 0x55,
  30422. 0x15, 0xE2, 0xA1, 0x70, 0x14, 0x5E, 0x34, 0xB9,
  30423. 0x5A, 0xB7, 0x5D, 0x3F, 0xB8, 0xB5, 0x45, 0x44,
  30424. 0xD2, 0x50, 0xD1, 0xC6, 0x7E, 0xE7, 0x3D, 0xF4,
  30425. 0xD3, 0xEC, 0xFB, 0x97, 0x32, 0x11, 0x72, 0x51,
  30426. 0xB7, 0x4A, 0xC8, 0x38, 0x96, 0xFC, 0x6F, 0x69,
  30427. 0xC2, 0xD5, 0xD3, 0x28, 0xE9, 0x63, 0x14, 0x14,
  30428. 0xFE, 0xB1, 0xA4, 0x02, 0x80, 0x65, 0x73, 0xD3,
  30429. 0x57, 0x07, 0x95, 0x21, 0x40, 0x00, 0x77, 0xA7,
  30430. 0x6D, 0x44, 0x2B, 0x0D, 0x77, 0x07, 0x92, 0x64,
  30431. 0xD4, 0x3A, 0xE2, 0x7F, 0xF2, 0x1C, 0x14, 0x08,
  30432. 0x60, 0x74, 0x8F, 0xFC, 0x0B, 0xE8, 0xEC, 0xA9,
  30433. 0xB7, 0x97, 0xA7, 0x85, 0x8A, 0xEF, 0xD7, 0x7E,
  30434. 0xD5, 0x15, 0xF7, 0x45, 0x8D, 0x9C, 0xBF, 0x23,
  30435. 0xEB, 0x8C, 0x4D, 0xD2, 0x28, 0x7E, 0x0A, 0x61,
  30436. 0x2E, 0xBA, 0xBE, 0x89, 0x1D, 0x64, 0x45, 0x22,
  30437. 0x70, 0x9D, 0x48, 0xEB, 0x2F, 0x96, 0xF1, 0xA7,
  30438. 0xDE, 0xD3, 0x28, 0x4C, 0xC9, 0xFB, 0xF2, 0x9C,
  30439. 0x5B, 0xFC, 0xBE, 0xBE, 0xF4, 0x38, 0xC9, 0x43,
  30440. 0xC3, 0x66, 0x53, 0xA9, 0x06, 0xE5, 0x71, 0x16,
  30441. 0xA4, 0xBB, 0x3B, 0x50, 0x53, 0xCF, 0xF4, 0x1F,
  30442. 0xD6, 0x00, 0x07, 0x46, 0xFB, 0x97, 0x0B, 0xF9,
  30443. 0x3D, 0xF4, 0xC6, 0x60, 0xD0, 0x37, 0x70, 0xC0,
  30444. 0x2D, 0xD1, 0x9F, 0xA5, 0x78, 0xF3, 0x1F, 0x03,
  30445. 0x81, 0xB1, 0x93, 0xBA, 0xE5, 0x82, 0xE6, 0xD1,
  30446. 0x66, 0x93, 0x83, 0x5B, 0xB9, 0xAD, 0xD9, 0x01,
  30447. 0xA5, 0xB6, 0x5C, 0x69, 0x82, 0xD7, 0x2F, 0x35,
  30448. 0x35, 0x98, 0xEE, 0xE9, 0xA0, 0x74, 0xC1, 0x91,
  30449. 0x44, 0x0A, 0x04, 0xCD, 0x97, 0xBE, 0x6B, 0x60,
  30450. 0x90, 0x9A, 0x48, 0x7B, 0x83, 0xA2, 0x28, 0x97,
  30451. 0xB5, 0xBA, 0xB1, 0x4D, 0x35, 0x8B, 0x34, 0x0A,
  30452. 0xA1, 0xCB, 0xA5, 0xC2, 0xA4, 0x6A, 0x36, 0xB3,
  30453. 0x12, 0x46, 0x59, 0xDB, 0x63, 0xE5, 0xF9, 0xF1,
  30454. 0x7F, 0xAD, 0x42, 0xF4, 0x24, 0xF0, 0x02, 0x3D,
  30455. 0x1E, 0x6C, 0xD5, 0xB3, 0x06, 0x8F, 0x1F, 0x59,
  30456. 0x79, 0xCC, 0xF9, 0x5B, 0x4F, 0x8B, 0xD6, 0x03,
  30457. 0xC7, 0x53, 0xE6, 0xCE, 0xBB, 0xD8, 0x52, 0x89,
  30458. 0x70, 0x5D, 0x98, 0x86, 0xA5, 0x9E, 0x44, 0xA9,
  30459. 0xC8, 0x17, 0xA2, 0x6F, 0x43, 0x2D, 0x8D, 0xA7,
  30460. 0xDE, 0x3E, 0xFA, 0xE7, 0x98, 0x7B, 0xB5, 0xBE,
  30461. 0x7B, 0x10, 0xB8, 0xB8, 0xA5, 0x3D, 0x3E, 0xCD,
  30462. 0x94, 0x19, 0x5E, 0x06, 0x51, 0xB8, 0x58, 0x1E,
  30463. 0x0E, 0xCF, 0xFE, 0xE5, 0xED, 0x84, 0xB5, 0xF5,
  30464. 0x0F, 0x34, 0x32, 0xAC, 0x0A, 0x7F, 0x03, 0xF0,
  30465. 0xF8, 0xFC, 0x69, 0xA0, 0x26, 0x0D, 0x2E, 0xFA,
  30466. 0x62, 0x49, 0x5C, 0xC4, 0xE5, 0xF6, 0x8B, 0xC5,
  30467. 0x26, 0x21, 0x23, 0x3B, 0xBD, 0x9A, 0x23, 0x95,
  30468. 0x69, 0xA7, 0x48, 0x94, 0x30, 0x1E, 0xC3, 0x82,
  30469. 0xB6, 0x75, 0x30, 0xA6, 0xF3, 0x1E, 0xBB, 0xBC,
  30470. 0xF7, 0x21, 0x27, 0x12, 0x2C, 0x51, 0x50, 0x55,
  30471. 0x87, 0x0D, 0xF1, 0xCC, 0x6C, 0xFF, 0xEA, 0x7E,
  30472. 0x2C, 0xDA, 0x8B, 0x9B, 0x20, 0xF4, 0x75, 0xFB,
  30473. 0xC2, 0x3F, 0xBE, 0x09, 0xA6, 0xC9, 0x26, 0xE7,
  30474. 0xB5, 0xC7, 0xE6, 0xB9, 0x35, 0x8C, 0xAF, 0xFA,
  30475. 0xC0, 0x8D, 0x43, 0x33, 0x25, 0xBA, 0xAA, 0xDC,
  30476. 0xCF, 0xBC, 0xE4, 0xC4, 0xC6, 0x26, 0x4A, 0x0D,
  30477. 0x9D, 0xCC, 0x2A, 0xE0, 0x5B, 0x1E, 0xC9, 0x78,
  30478. 0xF8, 0xA2, 0xB5, 0x46, 0xE5, 0x49, 0xB8, 0x4C,
  30479. 0xC2, 0x22, 0x40, 0xCE, 0x97, 0x9A, 0x95, 0x40,
  30480. 0xF7, 0xD6, 0x52, 0x54, 0x3B, 0xBB, 0x42, 0xC5,
  30481. 0x6F, 0x00, 0x7F, 0x83, 0xDD, 0x88, 0x71, 0xF7,
  30482. 0xD4, 0x1B, 0x3D, 0x81, 0xC4, 0xB1, 0x49, 0x9B,
  30483. 0xF3, 0x68, 0x15, 0xC5, 0x15, 0x97, 0x0F, 0xC5,
  30484. 0x43, 0xDD, 0x07, 0xBE, 0x98, 0x43, 0x2C, 0xB3,
  30485. 0xEF, 0x08, 0xCA, 0xDC, 0x9C, 0x27, 0x58, 0xFE,
  30486. 0x49, 0xE9, 0x77, 0xD9, 0x1C, 0x62, 0xA4, 0xA2,
  30487. 0xF9, 0x78, 0xCC, 0xB3, 0x21, 0x06, 0x10, 0xDE,
  30488. 0x5A, 0x52, 0xA3, 0x67, 0xBD, 0x5E, 0xBC, 0x9B,
  30489. 0x4E, 0x40, 0x87, 0x93, 0xCF, 0x0E, 0x27, 0x0E,
  30490. 0xE3, 0x11, 0x4B, 0xB3, 0xE0, 0xCE, 0x24, 0xB6,
  30491. 0x0A, 0x53, 0x03, 0xF8, 0x01, 0x6A, 0x7E, 0xFE,
  30492. 0xC8, 0x66, 0x9F, 0x29, 0xF3, 0x45, 0x94, 0xD6,
  30493. 0x0E, 0x30, 0xB5, 0x61, 0xA9, 0xEC, 0x8F, 0x71,
  30494. 0xF7, 0x36, 0xD6, 0x43, 0x4B, 0x0C, 0xCD, 0x45,
  30495. 0xBB, 0xA4, 0xBD, 0xE9, 0xA9, 0xC3, 0xC1, 0x95,
  30496. 0x1E, 0xF9, 0x42, 0x07, 0x18, 0xEA, 0xF5, 0x0B,
  30497. 0x27, 0xB6, 0xDE, 0xEF, 0x67, 0x33, 0x83, 0x0D,
  30498. 0xD9, 0x5E, 0x3A, 0x93, 0xD2, 0xD0, 0xDB, 0xB9,
  30499. 0x98, 0xF0, 0x25, 0x21, 0xF3, 0xDF, 0x0B, 0x1E
  30500. };
  30501. #endif /* !WOLFSSL_NO_ML_DSA_44 */
  30502. #ifndef WOLFSSL_NO_ML_DSA_65
  30503. static const byte seed_65[] = {
  30504. 0x41, 0xAF, 0x98, 0x7B, 0x02, 0x6E, 0x47, 0x5F,
  30505. 0x37, 0x91, 0x7F, 0x2A, 0x6A, 0x9A, 0x87, 0xE7,
  30506. 0x51, 0xAD, 0xF9, 0x5B, 0x92, 0x7F, 0x2D, 0xCE,
  30507. 0xF0, 0xD4, 0xF3, 0xDA, 0x8F, 0x8C, 0x86, 0x6B
  30508. };
  30509. static const byte pk_65[] = {
  30510. 0xDC, 0x38, 0xE5, 0x5F, 0xDF, 0x2E, 0x9D, 0xD4,
  30511. 0x34, 0x5C, 0xAE, 0x1A, 0x7D, 0xF4, 0x2E, 0x2E,
  30512. 0xBC, 0x58, 0x57, 0x80, 0x55, 0x02, 0xE4, 0x3F,
  30513. 0xA5, 0x19, 0x41, 0xE4, 0x44, 0x58, 0x66, 0x41,
  30514. 0x39, 0x5D, 0xF9, 0x20, 0x6C, 0x36, 0x0D, 0x4F,
  30515. 0x83, 0x43, 0xBE, 0x86, 0xEF, 0x6C, 0x43, 0xD0,
  30516. 0x3E, 0xD0, 0x63, 0x0A, 0x5B, 0x92, 0x8D, 0x31,
  30517. 0x19, 0x1D, 0xA9, 0x51, 0x61, 0x48, 0xE6, 0x26,
  30518. 0x50, 0x07, 0x54, 0x9B, 0xB0, 0xB7, 0x62, 0x54,
  30519. 0xDB, 0x80, 0x4E, 0x48, 0x7F, 0x48, 0xC5, 0x11,
  30520. 0x91, 0xFC, 0xA9, 0x26, 0x25, 0x08, 0xA5, 0x99,
  30521. 0xA0, 0x3C, 0xB9, 0x0C, 0xCF, 0x6C, 0xCD, 0x83,
  30522. 0x9A, 0x38, 0x6D, 0x22, 0xDE, 0x0A, 0xC3, 0x8F,
  30523. 0xF7, 0xD0, 0x57, 0x40, 0x53, 0xE9, 0xE9, 0x4E,
  30524. 0x73, 0xFA, 0x58, 0x40, 0x9F, 0x6D, 0x8A, 0xD3,
  30525. 0x6F, 0x86, 0x84, 0x4D, 0x18, 0xD7, 0x4C, 0x76,
  30526. 0x39, 0x57, 0x9E, 0xC0, 0xC7, 0xE4, 0xEE, 0x54,
  30527. 0xF4, 0xAD, 0x10, 0xC5, 0x69, 0x59, 0xE0, 0xBC,
  30528. 0x9B, 0xF4, 0x20, 0x8F, 0xBA, 0x0A, 0x94, 0x10,
  30529. 0x55, 0x07, 0x7E, 0xD1, 0xF9, 0x20, 0xCC, 0x2F,
  30530. 0xA9, 0xAE, 0x9D, 0xF5, 0xE4, 0x29, 0x40, 0x7E,
  30531. 0x44, 0xA4, 0xDF, 0xB2, 0xE9, 0x25, 0xE0, 0xBA,
  30532. 0x8D, 0x6C, 0x33, 0x88, 0x9C, 0xEE, 0x27, 0xDB,
  30533. 0xC7, 0x0A, 0x6E, 0x5A, 0x08, 0x92, 0x9B, 0x53,
  30534. 0xF8, 0xFD, 0xF9, 0x5B, 0xEB, 0x03, 0x8E, 0x45,
  30535. 0xCB, 0x91, 0x19, 0x4E, 0x6B, 0x1E, 0xA0, 0xA4,
  30536. 0xF0, 0x43, 0xC9, 0x8F, 0xDF, 0x93, 0x5E, 0x86,
  30537. 0xB0, 0x09, 0xD3, 0x47, 0x38, 0x7C, 0x8E, 0x78,
  30538. 0x85, 0x71, 0x3D, 0x07, 0x2E, 0x2E, 0x12, 0x6F,
  30539. 0x06, 0x97, 0x0E, 0x54, 0xAD, 0x71, 0x09, 0xEF,
  30540. 0xA5, 0x55, 0x0A, 0x39, 0x86, 0xE6, 0x17, 0x17,
  30541. 0x70, 0x9A, 0xA7, 0xA7, 0x1B, 0xCE, 0x78, 0x06,
  30542. 0x2C, 0x61, 0x1A, 0xB9, 0x48, 0x22, 0x41, 0x45,
  30543. 0x15, 0xEB, 0x10, 0x3C, 0x6E, 0x24, 0x37, 0xA4,
  30544. 0xB5, 0xE8, 0x82, 0x4D, 0x6D, 0xCC, 0x44, 0xC6,
  30545. 0xB0, 0x5D, 0xBE, 0x46, 0xDA, 0x5F, 0x00, 0x36,
  30546. 0x5B, 0xBD, 0x87, 0x65, 0x3A, 0x96, 0x21, 0x58,
  30547. 0x45, 0x65, 0xDB, 0xD8, 0x77, 0x76, 0x7B, 0x25,
  30548. 0xC3, 0x78, 0x6E, 0xD9, 0x14, 0xA7, 0x19, 0x69,
  30549. 0x4F, 0xBB, 0x1B, 0xDB, 0x37, 0xCE, 0xAF, 0x8C,
  30550. 0x88, 0x2E, 0x9E, 0x30, 0xF6, 0xAE, 0x43, 0xCC,
  30551. 0x59, 0x0F, 0x67, 0x8A, 0xCB, 0x4F, 0x08, 0x20,
  30552. 0x6D, 0x99, 0xD7, 0xA9, 0xDE, 0xE5, 0xE5, 0xB3,
  30553. 0xFF, 0xAA, 0x45, 0x3C, 0xF1, 0xE3, 0x02, 0x7D,
  30554. 0x2F, 0xEE, 0x69, 0x04, 0x81, 0x73, 0x01, 0x37,
  30555. 0x51, 0x68, 0xC8, 0x0B, 0x51, 0xFD, 0x05, 0xB4,
  30556. 0x05, 0xBB, 0xA1, 0xDB, 0x1D, 0xF6, 0x5F, 0x70,
  30557. 0xD3, 0x0A, 0x37, 0x4B, 0x9C, 0xC4, 0x45, 0x30,
  30558. 0x11, 0x36, 0xE2, 0x48, 0x9F, 0xC4, 0x2E, 0x4E,
  30559. 0x0C, 0x0C, 0xA1, 0x04, 0x41, 0x75, 0x95, 0xAA,
  30560. 0xED, 0xAC, 0xD4, 0xB2, 0xE7, 0x85, 0x7E, 0xE1,
  30561. 0xA6, 0xFE, 0x2A, 0x09, 0x19, 0x09, 0x3D, 0x7C,
  30562. 0x20, 0x1E, 0x98, 0x3D, 0x6E, 0x02, 0xC1, 0xCA,
  30563. 0xBB, 0x24, 0x82, 0x9F, 0x45, 0x1D, 0x26, 0x99,
  30564. 0xAE, 0x02, 0x82, 0xF9, 0x86, 0x3B, 0x67, 0x8C,
  30565. 0xBD, 0xFE, 0xF1, 0xD0, 0xB6, 0xB8, 0xAB, 0x00,
  30566. 0x0F, 0xEC, 0x30, 0xDC, 0x27, 0x58, 0xE2, 0x29,
  30567. 0x18, 0x05, 0x5A, 0x66, 0xA5, 0x88, 0x39, 0x8E,
  30568. 0x49, 0x5B, 0xB9, 0x52, 0x43, 0x84, 0xDC, 0xA9,
  30569. 0x50, 0x2B, 0x83, 0x3C, 0x84, 0x81, 0x37, 0x52,
  30570. 0x30, 0x79, 0xBD, 0x04, 0xB8, 0xDD, 0x47, 0xC1,
  30571. 0x02, 0x2E, 0xEC, 0x24, 0xD0, 0x56, 0x23, 0xE1,
  30572. 0x92, 0xD0, 0x65, 0x7F, 0xC7, 0xC2, 0xF7, 0x60,
  30573. 0x73, 0xB8, 0xAF, 0x0A, 0xF4, 0xEF, 0xFC, 0x1B,
  30574. 0xC2, 0xB9, 0x76, 0x87, 0x8A, 0xA6, 0xC2, 0x3F,
  30575. 0xD3, 0x9F, 0x1F, 0x2D, 0x94, 0xBC, 0x89, 0x4E,
  30576. 0x31, 0x8D, 0x28, 0xD0, 0x90, 0xB5, 0x5B, 0x60,
  30577. 0x30, 0xC6, 0x0B, 0x37, 0x63, 0x5D, 0xDC, 0xC6,
  30578. 0xE0, 0x1A, 0xBA, 0x6B, 0x23, 0xCD, 0x2E, 0x09,
  30579. 0x2D, 0x6A, 0x7E, 0x0C, 0xD9, 0x4F, 0xB1, 0xE2,
  30580. 0x89, 0x67, 0xE7, 0xB1, 0x54, 0x08, 0xB2, 0xFA,
  30581. 0x83, 0x43, 0x7C, 0x77, 0x06, 0xED, 0xE2, 0x29,
  30582. 0x53, 0xB7, 0x09, 0xC4, 0x1B, 0x81, 0x55, 0x12,
  30583. 0x41, 0x8E, 0x8B, 0x03, 0x36, 0xEE, 0x45, 0x70,
  30584. 0x57, 0xA8, 0x73, 0xEF, 0x70, 0x7B, 0x1F, 0x63,
  30585. 0xB0, 0xE8, 0x00, 0xBD, 0x1E, 0xE6, 0xA9, 0x93,
  30586. 0x9D, 0x03, 0x19, 0x22, 0xDF, 0xE1, 0x01, 0xF2,
  30587. 0xA9, 0x6B, 0x90, 0x5C, 0xD2, 0xC1, 0xAC, 0x9F,
  30588. 0xB2, 0x21, 0x1C, 0x2D, 0xC6, 0x80, 0x9A, 0xB5,
  30589. 0x1E, 0x46, 0x95, 0x6C, 0xCE, 0x47, 0x3E, 0x67,
  30590. 0xCD, 0xD6, 0xC9, 0xB9, 0x81, 0x74, 0x7F, 0x17,
  30591. 0xA3, 0xF7, 0x48, 0x99, 0xF3, 0x36, 0x84, 0xF3,
  30592. 0x16, 0x41, 0x55, 0x5F, 0xA7, 0xBF, 0x4B, 0x69,
  30593. 0x8D, 0xA3, 0x3D, 0x1E, 0xEA, 0xF5, 0x1E, 0xC6,
  30594. 0xB8, 0x1C, 0xD6, 0x89, 0x45, 0x68, 0xFA, 0xE7,
  30595. 0xCA, 0x86, 0xE4, 0xB1, 0xC9, 0x9C, 0xB2, 0xAB,
  30596. 0x89, 0x03, 0xE7, 0x19, 0x7B, 0xA9, 0xF2, 0x6B,
  30597. 0x4A, 0x43, 0x1D, 0x90, 0xAF, 0xA4, 0xE3, 0xBC,
  30598. 0xEF, 0xD4, 0x37, 0xC5, 0x55, 0x5C, 0x9E, 0x14,
  30599. 0xC6, 0x18, 0xDD, 0x45, 0x3F, 0x80, 0x49, 0x1C,
  30600. 0x93, 0xFF, 0xBD, 0xDD, 0x75, 0x54, 0x0B, 0xD1,
  30601. 0xA9, 0xF6, 0xBC, 0x89, 0x98, 0x7D, 0x6F, 0x03,
  30602. 0x7B, 0x06, 0xD5, 0x40, 0x7D, 0x85, 0x48, 0x2E,
  30603. 0x11, 0x3E, 0xF0, 0x47, 0x77, 0xD0, 0xBA, 0x03,
  30604. 0x33, 0x58, 0xC4, 0x8F, 0x76, 0xF8, 0x72, 0x47,
  30605. 0x04, 0x21, 0x5E, 0x85, 0x5A, 0x0F, 0x35, 0x77,
  30606. 0xFB, 0x96, 0x29, 0x81, 0x2D, 0x55, 0x6E, 0x53,
  30607. 0xC6, 0x13, 0x1E, 0xFA, 0x4D, 0xCE, 0xA9, 0x36,
  30608. 0x1D, 0x8F, 0xAB, 0xAC, 0x13, 0x19, 0x94, 0xFC,
  30609. 0x4B, 0xCD, 0x36, 0x4C, 0x6E, 0x21, 0xAE, 0xF1,
  30610. 0x13, 0xA4, 0xF7, 0x64, 0x8E, 0xE1, 0xAF, 0x50,
  30611. 0x6A, 0x63, 0x0E, 0xCA, 0x2F, 0xE9, 0x0C, 0x8A,
  30612. 0xE7, 0xF2, 0xE3, 0x68, 0x03, 0xE0, 0x40, 0x1C,
  30613. 0x64, 0xAB, 0xC3, 0xEC, 0xC0, 0x92, 0xE9, 0x57,
  30614. 0x3E, 0x66, 0x72, 0x36, 0x39, 0x22, 0x4E, 0xCD,
  30615. 0x13, 0x08, 0xBA, 0xF8, 0x2B, 0xA1, 0xF2, 0x69,
  30616. 0x44, 0x7E, 0x90, 0x5C, 0xC8, 0xEC, 0xB6, 0xBE,
  30617. 0x8C, 0x30, 0xE0, 0x69, 0xB7, 0x97, 0xA1, 0x1C,
  30618. 0x18, 0xE5, 0x54, 0x62, 0xC3, 0x29, 0x99, 0x21,
  30619. 0x16, 0xD9, 0x78, 0x1C, 0x4C, 0x9C, 0x88, 0x4C,
  30620. 0xA5, 0xE1, 0x11, 0x66, 0x5B, 0x6E, 0x71, 0xE7,
  30621. 0xE2, 0xE7, 0xE4, 0x02, 0xDD, 0x1A, 0x8D, 0x0C,
  30622. 0xF5, 0x32, 0xFD, 0x41, 0x28, 0x35, 0x75, 0xD0,
  30623. 0x0C, 0x5F, 0x06, 0x6A, 0x5A, 0x61, 0x49, 0x59,
  30624. 0xC1, 0x0C, 0xD4, 0x9E, 0xD6, 0x29, 0xE2, 0x37,
  30625. 0xDF, 0x2B, 0x3D, 0xE8, 0x98, 0xB9, 0xDF, 0x8E,
  30626. 0xA0, 0xC4, 0xE2, 0xFC, 0x45, 0x70, 0xE8, 0x1B,
  30627. 0xF4, 0xFA, 0xC5, 0xE6, 0xA7, 0xCF, 0x4F, 0xA2,
  30628. 0xDA, 0x3D, 0x90, 0x49, 0x24, 0x8F, 0x61, 0x54,
  30629. 0xD5, 0x50, 0x8E, 0xE8, 0x0C, 0x14, 0xAD, 0x6F,
  30630. 0x65, 0x88, 0x3A, 0xF6, 0x92, 0xDB, 0x35, 0x5D,
  30631. 0xFF, 0x21, 0x20, 0xAC, 0x01, 0x16, 0x0B, 0xEC,
  30632. 0x84, 0x15, 0x3B, 0xA9, 0x93, 0x92, 0x75, 0xB3,
  30633. 0x73, 0xF1, 0x23, 0x69, 0x94, 0x10, 0xF5, 0xFE,
  30634. 0x20, 0xA8, 0xAF, 0x05, 0x87, 0x49, 0x4E, 0x9C,
  30635. 0xEB, 0x21, 0x0A, 0xCF, 0x0B, 0xA1, 0x65, 0x38,
  30636. 0xA6, 0x18, 0x4D, 0xF7, 0xD8, 0xC1, 0x2C, 0x14,
  30637. 0x4C, 0xD9, 0x40, 0xC2, 0xF7, 0xBF, 0xE3, 0x07,
  30638. 0x79, 0x55, 0xAE, 0xB9, 0xB6, 0x50, 0x06, 0x92,
  30639. 0x94, 0x8C, 0x6A, 0x0E, 0x22, 0x14, 0xE2, 0xCC,
  30640. 0x65, 0xBA, 0x0C, 0x4D, 0xB6, 0x5C, 0x4A, 0xE9,
  30641. 0x0A, 0x08, 0x0C, 0xF9, 0x26, 0xA2, 0x51, 0x85,
  30642. 0x36, 0xE2, 0xC1, 0xF1, 0x0A, 0x66, 0x51, 0x66,
  30643. 0x7A, 0x98, 0x9B, 0x2C, 0x30, 0x1A, 0x0D, 0x49,
  30644. 0x3C, 0x1E, 0xEC, 0x63, 0x53, 0x5E, 0xD9, 0xDD,
  30645. 0x84, 0x69, 0xCD, 0x7E, 0x79, 0x58, 0x3D, 0x6E,
  30646. 0xD9, 0x98, 0x58, 0xD8, 0x0A, 0x48, 0xB5, 0x13,
  30647. 0x3F, 0x72, 0x4C, 0x11, 0x90, 0x15, 0x12, 0x74,
  30648. 0xFF, 0x5C, 0x0D, 0xC6, 0x20, 0x8C, 0xC1, 0x99,
  30649. 0xCA, 0x8E, 0xFC, 0xA2, 0xE8, 0xB8, 0xEE, 0xAA,
  30650. 0x27, 0xC2, 0x97, 0x8D, 0xFA, 0xBE, 0xE0, 0x43,
  30651. 0x99, 0xB6, 0x90, 0x60, 0x00, 0x7C, 0x33, 0xD4,
  30652. 0x87, 0x71, 0x7B, 0x56, 0x6C, 0xAA, 0xE0, 0xAC,
  30653. 0x9D, 0x7E, 0x7E, 0xA3, 0xCF, 0xBB, 0xB3, 0xA0,
  30654. 0x5F, 0xD4, 0xC4, 0x3A, 0xA7, 0xB9, 0x0C, 0xCE,
  30655. 0xF3, 0x05, 0x09, 0x91, 0xA7, 0xE9, 0x11, 0x55,
  30656. 0x32, 0x45, 0xA6, 0x08, 0x0E, 0x10, 0x37, 0x91,
  30657. 0xF3, 0xBF, 0xED, 0x64, 0x26, 0xEB, 0x39, 0xC2,
  30658. 0x57, 0xAE, 0x64, 0x79, 0x33, 0x7C, 0x51, 0xB2,
  30659. 0xC8, 0x85, 0xE0, 0xF9, 0x6D, 0x10, 0x52, 0x9F,
  30660. 0x72, 0xF4, 0xD1, 0x5B, 0x54, 0x5B, 0x93, 0x28,
  30661. 0x36, 0xA8, 0xCD, 0xB3, 0x30, 0x5B, 0x7A, 0xB0,
  30662. 0xB6, 0xF0, 0xD8, 0xA0, 0xBA, 0x24, 0x59, 0x5F,
  30663. 0x43, 0x02, 0x01, 0x57, 0x91, 0x7B, 0x94, 0x07,
  30664. 0x63, 0x23, 0x12, 0x94, 0xFB, 0x9F, 0xF2, 0xC1,
  30665. 0xD6, 0x80, 0x8F, 0x4E, 0xA7, 0x9E, 0x11, 0xD8,
  30666. 0xB3, 0x08, 0xB6, 0x3B, 0x3B, 0xF2, 0xEE, 0x14,
  30667. 0xA5, 0xDB, 0xB0, 0xBB, 0x17, 0xA5, 0x96, 0x3C,
  30668. 0x2F, 0xB9, 0xE7, 0x4A, 0xD7, 0x52, 0x34, 0x98,
  30669. 0xCB, 0x0C, 0xEB, 0x42, 0x5B, 0x2D, 0x2D, 0x2B,
  30670. 0x0D, 0x94, 0x66, 0xD3, 0xAD, 0x08, 0x0A, 0x28,
  30671. 0xF6, 0x0E, 0xDA, 0xD4, 0x54, 0xFD, 0xC6, 0x48,
  30672. 0x08, 0xA1, 0x8D, 0xB0, 0x30, 0xFD, 0x18, 0xB1,
  30673. 0x50, 0xB1, 0xFD, 0xE0, 0x6E, 0x33, 0x25, 0x0D,
  30674. 0x90, 0xB1, 0xC1, 0xE7, 0x88, 0x74, 0x87, 0x05,
  30675. 0xE7, 0xBE, 0xBD, 0xAA, 0x8C, 0x6D, 0xC2, 0x3D,
  30676. 0x6F, 0x95, 0x84, 0xFA, 0x03, 0x74, 0x85, 0xE1,
  30677. 0xED, 0xE5, 0xF4, 0xE8, 0x26, 0x4A, 0x0B, 0x20,
  30678. 0x87, 0xB6, 0xE1, 0x10, 0x75, 0x6D, 0x9F, 0x95,
  30679. 0x39, 0x4C, 0x0F, 0x50, 0x1B, 0xA8, 0x69, 0x82,
  30680. 0xBB, 0xE2, 0xD6, 0x11, 0xD7, 0xBE, 0xFB, 0x4F,
  30681. 0x60, 0xD3, 0x16, 0xC6, 0x04, 0x3A, 0x5A, 0xF5,
  30682. 0x78, 0x9B, 0x0B, 0x21, 0xA1, 0x00, 0x96, 0xCD,
  30683. 0x63, 0x78, 0x1D, 0x2D, 0x4F, 0x6E, 0x50, 0xEE,
  30684. 0x62, 0x2D, 0x88, 0x62, 0x01, 0xF6, 0xB4, 0x17,
  30685. 0x4F, 0x8C, 0xAD, 0xCB, 0x4B, 0xF9, 0xF6, 0x9D,
  30686. 0xC7, 0xD8, 0xCC, 0xBF, 0x96, 0x1B, 0x1B, 0x79,
  30687. 0xF3, 0x25, 0x85, 0x23, 0x10, 0x63, 0x30, 0x8D,
  30688. 0xA8, 0x3A, 0x4B, 0x92, 0x1B, 0x88, 0x53, 0x24,
  30689. 0x2D, 0x29, 0xA5, 0x2E, 0x7A, 0xD5, 0x58, 0xEB,
  30690. 0x1B, 0x1C, 0xE6, 0xB8, 0x94, 0x0C, 0x58, 0x96,
  30691. 0x5B, 0xA0, 0x2C, 0xBF, 0xE2, 0x99, 0xA0, 0x1F,
  30692. 0x0C, 0xCC, 0xBD, 0x83, 0x72, 0x56, 0xBB, 0x13,
  30693. 0x61, 0x5A, 0xC2, 0x04, 0x27, 0x29, 0x1F, 0xD4,
  30694. 0xE4, 0x3D, 0x8A, 0x87, 0xE3, 0x81, 0x91, 0x07,
  30695. 0xD3, 0x9B, 0xBC, 0xA9, 0xB3, 0xBA, 0xF5, 0x8B,
  30696. 0x6A, 0xAD, 0xDE, 0xB0, 0x54, 0x3E, 0xFE, 0xCC,
  30697. 0xD3, 0xCB, 0x2C, 0x69, 0xF0, 0x58, 0xD7, 0xEF,
  30698. 0xA9, 0xC0, 0x15, 0x9B, 0x5A, 0xDF, 0x71, 0x25,
  30699. 0x38, 0x44, 0xEC, 0xA9, 0x18, 0x47, 0x41, 0xCE,
  30700. 0x3D, 0x53, 0x10, 0x12, 0xC3, 0x1B, 0x59, 0x9A,
  30701. 0x93, 0xA1, 0xEA, 0xBE, 0x3E, 0xBA, 0x74, 0xF6,
  30702. 0x2D, 0x40, 0x9D, 0xCB, 0x9E, 0xA1, 0xA5, 0x85,
  30703. 0xFF, 0xDC, 0xC5, 0x60, 0x6F, 0x61, 0xE8, 0x17,
  30704. 0x6C, 0x36, 0x9F, 0x7A, 0x48, 0x47, 0xDD, 0xF1,
  30705. 0xF4, 0x43, 0x21, 0xCB, 0xB3, 0x55, 0x86, 0xD0,
  30706. 0xE9, 0x46, 0x7D, 0xB5, 0x3D, 0x90, 0x34, 0x1E,
  30707. 0xBB, 0x40, 0xD3, 0x2A, 0xEB, 0xE6, 0x4C, 0x46,
  30708. 0x42, 0xA2, 0x8A, 0xBF, 0x90, 0xE7, 0x4B, 0x6D,
  30709. 0x5C, 0x94, 0x97, 0xD2, 0xF0, 0x97, 0x74, 0x4C,
  30710. 0x76, 0x03, 0xAC, 0x3D, 0xDE, 0x15, 0x96, 0x0C,
  30711. 0xEF, 0x18, 0x9D, 0xBD, 0x1A, 0x20, 0x35, 0x7E,
  30712. 0x2A, 0x70, 0x9D, 0xEA, 0x2E, 0x11, 0xDF, 0xF3,
  30713. 0x2F, 0xFE, 0x23, 0xA9, 0xB6, 0xCF, 0xB7, 0xB9,
  30714. 0x3F, 0x4F, 0x30, 0x6B, 0x3B, 0x0D, 0x3B, 0xED,
  30715. 0xCD, 0x77, 0xD4, 0xBF, 0xEE, 0xDD, 0xB6, 0x56,
  30716. 0x24, 0xD4, 0x29, 0x83, 0xDE, 0xDB, 0xC1, 0xFB,
  30717. 0x6A, 0xCE, 0x7F, 0x47, 0xD2, 0xC5, 0xF1, 0x78,
  30718. 0x5C, 0x2C, 0x5A, 0x28, 0x3E, 0x05, 0x50, 0x2E,
  30719. 0xD9, 0xAE, 0x9B, 0x95, 0x64, 0xC7, 0xD2, 0x7B,
  30720. 0xCB, 0xC5, 0x91, 0x80, 0xEB, 0x79, 0xC7, 0xCC,
  30721. 0xA8, 0x06, 0xC8, 0xF9, 0xDF, 0x2A, 0x49, 0x4A,
  30722. 0xF8, 0xFE, 0xBA, 0xA5, 0x85, 0x67, 0x1B, 0xDA,
  30723. 0x51, 0x3B, 0xC2, 0x04, 0xA6, 0xA3, 0xFF, 0x99,
  30724. 0x21, 0xE8, 0x17, 0x91, 0x33, 0x9B, 0x83, 0x75,
  30725. 0x20, 0x5E, 0x95, 0xBE, 0x49, 0xDF, 0x53, 0xFC,
  30726. 0x05, 0xA2, 0x3C, 0xAA, 0x5A, 0x22, 0x15, 0xA5,
  30727. 0x56, 0xE0, 0x51, 0x30, 0x4E, 0x32, 0x14, 0xF2,
  30728. 0x9F, 0x03, 0x51, 0x8E, 0xDD, 0x8B, 0x39, 0x19,
  30729. 0x1E, 0x39, 0xC5, 0xA7, 0x1C, 0xC6, 0xA4, 0xE1,
  30730. 0x77, 0xCA, 0x8C, 0x9D, 0x27, 0xBC, 0xCC, 0x16,
  30731. 0xD6, 0xFC, 0x59, 0x10, 0x23, 0xFF, 0x64, 0x90,
  30732. 0x9C, 0x23, 0x5A, 0xFF, 0x7E, 0x27, 0x1B, 0xC7,
  30733. 0x7F, 0x21, 0x3B, 0x41, 0xDB, 0xBC, 0x96, 0x60,
  30734. 0x0B, 0x35, 0xA1, 0xF3, 0xF8, 0x51, 0x0A, 0x65,
  30735. 0xCF, 0xDF, 0x7A, 0xB8, 0x04, 0x56, 0x49, 0xD7,
  30736. 0xD3, 0xC5, 0x0B, 0x4A, 0x1F, 0x60, 0xE1, 0x86,
  30737. 0x36, 0x53, 0x8E, 0x6C, 0x3E, 0xAF, 0x5B, 0xC1,
  30738. 0xCA, 0xCB, 0x22, 0x1A, 0x07, 0xDA, 0x54, 0xEC,
  30739. 0xAA, 0x06, 0x72, 0x17, 0xCF, 0x80, 0xC4, 0x89,
  30740. 0x56, 0x24, 0x1B, 0xD4, 0xFF, 0x50, 0x6B, 0x51,
  30741. 0x55, 0x4D, 0x6E, 0x79, 0x7E, 0xEC, 0x61, 0xC6,
  30742. 0xE4, 0x21, 0xC8, 0x0E, 0x10, 0x3F, 0x8C, 0x85,
  30743. 0x3A, 0x27, 0xEA, 0x91, 0x07, 0xCB, 0x37, 0x18,
  30744. 0x14, 0xB5, 0x63, 0x6E, 0x00, 0xBC, 0x0F, 0x36,
  30745. 0xF9, 0x54, 0x75, 0xE7, 0x0B, 0xDC, 0xE7, 0xA0,
  30746. 0x59, 0xF0, 0x64, 0xFB, 0x73, 0x07, 0x0E, 0xFE,
  30747. 0x57, 0x7F, 0x0D, 0x12, 0xBC, 0xB0, 0xBF, 0xA2,
  30748. 0x3A, 0x18, 0x08, 0x7E, 0xD5, 0x6C, 0xF0, 0x6F,
  30749. 0xF8, 0x98, 0xFB, 0xA5, 0x10, 0x7B, 0x10, 0x5F,
  30750. 0x6B, 0xC8, 0x6D, 0xDE, 0x2F, 0x1F, 0xE0, 0xC8,
  30751. 0x19, 0xEE, 0xC2, 0x03, 0x39, 0x49, 0x70, 0x3E,
  30752. 0x36, 0xE3, 0x3C, 0x70, 0xE3, 0xEA, 0xAC, 0x34,
  30753. 0x32, 0xB7, 0x0D, 0xBA, 0x7C, 0xAB, 0xE6, 0x18
  30754. };
  30755. static const byte sk_65[] = {
  30756. 0xDC, 0x38, 0xE5, 0x5F, 0xDF, 0x2E, 0x9D, 0xD4,
  30757. 0x34, 0x5C, 0xAE, 0x1A, 0x7D, 0xF4, 0x2E, 0x2E,
  30758. 0xBC, 0x58, 0x57, 0x80, 0x55, 0x02, 0xE4, 0x3F,
  30759. 0xA5, 0x19, 0x41, 0xE4, 0x44, 0x58, 0x66, 0x41,
  30760. 0x52, 0x8D, 0xA0, 0xC7, 0xD2, 0x80, 0xDD, 0x49,
  30761. 0x0D, 0x5E, 0xB7, 0x65, 0xDB, 0x32, 0x33, 0x15,
  30762. 0x0F, 0x9E, 0xC8, 0xEB, 0xC9, 0x6E, 0xE8, 0xE8,
  30763. 0x5C, 0xBD, 0x18, 0x4F, 0xDC, 0xF8, 0xA8, 0xD9,
  30764. 0xC5, 0x33, 0x84, 0x79, 0x5A, 0x5E, 0xB7, 0x3C,
  30765. 0x6D, 0x82, 0xCA, 0xB9, 0xBA, 0x94, 0xB6, 0x46,
  30766. 0xAE, 0x3A, 0xD9, 0x19, 0x6C, 0xB4, 0xDA, 0xE2,
  30767. 0xF1, 0x4B, 0xB6, 0x43, 0xF0, 0x24, 0x08, 0xE5,
  30768. 0xF7, 0x9A, 0x41, 0xF1, 0x15, 0x9C, 0xA8, 0x08,
  30769. 0x79, 0x9F, 0xB8, 0x26, 0xD4, 0x08, 0x32, 0x47,
  30770. 0xC8, 0xF0, 0xD5, 0x31, 0xA1, 0xC1, 0x19, 0x04,
  30771. 0x02, 0x06, 0x2B, 0x4D, 0x46, 0xAE, 0x43, 0x6A,
  30772. 0x25, 0x82, 0x75, 0x41, 0x70, 0x36, 0x42, 0x48,
  30773. 0x78, 0x06, 0x36, 0x50, 0x23, 0x84, 0x68, 0x10,
  30774. 0x87, 0x08, 0x62, 0x00, 0x08, 0x34, 0x20, 0x73,
  30775. 0x32, 0x13, 0x36, 0x61, 0x87, 0x61, 0x43, 0x50,
  30776. 0x30, 0x02, 0x26, 0x07, 0x65, 0x45, 0x32, 0x00,
  30777. 0x25, 0x75, 0x01, 0x04, 0x88, 0x81, 0x58, 0x64,
  30778. 0x52, 0x40, 0x84, 0x22, 0x88, 0x42, 0x82, 0x56,
  30779. 0x47, 0x50, 0x05, 0x21, 0x88, 0x25, 0x32, 0x25,
  30780. 0x12, 0x85, 0x14, 0x52, 0x87, 0x77, 0x67, 0x18,
  30781. 0x46, 0x54, 0x63, 0x07, 0x88, 0x67, 0x37, 0x26,
  30782. 0x72, 0x62, 0x41, 0x02, 0x00, 0x01, 0x17, 0x84,
  30783. 0x33, 0x64, 0x32, 0x57, 0x06, 0x20, 0x05, 0x44,
  30784. 0x88, 0x57, 0x33, 0x45, 0x70, 0x55, 0x14, 0x43,
  30785. 0x12, 0x54, 0x04, 0x38, 0x37, 0x08, 0x42, 0x57,
  30786. 0x36, 0x05, 0x30, 0x03, 0x86, 0x53, 0x02, 0x53,
  30787. 0x75, 0x22, 0x62, 0x13, 0x38, 0x82, 0x48, 0x30,
  30788. 0x83, 0x83, 0x64, 0x83, 0x13, 0x74, 0x57, 0x32,
  30789. 0x46, 0x70, 0x06, 0x05, 0x82, 0x52, 0x73, 0x55,
  30790. 0x25, 0x77, 0x21, 0x78, 0x57, 0x83, 0x66, 0x20,
  30791. 0x38, 0x53, 0x21, 0x41, 0x77, 0x56, 0x77, 0x46,
  30792. 0x34, 0x42, 0x58, 0x31, 0x08, 0x06, 0x03, 0x62,
  30793. 0x20, 0x35, 0x11, 0x42, 0x35, 0x38, 0x63, 0x86,
  30794. 0x64, 0x13, 0x13, 0x75, 0x40, 0x01, 0x53, 0x74,
  30795. 0x41, 0x31, 0x56, 0x64, 0x38, 0x17, 0x14, 0x16,
  30796. 0x62, 0x33, 0x22, 0x12, 0x64, 0x40, 0x67, 0x11,
  30797. 0x62, 0x42, 0x25, 0x60, 0x38, 0x05, 0x83, 0x13,
  30798. 0x51, 0x00, 0x28, 0x36, 0x62, 0x56, 0x41, 0x43,
  30799. 0x58, 0x37, 0x51, 0x22, 0x70, 0x25, 0x82, 0x82,
  30800. 0x35, 0x24, 0x06, 0x83, 0x48, 0x58, 0x81, 0x78,
  30801. 0x07, 0x86, 0x23, 0x15, 0x75, 0x32, 0x46, 0x75,
  30802. 0x35, 0x40, 0x08, 0x43, 0x10, 0x66, 0x74, 0x05,
  30803. 0x13, 0x72, 0x74, 0x08, 0x83, 0x41, 0x81, 0x08,
  30804. 0x75, 0x87, 0x83, 0x28, 0x56, 0x66, 0x20, 0x01,
  30805. 0x18, 0x83, 0x57, 0x22, 0x14, 0x64, 0x18, 0x05,
  30806. 0x27, 0x75, 0x22, 0x84, 0x12, 0x38, 0x87, 0x52,
  30807. 0x32, 0x25, 0x28, 0x08, 0x14, 0x41, 0x81, 0x14,
  30808. 0x03, 0x24, 0x54, 0x23, 0x04, 0x81, 0x40, 0x36,
  30809. 0x38, 0x38, 0x64, 0x42, 0x46, 0x36, 0x68, 0x11,
  30810. 0x55, 0x00, 0x11, 0x25, 0x76, 0x16, 0x43, 0x07,
  30811. 0x23, 0x03, 0x34, 0x10, 0x46, 0x41, 0x14, 0x02,
  30812. 0x26, 0x10, 0x74, 0x38, 0x38, 0x72, 0x07, 0x87,
  30813. 0x54, 0x11, 0x12, 0x83, 0x75, 0x05, 0x82, 0x17,
  30814. 0x45, 0x20, 0x38, 0x41, 0x37, 0x20, 0x00, 0x08,
  30815. 0x32, 0x18, 0x16, 0x25, 0x58, 0x85, 0x16, 0x88,
  30816. 0x71, 0x82, 0x45, 0x60, 0x33, 0x11, 0x13, 0x42,
  30817. 0x43, 0x37, 0x68, 0x11, 0x16, 0x54, 0x04, 0x08,
  30818. 0x52, 0x78, 0x13, 0x56, 0x83, 0x52, 0x15, 0x24,
  30819. 0x03, 0x61, 0x78, 0x44, 0x13, 0x70, 0x67, 0x36,
  30820. 0x74, 0x86, 0x52, 0x50, 0x15, 0x41, 0x88, 0x74,
  30821. 0x53, 0x00, 0x05, 0x18, 0x65, 0x62, 0x14, 0x84,
  30822. 0x12, 0x32, 0x01, 0x88, 0x40, 0x42, 0x34, 0x05,
  30823. 0x32, 0x80, 0x72, 0x55, 0x20, 0x68, 0x16, 0x43,
  30824. 0x14, 0x15, 0x15, 0x38, 0x43, 0x85, 0x27, 0x60,
  30825. 0x70, 0x18, 0x27, 0x35, 0x53, 0x01, 0x28, 0x73,
  30826. 0x27, 0x84, 0x10, 0x53, 0x67, 0x10, 0x45, 0x40,
  30827. 0x81, 0x52, 0x86, 0x06, 0x11, 0x18, 0x04, 0x31,
  30828. 0x57, 0x25, 0x22, 0x44, 0x47, 0x81, 0x45, 0x44,
  30829. 0x55, 0x04, 0x72, 0x57, 0x06, 0x46, 0x76, 0x23,
  30830. 0x38, 0x85, 0x65, 0x30, 0x08, 0x48, 0x20, 0x13,
  30831. 0x22, 0x77, 0x44, 0x60, 0x43, 0x14, 0x15, 0x27,
  30832. 0x86, 0x22, 0x37, 0x37, 0x27, 0x04, 0x27, 0x50,
  30833. 0x74, 0x31, 0x10, 0x82, 0x00, 0x75, 0x80, 0x44,
  30834. 0x38, 0x10, 0x58, 0x40, 0x86, 0x60, 0x63, 0x13,
  30835. 0x65, 0x18, 0x33, 0x70, 0x57, 0x68, 0x05, 0x10,
  30836. 0x81, 0x03, 0x42, 0x05, 0x25, 0x65, 0x33, 0x57,
  30837. 0x38, 0x05, 0x65, 0x34, 0x46, 0x53, 0x68, 0x11,
  30838. 0x75, 0x10, 0x04, 0x54, 0x18, 0x47, 0x52, 0x24,
  30839. 0x63, 0x23, 0x74, 0x45, 0x11, 0x34, 0x68, 0x32,
  30840. 0x35, 0x38, 0x52, 0x85, 0x28, 0x08, 0x71, 0x78,
  30841. 0x37, 0x38, 0x27, 0x10, 0x80, 0x54, 0x26, 0x33,
  30842. 0x31, 0x82, 0x44, 0x88, 0x33, 0x24, 0x62, 0x86,
  30843. 0x32, 0x82, 0x73, 0x31, 0x28, 0x14, 0x73, 0x87,
  30844. 0x06, 0x35, 0x80, 0x36, 0x67, 0x02, 0x33, 0x75,
  30845. 0x27, 0x36, 0x38, 0x16, 0x35, 0x70, 0x52, 0x16,
  30846. 0x87, 0x58, 0x85, 0x17, 0x22, 0x13, 0x54, 0x85,
  30847. 0x07, 0x53, 0x31, 0x26, 0x78, 0x01, 0x85, 0x18,
  30848. 0x08, 0x68, 0x38, 0x52, 0x11, 0x73, 0x32, 0x25,
  30849. 0x58, 0x82, 0x70, 0x70, 0x36, 0x30, 0x50, 0x38,
  30850. 0x65, 0x12, 0x78, 0x31, 0x77, 0x72, 0x18, 0x41,
  30851. 0x05, 0x42, 0x32, 0x26, 0x26, 0x50, 0x52, 0x86,
  30852. 0x15, 0x76, 0x28, 0x66, 0x88, 0x03, 0x78, 0x28,
  30853. 0x70, 0x33, 0x36, 0x27, 0x16, 0x61, 0x43, 0x56,
  30854. 0x62, 0x81, 0x85, 0x75, 0x47, 0x60, 0x63, 0x38,
  30855. 0x66, 0x81, 0x51, 0x78, 0x03, 0x42, 0x60, 0x38,
  30856. 0x01, 0x24, 0x73, 0x63, 0x81, 0x12, 0x01, 0x27,
  30857. 0x63, 0x13, 0x11, 0x78, 0x36, 0x37, 0x15, 0x03,
  30858. 0x84, 0x58, 0x17, 0x25, 0x67, 0x87, 0x57, 0x83,
  30859. 0x71, 0x85, 0x37, 0x53, 0x86, 0x22, 0x33, 0x28,
  30860. 0x77, 0x30, 0x18, 0x15, 0x01, 0x37, 0x85, 0x40,
  30861. 0x15, 0x38, 0x51, 0x33, 0x17, 0x42, 0x64, 0x04,
  30862. 0x56, 0x27, 0x50, 0x45, 0x11, 0x27, 0x20, 0x17,
  30863. 0x76, 0x55, 0x33, 0x37, 0x58, 0x88, 0x88, 0x45,
  30864. 0x16, 0x55, 0x08, 0x53, 0x52, 0x48, 0x72, 0x85,
  30865. 0x30, 0x15, 0x23, 0x44, 0x22, 0x02, 0x43, 0x45,
  30866. 0x41, 0x10, 0x00, 0x52, 0x32, 0x73, 0x05, 0x75,
  30867. 0x72, 0x16, 0x08, 0x11, 0x51, 0x36, 0x20, 0x04,
  30868. 0x76, 0x48, 0x78, 0x56, 0x60, 0x88, 0x07, 0x47,
  30869. 0x70, 0x20, 0x46, 0x40, 0x43, 0x26, 0x04, 0x37,
  30870. 0x17, 0x51, 0x58, 0x46, 0x72, 0x44, 0x50, 0x23,
  30871. 0x67, 0x63, 0x60, 0x84, 0x30, 0x51, 0x52, 0x53,
  30872. 0x21, 0x74, 0x85, 0x45, 0x74, 0x43, 0x11, 0x72,
  30873. 0x52, 0x65, 0x76, 0x08, 0x78, 0x63, 0x14, 0x27,
  30874. 0x41, 0x34, 0x67, 0x07, 0x45, 0x15, 0x10, 0x83,
  30875. 0x24, 0x02, 0x80, 0x53, 0x07, 0x21, 0x58, 0x10,
  30876. 0x34, 0x20, 0x54, 0x12, 0x58, 0x44, 0x25, 0x53,
  30877. 0x33, 0x46, 0x02, 0x38, 0x60, 0x17, 0x70, 0x64,
  30878. 0x18, 0x52, 0x62, 0x26, 0x65, 0x61, 0x42, 0x31,
  30879. 0x22, 0x57, 0x34, 0x57, 0x02, 0x34, 0x62, 0x76,
  30880. 0x74, 0x38, 0x73, 0x21, 0x68, 0x71, 0x07, 0x21,
  30881. 0x61, 0x05, 0x20, 0x20, 0x86, 0x83, 0x30, 0x25,
  30882. 0x50, 0x50, 0x83, 0x30, 0x31, 0x56, 0x30, 0x31,
  30883. 0x76, 0x04, 0x54, 0x80, 0x75, 0x18, 0x82, 0x23,
  30884. 0x61, 0x87, 0x58, 0x25, 0x13, 0x63, 0x21, 0x51,
  30885. 0x48, 0x02, 0x67, 0x37, 0x12, 0x88, 0x70, 0x60,
  30886. 0x07, 0x36, 0x18, 0x15, 0x87, 0x74, 0x55, 0x60,
  30887. 0x00, 0x54, 0x37, 0x11, 0x01, 0x37, 0x14, 0x17,
  30888. 0x11, 0x72, 0x14, 0x55, 0x31, 0x75, 0x77, 0x48,
  30889. 0x10, 0x23, 0x83, 0x20, 0x00, 0x04, 0x32, 0x64,
  30890. 0x66, 0x61, 0x71, 0x31, 0x03, 0x15, 0x44, 0x32,
  30891. 0x57, 0x25, 0x64, 0x31, 0x28, 0x15, 0x33, 0x67,
  30892. 0x86, 0x87, 0x37, 0x03, 0x12, 0x78, 0x86, 0x13,
  30893. 0x47, 0x80, 0x61, 0x42, 0x50, 0x40, 0x23, 0x37,
  30894. 0x01, 0x01, 0x66, 0x24, 0x06, 0x57, 0x82, 0x02,
  30895. 0x22, 0x42, 0x41, 0x02, 0x26, 0x06, 0x41, 0x35,
  30896. 0x64, 0x16, 0x44, 0x42, 0x38, 0x30, 0x86, 0x88,
  30897. 0x47, 0x71, 0x62, 0x33, 0x24, 0x02, 0x12, 0x37,
  30898. 0x42, 0x33, 0x20, 0x81, 0x80, 0x53, 0x07, 0x65,
  30899. 0x71, 0x27, 0x13, 0x53, 0x15, 0x43, 0x76, 0x38,
  30900. 0x71, 0x30, 0x07, 0x87, 0x25, 0x63, 0x03, 0x33,
  30901. 0x70, 0x56, 0x18, 0x13, 0x83, 0x51, 0x44, 0x40,
  30902. 0x04, 0x80, 0x62, 0x24, 0x20, 0x64, 0x54, 0x40,
  30903. 0x20, 0x73, 0x61, 0x45, 0x01, 0x24, 0x47, 0x78,
  30904. 0x23, 0x34, 0x56, 0x10, 0x25, 0x32, 0x02, 0x70,
  30905. 0x08, 0x02, 0x23, 0x24, 0x80, 0x43, 0x04, 0x02,
  30906. 0x81, 0x11, 0x23, 0x82, 0x03, 0x61, 0x30, 0x33,
  30907. 0x15, 0x36, 0x25, 0x32, 0x14, 0x73, 0x22, 0x46,
  30908. 0x81, 0x25, 0x16, 0x13, 0x52, 0x58, 0x71, 0x61,
  30909. 0x67, 0x08, 0x38, 0x76, 0x71, 0x15, 0x88, 0x47,
  30910. 0x31, 0x25, 0x27, 0x18, 0x31, 0x50, 0x40, 0x71,
  30911. 0x06, 0x87, 0x37, 0x30, 0x85, 0x64, 0x62, 0x78,
  30912. 0x32, 0x74, 0x18, 0x83, 0x67, 0x40, 0x37, 0x44,
  30913. 0x56, 0x02, 0x72, 0x61, 0x27, 0x28, 0x38, 0x38,
  30914. 0x67, 0x17, 0x58, 0x04, 0x61, 0x28, 0x67, 0x37,
  30915. 0x46, 0x50, 0x38, 0x15, 0x45, 0x12, 0x71, 0x44,
  30916. 0x22, 0x02, 0x34, 0x83, 0x40, 0x70, 0x55, 0x75,
  30917. 0x54, 0x26, 0x88, 0x07, 0x25, 0x58, 0x73, 0x60,
  30918. 0x58, 0x61, 0x45, 0x63, 0x35, 0x05, 0x48, 0x63,
  30919. 0x48, 0x57, 0x03, 0x31, 0x28, 0x14, 0x05, 0x01,
  30920. 0x57, 0x34, 0x64, 0x50, 0x23, 0x86, 0x75, 0x85,
  30921. 0x18, 0x75, 0x56, 0x88, 0x08, 0x26, 0x01, 0x34,
  30922. 0x01, 0x57, 0x05, 0x28, 0x35, 0x48, 0x17, 0x57,
  30923. 0x71, 0x81, 0x41, 0x33, 0x77, 0x86, 0x07, 0x77,
  30924. 0x02, 0x25, 0x71, 0x74, 0x37, 0x31, 0x20, 0x14,
  30925. 0x32, 0x54, 0x20, 0x35, 0x54, 0x76, 0x83, 0x15,
  30926. 0x80, 0x73, 0x27, 0x23, 0x00, 0x58, 0x22, 0x84,
  30927. 0x64, 0x56, 0x14, 0x84, 0x38, 0x34, 0x16, 0x21,
  30928. 0x77, 0x07, 0x34, 0x81, 0x66, 0x87, 0x40, 0x11,
  30929. 0x62, 0x46, 0x45, 0x01, 0x20, 0x53, 0x21, 0x73,
  30930. 0x07, 0x76, 0x44, 0x15, 0x61, 0x50, 0x83, 0x48,
  30931. 0x58, 0x58, 0x45, 0x33, 0x25, 0x36, 0x07, 0x42,
  30932. 0x70, 0x24, 0x07, 0x41, 0x08, 0x35, 0x00, 0x78,
  30933. 0x41, 0x47, 0x02, 0x56, 0x07, 0x14, 0x68, 0x33,
  30934. 0x55, 0x77, 0x32, 0x40, 0x55, 0x24, 0x50, 0x26,
  30935. 0x47, 0x12, 0x65, 0x58, 0x43, 0x05, 0x52, 0x55,
  30936. 0x75, 0x50, 0x18, 0x46, 0x65, 0x48, 0x03, 0x32,
  30937. 0x85, 0x31, 0x16, 0x52, 0x71, 0x57, 0x87, 0x46,
  30938. 0x76, 0x14, 0x42, 0x81, 0x28, 0x74, 0x60, 0x34,
  30939. 0x35, 0x55, 0x52, 0x16, 0x58, 0x48, 0x61, 0x75,
  30940. 0x80, 0x88, 0x15, 0x32, 0x72, 0x26, 0x31, 0x03,
  30941. 0x05, 0x03, 0x16, 0x04, 0x07, 0x37, 0x37, 0x73,
  30942. 0x43, 0x81, 0x57, 0x31, 0x88, 0x04, 0x72, 0x76,
  30943. 0x01, 0x61, 0x81, 0x17, 0x37, 0x65, 0x44, 0x38,
  30944. 0x61, 0x23, 0x16, 0x26, 0x52, 0x45, 0x00, 0x73,
  30945. 0x83, 0x63, 0x64, 0x62, 0x26, 0x74, 0x60, 0x11,
  30946. 0x81, 0x08, 0x06, 0x30, 0x36, 0x05, 0x10, 0x48,
  30947. 0x47, 0x35, 0x10, 0x85, 0x30, 0x86, 0x71, 0x38,
  30948. 0x16, 0x37, 0x6F, 0x3B, 0x1C, 0x18, 0xB1, 0xE3,
  30949. 0xE8, 0xEE, 0x83, 0x3E, 0x8D, 0x38, 0x43, 0x9E,
  30950. 0x78, 0x1C, 0xA3, 0xB8, 0x94, 0x06, 0x54, 0xEF,
  30951. 0x44, 0x6C, 0x9A, 0xAC, 0xC3, 0xF1, 0xD3, 0x0E,
  30952. 0xE0, 0x10, 0x5B, 0x8F, 0x63, 0xEB, 0x89, 0x74,
  30953. 0x6E, 0xF4, 0xBE, 0xB5, 0x4C, 0xFC, 0xE8, 0x81,
  30954. 0x2C, 0xF9, 0x47, 0xCF, 0x54, 0x54, 0xFB, 0x1C,
  30955. 0xA5, 0x5F, 0x25, 0xA0, 0xFE, 0x57, 0xF5, 0xFC,
  30956. 0xFD, 0x73, 0xB0, 0xDA, 0x04, 0xB0, 0xBF, 0x28,
  30957. 0x92, 0x92, 0xAF, 0x39, 0x74, 0x72, 0x56, 0x69,
  30958. 0xC3, 0x00, 0x03, 0xE0, 0x50, 0x9F, 0xED, 0xC8,
  30959. 0x0F, 0x6C, 0x89, 0x4B, 0xB0, 0x47, 0xC2, 0xE2,
  30960. 0xAF, 0x48, 0x5C, 0xAD, 0x68, 0xC2, 0x1D, 0x80,
  30961. 0xEF, 0x33, 0xB0, 0xC4, 0xFD, 0xA6, 0x7B, 0x85,
  30962. 0x31, 0xA1, 0x58, 0x87, 0x67, 0x54, 0x71, 0x3F,
  30963. 0xF8, 0xA8, 0xA6, 0x8D, 0x9A, 0xBD, 0xC4, 0x81,
  30964. 0x6B, 0x24, 0xB4, 0xA3, 0x6A, 0x8A, 0x2B, 0xB1,
  30965. 0xFD, 0x1C, 0x2C, 0x25, 0xC3, 0x72, 0xC4, 0xB7,
  30966. 0x75, 0xF8, 0xCC, 0x17, 0x39, 0xCF, 0x2C, 0xE9,
  30967. 0xA4, 0x54, 0x58, 0xE4, 0x1A, 0xAE, 0xC6, 0x4A,
  30968. 0xEE, 0xDE, 0x75, 0x7C, 0xE7, 0x38, 0xBC, 0xDF,
  30969. 0x4D, 0xA0, 0xEE, 0x2B, 0xDD, 0x5F, 0x80, 0x5C,
  30970. 0xCF, 0xF7, 0x2A, 0x5F, 0x73, 0x8B, 0xAC, 0x12,
  30971. 0x34, 0x2E, 0xE3, 0xF1, 0x4C, 0xB7, 0x22, 0x68,
  30972. 0xC2, 0xD6, 0x36, 0x7D, 0xF1, 0x7F, 0x20, 0x46,
  30973. 0xA2, 0x4B, 0x47, 0x4B, 0x32, 0x58, 0xF7, 0xB0,
  30974. 0x88, 0x54, 0x6C, 0x99, 0x3B, 0x0D, 0xA1, 0xE2,
  30975. 0x92, 0x92, 0xEB, 0x72, 0x1E, 0xE7, 0xE5, 0xA1,
  30976. 0xF8, 0x6E, 0x14, 0xA5, 0x39, 0xB0, 0x63, 0x6F,
  30977. 0x78, 0x82, 0xA1, 0x9C, 0x8D, 0x79, 0x02, 0x85,
  30978. 0xA6, 0xDF, 0x7D, 0xEE, 0xCE, 0x17, 0x4D, 0x63,
  30979. 0xCF, 0xF3, 0xB2, 0xFF, 0x85, 0x68, 0x81, 0xCB,
  30980. 0x38, 0x6B, 0x1B, 0x38, 0xA2, 0xE0, 0xF2, 0x4C,
  30981. 0x31, 0xE0, 0x91, 0x93, 0xDD, 0xF3, 0x71, 0x47,
  30982. 0xF2, 0x69, 0xD9, 0x4C, 0xDE, 0xF9, 0x90, 0x61,
  30983. 0x34, 0x62, 0x07, 0x71, 0x79, 0xD0, 0xDD, 0x09,
  30984. 0x32, 0x64, 0x39, 0x49, 0x93, 0x1A, 0x02, 0xBA,
  30985. 0xFA, 0x80, 0x17, 0x6E, 0xDF, 0x97, 0xB6, 0xA2,
  30986. 0x31, 0x34, 0x71, 0xF0, 0xB1, 0x9B, 0x3B, 0x59,
  30987. 0xF4, 0x3B, 0xD2, 0x2A, 0x05, 0x49, 0x3E, 0xFB,
  30988. 0x0C, 0xF8, 0xB5, 0xD7, 0xB6, 0x25, 0x2B, 0x09,
  30989. 0x8B, 0x4B, 0xFA, 0x39, 0x5B, 0xF9, 0xA2, 0x09,
  30990. 0xE9, 0xBB, 0x46, 0x01, 0x30, 0x00, 0x90, 0x32,
  30991. 0x58, 0xA6, 0x9B, 0x67, 0xF5, 0x94, 0x11, 0xC8,
  30992. 0x35, 0x95, 0xFA, 0x6E, 0x67, 0x42, 0x8D, 0x96,
  30993. 0x6D, 0x20, 0xFC, 0xD3, 0x09, 0x61, 0x11, 0x86,
  30994. 0x77, 0xC0, 0x86, 0xA3, 0x54, 0xAE, 0x6D, 0x41,
  30995. 0xEE, 0x17, 0xDC, 0xA1, 0xB0, 0xB7, 0x50, 0x43,
  30996. 0xD6, 0xCE, 0x23, 0xBD, 0xB0, 0x1E, 0x02, 0xE5,
  30997. 0x9E, 0xCF, 0xC6, 0x2E, 0x8C, 0x39, 0x71, 0xB1,
  30998. 0x45, 0x02, 0x75, 0xBA, 0x7F, 0x60, 0xB0, 0x8B,
  30999. 0x1C, 0x33, 0xBA, 0x0C, 0xFF, 0x54, 0x63, 0xE3,
  31000. 0x47, 0x5B, 0x07, 0x77, 0x77, 0xC5, 0x72, 0x24,
  31001. 0x60, 0xFA, 0xDB, 0x0B, 0xF6, 0x41, 0x82, 0x69,
  31002. 0x3C, 0x68, 0x37, 0xF5, 0xFD, 0x45, 0x4A, 0x66,
  31003. 0x6C, 0xD7, 0x01, 0x10, 0x78, 0x4A, 0xED, 0x09,
  31004. 0xAE, 0x49, 0x0A, 0x60, 0xC7, 0x78, 0x56, 0x51,
  31005. 0x15, 0xE3, 0x4A, 0xB5, 0xAE, 0xAD, 0x09, 0xD1,
  31006. 0x71, 0xA8, 0xCA, 0x3C, 0x8A, 0xE6, 0xCA, 0x39,
  31007. 0x43, 0x60, 0x56, 0x83, 0x3C, 0x58, 0x04, 0xD4,
  31008. 0xB4, 0x62, 0xDD, 0x53, 0x05, 0xC8, 0x51, 0xAF,
  31009. 0x59, 0xF6, 0x4F, 0x04, 0xC3, 0x1E, 0x69, 0xFF,
  31010. 0x82, 0xBF, 0xD7, 0x89, 0xD2, 0x30, 0x9F, 0xF2,
  31011. 0xE6, 0x38, 0x05, 0x9C, 0xD5, 0x08, 0xB8, 0x25,
  31012. 0xF3, 0x3B, 0x99, 0x85, 0x4E, 0x40, 0xF8, 0x40,
  31013. 0xF2, 0x4B, 0x5C, 0x3A, 0xA8, 0x64, 0x41, 0x92,
  31014. 0xEA, 0xCA, 0x9A, 0x7B, 0xCF, 0xBA, 0x1F, 0xDE,
  31015. 0xE0, 0x9D, 0xCA, 0xAD, 0xB4, 0x0C, 0x90, 0xFF,
  31016. 0xE1, 0x6C, 0xEC, 0xDD, 0x32, 0x38, 0x2A, 0xF7,
  31017. 0x19, 0x20, 0x39, 0xCB, 0x29, 0x67, 0x2F, 0x70,
  31018. 0x71, 0x12, 0x10, 0xB6, 0xB8, 0x3E, 0x8D, 0xFD,
  31019. 0xB5, 0xFB, 0xBD, 0xBF, 0xA8, 0xCA, 0x19, 0xC4,
  31020. 0xC6, 0xAC, 0x37, 0x31, 0xFC, 0x33, 0xC2, 0x7F,
  31021. 0xA2, 0xA2, 0x6D, 0xEB, 0x15, 0x2E, 0xA1, 0x90,
  31022. 0xF8, 0x29, 0xC6, 0x34, 0xD1, 0x39, 0x30, 0x24,
  31023. 0x1C, 0xB9, 0x26, 0xAC, 0xDD, 0xE5, 0x24, 0x9C,
  31024. 0xDD, 0x35, 0x60, 0x7E, 0x38, 0x0C, 0xC1, 0x2A,
  31025. 0x7D, 0x1E, 0xA9, 0xBA, 0xA5, 0x58, 0x4C, 0xDD,
  31026. 0x26, 0x86, 0x09, 0xDC, 0xC3, 0xB0, 0x1F, 0xCD,
  31027. 0xC9, 0xAD, 0xCB, 0x4A, 0x7E, 0x51, 0x67, 0xE5,
  31028. 0xED, 0x5A, 0xD2, 0x21, 0xDB, 0x2E, 0xAB, 0xD9,
  31029. 0x0A, 0xEC, 0xAE, 0x71, 0xFA, 0x23, 0x7A, 0xEF,
  31030. 0x98, 0xDF, 0x53, 0x89, 0x93, 0xE8, 0x71, 0xD7,
  31031. 0x35, 0xDA, 0x6B, 0x88, 0x31, 0xAF, 0x67, 0xF2,
  31032. 0x97, 0x29, 0x1C, 0x39, 0x67, 0xEB, 0xAF, 0x60,
  31033. 0xD9, 0x53, 0xC4, 0x0F, 0x7A, 0x46, 0x4E, 0xF3,
  31034. 0x2F, 0x8E, 0xAE, 0xFA, 0x64, 0x2E, 0x37, 0xDE,
  31035. 0xA9, 0x74, 0x73, 0x5D, 0xDD, 0xBB, 0x83, 0x54,
  31036. 0x27, 0xB9, 0x7A, 0x63, 0x2B, 0x19, 0x8B, 0x26,
  31037. 0x22, 0x28, 0x84, 0xA0, 0x58, 0x00, 0x2D, 0x55,
  31038. 0xEA, 0x2A, 0x80, 0x0D, 0x6C, 0x97, 0x0E, 0x8B,
  31039. 0xF7, 0x67, 0xB2, 0x8B, 0x2D, 0xDE, 0x8F, 0x58,
  31040. 0xFE, 0x97, 0x81, 0xE7, 0xE2, 0x58, 0x8D, 0x7E,
  31041. 0x1B, 0xAB, 0xE5, 0x15, 0x9D, 0x54, 0xF4, 0x00,
  31042. 0x34, 0x1D, 0x12, 0x1B, 0x03, 0x23, 0x2B, 0x06,
  31043. 0x2E, 0x8C, 0xD0, 0x0A, 0xDC, 0x19, 0xA1, 0x69,
  31044. 0x1D, 0x72, 0x91, 0xB4, 0xED, 0x0E, 0x81, 0xF7,
  31045. 0x05, 0x99, 0x84, 0xFC, 0x74, 0x0F, 0x7D, 0xF8,
  31046. 0x9B, 0x3E, 0x7F, 0x63, 0x7C, 0x73, 0xEB, 0xF5,
  31047. 0x36, 0xB3, 0x24, 0x22, 0xAA, 0x33, 0x0C, 0x30,
  31048. 0x42, 0xC3, 0xE2, 0x04, 0x6B, 0x3F, 0x2A, 0x0D,
  31049. 0xAB, 0xE8, 0x5A, 0x9A, 0x09, 0xD7, 0xB6, 0xAA,
  31050. 0x9C, 0x3E, 0xD0, 0x9E, 0xB5, 0x9B, 0x52, 0x7B,
  31051. 0xAF, 0x2D, 0x6B, 0xE0, 0x40, 0x12, 0x34, 0xBE,
  31052. 0x49, 0xAB, 0xD2, 0xC8, 0xB5, 0x89, 0x1B, 0x79,
  31053. 0xEC, 0xAE, 0x88, 0x89, 0x3C, 0x05, 0xC7, 0x75,
  31054. 0xC5, 0x84, 0xF7, 0x10, 0x49, 0x48, 0x92, 0x69,
  31055. 0x9E, 0xD5, 0x56, 0xB2, 0x1E, 0x81, 0x18, 0x78,
  31056. 0xCB, 0x93, 0x5D, 0x70, 0x3A, 0xB2, 0x67, 0xD1,
  31057. 0xCC, 0x8F, 0x83, 0x03, 0xB9, 0x64, 0x46, 0x22,
  31058. 0x78, 0x0D, 0x55, 0x67, 0x22, 0x58, 0x0E, 0x22,
  31059. 0x6B, 0xBA, 0x01, 0xD4, 0x77, 0x05, 0xA7, 0xAC,
  31060. 0xB7, 0xE5, 0xFC, 0xE6, 0x11, 0xCC, 0x92, 0x5A,
  31061. 0x8C, 0xC0, 0x08, 0x24, 0xAF, 0xCC, 0x4D, 0xBD,
  31062. 0x79, 0xD3, 0x5C, 0x52, 0x2C, 0xFF, 0x1A, 0x48,
  31063. 0xBB, 0x91, 0x59, 0x6A, 0x80, 0x32, 0x8C, 0x75,
  31064. 0x7C, 0xD2, 0xC1, 0x94, 0x94, 0xA8, 0x55, 0x4B,
  31065. 0xF2, 0x96, 0xF7, 0x86, 0xF7, 0x53, 0x4F, 0x54,
  31066. 0x74, 0x05, 0x5C, 0xEF, 0x02, 0xA0, 0x8A, 0xD1,
  31067. 0x88, 0x72, 0xEB, 0x1B, 0x82, 0xF9, 0xFB, 0xDA,
  31068. 0xBC, 0xB9, 0x90, 0x98, 0xF2, 0x4B, 0x9A, 0xA6,
  31069. 0x89, 0xD5, 0xB3, 0xD8, 0x7B, 0x94, 0xE3, 0x1F,
  31070. 0x17, 0x4F, 0xEB, 0x24, 0x06, 0x2B, 0xAB, 0x5F,
  31071. 0x27, 0x9B, 0xCD, 0xCE, 0x50, 0x06, 0x40, 0xDD,
  31072. 0x7A, 0x8C, 0x67, 0xF0, 0x8E, 0x07, 0xB4, 0x1C,
  31073. 0x3C, 0x13, 0xB2, 0x07, 0x6A, 0x38, 0x59, 0x94,
  31074. 0x2C, 0xB1, 0x72, 0xA8, 0x77, 0x5B, 0x15, 0x8F,
  31075. 0x88, 0xC4, 0x5C, 0xDC, 0x92, 0xCA, 0xC0, 0xED,
  31076. 0x02, 0xFF, 0x1D, 0x57, 0x25, 0xBE, 0x67, 0x3E,
  31077. 0x4C, 0xE8, 0x95, 0x2A, 0x80, 0xB2, 0x5D, 0xBC,
  31078. 0xFA, 0x17, 0xA9, 0x35, 0x0A, 0x6B, 0x07, 0xC8,
  31079. 0x8F, 0x88, 0x8D, 0xBC, 0x97, 0x84, 0xE2, 0x07,
  31080. 0x57, 0x92, 0x99, 0x4B, 0xE8, 0xDD, 0xD7, 0xA4,
  31081. 0x58, 0xCB, 0x61, 0xCE, 0x16, 0xFC, 0x22, 0xCD,
  31082. 0x4B, 0x1A, 0x08, 0xC9, 0xAD, 0x3D, 0xB1, 0xF2,
  31083. 0xA9, 0x1B, 0x8E, 0xD0, 0xC7, 0xBC, 0xCE, 0xF9,
  31084. 0x0A, 0x7A, 0x4D, 0xBE, 0x82, 0x0A, 0xBD, 0x6C,
  31085. 0x42, 0x99, 0xBF, 0x86, 0x65, 0x53, 0xAA, 0x04,
  31086. 0x79, 0xD6, 0x6D, 0x7E, 0x0F, 0x40, 0xFA, 0xEE,
  31087. 0xCE, 0x38, 0x3B, 0x1C, 0x2F, 0xA4, 0x45, 0xA3,
  31088. 0x78, 0x2B, 0xA0, 0x29, 0xC5, 0xAA, 0xA9, 0x09,
  31089. 0x29, 0x51, 0xDC, 0x5B, 0xB5, 0x95, 0xE4, 0xCE,
  31090. 0xC8, 0x50, 0x71, 0x2D, 0xE9, 0x32, 0x12, 0xA0,
  31091. 0x7C, 0x88, 0x6B, 0xED, 0xE4, 0x38, 0xB7, 0x92,
  31092. 0xCA, 0xE4, 0xDC, 0xD4, 0x05, 0x3B, 0x2B, 0x84,
  31093. 0x95, 0x07, 0xFF, 0xF4, 0x79, 0xFF, 0x1E, 0x73,
  31094. 0x1B, 0x8E, 0xDF, 0xA3, 0x15, 0xBD, 0x56, 0xAC,
  31095. 0xDA, 0xAD, 0x73, 0x95, 0xC2, 0xD3, 0x72, 0xA8,
  31096. 0xF0, 0x8E, 0x6C, 0xE3, 0x7D, 0xBE, 0x4C, 0x87,
  31097. 0xFC, 0x0F, 0xA6, 0x3B, 0xED, 0xA4, 0x0F, 0x4F,
  31098. 0xF1, 0x5D, 0xF2, 0x56, 0x54, 0xD1, 0xCE, 0x6C,
  31099. 0xCA, 0x1C, 0xCB, 0xC2, 0x45, 0x7F, 0x90, 0x61,
  31100. 0x0E, 0x3D, 0xCE, 0xBB, 0x5E, 0x41, 0x38, 0x2B,
  31101. 0xD4, 0x41, 0x7C, 0x67, 0x7C, 0x71, 0x95, 0x34,
  31102. 0xD7, 0xED, 0x4D, 0xAC, 0x6E, 0xF1, 0x46, 0xEA,
  31103. 0x7D, 0xA4, 0x4C, 0x69, 0x0B, 0x9C, 0x2F, 0xAA,
  31104. 0xF1, 0x17, 0x90, 0x1B, 0xF4, 0x4C, 0x03, 0xBE,
  31105. 0x9D, 0x56, 0xCE, 0x0C, 0xCF, 0xE0, 0x87, 0x44,
  31106. 0xBE, 0x2C, 0x52, 0xD3, 0xBC, 0xAE, 0x02, 0x30,
  31107. 0xC7, 0x26, 0x06, 0x88, 0xA6, 0xAA, 0x9D, 0x50,
  31108. 0xF1, 0x94, 0x58, 0xC7, 0x60, 0xF3, 0xA0, 0x6F,
  31109. 0x53, 0x66, 0x53, 0xCD, 0x1D, 0xBE, 0xD1, 0xF2,
  31110. 0x39, 0xBA, 0x1F, 0xE8, 0x40, 0x84, 0xCD, 0x1C,
  31111. 0x8F, 0x3D, 0xB7, 0xD1, 0x51, 0x00, 0xDE, 0xB8,
  31112. 0x11, 0xD9, 0x66, 0xAD, 0xD5, 0xE9, 0x33, 0x09,
  31113. 0xE1, 0xA8, 0x00, 0x58, 0x65, 0xF1, 0xC1, 0x67,
  31114. 0xB4, 0x3A, 0xA7, 0x98, 0x90, 0x6A, 0xDB, 0x91,
  31115. 0xDB, 0x4A, 0x16, 0x35, 0xDC, 0x3D, 0x69, 0xEB,
  31116. 0x7B, 0xDE, 0xCC, 0x91, 0x1B, 0x8D, 0xE6, 0x46,
  31117. 0x61, 0x8E, 0x3F, 0x4C, 0x88, 0x81, 0x85, 0x4A,
  31118. 0x73, 0x08, 0x56, 0x52, 0xAE, 0xE6, 0x4A, 0x60,
  31119. 0x4A, 0x2E, 0x0C, 0x9A, 0x93, 0x76, 0x35, 0xC9,
  31120. 0x36, 0x28, 0x0C, 0x72, 0x19, 0xAD, 0x33, 0xCF,
  31121. 0x2B, 0xFB, 0xCE, 0x1A, 0x7D, 0xAC, 0xAA, 0x75,
  31122. 0x15, 0x76, 0x81, 0x52, 0x55, 0xCC, 0xB9, 0x39,
  31123. 0x07, 0xA3, 0x39, 0x12, 0x8D, 0x6F, 0x53, 0xAF,
  31124. 0xC7, 0x14, 0x7F, 0xC7, 0x96, 0x5A, 0x49, 0x3C,
  31125. 0x5C, 0xB0, 0x26, 0x47, 0xF4, 0x9D, 0xCA, 0x23,
  31126. 0xA6, 0x7D, 0xA6, 0x61, 0xC4, 0xA3, 0x26, 0x40,
  31127. 0x0F, 0xA7, 0x27, 0x09, 0xBC, 0x39, 0xFD, 0xA7,
  31128. 0x75, 0x38, 0x74, 0xD0, 0x9D, 0x29, 0x15, 0x97,
  31129. 0xDE, 0x25, 0x60, 0x4D, 0x19, 0x36, 0x04, 0xFB,
  31130. 0xA5, 0x2C, 0xB0, 0xC8, 0xB5, 0xFE, 0xE5, 0x94,
  31131. 0x7C, 0xE2, 0x1F, 0x84, 0xBB, 0xFB, 0x78, 0x9E,
  31132. 0xA5, 0x7C, 0x5D, 0x4A, 0xB2, 0x48, 0x6F, 0x6E,
  31133. 0x67, 0x95, 0x16, 0x5F, 0x01, 0x2A, 0xF8, 0x70,
  31134. 0x95, 0xCB, 0x06, 0x93, 0x26, 0x6E, 0x7A, 0x75,
  31135. 0xB5, 0xE5, 0x4E, 0x27, 0x1D, 0x8B, 0x30, 0xA6,
  31136. 0x67, 0x67, 0xD6, 0xE2, 0xD6, 0xD1, 0x99, 0xA4,
  31137. 0x55, 0x73, 0x19, 0x32, 0xF6, 0x0B, 0x6B, 0x4A,
  31138. 0xEE, 0x23, 0x33, 0x38, 0x30, 0x68, 0x6F, 0x8E,
  31139. 0x60, 0xA9, 0x60, 0x97, 0x3E, 0xEA, 0x5D, 0xE1,
  31140. 0x40, 0x6F, 0x0C, 0x76, 0x84, 0xCF, 0xAF, 0x86,
  31141. 0x8D, 0x36, 0xE5, 0x7D, 0xAE, 0x9A, 0x13, 0x70,
  31142. 0x22, 0x2A, 0x31, 0xFE, 0xC2, 0xFB, 0xE1, 0x58,
  31143. 0xA5, 0x4E, 0xEF, 0x10, 0x5B, 0x5E, 0xD4, 0x39,
  31144. 0xFC, 0xF9, 0x15, 0x64, 0x78, 0x43, 0x7D, 0x03,
  31145. 0x9F, 0x5B, 0xCB, 0x86, 0xD2, 0xEF, 0x28, 0xBD,
  31146. 0x14, 0xCB, 0x8A, 0x04, 0x1D, 0x59, 0x23, 0x53,
  31147. 0x4D, 0x13, 0xF9, 0x93, 0xFE, 0x19, 0x9C, 0xC3,
  31148. 0x3F, 0xD9, 0xC1, 0x12, 0x94, 0x84, 0x13, 0x95,
  31149. 0x8F, 0xD9, 0x10, 0xAB, 0x37, 0x69, 0x08, 0x04,
  31150. 0x4A, 0x97, 0x82, 0x28, 0x75, 0xBB, 0xC9, 0xF4,
  31151. 0x3F, 0x19, 0x6B, 0x00, 0x4C, 0x56, 0x16, 0x1F,
  31152. 0x50, 0x82, 0xD1, 0x45, 0xFF, 0x0C, 0x37, 0x28,
  31153. 0x04, 0xBB, 0x6C, 0x00, 0x97, 0x3A, 0x79, 0x2D,
  31154. 0x9A, 0xB9, 0xA5, 0x16, 0x52, 0x02, 0xA3, 0x86,
  31155. 0x81, 0xAA, 0x3A, 0x31, 0xE5, 0xB5, 0x44, 0x2D,
  31156. 0x34, 0xE2, 0x7A, 0xD8, 0xFE, 0xA1, 0x36, 0xC0,
  31157. 0x36, 0x65, 0x73, 0x12, 0x9F, 0x61, 0x3F, 0x59,
  31158. 0xC9, 0x68, 0xB6, 0x34, 0x41, 0x40, 0x25, 0xD6,
  31159. 0xE7, 0xAD, 0x25, 0x7D, 0xCB, 0xF1, 0x2A, 0xD8,
  31160. 0x53, 0x48, 0x9D, 0xBF, 0xB5, 0xD5, 0x61, 0x18,
  31161. 0x0E, 0x2A, 0x21, 0x3E, 0x61, 0x18, 0x07, 0x8E,
  31162. 0x6F, 0x9A, 0x96, 0xA8, 0x61, 0xFE, 0x8D, 0x66,
  31163. 0x1A, 0x21, 0x99, 0xD9, 0x60, 0x8B, 0xAC, 0x85,
  31164. 0x84, 0x3D, 0x41, 0xF9, 0x93, 0x35, 0x24, 0x32,
  31165. 0xFF, 0xC0, 0x8A, 0xFA, 0xBC, 0xA7, 0x85, 0x57,
  31166. 0x3C, 0x16, 0x83, 0xAE, 0x90, 0xDE, 0x40, 0x12,
  31167. 0xE4, 0x2B, 0xA2, 0x47, 0xA4, 0x92, 0x73, 0x54,
  31168. 0x6C, 0xA5, 0xB7, 0xEE, 0x62, 0xEA, 0x62, 0x37,
  31169. 0xD9, 0xD7, 0x73, 0x58, 0x43, 0xDB, 0x20, 0x60,
  31170. 0x8C, 0x4F, 0x87, 0x58, 0xB2, 0x2B, 0xC3, 0x40,
  31171. 0xB0, 0xC1, 0xB6, 0xB6, 0xA9, 0xCD, 0xCC, 0x05,
  31172. 0x4F, 0x38, 0x5F, 0x08, 0xB3, 0x3B, 0x08, 0x4D,
  31173. 0x78, 0x6B, 0x0D, 0x40, 0x46, 0xB9, 0x20, 0xDE,
  31174. 0x29, 0x6F, 0x23, 0x96, 0xDA, 0x02, 0xF5, 0x1C,
  31175. 0x1A, 0x1A, 0x36, 0xA3, 0x3A, 0xFA, 0x1D, 0x80,
  31176. 0x36, 0x3C, 0xF6, 0xB4, 0xDC, 0x2C, 0x88, 0x54,
  31177. 0xF7, 0x86, 0xC6, 0xF2, 0x15, 0xF8, 0x85, 0x33,
  31178. 0xFB, 0x21, 0x20, 0x59, 0xCE, 0x60, 0x4B, 0xE8,
  31179. 0xF1, 0xB7, 0x54, 0x17, 0x1E, 0x83, 0xCD, 0x82,
  31180. 0x39, 0x40, 0x14, 0x31, 0xEC, 0x89, 0xC8, 0xE2,
  31181. 0x6A, 0xAE, 0x3F, 0x49, 0x5B, 0x38, 0xE7, 0xCD,
  31182. 0xE2, 0xF6, 0xEF, 0x90, 0x51, 0x10, 0x83, 0x79,
  31183. 0x27, 0x80, 0x2F, 0x45, 0x78, 0x67, 0xAF, 0xF4,
  31184. 0x65, 0x95, 0x2D, 0xFE, 0x00, 0xF3, 0x2A, 0x60,
  31185. 0x00, 0xF7, 0x26, 0xFA, 0x3C, 0xAD, 0xA9, 0xAF,
  31186. 0xCA, 0xF6, 0x69, 0x48, 0x03, 0xBE, 0x18, 0x73,
  31187. 0x54, 0x06, 0x06, 0x3E, 0x4E, 0xAD, 0xFC, 0x8B,
  31188. 0xC3, 0x43, 0x24, 0x5D, 0xE9, 0xDE, 0x78, 0xDC,
  31189. 0xD0, 0xA7, 0x04, 0x77, 0xF0, 0x0D, 0xA3, 0x37,
  31190. 0x8C, 0x5F, 0x8B, 0xDF, 0xBE, 0x90, 0x1F, 0xA6,
  31191. 0xB3, 0x17, 0x9D, 0x68, 0x36, 0x45, 0x11, 0x60,
  31192. 0xFF, 0xF9, 0xBA, 0xDA, 0x80, 0xAA, 0x37, 0x57,
  31193. 0xDD, 0x34, 0x30, 0x42, 0x7A, 0x9C, 0x86, 0xB4,
  31194. 0x91, 0x30, 0xB8, 0xC0, 0xC4, 0x29, 0x15, 0x31,
  31195. 0xF3, 0x9A, 0xB0, 0xCD, 0xAC, 0x8C, 0x7C, 0x8C,
  31196. 0x4A, 0xDC, 0x76, 0xB6, 0x31, 0x30, 0xDE, 0x2D,
  31197. 0x81, 0x04, 0xC7, 0x48, 0x73, 0x69, 0x02, 0x40,
  31198. 0x30, 0x19, 0x66, 0x94, 0x21, 0x65, 0x13, 0x18,
  31199. 0xC2, 0x09, 0x14, 0x5F, 0xC4, 0x2F, 0xC4, 0xD6,
  31200. 0xA6, 0x05, 0x37, 0xAF, 0x72, 0x0C, 0x47, 0x02,
  31201. 0x29, 0x95, 0x08, 0x9D, 0xC9, 0x07, 0x31, 0x38,
  31202. 0xA9, 0xB5, 0xDA, 0x21, 0x76, 0x1D, 0x84, 0xD0,
  31203. 0x15, 0xAF, 0x2A, 0xA3, 0x69, 0x0A, 0xE9, 0x4F,
  31204. 0x75, 0x8A, 0x50, 0xA5, 0x11, 0xD4, 0x5F, 0xAF,
  31205. 0x70, 0x43, 0xCB, 0xD7, 0x03, 0x9E, 0xB0, 0xBD,
  31206. 0x19, 0x47, 0x94, 0x58, 0x22, 0x86, 0xC6, 0xE3,
  31207. 0x62, 0xD8, 0x63, 0x05, 0xD9, 0xE2, 0xE5, 0x4A,
  31208. 0x04, 0x54, 0x5A, 0x55, 0x25, 0xAD, 0x15, 0x5C,
  31209. 0x4B, 0x71, 0x25, 0xE1, 0x50, 0xE3, 0x62, 0x1B,
  31210. 0xD2, 0x43, 0x28, 0xD2, 0x84, 0xE4, 0xE2, 0x05,
  31211. 0xE3, 0x01, 0x4C, 0x8F, 0x38, 0x17, 0x49, 0xFD,
  31212. 0x3B, 0x52, 0x1A, 0x55, 0xB3, 0x1D, 0x69, 0x83,
  31213. 0xAB, 0x9E, 0xC4, 0x73, 0xEE, 0x64, 0x7A, 0x73,
  31214. 0x19, 0xEF, 0xCD, 0x7D, 0xB7, 0xF4, 0x2E, 0xCB,
  31215. 0x55, 0x2A, 0x8A, 0xCC, 0x8F, 0xF8, 0x4E, 0xFB,
  31216. 0xD2, 0x63, 0x8F, 0xF1, 0x10, 0x89, 0x02, 0x93,
  31217. 0x3E, 0xAC, 0xA4, 0xB4, 0x89, 0xC7, 0xF7, 0x8B,
  31218. 0x3E, 0xE1, 0xE8, 0x93, 0xB9, 0x8E, 0x36, 0x25,
  31219. 0xC1, 0xC0, 0xD9, 0x44, 0x81, 0xC0, 0x99, 0x3C,
  31220. 0x2B, 0x89, 0xF7, 0xDF, 0xDB, 0xD8, 0xCC, 0x84,
  31221. 0xE6, 0xFF, 0xFE, 0xAC, 0x21, 0x16, 0xF1, 0xE2,
  31222. 0xEF, 0x0A, 0x32, 0xA7, 0xDE, 0x87, 0x51, 0xEC,
  31223. 0xB1, 0x0C, 0x0B, 0xC7, 0x07, 0xD9, 0x9A, 0xF8,
  31224. 0xE8, 0xB0, 0xFE, 0xA5, 0x67, 0xAF, 0x53, 0x9F,
  31225. 0xEF, 0x23, 0xEF, 0x7D, 0xFF, 0xA8, 0x8E, 0xDE,
  31226. 0x97, 0x93, 0x32, 0xA6, 0x7C, 0xCF, 0x49, 0xBC,
  31227. 0x36, 0x0D, 0x88, 0x90, 0x89, 0x39, 0x76, 0xA8,
  31228. 0x82, 0x19, 0x02, 0xB6, 0x02, 0x82, 0xFE, 0xED,
  31229. 0x9C, 0x28, 0x8D, 0xB0, 0x1E, 0x2B, 0x2A, 0xCF,
  31230. 0xF3, 0x94, 0xFF, 0x66, 0x33, 0x93, 0x31, 0xD6,
  31231. 0xFC, 0xAF, 0xE7, 0xC5, 0x98, 0x01, 0x46, 0xCD,
  31232. 0xCB, 0xC4, 0x41, 0x13, 0x6D, 0x42, 0xF5, 0x13,
  31233. 0xDF, 0xF9, 0x97, 0x65, 0xD4, 0x7B, 0x6E, 0x10,
  31234. 0x79, 0x5D, 0x5A, 0x82, 0xA2, 0x49, 0x53, 0xA7,
  31235. 0x6D, 0x9C, 0xDD, 0x0A, 0x80, 0x98, 0x58, 0x07,
  31236. 0x30, 0xBF, 0x0B, 0x30, 0xAC, 0x24, 0x9E, 0xA0,
  31237. 0xE8, 0xE4, 0x7A, 0x0D, 0xD0, 0x50, 0x82, 0xAE,
  31238. 0xBB, 0xEC, 0x15, 0x30, 0x2A, 0xF2, 0xA7, 0xA6,
  31239. 0x6A, 0xC8, 0xAE, 0x1E, 0x14, 0x80, 0x7C, 0x18,
  31240. 0xE7, 0x2B, 0x88, 0x65, 0xB7, 0x93, 0x12, 0xB3,
  31241. 0xC1, 0x2A, 0x20, 0xAD, 0x3B, 0x2E, 0x84, 0xC4,
  31242. 0x0D, 0xA7, 0x62, 0x5C, 0x79, 0x52, 0x5D, 0x59,
  31243. 0xA4, 0x69, 0x5C, 0x26, 0xFD, 0x4F, 0x80, 0xCC,
  31244. 0xFE, 0x8E, 0x70, 0x72, 0xB1, 0x41, 0xE1, 0x75,
  31245. 0x53, 0x51, 0xCF, 0x4C, 0x0B, 0x57, 0xF2, 0xB8,
  31246. 0x59, 0x76, 0xE6, 0xEF, 0x6D, 0x74, 0xA6, 0x73,
  31247. 0x69, 0x7F, 0x7C, 0xB2, 0x35, 0xFE, 0x8A, 0x02,
  31248. 0x2F, 0xBE, 0x7C, 0x4D, 0x02, 0xBE, 0x8F, 0xFB,
  31249. 0x7A, 0x58, 0x45, 0xEC, 0xBA, 0x1B, 0xC6, 0xB9,
  31250. 0x8D, 0xF5, 0xB0, 0x82, 0xD1, 0xB4, 0x97, 0x86,
  31251. 0x9B, 0x33, 0x54, 0x49, 0x5B, 0x88, 0xD9, 0xB5,
  31252. 0xD0, 0x93, 0x8A, 0x00, 0x5D, 0x0F, 0x37, 0x88,
  31253. 0x57, 0xE3, 0xFA, 0x7E, 0x7B, 0xFA, 0x43, 0x74,
  31254. 0x8D, 0x64, 0x07, 0xD7, 0x07, 0x85, 0x4D, 0x49,
  31255. 0xBC, 0x83, 0xF5, 0xD4, 0x95, 0x3E, 0x3E, 0x09,
  31256. 0x65, 0xF3, 0xFC, 0x88, 0xA7, 0xF0, 0x46, 0x61,
  31257. 0x44, 0x7D, 0x76, 0xED, 0xC9, 0x8D, 0x0F, 0x8D,
  31258. 0xDA, 0x0D, 0x01, 0xC8, 0xB1, 0xA8, 0x9B, 0x4A,
  31259. 0xF0, 0xA3, 0x88, 0x54, 0xC1, 0xD6, 0x52, 0x97
  31260. };
  31261. #endif /* WOLFSSL_NO_ML_DSA_65 */
  31262. #ifndef WOLFSSL_NO_ML_DSA_87
  31263. static const byte seed_87[] = {
  31264. 0x22, 0x5F, 0x77, 0x07, 0x5E, 0x66, 0xCE, 0x1C,
  31265. 0x99, 0xBA, 0x95, 0xB4, 0xFC, 0xDF, 0x25, 0x8B,
  31266. 0xBB, 0x6F, 0xA5, 0xFE, 0x9C, 0x34, 0x9F, 0x0F,
  31267. 0xDE, 0x3F, 0x71, 0xD5, 0x33, 0x9F, 0x6F, 0xD8
  31268. };
  31269. static const byte pk_87[] = {
  31270. 0x8C, 0x52, 0x4B, 0xD9, 0xAC, 0x48, 0x5C, 0xC6,
  31271. 0x9A, 0xA0, 0x75, 0x64, 0xE1, 0x4F, 0x0F, 0x60,
  31272. 0x13, 0x0E, 0xDE, 0x34, 0x08, 0xA5, 0xD4, 0x81,
  31273. 0xFD, 0x76, 0xC2, 0x51, 0x74, 0x75, 0xA8, 0xFB,
  31274. 0x24, 0xBF, 0x9E, 0x97, 0x9C, 0xD2, 0x3E, 0xDA,
  31275. 0x8A, 0x1B, 0xB6, 0x76, 0xDA, 0x7D, 0x7F, 0x44,
  31276. 0xAD, 0x6B, 0xB9, 0xB0, 0x70, 0xD3, 0xD6, 0x44,
  31277. 0x7F, 0xBE, 0x6C, 0x0C, 0x71, 0x37, 0xC6, 0xFB,
  31278. 0x7B, 0x39, 0x83, 0x63, 0x9C, 0x41, 0x5C, 0xF2,
  31279. 0xC9, 0x15, 0xFF, 0xD4, 0x18, 0xEA, 0xA1, 0x4D,
  31280. 0xA9, 0xD1, 0xAD, 0x3C, 0x09, 0x8E, 0xA9, 0x05,
  31281. 0x34, 0x6C, 0xAA, 0x75, 0x78, 0xF8, 0x6B, 0x6E,
  31282. 0x52, 0xE6, 0x57, 0x55, 0x16, 0xF4, 0x92, 0x3E,
  31283. 0x74, 0x3F, 0x96, 0xA3, 0x2A, 0xD0, 0x0E, 0xEE,
  31284. 0xA1, 0xCE, 0x8A, 0x33, 0xF4, 0x87, 0xB9, 0xF3,
  31285. 0x22, 0x5D, 0x2D, 0x84, 0xCD, 0x27, 0x57, 0xCC,
  31286. 0xCF, 0xE6, 0xA3, 0x66, 0x24, 0x53, 0x0E, 0x52,
  31287. 0x8A, 0x2F, 0x64, 0xFC, 0xE7, 0x04, 0xE7, 0xA7,
  31288. 0x6C, 0x2E, 0x6A, 0xDC, 0x00, 0xEF, 0x9B, 0xEC,
  31289. 0x91, 0x07, 0xB9, 0x69, 0x8F, 0x11, 0x59, 0xFC,
  31290. 0x52, 0xEF, 0x4C, 0x36, 0x5A, 0xFD, 0xB1, 0x50,
  31291. 0xED, 0xC3, 0x43, 0x5E, 0x03, 0xBB, 0x70, 0x26,
  31292. 0x00, 0x6E, 0x5A, 0x55, 0x13, 0x51, 0xA4, 0xB1,
  31293. 0x5F, 0xB8, 0x9F, 0xD2, 0xE9, 0x98, 0x38, 0xE8,
  31294. 0xCF, 0x41, 0x73, 0xFD, 0x0D, 0xF1, 0xF6, 0x80,
  31295. 0x89, 0xE1, 0x51, 0x8D, 0xD4, 0xB5, 0x79, 0x27,
  31296. 0x76, 0xBD, 0xD9, 0x2F, 0xC7, 0xC7, 0x9B, 0xC7,
  31297. 0x99, 0x7F, 0x78, 0x84, 0xD2, 0xB8, 0x80, 0xC5,
  31298. 0xD2, 0xB7, 0xEE, 0xC8, 0x0A, 0xFE, 0x35, 0x59,
  31299. 0x84, 0x5D, 0x39, 0x08, 0x39, 0xBE, 0x5E, 0xBF,
  31300. 0x95, 0x93, 0xA7, 0x3E, 0xD0, 0x1E, 0xF6, 0x7D,
  31301. 0x50, 0x3F, 0xFB, 0x74, 0x47, 0x04, 0xA2, 0xDC,
  31302. 0x49, 0x48, 0x76, 0x2B, 0xC8, 0x43, 0x45, 0x75,
  31303. 0x72, 0x84, 0x4D, 0x15, 0x74, 0xE3, 0xEB, 0x37,
  31304. 0x83, 0x0A, 0x3B, 0x7C, 0xD4, 0x02, 0xC7, 0x6E,
  31305. 0xD5, 0xB4, 0xFC, 0x15, 0xF0, 0x5E, 0x76, 0x03,
  31306. 0x4C, 0xBB, 0x6A, 0x29, 0xDE, 0xBC, 0x7E, 0x2B,
  31307. 0x34, 0xB2, 0x14, 0x2A, 0x57, 0xCF, 0x1B, 0x39,
  31308. 0x73, 0xE5, 0x8B, 0xFF, 0x47, 0x50, 0x42, 0xDC,
  31309. 0x22, 0x6C, 0x7E, 0x13, 0x71, 0xF3, 0x37, 0x51,
  31310. 0x40, 0xF2, 0x90, 0x57, 0xAC, 0xB4, 0x64, 0x7C,
  31311. 0x5F, 0x92, 0x6D, 0x3F, 0xDC, 0xCC, 0xC8, 0xD2,
  31312. 0xE1, 0x6B, 0x81, 0xA9, 0xED, 0xCD, 0x0C, 0x8B,
  31313. 0x5B, 0x2E, 0x11, 0x89, 0x87, 0x42, 0x4B, 0xEC,
  31314. 0xAD, 0x40, 0xA5, 0xE5, 0xB4, 0x6D, 0x1C, 0xB4,
  31315. 0x01, 0x0A, 0x8E, 0x9F, 0x6F, 0x25, 0x92, 0x5D,
  31316. 0xFE, 0x6B, 0x6F, 0x24, 0x64, 0x5F, 0x9C, 0x88,
  31317. 0x86, 0x96, 0xE8, 0x79, 0x64, 0x5B, 0x6A, 0x3A,
  31318. 0x76, 0x21, 0x90, 0xCC, 0xB7, 0xD6, 0x26, 0x9D,
  31319. 0x35, 0x54, 0x79, 0xDF, 0x71, 0x90, 0x55, 0x2A,
  31320. 0x38, 0x52, 0xD1, 0xE9, 0x56, 0x73, 0xE7, 0x19,
  31321. 0x44, 0x6A, 0xD3, 0x10, 0x24, 0xB9, 0x4B, 0xF8,
  31322. 0xBB, 0xC9, 0x7B, 0x04, 0x66, 0x39, 0xCE, 0x12,
  31323. 0x3F, 0xDE, 0xC3, 0x75, 0xAF, 0x9F, 0x8D, 0x4C,
  31324. 0xF7, 0x16, 0x9B, 0xEB, 0x5F, 0xE5, 0x1B, 0xBF,
  31325. 0x82, 0x2C, 0x53, 0xBA, 0x2D, 0x98, 0xA4, 0xA0,
  31326. 0x14, 0xA2, 0xDE, 0x69, 0x7F, 0x03, 0x3C, 0x9E,
  31327. 0x4A, 0x57, 0xC6, 0xED, 0xF6, 0x10, 0x6A, 0x76,
  31328. 0x2A, 0x81, 0x92, 0x9F, 0x3E, 0xF0, 0xFD, 0xE9,
  31329. 0xB7, 0xB3, 0x8A, 0xF6, 0x1A, 0x19, 0x9A, 0x16,
  31330. 0x0F, 0x09, 0x45, 0xBD, 0xBB, 0x96, 0x7C, 0x72,
  31331. 0x40, 0xFE, 0x94, 0xBD, 0xE1, 0x60, 0x50, 0x53,
  31332. 0x13, 0xC9, 0x2B, 0xFA, 0x52, 0x40, 0xA2, 0xA7,
  31333. 0xF0, 0x8C, 0x85, 0x78, 0xDB, 0xD6, 0x7F, 0x21,
  31334. 0x39, 0xB5, 0x06, 0x72, 0xEE, 0x99, 0xA1, 0xBD,
  31335. 0x78, 0x1F, 0xA4, 0xE9, 0x54, 0xF4, 0xFA, 0xDF,
  31336. 0xA7, 0x9E, 0xDD, 0x8E, 0xB1, 0xCF, 0xA8, 0x48,
  31337. 0x84, 0x5D, 0x70, 0xCB, 0x2D, 0xA9, 0x66, 0x09,
  31338. 0x0B, 0x75, 0x75, 0xA2, 0x32, 0xFE, 0xDF, 0x96,
  31339. 0x33, 0x84, 0xA7, 0x84, 0x48, 0x1A, 0xFA, 0x82,
  31340. 0x79, 0x0A, 0x87, 0xE1, 0x1F, 0x11, 0x74, 0xD4,
  31341. 0x3C, 0xC0, 0x8D, 0x4F, 0xD2, 0x5D, 0xBB, 0x40,
  31342. 0x10, 0xB2, 0x6F, 0x23, 0xD2, 0xD6, 0xF4, 0xA5,
  31343. 0x87, 0xEF, 0x7D, 0xE8, 0xC6, 0xF7, 0xC6, 0x0F,
  31344. 0xF9, 0x6F, 0xF8, 0x4C, 0x39, 0xE4, 0x82, 0x1E,
  31345. 0x1E, 0x6A, 0x80, 0x2F, 0xEC, 0x22, 0xD6, 0xA0,
  31346. 0xAA, 0xB6, 0x2C, 0xCB, 0x16, 0x43, 0x68, 0xC2,
  31347. 0x27, 0xF6, 0xA2, 0x31, 0x62, 0x66, 0xEC, 0x2F,
  31348. 0xFF, 0x8D, 0xB4, 0x19, 0x51, 0x19, 0xA0, 0x8C,
  31349. 0x67, 0xE2, 0x04, 0x04, 0xB9, 0x1F, 0x08, 0x70,
  31350. 0x9E, 0xAA, 0xC2, 0xDE, 0xCB, 0x96, 0x19, 0x8F,
  31351. 0x02, 0x74, 0x10, 0xCC, 0x1B, 0x82, 0x5D, 0x9C,
  31352. 0x07, 0x00, 0xE5, 0xD7, 0x04, 0x51, 0xBA, 0x7F,
  31353. 0x67, 0xF9, 0x64, 0x0C, 0xA3, 0x6B, 0xF3, 0x12,
  31354. 0x21, 0x80, 0x68, 0xD6, 0xA2, 0xCA, 0xFF, 0x59,
  31355. 0x33, 0x43, 0x7D, 0x67, 0xBF, 0xD4, 0x88, 0x4A,
  31356. 0x6E, 0x92, 0xBA, 0x41, 0xE1, 0x28, 0xDA, 0xEB,
  31357. 0xE1, 0xEA, 0x25, 0x60, 0xE1, 0x2F, 0xED, 0x2C,
  31358. 0xD4, 0x4B, 0xC9, 0x4E, 0x9E, 0x9D, 0xFA, 0xBB,
  31359. 0xF9, 0x61, 0x41, 0x4C, 0x24, 0x24, 0xFC, 0x9B,
  31360. 0x62, 0xFE, 0x73, 0x74, 0xF6, 0xB8, 0x9B, 0xA9,
  31361. 0x02, 0x96, 0xF4, 0x90, 0x18, 0xA7, 0xF5, 0x49,
  31362. 0xC1, 0xA3, 0x94, 0xB8, 0xED, 0xBD, 0x0B, 0xF3,
  31363. 0xDB, 0xF3, 0xBC, 0x10, 0x6A, 0x6B, 0x3F, 0x79,
  31364. 0x07, 0xF2, 0x11, 0x09, 0xD5, 0x42, 0x8F, 0xA9,
  31365. 0x09, 0x94, 0xBE, 0xF2, 0x0D, 0x3A, 0x91, 0x33,
  31366. 0x01, 0x31, 0x34, 0xBF, 0x0A, 0xCA, 0xF1, 0x3E,
  31367. 0x66, 0x18, 0xA6, 0x69, 0xEC, 0xEA, 0xC5, 0xE9,
  31368. 0x8B, 0x80, 0xFE, 0x4D, 0x93, 0x7B, 0xD4, 0xE5,
  31369. 0x74, 0x90, 0xFA, 0xFD, 0xCE, 0x45, 0xE8, 0xD7,
  31370. 0xD8, 0x8F, 0x08, 0x8B, 0x3A, 0xA8, 0x01, 0xA2,
  31371. 0xB4, 0xE5, 0xF2, 0x29, 0x41, 0x02, 0xBD, 0xCB,
  31372. 0xF9, 0x4A, 0x62, 0x54, 0x99, 0x94, 0x61, 0xB7,
  31373. 0x8F, 0xA5, 0x8A, 0x7F, 0xDC, 0xAD, 0xD2, 0xF2,
  31374. 0x28, 0x1E, 0xF3, 0x18, 0xAE, 0x21, 0x81, 0xF7,
  31375. 0xE9, 0xE5, 0xBF, 0x2B, 0xC2, 0x98, 0x24, 0xB1,
  31376. 0x45, 0x56, 0x57, 0x31, 0xA1, 0x48, 0xAB, 0x39,
  31377. 0xC2, 0x04, 0x29, 0x1B, 0x5B, 0xD3, 0x23, 0x35,
  31378. 0xCC, 0x5A, 0x58, 0x10, 0x11, 0x5B, 0xD5, 0x88,
  31379. 0xC2, 0x60, 0x37, 0x3D, 0x1C, 0x1C, 0x7B, 0x09,
  31380. 0x95, 0xB5, 0x05, 0x12, 0xD8, 0x52, 0x8D, 0xF5,
  31381. 0xBD, 0x4A, 0xA5, 0x45, 0x6F, 0x3D, 0x55, 0x9D,
  31382. 0x90, 0xAD, 0xD7, 0xA9, 0xD0, 0x25, 0x0B, 0xD7,
  31383. 0x55, 0x11, 0x5C, 0x60, 0xBF, 0xBD, 0xFB, 0x9D,
  31384. 0x2A, 0xCE, 0x4F, 0xE6, 0xB8, 0x36, 0x3A, 0x4D,
  31385. 0xE7, 0xB6, 0xFF, 0x6B, 0xD8, 0xBA, 0xD4, 0xEE,
  31386. 0x95, 0x9A, 0x0A, 0x47, 0xD4, 0x76, 0xE0, 0xF7,
  31387. 0xAC, 0x02, 0xB6, 0xA8, 0x10, 0x1E, 0xA5, 0x98,
  31388. 0xC0, 0xF4, 0x68, 0x5E, 0x55, 0xC1, 0x67, 0xCD,
  31389. 0x16, 0x31, 0xBD, 0xA2, 0x86, 0xF3, 0xF8, 0xC0,
  31390. 0xED, 0x4A, 0xFF, 0xE8, 0xF5, 0x2C, 0xFA, 0xD2,
  31391. 0x06, 0x78, 0x6D, 0x34, 0xBE, 0xF9, 0x15, 0x84,
  31392. 0x6D, 0xE5, 0x5F, 0xA4, 0xAC, 0x84, 0x3B, 0x3A,
  31393. 0xA6, 0x2D, 0xC2, 0x01, 0xE0, 0x63, 0x92, 0xC7,
  31394. 0x77, 0xB5, 0x4E, 0x2C, 0x40, 0x90, 0x48, 0xAF,
  31395. 0x8B, 0xE9, 0x6C, 0x1E, 0xEE, 0x16, 0x8F, 0x4E,
  31396. 0x4F, 0xFF, 0x35, 0x15, 0xE5, 0x51, 0xF4, 0xB2,
  31397. 0x23, 0x1C, 0x6A, 0xCE, 0x05, 0xDC, 0xDC, 0xAD,
  31398. 0x7F, 0x9D, 0xDA, 0xB3, 0x0C, 0xAD, 0x9C, 0x62,
  31399. 0x68, 0xD6, 0x84, 0x00, 0x76, 0xFF, 0xD3, 0x01,
  31400. 0x18, 0xB0, 0xC4, 0xE5, 0xE5, 0x0D, 0x87, 0x8E,
  31401. 0xAF, 0x77, 0xEE, 0xCB, 0x56, 0x88, 0x7F, 0xED,
  31402. 0xC5, 0x7C, 0x54, 0xD6, 0x28, 0x46, 0xE0, 0x8C,
  31403. 0xE6, 0x87, 0xF2, 0x4D, 0x0D, 0x2F, 0x12, 0x62,
  31404. 0x06, 0xDF, 0xB2, 0x4E, 0x03, 0x04, 0x78, 0x0B,
  31405. 0x03, 0x4C, 0xCE, 0x86, 0xD1, 0xCD, 0x53, 0x00,
  31406. 0xED, 0xC6, 0xF8, 0x9A, 0xCB, 0x59, 0x14, 0xA6,
  31407. 0x0C, 0x87, 0x35, 0x92, 0x66, 0x0D, 0x02, 0xA9,
  31408. 0xEF, 0x0D, 0x7D, 0xC6, 0x45, 0xF3, 0x11, 0xEF,
  31409. 0x1F, 0x55, 0x72, 0x1F, 0x1B, 0x45, 0xD2, 0xE4,
  31410. 0x8F, 0x3F, 0x9F, 0xEB, 0x27, 0x02, 0xD8, 0x2C,
  31411. 0xEF, 0xAD, 0x7E, 0x7E, 0x10, 0xDD, 0x91, 0x5E,
  31412. 0x39, 0x06, 0x7C, 0x39, 0xEA, 0x61, 0xB9, 0xCC,
  31413. 0xF1, 0x45, 0x56, 0x81, 0x53, 0x55, 0x42, 0xD4,
  31414. 0x37, 0x0F, 0x53, 0xF0, 0x7F, 0xA0, 0xC6, 0x50,
  31415. 0x9B, 0x1D, 0xC6, 0x7E, 0x9F, 0x1D, 0x89, 0x3B,
  31416. 0xEB, 0x85, 0x59, 0x6D, 0x9C, 0x12, 0xEE, 0xAC,
  31417. 0xFC, 0xAE, 0xC0, 0xAE, 0x5F, 0xD4, 0x9C, 0x62,
  31418. 0xE7, 0x09, 0x8C, 0xFA, 0x80, 0x1A, 0x19, 0x09,
  31419. 0x0F, 0x8D, 0x68, 0x9E, 0x45, 0x33, 0xE2, 0x58,
  31420. 0x7B, 0xEF, 0xC7, 0x6A, 0xDC, 0x38, 0x33, 0x3E,
  31421. 0x5C, 0x53, 0xB5, 0x99, 0xDB, 0x04, 0xA7, 0xEA,
  31422. 0xFB, 0x07, 0x9B, 0x25, 0x47, 0xED, 0xAC, 0x5A,
  31423. 0xAA, 0x1E, 0xE5, 0x23, 0xDE, 0x64, 0xE5, 0x87,
  31424. 0x46, 0x8C, 0x41, 0x52, 0xC9, 0x4F, 0x90, 0x48,
  31425. 0x1C, 0xAA, 0xA6, 0xB0, 0x3A, 0x1E, 0xC9, 0x08,
  31426. 0xF7, 0x82, 0x71, 0x13, 0x76, 0x6B, 0x9E, 0x52,
  31427. 0x22, 0x32, 0xE0, 0xC6, 0xF7, 0xD7, 0x4C, 0xBD,
  31428. 0xC3, 0x1C, 0x18, 0xAF, 0xA0, 0x12, 0xD3, 0x22,
  31429. 0x6A, 0xFC, 0x71, 0x8A, 0x64, 0x24, 0xAC, 0x19,
  31430. 0x4E, 0x85, 0x3C, 0x51, 0xE6, 0xA3, 0xAD, 0xA9,
  31431. 0x59, 0x94, 0xD2, 0x7F, 0xC4, 0x9D, 0x93, 0x5B,
  31432. 0x51, 0xD7, 0xF3, 0x03, 0xE7, 0x7D, 0x5B, 0x13,
  31433. 0x0E, 0xCD, 0x7D, 0x0F, 0x77, 0x3E, 0x84, 0xD7,
  31434. 0x4E, 0x69, 0x57, 0x1B, 0x73, 0x99, 0xC9, 0x4D,
  31435. 0xC0, 0x19, 0x6B, 0x9D, 0x5F, 0xBA, 0x69, 0xEE,
  31436. 0x11, 0xBD, 0x7C, 0x45, 0xD9, 0xA9, 0x65, 0x88,
  31437. 0xA7, 0x0E, 0x16, 0xBF, 0xB3, 0x82, 0x5E, 0x5E,
  31438. 0x56, 0x13, 0x02, 0x7D, 0xB1, 0xDC, 0xF5, 0x4A,
  31439. 0x82, 0x73, 0x72, 0x35, 0x9B, 0x91, 0xAC, 0x04,
  31440. 0x69, 0xE9, 0xEA, 0x19, 0xC9, 0xD8, 0x59, 0xEB,
  31441. 0x8F, 0x22, 0x5F, 0x43, 0x11, 0x0C, 0xCF, 0xB4,
  31442. 0x16, 0x6C, 0x7D, 0x60, 0xCE, 0x14, 0x24, 0xAD,
  31443. 0xD7, 0x07, 0xC2, 0x4E, 0x98, 0xA0, 0xDE, 0x9E,
  31444. 0xE6, 0x31, 0xED, 0xF8, 0x5B, 0x9C, 0xAF, 0xF7,
  31445. 0x57, 0x59, 0x10, 0xA9, 0x92, 0xDC, 0x4F, 0x0C,
  31446. 0x2B, 0x88, 0x75, 0x19, 0x1D, 0xB3, 0xBF, 0x70,
  31447. 0x23, 0x17, 0xD5, 0x1A, 0x50, 0x30, 0x18, 0x14,
  31448. 0x1A, 0x14, 0xE6, 0x1D, 0x4F, 0x8A, 0x96, 0x3E,
  31449. 0xD8, 0x6E, 0xD9, 0xBF, 0x94, 0x4E, 0xDE, 0xB8,
  31450. 0xFF, 0xE1, 0x6F, 0xFD, 0x31, 0xE8, 0xFE, 0x43,
  31451. 0xC2, 0x40, 0x82, 0x45, 0x50, 0xFE, 0x1B, 0xBC,
  31452. 0x77, 0x4B, 0xB4, 0x30, 0xA7, 0xD4, 0x46, 0x32,
  31453. 0x6A, 0xF7, 0xC5, 0x92, 0xDA, 0x70, 0xB1, 0xB7,
  31454. 0xA1, 0x5A, 0x5D, 0x17, 0x3B, 0xDB, 0x2F, 0x28,
  31455. 0x8A, 0x6E, 0xEC, 0xDA, 0xC4, 0xF7, 0x2E, 0xCB,
  31456. 0xEB, 0x96, 0x60, 0x92, 0x1B, 0xDD, 0xD6, 0x13,
  31457. 0x7C, 0x85, 0x9F, 0x8A, 0x9A, 0xE9, 0x5F, 0xC4,
  31458. 0x24, 0xFD, 0x33, 0xDF, 0xB3, 0x98, 0x66, 0xF7,
  31459. 0xA1, 0x5A, 0xDC, 0x01, 0xC9, 0xFA, 0x37, 0xF1,
  31460. 0x7B, 0xD0, 0xF6, 0x66, 0x8A, 0x26, 0x7C, 0xC2,
  31461. 0x1B, 0xFF, 0x62, 0xBC, 0xFD, 0xCD, 0x47, 0xDA,
  31462. 0xEE, 0x75, 0xF2, 0xAC, 0x60, 0x69, 0x87, 0x26,
  31463. 0xCC, 0x92, 0x10, 0x1C, 0x92, 0xC1, 0x43, 0x09,
  31464. 0xE9, 0xCE, 0x7D, 0x05, 0x5C, 0x64, 0x55, 0xCB,
  31465. 0xBB, 0x7A, 0xAE, 0x05, 0xDB, 0x38, 0xD3, 0xD5,
  31466. 0xBB, 0xD9, 0x9F, 0xCB, 0xCF, 0xB7, 0x9C, 0xEF,
  31467. 0x7E, 0x7B, 0x2A, 0x6F, 0x84, 0x4E, 0x6A, 0x7F,
  31468. 0xD3, 0x5F, 0xF3, 0xB3, 0xC1, 0xF0, 0x02, 0x9C,
  31469. 0xA2, 0x4C, 0x86, 0x0E, 0x6B, 0xE2, 0x2B, 0x1D,
  31470. 0x1D, 0xB4, 0x55, 0x7F, 0x85, 0x54, 0x2D, 0x85,
  31471. 0x64, 0x89, 0x92, 0x19, 0x65, 0x44, 0xD7, 0x95,
  31472. 0x48, 0x2C, 0x46, 0x8D, 0x0E, 0xBA, 0xFB, 0x13,
  31473. 0x63, 0x52, 0x2E, 0x22, 0x19, 0x3F, 0x7F, 0xFB,
  31474. 0x54, 0x4D, 0x73, 0xA1, 0x3C, 0x22, 0xD6, 0x5D,
  31475. 0x2B, 0x4A, 0xBD, 0xD7, 0xBB, 0x72, 0x55, 0x80,
  31476. 0xD4, 0x57, 0x4E, 0xDC, 0xF2, 0x8B, 0xB3, 0x09,
  31477. 0x6A, 0xF9, 0x1A, 0xD3, 0x41, 0x0E, 0x72, 0x95,
  31478. 0x49, 0xE7, 0xD1, 0xDC, 0x05, 0x22, 0xC3, 0x3E,
  31479. 0x26, 0x95, 0x00, 0x01, 0x8C, 0xE1, 0x54, 0x47,
  31480. 0x84, 0x10, 0xA7, 0x67, 0x45, 0xBB, 0xB9, 0x7B,
  31481. 0x0B, 0xB4, 0x74, 0x82, 0xED, 0x6C, 0x26, 0x6E,
  31482. 0xF2, 0x56, 0xCA, 0x1A, 0xD1, 0x10, 0x68, 0x40,
  31483. 0x28, 0x23, 0xD5, 0x98, 0xB3, 0x6B, 0x75, 0x16,
  31484. 0x13, 0x87, 0xE1, 0xF2, 0x3F, 0xAB, 0xC0, 0x2A,
  31485. 0xF0, 0x16, 0x59, 0x85, 0x1A, 0x5B, 0x41, 0xB7,
  31486. 0x52, 0xB1, 0x79, 0x46, 0x20, 0xDF, 0x59, 0xFB,
  31487. 0x33, 0xB3, 0x05, 0xF1, 0x12, 0x8B, 0xDB, 0x7C,
  31488. 0x51, 0x90, 0xC9, 0x8A, 0xC9, 0x48, 0x10, 0x54,
  31489. 0xF4, 0x0F, 0x88, 0x1D, 0xDB, 0x40, 0x1B, 0x3A,
  31490. 0xD7, 0x62, 0xD1, 0x75, 0x73, 0xD6, 0xCA, 0x23,
  31491. 0x26, 0xB2, 0xBF, 0x4C, 0xCA, 0x22, 0xDD, 0xF6,
  31492. 0xAF, 0x22, 0xB8, 0x4F, 0xC2, 0xC3, 0xB3, 0xD3,
  31493. 0xED, 0xFA, 0xBA, 0x2E, 0x38, 0x28, 0x6A, 0xAE,
  31494. 0x60, 0xE9, 0x2D, 0x11, 0x33, 0xED, 0x7E, 0xE9,
  31495. 0x29, 0x8E, 0x01, 0xB0, 0x0F, 0x13, 0x83, 0x44,
  31496. 0x17, 0xFA, 0xB6, 0x54, 0x7C, 0xAC, 0x1F, 0xED,
  31497. 0xC9, 0x22, 0xF2, 0x4F, 0x69, 0x24, 0x04, 0xFE,
  31498. 0xC2, 0x6A, 0xEB, 0xB0, 0xE4, 0xF5, 0x03, 0xCB,
  31499. 0xB3, 0x99, 0x50, 0x66, 0x1F, 0x6B, 0xF3, 0xFE,
  31500. 0xB7, 0xBF, 0x8D, 0xBA, 0x59, 0x75, 0x75, 0x51,
  31501. 0xB0, 0xA5, 0xB9, 0x66, 0xC8, 0xDD, 0x35, 0xAE,
  31502. 0x20, 0x66, 0x21, 0x9B, 0x04, 0x3F, 0xC6, 0x90,
  31503. 0x6F, 0x2B, 0x5C, 0x78, 0x49, 0x3C, 0x40, 0xE6,
  31504. 0xF9, 0x6B, 0x1A, 0xEF, 0xCE, 0x5A, 0xC1, 0x68,
  31505. 0xD3, 0x34, 0x05, 0xD0, 0x21, 0x6C, 0xF8, 0xA8,
  31506. 0x55, 0xE4, 0x6E, 0x80, 0x9B, 0xAD, 0xA5, 0xC3,
  31507. 0x55, 0x0B, 0x28, 0xBB, 0x54, 0x02, 0xD4, 0xF6,
  31508. 0x82, 0x73, 0xAB, 0x56, 0x0B, 0xB1, 0x5F, 0x94,
  31509. 0xC3, 0xDA, 0x24, 0x1E, 0x7F, 0x62, 0x6B, 0x98,
  31510. 0x6B, 0x2A, 0xF3, 0x92, 0x37, 0x3A, 0xB9, 0xE6,
  31511. 0x27, 0xC4, 0xBB, 0xAB, 0xE4, 0x9A, 0x60, 0xD2,
  31512. 0xAE, 0xCE, 0xFD, 0x44, 0xEB, 0x1C, 0xCF, 0x74,
  31513. 0x54, 0xFC, 0xEC, 0x4F, 0xC2, 0xBA, 0xF4, 0x3B,
  31514. 0xAC, 0x03, 0xC7, 0x2E, 0xE6, 0x62, 0x44, 0x61,
  31515. 0x42, 0xC8, 0xAE, 0xF1, 0xB2, 0xA9, 0xAC, 0xE0,
  31516. 0xCE, 0x23, 0xAF, 0xCC, 0x86, 0x61, 0xFE, 0xC5,
  31517. 0xCB, 0xAC, 0x4A, 0x1B, 0x5C, 0xC7, 0x2B, 0xFF,
  31518. 0x8A, 0x20, 0x62, 0x0E, 0xB9, 0x1D, 0xDD, 0x93,
  31519. 0x19, 0x29, 0xE4, 0xD9, 0x13, 0x1D, 0x28, 0x32,
  31520. 0x03, 0x5A, 0xA6, 0x8E, 0x20, 0xC7, 0xD6, 0xC6,
  31521. 0x4D, 0x19, 0x17, 0xCC, 0x65, 0xB8, 0x84, 0x0C,
  31522. 0x38, 0xB4, 0xA9, 0x45, 0x2B, 0x91, 0x61, 0x79,
  31523. 0x87, 0x08, 0xA6, 0xBD, 0x28, 0x9A, 0x58, 0x48,
  31524. 0xD5, 0x58, 0xC6, 0xCE, 0xC2, 0xC5, 0x72, 0x16,
  31525. 0xD9, 0xF4, 0xED, 0x66, 0xAC, 0xFA, 0x93, 0xE8,
  31526. 0x26, 0x10, 0x3B, 0x3D, 0x8F, 0xEA, 0x51, 0xCC,
  31527. 0x82, 0xC0, 0xDB, 0xDF, 0xA7, 0x13, 0xFB, 0x1B,
  31528. 0x77, 0x7E, 0x6F, 0x9E, 0x3C, 0xC5, 0x86, 0x35,
  31529. 0x92, 0x5B, 0x6F, 0x76, 0xA1, 0x71, 0x0D, 0x8C,
  31530. 0xDC, 0x95, 0x9F, 0xAC, 0x2C, 0x8E, 0x21, 0x01,
  31531. 0x37, 0x06, 0x28, 0x64, 0x4C, 0x23, 0xE2, 0x75,
  31532. 0x0B, 0xA7, 0xA4, 0xF5, 0x90, 0x87, 0xD2, 0x43,
  31533. 0x71, 0x59, 0x7C, 0x8C, 0xCA, 0x77, 0x3B, 0xC5,
  31534. 0x36, 0x46, 0xF7, 0x2F, 0xD3, 0x47, 0x18, 0xD7,
  31535. 0xC9, 0x4E, 0x56, 0x2D, 0x49, 0x82, 0xAC, 0x7D,
  31536. 0xD7, 0x3D, 0xF1, 0xDD, 0x73, 0x8B, 0xE4, 0xA1,
  31537. 0x10, 0x85, 0xB6, 0x94, 0xBE, 0x6A, 0x5E, 0xEE,
  31538. 0xBD, 0x60, 0xEB, 0x95, 0x76, 0xA8, 0x52, 0xE1,
  31539. 0x47, 0x57, 0xA1, 0x9C, 0xEC, 0x44, 0xE5, 0x6F,
  31540. 0x68, 0x34, 0x7E, 0x19, 0xBE, 0xCE, 0x56, 0xC9,
  31541. 0xBE, 0xCE, 0xFC, 0xB8, 0x32, 0x6D, 0xCB, 0x84,
  31542. 0x59, 0xBF, 0x4D, 0xF6, 0xE1, 0x53, 0x41, 0x61,
  31543. 0x5C, 0xFB, 0xD2, 0x48, 0xA6, 0x7F, 0x05, 0xB2,
  31544. 0xFC, 0xE8, 0xB2, 0x8A, 0x55, 0x7D, 0x19, 0xC0,
  31545. 0x69, 0x3B, 0x91, 0x5D, 0x71, 0xE7, 0xBB, 0x72,
  31546. 0x7D, 0xB9, 0x64, 0x6E, 0x8B, 0x5B, 0x70, 0x51,
  31547. 0xB5, 0x69, 0x8C, 0xC0, 0xFC, 0x95, 0xB2, 0x43,
  31548. 0x08, 0xF8, 0x70, 0xE4, 0x6F, 0x87, 0xA7, 0xDF,
  31549. 0x23, 0x84, 0xEE, 0xCF, 0x73, 0x38, 0xDE, 0x99,
  31550. 0x4C, 0xF8, 0xF1, 0x2D, 0xA2, 0x68, 0x99, 0xE3,
  31551. 0x9B, 0xB8, 0xF6, 0xC1, 0x5C, 0x83, 0x07, 0xE9,
  31552. 0xB9, 0xE2, 0x51, 0x62, 0xC8, 0x53, 0xF1, 0xC2,
  31553. 0xF7, 0x57, 0x8A, 0xA0, 0x42, 0x3C, 0x18, 0x36,
  31554. 0xF3, 0x99, 0xFD, 0x34, 0xB2, 0xF0, 0x1D, 0xBA,
  31555. 0x43, 0xEA, 0x72, 0x1C, 0x0B, 0x37, 0x47, 0xBC,
  31556. 0xAF, 0xDA, 0x22, 0x1F, 0x1C, 0x08, 0x16, 0x13,
  31557. 0xBD, 0xAA, 0x07, 0xFD, 0x7E, 0xCA, 0x70, 0x57,
  31558. 0x74, 0xDF, 0x68, 0x6B, 0x9F, 0x2D, 0x56, 0xBD,
  31559. 0x21, 0x89, 0xFA, 0x09, 0x04, 0xCA, 0x09, 0xBD,
  31560. 0x4F, 0xE6, 0x15, 0xF5, 0x89, 0xAB, 0xAC, 0xB2,
  31561. 0xC9, 0xBF, 0xC8, 0xBB, 0x87, 0x83, 0xB4, 0xD3,
  31562. 0xDC, 0xB1, 0x25, 0x9B, 0xAE, 0xC5, 0x75, 0x0C,
  31563. 0x9E, 0x6A, 0x83, 0x41, 0x85, 0x9D, 0x4B, 0xBF,
  31564. 0x62, 0x0C, 0x7D, 0x77, 0xC9, 0x89, 0xA6, 0xE1,
  31565. 0x28, 0xBD, 0x13, 0x5D, 0x41, 0x26, 0x80, 0x75,
  31566. 0x23, 0x57, 0xE7, 0x4F, 0x4D, 0x02, 0x8E, 0x0F,
  31567. 0x43, 0x67, 0xF6, 0xA6, 0xE6, 0xB6, 0x84, 0x8D,
  31568. 0xF5, 0x7B, 0x6A, 0x95, 0x73, 0x27, 0x86, 0x02,
  31569. 0x72, 0xCB, 0xDF, 0x77, 0x1C, 0x6C, 0x5E, 0xD3,
  31570. 0xF0, 0x1C, 0x82, 0x7A, 0x0D, 0xBB, 0x70, 0xA3,
  31571. 0x98, 0x8B, 0x7B, 0x4A, 0xFE, 0x2D, 0xB1, 0x5C,
  31572. 0x61, 0x89, 0x34, 0x4C, 0x81, 0x4B, 0x52, 0x17,
  31573. 0x03, 0x81, 0x54, 0x4F, 0x9E, 0x9E, 0x07, 0x16,
  31574. 0xF3, 0xD9, 0x18, 0x01, 0x11, 0xFD, 0x67, 0x18,
  31575. 0xA2, 0x64, 0x35, 0x42, 0x81, 0x80, 0x4A, 0xBA,
  31576. 0xCB, 0xD5, 0xF5, 0x4A, 0x10, 0x7F, 0xE2, 0xCF,
  31577. 0xA5, 0x1E, 0xCB, 0x0C, 0xAB, 0x3E, 0x03, 0x98,
  31578. 0x73, 0x89, 0xA4, 0x10, 0x75, 0xD5, 0xAC, 0x3D,
  31579. 0xCF, 0x56, 0x75, 0xD8, 0x86, 0xC2, 0x21, 0x42,
  31580. 0x99, 0x8D, 0x1B, 0x49, 0x09, 0xFE, 0x86, 0x41,
  31581. 0xC9, 0xDC, 0x87, 0x8D, 0x5A, 0xF0, 0xF5, 0xBE,
  31582. 0xF5, 0x49, 0x64, 0x5A, 0x7A, 0xC3, 0x5D, 0xE4,
  31583. 0xD6, 0xB7, 0x30, 0x92, 0x2A, 0x15, 0x86, 0x02,
  31584. 0xBE, 0xBA, 0x6E, 0xF6, 0x3D, 0x2D, 0x70, 0x89,
  31585. 0xFB, 0xB5, 0x1E, 0xBA, 0xDA, 0x20, 0x12, 0x49,
  31586. 0x22, 0xA0, 0xD8, 0x33, 0x9E, 0x4C, 0xC0, 0x27,
  31587. 0x0F, 0x9C, 0x1F, 0xD2, 0xA9, 0xF4, 0xD2, 0xA9,
  31588. 0x6D, 0xC5, 0x32, 0x16, 0x35, 0x9F, 0x19, 0x88,
  31589. 0xC1, 0xAA, 0xA4, 0x66, 0x33, 0xE6, 0x2C, 0x6A,
  31590. 0x6E, 0xA2, 0x1B, 0x33, 0xCB, 0xC3, 0x7E, 0xC5,
  31591. 0x31, 0x4D, 0x5C, 0x17, 0x4C, 0x33, 0x7F, 0x09,
  31592. 0x01, 0x33, 0x82, 0x84, 0x37, 0x03, 0xEB, 0x0E,
  31593. 0xB1, 0x5F, 0x1B, 0x60, 0x8A, 0x2C, 0x9F, 0x39
  31594. };
  31595. static const byte sk_87[] = {
  31596. 0x8C, 0x52, 0x4B, 0xD9, 0xAC, 0x48, 0x5C, 0xC6,
  31597. 0x9A, 0xA0, 0x75, 0x64, 0xE1, 0x4F, 0x0F, 0x60,
  31598. 0x13, 0x0E, 0xDE, 0x34, 0x08, 0xA5, 0xD4, 0x81,
  31599. 0xFD, 0x76, 0xC2, 0x51, 0x74, 0x75, 0xA8, 0xFB,
  31600. 0x9A, 0xFE, 0xF5, 0x92, 0x58, 0xBB, 0x3C, 0xEB,
  31601. 0x4C, 0x5E, 0x83, 0xF9, 0xFF, 0xBC, 0x3B, 0x49,
  31602. 0xAE, 0xE1, 0xFC, 0x4B, 0x94, 0x4B, 0x8C, 0x75,
  31603. 0xD4, 0x67, 0x75, 0x66, 0x7D, 0x6B, 0xA4, 0xF2,
  31604. 0xDA, 0xC2, 0xB7, 0xC4, 0xD8, 0x50, 0x25, 0xCB,
  31605. 0x5A, 0xDB, 0xA4, 0xAD, 0xBB, 0x44, 0x20, 0x24,
  31606. 0x90, 0xEA, 0xA5, 0x2C, 0xAE, 0x80, 0x22, 0xC9,
  31607. 0x59, 0x02, 0xB7, 0x10, 0xB0, 0x5E, 0x1E, 0x5F,
  31608. 0x52, 0x7D, 0x88, 0xDA, 0xE2, 0x04, 0xBF, 0x45,
  31609. 0xA7, 0xA8, 0x49, 0x97, 0x7D, 0xAD, 0x7C, 0x7C,
  31610. 0x9E, 0x9C, 0x4A, 0xCC, 0x36, 0x33, 0x0F, 0x30,
  31611. 0xFA, 0xDE, 0x52, 0xE9, 0xAE, 0x23, 0x29, 0x13,
  31612. 0x10, 0x17, 0x8A, 0xD0, 0x08, 0x8E, 0xE1, 0x10,
  31613. 0x30, 0xD0, 0x84, 0x65, 0x92, 0x12, 0x2A, 0x81,
  31614. 0x26, 0x2E, 0x11, 0x14, 0x30, 0x61, 0x38, 0x61,
  31615. 0x64, 0x42, 0x05, 0x08, 0x91, 0x90, 0x4C, 0x06,
  31616. 0x82, 0xCC, 0x90, 0x45, 0x10, 0x39, 0x90, 0x22,
  31617. 0x40, 0x2A, 0x9B, 0x16, 0x26, 0x9A, 0xA8, 0x50,
  31618. 0x91, 0x12, 0x70, 0x91, 0x20, 0x4D, 0xC0, 0x34,
  31619. 0x90, 0x18, 0x28, 0x31, 0x10, 0x02, 0x11, 0x22,
  31620. 0xB3, 0x6C, 0x8B, 0xB8, 0x2C, 0x22, 0xB0, 0x69,
  31621. 0x53, 0x36, 0x31, 0x61, 0x42, 0x6C, 0xD9, 0x06,
  31622. 0x6A, 0xD9, 0x04, 0x45, 0xDB, 0x18, 0x05, 0x12,
  31623. 0x37, 0x4A, 0xD4, 0x06, 0x64, 0xD3, 0xA2, 0x85,
  31624. 0xA0, 0x38, 0x8A, 0x14, 0xA5, 0x85, 0x50, 0x20,
  31625. 0x85, 0xE4, 0xA8, 0x24, 0xC3, 0xC6, 0x31, 0xC9,
  31626. 0x34, 0x4E, 0xD2, 0x14, 0x68, 0x82, 0x90, 0x85,
  31627. 0xC4, 0x02, 0x61, 0x24, 0x38, 0x05, 0x01, 0xA3,
  31628. 0x50, 0x48, 0x08, 0x62, 0x20, 0xB0, 0x25, 0x5B,
  31629. 0xA6, 0x4D, 0x98, 0x92, 0x11, 0xC2, 0x06, 0x00,
  31630. 0xD1, 0xB0, 0x4D, 0x21, 0xA4, 0x8C, 0x01, 0x16,
  31631. 0x72, 0x11, 0xA6, 0x20, 0xD0, 0x16, 0x45, 0x10,
  31632. 0x31, 0x8E, 0xCB, 0xC2, 0x69, 0x02, 0x08, 0x91,
  31633. 0xD4, 0x30, 0x89, 0x03, 0x41, 0x05, 0x93, 0x16,
  31634. 0x8E, 0x5A, 0x18, 0x04, 0x41, 0x10, 0x6D, 0x18,
  31635. 0x42, 0x70, 0x53, 0x16, 0x31, 0x52, 0x30, 0x8E,
  31636. 0x0C, 0x49, 0x66, 0x0C, 0x90, 0x0C, 0xA4, 0x08,
  31637. 0x2E, 0x41, 0x92, 0x05, 0x24, 0x07, 0x30, 0x12,
  31638. 0x46, 0x72, 0x13, 0x99, 0x20, 0xE0, 0xA2, 0x4C,
  31639. 0x1B, 0x14, 0x52, 0x5A, 0x90, 0x05, 0x08, 0x82,
  31640. 0x31, 0x53, 0xC2, 0x90, 0xCC, 0x42, 0x68, 0x18,
  31641. 0xB0, 0x2C, 0x00, 0x80, 0x65, 0x58, 0x12, 0x84,
  31642. 0x19, 0x90, 0x08, 0x44, 0x26, 0x4A, 0x10, 0xA9,
  31643. 0x0C, 0x12, 0x25, 0x0C, 0x9C, 0x10, 0x25, 0x0C,
  31644. 0x28, 0x25, 0xD8, 0x46, 0x84, 0x1A, 0x22, 0x71,
  31645. 0x5B, 0x28, 0x6E, 0x98, 0x02, 0x51, 0x61, 0xB4,
  31646. 0x51, 0x01, 0xA1, 0x21, 0x24, 0x39, 0x12, 0xC8,
  31647. 0x08, 0x85, 0xD1, 0x34, 0x64, 0xA4, 0xA8, 0x04,
  31648. 0xA2, 0xC0, 0x09, 0x44, 0x48, 0x48, 0x03, 0x37,
  31649. 0x00, 0x20, 0x05, 0x4D, 0x20, 0xA4, 0x05, 0x11,
  31650. 0x18, 0x82, 0x42, 0x94, 0x4D, 0x24, 0x16, 0x01,
  31651. 0x02, 0x93, 0x4C, 0x00, 0x16, 0x06, 0xC1, 0xC0,
  31652. 0x0C, 0x8B, 0xC0, 0x41, 0x41, 0x06, 0x42, 0xA3,
  31653. 0xC6, 0x64, 0x1A, 0x85, 0x91, 0x41, 0x06, 0x49,
  31654. 0x04, 0xA7, 0x44, 0x82, 0x22, 0x6A, 0x50, 0x08,
  31655. 0x0E, 0x14, 0x18, 0x20, 0x4B, 0x88, 0x91, 0x01,
  31656. 0xA0, 0x49, 0x1A, 0x85, 0x4D, 0x94, 0x18, 0x10,
  31657. 0x0A, 0x05, 0x44, 0x94, 0x38, 0x05, 0x93, 0x40,
  31658. 0x68, 0x23, 0x07, 0x85, 0xE2, 0x12, 0x22, 0x9B,
  31659. 0xB8, 0x08, 0xD2, 0x10, 0x2A, 0x08, 0xA8, 0x10,
  31660. 0x92, 0x40, 0x2D, 0xD8, 0x44, 0x4C, 0xCC, 0x94,
  31661. 0x05, 0x24, 0x43, 0x4C, 0xD3, 0xC2, 0x48, 0x10,
  31662. 0x21, 0x2D, 0xC9, 0xB6, 0x08, 0xC9, 0x06, 0x4D,
  31663. 0xE1, 0x90, 0x20, 0x14, 0x24, 0x70, 0x5C, 0x84,
  31664. 0x28, 0xC0, 0xC2, 0x81, 0x22, 0x13, 0x50, 0x44,
  31665. 0x84, 0x91, 0xCA, 0xA2, 0x48, 0x12, 0x91, 0x05,
  31666. 0x5B, 0x92, 0x8D, 0x92, 0x92, 0x24, 0x82, 0x42,
  31667. 0x48, 0x03, 0x37, 0x46, 0xD8, 0x44, 0x86, 0x44,
  31668. 0x20, 0x89, 0xE4, 0xC2, 0x84, 0xC2, 0x04, 0x65,
  31669. 0x49, 0xA8, 0x4D, 0xA4, 0x38, 0x28, 0xDB, 0xA4,
  31670. 0x64, 0x24, 0x00, 0x51, 0xC8, 0x12, 0x6D, 0x19,
  31671. 0x82, 0x24, 0xCB, 0x00, 0x44, 0x4B, 0x20, 0x20,
  31672. 0x9B, 0x82, 0x4C, 0x5C, 0xA8, 0x08, 0xD2, 0xB6,
  31673. 0x8C, 0x08, 0x35, 0x20, 0xC0, 0x92, 0x45, 0xE3,
  31674. 0xB4, 0x2C, 0x50, 0x32, 0x0E, 0xD1, 0x82, 0x11,
  31675. 0x4A, 0x96, 0x08, 0x1C, 0x86, 0x29, 0x02, 0x19,
  31676. 0x71, 0x12, 0x03, 0x6E, 0x94, 0x08, 0x50, 0x12,
  31677. 0x27, 0x20, 0x0B, 0x10, 0x12, 0xA1, 0x18, 0x06,
  31678. 0x5A, 0x36, 0x4C, 0x93, 0xB4, 0x68, 0x21, 0xA7,
  31679. 0x28, 0x09, 0x34, 0x91, 0x18, 0x93, 0x49, 0x4A,
  31680. 0x32, 0x60, 0x00, 0x29, 0x2D, 0x94, 0x48, 0x44,
  31681. 0x09, 0x94, 0x2C, 0x21, 0x07, 0x6C, 0x41, 0x38,
  31682. 0x60, 0x8C, 0x10, 0x46, 0x11, 0x19, 0x65, 0x01,
  31683. 0x46, 0x60, 0x1A, 0x29, 0x42, 0x23, 0x30, 0x29,
  31684. 0x40, 0x96, 0x85, 0x81, 0xC6, 0x6C, 0x09, 0xA2,
  31685. 0x31, 0x23, 0xC9, 0x84, 0x18, 0x27, 0x61, 0x02,
  31686. 0xA6, 0x05, 0x1B, 0x11, 0x32, 0xD1, 0x80, 0x24,
  31687. 0x59, 0x22, 0x52, 0x21, 0x34, 0x64, 0x0A, 0x21,
  31688. 0x52, 0x10, 0xC2, 0x80, 0x5C, 0x98, 0x0D, 0x81,
  31689. 0xA0, 0x84, 0x14, 0x97, 0x04, 0xCC, 0xC2, 0x04,
  31690. 0x1A, 0x81, 0x45, 0x23, 0x44, 0x6C, 0x13, 0xC0,
  31691. 0x44, 0x59, 0xC2, 0x68, 0x64, 0x08, 0x52, 0x51,
  31692. 0x30, 0x71, 0x12, 0x49, 0x70, 0x12, 0x94, 0x84,
  31693. 0x80, 0x12, 0x12, 0x1B, 0x00, 0x50, 0x84, 0x10,
  31694. 0x45, 0x4A, 0x30, 0x10, 0x22, 0x95, 0x49, 0xC9,
  31695. 0x82, 0x24, 0x03, 0x35, 0x21, 0x18, 0x16, 0x72,
  31696. 0x09, 0x89, 0x65, 0x88, 0xB2, 0x89, 0x41, 0xB4,
  31697. 0x90, 0x92, 0x38, 0x8C, 0x08, 0x23, 0x26, 0x0B,
  31698. 0x80, 0x61, 0x84, 0x28, 0x6A, 0x4C, 0x98, 0x44,
  31699. 0x10, 0xB9, 0x30, 0x93, 0x02, 0x49, 0x22, 0x13,
  31700. 0x80, 0x1C, 0xC3, 0x48, 0x50, 0xA8, 0x20, 0x1C,
  31701. 0x05, 0x00, 0x5B, 0x02, 0x41, 0xD2, 0x84, 0x61,
  31702. 0x4B, 0x40, 0x46, 0x20, 0x21, 0x44, 0xD9, 0xC4,
  31703. 0x21, 0xD3, 0xA4, 0x4D, 0xC0, 0xC0, 0x09, 0x5B,
  31704. 0x28, 0x91, 0x18, 0x15, 0x41, 0x18, 0xC5, 0x4C,
  31705. 0x14, 0xB7, 0x61, 0xDB, 0x34, 0x25, 0x02, 0x06,
  31706. 0x41, 0x14, 0xA9, 0x65, 0x0B, 0x10, 0x04, 0x23,
  31707. 0xC7, 0x49, 0x13, 0x47, 0x0A, 0xD0, 0x30, 0x80,
  31708. 0x99, 0x32, 0x68, 0x50, 0x18, 0x06, 0xA2, 0x28,
  31709. 0x65, 0x13, 0x35, 0x82, 0xD3, 0x06, 0x81, 0x22,
  31710. 0x49, 0x4D, 0x48, 0x44, 0x30, 0xCA, 0x96, 0x2C,
  31711. 0x12, 0xC8, 0x08, 0xA1, 0x24, 0x2C, 0x52, 0xA8,
  31712. 0x28, 0x23, 0x14, 0x0A, 0xD4, 0x20, 0x4D, 0x18,
  31713. 0x12, 0x72, 0xD4, 0x80, 0x44, 0xDC, 0x26, 0x2C,
  31714. 0x88, 0x10, 0x0A, 0x04, 0x14, 0x51, 0xC1, 0x96,
  31715. 0x00, 0xA3, 0x40, 0x30, 0x99, 0x48, 0x92, 0x9B,
  31716. 0x08, 0x86, 0x81, 0x04, 0x20, 0x4C, 0xB2, 0x29,
  31717. 0x18, 0x31, 0x08, 0x09, 0x23, 0x8C, 0x4C, 0x02,
  31718. 0x6A, 0xCA, 0x00, 0x62, 0x09, 0x22, 0x2D, 0x21,
  31719. 0x00, 0x02, 0x0A, 0x39, 0x41, 0x04, 0xA3, 0x50,
  31720. 0x90, 0x80, 0x2D, 0x59, 0xB4, 0x71, 0x13, 0x16,
  31721. 0x31, 0x11, 0x90, 0x4C, 0xC3, 0x14, 0x20, 0x60,
  31722. 0xB2, 0x30, 0x0A, 0xB6, 0x24, 0x21, 0xA9, 0x10,
  31723. 0x89, 0x80, 0x88, 0x44, 0x06, 0x8A, 0x91, 0x22,
  31724. 0x8E, 0xD9, 0x36, 0x86, 0x10, 0x46, 0x0A, 0xE1,
  31725. 0x16, 0x85, 0x42, 0x40, 0x6C, 0x09, 0x49, 0x11,
  31726. 0xE0, 0x88, 0x68, 0x12, 0x08, 0x68, 0x5C, 0x26,
  31727. 0x24, 0x04, 0xA8, 0x70, 0xC8, 0x08, 0x05, 0x13,
  31728. 0x87, 0x41, 0x23, 0x29, 0x72, 0xC9, 0xB8, 0x88,
  31729. 0x1B, 0x22, 0x66, 0x11, 0xA5, 0x2D, 0x11, 0x29,
  31730. 0x12, 0x50, 0x12, 0x70, 0x03, 0x09, 0x6A, 0x4B,
  31731. 0x88, 0x4C, 0xD2, 0xC8, 0x31, 0x40, 0x26, 0x40,
  31732. 0x4C, 0x04, 0x50, 0x58, 0x16, 0x71, 0x90, 0xC2,
  31733. 0x00, 0x0A, 0x30, 0x8A, 0xDC, 0x24, 0x85, 0x19,
  31734. 0xB0, 0x65, 0x1A, 0xA3, 0x64, 0x13, 0xA3, 0x45,
  31735. 0xC8, 0x48, 0x91, 0x91, 0x12, 0x20, 0xDC, 0x42,
  31736. 0x40, 0x24, 0xC0, 0x4D, 0xA3, 0x98, 0x10, 0x40,
  31737. 0x26, 0x25, 0xDC, 0xB4, 0x68, 0x4B, 0xC2, 0x45,
  31738. 0x13, 0x06, 0x91, 0xC8, 0x92, 0x24, 0x82, 0xA8,
  31739. 0x20, 0x4C, 0x30, 0x48, 0x52, 0x06, 0x01, 0x0B,
  31740. 0x24, 0x51, 0x41, 0x36, 0x40, 0x93, 0xC4, 0x70,
  31741. 0x44, 0x40, 0x2C, 0x24, 0x28, 0x22, 0x81, 0xA4,
  31742. 0x4C, 0x43, 0x84, 0x60, 0x20, 0x23, 0x90, 0x01,
  31743. 0x94, 0x6C, 0xDB, 0x28, 0x21, 0x93, 0x30, 0x80,
  31744. 0x93, 0xC0, 0x25, 0xC8, 0xA6, 0x50, 0xCA, 0x24,
  31745. 0x26, 0xD1, 0x40, 0x31, 0x04, 0xC4, 0x8D, 0xE2,
  31746. 0xC0, 0x04, 0x08, 0x33, 0x8C, 0x18, 0x87, 0x91,
  31747. 0xC8, 0xC8, 0x71, 0x40, 0x46, 0x06, 0x00, 0x44,
  31748. 0x20, 0x22, 0x49, 0x70, 0x11, 0x45, 0x90, 0x02,
  31749. 0xC3, 0x61, 0x60, 0xB4, 0x25, 0x80, 0x16, 0x21,
  31750. 0x11, 0x09, 0x04, 0x88, 0x04, 0x05, 0xCC, 0x36,
  31751. 0x20, 0x01, 0xB1, 0x2C, 0x64, 0xB6, 0x50, 0x54,
  31752. 0x32, 0x42, 0x0B, 0x08, 0x8D, 0x12, 0x39, 0x0D,
  31753. 0x10, 0x29, 0x52, 0x88, 0xB0, 0x04, 0x11, 0x38,
  31754. 0x44, 0xD2, 0xA6, 0x71, 0x0B, 0x45, 0x48, 0x9C,
  31755. 0x34, 0x72, 0xA0, 0x28, 0x49, 0x82, 0x16, 0x86,
  31756. 0x12, 0x18, 0x61, 0x04, 0x41, 0x0D, 0x8A, 0xA6,
  31757. 0x41, 0x80, 0xA8, 0x61, 0xDA, 0x30, 0x65, 0x82,
  31758. 0x84, 0x30, 0x08, 0xA3, 0x29, 0x04, 0x33, 0x8E,
  31759. 0x02, 0x24, 0x0D, 0x9C, 0x44, 0x10, 0xC9, 0x02,
  31760. 0x81, 0x53, 0x06, 0x66, 0x8B, 0x06, 0x90, 0x03,
  31761. 0x87, 0x69, 0x21, 0xC9, 0x69, 0x83, 0x46, 0x4E,
  31762. 0x14, 0x24, 0x89, 0x8C, 0xA0, 0x6C, 0x99, 0xA2,
  31763. 0x2C, 0x11, 0x37, 0x66, 0x0C, 0xA6, 0x4D, 0xD3,
  31764. 0xC8, 0x70, 0x03, 0x02, 0x61, 0xC3, 0xB6, 0x65,
  31765. 0x23, 0xC1, 0x6C, 0x10, 0x34, 0x8D, 0x1A, 0xC1,
  31766. 0x31, 0x43, 0x40, 0x44, 0xD4, 0x08, 0x02, 0x0A,
  31767. 0x36, 0x20, 0xE3, 0x26, 0x42, 0x0A, 0x48, 0x26,
  31768. 0x1A, 0x13, 0x44, 0x0C, 0x18, 0x61, 0x91, 0x96,
  31769. 0x84, 0x02, 0x17, 0x46, 0x9C, 0x20, 0x40, 0x41,
  31770. 0xC6, 0x2D, 0x1B, 0x16, 0x0C, 0x98, 0xB2, 0x90,
  31771. 0x1A, 0x20, 0x84, 0xE2, 0x34, 0x2D, 0xCB, 0x14,
  31772. 0x44, 0x93, 0xC6, 0x8D, 0x58, 0xB2, 0x69, 0x22,
  31773. 0xB2, 0x88, 0xC0, 0xB8, 0x2D, 0xA2, 0xC2, 0x31,
  31774. 0x20, 0xA3, 0x24, 0x11, 0x46, 0x48, 0x4A, 0xA6,
  31775. 0x50, 0x24, 0x09, 0x21, 0x1A, 0x01, 0x0D, 0x20,
  31776. 0x36, 0x01, 0xC4, 0x34, 0x70, 0xDA, 0x16, 0x68,
  31777. 0x84, 0x22, 0x4C, 0x11, 0x14, 0x09, 0x13, 0xC4,
  31778. 0x68, 0x11, 0x41, 0x2D, 0x1C, 0x10, 0x31, 0xDC,
  31779. 0xB2, 0x64, 0x42, 0x36, 0x08, 0x5C, 0x10, 0x88,
  31780. 0x04, 0x91, 0x25, 0xE1, 0xA0, 0x20, 0x14, 0x18,
  31781. 0x12, 0x14, 0x94, 0x91, 0x4C, 0xC2, 0x24, 0xD4,
  31782. 0x06, 0x71, 0x21, 0x02, 0x8D, 0xD4, 0x88, 0x30,
  31783. 0xC9, 0x36, 0x0E, 0xE4, 0x82, 0x81, 0xC0, 0x04,
  31784. 0x6D, 0x24, 0x23, 0x09, 0x21, 0x45, 0x45, 0x20,
  31785. 0x06, 0x65, 0xC2, 0x30, 0x2A, 0x18, 0x30, 0x8E,
  31786. 0x24, 0x83, 0x89, 0x93, 0x32, 0x66, 0xC1, 0x48,
  31787. 0x45, 0x62, 0x48, 0x0A, 0x52, 0xB8, 0x80, 0x11,
  31788. 0x86, 0x21, 0x04, 0x34, 0x11, 0x24, 0xB5, 0x6C,
  31789. 0x50, 0x36, 0x0A, 0x19, 0xA7, 0x8C, 0x14, 0x90,
  31790. 0x0D, 0x1A, 0xA5, 0x68, 0x0B, 0xB1, 0x11, 0x50,
  31791. 0x40, 0x08, 0x48, 0xB6, 0x31, 0x14, 0x28, 0x8D,
  31792. 0xE3, 0x47, 0xB4, 0xA1, 0x44, 0x94, 0xCC, 0x9F,
  31793. 0x0B, 0x94, 0x9F, 0x25, 0x49, 0xD9, 0xB3, 0x8F,
  31794. 0x71, 0xF4, 0x17, 0xA4, 0xA6, 0xAC, 0x24, 0x58,
  31795. 0x14, 0x25, 0x03, 0xC8, 0x63, 0x3E, 0x10, 0xA8,
  31796. 0xD4, 0x10, 0xD7, 0x90, 0x4A, 0x28, 0x37, 0x90,
  31797. 0x70, 0x27, 0xE3, 0x56, 0x5F, 0x04, 0x67, 0x76,
  31798. 0xC3, 0x67, 0x3F, 0xF5, 0xA5, 0x11, 0xA2, 0x2C,
  31799. 0x11, 0x01, 0x5D, 0x63, 0x71, 0x1A, 0xE6, 0x70,
  31800. 0x86, 0x46, 0xAB, 0xCE, 0x03, 0xB6, 0x82, 0xAF,
  31801. 0x51, 0xBA, 0x81, 0x94, 0x9C, 0x82, 0x36, 0xA9,
  31802. 0x49, 0xA5, 0xA3, 0x11, 0x08, 0x8C, 0x4B, 0x13,
  31803. 0x41, 0xF0, 0x08, 0xFD, 0xB2, 0x99, 0xED, 0xA8,
  31804. 0x07, 0x61, 0x3C, 0x2E, 0xBC, 0x49, 0x7B, 0x1C,
  31805. 0xBC, 0x87, 0xBC, 0xAE, 0x5F, 0x5E, 0x8F, 0x5D,
  31806. 0xE7, 0xB9, 0x0C, 0x70, 0x36, 0x25, 0x61, 0xFD,
  31807. 0x95, 0x9F, 0xAE, 0x0F, 0x8D, 0xF3, 0xA2, 0x45,
  31808. 0x24, 0xA7, 0xDE, 0x60, 0xD1, 0x4E, 0x6D, 0xAC,
  31809. 0xC7, 0x6A, 0x32, 0x42, 0xC0, 0x73, 0xEB, 0x78,
  31810. 0x50, 0xF4, 0x49, 0x52, 0x5E, 0x6F, 0x81, 0x42,
  31811. 0x54, 0xF8, 0x82, 0x05, 0xC9, 0x64, 0x74, 0x6A,
  31812. 0x60, 0x5E, 0x36, 0x59, 0x40, 0x50, 0xA3, 0xFE,
  31813. 0xDA, 0xE2, 0x6D, 0x8D, 0x6E, 0xE4, 0x5A, 0x27,
  31814. 0x73, 0x89, 0xDB, 0x0C, 0x5B, 0x14, 0xD9, 0xED,
  31815. 0xB2, 0xC7, 0x1D, 0x71, 0x93, 0x91, 0x0A, 0x72,
  31816. 0x32, 0xBE, 0xA3, 0xD8, 0x95, 0x8C, 0x94, 0x7E,
  31817. 0x63, 0xEB, 0xCE, 0x8B, 0xFC, 0xB0, 0x3F, 0x77,
  31818. 0x5C, 0x43, 0x48, 0x18, 0x83, 0xFE, 0xC8, 0xDA,
  31819. 0x89, 0xF2, 0x3B, 0x54, 0x82, 0x44, 0xC6, 0x9C,
  31820. 0xCC, 0x77, 0x0A, 0xC1, 0x6F, 0xB9, 0x98, 0x10,
  31821. 0xD5, 0xF2, 0x60, 0xFF, 0x38, 0xD2, 0x0D, 0xD6,
  31822. 0x8C, 0x38, 0x54, 0x5B, 0xD8, 0x38, 0x84, 0x50,
  31823. 0x36, 0xF4, 0x02, 0xC1, 0x06, 0x0F, 0x15, 0x1B,
  31824. 0xC8, 0x90, 0x9B, 0x6E, 0x36, 0xC8, 0x3F, 0xE9,
  31825. 0x8B, 0x62, 0x15, 0x6F, 0xF0, 0xC2, 0x86, 0x7F,
  31826. 0xD1, 0xB5, 0x97, 0x53, 0xAE, 0x41, 0xAE, 0x21,
  31827. 0x84, 0xAC, 0x57, 0xA5, 0x1F, 0xA7, 0xC7, 0x24,
  31828. 0xDF, 0xDE, 0x2F, 0x3C, 0xCD, 0xA2, 0x7E, 0x1D,
  31829. 0x97, 0xE1, 0x96, 0xC5, 0xB4, 0x7D, 0xF9, 0x5F,
  31830. 0x7E, 0xEF, 0x09, 0xC4, 0xF3, 0x57, 0xF0, 0x51,
  31831. 0x73, 0xAB, 0x0E, 0x6A, 0xCA, 0x64, 0xE4, 0x99,
  31832. 0x0F, 0xD2, 0x20, 0xAC, 0x72, 0xF1, 0xA8, 0x23,
  31833. 0x8F, 0x94, 0x63, 0xDC, 0xB3, 0xBB, 0x62, 0x2C,
  31834. 0xEA, 0xA6, 0x27, 0x5A, 0x93, 0xC6, 0xCD, 0xCE,
  31835. 0x1E, 0x09, 0xAF, 0x89, 0xEC, 0x22, 0xE4, 0x30,
  31836. 0x2D, 0xB9, 0xCD, 0x08, 0x2E, 0x12, 0x76, 0x79,
  31837. 0x99, 0xBC, 0xA0, 0x34, 0x0B, 0xDA, 0x89, 0x08,
  31838. 0x14, 0x60, 0x7B, 0x98, 0xE6, 0xAF, 0xD2, 0xE1,
  31839. 0x87, 0xC8, 0xDA, 0x50, 0xF7, 0x10, 0x2C, 0x72,
  31840. 0x74, 0x50, 0xD0, 0x3C, 0x98, 0x06, 0xFE, 0xEB,
  31841. 0xC6, 0xC5, 0x69, 0x31, 0x06, 0xE2, 0x2E, 0x7E,
  31842. 0x7D, 0x3D, 0x2B, 0x1F, 0x48, 0x43, 0xC5, 0x95,
  31843. 0xDA, 0x84, 0x08, 0x1E, 0x2B, 0x50, 0x6D, 0x91,
  31844. 0xA6, 0x2B, 0xCD, 0x08, 0x43, 0x7B, 0xA2, 0xD8,
  31845. 0x60, 0x6E, 0xF7, 0x80, 0x08, 0xC3, 0x3F, 0x35,
  31846. 0xF3, 0x70, 0xA5, 0xC7, 0x56, 0xFC, 0xBD, 0x34,
  31847. 0x46, 0x7B, 0xBF, 0x63, 0x19, 0xAC, 0xB6, 0xC3,
  31848. 0x1B, 0x81, 0x84, 0x9F, 0xBB, 0x54, 0x05, 0x99,
  31849. 0xAE, 0x43, 0xE2, 0xA5, 0x20, 0xFD, 0x5C, 0xC7,
  31850. 0x25, 0x47, 0xB1, 0xFD, 0x80, 0xB5, 0x78, 0xC2,
  31851. 0x00, 0x98, 0x02, 0xB9, 0x61, 0x2A, 0xBA, 0x39,
  31852. 0xC7, 0x20, 0xB8, 0x7D, 0x7A, 0x03, 0x68, 0xE5,
  31853. 0x37, 0x71, 0x1F, 0x72, 0xAA, 0x41, 0x61, 0xB4,
  31854. 0xC0, 0xC2, 0xD3, 0x7A, 0xCD, 0xD2, 0xED, 0xC2,
  31855. 0xC5, 0x99, 0x8C, 0x62, 0xA3, 0x7D, 0xC8, 0x9C,
  31856. 0xD2, 0x50, 0x02, 0x0D, 0xCB, 0x68, 0x15, 0xB0,
  31857. 0xD6, 0x19, 0x03, 0xC8, 0x01, 0x12, 0x72, 0xA1,
  31858. 0x3A, 0xC2, 0xA6, 0x63, 0x51, 0x26, 0x03, 0x5D,
  31859. 0x3F, 0x1D, 0x3B, 0x0E, 0x30, 0x6B, 0xB7, 0xEC,
  31860. 0xB6, 0x8E, 0x2D, 0x76, 0xC8, 0xD7, 0xAE, 0x59,
  31861. 0x81, 0xFC, 0x5F, 0x57, 0x5E, 0xAD, 0xA0, 0x20,
  31862. 0xC8, 0xB4, 0x91, 0x2D, 0xEC, 0x03, 0xC4, 0xC6,
  31863. 0x55, 0x05, 0x87, 0xA4, 0xA2, 0x21, 0x09, 0x25,
  31864. 0x97, 0x21, 0xA4, 0x46, 0x45, 0x46, 0x40, 0x3B,
  31865. 0xDC, 0x6F, 0xCD, 0xFB, 0xFB, 0xD9, 0xF4, 0x2C,
  31866. 0xEC, 0xF1, 0xC4, 0x73, 0x41, 0x30, 0x60, 0x63,
  31867. 0x9A, 0xF2, 0xA5, 0x26, 0x78, 0x9A, 0x5E, 0x70,
  31868. 0x98, 0xDE, 0x35, 0x10, 0xA0, 0x5D, 0x45, 0xD5,
  31869. 0x95, 0xF7, 0x11, 0xBC, 0x99, 0xD3, 0x00, 0x67,
  31870. 0x9A, 0x30, 0x85, 0x36, 0x50, 0xDB, 0x18, 0xEA,
  31871. 0x6D, 0xB2, 0xF3, 0x14, 0xDA, 0x23, 0xE2, 0x8A,
  31872. 0x44, 0x21, 0x25, 0xD4, 0xA3, 0x28, 0x43, 0xA0,
  31873. 0xC6, 0x5C, 0x99, 0xB0, 0x72, 0x6B, 0xC2, 0x1A,
  31874. 0x30, 0xBE, 0x6B, 0x7B, 0xE0, 0x31, 0x54, 0x8C,
  31875. 0x29, 0xE5, 0xC6, 0x69, 0x53, 0xDE, 0x05, 0x1E,
  31876. 0x43, 0xCC, 0x7E, 0x9A, 0x82, 0x4A, 0xC4, 0x0A,
  31877. 0x50, 0x65, 0xDC, 0xD8, 0xF9, 0x01, 0x32, 0x65,
  31878. 0x1E, 0xF9, 0xA4, 0xCC, 0x07, 0xB9, 0x55, 0x97,
  31879. 0x45, 0xA9, 0x61, 0xF8, 0xBE, 0x99, 0x00, 0x12,
  31880. 0xD8, 0x17, 0x62, 0xFB, 0x89, 0xE7, 0x05, 0x5E,
  31881. 0x1B, 0xCD, 0x2B, 0x09, 0x6C, 0x5A, 0x5C, 0xA3,
  31882. 0x66, 0x4D, 0x02, 0x78, 0x0C, 0xC3, 0x63, 0x30,
  31883. 0xD0, 0xFA, 0x7B, 0x11, 0x00, 0x40, 0xDD, 0xF0,
  31884. 0x8C, 0x7C, 0xBA, 0x4C, 0x63, 0x78, 0xDA, 0xBB,
  31885. 0xDF, 0xF9, 0xC9, 0xA4, 0x40, 0x25, 0x86, 0xD1,
  31886. 0xBA, 0x22, 0xD7, 0x69, 0x98, 0x4E, 0x9D, 0x15,
  31887. 0x21, 0xA8, 0x56, 0xC0, 0xFF, 0x52, 0xE4, 0xB4,
  31888. 0x0F, 0xB2, 0x53, 0xE7, 0xA1, 0x34, 0x18, 0xEA,
  31889. 0x5B, 0x25, 0x42, 0x13, 0xE3, 0x13, 0xE7, 0xDF,
  31890. 0x54, 0x2B, 0x8D, 0x70, 0x51, 0xC7, 0x60, 0xB1,
  31891. 0x1E, 0x4D, 0x3A, 0x46, 0x04, 0xA1, 0x11, 0x43,
  31892. 0xAD, 0x24, 0x29, 0x90, 0xC9, 0x04, 0x15, 0xC5,
  31893. 0x07, 0xE5, 0x46, 0xB8, 0x50, 0x16, 0x6B, 0x66,
  31894. 0xFE, 0x1C, 0x8B, 0xFC, 0x20, 0x9C, 0xC4, 0x88,
  31895. 0x10, 0x36, 0x5E, 0x56, 0xE8, 0x45, 0x75, 0x89,
  31896. 0xFB, 0xD6, 0xD0, 0x8D, 0x9D, 0x53, 0xAE, 0x89,
  31897. 0x19, 0x54, 0xCF, 0xE1, 0xFF, 0x12, 0x13, 0xF2,
  31898. 0xC7, 0xBE, 0x4C, 0x1E, 0xB0, 0x70, 0x6E, 0xDC,
  31899. 0x0A, 0x64, 0x3B, 0x60, 0x3A, 0xEA, 0x0D, 0x41,
  31900. 0xDD, 0x8E, 0x09, 0xB9, 0x96, 0x8F, 0x6A, 0x49,
  31901. 0x50, 0xEF, 0xDF, 0xD7, 0x73, 0x8D, 0x16, 0x32,
  31902. 0xA8, 0x5C, 0x0A, 0x90, 0x18, 0xA1, 0xEB, 0x19,
  31903. 0xCC, 0x50, 0xD5, 0x59, 0xD7, 0x35, 0x3F, 0xBA,
  31904. 0x38, 0x1B, 0x5F, 0x71, 0x56, 0x70, 0xB3, 0x20,
  31905. 0x4D, 0x9E, 0x16, 0xA8, 0xF7, 0x35, 0x19, 0xD2,
  31906. 0x09, 0x0A, 0x22, 0x28, 0x81, 0x61, 0x26, 0x5B,
  31907. 0x9C, 0xEC, 0x9D, 0x4A, 0x61, 0xCF, 0x0D, 0x3C,
  31908. 0x88, 0xEA, 0x0B, 0x7A, 0xA7, 0xC6, 0xAE, 0x31,
  31909. 0xBE, 0xC2, 0xBA, 0x48, 0xBB, 0x9D, 0x06, 0xE1,
  31910. 0x32, 0x6D, 0x80, 0xCE, 0x27, 0x5C, 0x6F, 0x13,
  31911. 0x79, 0x35, 0x9F, 0x9C, 0x11, 0xEA, 0xDB, 0xF5,
  31912. 0x49, 0x15, 0xB6, 0x51, 0x86, 0xFC, 0x62, 0x34,
  31913. 0x3D, 0x58, 0x6B, 0x0E, 0xF8, 0x3B, 0xBB, 0x42,
  31914. 0xF6, 0x2D, 0x5C, 0xE2, 0xF3, 0xAA, 0x9F, 0x03,
  31915. 0x43, 0xE9, 0x9E, 0x90, 0xB9, 0xFF, 0x55, 0x93,
  31916. 0x60, 0xF8, 0x10, 0x2F, 0xFC, 0xBD, 0x40, 0x23,
  31917. 0xB8, 0x4F, 0x4C, 0x7A, 0x74, 0x9F, 0xDC, 0x55,
  31918. 0xDF, 0x5E, 0xCD, 0x23, 0xEB, 0xAC, 0x47, 0x4E,
  31919. 0x0D, 0x0F, 0xBE, 0xDE, 0x02, 0x64, 0x61, 0x7E,
  31920. 0x73, 0x78, 0x8E, 0x25, 0xE9, 0x7D, 0x66, 0xE5,
  31921. 0x82, 0xBF, 0x98, 0x5B, 0x36, 0xCE, 0x17, 0x72,
  31922. 0x56, 0x9C, 0xDA, 0x63, 0x77, 0x55, 0x8B, 0xA9,
  31923. 0x75, 0xF5, 0x28, 0xC3, 0x78, 0x6D, 0x8F, 0xC2,
  31924. 0x75, 0x5F, 0x28, 0x9E, 0x3F, 0xFB, 0xF1, 0xFD,
  31925. 0xB7, 0xDE, 0x05, 0x3C, 0xD3, 0xE8, 0xD7, 0x7A,
  31926. 0x7D, 0xC9, 0xF7, 0x9D, 0x58, 0xB4, 0xA6, 0x21,
  31927. 0x25, 0xFC, 0x52, 0x84, 0x21, 0xF6, 0x0B, 0x6D,
  31928. 0xA6, 0x62, 0x51, 0x97, 0xCD, 0xA9, 0xA1, 0x0C,
  31929. 0x88, 0x21, 0x67, 0xA5, 0xFB, 0x8C, 0x8A, 0x50,
  31930. 0xC5, 0x21, 0x91, 0x3A, 0xAB, 0x95, 0x96, 0xF3,
  31931. 0x30, 0x6D, 0x08, 0x42, 0x07, 0x4B, 0x78, 0x1F,
  31932. 0xC1, 0xD3, 0x41, 0x15, 0x68, 0xED, 0x93, 0x09,
  31933. 0xC7, 0x8B, 0xF9, 0x77, 0x25, 0xD3, 0xCE, 0x2B,
  31934. 0xA2, 0x0D, 0xB4, 0xC6, 0x84, 0x7F, 0x8E, 0xE5,
  31935. 0x24, 0x46, 0x59, 0x8D, 0x6F, 0x0F, 0x0C, 0xA8,
  31936. 0xFC, 0x04, 0x9B, 0x4D, 0x2B, 0xA7, 0x70, 0x1F,
  31937. 0x46, 0x7E, 0x76, 0x03, 0xC6, 0x7E, 0xA5, 0x3D,
  31938. 0x79, 0xE2, 0xF1, 0xAC, 0xBC, 0xDD, 0xF6, 0x91,
  31939. 0x69, 0x4C, 0x44, 0x1F, 0xC3, 0xBF, 0x9F, 0xFC,
  31940. 0x4E, 0xB0, 0x79, 0x30, 0x68, 0x89, 0xAC, 0xF2,
  31941. 0xD7, 0xC6, 0xE1, 0x6C, 0x37, 0xFB, 0xB3, 0x38,
  31942. 0x44, 0x2C, 0x97, 0xAB, 0xDA, 0x2C, 0x88, 0xC7,
  31943. 0xF2, 0x80, 0x08, 0x00, 0x4E, 0x44, 0xED, 0xBE,
  31944. 0xA4, 0x28, 0x3D, 0xC1, 0xCF, 0x9E, 0x83, 0xE7,
  31945. 0x2E, 0x7F, 0xF5, 0x08, 0x47, 0x26, 0xE0, 0xBD,
  31946. 0x1A, 0x17, 0xDB, 0x2F, 0xED, 0x19, 0x2E, 0x65,
  31947. 0x1B, 0x62, 0x5F, 0x08, 0x82, 0x10, 0x61, 0xCB,
  31948. 0xAA, 0xA7, 0xF8, 0x59, 0x4B, 0x46, 0xCB, 0xA2,
  31949. 0xCB, 0x41, 0x34, 0x30, 0x51, 0x58, 0x2A, 0xEE,
  31950. 0xE1, 0x5E, 0xAC, 0xCA, 0xBF, 0x37, 0x45, 0x98,
  31951. 0xBD, 0x93, 0x1B, 0x5A, 0x5E, 0x92, 0x14, 0x05,
  31952. 0x75, 0x2D, 0xFB, 0x8F, 0xBD, 0x24, 0x9B, 0x81,
  31953. 0xCD, 0xDD, 0xF5, 0xBE, 0x05, 0x0D, 0xBD, 0x4B,
  31954. 0x2B, 0x8C, 0x0A, 0xF0, 0x3A, 0x85, 0xD6, 0x74,
  31955. 0x65, 0x7F, 0x98, 0xF8, 0x57, 0xA2, 0x36, 0xA2,
  31956. 0xFE, 0xE4, 0xB4, 0xA4, 0x0D, 0xEA, 0x9A, 0xBE,
  31957. 0x41, 0x79, 0x68, 0x63, 0x70, 0x3F, 0x3E, 0x38,
  31958. 0x60, 0xC3, 0x40, 0x81, 0x72, 0xDD, 0x25, 0x34,
  31959. 0xB4, 0xFE, 0xAC, 0x41, 0x6E, 0x4A, 0xE7, 0xBF,
  31960. 0xE3, 0x87, 0xFA, 0x20, 0x8B, 0xBD, 0x68, 0x9E,
  31961. 0x06, 0xA9, 0x15, 0x23, 0x07, 0x04, 0x4B, 0xFA,
  31962. 0x45, 0x45, 0xB7, 0x75, 0xD3, 0x3E, 0x16, 0x70,
  31963. 0xF6, 0x26, 0xF2, 0x3A, 0x9D, 0xFB, 0xEA, 0xEB,
  31964. 0x47, 0xCE, 0x99, 0x6B, 0x0E, 0xB2, 0xE8, 0x2B,
  31965. 0x18, 0x15, 0x14, 0x2E, 0xF2, 0x14, 0x0D, 0x44,
  31966. 0x47, 0x1E, 0x63, 0x84, 0x5B, 0x3F, 0xA8, 0xEF,
  31967. 0x5F, 0xEB, 0xA0, 0x41, 0x77, 0xC1, 0xF4, 0x4F,
  31968. 0x8E, 0x2E, 0x29, 0xCD, 0xDB, 0xF2, 0x75, 0x24,
  31969. 0x24, 0x46, 0x73, 0xC3, 0x46, 0xB5, 0xCA, 0x13,
  31970. 0x35, 0x12, 0x0A, 0x8D, 0x88, 0x89, 0x17, 0x99,
  31971. 0x13, 0xCA, 0x66, 0x07, 0x67, 0x6B, 0x7B, 0x3B,
  31972. 0x20, 0xD3, 0x5F, 0x78, 0x1C, 0xC0, 0x99, 0x59,
  31973. 0x0A, 0xBA, 0x8F, 0xA0, 0xDB, 0xDF, 0xCC, 0x03,
  31974. 0xC4, 0xA6, 0xC7, 0x08, 0xB9, 0xFD, 0x95, 0xC2,
  31975. 0x45, 0xF9, 0xF3, 0x11, 0x62, 0xF7, 0x14, 0xB9,
  31976. 0xEB, 0x09, 0xB3, 0x7C, 0xF8, 0xF6, 0x67, 0xCC,
  31977. 0x03, 0xB3, 0x06, 0x6F, 0x60, 0xAC, 0x72, 0xF2,
  31978. 0xD3, 0x71, 0x6C, 0x4D, 0xAD, 0x3A, 0x99, 0x75,
  31979. 0x5C, 0x52, 0x2D, 0x87, 0x69, 0x3E, 0xD6, 0x7E,
  31980. 0x12, 0x96, 0xD3, 0x88, 0x8D, 0x11, 0x85, 0xAA,
  31981. 0x0A, 0xA5, 0x32, 0x90, 0x51, 0xC5, 0x65, 0x64,
  31982. 0xE0, 0xA9, 0x73, 0xA4, 0xF3, 0x8A, 0x32, 0x83,
  31983. 0xE5, 0x08, 0x09, 0x39, 0x6A, 0x90, 0x2C, 0xC3,
  31984. 0xFC, 0x92, 0x29, 0x7A, 0x45, 0xBE, 0x02, 0x79,
  31985. 0x15, 0x1B, 0xBB, 0x60, 0xBB, 0xD9, 0x42, 0xF1,
  31986. 0xE5, 0x14, 0xB4, 0xA5, 0xFF, 0x12, 0x42, 0x30,
  31987. 0xB0, 0xCB, 0xD0, 0x1D, 0xB4, 0x62, 0x49, 0xC5,
  31988. 0xB7, 0xDA, 0x37, 0x47, 0x2C, 0x8B, 0x16, 0xCA,
  31989. 0xD2, 0x2C, 0xA1, 0x24, 0xE6, 0x57, 0xFA, 0xEB,
  31990. 0x2C, 0x62, 0x2E, 0x12, 0x74, 0x37, 0x2B, 0x3F,
  31991. 0x56, 0x23, 0x9C, 0xED, 0x90, 0xDE, 0x0D, 0x6E,
  31992. 0x9E, 0x11, 0x78, 0xA4, 0x9C, 0xB3, 0xA1, 0x37,
  31993. 0xF7, 0x4B, 0x09, 0x61, 0xD8, 0x33, 0x1D, 0x80,
  31994. 0x68, 0x5C, 0xDD, 0xBD, 0x3E, 0xAE, 0x9D, 0xB8,
  31995. 0xBA, 0x42, 0x41, 0xDC, 0xC9, 0x93, 0xF1, 0x92,
  31996. 0x2F, 0x7A, 0xF9, 0xFE, 0x67, 0x13, 0x87, 0xBD,
  31997. 0x7D, 0x04, 0x17, 0x91, 0xB6, 0x03, 0x5E, 0xA0,
  31998. 0x5B, 0x23, 0xEA, 0x0C, 0xFA, 0x45, 0xCB, 0x1A,
  31999. 0xC5, 0x7F, 0x63, 0xD6, 0x3D, 0x3C, 0x66, 0x4A,
  32000. 0x83, 0x4E, 0x4E, 0x90, 0xA6, 0x63, 0xB0, 0x8A,
  32001. 0xD7, 0x0D, 0xB4, 0xB7, 0xA9, 0x0F, 0xC6, 0xC7,
  32002. 0x3B, 0xAD, 0x07, 0xA6, 0x94, 0x47, 0xDB, 0x63,
  32003. 0x26, 0x00, 0x18, 0x5E, 0x27, 0xB5, 0xE2, 0xE3,
  32004. 0xED, 0x8D, 0x97, 0x95, 0x38, 0x20, 0x24, 0x9F,
  32005. 0x40, 0x84, 0x44, 0x7E, 0x8C, 0x05, 0xAB, 0xB1,
  32006. 0x89, 0x26, 0x7D, 0x46, 0x2C, 0x9F, 0xE5, 0xC1,
  32007. 0x27, 0xCE, 0x1D, 0x5A, 0x9F, 0xF1, 0xF8, 0x57,
  32008. 0x8F, 0xCF, 0xB7, 0x4E, 0x07, 0xF3, 0xBA, 0x56,
  32009. 0xCF, 0xE9, 0x87, 0x21, 0x61, 0xD6, 0x97, 0x7B,
  32010. 0x26, 0x97, 0x07, 0xB4, 0x87, 0xFE, 0x25, 0x9C,
  32011. 0xA9, 0x8E, 0x06, 0x90, 0x17, 0x2C, 0x98, 0x26,
  32012. 0x23, 0xEE, 0xBB, 0x91, 0x8A, 0x15, 0x38, 0xA1,
  32013. 0x38, 0xCB, 0x8B, 0xA0, 0xF3, 0x4A, 0xF2, 0x12,
  32014. 0xA7, 0xB7, 0x05, 0xB6, 0x09, 0xD0, 0xEC, 0xDD,
  32015. 0x21, 0xB6, 0xFA, 0x29, 0x95, 0xB4, 0x08, 0xD5,
  32016. 0x95, 0xB7, 0xB8, 0x2E, 0x23, 0xAA, 0x89, 0x81,
  32017. 0xE2, 0xD0, 0xFD, 0x9C, 0x8D, 0xF0, 0xCA, 0x61,
  32018. 0xE3, 0x1E, 0x73, 0x9E, 0xD1, 0x72, 0x5C, 0x63,
  32019. 0xB8, 0x74, 0x0E, 0x2C, 0x27, 0x3A, 0x71, 0xF9,
  32020. 0xFE, 0x66, 0x33, 0xE9, 0x41, 0x27, 0x61, 0xA3,
  32021. 0xFA, 0xD8, 0x66, 0x2A, 0x52, 0x6D, 0xAB, 0xBF,
  32022. 0x32, 0xC2, 0x8E, 0x8F, 0xB0, 0x60, 0x52, 0xE1,
  32023. 0x96, 0xC8, 0x1E, 0x9A, 0x3E, 0x07, 0xFA, 0x34,
  32024. 0xFA, 0x9C, 0x4C, 0x0D, 0x29, 0x0F, 0x68, 0xA6,
  32025. 0x59, 0x28, 0x22, 0xB1, 0x99, 0x56, 0x2C, 0x01,
  32026. 0x04, 0x2F, 0x34, 0x65, 0xFD, 0xD4, 0xD0, 0xD5,
  32027. 0x17, 0x7C, 0x14, 0x92, 0x73, 0x6C, 0x31, 0xCE,
  32028. 0xD4, 0xB3, 0x59, 0x83, 0x6B, 0x34, 0x7C, 0x76,
  32029. 0x8C, 0xED, 0xD5, 0xE2, 0x4F, 0x39, 0x44, 0xBF,
  32030. 0x90, 0x53, 0x9A, 0xC7, 0xD4, 0x6A, 0x86, 0xA3,
  32031. 0xE2, 0x15, 0x59, 0xD0, 0x0F, 0x32, 0x92, 0xC2,
  32032. 0x9B, 0x9E, 0xE3, 0xF6, 0x94, 0x96, 0xFD, 0x0B,
  32033. 0xB6, 0x06, 0x8F, 0x0D, 0x1F, 0x38, 0xFC, 0x6F,
  32034. 0xA2, 0x78, 0xAC, 0xC5, 0xB5, 0x6A, 0x6B, 0xEC,
  32035. 0x78, 0x8A, 0x6F, 0xD8, 0x21, 0xB7, 0xCF, 0x66,
  32036. 0x73, 0x03, 0xCA, 0x2E, 0x3C, 0x7F, 0x2F, 0x29,
  32037. 0x41, 0xC9, 0x88, 0xFD, 0x0E, 0xA0, 0x43, 0xD6,
  32038. 0x9E, 0xB1, 0xE7, 0x13, 0x9C, 0xF0, 0x9C, 0xCF,
  32039. 0x33, 0x22, 0x57, 0xEF, 0xE5, 0xCE, 0xD9, 0xAC,
  32040. 0x7D, 0x34, 0x75, 0xBD, 0xAE, 0x84, 0xEE, 0xE8,
  32041. 0x5D, 0x8C, 0x55, 0x86, 0xBA, 0x19, 0xE5, 0x9D,
  32042. 0x35, 0x6D, 0xD8, 0x70, 0xC5, 0xE0, 0xEA, 0x77,
  32043. 0x3A, 0xE5, 0xB5, 0x2C, 0xD2, 0x28, 0xB5, 0xE8,
  32044. 0xAF, 0xB1, 0xD2, 0xC4, 0xE5, 0x59, 0x06, 0xB8,
  32045. 0x2E, 0xA6, 0x8F, 0xC4, 0x9B, 0x30, 0xF9, 0x37,
  32046. 0xDB, 0x29, 0xA1, 0x44, 0x0B, 0xB7, 0xB5, 0xB4,
  32047. 0x12, 0xD3, 0x4E, 0xB3, 0xB7, 0xD8, 0x2F, 0x19,
  32048. 0xDE, 0x3B, 0xC3, 0x53, 0xCE, 0x1C, 0x34, 0x4C,
  32049. 0xA4, 0x6A, 0xE2, 0xD0, 0x04, 0xDF, 0x3C, 0x53,
  32050. 0x8B, 0x06, 0x8F, 0x36, 0xE5, 0x77, 0xB2, 0x7A,
  32051. 0x1A, 0xC0, 0x0C, 0xBD, 0xA3, 0xA0, 0xEE, 0xB6,
  32052. 0x40, 0xAD, 0x5C, 0x04, 0xAE, 0xCF, 0x64, 0x2B,
  32053. 0x8A, 0x18, 0x58, 0x86, 0xDE, 0xC9, 0x3D, 0x7D,
  32054. 0x15, 0xBC, 0xEE, 0x4C, 0x22, 0xF4, 0x98, 0xD9,
  32055. 0x37, 0xEE, 0xE2, 0x40, 0x43, 0xFF, 0xB2, 0x6F,
  32056. 0x05, 0xC0, 0x0E, 0x30, 0xDE, 0xD8, 0x0C, 0x0B,
  32057. 0xAD, 0xED, 0xCC, 0xBC, 0x29, 0x95, 0x07, 0x40,
  32058. 0x10, 0x99, 0xA0, 0xD1, 0x08, 0xF7, 0xD5, 0xF1,
  32059. 0xAD, 0xC9, 0xDD, 0xC8, 0x6A, 0x1E, 0x9E, 0x06,
  32060. 0xDF, 0x12, 0xFF, 0x66, 0x33, 0x5E, 0x21, 0x47,
  32061. 0xC3, 0xDE, 0x36, 0x98, 0x5B, 0xBF, 0x42, 0x9E,
  32062. 0x30, 0xA0, 0x81, 0x5C, 0x28, 0x34, 0x1B, 0x3A,
  32063. 0x32, 0xBC, 0xDE, 0x52, 0x53, 0x25, 0x1E, 0xF6,
  32064. 0xE2, 0x99, 0x12, 0x92, 0x07, 0x1D, 0xEB, 0x08,
  32065. 0x36, 0xA7, 0xD5, 0x18, 0x1F, 0xDB, 0x44, 0xA7,
  32066. 0xE1, 0x13, 0x06, 0xB0, 0xDF, 0x63, 0x82, 0x68,
  32067. 0xEF, 0xF5, 0x2B, 0x04, 0x0B, 0x93, 0xE8, 0xB0,
  32068. 0x92, 0x7B, 0xDE, 0x1F, 0xC9, 0x39, 0x8F, 0x42,
  32069. 0x9D, 0x06, 0x22, 0x13, 0xC9, 0x97, 0x2F, 0x43,
  32070. 0x8A, 0xBA, 0xAF, 0xF9, 0x71, 0xE3, 0x55, 0x5D,
  32071. 0x06, 0x77, 0x38, 0x39, 0xA3, 0xED, 0x41, 0x63,
  32072. 0xFE, 0x2A, 0xB3, 0x23, 0x43, 0x0C, 0xF3, 0x17,
  32073. 0x3B, 0x69, 0xED, 0x32, 0x0A, 0x54, 0xF3, 0x8D,
  32074. 0x76, 0xC6, 0x09, 0xDD, 0x88, 0x5B, 0x23, 0x57,
  32075. 0x72, 0xC4, 0x87, 0xB8, 0x9D, 0xF7, 0xCA, 0xFB,
  32076. 0x7C, 0x61, 0x67, 0x5C, 0x65, 0xF8, 0xD6, 0xD7,
  32077. 0x1E, 0x95, 0xB9, 0x73, 0x4D, 0x2E, 0x1F, 0x43,
  32078. 0x3E, 0x2B, 0x58, 0x92, 0x15, 0x2E, 0xAA, 0x51,
  32079. 0xF0, 0xD4, 0xF2, 0xA6, 0xCD, 0x12, 0x21, 0xD6,
  32080. 0xCA, 0x46, 0x2A, 0xFF, 0xCB, 0x1B, 0x6B, 0xB4,
  32081. 0x09, 0x17, 0x3B, 0xA2, 0x94, 0xDF, 0x1D, 0x68,
  32082. 0x8B, 0x75, 0xEA, 0x11, 0xD6, 0x99, 0x04, 0xD1,
  32083. 0x00, 0xDB, 0x61, 0xBC, 0xF2, 0x3B, 0x88, 0x4B,
  32084. 0x33, 0xDF, 0x0F, 0xD4, 0xFB, 0x14, 0x0C, 0x6A,
  32085. 0x53, 0x61, 0x1F, 0xBD, 0x28, 0xB2, 0x11, 0x19,
  32086. 0x38, 0x71, 0x17, 0x76, 0x4D, 0xEE, 0x01, 0xC4,
  32087. 0x77, 0x53, 0x2A, 0xAF, 0xD3, 0x78, 0xFF, 0x45,
  32088. 0x7F, 0x97, 0x9D, 0x26, 0x92, 0x0E, 0xD9, 0x4E,
  32089. 0x34, 0x1D, 0xE8, 0xDD, 0xBF, 0x5F, 0x87, 0xE6,
  32090. 0x35, 0x9A, 0x39, 0x71, 0x59, 0x20, 0x01, 0xFB,
  32091. 0x53, 0x2C, 0x61, 0x38, 0x0C, 0x8C, 0x02, 0xD3,
  32092. 0xA0, 0x53, 0x95, 0x02, 0xED, 0x5C, 0xFE, 0x9B,
  32093. 0xD3, 0x6A, 0xF3, 0x3F, 0x92, 0x6F, 0x33, 0x37,
  32094. 0x19, 0x97, 0x81, 0x3A, 0x50, 0xE1, 0xD9, 0x27,
  32095. 0x7E, 0x64, 0xF8, 0x01, 0x52, 0x26, 0x51, 0xD1,
  32096. 0x06, 0xAF, 0x20, 0xA0, 0x28, 0x0F, 0x3F, 0xCB,
  32097. 0x21, 0xB7, 0x55, 0x1A, 0x76, 0xB8, 0x9B, 0x4D,
  32098. 0xED, 0x2A, 0x05, 0x0E, 0x6E, 0xAF, 0xCC, 0xA1,
  32099. 0x08, 0x9C, 0xBE, 0x3F, 0x98, 0xE6, 0xB4, 0xB9,
  32100. 0x83, 0xC9, 0x08, 0x41, 0x96, 0xDD, 0xD9, 0x0D,
  32101. 0x52, 0x66, 0x94, 0xA4, 0xEA, 0xFC, 0xE5, 0x48,
  32102. 0x04, 0x73, 0x64, 0x79, 0x68, 0xC9, 0x4A, 0x81,
  32103. 0xA8, 0x07, 0xF8, 0xD9, 0x4E, 0x07, 0x1E, 0xC1,
  32104. 0x8F, 0x62, 0xAB, 0xA6, 0xD7, 0x68, 0xFC, 0x57,
  32105. 0x5E, 0x75, 0x1B, 0xBF, 0x3D, 0xA6, 0x91, 0xC5,
  32106. 0x08, 0x14, 0x5E, 0xF2, 0x4C, 0x22, 0x8B, 0x4E,
  32107. 0x29, 0x2D, 0xC0, 0x46, 0x3A, 0x9C, 0x9D, 0x86,
  32108. 0xCF, 0x51, 0x85, 0x9D, 0x93, 0x23, 0xA1, 0xA1,
  32109. 0xF3, 0x76, 0xB1, 0x56, 0xB0, 0xF4, 0x1F, 0x39,
  32110. 0xDA, 0xDB, 0x13, 0x70, 0x29, 0x89, 0x95, 0xD2,
  32111. 0xC5, 0xF3, 0x76, 0xFE, 0xEE, 0x99, 0xCF, 0xA0,
  32112. 0x84, 0xEC, 0x70, 0xF0, 0xD3, 0xFA, 0x42, 0xDB,
  32113. 0xFD, 0x99, 0x65, 0x2F, 0x84, 0x11, 0x99, 0xCD,
  32114. 0x38, 0xB3, 0x1B, 0xAB, 0x8C, 0x2D, 0x33, 0x04,
  32115. 0xCA, 0xE1, 0xB3, 0x05, 0x9A, 0x20, 0x80, 0xDB,
  32116. 0xED, 0x59, 0x42, 0x30, 0x48, 0x37, 0xB3, 0x85,
  32117. 0x5C, 0xEE, 0x54, 0x06, 0x92, 0x97, 0x4E, 0xFC,
  32118. 0xFA, 0xF7, 0x25, 0xE0, 0x4E, 0x57, 0xC4, 0x72,
  32119. 0x38, 0x59, 0xCA, 0x3C, 0x4A, 0x3F, 0x09, 0xD6,
  32120. 0x09, 0x15, 0x83, 0xEF, 0x24, 0x21, 0xDD, 0xFD,
  32121. 0x66, 0x9E, 0xBF, 0xEE, 0xCC, 0xBF, 0x86, 0x20,
  32122. 0x29, 0x40, 0x5E, 0x42, 0xD2, 0xC0, 0x24, 0x2D,
  32123. 0x76, 0xE6, 0x64, 0xF9, 0x5D, 0xC2, 0x85, 0xB6,
  32124. 0x09, 0x41, 0x04, 0x62, 0x17, 0xDC, 0xF8, 0xFA,
  32125. 0x2A, 0x4C, 0xD1, 0x82, 0x31, 0x57, 0xB7, 0x2B,
  32126. 0x49, 0xE8, 0x40, 0x13, 0x2A, 0xA1, 0x86, 0xD2,
  32127. 0x9A, 0xB8, 0xA9, 0xBE, 0x39, 0xBE, 0xE9, 0xA5,
  32128. 0x35, 0x12, 0x08, 0xF1, 0xA9, 0x9E, 0x57, 0x46,
  32129. 0x3A, 0x55, 0x16, 0xA7, 0x41, 0xD9, 0x25, 0xB8,
  32130. 0x2F, 0xAF, 0xA8, 0x81, 0x5F, 0x5F, 0x46, 0xA4,
  32131. 0x3B, 0xB3, 0xE9, 0x1B, 0x74, 0xEF, 0x5D, 0x57,
  32132. 0x48, 0x4A, 0x72, 0x08, 0xDA, 0xFE, 0x1D, 0x55,
  32133. 0x6B, 0xAB, 0x8B, 0x13, 0x18, 0xBF, 0xDD, 0xF4,
  32134. 0x4E, 0x01, 0x5F, 0x4B, 0xF6, 0x80, 0xD4, 0x16,
  32135. 0x4B, 0x2F, 0x03, 0x4B, 0xF8, 0x93, 0x20, 0x21,
  32136. 0x55, 0x52, 0x49, 0x4A, 0x6C, 0x1F, 0x7D, 0xAD,
  32137. 0x04, 0xEF, 0xB3, 0x74, 0xEE, 0xC5, 0xB6, 0xBC,
  32138. 0x33, 0x7A, 0xCF, 0x64, 0xB9, 0xF9, 0x41, 0x70,
  32139. 0xAF, 0xE9, 0xC7, 0xD6, 0x25, 0x18, 0x17, 0xAB,
  32140. 0xBA, 0xC9, 0x05, 0xEF, 0x40, 0x89, 0xD5, 0x69,
  32141. 0x76, 0xAA, 0xA0, 0x3E, 0x4D, 0x1C, 0xE7, 0x9D,
  32142. 0x9E, 0x74, 0xF4, 0xF2, 0x7B, 0x40, 0xF6, 0x57,
  32143. 0x78, 0x66, 0xFC, 0xDA, 0xE3, 0x6B, 0xD2, 0x6E,
  32144. 0xC7, 0x9D, 0x65, 0x84, 0xAF, 0x7A, 0x1F, 0xE4,
  32145. 0x34, 0xD4, 0x1A, 0x17, 0xA2, 0x72, 0xB0, 0xEE,
  32146. 0x5A, 0x0C, 0xF4, 0x02, 0xAC, 0x1D, 0x6F, 0x4A,
  32147. 0xD0, 0xB2, 0x02, 0x3A, 0x7D, 0x2C, 0xF1, 0x43,
  32148. 0x0E, 0x1E, 0x96, 0xEB, 0x42, 0xF8, 0x3A, 0xF5,
  32149. 0x0B, 0x5D, 0xA9, 0x23, 0x02, 0x28, 0xE5, 0x26,
  32150. 0x5E, 0x69, 0x38, 0x2F, 0x85, 0x34, 0x32, 0x5E,
  32151. 0x5E, 0x29, 0x33, 0x94, 0x05, 0xBD, 0x58, 0xF8,
  32152. 0xE8, 0x9C, 0xBF, 0xB1, 0x5A, 0x05, 0xC6, 0x23,
  32153. 0x9B, 0xBB, 0x57, 0x69, 0x8C, 0xE6, 0x41, 0x97,
  32154. 0x48, 0x01, 0x95, 0xAF, 0xE9, 0x62, 0x8C, 0x6F,
  32155. 0x09, 0x43, 0xF3, 0x64, 0x50, 0x90, 0x2F, 0x14,
  32156. 0xF7, 0x30, 0x07, 0xE0, 0x4B, 0xA8, 0x39, 0xAC,
  32157. 0x21, 0xC4, 0x07, 0x45, 0x5F, 0xD9, 0x87, 0xB1,
  32158. 0x57, 0x47, 0x07, 0x66, 0xFF, 0xC7, 0xAB, 0xEE,
  32159. 0x1F, 0x55, 0x71, 0x50, 0x63, 0xCF, 0x58, 0x3B,
  32160. 0xC8, 0x1B, 0xEA, 0xA5, 0xE2, 0xF1, 0x57, 0xB3,
  32161. 0x77, 0x65, 0xA9, 0xBD, 0x23, 0xC8, 0x30, 0x86,
  32162. 0xC3, 0x5F, 0xBF, 0x16, 0x3F, 0x42, 0x28, 0x0A,
  32163. 0xC6, 0x5A, 0x57, 0x15, 0x2F, 0xA1, 0x96, 0xA9,
  32164. 0x25, 0xC5, 0x8E, 0x32, 0x11, 0x62, 0xB3, 0x54,
  32165. 0x18, 0x00, 0xA4, 0xA6, 0xD4, 0x0F, 0x68, 0x27,
  32166. 0x8F, 0x21, 0x78, 0x02, 0x37, 0x98, 0xBD, 0xCE,
  32167. 0x3F, 0xBC, 0xF2, 0x9C, 0x66, 0x8E, 0x79, 0xA1,
  32168. 0x54, 0x12, 0x55, 0x2E, 0xC0, 0x59, 0xC7, 0x18,
  32169. 0x18, 0x22, 0x4D, 0x27, 0x8B, 0x8D, 0xF3, 0x08,
  32170. 0x99, 0xE6, 0x35, 0x14, 0xB1, 0xE3, 0xB8, 0x7A,
  32171. 0x40, 0x7B, 0x68, 0x7B, 0xFF, 0xDC, 0x54, 0x41,
  32172. 0x06, 0xCA, 0x91, 0xFE, 0xDB, 0x2B, 0xDA, 0x9E,
  32173. 0xC5, 0x20, 0xD8, 0xBF, 0x42, 0xBC, 0xE6, 0x39,
  32174. 0xC4, 0x26, 0x9E, 0xF3, 0x82, 0xD9, 0xF1, 0xA0,
  32175. 0x04, 0xAF, 0xFB, 0x77, 0x13, 0x36, 0xAF, 0xD7,
  32176. 0x91, 0x9B, 0x3A, 0x57, 0x98, 0xFE, 0xAD, 0xCD,
  32177. 0x46, 0xF8, 0xF8, 0xF1, 0x87, 0x53, 0xBD, 0x57,
  32178. 0x3F, 0x99, 0xBC, 0xA6, 0xBD, 0x9B, 0x6E, 0xF4,
  32179. 0x17, 0x7A, 0x78, 0x30, 0x70, 0xA3, 0x43, 0xFF,
  32180. 0x92, 0xCD, 0x99, 0x73, 0xAE, 0x65, 0x6A, 0x10,
  32181. 0xFF, 0x70, 0x47, 0x0F, 0x16, 0x4C, 0x4A, 0x90,
  32182. 0xF4, 0x52, 0x05, 0x79, 0x33, 0x63, 0xDE, 0x14,
  32183. 0x65, 0xAF, 0x8A, 0x5E, 0x67, 0x20, 0x03, 0x9F,
  32184. 0xE6, 0x70, 0x13, 0x6B, 0xE0, 0xF3, 0x6A, 0x4C,
  32185. 0x6B, 0x5B, 0xCB, 0xE1, 0x7C, 0x5D, 0x7D, 0xE3,
  32186. 0x23, 0xFD, 0xB8, 0x6A, 0xDA, 0x56, 0x1E, 0xA8,
  32187. 0x36, 0xC4, 0x29, 0x2D, 0x70, 0x41, 0x03, 0x18,
  32188. 0x31, 0x40, 0x79, 0x2E, 0xC8, 0x22, 0x98, 0x5E,
  32189. 0x11, 0xED, 0xA6, 0xDD, 0xB9, 0xAF, 0x8C, 0x27,
  32190. 0x5C, 0x1B, 0x2E, 0xEA, 0xB8, 0xC6, 0x2F, 0xA0,
  32191. 0x40, 0xB2, 0x64, 0x61, 0xFC, 0x0A, 0x3A, 0x10,
  32192. 0x88, 0xC2, 0x58, 0xEC, 0xA5, 0x8D, 0x14, 0xE9,
  32193. 0x9D, 0x21, 0xAF, 0x64, 0xD6, 0xC2, 0x5D, 0xAA,
  32194. 0x0B, 0x8A, 0x57, 0x0F, 0x84, 0x3E, 0x60, 0x8D,
  32195. 0xED, 0x05, 0x1D, 0x98, 0xED, 0xAE, 0x11, 0xD9,
  32196. 0x27, 0x03, 0x55, 0xED, 0xF5, 0x34, 0x92, 0x52,
  32197. 0xF2, 0x6F, 0x30, 0x3E, 0x69, 0xA5, 0x54, 0xA7,
  32198. 0x2E, 0x1B, 0x85, 0xAB, 0xA2, 0x3B, 0xEC, 0xC8,
  32199. 0x9D, 0xA9, 0xA3, 0xE4, 0xEF, 0x58, 0xB5, 0x33,
  32200. 0x88, 0x55, 0x16, 0x5E, 0x7D, 0x7E, 0x69, 0xFC,
  32201. 0xCA, 0xBD, 0x9C, 0x65, 0xFA, 0x0B, 0xBD, 0x7B,
  32202. 0x16, 0xC4, 0xE2, 0x9C, 0xB4, 0xF1, 0x6A, 0x25,
  32203. 0x70, 0x30, 0x32, 0xED, 0xEA, 0xD3, 0x1D, 0xDB,
  32204. 0x6F, 0x29, 0x2E, 0x42, 0x14, 0xBE, 0x03, 0x29,
  32205. 0x0A, 0x8A, 0x98, 0x9A, 0xD7, 0xB7, 0x0C, 0xF8,
  32206. 0xB9, 0xCF, 0x37, 0xC6, 0xAC, 0xAC, 0x6D, 0xCC,
  32207. 0x03, 0x23, 0x9F, 0x66, 0x85, 0x4B, 0x70, 0x45
  32208. };
  32209. #endif /* WOLFSSL_NO_ML_DSA_87 */
  32210. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  32211. ExpectNotNull(key);
  32212. if (key != NULL) {
  32213. XMEMSET(key, 0, sizeof(*key));
  32214. }
  32215. ExpectIntEQ(wc_dilithium_init_ex(key, NULL, INVALID_DEVID), 0);
  32216. #ifndef WOLFSSL_NO_ML_DSA_44
  32217. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  32218. ExpectIntEQ(wc_dilithium_make_key_from_seed(key, seed_44), 0);
  32219. ExpectIntEQ(XMEMCMP(key->p, pk_44, sizeof(pk_44)), 0);
  32220. ExpectIntEQ(XMEMCMP(key->k, sk_44, sizeof(sk_44)), 0);
  32221. #endif
  32222. #ifndef WOLFSSL_NO_ML_DSA_65
  32223. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  32224. ExpectIntEQ(wc_dilithium_make_key_from_seed(key, seed_65), 0);
  32225. ExpectIntEQ(XMEMCMP(key->p, pk_65, sizeof(pk_65)), 0);
  32226. ExpectIntEQ(XMEMCMP(key->k, sk_65, sizeof(sk_65)), 0);
  32227. #endif
  32228. #ifndef WOLFSSL_NO_ML_DSA_87
  32229. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  32230. ExpectIntEQ(wc_dilithium_make_key_from_seed(key, seed_87), 0);
  32231. ExpectIntEQ(XMEMCMP(key->p, pk_87, sizeof(pk_87)), 0);
  32232. ExpectIntEQ(XMEMCMP(key->k, sk_87, sizeof(sk_87)), 0);
  32233. #endif
  32234. wc_dilithium_free(key);
  32235. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  32236. #endif
  32237. return EXPECT_RESULT();
  32238. }
  32239. static int test_wc_dilithium_verify_kats(void)
  32240. {
  32241. EXPECT_DECLS;
  32242. #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  32243. dilithium_key* key;
  32244. int res;
  32245. #ifndef WOLFSSL_NO_ML_DSA_44
  32246. static const byte pk_44[] = {
  32247. 0x35, 0x07, 0x31, 0x3A, 0xE3, 0x7A, 0xF6, 0x96,
  32248. 0x6C, 0x11, 0xA9, 0xE4, 0x0B, 0xEB, 0xEC, 0xE9,
  32249. 0x2B, 0x67, 0x3F, 0xD2, 0x67, 0x3C, 0x1C, 0x4C,
  32250. 0x08, 0xF0, 0x45, 0xA9, 0xDD, 0x5A, 0xB8, 0x8C,
  32251. 0x0A, 0x51, 0xA9, 0xBA, 0x89, 0x0F, 0x4C, 0xCB,
  32252. 0x9D, 0x0A, 0x41, 0x3F, 0x9C, 0xF4, 0x13, 0x36,
  32253. 0x79, 0x49, 0x00, 0x90, 0xBB, 0x57, 0x3B, 0xBD,
  32254. 0x2E, 0x18, 0xB3, 0xD0, 0xA5, 0x0E, 0x6B, 0x67,
  32255. 0xFF, 0x98, 0x8C, 0xDD, 0x07, 0xE8, 0xA7, 0xA1,
  32256. 0x3F, 0xAE, 0xFB, 0xD6, 0xC0, 0xF8, 0xF3, 0x34,
  32257. 0xA5, 0x17, 0xC2, 0x34, 0x88, 0x92, 0x65, 0xA6,
  32258. 0xE8, 0x66, 0x57, 0xFE, 0x86, 0x08, 0xF7, 0xDF,
  32259. 0xA0, 0x5B, 0x70, 0x3E, 0x91, 0x6C, 0x63, 0xA0,
  32260. 0xA3, 0x75, 0x55, 0xF8, 0xB6, 0xAA, 0xD4, 0x1B,
  32261. 0x78, 0x5E, 0x42, 0x9F, 0x96, 0xE4, 0xA0, 0x50,
  32262. 0xB6, 0x94, 0x2D, 0xC3, 0xE3, 0x36, 0x2B, 0x9D,
  32263. 0x6B, 0x7A, 0xEF, 0xF5, 0x68, 0xF2, 0x11, 0xDF,
  32264. 0x87, 0xA0, 0x9A, 0xC4, 0x61, 0xFB, 0xA4, 0x1C,
  32265. 0x98, 0x3F, 0xC9, 0x52, 0x59, 0x3B, 0x47, 0x4D,
  32266. 0xF5, 0x24, 0xA3, 0xD8, 0x63, 0xE1, 0xED, 0xDC,
  32267. 0xFD, 0xEB, 0x96, 0xFB, 0xF3, 0xE7, 0x50, 0x9C,
  32268. 0x72, 0x61, 0xC7, 0x3C, 0xCE, 0xF2, 0xEB, 0x22,
  32269. 0x55, 0x6B, 0x9F, 0x25, 0xE4, 0x45, 0xE1, 0xFB,
  32270. 0x3E, 0x2E, 0x4E, 0x92, 0x4F, 0x8A, 0x85, 0xEB,
  32271. 0x63, 0x2C, 0x05, 0x0F, 0x9A, 0xEC, 0x0E, 0x9D,
  32272. 0x05, 0x81, 0x46, 0x82, 0xEA, 0x74, 0x91, 0xD5,
  32273. 0x2A, 0xBC, 0xCC, 0xBB, 0xD6, 0x7F, 0x5F, 0x9A,
  32274. 0xD3, 0xBD, 0xEB, 0x14, 0xBA, 0x84, 0x27, 0x13,
  32275. 0x32, 0xB5, 0xF3, 0x71, 0xAC, 0x47, 0x19, 0x6B,
  32276. 0x5E, 0x43, 0x50, 0xC2, 0xA8, 0x82, 0xF5, 0x97,
  32277. 0x9B, 0x27, 0x62, 0xFB, 0xB7, 0xFF, 0x6B, 0xC8,
  32278. 0x52, 0x1E, 0xFB, 0x97, 0x39, 0x1E, 0x7F, 0x01,
  32279. 0xF8, 0x34, 0x47, 0xAA, 0xB2, 0x64, 0xB5, 0x9E,
  32280. 0x28, 0x18, 0xCB, 0x4A, 0x94, 0xBE, 0x6A, 0x43,
  32281. 0x5B, 0xAE, 0x21, 0xA4, 0x63, 0x64, 0x46, 0x0C,
  32282. 0x6B, 0x36, 0x1C, 0x2A, 0x3B, 0x64, 0xFA, 0xA0,
  32283. 0xAB, 0xE3, 0x3B, 0x7D, 0xB0, 0x23, 0x99, 0x21,
  32284. 0x55, 0x59, 0xBF, 0xD6, 0xDB, 0xB8, 0xDB, 0x09,
  32285. 0x5E, 0xBC, 0x32, 0x3C, 0xAC, 0xAB, 0x1A, 0x63,
  32286. 0x32, 0x21, 0x10, 0xD5, 0x8D, 0x7A, 0x5F, 0xCE,
  32287. 0x72, 0x8D, 0x2A, 0xED, 0x1D, 0x30, 0x38, 0x5D,
  32288. 0x3E, 0x62, 0xC2, 0x8E, 0xC9, 0x9F, 0x8C, 0x50,
  32289. 0x3F, 0xC6, 0xCE, 0x86, 0x4D, 0x67, 0x3D, 0x09,
  32290. 0xB6, 0x27, 0x14, 0x57, 0x14, 0xED, 0xC9, 0x8F,
  32291. 0xAC, 0x9E, 0xAC, 0x6F, 0xB5, 0xB2, 0xE6, 0x8D,
  32292. 0x9D, 0x5E, 0xE6, 0x78, 0x77, 0x09, 0x94, 0x35,
  32293. 0x5E, 0x3B, 0x64, 0x04, 0x38, 0xD4, 0x5B, 0x04,
  32294. 0xB8, 0x6C, 0x33, 0x97, 0xE1, 0x58, 0x54, 0x32,
  32295. 0xB3, 0x0F, 0x37, 0x69, 0x39, 0xCE, 0x57, 0x31,
  32296. 0x5C, 0x75, 0xA8, 0x94, 0xD0, 0x39, 0x2D, 0xB4,
  32297. 0x73, 0xA7, 0xA4, 0x7C, 0xBE, 0x34, 0x03, 0x2D,
  32298. 0x99, 0x1D, 0xDF, 0x32, 0x26, 0xB7, 0x45, 0x1B,
  32299. 0x03, 0xCD, 0xEE, 0x9D, 0x58, 0xA8, 0xA7, 0x76,
  32300. 0x1B, 0x17, 0x42, 0xD9, 0x69, 0x0F, 0x26, 0x3A,
  32301. 0x9D, 0x70, 0x9B, 0x4E, 0x81, 0xEB, 0x96, 0x02,
  32302. 0xB5, 0xB3, 0x92, 0x31, 0xFE, 0xBC, 0x38, 0x11,
  32303. 0x5A, 0x47, 0xED, 0x0A, 0x2A, 0xE2, 0xB8, 0x47,
  32304. 0x13, 0x5E, 0x43, 0x97, 0xD5, 0xFA, 0x31, 0x02,
  32305. 0x58, 0xE9, 0x9E, 0xB5, 0x3F, 0x85, 0x92, 0x0E,
  32306. 0xB9, 0xDB, 0xE0, 0xEE, 0x56, 0x76, 0x64, 0x8F,
  32307. 0xF2, 0xE2, 0x47, 0x81, 0xD4, 0xA4, 0x82, 0x43,
  32308. 0x69, 0xAE, 0x8E, 0x48, 0x50, 0x84, 0x93, 0x3B,
  32309. 0x9C, 0x65, 0xD3, 0x6F, 0xCD, 0x90, 0xA0, 0xD8,
  32310. 0xA0, 0xE1, 0x79, 0xCC, 0xD5, 0x1F, 0x71, 0x73,
  32311. 0x93, 0xE7, 0xB2, 0xB0, 0x78, 0x17, 0xD7, 0x79,
  32312. 0xDE, 0xCC, 0x83, 0x7D, 0x5A, 0xF2, 0x0E, 0xA6,
  32313. 0xB1, 0x76, 0x61, 0x15, 0x88, 0x8E, 0xD7, 0xA6,
  32314. 0x51, 0xBF, 0x9C, 0xD1, 0x0A, 0xFC, 0xDA, 0x65,
  32315. 0xA5, 0x65, 0xFE, 0xB2, 0xED, 0x07, 0x74, 0x42,
  32316. 0x4C, 0xF5, 0x42, 0x3D, 0xAF, 0x5F, 0x4D, 0x72,
  32317. 0x51, 0xE6, 0x3F, 0x68, 0xCC, 0xC5, 0x2D, 0x89,
  32318. 0x01, 0xD8, 0x80, 0xB4, 0xFC, 0xEB, 0x3B, 0xBE,
  32319. 0x7C, 0xFA, 0x24, 0x27, 0xE1, 0x05, 0x94, 0x67,
  32320. 0xAD, 0xB3, 0x47, 0x7D, 0x28, 0x18, 0xC1, 0xC9,
  32321. 0xB8, 0xA1, 0x2A, 0x95, 0xBB, 0x5D, 0xC9, 0x42,
  32322. 0x4F, 0x64, 0x94, 0x07, 0x5F, 0x65, 0xD3, 0xA5,
  32323. 0x65, 0xEE, 0x67, 0x2C, 0x10, 0x65, 0x81, 0x4D,
  32324. 0x7F, 0xAF, 0x2E, 0x97, 0x9E, 0x11, 0xA3, 0xF5,
  32325. 0x3E, 0xDE, 0xB1, 0x1D, 0x44, 0x72, 0x90, 0x74,
  32326. 0xFD, 0x47, 0x82, 0xA6, 0x04, 0x3E, 0x28, 0x3C,
  32327. 0x15, 0xDF, 0xC4, 0x7A, 0x7C, 0xF5, 0x5A, 0xC6,
  32328. 0xFB, 0xE4, 0xC2, 0xE0, 0x6E, 0x4C, 0x09, 0x2E,
  32329. 0xE3, 0xE6, 0x3A, 0xEF, 0xF6, 0x54, 0xDC, 0x92,
  32330. 0xBE, 0x8F, 0x24, 0x8E, 0x70, 0x53, 0x90, 0x3D,
  32331. 0x06, 0xA5, 0x0A, 0x72, 0xA0, 0x7B, 0x22, 0x14,
  32332. 0x80, 0x43, 0xAD, 0xDC, 0x11, 0xFC, 0xFF, 0xCF,
  32333. 0x5E, 0xA4, 0x69, 0x1C, 0x09, 0x09, 0xC3, 0x3D,
  32334. 0xF5, 0xE7, 0x05, 0x6F, 0x16, 0x33, 0x75, 0xB4,
  32335. 0x9B, 0x7B, 0x26, 0xDB, 0xE7, 0x27, 0x56, 0xD3,
  32336. 0x91, 0x82, 0x9D, 0xEB, 0x96, 0x3E, 0xE8, 0x40,
  32337. 0xAB, 0x5D, 0x6C, 0xB7, 0xA6, 0x36, 0x07, 0xD4,
  32338. 0xE7, 0x7C, 0xD4, 0x5C, 0x36, 0xE4, 0xFC, 0x7C,
  32339. 0x8A, 0x36, 0x8D, 0x53, 0x43, 0xD4, 0xAC, 0x0B,
  32340. 0x1B, 0xBA, 0x32, 0x88, 0xFA, 0xCE, 0xC1, 0xB9,
  32341. 0x34, 0x3C, 0xAC, 0xA0, 0xF4, 0xF2, 0x83, 0xA8,
  32342. 0xBB, 0x6F, 0x12, 0xC6, 0xB5, 0x3C, 0xDE, 0xA8,
  32343. 0x49, 0x66, 0x97, 0xD7, 0x7E, 0x37, 0xF7, 0xCE,
  32344. 0x7C, 0xF8, 0xC8, 0xBB, 0x8C, 0xB5, 0x3B, 0x3F,
  32345. 0xB9, 0x51, 0x68, 0x00, 0xD7, 0x2E, 0x1C, 0x10,
  32346. 0xAF, 0x9F, 0x3C, 0xD2, 0xAC, 0xE5, 0xBE, 0x94,
  32347. 0xB9, 0x60, 0xF5, 0xB2, 0x70, 0x24, 0xE8, 0x8A,
  32348. 0x2C, 0xD8, 0x95, 0xAF, 0xAA, 0xA9, 0xA5, 0x2B,
  32349. 0xCA, 0xE0, 0x58, 0x44, 0x02, 0x3F, 0xF8, 0x21,
  32350. 0x0C, 0x29, 0xB7, 0xD5, 0x08, 0x9E, 0x69, 0x81,
  32351. 0xD4, 0x6C, 0xC5, 0x0B, 0xF6, 0xEF, 0xAB, 0x01,
  32352. 0xEA, 0xDF, 0x36, 0x2C, 0x5C, 0xFB, 0xEB, 0xC8,
  32353. 0x4F, 0x71, 0x80, 0xD7, 0x00, 0xC9, 0x32, 0x5D,
  32354. 0x02, 0x4F, 0x96, 0x94, 0x71, 0xCD, 0x98, 0xC4,
  32355. 0x25, 0x7A, 0x92, 0xF1, 0x9B, 0xA0, 0x34, 0x30,
  32356. 0x6C, 0x41, 0x59, 0xD5, 0x01, 0x5D, 0xD6, 0x56,
  32357. 0xEA, 0x05, 0xF2, 0xFC, 0xF8, 0x58, 0xFA, 0x12,
  32358. 0x9C, 0x5A, 0x5C, 0xD5, 0x3D, 0xC7, 0x5D, 0x1B,
  32359. 0x99, 0x2A, 0x6A, 0x4C, 0xF9, 0xEA, 0x9D, 0x70,
  32360. 0x53, 0xBC, 0xBE, 0xAD, 0x61, 0xC7, 0x2D, 0x77,
  32361. 0xEF, 0x61, 0xC7, 0xBE, 0x9C, 0x73, 0xC1, 0xD5,
  32362. 0xD4, 0x5C, 0x5F, 0x21, 0x6A, 0x5C, 0xEE, 0x78,
  32363. 0xAA, 0xC6, 0x6C, 0x56, 0xDB, 0x38, 0x5A, 0x94,
  32364. 0x12, 0xB8, 0x73, 0x7C, 0xDF, 0x9A, 0x27, 0xCD,
  32365. 0xC5, 0xD1, 0xD3, 0xCA, 0x0E, 0x37, 0x0A, 0xC1,
  32366. 0x6F, 0xAD, 0xE3, 0x32, 0x94, 0x6C, 0x20, 0xB5,
  32367. 0xED, 0xE6, 0x2D, 0x34, 0x39, 0x58, 0xD2, 0x1E,
  32368. 0x63, 0x8D, 0xFA, 0xFF, 0xB5, 0xE8, 0x40, 0xC8,
  32369. 0x42, 0x38, 0x7A, 0x01, 0x80, 0xFF, 0x52, 0x3F,
  32370. 0xE9, 0x89, 0x63, 0xAD, 0x91, 0x5F, 0xCE, 0x0A,
  32371. 0x47, 0x87, 0xF9, 0x6D, 0xD7, 0x79, 0xEF, 0xCE,
  32372. 0x10, 0x7B, 0x73, 0x43, 0xBE, 0x51, 0xA0, 0xDF,
  32373. 0xE5, 0xEC, 0xA9, 0x63, 0xF6, 0x5E, 0x72, 0x36,
  32374. 0x22, 0x86, 0xEE, 0x4E, 0x4A, 0x76, 0xFD, 0x86,
  32375. 0xBA, 0xE6, 0xD6, 0xC4, 0xD2, 0xE6, 0xFF, 0xB2,
  32376. 0x5B, 0x39, 0xF9, 0xC3, 0x29, 0xA8, 0x61, 0x3A,
  32377. 0x33, 0x34, 0x89, 0xC9, 0x83, 0xF9, 0xB2, 0x70,
  32378. 0x21, 0x54, 0x44, 0x94, 0x70, 0xAD, 0x70, 0x18,
  32379. 0x84, 0x38, 0x91, 0xFB, 0xDE, 0x5E, 0x3D, 0xE3,
  32380. 0xB2, 0xA7, 0x3C, 0x1D, 0x49, 0xA6, 0x66, 0x7C,
  32381. 0x4B, 0xEB, 0xB0, 0xA7, 0x7C, 0xC5, 0xAE, 0x45,
  32382. 0x1F, 0xBE, 0x0E, 0x2F, 0x11, 0xDC, 0x92, 0x08,
  32383. 0xAA, 0x18, 0x38, 0xFE, 0x61, 0xBE, 0x9D, 0xC3,
  32384. 0x3A, 0x1F, 0x2F, 0xB6, 0x6E, 0xB6, 0x54, 0x97,
  32385. 0x74, 0x06, 0xBC, 0x12, 0x2D, 0x64, 0x18, 0x14,
  32386. 0x25, 0x5A, 0xCB, 0x7B, 0xD7, 0x9D, 0xC3, 0x2C,
  32387. 0xC2, 0x0B, 0x19, 0x10, 0xD2, 0x57, 0xF0, 0xDF,
  32388. 0xA4, 0x95, 0xA4, 0x5A, 0xA0, 0x2D, 0x0F, 0xA0,
  32389. 0xBC, 0xF7, 0x60, 0x7F, 0x38, 0xE1, 0x17, 0x0D,
  32390. 0x36, 0x08, 0xF5, 0xF9, 0x75, 0x28, 0x75, 0xAC,
  32391. 0xA9, 0x2B, 0x75, 0xC4, 0x41, 0xE0, 0x0D, 0x5C,
  32392. 0xBC, 0x5F, 0x49, 0x16, 0x25, 0x38, 0x16, 0xE1,
  32393. 0x0C, 0x2C, 0x9C, 0x63, 0xA8, 0x5F, 0x70, 0xF4,
  32394. 0x64, 0xC7, 0x10, 0x19, 0x52, 0x19, 0x6E, 0x9B,
  32395. 0x5C, 0x09, 0x4F, 0xEE, 0xB6, 0x7C, 0x85, 0xC9,
  32396. 0x6E, 0xCB, 0x33, 0x32, 0x42, 0x9D, 0x57, 0x18,
  32397. 0xE6, 0x55, 0x94, 0x74, 0x02, 0xEE, 0xEB, 0xAA,
  32398. 0xF7, 0xD3, 0x45, 0x7A, 0x49, 0x6F, 0x83, 0x89,
  32399. 0x00, 0xE4, 0xAA, 0x20, 0x87, 0x10, 0xAD, 0xC0,
  32400. 0x0E, 0xF5, 0x93, 0x57, 0xE5, 0x45, 0x7A, 0xBD,
  32401. 0x82, 0x87, 0x50, 0x0F, 0xE1, 0x2C, 0x0C, 0x6D,
  32402. 0xEE, 0xC8, 0x94, 0xB8, 0x39, 0xF3, 0x3C, 0xFE,
  32403. 0x7E, 0xC1, 0x0F, 0xB4, 0x67, 0xA2, 0xDF, 0xC6,
  32404. 0x9D, 0xB5, 0x9D, 0xB8, 0x72, 0x50, 0xBD, 0xB3,
  32405. 0xDB, 0xF6, 0x87, 0x5E, 0x26, 0x93, 0xF0, 0xD4,
  32406. 0x0D, 0x68, 0xA4, 0x8B, 0xBD, 0x2C, 0x6E, 0xD8,
  32407. 0x4F, 0x81, 0x5D, 0x0D, 0xAC, 0x72, 0x65, 0xEC,
  32408. 0x4E, 0xF2, 0x4E, 0x5F, 0x67, 0x04, 0xF3, 0x08,
  32409. 0x29, 0x4D, 0xB2, 0xE2, 0xD5, 0x9F, 0xD4, 0xB9,
  32410. 0x13, 0xB4, 0x33, 0x80, 0x27, 0x84, 0x7E, 0xF4
  32411. };
  32412. static const byte msg_44[] = {
  32413. 0x5C, 0x70, 0x7F, 0xBF, 0xF4, 0xFF, 0xE5, 0x9B,
  32414. 0x09, 0xAA, 0xF8, 0xDB, 0x21, 0xAD, 0xBE, 0xBA,
  32415. 0xC6, 0xB2, 0x65, 0x37, 0x9A, 0x9A, 0x43, 0x3A,
  32416. 0xA8, 0x23, 0x2B, 0x13, 0x9B, 0xBD, 0x46, 0x37,
  32417. 0x30, 0x60, 0xA7, 0x5B, 0xC4, 0x48, 0x63, 0x5F,
  32418. 0x41, 0x35, 0x38, 0x69, 0xF9, 0x6F, 0xB5, 0x65,
  32419. 0x26, 0xDB, 0xAE, 0xB7, 0x5C, 0xFE, 0x2C, 0x03,
  32420. 0xCB, 0x43, 0x08, 0x58, 0x5E, 0x27, 0xD1, 0x42,
  32421. 0x14, 0xF2, 0x4B, 0xD7, 0x13, 0xE4, 0x96, 0x74,
  32422. 0x6A, 0xC1, 0x36, 0xC7, 0x9D, 0x0F, 0x7D, 0xB0,
  32423. 0x7B, 0x8A, 0x3A, 0x6D, 0x00, 0x5B, 0x29, 0x7B,
  32424. 0x37, 0xBA, 0x3F, 0x5B, 0xBD, 0xCE, 0x21, 0x77,
  32425. 0xFD, 0xD6, 0x78, 0x77, 0x20, 0x31, 0xF0, 0x60,
  32426. 0x49, 0xAE, 0x12, 0x86, 0x7A, 0x64, 0xBD, 0x0B,
  32427. 0x9E, 0xC6, 0x26, 0x80, 0x9E, 0xCE, 0x19, 0x8D,
  32428. 0x6A, 0x6B, 0x09, 0x03, 0x45, 0xDF, 0x22, 0x7D
  32429. };
  32430. static const byte sig_44[] = {
  32431. 0x08, 0xF0, 0x10, 0xFA, 0x63, 0x3F, 0x2B, 0xA1,
  32432. 0x46, 0x81, 0x34, 0xC4, 0xBC, 0xAB, 0x62, 0x17,
  32433. 0x0B, 0x64, 0xEA, 0x00, 0x2D, 0xD6, 0x8A, 0xE5,
  32434. 0xC2, 0x45, 0x29, 0xB9, 0xEC, 0x6F, 0x3B, 0xF2,
  32435. 0xDC, 0x2F, 0xC7, 0x34, 0x5A, 0x1E, 0xFE, 0x0C,
  32436. 0xCA, 0xB9, 0x6A, 0xD8, 0xDA, 0xBA, 0xAA, 0x80,
  32437. 0x90, 0xDC, 0x8C, 0x6C, 0x22, 0xFF, 0xC4, 0x90,
  32438. 0x9E, 0xE9, 0xA5, 0x45, 0xFC, 0xE8, 0x64, 0x53,
  32439. 0x9E, 0xC4, 0x17, 0xE1, 0xB2, 0x1A, 0x31, 0x40,
  32440. 0x26, 0x9D, 0x5E, 0x03, 0x6A, 0xC6, 0x09, 0x19,
  32441. 0xDD, 0xB3, 0x63, 0xE0, 0x35, 0xCD, 0xB4, 0x2E,
  32442. 0x25, 0x38, 0x6E, 0x6C, 0x76, 0xA9, 0x19, 0x75,
  32443. 0x68, 0x6E, 0xB7, 0xAB, 0xAD, 0x8F, 0x63, 0x64,
  32444. 0x97, 0x4E, 0x56, 0x82, 0x30, 0x45, 0x86, 0x22,
  32445. 0x64, 0xDA, 0xD2, 0xAE, 0x54, 0x70, 0x5C, 0xF1,
  32446. 0xEB, 0xD1, 0x84, 0x8D, 0xFF, 0x86, 0x15, 0xE6,
  32447. 0x20, 0xCE, 0x14, 0x89, 0xEF, 0xFA, 0x2E, 0xF8,
  32448. 0x60, 0xCA, 0x53, 0x52, 0xE4, 0xD5, 0xC8, 0x2E,
  32449. 0x50, 0xD5, 0x9D, 0x90, 0xA6, 0x12, 0xC7, 0xF1,
  32450. 0x70, 0x0D, 0xE2, 0x89, 0x5B, 0x31, 0x6A, 0x21,
  32451. 0x79, 0x9C, 0xBE, 0x77, 0x6E, 0xA6, 0xBF, 0x51,
  32452. 0x05, 0x2A, 0x83, 0x50, 0x7E, 0x86, 0x14, 0xD1,
  32453. 0x50, 0x53, 0x1F, 0x1C, 0x5E, 0x50, 0x24, 0x69,
  32454. 0x6C, 0x91, 0x55, 0x35, 0x19, 0x6F, 0xE0, 0xDC,
  32455. 0xB5, 0xD6, 0x48, 0x7E, 0x78, 0x61, 0x59, 0x2C,
  32456. 0xD0, 0x1B, 0x42, 0x58, 0xAF, 0x7A, 0x39, 0xCA,
  32457. 0x02, 0x1C, 0x50, 0xEF, 0xE9, 0xE1, 0xDE, 0x31,
  32458. 0x8D, 0x09, 0x51, 0xC9, 0xDB, 0x16, 0xF9, 0xB9,
  32459. 0x45, 0x54, 0x81, 0x16, 0xD7, 0x14, 0xD8, 0xBE,
  32460. 0x9C, 0xCA, 0x53, 0xFE, 0x8F, 0x24, 0x99, 0x0D,
  32461. 0xBA, 0x7F, 0x99, 0x42, 0x11, 0x9B, 0x32, 0xDD,
  32462. 0x93, 0x5C, 0xBA, 0x2D, 0xD3, 0xB3, 0xF2, 0x48,
  32463. 0x13, 0x9C, 0x80, 0xBB, 0x8D, 0xF4, 0xC7, 0xAA,
  32464. 0xEB, 0xC6, 0xFD, 0xB8, 0x35, 0x95, 0x87, 0x2B,
  32465. 0x9E, 0xCF, 0x48, 0xF3, 0x2D, 0xFF, 0x70, 0xF4,
  32466. 0xCE, 0x35, 0x68, 0x7E, 0x9D, 0xDF, 0xD5, 0x0C,
  32467. 0xCD, 0xE3, 0x51, 0xB0, 0x90, 0x86, 0xE5, 0xD1,
  32468. 0xF1, 0x3B, 0x72, 0x42, 0x73, 0x07, 0x03, 0xE2,
  32469. 0xFB, 0x40, 0x3F, 0xD4, 0xC8, 0x30, 0xB6, 0x86,
  32470. 0x49, 0x8A, 0x17, 0xDB, 0x8F, 0x46, 0x6C, 0x3A,
  32471. 0xC3, 0x49, 0xCD, 0x59, 0x68, 0x81, 0x66, 0x03,
  32472. 0xD7, 0x24, 0xAF, 0x1F, 0x77, 0xC7, 0xFB, 0xF7,
  32473. 0x83, 0xCD, 0xA2, 0x6D, 0x35, 0x0C, 0x8B, 0xBC,
  32474. 0x29, 0x3A, 0x7F, 0xAC, 0xB9, 0xF9, 0x78, 0x50,
  32475. 0x6A, 0x67, 0xFC, 0xDC, 0x6F, 0x01, 0x65, 0x06,
  32476. 0x82, 0x81, 0xB0, 0x7D, 0x25, 0x5D, 0x74, 0x0B,
  32477. 0x68, 0x5F, 0x51, 0x2C, 0x82, 0xF3, 0x1D, 0x92,
  32478. 0xF6, 0xA9, 0xA9, 0x6A, 0x77, 0x57, 0x58, 0xAA,
  32479. 0x7C, 0xBE, 0x35, 0xF4, 0x56, 0xDE, 0x42, 0x01,
  32480. 0x2D, 0xB8, 0x28, 0x83, 0x7B, 0xA0, 0xA9, 0x7D,
  32481. 0xC3, 0x30, 0x13, 0x52, 0xD0, 0xA1, 0xC8, 0xA1,
  32482. 0x2C, 0x51, 0x49, 0xAE, 0xA8, 0x04, 0xCB, 0xA8,
  32483. 0x66, 0x01, 0x26, 0xDF, 0x2D, 0x1C, 0x21, 0xA2,
  32484. 0x4E, 0xBD, 0xA5, 0x48, 0x2A, 0x2D, 0x56, 0x60,
  32485. 0x20, 0x98, 0x4D, 0x15, 0x7D, 0x02, 0xB6, 0x3A,
  32486. 0xE4, 0x11, 0xAE, 0xF7, 0x3E, 0x5D, 0x56, 0x4F,
  32487. 0x6A, 0xA3, 0x0A, 0xEA, 0xCC, 0x35, 0x8A, 0xB7,
  32488. 0xC4, 0x8F, 0x25, 0x3E, 0x42, 0x41, 0x2B, 0xA5,
  32489. 0x1F, 0xA7, 0x3B, 0x87, 0x22, 0x86, 0x79, 0xD5,
  32490. 0xE5, 0x2A, 0xA2, 0xCD, 0x68, 0xCE, 0xB8, 0x18,
  32491. 0x6D, 0xEF, 0x1C, 0x36, 0x7F, 0x75, 0x50, 0x36,
  32492. 0x1B, 0x58, 0xEB, 0x32, 0xA1, 0xC8, 0xAF, 0x47,
  32493. 0xE1, 0x26, 0x73, 0x1F, 0x5D, 0x73, 0x30, 0x13,
  32494. 0x2F, 0xC7, 0x8B, 0xA3, 0x03, 0xB4, 0xA8, 0x86,
  32495. 0x25, 0x29, 0xD1, 0x75, 0x10, 0xEE, 0x7F, 0x56,
  32496. 0xBC, 0x0D, 0x59, 0xB4, 0xAE, 0xC9, 0x44, 0x0A,
  32497. 0xF7, 0x0D, 0xBF, 0x17, 0x6A, 0x22, 0x9C, 0x75,
  32498. 0x2B, 0x3E, 0x22, 0xB8, 0x2F, 0x4B, 0x68, 0xF1,
  32499. 0x07, 0xE3, 0x47, 0x47, 0x21, 0x9C, 0xA3, 0x5B,
  32500. 0x31, 0x0A, 0x14, 0xD9, 0x7C, 0xA8, 0xC0, 0xC6,
  32501. 0x5C, 0xAD, 0x05, 0xD6, 0x15, 0xD3, 0xEC, 0xEC,
  32502. 0x32, 0xC2, 0xFF, 0xF4, 0x96, 0x9C, 0xC8, 0x65,
  32503. 0xA0, 0xB2, 0xD6, 0xF4, 0x98, 0xBB, 0xB1, 0x4E,
  32504. 0xA5, 0x11, 0x3B, 0x4E, 0xA8, 0xEB, 0x90, 0xAB,
  32505. 0xD8, 0x25, 0x10, 0xE3, 0x66, 0xB5, 0xA5, 0x11,
  32506. 0x60, 0xA0, 0xCB, 0xDF, 0x77, 0x8A, 0x80, 0x4C,
  32507. 0x07, 0x9B, 0x1B, 0x45, 0x95, 0x29, 0x1D, 0x88,
  32508. 0x85, 0xAC, 0x32, 0x94, 0x26, 0x87, 0x12, 0x0A,
  32509. 0x2F, 0x9E, 0xAE, 0x69, 0x79, 0x25, 0x5A, 0x50,
  32510. 0xF4, 0xDB, 0x15, 0x20, 0x9F, 0x7A, 0x7A, 0xF2,
  32511. 0xE5, 0x8A, 0x63, 0x6A, 0xDD, 0xBD, 0x06, 0xCB,
  32512. 0x42, 0xF0, 0x20, 0xA9, 0x3B, 0x52, 0xD8, 0x68,
  32513. 0x37, 0x71, 0x07, 0xB8, 0x5B, 0xFE, 0xA0, 0xEC,
  32514. 0xBD, 0x75, 0xFF, 0x9C, 0x89, 0xDF, 0x01, 0xE7,
  32515. 0x17, 0x7D, 0xA7, 0xE8, 0x27, 0x9E, 0xA2, 0x41,
  32516. 0x66, 0xE6, 0xDB, 0x8B, 0x5A, 0x3F, 0x6C, 0xC9,
  32517. 0xE3, 0x4F, 0x0D, 0xD0, 0x92, 0x1E, 0x27, 0x41,
  32518. 0xF2, 0xB3, 0x08, 0x32, 0x03, 0x6D, 0x2C, 0x4F,
  32519. 0x78, 0xEC, 0x99, 0xB3, 0x94, 0x6C, 0xC1, 0x89,
  32520. 0xD9, 0x34, 0x0F, 0xEF, 0x10, 0xF0, 0xDA, 0xCE,
  32521. 0x09, 0x69, 0x7A, 0x93, 0xC6, 0xFF, 0x19, 0x4F,
  32522. 0xBD, 0xDE, 0xA6, 0x54, 0x8A, 0xE5, 0x81, 0x3F,
  32523. 0x96, 0xD3, 0xA0, 0x77, 0x7C, 0xF2, 0x4B, 0xF1,
  32524. 0x68, 0xA2, 0x23, 0x3D, 0xD4, 0x16, 0xC1, 0x66,
  32525. 0xDA, 0x13, 0x53, 0xE1, 0x9F, 0x9A, 0x36, 0x09,
  32526. 0x4D, 0x72, 0x08, 0x09, 0xEB, 0x87, 0x74, 0x9A,
  32527. 0xB2, 0x8C, 0x60, 0x7F, 0xFB, 0x70, 0x17, 0x51,
  32528. 0xB1, 0xAC, 0x18, 0xDF, 0xCB, 0x43, 0x2A, 0xD3,
  32529. 0x89, 0xDA, 0x78, 0xAE, 0xDC, 0xEA, 0xB2, 0x22,
  32530. 0xCA, 0x2F, 0xF1, 0xE4, 0xA7, 0xCC, 0xAF, 0xB1,
  32531. 0x63, 0x1B, 0x5D, 0xDD, 0xD1, 0x49, 0xB8, 0x90,
  32532. 0x2E, 0xC9, 0xC0, 0x83, 0x0D, 0xAB, 0x88, 0x88,
  32533. 0x4C, 0x74, 0x72, 0x00, 0x7D, 0xFE, 0xF2, 0x46,
  32534. 0x73, 0xFD, 0x99, 0xEC, 0x89, 0x8B, 0x3B, 0x0F,
  32535. 0xCE, 0x35, 0x5A, 0xEA, 0x13, 0x4F, 0x67, 0x67,
  32536. 0xFD, 0x0D, 0x87, 0xFC, 0xB1, 0x36, 0x48, 0x07,
  32537. 0x33, 0x0B, 0xCA, 0xD4, 0xD7, 0xD0, 0xCC, 0xA1,
  32538. 0x8F, 0xF0, 0x3F, 0x01, 0x8B, 0x6B, 0x74, 0x44,
  32539. 0x2F, 0x1B, 0xE0, 0x65, 0x31, 0x1B, 0x4E, 0xDB,
  32540. 0x67, 0x65, 0xA9, 0x34, 0xE8, 0x4D, 0x0C, 0xF3,
  32541. 0x29, 0xED, 0x53, 0xAB, 0x8A, 0x98, 0x07, 0x2B,
  32542. 0xE0, 0xCD, 0xC0, 0x08, 0x82, 0x4A, 0x72, 0x28,
  32543. 0x72, 0xA2, 0xAC, 0xFE, 0xF7, 0xBF, 0x6E, 0x8E,
  32544. 0xF8, 0x3E, 0x04, 0x58, 0xA4, 0x36, 0x46, 0x33,
  32545. 0xAB, 0xDD, 0x0E, 0xBF, 0x01, 0xD2, 0xEF, 0x19,
  32546. 0x5B, 0x78, 0x2B, 0x30, 0x51, 0x25, 0x50, 0xD0,
  32547. 0xB5, 0x82, 0xC7, 0x20, 0x0D, 0xA1, 0x2C, 0x38,
  32548. 0xAF, 0x44, 0xFC, 0xBD, 0x49, 0xB8, 0x7F, 0x89,
  32549. 0xEF, 0xBE, 0x37, 0x5C, 0xCB, 0xA2, 0x11, 0x75,
  32550. 0x7D, 0xDA, 0xA8, 0x7B, 0x3A, 0x3C, 0x10, 0x11,
  32551. 0x4D, 0x9F, 0x99, 0xAB, 0x4B, 0xA2, 0x20, 0x7A,
  32552. 0x5F, 0x96, 0xEF, 0x1C, 0x00, 0xD7, 0x27, 0x17,
  32553. 0x77, 0x7C, 0x51, 0x58, 0x4B, 0x13, 0x97, 0x53,
  32554. 0x2A, 0xC6, 0x86, 0x4D, 0x3B, 0x8E, 0xBB, 0x4F,
  32555. 0xB8, 0xA0, 0x84, 0x87, 0xF6, 0xEF, 0x55, 0x12,
  32556. 0x2B, 0xCF, 0x9E, 0x5C, 0xD0, 0x0E, 0xBC, 0x1E,
  32557. 0x79, 0x53, 0xE7, 0x8C, 0x4D, 0x8B, 0xCB, 0x20,
  32558. 0xF6, 0xEA, 0x72, 0x0A, 0x63, 0x2F, 0x0C, 0xCF,
  32559. 0x57, 0x27, 0x26, 0xF4, 0x3A, 0x95, 0xCA, 0xBE,
  32560. 0xB5, 0x7C, 0x47, 0x60, 0x10, 0xCD, 0x28, 0x9E,
  32561. 0x02, 0x64, 0xC9, 0x8D, 0x82, 0x49, 0xD0, 0xD6,
  32562. 0x60, 0xF8, 0xDC, 0xC8, 0x4B, 0x7D, 0xB5, 0xEF,
  32563. 0x11, 0x17, 0xC7, 0x94, 0x5F, 0x0D, 0x99, 0xBE,
  32564. 0x75, 0x48, 0x49, 0xC6, 0x58, 0x43, 0x64, 0x99,
  32565. 0x1A, 0x5A, 0x41, 0xBA, 0xC2, 0x31, 0xB3, 0xE0,
  32566. 0x45, 0x1B, 0x81, 0xD2, 0x12, 0xBE, 0x90, 0xDB,
  32567. 0xFF, 0xBC, 0xCB, 0x99, 0xA3, 0xF0, 0x74, 0xE8,
  32568. 0x2C, 0x48, 0x58, 0xB3, 0x17, 0xA4, 0x9A, 0xD2,
  32569. 0x22, 0x46, 0xFB, 0xF5, 0x85, 0x8D, 0x07, 0xDF,
  32570. 0xDB, 0x78, 0x07, 0xF4, 0x99, 0xA8, 0x6C, 0xEE,
  32571. 0x6E, 0x96, 0x20, 0xB8, 0xC2, 0xA9, 0xFA, 0x8B,
  32572. 0x6E, 0xA6, 0x79, 0x6D, 0xF9, 0xC3, 0x0C, 0x77,
  32573. 0x74, 0xAE, 0xB0, 0x40, 0xA9, 0xE5, 0xA7, 0x0B,
  32574. 0x30, 0x40, 0x4B, 0x4F, 0xB1, 0x0A, 0x0B, 0x7B,
  32575. 0xEE, 0x1F, 0x69, 0xFA, 0xD0, 0xF0, 0x2D, 0x5D,
  32576. 0x00, 0xB5, 0x4D, 0xEB, 0x32, 0x84, 0xB2, 0xB7,
  32577. 0x60, 0xAA, 0x6C, 0xF9, 0x98, 0x18, 0xB3, 0xD9,
  32578. 0xC1, 0x54, 0x8D, 0xAC, 0x12, 0xB0, 0x3A, 0x26,
  32579. 0xB2, 0x23, 0x2D, 0x9B, 0xF8, 0x20, 0xEE, 0x90,
  32580. 0xE0, 0x6D, 0x31, 0xDE, 0xF5, 0xCA, 0xBA, 0x6A,
  32581. 0x53, 0x40, 0x29, 0x6C, 0x18, 0x62, 0xA5, 0x8A,
  32582. 0xB8, 0x17, 0xA0, 0xAB, 0xCB, 0xDC, 0xE1, 0x3B,
  32583. 0xD6, 0xC6, 0x29, 0xA3, 0x1C, 0x5F, 0x8D, 0x6E,
  32584. 0x73, 0xF6, 0x98, 0x10, 0x0F, 0x9F, 0x7E, 0xCA,
  32585. 0x4C, 0xD8, 0xEB, 0xE4, 0xB8, 0xDF, 0x72, 0x78,
  32586. 0x65, 0xAF, 0x4A, 0x20, 0xFE, 0x7C, 0xB4, 0xCA,
  32587. 0x07, 0x81, 0xFD, 0xC5, 0xC5, 0xFD, 0x33, 0x4D,
  32588. 0xB8, 0x37, 0x37, 0xC4, 0x21, 0x81, 0x66, 0x45,
  32589. 0xAE, 0x81, 0x34, 0x13, 0xA6, 0x40, 0x81, 0x39,
  32590. 0x55, 0x90, 0xE6, 0xF1, 0x42, 0x56, 0x74, 0xFF,
  32591. 0x06, 0x9B, 0x50, 0x1F, 0x0F, 0xDA, 0x6B, 0x31,
  32592. 0xC6, 0x4B, 0xC5, 0xC2, 0x14, 0xE7, 0x01, 0x5E,
  32593. 0xA9, 0xDA, 0x12, 0x2D, 0x6C, 0xE0, 0x8C, 0xEB,
  32594. 0x2D, 0xF6, 0x2C, 0x45, 0xBC, 0x01, 0x73, 0x34,
  32595. 0x6D, 0xAB, 0xBC, 0x15, 0x4C, 0x16, 0x03, 0x35,
  32596. 0x9D, 0xD4, 0xF0, 0xAC, 0x49, 0x84, 0x4A, 0xEE,
  32597. 0x46, 0x47, 0x64, 0x93, 0xF2, 0x49, 0x59, 0x86,
  32598. 0x26, 0xFB, 0x24, 0x6B, 0x99, 0xB3, 0x9A, 0xCB,
  32599. 0xB4, 0x2B, 0x28, 0x4E, 0x0C, 0x2D, 0x3F, 0x9E,
  32600. 0xCE, 0x32, 0x71, 0xC4, 0xD5, 0xE0, 0x6C, 0x48,
  32601. 0x25, 0xEA, 0x1A, 0x8F, 0x08, 0x57, 0x23, 0x85,
  32602. 0x89, 0xCD, 0xC5, 0x48, 0x37, 0x19, 0x8E, 0xD4,
  32603. 0x23, 0x4D, 0xD0, 0x31, 0x73, 0xA8, 0x8E, 0x43,
  32604. 0xEE, 0x95, 0x67, 0xF5, 0x7A, 0x93, 0x27, 0xD3,
  32605. 0x90, 0x36, 0x30, 0x4C, 0xA1, 0xCD, 0xB5, 0xF8,
  32606. 0x65, 0xC5, 0x89, 0x54, 0x57, 0x2C, 0xAE, 0xF8,
  32607. 0x75, 0xF1, 0x2E, 0x14, 0x14, 0x14, 0x0D, 0x97,
  32608. 0x5B, 0x24, 0x52, 0x46, 0x7A, 0x57, 0x6D, 0x9C,
  32609. 0x4C, 0x79, 0xDB, 0x0A, 0xE0, 0x23, 0x69, 0x52,
  32610. 0x9B, 0xF8, 0x1B, 0x54, 0x40, 0x18, 0xDF, 0xE0,
  32611. 0x1E, 0xF0, 0x61, 0xE4, 0x79, 0x81, 0xF9, 0x98,
  32612. 0x9A, 0x8C, 0x48, 0xFF, 0x86, 0x93, 0x0B, 0x68,
  32613. 0x96, 0x78, 0x2F, 0xF1, 0x2D, 0xDC, 0x60, 0x1F,
  32614. 0x8B, 0x1C, 0x04, 0x43, 0x4E, 0x60, 0x96, 0x5B,
  32615. 0x8A, 0xF6, 0x89, 0xCC, 0xC8, 0xB2, 0x9B, 0xBF,
  32616. 0x87, 0x16, 0x2E, 0xA8, 0x6F, 0x9B, 0x4B, 0xFD,
  32617. 0x74, 0x4E, 0x8F, 0x36, 0x33, 0x23, 0xDE, 0x94,
  32618. 0xD2, 0xA1, 0x72, 0x4F, 0xB2, 0xE6, 0x75, 0x3D,
  32619. 0x6E, 0x47, 0x9B, 0xDB, 0x58, 0xE5, 0x4A, 0x0C,
  32620. 0x09, 0x8F, 0x9C, 0x83, 0x63, 0x98, 0x8B, 0xA4,
  32621. 0xF7, 0x3D, 0x01, 0xA6, 0x8B, 0x93, 0x97, 0x48,
  32622. 0x84, 0x75, 0x32, 0xC7, 0xD7, 0x03, 0xDF, 0x7E,
  32623. 0x94, 0x8C, 0x8A, 0xA6, 0x78, 0x1A, 0xAE, 0xDE,
  32624. 0x36, 0x8A, 0xAD, 0x13, 0x7E, 0xF0, 0x16, 0xC2,
  32625. 0x3B, 0xAF, 0xF9, 0xD8, 0x66, 0x12, 0x30, 0x72,
  32626. 0x76, 0x6D, 0x21, 0x4C, 0xF3, 0xEF, 0x0D, 0x8C,
  32627. 0x11, 0xA4, 0x12, 0xBE, 0xF5, 0x7E, 0x8E, 0x6A,
  32628. 0x11, 0x13, 0x48, 0x8D, 0xC2, 0x62, 0xCF, 0x45,
  32629. 0x7C, 0xE3, 0x91, 0x88, 0x59, 0xFF, 0xB0, 0xF1,
  32630. 0xC3, 0xBC, 0x1D, 0x2A, 0x3E, 0x9B, 0x78, 0xF3,
  32631. 0xB1, 0x2E, 0xB0, 0x27, 0xD8, 0x16, 0xF8, 0x9B,
  32632. 0x2A, 0xAF, 0xF1, 0xAB, 0xB0, 0xF1, 0x8C, 0x7F,
  32633. 0x94, 0x31, 0x97, 0x85, 0xDA, 0xF0, 0xF4, 0x27,
  32634. 0x51, 0x3E, 0x5A, 0xE1, 0xDD, 0x6D, 0x9E, 0x98,
  32635. 0x39, 0xBB, 0xDF, 0xA2, 0xBA, 0x2C, 0x08, 0xAD,
  32636. 0x1D, 0x3F, 0x86, 0xF6, 0xC2, 0x1A, 0x8C, 0xAD,
  32637. 0xE0, 0xDC, 0xDD, 0x02, 0x47, 0x4C, 0x7E, 0x2D,
  32638. 0xDA, 0x1D, 0x70, 0x92, 0x39, 0xAA, 0x4E, 0xBA,
  32639. 0x14, 0xC7, 0xEC, 0x26, 0xBD, 0x9D, 0x1F, 0x6D,
  32640. 0x91, 0x58, 0x3C, 0xB5, 0xEF, 0x37, 0xB9, 0x66,
  32641. 0x4E, 0x04, 0x7C, 0x29, 0xCF, 0xD7, 0x8E, 0x47,
  32642. 0x84, 0xF3, 0xD2, 0x21, 0x84, 0xC5, 0xF8, 0xDC,
  32643. 0xC9, 0xF2, 0x52, 0xD5, 0x6A, 0xBF, 0xF1, 0xF1,
  32644. 0xDE, 0x9E, 0x7A, 0xF1, 0xD5, 0x5A, 0xF6, 0xEF,
  32645. 0x94, 0x66, 0xF9, 0x25, 0x44, 0x7F, 0x8D, 0x92,
  32646. 0xA2, 0x25, 0x1C, 0x72, 0x92, 0x30, 0x2A, 0xB7,
  32647. 0xEF, 0x18, 0xF3, 0x8C, 0xEF, 0x69, 0xA5, 0x5C,
  32648. 0x19, 0x3E, 0xC5, 0xBD, 0xEE, 0x2C, 0x2D, 0x71,
  32649. 0xDB, 0x89, 0xD4, 0x11, 0xA6, 0x27, 0x80, 0x8F,
  32650. 0x5A, 0x39, 0x9A, 0x04, 0x28, 0x4F, 0x9F, 0x00,
  32651. 0xBE, 0xF9, 0xF7, 0x9B, 0x46, 0x69, 0xD6, 0xAC,
  32652. 0x12, 0xE9, 0xA7, 0xC2, 0xD1, 0xC8, 0xAD, 0x5D,
  32653. 0xF7, 0xCB, 0x0C, 0x98, 0x78, 0x2D, 0x04, 0x4D,
  32654. 0x2D, 0x41, 0xAB, 0xC6, 0x3F, 0x81, 0x1D, 0xB9,
  32655. 0x2C, 0x1F, 0x3F, 0x59, 0x11, 0xF4, 0x80, 0x4F,
  32656. 0x0B, 0xCA, 0x9F, 0x81, 0x6E, 0x9C, 0xD1, 0xB4,
  32657. 0x74, 0x06, 0x48, 0x0A, 0x87, 0x2C, 0xFD, 0x4D,
  32658. 0x85, 0xD4, 0x21, 0x65, 0x7C, 0x96, 0x69, 0x53,
  32659. 0x51, 0xC0, 0xC4, 0xB0, 0xEB, 0x20, 0xDB, 0xE0,
  32660. 0x41, 0x09, 0xA7, 0x62, 0xB2, 0xF3, 0xC7, 0x6A,
  32661. 0x1D, 0x53, 0xA0, 0x39, 0xBA, 0xCF, 0x78, 0x9E,
  32662. 0xBF, 0x1D, 0xA5, 0x98, 0x09, 0x8E, 0xA7, 0x1A,
  32663. 0xE7, 0x95, 0xFF, 0x10, 0x38, 0xCC, 0x8F, 0x44,
  32664. 0xCB, 0xE7, 0xF6, 0xD6, 0x2C, 0xFF, 0xA8, 0x1C,
  32665. 0xFF, 0xA3, 0x65, 0xE8, 0x4E, 0xAE, 0xC7, 0xEF,
  32666. 0x61, 0xE1, 0x16, 0x4B, 0x8C, 0xA8, 0xC8, 0xFB,
  32667. 0xA5, 0x2C, 0xD1, 0x0A, 0x39, 0xAB, 0x4A, 0xF9,
  32668. 0xEE, 0x0B, 0x9B, 0xB4, 0x33, 0x5E, 0x25, 0x15,
  32669. 0xD0, 0xAA, 0x93, 0xC4, 0x53, 0x42, 0x91, 0xC5,
  32670. 0x98, 0x15, 0x34, 0x9A, 0x22, 0x1D, 0x9A, 0xE7,
  32671. 0x0E, 0x81, 0xF6, 0x99, 0x55, 0xB3, 0xD6, 0x49,
  32672. 0x1B, 0xB8, 0xA8, 0xBE, 0xDF, 0x54, 0xF0, 0x78,
  32673. 0xF7, 0x02, 0x97, 0x74, 0x84, 0x67, 0x6B, 0xAE,
  32674. 0x2F, 0xEC, 0x6E, 0x59, 0x20, 0x68, 0xD8, 0xE3,
  32675. 0x5A, 0x07, 0x48, 0xE1, 0x99, 0x90, 0xEE, 0xCD,
  32676. 0x17, 0x2B, 0xB6, 0xD6, 0xAA, 0x1A, 0xF8, 0x97,
  32677. 0x4E, 0xE0, 0x67, 0x9E, 0x4C, 0x35, 0xFE, 0x68,
  32678. 0x71, 0x54, 0x43, 0x5D, 0x43, 0x59, 0x19, 0xEB,
  32679. 0x58, 0x8E, 0x9A, 0xF6, 0xBD, 0x88, 0x71, 0xEE,
  32680. 0x89, 0xC6, 0xF2, 0x10, 0x04, 0x33, 0x13, 0x88,
  32681. 0xCD, 0x08, 0xB5, 0xE3, 0x5D, 0xA8, 0xBC, 0x43,
  32682. 0xB3, 0x84, 0x5F, 0x70, 0x94, 0xD9, 0xAC, 0xAE,
  32683. 0x74, 0x70, 0x13, 0x1E, 0x21, 0xFB, 0xD5, 0x7F,
  32684. 0xEC, 0x66, 0x2F, 0xA0, 0xB1, 0x1D, 0xE3, 0xF8,
  32685. 0xB9, 0x36, 0x48, 0x25, 0x3D, 0xBA, 0x7D, 0x44,
  32686. 0x08, 0xC5, 0x71, 0x74, 0xDA, 0xD3, 0x4F, 0x97,
  32687. 0x86, 0xF1, 0x16, 0x38, 0xD8, 0xC9, 0xE3, 0x3A,
  32688. 0xA7, 0x2E, 0x06, 0x4D, 0x9D, 0xE8, 0xFC, 0x38,
  32689. 0x58, 0x2A, 0x8D, 0x2D, 0x07, 0x99, 0xEA, 0xDF,
  32690. 0xF3, 0x00, 0x3B, 0xBC, 0x5F, 0x67, 0x1E, 0x4B,
  32691. 0x6C, 0xF1, 0x4A, 0x47, 0xB0, 0x71, 0x90, 0x5A,
  32692. 0x3B, 0x75, 0x93, 0x75, 0x56, 0x50, 0x4C, 0x70,
  32693. 0xF3, 0xC7, 0x95, 0xD5, 0xEA, 0xCB, 0x4C, 0x92,
  32694. 0x4F, 0x22, 0x4F, 0xD9, 0x34, 0x46, 0x76, 0xFB,
  32695. 0x79, 0xD6, 0xBD, 0x4E, 0x84, 0xEE, 0xE7, 0x78,
  32696. 0x7C, 0xB8, 0x92, 0x9F, 0xAD, 0xF2, 0x17, 0x5D,
  32697. 0x38, 0xB1, 0x88, 0x2E, 0xE9, 0x65, 0xAC, 0x4C,
  32698. 0x24, 0x27, 0x1D, 0x7B, 0xA3, 0x69, 0x96, 0x55,
  32699. 0x5C, 0x26, 0x40, 0xAF, 0x04, 0xB1, 0xCE, 0xA8,
  32700. 0x5D, 0x1E, 0x1F, 0xE5, 0x5A, 0xC3, 0xAE, 0xF9,
  32701. 0x14, 0x03, 0x58, 0x10, 0x1C, 0x8B, 0x1F, 0xDB,
  32702. 0x6C, 0x71, 0x68, 0x60, 0x13, 0x32, 0xF1, 0xA9,
  32703. 0x69, 0x45, 0x28, 0x69, 0x7C, 0xE3, 0xC9, 0x56,
  32704. 0xAF, 0xF3, 0xBD, 0x4B, 0x9E, 0x0A, 0x06, 0x6A,
  32705. 0x62, 0x20, 0x40, 0x65, 0xBD, 0xBC, 0xBF, 0xC7,
  32706. 0x0A, 0x2A, 0xCF, 0x56, 0x7C, 0x0E, 0x64, 0xBB,
  32707. 0x64, 0x71, 0x2D, 0x90, 0xBB, 0x32, 0x00, 0x0A,
  32708. 0x4A, 0x45, 0x44, 0x08, 0x75, 0x2C, 0x86, 0x13,
  32709. 0x86, 0x52, 0x8D, 0x3D, 0xFC, 0xF3, 0x5E, 0x5B,
  32710. 0x3F, 0x7A, 0xAA, 0x98, 0x84, 0xCF, 0x92, 0xF9,
  32711. 0x0B, 0x40, 0x8F, 0xC0, 0xA3, 0x71, 0x84, 0xAD,
  32712. 0xEE, 0xDF, 0xC4, 0x91, 0x7E, 0x87, 0x7D, 0x06,
  32713. 0xCA, 0x65, 0x8C, 0xE4, 0x8E, 0x03, 0xF0, 0x59,
  32714. 0x3E, 0xB4, 0x90, 0x4C, 0xEE, 0x88, 0x29, 0xE4,
  32715. 0x26, 0x7D, 0xA6, 0x54, 0x82, 0x49, 0xC1, 0x9D,
  32716. 0x80, 0xAB, 0x6B, 0xD7, 0xBE, 0x7D, 0x09, 0x80,
  32717. 0x5E, 0xB6, 0xD1, 0x1E, 0xD1, 0x1B, 0xE9, 0x8D,
  32718. 0xFC, 0x6E, 0x9C, 0x14, 0x0C, 0x15, 0x02, 0x87,
  32719. 0xF3, 0x9D, 0x21, 0xF8, 0xCB, 0xC8, 0xB9, 0xBD,
  32720. 0xE1, 0x70, 0xEA, 0xE4, 0x86, 0x4C, 0x97, 0xC1,
  32721. 0xEE, 0x4C, 0x18, 0x95, 0xEC, 0xD2, 0x4D, 0x35,
  32722. 0x9F, 0xC6, 0x56, 0x10, 0x3E, 0xC0, 0xB9, 0x7B,
  32723. 0x13, 0x1A, 0x37, 0x3D, 0x40, 0x4C, 0x88, 0x8B,
  32724. 0x9A, 0xA5, 0xB2, 0xB8, 0xB9, 0xC3, 0xEC, 0xF1,
  32725. 0x14, 0x33, 0x63, 0x67, 0x84, 0x98, 0xC8, 0xF4,
  32726. 0x06, 0x0C, 0x0E, 0x0F, 0x10, 0x12, 0x15, 0x16,
  32727. 0x45, 0x4E, 0x55, 0x5A, 0x5F, 0x8A, 0x94, 0x97,
  32728. 0xA8, 0xAF, 0xB2, 0xCC, 0xD4, 0xDC, 0xE7, 0xF1,
  32729. 0xFE, 0xFF, 0x11, 0x24, 0x53, 0x62, 0x94, 0xB7,
  32730. 0xB9, 0xD3, 0xD9, 0x00, 0x00, 0x00, 0x00, 0x00,
  32731. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32732. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32733. 0x10, 0x18, 0x32, 0x3B
  32734. };
  32735. #endif
  32736. #ifndef WOLFSSL_NO_ML_DSA_65
  32737. static const byte pk_65[] = {
  32738. 0x6C, 0x84, 0x14, 0x38, 0x08, 0x56, 0xCB, 0x52,
  32739. 0xD7, 0x9C, 0x4B, 0x29, 0x13, 0x9F, 0xB1, 0x83,
  32740. 0x9B, 0x86, 0x06, 0xF5, 0x94, 0x8B, 0x9D, 0x72,
  32741. 0xA9, 0x56, 0xDC, 0xF1, 0x01, 0x16, 0xDA, 0x9E,
  32742. 0x2D, 0x79, 0x77, 0x01, 0x86, 0xFC, 0x74, 0xD9,
  32743. 0x42, 0xC0, 0xF4, 0xA3, 0xB5, 0x95, 0xFF, 0x6C,
  32744. 0x19, 0x80, 0x4B, 0x49, 0x90, 0x1C, 0x6A, 0xD5,
  32745. 0xFA, 0xF7, 0x16, 0x01, 0xC2, 0xB6, 0x00, 0x31,
  32746. 0x5E, 0x1F, 0x40, 0xC2, 0x05, 0x47, 0x67, 0xB0,
  32747. 0x09, 0x25, 0xDF, 0x3A, 0xA4, 0x90, 0xE8, 0xC7,
  32748. 0x6F, 0x05, 0xFB, 0xFB, 0x74, 0x91, 0x10, 0x75,
  32749. 0xE6, 0x51, 0x8C, 0x5F, 0x1D, 0x91, 0xB8, 0xA0,
  32750. 0xE5, 0xB5, 0x98, 0x30, 0xD3, 0xDF, 0x39, 0x94,
  32751. 0x76, 0x04, 0x11, 0xEB, 0xB9, 0x11, 0xED, 0x4C,
  32752. 0xC2, 0xC1, 0x60, 0xE3, 0x84, 0x9A, 0x93, 0x76,
  32753. 0x2D, 0xFC, 0xA7, 0xB9, 0x81, 0x2B, 0xC7, 0xAE,
  32754. 0xB2, 0xDD, 0xB2, 0x76, 0x7B, 0xEF, 0x36, 0x50,
  32755. 0x56, 0x05, 0xAE, 0x06, 0x92, 0x60, 0xBC, 0xC8,
  32756. 0xDC, 0x47, 0x87, 0xC4, 0x28, 0xCB, 0x3C, 0x07,
  32757. 0x6E, 0xF2, 0xA6, 0xB9, 0x35, 0x61, 0xD8, 0x94,
  32758. 0x3F, 0x45, 0xCA, 0xBE, 0x8F, 0x05, 0x53, 0xFF,
  32759. 0x2E, 0xA1, 0xAC, 0x95, 0xC1, 0xCE, 0x21, 0x59,
  32760. 0x3A, 0x17, 0x54, 0x59, 0xD7, 0xDF, 0x12, 0xC4,
  32761. 0x07, 0x0A, 0xDB, 0x0E, 0xEE, 0x55, 0xB4, 0xAB,
  32762. 0xAE, 0x59, 0xBE, 0x69, 0xC3, 0xFF, 0x0D, 0xE5,
  32763. 0xA9, 0xB0, 0x27, 0xFC, 0x7D, 0x8E, 0x6E, 0x05,
  32764. 0x7B, 0x71, 0x52, 0xEE, 0x6A, 0xB4, 0x80, 0xD1,
  32765. 0x05, 0xD3, 0x0B, 0x0F, 0x50, 0x51, 0xB6, 0x0C,
  32766. 0x79, 0x01, 0xC5, 0x25, 0xC4, 0x63, 0x5F, 0xE6,
  32767. 0x68, 0xCC, 0x00, 0xE9, 0xD3, 0x09, 0x7D, 0xB9,
  32768. 0x9D, 0x66, 0x32, 0x37, 0x15, 0xCE, 0x4F, 0x0B,
  32769. 0x79, 0xB4, 0x26, 0xB4, 0x54, 0x5E, 0x09, 0xF4,
  32770. 0xDE, 0x39, 0x32, 0x3D, 0xD1, 0x4C, 0xCB, 0x0D,
  32771. 0x17, 0x10, 0x8C, 0xD4, 0x6D, 0xEC, 0x61, 0x38,
  32772. 0xCD, 0xFA, 0x28, 0x72, 0xC1, 0xC4, 0xC8, 0xAE,
  32773. 0xAD, 0x5C, 0x8C, 0xE0, 0x41, 0x57, 0xE5, 0x53,
  32774. 0xA3, 0x75, 0x58, 0xC2, 0x34, 0x6A, 0x06, 0x19,
  32775. 0x4C, 0xB5, 0x0B, 0x49, 0x81, 0xBF, 0x4D, 0x09,
  32776. 0x0C, 0xE4, 0xE8, 0x60, 0x12, 0x6A, 0x82, 0x54,
  32777. 0xA4, 0xD4, 0xC0, 0x84, 0xC3, 0xE2, 0x02, 0x0B,
  32778. 0xC0, 0x75, 0x35, 0x21, 0x04, 0x9B, 0x0F, 0xD8,
  32779. 0x89, 0x97, 0xE0, 0x27, 0xAC, 0x51, 0xE7, 0x5C,
  32780. 0xF1, 0x35, 0x0C, 0x3F, 0x30, 0x3A, 0x0E, 0xCE,
  32781. 0x42, 0x64, 0x87, 0x15, 0x3D, 0xAF, 0x1F, 0xAA,
  32782. 0xD6, 0x80, 0x8B, 0x9D, 0x99, 0x07, 0xDA, 0x9F,
  32783. 0x35, 0x18, 0x5B, 0xD3, 0xBE, 0x8D, 0x9C, 0xEB,
  32784. 0xE9, 0x16, 0xCE, 0xD1, 0xFA, 0x29, 0x28, 0xD8,
  32785. 0x85, 0xA9, 0xCB, 0xA8, 0x81, 0x49, 0x70, 0x3F,
  32786. 0x5E, 0x47, 0x72, 0xE4, 0x85, 0x23, 0x12, 0x5D,
  32787. 0xDD, 0x02, 0x6E, 0x71, 0x4C, 0x49, 0xF4, 0xFB,
  32788. 0x4E, 0x54, 0x4B, 0xBF, 0x61, 0x7A, 0x40, 0xB0,
  32789. 0x0B, 0x68, 0xDF, 0x8F, 0x15, 0x5F, 0x58, 0x80,
  32790. 0xD4, 0x11, 0x87, 0x7E, 0x25, 0xB4, 0x2B, 0x24,
  32791. 0x48, 0xB3, 0x6B, 0xEC, 0x2F, 0x1F, 0x8F, 0x9A,
  32792. 0x77, 0x0C, 0x54, 0x51, 0x50, 0xA0, 0x27, 0x8E,
  32793. 0x9B, 0x72, 0x45, 0x00, 0xAE, 0xAA, 0xEA, 0x47,
  32794. 0x1C, 0x11, 0xCF, 0xF0, 0x4E, 0x30, 0xEA, 0xB2,
  32795. 0xF4, 0x73, 0xBC, 0x04, 0x8E, 0x32, 0xCD, 0x31,
  32796. 0xAE, 0xF2, 0x15, 0x79, 0xB6, 0x99, 0x22, 0x5B,
  32797. 0xF9, 0xE1, 0xB6, 0x70, 0x0C, 0x57, 0xE5, 0x09,
  32798. 0xFC, 0xA1, 0xF2, 0x36, 0x29, 0x4A, 0x59, 0x74,
  32799. 0xDA, 0xA1, 0x5F, 0xBC, 0xAD, 0x62, 0xD4, 0xBD,
  32800. 0xDC, 0x45, 0x32, 0xB2, 0x61, 0x41, 0x44, 0xDB,
  32801. 0xE2, 0x88, 0x07, 0x36, 0x8C, 0x28, 0x1A, 0x77,
  32802. 0x0E, 0xA2, 0x2B, 0x1E, 0x5A, 0x3F, 0xA5, 0xBA,
  32803. 0x14, 0x92, 0x6D, 0xC5, 0x5A, 0x54, 0xF8, 0x4A,
  32804. 0x2A, 0x77, 0xC5, 0xA7, 0x08, 0x41, 0xF0, 0x7B,
  32805. 0xC1, 0xDE, 0xEF, 0x74, 0x03, 0xB2, 0x47, 0xAB,
  32806. 0x42, 0xB8, 0x4A, 0xDF, 0x14, 0x1E, 0x03, 0x0C,
  32807. 0x98, 0x46, 0x84, 0x24, 0xDA, 0xAE, 0xB9, 0x9D,
  32808. 0x25, 0x77, 0xF9, 0x50, 0xC2, 0x37, 0x3C, 0xCA,
  32809. 0x1E, 0x2D, 0xC2, 0x76, 0x1B, 0x8E, 0xDD, 0x6D,
  32810. 0x08, 0xFF, 0x79, 0xE5, 0x28, 0x88, 0x0F, 0xFB,
  32811. 0x51, 0xC3, 0x6E, 0xD4, 0x20, 0xAC, 0x5D, 0x50,
  32812. 0xF2, 0x58, 0x2A, 0xA6, 0x64, 0xE5, 0x4E, 0xA5,
  32813. 0xF4, 0x18, 0x9E, 0xA0, 0x17, 0x6D, 0xAA, 0x61,
  32814. 0x22, 0xF6, 0x23, 0x5A, 0x70, 0xB1, 0x5C, 0xEB,
  32815. 0x4D, 0xDD, 0x65, 0xD3, 0xBE, 0x6E, 0xBF, 0x3D,
  32816. 0xC4, 0x31, 0x89, 0xEE, 0x0A, 0x2E, 0x31, 0x05,
  32817. 0x63, 0x8F, 0x23, 0x87, 0x36, 0x95, 0x28, 0x0F,
  32818. 0x1B, 0x74, 0x27, 0x43, 0x52, 0xD6, 0x0A, 0x48,
  32819. 0xE5, 0xD3, 0xDD, 0x02, 0xFB, 0x7A, 0x5E, 0xD8,
  32820. 0x3F, 0xE2, 0x7A, 0x69, 0x82, 0x51, 0x42, 0x1C,
  32821. 0x8E, 0x9C, 0x98, 0x80, 0x61, 0x02, 0x39, 0x6E,
  32822. 0x53, 0x73, 0x90, 0xAC, 0xFD, 0x8C, 0x1D, 0x0B,
  32823. 0x4F, 0x99, 0xB7, 0x02, 0xA9, 0xEA, 0x65, 0x98,
  32824. 0x78, 0x58, 0x3D, 0x92, 0x75, 0x89, 0x41, 0xB3,
  32825. 0x0E, 0xCE, 0x50, 0x7C, 0x10, 0x4B, 0x2C, 0xE4,
  32826. 0x87, 0x67, 0x9E, 0xCF, 0x68, 0xB4, 0xD8, 0xB9,
  32827. 0x80, 0x69, 0x8A, 0xCF, 0x6A, 0xA6, 0xA5, 0x7E,
  32828. 0x8E, 0xD6, 0xAF, 0x3F, 0xF1, 0x8D, 0x26, 0x68,
  32829. 0x95, 0x04, 0x28, 0xB5, 0x7D, 0x18, 0x2F, 0x73,
  32830. 0xBB, 0x49, 0xB9, 0xB0, 0x38, 0xCC, 0xC8, 0x2D,
  32831. 0x56, 0x12, 0x78, 0xA3, 0x86, 0xD5, 0x66, 0x45,
  32832. 0xEC, 0x3F, 0xAF, 0xFB, 0x41, 0x25, 0xE0, 0xE7,
  32833. 0xF3, 0x6B, 0x48, 0xB1, 0x4B, 0x45, 0x25, 0x47,
  32834. 0xA0, 0xB4, 0x81, 0xAA, 0x6B, 0x33, 0x42, 0x29,
  32835. 0x24, 0x91, 0x53, 0xE4, 0x2E, 0xDF, 0x7E, 0x49,
  32836. 0xDD, 0x6E, 0x76, 0x36, 0xBF, 0xC6, 0x15, 0xA2,
  32837. 0x3A, 0x40, 0x1E, 0xFD, 0x40, 0x34, 0xC8, 0x1B,
  32838. 0x4D, 0xCE, 0xF0, 0x27, 0xD3, 0x44, 0xDD, 0xCC,
  32839. 0xE0, 0xA7, 0x16, 0x18, 0xEB, 0x59, 0x10, 0xCE,
  32840. 0xC6, 0x22, 0x28, 0x81, 0x93, 0x85, 0x03, 0x3E,
  32841. 0x8D, 0x0A, 0xBD, 0x49, 0x3D, 0x98, 0x3E, 0x4F,
  32842. 0xC0, 0x87, 0xD7, 0x2B, 0x45, 0x5E, 0x4D, 0xB6,
  32843. 0x3A, 0x2F, 0x82, 0xCE, 0xFF, 0x65, 0xC1, 0xE6,
  32844. 0x28, 0xEA, 0xE6, 0x30, 0x59, 0x6D, 0xEC, 0x27,
  32845. 0xFB, 0x98, 0xB8, 0x4D, 0xBF, 0xDC, 0xDF, 0xAB,
  32846. 0x40, 0xE4, 0x72, 0x24, 0x49, 0x14, 0xAF, 0xF1,
  32847. 0x79, 0x32, 0x6D, 0x54, 0x2D, 0x40, 0x1A, 0x3C,
  32848. 0xBB, 0x86, 0xE5, 0xFF, 0x83, 0x51, 0xEF, 0xE5,
  32849. 0x3A, 0x73, 0xC5, 0x1A, 0xBB, 0x63, 0xFF, 0x55,
  32850. 0x3E, 0x7D, 0x79, 0x57, 0xEF, 0x89, 0x13, 0x5E,
  32851. 0x0F, 0x5B, 0xB1, 0xBD, 0x0C, 0x24, 0xF9, 0xE4,
  32852. 0x5E, 0x32, 0x36, 0x41, 0x3C, 0x60, 0xE1, 0x39,
  32853. 0x6A, 0x47, 0x56, 0x7C, 0x94, 0x39, 0x51, 0x0F,
  32854. 0x00, 0xD4, 0xA4, 0x3C, 0x14, 0x9A, 0x5C, 0xCC,
  32855. 0x04, 0xF3, 0xD4, 0x7E, 0x67, 0xA8, 0xE2, 0x94,
  32856. 0xA4, 0x61, 0xA5, 0xF6, 0x93, 0xDB, 0x0C, 0xAE,
  32857. 0x22, 0xCF, 0xAC, 0x61, 0xE8, 0x53, 0x47, 0x7D,
  32858. 0x33, 0x9A, 0x4E, 0x45, 0xF7, 0xB1, 0x7C, 0x3C,
  32859. 0x11, 0x6D, 0x56, 0xF3, 0xA0, 0x68, 0xFC, 0x5A,
  32860. 0xDF, 0xEF, 0x38, 0xFF, 0x85, 0x33, 0x2B, 0xD5,
  32861. 0x15, 0x3C, 0x4D, 0x8F, 0xB8, 0xF1, 0x48, 0xF1,
  32862. 0x17, 0x65, 0x9C, 0x2E, 0xA9, 0x4D, 0xB4, 0x2A,
  32863. 0xA0, 0xB0, 0xBE, 0xBB, 0x47, 0x5A, 0x11, 0x04,
  32864. 0x12, 0xF3, 0xCD, 0x33, 0x49, 0xFC, 0x1A, 0xD0,
  32865. 0x41, 0xB7, 0xD5, 0x30, 0x4A, 0x85, 0x93, 0x14,
  32866. 0x4E, 0xFA, 0x3A, 0x36, 0x1D, 0x1B, 0x0C, 0x76,
  32867. 0x13, 0xB8, 0x2C, 0x08, 0x6E, 0xA7, 0x12, 0x6E,
  32868. 0x43, 0xC6, 0x16, 0xCE, 0xE8, 0xF1, 0x44, 0x4E,
  32869. 0x99, 0x56, 0xE8, 0x7F, 0x5C, 0xAB, 0x95, 0xC7,
  32870. 0xC7, 0xFB, 0x17, 0x58, 0xEC, 0x7D, 0x97, 0x01,
  32871. 0x9E, 0x5B, 0xA9, 0x35, 0x43, 0xEF, 0x3B, 0xAC,
  32872. 0x1A, 0x17, 0x42, 0x99, 0xCA, 0x48, 0xBF, 0x78,
  32873. 0x59, 0xDB, 0xFB, 0xDF, 0xF2, 0x43, 0xB1, 0x14,
  32874. 0xF6, 0xBF, 0x42, 0x3C, 0xE9, 0x8B, 0x4D, 0x4D,
  32875. 0x09, 0x1D, 0xA4, 0x4F, 0x32, 0x74, 0xD5, 0x73,
  32876. 0xFD, 0xC9, 0x04, 0xBD, 0x88, 0x5E, 0x35, 0xC9,
  32877. 0x15, 0x2A, 0x65, 0x35, 0x48, 0x88, 0xF1, 0x1E,
  32878. 0xD4, 0xF3, 0xD6, 0x3F, 0x26, 0xA7, 0xBE, 0x2F,
  32879. 0x57, 0x26, 0xEA, 0xDA, 0xF4, 0x85, 0x86, 0x59,
  32880. 0x2B, 0xBD, 0xF6, 0xCE, 0xE2, 0x46, 0x76, 0x9E,
  32881. 0x0E, 0xDA, 0x2A, 0x80, 0x77, 0x1F, 0xED, 0x34,
  32882. 0x7D, 0x67, 0xAF, 0xEE, 0xC6, 0x8B, 0x89, 0x46,
  32883. 0x3F, 0xA0, 0x49, 0x6D, 0xBC, 0x15, 0xC8, 0x9E,
  32884. 0x8D, 0x56, 0x99, 0x83, 0xD1, 0xD6, 0x74, 0x73,
  32885. 0x3F, 0x2B, 0xF9, 0xDF, 0x4A, 0x98, 0x0E, 0xA8,
  32886. 0xC5, 0xE3, 0xAF, 0x15, 0x56, 0x0A, 0x0E, 0x28,
  32887. 0xD6, 0x72, 0xB5, 0x80, 0xAB, 0x65, 0x52, 0xED,
  32888. 0x76, 0xAA, 0xCB, 0x5F, 0x80, 0x26, 0x0B, 0x97,
  32889. 0x03, 0x76, 0x9D, 0x33, 0xF4, 0x13, 0x8A, 0xBC,
  32890. 0x10, 0xBF, 0x5B, 0x05, 0x82, 0xDC, 0xC6, 0x2D,
  32891. 0xBE, 0x58, 0xC8, 0x90, 0xF5, 0x1B, 0x41, 0x00,
  32892. 0x12, 0x77, 0x34, 0xFB, 0x7D, 0xB7, 0x44, 0x7A,
  32893. 0x72, 0x0A, 0xAE, 0x00, 0x9D, 0x00, 0xBE, 0x8C,
  32894. 0x61, 0x07, 0x92, 0xC6, 0x4F, 0x13, 0x1F, 0x2D,
  32895. 0x72, 0x11, 0x5C, 0x7E, 0x05, 0x8E, 0x48, 0xB9,
  32896. 0xDE, 0x64, 0xF5, 0x5B, 0x4D, 0x61, 0x0C, 0x36,
  32897. 0xD1, 0x12, 0x71, 0x6A, 0x31, 0xA3, 0xDF, 0xE2,
  32898. 0x66, 0x99, 0xE9, 0xC2, 0xAB, 0xA0, 0x56, 0x58,
  32899. 0xCE, 0xF1, 0xB2, 0xB0, 0x86, 0x7C, 0xF8, 0xD5,
  32900. 0x23, 0x3D, 0xB7, 0x4F, 0xA8, 0xDC, 0x3A, 0xD1,
  32901. 0x45, 0xF5, 0xD2, 0x85, 0x74, 0x36, 0x0A, 0x85,
  32902. 0xE3, 0xB0, 0xB1, 0x0A, 0xC0, 0xA6, 0x46, 0x7A,
  32903. 0x7B, 0x05, 0x98, 0x46, 0x28, 0xEC, 0xA1, 0x04,
  32904. 0x63, 0xF3, 0x48, 0xA3, 0x11, 0x1E, 0x00, 0x57,
  32905. 0x8D, 0x3C, 0xE5, 0x48, 0x0F, 0x53, 0x75, 0xA1,
  32906. 0xEE, 0x23, 0xEE, 0x82, 0x08, 0x7B, 0xAC, 0x41,
  32907. 0x23, 0x3A, 0x14, 0xAA, 0xA7, 0x24, 0x73, 0x4B,
  32908. 0x18, 0x74, 0xA4, 0xAC, 0xE1, 0x13, 0x37, 0x06,
  32909. 0x25, 0x8F, 0x5F, 0xEA, 0x3A, 0x0C, 0x16, 0x09,
  32910. 0xE3, 0x0C, 0x7F, 0xD2, 0x10, 0xDA, 0x0C, 0x4F,
  32911. 0xDE, 0x91, 0x62, 0xDF, 0x66, 0xFB, 0xAF, 0x79,
  32912. 0x2F, 0xA2, 0xAE, 0xAA, 0x51, 0x2F, 0x0F, 0xF7,
  32913. 0x83, 0x7B, 0x9C, 0xC0, 0x2E, 0xE9, 0xBD, 0x95,
  32914. 0x53, 0x9F, 0x00, 0x1B, 0xBD, 0x60, 0xDD, 0x8B,
  32915. 0x42, 0xD6, 0x16, 0xB2, 0xCA, 0x95, 0xF3, 0x83,
  32916. 0x5F, 0x5E, 0x47, 0xD4, 0x3B, 0x14, 0x34, 0xC4,
  32917. 0x56, 0x3F, 0xD8, 0x1C, 0x15, 0xBE, 0xFA, 0x20,
  32918. 0x2C, 0xF3, 0xD9, 0x54, 0x08, 0x73, 0xF6, 0x84,
  32919. 0xAF, 0xE1, 0x9A, 0xB5, 0xC0, 0x1F, 0xA9, 0x2E,
  32920. 0x95, 0xA8, 0xCD, 0x6F, 0x36, 0x07, 0x30, 0x85,
  32921. 0x6E, 0x59, 0xC9, 0xC6, 0xAB, 0x77, 0x0D, 0x65,
  32922. 0x75, 0x96, 0x2A, 0xF7, 0x58, 0x78, 0x57, 0x2A,
  32923. 0x2A, 0x26, 0x41, 0x3D, 0x01, 0xAB, 0x31, 0x8C,
  32924. 0x10, 0x0D, 0xFC, 0x34, 0xDC, 0x1D, 0xEF, 0xA5,
  32925. 0x92, 0x7C, 0x4B, 0x45, 0x99, 0x25, 0xD7, 0x3E,
  32926. 0x1E, 0xB9, 0x14, 0x70, 0xE3, 0x7A, 0x58, 0x45,
  32927. 0x5C, 0x22, 0xA9, 0x61, 0xFD, 0x53, 0xF7, 0xD9,
  32928. 0x90, 0x26, 0xFF, 0x88, 0x4B, 0xF4, 0xA2, 0x57,
  32929. 0x9F, 0x70, 0x63, 0x35, 0xEF, 0xB6, 0xFB, 0x22,
  32930. 0x50, 0xD5, 0x2A, 0xE5, 0x61, 0x89, 0x8B, 0xA1,
  32931. 0x60, 0x6E, 0x51, 0xE9, 0x6D, 0x37, 0xC9, 0xED,
  32932. 0x3E, 0xC6, 0xCF, 0xCB, 0x33, 0xBF, 0xBE, 0x9C,
  32933. 0x31, 0x43, 0xFD, 0x3B, 0x6B, 0x33, 0x4D, 0x5F,
  32934. 0x61, 0x92, 0x2B, 0x36, 0x9A, 0xFB, 0xB3, 0x1C,
  32935. 0x3E, 0x6E, 0x9B, 0x5F, 0x3A, 0xEB, 0xF9, 0x5C,
  32936. 0xB7, 0x08, 0x34, 0x6F, 0xEC, 0xF7, 0x15, 0x9C,
  32937. 0xAD, 0x94, 0xA9, 0x3D, 0x8C, 0xD4, 0xB8, 0xC4,
  32938. 0x89, 0x41, 0x92, 0xDF, 0xE5, 0x3E, 0xA4, 0x36,
  32939. 0xFB, 0xF3, 0xAF, 0x4E, 0x86, 0x4E, 0x8C, 0x39,
  32940. 0x91, 0xEA, 0x02, 0x0A, 0x81, 0x1F, 0x0A, 0xF5,
  32941. 0x0B, 0x42, 0x57, 0x43, 0x6A, 0x3F, 0xF5, 0x22,
  32942. 0xBE, 0x73, 0x67, 0x39, 0x1D, 0x0F, 0x95, 0x0B,
  32943. 0xA6, 0x45, 0x2F, 0xBF, 0xD8, 0xFD, 0x87, 0x28,
  32944. 0xF4, 0x0B, 0xD2, 0xFC, 0xB8, 0x94, 0x52, 0x99,
  32945. 0x85, 0xB4, 0x32, 0xDF, 0xEF, 0x62, 0x30, 0xEB,
  32946. 0x4D, 0xEE, 0x73, 0x7A, 0x8D, 0x10, 0xA3, 0xBC,
  32947. 0xDF, 0xB7, 0x63, 0xE0, 0x86, 0x9B, 0x22, 0x5C,
  32948. 0x1A, 0x8D, 0x0E, 0x1F, 0xBF, 0x2D, 0x16, 0x1C,
  32949. 0x2C, 0x65, 0xD6, 0xDF, 0xB9, 0x58, 0xE9, 0x82,
  32950. 0xD1, 0x17, 0x77, 0xAC, 0xBE, 0xAD, 0x8D, 0xFB,
  32951. 0x6B, 0x1F, 0x5E, 0xB2, 0x1E, 0xA9, 0x42, 0xF7,
  32952. 0xC4, 0x0D, 0xC2, 0x0D, 0x2E, 0x4E, 0xB3, 0xE7,
  32953. 0x29, 0xB4, 0xE2, 0x9F, 0x75, 0x01, 0xDA, 0x34,
  32954. 0x23, 0x45, 0x61, 0xF6, 0x28, 0x88, 0x12, 0xD6,
  32955. 0x12, 0xD4, 0x1D, 0xFA, 0x83, 0xC5, 0xB8, 0xD9,
  32956. 0x0F, 0xF3, 0x8B, 0xA5, 0x48, 0x20, 0x1B, 0x57,
  32957. 0x5B, 0x52, 0x93, 0xAD, 0x78, 0x12, 0x0D, 0x91,
  32958. 0xCE, 0xC0, 0x59, 0xCA, 0xE2, 0xE7, 0x6A, 0x9A,
  32959. 0xB4, 0x3E, 0xF1, 0x28, 0x1E, 0x2B, 0xEF, 0x3E,
  32960. 0x34, 0x8D, 0x28, 0xF2, 0x19, 0x47, 0xC8, 0x88,
  32961. 0x48, 0x96, 0x04, 0x59, 0x48, 0x97, 0x75, 0x17,
  32962. 0x6F, 0x8E, 0x40, 0xEE, 0x06, 0x42, 0x79, 0x53,
  32963. 0x68, 0x7F, 0xB6, 0x3E, 0x47, 0x0F, 0x7D, 0x59,
  32964. 0xFB, 0x60, 0xDF, 0x56, 0x9F, 0x8A, 0x11, 0xE2,
  32965. 0x8E, 0x09, 0x37, 0x16, 0x2C, 0x46, 0xAF, 0xC7,
  32966. 0xD2, 0x21, 0x0A, 0x88, 0x5F, 0xFA, 0x21, 0xB3,
  32967. 0xDB, 0xF5, 0x35, 0x4B, 0x29, 0x41, 0xF4, 0xED,
  32968. 0x5D, 0x50, 0x79, 0x08, 0x90, 0x84, 0x0C, 0xC3,
  32969. 0xB9, 0x73, 0xD2, 0xC3, 0xD0, 0x26, 0x02, 0xB2,
  32970. 0x9B, 0xAC, 0xCB, 0x6C, 0xE1, 0x7C, 0xED, 0xB9,
  32971. 0x7B, 0x08, 0x5A, 0x2A, 0xB3, 0x10, 0x57, 0x2B,
  32972. 0xA7, 0x37, 0x1D, 0x1F, 0x81, 0x20, 0xFF, 0xE3,
  32973. 0x7D, 0x0B, 0x0F, 0xCA, 0x35, 0xAF, 0xC5, 0xB5,
  32974. 0x62, 0xAA, 0x84, 0x99, 0x71, 0x5A, 0x29, 0x9C,
  32975. 0xE0, 0x59, 0xCC, 0xE3, 0xB0, 0xD1, 0x1C, 0xEF,
  32976. 0x0D, 0x92, 0x38, 0x96, 0x1A, 0xD4, 0xBE, 0x11,
  32977. 0xE9, 0xA6, 0xD1, 0xA4, 0x69, 0x21, 0x77, 0xC8,
  32978. 0xB0, 0xC5, 0x3F, 0x11, 0xA8, 0xED, 0x26, 0x50,
  32979. 0x21, 0x2E, 0x7A, 0x2F, 0x80, 0xEB, 0xFF, 0x6D,
  32980. 0xCF, 0xE4, 0x67, 0x21, 0x03, 0x65, 0x84, 0x34,
  32981. 0xD0, 0x32, 0x7A, 0xDD, 0xCD, 0x66, 0xBC, 0xB6
  32982. };
  32983. static const byte msg_65[] = {
  32984. 0xDB, 0x84, 0x94, 0xBA, 0x19, 0xC4, 0x11, 0x8F,
  32985. 0xB1, 0x5D, 0x0A, 0xCF, 0x42, 0x54, 0xFD, 0x37,
  32986. 0x48, 0x3F, 0xCF, 0x47, 0x48, 0xFD, 0x18, 0x44,
  32987. 0xF7, 0x17, 0xCE, 0x6F, 0x69, 0x58, 0x9E, 0x61,
  32988. 0x77, 0x2C, 0xFE, 0xFA, 0x7F, 0x97, 0x58, 0x65,
  32989. 0x34, 0x09, 0xD4, 0xEE, 0x5A, 0x26, 0x4B, 0x83,
  32990. 0x4E, 0x60, 0xD6, 0xBB, 0x96, 0x49, 0x9E, 0xBE,
  32991. 0xB2, 0xB0, 0x6B, 0x0B, 0xA8, 0x74, 0xBF, 0x31,
  32992. 0xE6, 0x41, 0x39, 0x4C, 0xFA, 0xA6, 0xA2, 0xD3,
  32993. 0x0D, 0xDB, 0x8F, 0x04, 0x58, 0x76, 0x20, 0x8D,
  32994. 0x2F, 0x51, 0xDE, 0x15, 0xE2, 0x05, 0xE8, 0xC9,
  32995. 0x1B, 0x87, 0xEC, 0xEB, 0x05, 0xFF, 0x31, 0x83,
  32996. 0x27, 0x1B, 0x26, 0x49, 0x66, 0x5D, 0xD3, 0xCC,
  32997. 0x49, 0xBF, 0xDB, 0x99, 0x8D, 0x53, 0x9D, 0xA8,
  32998. 0x09, 0x30, 0x55, 0x16, 0xBB, 0xBE, 0x9C, 0x90,
  32999. 0x60, 0x21, 0x19, 0x1C, 0x52, 0x23, 0xE5, 0x25,
  33000. 0xA8, 0xFC, 0x36, 0x16, 0xA1, 0x76, 0x5E, 0xC3,
  33001. 0xF9, 0xC5, 0xDB, 0x53, 0xCC, 0x33, 0x7E, 0x03,
  33002. 0x9F, 0x18, 0x6A, 0xCF, 0xEA, 0x91, 0x14, 0x8E,
  33003. 0xE2, 0xA7, 0x9C, 0xCA, 0x36, 0x89, 0xED, 0xB6,
  33004. 0x2A, 0xAF, 0x28, 0xB5, 0xD7, 0x52, 0xFD, 0xE2,
  33005. 0x65, 0xEE, 0x52, 0x80, 0xB5, 0x19, 0x72, 0x6C,
  33006. 0x1C, 0xA9, 0x80, 0x32, 0x95, 0xC6, 0x74, 0xB7,
  33007. 0xEF, 0xAF, 0xA4, 0xD6, 0x1B, 0x30, 0x6A, 0x79,
  33008. 0xE3, 0xF6, 0xE7, 0xA8, 0x87, 0xC2, 0xFB, 0x53,
  33009. 0x5B, 0x3B, 0x0F, 0xB3, 0xD9, 0xEB, 0xC8, 0x76,
  33010. 0x03, 0xEA, 0xFE, 0xF1, 0x70, 0xC1, 0xF1, 0xD2,
  33011. 0x8E, 0x99, 0xBB
  33012. };
  33013. static const byte sig_65[] = {
  33014. 0xF7, 0x78, 0x9A, 0x45, 0xA3, 0x58, 0x73, 0x30,
  33015. 0xE7, 0xFC, 0xF7, 0x06, 0x95, 0xF7, 0xF6, 0x96,
  33016. 0x88, 0xA2, 0xB8, 0xD0, 0xCE, 0x54, 0xF0, 0x90,
  33017. 0x21, 0x4F, 0x10, 0x9F, 0x56, 0x48, 0x4F, 0x98,
  33018. 0xC3, 0xAD, 0x1A, 0x53, 0xA5, 0x44, 0x1C, 0x2C,
  33019. 0xA7, 0x2A, 0x3B, 0x31, 0x91, 0xBC, 0x04, 0x6F,
  33020. 0x46, 0x37, 0x30, 0x45, 0xB9, 0xE5, 0x40, 0xC7,
  33021. 0x3D, 0xFE, 0x91, 0xB6, 0x1F, 0x05, 0x88, 0xD6,
  33022. 0x13, 0x59, 0x3F, 0xCE, 0x1B, 0x00, 0xEE, 0xF1,
  33023. 0xB2, 0x27, 0x03, 0x4C, 0x6F, 0xD3, 0xB1, 0x8B,
  33024. 0x3F, 0x22, 0x11, 0x10, 0xFB, 0x34, 0x5A, 0xA7,
  33025. 0x86, 0x31, 0xB8, 0xB5, 0x9F, 0xBD, 0xFD, 0xCC,
  33026. 0xDA, 0xE6, 0xA2, 0x4D, 0x25, 0x9D, 0x34, 0xAA,
  33027. 0xBA, 0xD2, 0x18, 0xB3, 0xAE, 0x4E, 0x77, 0x18,
  33028. 0x66, 0x53, 0xB8, 0x56, 0x3A, 0xA6, 0x12, 0x0A,
  33029. 0x0A, 0x53, 0x1A, 0x4E, 0x91, 0x37, 0x30, 0xDC,
  33030. 0x91, 0x4F, 0xE5, 0xE0, 0x08, 0xBE, 0xCE, 0x68,
  33031. 0x69, 0xB0, 0x2B, 0x07, 0xFD, 0xC1, 0x62, 0x14,
  33032. 0x54, 0x0D, 0x31, 0x6C, 0x43, 0xFA, 0x0C, 0x21,
  33033. 0x1B, 0x41, 0xAC, 0x7E, 0x52, 0x65, 0x67, 0x29,
  33034. 0xC7, 0x73, 0xE4, 0xC4, 0xB8, 0x8E, 0xD3, 0x11,
  33035. 0x88, 0x6D, 0xD4, 0xD2, 0x75, 0x41, 0x7D, 0x70,
  33036. 0x19, 0x66, 0x44, 0xEE, 0xD1, 0x5F, 0xA3, 0x15,
  33037. 0x06, 0x60, 0x03, 0xE3, 0x09, 0xF8, 0x32, 0xAF,
  33038. 0x91, 0x26, 0x2C, 0x94, 0x90, 0x11, 0xFC, 0xB0,
  33039. 0xAD, 0x2C, 0xCE, 0x65, 0xDD, 0x9E, 0xFF, 0x56,
  33040. 0x7E, 0xE2, 0x9C, 0xC4, 0x0A, 0x6F, 0xE0, 0x66,
  33041. 0x4E, 0x7D, 0x9F, 0x23, 0x65, 0x68, 0xFC, 0x94,
  33042. 0x29, 0x5D, 0xBB, 0x34, 0x28, 0x82, 0x33, 0xE8,
  33043. 0xC5, 0x11, 0xD2, 0x88, 0x15, 0xEC, 0x72, 0x10,
  33044. 0x32, 0x29, 0x6E, 0x1E, 0xDE, 0xCA, 0x7F, 0x72,
  33045. 0x6A, 0x6E, 0xB0, 0xF7, 0x6C, 0xC5, 0x82, 0x80,
  33046. 0x11, 0xC0, 0xE4, 0x01, 0x3C, 0xC7, 0xEE, 0x43,
  33047. 0x29, 0xB8, 0x1E, 0xCC, 0x0D, 0x52, 0xED, 0x1E,
  33048. 0x49, 0x1D, 0xD6, 0xD5, 0x5C, 0x52, 0x65, 0x66,
  33049. 0x5E, 0xD8, 0xAD, 0x21, 0x9B, 0x89, 0x4F, 0x31,
  33050. 0xC6, 0x8C, 0x61, 0x9A, 0xFC, 0xDB, 0x73, 0x58,
  33051. 0xE5, 0x55, 0x4C, 0x49, 0x5B, 0x8B, 0x6E, 0x33,
  33052. 0x25, 0x68, 0x8F, 0xB8, 0xC1, 0xA2, 0x53, 0x31,
  33053. 0xD5, 0x7B, 0xD3, 0x48, 0xA2, 0x7D, 0x39, 0x09,
  33054. 0x29, 0xBC, 0x46, 0xA1, 0x49, 0x6A, 0xB3, 0x5B,
  33055. 0x46, 0xBA, 0x61, 0xB6, 0xB9, 0xD2, 0x3C, 0xD0,
  33056. 0x63, 0x15, 0xFB, 0x72, 0xC2, 0x47, 0x76, 0x01,
  33057. 0x61, 0x30, 0xAD, 0xB1, 0xCF, 0x2D, 0xC7, 0x29,
  33058. 0x59, 0xEA, 0x9C, 0xAD, 0x96, 0xAF, 0x5D, 0xA9,
  33059. 0x96, 0x12, 0x6C, 0xDD, 0x85, 0xB1, 0x34, 0xCC,
  33060. 0x92, 0x7A, 0x51, 0xFD, 0x23, 0xF8, 0x47, 0x91,
  33061. 0xA3, 0xFC, 0xDA, 0x07, 0x7E, 0x15, 0x99, 0x17,
  33062. 0x48, 0xA0, 0x39, 0x4F, 0x33, 0x4E, 0xB8, 0xBC,
  33063. 0x48, 0xA9, 0x9A, 0xB9, 0xDF, 0xBB, 0x0F, 0x2A,
  33064. 0xAD, 0x6F, 0xBE, 0x48, 0x49, 0x61, 0xD3, 0xA4,
  33065. 0xE8, 0xF8, 0xB2, 0x1A, 0x6A, 0xC0, 0x92, 0xB2,
  33066. 0x26, 0xD6, 0xE1, 0x19, 0xFA, 0xD4, 0x4D, 0x8E,
  33067. 0x57, 0x6F, 0xE9, 0x6C, 0x6C, 0xDB, 0x68, 0x40,
  33068. 0xEA, 0x61, 0x4B, 0xAF, 0xC7, 0x07, 0x86, 0xC5,
  33069. 0x19, 0xE1, 0xD5, 0xDC, 0x0F, 0x98, 0x44, 0x43,
  33070. 0xC8, 0xB1, 0xE5, 0x4F, 0x8E, 0xE1, 0x76, 0xD9,
  33071. 0x8B, 0x2C, 0x70, 0x27, 0xF5, 0x7D, 0x7E, 0x3D,
  33072. 0xE9, 0xB2, 0xA0, 0xA3, 0x69, 0x11, 0xB8, 0xE4,
  33073. 0x71, 0x21, 0xDE, 0x0C, 0x07, 0xEB, 0xBA, 0x5D,
  33074. 0x7B, 0x59, 0x4E, 0xF2, 0x44, 0xC6, 0x83, 0x27,
  33075. 0xEC, 0x6C, 0x6D, 0x1D, 0xD5, 0x01, 0xF4, 0x83,
  33076. 0xFE, 0x9B, 0x95, 0x70, 0x59, 0x7E, 0x70, 0xDF,
  33077. 0x41, 0x3E, 0x7A, 0xF0, 0x38, 0x47, 0xF4, 0x09,
  33078. 0xED, 0x61, 0xE2, 0x84, 0x6E, 0x6C, 0x64, 0x1E,
  33079. 0x6A, 0x7F, 0xFA, 0x79, 0xDE, 0x6B, 0xFA, 0x37,
  33080. 0x3A, 0x06, 0x44, 0xB0, 0x0B, 0xF4, 0x1A, 0x03,
  33081. 0x49, 0x92, 0xA7, 0x94, 0xDA, 0x17, 0xC8, 0x88,
  33082. 0x85, 0x23, 0x90, 0x32, 0xC8, 0x51, 0x76, 0x4E,
  33083. 0x3E, 0x4D, 0xBD, 0xE7, 0xF1, 0x2A, 0x16, 0xC5,
  33084. 0xA2, 0x63, 0xE9, 0x64, 0xC1, 0xE7, 0xFD, 0xD3,
  33085. 0xCC, 0xE5, 0x76, 0xDD, 0x6D, 0x56, 0xB1, 0x81,
  33086. 0x82, 0x84, 0x8B, 0x75, 0x63, 0x64, 0x5D, 0x4E,
  33087. 0x42, 0xFF, 0x22, 0x74, 0x2A, 0x99, 0x67, 0x85,
  33088. 0x16, 0x9D, 0x7F, 0x50, 0x3B, 0x48, 0xA7, 0x15,
  33089. 0x8B, 0x3C, 0xBD, 0x29, 0x93, 0x5E, 0xD3, 0x20,
  33090. 0x49, 0xBE, 0xA1, 0xAD, 0x95, 0x3E, 0xF7, 0x07,
  33091. 0x32, 0x7B, 0x77, 0x8B, 0xFD, 0xDD, 0xFC, 0x60,
  33092. 0x51, 0x1D, 0xA1, 0x13, 0xA3, 0x4F, 0x65, 0x57,
  33093. 0x12, 0xE4, 0xE5, 0x9D, 0x6C, 0xCE, 0x40, 0x4E,
  33094. 0x94, 0xAB, 0xA6, 0x1E, 0x81, 0x35, 0x38, 0x8F,
  33095. 0xC2, 0x1C, 0x8E, 0x41, 0x34, 0x4F, 0x32, 0x4B,
  33096. 0x01, 0xAC, 0x8C, 0x06, 0x9F, 0x92, 0x57, 0x5D,
  33097. 0x34, 0xF8, 0x8B, 0xCA, 0x22, 0xCB, 0x30, 0x7E,
  33098. 0x37, 0x07, 0x00, 0x63, 0x32, 0x02, 0x56, 0xB8,
  33099. 0xBA, 0xD6, 0xEB, 0x7A, 0x81, 0xAF, 0xE9, 0xA2,
  33100. 0x54, 0x01, 0x6E, 0x1C, 0x8A, 0x12, 0x50, 0x89,
  33101. 0xAA, 0xA3, 0xED, 0xE8, 0x4E, 0x5B, 0x6C, 0x2E,
  33102. 0xCF, 0xAE, 0xFA, 0xA5, 0x2B, 0x9F, 0x57, 0x09,
  33103. 0x60, 0x2C, 0x06, 0xAE, 0xA4, 0xA0, 0x38, 0x4E,
  33104. 0x9B, 0x09, 0xE5, 0xB8, 0x81, 0x64, 0xB2, 0x74,
  33105. 0xEA, 0x32, 0x65, 0xFB, 0x51, 0x52, 0x39, 0x7D,
  33106. 0xFF, 0x5A, 0x3A, 0x08, 0x61, 0xE2, 0xBC, 0x12,
  33107. 0xD2, 0x10, 0x92, 0x89, 0x72, 0x97, 0x47, 0xE8,
  33108. 0x3F, 0xDF, 0x24, 0x3A, 0x1D, 0x17, 0xB9, 0x83,
  33109. 0x48, 0x37, 0x98, 0x45, 0xA9, 0xE9, 0x55, 0xE2,
  33110. 0xD6, 0xF9, 0x38, 0xDA, 0xA5, 0x91, 0x8E, 0x2A,
  33111. 0x14, 0xF9, 0x7B, 0xA2, 0xBE, 0x50, 0x1C, 0xCC,
  33112. 0xAF, 0xD6, 0x81, 0x91, 0x0F, 0x4A, 0x4F, 0x06,
  33113. 0x71, 0x5C, 0xE8, 0x40, 0x96, 0xF3, 0x7A, 0x91,
  33114. 0xDC, 0xCA, 0x2A, 0x8A, 0x4B, 0xE8, 0xDA, 0x79,
  33115. 0x21, 0xDB, 0xF8, 0xD3, 0xF4, 0xEF, 0xB9, 0x8C,
  33116. 0x6B, 0x4F, 0x94, 0x0E, 0xCE, 0xF8, 0x32, 0xB5,
  33117. 0x49, 0xD0, 0x68, 0x94, 0x7C, 0x3D, 0xFB, 0x58,
  33118. 0x09, 0xCB, 0x7B, 0x06, 0x0A, 0x3A, 0x0E, 0xF3,
  33119. 0xB2, 0x1C, 0x01, 0x64, 0x50, 0x1D, 0xDE, 0xA7,
  33120. 0xC9, 0xE5, 0xE7, 0x89, 0x7C, 0x6B, 0x1C, 0x46,
  33121. 0x34, 0x8B, 0x2C, 0x3E, 0x80, 0x5F, 0x6F, 0x22,
  33122. 0x87, 0xBA, 0x15, 0x8C, 0xF9, 0x25, 0xA7, 0xBA,
  33123. 0x7F, 0x08, 0x25, 0x49, 0x89, 0xC8, 0x7D, 0x24,
  33124. 0x97, 0x9A, 0xD9, 0x86, 0xAA, 0x97, 0xC5, 0x1B,
  33125. 0x01, 0xF4, 0x5D, 0x4A, 0x1F, 0x24, 0x75, 0x29,
  33126. 0x91, 0xF0, 0x42, 0x05, 0xEB, 0x55, 0x1F, 0xD0,
  33127. 0x2D, 0x41, 0x5F, 0x2D, 0xD1, 0xEF, 0xF1, 0x42,
  33128. 0xB0, 0xD7, 0x04, 0x16, 0xC6, 0xD8, 0x15, 0xEB,
  33129. 0x91, 0x73, 0x2B, 0x26, 0x8F, 0xB2, 0x0D, 0x08,
  33130. 0x67, 0x44, 0x2D, 0x71, 0xDE, 0xC0, 0x57, 0xB2,
  33131. 0x86, 0xCD, 0x93, 0x81, 0x1F, 0xF3, 0xF6, 0x46,
  33132. 0xEB, 0xD5, 0x65, 0xD5, 0x1D, 0x09, 0xA4, 0x2D,
  33133. 0x3A, 0xBA, 0xAC, 0x0F, 0x34, 0xCC, 0x81, 0x7B,
  33134. 0x18, 0x93, 0x8E, 0xCC, 0xBB, 0x1F, 0xEF, 0x05,
  33135. 0xBD, 0x3C, 0x2B, 0x49, 0x4F, 0xA5, 0x29, 0xED,
  33136. 0x4C, 0x63, 0x4C, 0x93, 0x25, 0xA4, 0x81, 0x73,
  33137. 0xF2, 0x0F, 0xFA, 0xC3, 0x2D, 0xC1, 0x01, 0xE6,
  33138. 0xEE, 0x03, 0xB2, 0xFC, 0xBE, 0xC2, 0x46, 0x8D,
  33139. 0xBC, 0x8F, 0x76, 0x75, 0x8C, 0x32, 0x15, 0x47,
  33140. 0x4F, 0x7E, 0xF2, 0x40, 0x65, 0xF7, 0x90, 0x60,
  33141. 0xAC, 0xA3, 0xC8, 0xD5, 0xD7, 0x4A, 0xF7, 0x0F,
  33142. 0x48, 0x30, 0x1D, 0xDB, 0x30, 0xC0, 0x5D, 0xB3,
  33143. 0xEF, 0xA7, 0x26, 0xCF, 0x88, 0x55, 0x59, 0x01,
  33144. 0x84, 0x12, 0x82, 0xAA, 0x08, 0xF6, 0x66, 0xA6,
  33145. 0x53, 0x51, 0xA6, 0xA2, 0x4E, 0xED, 0x6B, 0xE2,
  33146. 0x11, 0x77, 0x31, 0x07, 0xE1, 0x85, 0xE1, 0xB4,
  33147. 0x88, 0xA2, 0xE4, 0x91, 0xB6, 0xC1, 0x41, 0x52,
  33148. 0x84, 0x62, 0xA8, 0x64, 0x94, 0xB5, 0x4F, 0xDC,
  33149. 0xCE, 0xCC, 0xB6, 0xAA, 0x21, 0x25, 0x36, 0x86,
  33150. 0x69, 0x3A, 0xE7, 0x98, 0xC9, 0xCE, 0x9E, 0x0B,
  33151. 0xDD, 0xC6, 0xAE, 0x53, 0xD9, 0xB7, 0x06, 0xDC,
  33152. 0x4F, 0x4D, 0x81, 0xB9, 0xC7, 0x3C, 0x46, 0x1E,
  33153. 0xCD, 0x70, 0x35, 0xC5, 0x17, 0x2E, 0xFA, 0xE5,
  33154. 0x60, 0x2C, 0xAF, 0x88, 0xC6, 0x4E, 0x79, 0xE5,
  33155. 0x32, 0x40, 0x30, 0x55, 0x5D, 0xE2, 0x11, 0xF8,
  33156. 0x9F, 0xD4, 0x24, 0xC3, 0x38, 0xC3, 0x88, 0x3C,
  33157. 0x83, 0xCA, 0x94, 0x05, 0xC2, 0xB5, 0xD1, 0x44,
  33158. 0x5F, 0x7C, 0x98, 0xC4, 0x3E, 0xD3, 0xD2, 0xBE,
  33159. 0xCB, 0xE2, 0x5F, 0x5F, 0x3F, 0x54, 0x4C, 0xCC,
  33160. 0x5B, 0x5A, 0xEA, 0xE4, 0x7D, 0xDF, 0x3F, 0xB5,
  33161. 0x64, 0x9F, 0xF5, 0xD6, 0x1E, 0xAA, 0x02, 0xED,
  33162. 0xEB, 0xC7, 0x5C, 0xE4, 0x78, 0xBA, 0x00, 0x42,
  33163. 0x6C, 0xAF, 0x47, 0x4F, 0xA7, 0x9E, 0x5B, 0x08,
  33164. 0x9E, 0xB1, 0xA8, 0x82, 0xF1, 0x53, 0x54, 0x59,
  33165. 0x26, 0x95, 0x95, 0x2B, 0xA0, 0xA8, 0xEE, 0x91,
  33166. 0xE6, 0x49, 0xE3, 0xF2, 0xC3, 0x82, 0x26, 0x4D,
  33167. 0xAA, 0x30, 0xF6, 0xA6, 0xD2, 0x17, 0xF6, 0x12,
  33168. 0x9C, 0x19, 0x39, 0xB6, 0xDC, 0xAC, 0xCD, 0xA5,
  33169. 0xB6, 0x37, 0x32, 0x6E, 0x8A, 0x83, 0x61, 0xC3,
  33170. 0xB5, 0x6F, 0xCF, 0xFC, 0x48, 0x50, 0x36, 0x86,
  33171. 0x58, 0x22, 0xB9, 0xBB, 0x87, 0xB4, 0x35, 0x10,
  33172. 0xBC, 0xDD, 0x55, 0xBC, 0x35, 0x0D, 0xE7, 0xB2,
  33173. 0xAE, 0x90, 0xA2, 0x1E, 0x9E, 0x19, 0x97, 0x8E,
  33174. 0xDA, 0x10, 0xDF, 0x66, 0x76, 0x14, 0xA4, 0x4F,
  33175. 0xE2, 0xA8, 0x4D, 0x16, 0xBE, 0x04, 0x3E, 0xA8,
  33176. 0x77, 0x36, 0x33, 0xEA, 0x6B, 0xAD, 0xF6, 0x57,
  33177. 0x10, 0x05, 0x2F, 0x34, 0x1F, 0x65, 0xCB, 0xE9,
  33178. 0x28, 0xD3, 0x96, 0x2A, 0x5A, 0x2F, 0xE6, 0x4E,
  33179. 0x46, 0xD6, 0xBF, 0xB8, 0xFD, 0x0D, 0x99, 0x78,
  33180. 0xF0, 0x42, 0x3C, 0xBD, 0x19, 0x5F, 0x72, 0xF3,
  33181. 0xCB, 0x19, 0xD7, 0xEF, 0xD9, 0xEB, 0xE3, 0x3C,
  33182. 0xD2, 0xF5, 0x70, 0x9A, 0x57, 0x80, 0x7D, 0xF9,
  33183. 0x44, 0xEC, 0xE5, 0x68, 0xAA, 0xCA, 0x43, 0x36,
  33184. 0x42, 0x20, 0x83, 0xB0, 0x69, 0x7B, 0x6A, 0xA0,
  33185. 0x05, 0x86, 0xE4, 0xBF, 0x7D, 0xD6, 0x73, 0xA3,
  33186. 0xD5, 0x96, 0xB8, 0x61, 0x8A, 0xC3, 0xB4, 0x06,
  33187. 0x17, 0x50, 0xC6, 0xBE, 0x97, 0xCB, 0x53, 0x75,
  33188. 0x3D, 0x02, 0x39, 0x55, 0x56, 0x07, 0x5A, 0x26,
  33189. 0xF1, 0x40, 0xB9, 0x3F, 0x57, 0x7D, 0xAD, 0x50,
  33190. 0x5E, 0x1C, 0xF2, 0xB5, 0x51, 0xA0, 0x4C, 0x98,
  33191. 0xC7, 0xF0, 0x90, 0x18, 0x31, 0xB3, 0xCA, 0x61,
  33192. 0xD7, 0x5D, 0xA7, 0x93, 0xAC, 0x72, 0xA4, 0x4C,
  33193. 0x7A, 0x07, 0xF7, 0xDB, 0xBA, 0xD6, 0x0A, 0x55,
  33194. 0xF4, 0x9C, 0xBD, 0x79, 0xDE, 0xE4, 0x73, 0x9F,
  33195. 0xFD, 0x36, 0x77, 0x8E, 0xBD, 0x08, 0xEB, 0xDB,
  33196. 0x79, 0xEC, 0x07, 0xA1, 0x62, 0x39, 0xC5, 0xB9,
  33197. 0x21, 0x59, 0x9F, 0xEB, 0xFE, 0xA4, 0x6D, 0xDF,
  33198. 0x96, 0x6A, 0xA4, 0xA0, 0x15, 0x12, 0xE6, 0x10,
  33199. 0x94, 0x3F, 0x5D, 0xC5, 0x4B, 0x4C, 0x76, 0xB7,
  33200. 0x64, 0xB3, 0x80, 0xBF, 0x2F, 0x84, 0xED, 0xE3,
  33201. 0x21, 0x24, 0x91, 0x2F, 0x54, 0xF7, 0xB6, 0xE2,
  33202. 0x07, 0xB7, 0x38, 0x1F, 0x67, 0x0F, 0x7A, 0xA0,
  33203. 0xF3, 0xC3, 0xED, 0x10, 0x15, 0x74, 0x03, 0x84,
  33204. 0xDD, 0x61, 0xA9, 0x76, 0x5E, 0xE4, 0x69, 0x6E,
  33205. 0xAC, 0xF8, 0x2E, 0xA4, 0x10, 0x69, 0x18, 0x05,
  33206. 0xCB, 0x68, 0x89, 0x03, 0x53, 0x5D, 0x70, 0x46,
  33207. 0x10, 0x0D, 0xCC, 0x2B, 0xA7, 0xD8, 0x30, 0x2A,
  33208. 0xCB, 0x04, 0x30, 0xD5, 0x06, 0xCC, 0xC1, 0xC0,
  33209. 0xDD, 0xEA, 0x71, 0x11, 0xA7, 0x6F, 0x45, 0xB4,
  33210. 0x54, 0xE2, 0x5C, 0xDD, 0xFB, 0x63, 0x9B, 0x3D,
  33211. 0x66, 0x4C, 0x36, 0xD8, 0x84, 0x35, 0x13, 0xA3,
  33212. 0xFC, 0xAF, 0x9E, 0x60, 0x57, 0xE9, 0xBC, 0x06,
  33213. 0x82, 0x37, 0xFE, 0x24, 0x19, 0xA2, 0xD2, 0xD9,
  33214. 0x0B, 0x4A, 0x1F, 0xC2, 0xA7, 0x1A, 0x14, 0x6D,
  33215. 0x2B, 0xD0, 0x43, 0x64, 0xC7, 0x9B, 0x8E, 0xBA,
  33216. 0x8E, 0x3E, 0x88, 0xCE, 0x11, 0xE9, 0x16, 0xE4,
  33217. 0xA7, 0x52, 0x84, 0x21, 0x32, 0x8C, 0xF5, 0x4F,
  33218. 0xAA, 0xB2, 0xB1, 0x9F, 0x44, 0x46, 0x87, 0x81,
  33219. 0xF8, 0xAB, 0x84, 0xB7, 0xDD, 0x97, 0x2F, 0xF5,
  33220. 0x61, 0x50, 0x71, 0x43, 0x0A, 0x43, 0x74, 0xDA,
  33221. 0xFC, 0xAE, 0x1E, 0x60, 0x44, 0xAA, 0x98, 0xE9,
  33222. 0x85, 0x94, 0x1B, 0xA6, 0xB9, 0xDB, 0x8C, 0x02,
  33223. 0xF5, 0x89, 0x60, 0x3E, 0xEB, 0x8B, 0xE9, 0x0A,
  33224. 0x70, 0xEF, 0xC0, 0x88, 0xD7, 0x95, 0xE6, 0xDA,
  33225. 0x1F, 0x1F, 0x2E, 0x6E, 0xCE, 0xDD, 0x03, 0x1D,
  33226. 0x81, 0x99, 0xE6, 0x59, 0x12, 0xD4, 0x34, 0xD0,
  33227. 0x9B, 0xFB, 0xE5, 0x94, 0x40, 0x6D, 0xC1, 0x15,
  33228. 0x0E, 0x99, 0x35, 0x8C, 0xEA, 0x7F, 0xAD, 0x2E,
  33229. 0x7C, 0x44, 0xC3, 0x8B, 0x6E, 0x0C, 0xEE, 0xAB,
  33230. 0x9B, 0xDE, 0x0D, 0xB9, 0x7B, 0xCF, 0x5A, 0xC9,
  33231. 0x94, 0x10, 0xC9, 0x47, 0x0E, 0x26, 0x6B, 0x8B,
  33232. 0xE4, 0x5F, 0x66, 0x90, 0x83, 0x1F, 0x41, 0x45,
  33233. 0xE2, 0x63, 0x79, 0xDB, 0x80, 0x7C, 0x26, 0xDD,
  33234. 0xF9, 0x1E, 0x30, 0x9D, 0x4F, 0x4A, 0x3E, 0x7E,
  33235. 0xCA, 0xB7, 0x36, 0x2F, 0x15, 0xD2, 0x0E, 0xA4,
  33236. 0x33, 0xB7, 0xE7, 0x0A, 0x7D, 0xDE, 0x74, 0x16,
  33237. 0xCE, 0xA8, 0x71, 0x49, 0x8B, 0x2C, 0xE3, 0xF5,
  33238. 0x8D, 0x29, 0xD8, 0x62, 0x8C, 0x53, 0x18, 0x40,
  33239. 0xF0, 0x22, 0xDD, 0x3B, 0xD2, 0xF3, 0x80, 0x9B,
  33240. 0x11, 0x68, 0xD3, 0x8E, 0x63, 0xC7, 0xF6, 0x93,
  33241. 0x08, 0xA3, 0x1A, 0x2D, 0x4D, 0x5E, 0xEB, 0x97,
  33242. 0x42, 0x39, 0xB3, 0x4A, 0x62, 0xBC, 0x85, 0xE4,
  33243. 0xEC, 0xF9, 0x0C, 0x33, 0x6A, 0x0C, 0x37, 0xBD,
  33244. 0x9E, 0x0E, 0xF4, 0x26, 0x6B, 0x83, 0x5A, 0xC8,
  33245. 0x90, 0x6A, 0x83, 0xCF, 0x0B, 0x35, 0x13, 0x8A,
  33246. 0x65, 0xE5, 0xD9, 0xA6, 0x1F, 0xCC, 0x9B, 0x2D,
  33247. 0x5A, 0x33, 0x7B, 0x8A, 0xBE, 0xF8, 0x8A, 0x7F,
  33248. 0xB3, 0xC0, 0x94, 0x5D, 0x7C, 0xAF, 0x35, 0x61,
  33249. 0x1A, 0xE0, 0xE4, 0x46, 0x93, 0xA5, 0xBC, 0xE0,
  33250. 0xA6, 0xE2, 0xFE, 0xCA, 0xE9, 0xBD, 0xF4, 0xE3,
  33251. 0x56, 0xD6, 0x53, 0x6B, 0x58, 0x1A, 0x18, 0xF0,
  33252. 0x3A, 0x59, 0x16, 0x4E, 0xD5, 0x44, 0x7C, 0x7E,
  33253. 0xC8, 0xBD, 0x99, 0x7B, 0xE9, 0x53, 0xDE, 0xD9,
  33254. 0x32, 0x53, 0x5B, 0x5F, 0x43, 0x8A, 0x04, 0x31,
  33255. 0x9F, 0x5E, 0x0D, 0x8B, 0x0F, 0xEB, 0xC8, 0xDE,
  33256. 0x81, 0x46, 0x65, 0x8E, 0x52, 0xB9, 0x75, 0x9C,
  33257. 0x73, 0x93, 0x5B, 0x12, 0x0D, 0xC9, 0xB8, 0x54,
  33258. 0xF3, 0xC8, 0xF9, 0x4E, 0xC9, 0x33, 0x90, 0x57,
  33259. 0xD7, 0xD7, 0xCD, 0x91, 0xF7, 0xE0, 0xB9, 0x8D,
  33260. 0x84, 0xEC, 0x7B, 0x2F, 0x92, 0x32, 0x8D, 0x73,
  33261. 0x60, 0x18, 0xB0, 0x31, 0x65, 0xA8, 0x74, 0x5F,
  33262. 0x8E, 0x77, 0xEB, 0x80, 0x29, 0xF9, 0x78, 0x26,
  33263. 0x70, 0xCB, 0xD8, 0x6B, 0x43, 0x16, 0xC7, 0xBE,
  33264. 0x4A, 0x88, 0x03, 0x38, 0xBA, 0xCF, 0xB0, 0x15,
  33265. 0x69, 0x9B, 0xF3, 0x0D, 0x3A, 0x4B, 0x05, 0x32,
  33266. 0x54, 0x35, 0xBA, 0x5F, 0xA3, 0xB9, 0xD2, 0xB2,
  33267. 0xFE, 0x0B, 0x51, 0x9C, 0x2C, 0xB2, 0x46, 0xE5,
  33268. 0x3D, 0x1A, 0x34, 0x3D, 0x66, 0x1A, 0x66, 0x14,
  33269. 0x3C, 0x6F, 0x46, 0x8C, 0x55, 0x38, 0x64, 0x5C,
  33270. 0xC2, 0x6D, 0x4E, 0x2A, 0x87, 0x03, 0xEC, 0x9B,
  33271. 0x10, 0xFC, 0x89, 0xBE, 0x6F, 0x85, 0x99, 0x97,
  33272. 0x70, 0x8F, 0x31, 0x19, 0x4F, 0x0D, 0xFE, 0xE9,
  33273. 0x29, 0x98, 0xB2, 0x5E, 0x93, 0xB9, 0x70, 0x70,
  33274. 0xDE, 0x14, 0x40, 0x9D, 0x5B, 0xA4, 0x3D, 0xF8,
  33275. 0x8D, 0x15, 0xC2, 0xFB, 0xA9, 0x7B, 0xDD, 0xE6,
  33276. 0x18, 0xCC, 0x3F, 0xC0, 0x42, 0xF7, 0x74, 0x81,
  33277. 0x84, 0xBA, 0x9E, 0xC9, 0xCB, 0xA1, 0xB2, 0x00,
  33278. 0x68, 0x81, 0xD0, 0x51, 0x42, 0x64, 0x19, 0x8F,
  33279. 0xB6, 0x91, 0xC5, 0xC0, 0x38, 0xE0, 0x49, 0x50,
  33280. 0xCF, 0x69, 0x09, 0x93, 0x77, 0xFE, 0x66, 0xBA,
  33281. 0x64, 0xE2, 0x19, 0x52, 0xA4, 0x45, 0x81, 0x71,
  33282. 0x96, 0x64, 0xF5, 0xD9, 0x23, 0x97, 0xD2, 0x2A,
  33283. 0xA7, 0x03, 0x2B, 0xF5, 0x89, 0xAF, 0x8A, 0xCA,
  33284. 0x48, 0xDF, 0x6D, 0x14, 0xEB, 0x43, 0xCE, 0xF0,
  33285. 0xA9, 0xC8, 0xA8, 0xF9, 0xAD, 0x32, 0x95, 0x25,
  33286. 0xEF, 0x0A, 0xAA, 0x4F, 0x9E, 0x09, 0xC3, 0x51,
  33287. 0x3C, 0xF0, 0x29, 0xF3, 0xDE, 0xFC, 0xBB, 0x41,
  33288. 0x14, 0xFA, 0x0F, 0x66, 0x8D, 0xB4, 0x72, 0x2F,
  33289. 0xCC, 0xD9, 0xC2, 0x07, 0xB6, 0x6F, 0x10, 0x9E,
  33290. 0xD9, 0x5B, 0x45, 0x4B, 0xB6, 0x19, 0x5D, 0x59,
  33291. 0xC4, 0xA6, 0x78, 0xBA, 0x6F, 0x5A, 0x9B, 0x23,
  33292. 0x41, 0x21, 0xAD, 0x05, 0x16, 0xA1, 0xD4, 0x12,
  33293. 0x3D, 0x38, 0x26, 0xD9, 0x2A, 0x61, 0xB3, 0x5D,
  33294. 0xEB, 0x29, 0x5B, 0xAA, 0x2F, 0xE1, 0xB5, 0xEE,
  33295. 0x25, 0x02, 0x1D, 0xAE, 0xF8, 0x57, 0xB5, 0xDF,
  33296. 0x19, 0x2E, 0x17, 0x5E, 0x3A, 0x2A, 0x0D, 0x3F,
  33297. 0x08, 0x2F, 0x21, 0x1C, 0xB5, 0xBD, 0xC2, 0x36,
  33298. 0x27, 0x4F, 0x86, 0xC5, 0xDC, 0x74, 0xC3, 0x9B,
  33299. 0xE9, 0x7C, 0xCF, 0x5F, 0x57, 0x94, 0xEB, 0x64,
  33300. 0xEC, 0x64, 0x55, 0x45, 0x21, 0x0F, 0xC6, 0x67,
  33301. 0xD1, 0xE0, 0x74, 0x0E, 0x66, 0xCB, 0xED, 0xC2,
  33302. 0x06, 0x48, 0xCA, 0x1F, 0xA7, 0x34, 0x14, 0x59,
  33303. 0x6B, 0xA0, 0x89, 0x17, 0xA1, 0x9A, 0x46, 0x3A,
  33304. 0xD3, 0x02, 0x7C, 0x81, 0x83, 0x6B, 0x8F, 0x4F,
  33305. 0x02, 0xB9, 0x9F, 0xC5, 0x08, 0x3F, 0x06, 0xF3,
  33306. 0x4B, 0xD2, 0x30, 0x9C, 0x23, 0x42, 0xAD, 0x88,
  33307. 0xA8, 0x4F, 0xA9, 0x6E, 0x20, 0x7C, 0x01, 0x08,
  33308. 0xF6, 0x82, 0x54, 0x14, 0x94, 0x4F, 0x26, 0x4E,
  33309. 0xD6, 0xC4, 0x66, 0x7C, 0x78, 0x8D, 0x61, 0xA6,
  33310. 0xBC, 0x2C, 0x45, 0x6A, 0xF6, 0x6C, 0x2F, 0x76,
  33311. 0x9E, 0x16, 0x90, 0x17, 0x06, 0x91, 0x2C, 0xC9,
  33312. 0x0D, 0x4B, 0x6C, 0x90, 0xDC, 0xA1, 0x6C, 0xAC,
  33313. 0x8F, 0xFE, 0xD8, 0x39, 0x70, 0x20, 0xE2, 0x97,
  33314. 0x5E, 0x24, 0xFF, 0x4C, 0x80, 0x7C, 0x8A, 0xB7,
  33315. 0x31, 0xC8, 0x1D, 0x36, 0xCA, 0x84, 0xC9, 0x12,
  33316. 0x1A, 0x85, 0x13, 0xE0, 0xC9, 0xD0, 0xF4, 0x1B,
  33317. 0xC6, 0x8F, 0x88, 0xEA, 0xCA, 0xA3, 0x55, 0x99,
  33318. 0xFA, 0xE3, 0xBB, 0xA6, 0xFC, 0xC6, 0x52, 0x8D,
  33319. 0x47, 0xE4, 0x0C, 0x07, 0x64, 0xCF, 0x9C, 0x83,
  33320. 0x83, 0xB3, 0xA4, 0x45, 0x15, 0xE6, 0x1D, 0x92,
  33321. 0xCD, 0xAE, 0xC9, 0xCB, 0x90, 0x82, 0xB5, 0xA0,
  33322. 0xC0, 0x37, 0x94, 0x60, 0xD9, 0x17, 0x9A, 0x7D,
  33323. 0x9D, 0xF2, 0x9E, 0x0B, 0x4B, 0x6A, 0x41, 0x18,
  33324. 0x28, 0x52, 0x15, 0xE8, 0x7B, 0x6F, 0x11, 0x8E,
  33325. 0x97, 0x31, 0xE4, 0x66, 0xFB, 0x3F, 0xEB, 0xD1,
  33326. 0x95, 0xE1, 0x44, 0xFD, 0x20, 0x37, 0xD1, 0x16,
  33327. 0x62, 0x75, 0x79, 0xAC, 0x55, 0xFE, 0xD5, 0xE3,
  33328. 0x25, 0x85, 0xEC, 0x66, 0x38, 0xA0, 0xDF, 0xBE,
  33329. 0x6E, 0xD6, 0xC5, 0x87, 0x6C, 0xF8, 0x11, 0x4C,
  33330. 0x90, 0x2A, 0xEF, 0xA3, 0x63, 0xF4, 0xC9, 0xB7,
  33331. 0x2E, 0x7D, 0x5C, 0x85, 0x2D, 0xCC, 0x1A, 0xF2,
  33332. 0xB8, 0x85, 0x2A, 0x9D, 0x0F, 0x99, 0x59, 0x38,
  33333. 0x86, 0x50, 0x84, 0xCE, 0x52, 0x13, 0xB3, 0x08,
  33334. 0xA9, 0xCB, 0x37, 0xF6, 0x81, 0x96, 0x0D, 0x84,
  33335. 0xEF, 0xE1, 0xDF, 0x51, 0x34, 0xA5, 0x91, 0x5A,
  33336. 0xE5, 0x87, 0x8B, 0x10, 0xDA, 0x0F, 0xD4, 0xD9,
  33337. 0xAC, 0x2A, 0xEF, 0x0C, 0x7E, 0x01, 0xC2, 0xE9,
  33338. 0xE7, 0xC0, 0x17, 0xE7, 0xBA, 0x74, 0x0C, 0xEE,
  33339. 0x1A, 0x89, 0x94, 0x59, 0xBB, 0x75, 0x03, 0x3E,
  33340. 0xEA, 0xF3, 0x19, 0x0D, 0x67, 0x79, 0xED, 0x9E,
  33341. 0xDD, 0x84, 0x6A, 0x74, 0xE3, 0x21, 0x52, 0x8C,
  33342. 0x03, 0x08, 0x4A, 0x5D, 0x30, 0x87, 0x48, 0x39,
  33343. 0x71, 0x8A, 0x53, 0x54, 0x9B, 0x2E, 0xC6, 0xB2,
  33344. 0xB7, 0x30, 0xAA, 0x93, 0x5C, 0xA6, 0xE1, 0xC4,
  33345. 0xFD, 0x8B, 0xE0, 0x35, 0x7D, 0x93, 0xF6, 0x21,
  33346. 0x74, 0xEE, 0xED, 0xF8, 0xDA, 0xB7, 0x75, 0x5B,
  33347. 0x46, 0x65, 0x7E, 0x59, 0xD7, 0xAA, 0x00, 0xB9,
  33348. 0xF2, 0xF8, 0x5E, 0x4C, 0x0F, 0x77, 0xFA, 0x11,
  33349. 0xA5, 0xD6, 0x9A, 0x23, 0xB1, 0xEF, 0x3A, 0x09,
  33350. 0xF2, 0x19, 0xD8, 0x3B, 0x1F, 0x39, 0x1F, 0x84,
  33351. 0x13, 0x18, 0xEE, 0xF3, 0x5A, 0x32, 0x63, 0x67,
  33352. 0xBF, 0xA2, 0xB1, 0x5F, 0xD7, 0x14, 0x03, 0x20,
  33353. 0x92, 0xB9, 0xD0, 0x2B, 0xF6, 0x13, 0xAF, 0xF7,
  33354. 0x69, 0x6F, 0xAD, 0xF1, 0xDE, 0x2C, 0x81, 0x70,
  33355. 0x77, 0xCB, 0x7C, 0x99, 0x67, 0x76, 0xD6, 0x9E,
  33356. 0xC2, 0x41, 0xA2, 0x42, 0x54, 0xDA, 0x2D, 0x13,
  33357. 0x98, 0x76, 0x91, 0xEA, 0xC7, 0xEB, 0xA8, 0xCD,
  33358. 0x8D, 0xCF, 0xB3, 0x94, 0x7B, 0x1D, 0x99, 0xED,
  33359. 0xF9, 0x62, 0xD2, 0x15, 0xB3, 0x18, 0xBB, 0x5F,
  33360. 0x9A, 0xA0, 0x4D, 0x1C, 0x82, 0x62, 0x6A, 0x41,
  33361. 0x73, 0xD0, 0x2D, 0x41, 0x0C, 0x58, 0x6B, 0xCA,
  33362. 0x4E, 0x51, 0xCA, 0x4F, 0x3E, 0x15, 0x1B, 0x54,
  33363. 0xF1, 0x7A, 0x6B, 0xC9, 0x67, 0x76, 0x09, 0xBB,
  33364. 0xAF, 0x6C, 0x30, 0x38, 0xA6, 0x7C, 0xAD, 0xA6,
  33365. 0x6B, 0x4F, 0xDF, 0xB5, 0x10, 0x29, 0xE0, 0x78,
  33366. 0x07, 0xD7, 0x05, 0x96, 0x9D, 0x96, 0xC9, 0xAB,
  33367. 0xFB, 0x71, 0x62, 0xE4, 0x58, 0x10, 0xA1, 0xDC,
  33368. 0x4B, 0x56, 0xDA, 0x14, 0x77, 0xED, 0x90, 0x0A,
  33369. 0x89, 0xCC, 0xAC, 0x29, 0x8E, 0x17, 0x88, 0x42,
  33370. 0x69, 0xC3, 0x9E, 0x8D, 0x7A, 0xB9, 0x66, 0xF3,
  33371. 0x3D, 0xDA, 0xDB, 0xE5, 0x6A, 0x38, 0x4C, 0xA2,
  33372. 0x0A, 0x7B, 0x18, 0x99, 0xEC, 0x18, 0xE2, 0xAE,
  33373. 0x54, 0x70, 0x00, 0xB9, 0x04, 0xE3, 0x4E, 0x46,
  33374. 0x80, 0x1D, 0x85, 0x74, 0xDB, 0x00, 0x84, 0x17,
  33375. 0xBC, 0xFD, 0xD1, 0xA7, 0x4D, 0xC0, 0x18, 0xE5,
  33376. 0x07, 0xB7, 0x6B, 0x0F, 0xA0, 0x86, 0x26, 0x23,
  33377. 0x5B, 0x1C, 0xE2, 0x4B, 0xCF, 0xC3, 0x20, 0xFA,
  33378. 0xE3, 0x55, 0x1C, 0x1C, 0x92, 0x9B, 0x94, 0xC7,
  33379. 0xC4, 0x96, 0x53, 0x41, 0x82, 0x9D, 0x8A, 0x13,
  33380. 0x47, 0xD6, 0xA7, 0x38, 0x58, 0x03, 0xB0, 0x8B,
  33381. 0xCD, 0xA8, 0x4A, 0x27, 0xEA, 0x5E, 0x49, 0xCA,
  33382. 0x1E, 0x60, 0x06, 0xEA, 0x23, 0x2A, 0x53, 0xEE,
  33383. 0x41, 0x7E, 0xC8, 0x81, 0xD3, 0x32, 0x8A, 0x15,
  33384. 0x63, 0x82, 0xA6, 0xB2, 0x93, 0x89, 0x4D, 0xDF,
  33385. 0x9B, 0x36, 0x9C, 0xDE, 0x6B, 0x2F, 0xF5, 0x9C,
  33386. 0xB6, 0xA5, 0x64, 0xE2, 0x1C, 0x92, 0x79, 0xEC,
  33387. 0xA0, 0x31, 0x1F, 0x5D, 0x80, 0xCE, 0x39, 0xB9,
  33388. 0x8B, 0xF9, 0x0D, 0xB3, 0x27, 0xF7, 0x4D, 0x3F,
  33389. 0x76, 0x2D, 0x11, 0x7D, 0xF5, 0xF9, 0x13, 0x20,
  33390. 0x84, 0xFF, 0xB5, 0x55, 0xA5, 0xD1, 0x47, 0x22,
  33391. 0x1A, 0xF8, 0x63, 0xAB, 0xF7, 0x87, 0x15, 0xB7,
  33392. 0x21, 0x94, 0x52, 0x9A, 0x0E, 0x33, 0x4D, 0x4A,
  33393. 0x19, 0x1D, 0x42, 0xA9, 0x9B, 0xEA, 0x52, 0xAD,
  33394. 0xA2, 0xC7, 0xCC, 0x4A, 0x97, 0x74, 0xD5, 0xCB,
  33395. 0x28, 0xD4, 0xED, 0x82, 0xB6, 0x1F, 0x94, 0xE8,
  33396. 0x9F, 0x60, 0xF0, 0xC8, 0xEA, 0x52, 0xDC, 0x07,
  33397. 0x9D, 0x46, 0x58, 0xBF, 0x8C, 0x85, 0x6D, 0x61,
  33398. 0x52, 0xD9, 0x22, 0x51, 0x94, 0x8B, 0x3B, 0xA0,
  33399. 0x14, 0xD8, 0xBA, 0xF3, 0xDC, 0xD3, 0x6B, 0xC7,
  33400. 0x1F, 0x8E, 0x5B, 0x2C, 0xE6, 0xF5, 0x35, 0xB7,
  33401. 0xB9, 0xAE, 0x13, 0xDA, 0x4A, 0x1E, 0xAF, 0xFC,
  33402. 0x25, 0x3B, 0xE4, 0x3A, 0x9F, 0x60, 0x8E, 0xAC,
  33403. 0xE7, 0x33, 0xCF, 0xCE, 0x52, 0xEA, 0x5C, 0xDA,
  33404. 0x83, 0x59, 0xDB, 0x53, 0xFF, 0x3A, 0xF2, 0xCE,
  33405. 0xFE, 0x87, 0x79, 0xBC, 0xC5, 0x3C, 0x24, 0xA4,
  33406. 0xB1, 0x8D, 0x5E, 0x0D, 0x78, 0x1B, 0xEC, 0xF7,
  33407. 0x5B, 0x54, 0x77, 0x47, 0x3A, 0x20, 0x24, 0xAD,
  33408. 0x56, 0xC5, 0x4A, 0x7F, 0x99, 0x0E, 0xF6, 0xB1,
  33409. 0xDF, 0xAC, 0x50, 0x10, 0x88, 0x50, 0x9D, 0x3A,
  33410. 0x37, 0xF1, 0xC8, 0xD5, 0xC2, 0x64, 0x87, 0xE4,
  33411. 0x20, 0xB7, 0xF4, 0x35, 0x8E, 0x92, 0x69, 0x76,
  33412. 0x1F, 0xF1, 0xFA, 0x3A, 0xFC, 0xBE, 0xCA, 0xEB,
  33413. 0x68, 0xF5, 0xDD, 0xDE, 0x3A, 0xA8, 0xFD, 0x07,
  33414. 0x8C, 0xC4, 0x22, 0x4C, 0xEA, 0x67, 0x13, 0x2D,
  33415. 0x7E, 0xBF, 0x5D, 0x23, 0x2E, 0x43, 0xBA, 0xDD,
  33416. 0x21, 0x8C, 0x0B, 0x4D, 0xBE, 0x1E, 0x16, 0x52,
  33417. 0x98, 0x66, 0xB9, 0xAB, 0x93, 0x58, 0x85, 0xAC,
  33418. 0xB4, 0x15, 0xFB, 0xB1, 0xEE, 0xE6, 0x94, 0x08,
  33419. 0xA5, 0x21, 0xB4, 0x62, 0xEC, 0x59, 0xCD, 0x0D,
  33420. 0x3C, 0x54, 0x96, 0xD9, 0x85, 0xAE, 0xB0, 0xCE,
  33421. 0x37, 0x4F, 0x67, 0x72, 0xA4, 0xE6, 0x39, 0x3A,
  33422. 0x4E, 0xF0, 0x07, 0x43, 0x80, 0x90, 0xA8, 0xA9,
  33423. 0xE5, 0x2D, 0x2F, 0x55, 0x66, 0x6D, 0x70, 0xF0,
  33424. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33425. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33426. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  33427. 0x08, 0x0E, 0x12, 0x19, 0x20
  33428. };
  33429. #endif
  33430. #ifndef WOLFSSL_NO_ML_DSA_87
  33431. static const byte pk_87[] = {
  33432. 0x2D, 0x1E, 0x6B, 0xED, 0x84, 0x52, 0xEB, 0xF1,
  33433. 0x26, 0xED, 0xE7, 0x0C, 0xA0, 0xA2, 0xB5, 0x0D,
  33434. 0x03, 0x34, 0x2D, 0x5B, 0x13, 0xB2, 0xAE, 0x21,
  33435. 0x0F, 0x45, 0x62, 0xA3, 0xBF, 0x67, 0x0C, 0xB1,
  33436. 0x5C, 0xE9, 0x25, 0xFD, 0x22, 0xF2, 0x62, 0x42,
  33437. 0xBA, 0xE3, 0x10, 0xB3, 0xAA, 0x41, 0x3B, 0x6E,
  33438. 0x78, 0xD4, 0x42, 0xD9, 0x35, 0xD1, 0x72, 0x8A,
  33439. 0x32, 0x48, 0xCC, 0x20, 0x5C, 0xCD, 0x8D, 0x3F,
  33440. 0xD8, 0x34, 0x95, 0x55, 0x20, 0xCD, 0xFB, 0x2C,
  33441. 0x73, 0xE9, 0x0E, 0x60, 0x8B, 0x2C, 0x3F, 0xA8,
  33442. 0xB7, 0xD1, 0x79, 0xFD, 0xDC, 0xC8, 0x81, 0x11,
  33443. 0xC9, 0xE8, 0x41, 0x71, 0xE9, 0x70, 0x9B, 0x53,
  33444. 0x59, 0x33, 0xE4, 0x92, 0xB6, 0x81, 0x9C, 0x6A,
  33445. 0x92, 0xED, 0xA2, 0x5A, 0xC4, 0x07, 0x77, 0x1A,
  33446. 0x8F, 0xED, 0xB4, 0xE7, 0x11, 0xFB, 0x89, 0xEB,
  33447. 0x7B, 0xDF, 0xCC, 0xEA, 0xC5, 0x3B, 0x4E, 0xF4,
  33448. 0x6B, 0x6F, 0xBE, 0xE1, 0x32, 0xA9, 0xD7, 0xAD,
  33449. 0xB4, 0x36, 0xE7, 0x4A, 0x6D, 0x67, 0x11, 0x83,
  33450. 0xAF, 0x31, 0x1A, 0x7A, 0x31, 0x42, 0x9B, 0x01,
  33451. 0x21, 0x17, 0x52, 0x75, 0x85, 0xF7, 0x92, 0x0F,
  33452. 0x34, 0x8A, 0x69, 0x11, 0x88, 0x5A, 0x02, 0x08,
  33453. 0xB6, 0x6D, 0xE3, 0x07, 0x93, 0xB1, 0x3F, 0xE1,
  33454. 0xD5, 0x7B, 0xD9, 0x51, 0xF7, 0xAA, 0xC0, 0x34,
  33455. 0x9A, 0x78, 0x5D, 0x26, 0xDB, 0xF1, 0xF0, 0xA9,
  33456. 0x1E, 0x5C, 0x9F, 0x4F, 0xA7, 0x43, 0x5C, 0x44,
  33457. 0xA9, 0x43, 0xF1, 0x38, 0x11, 0x45, 0xED, 0xEB,
  33458. 0x1C, 0x8A, 0x05, 0xEE, 0xFF, 0xAB, 0x20, 0x2C,
  33459. 0xF6, 0x2C, 0xEE, 0x77, 0x42, 0x36, 0x3E, 0xE6,
  33460. 0x9D, 0x8E, 0x45, 0x0F, 0xF6, 0x7C, 0x39, 0x62,
  33461. 0xD6, 0xFF, 0x97, 0xBC, 0x3D, 0x02, 0xD6, 0xDF,
  33462. 0x4A, 0x35, 0xDA, 0x3F, 0x89, 0xA4, 0x88, 0x33,
  33463. 0xCD, 0xF2, 0x90, 0xF0, 0xE9, 0x37, 0x2F, 0x65,
  33464. 0xA5, 0x88, 0x65, 0xFD, 0x40, 0x44, 0xAD, 0x09,
  33465. 0x09, 0x92, 0xAA, 0x15, 0x9E, 0xEE, 0xF7, 0x2B,
  33466. 0x0D, 0xA7, 0xCB, 0x3A, 0x5E, 0x0A, 0xED, 0xD6,
  33467. 0x7D, 0x82, 0x8B, 0xBA, 0xCF, 0xE5, 0x9E, 0xE4,
  33468. 0x62, 0xAB, 0x69, 0x6B, 0xBA, 0xD0, 0xE5, 0xA9,
  33469. 0xBB, 0x1F, 0x5A, 0x51, 0xE0, 0xFA, 0x5D, 0xD4,
  33470. 0x4D, 0x8E, 0xC0, 0xDC, 0x43, 0x06, 0xDF, 0x23,
  33471. 0x67, 0xB2, 0x4A, 0xA2, 0xFB, 0x75, 0x2F, 0x82,
  33472. 0xD8, 0x44, 0xE4, 0xC0, 0xCE, 0x15, 0x9E, 0x3F,
  33473. 0xD6, 0xB4, 0x70, 0x5F, 0x3B, 0xD0, 0x56, 0x3E,
  33474. 0x0A, 0x7A, 0x4B, 0x94, 0xBF, 0xBA, 0x01, 0x2B,
  33475. 0x9C, 0x8B, 0x91, 0x35, 0xF2, 0xDB, 0x4C, 0x8C,
  33476. 0x8D, 0xD6, 0xEE, 0xC8, 0x65, 0x8D, 0xF3, 0x05,
  33477. 0x59, 0xBE, 0x3A, 0x17, 0xA7, 0x72, 0x10, 0x56,
  33478. 0x14, 0xEF, 0xB8, 0xC1, 0xBE, 0x18, 0x11, 0x0B,
  33479. 0xE6, 0x70, 0xF8, 0x39, 0xA5, 0x72, 0x7D, 0xF9,
  33480. 0x47, 0xFB, 0xAC, 0xFD, 0x1F, 0xC3, 0x71, 0x33,
  33481. 0x58, 0x44, 0x15, 0xD3, 0x7C, 0x93, 0x2E, 0x70,
  33482. 0x92, 0xFA, 0xBB, 0xF2, 0xD0, 0x9D, 0x25, 0xC4,
  33483. 0xCF, 0x4A, 0xB8, 0xEC, 0xBE, 0x5D, 0x8B, 0x7F,
  33484. 0xA4, 0x7C, 0xAB, 0xAD, 0xE7, 0x1E, 0x93, 0x83,
  33485. 0x92, 0x86, 0x1E, 0x8D, 0x15, 0xA4, 0x1C, 0x5B,
  33486. 0x42, 0x25, 0xDA, 0x3D, 0x16, 0xD3, 0x93, 0xF2,
  33487. 0x85, 0x50, 0x86, 0x0A, 0x86, 0x35, 0x6B, 0x14,
  33488. 0xAB, 0x5F, 0x22, 0xD0, 0xCF, 0x03, 0x7C, 0xEB,
  33489. 0xB4, 0x0E, 0xAC, 0x87, 0xA2, 0x41, 0x42, 0xA0,
  33490. 0x21, 0x93, 0x00, 0xB6, 0x47, 0x6F, 0x96, 0xD0,
  33491. 0x41, 0xD1, 0xC3, 0x0E, 0x3C, 0x52, 0xD2, 0x45,
  33492. 0xAB, 0x6A, 0xE7, 0xA1, 0xE5, 0xFD, 0x73, 0xC5,
  33493. 0x82, 0x9D, 0x60, 0x62, 0x8B, 0x6D, 0x87, 0xFC,
  33494. 0x88, 0x9C, 0x3E, 0xEF, 0xAE, 0xAA, 0xB6, 0x1C,
  33495. 0x18, 0xEE, 0xD7, 0x51, 0x1A, 0x96, 0xC4, 0x93,
  33496. 0x25, 0x05, 0xD3, 0x83, 0x3D, 0xD8, 0x33, 0x16,
  33497. 0x14, 0x44, 0x88, 0xE2, 0xAF, 0xC4, 0xEC, 0x59,
  33498. 0x18, 0x12, 0xB9, 0x99, 0xC1, 0xC9, 0x5F, 0x31,
  33499. 0x79, 0x00, 0x03, 0xF6, 0xC9, 0x55, 0x14, 0xAA,
  33500. 0x29, 0x08, 0x78, 0x24, 0xAF, 0x1D, 0x99, 0x12,
  33501. 0x36, 0xD9, 0x4A, 0xD9, 0x50, 0xEF, 0x66, 0xFC,
  33502. 0x7F, 0xF4, 0xBC, 0x3B, 0xA0, 0xF6, 0xFD, 0xF2,
  33503. 0x62, 0xCA, 0xA5, 0x9D, 0x2B, 0x55, 0xB8, 0x33,
  33504. 0xBC, 0xA6, 0x7A, 0xA5, 0x1E, 0xE1, 0x14, 0x5F,
  33505. 0x94, 0xE2, 0xDC, 0xF0, 0x5B, 0xBD, 0x43, 0x07,
  33506. 0xD8, 0xB1, 0xE0, 0x81, 0x3F, 0x84, 0x54, 0x90,
  33507. 0xBF, 0x23, 0x59, 0x92, 0x3C, 0xA5, 0x98, 0xAB,
  33508. 0x7D, 0x99, 0xD2, 0xF0, 0xED, 0x8E, 0x0B, 0xC9,
  33509. 0x9F, 0xAF, 0xB0, 0x13, 0xED, 0xC7, 0xDD, 0xB8,
  33510. 0x61, 0x72, 0x07, 0x3D, 0xCC, 0x35, 0x73, 0xA0,
  33511. 0xCF, 0x0C, 0xD9, 0x7E, 0x93, 0xDC, 0x63, 0xB8,
  33512. 0x82, 0xEC, 0xF4, 0x30, 0xCE, 0x43, 0x92, 0xEA,
  33513. 0x5E, 0xD8, 0xC8, 0xA1, 0xEC, 0x79, 0xDC, 0xAE,
  33514. 0x64, 0xD4, 0x33, 0xEB, 0x53, 0x8C, 0xFC, 0x49,
  33515. 0x79, 0xBF, 0x7A, 0x28, 0x65, 0x1E, 0x8C, 0xD5,
  33516. 0x21, 0xB0, 0x8E, 0xCA, 0xAD, 0xF8, 0x96, 0x9A,
  33517. 0x98, 0x10, 0x00, 0x35, 0x6D, 0x58, 0x9A, 0xEF,
  33518. 0x84, 0x84, 0x86, 0x72, 0xBA, 0xCD, 0x38, 0x66,
  33519. 0x96, 0x9B, 0xC2, 0x83, 0xB0, 0x65, 0xC1, 0xAB,
  33520. 0xCF, 0x63, 0x8C, 0x2D, 0xC3, 0x42, 0xB2, 0x7D,
  33521. 0xF6, 0xB8, 0xF0, 0x3D, 0x26, 0x21, 0x8F, 0xAE,
  33522. 0x4E, 0x96, 0xF2, 0x55, 0x66, 0xBC, 0x6F, 0xED,
  33523. 0xE7, 0x19, 0xD3, 0x8D, 0xC0, 0xCD, 0x55, 0x20,
  33524. 0x5F, 0x10, 0xCA, 0xDA, 0x09, 0xED, 0x91, 0x4A,
  33525. 0x43, 0x33, 0xD3, 0x82, 0x11, 0x5C, 0x2F, 0x5D,
  33526. 0xEC, 0xCD, 0x54, 0xF9, 0x6C, 0xE4, 0xE5, 0xF2,
  33527. 0x68, 0xBC, 0xE9, 0x27, 0xB2, 0x1D, 0xCA, 0xB5,
  33528. 0xCD, 0x04, 0x01, 0x1E, 0x92, 0xF5, 0xF6, 0x01,
  33529. 0x86, 0x2B, 0x20, 0x20, 0x9B, 0xB0, 0xF9, 0x56,
  33530. 0xD9, 0x33, 0xD5, 0x0A, 0xEC, 0x1B, 0xF4, 0xCE,
  33531. 0xD2, 0xB2, 0xC2, 0xD4, 0x3F, 0x9A, 0x25, 0x76,
  33532. 0x8E, 0x29, 0x87, 0x52, 0x64, 0x86, 0x4A, 0xA5,
  33533. 0x7B, 0x5A, 0x91, 0x72, 0x6E, 0xBE, 0x6D, 0x73,
  33534. 0x0A, 0x8D, 0x89, 0x53, 0x82, 0x33, 0x70, 0x44,
  33535. 0x20, 0xBE, 0xE0, 0xB0, 0x1B, 0x76, 0x30, 0x43,
  33536. 0xA5, 0x5B, 0x8F, 0xAB, 0x7E, 0xB8, 0x61, 0x5F,
  33537. 0x43, 0x70, 0x1B, 0x1A, 0x71, 0x61, 0x56, 0xF9,
  33538. 0x13, 0x31, 0x2A, 0x64, 0x33, 0x14, 0x00, 0x98,
  33539. 0x72, 0xEC, 0x32, 0x88, 0x09, 0xFB, 0x64, 0x46,
  33540. 0x3D, 0x56, 0x02, 0xD9, 0x76, 0xD3, 0xAA, 0x90,
  33541. 0x0F, 0xBD, 0xF0, 0xF9, 0x96, 0x43, 0x7B, 0x62,
  33542. 0x19, 0x26, 0x22, 0x6A, 0x93, 0x91, 0xEC, 0x07,
  33543. 0x34, 0xF5, 0x22, 0x32, 0xB3, 0x65, 0x66, 0xE0,
  33544. 0x6B, 0x11, 0x7F, 0x97, 0x9F, 0x1A, 0x89, 0x46,
  33545. 0xCE, 0x8F, 0xBD, 0xFD, 0x2F, 0xCC, 0x3D, 0xBF,
  33546. 0xF2, 0x83, 0xA4, 0x30, 0xE1, 0x02, 0x72, 0xF8,
  33547. 0x74, 0xE6, 0x21, 0x96, 0x77, 0xE1, 0x57, 0x8A,
  33548. 0xF7, 0x9E, 0xB3, 0x31, 0xAF, 0xD8, 0xC5, 0xD7,
  33549. 0x20, 0xDC, 0xFD, 0xCF, 0x79, 0x06, 0x0F, 0x1F,
  33550. 0xE5, 0x84, 0x3D, 0x0B, 0x9C, 0xB3, 0xC7, 0xAB,
  33551. 0xB8, 0xF1, 0xC0, 0xD0, 0xB5, 0xC7, 0x01, 0xE2,
  33552. 0x0E, 0x3B, 0xAF, 0x7E, 0xAC, 0x44, 0x5A, 0x75,
  33553. 0x50, 0x0A, 0x76, 0x1C, 0x13, 0xDB, 0x25, 0xD4,
  33554. 0x0D, 0x19, 0x75, 0x4C, 0x02, 0xD9, 0xF3, 0xDF,
  33555. 0x6D, 0xBB, 0xCF, 0x47, 0xA6, 0xAE, 0xF6, 0xD1,
  33556. 0xFB, 0xF4, 0xB4, 0x55, 0xD3, 0xA5, 0x87, 0xA1,
  33557. 0x55, 0xFB, 0xBF, 0xCD, 0xF6, 0xA1, 0x64, 0x57,
  33558. 0x12, 0x75, 0x9A, 0x11, 0xA3, 0xCE, 0x42, 0x70,
  33559. 0x84, 0x54, 0x93, 0x12, 0xE1, 0x3A, 0x0F, 0xFA,
  33560. 0xCA, 0xF2, 0x25, 0x91, 0xF1, 0x4D, 0x8F, 0x84,
  33561. 0xB1, 0xB5, 0x35, 0xAC, 0xE9, 0x81, 0x77, 0x34,
  33562. 0x4D, 0x6F, 0x5D, 0x14, 0x9D, 0xB9, 0xE1, 0xF0,
  33563. 0x3F, 0x3C, 0xE7, 0xAD, 0x48, 0xE6, 0x8C, 0x51,
  33564. 0x86, 0xF4, 0x4A, 0xB4, 0xD0, 0x98, 0xEC, 0x3A,
  33565. 0x4E, 0xAB, 0x58, 0x2F, 0x08, 0x9E, 0x5A, 0x9D,
  33566. 0x45, 0x30, 0xB0, 0x85, 0xDF, 0x4A, 0xE7, 0x92,
  33567. 0xC6, 0xC8, 0x18, 0x93, 0x08, 0xCE, 0x9A, 0x8C,
  33568. 0xE2, 0x91, 0x8D, 0x91, 0x57, 0x7B, 0x37, 0xC8,
  33569. 0x80, 0xA2, 0x31, 0x10, 0x0D, 0x4E, 0xEF, 0x51,
  33570. 0x07, 0x94, 0x8E, 0xF8, 0x3C, 0x3C, 0x2E, 0xD5,
  33571. 0x03, 0x26, 0xB8, 0x72, 0x7F, 0xB9, 0xBC, 0xD7,
  33572. 0x95, 0xC4, 0x31, 0x08, 0xEC, 0x6F, 0xEE, 0x11,
  33573. 0xAF, 0xC0, 0xA2, 0xEC, 0xD7, 0xC8, 0x0B, 0xBE,
  33574. 0x15, 0xAE, 0xC9, 0x17, 0xBE, 0x37, 0xE2, 0x40,
  33575. 0x83, 0x65, 0xDE, 0xB3, 0x4E, 0xB4, 0x15, 0xB3,
  33576. 0x5C, 0x14, 0xF6, 0x5F, 0xA9, 0x1F, 0x70, 0xB5,
  33577. 0x23, 0x93, 0x78, 0xB9, 0x47, 0xF9, 0x1D, 0x2B,
  33578. 0x1E, 0x8D, 0xB1, 0x25, 0x7E, 0xE5, 0x85, 0x3C,
  33579. 0x16, 0x9F, 0xD0, 0xC2, 0x67, 0x8B, 0x0D, 0xD2,
  33580. 0x72, 0x4E, 0x74, 0x30, 0xE1, 0xAF, 0xB8, 0x66,
  33581. 0xCB, 0x53, 0xDF, 0xC4, 0xFB, 0xA5, 0x6D, 0x03,
  33582. 0xF2, 0xAE, 0xEE, 0x90, 0xFE, 0xD7, 0x30, 0xAF,
  33583. 0x33, 0x98, 0x09, 0xEB, 0x75, 0xC7, 0x3E, 0xC8,
  33584. 0x2F, 0xE7, 0x22, 0x5F, 0x2F, 0x0A, 0xBD, 0xA4,
  33585. 0x22, 0x88, 0x28, 0x19, 0x35, 0x83, 0x12, 0x86,
  33586. 0xEE, 0x72, 0xB4, 0x26, 0x89, 0x2F, 0xC7, 0x11,
  33587. 0x6E, 0xDD, 0x14, 0x98, 0x22, 0xE7, 0x73, 0x3E,
  33588. 0xFA, 0x46, 0x75, 0xF9, 0x40, 0xC1, 0x84, 0x22,
  33589. 0xBC, 0x75, 0x36, 0xC7, 0x82, 0xD3, 0xAE, 0x6E,
  33590. 0x0D, 0xBF, 0x6F, 0xC3, 0x4B, 0x67, 0x49, 0x19,
  33591. 0xF3, 0x4B, 0x12, 0xF2, 0x83, 0xFD, 0x39, 0x56,
  33592. 0x44, 0x05, 0x3A, 0x24, 0x6A, 0x35, 0x69, 0x12,
  33593. 0xCF, 0xE4, 0x93, 0xFE, 0x26, 0xCC, 0xD6, 0x01,
  33594. 0xA0, 0x4A, 0x84, 0xA8, 0x1D, 0x85, 0xE6, 0x83,
  33595. 0x0F, 0x3C, 0xE6, 0x6D, 0xD2, 0xCB, 0xB1, 0x14,
  33596. 0x8C, 0xEC, 0x10, 0xB3, 0x63, 0x4B, 0x9C, 0xF5,
  33597. 0x11, 0xE0, 0xF9, 0x86, 0x6F, 0xA7, 0xC0, 0x3B,
  33598. 0x9D, 0x25, 0xD7, 0x54, 0xCA, 0x40, 0x4D, 0x26,
  33599. 0xBA, 0x71, 0x8E, 0x25, 0xF5, 0xA7, 0xE3, 0x9B,
  33600. 0x25, 0x20, 0x7F, 0x29, 0x05, 0xB6, 0x27, 0x14,
  33601. 0x17, 0x67, 0x26, 0x10, 0xAD, 0xA3, 0x06, 0x03,
  33602. 0xFE, 0x82, 0x85, 0x5D, 0x01, 0x04, 0x4D, 0xE0,
  33603. 0x64, 0x38, 0x38, 0x5E, 0x83, 0x1E, 0x21, 0x9A,
  33604. 0x39, 0x02, 0xF8, 0xF9, 0x69, 0x85, 0x52, 0xE5,
  33605. 0xEC, 0x6A, 0xAC, 0x96, 0x86, 0xA7, 0x88, 0x69,
  33606. 0xB5, 0xB5, 0x7E, 0x03, 0x1D, 0xA9, 0x68, 0xCA,
  33607. 0x45, 0x0F, 0xF9, 0x14, 0xD6, 0x7B, 0xCF, 0x9C,
  33608. 0x03, 0x6F, 0xD1, 0xD9, 0x6F, 0x01, 0x3D, 0xF8,
  33609. 0xF3, 0x11, 0xF3, 0x29, 0x17, 0x90, 0xE8, 0x9B,
  33610. 0xED, 0x58, 0x9B, 0xF0, 0xBC, 0xC7, 0xBA, 0xF4,
  33611. 0x60, 0xC8, 0xAA, 0x30, 0xB4, 0x2F, 0x22, 0x8F,
  33612. 0xD3, 0xAC, 0x18, 0xC2, 0xB7, 0xC4, 0x7B, 0x31,
  33613. 0x9E, 0x0F, 0x7E, 0x9D, 0xBF, 0xD4, 0x63, 0xC2,
  33614. 0x8B, 0x1B, 0x58, 0x50, 0x33, 0x53, 0x6D, 0x79,
  33615. 0xBB, 0xF8, 0x0D, 0x91, 0x33, 0xD9, 0x07, 0xE7,
  33616. 0xB0, 0x81, 0xD4, 0xB4, 0x47, 0x61, 0x93, 0xF0,
  33617. 0xFB, 0x68, 0xBC, 0x1B, 0x41, 0xC2, 0xF5, 0x43,
  33618. 0x30, 0x7E, 0x76, 0xF9, 0xB1, 0xA3, 0xD6, 0xD4,
  33619. 0x26, 0xEA, 0x77, 0x75, 0x12, 0x7A, 0xC8, 0x30,
  33620. 0x9B, 0xCF, 0x45, 0xBE, 0x74, 0x7D, 0x8A, 0x8B,
  33621. 0xEC, 0xED, 0x11, 0xE6, 0xA1, 0xD1, 0xB8, 0xF1,
  33622. 0x90, 0xAD, 0x6D, 0x6A, 0xC6, 0x54, 0xE9, 0xDB,
  33623. 0xAD, 0x4C, 0x97, 0x39, 0xC8, 0xD8, 0x44, 0xA9,
  33624. 0x1A, 0x37, 0x16, 0x7E, 0x68, 0x45, 0x0C, 0xBB,
  33625. 0x10, 0xF4, 0xAE, 0x8E, 0x2B, 0x69, 0xFA, 0x95,
  33626. 0x3E, 0xA5, 0xC9, 0x91, 0xD3, 0xF1, 0xA3, 0x89,
  33627. 0x3F, 0x90, 0x86, 0x93, 0x1B, 0xF1, 0xA0, 0x89,
  33628. 0xC7, 0xF2, 0x23, 0x57, 0xD4, 0x8E, 0x2F, 0xD5,
  33629. 0x71, 0xCD, 0x36, 0xF1, 0x90, 0xB3, 0x98, 0x3E,
  33630. 0x19, 0xEA, 0xC8, 0x0F, 0x12, 0x9D, 0xBF, 0x58,
  33631. 0xED, 0xDC, 0x6B, 0x9A, 0x79, 0x84, 0xFC, 0xF0,
  33632. 0x4C, 0xC3, 0xB4, 0x0D, 0xB8, 0x7A, 0x8D, 0xAD,
  33633. 0x75, 0x40, 0xD5, 0xD5, 0xDE, 0xC8, 0xCA, 0x39,
  33634. 0x3E, 0x45, 0xE4, 0xBC, 0xF4, 0x33, 0xEA, 0x64,
  33635. 0xE1, 0x5E, 0x94, 0x42, 0x91, 0xAB, 0xBC, 0x42,
  33636. 0x2A, 0xB3, 0xD0, 0x60, 0x23, 0xCE, 0x57, 0x8E,
  33637. 0xFF, 0xAD, 0xA2, 0x2B, 0x64, 0xD9, 0x94, 0xA0,
  33638. 0x80, 0x0F, 0x8E, 0x50, 0x17, 0x08, 0x1D, 0x16,
  33639. 0xCF, 0x51, 0xD0, 0xB9, 0x28, 0xB6, 0x59, 0xEF,
  33640. 0x78, 0xCC, 0xC9, 0x96, 0xF9, 0xCA, 0x87, 0x7A,
  33641. 0xEE, 0xD9, 0x15, 0x5E, 0xDF, 0x5D, 0xBC, 0xC2,
  33642. 0x58, 0xE6, 0x04, 0xEE, 0x17, 0xDC, 0xB3, 0xF9,
  33643. 0x90, 0xF9, 0x88, 0x32, 0x9E, 0xA1, 0xDB, 0x1C,
  33644. 0x38, 0x56, 0x53, 0x90, 0x30, 0x69, 0x2E, 0x52,
  33645. 0x00, 0x2C, 0xF3, 0x0F, 0xD5, 0x80, 0x2E, 0x02,
  33646. 0x5B, 0x99, 0xBF, 0xCD, 0x11, 0x12, 0x64, 0x5B,
  33647. 0x56, 0xC6, 0x0A, 0xE6, 0x38, 0xE7, 0x4D, 0x21,
  33648. 0xE5, 0x98, 0x78, 0x9D, 0xE6, 0xCB, 0x60, 0xB4,
  33649. 0x2E, 0xE4, 0x98, 0x56, 0xCB, 0xAD, 0xE6, 0xDD,
  33650. 0x53, 0xF4, 0xC5, 0x67, 0xA2, 0x9F, 0xA0, 0x5C,
  33651. 0x7C, 0xFB, 0x24, 0x5A, 0xA7, 0x72, 0xD0, 0xE7,
  33652. 0x63, 0xF2, 0x5D, 0xBF, 0xD8, 0xE9, 0xF1, 0x6B,
  33653. 0xB4, 0x29, 0xA6, 0x28, 0xE6, 0x93, 0xD3, 0x87,
  33654. 0xB6, 0xD9, 0x3C, 0x39, 0x8D, 0xEA, 0x28, 0xC0,
  33655. 0x96, 0x3D, 0xF5, 0xC2, 0x3C, 0x29, 0xF2, 0x80,
  33656. 0x21, 0x8A, 0x03, 0x9D, 0x64, 0xF8, 0xBA, 0x81,
  33657. 0xC1, 0xDD, 0xA2, 0x88, 0x2A, 0x84, 0x2E, 0x3C,
  33658. 0xB5, 0x03, 0x95, 0xED, 0xAA, 0x6E, 0xE2, 0x6F,
  33659. 0x5E, 0x99, 0x3C, 0x63, 0xEE, 0xB8, 0x4F, 0x66,
  33660. 0x32, 0x77, 0x42, 0x23, 0x36, 0x29, 0x89, 0xB0,
  33661. 0xED, 0x5F, 0xF2, 0x5A, 0x65, 0x66, 0x3F, 0xD2,
  33662. 0x8B, 0x48, 0x68, 0x65, 0xDC, 0xE0, 0xB0, 0xC2,
  33663. 0x72, 0x73, 0xF1, 0xA4, 0xC6, 0x56, 0x2C, 0x5D,
  33664. 0xD8, 0xC6, 0x5C, 0x41, 0xCE, 0x30, 0x89, 0x59,
  33665. 0xA9, 0xD6, 0x45, 0x96, 0xD0, 0x8E, 0x7B, 0x25,
  33666. 0xE0, 0x13, 0xFB, 0xFE, 0x7C, 0xEA, 0xF3, 0x67,
  33667. 0x0D, 0xB2, 0x9A, 0x21, 0x3C, 0xCE, 0x99, 0x75,
  33668. 0xA9, 0x13, 0xCE, 0xF4, 0x23, 0x6E, 0x64, 0x00,
  33669. 0x30, 0x87, 0x70, 0x9C, 0xAD, 0x61, 0x81, 0x71,
  33670. 0x0E, 0x95, 0x19, 0x26, 0xCA, 0x55, 0x29, 0x71,
  33671. 0x99, 0xA6, 0x08, 0xAE, 0x54, 0x58, 0x75, 0xCD,
  33672. 0xC3, 0x8F, 0xE3, 0x83, 0xC1, 0x45, 0x62, 0xB4,
  33673. 0x8D, 0xCA, 0x66, 0x02, 0xEA, 0x34, 0x05, 0x5D,
  33674. 0x98, 0x3F, 0x38, 0xE6, 0x1C, 0xCE, 0x53, 0x1A,
  33675. 0xD9, 0x3F, 0x58, 0xEC, 0x16, 0x28, 0x45, 0xF5,
  33676. 0x38, 0xCE, 0x48, 0x43, 0x87, 0x1D, 0x3C, 0x4A,
  33677. 0xDF, 0x05, 0xF3, 0x5E, 0x29, 0x7E, 0xA6, 0x2E,
  33678. 0xFC, 0xDD, 0x5E, 0xF9, 0x40, 0x1B, 0xA0, 0x42,
  33679. 0xA2, 0x35, 0x15, 0x0A, 0x09, 0xD9, 0x47, 0x4A,
  33680. 0x3F, 0xB0, 0x3A, 0xAA, 0x19, 0xE7, 0xE3, 0x7A,
  33681. 0x22, 0x8D, 0x5F, 0x5B, 0x07, 0x41, 0x4C, 0x3D,
  33682. 0xA2, 0xAD, 0x2E, 0x5C, 0x75, 0xEC, 0xF0, 0x4C,
  33683. 0x11, 0x2B, 0x90, 0x76, 0x9E, 0x19, 0x96, 0x0E,
  33684. 0x97, 0x5E, 0x8D, 0x19, 0x17, 0xB3, 0xBF, 0xDA,
  33685. 0x84, 0xFD, 0xC6, 0xD2, 0x32, 0x6F, 0xB8, 0xA3,
  33686. 0xB0, 0x0F, 0x95, 0xD9, 0xC5, 0x26, 0x50, 0x11,
  33687. 0x15, 0x72, 0xBE, 0xC2, 0x1B, 0x12, 0x12, 0x7C,
  33688. 0xA5, 0x70, 0xD8, 0xA9, 0x8A, 0xB9, 0x77, 0xEB,
  33689. 0xD8, 0xD7, 0x9A, 0x59, 0x37, 0x5E, 0xE1, 0x4F,
  33690. 0x64, 0xB5, 0xB0, 0x4F, 0xD9, 0x69, 0xFE, 0xB0,
  33691. 0x3D, 0x0A, 0xF7, 0x34, 0x89, 0xE3, 0xBA, 0xEF,
  33692. 0xE7, 0xC7, 0xBC, 0x8D, 0xC7, 0xE8, 0x54, 0x83,
  33693. 0xEE, 0x62, 0xF0, 0x23, 0x98, 0x58, 0x0F, 0x83,
  33694. 0xB9, 0x6D, 0xD8, 0x44, 0x77, 0xB9, 0xC4, 0x8F,
  33695. 0x0B, 0xB3, 0x9F, 0x54, 0x06, 0xA3, 0x70, 0x36,
  33696. 0xD6, 0xF3, 0x6E, 0x2B, 0x1B, 0x6B, 0x53, 0xFE,
  33697. 0x6F, 0xF6, 0x1C, 0x32, 0x7B, 0x29, 0xD4, 0xE0,
  33698. 0x5D, 0xD2, 0xB8, 0x11, 0x74, 0xC6, 0x0B, 0x59,
  33699. 0xC7, 0x9C, 0xB1, 0x97, 0x6B, 0xC0, 0x6E, 0x7A,
  33700. 0xC3, 0x4D, 0xF3, 0xE3, 0x8F, 0x7D, 0x2C, 0x1C,
  33701. 0x0E, 0x31, 0x51, 0xB7, 0x14, 0x7A, 0xB8, 0x31,
  33702. 0x77, 0x47, 0x70, 0x14, 0x3B, 0x92, 0x7B, 0x5F,
  33703. 0xEC, 0x5D, 0xF7, 0x76, 0xC1, 0xD7, 0x2D, 0xB6,
  33704. 0xBC, 0x99, 0x81, 0xD6, 0x58, 0x67, 0x71, 0x3C,
  33705. 0xF2, 0x97, 0xC8, 0xB0, 0xF1, 0xE9, 0x8D, 0x0E,
  33706. 0x16, 0xF0, 0xCC, 0x22, 0x7A, 0x39, 0xE4, 0x7E,
  33707. 0x50, 0xBA, 0x01, 0x16, 0x15, 0x6D, 0x5B, 0x54,
  33708. 0x67, 0x53, 0x66, 0x04, 0xBE, 0x05, 0xCC, 0x2E,
  33709. 0xF4, 0x0A, 0xBC, 0xE8, 0x52, 0xF1, 0x5D, 0xFA,
  33710. 0x2C, 0xAC, 0xF8, 0x6A, 0x78, 0x9E, 0x5B, 0x7B,
  33711. 0x0E, 0x5B, 0xB4, 0xB7, 0x77, 0xCD, 0x7C, 0xC9,
  33712. 0xF6, 0x54, 0x77, 0x9B, 0x10, 0x2F, 0x78, 0xB5,
  33713. 0xAA, 0x4B, 0x94, 0xC3, 0xB4, 0xFD, 0xE5, 0x5F,
  33714. 0xA7, 0xF7, 0xBF, 0x54, 0xAC, 0x22, 0x5E, 0x1F,
  33715. 0x26, 0x16, 0x5B, 0x65, 0xF1, 0x6D, 0x03, 0x21,
  33716. 0x66, 0x9F, 0xD9, 0xF6, 0xE4, 0x7F, 0xCA, 0x1D,
  33717. 0xD3, 0x47, 0x09, 0x6D, 0xF5, 0xDD, 0xA8, 0x64,
  33718. 0x66, 0xA5, 0x7C, 0x5B, 0x06, 0x8D, 0x9C, 0x67,
  33719. 0xB7, 0x32, 0x03, 0x66, 0xEA, 0x19, 0xC8, 0x99,
  33720. 0x3F, 0xF9, 0x0B, 0xD8, 0xFB, 0x06, 0x93, 0xFB,
  33721. 0xA3, 0x70, 0xE6, 0x6D, 0x2B, 0x20, 0x3B, 0x99,
  33722. 0x70, 0x11, 0xB0, 0xD1, 0x5B, 0x94, 0xE2, 0x8B,
  33723. 0xAA, 0x2E, 0xBF, 0x01, 0x77, 0x4F, 0x7A, 0xE7,
  33724. 0x8F, 0x84, 0xED, 0xBD, 0xAD, 0x9F, 0x65, 0xA4,
  33725. 0x50, 0x42, 0x7A, 0x47, 0x74, 0xC6, 0x0C, 0xC8,
  33726. 0x9A, 0x02, 0x0B, 0x37, 0xDA, 0x21, 0xC7, 0x91,
  33727. 0xDA, 0xC8, 0xF7, 0xA7, 0x45, 0x7E, 0x30, 0xD0,
  33728. 0x8B, 0x01, 0x37, 0x51, 0x60, 0x03, 0x9C, 0x30,
  33729. 0x1B, 0x60, 0x51, 0xA9, 0x65, 0xE8, 0xA7, 0xCC,
  33730. 0xA2, 0xAE, 0xF9, 0x3B, 0xD5, 0x2F, 0x82, 0xC0,
  33731. 0x20, 0xBE, 0xCE, 0x90, 0xA1, 0x29, 0x02, 0x4E,
  33732. 0xFE, 0xA4, 0xB2, 0xFA, 0x21, 0x27, 0x0F, 0x8E,
  33733. 0xB5, 0xED, 0x6A, 0xAA, 0xE5, 0x59, 0x29, 0xAA,
  33734. 0xC5, 0x99, 0xA5, 0x77, 0x97, 0x29, 0x57, 0x66,
  33735. 0x0C, 0xC4, 0x7A, 0xC4, 0xE3, 0xCE, 0x77, 0x2B,
  33736. 0xBF, 0x10, 0x05, 0x2D, 0xE7, 0xED, 0xB1, 0xB8,
  33737. 0xA4, 0x49, 0x41, 0xF8, 0x84, 0xC9, 0xF8, 0xBE,
  33738. 0x13, 0x17, 0x46, 0x69, 0x94, 0x56, 0x29, 0xF4,
  33739. 0x6D, 0xE2, 0x46, 0x74, 0x44, 0xF3, 0x10, 0x6A,
  33740. 0x73, 0xFA, 0x27, 0x9C, 0xF0, 0x2A, 0x80, 0x0A,
  33741. 0x04, 0x7E, 0x20, 0xBD, 0x4D, 0x82, 0x0B, 0x38,
  33742. 0x9C, 0x3B, 0xB6, 0xA8, 0x68, 0xA5, 0x38, 0x4C,
  33743. 0xF5, 0x72, 0x4C, 0x20, 0x4C, 0xEF, 0xB1, 0xA6,
  33744. 0xA1, 0xBE, 0xB9, 0x72, 0x3E, 0x36, 0xDD, 0xDD,
  33745. 0xD9, 0xC7, 0x07, 0xC8, 0xF6, 0x3E, 0x8B, 0xC2,
  33746. 0x66, 0x83, 0xCC, 0x8B, 0x43, 0xC7, 0xDF, 0xDA,
  33747. 0xA4, 0x08, 0xAC, 0x4D, 0xD2, 0xBA, 0x9A, 0xEC,
  33748. 0xBC, 0x3B, 0x6D, 0xDA, 0xED, 0xCE, 0x09, 0x4A,
  33749. 0xAB, 0x58, 0xFF, 0x73, 0x2B, 0x19, 0x66, 0x38,
  33750. 0xD8, 0xB8, 0xEF, 0xC4, 0x28, 0xBB, 0xA9, 0x61,
  33751. 0x57, 0x93, 0xC4, 0xDD, 0x9F, 0x00, 0xF9, 0x0D,
  33752. 0x62, 0xC6, 0x76, 0xD1, 0x27, 0xA0, 0xE1, 0x8C,
  33753. 0x14, 0xC6, 0xEE, 0x9C, 0x99, 0x05, 0x10, 0xB0,
  33754. 0x54, 0xAD, 0xB4, 0xB4, 0x17, 0x0A, 0xC7, 0x12,
  33755. 0x7F, 0x93, 0x17, 0x5C, 0x1E, 0xB2, 0x25, 0x12
  33756. };
  33757. static const byte msg_87[] = {
  33758. 0x14, 0x42, 0x63, 0x34, 0x94, 0x09, 0x60, 0x77,
  33759. 0x3B, 0xFF, 0x65, 0xF0, 0x8D, 0x1D, 0xE4, 0x89,
  33760. 0xC4, 0xC3, 0xED, 0x36
  33761. };
  33762. static const byte sig_87[] = {
  33763. 0x13, 0xE8, 0x99, 0xEE, 0xDC, 0xCC, 0x0F, 0xBA,
  33764. 0x62, 0x91, 0x44, 0xE4, 0xAC, 0x06, 0x79, 0x06,
  33765. 0xB5, 0x32, 0x6B, 0x8F, 0x9A, 0x6C, 0xCB, 0xAB,
  33766. 0xE1, 0x44, 0x4A, 0xDD, 0x46, 0x45, 0x16, 0x0D,
  33767. 0x22, 0x57, 0x82, 0x87, 0x10, 0xD1, 0xEE, 0x10,
  33768. 0x60, 0x21, 0xB5, 0x64, 0x1E, 0x78, 0x81, 0x55,
  33769. 0x75, 0xD4, 0xF0, 0x95, 0xD0, 0x15, 0xD8, 0x46,
  33770. 0x5C, 0x92, 0xD2, 0xDD, 0xF4, 0xAB, 0xDF, 0xBE,
  33771. 0xB1, 0x1E, 0xE5, 0xE0, 0x70, 0xE6, 0xDA, 0x52,
  33772. 0xE5, 0x48, 0xDC, 0x04, 0xFD, 0xEF, 0x54, 0x72,
  33773. 0xE7, 0xE5, 0xF1, 0x82, 0x10, 0xAA, 0xCB, 0xA0,
  33774. 0x4F, 0x4F, 0x18, 0xAE, 0x66, 0x86, 0xB9, 0xAF,
  33775. 0x96, 0x57, 0xE3, 0x8E, 0x3B, 0x9B, 0xDD, 0xB4,
  33776. 0xAA, 0x84, 0xE6, 0x7B, 0x4D, 0x81, 0x92, 0xD0,
  33777. 0x03, 0x87, 0x3D, 0xD3, 0xEE, 0xE7, 0x47, 0x00,
  33778. 0xFB, 0xD8, 0x1E, 0x38, 0x1C, 0x21, 0x98, 0xB7,
  33779. 0xCC, 0xC1, 0x37, 0xC1, 0x71, 0xB2, 0x2F, 0x93,
  33780. 0x53, 0x41, 0x9C, 0x48, 0xC1, 0x4B, 0x8D, 0x63,
  33781. 0x0F, 0x99, 0x63, 0x40, 0x27, 0x5F, 0x6E, 0x60,
  33782. 0x4B, 0x95, 0xC4, 0x35, 0x20, 0x8A, 0xED, 0x2B,
  33783. 0xCA, 0x1B, 0x41, 0x9F, 0x83, 0x63, 0xF0, 0x95,
  33784. 0x0E, 0x24, 0x0D, 0x6F, 0x9E, 0xAB, 0x11, 0x8E,
  33785. 0x4B, 0xD3, 0xDA, 0x0E, 0xC3, 0xA2, 0xBE, 0x26,
  33786. 0xA8, 0xA0, 0x98, 0x57, 0x71, 0x3C, 0x36, 0xDD,
  33787. 0x69, 0xC3, 0x4E, 0xDD, 0x2C, 0x61, 0x9E, 0x88,
  33788. 0x26, 0x70, 0x71, 0xCF, 0x9E, 0xE5, 0xA6, 0x0C,
  33789. 0xA3, 0x14, 0x2D, 0xF1, 0x63, 0xF0, 0x1D, 0x8D,
  33790. 0x79, 0x6A, 0xC8, 0x50, 0xCF, 0xF3, 0x66, 0x60,
  33791. 0x78, 0xB3, 0x18, 0xFB, 0x5B, 0xD1, 0x73, 0x60,
  33792. 0xC8, 0x76, 0xC9, 0xC9, 0x0D, 0x8A, 0x7F, 0x41,
  33793. 0x2C, 0x8A, 0x31, 0x61, 0x6B, 0xE7, 0xA3, 0x74,
  33794. 0x58, 0x71, 0x54, 0x84, 0x86, 0x71, 0x5C, 0x94,
  33795. 0x26, 0x3A, 0x17, 0xB3, 0x6C, 0xA4, 0x99, 0x25,
  33796. 0x45, 0x0C, 0x57, 0x8A, 0xD9, 0xD4, 0xB1, 0xC2,
  33797. 0x00, 0x43, 0xF4, 0x5E, 0x84, 0x31, 0x99, 0x4F,
  33798. 0xA6, 0xD2, 0x6A, 0x14, 0x1B, 0xAD, 0x9E, 0x49,
  33799. 0x6E, 0x00, 0x9E, 0x91, 0x46, 0x16, 0xCA, 0x57,
  33800. 0x0C, 0x09, 0xF6, 0x38, 0xD0, 0x62, 0xBE, 0xC6,
  33801. 0x87, 0x33, 0x3A, 0xC7, 0x28, 0x38, 0x34, 0x53,
  33802. 0x7E, 0xFB, 0x60, 0x42, 0xF3, 0x7D, 0x83, 0xF7,
  33803. 0x29, 0x5D, 0xEA, 0x30, 0xD5, 0x00, 0x90, 0xB6,
  33804. 0x38, 0x4C, 0x17, 0x29, 0xEF, 0x17, 0xA0, 0xD5,
  33805. 0x87, 0x50, 0xC0, 0x03, 0x75, 0x14, 0xE5, 0xE1,
  33806. 0x22, 0x78, 0x53, 0xBC, 0x5A, 0xA3, 0x1E, 0x95,
  33807. 0xBE, 0xEC, 0x37, 0xB1, 0x51, 0x82, 0x69, 0x26,
  33808. 0x2E, 0xA3, 0x5A, 0xDA, 0x4F, 0xDA, 0x77, 0x62,
  33809. 0x7E, 0xED, 0xDA, 0xAF, 0x57, 0x97, 0x1B, 0xA3,
  33810. 0x6D, 0x46, 0x7B, 0x19, 0xA9, 0x0B, 0x99, 0x1C,
  33811. 0xD2, 0x55, 0xDB, 0x79, 0xB0, 0x15, 0x48, 0x86,
  33812. 0x52, 0x30, 0x31, 0xD6, 0xC5, 0xB1, 0xAE, 0x8F,
  33813. 0xCF, 0x9A, 0x43, 0x10, 0xBB, 0xC8, 0x19, 0x74,
  33814. 0x84, 0xB2, 0x92, 0x3B, 0xFE, 0x0B, 0x12, 0x15,
  33815. 0xA1, 0xC4, 0xD8, 0xC6, 0x83, 0x90, 0x89, 0x8A,
  33816. 0xD5, 0x3E, 0x33, 0x69, 0xB7, 0x05, 0x3F, 0xB1,
  33817. 0x8B, 0x0D, 0x87, 0x40, 0x70, 0x90, 0x2A, 0x5D,
  33818. 0x3B, 0x3D, 0x91, 0xD8, 0x1D, 0x4D, 0xF1, 0x08,
  33819. 0x7E, 0xF7, 0xDC, 0x05, 0x84, 0xEB, 0xDC, 0x63,
  33820. 0xD7, 0xBA, 0x3C, 0x0D, 0x31, 0xF8, 0x6D, 0xA6,
  33821. 0xC0, 0xFD, 0x08, 0x11, 0x5C, 0x53, 0xF6, 0xAE,
  33822. 0xFE, 0xC0, 0x82, 0x9A, 0x68, 0xD2, 0xA3, 0x44,
  33823. 0x2E, 0xEE, 0x47, 0x36, 0x70, 0x2D, 0x66, 0x81,
  33824. 0x0D, 0x62, 0x30, 0x8A, 0x8C, 0xC8, 0x2A, 0xA6,
  33825. 0x21, 0x82, 0xF5, 0x98, 0xF4, 0x4E, 0x25, 0x37,
  33826. 0x11, 0xB5, 0xD6, 0x07, 0x88, 0xBD, 0x0D, 0x69,
  33827. 0x0E, 0xF9, 0x8F, 0x9A, 0xD5, 0x93, 0xE0, 0x3C,
  33828. 0xEF, 0x38, 0xB9, 0xC9, 0x77, 0x98, 0x3F, 0x69,
  33829. 0x11, 0xBA, 0x1A, 0xB9, 0xF7, 0x35, 0xE9, 0x28,
  33830. 0xCD, 0xA3, 0x8C, 0x03, 0xE6, 0xAD, 0x83, 0x62,
  33831. 0xF4, 0x60, 0xAE, 0x4C, 0xD0, 0xF4, 0x6E, 0x00,
  33832. 0xEE, 0xEC, 0x74, 0xB6, 0x12, 0x34, 0x98, 0xAB,
  33833. 0x31, 0xE7, 0xA7, 0x9D, 0x33, 0x4D, 0x72, 0xA7,
  33834. 0xA7, 0xEE, 0xF3, 0xB5, 0x51, 0xE7, 0x8D, 0x31,
  33835. 0xBC, 0x2C, 0xAF, 0xFB, 0x13, 0x9C, 0xAC, 0xA4,
  33836. 0xD7, 0x9C, 0x8B, 0xBD, 0x52, 0xBD, 0x78, 0xF4,
  33837. 0x90, 0x65, 0x09, 0xBE, 0x42, 0xE7, 0x76, 0x3A,
  33838. 0xE6, 0xAC, 0xB8, 0x98, 0x28, 0x5E, 0xC9, 0x32,
  33839. 0x3E, 0x68, 0x67, 0x6A, 0x8C, 0xC7, 0x4A, 0x58,
  33840. 0xC8, 0xDA, 0x8B, 0xE9, 0x11, 0xED, 0x6F, 0x51,
  33841. 0x3B, 0x66, 0x08, 0x70, 0x73, 0x10, 0xFB, 0x45,
  33842. 0xCB, 0xD9, 0x7D, 0x5F, 0xF0, 0xD2, 0xAB, 0xA3,
  33843. 0x6F, 0xCE, 0xF7, 0x3D, 0x46, 0xCB, 0x7F, 0x01,
  33844. 0xC2, 0xCF, 0xE3, 0x8E, 0x68, 0xE8, 0x4F, 0x4A,
  33845. 0x30, 0x19, 0x16, 0xD2, 0xF5, 0x10, 0xD8, 0x2B,
  33846. 0x49, 0x69, 0xBE, 0x7A, 0x0E, 0x9C, 0xC6, 0x0E,
  33847. 0xFF, 0x5C, 0x0A, 0x87, 0x17, 0xB8, 0x22, 0x83,
  33848. 0x8C, 0x77, 0xAF, 0x42, 0x06, 0xB1, 0x25, 0x45,
  33849. 0x08, 0x9B, 0xB2, 0xDD, 0x6A, 0x3F, 0xF0, 0x12,
  33850. 0xC8, 0x64, 0x15, 0xBB, 0xA0, 0x4F, 0xD7, 0xD4,
  33851. 0xEC, 0x70, 0x7A, 0xF3, 0xB1, 0x7F, 0x25, 0x57,
  33852. 0x47, 0x66, 0xF1, 0xE9, 0x27, 0x38, 0xE0, 0x62,
  33853. 0x10, 0xF4, 0x8A, 0x5E, 0xF2, 0x55, 0x0E, 0xBD,
  33854. 0xF8, 0x5A, 0x5C, 0xA3, 0x44, 0x97, 0xCF, 0x1D,
  33855. 0x4D, 0x3A, 0x75, 0x86, 0x48, 0xEC, 0x41, 0x17,
  33856. 0x24, 0x43, 0x83, 0x5E, 0x50, 0x91, 0xBE, 0x8F,
  33857. 0x04, 0x78, 0x23, 0xD9, 0x62, 0x0C, 0x2A, 0xD5,
  33858. 0x1C, 0x96, 0x11, 0xAA, 0xEE, 0x39, 0xB2, 0x1E,
  33859. 0x6D, 0x6A, 0xEC, 0x87, 0x0C, 0x89, 0x15, 0xE2,
  33860. 0x66, 0x47, 0x6A, 0x50, 0xEE, 0xCA, 0x59, 0x96,
  33861. 0x22, 0xF7, 0x09, 0x1A, 0x34, 0xC2, 0x3F, 0x14,
  33862. 0xB4, 0x04, 0x29, 0xD9, 0x5E, 0x3E, 0xF9, 0x8F,
  33863. 0xED, 0x3E, 0x74, 0x94, 0x37, 0xF0, 0x4B, 0xB4,
  33864. 0xA3, 0x37, 0x52, 0x2E, 0x68, 0x09, 0xFC, 0x10,
  33865. 0x45, 0x03, 0xE2, 0x53, 0xB4, 0x1C, 0x4F, 0x03,
  33866. 0x01, 0xAF, 0x46, 0x7F, 0x74, 0xD3, 0x31, 0x25,
  33867. 0xFA, 0x83, 0xEF, 0x71, 0x24, 0x45, 0xA1, 0x71,
  33868. 0xFA, 0x40, 0xEB, 0xF4, 0xE6, 0x55, 0x3E, 0x45,
  33869. 0x4A, 0xFE, 0x25, 0x68, 0x02, 0x1D, 0x2B, 0x2A,
  33870. 0x19, 0x8D, 0xEC, 0x9B, 0xF7, 0x20, 0xF9, 0xD7,
  33871. 0x2F, 0x81, 0x52, 0x0B, 0xE8, 0x74, 0x66, 0xAF,
  33872. 0x70, 0xD0, 0x0E, 0x0E, 0x86, 0x0F, 0xF9, 0xAB,
  33873. 0xD0, 0x39, 0x78, 0xC3, 0xE4, 0x29, 0xB5, 0xAA,
  33874. 0x17, 0xB9, 0x7F, 0x9A, 0xE9, 0x34, 0x48, 0x85,
  33875. 0x3D, 0x6E, 0xFD, 0x16, 0x8A, 0x30, 0xC6, 0xCB,
  33876. 0xE8, 0xDE, 0x2D, 0x28, 0x8D, 0x9A, 0x24, 0xEA,
  33877. 0x5D, 0x2A, 0x58, 0x23, 0x33, 0x2B, 0x84, 0xFD,
  33878. 0x2C, 0xE7, 0x93, 0xA2, 0x2B, 0xEC, 0x43, 0x98,
  33879. 0x48, 0xD4, 0xE6, 0x0F, 0x3B, 0xB9, 0xC7, 0x5D,
  33880. 0x7E, 0xB0, 0x87, 0x1E, 0x80, 0x3D, 0x61, 0xB0,
  33881. 0x7E, 0x74, 0x9E, 0xD7, 0x60, 0x72, 0xB2, 0x7C,
  33882. 0x87, 0xB6, 0x9D, 0x6C, 0x01, 0x42, 0x61, 0xF6,
  33883. 0x47, 0xAF, 0xA8, 0x8C, 0x4F, 0x1E, 0xC5, 0x5A,
  33884. 0x75, 0xA5, 0x0F, 0xB4, 0xC7, 0x9D, 0x2C, 0x94,
  33885. 0xC0, 0x50, 0x3D, 0xB2, 0x0D, 0xFD, 0xF7, 0x1F,
  33886. 0x62, 0x88, 0x74, 0x18, 0x8C, 0xDD, 0x73, 0x85,
  33887. 0xC0, 0x33, 0x81, 0xDA, 0xBB, 0x85, 0x4D, 0x4A,
  33888. 0xA9, 0xF4, 0x7B, 0x66, 0x43, 0x8C, 0x43, 0xFF,
  33889. 0x53, 0xEF, 0x5E, 0x78, 0xAB, 0x45, 0x0B, 0x45,
  33890. 0x01, 0x91, 0x27, 0x8A, 0xF6, 0xE2, 0x6A, 0x7B,
  33891. 0x5E, 0x64, 0x61, 0xF5, 0x77, 0xF9, 0x85, 0x2F,
  33892. 0x81, 0xC9, 0x02, 0x03, 0xC7, 0x13, 0xF5, 0xB1,
  33893. 0xF6, 0xC3, 0xEF, 0x55, 0x8C, 0x90, 0x32, 0x51,
  33894. 0x6D, 0x8D, 0x62, 0xFD, 0x5E, 0x24, 0xE4, 0xF0,
  33895. 0xF5, 0x07, 0x18, 0xF5, 0x6B, 0x5A, 0x59, 0xA0,
  33896. 0x09, 0xD5, 0x93, 0x8D, 0xAD, 0x55, 0x91, 0xF6,
  33897. 0x1F, 0x4C, 0x65, 0x9A, 0x76, 0x05, 0x26, 0xEF,
  33898. 0x41, 0x20, 0x2F, 0xA7, 0xE5, 0xF6, 0xC7, 0xD5,
  33899. 0xE0, 0xB0, 0xC0, 0xC4, 0x3B, 0x52, 0x4B, 0x66,
  33900. 0x71, 0x2C, 0x5A, 0x7C, 0x53, 0xC8, 0x4C, 0x50,
  33901. 0xB8, 0x3E, 0xB9, 0xC9, 0x8D, 0x2F, 0xD0, 0x84,
  33902. 0xC9, 0xC5, 0xF2, 0x1F, 0xEE, 0x77, 0x42, 0xE6,
  33903. 0xEF, 0xC8, 0xCB, 0xBE, 0x57, 0x18, 0xB7, 0x0C,
  33904. 0x06, 0x2D, 0x82, 0xE2, 0xF9, 0x86, 0xF3, 0x8D,
  33905. 0xF1, 0xE7, 0x15, 0x89, 0xDC, 0x79, 0x87, 0x24,
  33906. 0x35, 0x62, 0xA2, 0x31, 0x9D, 0x7C, 0x00, 0xB2,
  33907. 0x6E, 0x53, 0x1E, 0x93, 0xC3, 0x84, 0x44, 0x61,
  33908. 0x8C, 0xE7, 0x58, 0x73, 0x4F, 0xDE, 0xCF, 0xD0,
  33909. 0xC6, 0x85, 0x37, 0x28, 0xC6, 0x10, 0x00, 0x78,
  33910. 0x4E, 0xDF, 0xFE, 0xD7, 0xB3, 0x30, 0x86, 0xE1,
  33911. 0x68, 0xD6, 0xCB, 0x63, 0xE3, 0xDA, 0xCA, 0xF3,
  33912. 0x55, 0x2F, 0x88, 0x5B, 0x47, 0x82, 0x62, 0xDE,
  33913. 0x5E, 0x1E, 0x63, 0xCE, 0x7A, 0x4C, 0x66, 0x95,
  33914. 0xD1, 0x19, 0x38, 0x35, 0xE4, 0x5A, 0x67, 0x91,
  33915. 0x8C, 0x42, 0xD3, 0x9B, 0xF8, 0x80, 0x38, 0x53,
  33916. 0x30, 0x31, 0x0F, 0x2C, 0x7B, 0xF9, 0x1E, 0x6C,
  33917. 0x3E, 0x29, 0xB7, 0x81, 0xD0, 0x98, 0x70, 0xC2,
  33918. 0x6D, 0x76, 0xBD, 0x8A, 0xE2, 0x09, 0xC4, 0x2B,
  33919. 0xC7, 0x43, 0x2D, 0xBB, 0x4C, 0x16, 0x52, 0x63,
  33920. 0x57, 0xA5, 0x63, 0x4E, 0xEC, 0xDE, 0x93, 0xC5,
  33921. 0x1D, 0xD4, 0xD6, 0xF0, 0x06, 0x5B, 0x2E, 0xC5,
  33922. 0x7A, 0xD3, 0xB5, 0x82, 0x66, 0x53, 0x95, 0x97,
  33923. 0xC8, 0xF4, 0x2B, 0x55, 0x27, 0x1D, 0x6F, 0x90,
  33924. 0xE9, 0x86, 0xF6, 0x82, 0x8D, 0x95, 0x9E, 0xE8,
  33925. 0x00, 0xDB, 0xEB, 0xCF, 0x48, 0x23, 0x6B, 0xA3,
  33926. 0xDE, 0x25, 0x27, 0xE0, 0xEC, 0xA4, 0xA3, 0xC2,
  33927. 0xA3, 0x4B, 0xBC, 0xDD, 0x6C, 0xBB, 0x3A, 0x9C,
  33928. 0x96, 0xDC, 0x3B, 0xE1, 0x10, 0xD3, 0x49, 0x94,
  33929. 0x66, 0xE2, 0x85, 0x7F, 0xBA, 0x98, 0x12, 0x3A,
  33930. 0x6D, 0xBA, 0x90, 0x14, 0x87, 0x7E, 0x24, 0xEA,
  33931. 0xDC, 0xCA, 0x40, 0xF8, 0xAE, 0x94, 0xB2, 0xFE,
  33932. 0xD2, 0x36, 0xCB, 0xE5, 0xBC, 0xA9, 0xDF, 0xE0,
  33933. 0xCB, 0xA9, 0xA0, 0xF8, 0x62, 0x41, 0x33, 0x18,
  33934. 0x59, 0xF9, 0xD6, 0xC0, 0x87, 0xB2, 0x76, 0xDE,
  33935. 0xC9, 0x35, 0x6F, 0x1F, 0xEF, 0x69, 0xB3, 0x59,
  33936. 0xF9, 0xFB, 0x38, 0x4A, 0x84, 0x02, 0x2D, 0xEC,
  33937. 0xB7, 0x01, 0x08, 0xDA, 0xC8, 0xE9, 0x3B, 0xB6,
  33938. 0xC3, 0x00, 0xC0, 0x34, 0x5F, 0xC6, 0x40, 0xC0,
  33939. 0x06, 0xEA, 0xEB, 0xC1, 0x51, 0x13, 0x81, 0x2F,
  33940. 0xB3, 0x7D, 0xD9, 0x6E, 0x2A, 0x06, 0xA4, 0x63,
  33941. 0xAF, 0xCE, 0x66, 0xC5, 0x9F, 0x8D, 0x71, 0x4A,
  33942. 0xA1, 0xFF, 0x49, 0x4F, 0x08, 0x6F, 0xB9, 0xEA,
  33943. 0xDA, 0x18, 0x45, 0x63, 0xCA, 0x9D, 0x88, 0x08,
  33944. 0xB1, 0x6C, 0x19, 0xA8, 0x24, 0xAD, 0x85, 0x7D,
  33945. 0xDE, 0x51, 0xE5, 0x08, 0xB7, 0x04, 0x12, 0x35,
  33946. 0xF3, 0x00, 0xED, 0x2C, 0x79, 0x9C, 0x18, 0x23,
  33947. 0x05, 0x38, 0x95, 0x76, 0xCF, 0x39, 0x3C, 0xAE,
  33948. 0xB0, 0xD3, 0xBA, 0x3E, 0x4E, 0xE4, 0xB5, 0x77,
  33949. 0xA3, 0xE3, 0x7B, 0x27, 0x5F, 0xD8, 0x05, 0x19,
  33950. 0x42, 0xAE, 0x91, 0x54, 0xE5, 0xBD, 0x7C, 0x35,
  33951. 0xE0, 0xF8, 0x95, 0x52, 0x3A, 0x29, 0xB0, 0xE6,
  33952. 0xB7, 0xAE, 0x20, 0xBE, 0x21, 0xDF, 0xF5, 0x67,
  33953. 0xEC, 0x82, 0x52, 0xFF, 0x5B, 0xD0, 0xAA, 0x14,
  33954. 0x50, 0x15, 0xE1, 0x1C, 0x6A, 0x1B, 0x94, 0x1B,
  33955. 0xCC, 0x76, 0x01, 0xBF, 0x03, 0x94, 0x42, 0xF2,
  33956. 0x00, 0x61, 0x96, 0x58, 0xD9, 0xD0, 0x40, 0x21,
  33957. 0xFA, 0xCE, 0x6B, 0xAB, 0x5D, 0x49, 0xD8, 0xD7,
  33958. 0xBC, 0x9A, 0x66, 0xC2, 0xBA, 0x3F, 0xDC, 0x49,
  33959. 0x0D, 0xA5, 0x5C, 0xB4, 0x67, 0x08, 0x38, 0xEB,
  33960. 0x2D, 0x07, 0x24, 0x5B, 0xB1, 0x22, 0x7B, 0x02,
  33961. 0x4A, 0x8A, 0x53, 0x38, 0xE9, 0x42, 0x8E, 0xA5,
  33962. 0x57, 0x41, 0xD6, 0x71, 0xA7, 0x9D, 0x6A, 0x14,
  33963. 0xD2, 0x7D, 0x13, 0xFB, 0x59, 0xD0, 0xDA, 0xE5,
  33964. 0x23, 0x9E, 0x1B, 0xC4, 0x21, 0x87, 0xBB, 0x78,
  33965. 0xE0, 0x38, 0x01, 0x1D, 0xA0, 0xD1, 0x36, 0x3F,
  33966. 0xD0, 0xA7, 0x8F, 0x86, 0x26, 0x1E, 0xB0, 0x26,
  33967. 0xDE, 0x7E, 0x17, 0x3A, 0x90, 0xFC, 0xC0, 0x17,
  33968. 0xDD, 0x78, 0xF5, 0xA3, 0x2D, 0x3E, 0x29, 0xCE,
  33969. 0x38, 0x45, 0x76, 0xA9, 0x55, 0x11, 0xB6, 0xB4,
  33970. 0xE5, 0x6E, 0xDD, 0x01, 0x4B, 0x16, 0x07, 0x99,
  33971. 0xBD, 0x19, 0x77, 0xF5, 0xD7, 0x9E, 0x39, 0x9E,
  33972. 0xAA, 0x8E, 0x2B, 0x75, 0xC5, 0xEB, 0x33, 0x56,
  33973. 0x6C, 0xD8, 0xB6, 0x3F, 0x3F, 0x4E, 0x81, 0x7E,
  33974. 0x29, 0x0A, 0x68, 0xED, 0x1E, 0x9F, 0xDC, 0x6B,
  33975. 0xFA, 0x18, 0xE3, 0xE5, 0x7D, 0x05, 0x7F, 0x22,
  33976. 0xFA, 0xA2, 0xF6, 0x0F, 0xB6, 0x34, 0x56, 0x72,
  33977. 0x55, 0x16, 0x5E, 0xF4, 0x18, 0xD1, 0x82, 0xFA,
  33978. 0xDD, 0xF7, 0xB8, 0x9F, 0x7D, 0x30, 0x10, 0x69,
  33979. 0xC4, 0x85, 0xD8, 0xE8, 0x34, 0x89, 0xD4, 0x93,
  33980. 0xBE, 0x56, 0xEE, 0xDC, 0x43, 0xD4, 0x82, 0x00,
  33981. 0xFD, 0x1E, 0x2B, 0x06, 0x69, 0x07, 0x1B, 0xBF,
  33982. 0x33, 0x61, 0x39, 0x28, 0xCA, 0x31, 0x91, 0x0B,
  33983. 0xF2, 0xEA, 0x32, 0x8E, 0xA8, 0x64, 0x13, 0x9A,
  33984. 0xEF, 0x79, 0x1A, 0x9A, 0xBE, 0x52, 0x13, 0x32,
  33985. 0x49, 0x93, 0x7D, 0xA8, 0x8C, 0x48, 0xD4, 0xC0,
  33986. 0x1D, 0x10, 0x8A, 0x46, 0x85, 0xAD, 0x29, 0xDF,
  33987. 0x2E, 0xCD, 0x41, 0x83, 0x82, 0x01, 0x28, 0x44,
  33988. 0x0E, 0xE5, 0x37, 0x8D, 0x6B, 0xCA, 0x61, 0x98,
  33989. 0xDE, 0x89, 0xA9, 0x7B, 0xBB, 0x44, 0x48, 0xA2,
  33990. 0x8D, 0x82, 0x3A, 0x57, 0x40, 0x60, 0x7C, 0x6E,
  33991. 0x69, 0x98, 0x98, 0x93, 0xFA, 0x7E, 0x29, 0x9A,
  33992. 0x74, 0x53, 0xD8, 0xDC, 0xB3, 0x4B, 0xDB, 0x7E,
  33993. 0xFE, 0x95, 0xB0, 0xC7, 0x23, 0x14, 0xEF, 0xCB,
  33994. 0x49, 0x3C, 0x09, 0xD7, 0x7B, 0xD0, 0x11, 0x9B,
  33995. 0xAC, 0xF2, 0xC2, 0x2E, 0x7C, 0xCB, 0xCD, 0x59,
  33996. 0x7F, 0x6A, 0x09, 0xFE, 0xFE, 0xDF, 0xA0, 0xA7,
  33997. 0xAC, 0x3C, 0x90, 0xBA, 0x75, 0x19, 0xF4, 0x01,
  33998. 0x60, 0x56, 0xD5, 0xFB, 0x41, 0x2B, 0xA0, 0x2D,
  33999. 0x0D, 0x45, 0xCF, 0xF3, 0xA6, 0x3D, 0x36, 0xEE,
  34000. 0xE1, 0xE4, 0x68, 0xE6, 0xEA, 0x2F, 0x67, 0x3A,
  34001. 0x7A, 0x02, 0x92, 0x6B, 0xB3, 0x18, 0xBA, 0x73,
  34002. 0xEE, 0x1B, 0x2C, 0x13, 0x7D, 0xEF, 0x4A, 0x39,
  34003. 0xE8, 0x03, 0xFF, 0x57, 0x35, 0x53, 0xE9, 0xA5,
  34004. 0xC6, 0xAA, 0x1A, 0x17, 0x21, 0xCA, 0x54, 0x38,
  34005. 0x7C, 0xB1, 0xDF, 0xB8, 0xFA, 0x7D, 0xA7, 0x26,
  34006. 0xB2, 0xAE, 0x7A, 0x05, 0x45, 0x3B, 0x40, 0x0A,
  34007. 0x19, 0xE5, 0x32, 0x52, 0x78, 0x9D, 0xC3, 0x20,
  34008. 0x63, 0x24, 0xB2, 0x58, 0x4B, 0x86, 0x1F, 0x00,
  34009. 0xA2, 0x50, 0xF9, 0x9F, 0xD9, 0xDC, 0x7D, 0x51,
  34010. 0x3D, 0xD7, 0xA6, 0x5A, 0x04, 0x03, 0x4E, 0xB3,
  34011. 0x3D, 0x2D, 0x56, 0xA4, 0x96, 0xB3, 0x6A, 0xBA,
  34012. 0x0A, 0x30, 0x08, 0xE3, 0x0F, 0xC1, 0x38, 0x24,
  34013. 0x88, 0x5D, 0x9E, 0x6F, 0x68, 0x1A, 0x7D, 0xB6,
  34014. 0x2D, 0xDD, 0xE3, 0x50, 0x1B, 0xD4, 0x07, 0x75,
  34015. 0xE2, 0xE2, 0xCC, 0x09, 0xCC, 0x8E, 0x4E, 0x67,
  34016. 0x02, 0x72, 0x02, 0xA8, 0x11, 0x70, 0xA5, 0x7F,
  34017. 0x4A, 0xC1, 0x98, 0xC1, 0x7F, 0xBF, 0x95, 0xBB,
  34018. 0xCE, 0xD3, 0x6D, 0x49, 0x30, 0xB9, 0x50, 0x8C,
  34019. 0xFA, 0x3E, 0x8B, 0xF6, 0xE5, 0x54, 0xE9, 0x1B,
  34020. 0xD7, 0xD6, 0xE5, 0x32, 0x33, 0xBB, 0x91, 0xAD,
  34021. 0xC8, 0x15, 0x76, 0x1A, 0x04, 0x35, 0xDE, 0xCC,
  34022. 0xE1, 0x67, 0x26, 0x4C, 0x2F, 0x4E, 0x34, 0x34,
  34023. 0x3D, 0x1E, 0x5A, 0xF7, 0xBC, 0xE6, 0x0C, 0x9B,
  34024. 0x7B, 0x7E, 0xE5, 0xDF, 0x72, 0x9A, 0x0D, 0xDD,
  34025. 0x4B, 0xE6, 0x6F, 0x82, 0xFB, 0x5E, 0x2C, 0xC0,
  34026. 0x7B, 0x03, 0x85, 0x76, 0x11, 0x0E, 0xFD, 0xC7,
  34027. 0xD5, 0x50, 0x26, 0xBE, 0x75, 0x5E, 0xC1, 0xF0,
  34028. 0x2E, 0x47, 0x62, 0xD6, 0xF1, 0xDA, 0xDF, 0xF4,
  34029. 0x1C, 0xEE, 0x63, 0x52, 0xC4, 0x45, 0x37, 0xE6,
  34030. 0x85, 0xA5, 0x0A, 0x07, 0x54, 0x63, 0x21, 0x7B,
  34031. 0x92, 0xF7, 0x33, 0x0C, 0xD9, 0x29, 0xCF, 0xE3,
  34032. 0xAB, 0xB5, 0xFC, 0xAA, 0x26, 0x20, 0x93, 0x55,
  34033. 0x8A, 0x07, 0x33, 0xB2, 0x7D, 0x95, 0x02, 0x7A,
  34034. 0x76, 0x9E, 0x7D, 0xBB, 0xC1, 0xF3, 0x6E, 0x84,
  34035. 0x10, 0x30, 0x4B, 0x5D, 0x59, 0x73, 0x68, 0xEC,
  34036. 0x2A, 0x63, 0x2D, 0x46, 0xE8, 0xC2, 0xF8, 0xEA,
  34037. 0x2B, 0xC4, 0x4F, 0xA7, 0x6E, 0xF4, 0x74, 0xEB,
  34038. 0x96, 0xA3, 0x64, 0x40, 0x9B, 0x23, 0x63, 0x42,
  34039. 0x4B, 0x8F, 0x85, 0x00, 0x43, 0x04, 0xAD, 0x61,
  34040. 0x76, 0x93, 0xBD, 0xC3, 0x88, 0xC3, 0xFC, 0x29,
  34041. 0x61, 0xBD, 0xB1, 0x5A, 0x1F, 0x5B, 0x20, 0xEF,
  34042. 0x95, 0xED, 0x99, 0x84, 0x96, 0xB2, 0x93, 0x81,
  34043. 0x82, 0xFF, 0xE3, 0xB9, 0x27, 0xEA, 0x9A, 0x23,
  34044. 0xF6, 0x42, 0x8D, 0xD3, 0x5C, 0x86, 0x11, 0xC8,
  34045. 0x39, 0xE3, 0x16, 0xE9, 0xA5, 0x32, 0x7C, 0xC9,
  34046. 0xEA, 0x82, 0x50, 0x9B, 0x21, 0x5C, 0xC9, 0x66,
  34047. 0xBE, 0x1C, 0x78, 0x48, 0xEF, 0x39, 0x2D, 0xA1,
  34048. 0xC6, 0xF3, 0x69, 0xA3, 0x36, 0x25, 0x3A, 0xA1,
  34049. 0x15, 0x2B, 0x6D, 0xCF, 0xDA, 0xA7, 0xCA, 0xDD,
  34050. 0x4D, 0x9A, 0x1D, 0x58, 0x9F, 0x73, 0xD3, 0xEF,
  34051. 0x0F, 0xBF, 0x03, 0x88, 0x2F, 0xDE, 0xB9, 0x44,
  34052. 0xB5, 0xB6, 0xCF, 0xE2, 0x6F, 0x6A, 0xB5, 0x12,
  34053. 0x38, 0x29, 0x55, 0x8C, 0x4C, 0x73, 0x6F, 0x0B,
  34054. 0x68, 0x7A, 0xC7, 0x06, 0x83, 0x80, 0xFE, 0x7F,
  34055. 0x61, 0xBE, 0x6B, 0x40, 0xE3, 0xF0, 0x4D, 0x7B,
  34056. 0x36, 0x82, 0x0F, 0xD8, 0x63, 0x29, 0xB3, 0x10,
  34057. 0x9D, 0x02, 0xEC, 0x63, 0x90, 0xEA, 0xFC, 0x8C,
  34058. 0xA7, 0x30, 0x56, 0x2B, 0x68, 0x08, 0x24, 0x24,
  34059. 0xFD, 0xA9, 0x8D, 0x0B, 0x64, 0xBC, 0x97, 0x34,
  34060. 0xB4, 0x0B, 0x63, 0xF7, 0xE3, 0x7A, 0xF6, 0x89,
  34061. 0x0A, 0xF7, 0xC2, 0xD9, 0x2F, 0x79, 0xEE, 0xA3,
  34062. 0xCC, 0xEA, 0xC6, 0x0A, 0x6F, 0x38, 0x06, 0x92,
  34063. 0xF8, 0x02, 0xB1, 0x55, 0x6A, 0x78, 0xFE, 0x55,
  34064. 0x83, 0xFF, 0x20, 0xA9, 0xC6, 0xA7, 0xBF, 0xCC,
  34065. 0x86, 0x3A, 0x9E, 0x7B, 0x62, 0x01, 0x4D, 0x16,
  34066. 0x05, 0xDE, 0x89, 0x4F, 0xB5, 0x85, 0xE2, 0xD4,
  34067. 0xF9, 0x41, 0x15, 0xE0, 0x29, 0xE5, 0x85, 0x7E,
  34068. 0x6A, 0x0A, 0x73, 0x89, 0x27, 0x5F, 0x53, 0x0D,
  34069. 0x3D, 0x80, 0xCF, 0xAB, 0x1F, 0x22, 0x5D, 0x38,
  34070. 0x33, 0x5D, 0x24, 0x67, 0x91, 0x97, 0xD4, 0x8A,
  34071. 0x01, 0x8A, 0x34, 0x18, 0x7D, 0xE3, 0xBC, 0xCE,
  34072. 0xDE, 0x94, 0xFF, 0x8E, 0xC5, 0x34, 0xC0, 0x2D,
  34073. 0xA7, 0x24, 0xD4, 0x59, 0x8D, 0x66, 0x9E, 0x85,
  34074. 0xA9, 0xC6, 0x0E, 0x45, 0x21, 0x4F, 0xAA, 0x65,
  34075. 0x44, 0xD6, 0xA4, 0x7D, 0x1C, 0x4E, 0xD7, 0x40,
  34076. 0x9D, 0x55, 0xB1, 0xA7, 0xF1, 0x15, 0xAE, 0x15,
  34077. 0x44, 0x3A, 0x1C, 0x31, 0x06, 0x40, 0xD1, 0x16,
  34078. 0x23, 0x84, 0x93, 0xEF, 0x3E, 0xE2, 0x87, 0x9B,
  34079. 0xB8, 0x46, 0x1F, 0x7D, 0x68, 0x73, 0x64, 0x70,
  34080. 0xD4, 0xB5, 0x73, 0xAE, 0x45, 0x49, 0x93, 0xF5,
  34081. 0x32, 0x30, 0x1E, 0x35, 0xCB, 0x9E, 0xEE, 0xDF,
  34082. 0xFE, 0xA8, 0x2F, 0xAC, 0x49, 0x77, 0x53, 0xF7,
  34083. 0x50, 0x19, 0xF2, 0xB3, 0xB0, 0x2C, 0x70, 0xB6,
  34084. 0x4A, 0x57, 0x95, 0x31, 0xC3, 0x26, 0x07, 0x2A,
  34085. 0xCF, 0x1B, 0xD0, 0xAA, 0xA0, 0x9F, 0x0A, 0x97,
  34086. 0x8B, 0x78, 0xAB, 0x22, 0xBD, 0x61, 0x19, 0xF8,
  34087. 0x8D, 0xD2, 0xD5, 0x72, 0xF8, 0x91, 0x9D, 0x47,
  34088. 0x4F, 0x59, 0x1D, 0xAE, 0x9F, 0xCE, 0x47, 0x53,
  34089. 0xC9, 0x85, 0xFB, 0x25, 0x04, 0x25, 0xF2, 0x65,
  34090. 0x61, 0xFF, 0xA9, 0x44, 0x3F, 0x23, 0x76, 0x68,
  34091. 0x9F, 0xEB, 0x48, 0xC4, 0xCE, 0x51, 0x46, 0x04,
  34092. 0x52, 0x6A, 0x10, 0x0A, 0xF3, 0x3F, 0x0D, 0x43,
  34093. 0x37, 0xD1, 0x60, 0x42, 0x22, 0xC4, 0xD9, 0xF9,
  34094. 0x3A, 0x8E, 0x69, 0xE4, 0xCC, 0xD3, 0x66, 0x69,
  34095. 0x09, 0x0C, 0x5D, 0xFB, 0x0E, 0x95, 0x49, 0x42,
  34096. 0x29, 0xFF, 0x9B, 0x20, 0xCC, 0xB1, 0xAC, 0x81,
  34097. 0xB8, 0x1A, 0x36, 0xD6, 0x3A, 0x85, 0x0D, 0xDB,
  34098. 0x33, 0x33, 0x4D, 0xAA, 0x51, 0x46, 0xBF, 0x36,
  34099. 0xFE, 0x18, 0x80, 0x1E, 0x3B, 0xEB, 0xD0, 0xE9,
  34100. 0x1B, 0x5E, 0x1C, 0xFE, 0x7A, 0x98, 0x26, 0x85,
  34101. 0x0A, 0xF4, 0x39, 0x7D, 0x1B, 0x07, 0xD3, 0xB7,
  34102. 0x19, 0xE5, 0x7B, 0xB8, 0x32, 0xAF, 0x42, 0x34,
  34103. 0xC0, 0xCD, 0x9F, 0xD4, 0x0B, 0x88, 0x2F, 0xCE,
  34104. 0xDA, 0x93, 0x7E, 0xF9, 0xA2, 0xDA, 0x24, 0x59,
  34105. 0x2B, 0xCB, 0x5D, 0x1B, 0xE8, 0x3E, 0xC5, 0xF0,
  34106. 0x3D, 0xBD, 0xFB, 0xCB, 0x33, 0x5D, 0x90, 0xD5,
  34107. 0xC8, 0xA0, 0x2E, 0xE5, 0x3D, 0x50, 0x8E, 0xB5,
  34108. 0xDE, 0x4A, 0x96, 0x1B, 0x95, 0x8F, 0x75, 0x1E,
  34109. 0x5F, 0x89, 0xA1, 0xD2, 0x88, 0x95, 0xA3, 0xDB,
  34110. 0x7B, 0x62, 0xEF, 0x4A, 0xE1, 0x6D, 0x28, 0xFB,
  34111. 0x78, 0x9B, 0x32, 0x03, 0xAD, 0x24, 0x63, 0xD6,
  34112. 0xEA, 0xB8, 0x3A, 0x6D, 0x20, 0xCE, 0xA1, 0x31,
  34113. 0x4A, 0xE0, 0x2A, 0x3F, 0xF6, 0xF6, 0x53, 0x15,
  34114. 0x4A, 0xE1, 0x44, 0x23, 0x81, 0x86, 0x21, 0x47,
  34115. 0x41, 0xC2, 0x36, 0x14, 0x81, 0x83, 0xBC, 0x39,
  34116. 0xAE, 0xDF, 0x44, 0xDA, 0x97, 0xF7, 0x31, 0xCE,
  34117. 0x3D, 0xCB, 0x61, 0xA4, 0xCF, 0xE1, 0x4F, 0x9E,
  34118. 0x84, 0xAA, 0x05, 0xAB, 0x1C, 0x1B, 0x95, 0x1D,
  34119. 0x20, 0x15, 0x52, 0x33, 0xFA, 0xFA, 0xF1, 0x6C,
  34120. 0xF1, 0xBD, 0x0B, 0xAF, 0xE1, 0x99, 0xE6, 0x5D,
  34121. 0x56, 0x34, 0x53, 0xBF, 0xE5, 0x5D, 0x5F, 0x47,
  34122. 0x4A, 0xB1, 0x05, 0x94, 0xD7, 0x38, 0xA8, 0xC1,
  34123. 0x06, 0x28, 0x8D, 0x69, 0xD0, 0x7A, 0x16, 0x88,
  34124. 0x60, 0x14, 0x63, 0xF3, 0xBD, 0x21, 0x46, 0x81,
  34125. 0x9C, 0x83, 0x72, 0x6D, 0x14, 0xC6, 0xA8, 0x08,
  34126. 0x39, 0xB8, 0x79, 0x0B, 0x57, 0x16, 0xE7, 0x72,
  34127. 0xF6, 0xC2, 0x4C, 0x2B, 0xEB, 0x7E, 0x2C, 0xF3,
  34128. 0x7B, 0x3F, 0x42, 0xAC, 0xDD, 0x47, 0x3E, 0x8C,
  34129. 0xCD, 0xBE, 0x48, 0x4D, 0x6E, 0x07, 0xB0, 0x73,
  34130. 0xDE, 0xCB, 0x17, 0x4A, 0xC3, 0xB8, 0xBB, 0x2E,
  34131. 0xF5, 0x4E, 0x6D, 0xF9, 0xE0, 0x20, 0x71, 0xFA,
  34132. 0x60, 0x0A, 0xE5, 0x59, 0x67, 0xEB, 0x6F, 0x70,
  34133. 0x2F, 0x71, 0x91, 0x59, 0xF0, 0xEB, 0x06, 0x5C,
  34134. 0xC4, 0x60, 0x48, 0xE8, 0x75, 0xE7, 0xCF, 0x42,
  34135. 0x71, 0xAD, 0x2E, 0xDA, 0xF9, 0x10, 0x82, 0x9A,
  34136. 0xF6, 0x13, 0xBA, 0x89, 0xFC, 0x61, 0x2A, 0x00,
  34137. 0xFD, 0xAE, 0x53, 0x7B, 0x09, 0x3A, 0xE8, 0xCB,
  34138. 0xE6, 0xB7, 0x0D, 0x03, 0x01, 0xFA, 0x2E, 0x13,
  34139. 0xA9, 0x16, 0x38, 0x1C, 0x92, 0xEC, 0xB4, 0x51,
  34140. 0xA3, 0x6E, 0x3F, 0xA8, 0xB7, 0x37, 0x36, 0x20,
  34141. 0xC0, 0x71, 0xA3, 0x05, 0x34, 0xED, 0xCB, 0x4A,
  34142. 0x3F, 0x11, 0x31, 0x17, 0xA5, 0x02, 0xD6, 0xA7,
  34143. 0x2D, 0xE6, 0xC7, 0x7B, 0xBB, 0xF6, 0xAE, 0x99,
  34144. 0x85, 0x9A, 0xAC, 0xE6, 0x4A, 0x92, 0x8C, 0x37,
  34145. 0x4B, 0xD2, 0xC4, 0x65, 0x2A, 0xC9, 0x7E, 0xB7,
  34146. 0x44, 0xD2, 0x9A, 0x70, 0xCE, 0xA9, 0xA1, 0x9D,
  34147. 0x70, 0x13, 0x49, 0x7B, 0xCA, 0xB6, 0x96, 0x31,
  34148. 0x43, 0x3F, 0x9E, 0xD1, 0xFE, 0x20, 0xF8, 0x0B,
  34149. 0x59, 0x83, 0xE1, 0x28, 0x8B, 0xB6, 0xA2, 0xBE,
  34150. 0x91, 0x54, 0x3E, 0xD4, 0x79, 0x28, 0xBB, 0x5E,
  34151. 0x46, 0x2D, 0x01, 0xE9, 0xC0, 0xB7, 0xFF, 0xFA,
  34152. 0xC0, 0x6C, 0x10, 0xF1, 0x52, 0xF4, 0x3C, 0x32,
  34153. 0x9E, 0x89, 0xDF, 0x8A, 0x79, 0x99, 0x6A, 0x09,
  34154. 0x79, 0x8A, 0x36, 0x76, 0x40, 0xBE, 0x9F, 0xB5,
  34155. 0x3D, 0xCE, 0x27, 0xBD, 0x0B, 0xAA, 0x9B, 0xF0,
  34156. 0x21, 0xBF, 0x10, 0xD2, 0xFC, 0xFE, 0x5B, 0x13,
  34157. 0xFD, 0x7D, 0x84, 0xD1, 0xC1, 0xEB, 0xC0, 0xBC,
  34158. 0xEC, 0x26, 0xD0, 0x87, 0x80, 0xD1, 0x3B, 0x99,
  34159. 0x47, 0x67, 0x26, 0x61, 0xE0, 0xFA, 0x5F, 0xAE,
  34160. 0x6F, 0x31, 0x5B, 0x6D, 0xE4, 0x01, 0x68, 0xC2,
  34161. 0x35, 0x1D, 0xE3, 0x1F, 0x41, 0xFF, 0x6C, 0x53,
  34162. 0x32, 0x26, 0xE1, 0xBC, 0xE3, 0xF8, 0xE2, 0x16,
  34163. 0xAF, 0x3B, 0xE6, 0x4C, 0x69, 0x33, 0x72, 0xA0,
  34164. 0x66, 0xB1, 0x75, 0xF7, 0x26, 0xCF, 0xCD, 0x64,
  34165. 0x2B, 0xAE, 0x98, 0x02, 0x92, 0xC1, 0xCB, 0x65,
  34166. 0xE0, 0x1F, 0x07, 0x29, 0x64, 0x0A, 0xB0, 0x09,
  34167. 0xCB, 0x98, 0x89, 0x2D, 0x6C, 0xFE, 0x40, 0x03,
  34168. 0x34, 0x55, 0xDE, 0xE7, 0x30, 0x33, 0xB6, 0xD5,
  34169. 0xE1, 0x9C, 0x59, 0x9F, 0x8A, 0x40, 0x0E, 0xB1,
  34170. 0x41, 0x52, 0x7D, 0xF2, 0xBB, 0xDD, 0xEF, 0x50,
  34171. 0xBB, 0xD5, 0xFB, 0x55, 0xAA, 0x5E, 0xFD, 0xB3,
  34172. 0x5D, 0x08, 0x56, 0x9B, 0x02, 0x97, 0xE2, 0x48,
  34173. 0x14, 0x69, 0xF1, 0x7B, 0x87, 0xB5, 0x08, 0x93,
  34174. 0x6A, 0x9C, 0x5C, 0x11, 0x08, 0x9A, 0xE9, 0xE4,
  34175. 0xB0, 0xCA, 0xC5, 0x74, 0x93, 0x93, 0xC8, 0x03,
  34176. 0xE4, 0x70, 0x39, 0xF5, 0x1B, 0x5C, 0xBD, 0x42,
  34177. 0xA6, 0xC9, 0xE1, 0x9E, 0xC3, 0xF6, 0x3C, 0x23,
  34178. 0x32, 0xE8, 0x77, 0x68, 0xA9, 0x60, 0xFA, 0x02,
  34179. 0x18, 0x6B, 0x7A, 0x2B, 0x02, 0x92, 0x65, 0x09,
  34180. 0x11, 0x46, 0x73, 0x04, 0x63, 0xDF, 0x8B, 0x37,
  34181. 0x5F, 0x24, 0xAA, 0x83, 0xBD, 0xD4, 0x1D, 0x13,
  34182. 0x04, 0xFC, 0x2F, 0xB5, 0x2D, 0xA1, 0x0F, 0x1F,
  34183. 0xED, 0x65, 0x29, 0x08, 0xCF, 0x8C, 0x52, 0x8F,
  34184. 0xB2, 0x62, 0x5F, 0x39, 0x3F, 0xC8, 0xC7, 0xB3,
  34185. 0x3F, 0xAD, 0x45, 0xBA, 0xD4, 0x7D, 0x38, 0x3D,
  34186. 0x2C, 0x04, 0xCF, 0x32, 0xE8, 0x07, 0x42, 0x5F,
  34187. 0x93, 0xD2, 0x35, 0x07, 0x21, 0xB7, 0xB2, 0xF5,
  34188. 0x96, 0x64, 0x8E, 0xB5, 0xE1, 0x38, 0x6B, 0x43,
  34189. 0xD1, 0x2E, 0xFD, 0xDB, 0x8F, 0xE2, 0x43, 0x6A,
  34190. 0xEC, 0x27, 0x8E, 0xE7, 0x68, 0x75, 0xB5, 0x23,
  34191. 0xC5, 0x43, 0x1D, 0x99, 0x48, 0x57, 0x73, 0xD9,
  34192. 0xAD, 0xBC, 0xD0, 0x14, 0xDD, 0x87, 0xBC, 0x68,
  34193. 0xFB, 0x82, 0xEE, 0x47, 0x4B, 0x22, 0xA5, 0x43,
  34194. 0x3A, 0xF9, 0xF9, 0x91, 0xFC, 0x34, 0xB2, 0x58,
  34195. 0x34, 0xDF, 0x13, 0x09, 0x9A, 0x46, 0xF5, 0x68,
  34196. 0xAF, 0xD1, 0x15, 0x5F, 0x32, 0x1B, 0x9D, 0xA9,
  34197. 0xE9, 0xC0, 0x63, 0x47, 0xAB, 0x3C, 0x1F, 0x59,
  34198. 0xF7, 0xEA, 0x0E, 0xD6, 0xCF, 0x47, 0xB3, 0xE9,
  34199. 0xAF, 0x65, 0x7A, 0xA7, 0xAE, 0x9B, 0xF8, 0x26,
  34200. 0x0B, 0x96, 0x9D, 0xE4, 0xAD, 0x24, 0xD3, 0xA8,
  34201. 0xCE, 0x95, 0xE5, 0x77, 0xD0, 0x44, 0x13, 0x05,
  34202. 0x06, 0x4E, 0x07, 0xB9, 0xA2, 0xC7, 0x5C, 0x3C,
  34203. 0x43, 0x80, 0x1F, 0xCE, 0xB7, 0x36, 0xFE, 0x3D,
  34204. 0x27, 0x1B, 0xE1, 0xF3, 0x6B, 0xFF, 0xC8, 0xE4,
  34205. 0x3D, 0xB1, 0x4A, 0x16, 0x24, 0x76, 0xBA, 0xEA,
  34206. 0x9D, 0x34, 0x6B, 0x52, 0x11, 0xAB, 0xD0, 0x06,
  34207. 0x08, 0xB1, 0x5A, 0xF3, 0xB5, 0xE6, 0x3A, 0x00,
  34208. 0xFF, 0x92, 0x8D, 0x1E, 0xA1, 0xA1, 0x8D, 0x75,
  34209. 0xFA, 0x7C, 0x6C, 0x1B, 0x0F, 0xB6, 0x27, 0x2E,
  34210. 0x55, 0xC3, 0xFE, 0x7E, 0x4D, 0x42, 0x05, 0xE5,
  34211. 0xCF, 0x0A, 0x1F, 0x87, 0x18, 0x30, 0x4E, 0x14,
  34212. 0xF2, 0xB4, 0xCC, 0x54, 0x3D, 0x04, 0x37, 0x34,
  34213. 0x1A, 0x4A, 0x31, 0x16, 0x01, 0xA9, 0x2E, 0x92,
  34214. 0x56, 0x6B, 0x7D, 0xFB, 0x42, 0x64, 0xE8, 0x70,
  34215. 0xE1, 0xB3, 0xA8, 0x75, 0xED, 0xBC, 0x00, 0x3A,
  34216. 0x56, 0x19, 0x70, 0xCF, 0x8A, 0x66, 0x9F, 0x3D,
  34217. 0x1B, 0x69, 0x28, 0x8C, 0xC6, 0xE3, 0x59, 0xCE,
  34218. 0x28, 0xCA, 0x65, 0xF9, 0xDA, 0xE8, 0xCE, 0xCA,
  34219. 0x74, 0x3C, 0x1C, 0x8D, 0x9F, 0xFB, 0x55, 0x08,
  34220. 0x82, 0x4A, 0x83, 0x61, 0xE3, 0x3B, 0x43, 0x1A,
  34221. 0x2E, 0x9E, 0x9A, 0x99, 0x78, 0x47, 0xD2, 0xE6,
  34222. 0xE4, 0x3C, 0x83, 0xF0, 0x22, 0x62, 0xE2, 0x94,
  34223. 0x6D, 0xF7, 0x72, 0x6D, 0x54, 0xE3, 0xE6, 0xC9,
  34224. 0xCC, 0xDB, 0x6D, 0x3F, 0x13, 0x63, 0x46, 0xC1,
  34225. 0x1E, 0x59, 0x42, 0xE7, 0xA1, 0xBF, 0x85, 0x0C,
  34226. 0x2E, 0x99, 0xB4, 0xFA, 0xCE, 0x75, 0xFD, 0x40,
  34227. 0x88, 0x69, 0x33, 0x90, 0x7C, 0xCD, 0xFC, 0x0D,
  34228. 0xE1, 0x17, 0x70, 0x20, 0x31, 0x94, 0x1D, 0x00,
  34229. 0x1E, 0x2A, 0x68, 0x3C, 0x55, 0x78, 0xFD, 0x33,
  34230. 0x54, 0x21, 0x2C, 0xEA, 0xD9, 0x69, 0xBF, 0x1C,
  34231. 0x81, 0x23, 0x9E, 0xEC, 0xC7, 0x74, 0xFD, 0x0B,
  34232. 0x88, 0x3D, 0x0E, 0xEE, 0x82, 0x4B, 0x10, 0xB8,
  34233. 0x79, 0xCF, 0x70, 0x7C, 0xB2, 0x68, 0x47, 0x45,
  34234. 0x22, 0x06, 0x1E, 0x92, 0x7B, 0x12, 0x43, 0x24,
  34235. 0x41, 0x15, 0xC6, 0x69, 0xE9, 0xEB, 0x27, 0x2B,
  34236. 0x60, 0xA6, 0x44, 0xF5, 0x19, 0xEF, 0xEC, 0x06,
  34237. 0x34, 0x08, 0xB6, 0x58, 0x47, 0x2E, 0x91, 0x61,
  34238. 0xA1, 0xF7, 0x44, 0xFD, 0x66, 0x16, 0x9F, 0x0C,
  34239. 0xAE, 0x36, 0xB4, 0x2E, 0x23, 0x79, 0xCB, 0xE8,
  34240. 0x1E, 0x6E, 0x51, 0xA0, 0xF5, 0x34, 0x15, 0x18,
  34241. 0x4E, 0xA0, 0x06, 0xB2, 0x27, 0x0B, 0x33, 0xE2,
  34242. 0xCA, 0x36, 0x4C, 0xDB, 0x33, 0xAA, 0xAE, 0x77,
  34243. 0xFF, 0xD9, 0x53, 0xDB, 0x39, 0x70, 0x4D, 0x49,
  34244. 0x0C, 0xE9, 0xAC, 0x6F, 0x2D, 0xD1, 0xC7, 0xA1,
  34245. 0x8E, 0x61, 0x74, 0x19, 0xA9, 0xAA, 0xFB, 0x37,
  34246. 0xE7, 0x23, 0x9B, 0x23, 0x6A, 0x4B, 0x74, 0xCE,
  34247. 0x63, 0xE4, 0xA0, 0xAD, 0xFF, 0x85, 0x5D, 0xCD,
  34248. 0x78, 0xF6, 0x45, 0x8E, 0x76, 0x0B, 0xFD, 0x1D,
  34249. 0x2A, 0xB9, 0x5E, 0x83, 0xC0, 0x3B, 0x6F, 0xAE,
  34250. 0x0C, 0xD3, 0xC5, 0xCE, 0xEE, 0xEE, 0x1C, 0x69,
  34251. 0x51, 0x59, 0x65, 0xA3, 0x35, 0xFC, 0xF7, 0x8E,
  34252. 0x80, 0xAA, 0x73, 0x93, 0x39, 0x54, 0x21, 0x27,
  34253. 0x17, 0x0B, 0x2C, 0x3E, 0xE1, 0x0B, 0x0E, 0xAA,
  34254. 0x09, 0x9A, 0xC7, 0xAD, 0x4C, 0xD7, 0x6E, 0x7F,
  34255. 0xE4, 0xC1, 0x16, 0x4E, 0x62, 0xF4, 0xE5, 0x80,
  34256. 0x7D, 0xC0, 0x06, 0x1F, 0x77, 0xE4, 0xA8, 0xA5,
  34257. 0x28, 0xD7, 0x10, 0x37, 0x59, 0x30, 0xCB, 0x75,
  34258. 0x5B, 0x28, 0xBF, 0xFD, 0x92, 0x8C, 0xB0, 0x7B,
  34259. 0xB4, 0xA1, 0x07, 0xCD, 0xCA, 0xBB, 0x30, 0x8A,
  34260. 0x48, 0x65, 0x0D, 0xA4, 0xE5, 0x74, 0xD9, 0xBF,
  34261. 0x56, 0x07, 0xF5, 0x83, 0xDA, 0xC3, 0x40, 0xD7,
  34262. 0x20, 0x93, 0xEF, 0xB1, 0x2B, 0xBF, 0x93, 0x41,
  34263. 0x0F, 0x1E, 0xF5, 0xC9, 0x51, 0x6C, 0x74, 0x4D,
  34264. 0x23, 0x15, 0xEC, 0x9E, 0x00, 0x0A, 0x8D, 0xC5,
  34265. 0xD1, 0x7A, 0x7B, 0x6F, 0x0D, 0x07, 0x9D, 0x78,
  34266. 0x4B, 0x6D, 0x90, 0x19, 0x3F, 0x6E, 0x3E, 0xE7,
  34267. 0xEA, 0x0E, 0xAB, 0xFC, 0x6F, 0x68, 0xC5, 0x2B,
  34268. 0x37, 0xCB, 0xCE, 0x82, 0x18, 0xAF, 0xA3, 0x67,
  34269. 0x0A, 0x80, 0xBC, 0x17, 0xB9, 0x5D, 0x7B, 0x40,
  34270. 0x53, 0x62, 0x26, 0x35, 0x8F, 0x04, 0xAC, 0xD9,
  34271. 0x2A, 0x1B, 0xE1, 0x5B, 0x26, 0xA4, 0xE5, 0x81,
  34272. 0x7E, 0x62, 0x8B, 0xA6, 0x79, 0xB3, 0x52, 0x72,
  34273. 0x03, 0xCD, 0x36, 0x32, 0x62, 0x8E, 0xC8, 0x3A,
  34274. 0xA4, 0xF2, 0x18, 0x6D, 0x2F, 0x00, 0x5D, 0x5D,
  34275. 0xFE, 0x6F, 0x7F, 0xDB, 0x4F, 0xED, 0xAC, 0x9E,
  34276. 0x89, 0xD6, 0x66, 0xE3, 0x03, 0xBB, 0x56, 0x83,
  34277. 0x06, 0x15, 0x6C, 0x56, 0xF0, 0x95, 0x34, 0xE2,
  34278. 0x5C, 0x61, 0x9A, 0xB3, 0xB9, 0x50, 0x18, 0xF4,
  34279. 0x89, 0x6B, 0xAC, 0xAA, 0x48, 0x34, 0xF6, 0xD2,
  34280. 0xD8, 0xFE, 0x14, 0xA9, 0x38, 0xAA, 0x10, 0xE5,
  34281. 0x30, 0x54, 0xF0, 0x00, 0x84, 0x44, 0xAC, 0x2E,
  34282. 0xEA, 0x25, 0x38, 0xC1, 0x23, 0x0E, 0x6A, 0x18,
  34283. 0xC9, 0x2B, 0x01, 0xD9, 0x14, 0x7F, 0xDC, 0xEF,
  34284. 0xC9, 0xC8, 0xDA, 0xC1, 0xD4, 0xEC, 0xC8, 0xCF,
  34285. 0x1F, 0x96, 0x2E, 0xFA, 0x1B, 0x8C, 0xD3, 0xC9,
  34286. 0x69, 0x00, 0x0B, 0x7E, 0xBA, 0xC5, 0x98, 0xDC,
  34287. 0xA4, 0x5E, 0xB4, 0x0B, 0xCF, 0xB1, 0x98, 0x51,
  34288. 0x48, 0x38, 0x51, 0xCF, 0x34, 0x0F, 0x3E, 0x8C,
  34289. 0x23, 0x7A, 0x9E, 0xFF, 0x1C, 0x9F, 0x21, 0xE4,
  34290. 0x97, 0x55, 0x41, 0xC6, 0x1A, 0x8F, 0xEF, 0x2A,
  34291. 0xC6, 0x05, 0x7F, 0x59, 0xDC, 0xB2, 0x3A, 0x80,
  34292. 0xE8, 0x06, 0x10, 0xCD, 0x85, 0xDB, 0x20, 0x3C,
  34293. 0x35, 0xD2, 0x4B, 0xC8, 0x2B, 0x9C, 0xD7, 0x82,
  34294. 0x46, 0xF5, 0x9F, 0xEB, 0xB2, 0x48, 0x32, 0xD7,
  34295. 0xCD, 0x66, 0x4C, 0x99, 0x51, 0x88, 0xE0, 0x28,
  34296. 0x1C, 0xD7, 0x86, 0x79, 0x00, 0xDC, 0x0D, 0xF4,
  34297. 0x4D, 0x40, 0x90, 0x80, 0x26, 0x8B, 0x79, 0xE9,
  34298. 0x56, 0x82, 0x88, 0x5F, 0x22, 0x87, 0x70, 0x73,
  34299. 0x4F, 0xA5, 0x35, 0x18, 0xEC, 0x80, 0xCE, 0x23,
  34300. 0x06, 0xCE, 0x14, 0x48, 0x52, 0x4E, 0xF0, 0x18,
  34301. 0x43, 0x03, 0xD4, 0x50, 0xC7, 0x6E, 0xA6, 0x3B,
  34302. 0x73, 0x3E, 0xB0, 0xC8, 0xDC, 0x48, 0xBF, 0x12,
  34303. 0x42, 0x3A, 0xD2, 0x38, 0x89, 0xCF, 0xCD, 0xD8,
  34304. 0x91, 0xE5, 0x95, 0x00, 0x47, 0x24, 0x0D, 0xC0,
  34305. 0xC3, 0x8A, 0xB2, 0xDB, 0xC1, 0x65, 0xB8, 0x1E,
  34306. 0x63, 0x10, 0x02, 0xEA, 0x6F, 0x74, 0x11, 0x9E,
  34307. 0x27, 0xF9, 0xF8, 0x60, 0x73, 0xBF, 0x2D, 0xF7,
  34308. 0x10, 0x81, 0x86, 0x76, 0x98, 0x0C, 0x4C, 0xB6,
  34309. 0xBD, 0x53, 0xF9, 0xA5, 0x72, 0x17, 0x78, 0xB8,
  34310. 0x9F, 0x59, 0xC6, 0x8C, 0x89, 0x35, 0xF5, 0x03,
  34311. 0x1C, 0x8A, 0x93, 0x36, 0x7D, 0x71, 0x70, 0x57,
  34312. 0xFD, 0x4D, 0x5E, 0xFA, 0xBE, 0xDE, 0x70, 0x2C,
  34313. 0xC6, 0x45, 0xEF, 0xB6, 0xD7, 0xF4, 0x4C, 0x86,
  34314. 0x0F, 0xFF, 0x76, 0x37, 0xAA, 0xD9, 0x72, 0x24,
  34315. 0x8C, 0x84, 0x4D, 0x15, 0x13, 0x39, 0x20, 0x07,
  34316. 0x38, 0x91, 0xC3, 0x13, 0x5D, 0x29, 0x78, 0x68,
  34317. 0xB7, 0xDA, 0x86, 0xF0, 0x97, 0xD8, 0xFB, 0x39,
  34318. 0xC1, 0x3B, 0xA1, 0x4C, 0x4F, 0x24, 0x75, 0x16,
  34319. 0xAB, 0xA4, 0xC5, 0xF8, 0xCE, 0x38, 0x18, 0x48,
  34320. 0x2C, 0x8F, 0xF6, 0x0C, 0xCA, 0x51, 0xFD, 0xB2,
  34321. 0xCE, 0xE9, 0x6B, 0xC1, 0x13, 0x8D, 0xC0, 0x4A,
  34322. 0x86, 0xF8, 0x57, 0x72, 0x75, 0x91, 0xAA, 0xE6,
  34323. 0xF8, 0x7C, 0x30, 0x05, 0x9B, 0x3E, 0x81, 0xB6,
  34324. 0x80, 0x55, 0xB2, 0x4E, 0xA2, 0xFA, 0x98, 0x36,
  34325. 0x86, 0x49, 0x8B, 0xFC, 0x9D, 0x9E, 0x7D, 0x59,
  34326. 0x50, 0x79, 0xEB, 0x64, 0x6E, 0x85, 0xB2, 0x12,
  34327. 0xCE, 0xDD, 0x21, 0xD0, 0x08, 0x7E, 0x0F, 0x2A,
  34328. 0xF6, 0x63, 0xEB, 0x77, 0x2A, 0x98, 0x47, 0xB1,
  34329. 0xDF, 0x21, 0x97, 0xAF, 0x13, 0x62, 0x6B, 0x89,
  34330. 0x7C, 0x24, 0x63, 0x7A, 0xF5, 0xBF, 0xE8, 0x18,
  34331. 0x16, 0xA8, 0xC9, 0x0D, 0x30, 0x48, 0x37, 0x5B,
  34332. 0x69, 0x94, 0x97, 0x14, 0x3E, 0x57, 0x71, 0x85,
  34333. 0xA7, 0x0E, 0x11, 0x50, 0x58, 0xA3, 0xA9, 0x11,
  34334. 0x2B, 0x2C, 0x43, 0x51, 0xB6, 0xCA, 0xD0, 0x09,
  34335. 0x28, 0x2B, 0x4F, 0x7C, 0xB8, 0xBD, 0xFC, 0x28,
  34336. 0x57, 0x77, 0xD7, 0xDF, 0xE8, 0xF5, 0x00, 0x00,
  34337. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34338. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34339. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34340. 0x00, 0x00, 0x00, 0x03, 0x06, 0x0B, 0x11, 0x17,
  34341. 0x1F, 0x27, 0x2E
  34342. };
  34343. #endif
  34344. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  34345. ExpectNotNull(key);
  34346. if (key != NULL) {
  34347. XMEMSET(key, 0, sizeof(*key));
  34348. }
  34349. ExpectIntEQ(wc_dilithium_init_ex(key, NULL, INVALID_DEVID), 0);
  34350. #ifndef WOLFSSL_NO_ML_DSA_44
  34351. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  34352. ExpectIntEQ(wc_dilithium_import_public(pk_44, (word32)sizeof(pk_44), key),
  34353. 0);
  34354. ExpectIntEQ(wc_dilithium_verify_msg(sig_44, (word32)sizeof(sig_44), msg_44,
  34355. (word32)sizeof(msg_44), &res, key), 0);
  34356. ExpectIntEQ(res, 1);
  34357. #endif
  34358. #ifndef WOLFSSL_NO_ML_DSA_65
  34359. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  34360. ExpectIntEQ(wc_dilithium_import_public(pk_65, (word32)sizeof(pk_65), key),
  34361. 0);
  34362. ExpectIntEQ(wc_dilithium_verify_msg(sig_65, (word32)sizeof(sig_65), msg_65,
  34363. (word32)sizeof(msg_65), &res, key), 0);
  34364. ExpectIntEQ(res, 1);
  34365. #endif
  34366. #ifndef WOLFSSL_NO_ML_DSA_87
  34367. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  34368. ExpectIntEQ(wc_dilithium_import_public(pk_87, (word32)sizeof(pk_87), key),
  34369. 0);
  34370. ExpectIntEQ(wc_dilithium_verify_msg(sig_87, (word32)sizeof(sig_87), msg_87,
  34371. (word32)sizeof(msg_87), &res, key), 0);
  34372. ExpectIntEQ(res, 1);
  34373. #endif
  34374. wc_dilithium_free(key);
  34375. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  34376. #endif
  34377. return EXPECT_RESULT();
  34378. }
  34379. /*
  34380. * Testing wc_SetSubjectBuffer
  34381. */
  34382. static int test_wc_SetSubjectBuffer(void)
  34383. {
  34384. EXPECT_DECLS;
  34385. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  34386. Cert cert;
  34387. XFILE file = XBADFILE;
  34388. byte* der = NULL;
  34389. word32 derSz;
  34390. derSz = FOURK_BUF;
  34391. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34392. DYNAMIC_TYPE_TMP_BUFFER));
  34393. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  34394. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  34395. if (file != XBADFILE)
  34396. XFCLOSE(file);
  34397. ExpectIntEQ(wc_InitCert(&cert), 0);
  34398. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, (int)derSz), 0);
  34399. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, (int)derSz), BAD_FUNC_ARG);
  34400. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34401. #endif
  34402. return EXPECT_RESULT();
  34403. } /* End test_wc_SetSubjectBuffer*/
  34404. /*
  34405. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  34406. */
  34407. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  34408. {
  34409. EXPECT_DECLS;
  34410. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  34411. WC_RNG rng;
  34412. Cert cert;
  34413. #if !defined(NO_RSA) && defined(HAVE_RSA)
  34414. RsaKey rsaKey;
  34415. int bits = 2048;
  34416. #endif
  34417. #if defined(HAVE_ECC)
  34418. ecc_key eccKey;
  34419. int ret;
  34420. #endif
  34421. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  34422. ed25519_key ed25519Key;
  34423. #endif
  34424. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  34425. ed448_key ed448Key;
  34426. #endif
  34427. #ifndef HAVE_FIPS
  34428. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  34429. #else
  34430. ExpectIntEQ(wc_InitRng(&rng), 0);
  34431. #endif
  34432. ExpectIntEQ(wc_InitCert(&cert), 0);
  34433. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  34434. /* RSA */
  34435. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  34436. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  34437. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  34438. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  34439. 0);
  34440. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  34441. #endif
  34442. #if defined(HAVE_ECC)
  34443. /* ECC */
  34444. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  34445. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  34446. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  34447. #if defined(WOLFSSL_ASYNC_CRYPT)
  34448. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  34449. #endif
  34450. ExpectIntEQ(ret, 0);
  34451. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  34452. 0);
  34453. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  34454. #endif
  34455. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  34456. /* ED25519 */
  34457. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  34458. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  34459. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  34460. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  34461. &ed25519Key), 0);
  34462. wc_ed25519_free(&ed25519Key);
  34463. #endif
  34464. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  34465. /* ED448 */
  34466. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  34467. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  34468. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  34469. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  34470. &ed448Key), 0);
  34471. wc_ed448_free(&ed448Key);
  34472. #endif
  34473. wc_FreeRng(&rng);
  34474. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  34475. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  34476. return EXPECT_RESULT();
  34477. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  34478. /*
  34479. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  34480. */
  34481. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  34482. {
  34483. EXPECT_DECLS;
  34484. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  34485. WC_RNG rng;
  34486. Cert cert;
  34487. #if !defined(NO_RSA) && defined(HAVE_RSA)
  34488. RsaKey rsaKey;
  34489. int bits = 2048;
  34490. #endif
  34491. #if defined(HAVE_ECC)
  34492. ecc_key eccKey;
  34493. int ret;
  34494. #endif
  34495. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  34496. ed25519_key ed25519Key;
  34497. #endif
  34498. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  34499. ed448_key ed448Key;
  34500. #endif
  34501. #ifndef HAVE_FIPS
  34502. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  34503. #else
  34504. ExpectIntEQ(wc_InitRng(&rng), 0);
  34505. #endif
  34506. ExpectIntEQ(wc_InitCert(&cert), 0);
  34507. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  34508. /* RSA */
  34509. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  34510. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  34511. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  34512. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  34513. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  34514. #endif
  34515. #if defined(HAVE_ECC)
  34516. /* ECC */
  34517. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  34518. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  34519. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  34520. #if defined(WOLFSSL_ASYNC_CRYPT)
  34521. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  34522. #endif
  34523. ExpectIntEQ(ret, 0);
  34524. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  34525. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  34526. #endif
  34527. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  34528. /* ED25519 */
  34529. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  34530. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  34531. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  34532. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  34533. &ed25519Key), 0);
  34534. wc_ed25519_free(&ed25519Key);
  34535. #endif
  34536. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  34537. /* ED448 */
  34538. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  34539. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  34540. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  34541. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  34542. 0);
  34543. wc_ed448_free(&ed448Key);
  34544. #endif
  34545. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  34546. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  34547. return EXPECT_RESULT();
  34548. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  34549. /*
  34550. * Testing wc_PKCS7_New()
  34551. */
  34552. static int test_wc_PKCS7_New(void)
  34553. {
  34554. EXPECT_DECLS;
  34555. #if defined(HAVE_PKCS7)
  34556. PKCS7* pkcs7 = NULL;
  34557. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  34558. wc_PKCS7_Free(pkcs7);
  34559. #endif
  34560. return EXPECT_RESULT();
  34561. } /* END test-wc_PKCS7_New */
  34562. /*
  34563. * Testing wc_PKCS7_Init()
  34564. */
  34565. static int test_wc_PKCS7_Init(void)
  34566. {
  34567. EXPECT_DECLS;
  34568. #if defined(HAVE_PKCS7)
  34569. PKCS7* pkcs7 = NULL;
  34570. void* heap = NULL;
  34571. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  34572. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  34573. /* Pass in bad args. */
  34574. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  34575. wc_PKCS7_Free(pkcs7);
  34576. #endif
  34577. return EXPECT_RESULT();
  34578. } /* END test-wc_PKCS7_Init */
  34579. /*
  34580. * Testing wc_PKCS7_InitWithCert()
  34581. */
  34582. static int test_wc_PKCS7_InitWithCert(void)
  34583. {
  34584. EXPECT_DECLS;
  34585. #if defined(HAVE_PKCS7)
  34586. PKCS7* pkcs7 = NULL;
  34587. #ifndef NO_RSA
  34588. #if defined(USE_CERT_BUFFERS_2048)
  34589. unsigned char cert[sizeof(client_cert_der_2048)];
  34590. int certSz = (int)sizeof(cert);
  34591. XMEMSET(cert, 0, certSz);
  34592. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  34593. #elif defined(USE_CERT_BUFFERS_1024)
  34594. unsigned char cert[sizeof(client_cert_der_1024)];
  34595. int certSz = (int)sizeof(cert);
  34596. XMEMSET(cert, 0, certSz);
  34597. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  34598. #else
  34599. unsigned char cert[ONEK_BUF];
  34600. XFILE fp = XBADFILE;
  34601. int certSz;
  34602. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  34603. XBADFILE);
  34604. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  34605. fp), 0);
  34606. if (fp != XBADFILE)
  34607. XFCLOSE(fp);
  34608. #endif
  34609. #elif defined(HAVE_ECC)
  34610. #if defined(USE_CERT_BUFFERS_256)
  34611. unsigned char cert[sizeof(cliecc_cert_der_256)];
  34612. int certSz = (int)sizeof(cert);
  34613. XMEMSET(cert, 0, certSz);
  34614. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  34615. #else
  34616. unsigned char cert[ONEK_BUF];
  34617. XFILE fp = XBADFILE;
  34618. int certSz;
  34619. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  34620. XBADFILE);
  34621. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  34622. fp), 0);
  34623. if (fp != XBADFILE)
  34624. XFCLOSE(fp);
  34625. #endif
  34626. #else
  34627. #error PKCS7 requires ECC or RSA
  34628. #endif
  34629. #ifdef HAVE_ECC
  34630. {
  34631. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  34632. static unsigned char certWithInvalidEccKey[] = {
  34633. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  34634. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  34635. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  34636. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  34637. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  34638. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  34639. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  34640. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  34641. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  34642. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  34643. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  34644. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  34645. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  34646. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  34647. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  34648. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  34649. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  34650. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  34651. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  34652. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  34653. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  34654. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  34655. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  34656. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  34657. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  34658. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  34659. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  34660. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  34661. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  34662. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  34663. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  34664. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  34665. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  34666. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  34667. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  34668. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  34669. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  34670. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  34671. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  34672. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  34673. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  34674. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  34675. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  34676. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  34677. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  34678. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  34679. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  34680. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  34681. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  34682. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  34683. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  34684. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  34685. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  34686. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  34687. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  34688. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  34689. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  34690. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  34691. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  34692. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  34693. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  34694. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  34695. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  34696. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  34697. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  34698. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  34699. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  34700. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  34701. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  34702. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  34703. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  34704. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  34705. 0x08, 0xA8, 0xB4
  34706. };
  34707. #endif
  34708. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  34709. /* If initialization is not successful, it's free'd in init func. */
  34710. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  34711. 0);
  34712. wc_PKCS7_Free(pkcs7);
  34713. pkcs7 = NULL;
  34714. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  34715. /* Valid initialization usage. */
  34716. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  34717. /* Pass in bad args. No need free for null checks, free at end.*/
  34718. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  34719. BAD_FUNC_ARG);
  34720. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  34721. BAD_FUNC_ARG);
  34722. #ifdef HAVE_ECC
  34723. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  34724. sizeof(certWithInvalidEccKey)), 0);
  34725. }
  34726. #endif
  34727. wc_PKCS7_Free(pkcs7);
  34728. #endif
  34729. return EXPECT_RESULT();
  34730. } /* END test_wc_PKCS7_InitWithCert */
  34731. /*
  34732. * Testing wc_PKCS7_EncodeData()
  34733. */
  34734. static int test_wc_PKCS7_EncodeData(void)
  34735. {
  34736. EXPECT_DECLS;
  34737. #if defined(HAVE_PKCS7)
  34738. PKCS7* pkcs7 = NULL;
  34739. byte output[FOURK_BUF];
  34740. byte data[] = "My encoded DER cert.";
  34741. #ifndef NO_RSA
  34742. #if defined(USE_CERT_BUFFERS_2048)
  34743. unsigned char cert[sizeof(client_cert_der_2048)];
  34744. unsigned char key[sizeof(client_key_der_2048)];
  34745. int certSz = (int)sizeof(cert);
  34746. int keySz = (int)sizeof(key);
  34747. XMEMSET(cert, 0, certSz);
  34748. XMEMSET(key, 0, keySz);
  34749. XMEMCPY(cert, client_cert_der_2048, certSz);
  34750. XMEMCPY(key, client_key_der_2048, keySz);
  34751. #elif defined(USE_CERT_BUFFERS_1024)
  34752. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  34753. unsigned char key[sizeof_client_key_der_1024];
  34754. int certSz = (int)sizeof(cert);
  34755. int keySz = (int)sizeof(key);
  34756. XMEMSET(cert, 0, certSz);
  34757. XMEMSET(key, 0, keySz);
  34758. XMEMCPY(cert, client_cert_der_1024, certSz);
  34759. XMEMCPY(key, client_key_der_1024, keySz);
  34760. #else
  34761. unsigned char cert[ONEK_BUF];
  34762. unsigned char key[ONEK_BUF];
  34763. XFILE fp = XBADFILE;
  34764. int certSz;
  34765. int keySz;
  34766. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  34767. XBADFILE);
  34768. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  34769. fp), 0);
  34770. if (fp != XBADFILE) {
  34771. XFCLOSE(fp);
  34772. fp = XBADFILE;
  34773. }
  34774. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  34775. XBADFILE);
  34776. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  34777. 0);
  34778. if (fp != XBADFILE)
  34779. XFCLOSE(fp);
  34780. #endif
  34781. #elif defined(HAVE_ECC)
  34782. #if defined(USE_CERT_BUFFERS_256)
  34783. unsigned char cert[sizeof(cliecc_cert_der_256)];
  34784. unsigned char key[sizeof(ecc_clikey_der_256)];
  34785. int certSz = (int)sizeof(cert);
  34786. int keySz = (int)sizeof(key);
  34787. XMEMSET(cert, 0, certSz);
  34788. XMEMSET(key, 0, keySz);
  34789. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  34790. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  34791. #else
  34792. unsigned char cert[ONEK_BUF];
  34793. unsigned char key[ONEK_BUF];
  34794. XFILE fp = XBADFILE;
  34795. int certSz, keySz;
  34796. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  34797. XBADFILE);
  34798. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  34799. fp), 0);
  34800. if (fp != XBADFILE) {
  34801. XFCLOSE(fp);
  34802. fp = XBADFILE;
  34803. }
  34804. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  34805. XBADFILE);
  34806. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  34807. 0);
  34808. if (fp != XBADFILE)
  34809. XFCLOSE(fp);
  34810. #endif
  34811. #endif
  34812. XMEMSET(output, 0, sizeof(output));
  34813. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  34814. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  34815. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  34816. if (pkcs7 != NULL) {
  34817. pkcs7->content = data;
  34818. pkcs7->contentSz = sizeof(data);
  34819. pkcs7->privateKey = key;
  34820. pkcs7->privateKeySz = (word32)keySz;
  34821. }
  34822. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  34823. /* Test bad args. */
  34824. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  34825. BAD_FUNC_ARG);
  34826. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  34827. BAD_FUNC_ARG);
  34828. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  34829. wc_PKCS7_Free(pkcs7);
  34830. #endif
  34831. return EXPECT_RESULT();
  34832. } /* END test_wc_PKCS7_EncodeData */
  34833. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  34834. !defined(NO_RSA) && !defined(NO_SHA256)
  34835. /* RSA sign raw digest callback */
  34836. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  34837. byte* out, word32 outSz, byte* privateKey,
  34838. word32 privateKeySz, int devid, int hashOID)
  34839. {
  34840. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  34841. byte digInfoEncoding[] = {
  34842. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  34843. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  34844. 0x00, 0x04, 0x20
  34845. };
  34846. int ret;
  34847. byte digestInfo[ONEK_BUF];
  34848. byte sig[FOURK_BUF];
  34849. word32 digestInfoSz = 0;
  34850. word32 idx = 0;
  34851. RsaKey rsa;
  34852. /* SHA-256 required only for this example callback due to above
  34853. * digInfoEncoding[] */
  34854. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  34855. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  34856. (hashOID != SHA256h)) {
  34857. return -1;
  34858. }
  34859. /* build DigestInfo */
  34860. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  34861. digestInfoSz += sizeof(digInfoEncoding);
  34862. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  34863. digestInfoSz += digestSz;
  34864. /* set up RSA key */
  34865. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  34866. if (ret != 0) {
  34867. return ret;
  34868. }
  34869. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  34870. /* sign DigestInfo */
  34871. if (ret == 0) {
  34872. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  34873. &rsa, pkcs7->rng);
  34874. if (ret > 0) {
  34875. if (ret > (int)outSz) {
  34876. /* output buffer too small */
  34877. ret = -1;
  34878. }
  34879. else {
  34880. /* success, ret holds sig size */
  34881. XMEMCPY(out, sig, ret);
  34882. }
  34883. }
  34884. }
  34885. wc_FreeRsaKey(&rsa);
  34886. return ret;
  34887. }
  34888. #endif
  34889. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  34890. typedef struct encodeSignedDataStream {
  34891. byte out[FOURK_BUF*3];
  34892. int idx;
  34893. word32 outIdx;
  34894. } encodeSignedDataStream;
  34895. /* content is 8k of partially created bundle */
  34896. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  34897. {
  34898. int ret = 0;
  34899. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  34900. if (strm->outIdx < pkcs7->contentSz) {
  34901. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  34902. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  34903. *content = strm->out + strm->outIdx;
  34904. strm->outIdx += ret;
  34905. }
  34906. (void)pkcs7;
  34907. return ret;
  34908. }
  34909. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  34910. void* ctx)
  34911. {
  34912. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  34913. XMEMCPY(strm->out + strm->idx, output, outputSz);
  34914. strm->idx += outputSz;
  34915. (void)pkcs7;
  34916. return 0;
  34917. }
  34918. #endif
  34919. /*
  34920. * Testing wc_PKCS7_EncodeSignedData()
  34921. */
  34922. static int test_wc_PKCS7_EncodeSignedData(void)
  34923. {
  34924. EXPECT_DECLS;
  34925. #if defined(HAVE_PKCS7)
  34926. PKCS7* pkcs7 = NULL;
  34927. WC_RNG rng;
  34928. byte output[FOURK_BUF];
  34929. byte badOut[1];
  34930. word32 outputSz = (word32)sizeof(output);
  34931. word32 badOutSz = 0;
  34932. byte data[] = "Test data to encode.";
  34933. #ifndef NO_RSA
  34934. #if defined(USE_CERT_BUFFERS_2048)
  34935. byte key[sizeof(client_key_der_2048)];
  34936. byte cert[sizeof(client_cert_der_2048)];
  34937. word32 keySz = (word32)sizeof(key);
  34938. word32 certSz = (word32)sizeof(cert);
  34939. XMEMSET(key, 0, keySz);
  34940. XMEMSET(cert, 0, certSz);
  34941. XMEMCPY(key, client_key_der_2048, keySz);
  34942. XMEMCPY(cert, client_cert_der_2048, certSz);
  34943. #elif defined(USE_CERT_BUFFERS_1024)
  34944. byte key[sizeof_client_key_der_1024];
  34945. byte cert[sizeof(sizeof_client_cert_der_1024)];
  34946. word32 keySz = (word32)sizeof(key);
  34947. word32 certSz = (word32)sizeof(cert);
  34948. XMEMSET(key, 0, keySz);
  34949. XMEMSET(cert, 0, certSz);
  34950. XMEMCPY(key, client_key_der_1024, keySz);
  34951. XMEMCPY(cert, client_cert_der_1024, certSz);
  34952. #else
  34953. unsigned char cert[ONEK_BUF];
  34954. unsigned char key[ONEK_BUF];
  34955. XFILE fp = XBADFILE;
  34956. int certSz;
  34957. int keySz;
  34958. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  34959. XBADFILE);
  34960. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  34961. fp), 0);
  34962. if (fp != XBADFILE) {
  34963. XFCLOSE(fp);
  34964. fp = XBADFILE;
  34965. }
  34966. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  34967. XBADFILE);
  34968. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  34969. 0);
  34970. if (fp != XBADFILE)
  34971. XFCLOSE(fp);
  34972. #endif
  34973. #elif defined(HAVE_ECC)
  34974. #if defined(USE_CERT_BUFFERS_256)
  34975. unsigned char cert[sizeof(cliecc_cert_der_256)];
  34976. unsigned char key[sizeof(ecc_clikey_der_256)];
  34977. int certSz = (int)sizeof(cert);
  34978. int keySz = (int)sizeof(key);
  34979. XMEMSET(cert, 0, certSz);
  34980. XMEMSET(key, 0, keySz);
  34981. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  34982. XMEMCPY(key, ecc_clikey_der_256, keySz);
  34983. #else
  34984. unsigned char cert[ONEK_BUF];
  34985. unsigned char key[ONEK_BUF];
  34986. XFILE fp = XBADFILE;
  34987. int certSz;
  34988. int keySz;
  34989. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  34990. XBADFILE);
  34991. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  34992. if (fp != XBADFILE) {
  34993. XFCLOSE(fp);
  34994. fp = XBADFILE;
  34995. }
  34996. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  34997. XBADFILE);
  34998. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  34999. if (fp != XBADFILE)
  35000. XFCLOSE(fp);
  35001. #endif
  35002. #endif
  35003. XMEMSET(&rng, 0, sizeof(WC_RNG));
  35004. XMEMSET(output, 0, outputSz);
  35005. ExpectIntEQ(wc_InitRng(&rng), 0);
  35006. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35007. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  35008. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  35009. if (pkcs7 != NULL) {
  35010. pkcs7->content = data;
  35011. pkcs7->contentSz = (word32)sizeof(data);
  35012. pkcs7->privateKey = key;
  35013. pkcs7->privateKeySz = (word32)sizeof(key);
  35014. pkcs7->encryptOID = RSAk;
  35015. #ifdef NO_SHA
  35016. pkcs7->hashOID = SHA256h;
  35017. #else
  35018. pkcs7->hashOID = SHAh;
  35019. #endif
  35020. pkcs7->rng = &rng;
  35021. }
  35022. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  35023. wc_PKCS7_Free(pkcs7);
  35024. pkcs7 = NULL;
  35025. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35026. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35027. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  35028. #ifdef ASN_BER_TO_DER
  35029. wc_PKCS7_Free(pkcs7);
  35030. /* reinitialize and test setting stream mode */
  35031. {
  35032. int signedSz = 0;
  35033. encodeSignedDataStream strm;
  35034. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35035. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  35036. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  35037. if (pkcs7 != NULL) {
  35038. pkcs7->content = data;
  35039. pkcs7->contentSz = (word32)sizeof(data);
  35040. pkcs7->privateKey = key;
  35041. pkcs7->privateKeySz = (word32)sizeof(key);
  35042. pkcs7->encryptOID = RSAk;
  35043. #ifdef NO_SHA
  35044. pkcs7->hashOID = SHA256h;
  35045. #else
  35046. pkcs7->hashOID = SHAh;
  35047. #endif
  35048. pkcs7->rng = &rng;
  35049. }
  35050. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  35051. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  35052. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  35053. BAD_FUNC_ARG);
  35054. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  35055. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  35056. outputSz), 0);
  35057. wc_PKCS7_Free(pkcs7);
  35058. pkcs7 = NULL;
  35059. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35060. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35061. /* use exact signed buffer size since BER encoded */
  35062. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)signedSz), 0);
  35063. wc_PKCS7_Free(pkcs7);
  35064. /* now try with using callbacks for IO */
  35065. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35066. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  35067. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  35068. if (pkcs7 != NULL) {
  35069. pkcs7->contentSz = FOURK_BUF*2;
  35070. pkcs7->privateKey = key;
  35071. pkcs7->privateKeySz = (word32)sizeof(key);
  35072. pkcs7->encryptOID = RSAk;
  35073. #ifdef NO_SHA
  35074. pkcs7->hashOID = SHA256h;
  35075. #else
  35076. pkcs7->hashOID = SHAh;
  35077. #endif
  35078. pkcs7->rng = &rng;
  35079. }
  35080. XMEMSET(&strm, 0, sizeof(strm));
  35081. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  35082. StreamOutputCB, (void*)&strm), 0);
  35083. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  35084. wc_PKCS7_Free(pkcs7);
  35085. pkcs7 = NULL;
  35086. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35087. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35088. /* use exact signed buffer size since BER encoded */
  35089. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, (word32)signedSz), 0);
  35090. }
  35091. #endif
  35092. #ifndef NO_PKCS7_STREAM
  35093. wc_PKCS7_Free(pkcs7);
  35094. pkcs7 = NULL;
  35095. {
  35096. word32 z;
  35097. int ret;
  35098. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35099. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35100. /* test for streaming mode */
  35101. ret = -1;
  35102. for (z = 0; z < outputSz && ret != 0; z++) {
  35103. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  35104. if (ret < 0){
  35105. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  35106. }
  35107. }
  35108. ExpectIntEQ(ret, 0);
  35109. ExpectIntNE(pkcs7->contentSz, 0);
  35110. ExpectNotNull(pkcs7->contentDynamic);
  35111. }
  35112. #endif /* !NO_PKCS7_STREAM */
  35113. /* Pass in bad args. */
  35114. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  35115. BAD_FUNC_ARG);
  35116. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  35117. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  35118. badOutSz), BAD_FUNC_ARG);
  35119. if (pkcs7 != NULL) {
  35120. pkcs7->hashOID = 0; /* bad hashOID */
  35121. }
  35122. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  35123. BAD_FUNC_ARG);
  35124. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  35125. !defined(NO_RSA) && !defined(NO_SHA256)
  35126. /* test RSA sign raw digest callback, if using RSA and compiled in.
  35127. * Example callback assumes SHA-256, so only run test if compiled in. */
  35128. wc_PKCS7_Free(pkcs7);
  35129. pkcs7 = NULL;
  35130. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35131. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  35132. if (pkcs7 != NULL) {
  35133. pkcs7->content = data;
  35134. pkcs7->contentSz = (word32)sizeof(data);
  35135. pkcs7->privateKey = key;
  35136. pkcs7->privateKeySz = (word32)sizeof(key);
  35137. pkcs7->encryptOID = RSAk;
  35138. pkcs7->hashOID = SHA256h;
  35139. pkcs7->rng = &rng;
  35140. }
  35141. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  35142. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  35143. #endif
  35144. wc_PKCS7_Free(pkcs7);
  35145. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  35146. #endif
  35147. return EXPECT_RESULT();
  35148. } /* END test_wc_PKCS7_EncodeSignedData */
  35149. /*
  35150. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  35151. */
  35152. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  35153. {
  35154. EXPECT_DECLS;
  35155. #if defined(HAVE_PKCS7)
  35156. int i;
  35157. PKCS7* pkcs7 = NULL;
  35158. WC_RNG rng;
  35159. byte outputHead[FOURK_BUF/2];
  35160. byte outputFoot[FOURK_BUF/2];
  35161. word32 outputHeadSz = (word32)sizeof(outputHead);
  35162. word32 outputFootSz = (word32)sizeof(outputFoot);
  35163. byte data[FOURK_BUF];
  35164. wc_HashAlg hash;
  35165. #ifdef NO_SHA
  35166. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  35167. #else
  35168. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  35169. #endif
  35170. byte hashBuf[WC_MAX_DIGEST_SIZE];
  35171. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  35172. #ifndef NO_RSA
  35173. #if defined(USE_CERT_BUFFERS_2048)
  35174. byte key[sizeof(client_key_der_2048)];
  35175. byte cert[sizeof(client_cert_der_2048)];
  35176. word32 keySz = (word32)sizeof(key);
  35177. word32 certSz = (word32)sizeof(cert);
  35178. XMEMSET(key, 0, keySz);
  35179. XMEMSET(cert, 0, certSz);
  35180. XMEMCPY(key, client_key_der_2048, keySz);
  35181. XMEMCPY(cert, client_cert_der_2048, certSz);
  35182. #elif defined(USE_CERT_BUFFERS_1024)
  35183. byte key[sizeof_client_key_der_1024];
  35184. byte cert[sizeof(sizeof_client_cert_der_1024)];
  35185. word32 keySz = (word32)sizeof(key);
  35186. word32 certSz = (word32)sizeof(cert);
  35187. XMEMSET(key, 0, keySz);
  35188. XMEMSET(cert, 0, certSz);
  35189. XMEMCPY(key, client_key_der_1024, keySz);
  35190. XMEMCPY(cert, client_cert_der_1024, certSz);
  35191. #else
  35192. unsigned char cert[ONEK_BUF];
  35193. unsigned char key[ONEK_BUF];
  35194. XFILE fp = XBADFILE;
  35195. int certSz;
  35196. int keySz;
  35197. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  35198. XBADFILE);
  35199. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  35200. fp), 0);
  35201. if (fp != XBADFILE) {
  35202. XFCLOSE(fp);
  35203. fp = XBADFILE;
  35204. }
  35205. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  35206. XBADFILE);
  35207. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  35208. 0);
  35209. if (fp != XBADFILE)
  35210. XFCLOSE(fp);
  35211. #endif
  35212. #elif defined(HAVE_ECC)
  35213. #if defined(USE_CERT_BUFFERS_256)
  35214. unsigned char cert[sizeof(cliecc_cert_der_256)];
  35215. unsigned char key[sizeof(ecc_clikey_der_256)];
  35216. int certSz = (int)sizeof(cert);
  35217. int keySz = (int)sizeof(key);
  35218. XMEMSET(cert, 0, certSz);
  35219. XMEMSET(key, 0, keySz);
  35220. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  35221. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  35222. #else
  35223. unsigned char cert[ONEK_BUF];
  35224. unsigned char key[ONEK_BUF];
  35225. XFILE fp = XBADFILE;
  35226. int certSz;
  35227. int keySz;
  35228. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  35229. XBADFILE);
  35230. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  35231. fp), 0);
  35232. if (fp != XBADFILE) {
  35233. XFCLOSE(fp);
  35234. fp = XBADFILE;
  35235. }
  35236. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  35237. XBADFILE);
  35238. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  35239. 0);
  35240. if (fp != XBADFILE)
  35241. XFCLOSE(fp);
  35242. #endif
  35243. #endif
  35244. XMEMSET(&rng, 0, sizeof(WC_RNG));
  35245. /* initialize large data with sequence */
  35246. for (i=0; i<(int)sizeof(data); i++)
  35247. data[i] = i & 0xff;
  35248. XMEMSET(outputHead, 0, outputHeadSz);
  35249. XMEMSET(outputFoot, 0, outputFootSz);
  35250. ExpectIntEQ(wc_InitRng(&rng), 0);
  35251. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35252. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  35253. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  35254. if (pkcs7 != NULL) {
  35255. pkcs7->content = NULL; /* not used for ex */
  35256. pkcs7->contentSz = (word32)sizeof(data);
  35257. pkcs7->privateKey = key;
  35258. pkcs7->privateKeySz = (word32)sizeof(key);
  35259. pkcs7->encryptOID = RSAk;
  35260. #ifdef NO_SHA
  35261. pkcs7->hashOID = SHA256h;
  35262. #else
  35263. pkcs7->hashOID = SHAh;
  35264. #endif
  35265. pkcs7->rng = &rng;
  35266. }
  35267. /* calculate hash for content */
  35268. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  35269. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  35270. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  35271. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  35272. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  35273. /* Perform PKCS7 sign using hash directly */
  35274. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  35275. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  35276. ExpectIntGT(outputHeadSz, 0);
  35277. ExpectIntGT(outputFootSz, 0);
  35278. wc_PKCS7_Free(pkcs7);
  35279. pkcs7 = NULL;
  35280. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35281. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35282. /* required parameter even on verify when using _ex, if using outputHead
  35283. * and outputFoot */
  35284. if (pkcs7 != NULL) {
  35285. pkcs7->contentSz = (word32)sizeof(data);
  35286. }
  35287. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  35288. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  35289. wc_PKCS7_Free(pkcs7);
  35290. pkcs7 = NULL;
  35291. /* assembly complete PKCS7 sign and use normal verify */
  35292. {
  35293. byte* output = NULL;
  35294. word32 outputSz = 0;
  35295. #ifndef NO_PKCS7_STREAM
  35296. word32 z;
  35297. int ret;
  35298. #endif /* !NO_PKCS7_STREAM */
  35299. ExpectNotNull(output = (byte*)XMALLOC(
  35300. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  35301. DYNAMIC_TYPE_TMP_BUFFER));
  35302. if (output != NULL) {
  35303. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  35304. outputSz += outputHeadSz;
  35305. XMEMCPY(&output[outputSz], data, sizeof(data));
  35306. outputSz += sizeof(data);
  35307. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  35308. outputSz += outputFootSz;
  35309. }
  35310. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35311. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35312. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  35313. #ifndef NO_PKCS7_STREAM
  35314. wc_PKCS7_Free(pkcs7);
  35315. pkcs7 = NULL;
  35316. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35317. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35318. /* test for streaming mode */
  35319. ret = -1;
  35320. for (z = 0; z < outputSz && ret != 0; z++) {
  35321. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  35322. if (ret < 0){
  35323. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  35324. }
  35325. }
  35326. ExpectIntEQ(ret, 0);
  35327. ExpectIntNE(pkcs7->contentSz, 0);
  35328. ExpectNotNull(pkcs7->contentDynamic);
  35329. wc_PKCS7_Free(pkcs7);
  35330. pkcs7 = NULL;
  35331. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35332. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35333. #endif /* !NO_PKCS7_STREAM */
  35334. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35335. }
  35336. /* Pass in bad args. */
  35337. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  35338. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  35339. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  35340. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  35341. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  35342. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  35343. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  35344. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  35345. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  35346. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  35347. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  35348. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  35349. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  35350. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  35351. if (pkcs7 != NULL) {
  35352. pkcs7->hashOID = 0; /* bad hashOID */
  35353. }
  35354. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  35355. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  35356. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  35357. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  35358. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  35359. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  35360. #ifndef NO_PKCS7_STREAM
  35361. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  35362. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  35363. #else
  35364. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  35365. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  35366. #endif
  35367. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  35368. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  35369. #ifndef NO_PKCS7_STREAM
  35370. /* can pass in 0 buffer length with streaming API */
  35371. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  35372. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  35373. #else
  35374. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  35375. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  35376. #endif
  35377. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  35378. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  35379. #ifndef NO_PKCS7_STREAM
  35380. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  35381. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  35382. #else
  35383. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  35384. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  35385. #endif
  35386. wc_PKCS7_Free(pkcs7);
  35387. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  35388. #endif
  35389. return EXPECT_RESULT();
  35390. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  35391. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  35392. /**
  35393. * Loads certs/keys from files or buffers into the argument buffers,
  35394. * helper function called by CreatePKCS7SignedData().
  35395. *
  35396. * Returns 0 on success, negative on error.
  35397. */
  35398. static int LoadPKCS7SignedDataCerts(
  35399. int useIntermediateCertChain, int pkAlgoType,
  35400. byte* intCARoot, word32* intCARootSz,
  35401. byte* intCA1, word32* intCA1Sz,
  35402. byte* intCA2, word32* intCA2Sz,
  35403. byte* cert, word32* certSz,
  35404. byte* key, word32* keySz)
  35405. {
  35406. EXPECT_DECLS;
  35407. int ret = 0;
  35408. XFILE fp = XBADFILE;
  35409. #ifndef NO_RSA
  35410. const char* intCARootRSA = "./certs/ca-cert.der";
  35411. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  35412. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  35413. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  35414. const char* intServKeyRSA = "./certs/server-key.der";
  35415. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  35416. const char* cli1024Cert = "./certs/1024/client-cert.der";
  35417. const char* cli1024Key = "./certs/1024/client-key.der";
  35418. #endif
  35419. #endif
  35420. #ifdef HAVE_ECC
  35421. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  35422. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  35423. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  35424. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  35425. const char* intServKeyECC = "./certs/ecc-key.der";
  35426. #ifndef USE_CERT_BUFFERS_256
  35427. const char* cliEccCert = "./certs/client-ecc-cert.der";
  35428. const char* cliEccKey = "./certs/client-ecc-key.der";
  35429. #endif
  35430. #endif
  35431. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  35432. ((useIntermediateCertChain == 1) &&
  35433. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  35434. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  35435. return BAD_FUNC_ARG;
  35436. }
  35437. /* Read/load certs and keys to use for signing based on PK type and chain */
  35438. switch (pkAlgoType) {
  35439. #ifndef NO_RSA
  35440. case RSA_TYPE:
  35441. if (useIntermediateCertChain == 1) {
  35442. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  35443. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  35444. if (fp != XBADFILE) {
  35445. XFCLOSE(fp);
  35446. fp = XBADFILE;
  35447. }
  35448. ExpectIntGT(*intCARootSz, 0);
  35449. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  35450. if (fp != XBADFILE) {
  35451. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  35452. XFCLOSE(fp);
  35453. fp = XBADFILE;
  35454. }
  35455. ExpectIntGT(*intCA1Sz, 0);
  35456. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  35457. if (fp != XBADFILE) {
  35458. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  35459. XFCLOSE(fp);
  35460. fp = XBADFILE;
  35461. }
  35462. ExpectIntGT(*intCA2Sz, 0);
  35463. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  35464. if (fp != XBADFILE) {
  35465. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  35466. XFCLOSE(fp);
  35467. fp = XBADFILE;
  35468. }
  35469. ExpectIntGT(*certSz, 0);
  35470. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  35471. if (fp != XBADFILE) {
  35472. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  35473. XFCLOSE(fp);
  35474. fp = XBADFILE;
  35475. }
  35476. ExpectIntGT(*keySz, 0);
  35477. }
  35478. else {
  35479. #if defined(USE_CERT_BUFFERS_2048)
  35480. *keySz = sizeof_client_key_der_2048;
  35481. *certSz = sizeof_client_cert_der_2048;
  35482. XMEMCPY(key, client_key_der_2048, *keySz);
  35483. XMEMCPY(cert, client_cert_der_2048, *certSz);
  35484. #elif defined(USE_CERT_BUFFERS_1024)
  35485. *keySz = sizeof_client_key_der_1024;
  35486. *certSz = sizeof_client_cert_der_1024;
  35487. XMEMCPY(key, client_key_der_1024, *keySz);
  35488. XMEMCPY(cert, client_cert_der_1024, *certSz);
  35489. #else
  35490. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  35491. if (fp != XBADFILE) {
  35492. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  35493. XFCLOSE(fp);
  35494. fp = XBADFILE;
  35495. }
  35496. ExpectIntGT(*keySz, 0);
  35497. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  35498. if (fp != XBADFILE) {
  35499. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  35500. XFCLOSE(fp);
  35501. fp = XBADFILE;
  35502. }
  35503. ExpectIntGT(*certSz, 0);
  35504. #endif /* USE_CERT_BUFFERS_2048 */
  35505. }
  35506. break;
  35507. #endif /* !NO_RSA */
  35508. #ifdef HAVE_ECC
  35509. case ECC_TYPE:
  35510. if (useIntermediateCertChain == 1) {
  35511. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  35512. if (fp != XBADFILE) {
  35513. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  35514. fp);
  35515. XFCLOSE(fp);
  35516. fp = XBADFILE;
  35517. }
  35518. ExpectIntGT(*intCARootSz, 0);
  35519. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  35520. if (fp != XBADFILE) {
  35521. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  35522. XFCLOSE(fp);
  35523. fp = XBADFILE;
  35524. }
  35525. ExpectIntGT(*intCA1Sz, 0);
  35526. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  35527. if (fp != XBADFILE) {
  35528. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  35529. XFCLOSE(fp);
  35530. fp = XBADFILE;
  35531. }
  35532. ExpectIntGT(*intCA2Sz, 0);
  35533. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  35534. if (fp != XBADFILE) {
  35535. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  35536. XFCLOSE(fp);
  35537. fp = XBADFILE;
  35538. }
  35539. ExpectIntGT(*certSz, 0);
  35540. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  35541. if (fp != XBADFILE) {
  35542. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  35543. XFCLOSE(fp);
  35544. fp = XBADFILE;
  35545. }
  35546. ExpectIntGT(*keySz, 0);
  35547. }
  35548. else {
  35549. #if defined(USE_CERT_BUFFERS_256)
  35550. *keySz = sizeof_ecc_clikey_der_256;
  35551. *certSz = sizeof_cliecc_cert_der_256;
  35552. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  35553. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  35554. #else
  35555. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  35556. if (fp != XBADFILE) {
  35557. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  35558. XFCLOSE(fp);
  35559. fp = XBADFILE;
  35560. }
  35561. ExpectIntGT(*keySz, 0);
  35562. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  35563. if (fp != XBADFILE) {
  35564. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  35565. XFCLOSE(fp);
  35566. fp = XBADFILE;
  35567. }
  35568. ExpectIntGT(*certSz, 0);
  35569. #endif /* USE_CERT_BUFFERS_256 */
  35570. }
  35571. break;
  35572. #endif /* HAVE_ECC */
  35573. default:
  35574. WOLFSSL_MSG("Unsupported SignedData PK type");
  35575. ret = BAD_FUNC_ARG;
  35576. break;
  35577. }
  35578. if (EXPECT_FAIL() && (ret == 0)) {
  35579. ret = BAD_FUNC_ARG;
  35580. }
  35581. return ret;
  35582. }
  35583. /**
  35584. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  35585. *
  35586. * output output buffer to place SignedData
  35587. * outputSz size of output buffer
  35588. * data data buffer to be signed
  35589. * dataSz size of data buffer
  35590. * withAttribs [1/0] include attributes in SignedData message
  35591. * detachedSig [1/0] create detached signature, no content
  35592. * useIntCertChain [1/0] use certificate chain and include intermediate and
  35593. * root CAs in bundle
  35594. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  35595. *
  35596. * Return size of bundle created on success, negative on error */
  35597. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  35598. byte* data, word32 dataSz,
  35599. int withAttribs, int detachedSig,
  35600. int useIntermediateCertChain,
  35601. int pkAlgoType)
  35602. {
  35603. EXPECT_DECLS;
  35604. int ret = 0;
  35605. WC_RNG rng;
  35606. PKCS7* pkcs7 = NULL;
  35607. static byte messageTypeOid[] =
  35608. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  35609. 0x09, 0x02 };
  35610. static byte messageType[] = { 0x13, 2, '1', '9' };
  35611. PKCS7Attrib attribs[] =
  35612. {
  35613. { messageTypeOid, sizeof(messageTypeOid), messageType,
  35614. sizeof(messageType) }
  35615. };
  35616. byte intCARoot[TWOK_BUF];
  35617. byte intCA1[TWOK_BUF];
  35618. byte intCA2[TWOK_BUF];
  35619. byte cert[TWOK_BUF];
  35620. byte key[TWOK_BUF];
  35621. word32 intCARootSz = sizeof(intCARoot);
  35622. word32 intCA1Sz = sizeof(intCA1);
  35623. word32 intCA2Sz = sizeof(intCA2);
  35624. word32 certSz = sizeof(cert);
  35625. word32 keySz = sizeof(key);
  35626. XMEMSET(intCARoot, 0, intCARootSz);
  35627. XMEMSET(intCA1, 0, intCA1Sz);
  35628. XMEMSET(intCA2, 0, intCA2Sz);
  35629. XMEMSET(cert, 0, certSz);
  35630. XMEMSET(key, 0, keySz);
  35631. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  35632. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  35633. cert, &certSz, key, &keySz);
  35634. ExpectIntEQ(ret, 0);
  35635. XMEMSET(output, 0, outputSz);
  35636. ExpectIntEQ(wc_InitRng(&rng), 0);
  35637. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35638. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  35639. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  35640. if (useIntermediateCertChain == 1) {
  35641. /* Add intermediate and root CA certs into SignedData Certs SET */
  35642. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  35643. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  35644. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  35645. }
  35646. if (pkcs7 != NULL) {
  35647. pkcs7->content = data;
  35648. pkcs7->contentSz = dataSz;
  35649. pkcs7->privateKey = key;
  35650. pkcs7->privateKeySz = (word32)sizeof(key);
  35651. if (pkAlgoType == RSA_TYPE) {
  35652. pkcs7->encryptOID = RSAk;
  35653. }
  35654. else {
  35655. pkcs7->encryptOID = ECDSAk;
  35656. }
  35657. #ifdef NO_SHA
  35658. pkcs7->hashOID = SHA256h;
  35659. #else
  35660. pkcs7->hashOID = SHAh;
  35661. #endif
  35662. pkcs7->rng = &rng;
  35663. if (withAttribs) {
  35664. /* include a signed attribute */
  35665. pkcs7->signedAttribs = attribs;
  35666. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  35667. }
  35668. }
  35669. if (detachedSig) {
  35670. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  35671. }
  35672. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, (word32)outputSz);
  35673. ExpectIntGT(outputSz, 0);
  35674. wc_PKCS7_Free(pkcs7);
  35675. pkcs7 = NULL;
  35676. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35677. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35678. if (detachedSig && (pkcs7 != NULL)) {
  35679. pkcs7->content = data;
  35680. pkcs7->contentSz = dataSz;
  35681. }
  35682. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)outputSz), 0);
  35683. wc_PKCS7_Free(pkcs7);
  35684. wc_FreeRng(&rng);
  35685. if (EXPECT_FAIL()) {
  35686. outputSz = 0;
  35687. }
  35688. return outputSz;
  35689. }
  35690. #endif
  35691. /*
  35692. * Testing wc_PKCS_VerifySignedData()
  35693. */
  35694. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  35695. {
  35696. EXPECT_DECLS;
  35697. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35698. PKCS7* pkcs7 = NULL;
  35699. byte output[6000]; /* Large size needed for bundles with int CA certs */
  35700. word32 outputSz = sizeof(output);
  35701. byte data[] = "Test data to encode.";
  35702. byte badOut[1];
  35703. word32 badOutSz = 0;
  35704. byte badContent[] = "This is different content than was signed";
  35705. wc_HashAlg hash;
  35706. #ifdef NO_SHA
  35707. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  35708. #else
  35709. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  35710. #endif
  35711. byte hashBuf[WC_MAX_DIGEST_SIZE];
  35712. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  35713. #ifndef NO_RSA
  35714. PKCS7DecodedAttrib* decodedAttrib = NULL;
  35715. /* contentType OID (1.2.840.113549.1.9.3) */
  35716. static const byte contentTypeOid[] =
  35717. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  35718. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  35719. static const byte dataType[] =
  35720. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  35721. /* messageDigest OID (1.2.840.113549.1.9.4) */
  35722. static const byte messageDigestOid[] =
  35723. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  35724. #ifndef NO_ASN_TIME
  35725. /* signingTime OID () */
  35726. static const byte signingTimeOid[] =
  35727. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  35728. #endif
  35729. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  35730. int dateLength = 0;
  35731. byte dateFormat;
  35732. const byte* datePart = NULL;
  35733. struct tm timearg;
  35734. time_t now;
  35735. struct tm* nowTm = NULL;
  35736. #ifdef NEED_TMP_TIME
  35737. struct tm tmpTimeStorage;
  35738. struct tm* tmpTime = &tmpTimeStorage;
  35739. #endif
  35740. #endif /* !NO_ASN && !NO_ASN_TIME */
  35741. #ifndef NO_PKCS7_STREAM
  35742. word32 z;
  35743. int ret;
  35744. #endif /* !NO_PKCS7_STREAM */
  35745. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  35746. /* Success test with RSA certs/key */
  35747. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  35748. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  35749. /* calculate hash for content, used later */
  35750. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  35751. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  35752. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  35753. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  35754. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35755. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  35756. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35757. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  35758. #ifndef NO_PKCS7_STREAM
  35759. wc_PKCS7_Free(pkcs7);
  35760. pkcs7 = NULL;
  35761. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35762. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35763. /* test for streaming */
  35764. ret = -1;
  35765. for (z = 0; z < outputSz && ret != 0; z++) {
  35766. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  35767. if (ret < 0){
  35768. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  35769. }
  35770. }
  35771. ExpectIntEQ(ret, 0);
  35772. ExpectIntNE(pkcs7->contentSz, 0);
  35773. ExpectNotNull(pkcs7->contentDynamic);
  35774. #endif /* !NO_PKCS7_STREAM */
  35775. /* Check that decoded signed attributes are correct */
  35776. /* messageDigest should be first */
  35777. if (pkcs7 != NULL) {
  35778. decodedAttrib = pkcs7->decodedAttrib;
  35779. }
  35780. ExpectNotNull(decodedAttrib);
  35781. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  35782. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  35783. decodedAttrib->oidSz), 0);
  35784. /* + 2 for OCTET STRING and length bytes */
  35785. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  35786. ExpectNotNull(decodedAttrib->value);
  35787. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  35788. #ifndef NO_ASN_TIME
  35789. /* signingTime should be second */
  35790. if (decodedAttrib != NULL) {
  35791. decodedAttrib = decodedAttrib->next;
  35792. }
  35793. ExpectNotNull(decodedAttrib);
  35794. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  35795. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  35796. decodedAttrib->oidSz), 0);
  35797. ExpectIntGT(decodedAttrib->valueSz, 0);
  35798. ExpectNotNull(decodedAttrib->value);
  35799. #endif
  35800. /* Verify signingTime if ASN and time are available */
  35801. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  35802. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  35803. &datePart, &dateFormat, &dateLength), 0);
  35804. ExpectNotNull(datePart);
  35805. ExpectIntGT(dateLength, 0);
  35806. XMEMSET(&timearg, 0, sizeof(timearg));
  35807. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  35808. &timearg), 0);
  35809. /* Get current time and compare year/month/day against attribute value */
  35810. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  35811. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  35812. ExpectNotNull(nowTm);
  35813. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  35814. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  35815. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  35816. #endif /* !NO_ASN && !NO_ASN_TIME */
  35817. /* contentType should be third */
  35818. if (decodedAttrib != NULL) {
  35819. decodedAttrib = decodedAttrib->next;
  35820. }
  35821. ExpectNotNull(decodedAttrib);
  35822. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  35823. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  35824. decodedAttrib->oidSz), 0);
  35825. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  35826. ExpectNotNull(decodedAttrib->value);
  35827. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  35828. 0);
  35829. #endif /* !NO_RSA */
  35830. /* Test bad args. */
  35831. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  35832. BAD_FUNC_ARG);
  35833. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  35834. BAD_FUNC_ARG);
  35835. #ifndef NO_PKCS7_STREAM
  35836. /* can pass in 0 buffer length with streaming API */
  35837. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  35838. badOutSz), WC_PKCS7_WANT_READ_E);
  35839. #else
  35840. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  35841. badOutSz), BAD_FUNC_ARG);
  35842. #endif
  35843. wc_PKCS7_Free(pkcs7);
  35844. pkcs7 = NULL;
  35845. #ifndef NO_RSA
  35846. /* Try RSA certs/key/sig first */
  35847. outputSz = sizeof(output);
  35848. XMEMSET(output, 0, outputSz);
  35849. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  35850. (word32)sizeof(data),
  35851. 1, 1, 0, RSA_TYPE)), 0);
  35852. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35853. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35854. if (pkcs7 != NULL) {
  35855. pkcs7->content = badContent;
  35856. pkcs7->contentSz = sizeof(badContent);
  35857. }
  35858. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  35859. SIG_VERIFY_E);
  35860. wc_PKCS7_Free(pkcs7);
  35861. pkcs7 = NULL;
  35862. #ifndef NO_PKCS7_STREAM
  35863. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35864. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35865. if (pkcs7 != NULL) {
  35866. pkcs7->content = badContent;
  35867. pkcs7->contentSz = sizeof(badContent);
  35868. }
  35869. /* test for streaming */
  35870. ret = -1;
  35871. for (z = 0; z < outputSz && ret != 0; z++) {
  35872. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  35873. if (ret == WC_PKCS7_WANT_READ_E){
  35874. continue;
  35875. }
  35876. else if (ret < 0) {
  35877. break;
  35878. }
  35879. }
  35880. ExpectIntEQ(ret, SIG_VERIFY_E);
  35881. ExpectIntNE(pkcs7->contentSz, 0);
  35882. ExpectNotNull(pkcs7->contentDynamic);
  35883. wc_PKCS7_Free(pkcs7);
  35884. pkcs7 = NULL;
  35885. #endif /* !NO_PKCS7_STREAM */
  35886. /* Test success case with detached signature and valid content */
  35887. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35888. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35889. if (pkcs7 != NULL) {
  35890. pkcs7->content = data;
  35891. pkcs7->contentSz = sizeof(data);
  35892. }
  35893. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  35894. wc_PKCS7_Free(pkcs7);
  35895. pkcs7 = NULL;
  35896. #ifndef NO_PKCS7_STREAM
  35897. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35898. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35899. if (pkcs7 != NULL) {
  35900. pkcs7->content = data;
  35901. pkcs7->contentSz = sizeof(data);
  35902. }
  35903. /* test for streaming */
  35904. ret = -1;
  35905. for (z = 0; z < outputSz && ret != 0; z++) {
  35906. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  35907. if (ret < 0){
  35908. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  35909. }
  35910. }
  35911. ExpectIntEQ(ret, 0);
  35912. ExpectIntNE(pkcs7->contentSz, 0);
  35913. ExpectNotNull(pkcs7->contentDynamic);
  35914. wc_PKCS7_Free(pkcs7);
  35915. pkcs7 = NULL;
  35916. #endif /* !NO_PKCS7_STREAM */
  35917. /* verify using pre-computed content digest only (no content) */
  35918. {
  35919. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35920. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  35921. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  35922. output, outputSz, NULL, 0), 0);
  35923. wc_PKCS7_Free(pkcs7);
  35924. pkcs7 = NULL;
  35925. }
  35926. #endif /* !NO_RSA */
  35927. /* Test verify on signedData containing intermediate/root CA certs */
  35928. #ifndef NO_RSA
  35929. outputSz = sizeof(output);
  35930. XMEMSET(output, 0, outputSz);
  35931. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  35932. (word32)sizeof(data),
  35933. 0, 0, 1, RSA_TYPE)), 0);
  35934. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35935. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35936. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  35937. wc_PKCS7_Free(pkcs7);
  35938. pkcs7 = NULL;
  35939. #ifndef NO_PKCS7_STREAM
  35940. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35941. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35942. /* test for streaming */
  35943. ret = -1;
  35944. for (z = 0; z < outputSz && ret != 0; z++) {
  35945. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  35946. if (ret < 0){
  35947. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  35948. }
  35949. }
  35950. ExpectIntEQ(ret, 0);
  35951. ExpectIntNE(pkcs7->contentSz, 0);
  35952. ExpectNotNull(pkcs7->contentDynamic);
  35953. wc_PKCS7_Free(pkcs7);
  35954. pkcs7 = NULL;
  35955. #endif /* !NO_PKCS7_STREAM */
  35956. #endif /* !NO_RSA */
  35957. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  35958. !defined(NO_FILESYSTEM)
  35959. {
  35960. XFILE signedBundle = XBADFILE;
  35961. int signedBundleSz = 0;
  35962. int chunkSz = 1;
  35963. int i, rc = 0;
  35964. byte* buf = NULL;
  35965. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  35966. "rb")) != XBADFILE);
  35967. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  35968. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  35969. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  35970. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  35971. DYNAMIC_TYPE_FILE));
  35972. if (buf != NULL) {
  35973. ExpectIntEQ(XFREAD(buf, 1, (size_t)signedBundleSz, signedBundle),
  35974. signedBundleSz);
  35975. }
  35976. if (signedBundle != XBADFILE) {
  35977. XFCLOSE(signedBundle);
  35978. signedBundle = XBADFILE;
  35979. }
  35980. if (buf != NULL) {
  35981. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  35982. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  35983. for (i = 0; i < signedBundleSz;) {
  35984. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  35985. chunkSz;
  35986. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  35987. if (rc < 0 ) {
  35988. if (rc == WC_PKCS7_WANT_READ_E) {
  35989. i += sz;
  35990. continue;
  35991. }
  35992. break;
  35993. }
  35994. else {
  35995. break;
  35996. }
  35997. }
  35998. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  35999. wc_PKCS7_Free(pkcs7);
  36000. pkcs7 = NULL;
  36001. }
  36002. /* now try with malformed bundle */
  36003. if (buf != NULL) {
  36004. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36005. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36006. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  36007. for (i = 0; i < signedBundleSz;) {
  36008. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  36009. chunkSz;
  36010. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  36011. if (rc < 0 ) {
  36012. if (rc == WC_PKCS7_WANT_READ_E) {
  36013. i += sz;
  36014. continue;
  36015. }
  36016. break;
  36017. }
  36018. else {
  36019. break;
  36020. }
  36021. }
  36022. ExpectIntEQ(rc, ASN_PARSE_E);
  36023. wc_PKCS7_Free(pkcs7);
  36024. pkcs7 = NULL;
  36025. }
  36026. if (buf != NULL)
  36027. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36028. }
  36029. #endif /* BER and stream */
  36030. #endif
  36031. return EXPECT_RESULT();
  36032. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  36033. /*
  36034. * Testing wc_PKCS_VerifySignedData()
  36035. */
  36036. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  36037. {
  36038. EXPECT_DECLS;
  36039. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  36040. PKCS7* pkcs7 = NULL;
  36041. byte output[6000]; /* Large size needed for bundles with int CA certs */
  36042. word32 outputSz = sizeof(output);
  36043. byte data[] = "Test data to encode.";
  36044. byte badContent[] = "This is different content than was signed";
  36045. wc_HashAlg hash;
  36046. #ifndef NO_PKCS7_STREAM
  36047. word32 z;
  36048. int ret;
  36049. #endif /* !NO_PKCS7_STREAM */
  36050. #ifdef NO_SHA
  36051. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  36052. #else
  36053. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  36054. #endif
  36055. byte hashBuf[WC_MAX_DIGEST_SIZE];
  36056. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  36057. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  36058. /* Success test with ECC certs/key */
  36059. outputSz = sizeof(output);
  36060. XMEMSET(output, 0, outputSz);
  36061. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  36062. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  36063. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36064. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  36065. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36066. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  36067. wc_PKCS7_Free(pkcs7);
  36068. pkcs7 = NULL;
  36069. #ifndef NO_PKCS7_STREAM
  36070. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36071. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36072. /* test for streaming */
  36073. ret = -1;
  36074. for (z = 0; z < outputSz && ret != 0; z++) {
  36075. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  36076. if (ret < 0){
  36077. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  36078. }
  36079. }
  36080. ExpectIntEQ(ret, 0);
  36081. ExpectIntNE(pkcs7->contentSz, 0);
  36082. ExpectNotNull(pkcs7->contentDynamic);
  36083. wc_PKCS7_Free(pkcs7);
  36084. pkcs7 = NULL;
  36085. #endif /* !NO_PKCS7_STREAM */
  36086. /* Invalid content should error, use detached signature so we can
  36087. * easily change content */
  36088. outputSz = sizeof(output);
  36089. XMEMSET(output, 0, outputSz);
  36090. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  36091. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  36092. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36093. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36094. if (pkcs7 != NULL) {
  36095. pkcs7->content = badContent;
  36096. pkcs7->contentSz = sizeof(badContent);
  36097. }
  36098. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  36099. SIG_VERIFY_E);
  36100. wc_PKCS7_Free(pkcs7);
  36101. pkcs7 = NULL;
  36102. #ifndef NO_PKCS7_STREAM
  36103. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36104. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36105. if (pkcs7 != NULL) {
  36106. pkcs7->content = badContent;
  36107. pkcs7->contentSz = sizeof(badContent);
  36108. }
  36109. /* test for streaming */
  36110. ret = -1;
  36111. for (z = 0; z < outputSz && ret != 0; z++) {
  36112. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  36113. if (ret == WC_PKCS7_WANT_READ_E){
  36114. continue;
  36115. }
  36116. else if (ret < 0) {
  36117. break;
  36118. }
  36119. }
  36120. ExpectIntEQ(ret, SIG_VERIFY_E);
  36121. ExpectIntNE(pkcs7->contentSz, 0);
  36122. ExpectNotNull(pkcs7->contentDynamic);
  36123. wc_PKCS7_Free(pkcs7);
  36124. pkcs7 = NULL;
  36125. #endif /* !NO_PKCS7_STREAM */
  36126. /* Test success case with detached signature and valid content */
  36127. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36128. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36129. if (pkcs7 != NULL) {
  36130. pkcs7->content = data;
  36131. pkcs7->contentSz = sizeof(data);
  36132. }
  36133. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  36134. wc_PKCS7_Free(pkcs7);
  36135. pkcs7 = NULL;
  36136. #ifndef NO_PKCS7_STREAM
  36137. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36138. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36139. if (pkcs7 != NULL) {
  36140. pkcs7->content = data;
  36141. pkcs7->contentSz = sizeof(data);
  36142. }
  36143. /* test for streaming */
  36144. ret = -1;
  36145. for (z = 0; z < outputSz && ret != 0; z++) {
  36146. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  36147. if (ret < 0){
  36148. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  36149. }
  36150. }
  36151. ExpectIntEQ(ret, 0);
  36152. ExpectIntNE(pkcs7->contentSz, 0);
  36153. ExpectNotNull(pkcs7->contentDynamic);
  36154. wc_PKCS7_Free(pkcs7);
  36155. pkcs7 = NULL;
  36156. #endif /* !NO_PKCS7_STREAM */
  36157. /* verify using pre-computed content digest only (no content) */
  36158. {
  36159. /* calculate hash for content */
  36160. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  36161. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  36162. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  36163. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  36164. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36165. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  36166. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  36167. output, outputSz, NULL, 0), 0);
  36168. wc_PKCS7_Free(pkcs7);
  36169. pkcs7 = NULL;
  36170. }
  36171. /* Test verify on signedData containing intermediate/root CA certs */
  36172. outputSz = sizeof(output);
  36173. XMEMSET(output, 0, outputSz);
  36174. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  36175. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  36176. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36177. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36178. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  36179. wc_PKCS7_Free(pkcs7);
  36180. pkcs7 = NULL;
  36181. #ifndef NO_PKCS7_STREAM
  36182. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36183. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36184. /* test for streaming */
  36185. ret = -1;
  36186. for (z = 0; z < outputSz && ret != 0; z++) {
  36187. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  36188. if (ret < 0){
  36189. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  36190. }
  36191. }
  36192. ExpectIntEQ(ret, 0);
  36193. ExpectIntNE(pkcs7->contentSz, 0);
  36194. ExpectNotNull(pkcs7->contentDynamic);
  36195. wc_PKCS7_Free(pkcs7);
  36196. pkcs7 = NULL;
  36197. #endif /* !NO_PKCS7_STREAM */
  36198. #endif
  36199. return EXPECT_RESULT();
  36200. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  36201. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  36202. !defined(NO_AES_256)
  36203. static const byte defKey[] = {
  36204. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36205. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36206. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36207. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36208. };
  36209. static byte aesHandle[32]; /* simulated hardware key handle */
  36210. /* return 0 on success */
  36211. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  36212. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  36213. byte* in, int inSz, byte* out, void* usrCtx)
  36214. {
  36215. int ret;
  36216. Aes aes;
  36217. if (usrCtx == NULL) {
  36218. /* no simulated handle passed in */
  36219. return -1;
  36220. }
  36221. switch (encryptOID) {
  36222. case AES256CBCb:
  36223. if (ivSz != AES_BLOCK_SIZE)
  36224. return BAD_FUNC_ARG;
  36225. break;
  36226. default:
  36227. WOLFSSL_MSG("Unsupported content cipher type for test");
  36228. return ALGO_ID_E;
  36229. };
  36230. /* simulate using handle to get key */
  36231. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  36232. if (ret == 0) {
  36233. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  36234. if (ret == 0)
  36235. ret = wc_AesCbcDecrypt(&aes, out, in, (word32)inSz);
  36236. wc_AesFree(&aes);
  36237. }
  36238. (void)aad;
  36239. (void)aadSz;
  36240. (void)authTag;
  36241. (void)authTagSz;
  36242. (void)pkcs7;
  36243. return ret;
  36244. }
  36245. /* returns key size on success */
  36246. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  36247. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  36248. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  36249. {
  36250. int ret = -1;
  36251. (void)cekSz;
  36252. (void)cek;
  36253. (void)outSz;
  36254. (void)keyIdSz;
  36255. (void)direction;
  36256. (void)orginKey; /* used with KAKRI */
  36257. (void)orginKeySz;
  36258. if (out == NULL)
  36259. return BAD_FUNC_ARG;
  36260. if (keyId[0] != 0x00) {
  36261. return -1;
  36262. }
  36263. if (type != (int)PKCS7_KEKRI) {
  36264. return -1;
  36265. }
  36266. switch (keyWrapAlgo) {
  36267. case AES256_WRAP:
  36268. /* simulate setting a handle for later decryption but use key
  36269. * as handle in the test case here */
  36270. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  36271. aesHandle, sizeof(aesHandle), NULL);
  36272. if (ret < 0)
  36273. return ret;
  36274. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  36275. if (ret < 0)
  36276. return ret;
  36277. /* return key size on success */
  36278. return sizeof(defKey);
  36279. default:
  36280. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  36281. return BAD_KEYWRAP_ALG_E;
  36282. };
  36283. }
  36284. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  36285. /*
  36286. * Testing wc_PKCS7_EncodeEnvelopedData()
  36287. */
  36288. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  36289. {
  36290. EXPECT_DECLS;
  36291. #if defined(HAVE_PKCS7)
  36292. PKCS7* pkcs7 = NULL;
  36293. #ifdef ASN_BER_TO_DER
  36294. int encodedSz = 0;
  36295. #endif
  36296. #ifdef ECC_TIMING_RESISTANT
  36297. WC_RNG rng;
  36298. #endif
  36299. word32 tempWrd32 = 0;
  36300. byte* tmpBytePtr = NULL;
  36301. const char input[] = "Test data to encode.";
  36302. int i;
  36303. int testSz = 0;
  36304. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  36305. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  36306. byte* rsaCert = NULL;
  36307. byte* rsaPrivKey = NULL;
  36308. word32 rsaCertSz;
  36309. word32 rsaPrivKeySz;
  36310. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  36311. !defined(USE_CERT_BUFFERS_2048) )
  36312. static const char* rsaClientCert = "./certs/client-cert.der";
  36313. static const char* rsaClientKey = "./certs/client-key.der";
  36314. rsaCertSz = (word32)sizeof(rsaClientCert);
  36315. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  36316. #endif
  36317. #endif
  36318. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  36319. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  36320. byte* eccCert = NULL;
  36321. byte* eccPrivKey = NULL;
  36322. word32 eccCertSz;
  36323. word32 eccPrivKeySz;
  36324. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  36325. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  36326. static const char* eccClientKey = "./certs/ecc-client-key.der";
  36327. #endif
  36328. #endif
  36329. /* Generic buffer size. */
  36330. byte output[ONEK_BUF];
  36331. byte decoded[sizeof(input)/sizeof(char)];
  36332. int decodedSz = 0;
  36333. #ifndef NO_FILESYSTEM
  36334. XFILE certFile = XBADFILE;
  36335. XFILE keyFile = XBADFILE;
  36336. #endif
  36337. #ifdef ECC_TIMING_RESISTANT
  36338. XMEMSET(&rng, 0, sizeof(WC_RNG));
  36339. #endif
  36340. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  36341. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  36342. /* RSA certs and keys. */
  36343. #if defined(USE_CERT_BUFFERS_1024)
  36344. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  36345. /* Allocate buffer space. */
  36346. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  36347. DYNAMIC_TYPE_TMP_BUFFER));
  36348. /* Init buffer. */
  36349. if (rsaCert != NULL) {
  36350. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  36351. }
  36352. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  36353. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  36354. DYNAMIC_TYPE_TMP_BUFFER));
  36355. if (rsaPrivKey != NULL) {
  36356. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  36357. }
  36358. #elif defined(USE_CERT_BUFFERS_2048)
  36359. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  36360. /* Allocate buffer */
  36361. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  36362. DYNAMIC_TYPE_TMP_BUFFER));
  36363. /* Init buffer. */
  36364. if (rsaCert != NULL) {
  36365. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  36366. }
  36367. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  36368. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  36369. DYNAMIC_TYPE_TMP_BUFFER));
  36370. if (rsaPrivKey != NULL) {
  36371. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  36372. }
  36373. #else
  36374. /* File system. */
  36375. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  36376. rsaCertSz = (word32)FOURK_BUF;
  36377. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36378. DYNAMIC_TYPE_TMP_BUFFER));
  36379. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  36380. certFile)) > 0);
  36381. if (certFile != XBADFILE)
  36382. XFCLOSE(certFile);
  36383. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  36384. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36385. DYNAMIC_TYPE_TMP_BUFFER));
  36386. rsaPrivKeySz = (word32)FOURK_BUF;
  36387. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  36388. keyFile)) > 0);
  36389. if (keyFile != XBADFILE)
  36390. XFCLOSE(keyFile);
  36391. #endif /* USE_CERT_BUFFERS */
  36392. #endif /* NO_RSA */
  36393. /* ECC */
  36394. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  36395. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  36396. #ifdef USE_CERT_BUFFERS_256
  36397. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  36398. DYNAMIC_TYPE_TMP_BUFFER));
  36399. /* Init buffer. */
  36400. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  36401. if (eccCert != NULL) {
  36402. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  36403. }
  36404. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  36405. DYNAMIC_TYPE_TMP_BUFFER));
  36406. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  36407. if (eccPrivKey != NULL) {
  36408. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  36409. }
  36410. #else /* File system. */
  36411. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  36412. eccCertSz = (word32)FOURK_BUF;
  36413. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36414. DYNAMIC_TYPE_TMP_BUFFER));
  36415. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  36416. certFile)) > 0);
  36417. if (certFile != XBADFILE) {
  36418. XFCLOSE(certFile);
  36419. }
  36420. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  36421. eccPrivKeySz = (word32)FOURK_BUF;
  36422. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36423. DYNAMIC_TYPE_TMP_BUFFER));
  36424. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  36425. keyFile)) > 0);
  36426. if (keyFile != XBADFILE) {
  36427. XFCLOSE(keyFile);
  36428. }
  36429. #endif /* USE_CERT_BUFFERS_256 */
  36430. #endif /* END HAVE_ECC */
  36431. #ifndef NO_FILESYSTEM
  36432. /* Silence. */
  36433. (void)keyFile;
  36434. (void)certFile;
  36435. #endif
  36436. {
  36437. const pkcs7EnvelopedVector testVectors[] = {
  36438. /* DATA is a global variable defined in the makefile. */
  36439. #if !defined(NO_RSA)
  36440. #ifndef NO_DES3
  36441. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  36442. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  36443. #endif /* NO_DES3 */
  36444. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  36445. #ifndef NO_AES_128
  36446. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  36447. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  36448. #endif
  36449. #ifndef NO_AES_192
  36450. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  36451. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  36452. #endif
  36453. #ifndef NO_AES_256
  36454. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  36455. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  36456. #endif
  36457. #endif /* NO_AES && HAVE_AES_CBC */
  36458. #endif /* NO_RSA */
  36459. #if defined(HAVE_ECC)
  36460. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  36461. #if !defined(NO_SHA) && !defined(NO_AES_128)
  36462. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  36463. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  36464. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  36465. #endif
  36466. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  36467. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  36468. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  36469. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  36470. #endif
  36471. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  36472. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  36473. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  36474. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  36475. #endif
  36476. #endif /* NO_AES && HAVE_AES_CBC*/
  36477. #endif /* END HAVE_ECC */
  36478. }; /* END pkcs7EnvelopedVector */
  36479. #ifdef ECC_TIMING_RESISTANT
  36480. ExpectIntEQ(wc_InitRng(&rng), 0);
  36481. #endif
  36482. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36483. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  36484. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  36485. for (i = 0; i < testSz; i++) {
  36486. #ifdef ASN_BER_TO_DER
  36487. encodeSignedDataStream strm;
  36488. /* test setting stream mode, the first one using IO callbacks */
  36489. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  36490. (word32)(testVectors + i)->certSz), 0);
  36491. if (pkcs7 != NULL) {
  36492. #ifdef ECC_TIMING_RESISTANT
  36493. pkcs7->rng = &rng;
  36494. #endif
  36495. if (i != 0)
  36496. pkcs7->content = (byte*)(testVectors + i)->content;
  36497. pkcs7->contentSz = (testVectors + i)->contentSz;
  36498. pkcs7->contentOID = (testVectors + i)->contentOID;
  36499. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  36500. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  36501. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  36502. pkcs7->privateKey = (testVectors + i)->privateKey;
  36503. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  36504. }
  36505. if (i == 0) {
  36506. XMEMSET(&strm, 0, sizeof(strm));
  36507. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  36508. StreamOutputCB, (void*)&strm), 0);
  36509. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  36510. }
  36511. else {
  36512. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  36513. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  36514. (word32)sizeof(output));
  36515. }
  36516. switch ((testVectors + i)->encryptOID) {
  36517. #ifndef NO_DES3
  36518. case DES3b:
  36519. case DESb:
  36520. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  36521. break;
  36522. #endif
  36523. #ifdef HAVE_AESCCM
  36524. #ifdef WOLFSSL_AES_128
  36525. case AES128CCMb:
  36526. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  36527. break;
  36528. #endif
  36529. #ifdef WOLFSSL_AES_192
  36530. case AES192CCMb:
  36531. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  36532. break;
  36533. #endif
  36534. #ifdef WOLFSSL_AES_256
  36535. case AES256CCMb:
  36536. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  36537. break;
  36538. #endif
  36539. #endif
  36540. default:
  36541. ExpectIntGE(encodedSz, 0);
  36542. }
  36543. if (encodedSz > 0) {
  36544. if (i == 0) {
  36545. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  36546. strm.out, (word32)encodedSz, decoded,
  36547. (word32)sizeof(decoded));
  36548. }
  36549. else {
  36550. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36551. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  36552. }
  36553. ExpectIntGE(decodedSz, 0);
  36554. /* Verify the size of each buffer. */
  36555. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  36556. }
  36557. wc_PKCS7_Free(pkcs7);
  36558. pkcs7 = NULL;
  36559. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36560. #endif
  36561. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  36562. (word32)(testVectors + i)->certSz), 0);
  36563. if (pkcs7 != NULL) {
  36564. #ifdef ECC_TIMING_RESISTANT
  36565. pkcs7->rng = &rng;
  36566. #endif
  36567. pkcs7->content = (byte*)(testVectors + i)->content;
  36568. pkcs7->contentSz = (testVectors + i)->contentSz;
  36569. pkcs7->contentOID = (testVectors + i)->contentOID;
  36570. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  36571. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  36572. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  36573. pkcs7->privateKey = (testVectors + i)->privateKey;
  36574. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  36575. }
  36576. #ifdef ASN_BER_TO_DER
  36577. /* test without setting stream mode */
  36578. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  36579. #endif
  36580. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  36581. (word32)sizeof(output)), 0);
  36582. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36583. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  36584. ExpectIntGE(decodedSz, 0);
  36585. /* Verify the size of each buffer. */
  36586. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  36587. /* Don't free the last time through the loop. */
  36588. if (i < testSz - 1) {
  36589. wc_PKCS7_Free(pkcs7);
  36590. pkcs7 = NULL;
  36591. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36592. }
  36593. } /* END test loop. */
  36594. }
  36595. /* Test bad args. */
  36596. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  36597. (word32)sizeof(output)), BAD_FUNC_ARG);
  36598. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  36599. (word32)sizeof(output)), BAD_FUNC_ARG);
  36600. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  36601. /* Decode. */
  36602. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  36603. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  36604. BAD_FUNC_ARG);
  36605. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36606. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  36607. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36608. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  36609. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  36610. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  36611. BAD_FUNC_ARG);
  36612. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  36613. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  36614. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  36615. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  36616. /* only a failure for KARI test cases */
  36617. if (pkcs7 != NULL) {
  36618. tempWrd32 = pkcs7->singleCertSz;
  36619. pkcs7->singleCertSz = 0;
  36620. }
  36621. #if defined(WOLFSSL_ASN_TEMPLATE)
  36622. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36623. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  36624. BUFFER_E);
  36625. #else
  36626. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36627. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  36628. ASN_PARSE_E);
  36629. #endif
  36630. if (pkcs7 != NULL) {
  36631. pkcs7->singleCertSz = tempWrd32;
  36632. tmpBytePtr = pkcs7->singleCert;
  36633. pkcs7->singleCert = NULL;
  36634. }
  36635. #ifndef NO_RSA
  36636. #if defined(NO_PKCS7_STREAM)
  36637. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  36638. * is returned from kari parse which gets set to bad func arg */
  36639. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36640. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  36641. BAD_FUNC_ARG);
  36642. #else
  36643. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36644. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  36645. ASN_PARSE_E);
  36646. #endif
  36647. #endif /* !NO_RSA */
  36648. if (pkcs7 != NULL) {
  36649. pkcs7->singleCert = tmpBytePtr;
  36650. }
  36651. #endif
  36652. if (pkcs7 != NULL) {
  36653. tempWrd32 = pkcs7->privateKeySz;
  36654. pkcs7->privateKeySz = 0;
  36655. }
  36656. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36657. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  36658. BAD_FUNC_ARG);
  36659. if (pkcs7 != NULL) {
  36660. pkcs7->privateKeySz = tempWrd32;
  36661. tmpBytePtr = pkcs7->privateKey;
  36662. pkcs7->privateKey = NULL;
  36663. }
  36664. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36665. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  36666. BAD_FUNC_ARG);
  36667. if (pkcs7 != NULL) {
  36668. pkcs7->privateKey = tmpBytePtr;
  36669. }
  36670. wc_PKCS7_Free(pkcs7);
  36671. pkcs7 = NULL;
  36672. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  36673. /* test of decrypt callback with KEKRI enveloped data */
  36674. {
  36675. int envelopedSz = 0;
  36676. const byte keyId[] = { 0x00 };
  36677. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36678. if (pkcs7 != NULL) {
  36679. pkcs7->content = (byte*)input;
  36680. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  36681. pkcs7->contentOID = DATA;
  36682. pkcs7->encryptOID = AES256CBCb;
  36683. }
  36684. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  36685. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  36686. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  36687. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  36688. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  36689. (word32)sizeof(output))), 0);
  36690. wc_PKCS7_Free(pkcs7);
  36691. pkcs7 = NULL;
  36692. /* decode envelopedData */
  36693. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36694. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  36695. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  36696. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  36697. (word32)envelopedSz, decoded, sizeof(decoded))), 0);
  36698. wc_PKCS7_Free(pkcs7);
  36699. pkcs7 = NULL;
  36700. }
  36701. #endif /* !NO_AES && !NO_AES_256 */
  36702. #ifndef NO_RSA
  36703. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36704. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36705. #endif /* NO_RSA */
  36706. #ifdef HAVE_ECC
  36707. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36708. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36709. #endif /* HAVE_ECC */
  36710. #ifdef ECC_TIMING_RESISTANT
  36711. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  36712. #endif
  36713. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  36714. !defined(NO_RSA) && !defined(NO_SHA)
  36715. {
  36716. byte out[7];
  36717. byte *cms = NULL;
  36718. word32 cmsSz;
  36719. XFILE cmsFile = XBADFILE;
  36720. XMEMSET(out, 0, sizeof(out));
  36721. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36722. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  36723. != XBADFILE);
  36724. cmsSz = (word32)FOURK_BUF;
  36725. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36726. DYNAMIC_TYPE_TMP_BUFFER));
  36727. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  36728. if (cmsFile != XBADFILE)
  36729. XFCLOSE(cmsFile);
  36730. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  36731. sizeof_client_cert_der_2048), 0);
  36732. if (pkcs7 != NULL) {
  36733. pkcs7->privateKey = (byte*)client_key_der_2048;
  36734. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  36735. }
  36736. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  36737. 2), 0);
  36738. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  36739. sizeof(out)), 0);
  36740. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36741. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  36742. wc_PKCS7_Free(pkcs7);
  36743. pkcs7 = NULL;
  36744. }
  36745. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  36746. #endif /* HAVE_PKCS7 */
  36747. return EXPECT_RESULT();
  36748. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  36749. /*
  36750. * Testing wc_PKCS7_EncodeEncryptedData()
  36751. */
  36752. static int test_wc_PKCS7_EncodeEncryptedData(void)
  36753. {
  36754. EXPECT_DECLS;
  36755. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  36756. PKCS7* pkcs7 = NULL;
  36757. byte* tmpBytePtr = NULL;
  36758. byte encrypted[TWOK_BUF];
  36759. byte decoded[TWOK_BUF];
  36760. word32 tmpWrd32 = 0;
  36761. int tmpInt = 0;
  36762. int decodedSz = 0;
  36763. int encryptedSz = 0;
  36764. int testSz = 0;
  36765. int i = 0;
  36766. const byte data[] = { /* Hello World */
  36767. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  36768. 0x72,0x6c,0x64
  36769. };
  36770. #ifndef NO_DES3
  36771. byte desKey[] = {
  36772. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  36773. };
  36774. byte des3Key[] = {
  36775. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  36776. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  36777. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  36778. };
  36779. #endif
  36780. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  36781. #ifndef NO_AES_128
  36782. byte aes128Key[] = {
  36783. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36784. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36785. };
  36786. #endif
  36787. #ifndef NO_AES_192
  36788. byte aes192Key[] = {
  36789. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36790. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36791. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36792. };
  36793. #endif
  36794. #ifndef NO_AES_256
  36795. byte aes256Key[] = {
  36796. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36797. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36798. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36799. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36800. };
  36801. #endif
  36802. #endif /* !NO_AES && HAVE_AES_CBC */
  36803. const pkcs7EncryptedVector testVectors[] =
  36804. {
  36805. #ifndef NO_DES3
  36806. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  36807. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  36808. #endif /* !NO_DES3 */
  36809. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  36810. #ifndef NO_AES_128
  36811. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  36812. sizeof(aes128Key)},
  36813. #endif
  36814. #ifndef NO_AES_192
  36815. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  36816. sizeof(aes192Key)},
  36817. #endif
  36818. #ifndef NO_AES_256
  36819. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  36820. sizeof(aes256Key)},
  36821. #endif
  36822. #endif /* !NO_AES && HAVE_AES_CBC */
  36823. };
  36824. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  36825. for (i = 0; i < testSz; i++) {
  36826. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36827. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  36828. if (pkcs7 != NULL) {
  36829. pkcs7->content = (byte*)testVectors[i].content;
  36830. pkcs7->contentSz = testVectors[i].contentSz;
  36831. pkcs7->contentOID = testVectors[i].contentOID;
  36832. pkcs7->encryptOID = testVectors[i].encryptOID;
  36833. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  36834. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  36835. pkcs7->heap = HEAP_HINT;
  36836. }
  36837. /* encode encryptedData */
  36838. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  36839. sizeof(encrypted)), 0);
  36840. /* Decode encryptedData */
  36841. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  36842. (word32)encryptedSz, decoded, sizeof(decoded)), 0);
  36843. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  36844. /* Keep values for last itr. */
  36845. if (i < testSz - 1) {
  36846. wc_PKCS7_Free(pkcs7);
  36847. pkcs7 = NULL;
  36848. }
  36849. }
  36850. if (pkcs7 == NULL || testSz == 0) {
  36851. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36852. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  36853. }
  36854. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  36855. sizeof(encrypted)),BAD_FUNC_ARG);
  36856. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  36857. sizeof(encrypted)), BAD_FUNC_ARG);
  36858. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  36859. 0), BAD_FUNC_ARG);
  36860. /* Testing the struct. */
  36861. if (pkcs7 != NULL) {
  36862. tmpBytePtr = pkcs7->content;
  36863. pkcs7->content = NULL;
  36864. }
  36865. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  36866. sizeof(encrypted)), BAD_FUNC_ARG);
  36867. if (pkcs7 != NULL) {
  36868. pkcs7->content = tmpBytePtr;
  36869. tmpWrd32 = pkcs7->contentSz;
  36870. pkcs7->contentSz = 0;
  36871. }
  36872. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  36873. sizeof(encrypted)), BAD_FUNC_ARG);
  36874. if (pkcs7 != NULL) {
  36875. pkcs7->contentSz = tmpWrd32;
  36876. tmpInt = pkcs7->encryptOID;
  36877. pkcs7->encryptOID = 0;
  36878. }
  36879. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  36880. sizeof(encrypted)), BAD_FUNC_ARG);
  36881. if (pkcs7 != NULL) {
  36882. pkcs7->encryptOID = tmpInt;
  36883. tmpBytePtr = pkcs7->encryptionKey;
  36884. pkcs7->encryptionKey = NULL;
  36885. }
  36886. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  36887. sizeof(encrypted)), BAD_FUNC_ARG);
  36888. if (pkcs7 != NULL) {
  36889. pkcs7->encryptionKey = tmpBytePtr;
  36890. tmpWrd32 = pkcs7->encryptionKeySz;
  36891. pkcs7->encryptionKeySz = 0;
  36892. }
  36893. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  36894. sizeof(encrypted)), BAD_FUNC_ARG);
  36895. if (pkcs7 != NULL) {
  36896. pkcs7->encryptionKeySz = tmpWrd32;
  36897. }
  36898. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, (word32)encryptedSz,
  36899. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  36900. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, (word32)encryptedSz,
  36901. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  36902. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  36903. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  36904. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  36905. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  36906. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  36907. decoded, 0), BAD_FUNC_ARG);
  36908. /* Test struct fields */
  36909. if (pkcs7 != NULL) {
  36910. tmpBytePtr = pkcs7->encryptionKey;
  36911. pkcs7->encryptionKey = NULL;
  36912. }
  36913. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  36914. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  36915. if (pkcs7 != NULL) {
  36916. pkcs7->encryptionKey = tmpBytePtr;
  36917. pkcs7->encryptionKeySz = 0;
  36918. }
  36919. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  36920. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  36921. wc_PKCS7_Free(pkcs7);
  36922. #endif
  36923. return EXPECT_RESULT();
  36924. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  36925. /*
  36926. * Testing wc_PKCS7_Degenerate()
  36927. */
  36928. static int test_wc_PKCS7_Degenerate(void)
  36929. {
  36930. EXPECT_DECLS;
  36931. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  36932. PKCS7* pkcs7 = NULL;
  36933. char fName[] = "./certs/test-degenerate.p7b";
  36934. XFILE f = XBADFILE;
  36935. byte der[4096];
  36936. word32 derSz = 0;
  36937. #ifndef NO_PKCS7_STREAM
  36938. word32 z;
  36939. int ret;
  36940. #endif /* !NO_PKCS7_STREAM */
  36941. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  36942. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  36943. if (f != XBADFILE)
  36944. XFCLOSE(f);
  36945. /* test degenerate success */
  36946. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36947. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  36948. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36949. #ifndef NO_RSA
  36950. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  36951. #ifndef NO_PKCS7_STREAM
  36952. wc_PKCS7_Free(pkcs7);
  36953. pkcs7 = NULL;
  36954. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36955. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  36956. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36957. /* test for streaming */
  36958. ret = -1;
  36959. for (z = 0; z < derSz && ret != 0; z++) {
  36960. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  36961. if (ret < 0){
  36962. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  36963. }
  36964. }
  36965. ExpectIntEQ(ret, 0);
  36966. #endif /* !NO_PKCS7_STREAM */
  36967. #else
  36968. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  36969. #endif /* NO_RSA */
  36970. wc_PKCS7_Free(pkcs7);
  36971. pkcs7 = NULL;
  36972. /* test with turning off degenerate cases */
  36973. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36974. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  36975. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36976. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  36977. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  36978. PKCS7_NO_SIGNER_E);
  36979. #ifndef NO_PKCS7_STREAM
  36980. wc_PKCS7_Free(pkcs7);
  36981. pkcs7 = NULL;
  36982. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  36983. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  36984. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  36985. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  36986. /* test for streaming */
  36987. ret = -1;
  36988. for (z = 0; z < derSz && ret != 0; z++) {
  36989. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  36990. if (ret == WC_PKCS7_WANT_READ_E){
  36991. continue;
  36992. }
  36993. else
  36994. break;
  36995. }
  36996. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  36997. #endif /* !NO_PKCS7_STREAM */
  36998. wc_PKCS7_Free(pkcs7);
  36999. #endif
  37000. return EXPECT_RESULT();
  37001. } /* END test_wc_PKCS7_Degenerate() */
  37002. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  37003. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  37004. static byte berContent[] = {
  37005. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  37006. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  37007. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  37008. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  37009. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  37010. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  37011. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  37012. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  37013. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  37014. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  37015. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  37016. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  37017. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  37018. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  37019. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  37020. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  37021. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  37022. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  37023. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  37024. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  37025. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  37026. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  37027. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  37028. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  37029. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  37030. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  37031. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  37032. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  37033. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  37034. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  37035. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  37036. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  37037. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  37038. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  37039. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  37040. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  37041. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  37042. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  37043. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  37044. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  37045. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  37046. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  37047. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  37048. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  37049. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  37050. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  37051. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  37052. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  37053. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  37054. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  37055. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  37056. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  37057. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  37058. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  37059. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  37060. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  37061. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  37062. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  37063. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  37064. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  37065. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  37066. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  37067. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  37068. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  37069. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  37070. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  37071. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  37072. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  37073. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  37074. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  37075. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  37076. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  37077. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  37078. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  37079. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  37080. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  37081. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  37082. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  37083. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  37084. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  37085. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  37086. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  37087. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  37088. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  37089. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  37090. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  37091. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  37092. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  37093. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  37094. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  37095. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  37096. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  37097. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  37098. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  37099. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  37100. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  37101. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  37102. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  37103. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  37104. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  37105. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  37106. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  37107. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  37108. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  37109. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  37110. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  37111. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  37112. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  37113. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  37114. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  37115. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  37116. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  37117. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  37118. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  37119. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  37120. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  37121. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  37122. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  37123. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  37124. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  37125. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  37126. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  37127. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  37128. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  37129. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  37130. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  37131. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  37132. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  37133. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  37134. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  37135. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  37136. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  37137. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  37138. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  37139. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  37140. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  37141. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  37142. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  37143. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  37144. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  37145. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  37146. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  37147. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  37148. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  37149. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  37150. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  37151. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  37152. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  37153. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  37154. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  37155. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  37156. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  37157. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  37158. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  37159. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  37160. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  37161. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  37162. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  37163. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  37164. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  37165. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  37166. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  37167. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  37168. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  37169. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  37170. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  37171. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  37172. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  37173. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  37174. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  37175. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  37176. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  37177. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  37178. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  37179. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  37180. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  37181. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  37182. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  37183. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  37184. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  37185. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  37186. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  37187. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  37188. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  37189. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  37190. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  37191. 0x00, 0x00, 0x00, 0x00, 0x00
  37192. };
  37193. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  37194. * !NO_DES3 && !NO_SHA
  37195. */
  37196. /*
  37197. * Testing wc_PKCS7_BER()
  37198. */
  37199. static int test_wc_PKCS7_BER(void)
  37200. {
  37201. EXPECT_DECLS;
  37202. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  37203. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  37204. PKCS7* pkcs7 = NULL;
  37205. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  37206. XFILE f = XBADFILE;
  37207. byte der[4096];
  37208. #ifndef NO_DES3
  37209. byte decoded[2048];
  37210. #endif
  37211. word32 derSz = 0;
  37212. #ifndef NO_PKCS7_STREAM
  37213. word32 z;
  37214. int ret;
  37215. #endif /* !NO_PKCS7_STREAM */
  37216. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  37217. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  37218. if (f != XBADFILE) {
  37219. XFCLOSE(f);
  37220. f = XBADFILE;
  37221. }
  37222. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  37223. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  37224. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  37225. #ifndef NO_RSA
  37226. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  37227. #ifndef NO_PKCS7_STREAM
  37228. wc_PKCS7_Free(pkcs7);
  37229. pkcs7 = NULL;
  37230. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  37231. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  37232. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  37233. /* test for streaming */
  37234. ret = -1;
  37235. for (z = 0; z < derSz && ret != 0; z++) {
  37236. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  37237. if (ret < 0){
  37238. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  37239. }
  37240. }
  37241. ExpectIntEQ(ret, 0);
  37242. #endif /* !NO_PKCS7_STREAM */
  37243. #else
  37244. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  37245. #endif
  37246. wc_PKCS7_Free(pkcs7);
  37247. pkcs7 = NULL;
  37248. #ifndef NO_DES3
  37249. /* decode BER content */
  37250. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  37251. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  37252. if (f != XBADFILE) {
  37253. XFCLOSE(f);
  37254. f = XBADFILE;
  37255. }
  37256. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  37257. #ifndef NO_RSA
  37258. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  37259. #else
  37260. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  37261. #endif
  37262. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  37263. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  37264. if (f != XBADFILE) {
  37265. XFCLOSE(f);
  37266. f = XBADFILE;
  37267. }
  37268. if (pkcs7 != NULL) {
  37269. pkcs7->privateKey = der;
  37270. pkcs7->privateKeySz = derSz;
  37271. }
  37272. #ifndef NO_RSA
  37273. #ifdef WOLFSSL_SP_MATH
  37274. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  37275. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  37276. #else
  37277. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  37278. sizeof(berContent), decoded, sizeof(decoded)), 0);
  37279. #endif
  37280. #else
  37281. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  37282. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  37283. #endif
  37284. wc_PKCS7_Free(pkcs7);
  37285. #endif /* !NO_DES3 */
  37286. #endif
  37287. return EXPECT_RESULT();
  37288. } /* END test_wc_PKCS7_BER() */
  37289. static int test_wc_PKCS7_signed_enveloped(void)
  37290. {
  37291. EXPECT_DECLS;
  37292. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  37293. !defined(NO_FILESYSTEM)
  37294. XFILE f = XBADFILE;
  37295. PKCS7* pkcs7 = NULL;
  37296. #ifdef HAVE_AES_CBC
  37297. PKCS7* inner = NULL;
  37298. #endif
  37299. WC_RNG rng;
  37300. unsigned char key[FOURK_BUF/2];
  37301. unsigned char cert[FOURK_BUF/2];
  37302. unsigned char env[FOURK_BUF];
  37303. int envSz = FOURK_BUF;
  37304. int keySz = 0;
  37305. int certSz = 0;
  37306. unsigned char sig[FOURK_BUF * 2];
  37307. int sigSz = FOURK_BUF * 2;
  37308. #ifdef HAVE_AES_CBC
  37309. unsigned char decoded[FOURK_BUF];
  37310. int decodedSz = FOURK_BUF;
  37311. #endif
  37312. #ifndef NO_PKCS7_STREAM
  37313. int z;
  37314. int ret;
  37315. #endif /* !NO_PKCS7_STREAM */
  37316. XMEMSET(&rng, 0, sizeof(WC_RNG));
  37317. /* load cert */
  37318. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  37319. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  37320. if (f != XBADFILE) {
  37321. XFCLOSE(f);
  37322. f = XBADFILE;
  37323. }
  37324. /* load key */
  37325. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  37326. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  37327. if (f != XBADFILE) {
  37328. XFCLOSE(f);
  37329. f = XBADFILE;
  37330. }
  37331. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  37332. /* sign cert for envelope */
  37333. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37334. ExpectIntEQ(wc_InitRng(&rng), 0);
  37335. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  37336. if (pkcs7 != NULL) {
  37337. pkcs7->content = cert;
  37338. pkcs7->contentSz = (word32)certSz;
  37339. pkcs7->contentOID = DATA;
  37340. pkcs7->privateKey = key;
  37341. pkcs7->privateKeySz = (word32)keySz;
  37342. pkcs7->encryptOID = RSAk;
  37343. pkcs7->hashOID = SHA256h;
  37344. pkcs7->rng = &rng;
  37345. }
  37346. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  37347. wc_PKCS7_Free(pkcs7);
  37348. pkcs7 = NULL;
  37349. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  37350. #ifdef HAVE_AES_CBC
  37351. /* create envelope */
  37352. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37353. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  37354. if (pkcs7 != NULL) {
  37355. pkcs7->content = sig;
  37356. pkcs7->contentSz = (word32)sigSz;
  37357. pkcs7->contentOID = DATA;
  37358. pkcs7->encryptOID = AES256CBCb;
  37359. pkcs7->privateKey = key;
  37360. pkcs7->privateKeySz = (word32)keySz;
  37361. }
  37362. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, (word32)envSz)), 0);
  37363. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  37364. wc_PKCS7_Free(pkcs7);
  37365. pkcs7 = NULL;
  37366. #endif
  37367. /* create bad signed enveloped data */
  37368. sigSz = FOURK_BUF * 2;
  37369. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37370. ExpectIntEQ(wc_InitRng(&rng), 0);
  37371. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  37372. if (pkcs7 != NULL) {
  37373. pkcs7->content = env;
  37374. pkcs7->contentSz = (word32)envSz;
  37375. pkcs7->contentOID = DATA;
  37376. pkcs7->privateKey = key;
  37377. pkcs7->privateKeySz = (word32)keySz;
  37378. pkcs7->encryptOID = RSAk;
  37379. pkcs7->hashOID = SHA256h;
  37380. pkcs7->rng = &rng;
  37381. }
  37382. /* Set no certs in bundle for this test. */
  37383. if (pkcs7 != NULL) {
  37384. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  37385. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  37386. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  37387. }
  37388. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  37389. wc_PKCS7_Free(pkcs7);
  37390. pkcs7 = NULL;
  37391. /* check verify fails */
  37392. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37393. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  37394. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz),
  37395. PKCS7_SIGNEEDS_CHECK);
  37396. /* try verifying the signature manually */
  37397. {
  37398. RsaKey rKey;
  37399. word32 idx = 0;
  37400. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  37401. WC_MAX_DIGEST_SIZE];
  37402. int digestSz = 0;
  37403. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  37404. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, (word32)keySz), 0);
  37405. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  37406. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  37407. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  37408. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  37409. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  37410. /* verify was success */
  37411. }
  37412. wc_PKCS7_Free(pkcs7);
  37413. pkcs7 = NULL;
  37414. /* initializing the PKCS7 struct with the signing certificate should pass */
  37415. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37416. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  37417. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  37418. #ifndef NO_PKCS7_STREAM
  37419. wc_PKCS7_Free(pkcs7);
  37420. pkcs7 = NULL;
  37421. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  37422. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  37423. /* test for streaming */
  37424. ret = -1;
  37425. for (z = 0; z < sigSz && ret != 0; z++) {
  37426. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  37427. if (ret < 0){
  37428. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  37429. }
  37430. }
  37431. ExpectIntEQ(ret, 0);
  37432. #endif /* !NO_PKCS7_STREAM */
  37433. wc_PKCS7_Free(pkcs7);
  37434. pkcs7 = NULL;
  37435. /* create valid degenerate bundle */
  37436. sigSz = FOURK_BUF * 2;
  37437. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37438. if (pkcs7 != NULL) {
  37439. pkcs7->content = env;
  37440. pkcs7->contentSz = (word32)envSz;
  37441. pkcs7->contentOID = DATA;
  37442. pkcs7->privateKey = key;
  37443. pkcs7->privateKeySz = (word32)keySz;
  37444. pkcs7->encryptOID = RSAk;
  37445. pkcs7->hashOID = SHA256h;
  37446. pkcs7->rng = &rng;
  37447. }
  37448. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  37449. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  37450. wc_PKCS7_Free(pkcs7);
  37451. pkcs7 = NULL;
  37452. wc_FreeRng(&rng);
  37453. /* check verify */
  37454. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37455. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  37456. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  37457. ExpectNotNull(pkcs7->content);
  37458. #ifndef NO_PKCS7_STREAM
  37459. wc_PKCS7_Free(pkcs7);
  37460. pkcs7 = NULL;
  37461. /* create valid degenerate bundle */
  37462. sigSz = FOURK_BUF * 2;
  37463. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37464. if (pkcs7 != NULL) {
  37465. pkcs7->content = env;
  37466. pkcs7->contentSz = (word32)envSz;
  37467. pkcs7->contentOID = DATA;
  37468. pkcs7->privateKey = key;
  37469. pkcs7->privateKeySz = (word32)keySz;
  37470. pkcs7->encryptOID = RSAk;
  37471. pkcs7->hashOID = SHA256h;
  37472. pkcs7->rng = &rng;
  37473. }
  37474. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  37475. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  37476. wc_PKCS7_Free(pkcs7);
  37477. pkcs7 = NULL;
  37478. wc_FreeRng(&rng);
  37479. /* check verify */
  37480. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37481. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  37482. /* test for streaming */
  37483. ret = -1;
  37484. for (z = 0; z < sigSz && ret != 0; z++) {
  37485. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  37486. if (ret < 0){
  37487. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  37488. }
  37489. }
  37490. ExpectIntEQ(ret, 0);
  37491. #endif /* !NO_PKCS7_STREAM */
  37492. #ifdef HAVE_AES_CBC
  37493. /* check decode */
  37494. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  37495. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, (word32)certSz), 0);
  37496. if (inner != NULL) {
  37497. inner->privateKey = key;
  37498. inner->privateKeySz = (word32)keySz;
  37499. }
  37500. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  37501. pkcs7->contentSz, decoded, (word32)decodedSz)), 0);
  37502. wc_PKCS7_Free(inner);
  37503. inner = NULL;
  37504. #endif
  37505. wc_PKCS7_Free(pkcs7);
  37506. pkcs7 = NULL;
  37507. #ifdef HAVE_AES_CBC
  37508. /* check cert set */
  37509. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37510. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  37511. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, (word32)decodedSz), 0);
  37512. ExpectNotNull(pkcs7->singleCert);
  37513. ExpectIntNE(pkcs7->singleCertSz, 0);
  37514. wc_PKCS7_Free(pkcs7);
  37515. pkcs7 = NULL;
  37516. #ifndef NO_PKCS7_STREAM
  37517. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  37518. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  37519. /* test for streaming */
  37520. ret = -1;
  37521. for (z = 0; z < decodedSz && ret != 0; z++) {
  37522. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  37523. if (ret < 0){
  37524. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  37525. }
  37526. }
  37527. ExpectIntEQ(ret, 0);
  37528. ExpectNotNull(pkcs7->singleCert);
  37529. ExpectIntNE(pkcs7->singleCertSz, 0);
  37530. wc_PKCS7_Free(pkcs7);
  37531. pkcs7 = NULL;
  37532. #endif /* !NO_PKCS7_STREAM */
  37533. #endif
  37534. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  37535. return EXPECT_RESULT();
  37536. }
  37537. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  37538. {
  37539. EXPECT_DECLS;
  37540. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  37541. && !defined(NO_AES)
  37542. PKCS7* pkcs7 = NULL;
  37543. void* heap = NULL;
  37544. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  37545. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  37546. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  37547. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  37548. wc_PKCS7_Free(pkcs7);
  37549. #endif
  37550. return EXPECT_RESULT();
  37551. }
  37552. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  37553. {
  37554. EXPECT_DECLS;
  37555. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  37556. && !defined(NO_AES)
  37557. PKCS7* pkcs7 = NULL;
  37558. void* heap = NULL;
  37559. WOLFSSL_CTX* ctx = NULL;
  37560. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  37561. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  37562. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  37563. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  37564. wc_PKCS7_Free(pkcs7);
  37565. #endif
  37566. return EXPECT_RESULT();
  37567. }
  37568. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  37569. {
  37570. EXPECT_DECLS;
  37571. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  37572. && !defined(NO_AES)
  37573. PKCS7* pkcs7 = NULL;
  37574. void* heap = NULL;
  37575. WOLFSSL_CTX* ctx = NULL;
  37576. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  37577. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  37578. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  37579. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  37580. wc_PKCS7_Free(pkcs7);
  37581. #endif
  37582. return EXPECT_RESULT();
  37583. }
  37584. static int test_wc_PKCS7_DecodeCompressedData(void)
  37585. {
  37586. EXPECT_DECLS;
  37587. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  37588. && !defined(NO_AES) && defined(HAVE_LIBZ)
  37589. PKCS7* pkcs7 = NULL;
  37590. void* heap = NULL;
  37591. byte out[4096];
  37592. byte* decompressed = NULL;
  37593. int outSz;
  37594. int decompressedSz;
  37595. const char* cert = "./certs/client-cert.pem";
  37596. byte* cert_buf = NULL;
  37597. size_t cert_sz = 0;
  37598. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  37599. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  37600. DYNAMIC_TYPE_TMP_BUFFER)));
  37601. decompressedSz = (int)cert_sz;
  37602. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  37603. if (pkcs7 != NULL) {
  37604. pkcs7->content = (byte*)cert_buf;
  37605. pkcs7->contentSz = (word32)cert_sz;
  37606. pkcs7->contentOID = DATA;
  37607. }
  37608. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  37609. sizeof(out))), 0);
  37610. wc_PKCS7_Free(pkcs7);
  37611. pkcs7 = NULL;
  37612. /* compressed key should be smaller than when started */
  37613. ExpectIntLT(outSz, cert_sz);
  37614. /* test decompression */
  37615. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  37616. ExpectIntEQ(pkcs7->contentOID, 0);
  37617. /* fail case with out buffer too small */
  37618. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  37619. decompressed, outSz), 0);
  37620. /* success case */
  37621. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  37622. decompressed, decompressedSz), cert_sz);
  37623. ExpectIntEQ(pkcs7->contentOID, DATA);
  37624. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  37625. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  37626. decompressed = NULL;
  37627. /* test decompression function with different 'max' inputs */
  37628. outSz = sizeof(out);
  37629. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  37630. 0);
  37631. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  37632. out, outSz, 0, heap), 0);
  37633. ExpectNull(decompressed);
  37634. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  37635. out, outSz, 0, heap), 0);
  37636. ExpectNotNull(decompressed);
  37637. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  37638. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  37639. decompressed = NULL;
  37640. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  37641. out, outSz, 0, heap), 0);
  37642. ExpectNotNull(decompressed);
  37643. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  37644. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  37645. if (cert_buf != NULL)
  37646. free(cert_buf);
  37647. wc_PKCS7_Free(pkcs7);
  37648. #endif
  37649. return EXPECT_RESULT();
  37650. }
  37651. static int test_wc_i2d_PKCS12(void)
  37652. {
  37653. EXPECT_DECLS;
  37654. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  37655. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  37656. && !defined(NO_AES) && !defined(NO_SHA)
  37657. WC_PKCS12* pkcs12 = NULL;
  37658. unsigned char der[FOURK_BUF * 2];
  37659. unsigned char* pt;
  37660. int derSz = 0;
  37661. unsigned char out[FOURK_BUF * 2];
  37662. int outSz = FOURK_BUF * 2;
  37663. const char p12_f[] = "./certs/test-servercert.p12";
  37664. XFILE f = XBADFILE;
  37665. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  37666. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  37667. if (f != XBADFILE)
  37668. XFCLOSE(f);
  37669. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  37670. ExpectIntEQ(wc_d2i_PKCS12(der, (word32)derSz, pkcs12), 0);
  37671. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  37672. ExpectIntEQ(outSz, derSz);
  37673. outSz = derSz - 1;
  37674. pt = out;
  37675. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  37676. outSz = derSz;
  37677. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  37678. ExpectIntEQ((pt == out), 0);
  37679. pt = NULL;
  37680. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  37681. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  37682. wc_PKCS12_free(pkcs12);
  37683. pkcs12 = NULL;
  37684. /* Run the same test but use wc_d2i_PKCS12_fp. */
  37685. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  37686. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  37687. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  37688. ExpectIntEQ(outSz, derSz);
  37689. wc_PKCS12_free(pkcs12);
  37690. pkcs12 = NULL;
  37691. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  37692. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  37693. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  37694. ExpectIntEQ(outSz, derSz);
  37695. wc_PKCS12_free(pkcs12);
  37696. pkcs12 = NULL;
  37697. #endif
  37698. return EXPECT_RESULT();
  37699. }
  37700. /* Testing wc_SignatureGetSize() for signature type ECC */
  37701. static int test_wc_SignatureGetSize_ecc(void)
  37702. {
  37703. EXPECT_DECLS;
  37704. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  37705. enum wc_SignatureType sig_type;
  37706. word32 key_len;
  37707. ecc_key ecc;
  37708. const char* qx =
  37709. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  37710. const char* qy =
  37711. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  37712. const char* d =
  37713. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  37714. XMEMSET(&ecc, 0, sizeof(ecc_key));
  37715. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  37716. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  37717. /* Input for signature type ECC */
  37718. sig_type = WC_SIGNATURE_TYPE_ECC;
  37719. key_len = sizeof(ecc_key);
  37720. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  37721. /* Test bad args */
  37722. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  37723. sig_type = (enum wc_SignatureType) 100;
  37724. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  37725. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  37726. sig_type = WC_SIGNATURE_TYPE_ECC;
  37727. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  37728. key_len = (word32)0;
  37729. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  37730. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  37731. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  37732. return EXPECT_RESULT();
  37733. } /* END test_wc_SignatureGetSize_ecc() */
  37734. /* Testing wc_SignatureGetSize() for signature type rsa */
  37735. static int test_wc_SignatureGetSize_rsa(void)
  37736. {
  37737. EXPECT_DECLS;
  37738. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  37739. enum wc_SignatureType sig_type;
  37740. word32 key_len;
  37741. word32 idx = 0;
  37742. RsaKey rsa_key;
  37743. byte* tmp = NULL;
  37744. size_t bytes;
  37745. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  37746. #ifdef USE_CERT_BUFFERS_1024
  37747. bytes = (size_t)sizeof_client_key_der_1024;
  37748. if (bytes < (size_t)sizeof_client_key_der_1024)
  37749. bytes = (size_t)sizeof_client_cert_der_1024;
  37750. #elif defined(USE_CERT_BUFFERS_2048)
  37751. bytes = (size_t)sizeof_client_key_der_2048;
  37752. if (bytes < (size_t)sizeof_client_cert_der_2048)
  37753. bytes = (size_t)sizeof_client_cert_der_2048;
  37754. #else
  37755. bytes = FOURK_BUF;
  37756. #endif
  37757. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  37758. DYNAMIC_TYPE_TMP_BUFFER));
  37759. if (tmp != NULL) {
  37760. #ifdef USE_CERT_BUFFERS_1024
  37761. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  37762. #elif defined(USE_CERT_BUFFERS_2048)
  37763. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  37764. #elif !defined(NO_FILESYSTEM)
  37765. XFILE file = XBADFILE;
  37766. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  37767. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  37768. if (file != XBADFILE)
  37769. XFCLOSE(file);
  37770. }
  37771. #else
  37772. ExpectFail();
  37773. #endif
  37774. }
  37775. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  37776. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  37777. /* Input for signature type RSA */
  37778. sig_type = WC_SIGNATURE_TYPE_RSA;
  37779. key_len = sizeof(RsaKey);
  37780. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  37781. /* Test bad args */
  37782. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  37783. sig_type = (enum wc_SignatureType)100;
  37784. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  37785. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  37786. sig_type = WC_SIGNATURE_TYPE_RSA;
  37787. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  37788. key_len = (word32)0;
  37789. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  37790. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  37791. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37792. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  37793. return EXPECT_RESULT();
  37794. } /* END test_wc_SignatureGetSize_rsa(void) */
  37795. /*----------------------------------------------------------------------------*
  37796. | hash.h Tests
  37797. *----------------------------------------------------------------------------*/
  37798. static int test_wc_HashInit(void)
  37799. {
  37800. EXPECT_DECLS;
  37801. int i; /* 0 indicates tests passed, 1 indicates failure */
  37802. wc_HashAlg hash;
  37803. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  37804. enum wc_HashType enumArray[] = {
  37805. #ifndef NO_MD5
  37806. WC_HASH_TYPE_MD5,
  37807. #endif
  37808. #ifndef NO_SHA
  37809. WC_HASH_TYPE_SHA,
  37810. #endif
  37811. #ifdef WOLFSSL_SHA224
  37812. WC_HASH_TYPE_SHA224,
  37813. #endif
  37814. #ifndef NO_SHA256
  37815. WC_HASH_TYPE_SHA256,
  37816. #endif
  37817. #ifdef WOLFSSL_SHA384
  37818. WC_HASH_TYPE_SHA384,
  37819. #endif
  37820. #ifdef WOLFSSL_SHA512
  37821. WC_HASH_TYPE_SHA512,
  37822. #endif
  37823. };
  37824. /* dynamically finds the length */
  37825. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  37826. /* For loop to test various arguments... */
  37827. for (i = 0; i < enumlen; i++) {
  37828. /* check for bad args */
  37829. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  37830. wc_HashFree(&hash, enumArray[i]);
  37831. /* check for null ptr */
  37832. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  37833. } /* end of for loop */
  37834. return EXPECT_RESULT();
  37835. } /* end of test_wc_HashInit */
  37836. /*
  37837. * Unit test function for wc_HashSetFlags()
  37838. */
  37839. static int test_wc_HashSetFlags(void)
  37840. {
  37841. EXPECT_DECLS;
  37842. #ifdef WOLFSSL_HASH_FLAGS
  37843. wc_HashAlg hash;
  37844. word32 flags = 0;
  37845. int i, j;
  37846. int notSupportedLen;
  37847. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  37848. enum wc_HashType enumArray[] = {
  37849. #ifndef NO_MD5
  37850. WC_HASH_TYPE_MD5,
  37851. #endif
  37852. #ifndef NO_SHA
  37853. WC_HASH_TYPE_SHA,
  37854. #endif
  37855. #ifdef WOLFSSL_SHA224
  37856. WC_HASH_TYPE_SHA224,
  37857. #endif
  37858. #ifndef NO_SHA256
  37859. WC_HASH_TYPE_SHA256,
  37860. #endif
  37861. #ifdef WOLFSSL_SHA384
  37862. WC_HASH_TYPE_SHA384,
  37863. #endif
  37864. #ifdef WOLFSSL_SHA512
  37865. WC_HASH_TYPE_SHA512,
  37866. #endif
  37867. #ifdef WOLFSSL_SHA3
  37868. WC_HASH_TYPE_SHA3_224,
  37869. #endif
  37870. };
  37871. enum wc_HashType notSupported[] = {
  37872. WC_HASH_TYPE_MD5_SHA,
  37873. WC_HASH_TYPE_MD2,
  37874. WC_HASH_TYPE_MD4,
  37875. WC_HASH_TYPE_BLAKE2B,
  37876. WC_HASH_TYPE_BLAKE2S,
  37877. WC_HASH_TYPE_NONE,
  37878. };
  37879. /* dynamically finds the length */
  37880. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  37881. /* For loop to test various arguments... */
  37882. for (i = 0; i < enumlen; i++) {
  37883. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  37884. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  37885. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  37886. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  37887. wc_HashFree(&hash, enumArray[i]);
  37888. }
  37889. /* For loop to test not supported cases */
  37890. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  37891. for (j = 0; j < notSupportedLen; j++) {
  37892. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  37893. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  37894. BAD_FUNC_ARG);
  37895. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  37896. }
  37897. #endif
  37898. return EXPECT_RESULT();
  37899. } /* END test_wc_HashSetFlags */
  37900. /*
  37901. * Unit test function for wc_HashGetFlags()
  37902. */
  37903. static int test_wc_HashGetFlags(void)
  37904. {
  37905. EXPECT_DECLS;
  37906. #ifdef WOLFSSL_HASH_FLAGS
  37907. wc_HashAlg hash;
  37908. word32 flags = 0;
  37909. int i, j;
  37910. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  37911. enum wc_HashType enumArray[] = {
  37912. #ifndef NO_MD5
  37913. WC_HASH_TYPE_MD5,
  37914. #endif
  37915. #ifndef NO_SHA
  37916. WC_HASH_TYPE_SHA,
  37917. #endif
  37918. #ifdef WOLFSSL_SHA224
  37919. WC_HASH_TYPE_SHA224,
  37920. #endif
  37921. #ifndef NO_SHA256
  37922. WC_HASH_TYPE_SHA256,
  37923. #endif
  37924. #ifdef WOLFSSL_SHA384
  37925. WC_HASH_TYPE_SHA384,
  37926. #endif
  37927. #ifdef WOLFSSL_SHA512
  37928. WC_HASH_TYPE_SHA512,
  37929. #endif
  37930. #ifdef WOLFSSL_SHA3
  37931. WC_HASH_TYPE_SHA3_224,
  37932. #endif
  37933. };
  37934. enum wc_HashType notSupported[] = {
  37935. WC_HASH_TYPE_MD5_SHA,
  37936. WC_HASH_TYPE_MD2,
  37937. WC_HASH_TYPE_MD4,
  37938. WC_HASH_TYPE_BLAKE2B,
  37939. WC_HASH_TYPE_BLAKE2S,
  37940. WC_HASH_TYPE_NONE,
  37941. };
  37942. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  37943. int notSupportedLen;
  37944. /* For loop to test various arguments... */
  37945. for (i = 0; i < enumlen; i++) {
  37946. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  37947. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  37948. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  37949. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  37950. wc_HashFree(&hash, enumArray[i]);
  37951. }
  37952. /* For loop to test not supported cases */
  37953. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  37954. for (j = 0; j < notSupportedLen; j++) {
  37955. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  37956. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  37957. BAD_FUNC_ARG);
  37958. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  37959. }
  37960. #endif
  37961. return EXPECT_RESULT();
  37962. } /* END test_wc_HashGetFlags */
  37963. /*----------------------------------------------------------------------------*
  37964. | Compatibility Tests
  37965. *----------------------------------------------------------------------------*/
  37966. /*----------------------------------------------------------------------------*
  37967. | ASN.1 Tests
  37968. *----------------------------------------------------------------------------*/
  37969. static int test_wolfSSL_ASN1_BIT_STRING(void)
  37970. {
  37971. EXPECT_DECLS;
  37972. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  37973. ASN1_BIT_STRING* str = NULL;
  37974. ExpectNotNull(str = ASN1_BIT_STRING_new());
  37975. /* Empty data testing. */
  37976. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  37977. ASN1_BIT_STRING_free(str);
  37978. str = NULL;
  37979. ExpectNotNull(str = ASN1_BIT_STRING_new());
  37980. /* Invalid parameter testing. */
  37981. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  37982. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  37983. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  37984. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  37985. /* No bit string - bit is always 0. */
  37986. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  37987. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  37988. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  37989. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  37990. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  37991. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  37992. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  37993. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  37994. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  37995. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  37996. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  37997. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  37998. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  37999. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  38000. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  38001. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  38002. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  38003. ASN1_BIT_STRING_free(str);
  38004. ASN1_BIT_STRING_free(NULL);
  38005. #endif
  38006. return EXPECT_RESULT();
  38007. }
  38008. static int test_wolfSSL_ASN1_INTEGER(void)
  38009. {
  38010. EXPECT_DECLS;
  38011. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  38012. ASN1_INTEGER* a = NULL;
  38013. ASN1_INTEGER* dup = NULL;
  38014. const unsigned char invalidLenDer[] = {
  38015. 0x02, 0x20, 0x00
  38016. };
  38017. const unsigned char longDer[] = {
  38018. 0x02, 0x20,
  38019. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38020. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38021. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38022. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  38023. };
  38024. const unsigned char* p;
  38025. /* Invalid parameter testing. */
  38026. ASN1_INTEGER_free(NULL);
  38027. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  38028. ExpectNotNull(a = ASN1_INTEGER_new());
  38029. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  38030. ASN1_INTEGER_free(dup);
  38031. dup = NULL;
  38032. ASN1_INTEGER_free(a);
  38033. a = NULL;
  38034. p = longDer;
  38035. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  38036. p = longDer;
  38037. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  38038. ExpectPtrNE(p, longDer);
  38039. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  38040. ASN1_INTEGER_free(dup);
  38041. ASN1_INTEGER_free(a);
  38042. #endif
  38043. return EXPECT_RESULT();
  38044. }
  38045. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  38046. {
  38047. EXPECT_DECLS;
  38048. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  38049. ASN1_INTEGER* a = NULL;
  38050. ASN1_INTEGER* b = NULL;
  38051. ExpectNotNull(a = ASN1_INTEGER_new());
  38052. ExpectNotNull(b = ASN1_INTEGER_new());
  38053. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  38054. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  38055. /* Invalid parameter testing. */
  38056. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  38057. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  38058. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  38059. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  38060. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  38061. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  38062. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  38063. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  38064. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  38065. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  38066. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  38067. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  38068. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  38069. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  38070. ASN1_INTEGER_free(b);
  38071. ASN1_INTEGER_free(a);
  38072. #endif
  38073. return EXPECT_RESULT();
  38074. }
  38075. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  38076. {
  38077. EXPECT_DECLS;
  38078. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  38079. ASN1_INTEGER* ai = NULL;
  38080. ASN1_INTEGER* ai2 = NULL;
  38081. BIGNUM* bn = NULL;
  38082. BIGNUM* bn2 = NULL;
  38083. ExpectNotNull(ai = ASN1_INTEGER_new());
  38084. ExpectNotNull(bn2 = BN_new());
  38085. /* Invalid parameter testing. */
  38086. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  38087. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  38088. /* at the moment hard setting since no set function */
  38089. if (ai != NULL) {
  38090. ai->data[0] = 0xff; /* No DER encoding. */
  38091. ai->length = 1;
  38092. }
  38093. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  38094. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  38095. BN_free(bn);
  38096. bn = NULL;
  38097. #else
  38098. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  38099. #endif
  38100. if (ai != NULL) {
  38101. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  38102. ai->data[1] = 0x04; /* bad length of integer */
  38103. ai->data[2] = 0x03;
  38104. ai->length = 3;
  38105. }
  38106. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  38107. /* Interpreted as a number 0x020403. */
  38108. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  38109. BN_free(bn);
  38110. bn = NULL;
  38111. #else
  38112. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  38113. #endif
  38114. if (ai != NULL) {
  38115. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  38116. ai->data[1] = 0x01; /* length of integer */
  38117. ai->data[2] = 0x03;
  38118. ai->length = 3;
  38119. }
  38120. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  38121. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  38122. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  38123. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  38124. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  38125. if (ai != NULL) {
  38126. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  38127. ai->data[1] = 0x02; /* length of integer */
  38128. ai->data[2] = 0x00; /* padding byte to ensure positive */
  38129. ai->data[3] = 0xff;
  38130. ai->length = 4;
  38131. }
  38132. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  38133. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  38134. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  38135. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  38136. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  38137. if (ai != NULL) {
  38138. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  38139. ai->data[1] = 0x01; /* length of integer */
  38140. ai->data[2] = 0x00;
  38141. ai->length = 3;
  38142. }
  38143. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  38144. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  38145. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  38146. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  38147. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  38148. if (ai != NULL) {
  38149. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  38150. ai->data[1] = 0x01; /* length of integer */
  38151. ai->data[2] = 0x01;
  38152. ai->length = 3;
  38153. ai->negative = 1;
  38154. }
  38155. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  38156. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  38157. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  38158. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  38159. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  38160. BN_free(bn2);
  38161. BN_free(bn);
  38162. ASN1_INTEGER_free(ai2);
  38163. ASN1_INTEGER_free(ai);
  38164. #endif
  38165. return EXPECT_RESULT();
  38166. }
  38167. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  38168. {
  38169. EXPECT_DECLS;
  38170. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  38171. ASN1_INTEGER *a = NULL;
  38172. long val;
  38173. ExpectNotNull(a = ASN1_INTEGER_new());
  38174. /* Invalid parameter testing. */
  38175. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  38176. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  38177. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  38178. #else
  38179. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  38180. #endif
  38181. ASN1_INTEGER_free(a);
  38182. a = NULL;
  38183. ExpectNotNull(a = ASN1_INTEGER_new());
  38184. val = 0;
  38185. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  38186. ASN1_INTEGER_free(a);
  38187. a = NULL;
  38188. /* 0 */
  38189. ExpectNotNull(a = ASN1_INTEGER_new());
  38190. val = 0;
  38191. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  38192. ExpectTrue(ASN1_INTEGER_get(a) == val);
  38193. ASN1_INTEGER_free(a);
  38194. a = NULL;
  38195. /* 40 */
  38196. ExpectNotNull(a = ASN1_INTEGER_new());
  38197. val = 40;
  38198. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  38199. ExpectTrue(ASN1_INTEGER_get(a) == val);
  38200. ASN1_INTEGER_free(a);
  38201. a = NULL;
  38202. /* -40 */
  38203. ExpectNotNull(a = ASN1_INTEGER_new());
  38204. val = -40;
  38205. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  38206. ExpectTrue(ASN1_INTEGER_get(a) == val);
  38207. ASN1_INTEGER_free(a);
  38208. a = NULL;
  38209. /* 128 */
  38210. ExpectNotNull(a = ASN1_INTEGER_new());
  38211. val = 128;
  38212. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  38213. ExpectTrue(ASN1_INTEGER_get(a) == val);
  38214. ASN1_INTEGER_free(a);
  38215. a = NULL;
  38216. /* -128 */
  38217. ExpectNotNull(a = ASN1_INTEGER_new());
  38218. val = -128;
  38219. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  38220. ExpectTrue(ASN1_INTEGER_get(a) == val);
  38221. ASN1_INTEGER_free(a);
  38222. a = NULL;
  38223. /* 200 */
  38224. ExpectNotNull(a = ASN1_INTEGER_new());
  38225. val = 200;
  38226. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  38227. ExpectTrue(ASN1_INTEGER_get(a) == val);
  38228. ASN1_INTEGER_free(a);
  38229. a = NULL;
  38230. /* int max (2147483647) */
  38231. ExpectNotNull(a = ASN1_INTEGER_new());
  38232. val = 2147483647;
  38233. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  38234. ExpectTrue(ASN1_INTEGER_get(a) == val);
  38235. ASN1_INTEGER_free(a);
  38236. a = NULL;
  38237. /* int min (-2147483648) */
  38238. ExpectNotNull(a = ASN1_INTEGER_new());
  38239. val = -2147483647 - 1;
  38240. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  38241. ExpectTrue(ASN1_INTEGER_get(a) == val);
  38242. ASN1_INTEGER_free(a);
  38243. a = NULL;
  38244. /* long max positive */
  38245. ExpectNotNull(a = ASN1_INTEGER_new());
  38246. val = (long)(((unsigned long)-1) >> 1);
  38247. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  38248. ExpectTrue(ASN1_INTEGER_get(a) == val);
  38249. ASN1_INTEGER_free(a);
  38250. #endif
  38251. return EXPECT_RESULT();
  38252. }
  38253. #if defined(OPENSSL_EXTRA)
  38254. typedef struct ASN1IntTestVector {
  38255. const byte* der;
  38256. const size_t derSz;
  38257. const long value;
  38258. } ASN1IntTestVector;
  38259. #endif
  38260. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  38261. {
  38262. EXPECT_DECLS;
  38263. #if defined(OPENSSL_EXTRA)
  38264. size_t i;
  38265. WOLFSSL_ASN1_INTEGER* a = NULL;
  38266. WOLFSSL_ASN1_INTEGER* b = NULL;
  38267. WOLFSSL_ASN1_INTEGER* c = NULL;
  38268. const byte* p = NULL;
  38269. byte* p2 = NULL;
  38270. byte* reEncoded = NULL;
  38271. int reEncodedSz = 0;
  38272. static const byte zeroDer[] = {
  38273. 0x02, 0x01, 0x00
  38274. };
  38275. static const byte oneDer[] = {
  38276. 0x02, 0x01, 0x01
  38277. };
  38278. static const byte negativeDer[] = {
  38279. 0x02, 0x03, 0xC1, 0x16, 0x0D
  38280. };
  38281. static const byte positiveDer[] = {
  38282. 0x02, 0x03, 0x01, 0x00, 0x01
  38283. };
  38284. static const byte primeDer[] = {
  38285. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  38286. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  38287. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  38288. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  38289. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  38290. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  38291. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  38292. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  38293. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  38294. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  38295. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  38296. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  38297. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  38298. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  38299. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  38300. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  38301. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  38302. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  38303. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  38304. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  38305. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  38306. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  38307. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  38308. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  38309. };
  38310. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  38311. static const ASN1IntTestVector testVectors[] = {
  38312. {zeroDer, sizeof(zeroDer), 0},
  38313. {oneDer, sizeof(oneDer), 1},
  38314. {negativeDer, sizeof(negativeDer), -4123123},
  38315. {positiveDer, sizeof(positiveDer), 65537},
  38316. {primeDer, sizeof(primeDer), 0}
  38317. };
  38318. static const size_t NUM_TEST_VECTORS =
  38319. sizeof(testVectors)/sizeof(testVectors[0]);
  38320. /* Check d2i error conditions */
  38321. /* NULL pointer to input. */
  38322. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  38323. ExpectNull(b);
  38324. /* NULL input. */
  38325. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  38326. ExpectNull(b);
  38327. /* 0 length. */
  38328. p = testVectors[0].der;
  38329. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  38330. ExpectNull(b);
  38331. /* Negative length. */
  38332. p = testVectors[0].der;
  38333. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  38334. ExpectNull(b);
  38335. /* Garbage DER input. */
  38336. p = garbageDer;
  38337. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  38338. ExpectNull(b);
  38339. /* Check i2d error conditions */
  38340. /* NULL input. */
  38341. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  38342. /* 0 length input data buffer (a->length == 0). */
  38343. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  38344. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  38345. if (a != NULL)
  38346. a->data = NULL;
  38347. /* NULL input data buffer. */
  38348. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  38349. if (a != NULL) {
  38350. /* Reset a->data. */
  38351. a->data = a->intData;
  38352. }
  38353. /* Set a to valid value. */
  38354. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  38355. /* NULL output buffer. */
  38356. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  38357. wolfSSL_ASN1_INTEGER_free(a);
  38358. a = NULL;
  38359. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  38360. p = testVectors[i].der;
  38361. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  38362. testVectors[i].derSz));
  38363. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  38364. if (testVectors[i].derSz <= sizeof(long)) {
  38365. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  38366. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  38367. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  38368. wolfSSL_ASN1_INTEGER_free(c);
  38369. c = NULL;
  38370. }
  38371. /* Convert to DER without a pre-allocated output buffer. */
  38372. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  38373. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  38374. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  38375. /* Convert to DER with a pre-allocated output buffer. In this case, the
  38376. * output buffer pointer should be incremented just past the end of the
  38377. * encoded data. */
  38378. p2 = reEncoded;
  38379. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  38380. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  38381. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  38382. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  38383. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  38384. reEncoded = NULL;
  38385. wolfSSL_ASN1_INTEGER_free(a);
  38386. a = NULL;
  38387. }
  38388. #endif /* OPENSSL_EXTRA */
  38389. return EXPECT_RESULT();
  38390. }
  38391. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  38392. {
  38393. EXPECT_DECLS;
  38394. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  38395. BIO* bio = NULL;
  38396. BIO* out = NULL;
  38397. BIO* fixed = NULL;
  38398. ASN1_INTEGER* ai = NULL;
  38399. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  38400. char tmp[1024];
  38401. int tmpSz;
  38402. const char expected1[] = "123456";
  38403. const char expected2[] = "112345678912345678901234567890";
  38404. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  38405. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  38406. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  38407. ExpectNotNull(ai = ASN1_INTEGER_new());
  38408. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  38409. /* Invalid parameter testing. */
  38410. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  38411. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  38412. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  38413. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  38414. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  38415. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  38416. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  38417. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  38418. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  38419. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  38420. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  38421. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  38422. /* No data to read from BIO. */
  38423. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  38424. /* read first line */
  38425. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  38426. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  38427. XMEMSET(tmp, 0, 1024);
  38428. tmpSz = BIO_read(out, tmp, 1024);
  38429. ExpectIntEQ(tmpSz, 6);
  38430. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  38431. /* fail on second line (not % 2) */
  38432. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  38433. /* read 3rd long line */
  38434. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  38435. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  38436. XMEMSET(tmp, 0, 1024);
  38437. tmpSz = BIO_read(out, tmp, 1024);
  38438. ExpectIntEQ(tmpSz, 30);
  38439. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  38440. /* fail on empty line */
  38441. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  38442. BIO_free(bio);
  38443. bio = NULL;
  38444. /* Make long integer, requiring dynamic memory, even longer. */
  38445. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  38446. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  38447. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  38448. XMEMSET(tmp, 0, 1024);
  38449. tmpSz = BIO_read(out, tmp, 1024);
  38450. ExpectIntEQ(tmpSz, 48);
  38451. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  38452. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  38453. XMEMSET(tmp, 0, 1024);
  38454. tmpSz = BIO_read(out, tmp, 1024);
  38455. ExpectIntEQ(tmpSz, 56);
  38456. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  38457. BIO_free(bio);
  38458. BIO_free(out);
  38459. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  38460. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  38461. /* Ensure there is 0 bytes available to write into. */
  38462. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  38463. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  38464. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  38465. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  38466. BIO_free(fixed);
  38467. ASN1_INTEGER_free(ai);
  38468. #endif
  38469. return EXPECT_RESULT();
  38470. }
  38471. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  38472. {
  38473. EXPECT_DECLS;
  38474. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  38475. ASN1_INTEGER *a = NULL;
  38476. unsigned char *pp = NULL,*tpp = NULL;
  38477. int ret = 0;
  38478. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  38479. /* Invalid parameter testing. */
  38480. /* Set pp to an invalid value. */
  38481. pp = NULL;
  38482. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  38483. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  38484. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  38485. /* 40 */
  38486. if (a != NULL) {
  38487. a->intData[0] = ASN_INTEGER;
  38488. a->intData[1] = 1;
  38489. a->intData[2] = 40;
  38490. }
  38491. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  38492. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  38493. DYNAMIC_TYPE_TMP_BUFFER));
  38494. tpp = pp;
  38495. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  38496. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  38497. tpp--;
  38498. ExpectIntEQ(*tpp, 40);
  38499. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38500. pp = NULL;
  38501. /* 128 */
  38502. if (a != NULL) {
  38503. a->intData[0] = ASN_INTEGER;
  38504. a->intData[1] = 1;
  38505. a->intData[2] = 128;
  38506. }
  38507. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  38508. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  38509. DYNAMIC_TYPE_TMP_BUFFER));
  38510. tpp = pp;
  38511. if (tpp != NULL) {
  38512. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  38513. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  38514. tpp--;
  38515. ExpectIntEQ(*(tpp--), 128);
  38516. ExpectIntEQ(*tpp, 0);
  38517. }
  38518. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38519. pp = NULL;
  38520. /* -40 */
  38521. if (a != NULL) {
  38522. a->intData[0] = ASN_INTEGER;
  38523. a->intData[1] = 1;
  38524. a->intData[2] = 40;
  38525. a->negative = 1;
  38526. }
  38527. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  38528. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  38529. DYNAMIC_TYPE_TMP_BUFFER));
  38530. tpp = pp;
  38531. if (tpp != NULL) {
  38532. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  38533. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  38534. tpp--;
  38535. ExpectIntEQ(*tpp, 216);
  38536. }
  38537. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38538. pp = NULL;
  38539. /* -128 */
  38540. if (a != NULL) {
  38541. a->intData[0] = ASN_INTEGER;
  38542. a->intData[1] = 1;
  38543. a->intData[2] = 128;
  38544. a->negative = 1;
  38545. }
  38546. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  38547. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  38548. DYNAMIC_TYPE_TMP_BUFFER));
  38549. tpp = pp;
  38550. if (tpp != NULL) {
  38551. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  38552. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  38553. tpp--;
  38554. ExpectIntEQ(*tpp, 128);
  38555. }
  38556. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38557. pp = NULL;
  38558. /* -200 */
  38559. if (a != NULL) {
  38560. a->intData[0] = ASN_INTEGER;
  38561. a->intData[1] = 1;
  38562. a->intData[2] = 200;
  38563. a->negative = 1;
  38564. }
  38565. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  38566. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  38567. DYNAMIC_TYPE_TMP_BUFFER));
  38568. tpp = pp;
  38569. if (tpp != NULL) {
  38570. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  38571. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  38572. tpp--;
  38573. ExpectIntEQ(*(tpp--), 56);
  38574. ExpectIntEQ(*tpp, 255);
  38575. }
  38576. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38577. pp = NULL;
  38578. /* Empty */
  38579. if (a != NULL) {
  38580. a->intData[0] = ASN_INTEGER;
  38581. a->intData[1] = 0;
  38582. a->negative = 0;
  38583. }
  38584. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  38585. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  38586. DYNAMIC_TYPE_TMP_BUFFER));
  38587. tpp = pp;
  38588. if (tpp != NULL) {
  38589. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  38590. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  38591. tpp--;
  38592. ExpectIntEQ(*tpp, 0);
  38593. }
  38594. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38595. pp = NULL;
  38596. /* 0 */
  38597. if (a != NULL) {
  38598. a->intData[0] = ASN_INTEGER;
  38599. a->intData[1] = 1;
  38600. a->intData[2] = 0;
  38601. a->negative = 1;
  38602. }
  38603. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  38604. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  38605. DYNAMIC_TYPE_TMP_BUFFER));
  38606. if (tpp != NULL) {
  38607. tpp = pp;
  38608. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  38609. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  38610. tpp--;
  38611. ExpectIntEQ(*tpp, 0);
  38612. }
  38613. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38614. pp = NULL;
  38615. /* 0x100 */
  38616. if (a != NULL) {
  38617. a->intData[0] = ASN_INTEGER;
  38618. a->intData[1] = 2;
  38619. a->intData[2] = 0x01;
  38620. a->intData[3] = 0x00;
  38621. a->negative = 0;
  38622. }
  38623. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  38624. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  38625. DYNAMIC_TYPE_TMP_BUFFER));
  38626. if (tpp != NULL) {
  38627. tpp = pp;
  38628. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  38629. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  38630. tpp -= 2;
  38631. ExpectIntEQ(tpp[0], 0x01);
  38632. ExpectIntEQ(tpp[1], 0x00);
  38633. }
  38634. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38635. pp = NULL;
  38636. /* -0x8000 => 0x8000 */
  38637. if (a != NULL) {
  38638. a->intData[0] = ASN_INTEGER;
  38639. a->intData[1] = 2;
  38640. a->intData[2] = 0x80;
  38641. a->intData[3] = 0x00;
  38642. a->negative = 1;
  38643. }
  38644. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  38645. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  38646. DYNAMIC_TYPE_TMP_BUFFER));
  38647. tpp = pp;
  38648. if (tpp != NULL) {
  38649. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  38650. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  38651. tpp -= 2;
  38652. ExpectIntEQ(tpp[0], 0x80);
  38653. ExpectIntEQ(tpp[1], 0x00);
  38654. }
  38655. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38656. pp = NULL;
  38657. /* -0x8001 => 0xFF7FFF */
  38658. if (a != NULL) {
  38659. a->intData[0] = ASN_INTEGER;
  38660. a->intData[1] = 2;
  38661. a->intData[2] = 0x80;
  38662. a->intData[3] = 0x01;
  38663. a->negative = 1;
  38664. }
  38665. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  38666. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  38667. DYNAMIC_TYPE_TMP_BUFFER));
  38668. tpp = pp;
  38669. if (tpp != NULL) {
  38670. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  38671. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  38672. tpp -= 3;
  38673. ExpectIntEQ(tpp[0], 0xFF);
  38674. ExpectIntEQ(tpp[1], 0x7F);
  38675. ExpectIntEQ(tpp[2], 0xFF);
  38676. }
  38677. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38678. wolfSSL_ASN1_INTEGER_free(a);
  38679. #endif /* OPENSSL_EXTRA && !NO_ASN */
  38680. return EXPECT_RESULT();
  38681. }
  38682. static int test_wolfSSL_ASN1_OBJECT(void)
  38683. {
  38684. EXPECT_DECLS;
  38685. #if defined(OPENSSL_EXTRA)
  38686. ASN1_OBJECT* a = NULL;
  38687. ASN1_OBJECT s;
  38688. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  38689. /* Invalid parameter testing. */
  38690. ASN1_OBJECT_free(NULL);
  38691. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  38692. /* Test that a static ASN1_OBJECT can be freed. */
  38693. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  38694. ASN1_OBJECT_free(&s);
  38695. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  38696. ASN1_OBJECT_free(a);
  38697. a = NULL;
  38698. s.obj = der;
  38699. s.objSz = sizeof(der);
  38700. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  38701. ASN1_OBJECT_free(a);
  38702. ASN1_OBJECT_free(&s);
  38703. #endif /* OPENSSL_EXTRA */
  38704. return EXPECT_RESULT();
  38705. }
  38706. static int test_wolfSSL_ASN1_get_object(void)
  38707. {
  38708. EXPECT_DECLS;
  38709. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  38710. const unsigned char* derBuf = cliecc_cert_der_256;
  38711. const unsigned char* nullPtr = NULL;
  38712. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  38713. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  38714. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  38715. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  38716. const unsigned char* p;
  38717. unsigned char objDer[8];
  38718. unsigned char* der;
  38719. unsigned char* derPtr;
  38720. int len = sizeof_cliecc_cert_der_256;
  38721. long asnLen = 0;
  38722. int tag = 0;
  38723. int cls = 0;
  38724. ASN1_OBJECT* a = NULL;
  38725. ASN1_OBJECT s;
  38726. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  38727. /* Invalid encoding at length. */
  38728. p = objDerInvalidLen;
  38729. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  38730. 0x80);
  38731. p = objDerBadLen;
  38732. /* Error = 0x80, Constructed = 0x20 */
  38733. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  38734. 0x80 | 0x20);
  38735. /* Read a couple TLV triplets and make sure they match the expected values
  38736. */
  38737. /* SEQUENCE */
  38738. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  38739. ExpectIntEQ(asnLen, 861);
  38740. ExpectIntEQ(tag, 0x10);
  38741. ExpectIntEQ(cls, 0);
  38742. /* SEQUENCE */
  38743. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  38744. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  38745. ExpectIntEQ(asnLen, 772);
  38746. ExpectIntEQ(tag, 0x10);
  38747. ExpectIntEQ(cls, 0);
  38748. /* [0] */
  38749. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  38750. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  38751. ExpectIntEQ(asnLen, 3);
  38752. ExpectIntEQ(tag, 0);
  38753. ExpectIntEQ(cls, 0x80);
  38754. /* INTEGER */
  38755. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  38756. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  38757. ExpectIntEQ(asnLen, 1);
  38758. ExpectIntEQ(tag, 0x2);
  38759. ExpectIntEQ(cls, 0);
  38760. derBuf += asnLen;
  38761. /* INTEGER */
  38762. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  38763. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  38764. ExpectIntEQ(asnLen, 20);
  38765. ExpectIntEQ(tag, 0x2);
  38766. ExpectIntEQ(cls, 0);
  38767. derBuf += asnLen;
  38768. /* SEQUENCE */
  38769. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  38770. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  38771. ExpectIntEQ(asnLen, 10);
  38772. ExpectIntEQ(tag, 0x10);
  38773. ExpectIntEQ(cls, 0);
  38774. /* Found OBJECT_ID. */
  38775. /* Invalid parameter testing. */
  38776. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  38777. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  38778. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  38779. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  38780. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  38781. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  38782. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  38783. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  38784. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  38785. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  38786. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  38787. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  38788. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  38789. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  38790. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  38791. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  38792. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  38793. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  38794. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  38795. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  38796. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  38797. /* Invalid encoding at length. */
  38798. p = objDerInvalidLen;
  38799. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  38800. p = objDerBadLen;
  38801. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  38802. p = objDerNotObj;
  38803. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  38804. p = objDerNoData;
  38805. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  38806. /* Create an ASN OBJECT from content */
  38807. p = derBuf + 2;
  38808. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  38809. ASN1_OBJECT_free(a);
  38810. a = NULL;
  38811. /* Create an ASN OBJECT from DER */
  38812. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  38813. /* Invalid parameter testing. */
  38814. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  38815. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  38816. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  38817. der = NULL;
  38818. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  38819. derPtr = objDer;
  38820. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  38821. ExpectPtrNE(derPtr, objDer);
  38822. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  38823. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  38824. ASN1_OBJECT_free(a);
  38825. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  38826. return EXPECT_RESULT();
  38827. }
  38828. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  38829. {
  38830. EXPECT_DECLS;
  38831. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  38832. ASN1_OBJECT* obj = NULL;
  38833. ASN1_OBJECT* a = NULL;
  38834. BIO *bio = NULL;
  38835. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  38836. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  38837. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  38838. const unsigned char* p;
  38839. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  38840. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  38841. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  38842. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  38843. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  38844. /* No DER encoding in ASN1_OBJECT. */
  38845. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  38846. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  38847. ASN1_OBJECT_free(a);
  38848. a = NULL;
  38849. /* DER encoding - not OBJECT_ID */
  38850. p = notObjDer;
  38851. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  38852. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  38853. ASN1_OBJECT_free(a);
  38854. a = NULL;
  38855. /* Bad length encoding. */
  38856. p = badLenDer;
  38857. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  38858. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  38859. ASN1_OBJECT_free(a);
  38860. a = NULL;
  38861. /* Good encoding - but unknown. */
  38862. p = goodDer;
  38863. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  38864. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  38865. ASN1_OBJECT_free(a);
  38866. BIO_free(bio);
  38867. ASN1_OBJECT_free(obj);
  38868. #endif
  38869. return EXPECT_RESULT();
  38870. }
  38871. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  38872. {
  38873. EXPECT_DECLS;
  38874. #if defined(OPENSSL_EXTRA) && \
  38875. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  38876. char buf[50] = {0};
  38877. ASN1_OBJECT* obj;
  38878. const char* oid = "2.5.29.19";
  38879. const char* ln = "X509v3 Basic Constraints";
  38880. obj = NULL;
  38881. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  38882. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  38883. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  38884. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  38885. XMEMSET(buf, 0, sizeof(buf));
  38886. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  38887. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  38888. ASN1_OBJECT_free(obj);
  38889. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  38890. return EXPECT_RESULT();
  38891. }
  38892. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  38893. {
  38894. EXPECT_DECLS;
  38895. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  38896. WOLFSSL_STACK* sk = NULL;
  38897. WOLFSSL_ASN1_OBJECT* obj;
  38898. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  38899. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  38900. wolfSSL_sk_ASN1_OBJECT_free(sk);
  38901. sk = NULL;
  38902. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  38903. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  38904. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  38905. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  38906. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  38907. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  38908. sk = NULL;
  38909. /* obj freed in pop_free call. */
  38910. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  38911. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  38912. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  38913. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  38914. wolfSSL_sk_ASN1_OBJECT_free(sk);
  38915. wolfSSL_ASN1_OBJECT_free(obj);
  38916. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  38917. return EXPECT_RESULT();
  38918. }
  38919. static int test_wolfSSL_ASN1_STRING(void)
  38920. {
  38921. EXPECT_DECLS;
  38922. #if defined(OPENSSL_EXTRA)
  38923. ASN1_STRING* str = NULL;
  38924. ASN1_STRING* c = NULL;
  38925. const char data[] = "hello wolfSSL";
  38926. const char data2[] = "Same len data";
  38927. const char longData[] =
  38928. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  38929. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  38930. ASN1_STRING_free(str);
  38931. str = NULL;
  38932. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  38933. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  38934. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  38935. /* Check setting to NULL works. */
  38936. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  38937. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  38938. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  38939. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  38940. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  38941. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  38942. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  38943. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  38944. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  38945. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  38946. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  38947. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  38948. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  38949. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  38950. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  38951. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  38952. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  38953. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  38954. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  38955. /* Check setting back to smaller size frees dynamic data. */
  38956. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  38957. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  38958. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  38959. ExpectNull(ASN1_STRING_get0_data(NULL));
  38960. ExpectNotNull(ASN1_STRING_get0_data(str));
  38961. ExpectNull(ASN1_STRING_data(NULL));
  38962. ExpectNotNull(ASN1_STRING_data(str));
  38963. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  38964. ExpectIntGT(ASN1_STRING_length(str), 0);
  38965. ASN1_STRING_free(c);
  38966. ASN1_STRING_free(str);
  38967. ASN1_STRING_free(NULL);
  38968. #ifndef NO_WOLFSSL_STUB
  38969. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  38970. #endif
  38971. #endif
  38972. return EXPECT_RESULT();
  38973. }
  38974. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  38975. {
  38976. EXPECT_DECLS;
  38977. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  38978. !defined(NO_FILESYSTEM)
  38979. WOLFSSL_X509* x509 = NULL;
  38980. WOLFSSL_X509_NAME* subject = NULL;
  38981. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  38982. WOLFSSL_ASN1_STRING* a = NULL;
  38983. FILE* file = XBADFILE;
  38984. int idx = 0;
  38985. char targetOutput[16] = "www.wolfssl.com";
  38986. unsigned char* actual_output = NULL;
  38987. int len = 0;
  38988. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  38989. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  38990. if (file != XBADFILE)
  38991. fclose(file);
  38992. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  38993. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  38994. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  38995. NID_commonName, -1)), 5);
  38996. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  38997. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  38998. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  38999. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, (size_t)len), 0);
  39000. a = NULL;
  39001. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  39002. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  39003. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  39004. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  39005. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  39006. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  39007. wolfSSL_X509_free(x509);
  39008. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39009. ExpectNotNull(a = ASN1_STRING_new());
  39010. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  39011. ASN1_STRING_free(a);
  39012. #endif
  39013. return EXPECT_RESULT();
  39014. }
  39015. static int test_wolfSSL_i2s_ASN1_STRING(void)
  39016. {
  39017. EXPECT_DECLS;
  39018. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  39019. WOLFSSL_ASN1_STRING* str = NULL;
  39020. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  39021. char* ret = NULL;
  39022. ExpectNotNull(str = ASN1_STRING_new());
  39023. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  39024. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39025. ret = NULL;
  39026. /* No data. */
  39027. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  39028. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39029. ret = NULL;
  39030. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  39031. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  39032. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39033. ret = NULL;
  39034. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  39035. /* No type. */
  39036. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  39037. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39038. ASN1_STRING_free(str);
  39039. #endif
  39040. return EXPECT_RESULT();
  39041. }
  39042. static int test_wolfSSL_ASN1_STRING_canon(void)
  39043. {
  39044. EXPECT_DECLS;
  39045. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  39046. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  39047. defined(OPENSSL_EXTRA_X509_SMALL))
  39048. WOLFSSL_ASN1_STRING* orig = NULL;
  39049. WOLFSSL_ASN1_STRING* canon = NULL;
  39050. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  39051. const char* whitespaceOnly = "\t\r\n";
  39052. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  39053. const char* canonData = "\x01 \x02 \xff tt";
  39054. const char longData[] =
  39055. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  39056. ExpectNotNull(orig = ASN1_STRING_new());
  39057. ExpectNotNull(canon = ASN1_STRING_new());
  39058. /* Invalid parameter testing. */
  39059. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  39060. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  39061. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  39062. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  39063. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  39064. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  39065. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  39066. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  39067. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  39068. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  39069. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  39070. ASN1_STRING_free(orig);
  39071. orig = NULL;
  39072. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  39073. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  39074. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  39075. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  39076. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  39077. ASN1_STRING_free(orig);
  39078. orig = NULL;
  39079. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  39080. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  39081. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  39082. ASN1_STRING_free(orig);
  39083. orig = NULL;
  39084. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  39085. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  39086. ASN1_STRING_free(orig);
  39087. ASN1_STRING_free(canon);
  39088. #endif
  39089. #endif
  39090. return EXPECT_RESULT();
  39091. }
  39092. static int test_wolfSSL_ASN1_STRING_print(void)
  39093. {
  39094. EXPECT_DECLS;
  39095. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  39096. !defined(NO_BIO)
  39097. ASN1_STRING* asnStr = NULL;
  39098. const char HELLO_DATA[]= \
  39099. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  39100. #define MAX_UNPRINTABLE_CHAR 32
  39101. #define MAX_BUF 255
  39102. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  39103. unsigned char expected[sizeof(unprintableData)+1];
  39104. unsigned char rbuf[MAX_BUF];
  39105. BIO *bio = NULL;
  39106. int p_len;
  39107. int i;
  39108. /* setup */
  39109. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  39110. unprintableData[i] = (unsigned char)HELLO_DATA[i];
  39111. expected[i] = (unsigned char)HELLO_DATA[i];
  39112. }
  39113. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  39114. unprintableData[sizeof(HELLO_DATA)+i] = i;
  39115. if (i == (int)'\n' || i == (int)'\r')
  39116. expected[sizeof(HELLO_DATA)+i] = i;
  39117. else
  39118. expected[sizeof(HELLO_DATA)+i] = '.';
  39119. }
  39120. unprintableData[sizeof(unprintableData)-1] = '\0';
  39121. expected[sizeof(expected)-1] = '\0';
  39122. XMEMSET(rbuf, 0, MAX_BUF);
  39123. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39124. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  39125. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  39126. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  39127. (int)sizeof(unprintableData)), 1);
  39128. /* test */
  39129. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  39130. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  39131. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  39132. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  39133. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  39134. ExpectStrEQ((char*)rbuf, (const char*)expected);
  39135. BIO_free(bio);
  39136. bio = NULL;
  39137. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  39138. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  39139. /* Ensure there is 0 bytes available to write into. */
  39140. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  39141. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  39142. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  39143. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  39144. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  39145. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  39146. BIO_free(bio);
  39147. ASN1_STRING_free(asnStr);
  39148. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  39149. return EXPECT_RESULT();
  39150. }
  39151. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  39152. {
  39153. EXPECT_DECLS;
  39154. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  39155. ASN1_STRING* asn_str = NULL;
  39156. const char data[] = "Hello wolfSSL!";
  39157. ASN1_STRING* esc_str = NULL;
  39158. const char esc_data[] = "a+;<>";
  39159. ASN1_STRING* neg_int = NULL;
  39160. const char neg_int_data[] = "\xff";
  39161. ASN1_STRING* neg_enum = NULL;
  39162. const char neg_enum_data[] = "\xff";
  39163. BIO *bio = NULL;
  39164. BIO *fixed = NULL;
  39165. unsigned long flags;
  39166. int p_len;
  39167. unsigned char rbuf[255];
  39168. /* setup */
  39169. XMEMSET(rbuf, 0, 255);
  39170. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39171. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  39172. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  39173. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  39174. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  39175. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  39176. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  39177. sizeof(esc_data)), 1);
  39178. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  39179. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  39180. sizeof(neg_int_data) - 1), 1);
  39181. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  39182. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  39183. sizeof(neg_enum_data) - 1), 1);
  39184. /* Invalid parameter testing. */
  39185. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  39186. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  39187. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  39188. /* no flags */
  39189. XMEMSET(rbuf, 0, 255);
  39190. flags = 0;
  39191. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  39192. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  39193. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  39194. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39195. /* Ensure there is 0 bytes available to write into. */
  39196. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  39197. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39198. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39199. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39200. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  39201. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39202. /* RFC2253 Escape */
  39203. XMEMSET(rbuf, 0, 255);
  39204. flags = ASN1_STRFLGS_ESC_2253;
  39205. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  39206. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  39207. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  39208. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39209. /* Ensure there is 0 bytes available to write into. */
  39210. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  39211. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  39212. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39213. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  39214. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  39215. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  39216. /* Show type */
  39217. XMEMSET(rbuf, 0, 255);
  39218. flags = ASN1_STRFLGS_SHOW_TYPE;
  39219. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  39220. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  39221. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  39222. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39223. /* Ensure there is 0 bytes available to write into. */
  39224. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  39225. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39226. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39227. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39228. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  39229. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39230. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  39231. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39232. /* Dump All */
  39233. XMEMSET(rbuf, 0, 255);
  39234. flags = ASN1_STRFLGS_DUMP_ALL;
  39235. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  39236. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  39237. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  39238. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39239. /* Ensure there is 0 bytes available to write into. */
  39240. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  39241. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39242. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39243. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39244. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  39245. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39246. /* Dump Der */
  39247. XMEMSET(rbuf, 0, 255);
  39248. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  39249. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  39250. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  39251. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  39252. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39253. /* Ensure there is 0 bytes available to write into. */
  39254. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  39255. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39256. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39257. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39258. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  39259. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39260. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  39261. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  39262. /* Dump All + Show type */
  39263. XMEMSET(rbuf, 0, 255);
  39264. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  39265. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  39266. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  39267. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  39268. /* Dump All + Show type - Negative Integer. */
  39269. XMEMSET(rbuf, 0, 255);
  39270. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  39271. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  39272. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  39273. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  39274. /* Dump All + Show type - Negative Enumerated. */
  39275. XMEMSET(rbuf, 0, 255);
  39276. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  39277. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  39278. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  39279. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  39280. BIO_free(fixed);
  39281. BIO_free(bio);
  39282. ASN1_STRING_free(asn_str);
  39283. ASN1_STRING_free(esc_str);
  39284. ASN1_STRING_free(neg_int);
  39285. ASN1_STRING_free(neg_enum);
  39286. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  39287. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  39288. #endif
  39289. return EXPECT_RESULT();
  39290. }
  39291. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  39292. {
  39293. EXPECT_DECLS;
  39294. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  39295. ASN1_STRING* asn1str_test = NULL;
  39296. ASN1_STRING* asn1str_answer = NULL;
  39297. /* Each character is encoded using 4 bytes */
  39298. char input[] = {
  39299. 0, 0, 0, 'T',
  39300. 0, 0, 0, 'e',
  39301. 0, 0, 0, 's',
  39302. 0, 0, 0, 't',
  39303. };
  39304. char output[] = "Test";
  39305. char badInput[] = {
  39306. 1, 0, 0, 'T',
  39307. 0, 1, 0, 'e',
  39308. 0, 0, 1, 's',
  39309. };
  39310. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  39311. /* Test wrong type. */
  39312. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  39313. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  39314. ASN1_STRING_free(asn1str_test);
  39315. asn1str_test = NULL;
  39316. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  39317. /* Test bad length. */
  39318. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  39319. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  39320. /* Test bad input. */
  39321. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  39322. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  39323. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  39324. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  39325. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  39326. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  39327. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  39328. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  39329. ExpectNotNull(
  39330. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  39331. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  39332. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  39333. ASN1_STRING_free(asn1str_test);
  39334. ASN1_STRING_free(asn1str_answer);
  39335. #endif /* OPENSSL_ALL && !NO_ASN */
  39336. return EXPECT_RESULT();
  39337. }
  39338. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  39339. {
  39340. EXPECT_DECLS;
  39341. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  39342. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  39343. ExpectNotNull(asn1_gtime = ASN1_GENERALIZEDTIME_new());
  39344. if (asn1_gtime != NULL)
  39345. XMEMCPY(asn1_gtime->data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  39346. ASN1_GENERALIZEDTIME_free(asn1_gtime);
  39347. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME */
  39348. return EXPECT_RESULT();
  39349. }
  39350. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  39351. {
  39352. EXPECT_DECLS;
  39353. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  39354. WOLFSSL_ASN1_GENERALIZEDTIME* gtime = NULL;
  39355. BIO* bio = NULL;
  39356. unsigned char buf[24];
  39357. int i;
  39358. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39359. BIO_set_write_buf_size(bio, 24);
  39360. ExpectNotNull(gtime = ASN1_GENERALIZEDTIME_new());
  39361. /* Type not set. */
  39362. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  39363. ExpectIntEQ(wolfSSL_ASN1_TIME_set_string(gtime, "20180504123500Z"), 1);
  39364. /* Invalid parameters testing. */
  39365. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  39366. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  39367. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, gtime), BAD_FUNC_ARG);
  39368. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 1);
  39369. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  39370. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  39371. BIO_free(bio);
  39372. bio = NULL;
  39373. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  39374. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  39375. /* Ensure there is 0 bytes available to write into. */
  39376. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  39377. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  39378. for (i = 1; i < 20; i++) {
  39379. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  39380. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  39381. }
  39382. BIO_free(bio);
  39383. wolfSSL_ASN1_GENERALIZEDTIME_free(gtime);
  39384. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  39385. return EXPECT_RESULT();
  39386. }
  39387. static int test_wolfSSL_ASN1_TIME(void)
  39388. {
  39389. EXPECT_DECLS;
  39390. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  39391. WOLFSSL_ASN1_TIME* asn_time = NULL;
  39392. unsigned char *data;
  39393. ExpectNotNull(asn_time = ASN1_TIME_new());
  39394. #ifndef NO_WOLFSSL_STUB
  39395. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  39396. #endif
  39397. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  39398. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  39399. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  39400. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  39401. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  39402. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  39403. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  39404. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  39405. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  39406. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  39407. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  39408. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  39409. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  39410. ASN1_TIME_free(asn_time);
  39411. ASN1_TIME_free(NULL);
  39412. #endif
  39413. return EXPECT_RESULT();
  39414. }
  39415. static int test_wolfSSL_ASN1_TIME_to_string(void)
  39416. {
  39417. EXPECT_DECLS;
  39418. #ifndef NO_ASN_TIME
  39419. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  39420. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  39421. WOLFSSL_ASN1_TIME* t = NULL;
  39422. char buf[ASN_GENERALIZED_TIME_SIZE];
  39423. ExpectNotNull((t = ASN1_TIME_new()));
  39424. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  39425. /* Invalid parameter testing. */
  39426. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  39427. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  39428. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  39429. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  39430. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  39431. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  39432. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  39433. /* Buffer needs to be longer than minimum of 5 characters. */
  39434. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  39435. ASN1_TIME_free(t);
  39436. #endif
  39437. #endif /* NO_ASN_TIME */
  39438. return EXPECT_RESULT();
  39439. }
  39440. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  39441. {
  39442. EXPECT_DECLS;
  39443. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  39444. ASN1_TIME* fromTime = NULL;
  39445. ASN1_TIME* closeToTime = NULL;
  39446. ASN1_TIME* toTime = NULL;
  39447. ASN1_TIME* invalidTime = NULL;
  39448. int daysDiff = 0;
  39449. int secsDiff = 0;
  39450. ExpectNotNull((fromTime = ASN1_TIME_new()));
  39451. /* Feb 22, 2003, 21:15:15 */
  39452. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  39453. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  39454. /* Feb 22, 2003, 21:16:15 */
  39455. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  39456. ExpectNotNull((toTime = ASN1_TIME_new()));
  39457. /* Dec 19, 2010, 18:10:11 */
  39458. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  39459. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  39460. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  39461. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  39462. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  39463. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  39464. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  39465. /* Error conditions. */
  39466. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  39467. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  39468. /* If both times are NULL, difference is 0. */
  39469. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  39470. ExpectIntEQ(daysDiff, 0);
  39471. ExpectIntEQ(secsDiff, 0);
  39472. /* If one time is NULL, it defaults to the current time. */
  39473. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  39474. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  39475. /* Normal operation. Both times non-NULL. */
  39476. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  39477. ExpectIntEQ(daysDiff, 2856);
  39478. ExpectIntEQ(secsDiff, 75296);
  39479. /* Swapping the times should return negative values. */
  39480. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  39481. ExpectIntEQ(daysDiff, -2856);
  39482. ExpectIntEQ(secsDiff, -75296);
  39483. /* Compare with invalid time string. */
  39484. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  39485. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  39486. /* Compare with days difference of 0. */
  39487. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  39488. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  39489. /* Days and seconds differences not 0. */
  39490. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  39491. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  39492. /* Same time. */
  39493. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  39494. /* Compare regression test: No seconds difference, just difference in days.
  39495. */
  39496. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  39497. ASN1_TIME_set_string(toTime, "19800101000000Z");
  39498. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  39499. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  39500. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  39501. /* Edge case with Unix epoch. */
  39502. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  39503. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  39504. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  39505. ExpectIntEQ(daysDiff, 3652);
  39506. ExpectIntEQ(secsDiff, 0);
  39507. /* Edge case with year > 2038 (year 2038 problem). */
  39508. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  39509. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  39510. ExpectIntEQ(daysDiff, 2932896);
  39511. ExpectIntEQ(secsDiff, 86399);
  39512. ASN1_TIME_free(fromTime);
  39513. ASN1_TIME_free(closeToTime);
  39514. ASN1_TIME_free(toTime);
  39515. ASN1_TIME_free(invalidTime);
  39516. #endif
  39517. return EXPECT_RESULT();
  39518. }
  39519. static int test_wolfSSL_ASN1_TIME_adj(void)
  39520. {
  39521. EXPECT_DECLS;
  39522. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  39523. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  39524. const int year = 365*24*60*60;
  39525. const int day = 24*60*60;
  39526. const int hour = 60*60;
  39527. const int mini = 60;
  39528. const byte asn_utc_time = ASN_UTC_TIME;
  39529. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  39530. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  39531. #endif
  39532. WOLFSSL_ASN1_TIME* asn_time = NULL;
  39533. WOLFSSL_ASN1_TIME* s = NULL;
  39534. int offset_day;
  39535. long offset_sec;
  39536. char date_str[CTC_DATE_SIZE + 1];
  39537. time_t t;
  39538. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  39539. /* UTC notation test */
  39540. /* 2000/2/15 20:30:00 */
  39541. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  39542. offset_day = 7;
  39543. offset_sec = 45 * mini;
  39544. /* offset_sec = -45 * min;*/
  39545. ExpectNotNull(asn_time =
  39546. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  39547. ExpectTrue(asn_time->type == asn_utc_time);
  39548. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  39549. CTC_DATE_SIZE));
  39550. date_str[CTC_DATE_SIZE] = '\0';
  39551. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  39552. /* negative offset */
  39553. offset_sec = -45 * mini;
  39554. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  39555. ExpectNotNull(asn_time);
  39556. ExpectTrue(asn_time->type == asn_utc_time);
  39557. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  39558. CTC_DATE_SIZE));
  39559. date_str[CTC_DATE_SIZE] = '\0';
  39560. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  39561. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  39562. s = NULL;
  39563. XMEMSET(date_str, 0, sizeof(date_str));
  39564. /* Generalized time will overflow time_t if not long */
  39565. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  39566. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  39567. DYNAMIC_TYPE_OPENSSL);
  39568. /* GeneralizedTime notation test */
  39569. /* 2055/03/01 09:00:00 */
  39570. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  39571. offset_day = 12;
  39572. offset_sec = 10 * mini;
  39573. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  39574. offset_sec));
  39575. ExpectTrue(asn_time->type == asn_gen_time);
  39576. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  39577. CTC_DATE_SIZE));
  39578. date_str[CTC_DATE_SIZE] = '\0';
  39579. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  39580. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  39581. s = NULL;
  39582. XMEMSET(date_str, 0, sizeof(date_str));
  39583. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  39584. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  39585. s = NULL;
  39586. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  39587. offset_day = 7;
  39588. offset_sec = 45 * mini;
  39589. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  39590. offset_sec));
  39591. ExpectTrue(asn_time->type == asn_utc_time);
  39592. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  39593. CTC_DATE_SIZE));
  39594. date_str[CTC_DATE_SIZE] = '\0';
  39595. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  39596. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  39597. asn_time = NULL;
  39598. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  39599. offset_sec));
  39600. ExpectTrue(asn_time->type == asn_utc_time);
  39601. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  39602. CTC_DATE_SIZE));
  39603. date_str[CTC_DATE_SIZE] = '\0';
  39604. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  39605. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  39606. #endif
  39607. return EXPECT_RESULT();
  39608. }
  39609. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  39610. {
  39611. EXPECT_DECLS;
  39612. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  39613. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  39614. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  39615. ASN1_TIME asnTime;
  39616. struct tm tm;
  39617. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  39618. XMEMSET(&tm, 0, sizeof(struct tm));
  39619. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  39620. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  39621. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  39622. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  39623. ExpectIntEQ(tm.tm_sec, 15);
  39624. ExpectIntEQ(tm.tm_min, 15);
  39625. ExpectIntEQ(tm.tm_hour, 21);
  39626. ExpectIntEQ(tm.tm_mday, 22);
  39627. ExpectIntEQ(tm.tm_mon, 1);
  39628. ExpectIntEQ(tm.tm_year, 100);
  39629. ExpectIntEQ(tm.tm_isdst, 0);
  39630. #ifdef XMKTIME
  39631. ExpectIntEQ(tm.tm_wday, 2);
  39632. ExpectIntEQ(tm.tm_yday, 52);
  39633. #endif
  39634. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  39635. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  39636. ExpectIntEQ(tm.tm_year, 50);
  39637. /* Get current time. */
  39638. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  39639. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  39640. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  39641. /* 0 length. */
  39642. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  39643. /* No type. */
  39644. asnTime.length = 1;
  39645. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  39646. /* Not UTCTIME length. */
  39647. asnTime.type = V_ASN1_UTCTIME;
  39648. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  39649. /* Not GENERALIZEDTIME length. */
  39650. asnTime.type = V_ASN1_GENERALIZEDTIME;
  39651. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  39652. /* Not Zulu timezone. */
  39653. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  39654. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  39655. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  39656. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  39657. #ifdef XMKTIME
  39658. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  39659. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  39660. ExpectIntEQ(tm.tm_sec, 47);
  39661. ExpectIntEQ(tm.tm_min, 22);
  39662. ExpectIntEQ(tm.tm_hour, 21);
  39663. ExpectIntEQ(tm.tm_mday, 12);
  39664. ExpectIntEQ(tm.tm_mon, 4);
  39665. ExpectIntEQ(tm.tm_year, 123);
  39666. ExpectIntEQ(tm.tm_wday, 5);
  39667. ExpectIntEQ(tm.tm_yday, 131);
  39668. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  39669. fields are zeroed out as expected. */
  39670. ExpectIntEQ(tm.tm_isdst, 0);
  39671. #endif
  39672. #endif
  39673. return EXPECT_RESULT();
  39674. }
  39675. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  39676. {
  39677. EXPECT_DECLS;
  39678. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  39679. WOLFSSL_ASN1_TIME *t = NULL;
  39680. WOLFSSL_ASN1_TIME *out = NULL;
  39681. WOLFSSL_ASN1_TIME *gtime = NULL;
  39682. int tlen = 0;
  39683. unsigned char *data = NULL;
  39684. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  39685. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  39686. /* type not set. */
  39687. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  39688. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39689. t = NULL;
  39690. /* UTC Time test */
  39691. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  39692. if (t != NULL) {
  39693. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  39694. t->type = ASN_UTC_TIME;
  39695. t->length = ASN_UTC_TIME_SIZE;
  39696. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  39697. }
  39698. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  39699. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  39700. out = NULL;
  39701. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  39702. wolfSSL_ASN1_TIME_free(gtime);
  39703. gtime = NULL;
  39704. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  39705. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  39706. ExpectPtrEq(gtime, out);
  39707. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  39708. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  39709. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  39710. /* Generalized Time test */
  39711. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  39712. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  39713. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  39714. if (t != NULL) {
  39715. t->type = ASN_GENERALIZED_TIME;
  39716. t->length = ASN_GENERALIZED_TIME_SIZE;
  39717. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  39718. }
  39719. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  39720. ASN_GENERALIZED_TIME_SIZE);
  39721. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  39722. "20050727123456Z");
  39723. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  39724. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  39725. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  39726. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  39727. /* UTC Time to Generalized Time 1900's test */
  39728. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  39729. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  39730. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  39731. if (t != NULL) {
  39732. t->type = ASN_UTC_TIME;
  39733. t->length = ASN_UTC_TIME_SIZE;
  39734. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  39735. }
  39736. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  39737. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  39738. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  39739. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  39740. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39741. /* Null parameter test */
  39742. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  39743. gtime = NULL;
  39744. out = NULL;
  39745. if (t != NULL) {
  39746. t->type = ASN_UTC_TIME;
  39747. t->length = ASN_UTC_TIME_SIZE;
  39748. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  39749. }
  39750. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  39751. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  39752. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  39753. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  39754. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39755. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39756. #endif
  39757. return EXPECT_RESULT();
  39758. }
  39759. static int test_wolfSSL_ASN1_TIME_print(void)
  39760. {
  39761. EXPECT_DECLS;
  39762. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  39763. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  39764. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  39765. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  39766. !defined(NO_ASN_TIME)
  39767. BIO* bio = NULL;
  39768. BIO* fixed = NULL;
  39769. X509* x509 = NULL;
  39770. const unsigned char* der = client_cert_der_2048;
  39771. ASN1_TIME* notAfter = NULL;
  39772. ASN1_TIME* notBefore = NULL;
  39773. unsigned char buf[25];
  39774. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39775. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  39776. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  39777. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  39778. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  39779. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  39780. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  39781. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  39782. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  39783. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  39784. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  39785. /* Test BIO_write fails. */
  39786. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39787. /* Ensure there is 0 bytes available to write into. */
  39788. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  39789. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  39790. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  39791. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  39792. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  39793. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  39794. /* create a bad time and test results */
  39795. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  39796. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  39797. if (EXPECT_SUCCESS()) {
  39798. notAfter->data[8] = 0;
  39799. notAfter->data[3] = 0;
  39800. }
  39801. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  39802. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  39803. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  39804. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  39805. BIO_free(bio);
  39806. BIO_free(fixed);
  39807. X509_free(x509);
  39808. #endif
  39809. return EXPECT_RESULT();
  39810. }
  39811. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  39812. {
  39813. EXPECT_DECLS;
  39814. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  39815. BIO* bio = NULL;
  39816. ASN1_UTCTIME* utc = NULL;
  39817. unsigned char buf[25];
  39818. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  39819. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  39820. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  39821. /* Valid date */
  39822. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39823. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  39824. DYNAMIC_TYPE_ASN1));
  39825. if (utc != NULL) {
  39826. utc->type = ASN_UTC_TIME;
  39827. utc->length = ASN_UTC_TIME_SIZE;
  39828. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  39829. }
  39830. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  39831. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  39832. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  39833. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  39834. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  39835. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  39836. XMEMSET(buf, 0, sizeof(buf));
  39837. BIO_free(bio);
  39838. bio = NULL;
  39839. /* Invalid format */
  39840. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39841. if (utc != NULL) {
  39842. utc->type = ASN_UTC_TIME;
  39843. utc->length = ASN_UTC_TIME_SIZE;
  39844. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  39845. }
  39846. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  39847. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  39848. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  39849. /* Invalid type */
  39850. if (utc != NULL) {
  39851. utc->type = ASN_GENERALIZED_TIME;
  39852. utc->length = ASN_GENERALIZED_TIME_SIZE;
  39853. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  39854. }
  39855. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  39856. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  39857. BIO_free(bio);
  39858. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  39859. return EXPECT_RESULT();
  39860. }
  39861. static int test_wolfSSL_ASN1_TYPE(void)
  39862. {
  39863. EXPECT_DECLS;
  39864. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  39865. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  39866. WOLFSSL_ASN1_TYPE* t = NULL;
  39867. WOLFSSL_ASN1_OBJECT* obj = NULL;
  39868. #ifndef NO_ASN_TIME
  39869. WOLFSSL_ASN1_TIME* time = NULL;
  39870. #endif
  39871. WOLFSSL_ASN1_STRING* str = NULL;
  39872. unsigned char data[] = { 0x00 };
  39873. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  39874. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39875. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  39876. wolfSSL_ASN1_TYPE_free(t);
  39877. t = NULL;
  39878. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39879. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  39880. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  39881. wolfSSL_ASN1_TYPE_free(t);
  39882. t = NULL;
  39883. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39884. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  39885. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  39886. wolfSSL_ASN1_TYPE_free(t);
  39887. t = NULL;
  39888. #ifndef NO_ASN_TIME
  39889. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39890. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  39891. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  39892. wolfSSL_ASN1_TYPE_free(t);
  39893. t = NULL;
  39894. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39895. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  39896. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  39897. wolfSSL_ASN1_TYPE_free(t);
  39898. t = NULL;
  39899. #endif
  39900. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39901. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  39902. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  39903. wolfSSL_ASN1_TYPE_free(t);
  39904. t = NULL;
  39905. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39906. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  39907. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  39908. wolfSSL_ASN1_TYPE_free(t);
  39909. t = NULL;
  39910. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39911. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  39912. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  39913. wolfSSL_ASN1_TYPE_free(t);
  39914. t = NULL;
  39915. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39916. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  39917. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  39918. wolfSSL_ASN1_TYPE_free(t);
  39919. t = NULL;
  39920. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39921. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  39922. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  39923. wolfSSL_ASN1_TYPE_free(t);
  39924. t = NULL;
  39925. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  39926. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  39927. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  39928. wolfSSL_ASN1_TYPE_free(t);
  39929. t = NULL;
  39930. #endif
  39931. return EXPECT_RESULT();
  39932. }
  39933. /* Testing code used in dpp.c in hostap */
  39934. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  39935. typedef struct {
  39936. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  39937. * as an OID identifying the curve */
  39938. X509_ALGOR *alg;
  39939. /* Compressed format public key per ANSI X9.63 */
  39940. ASN1_BIT_STRING *pub_key;
  39941. } DPP_BOOTSTRAPPING_KEY;
  39942. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  39943. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  39944. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  39945. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  39946. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  39947. typedef struct {
  39948. ASN1_INTEGER *integer;
  39949. } TEST_ASN1;
  39950. ASN1_SEQUENCE(TEST_ASN1) = {
  39951. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  39952. } ASN1_SEQUENCE_END(TEST_ASN1)
  39953. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  39954. typedef struct {
  39955. ASN1_OCTET_STRING *octet_string;
  39956. } TEST_FAIL_ASN1;
  39957. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  39958. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  39959. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  39960. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  39961. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  39962. #endif
  39963. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  39964. {
  39965. EXPECT_DECLS;
  39966. /* Testing code used in dpp.c in hostap */
  39967. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  39968. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  39969. EC_KEY *eckey = NULL;
  39970. EVP_PKEY *key = NULL;
  39971. size_t len = 0;
  39972. unsigned char *der = NULL;
  39973. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  39974. const unsigned char *in = ecc_clikey_der_256;
  39975. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  39976. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  39977. const EC_GROUP *group = NULL;
  39978. const EC_POINT *point = NULL;
  39979. int nid;
  39980. TEST_ASN1 *test_asn1 = NULL;
  39981. TEST_FAIL_ASN1 test_fail_asn1;
  39982. const unsigned char badObjDer[] = { 0x06, 0x00 };
  39983. const unsigned char goodObjDer[] = {
  39984. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  39985. };
  39986. WOLFSSL_ASN1_ITEM emptyTemplate;
  39987. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  39988. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  39989. der = NULL;
  39990. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  39991. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  39992. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  39993. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  39994. (long)sizeof_ecc_clikey_der_256));
  39995. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  39996. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  39997. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  39998. nid = EC_GROUP_get_curve_name(group);
  39999. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  40000. group_obj = OBJ_nid2obj(nid);
  40001. if ((ec_obj != NULL) && (group_obj != NULL)) {
  40002. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  40003. group_obj), 1);
  40004. if (EXPECT_SUCCESS()) {
  40005. ec_obj = NULL;
  40006. group_obj = NULL;
  40007. }
  40008. }
  40009. wolfSSL_ASN1_OBJECT_free(group_obj);
  40010. wolfSSL_ASN1_OBJECT_free(ec_obj);
  40011. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  40012. #ifdef HAVE_COMP_KEY
  40013. ExpectIntGT((len = EC_POINT_point2oct(
  40014. group, point, POINT_CONVERSION_COMPRESSED,
  40015. NULL, 0, NULL)), 0);
  40016. #else
  40017. ExpectIntGT((len = EC_POINT_point2oct(
  40018. group, point, POINT_CONVERSION_UNCOMPRESSED,
  40019. NULL, 0, NULL)), 0);
  40020. #endif
  40021. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  40022. #ifdef HAVE_COMP_KEY
  40023. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  40024. der, len-1, NULL), 0);
  40025. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  40026. der, len, NULL), len);
  40027. #else
  40028. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  40029. der, len-1, NULL), 0);
  40030. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  40031. der, len, NULL), len);
  40032. #endif
  40033. if (EXPECT_SUCCESS()) {
  40034. bootstrap->pub_key->data = der;
  40035. bootstrap->pub_key->length = (int)len;
  40036. /* Not actually used */
  40037. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  40038. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  40039. }
  40040. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  40041. der = NULL;
  40042. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  40043. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  40044. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  40045. EVP_PKEY_free(key);
  40046. EC_KEY_free(eckey);
  40047. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  40048. bootstrap = NULL;
  40049. DPP_BOOTSTRAPPING_KEY_free(NULL);
  40050. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  40051. * a NULL and an empty BIT_STRING. */
  40052. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  40053. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  40054. if (EXPECT_SUCCESS()) {
  40055. bootstrap->alg->algorithm->obj = badObjDer;
  40056. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  40057. }
  40058. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  40059. if (EXPECT_SUCCESS()) {
  40060. bootstrap->alg->parameter->type = V_ASN1_NULL;
  40061. bootstrap->alg->parameter->value.ptr = NULL;
  40062. bootstrap->pub_key->data = NULL;
  40063. bootstrap->pub_key->length = 0;
  40064. /* Not actually used */
  40065. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  40066. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  40067. }
  40068. /* Encode with bad OBJECT_ID. */
  40069. der = NULL;
  40070. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  40071. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  40072. if (EXPECT_SUCCESS()) {
  40073. bootstrap->alg->algorithm->obj = goodObjDer;
  40074. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  40075. bootstrap->alg->algorithm->grp = 2;
  40076. }
  40077. der = NULL;
  40078. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  40079. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  40080. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  40081. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  40082. /* Test integer */
  40083. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  40084. der = NULL;
  40085. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  40086. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  40087. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  40088. TEST_ASN1_free(test_asn1);
  40089. /* Test integer cases. */
  40090. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  40091. TEST_ASN1_free(NULL);
  40092. /* Test error cases. */
  40093. ExpectNull(TEST_FAIL_ASN1_new());
  40094. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  40095. TEST_FAIL_ASN1_free(NULL);
  40096. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  40097. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  40098. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  40099. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  40100. return EXPECT_RESULT();
  40101. }
  40102. static int test_wolfSSL_lhash(void)
  40103. {
  40104. EXPECT_DECLS;
  40105. #ifdef OPENSSL_ALL
  40106. const char testStr[] = "Like a true nature's child\n"
  40107. "We were born\n"
  40108. "Born to be wild";
  40109. #ifdef NO_SHA
  40110. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  40111. #else
  40112. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  40113. #endif
  40114. #endif
  40115. return EXPECT_RESULT();
  40116. }
  40117. static int test_wolfSSL_X509_NAME(void)
  40118. {
  40119. EXPECT_DECLS;
  40120. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  40121. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  40122. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  40123. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  40124. defined(OPENSSL_EXTRA))
  40125. X509* x509 = NULL;
  40126. const unsigned char* c = NULL;
  40127. unsigned char buf[4096];
  40128. int bytes = 0;
  40129. XFILE f = XBADFILE;
  40130. const X509_NAME* a = NULL;
  40131. const X509_NAME* b = NULL;
  40132. X509_NAME* d2i_name = NULL;
  40133. int sz = 0;
  40134. unsigned char* tmp = NULL;
  40135. char file[] = "./certs/ca-cert.der";
  40136. #ifndef OPENSSL_EXTRA_X509_SMALL
  40137. byte empty[] = { /* CN=empty emailAddress= */
  40138. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  40139. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  40140. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  40141. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  40142. 0x01, 0x16, 0x00
  40143. };
  40144. #endif
  40145. #ifndef OPENSSL_EXTRA_X509_SMALL
  40146. /* test compile of deprecated function, returns 0 */
  40147. ExpectIntEQ(CRYPTO_thread_id(), 0);
  40148. #endif
  40149. ExpectNotNull(a = X509_NAME_new());
  40150. X509_NAME_free((X509_NAME*)a);
  40151. a = NULL;
  40152. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  40153. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  40154. if (f != XBADFILE)
  40155. XFCLOSE(f);
  40156. c = buf;
  40157. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  40158. /* test cmp function */
  40159. ExpectNotNull(a = X509_get_issuer_name(x509));
  40160. ExpectNotNull(b = X509_get_subject_name(x509));
  40161. #ifndef OPENSSL_EXTRA_X509_SMALL
  40162. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  40163. #endif
  40164. tmp = buf;
  40165. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  40166. if (sz > 0 && tmp == buf) {
  40167. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  40168. __LINE__);
  40169. fprintf(stderr, " Expected pointer to be incremented\n");
  40170. abort();
  40171. }
  40172. #ifndef OPENSSL_EXTRA_X509_SMALL
  40173. tmp = buf;
  40174. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  40175. #endif
  40176. /* if output parameter is NULL, should still return required size. */
  40177. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  40178. /* retry but with the function creating a buffer */
  40179. tmp = NULL;
  40180. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  40181. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  40182. tmp = NULL;
  40183. #ifdef WOLFSSL_CERT_NAME_ALL
  40184. /* test for givenName and name */
  40185. {
  40186. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  40187. const byte gName[] = "test-given-name";
  40188. const byte name[] = "test-name";
  40189. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  40190. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  40191. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  40192. 1);
  40193. wolfSSL_X509_NAME_ENTRY_free(entry);
  40194. entry = NULL;
  40195. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  40196. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  40197. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  40198. 1);
  40199. wolfSSL_X509_NAME_ENTRY_free(entry);
  40200. tmp = NULL;
  40201. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  40202. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  40203. }
  40204. #endif
  40205. b = NULL;
  40206. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  40207. #ifndef OPENSSL_EXTRA_X509_SMALL
  40208. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  40209. #endif
  40210. X509_NAME_free((X509_NAME*)b);
  40211. X509_NAME_free(d2i_name);
  40212. d2i_name = NULL;
  40213. X509_free(x509);
  40214. #ifndef OPENSSL_EXTRA_X509_SMALL
  40215. /* test with an empty domain component */
  40216. tmp = empty;
  40217. sz = sizeof(empty);
  40218. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  40219. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  40220. /* size of empty emailAddress will be 0 */
  40221. tmp = buf;
  40222. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  40223. (char*)tmp, sizeof(buf)), 0);
  40224. /* should contain no organization name */
  40225. tmp = buf;
  40226. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  40227. (char*)tmp, sizeof(buf)), -1);
  40228. X509_NAME_free(d2i_name);
  40229. #endif
  40230. #endif
  40231. return EXPECT_RESULT();
  40232. }
  40233. static int test_wolfSSL_X509_NAME_hash(void)
  40234. {
  40235. EXPECT_DECLS;
  40236. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  40237. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  40238. BIO* bio = NULL;
  40239. X509* x509 = NULL;
  40240. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  40241. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  40242. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  40243. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  40244. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  40245. X509_free(x509);
  40246. BIO_free(bio);
  40247. #endif
  40248. return EXPECT_RESULT();
  40249. }
  40250. static int test_wolfSSL_X509_NAME_print_ex(void)
  40251. {
  40252. EXPECT_DECLS;
  40253. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  40254. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  40255. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  40256. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  40257. !defined(NO_BIO) && !defined(NO_RSA)
  40258. int memSz = 0;
  40259. byte* mem = NULL;
  40260. BIO* bio = NULL;
  40261. BIO* membio = NULL;
  40262. X509* x509 = NULL;
  40263. X509_NAME* name = NULL;
  40264. const char* expNormal = "C=US, CN=wolfssl.com";
  40265. const char* expEqSpace = "C = US, CN = wolfssl.com";
  40266. const char* expReverse = "CN=wolfssl.com, C=US";
  40267. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  40268. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  40269. const char* expRFC5523 =
  40270. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  40271. /* Test with real cert (svrCertFile) first */
  40272. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  40273. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  40274. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  40275. ExpectNotNull(name = X509_get_subject_name(x509));
  40276. /* Test without flags */
  40277. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  40278. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  40279. BIO_free(membio);
  40280. membio = NULL;
  40281. /* Test flag: XN_FLAG_RFC2253 */
  40282. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  40283. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  40284. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  40285. BIO_free(membio);
  40286. membio = NULL;
  40287. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  40288. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  40289. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  40290. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  40291. BIO_free(membio);
  40292. membio = NULL;
  40293. X509_free(x509);
  40294. BIO_free(bio);
  40295. name = NULL;
  40296. /* Test normal case without escaped characters */
  40297. {
  40298. /* Create name: "/C=US/CN=wolfssl.com" */
  40299. ExpectNotNull(name = X509_NAME_new());
  40300. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  40301. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  40302. WOLFSSL_SUCCESS);
  40303. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  40304. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  40305. WOLFSSL_SUCCESS);
  40306. /* Test without flags */
  40307. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  40308. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  40309. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  40310. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  40311. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  40312. BIO_free(membio);
  40313. membio = NULL;
  40314. /* Test with XN_FLAG_ONELINE which should enable XN_FLAG_SPC_EQ for
  40315. spaces aroun '=' */
  40316. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  40317. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, XN_FLAG_ONELINE),
  40318. WOLFSSL_SUCCESS);
  40319. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  40320. ExpectIntEQ(memSz, XSTRLEN(expEqSpace));
  40321. ExpectIntEQ(XSTRNCMP((char*)mem, expEqSpace, XSTRLEN(expEqSpace)), 0);
  40322. BIO_free(membio);
  40323. membio = NULL;
  40324. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  40325. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  40326. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  40327. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  40328. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  40329. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  40330. BIO_free(membio);
  40331. membio = NULL;
  40332. /* Test flags: XN_FLAG_DN_REV - reversed */
  40333. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  40334. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  40335. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  40336. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  40337. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  40338. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  40339. BIO_free(membio);
  40340. membio = NULL;
  40341. X509_NAME_free(name);
  40342. name = NULL;
  40343. }
  40344. /* Test RFC2253 characters are escaped with backslashes */
  40345. {
  40346. ExpectNotNull(name = X509_NAME_new());
  40347. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  40348. /* space at beginning and end, and: ,+"\ */
  40349. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  40350. WOLFSSL_SUCCESS);
  40351. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  40352. /* # at beginning, and: <>;*/
  40353. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  40354. WOLFSSL_SUCCESS);
  40355. /* Test without flags */
  40356. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  40357. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  40358. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  40359. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  40360. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  40361. XSTRLEN(expNotEscaped)), 0);
  40362. BIO_free(membio);
  40363. membio = NULL;
  40364. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  40365. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  40366. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  40367. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  40368. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  40369. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  40370. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  40371. BIO_free(membio);
  40372. membio = NULL;
  40373. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  40374. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  40375. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  40376. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  40377. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  40378. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  40379. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  40380. XSTRLEN(expNotEscapedRev)), 0);
  40381. BIO_free(membio);
  40382. X509_NAME_free(name);
  40383. }
  40384. #endif
  40385. return EXPECT_RESULT();
  40386. }
  40387. #ifndef NO_BIO
  40388. static int test_wolfSSL_X509_INFO_multiple_info(void)
  40389. {
  40390. EXPECT_DECLS;
  40391. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40392. STACK_OF(X509_INFO) *info_stack = NULL;
  40393. X509_INFO *info = NULL;
  40394. int len;
  40395. int i;
  40396. const char* files[] = {
  40397. cliCertFile,
  40398. cliKeyFile,
  40399. /* This needs to be the order as svrCertFile contains the
  40400. * intermediate cert as well. */
  40401. svrKeyFile,
  40402. svrCertFile,
  40403. NULL,
  40404. };
  40405. const char** curFile;
  40406. BIO *fileBIO = NULL;
  40407. BIO *concatBIO = NULL;
  40408. byte tmp[FOURK_BUF];
  40409. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  40410. * to group objects together. */
  40411. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  40412. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  40413. int fileLen = 0;
  40414. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  40415. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  40416. if (EXPECT_SUCCESS()) {
  40417. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  40418. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  40419. fileLen -= len;
  40420. if (EXPECT_FAIL())
  40421. break;
  40422. }
  40423. /* Make sure we read the entire file */
  40424. ExpectIntEQ(fileLen, 0);
  40425. }
  40426. BIO_free(fileBIO);
  40427. fileBIO = NULL;
  40428. }
  40429. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  40430. NULL));
  40431. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  40432. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  40433. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  40434. ExpectNotNull(info->x509);
  40435. ExpectNull(info->crl);
  40436. if (i != 0) {
  40437. ExpectNotNull(info->x_pkey);
  40438. ExpectIntEQ(X509_check_private_key(info->x509,
  40439. info->x_pkey->dec_pkey), 1);
  40440. }
  40441. else {
  40442. ExpectNull(info->x_pkey);
  40443. }
  40444. }
  40445. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  40446. BIO_free(concatBIO);
  40447. #endif
  40448. return EXPECT_RESULT();
  40449. }
  40450. #endif
  40451. #ifndef NO_BIO
  40452. static int test_wolfSSL_X509_INFO(void)
  40453. {
  40454. EXPECT_DECLS;
  40455. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40456. STACK_OF(X509_INFO) *info_stack = NULL;
  40457. X509_INFO *info = NULL;
  40458. BIO *cert = NULL;
  40459. int i;
  40460. /* PEM in hex format to avoid null terminator */
  40461. byte data[] = {
  40462. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  40463. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  40464. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  40465. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  40466. 0x2d, 0x2d, 0x2d
  40467. };
  40468. /* PEM in hex format to avoid null terminator */
  40469. byte data2[] = {
  40470. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  40471. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  40472. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  40473. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  40474. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  40475. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  40476. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  40477. };
  40478. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  40479. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  40480. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  40481. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  40482. ExpectNotNull(info->x509);
  40483. ExpectNull(info->crl);
  40484. ExpectNull(info->x_pkey);
  40485. }
  40486. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  40487. info_stack = NULL;
  40488. BIO_free(cert);
  40489. cert = NULL;
  40490. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  40491. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  40492. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  40493. info_stack = NULL;
  40494. BIO_free(cert);
  40495. cert = NULL;
  40496. /* This case should fail due to invalid input. */
  40497. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  40498. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  40499. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  40500. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  40501. info_stack = NULL;
  40502. BIO_free(cert);
  40503. cert = NULL;
  40504. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  40505. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  40506. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  40507. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  40508. BIO_free(cert);
  40509. #endif
  40510. return EXPECT_RESULT();
  40511. }
  40512. #endif
  40513. static int test_wolfSSL_X509_subject_name_hash(void)
  40514. {
  40515. EXPECT_DECLS;
  40516. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  40517. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  40518. X509* x509 = NULL;
  40519. X509_NAME* subjectName = NULL;
  40520. unsigned long ret1 = 0;
  40521. unsigned long ret2 = 0;
  40522. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  40523. SSL_FILETYPE_PEM));
  40524. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  40525. /* These two
  40526. * - X509_subject_name_hash(x509)
  40527. * - X509_NAME_hash(X509_get_subject_name(x509))
  40528. * should give the same hash, if !defined(NO_SHA) is true. */
  40529. ret1 = X509_subject_name_hash(x509);
  40530. ExpectIntNE(ret1, 0);
  40531. #if !defined(NO_SHA)
  40532. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  40533. ExpectIntNE(ret2, 0);
  40534. ExpectIntEQ(ret1, ret2);
  40535. #else
  40536. (void) ret2;
  40537. #endif
  40538. X509_free(x509);
  40539. #endif
  40540. return EXPECT_RESULT();
  40541. }
  40542. static int test_wolfSSL_X509_issuer_name_hash(void)
  40543. {
  40544. EXPECT_DECLS;
  40545. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  40546. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  40547. X509* x509 = NULL;
  40548. X509_NAME* issuertName = NULL;
  40549. unsigned long ret1 = 0;
  40550. unsigned long ret2 = 0;
  40551. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  40552. SSL_FILETYPE_PEM));
  40553. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  40554. /* These two
  40555. * - X509_issuer_name_hash(x509)
  40556. * - X509_NAME_hash(X509_get_issuer_name(x509))
  40557. * should give the same hash, if !defined(NO_SHA) is true. */
  40558. ret1 = X509_issuer_name_hash(x509);
  40559. ExpectIntNE(ret1, 0);
  40560. #if !defined(NO_SHA)
  40561. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  40562. ExpectIntNE(ret2, 0);
  40563. ExpectIntEQ(ret1, ret2);
  40564. #else
  40565. (void) ret2;
  40566. #endif
  40567. X509_free(x509);
  40568. #endif
  40569. return EXPECT_RESULT();
  40570. }
  40571. static int test_wolfSSL_X509_check_host(void)
  40572. {
  40573. EXPECT_DECLS;
  40574. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  40575. && !defined(NO_SHA) && !defined(NO_RSA)
  40576. X509* x509 = NULL;
  40577. const char altName[] = "example.com";
  40578. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  40579. SSL_FILETYPE_PEM));
  40580. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  40581. WOLFSSL_SUCCESS);
  40582. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  40583. WOLFSSL_FAILURE);
  40584. X509_free(x509);
  40585. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  40586. WOLFSSL_FAILURE);
  40587. #endif
  40588. return EXPECT_RESULT();
  40589. }
  40590. static int test_wolfSSL_X509_check_email(void)
  40591. {
  40592. EXPECT_DECLS;
  40593. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  40594. X509* x509 = NULL;
  40595. const char goodEmail[] = "info@wolfssl.com";
  40596. const char badEmail[] = "disinfo@wolfssl.com";
  40597. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  40598. SSL_FILETYPE_PEM));
  40599. /* Should fail on non-matching email address */
  40600. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  40601. WOLFSSL_FAILURE);
  40602. /* Should succeed on matching email address */
  40603. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  40604. WOLFSSL_SUCCESS);
  40605. /* Should compute length internally when not provided */
  40606. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  40607. WOLFSSL_SUCCESS);
  40608. /* Should fail when email address is NULL */
  40609. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  40610. WOLFSSL_FAILURE);
  40611. X509_free(x509);
  40612. /* Should fail when x509 is NULL */
  40613. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  40614. WOLFSSL_FAILURE);
  40615. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  40616. return EXPECT_RESULT();
  40617. }
  40618. static int test_wc_PemToDer(void)
  40619. {
  40620. EXPECT_DECLS;
  40621. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  40622. int ret;
  40623. DerBuffer* pDer = NULL;
  40624. const char* ca_cert = "./certs/server-cert.pem";
  40625. byte* cert_buf = NULL;
  40626. size_t cert_sz = 0;
  40627. int eccKey = 0;
  40628. EncryptedInfo info;
  40629. XMEMSET(&info, 0, sizeof(info));
  40630. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  40631. ExpectIntEQ(ret = wc_PemToDer(cert_buf, (long int)cert_sz, CERT_TYPE, &pDer, NULL,
  40632. &info, &eccKey), 0);
  40633. wc_FreeDer(&pDer);
  40634. pDer = NULL;
  40635. if (cert_buf != NULL) {
  40636. free(cert_buf);
  40637. cert_buf = NULL;
  40638. }
  40639. #ifdef HAVE_ECC
  40640. {
  40641. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  40642. byte key_buf[256] = {0};
  40643. /* Test fail of loading a key with cert type */
  40644. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  40645. key_buf[0] = '\n';
  40646. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  40647. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  40648. &pDer, NULL, &info, &eccKey)), 0);
  40649. #ifdef OPENSSL_EXTRA
  40650. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  40651. &pDer, NULL, &info, &eccKey)), 0);
  40652. #endif
  40653. wc_FreeDer(&pDer);
  40654. if (cert_buf != NULL)
  40655. free(cert_buf);
  40656. }
  40657. #endif
  40658. #endif
  40659. return EXPECT_RESULT();
  40660. }
  40661. static int test_wc_AllocDer(void)
  40662. {
  40663. EXPECT_DECLS;
  40664. #if !defined(NO_CERTS)
  40665. DerBuffer* pDer = NULL;
  40666. word32 testSize = 1024;
  40667. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  40668. BAD_FUNC_ARG);
  40669. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  40670. ExpectNotNull(pDer);
  40671. wc_FreeDer(&pDer);
  40672. #endif
  40673. return EXPECT_RESULT();
  40674. }
  40675. static int test_wc_CertPemToDer(void)
  40676. {
  40677. EXPECT_DECLS;
  40678. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  40679. const char* ca_cert = "./certs/ca-cert.pem";
  40680. byte* cert_buf = NULL;
  40681. size_t cert_sz = 0;
  40682. size_t cert_dersz = 0;
  40683. byte* cert_der = NULL;
  40684. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  40685. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  40686. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  40687. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  40688. (int)cert_dersz, CERT_TYPE), 0);
  40689. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  40690. BAD_FUNC_ARG);
  40691. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  40692. BAD_FUNC_ARG);
  40693. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  40694. BAD_FUNC_ARG);
  40695. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  40696. CERT_TYPE), BAD_FUNC_ARG);
  40697. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  40698. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  40699. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  40700. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  40701. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  40702. CERT_TYPE), BAD_FUNC_ARG);
  40703. if (cert_der != NULL)
  40704. free(cert_der);
  40705. if (cert_buf != NULL)
  40706. free(cert_buf);
  40707. #endif
  40708. return EXPECT_RESULT();
  40709. }
  40710. static int test_wc_KeyPemToDer(void)
  40711. {
  40712. EXPECT_DECLS;
  40713. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  40714. int ret = 0;
  40715. const byte cert_buf[] = \
  40716. "-----BEGIN PRIVATE KEY-----\n"
  40717. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  40718. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  40719. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  40720. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  40721. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  40722. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  40723. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  40724. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  40725. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  40726. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  40727. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  40728. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  40729. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  40730. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  40731. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  40732. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  40733. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  40734. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  40735. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  40736. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  40737. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  40738. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  40739. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  40740. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  40741. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  40742. "AQue5o6oVfhgLiJzMdo/77gw\n"
  40743. "-----END PRIVATE KEY-----\n";
  40744. const int cert_sz = sizeof(cert_buf);
  40745. const char cert_pw[] = "password";
  40746. int cert_dersz = 0;
  40747. byte* cert_der = NULL;
  40748. /* Bad arg: Cert buffer is NULL */
  40749. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  40750. BAD_FUNC_ARG);
  40751. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  40752. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  40753. BAD_FUNC_ARG);
  40754. /* Test normal operation */
  40755. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  40756. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  40757. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  40758. cert_pw), 0);
  40759. ExpectIntLE(ret, cert_sz);
  40760. if (cert_der != NULL) {
  40761. free(cert_der);
  40762. cert_der = NULL;
  40763. }
  40764. /* Test NULL for DER buffer to return needed DER buffer size */
  40765. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  40766. ExpectIntLE(ret, cert_sz);
  40767. if (EXPECT_SUCCESS())
  40768. cert_dersz = ret;
  40769. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  40770. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  40771. cert_pw), 0);
  40772. ExpectIntLE(ret, cert_sz);
  40773. if (cert_der != NULL)
  40774. free(cert_der);
  40775. #endif
  40776. return EXPECT_RESULT();
  40777. }
  40778. static int test_wc_PubKeyPemToDer(void)
  40779. {
  40780. EXPECT_DECLS;
  40781. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  40782. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  40783. int ret = 0;
  40784. const char* key = "./certs/ecc-client-keyPub.pem";
  40785. byte* cert_buf = NULL;
  40786. size_t cert_sz = 0, cert_dersz = 0;
  40787. byte* cert_der = NULL;
  40788. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  40789. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  40790. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  40791. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  40792. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  40793. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  40794. (int)cert_dersz), 0);
  40795. if (cert_der != NULL) {
  40796. free(cert_der);
  40797. cert_der = NULL;
  40798. }
  40799. /* Test NULL for DER buffer to return needed DER buffer size */
  40800. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  40801. ExpectIntLE(ret, cert_sz);
  40802. cert_dersz = (size_t)ret;
  40803. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  40804. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  40805. (int)cert_dersz), 0);
  40806. if (cert_der != NULL) {
  40807. free(cert_der);
  40808. }
  40809. if (cert_buf != NULL) {
  40810. free(cert_buf);
  40811. }
  40812. #endif
  40813. return EXPECT_RESULT();
  40814. }
  40815. static int test_wc_PemPubKeyToDer(void)
  40816. {
  40817. EXPECT_DECLS;
  40818. #if !defined(NO_FILESYSTEM) && \
  40819. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  40820. const char* key = "./certs/ecc-client-keyPub.pem";
  40821. size_t cert_dersz = 1024;
  40822. byte* cert_der = NULL;
  40823. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  40824. BAD_FUNC_ARG);
  40825. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  40826. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  40827. if (cert_der != NULL) {
  40828. free(cert_der);
  40829. }
  40830. #endif
  40831. return EXPECT_RESULT();
  40832. }
  40833. static int test_wc_GetPubKeyDerFromCert(void)
  40834. {
  40835. EXPECT_DECLS;
  40836. #if !defined(NO_RSA) || defined(HAVE_ECC)
  40837. int ret;
  40838. word32 idx = 0;
  40839. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  40840. word32 keyDerSz = (word32)sizeof(keyDer);
  40841. DecodedCert decoded;
  40842. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  40843. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  40844. word32 certBufSz = sizeof(certBuf);
  40845. #endif
  40846. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  40847. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  40848. XFILE fp = XBADFILE;
  40849. #endif
  40850. #ifndef NO_RSA
  40851. RsaKey rsaKey;
  40852. #if defined(USE_CERT_BUFFERS_2048)
  40853. byte* rsaCertDer = (byte*)client_cert_der_2048;
  40854. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  40855. #elif defined(USE_CERT_BUFFERS_1024)
  40856. byte* rsaCertDer = (byte*)client_cert_der_1024;
  40857. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  40858. #else
  40859. unsigned char rsaCertDer[TWOK_BUF];
  40860. word32 rsaCertDerSz;
  40861. #endif
  40862. #endif
  40863. #ifdef HAVE_ECC
  40864. ecc_key eccKey;
  40865. #if defined(USE_CERT_BUFFERS_256)
  40866. byte* eccCert = (byte*)cliecc_cert_der_256;
  40867. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  40868. #else
  40869. unsigned char eccCert[ONEK_BUF];
  40870. word32 eccCertSz;
  40871. XFILE fp2 = XBADFILE;
  40872. #endif
  40873. #endif
  40874. #ifndef NO_RSA
  40875. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  40876. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  40877. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  40878. fp), 0);
  40879. if (fp != XBADFILE) {
  40880. XFCLOSE(fp);
  40881. fp = XBADFILE;
  40882. }
  40883. #endif
  40884. /* good test case - RSA DER cert */
  40885. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  40886. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  40887. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  40888. ExpectIntGT(keyDerSz, 0);
  40889. /* sanity check, verify we can import DER public key */
  40890. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  40891. ExpectIntEQ(ret, 0);
  40892. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  40893. if (ret == 0) {
  40894. wc_FreeRsaKey(&rsaKey);
  40895. }
  40896. /* test LENGTH_ONLY_E case */
  40897. keyDerSz = 0;
  40898. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  40899. LENGTH_ONLY_E);
  40900. ExpectIntGT(keyDerSz, 0);
  40901. /* bad args: DecodedCert NULL */
  40902. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  40903. /* bad args: output key buff size */
  40904. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  40905. /* bad args: zero size output key buffer */
  40906. keyDerSz = 0;
  40907. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  40908. BAD_FUNC_ARG);
  40909. wc_FreeDecodedCert(&decoded);
  40910. /* Certificate Request Tests */
  40911. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  40912. {
  40913. XMEMSET(certBuf, 0, sizeof(certBuf));
  40914. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  40915. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  40916. if (fp != XBADFILE) {
  40917. XFCLOSE(fp);
  40918. }
  40919. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  40920. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  40921. /* good test case - RSA DER certificate request */
  40922. keyDerSz = sizeof(keyDer);
  40923. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  40924. 0);
  40925. ExpectIntGT(keyDerSz, 0);
  40926. /* sanity check, verify we can import DER public key */
  40927. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  40928. ExpectIntEQ(ret, 0);
  40929. idx = 0;
  40930. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  40931. if (ret == 0) {
  40932. wc_FreeRsaKey(&rsaKey);
  40933. }
  40934. wc_FreeDecodedCert(&decoded);
  40935. }
  40936. #endif /* WOLFSSL_CERT_REQ */
  40937. #endif /* NO_RSA */
  40938. #ifdef HAVE_ECC
  40939. #ifndef USE_CERT_BUFFERS_256
  40940. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  40941. XBADFILE);
  40942. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  40943. if (fp2 != XBADFILE) {
  40944. XFCLOSE(fp2);
  40945. }
  40946. #endif
  40947. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  40948. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  40949. /* good test case - ECC */
  40950. XMEMSET(keyDer, 0, sizeof(keyDer));
  40951. keyDerSz = sizeof(keyDer);
  40952. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  40953. ExpectIntGT(keyDerSz, 0);
  40954. /* sanity check, verify we can import DER public key */
  40955. ret = wc_ecc_init(&eccKey);
  40956. ExpectIntEQ(ret, 0);
  40957. idx = 0; /* reset idx to 0, used above in RSA case */
  40958. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  40959. if (ret == 0) {
  40960. wc_ecc_free(&eccKey);
  40961. }
  40962. /* test LENGTH_ONLY_E case */
  40963. keyDerSz = 0;
  40964. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  40965. LENGTH_ONLY_E);
  40966. ExpectIntGT(keyDerSz, 0);
  40967. wc_FreeDecodedCert(&decoded);
  40968. #endif
  40969. #endif /* !NO_RSA || HAVE_ECC */
  40970. return EXPECT_RESULT();
  40971. }
  40972. static int test_wc_CheckCertSigPubKey(void)
  40973. {
  40974. EXPECT_DECLS;
  40975. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  40976. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  40977. int ret = 0;
  40978. const char* ca_cert = "./certs/ca-cert.pem";
  40979. byte* cert_buf = NULL;
  40980. size_t cert_sz = 0;
  40981. byte* cert_der = NULL;
  40982. word32 cert_dersz = 0;
  40983. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  40984. word32 keyDerSz = (word32)sizeof(keyDer);
  40985. DecodedCert decoded;
  40986. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  40987. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  40988. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  40989. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  40990. (int)cert_dersz, CERT_TYPE), 0);
  40991. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  40992. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  40993. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  40994. ExpectIntGT(keyDerSz, 0);
  40995. /* Good test case. */
  40996. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  40997. keyDerSz, RSAk), 0);
  40998. /* No certificate. */
  40999. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  41000. ECDSAk), BAD_FUNC_ARG);
  41001. /* Bad cert size. */
  41002. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  41003. RSAk), 0);
  41004. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  41005. /* No public key. */
  41006. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  41007. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  41008. /* Bad public key size. */
  41009. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  41010. RSAk), BAD_FUNC_ARG);
  41011. /* Wrong aglo. */
  41012. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  41013. keyDerSz, ECDSAk), ASN_PARSE_E);
  41014. wc_FreeDecodedCert(&decoded);
  41015. if (cert_der != NULL)
  41016. free(cert_der);
  41017. if (cert_buf != NULL)
  41018. free(cert_buf);
  41019. #endif
  41020. return EXPECT_RESULT();
  41021. }
  41022. static int test_wolfSSL_certs(void)
  41023. {
  41024. EXPECT_DECLS;
  41025. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  41026. !defined(NO_RSA)
  41027. X509* x509ext = NULL;
  41028. X509* x509 = NULL;
  41029. #ifdef OPENSSL_ALL
  41030. WOLFSSL_X509_EXTENSION* ext = NULL;
  41031. ASN1_OBJECT* obj = NULL;
  41032. #endif
  41033. WOLFSSL* ssl = NULL;
  41034. WOLFSSL_CTX* ctx = NULL;
  41035. STACK_OF(ASN1_OBJECT)* sk = NULL;
  41036. ASN1_STRING* asn1_str = NULL;
  41037. AUTHORITY_KEYID* akey = NULL;
  41038. BASIC_CONSTRAINTS* bc = NULL;
  41039. int crit = 0;
  41040. #ifndef NO_WOLFSSL_SERVER
  41041. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  41042. #else
  41043. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  41044. #endif
  41045. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  41046. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  41047. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  41048. #if !defined(NO_CHECK_PRIVATE_KEY)
  41049. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  41050. #endif
  41051. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  41052. #if !defined(NO_CHECK_PRIVATE_KEY)
  41053. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  41054. #endif
  41055. ExpectNotNull(ssl = SSL_new(ctx));
  41056. /* Invalid parameters. */
  41057. ExpectIntEQ(SSL_use_certificate_file(NULL, NULL, WOLFSSL_FILETYPE_PEM),
  41058. BAD_FUNC_ARG);
  41059. ExpectIntEQ(SSL_use_certificate_file(ssl, NULL, WOLFSSL_FILETYPE_PEM),
  41060. WOLFSSL_FAILURE);
  41061. ExpectIntEQ(SSL_use_certificate_file(NULL, "./certs/server-cert.pem",
  41062. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  41063. #if !defined(NO_CHECK_PRIVATE_KEY)
  41064. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41065. #endif
  41066. #ifdef HAVE_PK_CALLBACKS
  41067. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  41068. #endif /* HAVE_PK_CALLBACKS */
  41069. /* Invalid parameters. */
  41070. ExpectNotNull(x509 = wolfSSL_X509_new());
  41071. ExpectIntEQ(SSL_use_certificate(NULL, NULL), WOLFSSL_FAILURE);
  41072. ExpectIntEQ(SSL_use_certificate(ssl, NULL), WOLFSSL_FAILURE);
  41073. ExpectIntEQ(SSL_use_certificate(NULL, x509), WOLFSSL_FAILURE);
  41074. /* No data in certificate. */
  41075. ExpectIntEQ(SSL_use_certificate(ssl, x509), WOLFSSL_FAILURE);
  41076. wolfSSL_X509_free(x509);
  41077. x509 = NULL;
  41078. /* create and use x509 */
  41079. #ifdef OPENSSL_ALL
  41080. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  41081. WOLFSSL_FILETYPE_PEM));
  41082. #endif
  41083. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  41084. WOLFSSL_FILETYPE_PEM));
  41085. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  41086. #if !defined(NO_CHECK_PRIVATE_KEY)
  41087. /* with loading in a new cert the check on private key should now fail */
  41088. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41089. #endif
  41090. #if defined(USE_CERT_BUFFERS_2048)
  41091. /* Invalid parameters. */
  41092. ExpectIntEQ(SSL_use_certificate_ASN1(NULL, NULL, 0), WOLFSSL_FAILURE);
  41093. ExpectIntEQ(SSL_use_certificate_ASN1(ssl, NULL, 0), WOLFSSL_FAILURE);
  41094. ExpectIntEQ(SSL_use_certificate_ASN1(NULL,
  41095. (unsigned char*)server_cert_der_2048, 0), WOLFSSL_FAILURE);
  41096. /* No data. */
  41097. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  41098. (unsigned char*)server_cert_der_2048, 0), WOLFSSL_FAILURE);
  41099. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  41100. (unsigned char*)server_cert_der_2048,
  41101. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  41102. #endif
  41103. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  41104. /************* Get Digest of Certificate ******************/
  41105. {
  41106. byte digest[64]; /* max digest size */
  41107. word32 digestSz;
  41108. XMEMSET(digest, 0, sizeof(digest));
  41109. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  41110. WOLFSSL_SUCCESS);
  41111. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  41112. WOLFSSL_SUCCESS);
  41113. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  41114. WOLFSSL_FAILURE);
  41115. }
  41116. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  41117. /* test and checkout X509 extensions */
  41118. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  41119. NID_basic_constraints, &crit, NULL));
  41120. ExpectIntEQ(crit, 0);
  41121. #ifdef OPENSSL_ALL
  41122. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  41123. X509_EXTENSION_free(ext);
  41124. ext = NULL;
  41125. ExpectNotNull(ext = X509_EXTENSION_new());
  41126. X509_EXTENSION_set_critical(ext, 1);
  41127. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  41128. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  41129. ASN1_OBJECT_free(obj);
  41130. obj = NULL;
  41131. X509_EXTENSION_free(ext);
  41132. ext = NULL;
  41133. ExpectNotNull(ext = X509_EXTENSION_new());
  41134. X509_EXTENSION_set_critical(ext, 0);
  41135. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  41136. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  41137. NULL);
  41138. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  41139. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  41140. * and X509_get_ext_d2i has created new */
  41141. asn1_str = NULL;
  41142. X509_EXTENSION_free(ext);
  41143. ext = NULL;
  41144. #endif
  41145. BASIC_CONSTRAINTS_free(bc);
  41146. bc = NULL;
  41147. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  41148. NID_key_usage, &crit, NULL));
  41149. ExpectIntEQ(crit, 1);
  41150. ExpectIntEQ(asn1_str->type, NID_key_usage);
  41151. #ifdef OPENSSL_ALL
  41152. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  41153. X509_EXTENSION_free(ext);
  41154. ext = NULL;
  41155. #endif
  41156. ASN1_STRING_free(asn1_str);
  41157. asn1_str = NULL;
  41158. #ifdef OPENSSL_ALL
  41159. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  41160. NID_ext_key_usage, &crit, NULL));
  41161. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  41162. X509_EXTENSION_free(ext);
  41163. ext = NULL;
  41164. EXTENDED_KEY_USAGE_free(sk);
  41165. sk = NULL;
  41166. #else
  41167. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  41168. &crit, NULL);
  41169. ExpectNull(sk);
  41170. #endif
  41171. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  41172. NID_authority_key_identifier, &crit, NULL));
  41173. #ifdef OPENSSL_ALL
  41174. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  41175. akey));
  41176. X509_EXTENSION_free(ext);
  41177. ext = NULL;
  41178. #endif
  41179. wolfSSL_AUTHORITY_KEYID_free(akey);
  41180. akey = NULL;
  41181. /* NID not yet supported */
  41182. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  41183. NID_private_key_usage_period, &crit, NULL));
  41184. ExpectIntEQ(crit, -1);
  41185. sk_ASN1_OBJECT_free(sk);
  41186. sk = NULL;
  41187. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  41188. NID_subject_alt_name, &crit, NULL));
  41189. {
  41190. int i;
  41191. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  41192. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  41193. ExpectIntEQ(gen->type, GEN_DNS);
  41194. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  41195. }
  41196. }
  41197. sk_GENERAL_NAME_free(sk);
  41198. sk = NULL;
  41199. /* NID not yet supported */
  41200. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  41201. NID_issuer_alt_name, &crit, NULL));
  41202. ExpectIntEQ(crit, -1);
  41203. sk_ASN1_OBJECT_free(sk);
  41204. sk = NULL;
  41205. /* NID not yet supported */
  41206. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  41207. NID_info_access, &crit, NULL));
  41208. sk_ASN1_OBJECT_free(sk);
  41209. sk = NULL;
  41210. /* NID not yet supported */
  41211. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  41212. NID_sinfo_access, &crit, NULL));
  41213. ExpectIntEQ(crit, -1);
  41214. sk_ASN1_OBJECT_free(sk);
  41215. sk = NULL;
  41216. /* NID not yet supported */
  41217. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  41218. NID_name_constraints, &crit, NULL));
  41219. ExpectIntEQ(crit, -1);
  41220. sk_ASN1_OBJECT_free(sk);
  41221. sk = NULL;
  41222. /* no cert policy set */
  41223. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  41224. NID_certificate_policies, &crit, NULL));
  41225. sk_ASN1_OBJECT_free(sk);
  41226. sk = NULL;
  41227. /* NID not yet supported */
  41228. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  41229. NID_policy_mappings, &crit, NULL));
  41230. ExpectIntEQ(crit, -1);
  41231. sk_ASN1_OBJECT_free(sk);
  41232. sk = NULL;
  41233. /* NID not yet supported */
  41234. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  41235. NID_policy_constraints, &crit, NULL));
  41236. ExpectIntEQ(crit, -1);
  41237. sk_ASN1_OBJECT_free(sk);
  41238. sk = NULL;
  41239. /* NID not yet supported */
  41240. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  41241. NID_inhibit_any_policy, &crit, NULL));
  41242. ExpectIntEQ(crit, -1);
  41243. sk_ASN1_OBJECT_free(sk);
  41244. sk = NULL;
  41245. /* NID not yet supported */
  41246. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  41247. NID_tlsfeature, &crit, NULL));
  41248. ExpectIntEQ(crit, -1);
  41249. sk_ASN1_OBJECT_free(sk);
  41250. sk = NULL;
  41251. /* test invalid cases */
  41252. crit = 0;
  41253. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  41254. NULL));
  41255. ExpectIntEQ(crit, -1);
  41256. /* NULL passed for criticality. */
  41257. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  41258. NID_tlsfeature, NULL, NULL));
  41259. ExpectIntEQ(SSL_get_hit(ssl), 0);
  41260. #ifdef OPENSSL_ALL
  41261. X509_free(x509);
  41262. #endif
  41263. X509_free(x509ext);
  41264. SSL_free(ssl);
  41265. SSL_CTX_free(ctx);
  41266. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  41267. return EXPECT_RESULT();
  41268. }
  41269. static int test_wolfSSL_X509_check_private_key(void)
  41270. {
  41271. EXPECT_DECLS;
  41272. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  41273. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  41274. X509* x509 = NULL;
  41275. EVP_PKEY* pkey = NULL;
  41276. const byte* key;
  41277. /* Check with correct key */
  41278. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  41279. SSL_FILETYPE_PEM)));
  41280. key = client_key_der_2048;
  41281. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  41282. (long)sizeof_client_key_der_2048));
  41283. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  41284. EVP_PKEY_free(pkey);
  41285. pkey = NULL;
  41286. /* Check with wrong key */
  41287. key = server_key_der_2048;
  41288. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  41289. (long)sizeof_server_key_der_2048));
  41290. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  41291. /* test for incorrect parameter */
  41292. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  41293. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  41294. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  41295. EVP_PKEY_free(pkey);
  41296. X509_free(x509);
  41297. #endif
  41298. return EXPECT_RESULT();
  41299. }
  41300. static int test_wolfSSL_private_keys(void)
  41301. {
  41302. EXPECT_DECLS;
  41303. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  41304. !defined(NO_FILESYSTEM)
  41305. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  41306. WOLFSSL* ssl = NULL;
  41307. WOLFSSL_CTX* ctx = NULL;
  41308. EVP_PKEY* pkey = NULL;
  41309. OpenSSL_add_all_digests();
  41310. OpenSSL_add_all_algorithms();
  41311. #ifndef NO_RSA
  41312. #ifndef NO_WOLFSSL_SERVER
  41313. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41314. #else
  41315. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  41316. #endif
  41317. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  41318. WOLFSSL_FILETYPE_PEM));
  41319. /* Have to load a cert before you can check the private key against that
  41320. * certificates public key! */
  41321. #if !defined(NO_CHECK_PRIVATE_KEY)
  41322. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  41323. #endif
  41324. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  41325. WOLFSSL_FILETYPE_PEM));
  41326. #if !defined(NO_CHECK_PRIVATE_KEY)
  41327. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  41328. #endif
  41329. ExpectNotNull(ssl = SSL_new(ctx));
  41330. #if !defined(NO_CHECK_PRIVATE_KEY)
  41331. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41332. #endif
  41333. /* Invalid parameters. */
  41334. ExpectIntEQ(SSL_use_PrivateKey_file(NULL, NULL, WOLFSSL_FILETYPE_PEM),
  41335. BAD_FUNC_ARG);
  41336. ExpectIntEQ(SSL_use_PrivateKey_file(NULL, svrKeyFile, WOLFSSL_FILETYPE_PEM),
  41337. BAD_FUNC_ARG);
  41338. ExpectIntEQ(SSL_use_PrivateKey_file(ssl, NULL, WOLFSSL_FILETYPE_PEM),
  41339. WOLFSSL_FAILURE);
  41340. #ifdef USE_CERT_BUFFERS_2048
  41341. {
  41342. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  41343. unsigned char buf[FOURK_BUF];
  41344. word32 bufSz;
  41345. /* Invalid parameters. */
  41346. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(NULL, NULL, 0), WOLFSSL_FAILURE);
  41347. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl, NULL, 0), WOLFSSL_FAILURE);
  41348. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(NULL,
  41349. (unsigned char*)client_key_der_2048, 0), WOLFSSL_FAILURE);
  41350. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, NULL, NULL, 0), WOLFSSL_FAILURE);
  41351. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl, NULL, 0), WOLFSSL_FAILURE);
  41352. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, NULL, (unsigned char*)server_key, 0),
  41353. WOLFSSL_FAILURE);
  41354. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, NULL, NULL, 0), WOLFSSL_FAILURE);
  41355. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx, NULL, 0), WOLFSSL_FAILURE);
  41356. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, NULL, (unsigned char*)server_key,
  41357. 0), WOLFSSL_FAILURE);
  41358. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  41359. (unsigned char*)client_key_der_2048,
  41360. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  41361. #if !defined(NO_CHECK_PRIVATE_KEY)
  41362. /* Should mismatch now that a different private key loaded */
  41363. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41364. #endif
  41365. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  41366. (unsigned char*)server_key,
  41367. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  41368. #if !defined(NO_CHECK_PRIVATE_KEY)
  41369. /* After loading back in DER format of original key, should match */
  41370. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41371. #endif
  41372. /* test loading private key to the WOLFSSL_CTX */
  41373. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  41374. (unsigned char*)client_key_der_2048,
  41375. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  41376. #if !defined(NO_CHECK_PRIVATE_KEY)
  41377. /* Should mismatch now that a different private key loaded */
  41378. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  41379. #endif
  41380. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  41381. (unsigned char*)server_key,
  41382. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  41383. #if !defined(NO_CHECK_PRIVATE_KEY)
  41384. /* After loading back in DER format of original key, should match */
  41385. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  41386. #endif
  41387. /* Invalid parameters. */
  41388. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41389. ExpectIntEQ(SSL_use_PrivateKey(NULL, NULL), WOLFSSL_FAILURE);
  41390. ExpectIntEQ(SSL_use_PrivateKey(ssl, NULL), WOLFSSL_FAILURE);
  41391. ExpectIntEQ(SSL_use_PrivateKey(NULL, pkey), WOLFSSL_FAILURE);
  41392. /* pkey is empty - no key data to use. */
  41393. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), ASN_PARSE_E);
  41394. wolfSSL_EVP_PKEY_free(pkey);
  41395. pkey = NULL;
  41396. /* set PKEY and test again */
  41397. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  41398. &server_key, (long)sizeof_server_key_der_2048));
  41399. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  41400. /* reuse PKEY structure and test
  41401. * this should be checked with a memory management sanity checker */
  41402. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  41403. server_key = (const unsigned char*)server_key_der_2048;
  41404. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  41405. &server_key, (long)sizeof_server_key_der_2048));
  41406. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  41407. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  41408. bufSz = FOURK_BUF;
  41409. ExpectIntGT((bufSz = (word32)wc_CreatePKCS8Key(buf, &bufSz,
  41410. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  41411. RSAk, NULL, 0)), 0);
  41412. server_key = (const unsigned char*)buf;
  41413. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  41414. (long)bufSz));
  41415. }
  41416. #endif
  41417. EVP_PKEY_free(pkey);
  41418. pkey = NULL;
  41419. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  41420. ssl = NULL;
  41421. SSL_CTX_free(ctx);
  41422. ctx = NULL;
  41423. #endif /* end of RSA private key match tests */
  41424. #ifdef HAVE_ECC
  41425. #ifndef NO_WOLFSSL_SERVER
  41426. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41427. #else
  41428. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  41429. #endif
  41430. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  41431. WOLFSSL_FILETYPE_PEM));
  41432. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  41433. WOLFSSL_FILETYPE_PEM));
  41434. ExpectNotNull(ssl = SSL_new(ctx));
  41435. #if !defined(NO_CHECK_PRIVATE_KEY)
  41436. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41437. #endif
  41438. SSL_free(ssl);
  41439. ssl = NULL;
  41440. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  41441. WOLFSSL_FILETYPE_PEM));
  41442. ExpectNotNull(ssl = SSL_new(ctx));
  41443. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  41444. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41445. #endif
  41446. SSL_free(ssl);
  41447. ssl = NULL;
  41448. SSL_CTX_free(ctx);
  41449. ctx = NULL;
  41450. #endif /* end of ECC private key match tests */
  41451. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  41452. #ifndef NO_WOLFSSL_SERVER
  41453. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41454. #else
  41455. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  41456. #endif
  41457. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  41458. WOLFSSL_FILETYPE_PEM));
  41459. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  41460. WOLFSSL_FILETYPE_PEM));
  41461. ExpectNotNull(ssl = SSL_new(ctx));
  41462. #if !defined(NO_CHECK_PRIVATE_KEY)
  41463. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41464. #endif
  41465. SSL_free(ssl);
  41466. ssl = NULL;
  41467. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  41468. WOLFSSL_FILETYPE_PEM));
  41469. ExpectNotNull(ssl = SSL_new(ctx));
  41470. #if !defined(NO_CHECK_PRIVATE_KEY)
  41471. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41472. #endif
  41473. SSL_free(ssl);
  41474. ssl = NULL;
  41475. SSL_CTX_free(ctx);
  41476. ctx = NULL;
  41477. #endif /* end of Ed25519 private key match tests */
  41478. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  41479. #ifndef NO_WOLFSSL_SERVER
  41480. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41481. #else
  41482. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  41483. #endif
  41484. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  41485. WOLFSSL_FILETYPE_PEM));
  41486. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  41487. WOLFSSL_FILETYPE_PEM));
  41488. ExpectNotNull(ssl = SSL_new(ctx));
  41489. #if !defined(NO_CHECK_PRIVATE_KEY)
  41490. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41491. #endif
  41492. SSL_free(ssl);
  41493. ssl = NULL;
  41494. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  41495. WOLFSSL_FILETYPE_PEM));
  41496. ExpectNotNull(ssl = SSL_new(ctx));
  41497. #if !defined(NO_CHECK_PRIVATE_KEY)
  41498. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  41499. #endif
  41500. SSL_free(ssl);
  41501. ssl = NULL;
  41502. SSL_CTX_free(ctx);
  41503. ctx = NULL;
  41504. #endif /* end of Ed448 private key match tests */
  41505. EVP_cleanup();
  41506. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  41507. CONF_modules_free();
  41508. ENGINE_cleanup();
  41509. CONF_modules_unload();
  41510. (void)ssl;
  41511. (void)ctx;
  41512. (void)pkey;
  41513. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  41514. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  41515. return EXPECT_RESULT();
  41516. }
  41517. static int test_wolfSSL_PEM_def_callback(void)
  41518. {
  41519. EXPECT_DECLS;
  41520. #ifdef OPENSSL_EXTRA
  41521. char buf[10];
  41522. const char* defpwd = "DEF PWD";
  41523. int defpwdLen = (int)XSTRLEN(defpwd);
  41524. int smallLen = 1;
  41525. /* Bad parameters. */
  41526. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, NULL), 0);
  41527. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, (void*)defpwd),
  41528. 0);
  41529. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, NULL), 0);
  41530. XMEMSET(buf, 0, sizeof(buf));
  41531. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, (void*)defpwd),
  41532. defpwdLen);
  41533. ExpectIntEQ(XMEMCMP(buf, defpwd, defpwdLen), 0);
  41534. ExpectIntEQ(buf[defpwdLen], 0);
  41535. /* Size of buffer is smaller than default password. */
  41536. XMEMSET(buf, 0, sizeof(buf));
  41537. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, smallLen, 0, (void*)defpwd),
  41538. smallLen);
  41539. ExpectIntEQ(XMEMCMP(buf, defpwd, smallLen), 0);
  41540. ExpectIntEQ(buf[smallLen], 0);
  41541. #endif /* OPENSSL_EXTRA */
  41542. return EXPECT_RESULT();
  41543. }
  41544. static int test_wolfSSL_PEM_read_PrivateKey(void)
  41545. {
  41546. EXPECT_DECLS;
  41547. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || \
  41548. !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH))
  41549. XFILE file = XBADFILE;
  41550. #if !defined(NO_RSA)
  41551. const char* fname_rsa = "./certs/server-key.pem";
  41552. RSA* rsa = NULL;
  41553. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41554. unsigned char* sig = NULL;
  41555. size_t sigLen = 0;
  41556. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  41557. size_t tbsLen = sizeof(tbs);
  41558. #endif
  41559. #if !defined(NO_DSA)
  41560. const char* fname_dsa = "./certs/dsa2048.pem";
  41561. #endif
  41562. #if defined(HAVE_ECC)
  41563. const char* fname_ec = "./certs/ecc-key.pem";
  41564. #endif
  41565. #if !defined(NO_DH)
  41566. const char* fname_dh = "./certs/dh-priv-2048.pem";
  41567. #endif
  41568. EVP_PKEY* pkey = NULL;
  41569. /* Check error case. */
  41570. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  41571. /* not a PEM key. */
  41572. ExpectTrue((file = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  41573. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  41574. if (file != XBADFILE)
  41575. XFCLOSE(file);
  41576. file = XBADFILE;
  41577. #ifndef NO_RSA
  41578. /* Read in an RSA key. */
  41579. ExpectTrue((file = XFOPEN(fname_rsa, "rb")) != XBADFILE);
  41580. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  41581. if (file != XBADFILE)
  41582. XFCLOSE(file);
  41583. file = XBADFILE;
  41584. /* Make sure the key is usable by signing some data with it. */
  41585. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  41586. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  41587. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  41588. DYNAMIC_TYPE_TMP_BUFFER));
  41589. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41590. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41591. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  41592. WOLFSSL_SUCCESS);
  41593. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41594. EVP_PKEY_CTX_free(ctx);
  41595. EVP_PKEY_free(pkey);
  41596. pkey = NULL;
  41597. #endif
  41598. #ifndef NO_DSA
  41599. /* Read in a DSA key. */
  41600. ExpectTrue((file = XFOPEN(fname_dsa, "rb")) != XBADFILE);
  41601. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  41602. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  41603. EVP_PKEY_free(pkey);
  41604. pkey = NULL;
  41605. #else
  41606. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  41607. #endif
  41608. if (file != XBADFILE)
  41609. XFCLOSE(file);
  41610. file = XBADFILE;
  41611. #endif
  41612. #ifdef HAVE_ECC
  41613. /* Read in an EC key. */
  41614. ExpectTrue((file = XFOPEN(fname_ec, "rb")) != XBADFILE);
  41615. ExpectNotNull(pkey = EVP_PKEY_new());
  41616. ExpectPtrEq(PEM_read_PrivateKey(file, &pkey, NULL, NULL), pkey);
  41617. if (file != XBADFILE)
  41618. XFCLOSE(file);
  41619. file = XBADFILE;
  41620. EVP_PKEY_free(pkey);
  41621. pkey = NULL;
  41622. #endif
  41623. #ifndef NO_DH
  41624. /* Read in a DH key. */
  41625. ExpectTrue((file = XFOPEN(fname_dh, "rb")) != XBADFILE);
  41626. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  41627. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  41628. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  41629. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  41630. EVP_PKEY_free(pkey);
  41631. pkey = NULL;
  41632. #else
  41633. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  41634. #endif
  41635. if (file != XBADFILE)
  41636. XFCLOSE(file);
  41637. file = XBADFILE;
  41638. #endif
  41639. #endif
  41640. return EXPECT_RESULT();
  41641. }
  41642. static int test_wolfSSL_PEM_read_PUBKEY(void)
  41643. {
  41644. EXPECT_DECLS;
  41645. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  41646. && !defined(NO_FILESYSTEM)
  41647. XFILE file = XBADFILE;
  41648. const char* fname = "./certs/client-keyPub.pem";
  41649. EVP_PKEY* pkey = NULL;
  41650. /* Check error case. */
  41651. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  41652. /* Read in an RSA key. */
  41653. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41654. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  41655. EVP_PKEY_free(pkey);
  41656. pkey = NULL;
  41657. if (file != XBADFILE)
  41658. XFCLOSE(file);
  41659. file = XBADFILE;
  41660. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41661. ExpectNotNull(pkey = EVP_PKEY_new());
  41662. ExpectPtrEq(PEM_read_PUBKEY(file, &pkey, NULL, NULL), pkey);
  41663. EVP_PKEY_free(pkey);
  41664. if (file != XBADFILE)
  41665. XFCLOSE(file);
  41666. #endif
  41667. return EXPECT_RESULT();
  41668. }
  41669. /* test loading RSA key using BIO */
  41670. static int test_wolfSSL_PEM_PrivateKey_rsa(void)
  41671. {
  41672. EXPECT_DECLS;
  41673. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  41674. defined(USE_CERT_BUFFERS_2048) && !defined(NO_FILESYSTEM) && \
  41675. !defined(NO_BIO)
  41676. BIO* bio = NULL;
  41677. XFILE file = XBADFILE;
  41678. const char* fname = "./certs/server-key.pem";
  41679. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  41680. EVP_PKEY* pkey = NULL;
  41681. size_t sz = 0;
  41682. byte* buf = NULL;
  41683. EVP_PKEY* pkey2 = NULL;
  41684. EVP_PKEY* pkey3 = NULL;
  41685. RSA* rsa_key = NULL;
  41686. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  41687. unsigned char extra[10];
  41688. int i;
  41689. BIO* pub_bio = NULL;
  41690. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  41691. #endif
  41692. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41693. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41694. ExpectIntGT(sz = XFTELL(file), 0);
  41695. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41696. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  41697. if (buf != NULL) {
  41698. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41699. }
  41700. if (file != XBADFILE) {
  41701. XFCLOSE(file);
  41702. file = XBADFILE;
  41703. }
  41704. /* Test using BIO new mem and loading PEM private key */
  41705. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41706. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  41707. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  41708. buf = NULL;
  41709. BIO_free(bio);
  41710. bio = NULL;
  41711. /* New empty EVP_PKEY */
  41712. ExpectNotNull(pkey2 = EVP_PKEY_new());
  41713. if (pkey2 != NULL) {
  41714. pkey2->type = EVP_PKEY_RSA;
  41715. }
  41716. /* Test parameter copy */
  41717. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  41718. EVP_PKEY_free(pkey2);
  41719. EVP_PKEY_free(pkey);
  41720. pkey = NULL;
  41721. /* Qt unit test case : rsa pkcs8 key */
  41722. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  41723. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41724. ExpectIntGT(sz = XFTELL(file), 0);
  41725. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41726. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  41727. if (buf) {
  41728. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41729. }
  41730. if (file != XBADFILE) {
  41731. XFCLOSE(file);
  41732. file = XBADFILE;
  41733. }
  41734. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41735. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  41736. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  41737. buf = NULL;
  41738. BIO_free(bio);
  41739. bio = NULL;
  41740. ExpectNotNull(pkey3 = EVP_PKEY_new());
  41741. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  41742. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  41743. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  41744. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  41745. #else
  41746. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  41747. #endif
  41748. RSA_free(rsa_key);
  41749. EVP_PKEY_free(pkey3);
  41750. EVP_PKEY_free(pkey);
  41751. pkey = NULL;
  41752. pkey2 = NULL;
  41753. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  41754. #define BIO_PEM_TEST_CHAR 'a'
  41755. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  41756. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  41757. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  41758. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  41759. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  41760. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key,
  41761. (long)sizeof_server_key_der_2048));
  41762. ExpectNull(pkey);
  41763. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  41764. (long)sizeof_server_key_der_2048));
  41765. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL, NULL),
  41766. WOLFSSL_FAILURE);
  41767. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL, NULL),
  41768. WOLFSSL_FAILURE);
  41769. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  41770. WOLFSSL_SUCCESS);
  41771. ExpectIntGT(BIO_pending(bio), 0);
  41772. ExpectIntEQ(BIO_pending(bio), 1679);
  41773. /* Check if the pubkey API writes only the public key */
  41774. #ifdef WOLFSSL_KEY_GEN
  41775. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  41776. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  41777. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  41778. ExpectIntGT(BIO_pending(pub_bio), 0);
  41779. /* Previously both the private key and the pubkey calls would write
  41780. * out the private key and the PEM header was the only difference.
  41781. * The public PEM should be significantly shorter than the
  41782. * private key versison. */
  41783. ExpectIntEQ(BIO_pending(pub_bio), 451);
  41784. #else
  41785. /* Not supported. */
  41786. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), 0);
  41787. #endif
  41788. /* test creating new EVP_PKEY with good args */
  41789. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  41790. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  41791. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  41792. pkey->pkey_sz), 0);
  41793. }
  41794. /* test of reuse of EVP_PKEY */
  41795. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  41796. ExpectIntEQ(BIO_pending(bio), 0);
  41797. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  41798. SSL_SUCCESS);
  41799. /* add 10 extra bytes after PEM */
  41800. ExpectIntEQ(BIO_write(bio, extra, 10), 10);
  41801. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  41802. ExpectNotNull(pkey);
  41803. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  41804. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  41805. pkey->pkey_sz), 0);
  41806. }
  41807. /* check 10 extra bytes still there */
  41808. ExpectIntEQ(BIO_pending(bio), 10);
  41809. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  41810. for (i = 0; i < 10; i++) {
  41811. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  41812. }
  41813. BIO_free(pub_bio);
  41814. BIO_free(bio);
  41815. bio = NULL;
  41816. EVP_PKEY_free(pkey);
  41817. pkey = NULL;
  41818. EVP_PKEY_free(pkey2);
  41819. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  41820. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 &&
  41821. * !NO_FILESYSTEM && !NO_BIO */
  41822. return EXPECT_RESULT();
  41823. }
  41824. /* test loading ECC key using BIO */
  41825. static int test_wolfSSL_PEM_PrivateKey_ecc(void)
  41826. {
  41827. EXPECT_DECLS;
  41828. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_ECC) && \
  41829. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  41830. BIO* bio = NULL;
  41831. EVP_PKEY* pkey = NULL;
  41832. XFILE file = XBADFILE;
  41833. const char* fname = "./certs/ecc-key.pem";
  41834. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  41835. size_t sz = 0;
  41836. byte* buf = NULL;
  41837. EVP_PKEY* pkey2 = NULL;
  41838. EVP_PKEY* pkey3 = NULL;
  41839. EC_KEY* ec_key = NULL;
  41840. int nid = 0;
  41841. BIO* pub_bio = NULL;
  41842. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41843. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41844. ExpectIntGT(sz = XFTELL(file), 0);
  41845. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41846. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  41847. if (buf) {
  41848. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41849. }
  41850. if (file != XBADFILE) {
  41851. XFCLOSE(file);
  41852. file = XBADFILE;
  41853. }
  41854. /* Test using BIO new mem and loading PEM private key */
  41855. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41856. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  41857. BIO_free(bio);
  41858. bio = NULL;
  41859. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  41860. buf = NULL;
  41861. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  41862. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  41863. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  41864. WOLFSSL_SUCCESS);
  41865. ExpectIntGT(BIO_pending(bio), 0);
  41866. /* No parameters. */
  41867. ExpectIntEQ(BIO_pending(bio), 227);
  41868. /* Check if the pubkey API writes only the public key */
  41869. #ifdef WOLFSSL_KEY_GEN
  41870. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  41871. ExpectIntGT(BIO_pending(pub_bio), 0);
  41872. /* Previously both the private key and the pubkey calls would write
  41873. * out the private key and the PEM header was the only difference.
  41874. * The public PEM should be significantly shorter than the
  41875. * private key versison. */
  41876. ExpectIntEQ(BIO_pending(pub_bio), 178);
  41877. #endif
  41878. BIO_free(pub_bio);
  41879. BIO_free(bio);
  41880. bio = NULL;
  41881. ExpectNotNull(pkey2 = EVP_PKEY_new());
  41882. ExpectNotNull(pkey3 = EVP_PKEY_new());
  41883. if (pkey2 != NULL) {
  41884. pkey2->type = EVP_PKEY_EC;
  41885. }
  41886. /* Test parameter copy */
  41887. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  41888. /* Qt unit test case 1*/
  41889. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  41890. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  41891. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  41892. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  41893. #else
  41894. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  41895. #endif
  41896. /* Test default digest */
  41897. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  41898. ExpectIntEQ(nid, NID_sha256);
  41899. EC_KEY_free(ec_key);
  41900. ec_key = NULL;
  41901. EVP_PKEY_free(pkey3);
  41902. pkey3 = NULL;
  41903. EVP_PKEY_free(pkey2);
  41904. pkey2 = NULL;
  41905. EVP_PKEY_free(pkey);
  41906. pkey = NULL;
  41907. /* Qt unit test case ec pkcs8 key */
  41908. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  41909. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41910. ExpectIntGT(sz = XFTELL(file), 0);
  41911. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41912. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  41913. if (buf) {
  41914. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41915. }
  41916. if (file != XBADFILE) {
  41917. XFCLOSE(file);
  41918. file = XBADFILE;
  41919. }
  41920. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41921. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  41922. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  41923. buf = NULL;
  41924. BIO_free(bio);
  41925. bio = NULL;
  41926. ExpectNotNull(pkey3 = EVP_PKEY_new());
  41927. /* Qt unit test case */
  41928. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  41929. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  41930. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  41931. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  41932. #else
  41933. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  41934. #endif
  41935. EC_KEY_free(ec_key);
  41936. EVP_PKEY_free(pkey3);
  41937. EVP_PKEY_free(pkey);
  41938. pkey = NULL;
  41939. #endif
  41940. return EXPECT_RESULT();
  41941. }
  41942. /* test loading DSA key using BIO */
  41943. static int test_wolfSSL_PEM_PrivateKey_dsa(void)
  41944. {
  41945. EXPECT_DECLS;
  41946. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DSA) && \
  41947. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  41948. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  41949. BIO* bio = NULL;
  41950. EVP_PKEY* pkey = NULL;
  41951. ExpectNotNull(bio = BIO_new_file("./certs/dsa2048.pem", "rb"));
  41952. /* Private DSA EVP_PKEY */
  41953. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  41954. NULL));
  41955. BIO_free(bio);
  41956. bio = NULL;
  41957. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  41958. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  41959. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  41960. NULL), 0);
  41961. #endif
  41962. #ifdef WOLFSSL_KEY_GEN
  41963. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 1);
  41964. ExpectIntEQ(BIO_pending(bio), 1178);
  41965. BIO_reset(bio);
  41966. #endif
  41967. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  41968. 1);
  41969. ExpectIntEQ(BIO_pending(bio), 1196);
  41970. BIO_free(bio);
  41971. bio = NULL;
  41972. EVP_PKEY_free(pkey);
  41973. pkey = NULL;
  41974. #endif
  41975. #endif
  41976. return EXPECT_RESULT();
  41977. }
  41978. /* test loading DH key using BIO */
  41979. static int test_wolfSSL_PEM_PrivateKey_dh(void)
  41980. {
  41981. EXPECT_DECLS;
  41982. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DH) && \
  41983. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  41984. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  41985. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  41986. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  41987. BIO* bio = NULL;
  41988. EVP_PKEY* pkey = NULL;
  41989. ExpectNotNull(bio = BIO_new_file("./certs/dh-priv-2048.pem", "rb"));
  41990. /* Private DH EVP_PKEY */
  41991. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  41992. NULL));
  41993. BIO_free(bio);
  41994. bio = NULL;
  41995. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  41996. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  41997. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  41998. NULL), 0);
  41999. #endif
  42000. #ifdef WOLFSSL_KEY_GEN
  42001. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 0);
  42002. #endif
  42003. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  42004. 1);
  42005. ExpectIntEQ(BIO_pending(bio), 806);
  42006. BIO_free(bio);
  42007. bio = NULL;
  42008. EVP_PKEY_free(pkey);
  42009. pkey = NULL;
  42010. #endif
  42011. #endif
  42012. return EXPECT_RESULT();
  42013. }
  42014. static int test_wolfSSL_PEM_PrivateKey(void)
  42015. {
  42016. EXPECT_DECLS;
  42017. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42018. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  42019. #ifndef NO_BIO
  42020. BIO* bio = NULL;
  42021. #endif
  42022. EVP_PKEY* pkey = NULL;
  42023. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  42024. #ifndef NO_BIO
  42025. /* test creating new EVP_PKEY with bad arg */
  42026. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  42027. /* Test bad EVP_PKEY type. */
  42028. /* New HMAC EVP_PKEY */
  42029. ExpectNotNull(bio = BIO_new_mem_buf("", 1));
  42030. ExpectNotNull(pkey = EVP_PKEY_new());
  42031. if (pkey != NULL) {
  42032. pkey->type = EVP_PKEY_HMAC;
  42033. }
  42034. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  42035. 0);
  42036. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  42037. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  42038. NULL), 0);
  42039. #endif
  42040. #ifdef WOLFSSL_KEY_GEN
  42041. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), WOLFSSL_FAILURE);
  42042. #endif
  42043. EVP_PKEY_free(pkey);
  42044. pkey = NULL;
  42045. BIO_free(bio);
  42046. bio = NULL;
  42047. /* key is DES encrypted */
  42048. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  42049. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  42050. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  42051. {
  42052. XFILE f = XBADFILE;
  42053. wc_pem_password_cb* passwd_cb = NULL;
  42054. void* passwd_cb_userdata;
  42055. SSL_CTX* ctx = NULL;
  42056. char passwd[] = "bad password";
  42057. #ifndef WOLFSSL_NO_TLS12
  42058. #ifndef NO_WOLFSSL_SERVER
  42059. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  42060. #else
  42061. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  42062. #endif
  42063. #else
  42064. #ifndef NO_WOLFSSL_SERVER
  42065. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  42066. #else
  42067. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  42068. #endif
  42069. #endif
  42070. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  42071. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  42072. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  42073. ExpectNull(passwd_cb_userdata =
  42074. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  42075. /* fail case with password call back */
  42076. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  42077. (void*)passwd));
  42078. BIO_free(bio);
  42079. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  42080. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  42081. (void*)passwd));
  42082. BIO_free(bio);
  42083. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  42084. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  42085. if ((bio == NULL) && (f != XBADFILE)) {
  42086. XFCLOSE(f);
  42087. }
  42088. /* use callback that works */
  42089. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  42090. (void*)"yassl123"));
  42091. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  42092. EVP_PKEY_free(pkey);
  42093. pkey = NULL;
  42094. BIO_free(bio);
  42095. bio = NULL;
  42096. SSL_CTX_free(ctx);
  42097. }
  42098. #endif /* !defined(NO_DES3) */
  42099. #endif /* !NO_BIO */
  42100. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  42101. {
  42102. unsigned char buf[2048];
  42103. size_t bytes = 0;
  42104. XFILE f = XBADFILE;
  42105. SSL_CTX* ctx = NULL;
  42106. #ifndef WOLFSSL_NO_TLS12
  42107. #ifndef NO_WOLFSSL_SERVER
  42108. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  42109. #else
  42110. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  42111. #endif
  42112. #else
  42113. #ifndef NO_WOLFSSL_SERVER
  42114. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  42115. #else
  42116. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  42117. #endif
  42118. #endif
  42119. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  42120. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  42121. if (f != XBADFILE)
  42122. XFCLOSE(f);
  42123. server_key = buf;
  42124. pkey = NULL;
  42125. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, (long int)bytes));
  42126. ExpectNull(pkey);
  42127. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, (long int)bytes));
  42128. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  42129. EVP_PKEY_free(pkey);
  42130. pkey = NULL;
  42131. SSL_CTX_free(ctx);
  42132. server_key = NULL;
  42133. }
  42134. #endif
  42135. #ifndef NO_BIO
  42136. (void)bio;
  42137. #endif
  42138. (void)pkey;
  42139. (void)server_key;
  42140. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  42141. return EXPECT_RESULT();
  42142. }
  42143. static int test_wolfSSL_PEM_file_RSAKey(void)
  42144. {
  42145. EXPECT_DECLS;
  42146. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  42147. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  42148. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  42149. RSA* rsa = NULL;
  42150. XFILE fp = XBADFILE;
  42151. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  42152. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  42153. if (fp != XBADFILE)
  42154. XFCLOSE(fp);
  42155. ExpectIntEQ(RSA_size(rsa), 256);
  42156. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  42157. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  42158. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  42159. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  42160. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  42161. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  42162. RSA_free(rsa);
  42163. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  42164. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  42165. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  42166. return EXPECT_RESULT();
  42167. }
  42168. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  42169. {
  42170. EXPECT_DECLS;
  42171. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  42172. !defined(NO_FILESYSTEM) && \
  42173. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  42174. RSA* rsa = NULL;
  42175. XFILE f = NULL;
  42176. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  42177. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  42178. ExpectIntEQ(RSA_size(rsa), 256);
  42179. if (f != XBADFILE) {
  42180. XFCLOSE(f);
  42181. f = XBADFILE;
  42182. }
  42183. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  42184. NULL), WOLFSSL_FAILURE);
  42185. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  42186. NULL), WOLFSSL_FAILURE);
  42187. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  42188. WOLFSSL_SUCCESS);
  42189. RSA_free(rsa);
  42190. #ifdef HAVE_ECC
  42191. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  42192. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  42193. if (f != XBADFILE)
  42194. XFCLOSE(f);
  42195. #endif /* HAVE_ECC */
  42196. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  42197. return EXPECT_RESULT();
  42198. }
  42199. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  42200. {
  42201. EXPECT_DECLS;
  42202. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42203. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42204. XFILE file = XBADFILE;
  42205. const char* fname = "./certs/client-keyPub.pem";
  42206. RSA *rsa = NULL;
  42207. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  42208. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  42209. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  42210. ExpectIntEQ(RSA_size(rsa), 256);
  42211. RSA_free(rsa);
  42212. if (file != XBADFILE)
  42213. XFCLOSE(file);
  42214. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  42215. return EXPECT_RESULT();
  42216. }
  42217. #ifndef NO_BIO
  42218. static int test_wolfSSL_PEM_bio_RSAKey(void)
  42219. {
  42220. EXPECT_DECLS;
  42221. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  42222. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  42223. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  42224. RSA* rsa = NULL;
  42225. BIO* bio = NULL;
  42226. /* PrivateKey */
  42227. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  42228. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  42229. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  42230. ExpectNotNull(rsa);
  42231. ExpectIntEQ(RSA_size(rsa), 256);
  42232. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  42233. NULL), WOLFSSL_FAILURE);
  42234. BIO_free(bio);
  42235. bio = NULL;
  42236. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  42237. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  42238. NULL), WOLFSSL_SUCCESS);
  42239. BIO_free(bio);
  42240. bio = NULL;
  42241. RSA_free(rsa);
  42242. rsa = NULL;
  42243. /* PUBKEY */
  42244. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  42245. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  42246. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  42247. ExpectIntEQ(RSA_size(rsa), 256);
  42248. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  42249. BIO_free(bio);
  42250. bio = NULL;
  42251. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  42252. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  42253. BIO_free(bio);
  42254. bio = NULL;
  42255. RSA_free(rsa);
  42256. rsa = NULL;
  42257. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  42258. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  42259. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  42260. BIO_free(bio);
  42261. bio = NULL;
  42262. RSA_free(rsa);
  42263. rsa = NULL;
  42264. #ifdef HAVE_ECC
  42265. /* ensure that non-rsa keys do not work */
  42266. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  42267. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  42268. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  42269. BIO_free(bio);
  42270. bio = NULL;
  42271. RSA_free(rsa);
  42272. rsa = NULL;
  42273. #endif /* HAVE_ECC */
  42274. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  42275. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  42276. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  42277. return EXPECT_RESULT();
  42278. }
  42279. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  42280. {
  42281. EXPECT_DECLS;
  42282. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42283. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42284. RSA* rsa = NULL;
  42285. RSA* rsa_dup = NULL;
  42286. BIO* bio = NULL;
  42287. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  42288. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  42289. ExpectIntEQ(RSA_size(rsa), 256);
  42290. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  42291. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  42292. /* Test duplicating empty key. */
  42293. ExpectNotNull(rsa_dup = RSA_new());
  42294. ExpectNull(RSAPublicKey_dup(rsa_dup));
  42295. RSA_free(rsa_dup);
  42296. rsa_dup = NULL;
  42297. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  42298. ExpectPtrNE(rsa_dup, rsa);
  42299. #endif
  42300. /* test if valgrind complains about unreleased memory */
  42301. RSA_up_ref(rsa);
  42302. RSA_free(rsa);
  42303. BIO_free(bio);
  42304. bio = NULL;
  42305. RSA_free(rsa);
  42306. rsa = NULL;
  42307. RSA_free(rsa_dup);
  42308. rsa_dup = NULL;
  42309. #ifdef HAVE_ECC
  42310. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  42311. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  42312. BIO_free(bio);
  42313. #endif /* HAVE_ECC */
  42314. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  42315. return EXPECT_RESULT();
  42316. }
  42317. static int test_wolfSSL_PEM_bio_DSAKey(void)
  42318. {
  42319. EXPECT_DECLS;
  42320. #ifndef HAVE_SELFTEST
  42321. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  42322. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  42323. DSA* dsa = NULL;
  42324. BIO* bio = NULL;
  42325. /* PrivateKey */
  42326. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  42327. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  42328. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  42329. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  42330. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  42331. NULL), WOLFSSL_FAILURE);
  42332. BIO_free(bio);
  42333. bio = NULL;
  42334. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  42335. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  42336. NULL), WOLFSSL_SUCCESS);
  42337. BIO_free(bio);
  42338. bio = NULL;
  42339. DSA_free(dsa);
  42340. dsa = NULL;
  42341. /* PUBKEY */
  42342. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  42343. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  42344. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  42345. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  42346. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  42347. BIO_free(bio);
  42348. bio = NULL;
  42349. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  42350. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  42351. BIO_free(bio);
  42352. bio = NULL;
  42353. DSA_free(dsa);
  42354. dsa = NULL;
  42355. #ifdef HAVE_ECC
  42356. /* ensure that non-dsa keys do not work */
  42357. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  42358. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  42359. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  42360. BIO_free(bio);
  42361. bio = NULL;
  42362. DSA_free(dsa);
  42363. dsa = NULL;
  42364. #endif /* HAVE_ECC */
  42365. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  42366. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  42367. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  42368. #endif /* HAVE_SELFTEST */
  42369. return EXPECT_RESULT();
  42370. }
  42371. static int test_wolfSSL_PEM_bio_ECKey(void)
  42372. {
  42373. EXPECT_DECLS;
  42374. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  42375. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  42376. EC_KEY* ec = NULL;
  42377. EC_KEY* ec2;
  42378. BIO* bio = NULL;
  42379. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  42380. unsigned char* pem = NULL;
  42381. int pLen;
  42382. #endif
  42383. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  42384. "MAA=\n"
  42385. "-----END PUBLIC KEY-----";
  42386. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  42387. "MAA=\n"
  42388. "-----END EC PRIVATE KEY-----";
  42389. /* PrivateKey */
  42390. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  42391. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  42392. ec2 = NULL;
  42393. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  42394. ExpectIntEQ(ec == ec2, 1);
  42395. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  42396. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  42397. NULL), WOLFSSL_FAILURE);
  42398. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  42399. NULL), WOLFSSL_FAILURE);
  42400. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  42401. WOLFSSL_FAILURE);
  42402. BIO_free(bio);
  42403. bio = NULL;
  42404. /* Public key data - fail. */
  42405. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  42406. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  42407. BIO_free(bio);
  42408. bio = NULL;
  42409. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  42410. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  42411. NULL), WOLFSSL_SUCCESS);
  42412. BIO_free(bio);
  42413. bio = NULL;
  42414. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  42415. NULL),WOLFSSL_FAILURE);
  42416. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  42417. WOLFSSL_FAILURE);
  42418. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  42419. WOLFSSL_FAILURE);
  42420. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  42421. WOLFSSL_SUCCESS);
  42422. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  42423. NULL), 0);
  42424. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  42425. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  42426. NULL), 0);
  42427. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  42428. NULL), 0);
  42429. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  42430. &pLen), 0);
  42431. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  42432. &pLen), 0);
  42433. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  42434. &pLen), 0);
  42435. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  42436. NULL), 0);
  42437. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  42438. &pLen), 1);
  42439. ExpectIntGT(pLen, 0);
  42440. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42441. #endif
  42442. EC_KEY_free(ec);
  42443. ec = NULL;
  42444. /* PUBKEY */
  42445. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  42446. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  42447. ec2 = NULL;
  42448. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  42449. ExpectIntEQ(ec == ec2, 1);
  42450. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  42451. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  42452. BIO_free(bio);
  42453. bio = NULL;
  42454. /* Test 0x30, 0x00 fails. */
  42455. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  42456. sizeof(ec_key_bad_1)));
  42457. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  42458. BIO_free(bio);
  42459. bio = NULL;
  42460. /* Private key data - fail. */
  42461. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  42462. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  42463. BIO_free(bio);
  42464. bio = NULL;
  42465. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  42466. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  42467. BIO_free(bio);
  42468. bio = NULL;
  42469. /* Same test as above, but with a file pointer rather than a BIO. */
  42470. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  42471. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  42472. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  42473. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  42474. EC_KEY_free(ec);
  42475. ec = NULL;
  42476. #ifndef NO_RSA
  42477. /* ensure that non-ec keys do not work */
  42478. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  42479. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  42480. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  42481. BIO_free(bio);
  42482. bio = NULL;
  42483. EC_KEY_free(ec);
  42484. ec = NULL;
  42485. #endif /* !NO_RSA */
  42486. /* Test 0x30, 0x00 fails. */
  42487. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  42488. sizeof(ec_priv_key_bad_1)));
  42489. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  42490. BIO_free(bio);
  42491. bio = NULL;
  42492. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  42493. return EXPECT_RESULT();
  42494. }
  42495. static int test_wolfSSL_PEM_PUBKEY(void)
  42496. {
  42497. EXPECT_DECLS;
  42498. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42499. BIO* bio = NULL;
  42500. EVP_PKEY* pkey = NULL;
  42501. /* test creating new EVP_PKEY with bad arg */
  42502. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  42503. /* test loading ECC key using BIO */
  42504. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  42505. {
  42506. XFILE file = XBADFILE;
  42507. const char* fname = "./certs/ecc-client-keyPub.pem";
  42508. size_t sz = 0;
  42509. byte* buf = NULL;
  42510. EVP_PKEY* pkey2 = NULL;
  42511. EC_KEY* ec_key = NULL;
  42512. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  42513. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  42514. ExpectIntGT(sz = XFTELL(file), 0);
  42515. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  42516. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  42517. if (buf != NULL) {
  42518. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  42519. }
  42520. if (file != XBADFILE) {
  42521. XFCLOSE(file);
  42522. }
  42523. /* Test using BIO new mem and loading PEM private key */
  42524. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  42525. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  42526. BIO_free(bio);
  42527. bio = NULL;
  42528. EVP_PKEY_free(pkey);
  42529. pkey = NULL;
  42530. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  42531. ExpectNotNull(pkey = EVP_PKEY_new());
  42532. ExpectPtrEq(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL), pkey);
  42533. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  42534. BIO_free(bio);
  42535. bio = NULL;
  42536. /* Qt unit test case*/
  42537. ExpectNotNull(pkey2 = EVP_PKEY_new());
  42538. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  42539. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  42540. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  42541. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  42542. #else
  42543. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  42544. #endif
  42545. EC_KEY_free(ec_key);
  42546. EVP_PKEY_free(pkey2);
  42547. EVP_PKEY_free(pkey);
  42548. pkey = NULL;
  42549. }
  42550. #endif
  42551. (void)bio;
  42552. (void)pkey;
  42553. #endif
  42554. return EXPECT_RESULT();
  42555. }
  42556. #endif /* !NO_BIO */
  42557. static int test_DSA_do_sign_verify(void)
  42558. {
  42559. EXPECT_DECLS;
  42560. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  42561. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  42562. !defined(NO_DSA)
  42563. unsigned char digest[WC_SHA_DIGEST_SIZE];
  42564. DSA_SIG* sig = NULL;
  42565. DSA* dsa = NULL;
  42566. word32 bytes;
  42567. byte sigBin[DSA_SIG_SIZE];
  42568. int dsacheck;
  42569. #ifdef USE_CERT_BUFFERS_1024
  42570. byte tmp[ONEK_BUF];
  42571. XMEMSET(tmp, 0, sizeof(tmp));
  42572. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  42573. bytes = sizeof_dsa_key_der_1024;
  42574. #elif defined(USE_CERT_BUFFERS_2048)
  42575. byte tmp[TWOK_BUF];
  42576. XMEMSET(tmp, 0, sizeof(tmp));
  42577. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  42578. bytes = sizeof_dsa_key_der_2048;
  42579. #else
  42580. byte tmp[TWOK_BUF];
  42581. XFILE fp = XBADFILE;
  42582. XMEMSET(tmp, 0, sizeof(tmp));
  42583. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  42584. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  42585. if (fp != XBADFILE)
  42586. XFCLOSE(fp);
  42587. #endif /* END USE_CERT_BUFFERS_1024 */
  42588. XMEMSET(digest, 202, sizeof(digest));
  42589. ExpectNotNull(dsa = DSA_new());
  42590. ExpectIntEQ(DSA_LoadDer(dsa, tmp, (int)bytes), 1);
  42591. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  42592. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  42593. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  42594. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  42595. DSA_SIG_free(sig);
  42596. DSA_free(dsa);
  42597. #endif
  42598. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  42599. return EXPECT_RESULT();
  42600. }
  42601. static int test_wolfSSL_tmp_dh(void)
  42602. {
  42603. EXPECT_DECLS;
  42604. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  42605. !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  42606. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  42607. byte buff[6000];
  42608. static const unsigned char p[] = {
  42609. 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13, 0xba,
  42610. 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5, 0x00,
  42611. 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a, 0xc6,
  42612. 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53, 0x0a,
  42613. 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84, 0xbf,
  42614. 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1, 0x8a,
  42615. 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91, 0xe6,
  42616. 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66, 0x48,
  42617. 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9, 0x3d,
  42618. 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e, 0x19,
  42619. 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d, 0x9f,
  42620. 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d, 0x2a,
  42621. 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75, 0xe6,
  42622. 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa, 0x04,
  42623. 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93, 0x38,
  42624. 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c, 0xe5,
  42625. 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35, 0x8e,
  42626. 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e, 0x5a,
  42627. 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76, 0xcc,
  42628. 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62, 0xa7,
  42629. 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0, 0x36,
  42630. 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40, 0x90,
  42631. 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a, 0xc3,
  42632. 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4, 0x48,
  42633. 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4, 0x9a,
  42634. 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90, 0xab,
  42635. 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58, 0x4b,
  42636. 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f, 0x08,
  42637. 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6, 0xb6,
  42638. 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67, 0x6b,
  42639. 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7, 0xfa,
  42640. 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f, 0x93
  42641. };
  42642. int pSz = (int)sizeof(p);
  42643. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
  42644. !defined(HAVE_SELFTEST)
  42645. static const unsigned char bad_p[] = {
  42646. 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13, 0xba,
  42647. 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5, 0x00,
  42648. 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a, 0xc6,
  42649. 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53, 0x0a,
  42650. 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84, 0xbf,
  42651. 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1, 0x8a,
  42652. 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91, 0xe6,
  42653. 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66, 0x48,
  42654. 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9, 0x3d,
  42655. 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e, 0x19,
  42656. 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d, 0x9f,
  42657. 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d, 0x2a,
  42658. 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75, 0xe6,
  42659. 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa, 0x04,
  42660. 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93, 0x38,
  42661. 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c, 0xe5,
  42662. 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35, 0x8e,
  42663. 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e, 0x5a,
  42664. 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76, 0xcc,
  42665. 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62, 0xa7,
  42666. 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0, 0x36,
  42667. 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40, 0x90,
  42668. 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a, 0xc3,
  42669. 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4, 0x48,
  42670. 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4, 0x9a,
  42671. 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90, 0xab,
  42672. 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58, 0x4b,
  42673. 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f, 0x08,
  42674. 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6, 0xb6,
  42675. 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67, 0x6b,
  42676. 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7, 0xfa,
  42677. 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f, 0x91
  42678. };
  42679. #endif
  42680. static const unsigned char g[] = { 0x02 };
  42681. int gSz = (int)sizeof(g);
  42682. #if !defined(NO_DSA)
  42683. char file[] = "./certs/dsaparams.pem";
  42684. DSA* dsa = NULL;
  42685. #else
  42686. char file[] = "./certs/dh2048.pem";
  42687. #endif
  42688. XFILE f = XBADFILE;
  42689. int bytes = 0;
  42690. DH* dh = NULL;
  42691. DH* dh2 = NULL;
  42692. BIO* bio = NULL;
  42693. SSL* ssl = NULL;
  42694. SSL_CTX* ctx = NULL;
  42695. #ifndef NO_WOLFSSL_CLIENT
  42696. SSL* ssl_c = NULL;
  42697. SSL_CTX* ctx_c = NULL;
  42698. #endif
  42699. #ifndef NO_WOLFSSL_SERVER
  42700. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42701. #endif
  42702. #ifndef NO_WOLFSSL_CLIENT
  42703. ExpectNotNull(ctx_c = SSL_CTX_new(wolfSSLv23_client_method()));
  42704. #ifdef NO_WOLFSSL_SERVER
  42705. ctx = ctx_c;
  42706. #endif
  42707. #endif
  42708. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  42709. WOLFSSL_FILETYPE_PEM));
  42710. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  42711. WOLFSSL_FILETYPE_PEM));
  42712. ExpectNotNull(ssl = SSL_new(ctx));
  42713. #ifndef NO_WOLFSSL_CLIENT
  42714. ExpectTrue(SSL_CTX_use_certificate_file(ctx_c, svrCertFile,
  42715. WOLFSSL_FILETYPE_PEM));
  42716. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx_c, svrKeyFile,
  42717. WOLFSSL_FILETYPE_PEM));
  42718. ExpectNotNull(ssl_c = SSL_new(ctx_c));
  42719. #ifdef NO_WOLFSSL_SERVER
  42720. ssl = ssl_c;
  42721. #endif
  42722. #endif
  42723. XMEMSET(buff, 0, sizeof(buff));
  42724. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  42725. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  42726. if (f != XBADFILE)
  42727. XFCLOSE(f);
  42728. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  42729. #if !defined(NO_DSA)
  42730. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  42731. ExpectNotNull(dsa);
  42732. dh = wolfSSL_DSA_dup_DH(dsa);
  42733. #else
  42734. dh = wolfSSL_PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  42735. #endif
  42736. ExpectNotNull(dh);
  42737. #if defined(WOLFSSL_DH_EXTRA) && \
  42738. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  42739. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  42740. DH_free(dh2);
  42741. dh2 = NULL;
  42742. #endif
  42743. /* Failure cases */
  42744. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(NULL, NULL, 0, NULL, 0),
  42745. BAD_FUNC_ARG);
  42746. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(ctx , NULL, 0, NULL, 0),
  42747. BAD_FUNC_ARG);
  42748. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(NULL, p , 0, NULL, 0),
  42749. BAD_FUNC_ARG);
  42750. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(NULL, NULL, 0, g , 0),
  42751. BAD_FUNC_ARG);
  42752. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(ctx , p , 0, NULL, 0),
  42753. BAD_FUNC_ARG);
  42754. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(ctx , NULL, 0, g , 0),
  42755. BAD_FUNC_ARG);
  42756. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(NULL, p , 0, g , 0),
  42757. BAD_FUNC_ARG);
  42758. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(ctx , p , 1, g , 1),
  42759. DH_KEY_SIZE_E);
  42760. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(ctx , buff, 6000, g , 1),
  42761. DH_KEY_SIZE_E);
  42762. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
  42763. !defined(HAVE_SELFTEST)
  42764. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(ctx, bad_p, pSz, g, gSz),
  42765. DH_CHECK_PUB_E);
  42766. #endif
  42767. ExpectIntEQ((int)wolfSSL_SetTmpDH(NULL, NULL, 0, NULL, 0),
  42768. WOLFSSL_FAILURE);
  42769. ExpectIntEQ((int)wolfSSL_SetTmpDH(ssl , NULL, 0, NULL, 0),
  42770. WOLFSSL_FAILURE);
  42771. ExpectIntEQ((int)wolfSSL_SetTmpDH(NULL, p , 0, NULL, 0),
  42772. WOLFSSL_FAILURE);
  42773. ExpectIntEQ((int)wolfSSL_SetTmpDH(NULL, NULL, 0, g , 0),
  42774. WOLFSSL_FAILURE);
  42775. ExpectIntEQ((int)wolfSSL_SetTmpDH(ssl , p , 0, NULL, 0),
  42776. WOLFSSL_FAILURE);
  42777. ExpectIntEQ((int)wolfSSL_SetTmpDH(ssl , NULL, 0, g , 0),
  42778. WOLFSSL_FAILURE);
  42779. ExpectIntEQ((int)wolfSSL_SetTmpDH(NULL, p , 0, g , 0),
  42780. WOLFSSL_FAILURE);
  42781. ExpectIntEQ((int)wolfSSL_SetTmpDH(ssl , p , 1, g , 1),
  42782. DH_KEY_SIZE_E);
  42783. ExpectIntEQ((int)wolfSSL_SetTmpDH(ssl , buff, 6000, g , 1),
  42784. DH_KEY_SIZE_E);
  42785. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
  42786. !defined(HAVE_SELFTEST)
  42787. #ifndef NO_WOLFSSL_SERVER
  42788. /* Parameters will be tested later so it passes now. */
  42789. ExpectIntEQ((int)wolfSSL_SetTmpDH(ssl, bad_p, pSz, g, gSz),
  42790. WOLFSSL_SUCCESS);
  42791. #endif
  42792. #endif
  42793. #ifndef NO_WOLFSSL_CLIENT
  42794. ExpectIntEQ((int)wolfSSL_SetTmpDH(ssl_c, p, pSz, g, gSz),
  42795. SIDE_ERROR);
  42796. #endif
  42797. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(NULL, NULL), BAD_FUNC_ARG);
  42798. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx , NULL), BAD_FUNC_ARG);
  42799. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(NULL, dh ), BAD_FUNC_ARG);
  42800. ExpectIntEQ((int)SSL_set_tmp_dh(NULL, NULL), BAD_FUNC_ARG);
  42801. ExpectIntEQ((int)SSL_set_tmp_dh(ssl , NULL), BAD_FUNC_ARG);
  42802. ExpectIntEQ((int)SSL_set_tmp_dh(NULL, dh ), BAD_FUNC_ARG);
  42803. /* No p/g to use. */
  42804. dh2 = wolfSSL_DH_new();
  42805. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx , dh2 ), WOLFSSL_FATAL_ERROR);
  42806. ExpectIntEQ((int)SSL_set_tmp_dh(ssl , dh2 ), WOLFSSL_FATAL_ERROR);
  42807. DH_free(dh2);
  42808. dh2 = NULL;
  42809. ExpectIntEQ((int)wolfSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz),
  42810. WOLFSSL_SUCCESS);
  42811. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  42812. #ifndef NO_WOLFSSL_SERVER
  42813. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  42814. #else
  42815. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  42816. #endif
  42817. BIO_free(bio);
  42818. #if !defined(NO_DSA)
  42819. DSA_free(dsa);
  42820. #endif
  42821. DH_free(dh);
  42822. dh = NULL;
  42823. #ifndef NO_WOLFSSL_CLIENT
  42824. if (ssl != ssl_c) {
  42825. SSL_free(ssl_c);
  42826. }
  42827. #endif
  42828. SSL_free(ssl);
  42829. #ifndef NO_WOLFSSL_CLIENT
  42830. if (ctx != ctx_c) {
  42831. SSL_CTX_free(ctx_c);
  42832. }
  42833. #endif
  42834. SSL_CTX_free(ctx);
  42835. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  42836. #endif
  42837. return EXPECT_RESULT();
  42838. }
  42839. static int test_wolfSSL_ctrl(void)
  42840. {
  42841. EXPECT_DECLS;
  42842. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  42843. byte buff[6000];
  42844. BIO* bio = NULL;
  42845. int bytes;
  42846. BUF_MEM* ptr = NULL;
  42847. XMEMSET(buff, 0, sizeof(buff));
  42848. bytes = sizeof(buff);
  42849. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  42850. ExpectNotNull(BIO_s_socket());
  42851. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  42852. /* needs tested after stubs filled out @TODO
  42853. SSL_ctrl
  42854. SSL_CTX_ctrl
  42855. */
  42856. BIO_free(bio);
  42857. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  42858. return EXPECT_RESULT();
  42859. }
  42860. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  42861. {
  42862. EXPECT_DECLS;
  42863. #ifdef OPENSSL_EXTRA
  42864. static const unsigned char pw[] = "password";
  42865. static const int pwSz = sizeof(pw) - 1;
  42866. size_t checkPwSz = 0;
  42867. const unsigned char* checkPw = NULL;
  42868. WOLFSSL_EVP_PKEY* key = NULL;
  42869. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  42870. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  42871. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  42872. pwSz));
  42873. if (key != NULL) {
  42874. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  42875. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  42876. ExpectIntEQ(key->pkey_sz, pwSz);
  42877. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  42878. }
  42879. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  42880. ExpectIntEQ((int)checkPwSz, pwSz);
  42881. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  42882. wolfSSL_EVP_PKEY_free(key);
  42883. key = NULL;
  42884. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  42885. 0));
  42886. ExpectIntEQ(key->pkey_sz, 0);
  42887. if (EXPECT_SUCCESS()) {
  42888. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  42889. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  42890. }
  42891. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  42892. ExpectIntEQ((int)checkPwSz, 0);
  42893. wolfSSL_EVP_PKEY_free(key);
  42894. key = NULL;
  42895. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  42896. 0));
  42897. ExpectIntEQ(key->pkey_sz, 0);
  42898. if (EXPECT_SUCCESS()) {
  42899. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  42900. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  42901. }
  42902. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  42903. ExpectIntEQ((int)checkPwSz, 0);
  42904. wolfSSL_EVP_PKEY_free(key);
  42905. key = NULL;
  42906. #endif /* OPENSSL_EXTRA */
  42907. return EXPECT_RESULT();
  42908. }
  42909. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  42910. {
  42911. EXPECT_DECLS;
  42912. #ifdef OPENSSL_EXTRA
  42913. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  42914. const char *priv = "ABCDEFGHIJKLMNOP";
  42915. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  42916. WOLFSSL_EVP_PKEY* key = NULL;
  42917. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  42918. NULL, NULL, AES_128_KEY_SIZE, cipher));
  42919. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  42920. NULL, (const unsigned char *)priv, 0, cipher));
  42921. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  42922. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  42923. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  42924. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  42925. wolfSSL_EVP_PKEY_free(key);
  42926. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  42927. #endif /* OPENSSL_EXTRA */
  42928. return EXPECT_RESULT();
  42929. }
  42930. static int test_wolfSSL_EVP_Digest(void)
  42931. {
  42932. EXPECT_DECLS;
  42933. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  42934. const char* in = "abc";
  42935. int inLen = (int)XSTRLEN(in);
  42936. byte out[WC_SHA256_DIGEST_SIZE];
  42937. unsigned int outLen;
  42938. const char* expOut =
  42939. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  42940. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  42941. "\x15\xAD";
  42942. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  42943. "SHA256", NULL), 1);
  42944. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  42945. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  42946. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  42947. return EXPECT_RESULT();
  42948. }
  42949. static int test_wolfSSL_EVP_Digest_all(void)
  42950. {
  42951. EXPECT_DECLS;
  42952. #ifdef OPENSSL_EXTRA
  42953. const char* digests[] = {
  42954. #ifndef NO_MD5
  42955. "MD5",
  42956. #endif
  42957. #ifndef NO_SHA
  42958. "SHA",
  42959. #endif
  42960. #ifdef WOLFSSL_SHA224
  42961. "SHA224",
  42962. #endif
  42963. #ifndef NO_SHA256
  42964. "SHA256",
  42965. #endif
  42966. #ifdef WOLFSSL_SHA384
  42967. "SHA384",
  42968. #endif
  42969. #ifdef WOLFSSL_SHA512
  42970. "SHA512",
  42971. #endif
  42972. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  42973. "SHA512_224",
  42974. #endif
  42975. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  42976. "SHA512_256",
  42977. #endif
  42978. #ifdef WOLFSSL_SHA3
  42979. #ifndef WOLFSSL_NOSHA3_224
  42980. "SHA3_224",
  42981. #endif
  42982. #ifndef WOLFSSL_NOSHA3_256
  42983. "SHA3_256",
  42984. #endif
  42985. "SHA3_384",
  42986. #ifndef WOLFSSL_NOSHA3_512
  42987. "SHA3_512",
  42988. #endif
  42989. #endif /* WOLFSSL_SHA3 */
  42990. NULL
  42991. };
  42992. const char** d;
  42993. const unsigned char in[] = "abc";
  42994. int inLen = XSTR_SIZEOF(in);
  42995. byte out[WC_MAX_DIGEST_SIZE];
  42996. unsigned int outLen;
  42997. for (d = digests; *d != NULL; d++) {
  42998. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  42999. ExpectIntGT(outLen, 0);
  43000. ExpectIntEQ(EVP_MD_size(*d), outLen);
  43001. }
  43002. #endif
  43003. return EXPECT_RESULT();
  43004. }
  43005. static int test_wolfSSL_EVP_MD_size(void)
  43006. {
  43007. EXPECT_DECLS;
  43008. #ifdef OPENSSL_EXTRA
  43009. WOLFSSL_EVP_MD_CTX mdCtx;
  43010. #ifdef WOLFSSL_SHA3
  43011. #ifndef WOLFSSL_NOSHA3_224
  43012. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43013. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  43014. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  43015. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  43016. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43017. #endif
  43018. #ifndef WOLFSSL_NOSHA3_256
  43019. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43020. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  43021. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  43022. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  43023. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43024. #endif
  43025. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43026. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  43027. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  43028. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  43029. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43030. #ifndef WOLFSSL_NOSHA3_512
  43031. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43032. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  43033. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  43034. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  43035. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43036. #endif
  43037. #endif /* WOLFSSL_SHA3 */
  43038. #ifndef NO_SHA256
  43039. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43040. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  43041. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43042. WC_SHA256_DIGEST_SIZE);
  43043. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43044. WC_SHA256_BLOCK_SIZE);
  43045. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  43046. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  43047. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43048. #endif
  43049. #ifndef NO_MD5
  43050. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43051. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  43052. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43053. WC_MD5_DIGEST_SIZE);
  43054. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43055. WC_MD5_BLOCK_SIZE);
  43056. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  43057. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  43058. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43059. #endif
  43060. #ifdef WOLFSSL_SHA224
  43061. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43062. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  43063. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43064. WC_SHA224_DIGEST_SIZE);
  43065. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43066. WC_SHA224_BLOCK_SIZE);
  43067. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  43068. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  43069. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43070. #endif
  43071. #ifdef WOLFSSL_SHA384
  43072. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43073. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  43074. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43075. WC_SHA384_DIGEST_SIZE);
  43076. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43077. WC_SHA384_BLOCK_SIZE);
  43078. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  43079. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  43080. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43081. #endif
  43082. #ifdef WOLFSSL_SHA512
  43083. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43084. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  43085. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43086. WC_SHA512_DIGEST_SIZE);
  43087. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43088. WC_SHA512_BLOCK_SIZE);
  43089. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  43090. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  43091. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43092. #endif
  43093. #ifndef NO_SHA
  43094. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43095. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  43096. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43097. WC_SHA_DIGEST_SIZE);
  43098. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43099. WC_SHA_BLOCK_SIZE);
  43100. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  43101. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  43102. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43103. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43104. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  43105. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43106. WC_SHA_DIGEST_SIZE);
  43107. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43108. WC_SHA_BLOCK_SIZE);
  43109. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  43110. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  43111. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43112. #endif
  43113. /* error case */
  43114. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43115. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  43116. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  43117. BAD_FUNC_ARG);
  43118. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  43119. /* Cleanup is valid on uninit'ed struct */
  43120. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43121. #endif /* OPENSSL_EXTRA */
  43122. return EXPECT_RESULT();
  43123. }
  43124. static int test_wolfSSL_EVP_MD_pkey_type(void)
  43125. {
  43126. EXPECT_DECLS;
  43127. #ifdef OPENSSL_EXTRA
  43128. const WOLFSSL_EVP_MD* md;
  43129. #ifndef NO_MD5
  43130. ExpectNotNull(md = EVP_md5());
  43131. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  43132. #endif
  43133. #ifndef NO_SHA
  43134. ExpectNotNull(md = EVP_sha1());
  43135. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  43136. #endif
  43137. #ifdef WOLFSSL_SHA224
  43138. ExpectNotNull(md = EVP_sha224());
  43139. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  43140. #endif
  43141. ExpectNotNull(md = EVP_sha256());
  43142. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  43143. #ifdef WOLFSSL_SHA384
  43144. ExpectNotNull(md = EVP_sha384());
  43145. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  43146. #endif
  43147. #ifdef WOLFSSL_SHA512
  43148. ExpectNotNull(md = EVP_sha512());
  43149. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  43150. #endif
  43151. #endif
  43152. return EXPECT_RESULT();
  43153. }
  43154. #ifdef OPENSSL_EXTRA
  43155. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  43156. size_t testKeySz, const char* testData, size_t testDataSz,
  43157. const byte* testResult, size_t testResultSz)
  43158. {
  43159. EXPECT_DECLS;
  43160. unsigned char check[WC_MAX_DIGEST_SIZE];
  43161. size_t checkSz = -1;
  43162. WOLFSSL_EVP_PKEY* key = NULL;
  43163. WOLFSSL_EVP_MD_CTX mdCtx;
  43164. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  43165. testKey, (int)testKeySz));
  43166. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43167. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  43168. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  43169. (unsigned int)testDataSz), 1);
  43170. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  43171. ExpectIntEQ((int)checkSz, (int)testResultSz);
  43172. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  43173. ExpectIntEQ((int)checkSz,(int)testResultSz);
  43174. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  43175. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43176. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  43177. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  43178. (unsigned int)testDataSz), 1);
  43179. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  43180. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43181. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43182. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  43183. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  43184. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  43185. ExpectIntEQ((int)checkSz, (int)testResultSz);
  43186. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  43187. ExpectIntEQ((int)checkSz,(int)testResultSz);
  43188. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  43189. (unsigned int)testDataSz - 4), 1);
  43190. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  43191. ExpectIntEQ((int)checkSz,(int)testResultSz);
  43192. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  43193. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43194. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  43195. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  43196. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  43197. (unsigned int)testDataSz - 4), 1);
  43198. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  43199. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43200. wolfSSL_EVP_PKEY_free(key);
  43201. return EXPECT_RESULT();
  43202. }
  43203. #endif
  43204. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  43205. {
  43206. EXPECT_DECLS;
  43207. #ifdef OPENSSL_EXTRA
  43208. static const unsigned char testKey[] =
  43209. {
  43210. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  43211. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  43212. 0x0b, 0x0b, 0x0b, 0x0b
  43213. };
  43214. static const char testData[] = "Hi There";
  43215. #ifdef WOLFSSL_SHA224
  43216. static const unsigned char testResultSha224[] =
  43217. {
  43218. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  43219. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  43220. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  43221. 0x53, 0x68, 0x4b, 0x22
  43222. };
  43223. #endif
  43224. #ifndef NO_SHA256
  43225. static const unsigned char testResultSha256[] =
  43226. {
  43227. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  43228. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  43229. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  43230. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  43231. };
  43232. #endif
  43233. #ifdef WOLFSSL_SHA384
  43234. static const unsigned char testResultSha384[] =
  43235. {
  43236. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  43237. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  43238. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  43239. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  43240. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  43241. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  43242. };
  43243. #endif
  43244. #ifdef WOLFSSL_SHA512
  43245. static const unsigned char testResultSha512[] =
  43246. {
  43247. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  43248. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  43249. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  43250. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  43251. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  43252. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  43253. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  43254. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  43255. };
  43256. #endif
  43257. #ifdef WOLFSSL_SHA3
  43258. #ifndef WOLFSSL_NOSHA3_224
  43259. static const unsigned char testResultSha3_224[] =
  43260. {
  43261. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  43262. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  43263. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  43264. 0xf3, 0xc8, 0x60, 0xf7
  43265. };
  43266. #endif
  43267. #ifndef WOLFSSL_NOSHA3_256
  43268. static const unsigned char testResultSha3_256[] =
  43269. {
  43270. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  43271. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  43272. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  43273. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  43274. };
  43275. #endif
  43276. #ifndef WOLFSSL_NOSHA3_384
  43277. static const unsigned char testResultSha3_384[] =
  43278. {
  43279. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  43280. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  43281. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  43282. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  43283. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  43284. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  43285. };
  43286. #endif
  43287. #ifndef WOLFSSL_NOSHA3_512
  43288. static const unsigned char testResultSha3_512[] =
  43289. {
  43290. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  43291. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  43292. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  43293. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  43294. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  43295. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  43296. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  43297. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  43298. };
  43299. #endif
  43300. #endif
  43301. #ifndef NO_SHA256
  43302. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  43303. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  43304. sizeof(testResultSha256)), TEST_SUCCESS);
  43305. #endif
  43306. #ifdef WOLFSSL_SHA224
  43307. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  43308. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  43309. sizeof(testResultSha224)), TEST_SUCCESS);
  43310. #endif
  43311. #ifdef WOLFSSL_SHA384
  43312. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  43313. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  43314. sizeof(testResultSha384)), TEST_SUCCESS);
  43315. #endif
  43316. #ifdef WOLFSSL_SHA512
  43317. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  43318. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  43319. sizeof(testResultSha512)), TEST_SUCCESS);
  43320. #endif
  43321. #ifdef WOLFSSL_SHA3
  43322. #ifndef WOLFSSL_NOSHA3_224
  43323. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  43324. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  43325. sizeof(testResultSha3_224)), TEST_SUCCESS);
  43326. #endif
  43327. #ifndef WOLFSSL_NOSHA3_256
  43328. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  43329. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  43330. sizeof(testResultSha3_256)), TEST_SUCCESS);
  43331. #endif
  43332. #ifndef WOLFSSL_NOSHA3_384
  43333. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  43334. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  43335. sizeof(testResultSha3_384)), TEST_SUCCESS);
  43336. #endif
  43337. #ifndef WOLFSSL_NOSHA3_512
  43338. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  43339. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  43340. sizeof(testResultSha3_512)), TEST_SUCCESS);
  43341. #endif
  43342. #endif
  43343. #endif /* OPENSSL_EXTRA */
  43344. return EXPECT_RESULT();
  43345. }
  43346. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  43347. {
  43348. EXPECT_DECLS;
  43349. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  43350. WOLFSSL_EVP_PKEY* privKey = NULL;
  43351. WOLFSSL_EVP_PKEY* pubKey = NULL;
  43352. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  43353. const char testData[] = "Hi There";
  43354. WOLFSSL_EVP_MD_CTX mdCtx;
  43355. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  43356. int ret;
  43357. size_t checkSz = -1;
  43358. int sz = 2048 / 8;
  43359. const unsigned char* cp;
  43360. const unsigned char* p;
  43361. unsigned char check[2048/8];
  43362. size_t i;
  43363. int paddings[] = {
  43364. RSA_PKCS1_PADDING,
  43365. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  43366. RSA_PKCS1_PSS_PADDING,
  43367. #endif
  43368. };
  43369. cp = client_key_der_2048;
  43370. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  43371. sizeof_client_key_der_2048)));
  43372. p = client_keypub_der_2048;
  43373. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  43374. sizeof_client_keypub_der_2048)));
  43375. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43376. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  43377. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  43378. NULL, privKey), 1);
  43379. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  43380. (unsigned int)XSTRLEN(testData)), 1);
  43381. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  43382. ExpectIntEQ((int)checkSz, sz);
  43383. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  43384. ExpectIntEQ((int)checkSz,sz);
  43385. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  43386. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  43387. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  43388. ExpectIntEQ(ret, 1);
  43389. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  43390. ExpectIntEQ(ret, 1);
  43391. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43392. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  43393. NULL, pubKey), 1);
  43394. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  43395. (unsigned int)XSTRLEN(testData)),
  43396. 1);
  43397. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  43398. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  43399. ExpectIntEQ(ret, 1);
  43400. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43401. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  43402. NULL, privKey), 1);
  43403. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  43404. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  43405. ExpectIntEQ((int)checkSz, sz);
  43406. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  43407. ExpectIntEQ((int)checkSz, sz);
  43408. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  43409. (unsigned int)XSTRLEN(testData) - 4), 1);
  43410. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  43411. ExpectIntEQ((int)checkSz, sz);
  43412. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  43413. ExpectIntEQ(ret, 1);
  43414. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43415. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  43416. NULL, pubKey), 1);
  43417. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  43418. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  43419. (unsigned int)XSTRLEN(testData) - 4),
  43420. 1);
  43421. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  43422. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  43423. ExpectIntEQ(ret, 1);
  43424. /* Check all signing padding types */
  43425. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  43426. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43427. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  43428. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  43429. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  43430. paddings[i]), 1);
  43431. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  43432. (unsigned int)XSTRLEN(testData)), 1);
  43433. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  43434. ExpectIntEQ((int)checkSz, sz);
  43435. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  43436. ExpectIntEQ((int)checkSz,sz);
  43437. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  43438. ExpectIntEQ(ret, 1);
  43439. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43440. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  43441. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  43442. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  43443. paddings[i]), 1);
  43444. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  43445. (unsigned int)XSTRLEN(testData)), 1);
  43446. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  43447. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  43448. ExpectIntEQ(ret, 1);
  43449. }
  43450. wolfSSL_EVP_PKEY_free(pubKey);
  43451. wolfSSL_EVP_PKEY_free(privKey);
  43452. #endif
  43453. return EXPECT_RESULT();
  43454. }
  43455. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  43456. {
  43457. EXPECT_DECLS;
  43458. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  43459. WOLFSSL_EVP_PKEY* privKey = NULL;
  43460. WOLFSSL_EVP_PKEY* pubKey = NULL;
  43461. const char testData[] = "Hi There";
  43462. WOLFSSL_EVP_MD_CTX mdCtx;
  43463. int ret;
  43464. size_t checkSz = -1;
  43465. const unsigned char* cp;
  43466. const unsigned char* p;
  43467. unsigned char check[2048/8];
  43468. cp = ecc_clikey_der_256;
  43469. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  43470. sizeof_ecc_clikey_der_256));
  43471. p = ecc_clikeypub_der_256;
  43472. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  43473. sizeof_ecc_clikeypub_der_256)));
  43474. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43475. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  43476. NULL, privKey), 1);
  43477. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  43478. (unsigned int)XSTRLEN(testData)), 1);
  43479. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  43480. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  43481. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  43482. ExpectIntEQ(ret, 1);
  43483. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43484. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  43485. NULL, pubKey), 1);
  43486. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  43487. (unsigned int)XSTRLEN(testData)),
  43488. 1);
  43489. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  43490. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  43491. ExpectIntEQ(ret, 1);
  43492. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43493. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  43494. NULL, privKey), 1);
  43495. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  43496. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  43497. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  43498. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  43499. (unsigned int)XSTRLEN(testData) - 4), 1);
  43500. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  43501. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  43502. ExpectIntEQ(ret, 1);
  43503. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43504. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  43505. NULL, pubKey), 1);
  43506. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  43507. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  43508. (unsigned int)XSTRLEN(testData) - 4),
  43509. 1);
  43510. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  43511. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  43512. ExpectIntEQ(ret, 1);
  43513. wolfSSL_EVP_PKEY_free(pubKey);
  43514. wolfSSL_EVP_PKEY_free(privKey);
  43515. #endif
  43516. return EXPECT_RESULT();
  43517. }
  43518. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  43519. {
  43520. EXPECT_DECLS;
  43521. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43522. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  43523. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  43524. char caFile[] = "./certs/client-ca.pem";
  43525. char clientFile[] = "./certs/client-cert.pem";
  43526. SSL_CTX* ctx = NULL;
  43527. X509* x509 = NULL;
  43528. BIO *bio = NULL;
  43529. X509 *cert = NULL;
  43530. X509 *ca = NULL;
  43531. STACK_OF(X509) *chain = NULL;
  43532. STACK_OF(X509) *chain2 = NULL;
  43533. #ifndef NO_WOLFSSL_SERVER
  43534. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  43535. #else
  43536. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  43537. #endif
  43538. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  43539. WOLFSSL_FILETYPE_PEM));
  43540. /* Negative tests. */
  43541. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(NULL, NULL), WOLFSSL_FAILURE);
  43542. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, NULL), WOLFSSL_FAILURE);
  43543. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(NULL, x509), WOLFSSL_FAILURE);
  43544. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  43545. ExpectNotNull(x509 = wolfSSL_X509_new());
  43546. /* Empty certificate. */
  43547. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_FAILURE);
  43548. wolfSSL_X509_free(x509);
  43549. x509 = NULL;
  43550. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  43551. WOLFSSL_FILETYPE_PEM));
  43552. /* additional test of getting EVP_PKEY key size from X509
  43553. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  43554. * allowed with user RSA */
  43555. {
  43556. EVP_PKEY* pkey = NULL;
  43557. #if defined(HAVE_ECC)
  43558. X509* ecX509 = NULL;
  43559. #endif /* HAVE_ECC */
  43560. ExpectNotNull(pkey = X509_get_pubkey(x509));
  43561. /* current RSA key is 2048 bit (256 bytes) */
  43562. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  43563. EVP_PKEY_free(pkey);
  43564. pkey = NULL;
  43565. #if defined(HAVE_ECC)
  43566. #if defined(USE_CERT_BUFFERS_256)
  43567. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  43568. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  43569. SSL_FILETYPE_ASN1));
  43570. #else
  43571. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  43572. cliEccCertFile, SSL_FILETYPE_PEM));
  43573. #endif
  43574. pkey = X509_get_pubkey(ecX509);
  43575. ExpectNotNull(pkey);
  43576. /* current ECC key is 256 bit (32 bytes) */
  43577. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  43578. X509_free(ecX509);
  43579. ecX509 = NULL;
  43580. EVP_PKEY_free(pkey);
  43581. pkey = NULL;
  43582. #endif /* HAVE_ECC */
  43583. }
  43584. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  43585. if (EXPECT_SUCCESS()) {
  43586. x509 = NULL;
  43587. }
  43588. #ifdef WOLFSSL_ENCRYPTED_KEYS
  43589. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  43590. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  43591. #endif
  43592. SSL_CTX_free(ctx);
  43593. ctx = NULL;
  43594. #ifndef NO_WOLFSSL_SERVER
  43595. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  43596. #else
  43597. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  43598. #endif
  43599. /* Test haproxy use case */
  43600. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  43601. /* Read Certificate */
  43602. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  43603. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  43604. ExpectNotNull(chain = sk_X509_new_null());
  43605. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  43606. if (EXPECT_SUCCESS()) {
  43607. ca = NULL;
  43608. }
  43609. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  43610. ExpectNotNull(ca = sk_X509_shift(chain2));
  43611. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  43612. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  43613. if (EXPECT_SUCCESS()) {
  43614. ca = NULL;
  43615. }
  43616. BIO_free(bio);
  43617. X509_free(cert);
  43618. X509_free(ca);
  43619. X509_free(x509);
  43620. sk_X509_pop_free(chain, X509_free);
  43621. sk_X509_pop_free(chain2, X509_free);
  43622. SSL_CTX_free(ctx);
  43623. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  43624. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43625. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  43626. return EXPECT_RESULT();
  43627. }
  43628. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  43629. static int test_wolfSSL_ERR_peek_last_error_line(void)
  43630. {
  43631. EXPECT_DECLS;
  43632. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43633. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  43634. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  43635. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  43636. callback_functions client_cb;
  43637. callback_functions server_cb;
  43638. int line = 0;
  43639. int flag = ERR_TXT_STRING;
  43640. const char* file = NULL;
  43641. const char* data = NULL;
  43642. /* create a failed connection and inspect the error */
  43643. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  43644. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  43645. client_cb.method = wolfTLSv1_1_client_method;
  43646. server_cb.method = wolfTLSv1_2_server_method;
  43647. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  43648. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  43649. ExpectNotNull(data);
  43650. /* check clearing error state */
  43651. ERR_remove_state(0);
  43652. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  43653. ERR_peek_last_error_line(NULL, &line);
  43654. ExpectIntEQ(line, 0);
  43655. ERR_peek_last_error_line(&file, NULL);
  43656. ExpectNull(file);
  43657. /* retry connection to fill error queue */
  43658. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  43659. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  43660. client_cb.method = wolfTLSv1_1_client_method;
  43661. server_cb.method = wolfTLSv1_2_server_method;
  43662. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  43663. /* check that error code was stored */
  43664. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  43665. ERR_peek_last_error_line(NULL, &line);
  43666. ExpectIntNE(line, 0);
  43667. ERR_peek_last_error_line(&file, NULL);
  43668. ExpectNotNull(file);
  43669. fprintf(stderr, "\nTesting error print out\n");
  43670. ERR_print_errors_fp(stderr);
  43671. fprintf(stderr, "Done testing print out\n\n");
  43672. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  43673. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  43674. return EXPECT_RESULT();
  43675. }
  43676. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  43677. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43678. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43679. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  43680. {
  43681. (void) ok;
  43682. (void) ctx;
  43683. fprintf(stderr, "ENTER verify_cb\n");
  43684. return SSL_SUCCESS;
  43685. }
  43686. #endif
  43687. static int test_wolfSSL_X509_Name_canon(void)
  43688. {
  43689. EXPECT_DECLS;
  43690. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  43691. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  43692. defined(WOLFSSL_CERT_GEN) && \
  43693. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  43694. const long ex_hash1 = 0x0fdb2da4;
  43695. const long ex_hash2 = 0x9f3e8c9e;
  43696. X509_NAME *name = NULL;
  43697. X509 *x509 = NULL;
  43698. XFILE file = XBADFILE;
  43699. unsigned long hash = 0;
  43700. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  43701. byte *pbuf = NULL;
  43702. word32 len = 0;
  43703. (void) ex_hash2;
  43704. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  43705. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  43706. ExpectNotNull(name = X509_get_issuer_name(x509));
  43707. /* When output buffer is NULL, should return necessary output buffer
  43708. * length.*/
  43709. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  43710. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  43711. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  43712. hash = (((unsigned long)digest[3] << 24) |
  43713. ((unsigned long)digest[2] << 16) |
  43714. ((unsigned long)digest[1] << 8) |
  43715. ((unsigned long)digest[0]));
  43716. ExpectIntEQ(hash, ex_hash1);
  43717. if (file != XBADFILE) {
  43718. XFCLOSE(file);
  43719. file = XBADFILE;
  43720. }
  43721. X509_free(x509);
  43722. x509 = NULL;
  43723. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  43724. pbuf = NULL;
  43725. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  43726. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  43727. ExpectNotNull(name = X509_get_issuer_name(x509));
  43728. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  43729. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  43730. hash = (((unsigned long)digest[3] << 24) |
  43731. ((unsigned long)digest[2] << 16) |
  43732. ((unsigned long)digest[1] << 8) |
  43733. ((unsigned long)digest[0]));
  43734. ExpectIntEQ(hash, ex_hash2);
  43735. if (file != XBADFILE)
  43736. XFCLOSE(file);
  43737. X509_free(x509);
  43738. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  43739. #endif
  43740. return EXPECT_RESULT();
  43741. }
  43742. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  43743. {
  43744. EXPECT_DECLS;
  43745. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  43746. const int MAX_DIR = 4;
  43747. const char paths[][32] = {
  43748. "./certs/ed25519",
  43749. "./certs/ecc",
  43750. "./certs/crl",
  43751. "./certs/",
  43752. };
  43753. char CertCrl_path[MAX_FILENAME_SZ];
  43754. char *p;
  43755. X509_STORE* str = NULL;
  43756. X509_LOOKUP* lookup = NULL;
  43757. WOLFSSL_STACK* sk = NULL;
  43758. int len, total_len, i;
  43759. (void)sk;
  43760. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  43761. /* illegal string */
  43762. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  43763. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  43764. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  43765. SSL_FILETYPE_PEM,NULL), 0);
  43766. /* free store */
  43767. X509_STORE_free(str);
  43768. str = NULL;
  43769. /* short folder string */
  43770. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  43771. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  43772. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  43773. SSL_FILETYPE_PEM,NULL), 1);
  43774. #if defined(WOLFSSL_INT_H)
  43775. /* only available when including internal.h */
  43776. ExpectNotNull(sk = lookup->dirs->dir_entry);
  43777. #endif
  43778. /* free store */
  43779. X509_STORE_free(str);
  43780. str = NULL;
  43781. /* typical function check */
  43782. p = &CertCrl_path[0];
  43783. total_len = 0;
  43784. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  43785. len = (int)XSTRLEN((const char*)&paths[i]);
  43786. total_len += len;
  43787. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  43788. p += len;
  43789. if (i != 0) *(p++) = SEPARATOR_CHAR;
  43790. }
  43791. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  43792. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  43793. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  43794. SSL_FILETYPE_PEM,NULL), 1);
  43795. #if defined(WOLFSSL_INT_H)
  43796. /* only available when including internal.h */
  43797. ExpectNotNull(sk = lookup->dirs->dir_entry);
  43798. #endif
  43799. X509_STORE_free(str);
  43800. #endif
  43801. return EXPECT_RESULT();
  43802. }
  43803. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  43804. {
  43805. EXPECT_DECLS;
  43806. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  43807. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  43808. defined(WOLFSSL_SIGNER_DER_CERT)
  43809. X509_STORE_CTX* ctx = NULL;
  43810. X509_STORE* str = NULL;
  43811. X509_LOOKUP* lookup = NULL;
  43812. X509* cert1 = NULL;
  43813. X509* x509Ca = NULL;
  43814. X509* x509Svr = NULL;
  43815. X509* issuer = NULL;
  43816. WOLFSSL_STACK* sk = NULL;
  43817. X509_NAME* caName = NULL;
  43818. X509_NAME* issuerName = NULL;
  43819. XFILE file1 = XBADFILE;
  43820. int i;
  43821. int cert_count = 0;
  43822. int cmp;
  43823. char der[] = "certs/ca-cert.der";
  43824. #ifdef HAVE_CRL
  43825. char pem[][100] = {
  43826. "./certs/crl/crl.pem",
  43827. "./certs/crl/crl2.pem",
  43828. "./certs/crl/caEccCrl.pem",
  43829. "./certs/crl/eccCliCRL.pem",
  43830. "./certs/crl/eccSrvCRL.pem",
  43831. ""
  43832. };
  43833. #endif
  43834. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  43835. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  43836. if (file1 != XBADFILE)
  43837. XFCLOSE(file1);
  43838. ExpectNotNull(ctx = X509_STORE_CTX_new());
  43839. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  43840. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  43841. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  43842. SSL_FILETYPE_PEM,NULL), 1);
  43843. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  43844. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  43845. /* check if CA cert is loaded into the store */
  43846. for (i = 0; i < cert_count; i++) {
  43847. x509Ca = sk_X509_value(sk, i);
  43848. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  43849. }
  43850. ExpectNotNull((x509Svr =
  43851. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  43852. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  43853. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  43854. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  43855. ExpectNotNull(issuer);
  43856. caName = X509_get_subject_name(x509Ca);
  43857. ExpectNotNull(caName);
  43858. issuerName = X509_get_subject_name(issuer);
  43859. ExpectNotNull(issuerName);
  43860. cmp = X509_NAME_cmp(caName, issuerName);
  43861. ExpectIntEQ(cmp, 0);
  43862. /* load der format */
  43863. X509_free(issuer);
  43864. issuer = NULL;
  43865. X509_STORE_CTX_free(ctx);
  43866. ctx = NULL;
  43867. X509_STORE_free(str);
  43868. str = NULL;
  43869. sk_X509_pop_free(sk, NULL);
  43870. sk = NULL;
  43871. X509_free(x509Svr);
  43872. x509Svr = NULL;
  43873. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  43874. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  43875. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  43876. SSL_FILETYPE_ASN1,NULL), 1);
  43877. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  43878. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  43879. /* check if CA cert is loaded into the store */
  43880. for (i = 0; i < cert_count; i++) {
  43881. x509Ca = sk_X509_value(sk, i);
  43882. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  43883. }
  43884. X509_STORE_free(str);
  43885. str = NULL;
  43886. sk_X509_pop_free(sk, NULL);
  43887. sk = NULL;
  43888. X509_free(cert1);
  43889. cert1 = NULL;
  43890. #ifdef HAVE_CRL
  43891. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  43892. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  43893. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  43894. SSL_FILETYPE_PEM,NULL), 1);
  43895. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  43896. "certs/server-revoked-cert.pem",
  43897. SSL_FILETYPE_PEM,NULL), 1);
  43898. if (str) {
  43899. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  43900. WOLFSSL_FILETYPE_PEM), 1);
  43901. /* since store hasn't yet known the revoked cert*/
  43902. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  43903. "certs/server-revoked-cert.pem",
  43904. WOLFSSL_FILETYPE_PEM), 1);
  43905. }
  43906. for (i = 0; pem[i][0] != '\0'; i++)
  43907. {
  43908. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  43909. SSL_FILETYPE_PEM, NULL), 1);
  43910. }
  43911. if (str) {
  43912. /* since store knows crl list */
  43913. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  43914. "certs/server-revoked-cert.pem",
  43915. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  43916. }
  43917. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  43918. X509_STORE_free(str);
  43919. #endif
  43920. #endif
  43921. return EXPECT_RESULT();
  43922. }
  43923. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  43924. {
  43925. int res = TEST_SKIPPED;
  43926. #if defined(OPENSSL_EXTRA)
  43927. X509_STORE_CTX_cleanup(NULL);
  43928. X509_STORE_CTX_trusted_stack(NULL, NULL);
  43929. res = TEST_SUCCESS;
  43930. #endif
  43931. return res;
  43932. }
  43933. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  43934. {
  43935. EXPECT_DECLS;
  43936. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  43937. X509_STORE_CTX* ctx = NULL;
  43938. X509_STORE* str = NULL;
  43939. X509* x509Ca = NULL;
  43940. X509* x509Svr = NULL;
  43941. X509* issuer = NULL;
  43942. X509_NAME* caName = NULL;
  43943. X509_NAME* issuerName = NULL;
  43944. ExpectNotNull(ctx = X509_STORE_CTX_new());
  43945. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  43946. ExpectNotNull((x509Ca =
  43947. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  43948. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  43949. ExpectNotNull((x509Svr =
  43950. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  43951. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  43952. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  43953. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  43954. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  43955. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  43956. #ifdef WOLFSSL_SIGNER_DER_CERT
  43957. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  43958. #endif
  43959. X509_free(issuer);
  43960. X509_STORE_CTX_free(ctx);
  43961. X509_free(x509Svr);
  43962. X509_STORE_free(str);
  43963. X509_free(x509Ca);
  43964. #endif
  43965. return EXPECT_RESULT();
  43966. }
  43967. static int test_wolfSSL_PKCS7_certs(void)
  43968. {
  43969. EXPECT_DECLS;
  43970. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  43971. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  43972. STACK_OF(X509)* sk = NULL;
  43973. STACK_OF(X509_INFO)* info_sk = NULL;
  43974. PKCS7 *p7 = NULL;
  43975. BIO* bio = NULL;
  43976. const byte* p = NULL;
  43977. int buflen = 0;
  43978. int i;
  43979. /* Test twice. Once with d2i and once without to test
  43980. * that everything is free'd correctly. */
  43981. for (i = 0; i < 2; i++) {
  43982. ExpectNotNull(p7 = PKCS7_new());
  43983. if (p7 != NULL) {
  43984. p7->version = 1;
  43985. #ifdef NO_SHA
  43986. p7->hashOID = SHA256h;
  43987. #else
  43988. p7->hashOID = SHAh;
  43989. #endif
  43990. }
  43991. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  43992. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  43993. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  43994. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  43995. ExpectNotNull(sk = sk_X509_new_null());
  43996. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  43997. X509_INFO* info = NULL;
  43998. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  43999. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  44000. if (EXPECT_SUCCESS() && (info != NULL)) {
  44001. info->x509 = NULL;
  44002. }
  44003. X509_INFO_free(info);
  44004. }
  44005. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  44006. info_sk = NULL;
  44007. BIO_free(bio);
  44008. bio = NULL;
  44009. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44010. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  44011. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  44012. sk_X509_pop_free(sk, X509_free);
  44013. }
  44014. sk = NULL;
  44015. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  44016. if (i == 0) {
  44017. PKCS7_free(p7);
  44018. p7 = NULL;
  44019. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  44020. if (p7 != NULL) {
  44021. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  44022. * them */
  44023. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  44024. }
  44025. /* PKCS7_free free's the certs */
  44026. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  44027. }
  44028. BIO_free(bio);
  44029. bio = NULL;
  44030. PKCS7_free(p7);
  44031. p7 = NULL;
  44032. }
  44033. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  44034. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  44035. return EXPECT_RESULT();
  44036. }
  44037. static int test_wolfSSL_X509_STORE_CTX(void)
  44038. {
  44039. EXPECT_DECLS;
  44040. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  44041. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44042. X509_STORE_CTX* ctx = NULL;
  44043. X509_STORE* str = NULL;
  44044. X509* x509 = NULL;
  44045. #ifdef OPENSSL_ALL
  44046. X509* x5092 = NULL;
  44047. STACK_OF(X509) *sk = NULL;
  44048. STACK_OF(X509) *sk2 = NULL;
  44049. STACK_OF(X509) *sk3 = NULL;
  44050. #endif
  44051. ExpectNotNull(ctx = X509_STORE_CTX_new());
  44052. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  44053. ExpectNotNull((x509 =
  44054. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  44055. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  44056. #ifdef OPENSSL_ALL
  44057. /* sk_X509_new only in OPENSSL_ALL */
  44058. sk = sk_X509_new_null();
  44059. ExpectNotNull(sk);
  44060. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  44061. #else
  44062. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  44063. #endif
  44064. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  44065. X509_STORE_CTX_set_error(ctx, -5);
  44066. X509_STORE_CTX_set_error(NULL, -5);
  44067. X509_STORE_CTX_free(ctx);
  44068. ctx = NULL;
  44069. #ifdef OPENSSL_ALL
  44070. sk_X509_pop_free(sk, NULL);
  44071. sk = NULL;
  44072. #endif
  44073. X509_STORE_free(str);
  44074. str = NULL;
  44075. X509_free(x509);
  44076. x509 = NULL;
  44077. ExpectNotNull(ctx = X509_STORE_CTX_new());
  44078. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  44079. X509_STORE_CTX_free(ctx);
  44080. ctx = NULL;
  44081. #ifdef OPENSSL_ALL
  44082. /* test X509_STORE_CTX_get(1)_chain */
  44083. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  44084. SSL_FILETYPE_PEM)));
  44085. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  44086. SSL_FILETYPE_PEM)));
  44087. ExpectNotNull((sk = sk_X509_new_null()));
  44088. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  44089. if (EXPECT_FAIL()) {
  44090. X509_free(x509);
  44091. x509 = NULL;
  44092. }
  44093. ExpectNotNull((str = X509_STORE_new()));
  44094. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  44095. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  44096. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  44097. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  44098. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  44099. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  44100. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  44101. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  44102. X509_STORE_CTX_free(ctx);
  44103. ctx = NULL;
  44104. X509_STORE_free(str);
  44105. str = NULL;
  44106. /* CTX certs not freed yet */
  44107. X509_free(x5092);
  44108. x5092 = NULL;
  44109. sk_X509_pop_free(sk, NULL);
  44110. sk = NULL;
  44111. /* sk3 is dup so free here */
  44112. sk_X509_pop_free(sk3, NULL);
  44113. sk3 = NULL;
  44114. #endif
  44115. /* test X509_STORE_CTX_get/set_ex_data */
  44116. {
  44117. int i = 0, tmpData = 5;
  44118. void* tmpDataRet;
  44119. ExpectNotNull(ctx = X509_STORE_CTX_new());
  44120. #ifdef HAVE_EX_DATA
  44121. for (i = 0; i < MAX_EX_DATA; i++) {
  44122. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  44123. WOLFSSL_SUCCESS);
  44124. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  44125. ExpectNotNull(tmpDataRet);
  44126. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  44127. }
  44128. #else
  44129. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  44130. WOLFSSL_FAILURE);
  44131. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  44132. ExpectNull(tmpDataRet);
  44133. #endif
  44134. X509_STORE_CTX_free(ctx);
  44135. ctx = NULL;
  44136. }
  44137. /* test X509_STORE_get/set_ex_data */
  44138. {
  44139. int i = 0, tmpData = 99;
  44140. void* tmpDataRet;
  44141. ExpectNotNull(str = X509_STORE_new());
  44142. #ifdef HAVE_EX_DATA
  44143. for (i = 0; i < MAX_EX_DATA; i++) {
  44144. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  44145. WOLFSSL_SUCCESS);
  44146. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  44147. ExpectNotNull(tmpDataRet);
  44148. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  44149. }
  44150. #else
  44151. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  44152. WOLFSSL_FAILURE);
  44153. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  44154. ExpectNull(tmpDataRet);
  44155. #endif
  44156. X509_STORE_free(str);
  44157. str = NULL;
  44158. }
  44159. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  44160. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  44161. return EXPECT_RESULT();
  44162. }
  44163. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44164. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  44165. STACK_OF(X509)* chain)
  44166. {
  44167. EXPECT_DECLS;
  44168. XFILE fp = XBADFILE;
  44169. X509* cert = NULL;
  44170. ExpectTrue((fp = XFOPEN(filename, "rb"))
  44171. != XBADFILE);
  44172. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  44173. if (fp != XBADFILE) {
  44174. XFCLOSE(fp);
  44175. fp = XBADFILE;
  44176. }
  44177. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  44178. if (EXPECT_FAIL())
  44179. X509_free(cert);
  44180. return EXPECT_RESULT();
  44181. }
  44182. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  44183. int err, int loadCA)
  44184. {
  44185. EXPECT_DECLS;
  44186. X509_STORE_CTX* ctx = NULL;
  44187. X509_STORE* str = NULL;
  44188. XFILE fp = XBADFILE;
  44189. X509* cert = NULL;
  44190. STACK_OF(X509)* untrusted = NULL;
  44191. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  44192. != XBADFILE);
  44193. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  44194. if (fp != XBADFILE) {
  44195. XFCLOSE(fp);
  44196. fp = XBADFILE;
  44197. }
  44198. ExpectNotNull(str = X509_STORE_new());
  44199. ExpectNotNull(ctx = X509_STORE_CTX_new());
  44200. ExpectNotNull(untrusted = sk_X509_new_null());
  44201. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  44202. if (loadCA) {
  44203. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  44204. 1);
  44205. }
  44206. for (; *filenames; filenames++) {
  44207. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  44208. untrusted), TEST_SUCCESS);
  44209. }
  44210. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  44211. ExpectIntEQ(X509_verify_cert(ctx), ret);
  44212. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  44213. X509_free(cert);
  44214. X509_STORE_free(str);
  44215. X509_STORE_CTX_free(ctx);
  44216. sk_X509_pop_free(untrusted, NULL);
  44217. return EXPECT_RESULT();
  44218. }
  44219. #endif
  44220. static int test_X509_STORE_untrusted(void)
  44221. {
  44222. EXPECT_DECLS;
  44223. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44224. const char* untrusted1[] = {
  44225. "./certs/intermediate/ca-int2-cert.pem",
  44226. NULL
  44227. };
  44228. const char* untrusted2[] = {
  44229. "./certs/intermediate/ca-int-cert.pem",
  44230. "./certs/intermediate/ca-int2-cert.pem",
  44231. NULL
  44232. };
  44233. const char* untrusted3[] = {
  44234. "./certs/intermediate/ca-int-cert.pem",
  44235. "./certs/intermediate/ca-int2-cert.pem",
  44236. "./certs/ca-cert.pem",
  44237. NULL
  44238. };
  44239. /* Adding unrelated certs that should be ignored */
  44240. const char* untrusted4[] = {
  44241. "./certs/client-ca.pem",
  44242. "./certs/intermediate/ca-int-cert.pem",
  44243. "./certs/server-cert.pem",
  44244. "./certs/intermediate/ca-int2-cert.pem",
  44245. NULL
  44246. };
  44247. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  44248. * to loaded CA. */
  44249. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  44250. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  44251. /* Succeeds because path to loaded CA is available. */
  44252. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  44253. TEST_SUCCESS);
  44254. /* Fails because root CA is in the untrusted stack */
  44255. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  44256. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  44257. /* Succeeds because path to loaded CA is available. */
  44258. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  44259. TEST_SUCCESS);
  44260. #endif
  44261. return EXPECT_RESULT();
  44262. }
  44263. static int test_wolfSSL_X509_STORE_set_flags(void)
  44264. {
  44265. EXPECT_DECLS;
  44266. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  44267. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44268. X509_STORE* store = NULL;
  44269. X509* x509 = NULL;
  44270. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  44271. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  44272. WOLFSSL_FILETYPE_PEM)));
  44273. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  44274. #ifdef HAVE_CRL
  44275. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  44276. WOLFSSL_SUCCESS);
  44277. #else
  44278. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  44279. NOT_COMPILED_IN);
  44280. #endif
  44281. wolfSSL_X509_free(x509);
  44282. wolfSSL_X509_STORE_free(store);
  44283. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  44284. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  44285. return EXPECT_RESULT();
  44286. }
  44287. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  44288. {
  44289. EXPECT_DECLS;
  44290. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  44291. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  44292. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  44293. WOLFSSL_X509_STORE* store = NULL;
  44294. WOLFSSL_X509_LOOKUP* lookup = NULL;
  44295. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  44296. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44297. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  44298. X509_FILETYPE_PEM), 1);
  44299. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  44300. X509_FILETYPE_PEM), 1);
  44301. if (store != NULL) {
  44302. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  44303. WOLFSSL_FILETYPE_PEM), 1);
  44304. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44305. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  44306. }
  44307. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44308. X509_FILETYPE_PEM), 1);
  44309. if (store != NULL) {
  44310. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44311. WOLFSSL_FILETYPE_PEM), 1);
  44312. }
  44313. wolfSSL_X509_STORE_free(store);
  44314. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  44315. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  44316. return EXPECT_RESULT();
  44317. }
  44318. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  44319. {
  44320. EXPECT_DECLS;
  44321. #if defined(OPENSSL_EXTRA)
  44322. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  44323. time_t c_time;
  44324. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  44325. c_time = 365*24*60*60;
  44326. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  44327. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  44328. WOLFSSL_USE_CHECK_TIME);
  44329. ExpectTrue(ctx->param->check_time == c_time);
  44330. wolfSSL_X509_STORE_CTX_free(ctx);
  44331. #endif /* OPENSSL_EXTRA */
  44332. return EXPECT_RESULT();
  44333. }
  44334. static int test_wolfSSL_CTX_get0_set1_param(void)
  44335. {
  44336. EXPECT_DECLS;
  44337. #if defined(OPENSSL_EXTRA)
  44338. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  44339. SSL_CTX* ctx = NULL;
  44340. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  44341. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  44342. char testIPv4[] = "127.0.0.1";
  44343. char testhostName[] = "foo.hoge.com";
  44344. #ifndef NO_WOLFSSL_SERVER
  44345. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  44346. #else
  44347. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  44348. #endif
  44349. ExpectNull(SSL_CTX_get0_param(NULL));
  44350. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  44351. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  44352. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  44353. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  44354. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  44355. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  44356. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  44357. WOLFSSL_SUCCESS);
  44358. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  44359. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  44360. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  44361. (int)XSTRLEN(testhostName)));
  44362. ExpectIntEQ(0x01, pParam->hostFlags);
  44363. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  44364. /* test for incorrect parameter */
  44365. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  44366. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  44367. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  44368. SSL_CTX_free(ctx);
  44369. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  44370. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  44371. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  44372. return EXPECT_RESULT();
  44373. }
  44374. static int test_wolfSSL_get0_param(void)
  44375. {
  44376. EXPECT_DECLS;
  44377. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44378. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  44379. SSL_CTX* ctx = NULL;
  44380. SSL* ssl = NULL;
  44381. #ifndef NO_WOLFSSL_SERVER
  44382. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  44383. #else
  44384. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  44385. #endif
  44386. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  44387. SSL_FILETYPE_PEM));
  44388. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  44389. ExpectNotNull(ssl = SSL_new(ctx));
  44390. ExpectNotNull(SSL_get0_param(ssl));
  44391. SSL_free(ssl);
  44392. SSL_CTX_free(ctx);
  44393. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  44394. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  44395. return EXPECT_RESULT();
  44396. }
  44397. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  44398. {
  44399. EXPECT_DECLS;
  44400. #if defined(OPENSSL_EXTRA)
  44401. const char host[] = "www.example.com";
  44402. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  44403. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  44404. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  44405. if (pParam != NULL) {
  44406. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  44407. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  44408. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  44409. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  44410. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  44411. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44412. }
  44413. #endif /* OPENSSL_EXTRA */
  44414. return EXPECT_RESULT();
  44415. }
  44416. static int test_wolfSSL_set1_host(void)
  44417. {
  44418. EXPECT_DECLS;
  44419. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44420. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  44421. const char host[] = "www.test_wolfSSL_set1_host.com";
  44422. const char emptyStr[] = "";
  44423. SSL_CTX* ctx = NULL;
  44424. SSL* ssl = NULL;
  44425. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  44426. #ifndef NO_WOLFSSL_SERVER
  44427. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  44428. #else
  44429. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  44430. #endif
  44431. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  44432. SSL_FILETYPE_PEM));
  44433. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  44434. ExpectNotNull(ssl = SSL_new(ctx));
  44435. pParam = SSL_get0_param(ssl);
  44436. /* we should get back host string */
  44437. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  44438. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  44439. /* we should get back empty string */
  44440. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  44441. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  44442. /* we should get back host string */
  44443. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  44444. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  44445. /* we should get back empty string */
  44446. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  44447. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  44448. SSL_free(ssl);
  44449. SSL_CTX_free(ctx);
  44450. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  44451. #endif /* OPENSSL_EXTRA */
  44452. return EXPECT_RESULT();
  44453. }
  44454. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  44455. {
  44456. EXPECT_DECLS;
  44457. #if defined(OPENSSL_EXTRA)
  44458. unsigned char buf[16] = {0};
  44459. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  44460. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  44461. /* test 127.0.0.1 */
  44462. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  44463. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  44464. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  44465. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  44466. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  44467. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  44468. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  44469. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  44470. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  44471. ExpectIntEQ(XSTRNCMP(param->ipasc,
  44472. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  44473. /* test 2001:db8:: */
  44474. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  44475. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  44476. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  44477. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  44478. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  44479. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  44480. /* test ::1234:5678 */
  44481. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  44482. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  44483. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  44484. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  44485. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  44486. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  44487. /* test 2001:db8::1234:5678 */
  44488. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  44489. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  44490. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  44491. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  44492. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  44493. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  44494. sizeof(param->ipasc)), 0);
  44495. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  44496. /* 2001:db8:1::ab9:c0a8:102 */
  44497. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  44498. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  44499. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  44500. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  44501. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  44502. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  44503. sizeof(param->ipasc)), 0);
  44504. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44505. #endif /* OPENSSL_EXTRA */
  44506. return EXPECT_RESULT();
  44507. }
  44508. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  44509. {
  44510. EXPECT_DECLS;
  44511. #if defined(OPENSSL_EXTRA)
  44512. X509_STORE* store = NULL;
  44513. X509_STORE_CTX* ctx = NULL;
  44514. X509_STORE_CTX* ctx_no_init = NULL;
  44515. ExpectNotNull((store = X509_STORE_new()));
  44516. ExpectNotNull(ctx = X509_STORE_CTX_new());
  44517. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  44518. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  44519. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  44520. /* should return NULL if ctx has not bee initialized */
  44521. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  44522. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  44523. wolfSSL_X509_STORE_CTX_free(ctx);
  44524. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  44525. X509_STORE_free(store);
  44526. #endif /* OPENSSL_EXTRA */
  44527. return EXPECT_RESULT();
  44528. }
  44529. static int test_wolfSSL_CTX_set_client_CA_list(void)
  44530. {
  44531. EXPECT_DECLS;
  44532. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  44533. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  44534. WOLFSSL_CTX* ctx = NULL;
  44535. WOLFSSL* ssl = NULL;
  44536. X509_NAME* name = NULL;
  44537. STACK_OF(X509_NAME)* names = NULL;
  44538. STACK_OF(X509_NAME)* ca_list = NULL;
  44539. int names_len = 0;
  44540. int i;
  44541. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  44542. /* Send two X501 names in cert request */
  44543. names = SSL_load_client_CA_file(cliCertFile);
  44544. ExpectNotNull(names);
  44545. ca_list = SSL_load_client_CA_file(caCertFile);
  44546. ExpectNotNull(ca_list);
  44547. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  44548. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  44549. if (EXPECT_FAIL()) {
  44550. wolfSSL_X509_NAME_free(name);
  44551. name = NULL;
  44552. }
  44553. SSL_CTX_set_client_CA_list(ctx, names);
  44554. /* This should only free the stack structure */
  44555. sk_X509_NAME_free(ca_list);
  44556. ca_list = NULL;
  44557. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  44558. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  44559. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  44560. for (i = 0; i < names_len; i++) {
  44561. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  44562. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  44563. }
  44564. /* Needed to be able to create ssl object */
  44565. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  44566. SSL_FILETYPE_PEM));
  44567. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  44568. ExpectNotNull(ssl = wolfSSL_new(ctx));
  44569. /* load again as old names are responsibility of ctx to free*/
  44570. names = SSL_load_client_CA_file(cliCertFile);
  44571. ExpectNotNull(names);
  44572. SSL_set_client_CA_list(ssl, names);
  44573. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  44574. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  44575. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  44576. for (i = 0; i < names_len; i++) {
  44577. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  44578. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  44579. }
  44580. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  44581. {
  44582. tcp_ready ready;
  44583. func_args server_args;
  44584. callback_functions server_cb;
  44585. THREAD_TYPE serverThread;
  44586. WOLFSSL* ssl_client = NULL;
  44587. WOLFSSL_CTX* ctx_client = NULL;
  44588. SOCKET_T sockfd = 0;
  44589. /* wolfSSL_get_client_CA_list() with handshake */
  44590. StartTCP();
  44591. InitTcpReady(&ready);
  44592. XMEMSET(&server_args, 0, sizeof(func_args));
  44593. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  44594. server_args.signal = &ready;
  44595. server_args.callbacks = &server_cb;
  44596. /* we are responsible for free'ing WOLFSSL_CTX */
  44597. server_cb.ctx = ctx;
  44598. server_cb.isSharedCtx = 1;
  44599. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  44600. cliCertFile, 0));
  44601. start_thread(test_server_nofail, &server_args, &serverThread);
  44602. wait_tcp_ready(&server_args);
  44603. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  44604. ExpectNotNull(ctx_client =
  44605. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  44606. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  44607. ctx_client, caCertFile, 0));
  44608. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  44609. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  44610. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  44611. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  44612. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  44613. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  44614. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  44615. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  44616. /* We are expecting two cert names to be sent */
  44617. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  44618. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  44619. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  44620. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  44621. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  44622. }
  44623. wolfSSL_shutdown(ssl_client);
  44624. wolfSSL_free(ssl_client);
  44625. wolfSSL_CTX_free(ctx_client);
  44626. CloseSocket(sockfd);
  44627. join_thread(serverThread);
  44628. FreeTcpReady(&ready);
  44629. }
  44630. #endif
  44631. wolfSSL_free(ssl);
  44632. wolfSSL_CTX_free(ctx);
  44633. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  44634. * !NO_BIO */
  44635. return EXPECT_RESULT();
  44636. }
  44637. static int test_wolfSSL_CTX_add_client_CA(void)
  44638. {
  44639. EXPECT_DECLS;
  44640. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  44641. !defined(NO_WOLFSSL_CLIENT)
  44642. WOLFSSL_CTX* ctx = NULL;
  44643. WOLFSSL_X509* x509 = NULL;
  44644. WOLFSSL_X509* x509_a = NULL;
  44645. STACK_OF(X509_NAME)* ca_list = NULL;
  44646. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  44647. /* Add client cert */
  44648. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  44649. SSL_FILETYPE_PEM));
  44650. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  44651. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  44652. /* Add another client cert */
  44653. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  44654. SSL_FILETYPE_PEM));
  44655. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  44656. /* test for incorrect parameter */
  44657. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  44658. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  44659. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  44660. X509_free(x509);
  44661. X509_free(x509_a);
  44662. SSL_CTX_free(ctx);
  44663. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  44664. return EXPECT_RESULT();
  44665. }
  44666. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  44667. defined(HAVE_IO_TESTS_DEPENDENCIES)
  44668. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  44669. {
  44670. callback_functions* callbacks = ((func_args*)args)->callbacks;
  44671. WOLFSSL_CTX* ctx = callbacks->ctx;
  44672. WOLFSSL* ssl = NULL;
  44673. SOCKET_T sfd = 0;
  44674. SOCKET_T cfd = 0;
  44675. word16 port;
  44676. char input[1024];
  44677. int idx;
  44678. int ret, err = 0;
  44679. const char* privateName = "ech-private-name.com";
  44680. int privateNameLen = (int)XSTRLEN(privateName);
  44681. ((func_args*)args)->return_code = TEST_FAIL;
  44682. port = ((func_args*)args)->signal->port;
  44683. AssertIntEQ(WOLFSSL_SUCCESS,
  44684. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  44685. AssertIntEQ(WOLFSSL_SUCCESS,
  44686. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  44687. WOLFSSL_FILETYPE_PEM));
  44688. AssertIntEQ(WOLFSSL_SUCCESS,
  44689. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  44690. WOLFSSL_FILETYPE_PEM));
  44691. if (callbacks->ctx_ready)
  44692. callbacks->ctx_ready(ctx);
  44693. ssl = wolfSSL_new(ctx);
  44694. /* set the sni for the server */
  44695. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  44696. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  44697. CloseSocket(sfd);
  44698. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  44699. if (callbacks->ssl_ready)
  44700. callbacks->ssl_ready(ssl);
  44701. do {
  44702. err = 0; /* Reset error */
  44703. ret = wolfSSL_accept(ssl);
  44704. if (ret != WOLFSSL_SUCCESS) {
  44705. err = wolfSSL_get_error(ssl, 0);
  44706. }
  44707. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  44708. if (ret != WOLFSSL_SUCCESS) {
  44709. char buff[WOLFSSL_MAX_ERROR_SZ];
  44710. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  44711. }
  44712. else {
  44713. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  44714. input[idx] = 0;
  44715. fprintf(stderr, "Client message: %s\n", input);
  44716. }
  44717. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  44718. privateNameLen));
  44719. ((func_args*)args)->return_code = TEST_SUCCESS;
  44720. }
  44721. if (callbacks->on_result)
  44722. callbacks->on_result(ssl);
  44723. wolfSSL_shutdown(ssl);
  44724. wolfSSL_free(ssl);
  44725. wolfSSL_CTX_free(ctx);
  44726. CloseSocket(cfd);
  44727. #ifdef FP_ECC
  44728. wc_ecc_fp_free();
  44729. #endif
  44730. WOLFSSL_RETURN_FROM_THREAD(0);
  44731. }
  44732. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  44733. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  44734. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  44735. {
  44736. AssertNotNull(ssl);
  44737. AssertNotNull(line);
  44738. XFILE fp;
  44739. const byte lf = '\n';
  44740. fp = XFOPEN("./MyKeyLog.txt", "a");
  44741. XFWRITE( line, 1, strlen(line),fp);
  44742. XFWRITE( (void*)&lf,1,1,fp);
  44743. XFFLUSH(fp);
  44744. XFCLOSE(fp);
  44745. }
  44746. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  44747. static int test_wolfSSL_CTX_set_keylog_callback(void)
  44748. {
  44749. EXPECT_DECLS;
  44750. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  44751. !defined(NO_WOLFSSL_CLIENT)
  44752. SSL_CTX* ctx = NULL;
  44753. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  44754. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  44755. SSL_CTX_free(ctx);
  44756. SSL_CTX_set_keylog_callback(NULL, NULL);
  44757. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  44758. return EXPECT_RESULT();
  44759. }
  44760. static int test_wolfSSL_CTX_get_keylog_callback(void)
  44761. {
  44762. EXPECT_DECLS;
  44763. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  44764. !defined(NO_WOLFSSL_CLIENT)
  44765. SSL_CTX* ctx = NULL;
  44766. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  44767. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  44768. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  44769. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  44770. SSL_CTX_set_keylog_callback(ctx, NULL );
  44771. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  44772. SSL_CTX_free(ctx);
  44773. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  44774. return EXPECT_RESULT();
  44775. }
  44776. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  44777. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  44778. {
  44779. /* set keylog callback */
  44780. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  44781. return TEST_SUCCESS;
  44782. }
  44783. #endif
  44784. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  44785. {
  44786. EXPECT_DECLS;
  44787. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  44788. /* This test is intended for checking whether keylog callback is called
  44789. * in client during TLS handshake between the client and a server.
  44790. */
  44791. test_ssl_cbf server_cbf;
  44792. test_ssl_cbf client_cbf;
  44793. XFILE fp = XBADFILE;
  44794. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  44795. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  44796. server_cbf.method = wolfTLSv1_2_server_method;
  44797. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  44798. /* clean up keylog file */
  44799. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  44800. if (fp != XBADFILE) {
  44801. XFFLUSH(fp);
  44802. XFCLOSE(fp);
  44803. fp = XBADFILE;
  44804. }
  44805. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  44806. &server_cbf, NULL), TEST_SUCCESS);
  44807. XSLEEP_MS(100);
  44808. /* check if the keylog file exists */
  44809. char buff[300] = {0};
  44810. int found = 0;
  44811. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  44812. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  44813. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  44814. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  44815. found = 1;
  44816. break;
  44817. }
  44818. }
  44819. if (fp != XBADFILE) {
  44820. XFCLOSE(fp);
  44821. }
  44822. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  44823. ExpectIntEQ(found, 1);
  44824. /* clean up */
  44825. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  44826. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  44827. return EXPECT_RESULT();
  44828. }
  44829. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  44830. defined(HAVE_SECRET_CALLBACK)
  44831. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  44832. {
  44833. /* set keylog callback */
  44834. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  44835. return TEST_SUCCESS;
  44836. }
  44837. #endif
  44838. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  44839. {
  44840. EXPECT_DECLS;
  44841. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  44842. defined(HAVE_SECRET_CALLBACK)
  44843. /* This test is intended for checking whether keylog callback is called
  44844. * in client during TLS handshake between the client and a server.
  44845. */
  44846. test_ssl_cbf server_cbf;
  44847. test_ssl_cbf client_cbf;
  44848. XFILE fp = XBADFILE;
  44849. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  44850. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  44851. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  44852. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  44853. /* clean up keylog file */
  44854. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  44855. if (fp != XBADFILE) {
  44856. XFCLOSE(fp);
  44857. fp = XBADFILE;
  44858. }
  44859. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  44860. &server_cbf, NULL), TEST_SUCCESS);
  44861. /* check if the keylog file exists */
  44862. {
  44863. char buff[300] = {0};
  44864. int found[4] = {0};
  44865. int numfnd = 0;
  44866. int i;
  44867. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  44868. while (EXPECT_SUCCESS() &&
  44869. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  44870. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  44871. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  44872. found[0] = 1;
  44873. continue;
  44874. }
  44875. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  44876. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  44877. found[1] = 1;
  44878. continue;
  44879. }
  44880. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  44881. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  44882. found[2] = 1;
  44883. continue;
  44884. }
  44885. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  44886. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  44887. found[3] = 1;
  44888. continue;
  44889. }
  44890. }
  44891. if (fp != XBADFILE)
  44892. XFCLOSE(fp);
  44893. for (i = 0; i < 4; i++) {
  44894. if (found[i] != 0)
  44895. numfnd++;
  44896. }
  44897. ExpectIntEQ(numfnd, 4);
  44898. }
  44899. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  44900. return EXPECT_RESULT();
  44901. }
  44902. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  44903. defined(HAVE_IO_TESTS_DEPENDENCIES)
  44904. static int test_wolfSSL_Tls13_ECH_params(void)
  44905. {
  44906. EXPECT_DECLS;
  44907. #if !defined(NO_WOLFSSL_CLIENT)
  44908. word32 outputLen = 0;
  44909. byte testBuf[72];
  44910. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  44911. WOLFSSL *ssl = wolfSSL_new(ctx);
  44912. ExpectNotNull(ctx);
  44913. ExpectNotNull(ssl);
  44914. /* invalid ctx */
  44915. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  44916. "ech-public-name.com", 0, 0, 0));
  44917. /* invalid public name */
  44918. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  44919. 0, 0));
  44920. /* invalid algorithms */
  44921. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  44922. "ech-public-name.com", 1000, 1000, 1000));
  44923. /* invalid ctx */
  44924. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  44925. &outputLen));
  44926. /* invalid output len */
  44927. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  44928. /* invalid ssl */
  44929. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  44930. (char*)testBuf, sizeof(testBuf)));
  44931. /* invalid configs64 */
  44932. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  44933. sizeof(testBuf)));
  44934. /* invalid size */
  44935. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  44936. (char*)testBuf, 0));
  44937. /* invalid ssl */
  44938. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  44939. sizeof(testBuf)));
  44940. /* invalid configs */
  44941. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  44942. sizeof(testBuf)));
  44943. /* invalid size */
  44944. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  44945. /* invalid ssl */
  44946. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  44947. /* invalid size */
  44948. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  44949. wolfSSL_free(ssl);
  44950. wolfSSL_CTX_free(ctx);
  44951. #endif /* !NO_WOLFSSL_CLIENT */
  44952. return EXPECT_RESULT();
  44953. }
  44954. static int test_wolfSSL_Tls13_ECH(void)
  44955. {
  44956. EXPECT_DECLS;
  44957. tcp_ready ready;
  44958. func_args client_args;
  44959. func_args server_args;
  44960. THREAD_TYPE serverThread;
  44961. callback_functions server_cbf;
  44962. callback_functions client_cbf;
  44963. SOCKET_T sockfd = 0;
  44964. WOLFSSL_CTX* ctx = NULL;
  44965. WOLFSSL* ssl = NULL;
  44966. const char* publicName = "ech-public-name.com";
  44967. const char* privateName = "ech-private-name.com";
  44968. int privateNameLen = 20;
  44969. char reply[1024];
  44970. int replyLen = 0;
  44971. byte rawEchConfig[128];
  44972. word32 rawEchConfigLen = sizeof(rawEchConfig);
  44973. InitTcpReady(&ready);
  44974. ready.port = 22222;
  44975. XMEMSET(&client_args, 0, sizeof(func_args));
  44976. XMEMSET(&server_args, 0, sizeof(func_args));
  44977. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  44978. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  44979. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  44980. /* create the server context here so we can get the ech config */
  44981. ExpectNotNull(server_cbf.ctx =
  44982. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  44983. /* generate ech config */
  44984. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  44985. publicName, 0, 0, 0));
  44986. /* get the config for the client to use */
  44987. ExpectIntEQ(WOLFSSL_SUCCESS,
  44988. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  44989. &rawEchConfigLen));
  44990. server_args.callbacks = &server_cbf;
  44991. server_args.signal = &ready;
  44992. /* start server task */
  44993. start_thread(server_task_ech, &server_args, &serverThread);
  44994. wait_tcp_ready(&server_args);
  44995. /* run as a TLS1.3 client */
  44996. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  44997. ExpectIntEQ(WOLFSSL_SUCCESS,
  44998. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  44999. ExpectIntEQ(WOLFSSL_SUCCESS,
  45000. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  45001. ExpectIntEQ(WOLFSSL_SUCCESS,
  45002. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  45003. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  45004. /* get connected the server task */
  45005. ExpectNotNull(ssl = wolfSSL_new(ctx));
  45006. /* set the ech configs for the client */
  45007. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  45008. rawEchConfigLen));
  45009. /* set the sni for the client */
  45010. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  45011. privateName, privateNameLen));
  45012. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  45013. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  45014. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  45015. privateNameLen);
  45016. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  45017. /* add th null terminator for string compare */
  45018. reply[replyLen] = 0;
  45019. /* check that the server replied with the private name */
  45020. ExpectStrEQ(privateName, reply);
  45021. wolfSSL_free(ssl);
  45022. wolfSSL_CTX_free(ctx);
  45023. CloseSocket(sockfd);
  45024. join_thread(serverThread);
  45025. FreeTcpReady(&ready);
  45026. return EXPECT_RESULT();
  45027. }
  45028. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  45029. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  45030. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  45031. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  45032. static int post_auth_version_cb(WOLFSSL* ssl)
  45033. {
  45034. EXPECT_DECLS;
  45035. /* do handshake and then test version error */
  45036. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  45037. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  45038. return EXPECT_RESULT();
  45039. }
  45040. static int post_auth_version_client_cb(WOLFSSL* ssl)
  45041. {
  45042. EXPECT_DECLS;
  45043. /* do handshake and then test version error */
  45044. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  45045. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  45046. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  45047. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  45048. /* check was added to error queue */
  45049. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  45050. /* check the string matches expected string */
  45051. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  45052. "WRONG_SSL_VERSION");
  45053. #endif
  45054. return EXPECT_RESULT();
  45055. }
  45056. static int post_auth_cb(WOLFSSL* ssl)
  45057. {
  45058. EXPECT_DECLS;
  45059. WOLFSSL_X509* x509 = NULL;
  45060. /* do handshake and then test version error */
  45061. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  45062. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  45063. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  45064. wolfSSL_X509_free(x509);
  45065. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  45066. return EXPECT_RESULT();
  45067. }
  45068. static int set_post_auth_cb(WOLFSSL* ssl)
  45069. {
  45070. if (!wolfSSL_is_server(ssl)) {
  45071. EXPECT_DECLS;
  45072. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  45073. return EXPECT_RESULT();
  45074. }
  45075. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  45076. return TEST_SUCCESS;
  45077. }
  45078. #endif
  45079. static int test_wolfSSL_Tls13_postauth(void)
  45080. {
  45081. EXPECT_DECLS;
  45082. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  45083. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  45084. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  45085. test_ssl_cbf server_cbf;
  45086. test_ssl_cbf client_cbf;
  45087. /* test version failure doing post auth with TLS 1.2 connection */
  45088. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  45089. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  45090. server_cbf.method = wolfTLSv1_2_server_method;
  45091. server_cbf.ssl_ready = set_post_auth_cb;
  45092. server_cbf.on_result = post_auth_version_cb;
  45093. client_cbf.ssl_ready = set_post_auth_cb;
  45094. client_cbf.on_result = post_auth_version_client_cb;
  45095. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  45096. &server_cbf, NULL), TEST_SUCCESS);
  45097. /* tests on post auth with TLS 1.3 */
  45098. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  45099. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  45100. server_cbf.method = wolfTLSv1_3_server_method;
  45101. server_cbf.ssl_ready = set_post_auth_cb;
  45102. client_cbf.ssl_ready = set_post_auth_cb;
  45103. server_cbf.on_result = post_auth_cb;
  45104. client_cbf.on_result = NULL;
  45105. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  45106. &server_cbf, NULL), TEST_SUCCESS);
  45107. #endif
  45108. return EXPECT_RESULT();
  45109. }
  45110. static int test_wolfSSL_X509_NID(void)
  45111. {
  45112. EXPECT_DECLS;
  45113. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  45114. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  45115. int sigType;
  45116. int nameSz;
  45117. X509* cert = NULL;
  45118. EVP_PKEY* pubKeyTmp = NULL;
  45119. X509_NAME* name = NULL;
  45120. char commonName[80];
  45121. char countryName[80];
  45122. char localityName[80];
  45123. char stateName[80];
  45124. char orgName[80];
  45125. char orgUnit[80];
  45126. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  45127. /* convert cert from DER to internal WOLFSSL_X509 struct */
  45128. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  45129. sizeof_client_cert_der_2048, HEAP_HINT));
  45130. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  45131. /* extract PUBLIC KEY from cert */
  45132. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  45133. /* extract signatureType */
  45134. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  45135. /* extract subjectName info */
  45136. ExpectNotNull(name = X509_get_subject_name(cert));
  45137. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  45138. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  45139. NULL, 0)), 0);
  45140. ExpectIntEQ(nameSz, 15);
  45141. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  45142. commonName, sizeof(commonName))), 0);
  45143. ExpectIntEQ(nameSz, 15);
  45144. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  45145. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  45146. commonName, 9)), 0);
  45147. ExpectIntEQ(nameSz, 8);
  45148. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  45149. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  45150. countryName, sizeof(countryName))), 0);
  45151. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  45152. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  45153. localityName, sizeof(localityName))), 0);
  45154. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  45155. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  45156. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  45157. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  45158. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  45159. orgName, sizeof(orgName))), 0);
  45160. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  45161. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  45162. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  45163. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  45164. EVP_PKEY_free(pubKeyTmp);
  45165. X509_free(cert);
  45166. #endif
  45167. return EXPECT_RESULT();
  45168. }
  45169. static int test_wolfSSL_CTX_set_srp_username(void)
  45170. {
  45171. EXPECT_DECLS;
  45172. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  45173. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  45174. WOLFSSL_CTX* ctx = NULL;
  45175. WOLFSSL* ssl = NULL;
  45176. const char *username = "TESTUSER";
  45177. const char *password = "TESTPASSWORD";
  45178. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  45179. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  45180. SSL_SUCCESS);
  45181. wolfSSL_CTX_free(ctx);
  45182. ctx = NULL;
  45183. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  45184. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  45185. SSL_SUCCESS);
  45186. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  45187. SSL_SUCCESS);
  45188. ExpectNotNull(ssl = SSL_new(ctx));
  45189. ExpectNotNull(SSL_get_srp_username(ssl));
  45190. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  45191. wolfSSL_free(ssl);
  45192. wolfSSL_CTX_free(ctx);
  45193. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  45194. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  45195. return EXPECT_RESULT();
  45196. }
  45197. static int test_wolfSSL_CTX_set_srp_password(void)
  45198. {
  45199. EXPECT_DECLS;
  45200. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  45201. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  45202. WOLFSSL_CTX* ctx = NULL;
  45203. const char *username = "TESTUSER";
  45204. const char *password = "TESTPASSWORD";
  45205. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  45206. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  45207. SSL_SUCCESS);
  45208. wolfSSL_CTX_free(ctx);
  45209. ctx = NULL;
  45210. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  45211. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  45212. SSL_SUCCESS);
  45213. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  45214. SSL_SUCCESS);
  45215. wolfSSL_CTX_free(ctx);
  45216. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  45217. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  45218. return EXPECT_RESULT();
  45219. }
  45220. static int test_wolfSSL_X509_STORE(void)
  45221. {
  45222. EXPECT_DECLS;
  45223. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45224. X509_STORE *store = NULL;
  45225. #ifdef HAVE_CRL
  45226. X509_STORE_CTX *storeCtx = NULL;
  45227. X509 *ca = NULL;
  45228. X509 *cert = NULL;
  45229. const char srvCert[] = "./certs/server-revoked-cert.pem";
  45230. const char caCert[] = "./certs/ca-cert.pem";
  45231. #ifndef WOLFSSL_CRL_ALLOW_MISSING_CDP
  45232. X509_CRL *crl = NULL;
  45233. const char crlPem[] = "./certs/crl/crl.revoked";
  45234. XFILE fp = XBADFILE;
  45235. #endif /* !WOLFSSL_CRL_ALLOW_MISSING_CDP */
  45236. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45237. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  45238. SSL_FILETYPE_PEM)));
  45239. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  45240. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  45241. SSL_FILETYPE_PEM)));
  45242. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  45243. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  45244. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  45245. X509_STORE_free(store);
  45246. store = NULL;
  45247. X509_STORE_CTX_free(storeCtx);
  45248. storeCtx = NULL;
  45249. X509_free(cert);
  45250. cert = NULL;
  45251. X509_free(ca);
  45252. ca = NULL;
  45253. #ifndef WOLFSSL_CRL_ALLOW_MISSING_CDP
  45254. /* should fail to verify now after adding in CRL */
  45255. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45256. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  45257. SSL_FILETYPE_PEM)));
  45258. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  45259. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  45260. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45261. NULL, NULL));
  45262. if (fp != XBADFILE)
  45263. XFCLOSE(fp);
  45264. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  45265. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  45266. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  45267. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  45268. SSL_FILETYPE_PEM)));
  45269. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  45270. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  45271. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  45272. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  45273. X509_CRL_free(crl);
  45274. crl = NULL;
  45275. X509_STORE_free(store);
  45276. store = NULL;
  45277. X509_STORE_CTX_free(storeCtx);
  45278. storeCtx = NULL;
  45279. X509_free(cert);
  45280. cert = NULL;
  45281. X509_free(ca);
  45282. ca = NULL;
  45283. #endif /* !WOLFSSL_CRL_ALLOW_MISSING_CDP */
  45284. #endif /* HAVE_CRL */
  45285. #ifndef WOLFCRYPT_ONLY
  45286. {
  45287. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  45288. SSL_CTX* ctx = NULL;
  45289. SSL* ssl = NULL;
  45290. int i;
  45291. for (i = 0; i < 2; i++) {
  45292. #ifndef NO_WOLFSSL_SERVER
  45293. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  45294. #else
  45295. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  45296. #endif
  45297. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45298. SSL_CTX_set_cert_store(ctx, store);
  45299. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45300. SSL_CTX_set_cert_store(ctx, store);
  45301. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45302. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  45303. SSL_FILETYPE_PEM), SSL_SUCCESS);
  45304. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  45305. SSL_FILETYPE_PEM), SSL_SUCCESS);
  45306. ExpectNotNull(ssl = SSL_new(ctx));
  45307. if (i == 0) {
  45308. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  45309. SSL_SUCCESS);
  45310. }
  45311. else {
  45312. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  45313. #ifdef OPENSSL_ALL
  45314. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  45315. #endif
  45316. }
  45317. if (EXPECT_FAIL() || (i == 1)) {
  45318. X509_STORE_free(store);
  45319. store = NULL;
  45320. }
  45321. SSL_free(ssl);
  45322. ssl = NULL;
  45323. SSL_CTX_free(ctx);
  45324. ctx = NULL;
  45325. }
  45326. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  45327. }
  45328. #endif
  45329. #endif
  45330. return EXPECT_RESULT();
  45331. }
  45332. static int test_wolfSSL_X509_STORE_load_locations(void)
  45333. {
  45334. EXPECT_DECLS;
  45335. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  45336. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  45337. SSL_CTX *ctx = NULL;
  45338. X509_STORE *store = NULL;
  45339. const char ca_file[] = "./certs/ca-cert.pem";
  45340. const char client_pem_file[] = "./certs/client-cert.pem";
  45341. const char client_der_file[] = "./certs/client-cert.der";
  45342. const char ecc_file[] = "./certs/ecc-key.pem";
  45343. const char certs_path[] = "./certs/";
  45344. const char bad_path[] = "./bad-path/";
  45345. #ifdef HAVE_CRL
  45346. const char crl_path[] = "./certs/crl/";
  45347. const char crl_file[] = "./certs/crl/crl.pem";
  45348. #endif
  45349. #ifndef NO_WOLFSSL_SERVER
  45350. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  45351. #else
  45352. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  45353. #endif
  45354. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  45355. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  45356. WOLFSSL_SUCCESS);
  45357. /* Test bad arguments */
  45358. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  45359. WOLFSSL_FAILURE);
  45360. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  45361. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  45362. WOLFSSL_FAILURE);
  45363. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  45364. WOLFSSL_FAILURE);
  45365. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  45366. WOLFSSL_FAILURE);
  45367. #ifdef HAVE_CRL
  45368. /* Test with CRL */
  45369. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  45370. WOLFSSL_SUCCESS);
  45371. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  45372. WOLFSSL_SUCCESS);
  45373. #endif
  45374. /* Test with CA */
  45375. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  45376. WOLFSSL_SUCCESS);
  45377. /* Test with client_cert and certs path */
  45378. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  45379. WOLFSSL_SUCCESS);
  45380. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  45381. WOLFSSL_SUCCESS);
  45382. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  45383. /* Clear nodes */
  45384. ERR_clear_error();
  45385. #endif
  45386. SSL_CTX_free(ctx);
  45387. #endif
  45388. return EXPECT_RESULT();
  45389. }
  45390. static int test_X509_STORE_get0_objects(void)
  45391. {
  45392. EXPECT_DECLS;
  45393. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  45394. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  45395. X509_STORE *store = NULL;
  45396. X509_STORE *store_cpy = NULL;
  45397. SSL_CTX *ctx = NULL;
  45398. X509_OBJECT *obj = NULL;
  45399. STACK_OF(X509_OBJECT) *objs = NULL;
  45400. int i;
  45401. /* Setup store */
  45402. #ifndef NO_WOLFSSL_SERVER
  45403. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  45404. #else
  45405. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  45406. #endif
  45407. ExpectNotNull(store_cpy = X509_STORE_new());
  45408. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  45409. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  45410. WOLFSSL_SUCCESS);
  45411. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  45412. WOLFSSL_SUCCESS);
  45413. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  45414. WOLFSSL_SUCCESS);
  45415. #ifdef HAVE_CRL
  45416. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  45417. WOLFSSL_SUCCESS);
  45418. #endif
  45419. /* Store ready */
  45420. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  45421. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  45422. #ifdef HAVE_CRL
  45423. #ifdef WOLFSSL_SIGNER_DER_CERT
  45424. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  45425. #else
  45426. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  45427. #endif
  45428. #else
  45429. #ifdef WOLFSSL_SIGNER_DER_CERT
  45430. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  45431. #else
  45432. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  45433. #endif
  45434. #endif
  45435. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  45436. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  45437. switch (X509_OBJECT_get_type(obj)) {
  45438. case X509_LU_X509:
  45439. {
  45440. WOLFSSL_X509* x509;
  45441. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  45442. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  45443. break;
  45444. }
  45445. case X509_LU_CRL:
  45446. #ifdef HAVE_CRL
  45447. {
  45448. WOLFSSL_CRL* crl = NULL;
  45449. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  45450. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  45451. break;
  45452. }
  45453. #endif
  45454. case X509_LU_NONE:
  45455. default:
  45456. Fail(("X509_OBJECT_get_type should return x509 or crl "
  45457. "(when built with crl support)"),
  45458. ("Unrecognized X509_OBJECT type or none"));
  45459. }
  45460. }
  45461. X509_STORE_free(store_cpy);
  45462. SSL_CTX_free(ctx);
  45463. #endif
  45464. return EXPECT_RESULT();
  45465. }
  45466. static int test_wolfSSL_BN_CTX(void)
  45467. {
  45468. EXPECT_DECLS;
  45469. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  45470. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  45471. WOLFSSL_BN_CTX* bn_ctx = NULL;
  45472. WOLFSSL_BIGNUM* t = NULL;
  45473. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  45474. /* No implementation. */
  45475. BN_CTX_init(NULL);
  45476. ExpectNotNull(t = BN_CTX_get(NULL));
  45477. BN_free(t);
  45478. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  45479. BN_free(t);
  45480. #ifndef NO_WOLFSSL_STUB
  45481. /* No implementation. */
  45482. BN_CTX_start(NULL);
  45483. BN_CTX_start(bn_ctx);
  45484. #endif
  45485. BN_CTX_free(NULL);
  45486. BN_CTX_free(bn_ctx);
  45487. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  45488. return EXPECT_RESULT();
  45489. }
  45490. static int test_wolfSSL_BN(void)
  45491. {
  45492. EXPECT_DECLS;
  45493. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  45494. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  45495. BIGNUM* a = NULL;
  45496. BIGNUM* b = NULL;
  45497. BIGNUM* c = NULL;
  45498. BIGNUM* d = NULL;
  45499. BIGNUM emptyBN;
  45500. /* Setup */
  45501. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  45502. /* internal not set emptyBN. */
  45503. ExpectNotNull(a = BN_new());
  45504. ExpectNotNull(b = BN_new());
  45505. ExpectNotNull(c = BN_dup(b));
  45506. ExpectNotNull(d = BN_new());
  45507. /* Invalid parameter testing. */
  45508. BN_free(NULL);
  45509. ExpectNull(BN_dup(NULL));
  45510. ExpectNull(BN_dup(&emptyBN));
  45511. ExpectNull(BN_copy(NULL, NULL));
  45512. ExpectNull(BN_copy(b, NULL));
  45513. ExpectNull(BN_copy(NULL, c));
  45514. ExpectNull(BN_copy(b, &emptyBN));
  45515. ExpectNull(BN_copy(&emptyBN, c));
  45516. BN_clear(NULL);
  45517. BN_clear(&emptyBN);
  45518. ExpectIntEQ(BN_num_bytes(NULL), 0);
  45519. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  45520. ExpectIntEQ(BN_num_bits(NULL), 0);
  45521. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  45522. ExpectIntEQ(BN_is_negative(NULL), 0);
  45523. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  45524. /* END Invalid Parameters */
  45525. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  45526. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  45527. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  45528. ExpectIntEQ(BN_num_bits(a), 2);
  45529. ExpectIntEQ(BN_num_bytes(a), 1);
  45530. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  45531. defined(WOLFSSL_SP_INT_NEGATIVE))
  45532. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  45533. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  45534. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  45535. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  45536. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  45537. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  45538. {
  45539. /* Do additional tests on negative BN conversions. */
  45540. char* ret = NULL;
  45541. ASN1_INTEGER* asn1 = NULL;
  45542. BIGNUM* tmp = NULL;
  45543. /* Sanity check we have a negative BN. */
  45544. ExpectIntEQ(BN_is_negative(c), 1);
  45545. ExpectNotNull(ret = BN_bn2dec(c));
  45546. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  45547. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  45548. ret = NULL;
  45549. /* Convert to ASN1_INTEGER and back to BN. */
  45550. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  45551. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  45552. /* After converting back BN should be negative and correct. */
  45553. ExpectIntEQ(BN_is_negative(tmp), 1);
  45554. ExpectNotNull(ret = BN_bn2dec(tmp));
  45555. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  45556. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  45557. ASN1_INTEGER_free(asn1);
  45558. BN_free(tmp);
  45559. }
  45560. #endif
  45561. ExpectIntEQ(BN_get_word(c), 4);
  45562. #endif
  45563. ExpectIntEQ(BN_set_word(a, 3), 1);
  45564. ExpectIntEQ(BN_set_word(b, 3), 1);
  45565. ExpectIntEQ(BN_set_word(c, 4), 1);
  45566. /* NULL == NULL, NULL < num, num > NULL */
  45567. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  45568. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  45569. ExpectIntLT(BN_cmp(NULL, b), 0);
  45570. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  45571. ExpectIntGT(BN_cmp(a, NULL), 0);
  45572. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  45573. ExpectIntEQ(BN_cmp(a, b), 0);
  45574. ExpectIntLT(BN_cmp(a, c), 0);
  45575. ExpectIntGT(BN_cmp(c, b), 0);
  45576. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  45577. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  45578. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  45579. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  45580. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  45581. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  45582. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  45583. #endif
  45584. BN_clear(a);
  45585. BN_free(a);
  45586. BN_free(b);
  45587. BN_free(c);
  45588. BN_clear_free(d);
  45589. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  45590. return EXPECT_RESULT();
  45591. }
  45592. static int test_wolfSSL_BN_init(void)
  45593. {
  45594. EXPECT_DECLS;
  45595. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  45596. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  45597. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  45598. BIGNUM* ap = NULL;
  45599. BIGNUM bv;
  45600. BIGNUM cv;
  45601. BIGNUM dv;
  45602. ExpectNotNull(ap = BN_new());
  45603. BN_init(NULL);
  45604. XMEMSET(&bv, 0, sizeof(bv));
  45605. ExpectNull(BN_dup(&bv));
  45606. BN_init(&bv);
  45607. BN_init(&cv);
  45608. BN_init(&dv);
  45609. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  45610. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  45611. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  45612. /* a^b mod c = */
  45613. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  45614. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  45615. /* check result 3^2 mod 5 */
  45616. ExpectIntEQ(BN_get_word(&dv), 4);
  45617. /* a*b mod c = */
  45618. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  45619. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  45620. /* check result 3*2 mod 5 */
  45621. ExpectIntEQ(BN_get_word(&dv), 1);
  45622. BN_free(ap);
  45623. #endif
  45624. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  45625. return EXPECT_RESULT();
  45626. }
  45627. static int test_wolfSSL_BN_enc_dec(void)
  45628. {
  45629. EXPECT_DECLS;
  45630. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  45631. BIGNUM* a = NULL;
  45632. BIGNUM* b = NULL;
  45633. BIGNUM* c = NULL;
  45634. BIGNUM emptyBN;
  45635. char* str = NULL;
  45636. const char* emptyStr = "";
  45637. const char* numberStr = "12345";
  45638. const char* badStr = "g12345";
  45639. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  45640. const char* twoStr = "2";
  45641. #endif
  45642. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  45643. unsigned char outNum[5];
  45644. /* Setup */
  45645. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  45646. ExpectNotNull(a = BN_new());
  45647. ExpectNotNull(b = BN_new());
  45648. ExpectIntEQ(BN_set_word(a, 2), 1);
  45649. /* Invalid parameters */
  45650. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  45651. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  45652. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  45653. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  45654. ExpectNull(BN_bn2hex(NULL));
  45655. ExpectNull(BN_bn2hex(&emptyBN));
  45656. ExpectNull(BN_bn2dec(NULL));
  45657. ExpectNull(BN_bn2dec(&emptyBN));
  45658. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  45659. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  45660. ExpectNull(BN_bin2bn(binNum, -1, a));
  45661. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  45662. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  45663. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  45664. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  45665. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  45666. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  45667. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  45668. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  45669. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  45670. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  45671. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  45672. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  45673. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  45674. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  45675. ExpectIntEQ(BN_set_word(a, 2), 1);
  45676. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  45677. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  45678. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  45679. ExpectIntEQ(BN_cmp(a, b), 0);
  45680. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  45681. ExpectIntEQ(BN_cmp(a, b), -1);
  45682. ExpectNotNull(str = BN_bn2hex(a));
  45683. ExpectNotNull(BN_hex2bn(&b, str));
  45684. ExpectIntEQ(BN_cmp(a, b), 0);
  45685. ExpectNotNull(BN_hex2bn(&b, numberStr));
  45686. ExpectIntEQ(BN_cmp(a, b), -1);
  45687. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  45688. str = NULL;
  45689. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  45690. ExpectNotNull(str = BN_bn2dec(a));
  45691. ExpectStrEQ(str, twoStr);
  45692. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  45693. str = NULL;
  45694. #ifndef NO_RSA
  45695. ExpectNotNull(str = BN_bn2dec(a));
  45696. ExpectNotNull(BN_dec2bn(&b, str));
  45697. ExpectIntEQ(BN_cmp(a, b), 0);
  45698. ExpectNotNull(BN_dec2bn(&b, numberStr));
  45699. ExpectIntEQ(BN_cmp(a, b), -1);
  45700. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  45701. str = NULL;
  45702. #else
  45703. /* No implementation - fail with good parameters. */
  45704. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  45705. #endif
  45706. #endif
  45707. BN_free(b);
  45708. BN_free(a);
  45709. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  45710. return EXPECT_RESULT();
  45711. }
  45712. static int test_wolfSSL_BN_word(void)
  45713. {
  45714. EXPECT_DECLS;
  45715. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  45716. BIGNUM* a = NULL;
  45717. BIGNUM* b = NULL;
  45718. BIGNUM* c = NULL;
  45719. BIGNUM av;
  45720. ExpectNotNull(a = BN_new());
  45721. ExpectNotNull(b = BN_new());
  45722. ExpectNotNull(c = BN_new());
  45723. XMEMSET(&av, 0, sizeof(av));
  45724. /* Invalid parameter. */
  45725. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  45726. ExpectIntEQ(BN_add_word(&av, 3), 0);
  45727. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  45728. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  45729. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  45730. ExpectIntEQ(BN_set_word(&av, 3), 0);
  45731. ExpectIntEQ(BN_get_word(NULL), 0);
  45732. ExpectIntEQ(BN_get_word(&av), 0);
  45733. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  45734. ExpectIntEQ(BN_is_word(&av, 3), 0);
  45735. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  45736. !defined(NO_DSA))
  45737. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  45738. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  45739. #endif
  45740. ExpectIntEQ(BN_one(NULL), 0);
  45741. ExpectIntEQ(BN_one(&av), 0);
  45742. BN_zero(NULL);
  45743. BN_zero(&av);
  45744. ExpectIntEQ(BN_is_one(NULL), 0);
  45745. ExpectIntEQ(BN_is_one(&av), 0);
  45746. ExpectIntEQ(BN_is_zero(NULL), 0);
  45747. ExpectIntEQ(BN_is_zero(&av), 0);
  45748. ExpectIntEQ(BN_set_word(a, 3), 1);
  45749. ExpectIntEQ(BN_set_word(b, 2), 1);
  45750. ExpectIntEQ(BN_set_word(c, 5), 1);
  45751. /* a + 3 = */
  45752. ExpectIntEQ(BN_add_word(a, 3), 1);
  45753. /* check result 3 + 3*/
  45754. ExpectIntEQ(BN_get_word(a), 6);
  45755. ExpectIntEQ(BN_is_word(a, 6), 1);
  45756. ExpectIntEQ(BN_is_word(a, 5), 0);
  45757. /* set a back to 3 */
  45758. ExpectIntEQ(BN_set_word(a, 3), 1);
  45759. /* a - 3 = */
  45760. ExpectIntEQ(BN_sub_word(a, 3), 1);
  45761. /* check result 3 - 3*/
  45762. ExpectIntEQ(BN_get_word(a), 0);
  45763. ExpectIntEQ(BN_one(a), 1);
  45764. ExpectIntEQ(BN_is_word(a, 1), 1);
  45765. ExpectIntEQ(BN_is_word(a, 0), 0);
  45766. ExpectIntEQ(BN_is_one(a), 1);
  45767. ExpectIntEQ(BN_is_zero(a), 0);
  45768. BN_zero(a);
  45769. ExpectIntEQ(BN_is_word(a, 0), 1);
  45770. ExpectIntEQ(BN_is_word(a, 1), 0);
  45771. ExpectIntEQ(BN_is_zero(a), 1);
  45772. ExpectIntEQ(BN_is_one(a), 0);
  45773. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  45774. !defined(NO_DSA))
  45775. ExpectIntEQ(BN_set_word(a, 5), 1);
  45776. ExpectIntEQ(BN_mod_word(a, 3), 2);
  45777. ExpectIntEQ(BN_mod_word(a, 0), -1);
  45778. #endif
  45779. BN_free(c);
  45780. BN_free(b);
  45781. BN_free(a);
  45782. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  45783. return EXPECT_RESULT();
  45784. }
  45785. static int test_wolfSSL_BN_bits(void)
  45786. {
  45787. EXPECT_DECLS;
  45788. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  45789. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  45790. BIGNUM* a = NULL;
  45791. BIGNUM emptyBN;
  45792. /* Setup */
  45793. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  45794. ExpectNotNull(a = BN_new());
  45795. /* Invalid parameters. */
  45796. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  45797. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  45798. ExpectIntEQ(BN_set_bit(a, -1), 0);
  45799. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  45800. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  45801. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  45802. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  45803. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  45804. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  45805. ExpectIntEQ(BN_is_odd(NULL), 0);
  45806. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  45807. ExpectIntEQ(BN_set_word(a, 0), 1);
  45808. ExpectIntEQ(BN_is_zero(a), 1);
  45809. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  45810. ExpectIntEQ(BN_is_zero(a), 0);
  45811. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  45812. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  45813. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  45814. ExpectIntEQ(BN_is_zero(a), 1);
  45815. ExpectIntEQ(BN_set_bit(a, 0), 1);
  45816. ExpectIntEQ(BN_is_odd(a), 1);
  45817. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  45818. ExpectIntEQ(BN_is_odd(a), 0);
  45819. ExpectIntEQ(BN_set_bit(a, 1), 1);
  45820. ExpectIntEQ(BN_is_odd(a), 0);
  45821. ExpectIntEQ(BN_set_bit(a, 129), 1);
  45822. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  45823. #ifndef NO_WOLFSSL_STUB
  45824. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  45825. #endif
  45826. BN_free(a);
  45827. #endif
  45828. return EXPECT_RESULT();
  45829. }
  45830. static int test_wolfSSL_BN_shift(void)
  45831. {
  45832. EXPECT_DECLS;
  45833. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  45834. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  45835. BIGNUM* a = NULL;
  45836. BIGNUM* b = NULL;
  45837. BIGNUM emptyBN;
  45838. /* Setup */
  45839. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  45840. ExpectNotNull(a = BN_new());
  45841. ExpectNotNull(b = BN_new());
  45842. /* Invalid parameters. */
  45843. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  45844. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  45845. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  45846. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  45847. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  45848. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  45849. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  45850. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  45851. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  45852. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  45853. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  45854. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  45855. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  45856. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  45857. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  45858. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  45859. ExpectIntEQ(BN_set_word(a, 1), 1);
  45860. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  45861. ExpectIntEQ(BN_is_word(b, 2), 1);
  45862. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  45863. ExpectIntEQ(BN_is_word(a, 2), 1);
  45864. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  45865. ExpectIntEQ(BN_is_word(b, 1), 1);
  45866. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  45867. ExpectIntEQ(BN_is_word(a, 1), 1);
  45868. BN_free(b);
  45869. BN_free(a);
  45870. #endif
  45871. return EXPECT_RESULT();
  45872. }
  45873. static int test_wolfSSL_BN_math(void)
  45874. {
  45875. EXPECT_DECLS;
  45876. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  45877. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  45878. BIGNUM* a = NULL;
  45879. BIGNUM* b = NULL;
  45880. BIGNUM* r = NULL;
  45881. BIGNUM* rem = NULL;
  45882. BIGNUM emptyBN;
  45883. BN_ULONG val1;
  45884. BN_ULONG val2;
  45885. /* Setup */
  45886. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  45887. ExpectNotNull(a = BN_new());
  45888. ExpectNotNull(b = BN_new());
  45889. ExpectNotNull(r = BN_new());
  45890. ExpectNotNull(rem = BN_new());
  45891. /* Invalid parameters. */
  45892. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  45893. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  45894. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  45895. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  45896. ExpectIntEQ(BN_add(r, a, NULL), 0);
  45897. ExpectIntEQ(BN_add(r, NULL, b), 0);
  45898. ExpectIntEQ(BN_add(NULL, a, b), 0);
  45899. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  45900. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  45901. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  45902. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  45903. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  45904. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  45905. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  45906. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  45907. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  45908. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  45909. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  45910. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  45911. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  45912. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  45913. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  45914. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  45915. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  45916. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  45917. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  45918. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  45919. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  45920. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  45921. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  45922. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  45923. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  45924. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  45925. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  45926. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  45927. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  45928. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  45929. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  45930. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  45931. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  45932. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  45933. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  45934. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  45935. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  45936. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  45937. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  45938. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  45939. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  45940. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  45941. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  45942. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  45943. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  45944. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  45945. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  45946. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  45947. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  45948. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  45949. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  45950. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  45951. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  45952. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  45953. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  45954. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  45955. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  45956. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  45957. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  45958. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  45959. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  45960. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  45961. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  45962. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  45963. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  45964. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  45965. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  45966. /* END Invalid parameters. */
  45967. val1 = 8;
  45968. val2 = 3;
  45969. ExpectIntEQ(BN_set_word(a, val1), 1);
  45970. ExpectIntEQ(BN_set_word(b, val2), 1);
  45971. ExpectIntEQ(BN_add(r, a, b), 1);
  45972. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  45973. ExpectIntEQ(BN_sub(r, a, b), 1);
  45974. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  45975. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  45976. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  45977. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  45978. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  45979. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  45980. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  45981. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  45982. BN_free(rem);
  45983. BN_free(r);
  45984. BN_free(b);
  45985. BN_free(a);
  45986. #endif
  45987. return EXPECT_RESULT();
  45988. }
  45989. static int test_wolfSSL_BN_math_mod(void)
  45990. {
  45991. EXPECT_DECLS;
  45992. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  45993. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  45994. BIGNUM* a = NULL;
  45995. BIGNUM* b = NULL;
  45996. BIGNUM* m = NULL;
  45997. BIGNUM* r = NULL;
  45998. BIGNUM* t = NULL;
  45999. BIGNUM emptyBN;
  46000. BN_ULONG val1;
  46001. BN_ULONG val2;
  46002. BN_ULONG val3;
  46003. /* Setup */
  46004. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  46005. ExpectNotNull(a = BN_new());
  46006. ExpectNotNull(b = BN_new());
  46007. ExpectNotNull(m = BN_new());
  46008. ExpectNotNull(r = BN_new());
  46009. /* Invalid parameters. */
  46010. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  46011. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  46012. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  46013. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  46014. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  46015. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  46016. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  46017. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  46018. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  46019. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  46020. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  46021. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  46022. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  46023. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  46024. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  46025. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  46026. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  46027. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  46028. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  46029. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  46030. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  46031. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  46032. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  46033. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  46034. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  46035. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  46036. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  46037. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  46038. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  46039. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  46040. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  46041. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  46042. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  46043. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  46044. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  46045. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  46046. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  46047. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  46048. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  46049. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  46050. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  46051. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  46052. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  46053. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  46054. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  46055. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  46056. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  46057. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  46058. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  46059. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  46060. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  46061. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  46062. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  46063. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  46064. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  46065. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  46066. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  46067. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  46068. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  46069. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  46070. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  46071. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  46072. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  46073. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  46074. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  46075. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  46076. /* END Invalid parameters. */
  46077. val1 = 9;
  46078. val2 = 13;
  46079. val3 = 5;
  46080. ExpectIntEQ(BN_set_word(a, val1), 1);
  46081. ExpectIntEQ(BN_set_word(b, val2), 1);
  46082. ExpectIntEQ(BN_set_word(m, val3), 1);
  46083. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  46084. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  46085. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  46086. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  46087. ExpectIntEQ(BN_set_word(a, 2), 1);
  46088. ExpectIntEQ(BN_set_word(b, 3), 1);
  46089. ExpectIntEQ(BN_set_word(m, 5), 1);
  46090. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  46091. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  46092. ExpectIntEQ(BN_is_word(r, 3), 1);
  46093. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  46094. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  46095. ExpectIntEQ(BN_is_word(r, 3), 1);
  46096. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  46097. ExpectIntEQ(BN_is_word(t, 3), 1);
  46098. BN_free(t);
  46099. /* No inverse case. No inverse when a divides b. */
  46100. ExpectIntEQ(BN_set_word(a, 3), 1);
  46101. ExpectIntEQ(BN_set_word(m, 9), 1);
  46102. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  46103. BN_free(r);
  46104. BN_free(m);
  46105. BN_free(b);
  46106. BN_free(a);
  46107. #endif
  46108. return EXPECT_RESULT();
  46109. }
  46110. static int test_wolfSSL_BN_math_other(void)
  46111. {
  46112. EXPECT_DECLS;
  46113. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  46114. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  46115. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  46116. BIGNUM* a = NULL;
  46117. BIGNUM* b = NULL;
  46118. BIGNUM* r = NULL;
  46119. BIGNUM emptyBN;
  46120. /* Setup */
  46121. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  46122. ExpectNotNull(a = BN_new());
  46123. ExpectNotNull(b = BN_new());
  46124. ExpectNotNull(r = BN_new());
  46125. /* Invalid parameters. */
  46126. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  46127. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  46128. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  46129. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  46130. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  46131. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  46132. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  46133. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  46134. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  46135. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  46136. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  46137. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  46138. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  46139. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  46140. /* END Invalid parameters. */
  46141. /* No common factors between 2 and 3. */
  46142. ExpectIntEQ(BN_set_word(a, 2), 1);
  46143. ExpectIntEQ(BN_set_word(b, 3), 1);
  46144. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  46145. ExpectIntEQ(BN_is_word(r, 1), 1);
  46146. /* 3 is largest value that divides both 6 and 9. */
  46147. ExpectIntEQ(BN_set_word(a, 6), 1);
  46148. ExpectIntEQ(BN_set_word(b, 9), 1);
  46149. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  46150. ExpectIntEQ(BN_is_word(r, 3), 1);
  46151. /* GCD of 0 and 0 is undefined. */
  46152. ExpectIntEQ(BN_set_word(a, 0), 1);
  46153. ExpectIntEQ(BN_set_word(b, 0), 1);
  46154. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  46155. /* Teardown */
  46156. BN_free(r);
  46157. BN_free(b);
  46158. BN_free(a);
  46159. #endif
  46160. #endif
  46161. return EXPECT_RESULT();
  46162. }
  46163. static int test_wolfSSL_BN_rand(void)
  46164. {
  46165. EXPECT_DECLS;
  46166. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  46167. BIGNUM* bn = NULL;
  46168. BIGNUM* range = NULL;
  46169. BIGNUM emptyBN;
  46170. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  46171. ExpectNotNull(bn = BN_new());
  46172. ExpectNotNull(range = BN_new());
  46173. /* Invalid parameters. */
  46174. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  46175. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  46176. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  46177. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  46178. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  46179. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  46180. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  46181. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  46182. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  46183. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  46184. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  46185. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  46186. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  46187. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  46188. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  46189. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  46190. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  46191. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  46192. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  46193. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  46194. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  46195. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  46196. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  46197. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  46198. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  46199. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  46200. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  46201. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  46202. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  46203. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  46204. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  46205. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  46206. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  46207. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  46208. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  46209. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  46210. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  46211. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  46212. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  46213. /* 1 bit random value must have no more than one top bit set. */
  46214. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  46215. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  46216. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  46217. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  46218. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  46219. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  46220. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  46221. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  46222. /* END Invalid parameters. */
  46223. /* 0 bit random: 0. */
  46224. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  46225. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46226. ExpectIntEQ(BN_is_zero(bn), 1);
  46227. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  46228. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  46229. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46230. ExpectIntEQ(BN_is_zero(bn), 1);
  46231. /* 1 bit random: 0 or 1. */
  46232. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  46233. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46234. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  46235. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  46236. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46237. ExpectIntEQ(BN_get_word(bn), 1);
  46238. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  46239. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  46240. ExpectIntEQ(BN_get_word(bn), 1);
  46241. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  46242. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46243. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  46244. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  46245. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46246. ExpectIntEQ(BN_get_word(bn), 1);
  46247. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  46248. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  46249. ExpectIntEQ(BN_get_word(bn), 1);
  46250. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  46251. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46252. ExpectIntEQ(BN_num_bits(bn), 8);
  46253. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  46254. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  46255. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46256. ExpectIntEQ(BN_num_bits(bn), 8);
  46257. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  46258. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  46259. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46260. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  46261. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  46262. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  46263. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46264. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  46265. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  46266. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  46267. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  46268. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  46269. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  46270. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  46271. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  46272. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  46273. * requested number of bits up to the nearest multiple of 8. E.g. in this
  46274. * case, requesting a 13-bit random number would actually return a 16-bit
  46275. * random number. */
  46276. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  46277. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46278. ExpectIntEQ(BN_num_bits(bn), 13);
  46279. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  46280. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  46281. ExpectIntEQ(BN_rand_range(bn, range), 1);
  46282. ExpectIntEQ(BN_set_word(range, 0), 1);
  46283. ExpectIntEQ(BN_rand_range(bn, range), 1);
  46284. ExpectIntEQ(BN_set_word(range, 1), 1);
  46285. ExpectIntEQ(BN_rand_range(bn, range), 1);
  46286. BN_free(bn);
  46287. BN_free(range);
  46288. #endif
  46289. return EXPECT_RESULT();
  46290. }
  46291. static int test_wolfSSL_BN_prime(void)
  46292. {
  46293. EXPECT_DECLS;
  46294. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  46295. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  46296. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  46297. BIGNUM* a = NULL;
  46298. BIGNUM* add = NULL;
  46299. BIGNUM* rem = NULL;
  46300. BIGNUM emptyBN;
  46301. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  46302. ExpectNotNull(a = BN_new());
  46303. ExpectNotNull(add = BN_new());
  46304. ExpectNotNull(rem = BN_new());
  46305. /* Invalid parameters. */
  46306. /* BN_generate_prime_ex()
  46307. * prime - must have valid BIGNUM
  46308. * bits - Greater then 0
  46309. * safe - not supported, must be 0
  46310. * add - not supported, must be NULL
  46311. * rem - not supported, must be NULL
  46312. * cb - anything
  46313. */
  46314. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  46315. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  46316. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  46317. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  46318. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  46319. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  46320. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  46321. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  46322. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  46323. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  46324. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  46325. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  46326. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  46327. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  46328. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  46329. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  46330. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  46331. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  46332. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  46333. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  46334. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  46335. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  46336. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  46337. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  46338. /* END Invalid parameters. */
  46339. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  46340. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  46341. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  46342. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  46343. BN_free(rem);
  46344. BN_free(add);
  46345. BN_free(a);
  46346. #endif
  46347. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  46348. return EXPECT_RESULT();
  46349. }
  46350. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  46351. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46352. #define TEST_ARG 0x1234
  46353. static void msg_cb(int write_p, int version, int content_type,
  46354. const void *buf, size_t len, SSL *ssl, void *arg)
  46355. {
  46356. (void)write_p;
  46357. (void)version;
  46358. (void)content_type;
  46359. (void)buf;
  46360. (void)len;
  46361. (void)ssl;
  46362. AssertTrue(arg == (void*)TEST_ARG);
  46363. }
  46364. #endif
  46365. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  46366. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  46367. #if defined(SESSION_CERTS)
  46368. #include "wolfssl/internal.h"
  46369. #endif
  46370. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  46371. {
  46372. EXPECT_DECLS;
  46373. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  46374. STACK_OF(X509)* sk = NULL;
  46375. X509* x509 = NULL;
  46376. int i, num;
  46377. BIO* bio = NULL;
  46378. #endif
  46379. ExpectNotNull(ctx);
  46380. ExpectNotNull(ssl);
  46381. fprintf(stderr, "\n===== msgcb called ====\n");
  46382. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  46383. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  46384. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  46385. ExpectNotNull(SSL_get0_verified_chain(ssl));
  46386. #endif
  46387. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  46388. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  46389. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  46390. if (sk == NULL) {
  46391. BIO_free(bio);
  46392. return TEST_FAIL;
  46393. }
  46394. num = sk_X509_num(sk);
  46395. ExpectTrue(num > 0);
  46396. for (i = 0; i < num; i++) {
  46397. ExpectNotNull(x509 = sk_X509_value(sk,i));
  46398. if (x509 == NULL)
  46399. break;
  46400. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  46401. X509_print(bio,x509);
  46402. fprintf(stderr, "\n\n");
  46403. }
  46404. BIO_free(bio);
  46405. #endif
  46406. return EXPECT_RESULT();
  46407. }
  46408. #endif
  46409. static int test_wolfSSL_msgCb(void)
  46410. {
  46411. EXPECT_DECLS;
  46412. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  46413. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  46414. test_ssl_cbf client_cb;
  46415. test_ssl_cbf server_cb;
  46416. XMEMSET(&client_cb, 0, sizeof(client_cb));
  46417. XMEMSET(&server_cb, 0, sizeof(server_cb));
  46418. #ifndef WOLFSSL_NO_TLS12
  46419. client_cb.method = wolfTLSv1_2_client_method;
  46420. server_cb.method = wolfTLSv1_2_server_method;
  46421. #else
  46422. client_cb.method = wolfTLSv1_3_client_method;
  46423. server_cb.method = wolfTLSv1_3_server_method;
  46424. #endif
  46425. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  46426. &server_cb, msgCb), TEST_SUCCESS);
  46427. #endif
  46428. return EXPECT_RESULT();
  46429. }
  46430. static int test_wolfSSL_either_side(void)
  46431. {
  46432. EXPECT_DECLS;
  46433. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  46434. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  46435. test_ssl_cbf client_cb;
  46436. test_ssl_cbf server_cb;
  46437. XMEMSET(&client_cb, 0, sizeof(client_cb));
  46438. XMEMSET(&server_cb, 0, sizeof(server_cb));
  46439. /* Use different CTX for client and server */
  46440. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  46441. ExpectNotNull(client_cb.ctx);
  46442. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  46443. ExpectNotNull(server_cb.ctx);
  46444. /* we are responsible for free'ing WOLFSSL_CTX */
  46445. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  46446. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  46447. &server_cb, NULL), TEST_SUCCESS);
  46448. wolfSSL_CTX_free(client_cb.ctx);
  46449. wolfSSL_CTX_free(server_cb.ctx);
  46450. #endif
  46451. return EXPECT_RESULT();
  46452. }
  46453. static int test_wolfSSL_DTLS_either_side(void)
  46454. {
  46455. EXPECT_DECLS;
  46456. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  46457. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  46458. test_ssl_cbf client_cb;
  46459. test_ssl_cbf server_cb;
  46460. XMEMSET(&client_cb, 0, sizeof(client_cb));
  46461. XMEMSET(&server_cb, 0, sizeof(server_cb));
  46462. /* Use different CTX for client and server */
  46463. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  46464. ExpectNotNull(client_cb.ctx);
  46465. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  46466. ExpectNotNull(server_cb.ctx);
  46467. /* we are responsible for free'ing WOLFSSL_CTX */
  46468. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  46469. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  46470. &server_cb, NULL), TEST_SUCCESS);
  46471. wolfSSL_CTX_free(client_cb.ctx);
  46472. wolfSSL_CTX_free(server_cb.ctx);
  46473. #endif
  46474. return EXPECT_RESULT();
  46475. }
  46476. static int test_generate_cookie(void)
  46477. {
  46478. EXPECT_DECLS;
  46479. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  46480. SSL_CTX* ctx = NULL;
  46481. SSL* ssl = NULL;
  46482. byte buf[FOURK_BUF] = {0};
  46483. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  46484. ExpectNotNull(ssl = SSL_new(ctx));
  46485. /* Test unconnected */
  46486. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  46487. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  46488. wolfSSL_SetCookieCtx(ssl, ctx);
  46489. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  46490. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  46491. SSL_free(ssl);
  46492. SSL_CTX_free(ctx);
  46493. #endif
  46494. return EXPECT_RESULT();
  46495. }
  46496. static int test_wolfSSL_set_options(void)
  46497. {
  46498. EXPECT_DECLS;
  46499. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46500. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  46501. WOLFSSL* ssl = NULL;
  46502. WOLFSSL_CTX* ctx = NULL;
  46503. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  46504. char appData[] = "extra msg";
  46505. #endif
  46506. #ifdef OPENSSL_EXTRA
  46507. unsigned char protos[] = {
  46508. 7, 't', 'l', 's', '/', '1', '.', '2',
  46509. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  46510. };
  46511. unsigned int len = sizeof(protos);
  46512. void *arg = (void *)TEST_ARG;
  46513. #endif
  46514. #ifndef NO_WOLFSSL_SERVER
  46515. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46516. #else
  46517. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46518. #endif
  46519. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  46520. WOLFSSL_FILETYPE_PEM));
  46521. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  46522. WOLFSSL_FILETYPE_PEM));
  46523. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  46524. == WOLFSSL_OP_NO_TLSv1);
  46525. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  46526. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  46527. WOLFSSL_OP_NO_SSLv2)), 0);
  46528. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  46529. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  46530. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  46531. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  46532. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  46533. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  46534. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  46535. WOLFSSL_OP_NO_COMPRESSION));
  46536. wolfSSL_CTX_free(ctx);
  46537. ctx = NULL;
  46538. #ifndef NO_WOLFSSL_SERVER
  46539. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46540. #else
  46541. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46542. #endif
  46543. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  46544. WOLFSSL_FILETYPE_PEM));
  46545. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  46546. WOLFSSL_FILETYPE_PEM));
  46547. #ifdef OPENSSL_EXTRA
  46548. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  46549. #endif
  46550. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46551. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  46552. #ifdef HAVE_EX_DATA
  46553. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  46554. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  46555. if (ssl != NULL) {
  46556. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  46557. appData, sizeof(appData)), 0);
  46558. }
  46559. #else
  46560. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  46561. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  46562. #endif
  46563. #endif
  46564. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  46565. WOLFSSL_OP_NO_TLSv1);
  46566. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  46567. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  46568. WOLFSSL_OP_NO_SSLv2)), 0);
  46569. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  46570. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  46571. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  46572. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  46573. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  46574. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  46575. #ifdef OPENSSL_EXTRA
  46576. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  46577. WOLFSSL_OP_NO_COMPRESSION));
  46578. #endif
  46579. #ifdef OPENSSL_EXTRA
  46580. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  46581. wolfSSL_set_msg_callback_arg(ssl, arg);
  46582. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46583. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  46584. #else
  46585. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  46586. #endif
  46587. #endif
  46588. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  46589. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  46590. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  46591. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  46592. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46593. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  46594. #else
  46595. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  46596. #endif
  46597. #endif /* HAVE_ALPN && !NO_BIO */
  46598. #endif
  46599. wolfSSL_free(ssl);
  46600. wolfSSL_CTX_free(ctx);
  46601. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  46602. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  46603. return EXPECT_RESULT();
  46604. }
  46605. static int test_wolfSSL_sk_SSL_CIPHER(void)
  46606. {
  46607. EXPECT_DECLS;
  46608. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  46609. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46610. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  46611. SSL* ssl = NULL;
  46612. SSL_CTX* ctx = NULL;
  46613. STACK_OF(SSL_CIPHER) *sk = NULL;
  46614. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  46615. #ifndef NO_WOLFSSL_SERVER
  46616. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  46617. #else
  46618. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  46619. #endif
  46620. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  46621. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  46622. ExpectNotNull(ssl = SSL_new(ctx));
  46623. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  46624. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  46625. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  46626. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  46627. /* error case because connection has not been established yet */
  46628. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  46629. sk_SSL_CIPHER_free(dupSk);
  46630. /* sk is pointer to internal struct that should be free'd in SSL_free */
  46631. SSL_free(ssl);
  46632. SSL_CTX_free(ctx);
  46633. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  46634. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  46635. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  46636. return EXPECT_RESULT();
  46637. }
  46638. static int test_wolfSSL_set1_curves_list(void)
  46639. {
  46640. EXPECT_DECLS;
  46641. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46642. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  46643. SSL* ssl = NULL;
  46644. SSL_CTX* ctx = NULL;
  46645. #ifndef NO_WOLFSSL_SERVER
  46646. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  46647. #else
  46648. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  46649. #endif
  46650. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  46651. SSL_FILETYPE_PEM));
  46652. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  46653. ExpectNotNull(ssl = SSL_new(ctx));
  46654. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  46655. #ifdef HAVE_ECC
  46656. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  46657. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  46658. #endif
  46659. #ifdef HAVE_CURVE25519
  46660. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  46661. #else
  46662. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  46663. #endif
  46664. #ifdef HAVE_CURVE448
  46665. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  46666. #else
  46667. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  46668. #endif
  46669. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  46670. #ifdef HAVE_ECC
  46671. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  46672. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  46673. #endif
  46674. #ifdef HAVE_CURVE25519
  46675. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  46676. #else
  46677. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  46678. #endif
  46679. #ifdef HAVE_CURVE448
  46680. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  46681. #else
  46682. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  46683. #endif
  46684. SSL_free(ssl);
  46685. SSL_CTX_free(ctx);
  46686. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  46687. #endif
  46688. return EXPECT_RESULT();
  46689. }
  46690. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  46691. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  46692. static int test_wolfSSL_curves_mismatch_ctx_ready(WOLFSSL_CTX* ctx)
  46693. {
  46694. static int counter = 0;
  46695. EXPECT_DECLS;
  46696. if (counter % 2) {
  46697. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-256"),
  46698. WOLFSSL_SUCCESS);
  46699. }
  46700. else {
  46701. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-384"),
  46702. WOLFSSL_SUCCESS);
  46703. }
  46704. /* Ciphersuites that require curves */
  46705. wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384:"
  46706. "TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-GCM-SHA256:"
  46707. "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:"
  46708. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  46709. "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-CHACHA20-POLY1305:"
  46710. "ECDHE-ECDSA-CHACHA20-POLY1305");
  46711. counter++;
  46712. return EXPECT_RESULT();
  46713. }
  46714. #endif
  46715. static int test_wolfSSL_curves_mismatch(void)
  46716. {
  46717. EXPECT_DECLS;
  46718. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  46719. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  46720. test_ssl_cbf func_cb_client;
  46721. test_ssl_cbf func_cb_server;
  46722. size_t i;
  46723. struct {
  46724. method_provider client_meth;
  46725. method_provider server_meth;
  46726. const char* desc;
  46727. int client_last_err;
  46728. int server_last_err;
  46729. } test_params[] = {
  46730. #ifdef WOLFSSL_TLS13
  46731. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3",
  46732. FATAL_ERROR, BAD_KEY_SHARE_DATA},
  46733. #endif
  46734. #ifndef WOLFSSL_NO_TLS12
  46735. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2",
  46736. FATAL_ERROR, MATCH_SUITE_ERROR},
  46737. #endif
  46738. #ifndef NO_OLD_TLS
  46739. {wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLS 1.1",
  46740. FATAL_ERROR, MATCH_SUITE_ERROR},
  46741. #endif
  46742. };
  46743. for (i = 0; i < XELEM_CNT(test_params) && !EXPECT_FAIL(); i++) {
  46744. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  46745. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  46746. printf("\tTesting with %s...\n", test_params[i].desc);
  46747. func_cb_client.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  46748. func_cb_server.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  46749. func_cb_client.method = test_params[i].client_meth;
  46750. func_cb_server.method = test_params[i].server_meth;
  46751. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  46752. &func_cb_server, NULL), TEST_FAIL);
  46753. ExpectIntEQ(func_cb_client.last_err, test_params[i].client_last_err);
  46754. ExpectIntEQ(func_cb_server.last_err, test_params[i].server_last_err);
  46755. if (!EXPECT_SUCCESS())
  46756. break;
  46757. printf("\t%s passed\n", test_params[i].desc);
  46758. }
  46759. #endif
  46760. return EXPECT_RESULT();
  46761. }
  46762. static int test_wolfSSL_set1_sigalgs_list(void)
  46763. {
  46764. EXPECT_DECLS;
  46765. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  46766. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  46767. SSL* ssl = NULL;
  46768. SSL_CTX* ctx = NULL;
  46769. #ifndef NO_WOLFSSL_SERVER
  46770. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  46771. #else
  46772. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  46773. #endif
  46774. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  46775. SSL_FILETYPE_PEM));
  46776. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  46777. ExpectNotNull(ssl = SSL_new(ctx));
  46778. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  46779. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  46780. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  46781. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  46782. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  46783. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  46784. #ifndef NO_RSA
  46785. #ifndef NO_SHA256
  46786. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  46787. WOLFSSL_FAILURE);
  46788. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  46789. WOLFSSL_FAILURE);
  46790. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  46791. WOLFSSL_SUCCESS);
  46792. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  46793. WOLFSSL_SUCCESS);
  46794. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  46795. WOLFSSL_FAILURE);
  46796. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  46797. WOLFSSL_FAILURE);
  46798. #ifdef WC_RSA_PSS
  46799. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  46800. WOLFSSL_SUCCESS);
  46801. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  46802. WOLFSSL_SUCCESS);
  46803. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  46804. WOLFSSL_SUCCESS);
  46805. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  46806. WOLFSSL_SUCCESS);
  46807. #endif
  46808. #ifdef WOLFSSL_SHA512
  46809. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  46810. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  46811. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  46812. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  46813. #elif defined(WOLFSSL_SHA384)
  46814. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  46815. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  46816. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  46817. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  46818. #endif
  46819. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  46820. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  46821. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  46822. WOLFSSL_FAILURE);
  46823. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  46824. WOLFSSL_FAILURE);
  46825. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  46826. WOLFSSL_FAILURE);
  46827. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  46828. WOLFSSL_FAILURE);
  46829. #endif
  46830. #endif
  46831. #ifdef HAVE_ECC
  46832. #ifndef NO_SHA256
  46833. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  46834. WOLFSSL_SUCCESS);
  46835. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  46836. WOLFSSL_SUCCESS);
  46837. #ifdef WOLFSSL_SHA512
  46838. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  46839. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  46840. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  46841. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  46842. #elif defined(WOLFSSL_SHA384)
  46843. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  46844. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  46845. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  46846. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  46847. #endif
  46848. #endif
  46849. #endif
  46850. #ifdef HAVE_ED25519
  46851. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  46852. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  46853. #endif
  46854. #ifdef HAVE_ED448
  46855. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  46856. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  46857. #endif
  46858. #ifndef NO_DSA
  46859. #ifndef NO_SHA256
  46860. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  46861. WOLFSSL_SUCCESS);
  46862. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  46863. WOLFSSL_SUCCESS);
  46864. #endif
  46865. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  46866. defined(WOLFSSL_ALLOW_TLS_SHA1))
  46867. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  46868. WOLFSSL_SUCCESS);
  46869. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  46870. WOLFSSL_SUCCESS);
  46871. #endif
  46872. #endif
  46873. SSL_free(ssl);
  46874. SSL_CTX_free(ctx);
  46875. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  46876. #endif
  46877. return EXPECT_RESULT();
  46878. }
  46879. /* Testing wolfSSL_set_tlsext_status_type function.
  46880. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  46881. */
  46882. static int test_wolfSSL_set_tlsext_status_type(void)
  46883. {
  46884. EXPECT_DECLS;
  46885. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  46886. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  46887. SSL* ssl = NULL;
  46888. SSL_CTX* ctx = NULL;
  46889. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  46890. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  46891. SSL_FILETYPE_PEM));
  46892. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  46893. ExpectNotNull(ssl = SSL_new(ctx));
  46894. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  46895. SSL_SUCCESS);
  46896. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  46897. SSL_free(ssl);
  46898. SSL_CTX_free(ctx);
  46899. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  46900. return EXPECT_RESULT();
  46901. }
  46902. #ifndef NO_BIO
  46903. static int test_wolfSSL_PEM_read_bio(void)
  46904. {
  46905. EXPECT_DECLS;
  46906. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  46907. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46908. byte buff[6000];
  46909. XFILE f = XBADFILE;
  46910. int bytes;
  46911. X509* x509 = NULL;
  46912. BIO* bio = NULL;
  46913. BUF_MEM* buf = NULL;
  46914. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  46915. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  46916. if (f != XBADFILE)
  46917. XFCLOSE(f);
  46918. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  46919. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  46920. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  46921. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  46922. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  46923. /* BIO should return the set EOF value */
  46924. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  46925. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  46926. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  46927. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  46928. BIO_free(bio);
  46929. BUF_MEM_free(buf);
  46930. X509_free(x509);
  46931. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  46932. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  46933. return EXPECT_RESULT();
  46934. }
  46935. #if defined(OPENSSL_EXTRA)
  46936. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  46937. long argl, long ret)
  46938. {
  46939. (void)bio;
  46940. (void)cmd;
  46941. (void)argp;
  46942. (void)argi;
  46943. (void)argl;
  46944. return ret;
  46945. }
  46946. #endif
  46947. static int test_wolfSSL_BIO(void)
  46948. {
  46949. EXPECT_DECLS;
  46950. #if defined(OPENSSL_EXTRA)
  46951. const unsigned char* p = NULL;
  46952. byte buff[20];
  46953. BIO* bio1 = NULL;
  46954. BIO* bio2 = NULL;
  46955. BIO* bio3 = NULL;
  46956. char* bufPt = NULL;
  46957. int i;
  46958. for (i = 0; i < 20; i++) {
  46959. buff[i] = i;
  46960. }
  46961. /* test BIO_free with NULL */
  46962. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  46963. /* Creating and testing type BIO_s_bio */
  46964. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  46965. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  46966. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  46967. /* read/write before set up */
  46968. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  46969. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  46970. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  46971. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  46972. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  46973. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  46974. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  46975. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  46976. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  46977. /* write buffer full */
  46978. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  46979. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  46980. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  46981. /* write the other direction with pair */
  46982. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  46983. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  46984. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  46985. /* try read */
  46986. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  46987. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  46988. /* try read using ctrl function */
  46989. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  46990. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  46991. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  46992. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  46993. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  46994. for (i = 0; i < 20; i++) {
  46995. ExpectIntEQ((int)bufPt[i], i);
  46996. }
  46997. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  46998. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  46999. for (i = 0; i < 8; i++) {
  47000. ExpectIntEQ((int)bufPt[i], i);
  47001. }
  47002. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  47003. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  47004. /* new pair */
  47005. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  47006. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  47007. bio2 = NULL;
  47008. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  47009. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  47010. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  47011. /* test wrap around... */
  47012. ExpectIntEQ(BIO_reset(bio1), 0);
  47013. ExpectIntEQ(BIO_reset(bio3), 0);
  47014. /* fill write buffer, read only small amount then write again */
  47015. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  47016. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  47017. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  47018. for (i = 0; i < 4; i++) {
  47019. ExpectIntEQ(bufPt[i], i);
  47020. }
  47021. /* try writing over read index */
  47022. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  47023. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  47024. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  47025. /* read and write 0 bytes */
  47026. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  47027. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  47028. /* should read only to end of write buffer then need to read again */
  47029. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  47030. for (i = 0; i < 16; i++) {
  47031. ExpectIntEQ(bufPt[i], buff[4 + i]);
  47032. }
  47033. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  47034. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  47035. for (i = 0; i < 4; i++) {
  47036. ExpectIntEQ(bufPt[i], 0);
  47037. }
  47038. /* read index should not have advanced with nread0 */
  47039. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  47040. for (i = 0; i < 4; i++) {
  47041. ExpectIntEQ(bufPt[i], 0);
  47042. }
  47043. /* write and fill up buffer checking reset of index state */
  47044. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  47045. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  47046. /* test reset on data in bio1 write buffer */
  47047. ExpectIntEQ(BIO_reset(bio1), 0);
  47048. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  47049. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  47050. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  47051. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  47052. ExpectNotNull(p);
  47053. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  47054. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  47055. for (i = 0; i < 6; i++) {
  47056. ExpectIntEQ(bufPt[i], i);
  47057. }
  47058. /* test case of writing twice with offset read index */
  47059. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  47060. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  47061. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  47062. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  47063. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  47064. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  47065. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  47066. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  47067. BIO_free(bio1);
  47068. bio1 = NULL;
  47069. BIO_free(bio3);
  47070. bio3 = NULL;
  47071. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  47072. {
  47073. BIO* bioA = NULL;
  47074. BIO* bioB = NULL;
  47075. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  47076. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  47077. BIO_free(bioA);
  47078. bioA = NULL;
  47079. BIO_free(bioB);
  47080. bioB = NULL;
  47081. }
  47082. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  47083. /* BIOs with file pointers */
  47084. #if !defined(NO_FILESYSTEM)
  47085. {
  47086. XFILE f1 = XBADFILE;
  47087. XFILE f2 = XBADFILE;
  47088. BIO* f_bio1 = NULL;
  47089. BIO* f_bio2 = NULL;
  47090. unsigned char cert[300];
  47091. char testFile[] = "tests/bio_write_test.txt";
  47092. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  47093. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  47094. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  47095. /* Failure due to wrong BIO type */
  47096. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  47097. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  47098. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  47099. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  47100. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  47101. WOLFSSL_SUCCESS);
  47102. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  47103. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  47104. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  47105. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  47106. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  47107. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  47108. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  47109. ExpectIntEQ(BIO_reset(f_bio2), 0);
  47110. ExpectIntEQ(BIO_tell(NULL),-1);
  47111. ExpectIntEQ(BIO_tell(f_bio2),0);
  47112. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  47113. ExpectIntEQ(BIO_tell(f_bio2),4);
  47114. BIO_free(f_bio1);
  47115. f_bio1 = NULL;
  47116. BIO_free(f_bio2);
  47117. f_bio2 = NULL;
  47118. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  47119. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  47120. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  47121. BIO_free(f_bio1);
  47122. f_bio1 = NULL;
  47123. }
  47124. #endif /* !defined(NO_FILESYSTEM) */
  47125. /* BIO info callback */
  47126. {
  47127. const char* testArg = "test";
  47128. BIO* cb_bio = NULL;
  47129. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  47130. BIO_set_callback(cb_bio, bioCallback);
  47131. ExpectNotNull(BIO_get_callback(cb_bio));
  47132. BIO_set_callback(cb_bio, NULL);
  47133. ExpectNull(BIO_get_callback(cb_bio));
  47134. BIO_set_callback_arg(cb_bio, (char*)testArg);
  47135. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  47136. ExpectNull(BIO_get_callback_arg(NULL));
  47137. BIO_free(cb_bio);
  47138. cb_bio = NULL;
  47139. }
  47140. /* BIO_vfree */
  47141. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  47142. BIO_vfree(NULL);
  47143. BIO_vfree(bio1);
  47144. #endif
  47145. return EXPECT_RESULT();
  47146. }
  47147. static int test_wolfSSL_BIO_BIO_ring_read(void)
  47148. {
  47149. EXPECT_DECLS;
  47150. #if defined(OPENSSL_ALL)
  47151. BIO* bio1 = NULL;
  47152. BIO* bio2 = NULL;
  47153. byte data[50];
  47154. byte tmp[50];
  47155. XMEMSET(data, 42, sizeof(data));
  47156. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  47157. SSL_SUCCESS);
  47158. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  47159. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  47160. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  47161. ExpectBufEQ(tmp, data, 20);
  47162. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  47163. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  47164. ExpectBufEQ(tmp, data, 40);
  47165. BIO_free(bio1);
  47166. BIO_free(bio2);
  47167. #endif
  47168. return EXPECT_RESULT();
  47169. }
  47170. #endif /* !NO_BIO */
  47171. static int test_wolfSSL_a2i_IPADDRESS(void)
  47172. {
  47173. EXPECT_DECLS;
  47174. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  47175. const unsigned char* data = NULL;
  47176. int dataSz = 0;
  47177. ASN1_OCTET_STRING *st = NULL;
  47178. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  47179. const unsigned char ipv6_exp[] = {
  47180. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  47181. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  47182. };
  47183. const unsigned char ipv6_home[] = {
  47184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  47186. };
  47187. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  47188. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  47189. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  47190. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  47191. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  47192. ASN1_STRING_free(st);
  47193. st = NULL;
  47194. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  47195. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  47196. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  47197. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  47198. ASN1_STRING_free(st);
  47199. st = NULL;
  47200. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  47201. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  47202. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  47203. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  47204. ASN1_STRING_free(st);
  47205. #endif
  47206. return EXPECT_RESULT();
  47207. }
  47208. static int test_wolfSSL_X509_cmp_time(void)
  47209. {
  47210. EXPECT_DECLS;
  47211. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  47212. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  47213. WOLFSSL_ASN1_TIME asn_time;
  47214. time_t t;
  47215. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  47216. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  47217. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  47218. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  47219. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  47220. #endif
  47221. return EXPECT_RESULT();
  47222. }
  47223. static int test_wolfSSL_X509_time_adj(void)
  47224. {
  47225. EXPECT_DECLS;
  47226. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  47227. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  47228. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  47229. !defined(NO_ASN_TIME)
  47230. X509* x509 = NULL;
  47231. time_t t;
  47232. time_t not_before;
  47233. time_t not_after;
  47234. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  47235. client_cert_der_2048, sizeof_client_cert_der_2048,
  47236. WOLFSSL_FILETYPE_ASN1));
  47237. t = 0;
  47238. not_before = wc_Time(0);
  47239. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  47240. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  47241. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  47242. /* Check X509_gmtime_adj, too. */
  47243. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  47244. X509_free(x509);
  47245. #endif
  47246. return EXPECT_RESULT();
  47247. }
  47248. static int test_wolfSSL_X509_bad_altname(void)
  47249. {
  47250. EXPECT_DECLS;
  47251. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  47252. const unsigned char malformed_alt_name_cert[] = {
  47253. 0x30, 0x82, 0x02, 0xf9, 0x30, 0x82, 0x01, 0xe1, 0xa0, 0x03, 0x02, 0x01,
  47254. 0x02, 0x02, 0x02, 0x10, 0x21, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  47255. 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x0f, 0x31, 0x0d,
  47256. 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61, 0x31,
  47257. 0x31, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x32, 0x30, 0x37, 0x31,
  47258. 0x37, 0x32, 0x34, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34, 0x30, 0x32,
  47259. 0x31, 0x34, 0x30, 0x36, 0x32, 0x36, 0x35, 0x33, 0x5a, 0x30, 0x0f, 0x31,
  47260. 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61,
  47261. 0x61, 0x61, 0x30, 0x82, 0x01, 0x20, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  47262. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
  47263. 0x0d, 0x00, 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa8,
  47264. 0x8a, 0x5e, 0x26, 0x23, 0x1b, 0x31, 0xd3, 0x37, 0x1a, 0x70, 0xb2, 0xec,
  47265. 0x3f, 0x74, 0xd4, 0xb4, 0x44, 0xe3, 0x7a, 0xa5, 0xc0, 0xf5, 0xaa, 0x97,
  47266. 0x26, 0x9a, 0x04, 0xff, 0xda, 0xbe, 0xe5, 0x09, 0x03, 0x98, 0x3d, 0xb5,
  47267. 0xbf, 0x01, 0x2c, 0x9a, 0x0a, 0x3a, 0xfb, 0xbc, 0x3c, 0xe7, 0xbe, 0x83,
  47268. 0x5c, 0xb3, 0x70, 0xe8, 0x5c, 0xe3, 0xd1, 0x83, 0xc3, 0x94, 0x08, 0xcd,
  47269. 0x1a, 0x87, 0xe5, 0xe0, 0x5b, 0x9c, 0x5c, 0x6e, 0xb0, 0x7d, 0xe2, 0x58,
  47270. 0x6c, 0xc3, 0xb5, 0xc8, 0x9d, 0x11, 0xf1, 0x5d, 0x96, 0x0d, 0x66, 0x1e,
  47271. 0x56, 0x7f, 0x8f, 0x59, 0xa7, 0xa5, 0xe1, 0xc5, 0xe7, 0x81, 0x4c, 0x09,
  47272. 0x9d, 0x5e, 0x96, 0xf0, 0x9a, 0xc2, 0x8b, 0x70, 0xd5, 0xab, 0x79, 0x58,
  47273. 0x5d, 0xb7, 0x58, 0xaa, 0xfd, 0x75, 0x52, 0xaa, 0x4b, 0xa7, 0x25, 0x68,
  47274. 0x76, 0x59, 0x00, 0xee, 0x78, 0x2b, 0x91, 0xc6, 0x59, 0x91, 0x99, 0x38,
  47275. 0x3e, 0xa1, 0x76, 0xc3, 0xf5, 0x23, 0x6b, 0xe6, 0x07, 0xea, 0x63, 0x1c,
  47276. 0x97, 0x49, 0xef, 0xa0, 0xfe, 0xfd, 0x13, 0xc9, 0xa9, 0x9f, 0xc2, 0x0b,
  47277. 0xe6, 0x87, 0x92, 0x5b, 0xcc, 0xf5, 0x42, 0x95, 0x4a, 0xa4, 0x6d, 0x64,
  47278. 0xba, 0x7d, 0xce, 0xcb, 0x04, 0xd0, 0xf8, 0xe7, 0xe3, 0xda, 0x75, 0x60,
  47279. 0xd3, 0x8b, 0x6a, 0x64, 0xfc, 0x78, 0x56, 0x21, 0x69, 0x5a, 0xe8, 0xa7,
  47280. 0x8f, 0xfb, 0x8f, 0x82, 0xe3, 0xae, 0x36, 0xa2, 0x93, 0x66, 0x92, 0xcb,
  47281. 0x82, 0xa3, 0xbe, 0x84, 0x00, 0x86, 0xdc, 0x7e, 0x6d, 0x53, 0x77, 0x84,
  47282. 0x17, 0xb9, 0x55, 0x43, 0x0d, 0xf1, 0x16, 0x1f, 0xd5, 0x43, 0x75, 0x99,
  47283. 0x66, 0x19, 0x52, 0xd0, 0xac, 0x5f, 0x74, 0xad, 0xb2, 0x90, 0x15, 0x50,
  47284. 0x04, 0x74, 0x43, 0xdf, 0x6c, 0x35, 0xd0, 0xfd, 0x32, 0x37, 0xb3, 0x8d,
  47285. 0xf5, 0xe5, 0x09, 0x02, 0x01, 0x03, 0xa3, 0x61, 0x30, 0x5f, 0x30, 0x0c,
  47286. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00,
  47287. 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82,
  47288. 0x04, 0x61, 0x2a, 0x00, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
  47289. 0x04, 0x16, 0x04, 0x14, 0x92, 0x6a, 0x1e, 0x52, 0x3a, 0x1a, 0x57, 0x9f,
  47290. 0xc9, 0x82, 0x9a, 0xce, 0xc8, 0xc0, 0xa9, 0x51, 0x9d, 0x2f, 0xc7, 0x72,
  47291. 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
  47292. 0x14, 0x6b, 0xf9, 0xa4, 0x2d, 0xa5, 0xe9, 0x39, 0x89, 0xa8, 0x24, 0x58,
  47293. 0x79, 0x87, 0x11, 0xfc, 0x6f, 0x07, 0x91, 0xef, 0xa6, 0x30, 0x0d, 0x06,
  47294. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
  47295. 0x03, 0x82, 0x01, 0x01, 0x00, 0x3f, 0xd5, 0x37, 0x2f, 0xc7, 0xf8, 0x8b,
  47296. 0x39, 0x1c, 0xe3, 0xdf, 0x77, 0xee, 0xc6, 0x4b, 0x5f, 0x84, 0xcf, 0xfa,
  47297. 0x33, 0x2c, 0xb2, 0xb5, 0x4b, 0x09, 0xee, 0x56, 0xc0, 0xf2, 0xf0, 0xeb,
  47298. 0xad, 0x1c, 0x02, 0xef, 0xae, 0x09, 0x53, 0xc0, 0x06, 0xad, 0x4e, 0xfd,
  47299. 0x3e, 0x8c, 0x13, 0xb3, 0xbf, 0x80, 0x05, 0x36, 0xb5, 0x3f, 0x2b, 0xc7,
  47300. 0x60, 0x53, 0x14, 0xbf, 0x33, 0x63, 0x47, 0xc3, 0xc6, 0x28, 0xda, 0x10,
  47301. 0x12, 0xe2, 0xc4, 0xeb, 0xc5, 0x64, 0x66, 0xc0, 0xcc, 0x6b, 0x84, 0xda,
  47302. 0x0c, 0xe9, 0xf6, 0xe3, 0xf8, 0x8e, 0x3d, 0x95, 0x5f, 0xba, 0x9f, 0xe1,
  47303. 0xc7, 0xed, 0x6e, 0x97, 0xcc, 0xbd, 0x7d, 0xe5, 0x4e, 0xab, 0xbc, 0x1b,
  47304. 0xf1, 0x3a, 0x09, 0x33, 0x09, 0xe1, 0xcc, 0xec, 0x21, 0x16, 0x8e, 0xb1,
  47305. 0x74, 0x9e, 0xc8, 0x13, 0x7c, 0xdf, 0x07, 0xaa, 0xeb, 0x70, 0xd7, 0x91,
  47306. 0x5c, 0xc4, 0xef, 0x83, 0x88, 0xc3, 0xe4, 0x97, 0xfa, 0xe4, 0xdf, 0xd7,
  47307. 0x0d, 0xff, 0xba, 0x78, 0x22, 0xfc, 0x3f, 0xdc, 0xd8, 0x02, 0x8d, 0x93,
  47308. 0x57, 0xf9, 0x9e, 0x39, 0x3a, 0x77, 0x00, 0xd9, 0x19, 0xaa, 0x68, 0xa1,
  47309. 0xe6, 0x9e, 0x13, 0xeb, 0x37, 0x16, 0xf5, 0x77, 0xa4, 0x0b, 0x40, 0x04,
  47310. 0xd3, 0xa5, 0x49, 0x78, 0x35, 0xfa, 0x3b, 0xf6, 0x02, 0xab, 0x85, 0xee,
  47311. 0xcb, 0x9b, 0x62, 0xda, 0x05, 0x00, 0x22, 0x2f, 0xf8, 0xbd, 0x0b, 0xe5,
  47312. 0x2c, 0xb2, 0x53, 0x78, 0x0a, 0xcb, 0x69, 0xc0, 0xb6, 0x9f, 0x96, 0xff,
  47313. 0x58, 0x22, 0x70, 0x9c, 0x01, 0x2e, 0x56, 0x60, 0x5d, 0x37, 0xe3, 0x40,
  47314. 0x25, 0xc9, 0x90, 0xc8, 0x0f, 0x41, 0x68, 0xb4, 0xfd, 0x10, 0xe2, 0x09,
  47315. 0x99, 0x08, 0x5d, 0x7b, 0xc9, 0xe3, 0x29, 0xd4, 0x5a, 0xcf, 0xc9, 0x34,
  47316. 0x55, 0xa1, 0x40, 0x44, 0xd6, 0x88, 0x16, 0xbb, 0xdd
  47317. };
  47318. X509* x509 = NULL;
  47319. int certSize = (int)sizeof(malformed_alt_name_cert) / sizeof(unsigned char);
  47320. const char *name = "aaaaa";
  47321. int nameLen = (int)XSTRLEN(name);
  47322. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  47323. malformed_alt_name_cert, certSize, SSL_FILETYPE_ASN1));
  47324. /* malformed_alt_name_cert has a malformed alternative
  47325. * name of "a*\0*". Ensure that it does not match "aaaaa" */
  47326. ExpectIntNE(wolfSSL_X509_check_host(x509, name, nameLen,
  47327. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  47328. X509_free(x509);
  47329. #endif
  47330. return EXPECT_RESULT();
  47331. }
  47332. static int test_wolfSSL_X509_name_match(void)
  47333. {
  47334. EXPECT_DECLS;
  47335. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  47336. /* A certificate with the subject alternative name a* */
  47337. const unsigned char cert_der[] = {
  47338. 0x30, 0x82, 0x03, 0xac, 0x30, 0x82, 0x02, 0x94, 0xa0, 0x03, 0x02, 0x01,
  47339. 0x02, 0x02, 0x14, 0x0f, 0xa5, 0x10, 0x85, 0xef, 0x58, 0x10, 0x59, 0xfc,
  47340. 0x0f, 0x20, 0x1f, 0x53, 0xf5, 0x30, 0x39, 0x34, 0x49, 0x54, 0x05, 0x30,
  47341. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  47342. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  47343. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  47344. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  47345. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  47346. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  47347. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  47348. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  47349. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  47350. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  47351. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  47352. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  47353. 0x30, 0x32, 0x30, 0x31, 0x35, 0x35, 0x38, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  47354. 0x30, 0x35, 0x32, 0x38, 0x32, 0x30, 0x31, 0x35, 0x35, 0x38, 0x5a, 0x30,
  47355. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  47356. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  47357. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  47358. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  47359. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  47360. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  47361. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  47362. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  47363. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  47364. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  47365. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  47366. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  47367. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xf4, 0xca, 0x3d,
  47368. 0xd4, 0xbc, 0x9b, 0xea, 0x74, 0xfe, 0x73, 0xf4, 0x16, 0x23, 0x0b, 0x4a,
  47369. 0x09, 0x54, 0xf6, 0x7b, 0x10, 0x99, 0x11, 0x93, 0xb2, 0xdb, 0x4d, 0x7d,
  47370. 0x23, 0xab, 0xf9, 0xcd, 0xf6, 0x54, 0xd4, 0xf6, 0x39, 0x57, 0xee, 0x97,
  47371. 0xb2, 0xb9, 0xfc, 0x7e, 0x9c, 0xb3, 0xfb, 0x56, 0xb6, 0x84, 0xd6, 0x2d,
  47372. 0x59, 0x1c, 0xed, 0xda, 0x9b, 0x19, 0xf5, 0x8a, 0xa7, 0x8a, 0x89, 0xd6,
  47373. 0xa1, 0xc0, 0xe6, 0x16, 0xad, 0x04, 0xcf, 0x5a, 0x1f, 0xdf, 0x62, 0x6c,
  47374. 0x68, 0x45, 0xe9, 0x55, 0x2e, 0x42, 0xa3, 0x1b, 0x3b, 0x86, 0x23, 0x22,
  47375. 0xa1, 0x20, 0x48, 0xd1, 0x52, 0xc0, 0x8b, 0xab, 0xe2, 0x8a, 0x15, 0x68,
  47376. 0xbd, 0x89, 0x6f, 0x9f, 0x45, 0x75, 0xb4, 0x27, 0xc1, 0x72, 0x41, 0xfd,
  47377. 0x79, 0x89, 0xb0, 0x74, 0xa2, 0xe9, 0x61, 0x48, 0x4c, 0x54, 0xad, 0x6b,
  47378. 0x61, 0xbf, 0x0e, 0x27, 0x58, 0xb4, 0xf6, 0x9c, 0x2c, 0x9f, 0xc2, 0x3e,
  47379. 0x3b, 0xb3, 0x90, 0x41, 0xbc, 0x61, 0xcd, 0x01, 0x57, 0x90, 0x82, 0xec,
  47380. 0x46, 0xba, 0x4f, 0x89, 0x8e, 0x7f, 0x49, 0x4f, 0x46, 0x69, 0x37, 0x8b,
  47381. 0xa0, 0xba, 0x85, 0xe8, 0x42, 0xff, 0x9a, 0xa1, 0x53, 0x81, 0x5c, 0xf3,
  47382. 0x8e, 0x85, 0x1c, 0xd4, 0x90, 0x60, 0xa0, 0x37, 0x59, 0x04, 0x65, 0xa6,
  47383. 0xb5, 0x12, 0x00, 0xc3, 0x04, 0x51, 0xa7, 0x83, 0x96, 0x62, 0x3d, 0x49,
  47384. 0x97, 0xe8, 0x6b, 0x9a, 0x5d, 0x51, 0x24, 0xee, 0xad, 0x45, 0x18, 0x0f,
  47385. 0x3f, 0x97, 0xec, 0xdf, 0xcf, 0x42, 0x8a, 0x96, 0xc7, 0xd8, 0x82, 0x87,
  47386. 0x7f, 0x57, 0x70, 0x22, 0xfb, 0x29, 0x3e, 0x3c, 0xa3, 0xc1, 0xd5, 0x71,
  47387. 0xb3, 0x84, 0x06, 0x53, 0xa3, 0x86, 0x20, 0x35, 0xe3, 0x41, 0xb9, 0xd8,
  47388. 0x00, 0x22, 0x4f, 0x6d, 0xe6, 0xfd, 0xf0, 0xf4, 0xa2, 0x39, 0x0a, 0x1a,
  47389. 0x23, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x30, 0x30, 0x2e, 0x30, 0x0d,
  47390. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x06, 0x30, 0x04, 0x82, 0x02, 0x61,
  47391. 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  47392. 0x45, 0x05, 0xf3, 0x4d, 0x3e, 0x7e, 0x9c, 0xf5, 0x08, 0xee, 0x2c, 0x13,
  47393. 0x32, 0xe3, 0xf2, 0x14, 0xe8, 0x0e, 0x71, 0x21, 0x30, 0x0d, 0x06, 0x09,
  47394. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  47395. 0x82, 0x01, 0x01, 0x00, 0xa8, 0x28, 0xe5, 0x22, 0x65, 0xcf, 0x47, 0xfe,
  47396. 0x82, 0x17, 0x99, 0x20, 0xdb, 0xb1, 0x57, 0xd4, 0x91, 0x1a, 0x83, 0xde,
  47397. 0xc1, 0xaf, 0xc4, 0x1f, 0xfb, 0xa4, 0x6a, 0xad, 0xdc, 0x58, 0x72, 0xd9,
  47398. 0x9b, 0xab, 0xa5, 0xbb, 0xf4, 0x98, 0xd4, 0xdf, 0x36, 0xcb, 0xb5, 0x78,
  47399. 0xce, 0x4b, 0x25, 0x5b, 0x24, 0x92, 0xfe, 0xe8, 0xd4, 0xe4, 0xbd, 0x6f,
  47400. 0x71, 0x1a, 0x81, 0x2a, 0x6f, 0x35, 0x93, 0xf7, 0xcc, 0xed, 0xe5, 0x06,
  47401. 0xd2, 0x96, 0x41, 0xb5, 0xa9, 0x8a, 0xc0, 0xc9, 0x17, 0xe3, 0x13, 0x5e,
  47402. 0x94, 0x5e, 0xfa, 0xfc, 0xf0, 0x00, 0x2e, 0xe1, 0xd8, 0x1b, 0x23, 0x3f,
  47403. 0x7c, 0x4d, 0x9f, 0xfb, 0xb7, 0x95, 0xc1, 0x94, 0x7f, 0x7f, 0xb5, 0x4f,
  47404. 0x93, 0x6d, 0xc3, 0x2b, 0xb2, 0x28, 0x36, 0xd2, 0x7c, 0x01, 0x3c, 0xae,
  47405. 0x35, 0xdb, 0xc8, 0x95, 0x1b, 0x5f, 0x6c, 0x0f, 0x57, 0xb3, 0xcc, 0x97,
  47406. 0x98, 0x80, 0x06, 0xaa, 0xe4, 0x93, 0x1f, 0xb7, 0xa0, 0x54, 0xf1, 0x4f,
  47407. 0x6f, 0x11, 0xdf, 0xab, 0xd3, 0xbf, 0xf0, 0x3a, 0x81, 0x60, 0xaf, 0x7a,
  47408. 0xf7, 0x09, 0xd5, 0xae, 0x0c, 0x7d, 0xae, 0x8d, 0x47, 0x06, 0xbe, 0x11,
  47409. 0x6e, 0xf8, 0x7e, 0x49, 0xf8, 0xac, 0x24, 0x0a, 0x4b, 0xc2, 0xf6, 0xe8,
  47410. 0x2c, 0xec, 0x35, 0xef, 0xa9, 0x13, 0xb8, 0xd2, 0x9c, 0x92, 0x61, 0x91,
  47411. 0xec, 0x7b, 0x0c, 0xea, 0x9a, 0x71, 0x36, 0x15, 0x34, 0x2b, 0x7a, 0x25,
  47412. 0xac, 0xfe, 0xc7, 0x26, 0x89, 0x70, 0x3e, 0x64, 0x68, 0x97, 0x4b, 0xaa,
  47413. 0xc1, 0x24, 0x14, 0xbd, 0x45, 0x2f, 0xe0, 0xfe, 0xf4, 0x2b, 0x8e, 0x08,
  47414. 0x3e, 0xe4, 0xb5, 0x3d, 0x5d, 0xf4, 0xc3, 0xd6, 0x9c, 0xb5, 0x33, 0x1b,
  47415. 0x3b, 0xda, 0x6e, 0x99, 0x7b, 0x09, 0xd1, 0x30, 0x97, 0x23, 0x52, 0x6d,
  47416. 0x1b, 0x71, 0x3a, 0xf4, 0x54, 0xf0, 0xe5, 0x9e
  47417. };
  47418. WOLFSSL_X509* x509 = NULL;
  47419. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  47420. const char *name1 = "aaaaa";
  47421. int nameLen1 = (int)(XSTRLEN(name1));
  47422. const char *name2 = "a";
  47423. int nameLen2 = (int)(XSTRLEN(name2));
  47424. const char *name3 = "abbbb";
  47425. int nameLen3 = (int)(XSTRLEN(name3));
  47426. const char *name4 = "bbb";
  47427. int nameLen4 = (int)(XSTRLEN(name4));
  47428. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  47429. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  47430. /* Ensure that "a*" matches "aaaaa" */
  47431. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  47432. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47433. /* Ensure that "a*" matches "a" */
  47434. ExpectIntEQ(wolfSSL_X509_check_host(x509, name2, nameLen2,
  47435. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47436. /* Ensure that "a*" matches "abbbb" */
  47437. ExpectIntEQ(wolfSSL_X509_check_host(x509, name3, nameLen3,
  47438. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47439. /* Ensure that "a*" does not match "bbb" */
  47440. ExpectIntNE(wolfSSL_X509_check_host(x509, name4, nameLen4,
  47441. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  47442. wolfSSL_X509_free(x509);
  47443. #endif
  47444. return EXPECT_RESULT();
  47445. }
  47446. static int test_wolfSSL_X509_name_match2(void)
  47447. {
  47448. EXPECT_DECLS;
  47449. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  47450. /* A certificate with the subject alternative name a*b* */
  47451. const unsigned char cert_der[] = {
  47452. 0x30, 0x82, 0x03, 0xae, 0x30, 0x82, 0x02, 0x96, 0xa0, 0x03, 0x02, 0x01,
  47453. 0x02, 0x02, 0x14, 0x41, 0x8c, 0x8b, 0xaa, 0x0e, 0xd8, 0x5a, 0xc0, 0x52,
  47454. 0x46, 0x0e, 0xe5, 0xd8, 0xb9, 0x48, 0x93, 0x7e, 0x8a, 0x7c, 0x65, 0x30,
  47455. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  47456. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  47457. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  47458. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  47459. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  47460. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  47461. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  47462. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  47463. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  47464. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  47465. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  47466. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  47467. 0x30, 0x32, 0x30, 0x34, 0x33, 0x34, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  47468. 0x30, 0x35, 0x32, 0x38, 0x32, 0x30, 0x34, 0x33, 0x34, 0x30, 0x5a, 0x30,
  47469. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  47470. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  47471. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  47472. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  47473. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  47474. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  47475. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  47476. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  47477. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  47478. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  47479. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  47480. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  47481. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa5, 0x60, 0x80,
  47482. 0xf3, 0xee, 0x19, 0xd2, 0xe4, 0x15, 0x94, 0x54, 0x12, 0x88, 0xee, 0xda,
  47483. 0x11, 0x11, 0x87, 0x99, 0x88, 0xb3, 0x71, 0xc7, 0x97, 0x78, 0x1b, 0x57,
  47484. 0x37, 0x1d, 0x0b, 0x1f, 0x2f, 0x2c, 0x35, 0x13, 0x75, 0xd3, 0x31, 0x3e,
  47485. 0x6f, 0x80, 0x21, 0xa5, 0xa3, 0xad, 0x10, 0x81, 0xb6, 0x37, 0xd4, 0x55,
  47486. 0x2e, 0xc1, 0xb8, 0x37, 0xa3, 0x3c, 0xe8, 0x81, 0x03, 0x3c, 0xda, 0x5f,
  47487. 0x6f, 0x45, 0x32, 0x2b, 0x0e, 0x99, 0x27, 0xfd, 0xe5, 0x6c, 0x07, 0xd9,
  47488. 0x4e, 0x0a, 0x8b, 0x23, 0x74, 0x96, 0x25, 0x97, 0xae, 0x6d, 0x19, 0xba,
  47489. 0xbf, 0x0f, 0xc8, 0xa1, 0xe5, 0xea, 0xa8, 0x00, 0x09, 0xc3, 0x9a, 0xef,
  47490. 0x09, 0x33, 0xc1, 0x33, 0x2e, 0x7b, 0x6d, 0xa7, 0x66, 0x87, 0xb6, 0x3a,
  47491. 0xb9, 0xdb, 0x4c, 0x5e, 0xb5, 0x55, 0x69, 0x37, 0x17, 0x92, 0x1f, 0xe3,
  47492. 0x53, 0x1a, 0x2d, 0x25, 0xd0, 0xcf, 0x72, 0x37, 0xc2, 0x89, 0x83, 0x78,
  47493. 0xcf, 0xac, 0x2e, 0x46, 0x92, 0x5c, 0x4a, 0xba, 0x7d, 0xa0, 0x22, 0x34,
  47494. 0xb1, 0x22, 0x26, 0x99, 0xda, 0xe8, 0x97, 0xe2, 0x0c, 0xd3, 0xbc, 0x97,
  47495. 0x7e, 0xa8, 0xb9, 0xe3, 0xe2, 0x7f, 0x56, 0xef, 0x22, 0xee, 0x15, 0x95,
  47496. 0xa6, 0xd1, 0xf4, 0xa7, 0xac, 0x4a, 0xab, 0xc1, 0x1a, 0xda, 0xc5, 0x5f,
  47497. 0xa5, 0x5e, 0x2f, 0x15, 0x9c, 0x36, 0xbe, 0xd3, 0x47, 0xb6, 0x86, 0xb9,
  47498. 0xc6, 0x59, 0x39, 0x36, 0xad, 0x84, 0x53, 0x95, 0x72, 0x91, 0x89, 0x51,
  47499. 0x32, 0x77, 0xf1, 0xa5, 0x93, 0xfe, 0xf0, 0x41, 0x7c, 0x64, 0xf1, 0xb0,
  47500. 0x8b, 0x81, 0x8d, 0x3a, 0x2c, 0x9e, 0xbe, 0x2e, 0x8b, 0xf7, 0x80, 0x63,
  47501. 0x35, 0x32, 0xfa, 0x26, 0xe0, 0x63, 0xbf, 0x5e, 0xaf, 0xf0, 0x08, 0xe0,
  47502. 0x80, 0x65, 0x38, 0xfa, 0x21, 0xaa, 0x91, 0x34, 0x48, 0x3d, 0x32, 0x5c,
  47503. 0xbf, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x32, 0x30, 0x30, 0x30, 0x0f,
  47504. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82, 0x04, 0x61,
  47505. 0x2a, 0x62, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16,
  47506. 0x04, 0x14, 0x3d, 0x55, 0x74, 0xf8, 0x3a, 0x26, 0x03, 0x8c, 0x6a, 0x2e,
  47507. 0x91, 0x0e, 0x18, 0x70, 0xb4, 0xa4, 0xcc, 0x04, 0x00, 0xd3, 0x30, 0x0d,
  47508. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  47509. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x8f, 0x3b, 0xff, 0x46, 0x0c, 0xb5,
  47510. 0x21, 0xdc, 0xcf, 0x61, 0x9a, 0x25, 0x93, 0x99, 0x68, 0x2f, 0x16, 0x71,
  47511. 0x15, 0x00, 0x5f, 0xb0, 0x9b, 0x43, 0x5c, 0x47, 0xe2, 0x8e, 0xc8, 0xea,
  47512. 0xb3, 0x30, 0x4d, 0x87, 0x90, 0xcf, 0x24, 0x37, 0x5c, 0xfd, 0xc8, 0xc6,
  47513. 0x09, 0x36, 0xb2, 0xfb, 0xfd, 0xc1, 0x82, 0x92, 0x77, 0x5b, 0x9d, 0xeb,
  47514. 0xac, 0x47, 0xbc, 0xda, 0x7c, 0x89, 0x19, 0x03, 0x9e, 0xcd, 0x96, 0x2a,
  47515. 0x90, 0x55, 0x23, 0x19, 0xac, 0x9d, 0x49, 0xfb, 0xa0, 0x31, 0x7d, 0x6b,
  47516. 0x1a, 0x16, 0x13, 0xb1, 0xa9, 0xc9, 0xc4, 0xaf, 0xf1, 0xb4, 0xa7, 0x9b,
  47517. 0x08, 0x64, 0x6a, 0x09, 0xcd, 0x4a, 0x03, 0x4c, 0x93, 0xb6, 0xcf, 0x29,
  47518. 0xdb, 0x56, 0x88, 0x8e, 0xed, 0x08, 0x6d, 0x8d, 0x76, 0xa3, 0xd7, 0xc6,
  47519. 0x69, 0xa1, 0xf5, 0xd2, 0xd0, 0x0a, 0x4b, 0xfa, 0x88, 0x66, 0x6c, 0xe5,
  47520. 0x4a, 0xee, 0x13, 0xad, 0xad, 0x22, 0x25, 0x73, 0x39, 0x56, 0x74, 0x0e,
  47521. 0xda, 0xcd, 0x35, 0x67, 0xe3, 0x81, 0x5c, 0xc5, 0xae, 0x3c, 0x4f, 0x47,
  47522. 0x3e, 0x97, 0xde, 0xac, 0xf6, 0xe1, 0x26, 0xe2, 0xe0, 0x66, 0x48, 0x20,
  47523. 0x7c, 0x02, 0x81, 0x3e, 0x7d, 0x34, 0xb7, 0x73, 0x3e, 0x2e, 0xd6, 0x20,
  47524. 0x1c, 0xdf, 0xf1, 0xae, 0x86, 0x8b, 0xb2, 0xc2, 0x9b, 0x68, 0x9c, 0xf6,
  47525. 0x1a, 0x5e, 0x30, 0x06, 0x39, 0x0a, 0x1f, 0x7b, 0xd7, 0x18, 0x4b, 0x06,
  47526. 0x9d, 0xff, 0x84, 0x57, 0xcc, 0x92, 0xad, 0x81, 0x0a, 0x19, 0x11, 0xc4,
  47527. 0xac, 0x59, 0x00, 0xe8, 0x5a, 0x70, 0x78, 0xd6, 0x9f, 0xe0, 0x82, 0x2a,
  47528. 0x1f, 0x09, 0x36, 0x1c, 0x52, 0x98, 0xf7, 0x95, 0x8f, 0xf9, 0x48, 0x4f,
  47529. 0x30, 0x52, 0xb5, 0xf3, 0x8d, 0x13, 0x93, 0x27, 0xbe, 0xb4, 0x75, 0x39,
  47530. 0x65, 0xc6, 0x48, 0x4e, 0x32, 0xd7, 0xf4, 0xc3, 0x26, 0x8d
  47531. };
  47532. WOLFSSL_X509* x509 = NULL;
  47533. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  47534. const char *name1 = "ab";
  47535. int nameLen1 = (int)(XSTRLEN(name1));
  47536. const char *name2 = "acccbccc";
  47537. int nameLen2 = (int)(XSTRLEN(name2));
  47538. const char *name3 = "accb";
  47539. int nameLen3 = (int)(XSTRLEN(name3));
  47540. const char *name4 = "accda";
  47541. int nameLen4 = (int)(XSTRLEN(name4));
  47542. const char *name5 = "acc\0bcc";
  47543. int nameLen5 = 7;
  47544. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  47545. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  47546. /* Ensure that "a*b*" matches "ab" */
  47547. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  47548. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47549. /* Ensure that "a*b*" matches "acccbccc" */
  47550. ExpectIntEQ(wolfSSL_X509_check_host(x509, name2, nameLen2,
  47551. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47552. /* Ensure that "a*b*" matches "accb" */
  47553. ExpectIntEQ(wolfSSL_X509_check_host(x509, name3, nameLen3,
  47554. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47555. /* Ensure that "a*b*" does not match "accda" */
  47556. ExpectIntNE(wolfSSL_X509_check_host(x509, name4, nameLen4,
  47557. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47558. /* Ensure that "a*b*" matches "ab", testing openssl behavior replication
  47559. * on check len input handling, 0 for len is OK as it should then use
  47560. * strlen(name1) */
  47561. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, 0,
  47562. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47563. /* Openssl also allows for len to include NULL terminator */
  47564. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1 + 1,
  47565. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47566. /* Ensure that check string with NULL terminator in middle is
  47567. * rejected */
  47568. ExpectIntNE(wolfSSL_X509_check_host(x509, name5, nameLen5,
  47569. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47570. wolfSSL_X509_free(x509);
  47571. #endif
  47572. return EXPECT_RESULT();
  47573. }
  47574. static int test_wolfSSL_X509_name_match3(void)
  47575. {
  47576. EXPECT_DECLS;
  47577. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  47578. /* A certificate with the subject alternative name *.example.com */
  47579. const unsigned char cert_der[] = {
  47580. 0x30, 0x82, 0x03, 0xb7, 0x30, 0x82, 0x02, 0x9f, 0xa0, 0x03, 0x02, 0x01,
  47581. 0x02, 0x02, 0x14, 0x59, 0xbb, 0xf6, 0xde, 0xb8, 0x3d, 0x0e, 0x8c, 0xe4,
  47582. 0xbd, 0x98, 0xa3, 0xbe, 0x3e, 0x8f, 0xdc, 0xbd, 0x7f, 0xcc, 0xae, 0x30,
  47583. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  47584. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  47585. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  47586. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  47587. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  47588. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  47589. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  47590. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  47591. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  47592. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  47593. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  47594. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  47595. 0x31, 0x30, 0x30, 0x33, 0x37, 0x34, 0x39, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  47596. 0x30, 0x35, 0x32, 0x39, 0x30, 0x30, 0x33, 0x37, 0x34, 0x39, 0x5a, 0x30,
  47597. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  47598. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  47599. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  47600. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  47601. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  47602. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  47603. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  47604. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  47605. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  47606. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  47607. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  47608. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  47609. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xda, 0x78, 0x16,
  47610. 0x05, 0x65, 0xf2, 0x85, 0xf2, 0x61, 0x7f, 0xb1, 0x4d, 0x73, 0xe2, 0x82,
  47611. 0xb5, 0x3d, 0xf7, 0x9d, 0x05, 0x65, 0xed, 0x9d, 0xc3, 0x29, 0x7a, 0x92,
  47612. 0x2c, 0x06, 0x5f, 0xc8, 0x13, 0x55, 0x42, 0x4e, 0xbd, 0xe2, 0x56, 0x2a,
  47613. 0x4b, 0xac, 0xe6, 0x1b, 0x10, 0xc9, 0xdb, 0x9a, 0x45, 0x36, 0xed, 0xf3,
  47614. 0x26, 0x8c, 0x22, 0x88, 0x1e, 0x6d, 0x2b, 0x41, 0xfa, 0x0d, 0x43, 0x88,
  47615. 0x88, 0xde, 0x8d, 0x2e, 0xca, 0x6e, 0x7c, 0x62, 0x66, 0x3e, 0xfa, 0x4e,
  47616. 0x71, 0xea, 0x7d, 0x3b, 0x32, 0x33, 0x5c, 0x7a, 0x7e, 0xea, 0x74, 0xbd,
  47617. 0xb6, 0x8f, 0x4c, 0x1c, 0x7a, 0x79, 0x94, 0xf1, 0xe8, 0x02, 0x67, 0x98,
  47618. 0x25, 0xb4, 0x31, 0x80, 0xc1, 0xae, 0xbf, 0xef, 0xf2, 0x6c, 0x78, 0x42,
  47619. 0xef, 0xb5, 0xc6, 0x01, 0x47, 0x79, 0x8d, 0x92, 0xce, 0xc1, 0xb5, 0x98,
  47620. 0x76, 0xf0, 0x84, 0xa2, 0x53, 0x90, 0xe5, 0x39, 0xc7, 0xbd, 0xf2, 0xbb,
  47621. 0xe3, 0x3f, 0x00, 0xf6, 0xf0, 0x46, 0x86, 0xee, 0x55, 0xbd, 0x2c, 0x1f,
  47622. 0x97, 0x24, 0x7c, 0xbc, 0xda, 0x2f, 0x1b, 0x53, 0xef, 0x26, 0x56, 0xcc,
  47623. 0xb7, 0xd8, 0xca, 0x17, 0x20, 0x4e, 0x62, 0x03, 0x66, 0x32, 0xb3, 0xd1,
  47624. 0x71, 0x26, 0x6c, 0xff, 0xd1, 0x9e, 0x44, 0x86, 0x2a, 0xae, 0xba, 0x43,
  47625. 0x00, 0x13, 0x7e, 0x50, 0xdd, 0x3e, 0x27, 0x39, 0x70, 0x1c, 0x0c, 0x0b,
  47626. 0xe8, 0xa2, 0xae, 0x03, 0x09, 0x2e, 0xd8, 0x71, 0xee, 0x7b, 0x1a, 0x09,
  47627. 0x2d, 0xe1, 0xd5, 0xde, 0xf5, 0xa3, 0x36, 0x77, 0x90, 0x97, 0x99, 0xd7,
  47628. 0x6c, 0xb7, 0x5c, 0x9d, 0xf7, 0x7e, 0x41, 0x89, 0xfe, 0xe4, 0x08, 0xc6,
  47629. 0x0b, 0xe4, 0x9b, 0x5f, 0x51, 0xa6, 0x08, 0xb8, 0x99, 0x81, 0xe9, 0xce,
  47630. 0xb4, 0x2d, 0xb2, 0x92, 0x9f, 0xe5, 0x1a, 0x98, 0x76, 0x20, 0x70, 0x54,
  47631. 0x93, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x3b, 0x30, 0x39, 0x30, 0x18,
  47632. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x11, 0x30, 0x0f, 0x82, 0x0d, 0x2a,
  47633. 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
  47634. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x60,
  47635. 0xd4, 0x26, 0xbb, 0xcc, 0x7c, 0x29, 0xa2, 0x88, 0x3c, 0x76, 0x7d, 0xb4,
  47636. 0x86, 0x8b, 0x47, 0x64, 0x5b, 0x87, 0xe0, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  47637. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
  47638. 0x01, 0x01, 0x00, 0xc3, 0x0d, 0x03, 0x67, 0xbb, 0x47, 0x8b, 0xf3, 0x20,
  47639. 0xdc, 0x7d, 0x2e, 0xe1, 0xd9, 0xf0, 0x01, 0xc4, 0x66, 0xc2, 0xe1, 0xcd,
  47640. 0xc3, 0x4a, 0x72, 0xf0, 0x6e, 0x38, 0xcf, 0x63, 0x01, 0x96, 0x9e, 0x84,
  47641. 0xb9, 0xce, 0x1d, 0xba, 0x4b, 0xe0, 0x70, 0x86, 0x2b, 0x5a, 0xab, 0xec,
  47642. 0xbf, 0xc2, 0xaa, 0x64, 0xa2, 0x6c, 0xd2, 0x42, 0x52, 0xd4, 0xbe, 0x8a,
  47643. 0xca, 0x9c, 0x03, 0xf3, 0xd6, 0x5f, 0xcd, 0x23, 0x9f, 0xf5, 0xa9, 0x04,
  47644. 0x40, 0x5b, 0x66, 0x78, 0xc0, 0xac, 0xa1, 0xdb, 0x5d, 0xd1, 0x94, 0xfc,
  47645. 0x47, 0x94, 0xf5, 0x45, 0xe3, 0x70, 0x13, 0x3f, 0x66, 0x6d, 0xdd, 0x73,
  47646. 0x68, 0x68, 0xe2, 0xd2, 0x89, 0xcb, 0x7f, 0xc6, 0xca, 0xd6, 0x96, 0x0b,
  47647. 0xcc, 0xdd, 0xa1, 0x74, 0xda, 0x33, 0xe8, 0x9e, 0xda, 0xb7, 0xd9, 0x12,
  47648. 0xab, 0x85, 0x9d, 0x0c, 0xde, 0xa0, 0x7d, 0x7e, 0xa1, 0x91, 0xed, 0xe5,
  47649. 0x32, 0x7c, 0xc5, 0xea, 0x1d, 0x4a, 0xb5, 0x38, 0x63, 0x17, 0xf3, 0x4f,
  47650. 0x2c, 0x4a, 0x58, 0x86, 0x09, 0x33, 0x86, 0xc4, 0xe7, 0x56, 0x6f, 0x32,
  47651. 0x71, 0xb7, 0xd0, 0x83, 0x12, 0x9e, 0x26, 0x0a, 0x3a, 0x45, 0xcb, 0xd7,
  47652. 0x4e, 0xab, 0xa4, 0xc3, 0xee, 0x4c, 0xc0, 0x38, 0xa1, 0xfa, 0xba, 0xfa,
  47653. 0xb7, 0x80, 0x69, 0x67, 0xa3, 0xef, 0x89, 0xba, 0xce, 0x89, 0x91, 0x3d,
  47654. 0x6a, 0x76, 0xe9, 0x3b, 0x32, 0x86, 0x76, 0x85, 0x6b, 0x4f, 0x7f, 0xbc,
  47655. 0x7a, 0x5b, 0x31, 0x92, 0x79, 0x35, 0xf8, 0xb9, 0xb1, 0xd7, 0xdb, 0xa9,
  47656. 0x6a, 0x8a, 0x91, 0x60, 0x65, 0xd4, 0x76, 0x54, 0x55, 0x57, 0xb9, 0x35,
  47657. 0xe0, 0xf5, 0xbb, 0x8f, 0xd4, 0x40, 0x75, 0xbb, 0x47, 0xa8, 0xf9, 0x0f,
  47658. 0xea, 0xc9, 0x6e, 0x84, 0xd5, 0xf5, 0x58, 0x2d, 0xe5, 0x76, 0x7b, 0xdf,
  47659. 0x97, 0x05, 0x5e, 0xaf, 0x50, 0xf5, 0x48
  47660. };
  47661. WOLFSSL_X509* x509 = NULL;
  47662. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  47663. const char *name1 = "foo.example.com";
  47664. int nameLen1 = (int)(XSTRLEN(name1));
  47665. const char *name2 = "x.y.example.com";
  47666. int nameLen2 = (int)(XSTRLEN(name2));
  47667. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  47668. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  47669. /* Ensure that "*.example.com" matches "foo.example.com" */
  47670. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  47671. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47672. /* Ensure that "*.example.com" does NOT match "x.y.example.com" */
  47673. ExpectIntNE(wolfSSL_X509_check_host(x509, name2, nameLen2,
  47674. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  47675. wolfSSL_X509_free(x509);
  47676. #endif
  47677. return EXPECT_RESULT();
  47678. }
  47679. static int test_wolfSSL_X509_max_altnames(void)
  47680. {
  47681. EXPECT_DECLS;
  47682. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  47683. /* Only test if max alt names has not been modified */
  47684. #if WOLFSSL_MAX_ALT_NAMES <= 1024
  47685. WOLFSSL_CTX* ctx = NULL;
  47686. /* File contains a certificate encoded with 130 subject alternative names */
  47687. const char* over_max_altnames_cert = \
  47688. "./certs/test/cert-over-max-altnames.pem";
  47689. #ifndef NO_WOLFSSL_SERVER
  47690. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  47691. #else
  47692. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  47693. #endif
  47694. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx,
  47695. over_max_altnames_cert, NULL, WOLFSSL_LOAD_FLAG_NONE),
  47696. WOLFSSL_SUCCESS);
  47697. wolfSSL_CTX_free(ctx);
  47698. #endif
  47699. #endif
  47700. return EXPECT_RESULT();
  47701. }
  47702. static int test_wolfSSL_X509_max_name_constraints(void)
  47703. {
  47704. EXPECT_DECLS;
  47705. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  47706. !defined(IGNORE_NAME_CONSTRAINTS)
  47707. /* Only test if max name constraints has not been modified */
  47708. #if WOLFSSL_MAX_NAME_CONSTRAINTS == 128
  47709. WOLFSSL_CTX* ctx = NULL;
  47710. /* File contains a certificate with 130 name constraints */
  47711. const char* over_max_nc = "./certs/test/cert-over-max-nc.pem";
  47712. #ifndef NO_WOLFSSL_SERVER
  47713. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  47714. #else
  47715. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  47716. #endif
  47717. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, over_max_nc,
  47718. NULL, WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  47719. wolfSSL_CTX_free(ctx);
  47720. #endif
  47721. #endif
  47722. return EXPECT_RESULT();
  47723. }
  47724. static int test_wolfSSL_X509(void)
  47725. {
  47726. EXPECT_DECLS;
  47727. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  47728. !defined(NO_RSA)
  47729. X509* x509 = NULL;
  47730. #ifndef NO_BIO
  47731. BIO* bio = NULL;
  47732. X509_STORE_CTX* ctx = NULL;
  47733. X509_STORE* store = NULL;
  47734. #endif
  47735. char der[] = "certs/ca-cert.der";
  47736. XFILE fp = XBADFILE;
  47737. ExpectNotNull(x509 = X509_new());
  47738. X509_free(x509);
  47739. x509 = NULL;
  47740. #ifndef NO_BIO
  47741. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  47742. SSL_FILETYPE_PEM));
  47743. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47744. #ifdef WOLFSSL_CERT_GEN
  47745. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  47746. #endif
  47747. ExpectNotNull(ctx = X509_STORE_CTX_new());
  47748. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  47749. ExpectNotNull(store = X509_STORE_new());
  47750. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  47751. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  47752. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  47753. X509_STORE_CTX_free(ctx);
  47754. X509_STORE_free(store);
  47755. X509_free(x509);
  47756. x509 = NULL;
  47757. BIO_free(bio);
  47758. #endif
  47759. /** d2i_X509_fp test **/
  47760. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  47761. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  47762. ExpectNotNull(x509);
  47763. X509_free(x509);
  47764. x509 = NULL;
  47765. if (fp != XBADFILE) {
  47766. XFCLOSE(fp);
  47767. fp = XBADFILE;
  47768. }
  47769. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  47770. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  47771. ExpectNotNull(x509);
  47772. X509_free(x509);
  47773. if (fp != XBADFILE)
  47774. XFCLOSE(fp);
  47775. /* X509_up_ref test */
  47776. ExpectIntEQ(X509_up_ref(NULL), 0);
  47777. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  47778. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  47779. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  47780. X509_free(x509); /* refCount = 2 */
  47781. X509_free(x509); /* refCount = 1 */
  47782. X509_free(x509); /* refCount = 0, free */
  47783. #endif
  47784. return EXPECT_RESULT();
  47785. }
  47786. static int test_wolfSSL_X509_get_ext_count(void)
  47787. {
  47788. EXPECT_DECLS;
  47789. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  47790. !defined(NO_RSA)
  47791. int ret = 0;
  47792. WOLFSSL_X509* x509 = NULL;
  47793. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  47794. XFILE f = XBADFILE;
  47795. /* NULL parameter check */
  47796. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  47797. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  47798. SSL_FILETYPE_PEM));
  47799. ExpectIntEQ(X509_get_ext_count(x509), 5);
  47800. wolfSSL_X509_free(x509);
  47801. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  47802. SSL_FILETYPE_PEM));
  47803. ExpectIntEQ(X509_get_ext_count(x509), 5);
  47804. wolfSSL_X509_free(x509);
  47805. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47806. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47807. if (f != XBADFILE)
  47808. XFCLOSE(f);
  47809. /* wolfSSL_X509_get_ext_count() valid input */
  47810. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  47811. /* wolfSSL_X509_get_ext_count() NULL argument */
  47812. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  47813. wolfSSL_X509_free(x509);
  47814. #endif
  47815. return EXPECT_RESULT();
  47816. }
  47817. static int test_wolfSSL_X509_sign2(void)
  47818. {
  47819. EXPECT_DECLS;
  47820. /* test requires WOLFSSL_AKID_NAME to match expected output */
  47821. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  47822. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  47823. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  47824. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  47825. defined(WOLFSSL_IP_ALT_NAME))
  47826. WOLFSSL_X509 *x509 = NULL;
  47827. WOLFSSL_X509 *ca = NULL;
  47828. const unsigned char *der = NULL;
  47829. const unsigned char *pt = NULL;
  47830. WOLFSSL_EVP_PKEY *priv = NULL;
  47831. WOLFSSL_X509_NAME *name = NULL;
  47832. int derSz;
  47833. #ifndef NO_ASN_TIME
  47834. WOLFSSL_ASN1_TIME *notBefore = NULL;
  47835. WOLFSSL_ASN1_TIME *notAfter = NULL;
  47836. const int year = 365*24*60*60;
  47837. const int day = 24*60*60;
  47838. const int hour = 60*60;
  47839. const int mini = 60;
  47840. time_t t;
  47841. #endif
  47842. const unsigned char expected[] = {
  47843. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  47844. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  47845. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  47846. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  47847. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  47848. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  47849. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  47850. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  47851. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  47852. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  47853. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  47854. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  47855. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  47856. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  47857. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  47858. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  47859. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  47860. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  47861. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  47862. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  47863. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  47864. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  47865. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  47866. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  47867. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  47868. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  47869. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  47870. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  47871. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  47872. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  47873. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  47874. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  47875. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  47876. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  47877. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  47878. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  47879. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  47880. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  47881. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  47882. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  47883. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  47884. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  47885. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  47886. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  47887. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  47888. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  47889. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  47890. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  47891. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  47892. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  47893. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  47894. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  47895. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  47896. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  47897. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  47898. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  47899. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  47900. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  47901. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  47902. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  47903. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  47904. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  47905. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  47906. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  47907. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  47908. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  47909. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  47910. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  47911. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  47912. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  47913. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  47914. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  47915. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  47916. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  47917. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  47918. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  47919. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  47920. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  47921. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  47922. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  47923. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  47924. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  47925. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  47926. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  47927. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  47928. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  47929. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  47930. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  47931. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  47932. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  47933. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  47934. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  47935. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  47936. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  47937. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  47938. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  47939. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  47940. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  47941. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  47942. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  47943. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  47944. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  47945. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  47946. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  47947. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  47948. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  47949. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  47950. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  47951. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  47952. };
  47953. pt = ca_key_der_2048;
  47954. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  47955. sizeof_ca_key_der_2048));
  47956. pt = client_cert_der_2048;
  47957. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  47958. sizeof_client_cert_der_2048));
  47959. pt = ca_cert_der_2048;
  47960. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  47961. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  47962. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  47963. #ifndef NO_ASN_TIME
  47964. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  47965. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  47966. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  47967. ExpectIntEQ(notAfter->length, 13);
  47968. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  47969. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  47970. #endif
  47971. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  47972. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  47973. ExpectIntEQ(derSz, sizeof(expected));
  47974. #ifndef NO_ASN_TIME
  47975. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  47976. #endif
  47977. wolfSSL_X509_free(ca);
  47978. wolfSSL_X509_free(x509);
  47979. wolfSSL_EVP_PKEY_free(priv);
  47980. #ifndef NO_ASN_TIME
  47981. wolfSSL_ASN1_TIME_free(notBefore);
  47982. wolfSSL_ASN1_TIME_free(notAfter);
  47983. #endif
  47984. #endif
  47985. return EXPECT_RESULT();
  47986. }
  47987. static int test_wolfSSL_X509_sign(void)
  47988. {
  47989. EXPECT_DECLS;
  47990. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  47991. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  47992. int ret = 0;
  47993. char *cn = NULL;
  47994. word32 cnSz = 0;
  47995. X509_NAME *name = NULL;
  47996. X509 *x509 = NULL;
  47997. X509 *ca = NULL;
  47998. DecodedCert dCert;
  47999. EVP_PKEY *pub = NULL;
  48000. EVP_PKEY *priv = NULL;
  48001. EVP_MD_CTX *mctx = NULL;
  48002. #if defined(USE_CERT_BUFFERS_1024)
  48003. const unsigned char* rsaPriv = client_key_der_1024;
  48004. const unsigned char* rsaPub = client_keypub_der_1024;
  48005. const unsigned char* certIssuer = client_cert_der_1024;
  48006. long clientKeySz = (long)sizeof_client_key_der_1024;
  48007. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  48008. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  48009. #elif defined(USE_CERT_BUFFERS_2048)
  48010. const unsigned char* rsaPriv = client_key_der_2048;
  48011. const unsigned char* rsaPub = client_keypub_der_2048;
  48012. const unsigned char* certIssuer = client_cert_der_2048;
  48013. long clientKeySz = (long)sizeof_client_key_der_2048;
  48014. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  48015. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  48016. #endif
  48017. byte sn[16];
  48018. int snSz = sizeof(sn);
  48019. /* Set X509_NAME fields */
  48020. ExpectNotNull(name = X509_NAME_new());
  48021. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  48022. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  48023. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  48024. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  48025. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  48026. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  48027. /* Get private and public keys */
  48028. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  48029. clientKeySz));
  48030. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  48031. ExpectNotNull(x509 = X509_new());
  48032. /* Set version 3 */
  48033. ExpectIntNE(X509_set_version(x509, 2L), 0);
  48034. /* Set subject name, add pubkey, and sign certificate */
  48035. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  48036. X509_NAME_free(name);
  48037. name = NULL;
  48038. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  48039. #ifdef WOLFSSL_ALT_NAMES
  48040. /* Add some subject alt names */
  48041. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  48042. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  48043. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  48044. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  48045. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  48046. "sphygmomanometer",
  48047. ASN_DNS_TYPE), SSL_SUCCESS);
  48048. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  48049. "supercalifragilisticexpialidocious",
  48050. ASN_DNS_TYPE), SSL_SUCCESS);
  48051. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  48052. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  48053. ASN_DNS_TYPE), SSL_SUCCESS);
  48054. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  48055. {
  48056. unsigned char ip4_type[] = {127,128,0,255};
  48057. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  48058. 0xff, 0xee, 0x99, 0x88,
  48059. 0x77, 0x66, 0x55, 0x44,
  48060. 0x00, 0x33, 0x22, 0x11};
  48061. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  48062. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  48063. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  48064. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  48065. }
  48066. #endif
  48067. #endif /* WOLFSSL_ALT_NAMES */
  48068. {
  48069. ASN1_UTCTIME* infinite_past = NULL;
  48070. ExpectNotNull(infinite_past = ASN1_UTCTIME_set(NULL, 0));
  48071. ExpectIntEQ(X509_set1_notBefore(x509, infinite_past), 1);
  48072. ASN1_UTCTIME_free(infinite_past);
  48073. }
  48074. /* test valid sign case */
  48075. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  48076. /* test valid X509_sign_ctx case */
  48077. ExpectNotNull(mctx = EVP_MD_CTX_new());
  48078. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  48079. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  48080. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  48081. ExpectIntEQ(X509_get_ext_count(x509), 1);
  48082. #endif
  48083. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  48084. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  48085. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  48086. #endif
  48087. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  48088. WOLFSSL_SUCCESS);
  48089. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  48090. /* Variation in size depends on ASN.1 encoding when MSB is set.
  48091. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  48092. * with the MSB set. See GenerateInteger in asn.c */
  48093. #ifndef USE_CERT_BUFFERS_1024
  48094. #ifndef WOLFSSL_ALT_NAMES
  48095. /* Valid case - size should be 781-786 with 16 byte serial number */
  48096. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  48097. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  48098. /* Valid case - size should be 955-960 with 16 byte serial number */
  48099. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  48100. #else
  48101. /* Valid case - size should be 926-931 with 16 byte serial number */
  48102. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  48103. #endif
  48104. #else
  48105. #ifndef WOLFSSL_ALT_NAMES
  48106. /* Valid case - size should be 537-542 with 16 byte serial number */
  48107. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  48108. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  48109. /* Valid case - size should be 695-670 with 16 byte serial number */
  48110. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  48111. #else
  48112. /* Valid case - size should be 666-671 with 16 byte serial number */
  48113. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  48114. #endif
  48115. #endif
  48116. /* check that issuer name is as expected after signature */
  48117. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  48118. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  48119. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  48120. ExpectNotNull(name = X509_get_subject_name(ca));
  48121. cnSz = X509_NAME_get_sz(name);
  48122. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  48123. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  48124. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  48125. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  48126. cn = NULL;
  48127. #ifdef WOLFSSL_MULTI_ATTRIB
  48128. /* test adding multiple OU's to the signer */
  48129. ExpectNotNull(name = X509_get_subject_name(ca));
  48130. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  48131. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  48132. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  48133. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  48134. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  48135. #endif
  48136. ExpectNotNull(name = X509_get_subject_name(ca));
  48137. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  48138. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  48139. ExpectNotNull(name = X509_get_issuer_name(x509));
  48140. cnSz = X509_NAME_get_sz(name);
  48141. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  48142. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  48143. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  48144. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  48145. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  48146. cn = NULL;
  48147. FreeDecodedCert(&dCert);
  48148. /* Test invalid parameters */
  48149. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  48150. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  48151. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  48152. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  48153. EVP_MD_CTX_free(mctx);
  48154. mctx = NULL;
  48155. ExpectNotNull(mctx = EVP_MD_CTX_new());
  48156. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  48157. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  48158. /* test invalid version number */
  48159. #if defined(OPENSSL_ALL)
  48160. ExpectIntNE(X509_set_version(x509, 6L), 0);
  48161. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  48162. /* uses ParseCert which fails on bad version number */
  48163. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  48164. #endif
  48165. EVP_MD_CTX_free(mctx);
  48166. EVP_PKEY_free(priv);
  48167. EVP_PKEY_free(pub);
  48168. X509_free(x509);
  48169. X509_free(ca);
  48170. #endif
  48171. return EXPECT_RESULT();
  48172. }
  48173. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  48174. {
  48175. EXPECT_DECLS;
  48176. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  48177. X509* x509 = NULL;
  48178. const X509_ALGOR* alg;
  48179. ExpectNotNull(x509 = X509_new());
  48180. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  48181. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  48182. X509_free(x509);
  48183. #endif
  48184. return EXPECT_RESULT();
  48185. }
  48186. static int test_wolfSSL_X509_ALGOR_get0(void)
  48187. {
  48188. EXPECT_DECLS;
  48189. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  48190. !defined(NO_SHA256) && !defined(NO_RSA)
  48191. X509* x509 = NULL;
  48192. const ASN1_OBJECT* obj = NULL;
  48193. const X509_ALGOR* alg = NULL;
  48194. int pptype = 0;
  48195. const void *ppval = NULL;
  48196. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  48197. SSL_FILETYPE_PEM));
  48198. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  48199. /* Invalid case */
  48200. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  48201. ExpectNull(obj);
  48202. /* Valid case */
  48203. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  48204. ExpectNotNull(obj);
  48205. ExpectNull(ppval);
  48206. ExpectIntNE(pptype, 0);
  48207. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  48208. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  48209. X509_free(x509);
  48210. #endif
  48211. return EXPECT_RESULT();
  48212. }
  48213. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  48214. {
  48215. EXPECT_DECLS;
  48216. #if defined(OPENSSL_EXTRA)
  48217. X509_VERIFY_PARAM *paramTo = NULL;
  48218. X509_VERIFY_PARAM *paramFrom = NULL;
  48219. char testIPv4[] = "127.0.0.1";
  48220. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  48221. char testhostName1[] = "foo.hoge.com";
  48222. char testhostName2[] = "foobar.hoge.com";
  48223. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  48224. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  48225. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  48226. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  48227. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  48228. (int)XSTRLEN(testhostName1)), 1);
  48229. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  48230. (int)XSTRLEN(testhostName1)));
  48231. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  48232. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  48233. ExpectIntEQ(0x01, paramFrom->hostFlags);
  48234. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  48235. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  48236. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  48237. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  48238. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  48239. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  48240. /* null pointer */
  48241. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  48242. /* in the case of "from" null, returns success */
  48243. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  48244. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  48245. /* inherit flags test : VPARAM_DEFAULT */
  48246. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  48247. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  48248. (int)XSTRLEN(testhostName1)));
  48249. ExpectIntEQ(0x01, paramTo->hostFlags);
  48250. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  48251. /* inherit flags test : VPARAM OVERWRITE */
  48252. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  48253. (int)XSTRLEN(testhostName2)), 1);
  48254. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  48255. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  48256. if (paramTo != NULL) {
  48257. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  48258. }
  48259. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  48260. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  48261. (int)XSTRLEN(testhostName1)));
  48262. ExpectIntEQ(0x01, paramTo->hostFlags);
  48263. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  48264. /* inherit flags test : VPARAM_RESET_FLAGS */
  48265. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  48266. (int)XSTRLEN(testhostName2)), 1);
  48267. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  48268. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  48269. if (paramTo != NULL) {
  48270. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  48271. }
  48272. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  48273. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  48274. (int)XSTRLEN(testhostName1)));
  48275. ExpectIntEQ(0x01, paramTo->hostFlags);
  48276. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  48277. /* inherit flags test : VPARAM_LOCKED */
  48278. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  48279. (int)XSTRLEN(testhostName2)), 1);
  48280. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  48281. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  48282. if (paramTo != NULL) {
  48283. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  48284. }
  48285. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  48286. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  48287. (int)XSTRLEN(testhostName2)));
  48288. ExpectIntEQ(0x00, paramTo->hostFlags);
  48289. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  48290. /* test for incorrect parameters */
  48291. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  48292. 0);
  48293. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  48294. /* inherit flags test : VPARAM_ONCE, not testable yet */
  48295. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  48296. 1);
  48297. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  48298. X509_V_FLAG_CRL_CHECK_ALL);
  48299. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  48300. X509_V_FLAG_CRL_CHECK_ALL), 1);
  48301. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  48302. X509_VERIFY_PARAM_free(paramTo);
  48303. X509_VERIFY_PARAM_free(paramFrom);
  48304. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  48305. #endif
  48306. return EXPECT_RESULT();
  48307. }
  48308. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  48309. !defined(WOLFSSL_HOSTNAME_VERIFY_ALT_NAME_ONLY)
  48310. static int test_wolfSSL_check_domain_verify_count = 0;
  48311. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  48312. WOLFSSL_X509_STORE_CTX* store)
  48313. {
  48314. EXPECT_DECLS;
  48315. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  48316. ExpectIntEQ(preverify, 1);
  48317. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  48318. return EXPECT_SUCCESS();
  48319. }
  48320. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  48321. {
  48322. EXPECT_DECLS;
  48323. X509_VERIFY_PARAM *param = NULL;
  48324. ExpectNotNull(param = SSL_get0_param(ssl));
  48325. /* Domain check should only be done on the leaf cert */
  48326. X509_VERIFY_PARAM_set_hostflags(param,
  48327. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  48328. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  48329. "wolfSSL Server Chain", 0), 1);
  48330. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  48331. test_wolfSSL_check_domain_verify_cb);
  48332. return EXPECT_RESULT();
  48333. }
  48334. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  48335. {
  48336. EXPECT_DECLS;
  48337. /* Use a cert with different domains in chain */
  48338. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  48339. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  48340. return EXPECT_RESULT();
  48341. }
  48342. static int test_wolfSSL_check_domain(void)
  48343. {
  48344. EXPECT_DECLS;
  48345. test_ssl_cbf func_cb_client;
  48346. test_ssl_cbf func_cb_server;
  48347. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  48348. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  48349. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  48350. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  48351. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  48352. &func_cb_server, NULL), TEST_SUCCESS);
  48353. /* Should have been called once for each cert in sent chain */
  48354. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  48355. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  48356. #else
  48357. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  48358. #endif
  48359. return EXPECT_RESULT();
  48360. }
  48361. #else
  48362. static int test_wolfSSL_check_domain(void)
  48363. {
  48364. EXPECT_DECLS;
  48365. return EXPECT_RESULT();
  48366. }
  48367. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  48368. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  48369. {
  48370. EXPECT_DECLS;
  48371. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  48372. X509* x509 = NULL;
  48373. X509_PUBKEY* pubKey;
  48374. ExpectNotNull(x509 = X509_new());
  48375. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  48376. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  48377. X509_free(x509);
  48378. #endif
  48379. return EXPECT_RESULT();
  48380. }
  48381. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  48382. {
  48383. EXPECT_DECLS;
  48384. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  48385. !defined(NO_SHA256) && !defined(NO_RSA)
  48386. X509* x509 = NULL;
  48387. ASN1_OBJECT* obj = NULL;
  48388. const ASN1_OBJECT* pa_oid = NULL;
  48389. X509_PUBKEY* pubKey = NULL;
  48390. X509_PUBKEY* pubKey2 = NULL;
  48391. EVP_PKEY* evpKey = NULL;
  48392. const unsigned char *pk = NULL;
  48393. int ppklen;
  48394. int pptype;
  48395. X509_ALGOR *pa = NULL;
  48396. const void *pval;
  48397. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  48398. SSL_FILETYPE_PEM));
  48399. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  48400. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  48401. ExpectNotNull(pk);
  48402. ExpectNotNull(pa);
  48403. ExpectNotNull(pubKey);
  48404. ExpectIntGT(ppklen, 0);
  48405. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  48406. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  48407. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  48408. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  48409. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  48410. ExpectNotNull(pk);
  48411. ExpectNotNull(pa);
  48412. ExpectIntGT(ppklen, 0);
  48413. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  48414. ExpectNotNull(pa_oid);
  48415. ExpectNull(pval);
  48416. ExpectIntEQ(pptype, V_ASN1_NULL);
  48417. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  48418. X509_PUBKEY_free(pubKey2);
  48419. X509_free(x509);
  48420. EVP_PKEY_free(evpKey);
  48421. #endif
  48422. return EXPECT_RESULT();
  48423. }
  48424. static int test_wolfSSL_X509_PUBKEY_EC(void)
  48425. {
  48426. EXPECT_DECLS;
  48427. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  48428. X509* x509 = NULL;
  48429. ASN1_OBJECT* obj = NULL;
  48430. ASN1_OBJECT* poid = NULL;
  48431. const ASN1_OBJECT* pa_oid = NULL;
  48432. X509_PUBKEY* pubKey = NULL;
  48433. X509_PUBKEY* pubKey2 = NULL;
  48434. EVP_PKEY* evpKey = NULL;
  48435. const unsigned char *pk = NULL;
  48436. int ppklen;
  48437. int pptype;
  48438. X509_ALGOR *pa = NULL;
  48439. const void *pval;
  48440. char buf[50];
  48441. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  48442. SSL_FILETYPE_PEM));
  48443. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  48444. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  48445. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  48446. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  48447. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  48448. ExpectNotNull(pk);
  48449. ExpectNotNull(pa);
  48450. ExpectIntGT(ppklen, 0);
  48451. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  48452. ExpectNotNull(pa_oid);
  48453. ExpectNotNull(pval);
  48454. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  48455. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  48456. poid = (ASN1_OBJECT *)pval;
  48457. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  48458. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  48459. X509_PUBKEY_free(pubKey2);
  48460. X509_free(x509);
  48461. EVP_PKEY_free(evpKey);
  48462. #endif
  48463. return EXPECT_RESULT();
  48464. }
  48465. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  48466. {
  48467. EXPECT_DECLS;
  48468. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  48469. word32 bytes;
  48470. #ifdef USE_CERT_BUFFERS_1024
  48471. byte tmp[ONEK_BUF];
  48472. #elif defined(USE_CERT_BUFFERS_2048)
  48473. byte tmp[TWOK_BUF];
  48474. #else
  48475. byte tmp[TWOK_BUF];
  48476. #endif /* END USE_CERT_BUFFERS_1024 */
  48477. const unsigned char* dsaKeyDer = tmp;
  48478. ASN1_OBJECT* obj = NULL;
  48479. ASN1_STRING* str;
  48480. const ASN1_OBJECT* pa_oid = NULL;
  48481. X509_PUBKEY* pubKey = NULL;
  48482. EVP_PKEY* evpKey = NULL;
  48483. const unsigned char *pk = NULL;
  48484. int ppklen, pptype;
  48485. X509_ALGOR *pa = NULL;
  48486. const void *pval;
  48487. #ifdef USE_CERT_BUFFERS_1024
  48488. XMEMSET(tmp, 0, sizeof(tmp));
  48489. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  48490. bytes = sizeof_dsa_key_der_1024;
  48491. #elif defined(USE_CERT_BUFFERS_2048)
  48492. XMEMSET(tmp, 0, sizeof(tmp));
  48493. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  48494. bytes = sizeof_dsa_key_der_2048;
  48495. #else
  48496. {
  48497. XFILE fp = XBADFILE;
  48498. XMEMSET(tmp, 0, sizeof(tmp));
  48499. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  48500. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  48501. if (fp != XBADFILE)
  48502. XFCLOSE(fp);
  48503. }
  48504. #endif
  48505. /* Initialize pkey with der format dsa key */
  48506. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  48507. ExpectNotNull(pubKey = X509_PUBKEY_new());
  48508. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  48509. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  48510. ExpectNotNull(pk);
  48511. ExpectNotNull(pa);
  48512. ExpectIntGT(ppklen, 0);
  48513. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  48514. ExpectNotNull(pa_oid);
  48515. ExpectNotNull(pval);
  48516. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  48517. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  48518. str = (ASN1_STRING *)pval;
  48519. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  48520. #ifdef USE_CERT_BUFFERS_1024
  48521. ExpectIntEQ(ASN1_STRING_length(str), 291);
  48522. #else
  48523. ExpectIntEQ(ASN1_STRING_length(str), 549);
  48524. #endif /* END USE_CERT_BUFFERS_1024 */
  48525. X509_PUBKEY_free(pubKey);
  48526. EVP_PKEY_free(evpKey);
  48527. #endif
  48528. return EXPECT_RESULT();
  48529. }
  48530. static int test_wolfSSL_BUF(void)
  48531. {
  48532. EXPECT_DECLS;
  48533. #if defined(OPENSSL_EXTRA)
  48534. BUF_MEM* buf = NULL;
  48535. ExpectNotNull(buf = BUF_MEM_new());
  48536. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  48537. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  48538. BUF_MEM_free(buf);
  48539. #endif
  48540. return EXPECT_RESULT();
  48541. }
  48542. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  48543. static int stub_rand_seed(const void *buf, int num)
  48544. {
  48545. (void)buf;
  48546. (void)num;
  48547. return 123;
  48548. }
  48549. static int stub_rand_bytes(unsigned char *buf, int num)
  48550. {
  48551. (void)buf;
  48552. (void)num;
  48553. return 456;
  48554. }
  48555. static byte* was_stub_rand_cleanup_called(void)
  48556. {
  48557. static byte was_called = 0;
  48558. return &was_called;
  48559. }
  48560. static void stub_rand_cleanup(void)
  48561. {
  48562. byte* was_called = was_stub_rand_cleanup_called();
  48563. *was_called = 1;
  48564. return;
  48565. }
  48566. static byte* was_stub_rand_add_called(void)
  48567. {
  48568. static byte was_called = 0;
  48569. return &was_called;
  48570. }
  48571. static int stub_rand_add(const void *buf, int num, double entropy)
  48572. {
  48573. byte* was_called = was_stub_rand_add_called();
  48574. (void)buf;
  48575. (void)num;
  48576. (void)entropy;
  48577. *was_called = 1;
  48578. return 0;
  48579. }
  48580. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  48581. {
  48582. (void)buf;
  48583. (void)num;
  48584. return 9876;
  48585. }
  48586. static int stub_rand_status(void)
  48587. {
  48588. return 5432;
  48589. }
  48590. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  48591. static int test_wolfSSL_RAND_set_rand_method(void)
  48592. {
  48593. EXPECT_DECLS;
  48594. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  48595. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  48596. unsigned char* buf = NULL;
  48597. int num = 0;
  48598. double entropy = 0;
  48599. int ret;
  48600. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  48601. byte* was_add_called = was_stub_rand_add_called();
  48602. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  48603. DYNAMIC_TYPE_TMP_BUFFER));
  48604. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  48605. ExpectIntEQ(*was_cleanup_called, 0);
  48606. RAND_cleanup();
  48607. ExpectIntEQ(*was_cleanup_called, 0);
  48608. rand_methods.seed = &stub_rand_seed;
  48609. rand_methods.bytes = &stub_rand_bytes;
  48610. rand_methods.cleanup = &stub_rand_cleanup;
  48611. rand_methods.add = &stub_rand_add;
  48612. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  48613. rand_methods.status = &stub_rand_status;
  48614. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  48615. ExpectIntEQ(RAND_seed(buf, num), 123);
  48616. ExpectIntEQ(RAND_bytes(buf, num), 456);
  48617. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  48618. ExpectIntEQ(RAND_status(), 5432);
  48619. ExpectIntEQ(*was_add_called, 0);
  48620. /* The function pointer for RAND_add returns int, but RAND_add itself
  48621. * returns void. */
  48622. RAND_add(buf, num, entropy);
  48623. ExpectIntEQ(*was_add_called, 1);
  48624. was_add_called = 0;
  48625. ExpectIntEQ(*was_cleanup_called, 0);
  48626. RAND_cleanup();
  48627. ExpectIntEQ(*was_cleanup_called, 1);
  48628. *was_cleanup_called = 0;
  48629. ret = RAND_set_rand_method(NULL);
  48630. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48631. ExpectIntNE(RAND_status(), 5432);
  48632. ExpectIntEQ(*was_cleanup_called, 0);
  48633. RAND_cleanup();
  48634. ExpectIntEQ(*was_cleanup_called, 0);
  48635. RAND_set_rand_method(NULL);
  48636. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48637. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  48638. return EXPECT_RESULT();
  48639. }
  48640. static int test_wolfSSL_RAND_bytes(void)
  48641. {
  48642. EXPECT_DECLS;
  48643. #if defined(OPENSSL_EXTRA)
  48644. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  48645. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  48646. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  48647. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  48648. int max_bufsize;
  48649. byte *my_buf = NULL;
  48650. /* sanity check */
  48651. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  48652. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  48653. max_bufsize = size4;
  48654. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  48655. DYNAMIC_TYPE_TMP_BUFFER));
  48656. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  48657. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  48658. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  48659. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  48660. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  48661. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  48662. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  48663. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48664. #endif
  48665. return EXPECT_RESULT();
  48666. }
  48667. static int test_wolfSSL_RAND(void)
  48668. {
  48669. EXPECT_DECLS;
  48670. #if defined(OPENSSL_EXTRA)
  48671. byte seed[16];
  48672. XMEMSET(seed, 0, sizeof(seed));
  48673. /* No global methods set. */
  48674. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  48675. ExpectIntEQ(RAND_poll(), 1);
  48676. RAND_cleanup();
  48677. ExpectIntEQ(RAND_egd(NULL), -1);
  48678. #ifndef NO_FILESYSTEM
  48679. {
  48680. char fname[100];
  48681. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  48682. ExpectIntEQ(RAND_write_file(NULL), 0);
  48683. }
  48684. #endif
  48685. #endif
  48686. return EXPECT_RESULT();
  48687. }
  48688. static int test_wolfSSL_PKCS8_Compat(void)
  48689. {
  48690. EXPECT_DECLS;
  48691. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  48692. !defined(NO_BIO)
  48693. PKCS8_PRIV_KEY_INFO* pt = NULL;
  48694. BIO* bio = NULL;
  48695. XFILE f = XBADFILE;
  48696. int bytes;
  48697. char pkcs8_buffer[512];
  48698. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  48699. EVP_PKEY *pkey = NULL;
  48700. #endif
  48701. /* file from wolfssl/certs/ directory */
  48702. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  48703. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  48704. 0);
  48705. if (f != XBADFILE)
  48706. XFCLOSE(f);
  48707. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  48708. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  48709. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  48710. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  48711. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  48712. /* gets PKCS8 pointer to pkey */
  48713. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  48714. EVP_PKEY_free(pkey);
  48715. #endif
  48716. BIO_free(bio);
  48717. PKCS8_PRIV_KEY_INFO_free(pt);
  48718. #endif
  48719. return EXPECT_RESULT();
  48720. }
  48721. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  48722. static int NoPasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  48723. {
  48724. (void)passwd;
  48725. (void)sz;
  48726. (void)rw;
  48727. (void)userdata;
  48728. return -1;
  48729. }
  48730. #endif
  48731. static int test_wolfSSL_PKCS8_d2i(void)
  48732. {
  48733. EXPECT_DECLS;
  48734. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  48735. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  48736. * requires a 12 byte password in FIPS mode. This test ends up
  48737. * trying to use an 8 byte password. */
  48738. #ifndef NO_FILESYSTEM
  48739. unsigned char pkcs8_buffer[2048];
  48740. const unsigned char* p = NULL;
  48741. int bytes = 0;
  48742. XFILE file = XBADFILE;
  48743. WOLFSSL_EVP_PKEY* pkey = NULL;
  48744. #ifndef NO_BIO
  48745. BIO* bio = NULL;
  48746. #if defined(OPENSSL_ALL) && \
  48747. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  48748. defined(HAVE_ECC)) && \
  48749. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  48750. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  48751. #endif
  48752. #endif
  48753. #ifndef NO_RSA
  48754. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  48755. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  48756. #ifndef NO_DES3
  48757. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  48758. #endif
  48759. #endif /* NO_RSA */
  48760. #ifdef HAVE_ECC
  48761. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  48762. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  48763. #ifndef NO_DES3
  48764. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  48765. #endif
  48766. #endif /* HAVE_ECC */
  48767. #endif /* !NO_FILESYSTEM */
  48768. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  48769. #ifndef NO_RSA
  48770. #ifdef USE_CERT_BUFFERS_1024
  48771. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  48772. int rsaSz = sizeof_server_key_der_1024;
  48773. #else
  48774. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  48775. int rsaSz = sizeof_server_key_der_2048;
  48776. #endif
  48777. #endif
  48778. #ifdef HAVE_ECC
  48779. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  48780. int ecSz = sizeof_ecc_key_der_256;
  48781. #endif
  48782. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  48783. #ifndef NO_FILESYSTEM
  48784. (void)pkcs8_buffer;
  48785. (void)p;
  48786. (void)bytes;
  48787. (void)file;
  48788. #ifndef NO_BIO
  48789. (void)bio;
  48790. #endif
  48791. #endif
  48792. #ifdef OPENSSL_ALL
  48793. #ifndef NO_RSA
  48794. /* Try to auto-detect normal RSA private key */
  48795. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  48796. EVP_PKEY_free(pkey);
  48797. pkey = NULL;
  48798. #endif
  48799. #ifdef HAVE_ECC
  48800. /* Try to auto-detect normal EC private key */
  48801. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  48802. EVP_PKEY_free(pkey);
  48803. pkey = NULL;
  48804. #endif
  48805. #endif /* OPENSSL_ALL */
  48806. #ifndef NO_FILESYSTEM
  48807. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  48808. ExpectIntEQ(PEM_write_PKCS8PrivateKey(XBADFILE, pkey, NULL, NULL, 0, NULL,
  48809. NULL), 0);
  48810. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  48811. NULL), 0);
  48812. #endif
  48813. #ifndef NO_RSA
  48814. /* Get DER encoded RSA PKCS#8 data. */
  48815. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  48816. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  48817. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  48818. file)), 0);
  48819. if (file != XBADFILE) {
  48820. XFCLOSE(file);
  48821. file = XBADFILE;
  48822. }
  48823. p = pkcs8_buffer;
  48824. #ifdef OPENSSL_ALL
  48825. /* Try to decode - auto-detect key type. */
  48826. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  48827. #else
  48828. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  48829. #endif
  48830. /* Get PEM encoded RSA PKCS#8 data. */
  48831. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  48832. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  48833. file)), 0);
  48834. if (file != XBADFILE) {
  48835. XFCLOSE(file);
  48836. file = XBADFILE;
  48837. }
  48838. #if defined(OPENSSL_ALL) && \
  48839. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  48840. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48841. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  48842. NULL), 0);
  48843. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  48844. NULL), 0);
  48845. /* Write PKCS#8 PEM to BIO. */
  48846. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  48847. NULL), bytes);
  48848. /* Write PKCS#8 PEM to stderr. */
  48849. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  48850. NULL), bytes);
  48851. /* Compare file and written data */
  48852. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  48853. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  48854. BIO_free(bio);
  48855. bio = NULL;
  48856. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  48857. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_gcm(),
  48858. NULL, 0, PasswordCallBack, (void*)"yassl123"), 0);
  48859. #endif
  48860. #if !defined(NO_DES3) && !defined(NO_SHA)
  48861. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48862. /* Write Encrypted PKCS#8 PEM to BIO. */
  48863. bytes = 1834;
  48864. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  48865. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  48866. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
  48867. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  48868. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  48869. (void*)"yassl123"));
  48870. EVP_PKEY_free(evpPkey);
  48871. evpPkey = NULL;
  48872. BIO_free(bio);
  48873. bio = NULL;
  48874. #endif /* !NO_DES3 && !NO_SHA */
  48875. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  48876. EVP_PKEY_free(pkey);
  48877. pkey = NULL;
  48878. /* PKCS#8 encrypted RSA key */
  48879. #ifndef NO_DES3
  48880. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  48881. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  48882. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  48883. file)), 0);
  48884. if (file != XBADFILE) {
  48885. XFCLOSE(file);
  48886. file = XBADFILE;
  48887. }
  48888. #if defined(OPENSSL_ALL) && \
  48889. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  48890. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  48891. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  48892. (void*)"yassl123"));
  48893. EVP_PKEY_free(pkey);
  48894. pkey = NULL;
  48895. BIO_free(bio);
  48896. bio = NULL;
  48897. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  48898. #endif /* !NO_DES3 */
  48899. #endif /* NO_RSA */
  48900. #ifdef HAVE_ECC
  48901. /* PKCS#8 encode EC key */
  48902. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  48903. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  48904. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  48905. file)), 0);
  48906. if (file != XBADFILE) {
  48907. XFCLOSE(file);
  48908. file = XBADFILE;
  48909. }
  48910. p = pkcs8_buffer;
  48911. #ifdef OPENSSL_ALL
  48912. /* Try to decode - auto-detect key type. */
  48913. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  48914. #else
  48915. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  48916. #endif
  48917. /* Get PEM encoded RSA PKCS#8 data. */
  48918. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  48919. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  48920. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  48921. file)), 0);
  48922. if (file != XBADFILE) {
  48923. XFCLOSE(file);
  48924. file = XBADFILE;
  48925. }
  48926. #if defined(OPENSSL_ALL) && \
  48927. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  48928. defined(HAVE_AES_CBC)
  48929. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48930. /* Write PKCS#8 PEM to BIO. */
  48931. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  48932. NULL), bytes);
  48933. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  48934. NULL), bytes);
  48935. /* Compare file and written data */
  48936. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  48937. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  48938. BIO_free(bio);
  48939. bio = NULL;
  48940. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48941. /* Write Encrypted PKCS#8 PEM to BIO. */
  48942. bytes = 379;
  48943. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  48944. NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
  48945. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  48946. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  48947. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  48948. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  48949. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  48950. (char*)"yassl123", 8, PasswordCallBack, NULL), bytes);
  48951. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_256_cbc(),
  48952. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  48953. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  48954. (void*)"yassl123"));
  48955. EVP_PKEY_free(evpPkey);
  48956. evpPkey = NULL;
  48957. BIO_free(bio);
  48958. bio = NULL;
  48959. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  48960. EVP_PKEY_free(pkey);
  48961. pkey = NULL;
  48962. /* PKCS#8 encrypted EC key */
  48963. #ifndef NO_DES3
  48964. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  48965. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  48966. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  48967. file)), 0);
  48968. if (file != XBADFILE) {
  48969. XFCLOSE(file);
  48970. file = XBADFILE;
  48971. }
  48972. #if defined(OPENSSL_ALL) && \
  48973. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  48974. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  48975. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  48976. (void*)"yassl123"));
  48977. EVP_PKEY_free(pkey);
  48978. pkey = NULL;
  48979. BIO_free(bio);
  48980. bio = NULL;
  48981. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  48982. #endif /* !NO_DES3 */
  48983. #endif /* HAVE_ECC */
  48984. #endif /* !NO_FILESYSTEM */
  48985. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  48986. return EXPECT_RESULT();
  48987. }
  48988. #if !defined(SINGLE_THREADED) && defined(ERROR_QUEUE_PER_THREAD) && \
  48989. !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  48990. defined(DEBUG_WOLFSSL)
  48991. #define LOGGING_THREADS 5
  48992. #define ERROR_COUNT 10
  48993. /* copied from logging.c since this is not exposed otherwise */
  48994. #ifndef ERROR_QUEUE_MAX
  48995. #ifdef ERROR_QUEUE_PER_THREAD
  48996. #define ERROR_QUEUE_MAX 16
  48997. #else
  48998. /* this breaks from compat of unlimited error queue size */
  48999. #define ERROR_QUEUE_MAX 100
  49000. #endif
  49001. #endif
  49002. static volatile int loggingThreadsReady;
  49003. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  49004. {
  49005. const char* file;
  49006. int line;
  49007. unsigned long err;
  49008. int errorCount = 0;
  49009. int i;
  49010. (void)args;
  49011. while (!loggingThreadsReady);
  49012. for (i = 0; i < ERROR_COUNT; i++)
  49013. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  49014. while ((err = ERR_get_error_line(&file, &line))) {
  49015. AssertIntEQ(err, 990 + errorCount);
  49016. errorCount++;
  49017. }
  49018. AssertIntEQ(errorCount, ERROR_COUNT);
  49019. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  49020. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  49021. errorCount = 0;
  49022. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  49023. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  49024. while ((err = ERR_get_error_line(&file, &line))) {
  49025. AssertIntEQ(err, 990 + errorCount);
  49026. errorCount++;
  49027. }
  49028. /* test that the 3 errors over the max were dropped */
  49029. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  49030. WOLFSSL_RETURN_FROM_THREAD(0);
  49031. }
  49032. #endif
  49033. static int test_error_queue_per_thread(void)
  49034. {
  49035. int res = TEST_SKIPPED;
  49036. #if !defined(SINGLE_THREADED) && defined(ERROR_QUEUE_PER_THREAD) && \
  49037. !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  49038. defined(DEBUG_WOLFSSL)
  49039. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  49040. int i;
  49041. ERR_clear_error(); /* clear out any error nodes */
  49042. loggingThreadsReady = 0;
  49043. for (i = 0; i < LOGGING_THREADS; i++)
  49044. start_thread(test_logging, NULL, &loggingThreads[i]);
  49045. loggingThreadsReady = 1;
  49046. for (i = 0; i < LOGGING_THREADS; i++)
  49047. join_thread(loggingThreads[i]);
  49048. res = TEST_SUCCESS;
  49049. #endif
  49050. return res;
  49051. }
  49052. static int test_wolfSSL_ERR_put_error(void)
  49053. {
  49054. EXPECT_DECLS;
  49055. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  49056. defined(DEBUG_WOLFSSL)
  49057. const char* file;
  49058. int line;
  49059. ERR_clear_error(); /* clear out any error nodes */
  49060. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  49061. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  49062. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  49063. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  49064. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  49065. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  49066. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  49067. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  49068. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  49069. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  49070. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  49071. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  49072. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  49073. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  49074. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  49075. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  49076. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  49077. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  49078. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  49079. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  49080. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  49081. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  49082. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  49083. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  49084. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  49085. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  49086. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  49087. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  49088. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  49089. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  49090. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  49091. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  49092. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  49093. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  49094. "this file", 100);
  49095. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  49096. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  49097. ExpectIntEQ(line, 100);
  49098. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  49099. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  49100. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  49101. #endif
  49102. /* try reading past end of error queue */
  49103. file = NULL;
  49104. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  49105. ExpectNull(file);
  49106. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  49107. PEMerr(4,4);
  49108. ExpectIntEQ(ERR_get_error(), 4);
  49109. /* Empty and free up all error nodes */
  49110. ERR_clear_error();
  49111. /* Verify all nodes are cleared */
  49112. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  49113. ERR_clear_error();
  49114. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  49115. #endif
  49116. return EXPECT_RESULT();
  49117. }
  49118. /*
  49119. * This is a regression test for a bug where the peek/get error functions were
  49120. * drawing from the end of the queue rather than the front.
  49121. */
  49122. static int test_wolfSSL_ERR_get_error_order(void)
  49123. {
  49124. EXPECT_DECLS;
  49125. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  49126. /* Empty the queue. */
  49127. wolfSSL_ERR_clear_error();
  49128. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  49129. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  49130. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  49131. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  49132. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  49133. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  49134. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  49135. return EXPECT_RESULT();
  49136. }
  49137. #ifndef NO_BIO
  49138. static int test_wolfSSL_ERR_print_errors(void)
  49139. {
  49140. EXPECT_DECLS;
  49141. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  49142. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  49143. BIO* bio = NULL;
  49144. char buf[1024];
  49145. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49146. ERR_clear_error(); /* clear out any error nodes */
  49147. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  49148. /* Choosing -600 as an unused errno. */
  49149. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  49150. ERR_print_errors(bio);
  49151. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  49152. ExpectIntEQ(XSTRNCMP(
  49153. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  49154. buf, 55), 0);
  49155. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  49156. ExpectIntEQ(XSTRNCMP(
  49157. "error:600:wolfSSL library:unknown error number:asn.c:100",
  49158. buf, 56), 0);
  49159. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  49160. ExpectIntEQ(buf[0], '\0');
  49161. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  49162. BIO_free(bio);
  49163. #endif
  49164. return EXPECT_RESULT();
  49165. }
  49166. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  49167. defined(DEBUG_WOLFSSL)
  49168. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  49169. {
  49170. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  49171. return 0;
  49172. }
  49173. #endif
  49174. static int test_wolfSSL_ERR_print_errors_cb(void)
  49175. {
  49176. EXPECT_DECLS;
  49177. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  49178. defined(DEBUG_WOLFSSL)
  49179. BIO* bio = NULL;
  49180. char buf[1024];
  49181. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49182. ERR_clear_error(); /* clear out any error nodes */
  49183. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  49184. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  49185. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  49186. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  49187. ExpectIntEQ(XSTRNCMP(
  49188. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  49189. buf, 53), 0);
  49190. ExpectIntEQ(XSTRNCMP(
  49191. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  49192. buf + 53, 55), 0);
  49193. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  49194. BIO_free(bio);
  49195. #endif
  49196. return EXPECT_RESULT();
  49197. }
  49198. /*
  49199. * Testing WOLFSSL_ERROR_MSG
  49200. */
  49201. static int test_WOLFSSL_ERROR_MSG(void)
  49202. {
  49203. int res = TEST_SKIPPED;
  49204. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  49205. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  49206. const char* msg = TEST_STRING;
  49207. WOLFSSL_ERROR_MSG(msg);
  49208. res = TEST_SUCCESS;
  49209. #endif
  49210. return res;
  49211. } /* End test_WOLFSSL_ERROR_MSG */
  49212. /*
  49213. * Testing wc_ERR_remove_state
  49214. */
  49215. static int test_wc_ERR_remove_state(void)
  49216. {
  49217. int res = TEST_SKIPPED;
  49218. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  49219. wc_ERR_remove_state();
  49220. res = TEST_SUCCESS;
  49221. #endif
  49222. return res;
  49223. } /* End test_wc_ERR_remove_state */
  49224. /*
  49225. * Testing wc_ERR_print_errors_fp
  49226. */
  49227. static int test_wc_ERR_print_errors_fp(void)
  49228. {
  49229. EXPECT_DECLS;
  49230. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  49231. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  49232. long sz;
  49233. XFILE fp = XBADFILE;
  49234. WOLFSSL_ERROR(BAD_FUNC_ARG);
  49235. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  49236. XBADFILE);
  49237. wc_ERR_print_errors_fp(fp);
  49238. #if defined(DEBUG_WOLFSSL)
  49239. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  49240. #ifdef NO_ERROR_QUEUE
  49241. ExpectIntEQ(sz = XFTELL(fp), 0);
  49242. #else
  49243. ExpectIntNE(sz = XFTELL(fp), 0);
  49244. #endif
  49245. #endif
  49246. if (fp != XBADFILE)
  49247. XFCLOSE(fp);
  49248. (void)sz;
  49249. #endif
  49250. return EXPECT_RESULT();
  49251. } /* End test_wc_ERR_print_errors_fp */
  49252. #ifdef DEBUG_WOLFSSL
  49253. static void Logging_cb(const int logLevel, const char *const logMessage)
  49254. {
  49255. (void)logLevel;
  49256. (void)logMessage;
  49257. }
  49258. #endif
  49259. /*
  49260. * Testing wolfSSL_GetLoggingCb
  49261. */
  49262. static int test_wolfSSL_GetLoggingCb(void)
  49263. {
  49264. EXPECT_DECLS;
  49265. #ifdef DEBUG_WOLFSSL
  49266. /* Testing without wolfSSL_SetLoggingCb() */
  49267. ExpectNull(wolfSSL_GetLoggingCb());
  49268. /* Testing with wolfSSL_SetLoggingCb() */
  49269. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  49270. ExpectNotNull(wolfSSL_GetLoggingCb());
  49271. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  49272. #endif
  49273. ExpectNull(wolfSSL_GetLoggingCb());
  49274. return EXPECT_RESULT();
  49275. } /* End test_wolfSSL_GetLoggingCb */
  49276. #endif /* !NO_BIO */
  49277. static int test_wolfSSL_MD4(void)
  49278. {
  49279. EXPECT_DECLS;
  49280. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  49281. MD4_CTX md4;
  49282. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  49283. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  49284. "789012345678901234567890";
  49285. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  49286. "\xcc\x05\x36";
  49287. int msgSz = (int)XSTRLEN(msg);
  49288. XMEMSET(out, 0, sizeof(out));
  49289. MD4_Init(&md4);
  49290. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  49291. MD4_Final(out, &md4);
  49292. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  49293. #endif
  49294. return EXPECT_RESULT();
  49295. }
  49296. static int test_wolfSSL_MD5(void)
  49297. {
  49298. EXPECT_DECLS;
  49299. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  49300. byte input1[] = "";
  49301. byte input2[] = "message digest";
  49302. byte hash[WC_MD5_DIGEST_SIZE];
  49303. unsigned char output1[] =
  49304. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  49305. unsigned char output2[] =
  49306. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  49307. WOLFSSL_MD5_CTX md5;
  49308. XMEMSET(&md5, 0, sizeof(md5));
  49309. /* Test cases for illegal parameters */
  49310. ExpectIntEQ(MD5_Init(NULL), 0);
  49311. ExpectIntEQ(MD5_Init(&md5), 1);
  49312. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  49313. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  49314. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  49315. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  49316. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  49317. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  49318. /* Init MD5 CTX */
  49319. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  49320. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  49321. 1);
  49322. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  49323. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  49324. /* Init MD5 CTX */
  49325. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  49326. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  49327. (int)XSTRLEN((const char*)input2)), 1);
  49328. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  49329. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  49330. #if !defined(NO_OLD_NAMES) && \
  49331. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  49332. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  49333. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  49334. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  49335. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  49336. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  49337. &hash);
  49338. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  49339. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  49340. &hash);
  49341. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  49342. {
  49343. byte data[] = "Data to be hashed.";
  49344. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  49345. ExpectNotNull(MD5(data, sizeof(data), NULL));
  49346. ExpectNotNull(MD5(data, sizeof(data), hash));
  49347. ExpectNotNull(MD5(NULL, 0, hash));
  49348. ExpectNull(MD5(NULL, sizeof(data), hash));
  49349. }
  49350. #endif
  49351. #endif
  49352. return EXPECT_RESULT();
  49353. }
  49354. static int test_wolfSSL_MD5_Transform(void)
  49355. {
  49356. EXPECT_DECLS;
  49357. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  49358. byte input1[] = "";
  49359. byte input2[] = "abc";
  49360. byte local[WC_MD5_BLOCK_SIZE];
  49361. word32 sLen = 0;
  49362. #ifdef BIG_ENDIAN_ORDER
  49363. unsigned char output1[] =
  49364. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  49365. unsigned char output2[] =
  49366. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  49367. #else
  49368. unsigned char output1[] =
  49369. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  49370. unsigned char output2[] =
  49371. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  49372. #endif
  49373. union {
  49374. wc_Md5 native;
  49375. MD5_CTX compat;
  49376. } md5;
  49377. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  49378. XMEMSET(&local, 0, sizeof(local));
  49379. /* sanity check */
  49380. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  49381. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  49382. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  49383. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  49384. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  49385. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  49386. /* Init MD5 CTX */
  49387. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  49388. /* Do Transform*/
  49389. sLen = (word32)XSTRLEN((char*)input1);
  49390. XMEMCPY(local, input1, sLen);
  49391. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  49392. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  49393. /* Init MD5 CTX */
  49394. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  49395. sLen = (word32)XSTRLEN((char*)input2);
  49396. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  49397. XMEMCPY(local, input2, sLen);
  49398. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  49399. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  49400. #endif
  49401. return EXPECT_RESULT();
  49402. }
  49403. static int test_wolfSSL_SHA(void)
  49404. {
  49405. EXPECT_DECLS;
  49406. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  49407. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  49408. (!defined(HAVE_FIPS) || \
  49409. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  49410. {
  49411. const unsigned char in[] = "abc";
  49412. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  49413. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  49414. unsigned char out[WC_SHA_DIGEST_SIZE];
  49415. unsigned char* p = NULL;
  49416. WOLFSSL_SHA_CTX sha;
  49417. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  49418. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  49419. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  49420. /* SHA interface test */
  49421. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  49422. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  49423. ExpectNotNull(SHA(in, 0, out));
  49424. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  49425. ExpectNotNull(SHA(NULL, 0, out));
  49426. ExpectNotNull(SHA(NULL, 0, NULL));
  49427. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  49428. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  49429. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  49430. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  49431. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  49432. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  49433. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  49434. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  49435. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  49436. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  49437. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  49438. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  49439. }
  49440. #endif
  49441. #if !defined(NO_SHA256)
  49442. {
  49443. const unsigned char in[] = "abc";
  49444. unsigned char expected[] =
  49445. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  49446. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  49447. "\x15\xAD";
  49448. unsigned char out[WC_SHA256_DIGEST_SIZE];
  49449. unsigned char* p = NULL;
  49450. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  49451. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  49452. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  49453. #else
  49454. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  49455. #endif
  49456. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  49457. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  49458. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  49459. #else
  49460. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  49461. #endif
  49462. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  49463. }
  49464. #endif
  49465. #if defined(WOLFSSL_SHA384)
  49466. {
  49467. const unsigned char in[] = "abc";
  49468. unsigned char expected[] =
  49469. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  49470. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  49471. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  49472. "\xc8\x25\xa7";
  49473. unsigned char out[WC_SHA384_DIGEST_SIZE];
  49474. unsigned char* p = NULL;
  49475. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  49476. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  49477. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  49478. #else
  49479. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  49480. #endif
  49481. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  49482. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  49483. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  49484. #else
  49485. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  49486. #endif
  49487. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  49488. }
  49489. #endif
  49490. #if defined(WOLFSSL_SHA512)
  49491. {
  49492. const unsigned char in[] = "abc";
  49493. unsigned char expected[] =
  49494. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  49495. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  49496. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  49497. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  49498. "\xa5\x4c\xa4\x9f";
  49499. unsigned char out[WC_SHA512_DIGEST_SIZE];
  49500. unsigned char* p = NULL;
  49501. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  49502. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  49503. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  49504. #else
  49505. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  49506. #endif
  49507. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  49508. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  49509. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  49510. #else
  49511. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  49512. #endif
  49513. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  49514. }
  49515. #endif
  49516. #endif
  49517. return EXPECT_RESULT();
  49518. }
  49519. static int test_wolfSSL_SHA_Transform(void)
  49520. {
  49521. EXPECT_DECLS;
  49522. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  49523. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49524. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  49525. byte input1[] = "";
  49526. byte input2[] = "abc";
  49527. byte local[WC_SHA_BLOCK_SIZE];
  49528. word32 sLen = 0;
  49529. #ifdef BIG_ENDIAN_ORDER
  49530. unsigned char output1[] =
  49531. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  49532. "\x09\xf7\x3a\x93";
  49533. unsigned char output2[] =
  49534. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  49535. "\x7c\x6e\x08\xb8";
  49536. #else
  49537. unsigned char output1[] =
  49538. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  49539. "\x93\x3a\xf7\x09";
  49540. unsigned char output2[] =
  49541. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  49542. "\xb8\x08\x6e\x7c";
  49543. #endif
  49544. union {
  49545. wc_Sha native;
  49546. SHA_CTX compat;
  49547. } sha;
  49548. union {
  49549. wc_Sha native;
  49550. SHA_CTX compat;
  49551. } sha1;
  49552. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  49553. XMEMSET(&local, 0, sizeof(local));
  49554. /* sanity check */
  49555. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  49556. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  49557. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  49558. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  49559. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  49560. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  49561. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  49562. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  49563. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  49564. /* Init SHA CTX */
  49565. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  49566. /* Do Transform*/
  49567. sLen = (word32)XSTRLEN((char*)input1);
  49568. XMEMCPY(local, input1, sLen);
  49569. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  49570. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  49571. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  49572. /* Init SHA CTX */
  49573. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  49574. sLen = (word32)XSTRLEN((char*)input2);
  49575. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  49576. XMEMCPY(local, input2, sLen);
  49577. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  49578. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  49579. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  49580. /* SHA1 */
  49581. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  49582. /* Init SHA CTX */
  49583. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  49584. /* Do Transform*/
  49585. sLen = (word32)XSTRLEN((char*)input1);
  49586. XMEMCPY(local, input1, sLen);
  49587. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  49588. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  49589. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  49590. /* Init SHA CTX */
  49591. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  49592. sLen = (word32)XSTRLEN((char*)input2);
  49593. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  49594. XMEMCPY(local, input2, sLen);
  49595. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  49596. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  49597. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  49598. #endif
  49599. #endif
  49600. return EXPECT_RESULT();
  49601. }
  49602. static int test_wolfSSL_SHA224(void)
  49603. {
  49604. EXPECT_DECLS;
  49605. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  49606. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49607. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  49608. unsigned char input[] =
  49609. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  49610. unsigned char output[] =
  49611. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  49612. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  49613. size_t inLen;
  49614. byte hash[WC_SHA224_DIGEST_SIZE];
  49615. unsigned char* p;
  49616. inLen = XSTRLEN((char*)input);
  49617. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  49618. ExpectNull(SHA224(NULL, inLen, hash));
  49619. ExpectNotNull(SHA224(input, 0, hash));
  49620. ExpectNotNull(SHA224(input, inLen, NULL));
  49621. ExpectNotNull(SHA224(NULL, 0, hash));
  49622. ExpectNotNull(SHA224(NULL, 0, NULL));
  49623. ExpectNotNull(SHA224(input, inLen, hash));
  49624. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  49625. ExpectNotNull(p = SHA224(input, inLen, NULL));
  49626. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  49627. #endif
  49628. return EXPECT_RESULT();
  49629. }
  49630. static int test_wolfSSL_SHA256(void)
  49631. {
  49632. EXPECT_DECLS;
  49633. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  49634. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  49635. unsigned char input[] =
  49636. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  49637. unsigned char output[] =
  49638. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  49639. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  49640. "\x06\xC1";
  49641. size_t inLen;
  49642. byte hash[WC_SHA256_DIGEST_SIZE];
  49643. inLen = XSTRLEN((char*)input);
  49644. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  49645. ExpectNotNull(SHA256(input, inLen, hash));
  49646. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  49647. #endif
  49648. return EXPECT_RESULT();
  49649. }
  49650. static int test_wolfSSL_SHA256_Transform(void)
  49651. {
  49652. EXPECT_DECLS;
  49653. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  49654. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49655. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  49656. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  49657. !defined(WOLFSSL_KCAPI_HASH)
  49658. byte input1[] = "";
  49659. byte input2[] = "abc";
  49660. byte local[WC_SHA256_BLOCK_SIZE];
  49661. word32 sLen = 0;
  49662. #ifdef BIG_ENDIAN_ORDER
  49663. unsigned char output1[] =
  49664. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  49665. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  49666. unsigned char output2[] =
  49667. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  49668. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  49669. #else
  49670. unsigned char output1[] =
  49671. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  49672. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  49673. unsigned char output2[] =
  49674. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  49675. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  49676. #endif
  49677. union {
  49678. wc_Sha256 native;
  49679. SHA256_CTX compat;
  49680. } sha256;
  49681. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  49682. XMEMSET(&local, 0, sizeof(local));
  49683. /* sanity check */
  49684. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  49685. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  49686. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  49687. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  49688. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  49689. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  49690. /* Init SHA256 CTX */
  49691. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  49692. /* Do Transform*/
  49693. sLen = (word32)XSTRLEN((char*)input1);
  49694. XMEMCPY(local, input1, sLen);
  49695. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  49696. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  49697. 0);
  49698. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  49699. /* Init SHA256 CTX */
  49700. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  49701. sLen = (word32)XSTRLEN((char*)input2);
  49702. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  49703. XMEMCPY(local, input2, sLen);
  49704. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  49705. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  49706. 0);
  49707. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  49708. #endif
  49709. #endif
  49710. return EXPECT_RESULT();
  49711. }
  49712. static int test_wolfSSL_SHA512_Transform(void)
  49713. {
  49714. EXPECT_DECLS;
  49715. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  49716. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49717. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  49718. !defined(WOLFSSL_KCAPI_HASH)
  49719. byte input1[] = "";
  49720. byte input2[] = "abc";
  49721. byte local[WC_SHA512_BLOCK_SIZE];
  49722. word32 sLen = 0;
  49723. #ifdef BIG_ENDIAN_ORDER
  49724. unsigned char output1[] =
  49725. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  49726. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  49727. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  49728. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  49729. unsigned char output2[] =
  49730. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  49731. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  49732. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  49733. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  49734. #else
  49735. unsigned char output1[] =
  49736. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  49737. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  49738. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  49739. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  49740. unsigned char output2[] =
  49741. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  49742. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  49743. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  49744. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  49745. #endif
  49746. union {
  49747. wc_Sha512 native;
  49748. SHA512_CTX compat;
  49749. } sha512;
  49750. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  49751. XMEMSET(&local, 0, sizeof(local));
  49752. /* sanity check */
  49753. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  49754. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  49755. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  49756. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  49757. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  49758. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  49759. /* Init SHA512 CTX */
  49760. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  49761. /* Do Transform*/
  49762. sLen = (word32)XSTRLEN((char*)input1);
  49763. XMEMCPY(local, input1, sLen);
  49764. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  49765. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  49766. WC_SHA512_DIGEST_SIZE), 0);
  49767. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  49768. /* Init SHA512 CTX */
  49769. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  49770. sLen = (word32)XSTRLEN((char*)input2);
  49771. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  49772. XMEMCPY(local, input2, sLen);
  49773. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  49774. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  49775. WC_SHA512_DIGEST_SIZE), 0);
  49776. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  49777. (void)input1;
  49778. #endif
  49779. #endif
  49780. return EXPECT_RESULT();
  49781. }
  49782. static int test_wolfSSL_SHA512_224_Transform(void)
  49783. {
  49784. EXPECT_DECLS;
  49785. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  49786. !defined(WOLFSSL_NOSHA512_224)
  49787. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49788. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  49789. !defined(WOLFSSL_KCAPI_HASH)
  49790. byte input1[] = "";
  49791. byte input2[] = "abc";
  49792. byte local[WC_SHA512_BLOCK_SIZE];
  49793. word32 sLen = 0;
  49794. unsigned char output1[] =
  49795. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  49796. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  49797. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  49798. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  49799. unsigned char output2[] =
  49800. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  49801. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  49802. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  49803. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  49804. union {
  49805. wc_Sha512 native;
  49806. SHA512_CTX compat;
  49807. } sha512;
  49808. #ifdef BIG_ENDIAN_ORDER
  49809. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  49810. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  49811. #endif
  49812. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  49813. XMEMSET(&local, 0, sizeof(local));
  49814. /* sanity check */
  49815. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  49816. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  49817. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  49818. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  49819. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  49820. BAD_FUNC_ARG);
  49821. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  49822. /* Init SHA512 CTX */
  49823. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  49824. /* Do Transform*/
  49825. sLen = (word32)XSTRLEN((char*)input1);
  49826. XMEMCPY(local, input1, sLen);
  49827. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  49828. 1);
  49829. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  49830. WC_SHA512_DIGEST_SIZE), 0);
  49831. /* frees resources */
  49832. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  49833. /* Init SHA512 CTX */
  49834. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  49835. sLen = (word32)XSTRLEN((char*)input2);
  49836. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  49837. XMEMCPY(local, input2, sLen);
  49838. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  49839. 1);
  49840. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  49841. WC_SHA512_DIGEST_SIZE), 0);
  49842. /* frees resources */
  49843. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  49844. #endif
  49845. #endif
  49846. return EXPECT_RESULT();
  49847. }
  49848. static int test_wolfSSL_SHA512_256_Transform(void)
  49849. {
  49850. EXPECT_DECLS;
  49851. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  49852. !defined(WOLFSSL_NOSHA512_256)
  49853. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49854. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  49855. !defined(WOLFSSL_KCAPI_HASH)
  49856. byte input1[] = "";
  49857. byte input2[] = "abc";
  49858. byte local[WC_SHA512_BLOCK_SIZE];
  49859. word32 sLen = 0;
  49860. unsigned char output1[] =
  49861. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  49862. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  49863. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  49864. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  49865. unsigned char output2[] =
  49866. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  49867. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  49868. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  49869. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  49870. union {
  49871. wc_Sha512 native;
  49872. SHA512_CTX compat;
  49873. } sha512;
  49874. #ifdef BIG_ENDIAN_ORDER
  49875. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  49876. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  49877. #endif
  49878. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  49879. XMEMSET(&local, 0, sizeof(local));
  49880. /* sanity check */
  49881. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  49882. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  49883. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  49884. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  49885. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  49886. BAD_FUNC_ARG);
  49887. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  49888. /* Init SHA512 CTX */
  49889. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  49890. /* Do Transform*/
  49891. sLen = (word32)XSTRLEN((char*)input1);
  49892. XMEMCPY(local, input1, sLen);
  49893. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  49894. 1);
  49895. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  49896. WC_SHA512_DIGEST_SIZE), 0);
  49897. /* frees resources */
  49898. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  49899. /* Init SHA512 CTX */
  49900. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  49901. sLen = (word32)XSTRLEN((char*)input2);
  49902. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  49903. XMEMCPY(local, input2, sLen);
  49904. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  49905. 1);
  49906. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  49907. WC_SHA512_DIGEST_SIZE), 0);
  49908. /* frees resources */
  49909. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  49910. #endif
  49911. #endif
  49912. return EXPECT_RESULT();
  49913. }
  49914. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  49915. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  49916. * buffer of 64 bytes.
  49917. *
  49918. * returns the size of the digest buffer on success and a negative value on
  49919. * failure.
  49920. */
  49921. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  49922. int* sz)
  49923. {
  49924. EXPECT_DECLS;
  49925. HMAC_CTX ctx1;
  49926. HMAC_CTX ctx2;
  49927. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  49928. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  49929. unsigned char long_key[] =
  49930. "0123456789012345678901234567890123456789"
  49931. "0123456789012345678901234567890123456789"
  49932. "0123456789012345678901234567890123456789"
  49933. "0123456789012345678901234567890123456789";
  49934. unsigned char msg[] = "message to hash";
  49935. unsigned int digestSz = 64;
  49936. int keySz = sizeof(key);
  49937. int long_keySz = sizeof(long_key);
  49938. int msgSz = sizeof(msg);
  49939. unsigned char digest2[64];
  49940. unsigned int digestSz2 = 64;
  49941. HMAC_CTX_init(&ctx1);
  49942. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  49943. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49944. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  49945. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49946. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  49947. HMAC_CTX_cleanup(&ctx1);
  49948. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  49949. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  49950. HMAC_CTX_cleanup(&ctx2);
  49951. ExpectIntEQ(digestSz, digestSz2);
  49952. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  49953. /* test HMAC_Init with NULL key */
  49954. /* init after copy */
  49955. HMAC_CTX_init(&ctx1);
  49956. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  49957. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49958. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  49959. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  49960. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49961. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49962. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  49963. HMAC_CTX_cleanup(&ctx1);
  49964. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  49965. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  49966. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  49967. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  49968. HMAC_CTX_cleanup(&ctx2);
  49969. ExpectIntEQ(digestSz, digestSz2);
  49970. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  49971. /* long key */
  49972. HMAC_CTX_init(&ctx1);
  49973. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  49974. SSL_SUCCESS);
  49975. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49976. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  49977. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  49978. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49979. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49980. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  49981. HMAC_CTX_cleanup(&ctx1);
  49982. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  49983. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  49984. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  49985. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  49986. HMAC_CTX_cleanup(&ctx2);
  49987. ExpectIntEQ(digestSz, digestSz2);
  49988. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  49989. /* init before copy */
  49990. HMAC_CTX_init(&ctx1);
  49991. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  49992. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49993. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  49994. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  49995. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49996. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  49997. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  49998. HMAC_CTX_cleanup(&ctx1);
  49999. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  50000. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  50001. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  50002. HMAC_CTX_cleanup(&ctx2);
  50003. ExpectIntEQ(digestSz, digestSz2);
  50004. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  50005. *sz = (int)digestSz;
  50006. return EXPECT_RESULT();
  50007. }
  50008. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  50009. static int test_wolfSSL_HMAC_CTX(void)
  50010. {
  50011. EXPECT_DECLS;
  50012. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  50013. unsigned char digest[64];
  50014. int digestSz;
  50015. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  50016. WOLFSSL_HMAC_CTX ctx1;
  50017. WOLFSSL_HMAC_CTX ctx2;
  50018. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  50019. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  50020. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  50021. wolfSSL_HMAC_CTX_free(NULL);
  50022. wolfSSL_HMAC_CTX_free(hmac_ctx);
  50023. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  50024. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  50025. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  50026. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  50027. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  50028. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  50029. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  50030. ((! defined(HAVE_FIPS_VERSION)) || \
  50031. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  50032. /* Copy object that hasn't had a digest set - MD5. */
  50033. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  50034. #else
  50035. /* Copy object that hasn't had a digest set. */
  50036. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  50037. #endif
  50038. HMAC_CTX_cleanup(NULL);
  50039. HMAC_CTX_cleanup(&ctx2);
  50040. ExpectNull(HMAC_CTX_get_md(NULL));
  50041. #ifndef NO_SHA
  50042. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  50043. TEST_SUCCESS);
  50044. ExpectIntEQ(digestSz, 20);
  50045. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  50046. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  50047. #endif /* !NO_SHA */
  50048. #ifdef WOLFSSL_SHA224
  50049. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  50050. TEST_SUCCESS);
  50051. ExpectIntEQ(digestSz, 28);
  50052. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  50053. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  50054. "\x02\x0E", digest, digestSz), 0);
  50055. #endif /* WOLFSSL_SHA224 */
  50056. #ifndef NO_SHA256
  50057. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  50058. TEST_SUCCESS);
  50059. ExpectIntEQ(digestSz, 32);
  50060. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  50061. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  50062. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  50063. #endif /* !NO_SHA256 */
  50064. #ifdef WOLFSSL_SHA384
  50065. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  50066. TEST_SUCCESS);
  50067. ExpectIntEQ(digestSz, 48);
  50068. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  50069. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  50070. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  50071. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  50072. digest, digestSz), 0);
  50073. #endif /* WOLFSSL_SHA384 */
  50074. #ifdef WOLFSSL_SHA512
  50075. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  50076. TEST_SUCCESS);
  50077. ExpectIntEQ(digestSz, 64);
  50078. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  50079. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  50080. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  50081. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  50082. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  50083. digest, digestSz), 0);
  50084. #endif /* WOLFSSL_SHA512 */
  50085. #ifdef WOLFSSL_SHA3
  50086. #ifndef WOLFSSL_NOSHA3_224
  50087. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  50088. TEST_SUCCESS);
  50089. ExpectIntEQ(digestSz, 28);
  50090. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  50091. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  50092. "\x29\x0f", digest, digestSz), 0);
  50093. #endif
  50094. #ifndef WOLFSSL_NOSHA3_256
  50095. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  50096. TEST_SUCCESS);
  50097. ExpectIntEQ(digestSz, 32);
  50098. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  50099. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  50100. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  50101. #endif
  50102. #ifndef WOLFSSL_NOSHA3_384
  50103. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  50104. TEST_SUCCESS);
  50105. ExpectIntEQ(digestSz, 48);
  50106. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  50107. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  50108. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  50109. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  50110. digest, digestSz), 0);
  50111. #endif
  50112. #ifndef WOLFSSL_NOSHA3_512
  50113. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  50114. TEST_SUCCESS);
  50115. ExpectIntEQ(digestSz, 64);
  50116. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  50117. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  50118. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  50119. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  50120. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  50121. digest, digestSz), 0);
  50122. #endif
  50123. #endif
  50124. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  50125. HAVE_FIPS_VERSION <= 2)
  50126. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  50127. TEST_SUCCESS);
  50128. ExpectIntEQ(digestSz, 16);
  50129. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  50130. "\xE4\x98\xDD", digest, digestSz), 0);
  50131. #endif /* !NO_MD5 */
  50132. #endif
  50133. return EXPECT_RESULT();
  50134. }
  50135. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  50136. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  50137. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  50138. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  50139. {
  50140. EXPECT_DECLS;
  50141. static const unsigned char key[] = "simple test key";
  50142. HMAC_CTX* hmac = NULL;
  50143. ENGINE* e = NULL;
  50144. unsigned char hash[WC_MAX_DIGEST_SIZE];
  50145. unsigned int len;
  50146. ExpectNotNull(hmac = HMAC_CTX_new());
  50147. HMAC_CTX_init(hmac);
  50148. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  50149. ((! defined(HAVE_FIPS_VERSION)) || \
  50150. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  50151. /* Get size on object that hasn't had a digest set - MD5. */
  50152. ExpectIntEQ(HMAC_size(hmac), 16);
  50153. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  50154. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  50155. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  50156. #else
  50157. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  50158. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  50159. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  50160. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  50161. #endif
  50162. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  50163. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  50164. /* re-using test key as data to hash */
  50165. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  50166. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  50167. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  50168. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  50169. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  50170. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  50171. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  50172. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  50173. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  50174. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  50175. ExpectIntEQ(len, md_len);
  50176. ExpectIntEQ(HMAC_size(NULL), 0);
  50177. ExpectIntEQ(HMAC_size(hmac), md_len);
  50178. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  50179. HMAC_cleanup(NULL);
  50180. HMAC_cleanup(hmac);
  50181. HMAC_CTX_free(hmac);
  50182. len = 0;
  50183. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  50184. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  50185. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  50186. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  50187. ExpectIntEQ(len, md_len);
  50188. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  50189. /* With data. */
  50190. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  50191. &len));
  50192. /* With NULL data. */
  50193. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  50194. &len));
  50195. /* With zero length data. */
  50196. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  50197. return EXPECT_RESULT();
  50198. }
  50199. #endif
  50200. static int test_wolfSSL_HMAC(void)
  50201. {
  50202. EXPECT_DECLS;
  50203. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  50204. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  50205. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  50206. #ifndef NO_SHA256
  50207. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  50208. TEST_SUCCESS);
  50209. #endif
  50210. #ifdef WOLFSSL_SHA224
  50211. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  50212. TEST_SUCCESS);
  50213. #endif
  50214. #ifdef WOLFSSL_SHA384
  50215. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  50216. TEST_SUCCESS);
  50217. #endif
  50218. #ifdef WOLFSSL_SHA512
  50219. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  50220. TEST_SUCCESS);
  50221. #endif
  50222. #ifdef WOLFSSL_SHA3
  50223. #ifndef WOLFSSL_NOSHA3_224
  50224. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  50225. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  50226. #endif
  50227. #ifndef WOLFSSL_NOSHA3_256
  50228. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  50229. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  50230. #endif
  50231. #ifndef WOLFSSL_NOSHA3_384
  50232. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  50233. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  50234. #endif
  50235. #ifndef WOLFSSL_NOSHA3_512
  50236. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  50237. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  50238. #endif
  50239. #endif
  50240. #ifndef NO_SHA
  50241. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  50242. TEST_SUCCESS);
  50243. #endif
  50244. #endif
  50245. return EXPECT_RESULT();
  50246. }
  50247. static int test_wolfSSL_CMAC(void)
  50248. {
  50249. EXPECT_DECLS;
  50250. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  50251. defined(WOLFSSL_AES_DIRECT)
  50252. int i;
  50253. byte key[AES_256_KEY_SIZE];
  50254. CMAC_CTX* cmacCtx = NULL;
  50255. byte out[AES_BLOCK_SIZE];
  50256. size_t outLen = AES_BLOCK_SIZE;
  50257. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  50258. key[i] = i;
  50259. }
  50260. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  50261. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  50262. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  50263. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  50264. NULL), 1);
  50265. /* re-using test key as data to hash */
  50266. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  50267. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  50268. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  50269. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  50270. /* No Update works. */
  50271. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  50272. NULL), 1);
  50273. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  50274. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  50275. NULL), 1);
  50276. /* Test parameters with CMAC_Update. */
  50277. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  50278. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  50279. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  50280. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  50281. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  50282. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  50283. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  50284. /* Test parameters with CMAC_Final. */
  50285. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  50286. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  50287. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  50288. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  50289. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  50290. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  50291. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  50292. CMAC_CTX_free(cmacCtx);
  50293. /* Test parameters with CMAC Init. */
  50294. cmacCtx = NULL;
  50295. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  50296. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  50297. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  50298. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  50299. NULL), 0);
  50300. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  50301. NULL), 0);
  50302. /* give a key too small for the cipher, verify we get failure */
  50303. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  50304. NULL), 0);
  50305. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  50306. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  50307. /* Only AES-CBC supported. */
  50308. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  50309. NULL), 0);
  50310. #endif
  50311. CMAC_CTX_free(cmacCtx);
  50312. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  50313. cmacCtx = NULL;
  50314. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  50315. /* No Init. */
  50316. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  50317. CMAC_CTX_free(cmacCtx);
  50318. /* Test AES-256-CBC */
  50319. cmacCtx = NULL;
  50320. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  50321. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  50322. NULL), 1);
  50323. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  50324. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  50325. CMAC_CTX_free(cmacCtx);
  50326. /* Test AES-192-CBC */
  50327. cmacCtx = NULL;
  50328. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  50329. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  50330. NULL), 1);
  50331. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  50332. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  50333. CMAC_CTX_free(cmacCtx);
  50334. cmacCtx = NULL;
  50335. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  50336. CMAC_CTX_free(cmacCtx);
  50337. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  50338. return EXPECT_RESULT();
  50339. }
  50340. static int test_wolfSSL_DES(void)
  50341. {
  50342. EXPECT_DECLS;
  50343. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  50344. const_DES_cblock myDes;
  50345. DES_cblock iv;
  50346. DES_key_schedule key;
  50347. word32 i = 0;
  50348. DES_LONG dl = 0;
  50349. unsigned char msg[] = "hello wolfssl";
  50350. unsigned char weakKey[][8] = {
  50351. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  50352. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  50353. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  50354. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  50355. };
  50356. unsigned char semiWeakKey[][8] = {
  50357. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  50358. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  50359. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  50360. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  50361. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  50362. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  50363. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  50364. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  50365. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  50366. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  50367. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  50368. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  50369. };
  50370. DES_check_key(1);
  50371. DES_set_key(&myDes, &key);
  50372. /* check, check of odd parity */
  50373. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  50374. myDes[0] = 6; /* set even parity */
  50375. XMEMSET(key, 5, sizeof(DES_key_schedule));
  50376. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  50377. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  50378. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  50379. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  50380. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  50381. /* set odd parity for success case */
  50382. DES_set_odd_parity(&myDes);
  50383. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  50384. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  50385. myDes[3]);
  50386. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  50387. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  50388. ExpectIntEQ(key[i], myDes[i]);
  50389. }
  50390. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  50391. /* check weak key */
  50392. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  50393. XMEMSET(key, 5, sizeof(DES_key_schedule));
  50394. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  50395. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  50396. DES_set_key_unchecked(NULL, NULL);
  50397. DES_set_key_unchecked(&myDes, NULL);
  50398. DES_set_key_unchecked(NULL, &key);
  50399. /* compare arrays, should be the same */
  50400. /* now do unchecked copy of a weak key over */
  50401. DES_set_key_unchecked(&myDes, &key);
  50402. /* compare arrays, should be the same */
  50403. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  50404. ExpectIntEQ(key[i], myDes[i]);
  50405. }
  50406. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  50407. myDes[7] = 2;
  50408. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  50409. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  50410. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  50411. /* Test all weak keys. */
  50412. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  50413. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  50414. }
  50415. /* Test all semi-weak keys. */
  50416. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  50417. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  50418. }
  50419. /* check DES_key_sched API */
  50420. XMEMSET(key, 1, sizeof(DES_key_schedule));
  50421. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  50422. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  50423. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  50424. /* compare arrays, should be the same */
  50425. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  50426. ExpectIntEQ(key[i], myDes[i]);
  50427. }
  50428. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  50429. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  50430. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  50431. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  50432. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  50433. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  50434. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  50435. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  50436. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  50437. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  50438. * DES_cbc_encrypt on the input */
  50439. XMEMSET(iv, 0, sizeof(DES_cblock));
  50440. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  50441. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  50442. ExpectIntEQ(dl, 480052723);
  50443. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  50444. return EXPECT_RESULT();
  50445. }
  50446. static int test_wolfSSL_DES_ncbc(void)
  50447. {
  50448. EXPECT_DECLS;
  50449. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  50450. const_DES_cblock myDes;
  50451. DES_cblock iv = {1};
  50452. DES_key_schedule key = {0};
  50453. unsigned char msg[] = "hello wolfssl";
  50454. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  50455. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  50456. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  50457. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  50458. /* partial block test */
  50459. DES_set_key(&key, &myDes);
  50460. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  50461. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  50462. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  50463. DES_set_key(&key, &myDes);
  50464. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  50465. *((byte*)&iv) = 1;
  50466. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  50467. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  50468. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  50469. /* full block test */
  50470. DES_set_key(&key, &myDes);
  50471. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  50472. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  50473. *((byte*)&iv) = 1;
  50474. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  50475. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  50476. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  50477. DES_set_key(&key, &myDes);
  50478. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  50479. *((byte*)&iv) = 1;
  50480. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  50481. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  50482. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  50483. #endif
  50484. return EXPECT_RESULT();
  50485. }
  50486. static int test_wolfSSL_DES_ecb_encrypt(void)
  50487. {
  50488. EXPECT_DECLS;
  50489. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  50490. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  50491. WOLFSSL_DES_key_schedule key;
  50492. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  50493. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  50494. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  50495. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  50496. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  50497. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  50498. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  50499. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  50500. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  50501. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  50502. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  50503. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  50504. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  50505. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  50506. /* Encrypt messages */
  50507. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  50508. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  50509. {
  50510. /* Decrypt messages */
  50511. int ret1 = 0;
  50512. int ret2 = 0;
  50513. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  50514. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  50515. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  50516. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  50517. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  50518. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  50519. }
  50520. #endif
  50521. return EXPECT_RESULT();
  50522. }
  50523. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  50524. {
  50525. EXPECT_DECLS;
  50526. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  50527. unsigned char input1[8], input2[8];
  50528. unsigned char output1[8], output2[8];
  50529. unsigned char back1[8], back2[8];
  50530. WOLFSSL_DES_cblock iv1, iv2;
  50531. WOLFSSL_DES_key_schedule key1, key2, key3;
  50532. int i;
  50533. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  50534. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  50535. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  50536. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  50537. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  50538. XMEMSET(output1, 0, sizeof(output1));
  50539. XMEMSET(output2, 0, sizeof(output2));
  50540. XMEMSET(back1, 0, sizeof(back1));
  50541. XMEMSET(back2, 0, sizeof(back2));
  50542. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  50543. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  50544. /* Encrypt messages */
  50545. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  50546. DES_ENCRYPT);
  50547. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  50548. DES_ENCRYPT);
  50549. {
  50550. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  50551. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  50552. /* Decrypt messages */
  50553. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  50554. &iv1, DES_DECRYPT);
  50555. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  50556. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  50557. &iv2, DES_DECRYPT);
  50558. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  50559. }
  50560. for (i = 0; i < 8; i++) {
  50561. XMEMSET(output1, 0, sizeof(output1));
  50562. XMEMSET(output2, 0, sizeof(output2));
  50563. XMEMSET(back1, 0, sizeof(back1));
  50564. XMEMSET(back2, 0, sizeof(back2));
  50565. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  50566. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  50567. /* Encrypt partial messages */
  50568. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  50569. &iv1, DES_ENCRYPT);
  50570. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  50571. &iv2, DES_ENCRYPT);
  50572. {
  50573. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  50574. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  50575. /* Decrypt messages */
  50576. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  50577. &key3, &iv1, DES_DECRYPT);
  50578. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  50579. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  50580. &key3, &iv2, DES_DECRYPT);
  50581. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  50582. }
  50583. }
  50584. #endif
  50585. return EXPECT_RESULT();
  50586. }
  50587. static int test_wolfSSL_AES_encrypt(void)
  50588. {
  50589. EXPECT_DECLS;
  50590. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  50591. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  50592. AES_KEY enc;
  50593. AES_KEY dec;
  50594. const byte msg[] = {
  50595. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  50596. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  50597. };
  50598. const byte exp[] = {
  50599. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  50600. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  50601. };
  50602. const byte key[] = {
  50603. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  50604. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  50605. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  50606. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  50607. };
  50608. byte eout[sizeof(msg)];
  50609. byte dout[sizeof(msg)];
  50610. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  50611. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  50612. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  50613. wolfSSL_AES_encrypt(msg, NULL, NULL);
  50614. wolfSSL_AES_encrypt(NULL, eout, NULL);
  50615. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  50616. wolfSSL_AES_encrypt(msg, eout, NULL);
  50617. wolfSSL_AES_encrypt(msg, NULL, &enc);
  50618. wolfSSL_AES_encrypt(NULL, eout, &enc);
  50619. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  50620. wolfSSL_AES_decrypt(eout, NULL, NULL);
  50621. wolfSSL_AES_decrypt(NULL, dout, NULL);
  50622. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  50623. wolfSSL_AES_decrypt(eout, dout, NULL);
  50624. wolfSSL_AES_decrypt(eout, NULL, &dec);
  50625. wolfSSL_AES_decrypt(NULL, dout, &dec);
  50626. wolfSSL_AES_encrypt(msg, eout, &enc);
  50627. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  50628. wolfSSL_AES_decrypt(eout, dout, &dec);
  50629. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  50630. #endif
  50631. return EXPECT_RESULT();
  50632. }
  50633. static int test_wolfSSL_AES_ecb_encrypt(void)
  50634. {
  50635. EXPECT_DECLS;
  50636. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  50637. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  50638. AES_KEY aes;
  50639. const byte msg[] =
  50640. {
  50641. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  50642. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  50643. };
  50644. const byte verify[] =
  50645. {
  50646. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  50647. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  50648. };
  50649. const byte key[] =
  50650. {
  50651. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  50652. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  50653. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  50654. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  50655. };
  50656. byte out[AES_BLOCK_SIZE];
  50657. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  50658. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50659. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  50660. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  50661. #ifdef HAVE_AES_DECRYPT
  50662. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  50663. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50664. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  50665. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  50666. #endif
  50667. /* test bad arguments */
  50668. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  50669. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  50670. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  50671. #endif
  50672. return EXPECT_RESULT();
  50673. }
  50674. static int test_wolfSSL_AES_cbc_encrypt(void)
  50675. {
  50676. EXPECT_DECLS;
  50677. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  50678. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  50679. AES_KEY aes;
  50680. AES_KEY* aesN = NULL;
  50681. size_t len = 0;
  50682. size_t lenB = 0;
  50683. int keySz0 = 0;
  50684. int keySzN = -1;
  50685. byte out[AES_BLOCK_SIZE] = {0};
  50686. byte* outN = NULL;
  50687. /* Test vectors retrieved from:
  50688. * <begin URL>
  50689. * https://csrc.nist.gov/
  50690. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  50691. * documents/aes/KAT_AES.zip
  50692. * </end URL>
  50693. */
  50694. const byte* pt128N = NULL;
  50695. byte* key128N = NULL;
  50696. byte* iv128N = NULL;
  50697. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  50698. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50699. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  50700. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  50701. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  50702. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50703. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  50704. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  50705. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  50706. len = sizeof(pt128);
  50707. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  50708. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  50709. ExpectIntNE(XMEMCMP(b, g, h), i)
  50710. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  50711. /* Stressing wolfSSL_AES_cbc_encrypt() */
  50712. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  50713. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  50714. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  50715. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  50716. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  50717. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  50718. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  50719. /* Stressing wolfSSL_AES_set_encrypt_key */
  50720. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  50721. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  50722. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  50723. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  50724. /* Stressing wolfSSL_AES_set_decrypt_key */
  50725. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  50726. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  50727. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  50728. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  50729. #ifdef WOLFSSL_AES_128
  50730. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  50731. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50732. RESET_IV(iv128tmp, iv128);
  50733. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  50734. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  50735. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  50736. wc_AesFree((Aes*)&aes);
  50737. #ifdef HAVE_AES_DECRYPT
  50738. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  50739. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50740. RESET_IV(iv128tmp, iv128);
  50741. len = sizeof(ct128);
  50742. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  50743. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  50744. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  50745. wc_AesFree((Aes*)&aes);
  50746. #endif
  50747. #endif /* WOLFSSL_AES_128 */
  50748. #ifdef WOLFSSL_AES_192
  50749. {
  50750. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  50751. * Appendix F.2.3 */
  50752. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  50753. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  50754. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  50755. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  50756. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  50757. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  50758. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  50759. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  50760. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  50761. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  50762. len = sizeof(pt192);
  50763. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  50764. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50765. RESET_IV(iv192tmp, iv192);
  50766. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  50767. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  50768. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  50769. wc_AesFree((Aes*)&aes);
  50770. #ifdef HAVE_AES_DECRYPT
  50771. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  50772. len = sizeof(ct192);
  50773. RESET_IV(iv192tmp, iv192);
  50774. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50775. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  50776. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  50777. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  50778. wc_AesFree((Aes*)&aes);
  50779. #endif
  50780. }
  50781. #endif /* WOLFSSL_AES_192 */
  50782. #ifdef WOLFSSL_AES_256
  50783. {
  50784. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  50785. * Appendix F.2.5 */
  50786. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  50787. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  50788. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  50789. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  50790. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  50791. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  50792. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  50793. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  50794. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  50795. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  50796. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  50797. len = sizeof(pt256);
  50798. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  50799. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50800. RESET_IV(iv256tmp, iv256);
  50801. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  50802. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  50803. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  50804. wc_AesFree((Aes*)&aes);
  50805. #ifdef HAVE_AES_DECRYPT
  50806. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  50807. len = sizeof(ct256);
  50808. RESET_IV(iv256tmp, iv256);
  50809. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50810. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  50811. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  50812. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  50813. wc_AesFree((Aes*)&aes);
  50814. #endif
  50815. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  50816. !defined(HAVE_SELFTEST)
  50817. {
  50818. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  50819. byte wrapPlain[sizeof(key256)] = { 0 };
  50820. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  50821. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  50822. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  50823. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  50824. 15), WOLFSSL_FAILURE);
  50825. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  50826. sizeof(key256)), sizeof(wrapCipher));
  50827. wc_AesFree((Aes*)&aes);
  50828. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  50829. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  50830. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  50831. 23), WOLFSSL_FAILURE);
  50832. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  50833. sizeof(wrapCipher)), sizeof(wrapPlain));
  50834. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  50835. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  50836. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  50837. wc_AesFree((Aes*)&aes);
  50838. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  50839. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  50840. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  50841. sizeof(key256)), sizeof(wrapCipher));
  50842. wc_AesFree((Aes*)&aes);
  50843. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  50844. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  50845. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  50846. sizeof(wrapCipher)), sizeof(wrapPlain));
  50847. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  50848. wc_AesFree((Aes*)&aes);
  50849. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  50850. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  50851. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  50852. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  50853. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  50854. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  50855. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  50856. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  50857. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  50858. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  50859. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  50860. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  50861. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  50862. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  50863. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  50864. 0);
  50865. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  50866. 0);
  50867. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  50868. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  50869. }
  50870. #endif /* HAVE_AES_KEYWRAP */
  50871. }
  50872. #endif /* WOLFSSL_AES_256 */
  50873. #endif
  50874. return EXPECT_RESULT();
  50875. }
  50876. static int test_wolfSSL_AES_cfb128_encrypt(void)
  50877. {
  50878. EXPECT_DECLS;
  50879. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  50880. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  50881. AES_KEY aesEnc;
  50882. AES_KEY aesDec;
  50883. const byte msg[] = {
  50884. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  50885. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  50886. };
  50887. const byte exp[] = {
  50888. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  50889. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  50890. };
  50891. const byte key[] = {
  50892. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  50893. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  50894. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  50895. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  50896. };
  50897. const byte ivData[] = {
  50898. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  50899. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  50900. };
  50901. byte out[AES_BLOCK_SIZE];
  50902. byte iv[AES_BLOCK_SIZE];
  50903. word32 i;
  50904. int num;
  50905. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  50906. XMEMCPY(iv, ivData, sizeof(iv));
  50907. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50908. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  50909. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  50910. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  50911. #ifdef HAVE_AES_DECRYPT
  50912. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  50913. XMEMCPY(iv, ivData, sizeof(iv));
  50914. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50915. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  50916. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  50917. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  50918. #endif
  50919. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  50920. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  50921. XMEMCPY(iv, ivData, sizeof(iv));
  50922. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50923. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  50924. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  50925. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  50926. if (i == 0) {
  50927. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  50928. }
  50929. else {
  50930. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  50931. }
  50932. #ifdef HAVE_AES_DECRYPT
  50933. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  50934. XMEMCPY(iv, ivData, sizeof(iv));
  50935. XMEMSET(out, 0, AES_BLOCK_SIZE);
  50936. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  50937. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  50938. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  50939. if (i == 0) {
  50940. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  50941. }
  50942. else {
  50943. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  50944. }
  50945. #endif
  50946. }
  50947. if (EXPECT_SUCCESS()) {
  50948. /* test bad arguments */
  50949. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  50950. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  50951. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  50952. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  50953. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  50954. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  50955. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  50956. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  50957. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  50958. }
  50959. #endif
  50960. return EXPECT_RESULT();
  50961. }
  50962. static int test_wolfSSL_CRYPTO_cts128(void)
  50963. {
  50964. EXPECT_DECLS;
  50965. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  50966. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  50967. byte tmp[64]; /* Largest vector size */
  50968. /* Test vectors taken form RFC3962 Appendix B */
  50969. const testVector vects[] = {
  50970. {
  50971. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  50972. "\x20",
  50973. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  50974. "\x97",
  50975. 17, 17
  50976. },
  50977. {
  50978. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  50979. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  50980. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  50981. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  50982. 31, 31
  50983. },
  50984. {
  50985. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  50986. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  50987. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  50988. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  50989. 32, 32
  50990. },
  50991. {
  50992. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  50993. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  50994. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  50995. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  50996. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  50997. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  50998. 47, 47
  50999. },
  51000. {
  51001. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  51002. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  51003. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  51004. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  51005. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  51006. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  51007. 48, 48
  51008. },
  51009. {
  51010. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  51011. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  51012. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  51013. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  51014. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  51015. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  51016. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  51017. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  51018. 64, 64
  51019. }
  51020. };
  51021. byte keyBytes[AES_128_KEY_SIZE] = {
  51022. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  51023. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  51024. };
  51025. size_t i;
  51026. AES_KEY encKey;
  51027. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  51028. XMEMSET(tmp, 0, sizeof(tmp));
  51029. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  51030. AES_KEY decKey;
  51031. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  51032. &encKey), 0);
  51033. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  51034. &decKey), 0);
  51035. XMEMSET(iv, 0, sizeof(iv));
  51036. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  51037. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  51038. vects[i].outLen);
  51039. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  51040. XMEMSET(iv, 0, sizeof(iv));
  51041. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  51042. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  51043. vects[i].inLen);
  51044. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  51045. }
  51046. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  51047. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  51048. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  51049. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  51050. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  51051. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  51052. (cbc128_f)AES_cbc_encrypt), 0);
  51053. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  51054. (cbc128_f)AES_cbc_encrypt), 0);
  51055. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  51056. (cbc128_f)AES_cbc_encrypt), 0);
  51057. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  51058. (cbc128_f)AES_cbc_encrypt), 0);
  51059. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  51060. (cbc128_f)AES_cbc_encrypt), 0);
  51061. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  51062. /* Length too small. */
  51063. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  51064. (cbc128_f)AES_cbc_encrypt), 0);
  51065. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  51066. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  51067. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  51068. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  51069. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  51070. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  51071. (cbc128_f)AES_cbc_encrypt), 0);
  51072. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  51073. (cbc128_f)AES_cbc_encrypt), 0);
  51074. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  51075. (cbc128_f)AES_cbc_encrypt), 0);
  51076. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  51077. (cbc128_f)AES_cbc_encrypt), 0);
  51078. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  51079. (cbc128_f)AES_cbc_encrypt), 0);
  51080. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  51081. /* Length too small. */
  51082. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  51083. (cbc128_f)AES_cbc_encrypt), 0);
  51084. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  51085. return EXPECT_RESULT();
  51086. }
  51087. static int test_wolfSSL_RC4(void)
  51088. {
  51089. EXPECT_DECLS;
  51090. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  51091. WOLFSSL_RC4_KEY rc4Key;
  51092. unsigned char key[] = {
  51093. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  51094. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  51095. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  51096. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  51097. };
  51098. unsigned char data[] = {
  51099. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  51100. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  51101. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  51102. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  51103. };
  51104. unsigned char enc[sizeof(data)];
  51105. unsigned char dec[sizeof(data)];
  51106. word32 i;
  51107. word32 j;
  51108. wolfSSL_RC4_set_key(NULL, -1, NULL);
  51109. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  51110. wolfSSL_RC4_set_key(NULL, 0, NULL);
  51111. wolfSSL_RC4_set_key(NULL, -1, key);
  51112. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  51113. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  51114. wolfSSL_RC4_set_key(NULL, 0, key);
  51115. wolfSSL_RC4(NULL, 0, NULL, NULL);
  51116. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  51117. wolfSSL_RC4(NULL, 0, data, NULL);
  51118. wolfSSL_RC4(NULL, 0, NULL, enc);
  51119. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  51120. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  51121. wolfSSL_RC4(NULL, 0, data, enc);
  51122. ExpectIntEQ(1, 1);
  51123. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  51124. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  51125. XMEMSET(enc, 0, sizeof(enc));
  51126. XMEMSET(dec, 0, sizeof(dec));
  51127. /* Encrypt */
  51128. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  51129. wolfSSL_RC4(&rc4Key, j, data, enc);
  51130. /* Decrypt */
  51131. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  51132. wolfSSL_RC4(&rc4Key, j, enc, dec);
  51133. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  51134. }
  51135. }
  51136. #endif
  51137. return EXPECT_RESULT();
  51138. }
  51139. static int test_wolfSSL_OBJ(void)
  51140. {
  51141. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  51142. * mode
  51143. */
  51144. EXPECT_DECLS;
  51145. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  51146. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  51147. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  51148. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  51149. ASN1_OBJECT *obj = NULL;
  51150. ASN1_OBJECT *obj2 = NULL;
  51151. char buf[50];
  51152. XFILE fp = XBADFILE;
  51153. X509 *x509 = NULL;
  51154. X509_NAME *x509Name = NULL;
  51155. X509_NAME_ENTRY *x509NameEntry = NULL;
  51156. ASN1_OBJECT *asn1Name = NULL;
  51157. int numNames = 0;
  51158. BIO *bio = NULL;
  51159. int nid;
  51160. int i, j;
  51161. const char *f[] = {
  51162. #ifndef NO_RSA
  51163. "./certs/ca-cert.der",
  51164. #endif
  51165. #ifdef HAVE_ECC
  51166. "./certs/ca-ecc-cert.der",
  51167. "./certs/ca-ecc384-cert.der",
  51168. #endif
  51169. NULL};
  51170. ASN1_OBJECT *field_name_obj = NULL;
  51171. int lastpos = -1;
  51172. int tmp = -1;
  51173. ASN1_STRING *asn1 = NULL;
  51174. unsigned char *buf_dyn = NULL;
  51175. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  51176. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  51177. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  51178. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  51179. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  51180. ASN1_OBJECT_free(obj);
  51181. obj = NULL;
  51182. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  51183. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  51184. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  51185. #ifdef WOLFSSL_CERT_EXT
  51186. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  51187. #endif
  51188. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  51189. ExpectNotNull(obj2 = OBJ_dup(obj));
  51190. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  51191. ASN1_OBJECT_free(obj);
  51192. obj = NULL;
  51193. ASN1_OBJECT_free(obj2);
  51194. obj2 = NULL;
  51195. for (i = 0; f[i] != NULL; i++)
  51196. {
  51197. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  51198. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  51199. if (fp != XBADFILE) {
  51200. XFCLOSE(fp);
  51201. fp = XBADFILE;
  51202. }
  51203. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  51204. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  51205. /* Get the Common Name by using OBJ_txt2obj */
  51206. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  51207. do
  51208. {
  51209. lastpos = tmp;
  51210. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  51211. } while (tmp > -1);
  51212. ExpectIntNE(lastpos, -1);
  51213. ASN1_OBJECT_free(field_name_obj);
  51214. field_name_obj = NULL;
  51215. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  51216. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  51217. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  51218. /*
  51219. * All Common Names should be www.wolfssl.com
  51220. * This makes testing easier as we can test for the expected value.
  51221. */
  51222. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  51223. OPENSSL_free(buf_dyn);
  51224. buf_dyn = NULL;
  51225. bio = BIO_new(BIO_s_mem());
  51226. ExpectTrue(bio != NULL);
  51227. for (j = 0; j < numNames; j++)
  51228. {
  51229. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  51230. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  51231. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  51232. }
  51233. BIO_free(bio);
  51234. bio = NULL;
  51235. X509_free(x509);
  51236. x509 = NULL;
  51237. }
  51238. #ifdef HAVE_PKCS12
  51239. {
  51240. PKCS12 *p12 = NULL;
  51241. int boolRet;
  51242. EVP_PKEY *pkey = NULL;
  51243. const char *p12_f[] = {
  51244. #if !defined(NO_DES3) && !defined(NO_RSA)
  51245. "./certs/test-servercert.p12",
  51246. #endif
  51247. NULL};
  51248. for (i = 0; p12_f[i] != NULL; i++)
  51249. {
  51250. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  51251. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  51252. if (fp != XBADFILE) {
  51253. XFCLOSE(fp);
  51254. fp = XBADFILE;
  51255. }
  51256. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  51257. &pkey, &x509, NULL)) > 0);
  51258. wc_PKCS12_free(p12);
  51259. p12 = NULL;
  51260. EVP_PKEY_free(pkey);
  51261. x509Name = X509_get_issuer_name(x509);
  51262. ExpectNotNull(x509Name);
  51263. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  51264. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  51265. for (j = 0; j < numNames; j++)
  51266. {
  51267. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  51268. ExpectNotNull(asn1Name =
  51269. X509_NAME_ENTRY_get_object(x509NameEntry));
  51270. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  51271. }
  51272. BIO_free(bio);
  51273. bio = NULL;
  51274. X509_free(x509);
  51275. x509 = NULL;
  51276. }
  51277. }
  51278. #endif /* HAVE_PKCS12 */
  51279. #endif
  51280. return EXPECT_RESULT();
  51281. }
  51282. static int test_wolfSSL_OBJ_cmp(void)
  51283. {
  51284. EXPECT_DECLS;
  51285. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  51286. ASN1_OBJECT *obj = NULL;
  51287. ASN1_OBJECT *obj2 = NULL;
  51288. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  51289. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  51290. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  51291. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  51292. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  51293. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  51294. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  51295. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  51296. ASN1_OBJECT_free(obj);
  51297. ASN1_OBJECT_free(obj2);
  51298. #endif
  51299. return EXPECT_RESULT();
  51300. }
  51301. static int test_wolfSSL_OBJ_txt2nid(void)
  51302. {
  51303. EXPECT_DECLS;
  51304. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  51305. defined(WOLFSSL_APACHE_HTTPD)
  51306. int i;
  51307. static const struct {
  51308. const char* sn;
  51309. const char* ln;
  51310. const char* oid;
  51311. int nid;
  51312. } testVals[] = {
  51313. #ifdef WOLFSSL_APACHE_HTTPD
  51314. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  51315. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  51316. NID_id_on_dnsSRV },
  51317. { "msUPN", "Microsoft User Principal Name",
  51318. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  51319. #endif
  51320. { NULL, NULL, NULL, NID_undef }
  51321. };
  51322. /* Invalid cases */
  51323. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  51324. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  51325. /* Valid cases */
  51326. for (i = 0; testVals[i].sn != NULL; i++) {
  51327. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  51328. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  51329. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  51330. }
  51331. #endif
  51332. return EXPECT_RESULT();
  51333. }
  51334. static int test_wolfSSL_OBJ_txt2obj(void)
  51335. {
  51336. EXPECT_DECLS;
  51337. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  51338. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  51339. int i;
  51340. char buf[50];
  51341. ASN1_OBJECT* obj = NULL;
  51342. static const struct {
  51343. const char* oidStr;
  51344. const char* sn;
  51345. const char* ln;
  51346. } objs_list[] = {
  51347. #if defined(WOLFSSL_APACHE_HTTPD)
  51348. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  51349. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  51350. #endif
  51351. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  51352. { NULL, NULL, NULL }
  51353. };
  51354. static const struct {
  51355. const char* numeric;
  51356. const char* name;
  51357. } objs_named[] = {
  51358. /* In dictionary but not in normal list. */
  51359. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  51360. /* Made up OID. */
  51361. { "1.3.5.7", "1.3.5.7" },
  51362. { NULL, NULL }
  51363. };
  51364. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  51365. ASN1_OBJECT_free(obj);
  51366. obj = NULL;
  51367. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  51368. ASN1_OBJECT_free(obj);
  51369. obj = NULL;
  51370. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  51371. /* Test numerical value of oid (oidStr) */
  51372. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  51373. /* Convert object back to text to confirm oid is correct */
  51374. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  51375. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  51376. ASN1_OBJECT_free(obj);
  51377. obj = NULL;
  51378. XMEMSET(buf, 0, sizeof(buf));
  51379. /* Test short name (sn) */
  51380. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  51381. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  51382. /* Convert object back to text to confirm oid is correct */
  51383. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  51384. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  51385. ASN1_OBJECT_free(obj);
  51386. obj = NULL;
  51387. XMEMSET(buf, 0, sizeof(buf));
  51388. /* Test long name (ln) - should fail when no_name = 1 */
  51389. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  51390. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  51391. /* Convert object back to text to confirm oid is correct */
  51392. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  51393. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  51394. ASN1_OBJECT_free(obj);
  51395. obj = NULL;
  51396. XMEMSET(buf, 0, sizeof(buf));
  51397. }
  51398. for (i = 0; objs_named[i].numeric != NULL; i++) {
  51399. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  51400. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  51401. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  51402. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  51403. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  51404. ASN1_OBJECT_free(obj);
  51405. obj = NULL;
  51406. }
  51407. #endif
  51408. return EXPECT_RESULT();
  51409. }
  51410. static int test_wolfSSL_PEM_write_bio_X509(void)
  51411. {
  51412. EXPECT_DECLS;
  51413. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  51414. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  51415. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  51416. !defined(NO_FILESYSTEM)
  51417. /* This test contains the hard coded expected
  51418. * lengths. Update if necessary */
  51419. XFILE fp = XBADFILE;
  51420. WOLFSSL_EVP_PKEY *priv = NULL;
  51421. BIO* input = NULL;
  51422. BIO* output = NULL;
  51423. X509* x509a = NULL;
  51424. X509* x509b = NULL;
  51425. ASN1_TIME* notBeforeA = NULL;
  51426. ASN1_TIME* notAfterA = NULL;
  51427. #ifndef NO_ASN_TIME
  51428. ASN1_TIME* notBeforeB = NULL;
  51429. ASN1_TIME* notAfterB = NULL;
  51430. #endif
  51431. int expectedLen;
  51432. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  51433. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  51434. if (fp != XBADFILE) {
  51435. XFCLOSE(fp);
  51436. fp = XBADFILE;
  51437. }
  51438. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  51439. "rb"));
  51440. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  51441. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  51442. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  51443. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  51444. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  51445. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  51446. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  51447. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  51448. /* compare length against expected */
  51449. expectedLen = 2000;
  51450. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  51451. #ifndef NO_ASN_TIME
  51452. /* read exported X509 PEM back into struct, sanity check on export,
  51453. * make sure notBefore/notAfter are the same and certs are identical. */
  51454. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  51455. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  51456. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  51457. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  51458. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  51459. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  51460. X509_free(x509b);
  51461. x509b = NULL;
  51462. #endif
  51463. /* Reset output buffer */
  51464. BIO_free(output);
  51465. output = NULL;
  51466. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  51467. /* Test forcing the AKID to be generated just from KeyIdentifier */
  51468. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  51469. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  51470. x509a->authKeyId = x509a->authKeyIdSrc;
  51471. x509a->authKeyIdSrc = NULL;
  51472. x509a->authKeyIdSrcSz = 0;
  51473. }
  51474. /* Resign to re-generate the der */
  51475. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  51476. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  51477. /* Check that we generate a smaller output since the AKID will
  51478. * only contain the KeyIdentifier without any additional
  51479. * information */
  51480. /* Here we copy the validity struct from the original */
  51481. expectedLen = 1688;
  51482. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  51483. /* Reset buffers and x509 */
  51484. BIO_free(input);
  51485. input = NULL;
  51486. BIO_free(output);
  51487. output = NULL;
  51488. X509_free(x509a);
  51489. x509a = NULL;
  51490. /* test CA and basicConstSet values are encoded when
  51491. * the cert is a CA */
  51492. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  51493. /* read PEM into X509 struct */
  51494. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  51495. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  51496. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  51497. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  51498. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  51499. * values are maintained and certs are identical.*/
  51500. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  51501. ExpectIntEQ(x509b->isCa, 1);
  51502. ExpectIntEQ(x509b->basicConstSet, 1);
  51503. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  51504. X509_free(x509a);
  51505. x509a = NULL;
  51506. X509_free(x509b);
  51507. x509b = NULL;
  51508. BIO_free(input);
  51509. input = NULL;
  51510. BIO_free(output);
  51511. output = NULL;
  51512. /* test CA and basicConstSet values are encoded when
  51513. * the cert is not CA */
  51514. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  51515. /* read PEM into X509 struct */
  51516. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  51517. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  51518. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  51519. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  51520. /* read exported X509 PEM back into struct, ensure isCa and
  51521. * basicConstSet values are maintained and certs are identical */
  51522. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  51523. ExpectIntEQ(x509b->isCa, 0);
  51524. ExpectIntEQ(x509b->basicConstSet, 1);
  51525. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  51526. wolfSSL_EVP_PKEY_free(priv);
  51527. X509_free(x509a);
  51528. X509_free(x509b);
  51529. BIO_free(input);
  51530. BIO_free(output);
  51531. #endif
  51532. return EXPECT_RESULT();
  51533. }
  51534. static int test_wolfSSL_X509_NAME_ENTRY(void)
  51535. {
  51536. EXPECT_DECLS;
  51537. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  51538. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  51539. X509* x509 = NULL;
  51540. #ifndef NO_BIO
  51541. BIO* bio = NULL;
  51542. #endif
  51543. X509_NAME* nm = NULL;
  51544. X509_NAME_ENTRY* entry = NULL;
  51545. unsigned char cn[] = "another name to add";
  51546. #ifdef OPENSSL_ALL
  51547. int i;
  51548. int names_len = 0;
  51549. #endif
  51550. ExpectNotNull(x509 =
  51551. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  51552. #ifndef NO_BIO
  51553. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51554. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  51555. #endif
  51556. #ifdef WOLFSSL_CERT_REQ
  51557. {
  51558. X509_REQ* req = NULL;
  51559. #ifndef NO_BIO
  51560. BIO* bReq = NULL;
  51561. #endif
  51562. ExpectNotNull(req =
  51563. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  51564. #ifndef NO_BIO
  51565. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  51566. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  51567. BIO_free(bReq);
  51568. #endif
  51569. X509_free(req);
  51570. }
  51571. #endif
  51572. ExpectNotNull(nm = X509_get_subject_name(x509));
  51573. /* Test add entry */
  51574. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  51575. 0x0c, cn, (int)sizeof(cn)));
  51576. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  51577. #ifdef WOLFSSL_CERT_EXT
  51578. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  51579. (byte*)"support@wolfssl.com", 19, -1,
  51580. 1), WOLFSSL_SUCCESS);
  51581. #endif
  51582. X509_NAME_ENTRY_free(entry);
  51583. entry = NULL;
  51584. #ifdef WOLFSSL_CERT_REQ
  51585. {
  51586. unsigned char srv_pkcs9p[] = "Server";
  51587. unsigned char fvrtDrnk[] = "tequila";
  51588. unsigned char* der = NULL;
  51589. char* subject = NULL;
  51590. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  51591. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  51592. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  51593. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  51594. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  51595. ExpectNotNull(der);
  51596. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  51597. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  51598. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  51599. #ifdef DEBUG_WOLFSSL
  51600. if (subject != NULL) {
  51601. fprintf(stderr, "\n\t%s\n", subject);
  51602. }
  51603. #endif
  51604. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51605. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  51606. }
  51607. #endif
  51608. /* Test add entry by text */
  51609. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  51610. 0x0c, cn, (int)sizeof(cn)));
  51611. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  51612. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  51613. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  51614. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  51615. #endif
  51616. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  51617. X509_NAME_ENTRY_free(entry);
  51618. entry = NULL;
  51619. /* Test add entry by NID */
  51620. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  51621. cn, -1, -1, 0), SSL_SUCCESS);
  51622. #ifdef OPENSSL_ALL
  51623. /* stack of name entry */
  51624. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  51625. for (i = 0; i < names_len; i++) {
  51626. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  51627. }
  51628. #endif
  51629. #ifndef NO_BIO
  51630. BIO_free(bio);
  51631. #endif
  51632. X509_free(x509); /* free's nm */
  51633. #endif
  51634. return EXPECT_RESULT();
  51635. }
  51636. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  51637. static int test_GENERAL_NAME_set0_othername(void) {
  51638. EXPECT_DECLS;
  51639. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  51640. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  51641. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  51642. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  51643. defined(WOLFSSL_FPKI)
  51644. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  51645. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  51646. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  51647. const char * cert_fname = "./certs/server-cert.der";
  51648. const char * key_fname = "./certs/server-key.der";
  51649. X509* x509 = NULL;
  51650. GENERAL_NAME* gn = NULL;
  51651. GENERAL_NAMES* gns = NULL;
  51652. ASN1_OBJECT* upn_oid = NULL;
  51653. ASN1_UTF8STRING *utf8str = NULL;
  51654. ASN1_TYPE *value = NULL;
  51655. X509_EXTENSION * ext = NULL;
  51656. byte* pt = NULL;
  51657. byte der[4096];
  51658. int derSz = 0;
  51659. EVP_PKEY* priv = NULL;
  51660. XFILE f = XBADFILE;
  51661. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  51662. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  51663. if (f != XBADFILE) {
  51664. XFCLOSE(f);
  51665. f = XBADFILE;
  51666. }
  51667. ExpectNotNull(gn = GENERAL_NAME_new());
  51668. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  51669. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  51670. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  51671. ExpectNotNull(value = ASN1_TYPE_new());
  51672. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  51673. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  51674. wolfSSL_ASN1_STRING_free(utf8str);
  51675. }
  51676. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  51677. if (EXPECT_FAIL()) {
  51678. ASN1_TYPE_free(value);
  51679. }
  51680. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  51681. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  51682. if (EXPECT_FAIL()) {
  51683. GENERAL_NAME_free(gn);
  51684. gn = NULL;
  51685. }
  51686. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  51687. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  51688. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  51689. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  51690. if (f != XBADFILE) {
  51691. XFCLOSE(f);
  51692. f = XBADFILE;
  51693. }
  51694. pt = der;
  51695. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  51696. (const unsigned char**)&pt, derSz));
  51697. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  51698. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  51699. gns = NULL;
  51700. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  51701. NULL));
  51702. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  51703. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  51704. ExpectIntEQ(gn->type, 0);
  51705. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  51706. ASN1_OBJECT_free(upn_oid);
  51707. X509_EXTENSION_free(ext);
  51708. X509_free(x509);
  51709. EVP_PKEY_free(priv);
  51710. #endif
  51711. return EXPECT_RESULT();
  51712. }
  51713. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  51714. static int test_othername_and_SID_ext(void) {
  51715. EXPECT_DECLS;
  51716. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  51717. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  51718. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  51719. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  51720. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  51721. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  51722. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  51723. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  51724. const char* csr_fname = "./certs/csr.signed.der";
  51725. const char* key_fname = "./certs/server-key.der";
  51726. byte der[4096];
  51727. int derSz = 0;
  51728. X509_REQ* x509 = NULL;
  51729. STACK_OF(X509_EXTENSION) *exts = NULL;
  51730. X509_EXTENSION * san_ext = NULL;
  51731. X509_EXTENSION * ext = NULL;
  51732. GENERAL_NAME* gn = NULL;
  51733. GENERAL_NAMES* gns = NULL;
  51734. ASN1_OBJECT* upn_oid = NULL;
  51735. ASN1_UTF8STRING *utf8str = NULL;
  51736. ASN1_TYPE *value = NULL;
  51737. ASN1_STRING *extval = NULL;
  51738. /* SID extension. SID data format explained here:
  51739. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  51740. */
  51741. byte SidExtension[] = {
  51742. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  51743. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  51744. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  51745. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  51746. byte expectedAltName[] = {
  51747. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  51748. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  51749. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  51750. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  51751. X509_EXTENSION *sid_ext = NULL;
  51752. ASN1_OBJECT* sid_oid = NULL;
  51753. ASN1_OCTET_STRING *sid_data = NULL;
  51754. EVP_PKEY* priv = NULL;
  51755. XFILE f = XBADFILE;
  51756. byte* pt = NULL;
  51757. BIO* bio = NULL;
  51758. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  51759. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  51760. if (f != XBADFILE) {
  51761. XFCLOSE(f);
  51762. f = XBADFILE;
  51763. }
  51764. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  51765. ExpectNotNull(gn = GENERAL_NAME_new());
  51766. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  51767. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  51768. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  51769. ExpectNotNull(value = ASN1_TYPE_new());
  51770. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  51771. if (EXPECT_FAIL()) {
  51772. ASN1_UTF8STRING_free(utf8str);
  51773. }
  51774. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  51775. if (EXPECT_FAIL()) {
  51776. ASN1_TYPE_free(value);
  51777. GENERAL_NAME_free(gn);
  51778. gn = NULL;
  51779. }
  51780. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  51781. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  51782. if (EXPECT_FAIL()) {
  51783. GENERAL_NAME_free(gn);
  51784. }
  51785. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  51786. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  51787. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  51788. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  51789. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  51790. sid_data));
  51791. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  51792. /* Ensure an empty stack doesn't raise an error. */
  51793. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  51794. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  51795. if (EXPECT_FAIL()) {
  51796. X509_EXTENSION_free(san_ext);
  51797. }
  51798. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  51799. if (EXPECT_FAIL()) {
  51800. X509_EXTENSION_free(sid_ext);
  51801. }
  51802. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  51803. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  51804. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  51805. if (f != XBADFILE)
  51806. XFCLOSE(f);
  51807. pt = der;
  51808. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  51809. (const unsigned char**)&pt, derSz));
  51810. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  51811. pt = der;
  51812. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  51813. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  51814. gns = NULL;
  51815. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  51816. exts = NULL;
  51817. ASN1_OBJECT_free(upn_oid);
  51818. ASN1_OBJECT_free(sid_oid);
  51819. ASN1_OCTET_STRING_free(sid_data);
  51820. X509_REQ_free(x509);
  51821. x509 = NULL;
  51822. EVP_PKEY_free(priv);
  51823. /* At this point everything used to generate what is in der is cleaned up.
  51824. * We now read back from der to confirm the extensions were inserted
  51825. * correctly. */
  51826. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  51827. ExpectNotNull(bio);
  51828. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  51829. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  51830. ExpectNotNull(x509);
  51831. BIO_free(bio);
  51832. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  51833. x509));
  51834. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  51835. /* Check the SID extension. */
  51836. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  51837. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  51838. ExpectIntEQ(extval->length, sizeof(SidExtension));
  51839. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  51840. /* Check the AltNames extension. */
  51841. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  51842. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  51843. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  51844. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  51845. 0);
  51846. /* Cleanup */
  51847. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  51848. NULL));
  51849. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  51850. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  51851. ExpectIntEQ(gn->type, 0);
  51852. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  51853. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  51854. X509_REQ_free(x509);
  51855. #endif
  51856. return EXPECT_RESULT();
  51857. }
  51858. static int test_wolfSSL_X509_set_name(void)
  51859. {
  51860. EXPECT_DECLS;
  51861. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  51862. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  51863. X509* x509 = NULL;
  51864. X509_NAME* name = NULL;
  51865. ExpectNotNull(name = X509_NAME_new());
  51866. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  51867. (byte*)"wolfssl.com", 11, 0, 1),
  51868. WOLFSSL_SUCCESS);
  51869. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  51870. (byte*)"support@wolfssl.com", 19, -1,
  51871. 1), WOLFSSL_SUCCESS);
  51872. ExpectNotNull(x509 = X509_new());
  51873. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  51874. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  51875. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  51876. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  51877. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  51878. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  51879. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  51880. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  51881. X509_free(x509);
  51882. X509_NAME_free(name);
  51883. #endif /* OPENSSL_ALL && !NO_CERTS */
  51884. return EXPECT_RESULT();
  51885. }
  51886. static int test_wolfSSL_X509_set_notAfter(void)
  51887. {
  51888. EXPECT_DECLS;
  51889. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  51890. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  51891. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  51892. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  51893. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  51894. /* Generalized time will overflow time_t if not long */
  51895. X509* x = NULL;
  51896. BIO* bio = NULL;
  51897. ASN1_TIME *asn_time = NULL;
  51898. ASN1_TIME *time_check = NULL;
  51899. const int year = 365*24*60*60;
  51900. const int day = 24*60*60;
  51901. const int hour = 60*60;
  51902. const int mini = 60;
  51903. int offset_day;
  51904. unsigned char buf[25];
  51905. time_t t;
  51906. /*
  51907. * Setup asn_time. APACHE HTTPD uses time(NULL)
  51908. */
  51909. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  51910. offset_day = 7;
  51911. /*
  51912. * Free these.
  51913. */
  51914. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  51915. ExpectNotNull(asn_time);
  51916. ExpectNotNull(x = X509_new());
  51917. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51918. /*
  51919. * Tests
  51920. */
  51921. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  51922. /* time_check is simply (ANS1_TIME*)x->notAfter */
  51923. ExpectNotNull(time_check = X509_get_notAfter(x));
  51924. /* ANS1_TIME_check validates by checking if argument can be parsed */
  51925. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  51926. /* Convert to human readable format and compare to intended date */
  51927. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  51928. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  51929. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  51930. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  51931. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  51932. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  51933. /*
  51934. * Cleanup
  51935. */
  51936. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  51937. X509_free(x);
  51938. BIO_free(bio);
  51939. #endif
  51940. return EXPECT_RESULT();
  51941. }
  51942. static int test_wolfSSL_X509_set_notBefore(void)
  51943. {
  51944. EXPECT_DECLS;
  51945. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  51946. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  51947. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  51948. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  51949. X509* x = NULL;
  51950. BIO* bio = NULL;
  51951. ASN1_TIME *asn_time = NULL;
  51952. ASN1_TIME *time_check = NULL;
  51953. const int year = 365*24*60*60;
  51954. const int day = 24*60*60;
  51955. const int hour = 60*60;
  51956. const int mini = 60;
  51957. int offset_day;
  51958. unsigned char buf[25];
  51959. time_t t;
  51960. /*
  51961. * Setup asn_time. APACHE HTTPD uses time(NULL)
  51962. */
  51963. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  51964. offset_day = 7;
  51965. /*
  51966. * Free these.
  51967. */
  51968. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  51969. ExpectNotNull(asn_time);
  51970. ExpectNotNull(x = X509_new());
  51971. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51972. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  51973. /*
  51974. * Main Tests
  51975. */
  51976. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  51977. /* time_check == (ANS1_TIME*)x->notBefore */
  51978. ExpectNotNull(time_check = X509_get_notBefore(x));
  51979. /* ANS1_TIME_check validates by checking if argument can be parsed */
  51980. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  51981. /* Convert to human readable format and compare to intended date */
  51982. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  51983. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  51984. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  51985. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  51986. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  51987. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  51988. /*
  51989. * Cleanup
  51990. */
  51991. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  51992. X509_free(x);
  51993. BIO_free(bio);
  51994. #endif
  51995. return EXPECT_RESULT();
  51996. }
  51997. static int test_wolfSSL_X509_set_version(void)
  51998. {
  51999. EXPECT_DECLS;
  52000. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  52001. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  52002. X509* x509 = NULL;
  52003. long v = 2L;
  52004. long maxInt = INT_MAX;
  52005. ExpectNotNull(x509 = X509_new());
  52006. /* These should pass. */
  52007. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  52008. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  52009. /* Fail Case: When v(long) is greater than x509->version(int). */
  52010. v = maxInt+1;
  52011. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  52012. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  52013. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  52014. /* Cleanup */
  52015. X509_free(x509);
  52016. #endif
  52017. return EXPECT_RESULT();
  52018. }
  52019. #ifndef NO_BIO
  52020. static int test_wolfSSL_BIO_gets(void)
  52021. {
  52022. EXPECT_DECLS;
  52023. #if defined(OPENSSL_EXTRA)
  52024. BIO* bio = NULL;
  52025. BIO* bio2 = NULL;
  52026. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  52027. char emp[] = "";
  52028. char bio_buffer[20];
  52029. int bufferSz = 20;
  52030. #ifdef OPENSSL_ALL
  52031. BUF_MEM* emp_bm = NULL;
  52032. BUF_MEM* msg_bm = NULL;
  52033. #endif
  52034. /* try with bad args */
  52035. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  52036. #ifdef OPENSSL_ALL
  52037. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  52038. #endif
  52039. /* try with real msg */
  52040. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  52041. XMEMSET(bio_buffer, 0, bufferSz);
  52042. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  52043. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  52044. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  52045. ExpectFalse(bio2 != BIO_next(bio));
  52046. /* make buffer filled with no terminating characters */
  52047. XMEMSET(bio_buffer, 1, bufferSz);
  52048. /* BIO_gets reads a line of data */
  52049. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  52050. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  52051. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  52052. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  52053. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  52054. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  52055. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  52056. #ifdef OPENSSL_ALL
  52057. /* test setting the mem_buf manually */
  52058. BIO_free(bio);
  52059. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  52060. ExpectNotNull(emp_bm = BUF_MEM_new());
  52061. ExpectNotNull(msg_bm = BUF_MEM_new());
  52062. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  52063. if (EXPECT_SUCCESS()) {
  52064. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  52065. msg_bm->data = NULL;
  52066. }
  52067. /* emp size is 1 for terminator */
  52068. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  52069. if (EXPECT_SUCCESS()) {
  52070. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  52071. emp_bm->data = emp;
  52072. msg_bm->data = msg;
  52073. }
  52074. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  52075. /* check reading an empty string */
  52076. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  52077. ExpectStrEQ(emp, bio_buffer);
  52078. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  52079. /* BIO_gets reads a line of data */
  52080. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  52081. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  52082. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  52083. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  52084. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  52085. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  52086. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  52087. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  52088. if (EXPECT_SUCCESS())
  52089. emp_bm->data = NULL;
  52090. BUF_MEM_free(emp_bm);
  52091. if (EXPECT_SUCCESS())
  52092. msg_bm->data = NULL;
  52093. BUF_MEM_free(msg_bm);
  52094. #endif
  52095. /* check not null terminated string */
  52096. BIO_free(bio);
  52097. bio = NULL;
  52098. msg[0] = 0x33;
  52099. msg[1] = 0x33;
  52100. msg[2] = 0x33;
  52101. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  52102. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  52103. ExpectIntEQ(bio_buffer[0], msg[0]);
  52104. ExpectIntEQ(bio_buffer[1], msg[1]);
  52105. ExpectIntNE(bio_buffer[2], msg[2]);
  52106. BIO_free(bio);
  52107. bio = NULL;
  52108. msg[3] = 0x33;
  52109. bio_buffer[3] = 0x33;
  52110. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  52111. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  52112. ExpectIntEQ(bio_buffer[0], msg[0]);
  52113. ExpectIntEQ(bio_buffer[1], msg[1]);
  52114. ExpectIntEQ(bio_buffer[2], msg[2]);
  52115. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  52116. /* check reading an empty string */
  52117. BIO_free(bio);
  52118. bio = NULL;
  52119. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  52120. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  52121. ExpectStrEQ(emp, bio_buffer);
  52122. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  52123. /* check error cases */
  52124. BIO_free(bio);
  52125. bio = NULL;
  52126. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  52127. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52128. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  52129. #if !defined(NO_FILESYSTEM)
  52130. {
  52131. BIO* f_bio = NULL;
  52132. XFILE f = XBADFILE;
  52133. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  52134. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  52135. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  52136. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  52137. if (EXPECT_FAIL() && (f != XBADFILE)) {
  52138. XFCLOSE(f);
  52139. }
  52140. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  52141. BIO_free(f_bio);
  52142. f_bio = NULL;
  52143. }
  52144. #endif /* NO_FILESYSTEM */
  52145. BIO_free(bio);
  52146. bio = NULL;
  52147. BIO_free(bio2);
  52148. bio2 = NULL;
  52149. /* try with type BIO */
  52150. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  52151. sizeof(msg));
  52152. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  52153. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  52154. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  52155. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  52156. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  52157. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  52158. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  52159. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  52160. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  52161. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  52162. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  52163. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  52164. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  52165. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  52166. BIO_free(bio);
  52167. bio = NULL;
  52168. BIO_free(bio2);
  52169. bio2 = NULL;
  52170. /* check reading an empty string */
  52171. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  52172. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  52173. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  52174. ExpectStrEQ(emp, bio_buffer);
  52175. BIO_free(bio);
  52176. bio = NULL;
  52177. #endif
  52178. return EXPECT_RESULT();
  52179. }
  52180. static int test_wolfSSL_BIO_puts(void)
  52181. {
  52182. EXPECT_DECLS;
  52183. #if defined(OPENSSL_EXTRA)
  52184. BIO* bio = NULL;
  52185. char input[] = "hello\0world\n.....ok\n\0";
  52186. char output[128];
  52187. XMEMSET(output, 0, sizeof(output));
  52188. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52189. ExpectIntEQ(BIO_puts(bio, input), 5);
  52190. ExpectIntEQ(BIO_pending(bio), 5);
  52191. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  52192. ExpectIntEQ(BIO_pending(bio), 19);
  52193. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  52194. ExpectStrEQ(output, "helloworld\n");
  52195. ExpectIntEQ(BIO_pending(bio), 8);
  52196. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  52197. ExpectStrEQ(output, ".....ok\n");
  52198. ExpectIntEQ(BIO_pending(bio), 0);
  52199. ExpectIntEQ(BIO_puts(bio, ""), -1);
  52200. BIO_free(bio);
  52201. #endif
  52202. return EXPECT_RESULT();
  52203. }
  52204. static int test_wolfSSL_BIO_dump(void)
  52205. {
  52206. EXPECT_DECLS;
  52207. #if defined(OPENSSL_EXTRA)
  52208. BIO* bio;
  52209. static const unsigned char data[] = {
  52210. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  52211. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  52212. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  52213. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  52214. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  52215. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  52216. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  52217. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  52218. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  52219. 0xB4
  52220. };
  52221. /* Generated with OpenSSL. */
  52222. static const char expected[] =
  52223. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  52224. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  52225. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  52226. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  52227. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  52228. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  52229. static const char expectedAll[] =
  52230. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  52231. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  52232. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  52233. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  52234. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  52235. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  52236. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  52237. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  52238. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  52239. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  52240. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  52241. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  52242. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  52243. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  52244. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  52245. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  52246. char output[16 * 80];
  52247. int i;
  52248. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52249. /* Example key dumped. */
  52250. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  52251. sizeof(expected) - 1);
  52252. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  52253. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  52254. /* Try every possible value for a character. */
  52255. for (i = 0; i < 256; i++)
  52256. output[i] = i;
  52257. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  52258. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  52259. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  52260. BIO_free(bio);
  52261. #endif
  52262. return EXPECT_RESULT();
  52263. }
  52264. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  52265. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  52266. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  52267. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  52268. {
  52269. (void)ssl;
  52270. (void)buf;
  52271. (void)sz;
  52272. (void)ctx;
  52273. return WOLFSSL_CBIO_ERR_WANT_READ;
  52274. }
  52275. #endif
  52276. static int test_wolfSSL_BIO_should_retry(void)
  52277. {
  52278. EXPECT_DECLS;
  52279. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  52280. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  52281. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  52282. tcp_ready ready;
  52283. func_args server_args;
  52284. THREAD_TYPE serverThread;
  52285. SOCKET_T sockfd = 0;
  52286. WOLFSSL_CTX* ctx = NULL;
  52287. WOLFSSL* ssl = NULL;
  52288. char msg[64] = "hello wolfssl!";
  52289. char reply[1024];
  52290. int msgSz = (int)XSTRLEN(msg);
  52291. int ret;
  52292. BIO* bio = NULL;
  52293. XMEMSET(&server_args, 0, sizeof(func_args));
  52294. #ifdef WOLFSSL_TIRTOS
  52295. fdOpenSession(Task_self());
  52296. #endif
  52297. StartTCP();
  52298. InitTcpReady(&ready);
  52299. #if defined(USE_WINDOWS_API)
  52300. /* use RNG to get random port if using windows */
  52301. ready.port = GetRandomPort();
  52302. #endif
  52303. server_args.signal = &ready;
  52304. start_thread(test_server_nofail, &server_args, &serverThread);
  52305. wait_tcp_ready(&server_args);
  52306. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52307. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  52308. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  52309. #endif
  52310. ExpectIntEQ(WOLFSSL_SUCCESS,
  52311. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  52312. ExpectIntEQ(WOLFSSL_SUCCESS,
  52313. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  52314. ExpectIntEQ(WOLFSSL_SUCCESS,
  52315. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  52316. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  52317. /* force retry */
  52318. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  52319. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  52320. ExpectNotNull(ssl);
  52321. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  52322. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  52323. if (EXPECT_FAIL()) {
  52324. wolfSSL_free(ssl);
  52325. ssl = NULL;
  52326. }
  52327. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  52328. ExpectIntNE(BIO_should_retry(bio), 0);
  52329. ExpectIntEQ(BIO_should_read(bio), 0);
  52330. ExpectIntEQ(BIO_should_write(bio), 0);
  52331. /* now perform successful connection */
  52332. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  52333. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  52334. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  52335. ret = wolfSSL_get_error(ssl, -1);
  52336. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  52337. ExpectIntNE(BIO_should_retry(bio), 0);
  52338. if (ret == WOLFSSL_ERROR_WANT_READ)
  52339. ExpectIntEQ(BIO_should_read(bio), 1);
  52340. else
  52341. ExpectIntEQ(BIO_should_read(bio), 0);
  52342. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  52343. ExpectIntEQ(BIO_should_write(bio), 1);
  52344. else
  52345. ExpectIntEQ(BIO_should_write(bio), 0);
  52346. }
  52347. else {
  52348. ExpectIntEQ(BIO_should_retry(bio), 0);
  52349. ExpectIntEQ(BIO_should_read(bio), 0);
  52350. ExpectIntEQ(BIO_should_write(bio), 0);
  52351. }
  52352. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  52353. XSTRLEN("I hear you fa shizzle!")), 0);
  52354. BIO_free(bio);
  52355. wolfSSL_CTX_free(ctx);
  52356. CloseSocket(sockfd);
  52357. join_thread(serverThread);
  52358. FreeTcpReady(&ready);
  52359. #ifdef WOLFSSL_TIRTOS
  52360. fdOpenSession(Task_self());
  52361. #endif
  52362. #endif
  52363. return EXPECT_RESULT();
  52364. }
  52365. static int test_wolfSSL_BIO_connect(void)
  52366. {
  52367. EXPECT_DECLS;
  52368. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  52369. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  52370. tcp_ready ready;
  52371. func_args server_args;
  52372. THREAD_TYPE serverThread;
  52373. BIO *tcpBio = NULL;
  52374. BIO *sslBio = NULL;
  52375. SSL_CTX* ctx = NULL;
  52376. SSL *ssl = NULL;
  52377. SSL *sslPtr;
  52378. char msg[] = "hello wolfssl!";
  52379. char reply[30];
  52380. char buff[10] = {0};
  52381. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52382. ExpectIntEQ(WOLFSSL_SUCCESS,
  52383. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  52384. ExpectIntEQ(WOLFSSL_SUCCESS,
  52385. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  52386. ExpectIntEQ(WOLFSSL_SUCCESS,
  52387. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  52388. /* Setup server */
  52389. XMEMSET(&server_args, 0, sizeof(func_args));
  52390. StartTCP();
  52391. InitTcpReady(&ready);
  52392. #if defined(USE_WINDOWS_API)
  52393. /* use RNG to get random port if using windows */
  52394. ready.port = GetRandomPort();
  52395. #endif
  52396. server_args.signal = &ready;
  52397. start_thread(test_server_nofail, &server_args, &serverThread);
  52398. wait_tcp_ready(&server_args);
  52399. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  52400. /* Start the test proper */
  52401. /* Setup the TCP BIO */
  52402. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  52403. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  52404. /* Setup the SSL object */
  52405. ExpectNotNull(ssl = SSL_new(ctx));
  52406. SSL_set_connect_state(ssl);
  52407. /* Setup the SSL BIO */
  52408. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  52409. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  52410. if (EXPECT_FAIL()) {
  52411. wolfSSL_free(ssl);
  52412. }
  52413. /* Verify that BIO_get_ssl works. */
  52414. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  52415. ExpectPtrEq(ssl, sslPtr);
  52416. /* Link BIO's so that sslBio uses tcpBio for IO */
  52417. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  52418. /* Do TCP connect */
  52419. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  52420. /* Do TLS handshake */
  52421. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  52422. /* Test writing */
  52423. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  52424. /* Expect length of default wolfSSL reply */
  52425. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  52426. /* Clean it all up */
  52427. BIO_free_all(sslBio);
  52428. /* Server clean up */
  52429. join_thread(serverThread);
  52430. FreeTcpReady(&ready);
  52431. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  52432. * after. */
  52433. XMEMSET(&server_args, 0, sizeof(func_args));
  52434. StartTCP();
  52435. InitTcpReady(&ready);
  52436. #if defined(USE_WINDOWS_API)
  52437. /* use RNG to get random port if using windows */
  52438. ready.port = GetRandomPort();
  52439. #endif
  52440. server_args.signal = &ready;
  52441. start_thread(test_server_nofail, &server_args, &serverThread);
  52442. wait_tcp_ready(&server_args);
  52443. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  52444. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  52445. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  52446. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  52447. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  52448. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  52449. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  52450. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  52451. /* Attempt to close the TLS connection gracefully. */
  52452. BIO_ssl_shutdown(sslBio);
  52453. BIO_free_all(sslBio);
  52454. join_thread(serverThread);
  52455. FreeTcpReady(&ready);
  52456. SSL_CTX_free(ctx);
  52457. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  52458. wc_ecc_fp_free(); /* free per thread cache */
  52459. #endif
  52460. #endif
  52461. return EXPECT_RESULT();
  52462. }
  52463. static int test_wolfSSL_BIO_tls(void)
  52464. {
  52465. EXPECT_DECLS;
  52466. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  52467. SSL_CTX* ctx = NULL;
  52468. SSL *ssl = NULL;
  52469. BIO *readBio = NULL;
  52470. BIO *writeBio = NULL;
  52471. int ret;
  52472. int err = 0;
  52473. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  52474. ExpectNotNull(ssl = SSL_new(ctx));
  52475. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  52476. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  52477. /* Qt reads data from write-bio,
  52478. * then writes the read data into plain packet.
  52479. * Qt reads data from plain packet,
  52480. * then writes the read data into read-bio.
  52481. */
  52482. SSL_set_bio(ssl, readBio, writeBio);
  52483. do {
  52484. #ifdef WOLFSSL_ASYNC_CRYPT
  52485. if (err == WC_PENDING_E) {
  52486. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  52487. if (ret < 0) { break; } else if (ret == 0) { continue; }
  52488. }
  52489. #endif
  52490. ret = SSL_connect(ssl);
  52491. err = SSL_get_error(ssl, 0);
  52492. } while (err == WC_PENDING_E);
  52493. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  52494. /* in this use case, should return WANT READ
  52495. * so that Qt will read the data from plain packet for next state.
  52496. */
  52497. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  52498. SSL_free(ssl);
  52499. SSL_CTX_free(ctx);
  52500. #endif
  52501. return EXPECT_RESULT();
  52502. }
  52503. static int test_wolfSSL_BIO_datagram(void)
  52504. {
  52505. EXPECT_DECLS;
  52506. #if !defined(NO_BIO) && defined(WOLFSSL_DTLS) && defined(WOLFSSL_HAVE_BIO_ADDR) && defined(OPENSSL_EXTRA)
  52507. int ret;
  52508. SOCKET_T fd1 = SOCKET_INVALID, fd2 = SOCKET_INVALID;
  52509. WOLFSSL_BIO *bio1 = NULL, *bio2 = NULL;
  52510. WOLFSSL_BIO_ADDR *bio_addr1 = NULL, *bio_addr2 = NULL;
  52511. SOCKADDR_IN sin1, sin2;
  52512. socklen_t slen;
  52513. static const char test_msg[] = "I am a datagram, short and stout.";
  52514. char test_msg_recvd[sizeof(test_msg) + 10];
  52515. #ifdef USE_WINDOWS_API
  52516. static const DWORD timeout = 250; /* ms */
  52517. #else
  52518. static const struct timeval timeout = { 0, 250000 };
  52519. #endif
  52520. StartTCP();
  52521. if (EXPECT_SUCCESS()) {
  52522. fd1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  52523. ExpectIntNE(fd1, SOCKET_INVALID);
  52524. }
  52525. if (EXPECT_SUCCESS()) {
  52526. fd2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  52527. ExpectIntNE(fd2, SOCKET_INVALID);
  52528. }
  52529. if (EXPECT_SUCCESS()) {
  52530. bio1 = wolfSSL_BIO_new_dgram(fd1, 1 /* closeF */);
  52531. ExpectNotNull(bio1);
  52532. }
  52533. if (EXPECT_SUCCESS()) {
  52534. bio2 = wolfSSL_BIO_new_dgram(fd2, 1 /* closeF */);
  52535. ExpectNotNull(bio2);
  52536. }
  52537. if (EXPECT_SUCCESS()) {
  52538. sin1.sin_family = AF_INET;
  52539. sin1.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  52540. sin1.sin_port = 0;
  52541. slen = (socklen_t)sizeof(sin1);
  52542. ExpectIntEQ(bind(fd1, (const struct sockaddr *)&sin1, slen), 0);
  52543. ExpectIntEQ(setsockopt(fd1, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)), 0);
  52544. ExpectIntEQ(getsockname(fd1, (struct sockaddr *)&sin1, &slen), 0);
  52545. }
  52546. if (EXPECT_SUCCESS()) {
  52547. sin2.sin_family = AF_INET;
  52548. sin2.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  52549. sin2.sin_port = 0;
  52550. slen = (socklen_t)sizeof(sin2);
  52551. ExpectIntEQ(bind(fd2, (const struct sockaddr *)&sin2, slen), 0);
  52552. ExpectIntEQ(setsockopt(fd2, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)), 0);
  52553. ExpectIntEQ(getsockname(fd2, (struct sockaddr *)&sin2, &slen), 0);
  52554. }
  52555. if (EXPECT_SUCCESS()) {
  52556. bio_addr1 = wolfSSL_BIO_ADDR_new();
  52557. ExpectNotNull(bio_addr1);
  52558. }
  52559. if (EXPECT_SUCCESS()) {
  52560. bio_addr2 = wolfSSL_BIO_ADDR_new();
  52561. ExpectNotNull(bio_addr2);
  52562. }
  52563. if (EXPECT_SUCCESS()) {
  52564. /* for OpenSSL compatibility, direct copying of sockaddrs into BIO_ADDRs must work right. */
  52565. XMEMCPY(&bio_addr2->sa_in, &sin2, sizeof(sin2));
  52566. ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_PEER, 0, bio_addr2), WOLFSSL_SUCCESS);
  52567. wolfSSL_BIO_ADDR_clear(bio_addr2);
  52568. }
  52569. test_msg_recvd[0] = 0;
  52570. ExpectIntEQ(wolfSSL_BIO_write(bio1, test_msg, sizeof(test_msg)), (int)sizeof(test_msg));
  52571. ExpectIntEQ(wolfSSL_BIO_read(bio2, test_msg_recvd, sizeof(test_msg_recvd)), (int)sizeof(test_msg));
  52572. ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0);
  52573. #ifdef WOLFSSL_BIO_HAVE_FLOW_STATS
  52574. ExpectIntEQ(wolfSSL_BIO_number_written(bio1), sizeof(test_msg));
  52575. ExpectIntEQ(wolfSSL_BIO_number_read(bio2), sizeof(test_msg));
  52576. #endif
  52577. /* bio2 should now have bio1's addr stored as its peer_addr, because the
  52578. * BIOs aren't "connected" yet. use it to send a reply.
  52579. */
  52580. test_msg_recvd[0] = 0;
  52581. ExpectIntEQ(wolfSSL_BIO_write(bio2, test_msg, sizeof(test_msg)), (int)sizeof(test_msg));
  52582. ExpectIntEQ(wolfSSL_BIO_read(bio1, test_msg_recvd, sizeof(test_msg_recvd)), (int)sizeof(test_msg));
  52583. ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0);
  52584. ExpectIntEQ(wolfSSL_BIO_read(bio1, test_msg_recvd, sizeof(test_msg_recvd)), WOLFSSL_BIO_ERROR);
  52585. ExpectIntNE(BIO_should_retry(bio1), 0);
  52586. ExpectIntEQ(wolfSSL_BIO_read(bio2, test_msg_recvd, sizeof(test_msg_recvd)), WOLFSSL_BIO_ERROR);
  52587. ExpectIntNE(BIO_should_retry(bio2), 0);
  52588. /* now "connect" the sockets. */
  52589. ExpectIntEQ(connect(fd1, (const struct sockaddr *)&sin2, (socklen_t)sizeof(sin2)), 0);
  52590. ExpectIntEQ(connect(fd2, (const struct sockaddr *)&sin1, (socklen_t)sizeof(sin1)), 0);
  52591. if (EXPECT_SUCCESS()) {
  52592. XMEMCPY(&bio_addr2->sa_in, &sin2, sizeof(sin2));
  52593. ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_CONNECTED, 0, bio_addr2), WOLFSSL_SUCCESS);
  52594. wolfSSL_BIO_ADDR_clear(bio_addr2);
  52595. }
  52596. if (EXPECT_SUCCESS()) {
  52597. XMEMCPY(&bio_addr1->sa_in, &sin1, sizeof(sin1));
  52598. ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio2, BIO_CTRL_DGRAM_SET_CONNECTED, 0, bio_addr1), WOLFSSL_SUCCESS);
  52599. wolfSSL_BIO_ADDR_clear(bio_addr1);
  52600. }
  52601. test_msg_recvd[0] = 0;
  52602. ExpectIntEQ(wolfSSL_BIO_write(bio2, test_msg, sizeof(test_msg)), (int)sizeof(test_msg));
  52603. ExpectIntEQ(wolfSSL_BIO_read(bio1, test_msg_recvd, sizeof(test_msg_recvd)), (int)sizeof(test_msg));
  52604. ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0);
  52605. test_msg_recvd[0] = 0;
  52606. ExpectIntEQ(wolfSSL_BIO_write(bio1, test_msg, sizeof(test_msg)), (int)sizeof(test_msg));
  52607. ExpectIntEQ(wolfSSL_BIO_read(bio2, test_msg_recvd, sizeof(test_msg_recvd)), (int)sizeof(test_msg));
  52608. ExpectIntEQ(XMEMCMP(test_msg_recvd, test_msg, sizeof(test_msg)), 0);
  52609. #ifdef __linux__
  52610. /* now "disconnect" the sockets and attempt transmits expected to fail. */
  52611. sin1.sin_family = AF_UNSPEC;
  52612. ExpectIntEQ(connect(fd1, (const struct sockaddr *)&sin1, (socklen_t)sizeof(sin1)), 0);
  52613. ExpectIntEQ(connect(fd2, (const struct sockaddr *)&sin1, (socklen_t)sizeof(sin1)), 0);
  52614. sin1.sin_family = AF_INET;
  52615. ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_CONNECTED, 0, NULL), WOLFSSL_SUCCESS);
  52616. ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio2, BIO_CTRL_DGRAM_SET_CONNECTED, 0, NULL), WOLFSSL_SUCCESS);
  52617. if (EXPECT_SUCCESS()) {
  52618. sin2.sin_addr.s_addr = htonl(0xc0a8c0a8); /* 192.168.192.168 -- invalid for loopback interface. */
  52619. XMEMCPY(&bio_addr2->sa_in, &sin2, sizeof(sin2));
  52620. ExpectIntEQ((int)wolfSSL_BIO_ctrl(bio1, BIO_CTRL_DGRAM_SET_PEER, 0, bio_addr2), WOLFSSL_SUCCESS);
  52621. wolfSSL_BIO_ADDR_clear(bio_addr2);
  52622. }
  52623. test_msg_recvd[0] = 0;
  52624. errno = 0;
  52625. ExpectIntEQ(wolfSSL_BIO_write(bio1, test_msg, sizeof(test_msg)), -1);
  52626. ExpectTrue((errno == EINVAL) || (errno == ENETUNREACH));
  52627. #endif /* __linux__ */
  52628. if (bio1) {
  52629. ret = wolfSSL_BIO_free(bio1);
  52630. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  52631. } else if (fd1 != SOCKET_INVALID)
  52632. CloseSocket(fd1);
  52633. if (bio2) {
  52634. ret = wolfSSL_BIO_free(bio2);
  52635. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  52636. } else if (fd2 != SOCKET_INVALID)
  52637. CloseSocket(fd2);
  52638. if (bio_addr1)
  52639. wolfSSL_BIO_ADDR_free(bio_addr1);
  52640. if (bio_addr2)
  52641. wolfSSL_BIO_ADDR_free(bio_addr2);
  52642. #endif /* !NO_BIO && WOLFSSL_DTLS && WOLFSSL_HAVE_BIO_ADDR && OPENSSL_EXTRA */
  52643. return EXPECT_RESULT();
  52644. }
  52645. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  52646. defined(HAVE_HTTP_CLIENT)
  52647. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  52648. {
  52649. BIO* clientBio;
  52650. SSL* sslClient;
  52651. SSL_CTX* ctx;
  52652. char connectAddr[20]; /* IP + port */;
  52653. (void)args;
  52654. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  52655. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  52656. AssertIntEQ(BIO_do_connect(clientBio), 1);
  52657. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  52658. AssertNotNull(sslClient = SSL_new(ctx));
  52659. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  52660. SSL_set_bio(sslClient, clientBio, clientBio);
  52661. AssertIntEQ(SSL_connect(sslClient), 1);
  52662. SSL_free(sslClient);
  52663. SSL_CTX_free(ctx);
  52664. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  52665. wc_ecc_fp_free(); /* free per thread cache */
  52666. #endif
  52667. WOLFSSL_RETURN_FROM_THREAD(0);
  52668. }
  52669. #endif
  52670. static int test_wolfSSL_BIO_accept(void)
  52671. {
  52672. EXPECT_DECLS;
  52673. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  52674. defined(HAVE_HTTP_CLIENT)
  52675. BIO* serverBindBio = NULL;
  52676. BIO* serverAcceptBio = NULL;
  52677. SSL* sslServer = NULL;
  52678. SSL_CTX* ctx = NULL;
  52679. func_args args;
  52680. THREAD_TYPE thread;
  52681. char port[10]; /* 10 bytes should be enough to store the string
  52682. * representation of the port */
  52683. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  52684. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  52685. /* First BIO_do_accept binds the port */
  52686. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  52687. XMEMSET(&args, 0, sizeof(func_args));
  52688. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  52689. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  52690. /* Let's plug it into SSL to test */
  52691. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  52692. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  52693. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  52694. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  52695. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  52696. ExpectNotNull(sslServer = SSL_new(ctx));
  52697. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  52698. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  52699. ExpectIntEQ(SSL_accept(sslServer), 1);
  52700. join_thread(thread);
  52701. BIO_free(serverBindBio);
  52702. SSL_free(sslServer);
  52703. SSL_CTX_free(ctx);
  52704. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  52705. wc_ecc_fp_free(); /* free per thread cache */
  52706. #endif
  52707. #endif
  52708. return EXPECT_RESULT();
  52709. }
  52710. static int test_wolfSSL_BIO_write(void)
  52711. {
  52712. EXPECT_DECLS;
  52713. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  52714. BIO* bio = NULL;
  52715. BIO* bio64 = NULL;
  52716. BIO* bio_mem = NULL;
  52717. BIO* ptr = NULL;
  52718. int sz;
  52719. char msg[] = "conversion test";
  52720. char out[40];
  52721. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  52722. void* bufPtr = NULL;
  52723. BUF_MEM* buf = NULL;
  52724. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  52725. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  52726. if (EXPECT_FAIL()) {
  52727. BIO_free(bio64);
  52728. }
  52729. /* now should convert to base64 then write to memory */
  52730. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  52731. BIO_flush(bio);
  52732. /* test BIO chain */
  52733. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  52734. ExpectNotNull(buf);
  52735. ExpectIntEQ(buf->length, 25);
  52736. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  52737. ExpectPtrEq(buf->data, bufPtr);
  52738. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  52739. sz = sizeof(out);
  52740. XMEMSET(out, 0, sz);
  52741. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  52742. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  52743. /* write then read should return the same message */
  52744. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  52745. sz = sizeof(out);
  52746. XMEMSET(out, 0, sz);
  52747. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  52748. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  52749. /* now try encoding with no line ending */
  52750. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  52751. #ifdef HAVE_EX_DATA
  52752. BIO_set_ex_data(bio64, 0, (void*) "data");
  52753. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  52754. #endif
  52755. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  52756. BIO_flush(bio);
  52757. sz = sizeof(out);
  52758. XMEMSET(out, 0, sz);
  52759. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  52760. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  52761. BIO_free_all(bio); /* frees bio64 also */
  52762. bio = NULL;
  52763. /* test with more than one bio64 in list */
  52764. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  52765. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  52766. if (EXPECT_FAIL()) {
  52767. BIO_free(bio64);
  52768. bio64 = NULL;
  52769. }
  52770. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  52771. ExpectNotNull(BIO_push(bio64, bio_mem));
  52772. if (EXPECT_FAIL()) {
  52773. BIO_free(bio_mem);
  52774. }
  52775. /* now should convert to base64 when stored and then decode with read */
  52776. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  52777. BIO_flush(bio);
  52778. sz = sizeof(out);
  52779. XMEMSET(out, 0, sz);
  52780. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  52781. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  52782. BIO_clear_flags(bio64, ~0);
  52783. BIO_set_retry_read(bio);
  52784. BIO_free_all(bio); /* frees bio64s also */
  52785. bio = NULL;
  52786. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  52787. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  52788. BIO_free(bio);
  52789. #endif
  52790. return EXPECT_RESULT();
  52791. }
  52792. static int test_wolfSSL_BIO_printf(void)
  52793. {
  52794. EXPECT_DECLS;
  52795. #if defined(OPENSSL_ALL)
  52796. BIO* bio = NULL;
  52797. int sz = 7;
  52798. char msg[] = "TLS 1.3 for the world";
  52799. char out[60];
  52800. char expected[] = "TLS 1.3 for the world : sz = 7";
  52801. XMEMSET(out, 0, sizeof(out));
  52802. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52803. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  52804. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  52805. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  52806. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  52807. BIO_free(bio);
  52808. #endif
  52809. return EXPECT_RESULT();
  52810. }
  52811. static int test_wolfSSL_BIO_f_md(void)
  52812. {
  52813. EXPECT_DECLS;
  52814. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  52815. BIO* bio = NULL;
  52816. BIO* mem = NULL;
  52817. char msg[] = "message to hash";
  52818. char out[60];
  52819. EVP_MD_CTX* ctx = NULL;
  52820. const unsigned char testKey[] =
  52821. {
  52822. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  52823. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  52824. 0x0b, 0x0b, 0x0b, 0x0b
  52825. };
  52826. const char testData[] = "Hi There";
  52827. const unsigned char testResult[] =
  52828. {
  52829. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  52830. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  52831. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  52832. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  52833. };
  52834. const unsigned char expectedHash[] =
  52835. {
  52836. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  52837. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  52838. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  52839. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  52840. };
  52841. const unsigned char emptyHash[] =
  52842. {
  52843. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  52844. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  52845. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  52846. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  52847. };
  52848. unsigned char check[sizeof(testResult) + 1];
  52849. size_t checkSz = -1;
  52850. EVP_PKEY* key = NULL;
  52851. XMEMSET(out, 0, sizeof(out));
  52852. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  52853. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  52854. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  52855. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  52856. /* should not be able to write/read yet since just digest wrapper and no
  52857. * data is passing through the bio */
  52858. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  52859. ExpectIntEQ(BIO_pending(bio), 0);
  52860. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  52861. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  52862. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  52863. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  52864. BIO_reset(bio);
  52865. /* append BIO mem to bio in order to read/write */
  52866. ExpectNotNull(bio = BIO_push(bio, mem));
  52867. XMEMSET(out, 0, sizeof(out));
  52868. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  52869. ExpectIntEQ(BIO_pending(bio), 16);
  52870. /* this just reads the message and does not hash it (gets calls final) */
  52871. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  52872. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  52873. /* create a message digest using BIO */
  52874. XMEMSET(out, 0, sizeof(out));
  52875. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  52876. ExpectIntEQ(BIO_pending(mem), 16);
  52877. ExpectIntEQ(BIO_pending(bio), 16);
  52878. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  52879. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  52880. BIO_free(bio);
  52881. bio = NULL;
  52882. BIO_free(mem);
  52883. mem = NULL;
  52884. /* test with HMAC */
  52885. XMEMSET(out, 0, sizeof(out));
  52886. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  52887. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  52888. BIO_get_md_ctx(bio, &ctx);
  52889. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  52890. (int)sizeof(testKey)));
  52891. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  52892. ExpectNotNull(bio = BIO_push(bio, mem));
  52893. BIO_write(bio, testData, (int)strlen(testData));
  52894. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  52895. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  52896. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  52897. EVP_PKEY_free(key);
  52898. BIO_free(bio);
  52899. BIO_free(mem);
  52900. #endif
  52901. return EXPECT_RESULT();
  52902. }
  52903. static int test_wolfSSL_BIO_up_ref(void)
  52904. {
  52905. EXPECT_DECLS;
  52906. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  52907. BIO* bio = NULL;
  52908. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  52909. ExpectIntEQ(BIO_up_ref(NULL), 0);
  52910. ExpectIntEQ(BIO_up_ref(bio), 1);
  52911. BIO_free(bio);
  52912. ExpectIntEQ(BIO_up_ref(bio), 1);
  52913. BIO_free(bio);
  52914. BIO_free(bio);
  52915. #endif
  52916. return EXPECT_RESULT();
  52917. }
  52918. static int test_wolfSSL_BIO_reset(void)
  52919. {
  52920. EXPECT_DECLS;
  52921. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  52922. BIO* bio = NULL;
  52923. byte buf[16];
  52924. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  52925. (word32)XSTRLEN("secure your data")));
  52926. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  52927. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  52928. XMEMSET(buf, 0, 16);
  52929. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  52930. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  52931. /* You cannot write to MEM BIO with read-only mode. */
  52932. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  52933. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  52934. XMEMSET(buf, 0, 16);
  52935. ExpectIntEQ(BIO_reset(bio), 0);
  52936. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  52937. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  52938. BIO_free(bio);
  52939. #endif
  52940. return EXPECT_RESULT();
  52941. }
  52942. #endif /* !NO_BIO */
  52943. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  52944. /* test that the callback arg is correct */
  52945. static int certCbArg = 0;
  52946. static int certCb(WOLFSSL* ssl, void* arg)
  52947. {
  52948. if (ssl == NULL || arg != &certCbArg)
  52949. return 0;
  52950. if (wolfSSL_is_server(ssl)) {
  52951. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  52952. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  52953. return 0;
  52954. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  52955. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  52956. return 0;
  52957. }
  52958. else {
  52959. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  52960. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  52961. return 0;
  52962. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  52963. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  52964. return 0;
  52965. }
  52966. return 1;
  52967. }
  52968. static int certSetupCb(WOLFSSL_CTX* ctx)
  52969. {
  52970. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  52971. return TEST_SUCCESS;
  52972. }
  52973. /**
  52974. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  52975. * to stop certificate and key loading
  52976. */
  52977. static int certClearCb(WOLFSSL* ssl)
  52978. {
  52979. /* Clear the loaded certs to force the callbacks to set them up */
  52980. SSL_certs_clear(ssl);
  52981. return TEST_SUCCESS;
  52982. }
  52983. #endif
  52984. static int test_wolfSSL_cert_cb(void)
  52985. {
  52986. EXPECT_DECLS;
  52987. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  52988. test_ssl_cbf func_cb_client;
  52989. test_ssl_cbf func_cb_server;
  52990. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  52991. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  52992. func_cb_client.ctx_ready = certSetupCb;
  52993. func_cb_client.ssl_ready = certClearCb;
  52994. func_cb_server.ctx_ready = certSetupCb;
  52995. func_cb_server.ssl_ready = certClearCb;
  52996. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  52997. &func_cb_server, NULL), TEST_SUCCESS);
  52998. #endif
  52999. return EXPECT_RESULT();
  53000. }
  53001. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  53002. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  53003. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  53004. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  53005. {
  53006. EXPECT_DECLS;
  53007. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  53008. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  53009. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  53010. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  53011. return EXPECT_RESULT();
  53012. }
  53013. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  53014. {
  53015. const byte* suites = NULL;
  53016. word16 suiteSz = 0;
  53017. const byte* hashSigAlgo = NULL;
  53018. word16 hashSigAlgoSz = 0;
  53019. word16 idx = 0;
  53020. int haveRSA = 0;
  53021. int haveECC = 0;
  53022. (void)arg;
  53023. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  53024. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  53025. return 0;
  53026. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  53027. hashSigAlgoSz == 0)
  53028. return 0;
  53029. for (idx = 0; idx < suiteSz; idx += 2) {
  53030. WOLFSSL_CIPHERSUITE_INFO info =
  53031. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  53032. if (info.rsaAuth)
  53033. haveRSA = 1;
  53034. else if (info.eccAuth)
  53035. haveECC = 1;
  53036. }
  53037. if (hashSigAlgoSz > 0) {
  53038. /* sigalgs extension takes precedence over ciphersuites */
  53039. haveRSA = 0;
  53040. haveECC = 0;
  53041. }
  53042. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  53043. int hashAlgo = 0;
  53044. int sigAlgo = 0;
  53045. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  53046. &hashAlgo, &sigAlgo) != 0)
  53047. return 0;
  53048. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  53049. haveRSA = 1;
  53050. else if (sigAlgo == ECDSAk)
  53051. haveECC = 1;
  53052. }
  53053. if (haveRSA) {
  53054. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  53055. != WOLFSSL_SUCCESS)
  53056. return 0;
  53057. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  53058. != WOLFSSL_SUCCESS)
  53059. return 0;
  53060. }
  53061. else if (haveECC) {
  53062. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  53063. != WOLFSSL_SUCCESS)
  53064. return 0;
  53065. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  53066. != WOLFSSL_SUCCESS)
  53067. return 0;
  53068. }
  53069. return 1;
  53070. }
  53071. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  53072. {
  53073. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  53074. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  53075. return TEST_SUCCESS;
  53076. }
  53077. #endif
  53078. /* Testing dynamic ciphers offered by client */
  53079. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  53080. {
  53081. EXPECT_DECLS;
  53082. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  53083. test_ssl_cbf func_cb_client;
  53084. test_ssl_cbf func_cb_server;
  53085. struct {
  53086. method_provider client_meth;
  53087. const char* client_ciphers;
  53088. const char* client_sigalgs;
  53089. const char* client_ca;
  53090. method_provider server_meth;
  53091. } test_params[] = {
  53092. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  53093. #ifdef WOLFSSL_TLS13
  53094. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  53095. {wolfTLSv1_3_client_method,
  53096. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  53097. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  53098. #endif
  53099. #ifdef HAVE_ECC
  53100. {wolfTLSv1_3_client_method,
  53101. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  53102. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  53103. #endif
  53104. #endif
  53105. #ifndef WOLFSSL_NO_TLS12
  53106. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  53107. {wolfTLSv1_2_client_method,
  53108. "DHE-RSA-AES128-GCM-SHA256",
  53109. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  53110. #endif
  53111. #ifdef HAVE_ECC
  53112. {wolfTLSv1_2_client_method,
  53113. "ECDHE-ECDSA-AES128-GCM-SHA256",
  53114. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  53115. #endif
  53116. #endif
  53117. #endif
  53118. };
  53119. size_t i;
  53120. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  53121. if (testCount > 0) {
  53122. for (i = 0; i < testCount; i++) {
  53123. printf("\tTesting %s ciphers with %s sigalgs\n",
  53124. test_params[i].client_ciphers,
  53125. test_params[i].client_sigalgs);
  53126. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  53127. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  53128. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  53129. test_params[i].client_ciphers;
  53130. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  53131. test_params[i].client_sigalgs;
  53132. func_cb_client.method = test_params[i].client_meth;
  53133. func_cb_client.caPemFile = test_params[i].client_ca;
  53134. func_cb_client.ctx_ready =
  53135. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  53136. func_cb_server.ctx_ready =
  53137. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  53138. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  53139. func_cb_server.method = test_params[i].server_meth;
  53140. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  53141. &func_cb_server, NULL), TEST_SUCCESS);
  53142. }
  53143. }
  53144. #endif
  53145. return EXPECT_RESULT();
  53146. }
  53147. static int test_wolfSSL_ciphersuite_auth(void)
  53148. {
  53149. EXPECT_DECLS;
  53150. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  53151. WOLFSSL_CIPHERSUITE_INFO info;
  53152. (void)info;
  53153. #ifndef WOLFSSL_NO_TLS12
  53154. #ifdef HAVE_CHACHA
  53155. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  53156. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  53157. ExpectIntEQ(info.rsaAuth, 1);
  53158. ExpectIntEQ(info.eccAuth, 0);
  53159. ExpectIntEQ(info.eccStatic, 0);
  53160. ExpectIntEQ(info.psk, 0);
  53161. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  53162. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  53163. ExpectIntEQ(info.rsaAuth, 0);
  53164. ExpectIntEQ(info.eccAuth, 1);
  53165. ExpectIntEQ(info.eccStatic, 0);
  53166. ExpectIntEQ(info.psk, 0);
  53167. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  53168. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  53169. ExpectIntEQ(info.rsaAuth, 0);
  53170. ExpectIntEQ(info.eccAuth, 0);
  53171. ExpectIntEQ(info.eccStatic, 0);
  53172. ExpectIntEQ(info.psk, 1);
  53173. #endif
  53174. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  53175. #ifndef NO_RSA
  53176. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  53177. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  53178. ExpectIntEQ(info.rsaAuth, 1);
  53179. ExpectIntEQ(info.eccAuth, 0);
  53180. ExpectIntEQ(info.eccStatic, 0);
  53181. ExpectIntEQ(info.psk, 0);
  53182. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  53183. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  53184. ExpectIntEQ(info.rsaAuth, 1);
  53185. ExpectIntEQ(info.eccAuth, 0);
  53186. ExpectIntEQ(info.eccStatic, 1);
  53187. ExpectIntEQ(info.psk, 0);
  53188. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  53189. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  53190. ExpectIntEQ(info.rsaAuth, 1);
  53191. ExpectIntEQ(info.eccAuth, 0);
  53192. ExpectIntEQ(info.eccStatic, 1);
  53193. ExpectIntEQ(info.psk, 0);
  53194. #endif
  53195. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  53196. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  53197. ExpectIntEQ(info.rsaAuth, 0);
  53198. ExpectIntEQ(info.eccAuth, 1);
  53199. ExpectIntEQ(info.eccStatic, 0);
  53200. ExpectIntEQ(info.psk, 0);
  53201. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  53202. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  53203. ExpectIntEQ(info.rsaAuth, 0);
  53204. ExpectIntEQ(info.eccAuth, 1);
  53205. ExpectIntEQ(info.eccStatic, 1);
  53206. ExpectIntEQ(info.psk, 0);
  53207. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  53208. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  53209. ExpectIntEQ(info.rsaAuth, 0);
  53210. ExpectIntEQ(info.eccAuth, 0);
  53211. ExpectIntEQ(info.eccStatic, 0);
  53212. ExpectIntEQ(info.psk, 1);
  53213. #endif
  53214. #endif
  53215. #ifdef WOLFSSL_TLS13
  53216. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  53217. TLS_AES_128_GCM_SHA256);
  53218. ExpectIntEQ(info.rsaAuth, 0);
  53219. ExpectIntEQ(info.eccAuth, 0);
  53220. ExpectIntEQ(info.eccStatic, 0);
  53221. ExpectIntEQ(info.psk, 0);
  53222. #endif
  53223. #endif
  53224. return EXPECT_RESULT();
  53225. }
  53226. static int test_wolfSSL_sigalg_info(void)
  53227. {
  53228. EXPECT_DECLS;
  53229. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  53230. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  53231. word16 len = 0;
  53232. word16 idx = 0;
  53233. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  53234. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  53235. for (idx = 0; idx < len; idx += 2) {
  53236. int hashAlgo = 0;
  53237. int sigAlgo = 0;
  53238. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  53239. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  53240. ExpectIntNE(hashAlgo, 0);
  53241. ExpectIntNE(sigAlgo, 0);
  53242. }
  53243. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  53244. 0xFFFFFFFF, &len);
  53245. for (idx = 0; idx < len; idx += 2) {
  53246. int hashAlgo = 0;
  53247. int sigAlgo = 0;
  53248. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  53249. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  53250. ExpectIntNE(hashAlgo, 0);
  53251. }
  53252. #endif
  53253. return EXPECT_RESULT();
  53254. }
  53255. static int test_wolfSSL_SESSION(void)
  53256. {
  53257. EXPECT_DECLS;
  53258. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  53259. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  53260. !defined(NO_SESSION_CACHE)
  53261. WOLFSSL* ssl = NULL;
  53262. WOLFSSL_CTX* ctx = NULL;
  53263. WOLFSSL_SESSION* sess = NULL;
  53264. WOLFSSL_SESSION* sess_copy = NULL;
  53265. #ifdef OPENSSL_EXTRA
  53266. #ifdef HAVE_EXT_CACHE
  53267. unsigned char* sessDer = NULL;
  53268. unsigned char* ptr = NULL;
  53269. int sz = 0;
  53270. #endif
  53271. const unsigned char context[] = "user app context";
  53272. unsigned int contextSz = (unsigned int)sizeof(context);
  53273. #endif
  53274. int ret = 0, err = 0;
  53275. SOCKET_T sockfd;
  53276. tcp_ready ready;
  53277. func_args server_args;
  53278. THREAD_TYPE serverThread;
  53279. char msg[80];
  53280. const char* sendGET = "GET";
  53281. /* TLS v1.3 requires session tickets */
  53282. /* CHACHA and POLY1305 required for myTicketEncCb */
  53283. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  53284. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  53285. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  53286. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  53287. #else
  53288. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53289. #endif
  53290. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  53291. WOLFSSL_FILETYPE_PEM));
  53292. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  53293. WOLFSSL_FILETYPE_PEM));
  53294. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  53295. WOLFSSL_SUCCESS);
  53296. #ifdef WOLFSSL_ENCRYPTED_KEYS
  53297. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  53298. #endif
  53299. #ifdef HAVE_SESSION_TICKET
  53300. /* Use session tickets, for ticket tests below */
  53301. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  53302. #endif
  53303. XMEMSET(&server_args, 0, sizeof(func_args));
  53304. #ifdef WOLFSSL_TIRTOS
  53305. fdOpenSession(Task_self());
  53306. #endif
  53307. StartTCP();
  53308. InitTcpReady(&ready);
  53309. #if defined(USE_WINDOWS_API)
  53310. /* use RNG to get random port if using windows */
  53311. ready.port = GetRandomPort();
  53312. #endif
  53313. server_args.signal = &ready;
  53314. start_thread(test_server_nofail, &server_args, &serverThread);
  53315. wait_tcp_ready(&server_args);
  53316. /* client connection */
  53317. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53318. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  53319. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  53320. #ifdef WOLFSSL_ASYNC_CRYPT
  53321. err = 0; /* Reset error */
  53322. #endif
  53323. do {
  53324. #ifdef WOLFSSL_ASYNC_CRYPT
  53325. if (err == WC_PENDING_E) {
  53326. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  53327. if (ret < 0) { break; } else if (ret == 0) { continue; }
  53328. }
  53329. #endif
  53330. ret = wolfSSL_connect(ssl);
  53331. err = wolfSSL_get_error(ssl, 0);
  53332. } while (err == WC_PENDING_E);
  53333. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  53334. #ifdef WOLFSSL_ASYNC_CRYPT
  53335. err = 0; /* Reset error */
  53336. #endif
  53337. do {
  53338. #ifdef WOLFSSL_ASYNC_CRYPT
  53339. if (err == WC_PENDING_E) {
  53340. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  53341. if (ret < 0) { break; } else if (ret == 0) { continue; }
  53342. }
  53343. #endif
  53344. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  53345. err = wolfSSL_get_error(ssl, 0);
  53346. } while (err == WC_PENDING_E);
  53347. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  53348. #ifdef WOLFSSL_ASYNC_CRYPT
  53349. err = 0; /* Reset error */
  53350. #endif
  53351. do {
  53352. #ifdef WOLFSSL_ASYNC_CRYPT
  53353. if (err == WC_PENDING_E) {
  53354. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  53355. if (ret < 0) { break; } else if (ret == 0) { continue; }
  53356. }
  53357. #endif
  53358. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  53359. err = wolfSSL_get_error(ssl, 0);
  53360. } while (err == WC_PENDING_E);
  53361. ExpectIntEQ(ret, 23);
  53362. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  53363. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  53364. ExpectIntEQ(wolfSSL_SessionIsSetup(sess), 1);
  53365. #ifdef HAVE_EXT_CACHE
  53366. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  53367. * HAVE_EXT_CACHE we get new objects each time */
  53368. #endif
  53369. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  53370. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  53371. sess = wolfSSL_get_session(ssl);
  53372. #ifdef OPENSSL_EXTRA
  53373. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  53374. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  53375. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  53376. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  53377. #ifdef HAVE_SESSION_TICKET
  53378. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  53379. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  53380. SESSION_TICKET_HINT_DEFAULT);
  53381. #else
  53382. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  53383. #endif
  53384. #else
  53385. (void)sess;
  53386. #endif /* OPENSSL_EXTRA */
  53387. /* Retain copy of the session for later testing */
  53388. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  53389. wolfSSL_shutdown(ssl);
  53390. wolfSSL_free(ssl); ssl = NULL;
  53391. CloseSocket(sockfd);
  53392. join_thread(serverThread);
  53393. FreeTcpReady(&ready);
  53394. #ifdef WOLFSSL_TIRTOS
  53395. fdOpenSession(Task_self());
  53396. #endif
  53397. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  53398. {
  53399. X509 *x509 = NULL;
  53400. char buf[30];
  53401. int bufSz = 0;
  53402. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  53403. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  53404. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  53405. sizeof(buf))), 0);
  53406. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  53407. if (bufSz == 7) {
  53408. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  53409. }
  53410. if (bufSz == 16) {
  53411. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  53412. }
  53413. }
  53414. #endif
  53415. #ifdef HAVE_EXT_CACHE
  53416. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  53417. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  53418. sess_copy = NULL;
  53419. #endif
  53420. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  53421. /* get session from DER and update the timeout */
  53422. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  53423. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  53424. wolfSSL_SESSION_free(sess); sess = NULL;
  53425. sess = NULL;
  53426. ptr = sessDer;
  53427. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  53428. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  53429. (const unsigned char**)&ptr, sz));
  53430. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  53431. sessDer = NULL;
  53432. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  53433. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  53434. #endif
  53435. /* successful set session test */
  53436. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53437. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  53438. #ifdef HAVE_SESSION_TICKET
  53439. /* Test set/get session ticket */
  53440. {
  53441. const char* ticket = "This is a session ticket";
  53442. char buf[64] = {0};
  53443. word32 bufSz = (word32)sizeof(buf);
  53444. ExpectIntEQ(SSL_SUCCESS,
  53445. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  53446. (word32)XSTRLEN(ticket)));
  53447. ExpectIntEQ(SSL_SUCCESS,
  53448. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  53449. ExpectStrEQ(ticket, buf);
  53450. }
  53451. #endif
  53452. #ifdef OPENSSL_EXTRA
  53453. /* session timeout case */
  53454. /* make the session to be expired */
  53455. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  53456. XSLEEP_MS(1200);
  53457. /* SSL_set_session should reject specified session but return success
  53458. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  53459. */
  53460. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  53461. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  53462. #else
  53463. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  53464. #endif
  53465. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  53466. #ifdef WOLFSSL_SESSION_ID_CTX
  53467. /* fail case with miss match session context IDs (use compatibility API) */
  53468. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  53469. SSL_SUCCESS);
  53470. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  53471. wolfSSL_free(ssl); ssl = NULL;
  53472. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  53473. SSL_FAILURE);
  53474. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  53475. SSL_SUCCESS);
  53476. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53477. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  53478. #endif
  53479. #endif /* OPENSSL_EXTRA */
  53480. wolfSSL_free(ssl);
  53481. wolfSSL_SESSION_free(sess);
  53482. wolfSSL_CTX_free(ctx);
  53483. #endif
  53484. return EXPECT_RESULT();
  53485. }
  53486. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  53487. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  53488. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  53489. !defined(WOLFSSL_NO_TLS12)
  53490. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  53491. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  53492. {
  53493. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  53494. /* returns previous timeout value */
  53495. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  53496. #else
  53497. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  53498. #endif
  53499. }
  53500. /* set the session to timeout in a second */
  53501. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  53502. {
  53503. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  53504. }
  53505. /* store the client side session from the first successful connection */
  53506. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  53507. {
  53508. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  53509. NULL); /* ref count 1 */
  53510. }
  53511. /* wait till session is expired then set it in the WOLFSSL struct for use */
  53512. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  53513. {
  53514. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  53515. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  53516. WOLFSSL_SUCCESS);
  53517. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  53518. }
  53519. /* set expired session in the WOLFSSL struct for use */
  53520. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  53521. {
  53522. XSLEEP_MS(1200); /* wait a second for session to expire */
  53523. /* set the expired session, call to set session fails but continuing on
  53524. after failure should be handled here */
  53525. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  53526. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  53527. WOLFSSL_SUCCESS);
  53528. #else
  53529. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  53530. WOLFSSL_SUCCESS);
  53531. #endif
  53532. }
  53533. /* check that the expired session was not reused */
  53534. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  53535. {
  53536. /* since the session has expired it should not have been reused */
  53537. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  53538. }
  53539. #endif
  53540. static int test_wolfSSL_SESSION_expire_downgrade(void)
  53541. {
  53542. EXPECT_DECLS;
  53543. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  53544. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  53545. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  53546. !defined(WOLFSSL_NO_TLS12)
  53547. WOLFSSL_CTX* ctx = NULL;
  53548. callback_functions server_cbf, client_cbf;
  53549. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  53550. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  53551. /* force server side to use TLS 1.2 */
  53552. server_cbf.ctx = ctx;
  53553. server_cbf.method = wolfTLSv1_2_server_method;
  53554. client_cbf.method = wolfSSLv23_client_method;
  53555. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  53556. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  53557. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  53558. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  53559. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53560. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53561. /* set the previously created session and wait till expired */
  53562. server_cbf.ctx = ctx;
  53563. client_cbf.method = wolfSSLv23_client_method;
  53564. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  53565. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  53566. client_cbf.on_result =
  53567. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  53568. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  53569. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53570. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53571. /* set the previously created expired session */
  53572. server_cbf.ctx = ctx;
  53573. client_cbf.method = wolfSSLv23_client_method;
  53574. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  53575. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  53576. client_cbf.on_result =
  53577. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  53578. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  53579. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53580. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53581. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  53582. wolfSSL_CTX_free(ctx);
  53583. #endif
  53584. return EXPECT_RESULT();
  53585. }
  53586. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  53587. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  53588. static int clientSessRemCountMalloc = 0;
  53589. static int serverSessRemCountMalloc = 0;
  53590. static int clientSessRemCountFree = 0;
  53591. static int serverSessRemCountFree = 0;
  53592. static WOLFSSL_CTX* serverSessCtx = NULL;
  53593. static WOLFSSL_SESSION* serverSess = NULL;
  53594. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  53595. !defined(NO_SESSION_CACHE_REF)
  53596. static WOLFSSL_CTX* clientSessCtx = NULL;
  53597. static WOLFSSL_SESSION* clientSess = NULL;
  53598. #endif
  53599. static int serverSessRemIdx = 3;
  53600. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  53601. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  53602. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  53603. {
  53604. int* side;
  53605. (void)ctx;
  53606. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  53607. if (side != NULL) {
  53608. if (*side == WOLFSSL_CLIENT_END)
  53609. clientSessRemCountFree++;
  53610. else
  53611. serverSessRemCountFree++;
  53612. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  53613. }
  53614. }
  53615. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  53616. {
  53617. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  53618. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  53619. !defined(NO_SESSION_CACHE_REF)
  53620. {
  53621. EXPECT_DECLS;
  53622. /* Allow downgrade, set min version, and disable TLS 1.3.
  53623. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  53624. * reference to the session cache. But with WOLFSSL_TLS13 and without
  53625. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  53626. * the session in the cache. In this case we need to downgrade to
  53627. * previous versions to just use the legacy session ID field. */
  53628. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  53629. SSL_SUCCESS);
  53630. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  53631. SSL_SUCCESS);
  53632. return EXPECT_RESULT();
  53633. }
  53634. #else
  53635. return TEST_SUCCESS;
  53636. #endif
  53637. }
  53638. static int SessRemSslSetupCb(WOLFSSL* ssl)
  53639. {
  53640. EXPECT_DECLS;
  53641. int* side;
  53642. if (SSL_is_server(ssl)) {
  53643. side = &sessRemCtx_Server;
  53644. serverSessRemCountMalloc++;
  53645. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  53646. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  53647. SSL_SUCCESS);
  53648. }
  53649. else {
  53650. side = &sessRemCtx_Client;
  53651. clientSessRemCountMalloc++;
  53652. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  53653. !defined(NO_SESSION_CACHE_REF)
  53654. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  53655. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  53656. SSL_SUCCESS);
  53657. #endif
  53658. }
  53659. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  53660. serverSessRemIdx, side), SSL_SUCCESS);
  53661. return EXPECT_RESULT();
  53662. }
  53663. #endif
  53664. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  53665. {
  53666. EXPECT_DECLS;
  53667. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  53668. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  53669. /* Check that the remove callback gets called for external data in a
  53670. * session object */
  53671. test_ssl_cbf func_cb;
  53672. XMEMSET(&func_cb, 0, sizeof(func_cb));
  53673. func_cb.ctx_ready = SessRemCtxSetupCb;
  53674. func_cb.on_result = SessRemSslSetupCb;
  53675. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  53676. NULL), TEST_SUCCESS);
  53677. /* Both should have been allocated */
  53678. ExpectIntEQ(clientSessRemCountMalloc, 1);
  53679. ExpectIntEQ(serverSessRemCountMalloc, 1);
  53680. /* This should not be called yet. Session wasn't evicted from cache yet. */
  53681. ExpectIntEQ(clientSessRemCountFree, 0);
  53682. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  53683. !defined(NO_SESSION_CACHE_REF)
  53684. /* Force a cache lookup */
  53685. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  53686. /* Force a cache update */
  53687. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  53688. /* This should set the timeout to 0 and call the remove callback from within
  53689. * the session cache. */
  53690. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  53691. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  53692. ExpectIntEQ(clientSessRemCountFree, 1);
  53693. #endif
  53694. /* Server session is in the cache so ex_data isn't free'd with the SSL
  53695. * object */
  53696. ExpectIntEQ(serverSessRemCountFree, 0);
  53697. /* Force a cache lookup */
  53698. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  53699. /* Force a cache update */
  53700. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  53701. /* This should set the timeout to 0 and call the remove callback from within
  53702. * the session cache. */
  53703. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  53704. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  53705. ExpectIntEQ(serverSessRemCountFree, 1);
  53706. /* Need to free the references that we kept */
  53707. SSL_CTX_free(serverSessCtx);
  53708. SSL_SESSION_free(serverSess);
  53709. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  53710. !defined(NO_SESSION_CACHE_REF)
  53711. SSL_CTX_free(clientSessCtx);
  53712. SSL_SESSION_free(clientSess);
  53713. #endif
  53714. #endif
  53715. return EXPECT_RESULT();
  53716. }
  53717. static int test_wolfSSL_ticket_keys(void)
  53718. {
  53719. EXPECT_DECLS;
  53720. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  53721. !defined(NO_WOLFSSL_SERVER)
  53722. WOLFSSL_CTX* ctx = NULL;
  53723. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  53724. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  53725. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  53726. WOLFSSL_FAILURE);
  53727. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  53728. WOLFSSL_FAILURE);
  53729. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  53730. WOLFSSL_FAILURE);
  53731. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  53732. WOLFSSL_FAILURE);
  53733. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  53734. WOLFSSL_FAILURE);
  53735. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  53736. WOLFSSL_FAILURE);
  53737. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  53738. WOLFSSL_FAILURE);
  53739. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  53740. WOLFSSL_FAILURE);
  53741. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  53742. WOLFSSL_FAILURE);
  53743. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  53744. WOLFSSL_FAILURE);
  53745. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  53746. WOLFSSL_FAILURE);
  53747. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  53748. WOLFSSL_FAILURE);
  53749. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  53750. WOLFSSL_FAILURE);
  53751. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  53752. WOLFSSL_FAILURE);
  53753. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  53754. WOLFSSL_SUCCESS);
  53755. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  53756. WOLFSSL_SUCCESS);
  53757. wolfSSL_CTX_free(ctx);
  53758. #endif
  53759. return EXPECT_RESULT();
  53760. }
  53761. #ifndef NO_BIO
  53762. static int test_wolfSSL_d2i_PUBKEY(void)
  53763. {
  53764. EXPECT_DECLS;
  53765. #if defined(OPENSSL_EXTRA)
  53766. BIO* bio = NULL;
  53767. EVP_PKEY* pkey = NULL;
  53768. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53769. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  53770. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  53771. /* RSA PUBKEY test */
  53772. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  53773. sizeof_client_keypub_der_2048), 0);
  53774. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  53775. EVP_PKEY_free(pkey);
  53776. pkey = NULL;
  53777. #endif
  53778. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  53779. /* ECC PUBKEY test */
  53780. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  53781. sizeof_ecc_clikeypub_der_256), 0);
  53782. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  53783. EVP_PKEY_free(pkey);
  53784. pkey = NULL;
  53785. #endif
  53786. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  53787. /* DSA PUBKEY test */
  53788. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  53789. sizeof_dsa_pub_key_der_2048), 0);
  53790. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  53791. EVP_PKEY_free(pkey);
  53792. pkey = NULL;
  53793. #endif
  53794. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  53795. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  53796. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  53797. (HAVE_FIPS_VERSION > 2))
  53798. /* DH PUBKEY test */
  53799. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  53800. sizeof_dh_pub_key_der_2048), 0);
  53801. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  53802. EVP_PKEY_free(pkey);
  53803. pkey = NULL;
  53804. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53805. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  53806. BIO_free(bio);
  53807. (void)pkey;
  53808. #endif
  53809. return EXPECT_RESULT();
  53810. }
  53811. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  53812. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  53813. {
  53814. EXPECT_DECLS;
  53815. BIO* bio = NULL;
  53816. EVP_PKEY* pkey = NULL;
  53817. WOLFSSL_CTX* ctx = NULL;
  53818. #if defined(WOLFSSL_KEY_GEN)
  53819. unsigned char buff[4096];
  53820. unsigned char* bufPtr = buff;
  53821. #endif
  53822. /* test creating new EVP_PKEY with bad arg */
  53823. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  53824. /* test loading RSA key using BIO */
  53825. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  53826. {
  53827. XFILE file = XBADFILE;
  53828. const char* fname = "./certs/server-key.der";
  53829. size_t sz = 0;
  53830. byte* buf = NULL;
  53831. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  53832. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  53833. ExpectTrue((sz = XFTELL(file)) != 0);
  53834. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  53835. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  53836. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  53837. if (file != XBADFILE) {
  53838. XFCLOSE(file);
  53839. }
  53840. /* Test using BIO new mem and loading DER private key */
  53841. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  53842. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  53843. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  53844. BIO_free(bio);
  53845. bio = NULL;
  53846. EVP_PKEY_free(pkey);
  53847. pkey = NULL;
  53848. }
  53849. #endif
  53850. /* test loading ECC key using BIO */
  53851. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  53852. {
  53853. XFILE file = XBADFILE;
  53854. const char* fname = "./certs/ecc-key.der";
  53855. size_t sz = 0;
  53856. byte* buf = NULL;
  53857. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  53858. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  53859. ExpectTrue((sz = XFTELL(file)) != 0);
  53860. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  53861. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  53862. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  53863. if (file != XBADFILE)
  53864. XFCLOSE(file);
  53865. /* Test using BIO new mem and loading DER private key */
  53866. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  53867. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  53868. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  53869. BIO_free(bio);
  53870. bio = NULL;
  53871. EVP_PKEY_free(pkey);
  53872. pkey = NULL;
  53873. }
  53874. #endif
  53875. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53876. #ifndef NO_WOLFSSL_SERVER
  53877. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  53878. #else
  53879. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  53880. #endif
  53881. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  53882. {
  53883. RSA* rsa = NULL;
  53884. /* Tests bad parameters */
  53885. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  53886. /* RSA not set yet, expecting to fail*/
  53887. rsa = wolfSSL_RSA_new();
  53888. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_FAILURE);
  53889. wolfSSL_RSA_free(rsa);
  53890. rsa = NULL;
  53891. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  53892. /* set RSA using bio*/
  53893. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  53894. sizeof_client_key_der_2048), 0);
  53895. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  53896. ExpectNotNull(rsa);
  53897. /* Tests bad parameters */
  53898. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  53899. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, NULL), BAD_FUNC_ARG);
  53900. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(NULL, rsa), BAD_FUNC_ARG);
  53901. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  53902. /* i2d RSAprivate key tests */
  53903. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  53904. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  53905. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  53906. sizeof_client_key_der_2048);
  53907. bufPtr -= sizeof_client_key_der_2048;
  53908. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  53909. sizeof_client_key_der_2048), 0);
  53910. bufPtr = NULL;
  53911. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  53912. sizeof_client_key_der_2048);
  53913. ExpectNotNull(bufPtr);
  53914. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  53915. sizeof_client_key_der_2048), 0);
  53916. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  53917. RSA_free(rsa);
  53918. rsa = RSA_new();
  53919. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  53920. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  53921. RSA_free(rsa);
  53922. }
  53923. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  53924. SSL_CTX_free(ctx);
  53925. ctx = NULL;
  53926. BIO_free(bio);
  53927. bio = NULL;
  53928. return EXPECT_RESULT();
  53929. }
  53930. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  53931. #endif /* !NO_BIO */
  53932. static int test_wolfSSL_sk_GENERAL_NAME(void)
  53933. {
  53934. EXPECT_DECLS;
  53935. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  53936. !defined(NO_RSA)
  53937. X509* x509 = NULL;
  53938. GENERAL_NAME* gn = NULL;
  53939. unsigned char buf[4096];
  53940. const unsigned char* bufPt = NULL;
  53941. int bytes = 0;
  53942. int i;
  53943. int j;
  53944. XFILE f = XBADFILE;
  53945. STACK_OF(GENERAL_NAME)* sk = NULL;
  53946. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  53947. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  53948. if (f != XBADFILE)
  53949. XFCLOSE(f);
  53950. for (j = 0; j < 2; ++j) {
  53951. bufPt = buf;
  53952. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  53953. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  53954. NID_subject_alt_name, NULL, NULL));
  53955. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  53956. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  53957. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  53958. if (gn != NULL) {
  53959. switch (gn->type) {
  53960. case GEN_DNS:
  53961. fprintf(stderr, "found type GEN_DNS\n");
  53962. break;
  53963. case GEN_EMAIL:
  53964. fprintf(stderr, "found type GEN_EMAIL\n");
  53965. break;
  53966. case GEN_URI:
  53967. fprintf(stderr, "found type GEN_URI\n");
  53968. break;
  53969. }
  53970. }
  53971. }
  53972. X509_free(x509);
  53973. x509 = NULL;
  53974. if (j == 0) {
  53975. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  53976. }
  53977. else {
  53978. /*
  53979. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  53980. * it was supposed to. This is a regression test for that bug.
  53981. */
  53982. GENERAL_NAMES_free(sk);
  53983. }
  53984. sk = NULL;
  53985. }
  53986. #endif
  53987. return EXPECT_RESULT();
  53988. }
  53989. static int test_wolfSSL_GENERAL_NAME_print(void)
  53990. {
  53991. EXPECT_DECLS;
  53992. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  53993. X509* x509 = NULL;
  53994. GENERAL_NAME* gn = NULL;
  53995. unsigned char buf[4096];
  53996. const unsigned char* bufPt = NULL;
  53997. int bytes;
  53998. XFILE f = XBADFILE;
  53999. STACK_OF(GENERAL_NAME)* sk = NULL;
  54000. BIO* out = NULL;
  54001. unsigned char outbuf[128];
  54002. X509_EXTENSION* ext = NULL;
  54003. AUTHORITY_INFO_ACCESS* aia = NULL;
  54004. ACCESS_DESCRIPTION* ad = NULL;
  54005. ASN1_IA5STRING *dnsname = NULL;
  54006. const unsigned char v4Addr[] = {192,168,53,1};
  54007. const unsigned char v6Addr[] =
  54008. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  54009. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  54010. const unsigned char email[] =
  54011. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  54012. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  54013. const char* dnsStr = "DNS:example.com";
  54014. const char* uriStr = "URI:http://127.0.0.1:22220";
  54015. const char* v4addStr = "IP Address:192.168.53.1";
  54016. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  54017. const char* emailStr = "email:info@wolfssl.com";
  54018. const char* othrStr = "othername:<unsupported>";
  54019. const char* x400Str = "X400Name:<unsupported>";
  54020. const char* ediStr = "EdiPartyName:<unsupported>";
  54021. /* BIO to output */
  54022. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  54023. /* test for NULL param */
  54024. gn = NULL;
  54025. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  54026. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  54027. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  54028. /* test for GEN_DNS */
  54029. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  54030. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  54031. if (f != XBADFILE) {
  54032. XFCLOSE(f);
  54033. f = XBADFILE;
  54034. }
  54035. bufPt = buf;
  54036. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  54037. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  54038. NID_subject_alt_name, NULL, NULL));
  54039. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  54040. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  54041. XMEMSET(outbuf, 0, sizeof(outbuf));
  54042. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  54043. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  54044. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  54045. gn = NULL;
  54046. sk = NULL;
  54047. X509_free(x509);
  54048. x509 = NULL;
  54049. /* Lets test for setting as well. */
  54050. ExpectNotNull(gn = GENERAL_NAME_new());
  54051. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  54052. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  54053. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  54054. dnsname = NULL;
  54055. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  54056. XMEMSET(outbuf, 0, sizeof(outbuf));
  54057. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  54058. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  54059. GENERAL_NAME_free(gn);
  54060. /* test for GEN_URI */
  54061. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  54062. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  54063. if (f != XBADFILE) {
  54064. XFCLOSE(f);
  54065. f = XBADFILE;
  54066. }
  54067. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  54068. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  54069. ext));
  54070. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  54071. if (ad != NULL) {
  54072. gn = ad->location;
  54073. }
  54074. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  54075. gn = NULL;
  54076. XMEMSET(outbuf,0,sizeof(outbuf));
  54077. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  54078. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  54079. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  54080. aia = NULL;
  54081. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  54082. ExpectNotNull(aia);
  54083. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  54084. aia = NULL;
  54085. X509_free(x509);
  54086. x509 = NULL;
  54087. /* test for GEN_IPADD */
  54088. /* ip v4 address */
  54089. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  54090. if (gn != NULL) {
  54091. gn->type = GEN_IPADD;
  54092. if (gn->d.iPAddress != NULL) {
  54093. gn->d.iPAddress->length = sizeof(v4Addr);
  54094. }
  54095. }
  54096. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  54097. sizeof(v4Addr)), 1);
  54098. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  54099. XMEMSET(outbuf,0,sizeof(outbuf));
  54100. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  54101. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  54102. GENERAL_NAME_free(gn);
  54103. gn = NULL;
  54104. /* ip v6 address */
  54105. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  54106. if (gn != NULL) {
  54107. gn->type = GEN_IPADD;
  54108. if (gn->d.iPAddress != NULL) {
  54109. gn->d.iPAddress->length = sizeof(v6Addr);
  54110. }
  54111. }
  54112. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  54113. sizeof(v6Addr)), 1);
  54114. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  54115. XMEMSET(outbuf,0,sizeof(outbuf));
  54116. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  54117. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  54118. GENERAL_NAME_free(gn);
  54119. gn = NULL;
  54120. /* test for GEN_EMAIL */
  54121. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  54122. if (gn != NULL) {
  54123. gn->type = GEN_EMAIL;
  54124. if (gn->d.rfc822Name != NULL) {
  54125. gn->d.rfc822Name->length = sizeof(email);
  54126. }
  54127. }
  54128. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  54129. 1);
  54130. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  54131. XMEMSET(outbuf,0,sizeof(outbuf));
  54132. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  54133. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  54134. GENERAL_NAME_free(gn);
  54135. gn = NULL;
  54136. /* test for GEN_OTHERNAME */
  54137. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  54138. if (gn != NULL) {
  54139. gn->type = GEN_OTHERNAME;
  54140. }
  54141. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  54142. XMEMSET(outbuf,0,sizeof(outbuf));
  54143. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  54144. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  54145. GENERAL_NAME_free(gn);
  54146. gn = NULL;
  54147. /* test for GEN_X400 */
  54148. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  54149. if (gn != NULL) {
  54150. gn->type = GEN_X400;
  54151. }
  54152. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  54153. XMEMSET(outbuf,0,sizeof(outbuf));
  54154. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  54155. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  54156. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  54157. if (gn != NULL) {
  54158. gn->type = GEN_IA5;
  54159. }
  54160. GENERAL_NAME_free(gn);
  54161. gn = NULL;
  54162. /* test for GEN_EDIPARTY */
  54163. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  54164. if (gn != NULL) {
  54165. gn->type = GEN_EDIPARTY;
  54166. }
  54167. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  54168. XMEMSET(outbuf,0,sizeof(outbuf));
  54169. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  54170. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  54171. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  54172. if (gn != NULL) {
  54173. gn->type = GEN_IA5;
  54174. }
  54175. GENERAL_NAME_free(gn);
  54176. gn = NULL;
  54177. BIO_free(out);
  54178. #endif /* OPENSSL_ALL */
  54179. return EXPECT_RESULT();
  54180. }
  54181. static int test_wolfSSL_sk_DIST_POINT(void)
  54182. {
  54183. EXPECT_DECLS;
  54184. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  54185. !defined(NO_RSA)
  54186. X509* x509 = NULL;
  54187. unsigned char buf[4096];
  54188. const unsigned char* bufPt;
  54189. int bytes = 0;
  54190. int i = 0;
  54191. int j = 0;
  54192. XFILE f = XBADFILE;
  54193. DIST_POINT* dp = NULL;
  54194. DIST_POINT_NAME* dpn = NULL;
  54195. GENERAL_NAME* gn = NULL;
  54196. ASN1_IA5STRING* uri = NULL;
  54197. STACK_OF(DIST_POINT)* dps = NULL;
  54198. STACK_OF(GENERAL_NAME)* gns = NULL;
  54199. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  54200. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  54201. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  54202. if (f != XBADFILE)
  54203. XFCLOSE(f);
  54204. bufPt = buf;
  54205. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  54206. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  54207. NID_crl_distribution_points, NULL, NULL));
  54208. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  54209. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  54210. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  54211. ExpectNotNull(dpn = dp->distpoint);
  54212. /* this should be type 0, fullname */
  54213. ExpectIntEQ(dpn->type, 0);
  54214. ExpectNotNull(gns = dp->distpoint->name.fullname);
  54215. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  54216. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  54217. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  54218. ExpectIntEQ(gn->type, GEN_URI);
  54219. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  54220. ExpectNotNull(uri->data);
  54221. ExpectIntGT(uri->length, 0);
  54222. }
  54223. }
  54224. X509_free(x509);
  54225. CRL_DIST_POINTS_free(dps);
  54226. #endif
  54227. return EXPECT_RESULT();
  54228. }
  54229. static int test_wolfSSL_verify_mode(void)
  54230. {
  54231. EXPECT_DECLS;
  54232. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  54233. WOLFSSL* ssl = NULL;
  54234. WOLFSSL_CTX* ctx = NULL;
  54235. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54236. ExpectNotNull(ssl = SSL_new(ctx));
  54237. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  54238. SSL_free(ssl);
  54239. ssl = NULL;
  54240. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  54241. ExpectNotNull(ssl = SSL_new(ctx));
  54242. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  54243. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  54244. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  54245. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  54246. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  54247. SSL_free(ssl);
  54248. ssl = NULL;
  54249. wolfSSL_CTX_set_verify(ctx,
  54250. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  54251. ExpectNotNull(ssl = SSL_new(ctx));
  54252. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  54253. ExpectIntEQ(SSL_get_verify_mode(ssl),
  54254. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  54255. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  54256. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  54257. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  54258. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  54259. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  54260. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  54261. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  54262. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  54263. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  54264. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  54265. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  54266. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  54267. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  54268. #endif
  54269. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  54270. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  54271. SSL_free(ssl);
  54272. SSL_CTX_free(ctx);
  54273. #endif
  54274. return EXPECT_RESULT();
  54275. }
  54276. static int test_wolfSSL_verify_depth(void)
  54277. {
  54278. EXPECT_DECLS;
  54279. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  54280. WOLFSSL* ssl = NULL;
  54281. WOLFSSL_CTX* ctx = NULL;
  54282. long depth;
  54283. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54284. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  54285. ExpectNotNull(ssl = SSL_new(ctx));
  54286. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  54287. SSL_free(ssl);
  54288. ssl = NULL;
  54289. SSL_CTX_set_verify_depth(ctx, -1);
  54290. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  54291. SSL_CTX_set_verify_depth(ctx, 2);
  54292. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  54293. ExpectNotNull(ssl = SSL_new(ctx));
  54294. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  54295. SSL_free(ssl);
  54296. SSL_CTX_free(ctx);
  54297. #endif
  54298. return EXPECT_RESULT();
  54299. }
  54300. static int test_wolfSSL_verify_result(void)
  54301. {
  54302. EXPECT_DECLS;
  54303. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  54304. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  54305. WOLFSSL* ssl = NULL;
  54306. WOLFSSL_CTX* ctx = NULL;
  54307. long result = 0xDEADBEEF;
  54308. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  54309. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54310. ExpectNotNull(ssl = SSL_new(ctx));
  54311. wolfSSL_set_verify_result(ssl, result);
  54312. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  54313. SSL_free(ssl);
  54314. SSL_CTX_free(ctx);
  54315. #endif
  54316. return EXPECT_RESULT();
  54317. }
  54318. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  54319. static void sslMsgCb(int w, int version, int type, const void* buf,
  54320. size_t sz, SSL* ssl, void* arg)
  54321. {
  54322. int i;
  54323. unsigned char* pt = (unsigned char*)buf;
  54324. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  54325. (w)?"Writing":"Reading", (int)sz, version, type);
  54326. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  54327. fprintf(stderr, "\n");
  54328. (void)ssl;
  54329. (void)arg;
  54330. }
  54331. #endif /* OPENSSL_EXTRA */
  54332. static int test_wolfSSL_msg_callback(void)
  54333. {
  54334. EXPECT_DECLS;
  54335. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  54336. WOLFSSL* ssl = NULL;
  54337. WOLFSSL_CTX* ctx = NULL;
  54338. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54339. ExpectNotNull(ssl = SSL_new(ctx));
  54340. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  54341. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  54342. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  54343. SSL_free(ssl);
  54344. SSL_CTX_free(ctx);
  54345. #endif
  54346. return EXPECT_RESULT();
  54347. }
  54348. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  54349. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  54350. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  54351. static void binary_dump(void *ptr, int size)
  54352. {
  54353. #ifdef WOLFSSL_EVP_PRINT
  54354. int i = 0;
  54355. unsigned char *p = (unsigned char *) ptr;
  54356. fprintf(stderr, "{");
  54357. while ((p != NULL) && (i < size)) {
  54358. if ((i % 8) == 0) {
  54359. fprintf(stderr, "\n");
  54360. fprintf(stderr, " ");
  54361. }
  54362. fprintf(stderr, "0x%02x, ", p[i]);
  54363. i++;
  54364. }
  54365. fprintf(stderr, "\n};\n");
  54366. #else
  54367. (void) ptr;
  54368. (void) size;
  54369. #endif
  54370. }
  54371. static int last_val = 0x0f;
  54372. static int check_result(unsigned char *data, int len)
  54373. {
  54374. int i;
  54375. for ( ; len; ) {
  54376. last_val = (last_val + 1) % 16;
  54377. for (i = 0; i < 16; len--, i++, data++)
  54378. if (*data != last_val) {
  54379. return -1;
  54380. }
  54381. }
  54382. return 0;
  54383. }
  54384. static int r_offset;
  54385. static int w_offset;
  54386. static void init_offset(void)
  54387. {
  54388. r_offset = 0;
  54389. w_offset = 0;
  54390. }
  54391. static void get_record(unsigned char *data, unsigned char *buf, int len)
  54392. {
  54393. XMEMCPY(buf, data+r_offset, len);
  54394. r_offset += len;
  54395. }
  54396. static void set_record(unsigned char *data, unsigned char *buf, int len)
  54397. {
  54398. XMEMCPY(data+w_offset, buf, len);
  54399. w_offset += len;
  54400. }
  54401. static void set_plain(unsigned char *plain, int rec)
  54402. {
  54403. int i, j;
  54404. unsigned char *p = plain;
  54405. #define BLOCKSZ 16
  54406. for (i=0; i<(rec/BLOCKSZ); i++) {
  54407. for (j=0; j<BLOCKSZ; j++)
  54408. *p++ = (i % 16);
  54409. }
  54410. }
  54411. #endif
  54412. static int test_wolfSSL_EVP_Cipher_extra(void)
  54413. {
  54414. EXPECT_DECLS;
  54415. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  54416. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  54417. /* aes128-cbc, keylen=16, ivlen=16 */
  54418. byte aes128_cbc_key[] = {
  54419. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  54420. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  54421. };
  54422. byte aes128_cbc_iv[] = {
  54423. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  54424. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  54425. };
  54426. /* teset data size table */
  54427. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  54428. int test_drive2[] = {8, 3, 8, 512, 0};
  54429. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  54430. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  54431. int test_drive_len[100];
  54432. int ret = 0;
  54433. EVP_CIPHER_CTX *evp = NULL;
  54434. int ilen = 0;
  54435. int klen = 0;
  54436. int i, j;
  54437. const EVP_CIPHER *type;
  54438. byte *iv;
  54439. byte *key;
  54440. int ivlen;
  54441. int keylen;
  54442. #define RECORDS 16
  54443. #define BUFFSZ 512
  54444. byte plain [BUFFSZ * RECORDS];
  54445. byte cipher[BUFFSZ * RECORDS];
  54446. byte inb[BUFFSZ];
  54447. byte outb[BUFFSZ+16];
  54448. int outl = 0;
  54449. int inl;
  54450. iv = aes128_cbc_iv;
  54451. ivlen = sizeof(aes128_cbc_iv);
  54452. key = aes128_cbc_key;
  54453. keylen = sizeof(aes128_cbc_key);
  54454. type = EVP_aes_128_cbc();
  54455. set_plain(plain, BUFFSZ * RECORDS);
  54456. SSL_library_init();
  54457. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  54458. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  54459. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  54460. klen = EVP_CIPHER_CTX_key_length(evp);
  54461. if (klen > 0 && keylen != klen) {
  54462. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  54463. }
  54464. ilen = EVP_CIPHER_CTX_iv_length(evp);
  54465. if (ilen > 0 && ivlen != ilen) {
  54466. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  54467. }
  54468. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  54469. for (j = 0; j<RECORDS; j++)
  54470. {
  54471. inl = BUFFSZ;
  54472. get_record(plain, inb, inl);
  54473. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  54474. set_record(cipher, outb, outl);
  54475. }
  54476. for (i = 0; test_drive[i]; i++) {
  54477. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  54478. init_offset();
  54479. test_drive_len[i] = 0;
  54480. for (j = 0; test_drive[i][j]; j++)
  54481. {
  54482. inl = test_drive[i][j];
  54483. test_drive_len[i] += inl;
  54484. get_record(plain, inb, inl);
  54485. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  54486. 0);
  54487. /* output to cipher buffer, so that following Dec test can detect
  54488. if any error */
  54489. set_record(cipher, outb, outl);
  54490. }
  54491. EVP_CipherFinal(evp, outb, &outl);
  54492. if (outl > 0)
  54493. set_record(cipher, outb, outl);
  54494. }
  54495. for (i = 0; test_drive[i]; i++) {
  54496. last_val = 0x0f;
  54497. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  54498. init_offset();
  54499. for (j = 0; test_drive[i][j]; j++) {
  54500. inl = test_drive[i][j];
  54501. get_record(cipher, inb, inl);
  54502. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  54503. 0);
  54504. binary_dump(outb, outl);
  54505. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  54506. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  54507. }
  54508. ret = EVP_CipherFinal(evp, outb, &outl);
  54509. binary_dump(outb, outl);
  54510. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  54511. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  54512. ExpectTrue(ret);
  54513. }
  54514. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  54515. EVP_CIPHER_CTX_free(evp);
  54516. evp = NULL;
  54517. /* Do an extra test to verify correct behavior with empty input. */
  54518. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  54519. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  54520. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  54521. klen = EVP_CIPHER_CTX_key_length(evp);
  54522. if (klen > 0 && keylen != klen) {
  54523. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  54524. }
  54525. ilen = EVP_CIPHER_CTX_iv_length(evp);
  54526. if (ilen > 0 && ivlen != ilen) {
  54527. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  54528. }
  54529. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  54530. /* outl should be set to 0 after passing NULL, 0 for input args. */
  54531. outl = -1;
  54532. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  54533. ExpectIntEQ(outl, 0);
  54534. EVP_CIPHER_CTX_free(evp);
  54535. #endif /* test_EVP_Cipher */
  54536. return EXPECT_RESULT();
  54537. }
  54538. static int test_wolfSSL_PEM_read_DHparams(void)
  54539. {
  54540. EXPECT_DECLS;
  54541. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  54542. !defined(NO_FILESYSTEM)
  54543. DH* dh = NULL;
  54544. XFILE fp = XBADFILE;
  54545. unsigned char derOut[300];
  54546. unsigned char* derOutBuf = derOut;
  54547. int derOutSz = 0;
  54548. unsigned char derExpected[300];
  54549. int derExpectedSz = 0;
  54550. XMEMSET(derOut, 0, sizeof(derOut));
  54551. XMEMSET(derExpected, 0, sizeof(derExpected));
  54552. /* open DH param file, read into DH struct */
  54553. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  54554. /* bad args */
  54555. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  54556. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  54557. /* good args */
  54558. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  54559. if (fp != XBADFILE) {
  54560. XFCLOSE(fp);
  54561. fp = XBADFILE;
  54562. }
  54563. /* read in certs/dh2048.der for comparison against exported params */
  54564. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  54565. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  54566. fp), 0);
  54567. if (fp != XBADFILE) {
  54568. XFCLOSE(fp);
  54569. fp = XBADFILE;
  54570. }
  54571. /* export DH back to DER and compare */
  54572. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  54573. ExpectIntEQ(derOutSz, derExpectedSz);
  54574. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  54575. DH_free(dh);
  54576. dh = NULL;
  54577. /* Test parsing with X9.42 header */
  54578. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  54579. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  54580. if (fp != XBADFILE)
  54581. XFCLOSE(fp);
  54582. DH_free(dh);
  54583. dh = NULL;
  54584. #endif
  54585. return EXPECT_RESULT();
  54586. }
  54587. static int test_wolfSSL_X509_get_serialNumber(void)
  54588. {
  54589. EXPECT_DECLS;
  54590. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  54591. ASN1_INTEGER* a = NULL;
  54592. BIGNUM* bn = NULL;
  54593. X509* x509 = NULL;
  54594. char *serialHex = NULL;
  54595. byte serial[3];
  54596. int serialSz;
  54597. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  54598. SSL_FILETYPE_PEM));
  54599. ExpectNotNull(a = X509_get_serialNumber(x509));
  54600. /* check on value of ASN1 Integer */
  54601. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  54602. a = NULL;
  54603. /* test setting serial number and then retrieving it */
  54604. ExpectNotNull(a = ASN1_INTEGER_new());
  54605. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  54606. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  54607. serialSz = sizeof(serial);
  54608. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  54609. WOLFSSL_SUCCESS);
  54610. ExpectIntEQ(serialSz, 1);
  54611. ExpectIntEQ(serial[0], 3);
  54612. ASN1_INTEGER_free(a);
  54613. a = NULL;
  54614. /* test setting serial number with 0's in it */
  54615. serial[0] = 0x01;
  54616. serial[1] = 0x00;
  54617. serial[2] = 0x02;
  54618. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  54619. if (a != NULL) {
  54620. a->data[0] = ASN_INTEGER;
  54621. a->data[1] = sizeof(serial);
  54622. XMEMCPY(&a->data[2], serial, sizeof(serial));
  54623. a->length = sizeof(serial) + 2;
  54624. }
  54625. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  54626. XMEMSET(serial, 0, sizeof(serial));
  54627. serialSz = sizeof(serial);
  54628. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  54629. WOLFSSL_SUCCESS);
  54630. ExpectIntEQ(serialSz, 3);
  54631. ExpectIntEQ(serial[0], 0x01);
  54632. ExpectIntEQ(serial[1], 0x00);
  54633. ExpectIntEQ(serial[2], 0x02);
  54634. ASN1_INTEGER_free(a);
  54635. a = NULL;
  54636. X509_free(x509); /* free's a */
  54637. ExpectNotNull(serialHex = BN_bn2hex(bn));
  54638. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  54639. ExpectStrEQ(serialHex, "01");
  54640. #else
  54641. ExpectStrEQ(serialHex, "1");
  54642. #endif
  54643. OPENSSL_free(serialHex);
  54644. ExpectIntEQ(BN_get_word(bn), 1);
  54645. BN_free(bn);
  54646. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  54647. ExpectNotNull(a = ASN1_INTEGER_new());
  54648. if (a != NULL) {
  54649. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  54650. DYNAMIC_TYPE_OPENSSL));
  54651. a->isDynamic = 1;
  54652. ASN1_INTEGER_free(a);
  54653. }
  54654. #endif
  54655. return EXPECT_RESULT();
  54656. }
  54657. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  54658. {
  54659. EXPECT_DECLS;
  54660. #if defined(OPENSSL_EXTRA)
  54661. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  54662. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  54663. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  54664. #endif
  54665. return EXPECT_RESULT();
  54666. }
  54667. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  54668. {
  54669. EXPECT_DECLS;
  54670. #if defined(OPENSSL_EXTRA)
  54671. #define MAX_HEXSTR_BUFSZ 9
  54672. #define NUM_CASES 5
  54673. struct Output {
  54674. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  54675. long ret;
  54676. };
  54677. int i;
  54678. int j;
  54679. const char* inputs[NUM_CASES] = {
  54680. "aabcd1357e",
  54681. "01:12:23:34:a5:b6:c7:d8:e9",
  54682. ":01:02",
  54683. "012",
  54684. ":ab:ac:d"
  54685. };
  54686. struct Output expectedOutputs[NUM_CASES] = {
  54687. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  54688. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  54689. {{0x01, 0x02}, 2},
  54690. {{0x00}, 0},
  54691. {{0x00}, 0}
  54692. };
  54693. long len = 0;
  54694. unsigned char* returnedBuf = NULL;
  54695. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  54696. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  54697. if (returnedBuf == NULL) {
  54698. ExpectIntEQ(expectedOutputs[i].ret, 0);
  54699. continue;
  54700. }
  54701. ExpectIntEQ(expectedOutputs[i].ret, len);
  54702. for (j = 0; j < len; ++j) {
  54703. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  54704. }
  54705. OPENSSL_free(returnedBuf);
  54706. returnedBuf = NULL;
  54707. }
  54708. #endif
  54709. return EXPECT_RESULT();
  54710. }
  54711. static int test_wolfSSL_X509_CA_num(void)
  54712. {
  54713. EXPECT_DECLS;
  54714. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  54715. defined(HAVE_ECC) && !defined(NO_RSA)
  54716. WOLFSSL_X509_STORE *store = NULL;
  54717. WOLFSSL_X509 *x509_1 = NULL;
  54718. WOLFSSL_X509 *x509_2 = NULL;
  54719. int ca_num = 0;
  54720. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  54721. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  54722. WOLFSSL_FILETYPE_PEM));
  54723. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  54724. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  54725. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  54726. WOLFSSL_FILETYPE_PEM));
  54727. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  54728. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  54729. wolfSSL_X509_free(x509_1);
  54730. wolfSSL_X509_free(x509_2);
  54731. wolfSSL_X509_STORE_free(store);
  54732. #endif
  54733. return EXPECT_RESULT();
  54734. }
  54735. static int test_wolfSSL_X509_check_ca(void)
  54736. {
  54737. EXPECT_DECLS;
  54738. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  54739. WOLFSSL_X509 *x509 = NULL;
  54740. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  54741. WOLFSSL_FILETYPE_PEM));
  54742. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  54743. wolfSSL_X509_free(x509);
  54744. #endif
  54745. return EXPECT_RESULT();
  54746. }
  54747. static int test_wolfSSL_X509_check_ip_asc(void)
  54748. {
  54749. EXPECT_DECLS;
  54750. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  54751. WOLFSSL_X509 *x509 = NULL;
  54752. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  54753. WOLFSSL_FILETYPE_PEM));
  54754. #if 0
  54755. /* TODO: add cert gen for testing positive case */
  54756. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  54757. #endif
  54758. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  54759. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  54760. wolfSSL_X509_free(x509);
  54761. #endif
  54762. return EXPECT_RESULT();
  54763. }
  54764. static int test_wolfSSL_make_cert(void)
  54765. {
  54766. EXPECT_DECLS;
  54767. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  54768. defined(WOLFSSL_CERT_EXT)
  54769. int ret = 0;
  54770. Cert cert;
  54771. CertName name;
  54772. RsaKey key;
  54773. WC_RNG rng;
  54774. byte der[FOURK_BUF];
  54775. word32 idx = 0;
  54776. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  54777. #ifdef OPENSSL_EXTRA
  54778. const unsigned char* pt = NULL;
  54779. int certSz = 0;
  54780. X509* x509 = NULL;
  54781. X509_NAME* x509name = NULL;
  54782. X509_NAME_ENTRY* entry = NULL;
  54783. ASN1_STRING* entryValue = NULL;
  54784. #endif
  54785. XMEMSET(&name, 0, sizeof(CertName));
  54786. /* set up cert name */
  54787. XMEMCPY(name.country, "US", sizeof("US"));
  54788. name.countryEnc = CTC_PRINTABLE;
  54789. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  54790. name.stateEnc = CTC_UTF8;
  54791. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  54792. name.localityEnc = CTC_UTF8;
  54793. XMEMCPY(name.sur, "Test", sizeof("Test"));
  54794. name.surEnc = CTC_UTF8;
  54795. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  54796. name.orgEnc = CTC_UTF8;
  54797. XMEMCPY(name.unit, "Development", sizeof("Development"));
  54798. name.unitEnc = CTC_UTF8;
  54799. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  54800. name.commonNameEnc = CTC_UTF8;
  54801. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  54802. name.serialDevEnc = CTC_PRINTABLE;
  54803. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  54804. name.userIdEnc = CTC_PRINTABLE;
  54805. #ifdef WOLFSSL_MULTI_ATTRIB
  54806. #if CTC_MAX_ATTRIB > 2
  54807. {
  54808. NameAttrib* n;
  54809. n = &name.name[0];
  54810. n->id = ASN_DOMAIN_COMPONENT;
  54811. n->type = CTC_UTF8;
  54812. n->sz = sizeof("com");
  54813. XMEMCPY(n->value, "com", sizeof("com"));
  54814. n = &name.name[1];
  54815. n->id = ASN_DOMAIN_COMPONENT;
  54816. n->type = CTC_UTF8;
  54817. n->sz = sizeof("wolfssl");
  54818. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  54819. }
  54820. #endif
  54821. #endif /* WOLFSSL_MULTI_ATTRIB */
  54822. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  54823. #ifndef HAVE_FIPS
  54824. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  54825. #else
  54826. ExpectIntEQ(wc_InitRng(&rng), 0);
  54827. #endif
  54828. /* load test RSA key */
  54829. idx = 0;
  54830. #if defined(USE_CERT_BUFFERS_1024)
  54831. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  54832. sizeof_server_key_der_1024), 0);
  54833. #elif defined(USE_CERT_BUFFERS_2048)
  54834. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  54835. sizeof_server_key_der_2048), 0);
  54836. #else
  54837. /* error case, no RSA key loaded, happens later */
  54838. (void)idx;
  54839. #endif
  54840. XMEMSET(&cert, 0 , sizeof(Cert));
  54841. ExpectIntEQ(wc_InitCert(&cert), 0);
  54842. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  54843. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  54844. cert.serialSz = (int)sizeof(mySerial);
  54845. cert.isCA = 1;
  54846. #ifndef NO_SHA256
  54847. cert.sigType = CTC_SHA256wRSA;
  54848. #else
  54849. cert.sigType = CTC_SHAwRSA;
  54850. #endif
  54851. /* add SKID from the Public Key */
  54852. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  54853. /* add AKID from the Public Key */
  54854. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  54855. ret = 0;
  54856. do {
  54857. #if defined(WOLFSSL_ASYNC_CRYPT)
  54858. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  54859. #endif
  54860. if (ret >= 0) {
  54861. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  54862. }
  54863. } while (ret == WC_PENDING_E);
  54864. ExpectIntGT(ret, 0);
  54865. #ifdef OPENSSL_EXTRA
  54866. /* der holds a certificate with DC's now check X509 parsing of it */
  54867. certSz = ret;
  54868. pt = der;
  54869. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  54870. ExpectNotNull(x509name = X509_get_subject_name(x509));
  54871. #ifdef WOLFSSL_MULTI_ATTRIB
  54872. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  54873. -1)), 5);
  54874. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  54875. (int)idx)), 6);
  54876. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  54877. (int)idx)), -1);
  54878. #endif /* WOLFSSL_MULTI_ATTRIB */
  54879. /* compare DN at index 0 */
  54880. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  54881. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  54882. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  54883. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  54884. #ifndef WOLFSSL_MULTI_ATTRIB
  54885. /* compare Serial Number */
  54886. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  54887. -1)), 7);
  54888. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  54889. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  54890. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  54891. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  54892. #endif
  54893. #ifdef WOLFSSL_MULTI_ATTRIB
  54894. /* get first and second DC and compare result */
  54895. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  54896. -1)), 5);
  54897. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  54898. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  54899. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  54900. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  54901. (int)idx)), 6);
  54902. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  54903. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  54904. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  54905. #endif /* WOLFSSL_MULTI_ATTRIB */
  54906. /* try invalid index locations for regression test and sanity check */
  54907. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  54908. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  54909. X509_free(x509);
  54910. #endif /* OPENSSL_EXTRA */
  54911. wc_FreeRsaKey(&key);
  54912. wc_FreeRng(&rng);
  54913. #endif
  54914. return EXPECT_RESULT();
  54915. }
  54916. static int test_wolfSSL_X509_get_version(void)
  54917. {
  54918. EXPECT_DECLS;
  54919. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  54920. WOLFSSL_X509 *x509 = NULL;
  54921. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  54922. WOLFSSL_FILETYPE_PEM));
  54923. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  54924. wolfSSL_X509_free(x509);
  54925. #endif
  54926. return EXPECT_RESULT();
  54927. }
  54928. #if defined(OPENSSL_ALL)
  54929. static int test_wolfSSL_sk_CIPHER_description(void)
  54930. {
  54931. EXPECT_DECLS;
  54932. #if !defined(NO_RSA)
  54933. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  54934. int i;
  54935. int numCiphers = 0;
  54936. const SSL_METHOD *method = NULL;
  54937. const SSL_CIPHER *cipher = NULL;
  54938. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  54939. SSL_CTX *ctx = NULL;
  54940. SSL *ssl = NULL;
  54941. char buf[256];
  54942. char test_str[9] = "0000000";
  54943. const char badStr[] = "unknown";
  54944. const char certPath[] = "./certs/client-cert.pem";
  54945. XMEMSET(buf, 0, sizeof(buf));
  54946. ExpectNotNull(method = TLSv1_2_client_method());
  54947. ExpectNotNull(ctx = SSL_CTX_new(method));
  54948. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  54949. SSL_CTX_set_verify_depth(ctx, 4);
  54950. SSL_CTX_set_options(ctx, flags);
  54951. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  54952. WOLFSSL_SUCCESS);
  54953. ExpectNotNull(ssl = SSL_new(ctx));
  54954. /* SSL_get_ciphers returns a stack of all configured ciphers
  54955. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  54956. */
  54957. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  54958. /* loop through the amount of supportedCiphers */
  54959. numCiphers = sk_num(supportedCiphers);
  54960. for (i = 0; i < numCiphers; ++i) {
  54961. int j;
  54962. /* sk_value increments "sk->data.cipher->cipherOffset".
  54963. * wolfSSL_sk_CIPHER_description sets the description for
  54964. * the cipher based on the provided offset.
  54965. */
  54966. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  54967. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  54968. }
  54969. /* Search cipher description string for "unknown" descriptor */
  54970. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  54971. int k = 0;
  54972. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  54973. test_str[k] = badStr[k];
  54974. j++;
  54975. k++;
  54976. }
  54977. }
  54978. /* Fail if test_str == badStr == "unknown" */
  54979. ExpectStrNE(test_str,badStr);
  54980. }
  54981. SSL_free(ssl);
  54982. SSL_CTX_free(ctx);
  54983. #endif
  54984. return EXPECT_RESULT();
  54985. }
  54986. static int test_wolfSSL_get_ciphers_compat(void)
  54987. {
  54988. EXPECT_DECLS;
  54989. #if !defined(NO_RSA)
  54990. const SSL_METHOD *method = NULL;
  54991. const char certPath[] = "./certs/client-cert.pem";
  54992. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  54993. SSL_CTX *ctx = NULL;
  54994. WOLFSSL *ssl = NULL;
  54995. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  54996. ExpectNotNull(method = SSLv23_client_method());
  54997. ExpectNotNull(ctx = SSL_CTX_new(method));
  54998. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  54999. SSL_CTX_set_verify_depth(ctx, 4);
  55000. SSL_CTX_set_options(ctx, flags);
  55001. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  55002. WOLFSSL_SUCCESS);
  55003. ExpectNotNull(ssl = SSL_new(ctx));
  55004. /* Test Bad NULL input */
  55005. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  55006. /* Test for Good input */
  55007. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  55008. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  55009. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  55010. SSL_free(ssl);
  55011. SSL_CTX_free(ctx);
  55012. #endif
  55013. return EXPECT_RESULT();
  55014. }
  55015. static int test_wolfSSL_X509_PUBKEY_get(void)
  55016. {
  55017. EXPECT_DECLS;
  55018. WOLFSSL_X509_PUBKEY pubkey;
  55019. WOLFSSL_X509_PUBKEY* key;
  55020. WOLFSSL_EVP_PKEY evpkey ;
  55021. WOLFSSL_EVP_PKEY* evpPkey;
  55022. WOLFSSL_EVP_PKEY* retEvpPkey;
  55023. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  55024. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  55025. key = &pubkey;
  55026. evpPkey = &evpkey;
  55027. evpPkey->type = WOLFSSL_SUCCESS;
  55028. key->pkey = evpPkey;
  55029. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  55030. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  55031. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  55032. key->pkey = NULL;
  55033. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  55034. return EXPECT_RESULT();
  55035. }
  55036. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  55037. {
  55038. EXPECT_DECLS;
  55039. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  55040. DSA *dsa = NULL;
  55041. DSA *setDsa = NULL;
  55042. EVP_PKEY *pkey = NULL;
  55043. EVP_PKEY *set1Pkey = NULL;
  55044. SHA_CTX sha;
  55045. byte signature[DSA_SIG_SIZE];
  55046. byte hash[WC_SHA_DIGEST_SIZE];
  55047. word32 bytes;
  55048. int answer;
  55049. #ifdef USE_CERT_BUFFERS_1024
  55050. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  55051. int dsaKeySz = sizeof_dsa_key_der_1024;
  55052. byte tmp[ONEK_BUF];
  55053. XMEMSET(tmp, 0, sizeof(tmp));
  55054. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  55055. bytes = dsaKeySz;
  55056. #elif defined(USE_CERT_BUFFERS_2048)
  55057. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  55058. int dsaKeySz = sizeof_dsa_key_der_2048;
  55059. byte tmp[TWOK_BUF];
  55060. XMEMSET(tmp, 0, sizeof(tmp));
  55061. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  55062. bytes = (word32)dsaKeySz;
  55063. #else
  55064. byte tmp[TWOK_BUF];
  55065. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  55066. int dsaKeySz;
  55067. XFILE fp = XBADFILE;
  55068. XMEMSET(tmp, 0, sizeof(tmp));
  55069. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  55070. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  55071. if (fp != XBADFILE)
  55072. XFCLOSE(fp);
  55073. #endif /* END USE_CERT_BUFFERS_1024 */
  55074. /* Create hash to later Sign and Verify */
  55075. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  55076. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  55077. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  55078. /* Initialize pkey with der format dsa key */
  55079. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  55080. (long)dsaKeySz));
  55081. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  55082. /* Should Fail: NULL argument */
  55083. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  55084. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  55085. /* Should Pass: Initialized pkey argument */
  55086. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  55087. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  55088. #ifdef USE_CERT_BUFFERS_1024
  55089. ExpectIntEQ(DSA_bits(dsa), 1024);
  55090. #else
  55091. ExpectIntEQ(DSA_bits(dsa), 2048);
  55092. #endif
  55093. /* Sign */
  55094. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  55095. /* Verify. */
  55096. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  55097. WOLFSSL_SUCCESS);
  55098. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  55099. /* Should Fail: set1Pkey not initialized */
  55100. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  55101. /* Initialize set1Pkey */
  55102. set1Pkey = EVP_PKEY_new();
  55103. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  55104. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  55105. WOLFSSL_SUCCESS);
  55106. /* Should Pass: set dsa into set1Pkey */
  55107. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  55108. DSA_free(dsa);
  55109. DSA_free(setDsa);
  55110. EVP_PKEY_free(pkey);
  55111. EVP_PKEY_free(set1Pkey);
  55112. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  55113. return EXPECT_RESULT();
  55114. } /* END test_EVP_PKEY_set1_get1_DSA */
  55115. static int test_wolfSSL_DSA_generate_parameters(void)
  55116. {
  55117. EXPECT_DECLS;
  55118. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  55119. !defined(HAVE_FIPS)
  55120. DSA *dsa = NULL;
  55121. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  55122. NULL));
  55123. DSA_free(dsa);
  55124. #endif
  55125. return EXPECT_RESULT();
  55126. }
  55127. static int test_wolfSSL_DSA_SIG(void)
  55128. {
  55129. EXPECT_DECLS;
  55130. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  55131. !defined(HAVE_FIPS)
  55132. DSA *dsa = NULL;
  55133. DSA *dsa2 = NULL;
  55134. DSA_SIG *sig = NULL;
  55135. const BIGNUM *p = NULL;
  55136. const BIGNUM *q = NULL;
  55137. const BIGNUM *g = NULL;
  55138. const BIGNUM *pub = NULL;
  55139. const BIGNUM *priv = NULL;
  55140. BIGNUM *dup_p = NULL;
  55141. BIGNUM *dup_q = NULL;
  55142. BIGNUM *dup_g = NULL;
  55143. BIGNUM *dup_pub = NULL;
  55144. BIGNUM *dup_priv = NULL;
  55145. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  55146. ExpectNotNull(dsa = DSA_new());
  55147. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  55148. NULL), 1);
  55149. ExpectIntEQ(DSA_generate_key(dsa), 1);
  55150. DSA_get0_pqg(dsa, &p, &q, &g);
  55151. DSA_get0_key(dsa, &pub, &priv);
  55152. ExpectNotNull(dup_p = BN_dup(p));
  55153. ExpectNotNull(dup_q = BN_dup(q));
  55154. ExpectNotNull(dup_g = BN_dup(g));
  55155. ExpectNotNull(dup_pub = BN_dup(pub));
  55156. ExpectNotNull(dup_priv = BN_dup(priv));
  55157. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  55158. ExpectNotNull(dsa2 = DSA_new());
  55159. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  55160. if (EXPECT_FAIL()) {
  55161. BN_free(dup_p);
  55162. BN_free(dup_q);
  55163. BN_free(dup_g);
  55164. }
  55165. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  55166. if (EXPECT_FAIL()) {
  55167. BN_free(dup_pub);
  55168. BN_free(dup_priv);
  55169. }
  55170. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  55171. DSA_free(dsa);
  55172. DSA_free(dsa2);
  55173. DSA_SIG_free(sig);
  55174. #endif
  55175. return EXPECT_RESULT();
  55176. }
  55177. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  55178. {
  55179. EXPECT_DECLS;
  55180. #ifdef HAVE_ECC
  55181. WOLFSSL_EC_KEY* ecKey = NULL;
  55182. WOLFSSL_EC_KEY* ecGet1 = NULL;
  55183. EVP_PKEY* pkey = NULL;
  55184. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55185. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55186. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  55187. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  55188. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  55189. /* Should fail since ecKey is empty */
  55190. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  55191. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55192. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  55193. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  55194. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  55195. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  55196. wolfSSL_EC_KEY_free(ecKey);
  55197. wolfSSL_EC_KEY_free(ecGet1);
  55198. EVP_PKEY_free(pkey);
  55199. #endif /* HAVE_ECC */
  55200. return EXPECT_RESULT();
  55201. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  55202. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  55203. {
  55204. EXPECT_DECLS;
  55205. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  55206. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  55207. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  55208. DH *dh = NULL;
  55209. DH *setDh = NULL;
  55210. EVP_PKEY *pkey = NULL;
  55211. XFILE f = XBADFILE;
  55212. unsigned char buf[4096];
  55213. const unsigned char* pt = buf;
  55214. const char* dh2048 = "./certs/dh2048.der";
  55215. long len = 0;
  55216. int code = -1;
  55217. XMEMSET(buf, 0, sizeof(buf));
  55218. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  55219. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55220. if (f != XBADFILE)
  55221. XFCLOSE(f);
  55222. /* Load dh2048.der into DH with internal format */
  55223. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  55224. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  55225. ExpectIntEQ(code, 0);
  55226. code = -1;
  55227. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55228. /* Set DH into PKEY */
  55229. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  55230. /* Get DH from PKEY */
  55231. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  55232. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  55233. ExpectIntEQ(code, 0);
  55234. EVP_PKEY_free(pkey);
  55235. DH_free(setDh);
  55236. setDh = NULL;
  55237. DH_free(dh);
  55238. dh = NULL;
  55239. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  55240. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  55241. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  55242. return EXPECT_RESULT();
  55243. } /* END test_EVP_PKEY_set1_get1_DH */
  55244. static int test_wolfSSL_CTX_ctrl(void)
  55245. {
  55246. EXPECT_DECLS;
  55247. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  55248. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  55249. char caFile[] = "./certs/client-ca.pem";
  55250. char clientFile[] = "./certs/client-cert.pem";
  55251. SSL_CTX* ctx = NULL;
  55252. X509* x509 = NULL;
  55253. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  55254. byte buf[6000];
  55255. char file[] = "./certs/dsaparams.pem";
  55256. XFILE f = XBADFILE;
  55257. int bytes = 0;
  55258. BIO* bio = NULL;
  55259. DSA* dsa = NULL;
  55260. DH* dh = NULL;
  55261. #endif
  55262. #ifdef HAVE_ECC
  55263. WOLFSSL_EC_KEY* ecKey = NULL;
  55264. #endif
  55265. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  55266. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  55267. WOLFSSL_FILETYPE_PEM));
  55268. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  55269. if (EXPECT_FAIL()) {
  55270. wolfSSL_X509_free(x509);
  55271. }
  55272. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  55273. WOLFSSL_FILETYPE_PEM));
  55274. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  55275. /* Initialize DH */
  55276. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  55277. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  55278. if (f != XBADFILE)
  55279. XFCLOSE(f);
  55280. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  55281. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  55282. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  55283. #endif
  55284. #ifdef HAVE_ECC
  55285. /* Initialize WOLFSSL_EC_KEY */
  55286. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55287. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55288. #endif
  55289. /* additional test of getting EVP_PKEY key size from X509
  55290. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  55291. * allowed with user RSA */
  55292. {
  55293. EVP_PKEY* pkey = NULL;
  55294. #if defined(HAVE_ECC)
  55295. X509* ecX509 = NULL;
  55296. #endif /* HAVE_ECC */
  55297. ExpectNotNull(pkey = X509_get_pubkey(x509));
  55298. /* current RSA key is 2048 bit (256 bytes) */
  55299. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  55300. EVP_PKEY_free(pkey);
  55301. pkey = NULL;
  55302. #if defined(HAVE_ECC)
  55303. #if defined(USE_CERT_BUFFERS_256)
  55304. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  55305. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  55306. SSL_FILETYPE_ASN1));
  55307. #else
  55308. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  55309. cliEccCertFile, SSL_FILETYPE_PEM));
  55310. #endif
  55311. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  55312. /* current ECC key is 256 bit (32 bytes) */
  55313. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  55314. X509_free(ecX509);
  55315. EVP_PKEY_free(pkey);
  55316. #endif /* HAVE_ECC */
  55317. }
  55318. /* Tests should fail with passed in NULL pointer */
  55319. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  55320. SSL_FAILURE);
  55321. #if !defined(NO_DH) && !defined(NO_DSA)
  55322. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  55323. SSL_FAILURE);
  55324. #endif
  55325. #ifdef HAVE_ECC
  55326. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  55327. SSL_FAILURE);
  55328. #endif
  55329. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  55330. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  55331. */
  55332. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  55333. SSL_SUCCESS);
  55334. if (EXPECT_FAIL()) {
  55335. wolfSSL_X509_free(x509);
  55336. }
  55337. /* Test with SSL_CTRL_OPTIONS
  55338. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  55339. */
  55340. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  55341. NULL) == SSL_OP_NO_TLSv1);
  55342. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  55343. /* Test with SSL_CTRL_SET_TMP_DH
  55344. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  55345. */
  55346. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  55347. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  55348. SSL_SUCCESS);
  55349. #endif
  55350. /* Test with SSL_CTRL_SET_TMP_ECDH
  55351. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  55352. */
  55353. #ifdef HAVE_ECC
  55354. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  55355. SSL_SUCCESS);
  55356. #endif
  55357. #ifdef WOLFSSL_ENCRYPTED_KEYS
  55358. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  55359. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  55360. #endif
  55361. /* Test for min/max proto */
  55362. #ifndef WOLFSSL_NO_TLS12
  55363. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  55364. 0, NULL), SSL_SUCCESS);
  55365. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  55366. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  55367. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  55368. #endif
  55369. #ifdef WOLFSSL_TLS13
  55370. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  55371. 0, NULL), SSL_SUCCESS);
  55372. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  55373. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  55374. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  55375. #ifndef WOLFSSL_NO_TLS12
  55376. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  55377. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  55378. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  55379. #endif
  55380. #endif
  55381. /* Cleanup and Pass */
  55382. #if !defined(NO_DH) && !defined(NO_DSA)
  55383. #ifndef NO_BIO
  55384. BIO_free(bio);
  55385. DSA_free(dsa);
  55386. DH_free(dh);
  55387. dh = NULL;
  55388. #endif
  55389. #endif
  55390. #ifdef HAVE_ECC
  55391. wolfSSL_EC_KEY_free(ecKey);
  55392. #endif
  55393. SSL_CTX_free(ctx);
  55394. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  55395. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  55396. return EXPECT_RESULT();
  55397. }
  55398. static int test_wolfSSL_EVP_PKEY_assign(void)
  55399. {
  55400. EXPECT_DECLS;
  55401. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  55402. int type;
  55403. WOLFSSL_EVP_PKEY* pkey = NULL;
  55404. #ifndef NO_RSA
  55405. WOLFSSL_RSA* rsa = NULL;
  55406. #endif
  55407. #ifndef NO_DSA
  55408. WOLFSSL_DSA* dsa = NULL;
  55409. #endif
  55410. #ifdef HAVE_ECC
  55411. WOLFSSL_EC_KEY* ecKey = NULL;
  55412. #endif
  55413. #ifndef NO_RSA
  55414. type = EVP_PKEY_RSA;
  55415. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55416. ExpectNotNull(rsa = wolfSSL_RSA_new());
  55417. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  55418. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  55419. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  55420. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  55421. if (EXPECT_FAIL()) {
  55422. wolfSSL_RSA_free(rsa);
  55423. }
  55424. wolfSSL_EVP_PKEY_free(pkey);
  55425. pkey = NULL;
  55426. #endif /* NO_RSA */
  55427. #ifndef NO_DSA
  55428. type = EVP_PKEY_DSA;
  55429. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55430. ExpectNotNull(dsa = wolfSSL_DSA_new());
  55431. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  55432. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  55433. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  55434. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  55435. if (EXPECT_FAIL()) {
  55436. wolfSSL_DSA_free(dsa);
  55437. }
  55438. wolfSSL_EVP_PKEY_free(pkey);
  55439. pkey = NULL;
  55440. #endif /* NO_DSA */
  55441. #ifdef HAVE_ECC
  55442. type = EVP_PKEY_EC;
  55443. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55444. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55445. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  55446. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  55447. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  55448. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  55449. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55450. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  55451. if (EXPECT_FAIL()) {
  55452. wolfSSL_EC_KEY_free(ecKey);
  55453. }
  55454. wolfSSL_EVP_PKEY_free(pkey);
  55455. pkey = NULL;
  55456. #endif /* HAVE_ECC */
  55457. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  55458. return EXPECT_RESULT();
  55459. }
  55460. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  55461. {
  55462. EXPECT_DECLS;
  55463. #if !defined(NO_DH) && \
  55464. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  55465. XFILE f = XBADFILE;
  55466. unsigned char buf[4096];
  55467. const unsigned char* pt = buf;
  55468. const char* params1 = "./certs/dh2048.der";
  55469. long len = 0;
  55470. WOLFSSL_DH* dh = NULL;
  55471. WOLFSSL_EVP_PKEY* pkey = NULL;
  55472. XMEMSET(buf, 0, sizeof(buf));
  55473. /* Load DH parameters DER. */
  55474. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  55475. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55476. if (f != XBADFILE)
  55477. XFCLOSE(f);
  55478. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  55479. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  55480. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55481. /* Bad cases */
  55482. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  55483. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  55484. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  55485. /* Good case */
  55486. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  55487. if (EXPECT_FAIL()) {
  55488. wolfSSL_DH_free(dh);
  55489. }
  55490. EVP_PKEY_free(pkey);
  55491. #endif
  55492. return EXPECT_RESULT();
  55493. }
  55494. static int test_wolfSSL_EVP_PKEY_base_id(void)
  55495. {
  55496. EXPECT_DECLS;
  55497. WOLFSSL_EVP_PKEY* pkey = NULL;
  55498. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55499. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  55500. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  55501. EVP_PKEY_free(pkey);
  55502. return EXPECT_RESULT();
  55503. }
  55504. static int test_wolfSSL_EVP_PKEY_id(void)
  55505. {
  55506. EXPECT_DECLS;
  55507. WOLFSSL_EVP_PKEY* pkey = NULL;
  55508. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55509. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  55510. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  55511. EVP_PKEY_free(pkey);
  55512. return EXPECT_RESULT();
  55513. }
  55514. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  55515. {
  55516. EXPECT_DECLS;
  55517. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  55518. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  55519. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  55520. ECC_MIN_KEY_SZ <= 256)
  55521. EVP_PKEY_CTX* ctx = NULL;
  55522. EVP_PKEY* pkey = NULL;
  55523. /* Test error conditions. */
  55524. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  55525. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  55526. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  55527. #ifndef NO_RSA
  55528. EVP_PKEY_CTX_free(ctx);
  55529. /* Parameter generation for RSA not supported yet. */
  55530. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  55531. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  55532. #endif
  55533. #ifdef HAVE_ECC
  55534. EVP_PKEY_CTX_free(ctx);
  55535. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  55536. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  55537. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  55538. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  55539. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  55540. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  55541. WOLFSSL_SUCCESS);
  55542. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  55543. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  55544. #endif
  55545. EVP_PKEY_CTX_free(ctx);
  55546. EVP_PKEY_free(pkey);
  55547. #endif
  55548. return EXPECT_RESULT();
  55549. }
  55550. static int test_wolfSSL_EVP_PKEY_keygen(void)
  55551. {
  55552. EXPECT_DECLS;
  55553. WOLFSSL_EVP_PKEY* pkey = NULL;
  55554. EVP_PKEY_CTX* ctx = NULL;
  55555. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  55556. WOLFSSL_EVP_PKEY* params = NULL;
  55557. DH* dh = NULL;
  55558. const BIGNUM* pubkey = NULL;
  55559. const BIGNUM* privkey = NULL;
  55560. ASN1_INTEGER* asn1int = NULL;
  55561. unsigned int length = 0;
  55562. byte* derBuffer = NULL;
  55563. #endif
  55564. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55565. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  55566. /* Bad cases */
  55567. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  55568. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  55569. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  55570. /* Good case */
  55571. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  55572. EVP_PKEY_CTX_free(ctx);
  55573. ctx = NULL;
  55574. EVP_PKEY_free(pkey);
  55575. pkey = NULL;
  55576. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  55577. /* Test DH keygen */
  55578. {
  55579. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  55580. ExpectNotNull(dh = DH_get_2048_256());
  55581. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  55582. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  55583. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  55584. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  55585. DH_free(dh);
  55586. dh = NULL;
  55587. EVP_PKEY_CTX_free(ctx);
  55588. EVP_PKEY_free(params);
  55589. /* try exporting generated key to DER, to verify */
  55590. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  55591. DH_get0_key(dh, &pubkey, &privkey);
  55592. ExpectNotNull(pubkey);
  55593. ExpectNotNull(privkey);
  55594. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  55595. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  55596. ASN1_INTEGER_free(asn1int);
  55597. DH_free(dh);
  55598. dh = NULL;
  55599. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55600. EVP_PKEY_free(pkey);
  55601. }
  55602. #endif
  55603. return EXPECT_RESULT();
  55604. }
  55605. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  55606. {
  55607. EXPECT_DECLS;
  55608. WOLFSSL_EVP_PKEY* pkey = NULL;
  55609. EVP_PKEY_CTX *ctx = NULL;
  55610. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55611. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  55612. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  55613. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  55614. EVP_PKEY_CTX_free(ctx);
  55615. EVP_PKEY_free(pkey);
  55616. return EXPECT_RESULT();
  55617. }
  55618. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  55619. {
  55620. EXPECT_DECLS;
  55621. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  55622. WOLFSSL_EVP_PKEY* pkey = NULL;
  55623. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55624. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  55625. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  55626. EVP_PKEY_free(pkey);
  55627. #endif
  55628. return EXPECT_RESULT();
  55629. }
  55630. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  55631. {
  55632. EXPECT_DECLS;
  55633. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  55634. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  55635. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  55636. !defined(NO_FILESYSTEM)
  55637. WOLFSSL_EVP_PKEY* params = NULL;
  55638. WOLFSSL_EVP_PKEY* copy = NULL;
  55639. DH* dh = NULL;
  55640. BIGNUM* p1;
  55641. BIGNUM* g1;
  55642. BIGNUM* q1;
  55643. BIGNUM* p2;
  55644. BIGNUM* g2;
  55645. BIGNUM* q2;
  55646. /* create DH with DH_get_2048_256 params */
  55647. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  55648. ExpectNotNull(dh = DH_get_2048_256());
  55649. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  55650. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  55651. (const BIGNUM**)&q1,
  55652. (const BIGNUM**)&g1);
  55653. DH_free(dh);
  55654. dh = NULL;
  55655. /* create DH with random generated DH params */
  55656. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  55657. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  55658. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  55659. DH_free(dh);
  55660. dh = NULL;
  55661. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  55662. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  55663. ExpectNotNull(dh->p);
  55664. ExpectNotNull(dh->g);
  55665. ExpectNotNull(dh->q);
  55666. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  55667. (const BIGNUM**)&q2,
  55668. (const BIGNUM**)&g2);
  55669. ExpectIntEQ(BN_cmp(p1, p2), 0);
  55670. ExpectIntEQ(BN_cmp(q1, q2), 0);
  55671. ExpectIntEQ(BN_cmp(g1, g2), 0);
  55672. DH_free(dh);
  55673. dh = NULL;
  55674. EVP_PKEY_free(copy);
  55675. EVP_PKEY_free(params);
  55676. #endif
  55677. return EXPECT_RESULT();
  55678. }
  55679. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  55680. {
  55681. EXPECT_DECLS;
  55682. WOLFSSL_EVP_PKEY* pkey = NULL;
  55683. EVP_PKEY_CTX* ctx = NULL;
  55684. int bits = 2048;
  55685. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  55686. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  55687. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  55688. WOLFSSL_SUCCESS);
  55689. EVP_PKEY_CTX_free(ctx);
  55690. EVP_PKEY_free(pkey);
  55691. return EXPECT_RESULT();
  55692. }
  55693. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  55694. {
  55695. EXPECT_DECLS;
  55696. /* This is large enough to be used for all key sizes */
  55697. byte key[AES_256_KEY_SIZE] = {0};
  55698. byte iv[AES_BLOCK_SIZE] = {0};
  55699. int i;
  55700. int nids[] = {
  55701. #ifdef HAVE_AES_CBC
  55702. NID_aes_128_cbc,
  55703. #endif
  55704. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  55705. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  55706. #ifdef HAVE_AESGCM
  55707. NID_aes_128_gcm,
  55708. #endif
  55709. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  55710. #ifdef WOLFSSL_AES_COUNTER
  55711. NID_aes_128_ctr,
  55712. #endif
  55713. #ifndef NO_DES3
  55714. NID_des_cbc,
  55715. NID_des_ede3_cbc,
  55716. #endif
  55717. };
  55718. int iv_lengths[] = {
  55719. #ifdef HAVE_AES_CBC
  55720. AES_BLOCK_SIZE,
  55721. #endif
  55722. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  55723. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  55724. #ifdef HAVE_AESGCM
  55725. GCM_NONCE_MID_SZ,
  55726. #endif
  55727. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  55728. #ifdef WOLFSSL_AES_COUNTER
  55729. AES_BLOCK_SIZE,
  55730. #endif
  55731. #ifndef NO_DES3
  55732. DES_BLOCK_SIZE,
  55733. DES_BLOCK_SIZE,
  55734. #endif
  55735. };
  55736. int nidsLen = (sizeof(nids)/sizeof(int));
  55737. for (i = 0; i < nidsLen; i++) {
  55738. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  55739. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  55740. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  55741. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  55742. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  55743. EVP_CIPHER_CTX_free(ctx);
  55744. }
  55745. return EXPECT_RESULT();
  55746. }
  55747. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  55748. {
  55749. EXPECT_DECLS;
  55750. byte key[AES_256_KEY_SIZE] = {0};
  55751. byte iv[AES_BLOCK_SIZE] = {0};
  55752. int i;
  55753. int nids[] = {
  55754. #ifdef HAVE_AES_CBC
  55755. NID_aes_128_cbc,
  55756. NID_aes_256_cbc,
  55757. #endif
  55758. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  55759. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  55760. #ifdef HAVE_AESGCM
  55761. NID_aes_128_gcm,
  55762. NID_aes_256_gcm,
  55763. #endif
  55764. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  55765. #ifdef WOLFSSL_AES_COUNTER
  55766. NID_aes_128_ctr,
  55767. NID_aes_256_ctr,
  55768. #endif
  55769. #ifndef NO_DES3
  55770. NID_des_cbc,
  55771. NID_des_ede3_cbc,
  55772. #endif
  55773. };
  55774. int key_lengths[] = {
  55775. #ifdef HAVE_AES_CBC
  55776. AES_128_KEY_SIZE,
  55777. AES_256_KEY_SIZE,
  55778. #endif
  55779. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  55780. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  55781. #ifdef HAVE_AESGCM
  55782. AES_128_KEY_SIZE,
  55783. AES_256_KEY_SIZE,
  55784. #endif
  55785. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  55786. #ifdef WOLFSSL_AES_COUNTER
  55787. AES_128_KEY_SIZE,
  55788. AES_256_KEY_SIZE,
  55789. #endif
  55790. #ifndef NO_DES3
  55791. DES_KEY_SIZE,
  55792. DES3_KEY_SIZE,
  55793. #endif
  55794. };
  55795. int nidsLen = (sizeof(nids)/sizeof(int));
  55796. for (i = 0; i < nidsLen; i++) {
  55797. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  55798. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  55799. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  55800. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  55801. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  55802. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  55803. WOLFSSL_SUCCESS);
  55804. EVP_CIPHER_CTX_free(ctx);
  55805. }
  55806. return EXPECT_RESULT();
  55807. }
  55808. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  55809. {
  55810. EXPECT_DECLS;
  55811. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  55812. int ivLen, keyLen;
  55813. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  55814. #ifdef HAVE_AESGCM
  55815. byte key[AES_128_KEY_SIZE] = {0};
  55816. byte iv[AES_BLOCK_SIZE] = {0};
  55817. const EVP_CIPHER *init = EVP_aes_128_gcm();
  55818. #else
  55819. byte key[DES3_KEY_SIZE] = {0};
  55820. byte iv[DES_BLOCK_SIZE] = {0};
  55821. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  55822. #endif
  55823. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  55824. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  55825. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  55826. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  55827. /* Bad cases */
  55828. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  55829. WOLFSSL_FAILURE);
  55830. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  55831. WOLFSSL_FAILURE);
  55832. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  55833. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  55834. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  55835. WOLFSSL_FAILURE);
  55836. /* Good case */
  55837. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  55838. EVP_CIPHER_CTX_free(ctx);
  55839. #endif
  55840. return EXPECT_RESULT();
  55841. }
  55842. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  55843. {
  55844. EXPECT_DECLS;
  55845. WOLFSSL_ENGINE* e = NULL;
  55846. int id = 0;
  55847. EVP_PKEY_CTX *ctx = NULL;
  55848. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  55849. EVP_PKEY_CTX_free(ctx);
  55850. return EXPECT_RESULT();
  55851. }
  55852. static int test_wolfSSL_EVP_rc4(void)
  55853. {
  55854. EXPECT_DECLS;
  55855. #if !defined(NO_RC4)
  55856. ExpectNotNull(wolfSSL_EVP_rc4());
  55857. #endif
  55858. return EXPECT_RESULT();
  55859. }
  55860. static int test_wolfSSL_EVP_enc_null(void)
  55861. {
  55862. EXPECT_DECLS;
  55863. ExpectNotNull(wolfSSL_EVP_enc_null());
  55864. return EXPECT_RESULT();
  55865. }
  55866. static int test_wolfSSL_EVP_rc2_cbc(void)
  55867. {
  55868. EXPECT_DECLS;
  55869. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  55870. ExpectNull(wolfSSL_EVP_rc2_cbc());
  55871. #endif
  55872. return EXPECT_RESULT();
  55873. }
  55874. static int test_wolfSSL_EVP_mdc2(void)
  55875. {
  55876. EXPECT_DECLS;
  55877. #if !defined(NO_WOLFSSL_STUB)
  55878. ExpectNull(wolfSSL_EVP_mdc2());
  55879. #endif
  55880. return EXPECT_RESULT();
  55881. }
  55882. static int test_wolfSSL_EVP_md4(void)
  55883. {
  55884. EXPECT_DECLS;
  55885. #if !defined(NO_MD4)
  55886. ExpectNotNull(wolfSSL_EVP_md4());
  55887. #endif
  55888. return EXPECT_RESULT();
  55889. }
  55890. static int test_wolfSSL_EVP_aes_256_gcm(void)
  55891. {
  55892. EXPECT_DECLS;
  55893. #ifdef HAVE_AESGCM
  55894. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  55895. #endif
  55896. return EXPECT_RESULT();
  55897. }
  55898. static int test_wolfSSL_EVP_aes_192_gcm(void)
  55899. {
  55900. EXPECT_DECLS;
  55901. #ifdef HAVE_AESGCM
  55902. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  55903. #endif
  55904. return EXPECT_RESULT();
  55905. }
  55906. static int test_wolfSSL_EVP_aes_256_ccm(void)
  55907. {
  55908. EXPECT_DECLS;
  55909. #ifdef HAVE_AESCCM
  55910. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  55911. #endif
  55912. return EXPECT_RESULT();
  55913. }
  55914. static int test_wolfSSL_EVP_aes_192_ccm(void)
  55915. {
  55916. EXPECT_DECLS;
  55917. #ifdef HAVE_AESCCM
  55918. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  55919. #endif
  55920. return EXPECT_RESULT();
  55921. }
  55922. static int test_wolfSSL_EVP_aes_128_ccm(void)
  55923. {
  55924. EXPECT_DECLS;
  55925. #ifdef HAVE_AESCCM
  55926. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  55927. #endif
  55928. return EXPECT_RESULT();
  55929. }
  55930. static int test_wolfSSL_EVP_ripemd160(void)
  55931. {
  55932. EXPECT_DECLS;
  55933. #if !defined(NO_WOLFSSL_STUB)
  55934. ExpectNull(wolfSSL_EVP_ripemd160());
  55935. #endif
  55936. return EXPECT_RESULT();
  55937. }
  55938. static int test_wolfSSL_EVP_get_digestbynid(void)
  55939. {
  55940. EXPECT_DECLS;
  55941. #ifndef NO_MD5
  55942. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  55943. #endif
  55944. #ifndef NO_SHA
  55945. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  55946. #endif
  55947. #ifndef NO_SHA256
  55948. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  55949. #endif
  55950. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  55951. return EXPECT_RESULT();
  55952. }
  55953. static int test_wolfSSL_EVP_MD_nid(void)
  55954. {
  55955. EXPECT_DECLS;
  55956. #ifndef NO_MD5
  55957. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  55958. #endif
  55959. #ifndef NO_SHA
  55960. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  55961. #endif
  55962. #ifndef NO_SHA256
  55963. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  55964. #endif
  55965. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  55966. return EXPECT_RESULT();
  55967. }
  55968. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  55969. {
  55970. EXPECT_DECLS;
  55971. #if defined(HAVE_ECC)
  55972. WOLFSSL_EVP_PKEY* pkey = NULL;
  55973. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  55974. ExpectNotNull(pkey = EVP_PKEY_new());
  55975. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  55976. EVP_PKEY_free(pkey);
  55977. #endif
  55978. return EXPECT_RESULT();
  55979. }
  55980. static int test_wolfSSL_EVP_X_STATE(void)
  55981. {
  55982. EXPECT_DECLS;
  55983. #if !defined(NO_DES3) && !defined(NO_RC4)
  55984. byte key[DES3_KEY_SIZE] = {0};
  55985. byte iv[DES_IV_SIZE] = {0};
  55986. EVP_CIPHER_CTX *ctx = NULL;
  55987. const EVP_CIPHER *init = NULL;
  55988. /* Bad test cases */
  55989. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  55990. ExpectNotNull(init = EVP_des_ede3_cbc());
  55991. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  55992. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  55993. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  55994. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  55995. EVP_CIPHER_CTX_free(ctx);
  55996. ctx = NULL;
  55997. /* Good test case */
  55998. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  55999. ExpectNotNull(init = wolfSSL_EVP_rc4());
  56000. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  56001. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  56002. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  56003. EVP_CIPHER_CTX_free(ctx);
  56004. #endif
  56005. return EXPECT_RESULT();
  56006. }
  56007. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  56008. {
  56009. EXPECT_DECLS;
  56010. #if !defined(NO_DES3) && !defined(NO_RC4)
  56011. byte key[DES3_KEY_SIZE] = {0};
  56012. byte iv[DES_IV_SIZE] = {0};
  56013. EVP_CIPHER_CTX *ctx = NULL;
  56014. const EVP_CIPHER *init = NULL;
  56015. /* Bad test cases */
  56016. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  56017. ExpectNotNull(init = EVP_des_ede3_cbc());
  56018. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  56019. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  56020. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  56021. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  56022. EVP_CIPHER_CTX_free(ctx);
  56023. ctx = NULL;
  56024. /* Good test case */
  56025. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  56026. ExpectNotNull(init = wolfSSL_EVP_rc4());
  56027. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  56028. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  56029. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  56030. EVP_CIPHER_CTX_free(ctx);
  56031. #endif
  56032. return EXPECT_RESULT();
  56033. }
  56034. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  56035. {
  56036. EXPECT_DECLS;
  56037. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  56038. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  56039. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  56040. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  56041. #ifdef HAVE_AES_CBC
  56042. #ifdef WOLFSSL_AES_128
  56043. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  56044. #endif
  56045. #ifdef WOLFSSL_AES_192
  56046. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  56047. #endif
  56048. #ifdef WOLFSSL_AES_256
  56049. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  56050. #endif
  56051. #endif
  56052. #ifdef HAVE_AESGCM
  56053. #ifdef WOLFSSL_AES_128
  56054. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  56055. #endif
  56056. #ifdef WOLFSSL_AES_192
  56057. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  56058. #endif
  56059. #ifdef WOLFSSL_AES_256
  56060. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  56061. #endif
  56062. #endif
  56063. #ifdef HAVE_AESCCM
  56064. #ifdef WOLFSSL_AES_128
  56065. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  56066. #endif
  56067. #ifdef WOLFSSL_AES_192
  56068. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  56069. #endif
  56070. #ifdef WOLFSSL_AES_256
  56071. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  56072. #endif
  56073. #endif
  56074. #ifdef WOLFSSL_AES_COUNTER
  56075. #ifdef WOLFSSL_AES_128
  56076. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  56077. #endif
  56078. #ifdef WOLFSSL_AES_192
  56079. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  56080. #endif
  56081. #ifdef WOLFSSL_AES_256
  56082. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  56083. #endif
  56084. #endif
  56085. #ifdef HAVE_AES_ECB
  56086. #ifdef WOLFSSL_AES_128
  56087. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  56088. #endif
  56089. #ifdef WOLFSSL_AES_192
  56090. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  56091. #endif
  56092. #ifdef WOLFSSL_AES_256
  56093. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  56094. #endif
  56095. #endif
  56096. #ifdef WOLFSSL_AES_OFB
  56097. #ifdef WOLFSSL_AES_128
  56098. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  56099. #endif
  56100. #ifdef WOLFSSL_AES_192
  56101. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  56102. #endif
  56103. #ifdef WOLFSSL_AES_256
  56104. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  56105. #endif
  56106. #endif
  56107. #ifndef NO_RC4
  56108. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  56109. #endif
  56110. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  56111. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  56112. #endif
  56113. #endif
  56114. #ifdef WOLFSSL_SM4_ECB
  56115. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  56116. #endif
  56117. #ifdef WOLFSSL_SM4_CBC
  56118. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  56119. #endif
  56120. #ifdef WOLFSSL_SM4_CTR
  56121. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  56122. #endif
  56123. #ifdef WOLFSSL_SM4_GCM
  56124. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  56125. #endif
  56126. #ifdef WOLFSSL_SM4_CCM
  56127. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  56128. #endif
  56129. return EXPECT_RESULT();
  56130. }
  56131. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  56132. {
  56133. EXPECT_DECLS;
  56134. int nids[] = {
  56135. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  56136. #ifdef WOLFSSL_AES_128
  56137. NID_aes_128_cbc,
  56138. #endif
  56139. #ifdef WOLFSSL_AES_192
  56140. NID_aes_192_cbc,
  56141. #endif
  56142. #ifdef WOLFSSL_AES_256
  56143. NID_aes_256_cbc,
  56144. #endif
  56145. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  56146. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  56147. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  56148. #ifdef HAVE_AESGCM
  56149. #ifdef WOLFSSL_AES_128
  56150. NID_aes_128_gcm,
  56151. #endif
  56152. #ifdef WOLFSSL_AES_192
  56153. NID_aes_192_gcm,
  56154. #endif
  56155. #ifdef WOLFSSL_AES_256
  56156. NID_aes_256_gcm,
  56157. #endif
  56158. #endif /* HAVE_AESGCM */
  56159. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  56160. #ifdef WOLFSSL_AES_COUNTER
  56161. #ifdef WOLFSSL_AES_128
  56162. NID_aes_128_ctr,
  56163. #endif
  56164. #ifdef WOLFSSL_AES_192
  56165. NID_aes_192_ctr,
  56166. #endif
  56167. #ifdef WOLFSSL_AES_256
  56168. NID_aes_256_ctr,
  56169. #endif
  56170. #endif
  56171. #ifndef NO_DES3
  56172. NID_des_cbc,
  56173. NID_des_ede3_cbc,
  56174. #endif
  56175. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  56176. NID_chacha20_poly1305,
  56177. #endif
  56178. };
  56179. int iv_lengths[] = {
  56180. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  56181. #ifdef WOLFSSL_AES_128
  56182. AES_BLOCK_SIZE,
  56183. #endif
  56184. #ifdef WOLFSSL_AES_192
  56185. AES_BLOCK_SIZE,
  56186. #endif
  56187. #ifdef WOLFSSL_AES_256
  56188. AES_BLOCK_SIZE,
  56189. #endif
  56190. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  56191. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  56192. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  56193. #ifdef HAVE_AESGCM
  56194. #ifdef WOLFSSL_AES_128
  56195. GCM_NONCE_MID_SZ,
  56196. #endif
  56197. #ifdef WOLFSSL_AES_192
  56198. GCM_NONCE_MID_SZ,
  56199. #endif
  56200. #ifdef WOLFSSL_AES_256
  56201. GCM_NONCE_MID_SZ,
  56202. #endif
  56203. #endif /* HAVE_AESGCM */
  56204. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  56205. #ifdef WOLFSSL_AES_COUNTER
  56206. #ifdef WOLFSSL_AES_128
  56207. AES_BLOCK_SIZE,
  56208. #endif
  56209. #ifdef WOLFSSL_AES_192
  56210. AES_BLOCK_SIZE,
  56211. #endif
  56212. #ifdef WOLFSSL_AES_256
  56213. AES_BLOCK_SIZE,
  56214. #endif
  56215. #endif
  56216. #ifndef NO_DES3
  56217. DES_BLOCK_SIZE,
  56218. DES_BLOCK_SIZE,
  56219. #endif
  56220. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  56221. CHACHA20_POLY1305_AEAD_IV_SIZE,
  56222. #endif
  56223. };
  56224. int i;
  56225. int nidsLen = (sizeof(nids)/sizeof(int));
  56226. for (i = 0; i < nidsLen; i++) {
  56227. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  56228. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  56229. }
  56230. return EXPECT_RESULT();
  56231. }
  56232. static int test_wolfSSL_EVP_SignInit_ex(void)
  56233. {
  56234. EXPECT_DECLS;
  56235. WOLFSSL_EVP_MD_CTX mdCtx;
  56236. WOLFSSL_ENGINE* e = 0;
  56237. const EVP_MD* md = EVP_sha256();
  56238. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  56239. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  56240. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  56241. return EXPECT_RESULT();
  56242. }
  56243. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  56244. {
  56245. EXPECT_DECLS;
  56246. #if !defined(NO_SHA256)
  56247. WOLFSSL_EVP_MD_CTX mdCtx;
  56248. unsigned int s = 0;
  56249. unsigned char md[WC_SHA256_DIGEST_SIZE];
  56250. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  56251. /* Bad Case */
  56252. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  56253. (HAVE_FIPS_VERSION > 2))
  56254. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  56255. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  56256. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  56257. #else
  56258. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  56259. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  56260. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  56261. #endif
  56262. /* Good Case */
  56263. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  56264. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  56265. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  56266. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  56267. #endif
  56268. return EXPECT_RESULT();
  56269. }
  56270. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  56271. {
  56272. EXPECT_DECLS;
  56273. #if defined(OPENSSL_EXTRA)
  56274. EVP_PKEY* pkey = NULL;
  56275. EVP_PKEY_CTX* ctx = NULL;
  56276. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  56277. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  56278. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  56279. /* void */
  56280. EVP_PKEY_CTX_free(ctx);
  56281. #else
  56282. /* int */
  56283. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  56284. #endif
  56285. EVP_PKEY_free(pkey);
  56286. #endif
  56287. return EXPECT_RESULT();
  56288. }
  56289. static int test_wolfSSL_EVP_PKEY_param_check(void)
  56290. {
  56291. EXPECT_DECLS;
  56292. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  56293. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  56294. DH *dh = NULL;
  56295. DH *setDh = NULL;
  56296. EVP_PKEY *pkey = NULL;
  56297. EVP_PKEY_CTX* ctx = NULL;
  56298. FILE* f = NULL;
  56299. unsigned char buf[512];
  56300. const unsigned char* pt = buf;
  56301. const char* dh2048 = "./certs/dh2048.der";
  56302. long len = 0;
  56303. int code = -1;
  56304. XMEMSET(buf, 0, sizeof(buf));
  56305. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  56306. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  56307. if (f != XBADFILE)
  56308. XFCLOSE(f);
  56309. /* Load dh2048.der into DH with internal format */
  56310. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  56311. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  56312. ExpectIntEQ(code, 0);
  56313. code = -1;
  56314. pkey = wolfSSL_EVP_PKEY_new();
  56315. /* Set DH into PKEY */
  56316. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  56317. /* create ctx from pkey */
  56318. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  56319. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  56320. /* TODO: more invalid cases */
  56321. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  56322. EVP_PKEY_CTX_free(ctx);
  56323. EVP_PKEY_free(pkey);
  56324. DH_free(setDh);
  56325. setDh = NULL;
  56326. DH_free(dh);
  56327. dh = NULL;
  56328. #endif
  56329. #endif
  56330. return EXPECT_RESULT();
  56331. }
  56332. static int test_wolfSSL_EVP_BytesToKey(void)
  56333. {
  56334. EXPECT_DECLS;
  56335. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  56336. byte key[AES_BLOCK_SIZE] = {0};
  56337. byte iv[AES_BLOCK_SIZE] = {0};
  56338. int count = 0;
  56339. const EVP_MD* md = EVP_sha256();
  56340. const EVP_CIPHER *type;
  56341. const unsigned char *salt = (unsigned char *)"salt1234";
  56342. int sz = 5;
  56343. const byte data[] = {
  56344. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  56345. 0x72,0x6c,0x64
  56346. };
  56347. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  56348. /* Bad cases */
  56349. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  56350. 0);
  56351. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  56352. 16);
  56353. md = "2";
  56354. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  56355. WOLFSSL_FAILURE);
  56356. /* Good case */
  56357. md = EVP_sha256();
  56358. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  56359. 16);
  56360. #endif
  56361. return EXPECT_RESULT();
  56362. }
  56363. static int test_evp_cipher_aes_gcm(void)
  56364. {
  56365. EXPECT_DECLS;
  56366. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  56367. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  56368. (HAVE_FIPS_VERSION >= 2)))
  56369. /*
  56370. * This test checks data at various points in the encrypt/decrypt process
  56371. * against known values produced using the same test with OpenSSL. This
  56372. * interop testing is critical for verifying the correctness of our
  56373. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  56374. * a flow supported by OpenSSL that uses the control command
  56375. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  56376. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  56377. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  56378. * wolfSSL OpenSSH clients because there was a bug in this flow that
  56379. * happened to "cancel out" if both sides of the connection had the bug.
  56380. */
  56381. enum {
  56382. NUM_ENCRYPTIONS = 3,
  56383. AAD_SIZE = 4
  56384. };
  56385. byte plainText1[] = {
  56386. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  56387. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  56388. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  56389. };
  56390. byte plainText2[] = {
  56391. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  56392. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  56393. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  56394. };
  56395. byte plainText3[] = {
  56396. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  56397. 0x5b, 0x57, 0x10
  56398. };
  56399. byte* plainTexts[NUM_ENCRYPTIONS] = {
  56400. plainText1,
  56401. plainText2,
  56402. plainText3
  56403. };
  56404. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  56405. sizeof(plainText1),
  56406. sizeof(plainText2),
  56407. sizeof(plainText3)
  56408. };
  56409. byte aad1[AAD_SIZE] = {
  56410. 0x00, 0x00, 0x00, 0x01
  56411. };
  56412. byte aad2[AAD_SIZE] = {
  56413. 0x00, 0x00, 0x00, 0x10
  56414. };
  56415. byte aad3[AAD_SIZE] = {
  56416. 0x00, 0x00, 0x01, 0x00
  56417. };
  56418. byte* aads[NUM_ENCRYPTIONS] = {
  56419. aad1,
  56420. aad2,
  56421. aad3
  56422. };
  56423. const byte iv[GCM_NONCE_MID_SZ] = {
  56424. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  56425. };
  56426. byte currentIv[GCM_NONCE_MID_SZ];
  56427. const byte key[] = {
  56428. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  56429. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  56430. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  56431. };
  56432. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  56433. {
  56434. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  56435. 0xEF
  56436. },
  56437. {
  56438. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  56439. 0xF0
  56440. },
  56441. {
  56442. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  56443. 0xF1
  56444. }
  56445. };
  56446. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  56447. {
  56448. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  56449. 0x3D, 0x32, 0x18, 0x34, 0xA9
  56450. },
  56451. {
  56452. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  56453. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  56454. },
  56455. {
  56456. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  56457. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  56458. }
  56459. };
  56460. const byte expCipherText1[] = {
  56461. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  56462. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  56463. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  56464. };
  56465. const byte expCipherText2[] = {
  56466. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  56467. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  56468. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  56469. };
  56470. const byte expCipherText3[] = {
  56471. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  56472. 0x80, 0x5E, 0x6B,
  56473. };
  56474. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  56475. expCipherText1,
  56476. expCipherText2,
  56477. expCipherText3
  56478. };
  56479. byte* cipherText = NULL;
  56480. byte* calcPlainText = NULL;
  56481. byte tag[AES_BLOCK_SIZE];
  56482. EVP_CIPHER_CTX* encCtx = NULL;
  56483. EVP_CIPHER_CTX* decCtx = NULL;
  56484. int i, j, outl;
  56485. /****************************************************/
  56486. for (i = 0; i < 3; ++i) {
  56487. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  56488. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  56489. /* First iteration, set key before IV. */
  56490. if (i == 0) {
  56491. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  56492. SSL_SUCCESS);
  56493. /*
  56494. * The call to EVP_CipherInit below (with NULL key) should clear the
  56495. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  56496. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  56497. * behavior.
  56498. */
  56499. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  56500. (void*)iv), SSL_SUCCESS);
  56501. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  56502. SSL_SUCCESS);
  56503. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  56504. currentIv), SSL_FAILURE);
  56505. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  56506. SSL_SUCCESS);
  56507. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  56508. SSL_SUCCESS);
  56509. }
  56510. /* Second iteration, IV before key. */
  56511. else {
  56512. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  56513. SSL_SUCCESS);
  56514. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  56515. SSL_SUCCESS);
  56516. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  56517. SSL_SUCCESS);
  56518. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  56519. SSL_SUCCESS);
  56520. }
  56521. /*
  56522. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  56523. * been issued first.
  56524. */
  56525. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  56526. currentIv), SSL_FAILURE);
  56527. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  56528. (void*)iv), SSL_SUCCESS);
  56529. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  56530. (void*)iv), SSL_SUCCESS);
  56531. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  56532. /*************** Encrypt ***************/
  56533. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  56534. currentIv), SSL_SUCCESS);
  56535. /* Check current IV against expected. */
  56536. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  56537. /* Add AAD. */
  56538. if (i == 2) {
  56539. /* Test streaming API. */
  56540. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  56541. AAD_SIZE), SSL_SUCCESS);
  56542. }
  56543. else {
  56544. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  56545. AAD_SIZE);
  56546. }
  56547. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  56548. DYNAMIC_TYPE_TMP_BUFFER));
  56549. /* Encrypt plaintext. */
  56550. if (i == 2) {
  56551. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  56552. plainTexts[j], plainTextSzs[j]),
  56553. SSL_SUCCESS);
  56554. }
  56555. else {
  56556. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  56557. plainTextSzs[j]), plainTextSzs[j]);
  56558. }
  56559. if (i == 2) {
  56560. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  56561. SSL_SUCCESS);
  56562. }
  56563. else {
  56564. /*
  56565. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  56566. * akin to calling EVP_CipherFinal.
  56567. */
  56568. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  56569. }
  56570. /* Check ciphertext against expected. */
  56571. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  56572. 0);
  56573. /* Get and check tag against expected. */
  56574. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  56575. sizeof(tag), tag), SSL_SUCCESS);
  56576. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  56577. /*************** Decrypt ***************/
  56578. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  56579. currentIv), SSL_SUCCESS);
  56580. /* Check current IV against expected. */
  56581. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  56582. /* Add AAD. */
  56583. if (i == 2) {
  56584. /* Test streaming API. */
  56585. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  56586. AAD_SIZE), SSL_SUCCESS);
  56587. }
  56588. else {
  56589. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  56590. AAD_SIZE);
  56591. }
  56592. /* Set expected tag. */
  56593. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  56594. sizeof(tag), tag), SSL_SUCCESS);
  56595. /* Decrypt ciphertext. */
  56596. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  56597. DYNAMIC_TYPE_TMP_BUFFER));
  56598. if (i == 2) {
  56599. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  56600. cipherText, plainTextSzs[j]),
  56601. SSL_SUCCESS);
  56602. }
  56603. else {
  56604. /* This first EVP_Cipher call will check the tag, too. */
  56605. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  56606. plainTextSzs[j]), plainTextSzs[j]);
  56607. }
  56608. if (i == 2) {
  56609. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  56610. SSL_SUCCESS);
  56611. }
  56612. else {
  56613. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  56614. }
  56615. /* Check plaintext against expected. */
  56616. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  56617. 0);
  56618. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  56619. cipherText = NULL;
  56620. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  56621. calcPlainText = NULL;
  56622. }
  56623. EVP_CIPHER_CTX_free(encCtx);
  56624. encCtx = NULL;
  56625. EVP_CIPHER_CTX_free(decCtx);
  56626. decCtx = NULL;
  56627. }
  56628. #endif
  56629. return EXPECT_RESULT();
  56630. }
  56631. static int test_wolfSSL_OBJ_ln(void)
  56632. {
  56633. EXPECT_DECLS;
  56634. const int nid_set[] = {
  56635. NID_commonName,
  56636. NID_serialNumber,
  56637. NID_countryName,
  56638. NID_localityName,
  56639. NID_stateOrProvinceName,
  56640. NID_organizationName,
  56641. NID_organizationalUnitName,
  56642. NID_domainComponent,
  56643. NID_businessCategory,
  56644. NID_jurisdictionCountryName,
  56645. NID_jurisdictionStateOrProvinceName,
  56646. NID_emailAddress
  56647. };
  56648. const char* ln_set[] = {
  56649. "commonName",
  56650. "serialNumber",
  56651. "countryName",
  56652. "localityName",
  56653. "stateOrProvinceName",
  56654. "organizationName",
  56655. "organizationalUnitName",
  56656. "domainComponent",
  56657. "businessCategory",
  56658. "jurisdictionCountryName",
  56659. "jurisdictionStateOrProvinceName",
  56660. "emailAddress",
  56661. };
  56662. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  56663. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  56664. #ifdef HAVE_ECC
  56665. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  56666. {
  56667. EC_builtin_curve r[27];
  56668. size_t nCurves = sizeof(r) / sizeof(r[0]);
  56669. nCurves = EC_get_builtin_curves(r, nCurves);
  56670. for (i = 0; i < nCurves; i++) {
  56671. /* skip ECC_CURVE_INVALID */
  56672. if (r[i].nid != ECC_CURVE_INVALID) {
  56673. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  56674. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  56675. }
  56676. }
  56677. }
  56678. #endif
  56679. #endif
  56680. for (i = 0; i < maxIdx; i++) {
  56681. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  56682. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  56683. }
  56684. return EXPECT_RESULT();
  56685. }
  56686. static int test_wolfSSL_OBJ_sn(void)
  56687. {
  56688. EXPECT_DECLS;
  56689. int i = 0, maxIdx = 7;
  56690. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  56691. NID_stateOrProvinceName,NID_organizationName,
  56692. NID_organizationalUnitName,NID_emailAddress};
  56693. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  56694. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  56695. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  56696. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  56697. WOLFSSL_EMAIL_ADDR};
  56698. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  56699. for (i = 0; i < maxIdx; i++) {
  56700. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  56701. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  56702. }
  56703. return EXPECT_RESULT();
  56704. }
  56705. #if !defined(NO_BIO)
  56706. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  56707. {
  56708. return lh_strhash(s[3]);
  56709. }
  56710. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  56711. {
  56712. return XSTRCMP(a[3], b[3]);
  56713. }
  56714. #endif
  56715. static int test_wolfSSL_TXT_DB(void)
  56716. {
  56717. EXPECT_DECLS;
  56718. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  56719. BIO *bio = NULL;
  56720. TXT_DB *db = NULL;
  56721. const int columns = 6;
  56722. const char *fields[6] = {
  56723. "V",
  56724. "320926161116Z",
  56725. "",
  56726. "12BD",
  56727. "unknown",
  56728. "/CN=rsa doe",
  56729. };
  56730. char** fields_copy = NULL;
  56731. /* Test read */
  56732. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  56733. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  56734. ExpectNotNull(db = TXT_DB_read(bio, columns));
  56735. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  56736. DYNAMIC_TYPE_OPENSSL));
  56737. if (fields_copy != NULL) {
  56738. XMEMCPY(fields_copy, fields, sizeof(fields));
  56739. }
  56740. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  56741. if (EXPECT_FAIL()) {
  56742. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  56743. }
  56744. BIO_free(bio);
  56745. bio = NULL;
  56746. /* Test write */
  56747. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  56748. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  56749. BIO_free(bio);
  56750. /* Test index */
  56751. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  56752. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  56753. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  56754. fields[3] = "12DA";
  56755. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  56756. fields[3] = "FFFF";
  56757. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  56758. fields[3] = "";
  56759. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  56760. TXT_DB_free(db);
  56761. #endif
  56762. return EXPECT_RESULT();
  56763. }
  56764. static int test_wolfSSL_NCONF(void)
  56765. {
  56766. EXPECT_DECLS;
  56767. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  56768. const char* confFile = "./tests/NCONF_test.cnf";
  56769. CONF* conf = NULL;
  56770. long eline = 0;
  56771. long num = 0;
  56772. ExpectNotNull(conf = NCONF_new(NULL));
  56773. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  56774. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  56775. ExpectIntEQ(num, 1234);
  56776. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  56777. ExpectIntEQ(num, 4321);
  56778. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  56779. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  56780. "./test-dir/file1");
  56781. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  56782. "./test-dir/file1:./test-dir/file2:./section1:file2");
  56783. NCONF_free(conf);
  56784. #endif
  56785. return EXPECT_RESULT();
  56786. }
  56787. #endif /* OPENSSL_ALL */
  56788. static int test_wolfSSL_X509V3_EXT_get(void) {
  56789. EXPECT_DECLS;
  56790. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  56791. XFILE f = XBADFILE;
  56792. int numOfExt =0;
  56793. int extNid = 0;
  56794. int i = 0;
  56795. WOLFSSL_X509* x509 = NULL;
  56796. WOLFSSL_X509_EXTENSION* ext = NULL;
  56797. const WOLFSSL_v3_ext_method* method = NULL;
  56798. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  56799. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  56800. if (f != XBADFILE)
  56801. XFCLOSE(f);
  56802. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  56803. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  56804. for (i = 0; i < numOfExt; i++) {
  56805. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  56806. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  56807. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  56808. ExpectIntEQ(method->ext_nid, extNid);
  56809. }
  56810. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  56811. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  56812. wolfSSL_X509_free(x509);
  56813. #endif
  56814. return EXPECT_RESULT();
  56815. }
  56816. static int test_wolfSSL_X509V3_EXT_nconf(void)
  56817. {
  56818. EXPECT_DECLS;
  56819. #ifdef OPENSSL_ALL
  56820. const char *ext_names[] = {
  56821. "subjectKeyIdentifier",
  56822. "authorityKeyIdentifier",
  56823. "subjectAltName",
  56824. "keyUsage",
  56825. "extendedKeyUsage",
  56826. };
  56827. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  56828. int ext_nids[] = {
  56829. NID_subject_key_identifier,
  56830. NID_authority_key_identifier,
  56831. NID_subject_alt_name,
  56832. NID_key_usage,
  56833. NID_ext_key_usage,
  56834. };
  56835. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  56836. const char *ext_values[] = {
  56837. "hash",
  56838. "hash",
  56839. "DNS:example.com, IP:127.0.0.1",
  56840. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  56841. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  56842. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  56843. "OCSPSigning",
  56844. };
  56845. size_t i;
  56846. X509_EXTENSION* ext = NULL;
  56847. X509* x509 = NULL;
  56848. unsigned int keyUsageFlags;
  56849. unsigned int extKeyUsageFlags;
  56850. ExpectNotNull(x509 = X509_new());
  56851. /* keyUsage / extKeyUsage should match string above */
  56852. keyUsageFlags = KU_DIGITAL_SIGNATURE
  56853. | KU_NON_REPUDIATION
  56854. | KU_KEY_ENCIPHERMENT
  56855. | KU_DATA_ENCIPHERMENT
  56856. | KU_KEY_AGREEMENT
  56857. | KU_KEY_CERT_SIGN
  56858. | KU_CRL_SIGN
  56859. | KU_ENCIPHER_ONLY
  56860. | KU_DECIPHER_ONLY;
  56861. extKeyUsageFlags = XKU_SSL_CLIENT
  56862. | XKU_SSL_SERVER
  56863. | XKU_CODE_SIGN
  56864. | XKU_SMIME
  56865. | XKU_TIMESTAMP
  56866. | XKU_OCSP_SIGN;
  56867. for (i = 0; i < ext_names_count; i++) {
  56868. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  56869. ext_values[i]));
  56870. X509_EXTENSION_free(ext);
  56871. ext = NULL;
  56872. }
  56873. for (i = 0; i < ext_nids_count; i++) {
  56874. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  56875. ext_values[i]));
  56876. X509_EXTENSION_free(ext);
  56877. ext = NULL;
  56878. }
  56879. /* Test adding extension to X509 */
  56880. for (i = 0; i < ext_nids_count; i++) {
  56881. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  56882. ext_values[i]));
  56883. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  56884. if (ext_nids[i] == NID_key_usage) {
  56885. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  56886. }
  56887. else if (ext_nids[i] == NID_ext_key_usage) {
  56888. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  56889. }
  56890. X509_EXTENSION_free(ext);
  56891. ext = NULL;
  56892. }
  56893. X509_free(x509);
  56894. #endif
  56895. return EXPECT_RESULT();
  56896. }
  56897. static int test_wolfSSL_X509V3_EXT(void) {
  56898. EXPECT_DECLS;
  56899. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  56900. XFILE f = XBADFILE;
  56901. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  56902. char* str = NULL;
  56903. unsigned char* data = NULL;
  56904. const WOLFSSL_v3_ext_method* method = NULL;
  56905. WOLFSSL_X509* x509 = NULL;
  56906. WOLFSSL_X509_EXTENSION* ext = NULL;
  56907. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  56908. WOLFSSL_ASN1_OBJECT *obj = NULL;
  56909. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  56910. WOLFSSL_ASN1_STRING* asn1str = NULL;
  56911. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  56912. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  56913. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  56914. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  56915. WOLFSSL_GENERAL_NAME* gn = NULL;
  56916. /* Check NULL argument */
  56917. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  56918. /* Using OCSP cert with X509V3 extensions */
  56919. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  56920. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  56921. if (f != XBADFILE)
  56922. XFCLOSE(f);
  56923. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  56924. /* Basic Constraints */
  56925. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  56926. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  56927. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  56928. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  56929. ExpectIntEQ(bc->ca, 1);
  56930. ExpectNull(bc->pathlen);
  56931. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  56932. bc = NULL;
  56933. i++;
  56934. /* Subject Key Identifier */
  56935. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  56936. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  56937. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  56938. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  56939. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  56940. asn1str));
  56941. X509_EXTENSION_free(ext2);
  56942. ext2 = NULL;
  56943. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  56944. ExpectNotNull(method->i2s);
  56945. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  56946. wolfSSL_ASN1_STRING_free(asn1str);
  56947. asn1str = NULL;
  56948. if (str != NULL) {
  56949. actual = strcmp(str,
  56950. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  56951. }
  56952. ExpectIntEQ(actual, 0);
  56953. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  56954. str = NULL;
  56955. i++;
  56956. /* Authority Key Identifier */
  56957. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  56958. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  56959. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  56960. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  56961. ext));
  56962. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  56963. ExpectNotNull(asn1str = aKeyId->keyid);
  56964. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  56965. asn1str));
  56966. asn1str = NULL;
  56967. if (str != NULL) {
  56968. actual = strcmp(str,
  56969. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  56970. }
  56971. ExpectIntEQ(actual, 0);
  56972. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  56973. str = NULL;
  56974. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  56975. aKeyId = NULL;
  56976. i++;
  56977. /* Key Usage */
  56978. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  56979. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  56980. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  56981. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  56982. #if defined(WOLFSSL_QT)
  56983. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  56984. #else
  56985. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  56986. #endif
  56987. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  56988. if (data != NULL) {
  56989. #ifdef BIG_ENDIAN_ORDER
  56990. actual = data[1];
  56991. #else
  56992. actual = data[0];
  56993. #endif
  56994. }
  56995. ExpectIntEQ(actual, expected);
  56996. wolfSSL_ASN1_STRING_free(asn1str);
  56997. asn1str = NULL;
  56998. #if 1
  56999. i++;
  57000. /* Authority Info Access */
  57001. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  57002. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  57003. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  57004. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  57005. ext));
  57006. #if defined(WOLFSSL_QT)
  57007. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  57008. #else
  57009. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  57010. #endif
  57011. /* URI entry is an ACCESS_DESCRIPTION type */
  57012. #if defined(WOLFSSL_QT)
  57013. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  57014. #else
  57015. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  57016. #endif
  57017. ExpectNotNull(adObj = ad->method);
  57018. /* Make sure nid is OCSP */
  57019. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  57020. /* GENERAL_NAME stores URI as an ASN1_STRING */
  57021. ExpectNotNull(gn = ad->location);
  57022. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  57023. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  57024. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  57025. #if defined(WOLFSSL_QT)
  57026. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  57027. #else
  57028. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  57029. #endif
  57030. if (str != NULL) {
  57031. actual = strcmp(str, "http://127.0.0.1:22220");
  57032. }
  57033. ExpectIntEQ(actual, 0);
  57034. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  57035. aia = NULL;
  57036. #else
  57037. (void) aia; (void) ad; (void) adObj; (void) gn;
  57038. #endif
  57039. wolfSSL_X509_free(x509);
  57040. #endif
  57041. return EXPECT_RESULT();
  57042. }
  57043. static int test_wolfSSL_X509_get_extension_flags(void)
  57044. {
  57045. EXPECT_DECLS;
  57046. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  57047. XFILE f = XBADFILE;
  57048. X509* x509 = NULL;
  57049. unsigned int extFlags;
  57050. unsigned int keyUsageFlags;
  57051. unsigned int extKeyUsageFlags;
  57052. /* client-int-cert.pem has the following extension flags. */
  57053. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  57054. /* and the following key usage flags. */
  57055. keyUsageFlags = KU_DIGITAL_SIGNATURE
  57056. | KU_NON_REPUDIATION
  57057. | KU_KEY_ENCIPHERMENT;
  57058. /* and the following extended key usage flags. */
  57059. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  57060. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  57061. XBADFILE);
  57062. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  57063. if (f != XBADFILE) {
  57064. XFCLOSE(f);
  57065. f = XBADFILE;
  57066. }
  57067. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  57068. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  57069. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  57070. X509_free(x509);
  57071. x509 = NULL;
  57072. /* client-cert-ext.pem has the following extension flags. */
  57073. extFlags = EXFLAG_KUSAGE;
  57074. /* and the following key usage flags. */
  57075. keyUsageFlags = KU_DIGITAL_SIGNATURE
  57076. | KU_KEY_CERT_SIGN
  57077. | KU_CRL_SIGN;
  57078. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  57079. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  57080. if (f != XBADFILE)
  57081. XFCLOSE(f);
  57082. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  57083. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  57084. X509_free(x509);
  57085. #endif /* OPENSSL_ALL */
  57086. return EXPECT_RESULT();
  57087. }
  57088. static int test_wolfSSL_X509_get_ext(void)
  57089. {
  57090. EXPECT_DECLS;
  57091. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  57092. int ret = 0;
  57093. XFILE f = XBADFILE;
  57094. WOLFSSL_X509* x509 = NULL;
  57095. WOLFSSL_X509_EXTENSION* foundExtension;
  57096. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  57097. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  57098. if (f != XBADFILE)
  57099. XFCLOSE(f);
  57100. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  57101. /* wolfSSL_X509_get_ext() valid input */
  57102. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  57103. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  57104. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  57105. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  57106. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  57107. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  57108. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  57109. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  57110. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  57111. wolfSSL_X509_free(x509);
  57112. #endif
  57113. return EXPECT_RESULT();
  57114. }
  57115. static int test_wolfSSL_X509_get_ext_by_NID(void)
  57116. {
  57117. EXPECT_DECLS;
  57118. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  57119. int rc = 0;
  57120. XFILE f = XBADFILE;
  57121. WOLFSSL_X509* x509 = NULL;
  57122. ASN1_OBJECT* obj = NULL;
  57123. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  57124. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  57125. if (f != XBADFILE)
  57126. XFCLOSE(f);
  57127. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  57128. -1), 0);
  57129. /* Start search from last location (should fail) */
  57130. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  57131. rc), -1);
  57132. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  57133. -2), -1);
  57134. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  57135. -1), -1);
  57136. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  57137. /* NID_ext_key_usage, check also its nid and oid */
  57138. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  57139. -1);
  57140. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  57141. x509, rc)));
  57142. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  57143. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  57144. wolfSSL_X509_free(x509);
  57145. #endif
  57146. return EXPECT_RESULT();
  57147. }
  57148. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  57149. {
  57150. EXPECT_DECLS;
  57151. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  57152. int rc = 0;
  57153. XFILE f = XBADFILE;
  57154. WOLFSSL_X509* x509 = NULL;
  57155. WOLFSSL_X509_EXTENSION* ext = NULL;
  57156. WOLFSSL_ASN1_STRING* sanString = NULL;
  57157. byte* sanDer = NULL;
  57158. const byte expectedDer[] = {
  57159. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  57160. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  57161. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  57162. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  57163. if (f != XBADFILE)
  57164. XFCLOSE(f);
  57165. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  57166. ExpectNotNull(ext = X509_get_ext(x509, rc));
  57167. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  57168. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  57169. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  57170. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  57171. X509_free(x509);
  57172. #endif
  57173. return EXPECT_RESULT();
  57174. }
  57175. static int test_wolfSSL_X509_EXTENSION_new(void)
  57176. {
  57177. EXPECT_DECLS;
  57178. #if defined (OPENSSL_ALL)
  57179. WOLFSSL_X509_EXTENSION* ext = NULL;
  57180. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  57181. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  57182. wolfSSL_X509_EXTENSION_free(ext);
  57183. #endif
  57184. return EXPECT_RESULT();
  57185. }
  57186. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  57187. {
  57188. EXPECT_DECLS;
  57189. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  57190. WOLFSSL_X509* x509 = NULL;
  57191. WOLFSSL_X509_EXTENSION* ext = NULL;
  57192. WOLFSSL_ASN1_OBJECT* o = NULL;
  57193. XFILE file = XBADFILE;
  57194. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  57195. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  57196. if (file != XBADFILE)
  57197. XFCLOSE(file);
  57198. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  57199. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  57200. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  57201. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  57202. ExpectIntEQ(o->nid, 128);
  57203. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  57204. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  57205. wolfSSL_X509_free(x509);
  57206. #endif
  57207. return EXPECT_RESULT();
  57208. }
  57209. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  57210. {
  57211. EXPECT_DECLS;
  57212. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  57213. WOLFSSL_X509* x509 = NULL;
  57214. WOLFSSL_X509_EXTENSION* ext = NULL;
  57215. WOLFSSL_ASN1_STRING* str = NULL;
  57216. XFILE file = XBADFILE;
  57217. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  57218. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  57219. if (file != XBADFILE)
  57220. XFCLOSE(file);
  57221. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  57222. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  57223. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  57224. wolfSSL_X509_free(x509);
  57225. #endif
  57226. return EXPECT_RESULT();
  57227. }
  57228. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  57229. {
  57230. EXPECT_DECLS;
  57231. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  57232. WOLFSSL_X509* x509 = NULL;
  57233. WOLFSSL_X509_EXTENSION* ext = NULL;
  57234. XFILE file = XBADFILE;
  57235. int crit = 0;
  57236. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  57237. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  57238. if (file != XBADFILE)
  57239. XFCLOSE(file);
  57240. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  57241. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  57242. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  57243. wolfSSL_X509_free(x509);
  57244. #endif
  57245. return EXPECT_RESULT();
  57246. }
  57247. static int test_wolfSSL_X509V3_EXT_print(void)
  57248. {
  57249. EXPECT_DECLS;
  57250. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  57251. !defined(NO_RSA)
  57252. {
  57253. XFILE f = XBADFILE;
  57254. WOLFSSL_X509* x509 = NULL;
  57255. X509_EXTENSION * ext = NULL;
  57256. int loc = 0;
  57257. BIO *bio = NULL;
  57258. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  57259. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  57260. if (f != XBADFILE)
  57261. fclose(f);
  57262. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  57263. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  57264. NID_basic_constraints, -1), -1);
  57265. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  57266. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  57267. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  57268. NID_subject_key_identifier, -1), -1);
  57269. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  57270. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  57271. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  57272. NID_authority_key_identifier, -1), -1);
  57273. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  57274. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  57275. wolfSSL_BIO_free(bio);
  57276. wolfSSL_X509_free(x509);
  57277. }
  57278. {
  57279. X509 *x509 = NULL;
  57280. BIO *bio = NULL;
  57281. X509_EXTENSION *ext = NULL;
  57282. unsigned int i = 0;
  57283. unsigned int idx = 0;
  57284. /* Some NIDs to test with */
  57285. int nids[] = {
  57286. /* NID_key_usage, currently X509_get_ext returns this as a bit
  57287. * string, which messes up X509V3_EXT_print */
  57288. /* NID_ext_key_usage, */
  57289. NID_subject_alt_name,
  57290. };
  57291. int* n = NULL;
  57292. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  57293. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  57294. WOLFSSL_FILETYPE_PEM));
  57295. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  57296. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  57297. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  57298. * update the index */
  57299. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  57300. ExpectNotNull(ext = X509_get_ext(x509, (int)idx));
  57301. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  57302. ExpectIntGT(fprintf(stderr, "\n"), 0);
  57303. }
  57304. BIO_free(bio);
  57305. X509_free(x509);
  57306. }
  57307. #endif
  57308. return EXPECT_RESULT();
  57309. }
  57310. static int test_wolfSSL_X509_cmp(void)
  57311. {
  57312. EXPECT_DECLS;
  57313. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  57314. XFILE file1 = XBADFILE;
  57315. XFILE file2 = XBADFILE;
  57316. WOLFSSL_X509* cert1 = NULL;
  57317. WOLFSSL_X509* cert2 = NULL;
  57318. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  57319. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  57320. XBADFILE);
  57321. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  57322. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  57323. if (file1 != XBADFILE)
  57324. fclose(file1);
  57325. if (file2 != XBADFILE)
  57326. fclose(file2);
  57327. /* wolfSSL_X509_cmp() testing matching certs */
  57328. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  57329. /* wolfSSL_X509_cmp() testing mismatched certs */
  57330. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  57331. /* wolfSSL_X509_cmp() testing NULL, valid args */
  57332. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  57333. /* wolfSSL_X509_cmp() testing valid, NULL args */
  57334. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  57335. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  57336. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  57337. wolfSSL_X509_free(cert1);
  57338. wolfSSL_X509_free(cert2);
  57339. #endif
  57340. return EXPECT_RESULT();
  57341. }
  57342. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  57343. {
  57344. EXPECT_DECLS;
  57345. #if defined(OPENSSL_ALL)
  57346. EVP_PKEY* pkey;
  57347. pkey = EVP_PKEY_new();
  57348. ExpectNotNull(pkey);
  57349. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  57350. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  57351. EVP_PKEY_free(pkey);
  57352. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  57353. EVP_PKEY_free(pkey);
  57354. EVP_PKEY_free(pkey);
  57355. #endif
  57356. return EXPECT_RESULT();
  57357. }
  57358. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  57359. {
  57360. EXPECT_DECLS;
  57361. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  57362. EVP_PKEY* pkey = NULL;
  57363. const unsigned char* p;
  57364. unsigned char *der = NULL;
  57365. unsigned char *tmp = NULL;
  57366. int derLen;
  57367. p = client_keypub_der_2048;
  57368. /* Check that key can be successfully decoded. */
  57369. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  57370. sizeof_client_keypub_der_2048));
  57371. /* Check that key can be successfully encoded. */
  57372. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  57373. /* Ensure that the encoded version matches the original. */
  57374. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  57375. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  57376. /* Do same test except with pre-allocated buffer to ensure the der pointer
  57377. * is advanced. */
  57378. tmp = der;
  57379. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  57380. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  57381. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  57382. ExpectTrue(der + derLen == tmp);
  57383. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  57384. EVP_PKEY_free(pkey);
  57385. #endif
  57386. return EXPECT_RESULT();
  57387. }
  57388. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  57389. {
  57390. EXPECT_DECLS;
  57391. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  57392. !defined(NO_ASN) && !defined(NO_PWDBASED)
  57393. EVP_PKEY* pkey = NULL;
  57394. const unsigned char* p;
  57395. unsigned char *der = NULL;
  57396. unsigned char *tmp = NULL;
  57397. int derLen;
  57398. unsigned char pub_buf[65];
  57399. const int pub_len = 65;
  57400. BN_CTX* ctx;
  57401. EC_GROUP* curve = NULL;
  57402. EC_KEY* ephemeral_key = NULL;
  57403. const EC_POINT* h;
  57404. /* Generate an x963 key pair and get public part into pub_buf */
  57405. ExpectNotNull(ctx = BN_CTX_new());
  57406. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  57407. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  57408. NID_X9_62_prime256v1));
  57409. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  57410. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  57411. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  57412. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  57413. /* Prepare the EVP_PKEY */
  57414. ExpectNotNull(pkey = EVP_PKEY_new());
  57415. p = pub_buf;
  57416. /* Check that key can be successfully decoded. */
  57417. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  57418. pub_len));
  57419. /* Check that key can be successfully encoded. */
  57420. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  57421. /* Ensure that the encoded version matches the original. */
  57422. ExpectIntEQ(derLen, pub_len);
  57423. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  57424. /* Do same test except with pre-allocated buffer to ensure the der pointer
  57425. * is advanced. */
  57426. tmp = der;
  57427. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  57428. ExpectIntEQ(derLen, pub_len);
  57429. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  57430. ExpectTrue(der + derLen == tmp);
  57431. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  57432. EVP_PKEY_free(pkey);
  57433. EC_KEY_free(ephemeral_key);
  57434. EC_GROUP_free(curve);
  57435. #endif
  57436. return EXPECT_RESULT();
  57437. }
  57438. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  57439. {
  57440. EXPECT_DECLS;
  57441. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  57442. DSA* dsa = NULL;
  57443. byte derIn[] = {
  57444. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  57445. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  57446. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  57447. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  57448. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  57449. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  57450. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  57451. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  57452. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  57453. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  57454. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  57455. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  57456. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  57457. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  57458. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  57459. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  57460. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  57461. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  57462. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  57463. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  57464. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  57465. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  57466. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  57467. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  57468. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  57469. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  57470. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  57471. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  57472. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  57473. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  57474. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  57475. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  57476. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  57477. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  57478. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  57479. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  57480. 0x1e, 0xde, 0xc4
  57481. };
  57482. int derInLen = sizeof(derIn);
  57483. byte* derOut = NULL;
  57484. int derOutLen;
  57485. byte* p = derIn;
  57486. /* Check that params can be successfully decoded. */
  57487. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  57488. /* Check that params can be successfully encoded. */
  57489. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  57490. /* Ensure that the encoded version matches the original. */
  57491. ExpectIntEQ(derInLen, derOutLen);
  57492. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  57493. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  57494. DSA_free(dsa);
  57495. #endif
  57496. return EXPECT_RESULT();
  57497. }
  57498. static int test_wolfSSL_i2d_PrivateKey(void)
  57499. {
  57500. EXPECT_DECLS;
  57501. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  57502. !defined(NO_ASN) && !defined(NO_PWDBASED)
  57503. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  57504. {
  57505. EVP_PKEY* pkey = NULL;
  57506. const unsigned char* server_key =
  57507. (const unsigned char*)server_key_der_2048;
  57508. unsigned char buf[FOURK_BUF];
  57509. unsigned char* pt = NULL;
  57510. int bufSz = 0;
  57511. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  57512. (long)sizeof_server_key_der_2048));
  57513. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  57514. pt = buf;
  57515. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  57516. ExpectIntNE((pt - buf), 0);
  57517. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  57518. EVP_PKEY_free(pkey);
  57519. }
  57520. #endif
  57521. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  57522. {
  57523. EVP_PKEY* pkey = NULL;
  57524. const unsigned char* client_key =
  57525. (const unsigned char*)ecc_clikey_der_256;
  57526. unsigned char buf[FOURK_BUF];
  57527. unsigned char* pt = NULL;
  57528. int bufSz = 0;
  57529. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  57530. (long)sizeof_ecc_clikey_der_256)));
  57531. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  57532. pt = buf;
  57533. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  57534. ExpectIntNE((pt - buf), 0);
  57535. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  57536. EVP_PKEY_free(pkey);
  57537. }
  57538. #endif
  57539. #endif
  57540. return EXPECT_RESULT();
  57541. }
  57542. static int test_wolfSSL_OCSP_id_get0_info(void)
  57543. {
  57544. EXPECT_DECLS;
  57545. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  57546. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  57547. X509* cert = NULL;
  57548. X509* issuer = NULL;
  57549. OCSP_CERTID* id = NULL;
  57550. OCSP_CERTID* id2 = NULL;
  57551. ASN1_STRING* name = NULL;
  57552. ASN1_OBJECT* pmd = NULL;
  57553. ASN1_STRING* keyHash = NULL;
  57554. ASN1_INTEGER* serial = NULL;
  57555. ASN1_INTEGER* x509Int = NULL;
  57556. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  57557. SSL_FILETYPE_PEM));
  57558. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  57559. SSL_FILETYPE_PEM));
  57560. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  57561. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  57562. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  57563. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  57564. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  57565. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  57566. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  57567. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  57568. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  57569. ExpectNotNull(serial);
  57570. /* compare serial number to one in cert, should be equal */
  57571. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  57572. ExpectIntEQ(x509Int->length, serial->length);
  57573. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  57574. /* test OCSP_id_cmp */
  57575. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  57576. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  57577. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  57578. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  57579. if (id != NULL) {
  57580. id->issuerHash[0] = ~id->issuerHash[0];
  57581. }
  57582. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  57583. OCSP_CERTID_free(id);
  57584. OCSP_CERTID_free(id2);
  57585. X509_free(cert); /* free's x509Int */
  57586. X509_free(issuer);
  57587. #endif
  57588. return EXPECT_RESULT();
  57589. }
  57590. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  57591. {
  57592. EXPECT_DECLS;
  57593. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  57594. WOLFSSL_OCSP_CERTID certId;
  57595. byte* targetBuffer = NULL;
  57596. byte* p;
  57597. /* OCSP CertID bytes taken from PCAP */
  57598. byte rawCertId[] = {
  57599. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  57600. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  57601. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  57602. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  57603. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  57604. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  57605. 0xcf, 0xbc, 0x91
  57606. };
  57607. int ret = 0;
  57608. int i;
  57609. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  57610. certId.rawCertId = rawCertId;
  57611. certId.rawCertIdSize = sizeof(rawCertId);
  57612. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  57613. DYNAMIC_TYPE_TMP_BUFFER));
  57614. p = targetBuffer;
  57615. /* Function returns the size of the encoded data. */
  57616. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  57617. /* If target buffer is not null, function increments targetBuffer to point
  57618. * just past the end of the encoded data. */
  57619. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  57620. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  57621. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  57622. }
  57623. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  57624. targetBuffer = NULL;
  57625. /* If target buffer is null, function allocates memory for a buffer and
  57626. * copies the encoded data into it. targetBuffer then points to the start of
  57627. * this newly allocate buffer. */
  57628. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  57629. sizeof(rawCertId));
  57630. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  57631. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  57632. }
  57633. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  57634. #endif
  57635. return EXPECT_RESULT();
  57636. }
  57637. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  57638. {
  57639. EXPECT_DECLS;
  57640. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  57641. WOLFSSL_OCSP_CERTID* certId;
  57642. WOLFSSL_OCSP_CERTID* certIdGood;
  57643. WOLFSSL_OCSP_CERTID* certIdBad;
  57644. const unsigned char* rawCertIdPtr;
  57645. const unsigned char rawCertId[] = {
  57646. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  57647. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  57648. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  57649. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  57650. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  57651. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  57652. 0xcf, 0xbc, 0x91
  57653. };
  57654. rawCertIdPtr = &rawCertId[0];
  57655. /* If the cert ID is NULL the function should allocate it and copy the
  57656. * data to it. */
  57657. certId = NULL;
  57658. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  57659. sizeof(rawCertId)));
  57660. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  57661. if (certId != NULL) {
  57662. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  57663. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  57664. }
  57665. /* If the cert ID is not NULL the function will just copy the data to it. */
  57666. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  57667. DYNAMIC_TYPE_TMP_BUFFER));
  57668. ExpectNotNull(certId);
  57669. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  57670. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  57671. rawCertIdPtr = &rawCertId[0];
  57672. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  57673. sizeof(rawCertId)));
  57674. ExpectPtrEq(certIdGood, certId);
  57675. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  57676. if (certId != NULL) {
  57677. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  57678. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  57679. certId = NULL;
  57680. }
  57681. /* The below tests should fail when passed bad parameters. NULL should
  57682. * always be returned. */
  57683. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  57684. sizeof(rawCertId)));
  57685. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  57686. sizeof(rawCertId)));
  57687. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  57688. #endif
  57689. return EXPECT_RESULT();
  57690. }
  57691. static int test_wolfSSL_OCSP_id_cmp(void)
  57692. {
  57693. EXPECT_DECLS;
  57694. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  57695. OCSP_CERTID id1;
  57696. OCSP_CERTID id2;
  57697. XMEMSET(&id1, 0, sizeof(id1));
  57698. XMEMSET(&id2, 0, sizeof(id2));
  57699. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  57700. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  57701. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  57702. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  57703. #endif
  57704. return EXPECT_RESULT();
  57705. }
  57706. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  57707. {
  57708. EXPECT_DECLS;
  57709. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  57710. WOLFSSL_OCSP_SINGLERESP single;
  57711. const WOLFSSL_OCSP_CERTID* certId;
  57712. XMEMSET(&single, 0, sizeof(single));
  57713. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  57714. ExpectPtrEq(&single, certId);
  57715. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  57716. #endif
  57717. return EXPECT_RESULT();
  57718. }
  57719. static int test_wolfSSL_OCSP_single_get0_status(void)
  57720. {
  57721. EXPECT_DECLS;
  57722. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  57723. WOLFSSL_OCSP_SINGLERESP single;
  57724. CertStatus certStatus;
  57725. WOLFSSL_ASN1_TIME* thisDate;
  57726. WOLFSSL_ASN1_TIME* nextDate;
  57727. int ret, i;
  57728. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  57729. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  57730. /* Fill the date fields with some dummy data. */
  57731. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  57732. certStatus.thisDateParsed.data[i] = i;
  57733. certStatus.nextDateParsed.data[i] = i;
  57734. }
  57735. certStatus.status = CERT_GOOD;
  57736. single.status = &certStatus;
  57737. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  57738. &nextDate);
  57739. ExpectIntEQ(ret, CERT_GOOD);
  57740. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  57741. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  57742. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  57743. CERT_GOOD);
  57744. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  57745. NULL), CERT_GOOD);
  57746. #endif
  57747. return EXPECT_RESULT();
  57748. }
  57749. static int test_wolfSSL_OCSP_resp_count(void)
  57750. {
  57751. EXPECT_DECLS;
  57752. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  57753. WOLFSSL_OCSP_BASICRESP basicResp;
  57754. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  57755. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  57756. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  57757. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  57758. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  57759. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  57760. basicResp.single = &singleRespOne;
  57761. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  57762. singleRespOne.next = &singleRespTwo;
  57763. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  57764. #endif
  57765. return EXPECT_RESULT();
  57766. }
  57767. static int test_wolfSSL_OCSP_resp_get0(void)
  57768. {
  57769. EXPECT_DECLS;
  57770. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  57771. WOLFSSL_OCSP_BASICRESP basicResp;
  57772. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  57773. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  57774. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  57775. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  57776. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  57777. basicResp.single = &singleRespOne;
  57778. singleRespOne.next = &singleRespTwo;
  57779. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  57780. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  57781. #endif
  57782. return EXPECT_RESULT();
  57783. }
  57784. static int test_wolfSSL_EVP_PKEY_derive(void)
  57785. {
  57786. EXPECT_DECLS;
  57787. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  57788. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  57789. EVP_PKEY_CTX *ctx = NULL;
  57790. unsigned char *skey = NULL;
  57791. size_t skeylen;
  57792. EVP_PKEY *pkey = NULL;
  57793. EVP_PKEY *peerkey = NULL;
  57794. const unsigned char* key;
  57795. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  57796. /* DH */
  57797. key = dh_key_der_2048;
  57798. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  57799. sizeof_dh_key_der_2048)));
  57800. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  57801. key = dh_key_der_2048;
  57802. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  57803. sizeof_dh_key_der_2048)));
  57804. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  57805. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  57806. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  57807. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  57808. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  57809. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  57810. DYNAMIC_TYPE_OPENSSL));
  57811. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  57812. EVP_PKEY_CTX_free(ctx);
  57813. ctx = NULL;
  57814. EVP_PKEY_free(peerkey);
  57815. peerkey = NULL;
  57816. EVP_PKEY_free(pkey);
  57817. pkey = NULL;
  57818. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  57819. skey = NULL;
  57820. #endif
  57821. #ifdef HAVE_ECC
  57822. /* ECDH */
  57823. key = ecc_clikey_der_256;
  57824. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  57825. sizeof_ecc_clikey_der_256)));
  57826. key = ecc_clikeypub_der_256;
  57827. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  57828. sizeof_ecc_clikeypub_der_256)));
  57829. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  57830. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  57831. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  57832. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  57833. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  57834. DYNAMIC_TYPE_OPENSSL));
  57835. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  57836. EVP_PKEY_CTX_free(ctx);
  57837. EVP_PKEY_free(peerkey);
  57838. EVP_PKEY_free(pkey);
  57839. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  57840. #endif /* HAVE_ECC */
  57841. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  57842. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  57843. return EXPECT_RESULT();
  57844. }
  57845. static int test_wolfSSL_EVP_PBE_scrypt(void)
  57846. {
  57847. EXPECT_DECLS;
  57848. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  57849. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  57850. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  57851. int ret;
  57852. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  57853. int pwdlen = sizeof(pwd);
  57854. const byte salt[] = {'N','a','C','l'};
  57855. int saltlen = sizeof(salt);
  57856. byte key[80];
  57857. word64 numOvr32 = (word64)INT32_MAX + 1;
  57858. /* expected derived key for N:16, r:1, p:1 */
  57859. const byte expectedKey[] = {
  57860. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  57861. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  57862. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  57863. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  57864. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  57865. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  57866. 0xE7, 0xE9, 0xC0, 0x9A};
  57867. /* N r p mx key keylen */
  57868. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  57869. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  57870. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  57871. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  57872. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  57873. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  57874. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  57875. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  57876. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  57877. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  57878. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  57879. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  57880. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  57881. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  57882. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  57883. ExpectIntEQ(ret, 1); /* should succeed */
  57884. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  57885. key, 64);
  57886. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  57887. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  57888. key, 64);
  57889. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  57890. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  57891. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  57892. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  57893. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  57894. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  57895. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  57896. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  57897. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  57898. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  57899. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  57900. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  57901. ExpectIntEQ(ret, 1);
  57902. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  57903. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  57904. #endif /* !NO_PWDBASED && !NO_SHA256 */
  57905. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  57906. return EXPECT_RESULT();
  57907. }
  57908. static int test_no_op_functions(void)
  57909. {
  57910. EXPECT_DECLS;
  57911. #if defined(OPENSSL_EXTRA)
  57912. /* this makes sure wolfSSL can compile and run these no-op functions */
  57913. SSL_load_error_strings();
  57914. ENGINE_load_builtin_engines();
  57915. OpenSSL_add_all_ciphers();
  57916. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  57917. #endif
  57918. return EXPECT_RESULT();
  57919. }
  57920. static int test_wolfSSL_CRYPTO_memcmp(void)
  57921. {
  57922. EXPECT_DECLS;
  57923. #ifdef OPENSSL_EXTRA
  57924. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  57925. "implementation of TLS/SSL for embedded devices to the cloud.";
  57926. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  57927. "implementation of TLS/SSL for embedded devices to the cloud.";
  57928. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  57929. "implementation of TLS/SSL for embedded devices to the cloud!";
  57930. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  57931. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  57932. #endif
  57933. return EXPECT_RESULT();
  57934. }
  57935. /*----------------------------------------------------------------------------*
  57936. | wolfCrypt ASN
  57937. *----------------------------------------------------------------------------*/
  57938. static int test_wc_CreateEncryptedPKCS8Key(void)
  57939. {
  57940. EXPECT_DECLS;
  57941. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  57942. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  57943. WC_RNG rng;
  57944. byte* encKey = NULL;
  57945. word32 encKeySz = 0;
  57946. word32 decKeySz = 0;
  57947. const char password[] = "Lorem ipsum dolor sit amet";
  57948. word32 passwordSz = (word32)XSTRLEN(password);
  57949. word32 tradIdx = 0;
  57950. XMEMSET(&rng, 0, sizeof(WC_RNG));
  57951. ExpectIntEQ(wc_InitRng(&rng), 0);
  57952. PRIVATE_KEY_UNLOCK();
  57953. /* Call with NULL for out buffer to get necessary length. */
  57954. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  57955. sizeof_server_key_der_2048, NULL, &encKeySz, password, (int)passwordSz,
  57956. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  57957. LENGTH_ONLY_E);
  57958. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  57959. DYNAMIC_TYPE_TMP_BUFFER));
  57960. /* Call with the allocated out buffer. */
  57961. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  57962. sizeof_server_key_der_2048, encKey, &encKeySz, password, (int)passwordSz,
  57963. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  57964. 0);
  57965. /* Decrypt the encrypted PKCS8 key we just made. */
  57966. ExpectIntGT((decKeySz = (word32)wc_DecryptPKCS8Key(encKey, encKeySz, password,
  57967. (int)passwordSz)), 0);
  57968. /* encKey now holds the decrypted key (decrypted in place). */
  57969. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  57970. /* Check that the decrypted key matches the key prior to encryption. */
  57971. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  57972. sizeof_server_key_der_2048), 0);
  57973. PRIVATE_KEY_LOCK();
  57974. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  57975. wc_FreeRng(&rng);
  57976. #endif
  57977. return EXPECT_RESULT();
  57978. }
  57979. static int test_wc_GetPkcs8TraditionalOffset(void)
  57980. {
  57981. EXPECT_DECLS;
  57982. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  57983. int length;
  57984. int derSz = 0;
  57985. word32 inOutIdx;
  57986. const char* path = "./certs/server-keyPkcs8.der";
  57987. XFILE file = XBADFILE;
  57988. byte der[2048];
  57989. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  57990. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  57991. if (file != XBADFILE)
  57992. XFCLOSE(file);
  57993. /* valid case */
  57994. inOutIdx = 0;
  57995. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  57996. 0);
  57997. /* inOutIdx > sz */
  57998. inOutIdx = 4000;
  57999. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  58000. BAD_FUNC_ARG);
  58001. /* null input */
  58002. inOutIdx = 0;
  58003. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  58004. BAD_FUNC_ARG);
  58005. /* invalid input, fill buffer with 1's */
  58006. XMEMSET(der, 1, sizeof(der));
  58007. inOutIdx = 0;
  58008. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  58009. ASN_PARSE_E);
  58010. #endif /* NO_ASN */
  58011. return EXPECT_RESULT();
  58012. }
  58013. static int test_wc_SetSubjectRaw(void)
  58014. {
  58015. EXPECT_DECLS;
  58016. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  58017. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  58018. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  58019. WOLFSSL_X509* x509 = NULL;
  58020. int peerCertSz;
  58021. const byte* peerCertBuf = NULL;
  58022. Cert forgedCert;
  58023. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  58024. WOLFSSL_FILETYPE_ASN1));
  58025. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  58026. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  58027. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  58028. wolfSSL_FreeX509(x509);
  58029. #endif
  58030. return EXPECT_RESULT();
  58031. }
  58032. static int test_wc_GetSubjectRaw(void)
  58033. {
  58034. EXPECT_DECLS;
  58035. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  58036. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  58037. Cert cert;
  58038. byte *subjectRaw;
  58039. ExpectIntEQ(0, wc_InitCert(&cert));
  58040. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  58041. #endif
  58042. return EXPECT_RESULT();
  58043. }
  58044. static int test_wc_SetIssuerRaw(void)
  58045. {
  58046. EXPECT_DECLS;
  58047. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  58048. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  58049. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  58050. WOLFSSL_X509* x509 = NULL;
  58051. int peerCertSz;
  58052. const byte* peerCertBuf;
  58053. Cert forgedCert;
  58054. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  58055. WOLFSSL_FILETYPE_ASN1));
  58056. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  58057. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  58058. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  58059. wolfSSL_FreeX509(x509);
  58060. #endif
  58061. return EXPECT_RESULT();
  58062. }
  58063. static int test_wc_SetIssueBuffer(void)
  58064. {
  58065. EXPECT_DECLS;
  58066. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  58067. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  58068. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  58069. WOLFSSL_X509* x509 = NULL;
  58070. int peerCertSz;
  58071. const byte* peerCertBuf;
  58072. Cert forgedCert;
  58073. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  58074. WOLFSSL_FILETYPE_ASN1));
  58075. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  58076. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  58077. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  58078. wolfSSL_FreeX509(x509);
  58079. #endif
  58080. return EXPECT_RESULT();
  58081. }
  58082. /*
  58083. * Testing wc_SetSubjectKeyId
  58084. */
  58085. static int test_wc_SetSubjectKeyId(void)
  58086. {
  58087. EXPECT_DECLS;
  58088. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  58089. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  58090. Cert cert;
  58091. const char* file = "certs/ecc-client-keyPub.pem";
  58092. ExpectIntEQ(0, wc_InitCert(&cert));
  58093. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  58094. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  58095. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  58096. #endif
  58097. return EXPECT_RESULT();
  58098. } /* END test_wc_SetSubjectKeyId */
  58099. /*
  58100. * Testing wc_SetSubject
  58101. */
  58102. static int test_wc_SetSubject(void)
  58103. {
  58104. EXPECT_DECLS;
  58105. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  58106. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  58107. Cert cert;
  58108. const char* file = "./certs/ca-ecc-cert.pem";
  58109. ExpectIntEQ(0, wc_InitCert(&cert));
  58110. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  58111. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  58112. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  58113. #endif
  58114. return EXPECT_RESULT();
  58115. } /* END test_wc_SetSubject */
  58116. static int test_CheckCertSignature(void)
  58117. {
  58118. EXPECT_DECLS;
  58119. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  58120. WOLFSSL_CERT_MANAGER* cm = NULL;
  58121. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  58122. XFILE fp = XBADFILE;
  58123. byte cert[4096];
  58124. int certSz;
  58125. #endif
  58126. ExpectIntEQ(BAD_FUNC_ARG, wc_CheckCertSignature(NULL, 0, NULL, NULL));
  58127. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  58128. ExpectIntEQ(BAD_FUNC_ARG, wc_CheckCertSignature(NULL, 0, NULL, cm));
  58129. #ifndef NO_RSA
  58130. #ifdef USE_CERT_BUFFERS_1024
  58131. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(server_cert_der_1024,
  58132. sizeof_server_cert_der_1024, NULL, cm));
  58133. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  58134. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  58135. WOLFSSL_FILETYPE_ASN1));
  58136. ExpectIntEQ(0, wc_CheckCertSignature(server_cert_der_1024,
  58137. sizeof_server_cert_der_1024, NULL, cm));
  58138. #elif defined(USE_CERT_BUFFERS_2048)
  58139. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(server_cert_der_2048,
  58140. sizeof_server_cert_der_2048, NULL, cm));
  58141. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  58142. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  58143. WOLFSSL_FILETYPE_ASN1));
  58144. ExpectIntEQ(0, wc_CheckCertSignature(server_cert_der_2048,
  58145. sizeof_server_cert_der_2048, NULL, cm));
  58146. #endif
  58147. #endif
  58148. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  58149. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(serv_ecc_der_256,
  58150. sizeof_serv_ecc_der_256, NULL, cm));
  58151. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  58152. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  58153. WOLFSSL_FILETYPE_ASN1));
  58154. ExpectIntEQ(0, wc_CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  58155. NULL, cm));
  58156. #endif
  58157. #if !defined(NO_FILESYSTEM)
  58158. wolfSSL_CertManagerFree(cm);
  58159. cm = NULL;
  58160. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  58161. #ifndef NO_RSA
  58162. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  58163. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  58164. if (fp != XBADFILE) {
  58165. XFCLOSE(fp);
  58166. fp = XBADFILE;
  58167. }
  58168. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(cert, certSz, NULL, cm));
  58169. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  58170. "./certs/ca-cert.pem", NULL));
  58171. ExpectIntEQ(0, wc_CheckCertSignature(cert, certSz, NULL, cm));
  58172. #endif
  58173. #ifdef HAVE_ECC
  58174. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  58175. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  58176. if (fp != XBADFILE) {
  58177. XFCLOSE(fp);
  58178. fp = XBADFILE;
  58179. }
  58180. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(cert, certSz, NULL, cm));
  58181. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  58182. "./certs/ca-ecc-cert.pem", NULL));
  58183. ExpectIntEQ(0, wc_CheckCertSignature(cert, certSz, NULL, cm));
  58184. #endif
  58185. #endif
  58186. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  58187. (void)fp;
  58188. (void)cert;
  58189. (void)certSz;
  58190. #endif
  58191. wolfSSL_CertManagerFree(cm);
  58192. #endif
  58193. return EXPECT_RESULT();
  58194. }
  58195. static int test_wc_ParseCert(void)
  58196. {
  58197. EXPECT_DECLS;
  58198. #if !defined(NO_CERTS) && !defined(NO_RSA)
  58199. DecodedCert decodedCert;
  58200. const byte* rawCert = client_cert_der_2048;
  58201. const int rawCertSize = sizeof_client_cert_der_2048;
  58202. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  58203. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  58204. #ifndef IGNORE_NAME_CONSTRAINTS
  58205. /* check that the subjects emailAddress was not put in the alt name list */
  58206. ExpectNotNull(decodedCert.subjectEmail);
  58207. ExpectNull(decodedCert.altEmailNames);
  58208. #endif
  58209. wc_FreeDecodedCert(&decodedCert);
  58210. #endif
  58211. return EXPECT_RESULT();
  58212. }
  58213. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  58214. * the API safely errors out on badly-formed ASN input.
  58215. * NOTE: Test not compatible with released FIPS implementations!
  58216. */
  58217. static int test_wc_ParseCert_Error(void)
  58218. {
  58219. EXPECT_DECLS;
  58220. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  58221. (!defined(HAVE_FIPS) || \
  58222. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  58223. DecodedCert decodedCert;
  58224. int i;
  58225. /* Certificate data */
  58226. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  58227. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  58228. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  58229. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  58230. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  58231. /* Test data */
  58232. const struct testStruct {
  58233. const byte* c;
  58234. const int cSz;
  58235. const int expRet;
  58236. } t[] = {
  58237. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  58238. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  58239. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  58240. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  58241. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  58242. };
  58243. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  58244. for (i = 0; i < tSz; i++) {
  58245. WOLFSSL_MSG_EX("i == %d", i);
  58246. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  58247. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  58248. wc_FreeDecodedCert(&decodedCert);
  58249. }
  58250. #endif
  58251. return EXPECT_RESULT();
  58252. }
  58253. static int test_MakeCertWithPathLen(void)
  58254. {
  58255. EXPECT_DECLS;
  58256. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  58257. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  58258. const byte expectedPathLen = 7;
  58259. Cert cert;
  58260. DecodedCert decodedCert;
  58261. byte der[FOURK_BUF];
  58262. int derSize = 0;
  58263. WC_RNG rng;
  58264. ecc_key key;
  58265. int ret;
  58266. XMEMSET(&rng, 0, sizeof(WC_RNG));
  58267. XMEMSET(&key, 0, sizeof(ecc_key));
  58268. XMEMSET(&cert, 0, sizeof(Cert));
  58269. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  58270. ExpectIntEQ(wc_InitRng(&rng), 0);
  58271. ExpectIntEQ(wc_ecc_init(&key), 0);
  58272. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  58273. ExpectIntEQ(wc_InitCert(&cert), 0);
  58274. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  58275. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  58276. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  58277. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  58278. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  58279. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  58280. CTC_NAME_SIZE);
  58281. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  58282. CTC_NAME_SIZE);
  58283. cert.selfSigned = 1;
  58284. cert.isCA = 1;
  58285. cert.pathLen = expectedPathLen;
  58286. cert.pathLenSet = 1;
  58287. cert.sigType = CTC_SHA256wECDSA;
  58288. #ifdef WOLFSSL_CERT_EXT
  58289. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  58290. #endif
  58291. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  58292. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  58293. FOURK_BUF, NULL, &key, &rng), 0);
  58294. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  58295. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  58296. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  58297. wc_FreeDecodedCert(&decodedCert);
  58298. ret = wc_ecc_free(&key);
  58299. ExpectIntEQ(ret, 0);
  58300. ret = wc_FreeRng(&rng);
  58301. ExpectIntEQ(ret, 0);
  58302. #endif
  58303. return EXPECT_RESULT();
  58304. }
  58305. static int test_MakeCertWith0Ser(void)
  58306. {
  58307. EXPECT_DECLS;
  58308. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  58309. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC) && \
  58310. defined(WOLFSSL_ASN_TEMPLATE)
  58311. Cert cert;
  58312. DecodedCert decodedCert;
  58313. byte der[FOURK_BUF];
  58314. int derSize = 0;
  58315. WC_RNG rng;
  58316. ecc_key key;
  58317. int ret;
  58318. XMEMSET(&rng, 0, sizeof(WC_RNG));
  58319. XMEMSET(&key, 0, sizeof(ecc_key));
  58320. XMEMSET(&cert, 0, sizeof(Cert));
  58321. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  58322. ExpectIntEQ(wc_InitRng(&rng), 0);
  58323. ExpectIntEQ(wc_ecc_init(&key), 0);
  58324. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  58325. ExpectIntEQ(wc_InitCert(&cert), 0);
  58326. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  58327. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  58328. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  58329. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  58330. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  58331. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  58332. CTC_NAME_SIZE);
  58333. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  58334. CTC_NAME_SIZE);
  58335. cert.selfSigned = 1;
  58336. cert.isCA = 1;
  58337. cert.sigType = CTC_SHA256wECDSA;
  58338. #ifdef WOLFSSL_CERT_EXT
  58339. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  58340. #endif
  58341. /* set serial number to 0 */
  58342. cert.serialSz = 1;
  58343. cert.serial[0] = 0;
  58344. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  58345. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  58346. FOURK_BUF, NULL, &key, &rng), 0);
  58347. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  58348. #if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON)
  58349. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL),
  58350. ASN_PARSE_E);
  58351. #else
  58352. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  58353. #endif
  58354. wc_FreeDecodedCert(&decodedCert);
  58355. ret = wc_ecc_free(&key);
  58356. ExpectIntEQ(ret, 0);
  58357. ret = wc_FreeRng(&rng);
  58358. ExpectIntEQ(ret, 0);
  58359. #endif
  58360. return EXPECT_RESULT();
  58361. }
  58362. static int test_MakeCertWithCaFalse(void)
  58363. {
  58364. EXPECT_DECLS;
  58365. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  58366. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  58367. const byte expectedIsCa = 0;
  58368. Cert cert;
  58369. DecodedCert decodedCert;
  58370. byte der[FOURK_BUF];
  58371. int derSize = 0;
  58372. WC_RNG rng;
  58373. ecc_key key;
  58374. int ret;
  58375. XMEMSET(&rng, 0, sizeof(WC_RNG));
  58376. XMEMSET(&key, 0, sizeof(ecc_key));
  58377. XMEMSET(&cert, 0, sizeof(Cert));
  58378. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  58379. ExpectIntEQ(wc_InitRng(&rng), 0);
  58380. ExpectIntEQ(wc_ecc_init(&key), 0);
  58381. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  58382. ExpectIntEQ(wc_InitCert(&cert), 0);
  58383. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  58384. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  58385. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  58386. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  58387. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  58388. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  58389. CTC_NAME_SIZE);
  58390. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  58391. CTC_NAME_SIZE);
  58392. cert.selfSigned = 1;
  58393. cert.isCA = expectedIsCa;
  58394. cert.isCaSet = 1;
  58395. cert.sigType = CTC_SHA256wECDSA;
  58396. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  58397. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  58398. FOURK_BUF, NULL, &key, &rng), 0);
  58399. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  58400. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  58401. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  58402. wc_FreeDecodedCert(&decodedCert);
  58403. ret = wc_ecc_free(&key);
  58404. ExpectIntEQ(ret, 0);
  58405. ret = wc_FreeRng(&rng);
  58406. ExpectIntEQ(ret, 0);
  58407. #endif
  58408. return EXPECT_RESULT();
  58409. }
  58410. /*----------------------------------------------------------------------------*
  58411. | wolfCrypt ECC
  58412. *----------------------------------------------------------------------------*/
  58413. static int test_wc_ecc_get_curve_size_from_name(void)
  58414. {
  58415. EXPECT_DECLS;
  58416. #ifdef HAVE_ECC
  58417. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  58418. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  58419. #endif
  58420. /* invalid case */
  58421. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  58422. /* NULL input */
  58423. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  58424. #endif /* HAVE_ECC */
  58425. return EXPECT_RESULT();
  58426. }
  58427. static int test_wc_ecc_get_curve_id_from_name(void)
  58428. {
  58429. EXPECT_DECLS;
  58430. #ifdef HAVE_ECC
  58431. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  58432. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  58433. ECC_SECP256R1);
  58434. #endif
  58435. /* invalid case */
  58436. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  58437. /* NULL input */
  58438. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  58439. #endif /* HAVE_ECC */
  58440. return EXPECT_RESULT();
  58441. }
  58442. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  58443. !defined(HAVE_SELFTEST) && \
  58444. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  58445. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  58446. {
  58447. EXPECT_DECLS;
  58448. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  58449. ecc_key* key;
  58450. const ecc_set_type* params = NULL;
  58451. int ret;
  58452. #endif
  58453. WOLFSSL_EC_KEY *ecKey = NULL;
  58454. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  58455. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  58456. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  58457. if (EXPECT_SUCCESS()) {
  58458. ret = EC_KEY_generate_key(ecKey);
  58459. } else
  58460. ret = 0;
  58461. if (ret == 1) {
  58462. /* normal test */
  58463. key = (ecc_key*)ecKey->internal;
  58464. if (key != NULL) {
  58465. params = key->dp;
  58466. }
  58467. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  58468. ECC_SECP256R1);
  58469. }
  58470. #endif
  58471. /* invalid case, NULL input*/
  58472. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  58473. wolfSSL_EC_KEY_free(ecKey);
  58474. return EXPECT_RESULT();
  58475. }
  58476. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  58477. static int test_wc_ecc_get_curve_id_from_params(void)
  58478. {
  58479. EXPECT_DECLS;
  58480. #ifdef HAVE_ECC
  58481. const byte prime[] =
  58482. {
  58483. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  58484. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  58485. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  58486. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  58487. };
  58488. const byte primeInvalid[] =
  58489. {
  58490. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  58491. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  58492. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  58493. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  58494. };
  58495. const byte Af[] =
  58496. {
  58497. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  58498. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  58499. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  58500. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  58501. };
  58502. const byte Bf[] =
  58503. {
  58504. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  58505. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  58506. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  58507. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  58508. };
  58509. const byte order[] =
  58510. {
  58511. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  58512. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  58513. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  58514. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  58515. };
  58516. const byte Gx[] =
  58517. {
  58518. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  58519. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  58520. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  58521. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  58522. };
  58523. const byte Gy[] =
  58524. {
  58525. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  58526. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  58527. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  58528. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  58529. };
  58530. int cofactor = 1;
  58531. int fieldSize = 256;
  58532. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  58533. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  58534. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  58535. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  58536. ECC_SECP256R1);
  58537. #endif
  58538. /* invalid case, fieldSize = 0 */
  58539. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  58540. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  58541. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  58542. /* invalid case, NULL prime */
  58543. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  58544. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  58545. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  58546. /* invalid case, invalid prime */
  58547. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  58548. primeInvalid, sizeof(primeInvalid),
  58549. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  58550. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  58551. #endif
  58552. return EXPECT_RESULT();
  58553. }
  58554. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  58555. {
  58556. EXPECT_DECLS;
  58557. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  58558. WOLFSSL_RSA* rsa = NULL;
  58559. WOLFSSL_EVP_PKEY* pkey = NULL;
  58560. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  58561. const char* in = "What is easy to do is easy not to do.";
  58562. size_t inlen = XSTRLEN(in);
  58563. size_t outEncLen = 0;
  58564. byte* outEnc = NULL;
  58565. byte* outDec = NULL;
  58566. size_t outDecLen = 0;
  58567. size_t rsaKeySz = 2048/8; /* Bytes */
  58568. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  58569. byte* inTmp = NULL;
  58570. byte* outEncTmp = NULL;
  58571. byte* outDecTmp = NULL;
  58572. #endif
  58573. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  58574. DYNAMIC_TYPE_TMP_BUFFER));
  58575. if (outEnc != NULL) {
  58576. XMEMSET(outEnc, 0, rsaKeySz);
  58577. }
  58578. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  58579. DYNAMIC_TYPE_TMP_BUFFER));
  58580. if (outDec != NULL) {
  58581. XMEMSET(outDec, 0, rsaKeySz);
  58582. }
  58583. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  58584. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  58585. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  58586. if (EXPECT_FAIL()) {
  58587. RSA_free(rsa);
  58588. }
  58589. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  58590. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  58591. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  58592. WOLFSSL_SUCCESS);
  58593. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  58594. since ctx uses it.*/
  58595. ExpectIntEQ(pkey->ref.count, 2);
  58596. EVP_PKEY_free(pkey);
  58597. ExpectIntEQ(pkey->ref.count, 1);
  58598. /* Encrypt data */
  58599. /* Check that we can get the required output buffer length by passing in a
  58600. * NULL output buffer. */
  58601. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  58602. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  58603. ExpectIntEQ(rsaKeySz, outEncLen);
  58604. /* Now do the actual encryption. */
  58605. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  58606. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  58607. /* Decrypt data */
  58608. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  58609. /* Check that we can get the required output buffer length by passing in a
  58610. * NULL output buffer. */
  58611. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  58612. WOLFSSL_SUCCESS);
  58613. ExpectIntEQ(rsaKeySz, outDecLen);
  58614. /* Now do the actual decryption. */
  58615. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  58616. WOLFSSL_SUCCESS);
  58617. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  58618. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  58619. /* The input length must be the same size as the RSA key.*/
  58620. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  58621. DYNAMIC_TYPE_TMP_BUFFER));
  58622. if (inTmp != NULL) {
  58623. XMEMSET(inTmp, 9, rsaKeySz);
  58624. }
  58625. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  58626. DYNAMIC_TYPE_TMP_BUFFER));
  58627. if (outEncTmp != NULL) {
  58628. XMEMSET(outEncTmp, 0, rsaKeySz);
  58629. }
  58630. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  58631. DYNAMIC_TYPE_TMP_BUFFER));
  58632. if (outDecTmp != NULL) {
  58633. XMEMSET(outDecTmp, 0, rsaKeySz);
  58634. }
  58635. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  58636. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  58637. WOLFSSL_SUCCESS);
  58638. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  58639. WOLFSSL_SUCCESS);
  58640. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  58641. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  58642. outEncLen), WOLFSSL_SUCCESS);
  58643. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  58644. #endif
  58645. EVP_PKEY_CTX_free(ctx);
  58646. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  58647. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  58648. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  58649. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  58650. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  58651. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  58652. #endif
  58653. #endif
  58654. return EXPECT_RESULT();
  58655. }
  58656. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  58657. !defined(HAVE_SELFTEST)
  58658. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58659. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  58660. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  58661. #endif
  58662. #endif
  58663. #endif
  58664. #if defined(OPENSSL_EXTRA)
  58665. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  58666. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  58667. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  58668. #endif
  58669. #endif
  58670. #endif
  58671. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  58672. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58673. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  58674. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  58675. #endif
  58676. #endif
  58677. #endif
  58678. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  58679. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  58680. {
  58681. EXPECT_DECLS;
  58682. #if defined(OPENSSL_EXTRA)
  58683. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  58684. !defined(HAVE_SELFTEST)
  58685. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58686. WOLFSSL_RSA* rsa = NULL;
  58687. #endif
  58688. #endif
  58689. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  58690. WOLFSSL_DSA* dsa = NULL;
  58691. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  58692. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  58693. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58694. WOLFSSL_EC_KEY* ecKey = NULL;
  58695. #endif
  58696. #endif
  58697. WOLFSSL_EVP_PKEY* pkey = NULL;
  58698. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  58699. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  58700. const char* in = "What is easy to do is easy not to do.";
  58701. size_t inlen = XSTRLEN(in);
  58702. byte hash[SHA256_DIGEST_LENGTH] = {0};
  58703. byte zero[SHA256_DIGEST_LENGTH] = {0};
  58704. SHA256_CTX c;
  58705. byte* sig = NULL;
  58706. byte* sigVerify = NULL;
  58707. size_t siglen;
  58708. size_t siglenOnlyLen;
  58709. size_t keySz = 2048/8; /* Bytes */
  58710. ExpectNotNull(sig =
  58711. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  58712. ExpectNotNull(sigVerify =
  58713. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  58714. siglen = keySz;
  58715. ExpectNotNull(XMEMSET(sig, 0, keySz));
  58716. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  58717. /* Generate hash */
  58718. SHA256_Init(&c);
  58719. SHA256_Update(&c, in, inlen);
  58720. SHA256_Final(hash, &c);
  58721. #ifdef WOLFSSL_SMALL_STACK_CACHE
  58722. /* workaround for small stack cache case */
  58723. wc_Sha256Free((wc_Sha256*)&c);
  58724. #endif
  58725. /* Generate key */
  58726. ExpectNotNull(pkey = EVP_PKEY_new());
  58727. switch (keyType) {
  58728. case EVP_PKEY_RSA:
  58729. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  58730. !defined(HAVE_SELFTEST)
  58731. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58732. {
  58733. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  58734. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  58735. }
  58736. #endif
  58737. #endif
  58738. break;
  58739. case EVP_PKEY_DSA:
  58740. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  58741. ExpectNotNull(dsa = DSA_new());
  58742. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  58743. NULL, 0, NULL, NULL, NULL), 1);
  58744. ExpectIntEQ(DSA_generate_key(dsa), 1);
  58745. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  58746. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  58747. break;
  58748. case EVP_PKEY_EC:
  58749. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  58750. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58751. {
  58752. ExpectNotNull(ecKey = EC_KEY_new());
  58753. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  58754. ExpectIntEQ(
  58755. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  58756. if (EXPECT_FAIL()) {
  58757. EC_KEY_free(ecKey);
  58758. }
  58759. }
  58760. #endif
  58761. #endif
  58762. break;
  58763. }
  58764. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  58765. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  58766. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  58767. !defined(HAVE_SELFTEST)
  58768. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58769. if (keyType == EVP_PKEY_RSA)
  58770. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  58771. WOLFSSL_SUCCESS);
  58772. #endif
  58773. #endif
  58774. /* Check returning only length */
  58775. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  58776. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  58777. ExpectIntGT(siglenOnlyLen, 0);
  58778. /* Sign data */
  58779. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  58780. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  58781. ExpectIntGE(siglenOnlyLen, siglen);
  58782. /* Verify signature */
  58783. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  58784. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  58785. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  58786. !defined(HAVE_SELFTEST)
  58787. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58788. if (keyType == EVP_PKEY_RSA)
  58789. ExpectIntEQ(
  58790. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  58791. WOLFSSL_SUCCESS);
  58792. #endif
  58793. #endif
  58794. ExpectIntEQ(EVP_PKEY_verify(
  58795. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  58796. WOLFSSL_SUCCESS);
  58797. ExpectIntEQ(EVP_PKEY_verify(
  58798. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  58799. WOLFSSL_FAILURE);
  58800. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  58801. !defined(HAVE_SELFTEST)
  58802. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58803. if (keyType == EVP_PKEY_RSA) {
  58804. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  58805. /* Try RSA sign/verify with no padding. */
  58806. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  58807. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  58808. WOLFSSL_SUCCESS);
  58809. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  58810. siglen), WOLFSSL_SUCCESS);
  58811. ExpectIntGE(siglenOnlyLen, siglen);
  58812. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  58813. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  58814. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  58815. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  58816. siglen), WOLFSSL_SUCCESS);
  58817. #endif
  58818. /* Wrong padding schemes. */
  58819. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  58820. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  58821. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  58822. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  58823. siglen), WOLFSSL_SUCCESS);
  58824. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  58825. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  58826. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  58827. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  58828. siglen), WOLFSSL_SUCCESS);
  58829. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  58830. WOLFSSL_SUCCESS);
  58831. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  58832. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  58833. }
  58834. #endif
  58835. #endif
  58836. /* error cases */
  58837. siglen = keySz; /* Reset because sig size may vary slightly */
  58838. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  58839. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  58840. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  58841. WOLFSSL_SUCCESS);
  58842. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  58843. WOLFSSL_SUCCESS);
  58844. EVP_PKEY_free(pkey);
  58845. pkey = NULL;
  58846. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  58847. DSA_free(dsa);
  58848. dsa = NULL;
  58849. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  58850. EVP_PKEY_CTX_free(ctx_verify);
  58851. ctx_verify = NULL;
  58852. EVP_PKEY_CTX_free(ctx);
  58853. ctx = NULL;
  58854. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  58855. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  58856. #endif /* OPENSSL_EXTRA */
  58857. return EXPECT_RESULT();
  58858. }
  58859. #endif
  58860. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  58861. {
  58862. EXPECT_DECLS;
  58863. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  58864. !defined(HAVE_SELFTEST)
  58865. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58866. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  58867. #endif
  58868. #endif
  58869. return EXPECT_RESULT();
  58870. }
  58871. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  58872. {
  58873. EXPECT_DECLS;
  58874. #if defined(OPENSSL_EXTRA)
  58875. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  58876. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  58877. #endif
  58878. #endif
  58879. return EXPECT_RESULT();
  58880. }
  58881. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  58882. {
  58883. EXPECT_DECLS;
  58884. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  58885. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58886. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  58887. #endif
  58888. #endif
  58889. return EXPECT_RESULT();
  58890. }
  58891. static int test_EVP_PKEY_rsa(void)
  58892. {
  58893. EXPECT_DECLS;
  58894. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  58895. WOLFSSL_RSA* rsa = NULL;
  58896. WOLFSSL_EVP_PKEY* pkey = NULL;
  58897. ExpectNotNull(rsa = wolfSSL_RSA_new());
  58898. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  58899. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  58900. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  58901. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  58902. if (EXPECT_FAIL()) {
  58903. wolfSSL_RSA_free(rsa);
  58904. }
  58905. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  58906. wolfSSL_EVP_PKEY_free(pkey);
  58907. #endif
  58908. return EXPECT_RESULT();
  58909. }
  58910. static int test_EVP_PKEY_ec(void)
  58911. {
  58912. EXPECT_DECLS;
  58913. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  58914. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  58915. WOLFSSL_EC_KEY* ecKey = NULL;
  58916. WOLFSSL_EVP_PKEY* pkey = NULL;
  58917. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  58918. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  58919. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  58920. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  58921. /* Should fail since ecKey is empty */
  58922. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  58923. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  58924. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  58925. if (EXPECT_FAIL()) {
  58926. wolfSSL_EC_KEY_free(ecKey);
  58927. }
  58928. wolfSSL_EVP_PKEY_free(pkey);
  58929. #endif
  58930. #endif
  58931. return EXPECT_RESULT();
  58932. }
  58933. static int test_EVP_PKEY_cmp(void)
  58934. {
  58935. EXPECT_DECLS;
  58936. #if defined(OPENSSL_EXTRA)
  58937. EVP_PKEY *a = NULL;
  58938. EVP_PKEY *b = NULL;
  58939. const unsigned char *in;
  58940. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  58941. in = client_key_der_2048;
  58942. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  58943. &in, (long)sizeof_client_key_der_2048));
  58944. in = client_key_der_2048;
  58945. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  58946. &in, (long)sizeof_client_key_der_2048));
  58947. /* Test success case RSA */
  58948. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  58949. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  58950. #else
  58951. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  58952. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  58953. EVP_PKEY_free(b);
  58954. b = NULL;
  58955. EVP_PKEY_free(a);
  58956. a = NULL;
  58957. #endif
  58958. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  58959. in = ecc_clikey_der_256;
  58960. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  58961. &in, (long)sizeof_ecc_clikey_der_256));
  58962. in = ecc_clikey_der_256;
  58963. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  58964. &in, (long)sizeof_ecc_clikey_der_256));
  58965. /* Test success case ECC */
  58966. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  58967. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  58968. #else
  58969. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  58970. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  58971. EVP_PKEY_free(b);
  58972. b = NULL;
  58973. EVP_PKEY_free(a);
  58974. a = NULL;
  58975. #endif
  58976. /* Test failure cases */
  58977. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  58978. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  58979. in = client_key_der_2048;
  58980. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  58981. &in, (long)sizeof_client_key_der_2048));
  58982. in = ecc_clikey_der_256;
  58983. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  58984. &in, (long)sizeof_ecc_clikey_der_256));
  58985. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  58986. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  58987. #else
  58988. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  58989. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  58990. EVP_PKEY_free(b);
  58991. b = NULL;
  58992. EVP_PKEY_free(a);
  58993. a = NULL;
  58994. #endif
  58995. /* invalid or empty failure cases */
  58996. a = EVP_PKEY_new();
  58997. b = EVP_PKEY_new();
  58998. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  58999. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  59000. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  59001. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  59002. #ifdef NO_RSA
  59003. /* Type check will fail since RSA is the default EVP key type */
  59004. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  59005. #else
  59006. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  59007. #endif
  59008. #else
  59009. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  59010. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  59011. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  59012. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  59013. #endif
  59014. EVP_PKEY_free(b);
  59015. EVP_PKEY_free(a);
  59016. (void)in;
  59017. #endif
  59018. return EXPECT_RESULT();
  59019. }
  59020. static int test_ERR_load_crypto_strings(void)
  59021. {
  59022. #if defined(OPENSSL_ALL)
  59023. ERR_load_crypto_strings();
  59024. return TEST_SUCCESS;
  59025. #else
  59026. return TEST_SKIPPED;
  59027. #endif
  59028. }
  59029. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  59030. static void free_x509(X509* x)
  59031. {
  59032. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  59033. }
  59034. #endif
  59035. static int test_sk_X509(void)
  59036. {
  59037. EXPECT_DECLS;
  59038. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  59039. {
  59040. STACK_OF(X509)* s = NULL;
  59041. ExpectNotNull(s = sk_X509_new_null());
  59042. ExpectIntEQ(sk_X509_num(s), 0);
  59043. sk_X509_pop_free(s, NULL);
  59044. ExpectNotNull(s = sk_X509_new_null());
  59045. ExpectIntEQ(sk_X509_num(s), 0);
  59046. sk_X509_pop_free(s, NULL);
  59047. ExpectNotNull(s = sk_X509_new_null());
  59048. sk_X509_push(s, (X509*)1);
  59049. ExpectIntEQ(sk_X509_num(s), 1);
  59050. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  59051. sk_X509_push(s, (X509*)2);
  59052. ExpectIntEQ(sk_X509_num(s), 2);
  59053. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  59054. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  59055. sk_X509_push(s, (X509*)2);
  59056. sk_X509_pop_free(s, free_x509);
  59057. }
  59058. {
  59059. /* Push a list of 10 X509s onto stack, then verify that
  59060. * value(), push(), shift(), and pop() behave as expected. */
  59061. STACK_OF(X509)* s = NULL;
  59062. X509* xList[10];
  59063. int i = 0;
  59064. const int len = (sizeof(xList) / sizeof(xList[0]));
  59065. for (i = 0; i < len; ++i) {
  59066. xList[i] = NULL;
  59067. ExpectNotNull(xList[i] = X509_new());
  59068. }
  59069. /* test push, pop, and free */
  59070. ExpectNotNull(s = sk_X509_new_null());
  59071. for (i = 0; i < len; ++i) {
  59072. sk_X509_push(s, xList[i]);
  59073. ExpectIntEQ(sk_X509_num(s), i + 1);
  59074. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  59075. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  59076. }
  59077. /* pop returns and removes last pushed on stack, which is index 0
  59078. * in sk_x509_value */
  59079. for (i = 0; i < len; ++i) {
  59080. X509 * x = sk_X509_value(s, 0);
  59081. X509 * y = sk_X509_pop(s);
  59082. X509 * z = xList[len - 1 - i];
  59083. ExpectIntEQ((x == y), 1);
  59084. ExpectIntEQ((x == z), 1);
  59085. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  59086. }
  59087. sk_free(s);
  59088. s = NULL;
  59089. /* test push, shift, and free */
  59090. ExpectNotNull(s = sk_X509_new_null());
  59091. for (i = 0; i < len; ++i) {
  59092. sk_X509_push(s, xList[i]);
  59093. ExpectIntEQ(sk_X509_num(s), i + 1);
  59094. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  59095. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  59096. }
  59097. /* shift returns and removes first pushed on stack, which is index i
  59098. * in sk_x509_value() */
  59099. for (i = 0; i < len; ++i) {
  59100. X509 * x = sk_X509_value(s, len - 1 - i);
  59101. X509 * y = sk_X509_shift(s);
  59102. X509 * z = xList[i];
  59103. ExpectIntEQ((x == y), 1);
  59104. ExpectIntEQ((x == z), 1);
  59105. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  59106. }
  59107. sk_free(s);
  59108. for (i = 0; i < len; ++i)
  59109. X509_free(xList[i]);
  59110. }
  59111. #endif
  59112. return EXPECT_RESULT();
  59113. }
  59114. static int test_sk_X509_CRL(void)
  59115. {
  59116. EXPECT_DECLS;
  59117. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  59118. X509_CRL* crl = NULL;
  59119. XFILE fp = XBADFILE;
  59120. STACK_OF(X509_CRL)* s = NULL;
  59121. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  59122. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  59123. NULL, NULL));
  59124. if (fp != XBADFILE)
  59125. XFCLOSE(fp);
  59126. ExpectNotNull(s = sk_X509_CRL_new());
  59127. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  59128. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  59129. if (EXPECT_FAIL()) {
  59130. X509_CRL_free(crl);
  59131. }
  59132. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  59133. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  59134. sk_X509_CRL_free(s);
  59135. #endif
  59136. return EXPECT_RESULT();
  59137. }
  59138. static int test_X509_get_signature_nid(void)
  59139. {
  59140. EXPECT_DECLS;
  59141. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  59142. X509* x509 = NULL;
  59143. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  59144. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  59145. SSL_FILETYPE_PEM));
  59146. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  59147. X509_free(x509);
  59148. #endif
  59149. return EXPECT_RESULT();
  59150. }
  59151. static int test_X509_REQ(void)
  59152. {
  59153. EXPECT_DECLS;
  59154. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  59155. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  59156. X509_NAME* name = NULL;
  59157. #ifndef NO_RSA
  59158. X509_NAME* subject = NULL;
  59159. #endif
  59160. #if !defined(NO_RSA) || defined(HAVE_ECC)
  59161. X509_REQ* req = NULL;
  59162. EVP_PKEY* priv = NULL;
  59163. EVP_PKEY* pub = NULL;
  59164. unsigned char* der = NULL;
  59165. int len;
  59166. #endif
  59167. #ifndef NO_RSA
  59168. EVP_MD_CTX *mctx = NULL;
  59169. EVP_PKEY_CTX *pkctx = NULL;
  59170. #ifdef USE_CERT_BUFFERS_1024
  59171. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  59172. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  59173. #elif defined(USE_CERT_BUFFERS_2048)
  59174. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  59175. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  59176. #endif
  59177. #endif
  59178. #ifdef HAVE_ECC
  59179. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  59180. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  59181. #endif
  59182. ExpectNotNull(name = X509_NAME_new());
  59183. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  59184. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  59185. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  59186. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  59187. #ifndef NO_RSA
  59188. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  59189. (long)sizeof_client_key_der_2048));
  59190. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  59191. (long)sizeof_client_keypub_der_2048));
  59192. ExpectNotNull(req = X509_REQ_new());
  59193. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  59194. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  59195. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  59196. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  59197. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  59198. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  59199. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  59200. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  59201. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  59202. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  59203. len = i2d_X509_REQ(req, &der);
  59204. DEBUG_WRITE_DER(der, len, "req.der");
  59205. #ifdef USE_CERT_BUFFERS_1024
  59206. ExpectIntEQ(len, 381);
  59207. #else
  59208. ExpectIntEQ(len, 643);
  59209. #endif
  59210. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  59211. der = NULL;
  59212. mctx = EVP_MD_CTX_new();
  59213. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  59214. WOLFSSL_SUCCESS);
  59215. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  59216. EVP_MD_CTX_free(mctx);
  59217. mctx = NULL;
  59218. X509_REQ_free(NULL);
  59219. X509_REQ_free(req);
  59220. req = NULL;
  59221. /* Test getting the subject from a newly created X509_REQ */
  59222. ExpectNotNull(req = X509_REQ_new());
  59223. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  59224. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  59225. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  59226. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  59227. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  59228. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  59229. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  59230. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  59231. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  59232. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  59233. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  59234. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  59235. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  59236. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  59237. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  59238. len = i2d_X509_REQ(req, &der);
  59239. DEBUG_WRITE_DER(der, len, "req2.der");
  59240. #ifdef USE_CERT_BUFFERS_1024
  59241. ExpectIntEQ(len, 435);
  59242. #else
  59243. ExpectIntEQ(len, 696);
  59244. #endif
  59245. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  59246. der = NULL;
  59247. EVP_PKEY_free(pub);
  59248. pub = NULL;
  59249. EVP_PKEY_free(priv);
  59250. priv = NULL;
  59251. X509_REQ_free(req);
  59252. req = NULL;
  59253. #endif
  59254. #ifdef HAVE_ECC
  59255. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  59256. sizeof_ecc_clikey_der_256));
  59257. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  59258. sizeof_ecc_clikeypub_der_256));
  59259. ExpectNotNull(req = X509_REQ_new());
  59260. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  59261. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  59262. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  59263. /* Signature is random and may be shorter or longer. */
  59264. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  59265. ExpectIntLE(len, 253);
  59266. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  59267. X509_REQ_free(req);
  59268. EVP_PKEY_free(pub);
  59269. EVP_PKEY_free(priv);
  59270. #ifdef FP_ECC
  59271. wc_ecc_fp_free();
  59272. #endif
  59273. #endif /* HAVE_ECC */
  59274. X509_NAME_free(name);
  59275. #endif
  59276. return EXPECT_RESULT();
  59277. }
  59278. static int test_wolfssl_PKCS7(void)
  59279. {
  59280. EXPECT_DECLS;
  59281. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  59282. !defined(NO_RSA)
  59283. PKCS7* pkcs7 = NULL;
  59284. byte data[FOURK_BUF];
  59285. word32 len = sizeof(data);
  59286. const byte* p = data;
  59287. byte content[] = "Test data to encode.";
  59288. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  59289. BIO* bio = NULL;
  59290. byte key[sizeof(client_key_der_2048)];
  59291. word32 keySz = (word32)sizeof(key);
  59292. byte* out = NULL;
  59293. #endif
  59294. ExpectIntGT((len = (word32)CreatePKCS7SignedData(data, (int)len, content,
  59295. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  59296. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, (int)len));
  59297. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  59298. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  59299. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  59300. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  59301. PKCS7_free(pkcs7);
  59302. pkcs7 = NULL;
  59303. /* fail case, without PKCS7_NOVERIFY */
  59304. p = data;
  59305. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  59306. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  59307. 0), WOLFSSL_FAILURE);
  59308. PKCS7_free(pkcs7);
  59309. pkcs7 = NULL;
  59310. /* success case, with PKCS7_NOVERIFY */
  59311. p = data;
  59312. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  59313. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  59314. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  59315. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  59316. /* test i2d */
  59317. XMEMCPY(key, client_key_der_2048, keySz);
  59318. if (pkcs7 != NULL) {
  59319. pkcs7->privateKey = key;
  59320. pkcs7->privateKeySz = (word32)sizeof(key);
  59321. pkcs7->encryptOID = RSAk;
  59322. #ifdef NO_SHA
  59323. pkcs7->hashOID = SHA256h;
  59324. #else
  59325. pkcs7->hashOID = SHAh;
  59326. #endif
  59327. }
  59328. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  59329. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  59330. #ifndef NO_ASN_TIME
  59331. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  59332. #else
  59333. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  59334. #endif
  59335. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  59336. BIO_free(bio);
  59337. #endif
  59338. PKCS7_free(NULL);
  59339. PKCS7_free(pkcs7);
  59340. #endif
  59341. return EXPECT_RESULT();
  59342. }
  59343. static int test_wolfSSL_PKCS7_sign(void)
  59344. {
  59345. EXPECT_DECLS;
  59346. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  59347. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  59348. PKCS7* p7 = NULL;
  59349. PKCS7* p7Ver = NULL;
  59350. byte* out = NULL;
  59351. byte* tmpPtr = NULL;
  59352. int outLen = 0;
  59353. int flags = 0;
  59354. byte data[] = "Test data to encode.";
  59355. const char* cert = "./certs/server-cert.pem";
  59356. const char* key = "./certs/server-key.pem";
  59357. const char* ca = "./certs/ca-cert.pem";
  59358. WOLFSSL_BIO* certBio = NULL;
  59359. WOLFSSL_BIO* keyBio = NULL;
  59360. WOLFSSL_BIO* caBio = NULL;
  59361. WOLFSSL_BIO* inBio = NULL;
  59362. X509* signCert = NULL;
  59363. EVP_PKEY* signKey = NULL;
  59364. X509* caCert = NULL;
  59365. X509_STORE* store = NULL;
  59366. #ifndef NO_PKCS7_STREAM
  59367. int z;
  59368. int ret;
  59369. #endif /* !NO_PKCS7_STREAM */
  59370. /* read signer cert/key into BIO */
  59371. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  59372. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  59373. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  59374. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  59375. /* read CA cert into store (for verify) */
  59376. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  59377. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  59378. ExpectNotNull(store = X509_STORE_new());
  59379. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  59380. /* data to be signed into BIO */
  59381. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  59382. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  59383. /* PKCS7_sign, bad args: signer NULL */
  59384. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  59385. /* PKCS7_sign, bad args: signer key NULL */
  59386. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  59387. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  59388. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  59389. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  59390. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  59391. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  59392. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  59393. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  59394. {
  59395. flags = PKCS7_BINARY;
  59396. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  59397. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  59398. /* verify with d2i_PKCS7 */
  59399. tmpPtr = out;
  59400. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  59401. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  59402. PKCS7_free(p7Ver);
  59403. p7Ver = NULL;
  59404. /* verify with wc_PKCS7_VerifySignedData */
  59405. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  59406. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  59407. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  59408. #ifndef NO_PKCS7_STREAM
  59409. /* verify with wc_PKCS7_VerifySignedData streaming */
  59410. wc_PKCS7_Free(p7Ver);
  59411. p7Ver = NULL;
  59412. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  59413. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  59414. /* test for streaming */
  59415. ret = -1;
  59416. for (z = 0; z < outLen && ret != 0; z++) {
  59417. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  59418. if (ret < 0){
  59419. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  59420. }
  59421. }
  59422. ExpectIntEQ(ret, 0);
  59423. #endif /* !NO_PKCS7_STREAM */
  59424. /* compare the signer found to expected signer */
  59425. ExpectIntNE(p7Ver->verifyCertSz, 0);
  59426. tmpPtr = NULL;
  59427. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  59428. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  59429. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  59430. tmpPtr = NULL;
  59431. wc_PKCS7_Free(p7Ver);
  59432. p7Ver = NULL;
  59433. ExpectNotNull(out);
  59434. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  59435. out = NULL;
  59436. PKCS7_free(p7);
  59437. p7 = NULL;
  59438. }
  59439. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  59440. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  59441. {
  59442. /* re-populate input BIO, may have been consumed */
  59443. BIO_free(inBio);
  59444. inBio = NULL;
  59445. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  59446. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  59447. flags = PKCS7_BINARY | PKCS7_STREAM;
  59448. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  59449. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  59450. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  59451. /* PKCS7_final, bad args: PKCS7 null */
  59452. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  59453. /* PKCS7_final, bad args: PKCS7 null */
  59454. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  59455. tmpPtr = out;
  59456. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  59457. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  59458. PKCS7_free(p7Ver);
  59459. p7Ver = NULL;
  59460. ExpectNotNull(out);
  59461. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  59462. out = NULL;
  59463. PKCS7_free(p7);
  59464. p7 = NULL;
  59465. }
  59466. /* TEST SUCCESS: Detached, not streaming, not MIME */
  59467. {
  59468. /* re-populate input BIO, may have been consumed */
  59469. BIO_free(inBio);
  59470. inBio = NULL;
  59471. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  59472. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  59473. flags = PKCS7_BINARY | PKCS7_DETACHED;
  59474. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  59475. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  59476. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  59477. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  59478. if (p7Ver != NULL) {
  59479. p7Ver->content = data;
  59480. p7Ver->contentSz = sizeof(data);
  59481. }
  59482. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  59483. wc_PKCS7_Free(p7Ver);
  59484. p7Ver = NULL;
  59485. #ifndef NO_PKCS7_STREAM
  59486. /* verify with wc_PKCS7_VerifySignedData streaming */
  59487. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  59488. if (p7Ver != NULL) {
  59489. p7Ver->content = data;
  59490. p7Ver->contentSz = sizeof(data);
  59491. }
  59492. /* test for streaming */
  59493. ret = -1;
  59494. for (z = 0; z < outLen && ret != 0; z++) {
  59495. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  59496. if (ret < 0){
  59497. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  59498. }
  59499. }
  59500. ExpectIntEQ(ret, 0);
  59501. wc_PKCS7_Free(p7Ver);
  59502. p7Ver = NULL;
  59503. #endif /* !NO_PKCS7_STREAM */
  59504. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  59505. * yet support detached content */
  59506. tmpPtr = out;
  59507. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  59508. PKCS7_free(p7Ver);
  59509. p7Ver = NULL;
  59510. ExpectNotNull(out);
  59511. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  59512. out = NULL;
  59513. PKCS7_free(p7);
  59514. p7 = NULL;
  59515. }
  59516. /* TEST SUCCESS: Detached, streaming, not MIME */
  59517. {
  59518. /* re-populate input BIO, may have been consumed */
  59519. BIO_free(inBio);
  59520. inBio = NULL;
  59521. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  59522. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  59523. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  59524. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  59525. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  59526. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  59527. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  59528. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  59529. if (p7Ver != NULL) {
  59530. p7Ver->content = data;
  59531. p7Ver->contentSz = sizeof(data);
  59532. }
  59533. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  59534. wc_PKCS7_Free(p7Ver);
  59535. p7Ver = NULL;
  59536. ExpectNotNull(out);
  59537. #ifndef NO_PKCS7_STREAM
  59538. /* verify with wc_PKCS7_VerifySignedData streaming */
  59539. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  59540. if (p7Ver != NULL) {
  59541. p7Ver->content = data;
  59542. p7Ver->contentSz = sizeof(data);
  59543. }
  59544. /* test for streaming */
  59545. ret = -1;
  59546. for (z = 0; z < outLen && ret != 0; z++) {
  59547. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  59548. if (ret < 0){
  59549. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  59550. }
  59551. }
  59552. ExpectIntEQ(ret, 0);
  59553. ExpectNotNull(out);
  59554. wc_PKCS7_Free(p7Ver);
  59555. p7Ver = NULL;
  59556. #endif /* !NO_PKCS7_STREAM */
  59557. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  59558. PKCS7_free(p7);
  59559. p7 = NULL;
  59560. }
  59561. X509_STORE_free(store);
  59562. X509_free(caCert);
  59563. X509_free(signCert);
  59564. EVP_PKEY_free(signKey);
  59565. BIO_free(inBio);
  59566. BIO_free(keyBio);
  59567. BIO_free(certBio);
  59568. BIO_free(caBio);
  59569. #endif
  59570. return EXPECT_RESULT();
  59571. }
  59572. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  59573. {
  59574. EXPECT_DECLS;
  59575. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  59576. PKCS7_SIGNED* pkcs7 = NULL;
  59577. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  59578. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  59579. PKCS7_SIGNED_free(pkcs7);
  59580. #endif
  59581. return EXPECT_RESULT();
  59582. }
  59583. #ifndef NO_BIO
  59584. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  59585. {
  59586. EXPECT_DECLS;
  59587. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  59588. PKCS7* pkcs7 = NULL;
  59589. BIO* bio = NULL;
  59590. const byte* cert_buf = NULL;
  59591. int ret = 0;
  59592. WC_RNG rng;
  59593. const byte data[] = { /* Hello World */
  59594. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  59595. 0x72,0x6c,0x64
  59596. };
  59597. #ifndef NO_RSA
  59598. #if defined(USE_CERT_BUFFERS_2048)
  59599. byte key[sizeof(client_key_der_2048)];
  59600. byte cert[sizeof(client_cert_der_2048)];
  59601. word32 keySz = (word32)sizeof(key);
  59602. word32 certSz = (word32)sizeof(cert);
  59603. XMEMSET(key, 0, keySz);
  59604. XMEMSET(cert, 0, certSz);
  59605. XMEMCPY(key, client_key_der_2048, keySz);
  59606. XMEMCPY(cert, client_cert_der_2048, certSz);
  59607. #elif defined(USE_CERT_BUFFERS_1024)
  59608. byte key[sizeof_client_key_der_1024];
  59609. byte cert[sizeof(sizeof_client_cert_der_1024)];
  59610. word32 keySz = (word32)sizeof(key);
  59611. word32 certSz = (word32)sizeof(cert);
  59612. XMEMSET(key, 0, keySz);
  59613. XMEMSET(cert, 0, certSz);
  59614. XMEMCPY(key, client_key_der_1024, keySz);
  59615. XMEMCPY(cert, client_cert_der_1024, certSz);
  59616. #else
  59617. unsigned char cert[ONEK_BUF];
  59618. unsigned char key[ONEK_BUF];
  59619. XFILE fp = XBADFILE;
  59620. int certSz;
  59621. int keySz;
  59622. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  59623. XBADFILE);
  59624. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  59625. fp), 0);
  59626. if (fp != XBADFILE) {
  59627. XFCLOSE(fp);
  59628. fp = XBADFILE;
  59629. }
  59630. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  59631. XBADFILE);
  59632. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  59633. 0);
  59634. if (fp != XBADFILE) {
  59635. XFCLOSE(fp);
  59636. fp = XBADFILE;
  59637. }
  59638. #endif
  59639. #elif defined(HAVE_ECC)
  59640. #if defined(USE_CERT_BUFFERS_256)
  59641. unsigned char cert[sizeof(cliecc_cert_der_256)];
  59642. unsigned char key[sizeof(ecc_clikey_der_256)];
  59643. int certSz = (int)sizeof(cert);
  59644. int keySz = (int)sizeof(key);
  59645. XMEMSET(cert, 0, certSz);
  59646. XMEMSET(key, 0, keySz);
  59647. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  59648. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  59649. #else
  59650. unsigned char cert[ONEK_BUF];
  59651. unsigned char key[ONEK_BUF];
  59652. XFILE fp = XBADFILE;
  59653. int certSz, keySz;
  59654. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  59655. XBADFILE);
  59656. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  59657. fp), 0);
  59658. if (fp != XBADFILE) {
  59659. XFCLOSE(fp);
  59660. fp = XBADFILE;
  59661. }
  59662. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  59663. XBADFILE);
  59664. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  59665. 0);
  59666. if (fp != XBADFILE) {
  59667. XFCLOSE(fp);
  59668. fp = XBADFILE;
  59669. }
  59670. #endif
  59671. #else
  59672. #error PKCS7 requires ECC or RSA
  59673. #endif
  59674. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  59675. /* initialize with DER encoded cert */
  59676. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  59677. /* init rng */
  59678. XMEMSET(&rng, 0, sizeof(WC_RNG));
  59679. ExpectIntEQ(wc_InitRng(&rng), 0);
  59680. if (pkcs7 != NULL) {
  59681. pkcs7->rng = &rng;
  59682. pkcs7->content = (byte*)data; /* not used for ex */
  59683. pkcs7->contentSz = (word32)sizeof(data);
  59684. pkcs7->contentOID = SIGNED_DATA;
  59685. pkcs7->privateKey = key;
  59686. pkcs7->privateKeySz = (word32)sizeof(key);
  59687. pkcs7->encryptOID = RSAk;
  59688. #ifdef NO_SHA
  59689. pkcs7->hashOID = SHA256h;
  59690. #else
  59691. pkcs7->hashOID = SHAh;
  59692. #endif
  59693. pkcs7->signedAttribs = NULL;
  59694. pkcs7->signedAttribsSz = 0;
  59695. }
  59696. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  59697. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  59698. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  59699. /* Read PKCS#7 PEM from BIO */
  59700. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  59701. ExpectIntGE(ret, 0);
  59702. BIO_free(bio);
  59703. wc_PKCS7_Free(pkcs7);
  59704. wc_FreeRng(&rng);
  59705. #endif
  59706. return EXPECT_RESULT();
  59707. }
  59708. #ifdef HAVE_SMIME
  59709. /* // NOLINTBEGIN(clang-analyzer-unix.Stream) */
  59710. static int test_wolfSSL_SMIME_read_PKCS7(void)
  59711. {
  59712. EXPECT_DECLS;
  59713. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  59714. !defined(NO_RSA)
  59715. PKCS7* pkcs7 = NULL;
  59716. BIO* bio = NULL;
  59717. BIO* bcont = NULL;
  59718. BIO* out = NULL;
  59719. const byte* outBuf = NULL;
  59720. int outBufLen = 0;
  59721. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  59722. XFILE smimeTestFile = XBADFILE;
  59723. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  59724. XBADFILE);
  59725. /* smime-test.p7s */
  59726. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  59727. ExpectNotNull(bio);
  59728. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  59729. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  59730. ExpectNotNull(pkcs7);
  59731. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  59732. PKCS7_NOVERIFY), SSL_SUCCESS);
  59733. if (smimeTestFile != XBADFILE) {
  59734. XFCLOSE(smimeTestFile);
  59735. smimeTestFile = XBADFILE;
  59736. }
  59737. if (bcont) BIO_free(bcont);
  59738. bcont = NULL;
  59739. wolfSSL_PKCS7_free(pkcs7);
  59740. pkcs7 = NULL;
  59741. /* smime-test-multipart.p7s */
  59742. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  59743. ExpectFalse(smimeTestFile == XBADFILE);
  59744. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  59745. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  59746. ExpectNotNull(pkcs7);
  59747. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  59748. PKCS7_NOVERIFY), SSL_SUCCESS);
  59749. if (smimeTestFile != XBADFILE) {
  59750. XFCLOSE(smimeTestFile);
  59751. smimeTestFile = XBADFILE;
  59752. }
  59753. if (bcont) BIO_free(bcont);
  59754. bcont = NULL;
  59755. wolfSSL_PKCS7_free(pkcs7);
  59756. pkcs7 = NULL;
  59757. /* smime-test-multipart-badsig.p7s */
  59758. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  59759. ExpectFalse(smimeTestFile == XBADFILE);
  59760. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  59761. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  59762. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  59763. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  59764. PKCS7_NOVERIFY), SSL_FAILURE);
  59765. if (smimeTestFile != XBADFILE) {
  59766. XFCLOSE(smimeTestFile);
  59767. smimeTestFile = XBADFILE;
  59768. }
  59769. if (bcont) BIO_free(bcont);
  59770. bcont = NULL;
  59771. wolfSSL_PKCS7_free(pkcs7);
  59772. pkcs7 = NULL;
  59773. /* smime-test-canon.p7s */
  59774. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  59775. ExpectFalse(smimeTestFile == XBADFILE);
  59776. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  59777. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  59778. ExpectNotNull(pkcs7);
  59779. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  59780. PKCS7_NOVERIFY), SSL_SUCCESS);
  59781. if (smimeTestFile != XBADFILE) {
  59782. XFCLOSE(smimeTestFile);
  59783. smimeTestFile = XBADFILE;
  59784. }
  59785. if (bcont) BIO_free(bcont);
  59786. bcont = NULL;
  59787. wolfSSL_PKCS7_free(pkcs7);
  59788. pkcs7 = NULL;
  59789. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  59790. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  59791. ExpectFalse(smimeTestFile == XBADFILE);
  59792. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  59793. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  59794. ExpectNotNull(pkcs7);
  59795. out = wolfSSL_BIO_new(BIO_s_mem());
  59796. ExpectNotNull(out);
  59797. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  59798. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  59799. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  59800. /* Content-Type should not show up at beginning of output buffer */
  59801. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  59802. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  59803. BIO_free(out);
  59804. BIO_free(bio);
  59805. if (bcont) BIO_free(bcont);
  59806. wolfSSL_PKCS7_free(pkcs7);
  59807. #endif
  59808. return EXPECT_RESULT();
  59809. }
  59810. /* // NOLINTEND(clang-analyzer-unix.Stream) */
  59811. static int test_wolfSSL_SMIME_write_PKCS7(void)
  59812. {
  59813. EXPECT_DECLS;
  59814. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  59815. PKCS7* p7 = NULL;
  59816. PKCS7* p7Ver = NULL;
  59817. int flags = 0;
  59818. byte data[] = "Test data to encode.";
  59819. const char* cert = "./certs/server-cert.pem";
  59820. const char* key = "./certs/server-key.pem";
  59821. const char* ca = "./certs/ca-cert.pem";
  59822. WOLFSSL_BIO* certBio = NULL;
  59823. WOLFSSL_BIO* keyBio = NULL;
  59824. WOLFSSL_BIO* caBio = NULL;
  59825. WOLFSSL_BIO* inBio = NULL;
  59826. WOLFSSL_BIO* outBio = NULL;
  59827. WOLFSSL_BIO* content = NULL;
  59828. X509* signCert = NULL;
  59829. EVP_PKEY* signKey = NULL;
  59830. X509* caCert = NULL;
  59831. X509_STORE* store = NULL;
  59832. /* read signer cert/key into BIO */
  59833. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  59834. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  59835. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  59836. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  59837. /* read CA cert into store (for verify) */
  59838. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  59839. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  59840. ExpectNotNull(store = X509_STORE_new());
  59841. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  59842. /* generate and verify SMIME: not detached */
  59843. {
  59844. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  59845. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  59846. flags = PKCS7_STREAM;
  59847. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  59848. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  59849. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  59850. /* bad arg: out NULL */
  59851. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  59852. /* bad arg: pkcs7 NULL */
  59853. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  59854. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  59855. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  59856. BIO_free(content);
  59857. content = NULL;
  59858. BIO_free(inBio);
  59859. inBio = NULL;
  59860. BIO_free(outBio);
  59861. outBio = NULL;
  59862. PKCS7_free(p7Ver);
  59863. p7Ver = NULL;
  59864. PKCS7_free(p7);
  59865. p7 = NULL;
  59866. }
  59867. /* generate and verify SMIME: not detached, add Content-Type */
  59868. {
  59869. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  59870. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  59871. flags = PKCS7_STREAM | PKCS7_TEXT;
  59872. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  59873. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  59874. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  59875. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  59876. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  59877. BIO_free(content);
  59878. content = NULL;
  59879. BIO_free(inBio);
  59880. inBio = NULL;
  59881. BIO_free(outBio);
  59882. outBio = NULL;
  59883. PKCS7_free(p7Ver);
  59884. p7Ver = NULL;
  59885. PKCS7_free(p7);
  59886. p7 = NULL;
  59887. }
  59888. /* generate and verify SMIME: detached */
  59889. {
  59890. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  59891. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  59892. flags = PKCS7_DETACHED | PKCS7_STREAM;
  59893. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  59894. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  59895. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  59896. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  59897. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  59898. BIO_free(content);
  59899. content = NULL;
  59900. BIO_free(inBio);
  59901. inBio = NULL;
  59902. BIO_free(outBio);
  59903. outBio = NULL;
  59904. PKCS7_free(p7Ver);
  59905. p7Ver = NULL;
  59906. PKCS7_free(p7);
  59907. p7 = NULL;
  59908. }
  59909. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  59910. {
  59911. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  59912. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  59913. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  59914. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  59915. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  59916. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  59917. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  59918. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  59919. BIO_free(content);
  59920. content = NULL;
  59921. BIO_free(inBio);
  59922. inBio = NULL;
  59923. BIO_free(outBio);
  59924. outBio = NULL;
  59925. PKCS7_free(p7Ver);
  59926. p7Ver = NULL;
  59927. PKCS7_free(p7);
  59928. p7 = NULL;
  59929. }
  59930. X509_STORE_free(store);
  59931. X509_free(caCert);
  59932. X509_free(signCert);
  59933. EVP_PKEY_free(signKey);
  59934. BIO_free(keyBio);
  59935. BIO_free(certBio);
  59936. BIO_free(caBio);
  59937. #endif
  59938. return EXPECT_RESULT();
  59939. }
  59940. #endif /* HAVE_SMIME */
  59941. #endif /* !NO_BIO */
  59942. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  59943. * returns 0) */
  59944. static int test_X509_STORE_No_SSL_CTX(void)
  59945. {
  59946. EXPECT_DECLS;
  59947. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  59948. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  59949. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  59950. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  59951. defined(HAVE_CRL) && !defined(NO_RSA)
  59952. X509_STORE * store = NULL;
  59953. X509_STORE_CTX * storeCtx = NULL;
  59954. X509_CRL * crl = NULL;
  59955. X509 * ca = NULL;
  59956. X509 * cert = NULL;
  59957. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  59958. const char srvCert[] = "./certs/server-cert.pem";
  59959. const char caCert[] = "./certs/ca-cert.pem";
  59960. const char caDir[] = "./certs/crl/hash_pem";
  59961. XFILE fp = XBADFILE;
  59962. X509_LOOKUP * lookup = NULL;
  59963. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  59964. /* Set up store with CA */
  59965. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  59966. SSL_FILETYPE_PEM)));
  59967. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  59968. /* Add CRL lookup directory to store
  59969. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  59970. * of crl.pem */
  59971. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  59972. X509_LOOKUP_hash_dir())));
  59973. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  59974. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  59975. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  59976. SSL_SUCCESS);
  59977. /* Add CRL to store NOT containing the verified certificate, which
  59978. * forces use of the CRL lookup directory */
  59979. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  59980. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  59981. NULL, NULL));
  59982. if (fp != XBADFILE)
  59983. XFCLOSE(fp);
  59984. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  59985. /* Create verification context outside of an SSL session */
  59986. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  59987. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  59988. SSL_FILETYPE_PEM)));
  59989. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  59990. /* Perform verification, which should NOT indicate CRL missing due to the
  59991. * store CM's X509 store pointer being NULL */
  59992. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  59993. X509_CRL_free(crl);
  59994. X509_STORE_free(store);
  59995. X509_STORE_CTX_free(storeCtx);
  59996. X509_free(cert);
  59997. X509_free(ca);
  59998. #endif
  59999. return EXPECT_RESULT();
  60000. }
  60001. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  60002. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  60003. static int test_X509_LOOKUP_add_dir(void)
  60004. {
  60005. EXPECT_DECLS;
  60006. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  60007. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  60008. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  60009. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  60010. defined(HAVE_CRL) && !defined(NO_RSA)
  60011. X509_STORE * store = NULL;
  60012. X509_STORE_CTX * storeCtx = NULL;
  60013. X509_CRL * crl = NULL;
  60014. X509 * ca = NULL;
  60015. X509 * cert = NULL;
  60016. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  60017. const char srvCert[] = "./certs/server-cert.pem";
  60018. const char caCert[] = "./certs/ca-cert.pem";
  60019. const char caDir[] = "./certs/crl/hash_der";
  60020. XFILE fp = XBADFILE;
  60021. X509_LOOKUP * lookup = NULL;
  60022. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  60023. /* Set up store with CA */
  60024. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  60025. SSL_FILETYPE_PEM)));
  60026. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  60027. /* Add CRL lookup directory to store.
  60028. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  60029. * of crl.der */
  60030. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  60031. X509_LOOKUP_hash_dir())));
  60032. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  60033. SSL_SUCCESS);
  60034. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  60035. SSL_SUCCESS);
  60036. /* Add CRL to store NOT containing the verified certificate, which
  60037. * forces use of the CRL lookup directory */
  60038. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  60039. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  60040. NULL, NULL));
  60041. if (fp != XBADFILE) {
  60042. XFCLOSE(fp);
  60043. fp = XBADFILE;
  60044. }
  60045. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  60046. /* Create verification context outside of an SSL session */
  60047. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  60048. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  60049. SSL_FILETYPE_PEM)));
  60050. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  60051. /* Perform verification, which should NOT return CRL missing */
  60052. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  60053. X509_CRL_free(crl);
  60054. crl = NULL;
  60055. X509_STORE_free(store);
  60056. store = NULL;
  60057. X509_STORE_CTX_free(storeCtx);
  60058. storeCtx = NULL;
  60059. X509_free(cert);
  60060. cert = NULL;
  60061. X509_free(ca);
  60062. ca = NULL;
  60063. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  60064. * We should get CRL_MISSING at the end, because the lookup
  60065. * dir has only ASN1 CRLs. */
  60066. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  60067. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  60068. SSL_FILETYPE_PEM)));
  60069. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  60070. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  60071. X509_LOOKUP_hash_dir())));
  60072. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  60073. SSL_SUCCESS);
  60074. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  60075. SSL_SUCCESS);
  60076. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  60077. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  60078. NULL, NULL));
  60079. if (fp != XBADFILE) {
  60080. XFCLOSE(fp);
  60081. fp = XBADFILE;
  60082. }
  60083. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  60084. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  60085. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  60086. SSL_FILETYPE_PEM)));
  60087. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  60088. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  60089. * in dir containing only ASN1/DER. */
  60090. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  60091. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  60092. X509_V_ERR_UNABLE_TO_GET_CRL);
  60093. X509_CRL_free(crl);
  60094. X509_STORE_free(store);
  60095. X509_STORE_CTX_free(storeCtx);
  60096. X509_free(cert);
  60097. X509_free(ca);
  60098. #endif
  60099. return EXPECT_RESULT();
  60100. }
  60101. /*----------------------------------------------------------------------------*
  60102. | Certificate Failure Checks
  60103. *----------------------------------------------------------------------------*/
  60104. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  60105. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  60106. #if !defined(NO_RSA) || defined(HAVE_ECC)
  60107. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  60108. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  60109. int type)
  60110. {
  60111. int ret;
  60112. WOLFSSL_CERT_MANAGER* cm = NULL;
  60113. switch (type) {
  60114. case TESTING_RSA:
  60115. #ifdef NO_RSA
  60116. fprintf(stderr, "RSA disabled, skipping test\n");
  60117. return ASN_SIG_CONFIRM_E;
  60118. #else
  60119. break;
  60120. #endif
  60121. case TESTING_ECC:
  60122. #ifndef HAVE_ECC
  60123. fprintf(stderr, "ECC disabled, skipping test\n");
  60124. return ASN_SIG_CONFIRM_E;
  60125. #else
  60126. break;
  60127. #endif
  60128. default:
  60129. fprintf(stderr, "Bad function argument\n");
  60130. return BAD_FUNC_ARG;
  60131. }
  60132. cm = wolfSSL_CertManagerNew();
  60133. if (cm == NULL) {
  60134. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  60135. return -1;
  60136. }
  60137. #ifndef NO_FILESYSTEM
  60138. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  60139. if (ret != WOLFSSL_SUCCESS) {
  60140. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  60141. wolfSSL_CertManagerFree(cm);
  60142. return ret;
  60143. }
  60144. #else
  60145. (void)ca;
  60146. #endif
  60147. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, (long int)cert_sz,
  60148. WOLFSSL_FILETYPE_ASN1);
  60149. /* Let ExpectIntEQ handle return code */
  60150. wolfSSL_CertManagerFree(cm);
  60151. return ret;
  60152. }
  60153. #endif
  60154. #if !defined(NO_FILESYSTEM)
  60155. static int test_RsaSigFailure_cm(void)
  60156. {
  60157. EXPECT_DECLS;
  60158. #ifndef NO_RSA
  60159. const char* ca_cert = "./certs/ca-cert.pem";
  60160. const char* server_cert = "./certs/server-cert.der";
  60161. byte* cert_buf = NULL;
  60162. size_t cert_sz = 0;
  60163. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  60164. if ((cert_buf != NULL) && (cert_sz > 0)) {
  60165. /* corrupt DER - invert last byte, which is signature */
  60166. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  60167. /* test bad cert */
  60168. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  60169. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  60170. WOLFSSL_FATAL_ERROR);
  60171. #else
  60172. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  60173. ASN_SIG_CONFIRM_E);
  60174. #endif
  60175. }
  60176. /* load_file() uses malloc. */
  60177. if (cert_buf != NULL) {
  60178. free(cert_buf);
  60179. }
  60180. #endif /* !NO_RSA */
  60181. return EXPECT_RESULT();
  60182. }
  60183. static int test_EccSigFailure_cm(void)
  60184. {
  60185. EXPECT_DECLS;
  60186. #ifdef HAVE_ECC
  60187. /* self-signed ECC cert, so use server cert as CA */
  60188. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  60189. const char* server_cert = "./certs/server-ecc.der";
  60190. byte* cert_buf = NULL;
  60191. size_t cert_sz = 0;
  60192. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  60193. if (cert_buf != NULL && cert_sz > 0) {
  60194. /* corrupt DER - invert last byte, which is signature */
  60195. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  60196. /* test bad cert */
  60197. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  60198. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  60199. WOLFSSL_FATAL_ERROR);
  60200. #else
  60201. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  60202. ASN_SIG_CONFIRM_E);
  60203. #endif
  60204. }
  60205. /* load_file() uses malloc. */
  60206. if (cert_buf != NULL) {
  60207. free(cert_buf);
  60208. }
  60209. #ifdef FP_ECC
  60210. wc_ecc_fp_free();
  60211. #endif
  60212. #endif /* HAVE_ECC */
  60213. return EXPECT_RESULT();
  60214. }
  60215. #endif /* !NO_FILESYSTEM */
  60216. #endif /* NO_CERTS */
  60217. #ifdef WOLFSSL_TLS13
  60218. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  60219. #ifdef WC_SHA384_DIGEST_SIZE
  60220. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  60221. #else
  60222. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  60223. #endif
  60224. #endif
  60225. #ifdef WOLFSSL_EARLY_DATA
  60226. static const char earlyData[] = "Early Data";
  60227. static char earlyDataBuffer[1];
  60228. #endif
  60229. static int test_tls13_apis(void)
  60230. {
  60231. EXPECT_DECLS;
  60232. int ret;
  60233. #ifndef WOLFSSL_NO_TLS12
  60234. #ifndef NO_WOLFSSL_CLIENT
  60235. WOLFSSL_CTX* clientTls12Ctx = NULL;
  60236. WOLFSSL* clientTls12Ssl = NULL;
  60237. #endif
  60238. #ifndef NO_WOLFSSL_SERVER
  60239. WOLFSSL_CTX* serverTls12Ctx = NULL;
  60240. WOLFSSL* serverTls12Ssl = NULL;
  60241. #endif
  60242. #endif
  60243. #ifndef NO_WOLFSSL_CLIENT
  60244. WOLFSSL_CTX* clientCtx = NULL;
  60245. WOLFSSL* clientSsl = NULL;
  60246. #endif
  60247. #ifndef NO_WOLFSSL_SERVER
  60248. WOLFSSL_CTX* serverCtx = NULL;
  60249. WOLFSSL* serverSsl = NULL;
  60250. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  60251. const char* ourCert = svrCertFile;
  60252. const char* ourKey = svrKeyFile;
  60253. #endif
  60254. #endif
  60255. int required;
  60256. #ifdef WOLFSSL_EARLY_DATA
  60257. int outSz;
  60258. #endif
  60259. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  60260. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  60261. #ifdef WOLFSSL_HAVE_KYBER
  60262. WOLFSSL_KYBER_LEVEL1
  60263. #else
  60264. WOLFSSL_ECC_SECP256R1
  60265. #endif
  60266. };
  60267. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  60268. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  60269. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  60270. int numGroups = 2;
  60271. #endif
  60272. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  60273. char groupList[] =
  60274. #ifdef HAVE_CURVE25519
  60275. "X25519:"
  60276. #endif
  60277. #ifdef HAVE_CURVE448
  60278. "X448:"
  60279. #endif
  60280. #ifndef NO_ECC_SECP
  60281. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  60282. "P-521:secp521r1:"
  60283. #endif
  60284. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  60285. "P-384:secp384r1:"
  60286. #endif
  60287. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  60288. "P-256:secp256r1"
  60289. #if defined(WOLFSSL_HAVE_KYBER)
  60290. ":P256_KYBER_LEVEL1"
  60291. #endif
  60292. #endif
  60293. #endif /* !defined(NO_ECC_SECP) */
  60294. #if defined(WOLFSSL_HAVE_KYBER)
  60295. ":KYBER_LEVEL1"
  60296. #endif
  60297. "";
  60298. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  60299. (void)ret;
  60300. #ifndef WOLFSSL_NO_TLS12
  60301. #ifndef NO_WOLFSSL_CLIENT
  60302. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  60303. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  60304. #endif
  60305. #ifndef NO_WOLFSSL_SERVER
  60306. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  60307. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  60308. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  60309. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  60310. WOLFSSL_FILETYPE_PEM);
  60311. #endif
  60312. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  60313. #endif
  60314. #endif
  60315. #ifndef NO_WOLFSSL_CLIENT
  60316. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  60317. clientSsl = wolfSSL_new(clientCtx);
  60318. #endif
  60319. #ifndef NO_WOLFSSL_SERVER
  60320. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  60321. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  60322. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  60323. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  60324. #endif
  60325. serverSsl = wolfSSL_new(serverCtx);
  60326. ExpectNotNull(serverSsl);
  60327. #endif
  60328. #ifdef WOLFSSL_SEND_HRR_COOKIE
  60329. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  60330. #ifndef NO_WOLFSSL_CLIENT
  60331. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  60332. #endif
  60333. #ifndef NO_WOLFSSL_SERVER
  60334. #ifndef WOLFSSL_NO_TLS12
  60335. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  60336. BAD_FUNC_ARG);
  60337. #endif
  60338. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  60339. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  60340. WOLFSSL_SUCCESS);
  60341. #endif
  60342. #endif
  60343. #ifdef HAVE_SUPPORTED_CURVES
  60344. #ifdef HAVE_ECC
  60345. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  60346. BAD_FUNC_ARG);
  60347. #ifndef NO_WOLFSSL_SERVER
  60348. do {
  60349. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  60350. #ifdef WOLFSSL_ASYNC_CRYPT
  60351. if (ret == WC_PENDING_E)
  60352. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  60353. #endif
  60354. }
  60355. while (ret == WC_PENDING_E);
  60356. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  60357. #endif
  60358. #ifndef NO_WOLFSSL_CLIENT
  60359. #ifndef WOLFSSL_NO_TLS12
  60360. do {
  60361. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  60362. #ifdef WOLFSSL_ASYNC_CRYPT
  60363. if (ret == WC_PENDING_E)
  60364. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  60365. #endif
  60366. }
  60367. while (ret == WC_PENDING_E);
  60368. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  60369. #endif
  60370. do {
  60371. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  60372. #ifdef WOLFSSL_ASYNC_CRYPT
  60373. if (ret == WC_PENDING_E)
  60374. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  60375. #endif
  60376. }
  60377. while (ret == WC_PENDING_E);
  60378. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  60379. #endif
  60380. #elif defined(HAVE_CURVE25519)
  60381. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  60382. #ifndef NO_WOLFSSL_SERVER
  60383. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  60384. WOLFSSL_SUCCESS);
  60385. #endif
  60386. #ifndef NO_WOLFSSL_CLIENT
  60387. #ifndef WOLFSSL_NO_TLS12
  60388. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  60389. WOLFSSL_SUCCESS);
  60390. #endif
  60391. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  60392. WOLFSSL_SUCCESS);
  60393. #endif
  60394. #elif defined(HAVE_CURVE448)
  60395. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  60396. #ifndef NO_WOLFSSL_SERVER
  60397. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  60398. WOLFSSL_SUCCESS);
  60399. #endif
  60400. #ifndef NO_WOLFSSL_CLIENT
  60401. #ifndef WOLFSSL_NO_TLS12
  60402. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  60403. WOLFSSL_SUCCESS);
  60404. #endif
  60405. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  60406. WOLFSSL_SUCCESS);
  60407. #endif
  60408. #else
  60409. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  60410. BAD_FUNC_ARG);
  60411. #ifndef NO_WOLFSSL_CLIENT
  60412. #ifndef WOLFSSL_NO_TLS12
  60413. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  60414. NOT_COMPILED_IN);
  60415. #endif
  60416. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  60417. NOT_COMPILED_IN);
  60418. #endif
  60419. #endif
  60420. #if defined(WOLFSSL_HAVE_KYBER)
  60421. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  60422. #ifndef NO_WOLFSSL_SERVER
  60423. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  60424. WOLFSSL_SUCCESS);
  60425. #endif
  60426. #ifndef NO_WOLFSSL_CLIENT
  60427. #ifndef WOLFSSL_NO_TLS12
  60428. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  60429. BAD_FUNC_ARG);
  60430. #endif
  60431. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  60432. WOLFSSL_SUCCESS);
  60433. #endif
  60434. #endif
  60435. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  60436. #ifndef NO_WOLFSSL_SERVER
  60437. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  60438. #endif
  60439. #ifndef NO_WOLFSSL_CLIENT
  60440. #ifndef WOLFSSL_NO_TLS12
  60441. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  60442. #endif
  60443. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  60444. #endif
  60445. #endif /* HAVE_SUPPORTED_CURVES */
  60446. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  60447. #ifndef NO_WOLFSSL_CLIENT
  60448. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  60449. #endif
  60450. #ifndef NO_WOLFSSL_SERVER
  60451. #ifndef WOLFSSL_NO_TLS12
  60452. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  60453. #endif
  60454. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  60455. #endif
  60456. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  60457. #ifndef NO_WOLFSSL_CLIENT
  60458. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  60459. #endif
  60460. #ifndef NO_WOLFSSL_SERVER
  60461. #ifndef WOLFSSL_NO_TLS12
  60462. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  60463. #endif
  60464. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  60465. #endif
  60466. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  60467. #ifndef NO_WOLFSSL_CLIENT
  60468. #ifndef WOLFSSL_NO_TLS12
  60469. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  60470. #endif
  60471. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  60472. #endif
  60473. #ifndef NO_WOLFSSL_SERVER
  60474. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  60475. #endif
  60476. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  60477. #ifndef NO_WOLFSSL_CLIENT
  60478. #ifndef WOLFSSL_NO_TLS12
  60479. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  60480. #endif
  60481. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  60482. #endif
  60483. #ifndef NO_WOLFSSL_SERVER
  60484. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  60485. #endif
  60486. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  60487. #ifndef NO_WOLFSSL_CLIENT
  60488. #ifndef WOLFSSL_NO_TLS12
  60489. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  60490. #endif
  60491. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  60492. #endif
  60493. #ifndef NO_WOLFSSL_SERVER
  60494. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  60495. #endif
  60496. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  60497. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  60498. #ifndef NO_WOLFSSL_CLIENT
  60499. #ifndef WOLFSSL_NO_TLS12
  60500. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  60501. BAD_FUNC_ARG);
  60502. #endif
  60503. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  60504. #endif
  60505. #ifndef NO_WOLFSSL_SERVER
  60506. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  60507. #endif
  60508. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  60509. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  60510. #ifndef NO_WOLFSSL_SERVER
  60511. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  60512. #endif
  60513. #ifndef NO_WOLFSSL_CLIENT
  60514. #ifndef WOLFSSL_NO_TLS12
  60515. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  60516. BAD_FUNC_ARG);
  60517. #endif
  60518. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  60519. #endif
  60520. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  60521. #ifndef NO_WOLFSSL_SERVER
  60522. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  60523. #endif
  60524. #ifndef NO_WOLFSSL_CLIENT
  60525. #ifndef WOLFSSL_NO_TLS12
  60526. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  60527. BAD_FUNC_ARG);
  60528. #endif
  60529. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  60530. #endif
  60531. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  60532. #ifndef NO_WOLFSSL_CLIENT
  60533. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  60534. #endif
  60535. #ifndef NO_WOLFSSL_SERVER
  60536. #ifndef WOLFSSL_NO_TLS12
  60537. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  60538. BAD_FUNC_ARG);
  60539. #endif
  60540. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  60541. #endif
  60542. #endif
  60543. #ifdef HAVE_ECC
  60544. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  60545. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  60546. #ifndef NO_WOLFSSL_SERVER
  60547. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  60548. #endif
  60549. #ifndef NO_WOLFSSL_CLIENT
  60550. #ifndef WOLFSSL_NO_TLS12
  60551. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  60552. #endif
  60553. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  60554. #endif
  60555. #endif
  60556. #ifdef HAVE_SUPPORTED_CURVES
  60557. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  60558. #ifndef NO_WOLFSSL_CLIENT
  60559. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  60560. #endif
  60561. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  60562. #ifndef NO_WOLFSSL_CLIENT
  60563. #ifndef WOLFSSL_NO_TLS12
  60564. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  60565. WOLFSSL_SUCCESS);
  60566. #endif
  60567. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  60568. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  60569. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  60570. WOLFSSL_SUCCESS);
  60571. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  60572. BAD_FUNC_ARG);
  60573. #endif
  60574. #ifndef NO_WOLFSSL_SERVER
  60575. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  60576. WOLFSSL_SUCCESS);
  60577. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  60578. BAD_FUNC_ARG);
  60579. #endif
  60580. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  60581. #ifndef NO_WOLFSSL_CLIENT
  60582. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  60583. #endif
  60584. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  60585. #ifndef NO_WOLFSSL_CLIENT
  60586. #ifndef WOLFSSL_NO_TLS12
  60587. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  60588. WOLFSSL_SUCCESS);
  60589. #endif
  60590. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  60591. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  60592. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  60593. WOLFSSL_SUCCESS);
  60594. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  60595. BAD_FUNC_ARG);
  60596. #endif
  60597. #ifndef NO_WOLFSSL_SERVER
  60598. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  60599. WOLFSSL_SUCCESS);
  60600. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  60601. BAD_FUNC_ARG);
  60602. #endif
  60603. #ifdef OPENSSL_EXTRA
  60604. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  60605. #ifndef NO_WOLFSSL_CLIENT
  60606. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  60607. WOLFSSL_FAILURE);
  60608. #endif
  60609. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  60610. WOLFSSL_FAILURE);
  60611. #ifndef NO_WOLFSSL_CLIENT
  60612. #ifndef WOLFSSL_NO_TLS12
  60613. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  60614. WOLFSSL_SUCCESS);
  60615. #endif
  60616. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  60617. WOLFSSL_SUCCESS);
  60618. #endif
  60619. #ifndef NO_WOLFSSL_SERVER
  60620. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  60621. WOLFSSL_SUCCESS);
  60622. #endif
  60623. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  60624. #ifndef NO_WOLFSSL_CLIENT
  60625. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  60626. #endif
  60627. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  60628. #ifndef NO_WOLFSSL_CLIENT
  60629. #ifndef WOLFSSL_NO_TLS12
  60630. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  60631. WOLFSSL_SUCCESS);
  60632. #endif
  60633. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  60634. WOLFSSL_SUCCESS);
  60635. #endif
  60636. #ifndef NO_WOLFSSL_SERVER
  60637. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  60638. WOLFSSL_SUCCESS);
  60639. #endif
  60640. #endif /* OPENSSL_EXTRA */
  60641. #endif /* HAVE_SUPPORTED_CURVES */
  60642. #endif /* HAVE_ECC */
  60643. #ifdef WOLFSSL_EARLY_DATA
  60644. #ifndef OPENSSL_EXTRA
  60645. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  60646. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  60647. #else
  60648. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  60649. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  60650. #endif
  60651. #ifndef NO_WOLFSSL_CLIENT
  60652. #ifndef OPENSSL_EXTRA
  60653. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  60654. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  60655. #else
  60656. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  60657. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  60658. #endif
  60659. #endif
  60660. #ifndef NO_WOLFSSL_SERVER
  60661. #ifndef WOLFSSL_NO_TLS12
  60662. #ifndef OPENSSL_EXTRA
  60663. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  60664. BAD_FUNC_ARG);
  60665. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  60666. #else
  60667. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  60668. BAD_FUNC_ARG);
  60669. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  60670. #endif
  60671. #endif
  60672. #ifndef OPENSSL_EXTRA
  60673. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  60674. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  60675. WOLFSSL_SUCCESS);
  60676. #else
  60677. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  60678. #endif
  60679. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  60680. #else
  60681. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  60682. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  60683. #endif
  60684. #endif
  60685. #ifndef OPENSSL_EXTRA
  60686. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  60687. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  60688. #else
  60689. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  60690. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  60691. #endif
  60692. #ifndef NO_WOLFSSL_CLIENT
  60693. #ifndef OPENSSL_EXTRA
  60694. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  60695. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  60696. #else
  60697. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  60698. #endif
  60699. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  60700. #else
  60701. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  60702. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  60703. #endif
  60704. #endif
  60705. #ifndef NO_WOLFSSL_SERVER
  60706. #ifndef WOLFSSL_NO_TLS12
  60707. #ifndef OPENSSL_EXTRA
  60708. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  60709. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  60710. #else
  60711. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  60712. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  60713. #endif
  60714. #endif
  60715. #ifndef OPENSSL_EXTRA
  60716. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  60717. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  60718. #else
  60719. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  60720. #endif
  60721. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  60722. #else
  60723. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  60724. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  60725. #endif
  60726. #endif
  60727. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  60728. &outSz), BAD_FUNC_ARG);
  60729. #ifndef NO_WOLFSSL_CLIENT
  60730. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  60731. &outSz), BAD_FUNC_ARG);
  60732. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  60733. BAD_FUNC_ARG);
  60734. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  60735. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  60736. #endif
  60737. #ifndef NO_WOLFSSL_SERVER
  60738. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  60739. sizeof(earlyData), &outSz), SIDE_ERROR);
  60740. #endif
  60741. #ifndef NO_WOLFSSL_CLIENT
  60742. #ifndef WOLFSSL_NO_TLS12
  60743. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  60744. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  60745. #endif
  60746. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  60747. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  60748. #endif
  60749. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  60750. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  60751. #ifndef NO_WOLFSSL_SERVER
  60752. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  60753. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  60754. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  60755. &outSz), BAD_FUNC_ARG);
  60756. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  60757. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  60758. #endif
  60759. #ifndef NO_WOLFSSL_CLIENT
  60760. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  60761. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  60762. #endif
  60763. #ifndef NO_WOLFSSL_SERVER
  60764. #ifndef WOLFSSL_NO_TLS12
  60765. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  60766. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  60767. #endif
  60768. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  60769. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  60770. #endif
  60771. #endif
  60772. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  60773. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  60774. #endif
  60775. #ifndef NO_WOLFSSL_SERVER
  60776. wolfSSL_free(serverSsl);
  60777. wolfSSL_CTX_free(serverCtx);
  60778. #endif
  60779. #ifndef NO_WOLFSSL_CLIENT
  60780. wolfSSL_free(clientSsl);
  60781. wolfSSL_CTX_free(clientCtx);
  60782. #endif
  60783. #ifndef WOLFSSL_NO_TLS12
  60784. #ifndef NO_WOLFSSL_SERVER
  60785. wolfSSL_free(serverTls12Ssl);
  60786. wolfSSL_CTX_free(serverTls12Ctx);
  60787. #endif
  60788. #ifndef NO_WOLFSSL_CLIENT
  60789. wolfSSL_free(clientTls12Ssl);
  60790. wolfSSL_CTX_free(clientTls12Ctx);
  60791. #endif
  60792. #endif
  60793. return EXPECT_RESULT();
  60794. }
  60795. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  60796. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  60797. defined(BUILD_TLS_AES_256_GCM_SHA384)
  60798. /* Called when writing. */
  60799. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  60800. {
  60801. (void)ssl;
  60802. (void)buf;
  60803. (void)sz;
  60804. (void)ctx;
  60805. /* Force error return from wolfSSL_accept_TLSv13(). */
  60806. return WANT_WRITE;
  60807. }
  60808. /* Called when reading. */
  60809. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  60810. {
  60811. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  60812. int len = (int)msg->length;
  60813. (void)ssl;
  60814. (void)sz;
  60815. /* Pass back as much of message as will fit in buffer. */
  60816. if (len > sz)
  60817. len = sz;
  60818. XMEMCPY(buf, msg->buffer, len);
  60819. /* Move over returned data. */
  60820. msg->buffer += len;
  60821. msg->length -= len;
  60822. /* Amount actually copied. */
  60823. return len;
  60824. }
  60825. #endif
  60826. static int test_tls13_cipher_suites(void)
  60827. {
  60828. EXPECT_DECLS;
  60829. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  60830. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  60831. defined(BUILD_TLS_AES_256_GCM_SHA384)
  60832. WOLFSSL_CTX* ctx = NULL;
  60833. WOLFSSL *ssl = NULL;
  60834. int i;
  60835. byte clientHello[] = {
  60836. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  60837. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  60838. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  60839. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  60840. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  60841. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  60842. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  60843. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  60844. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60845. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  60846. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  60847. 0x13, 0x01,
  60848. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  60849. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  60850. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  60851. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  60852. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  60853. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  60854. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  60855. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  60856. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  60857. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  60858. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  60859. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  60860. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  60861. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  60862. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  60863. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  60864. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  60865. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  60866. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  60867. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  60868. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  60869. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  60870. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  60871. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  60872. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  60873. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  60874. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  60875. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  60876. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  60877. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  60878. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  60879. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  60880. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  60881. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  60882. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  60883. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60884. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60885. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  60886. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  60887. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  60888. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  60889. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  60890. };
  60891. WOLFSSL_BUFFER_INFO msg;
  60892. /* Offset into ClientHello message data of first cipher suite. */
  60893. const int csOff = 78;
  60894. /* Server cipher list. */
  60895. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  60896. /* Suite list with duplicates. */
  60897. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  60898. "TLS13-AES128-GCM-SHA256:"
  60899. "TLS13-AES256-GCM-SHA384:"
  60900. "TLS13-AES256-GCM-SHA384:"
  60901. "TLS13-AES128-GCM-SHA256";
  60902. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  60903. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  60904. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  60905. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  60906. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  60907. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  60908. #endif
  60909. /* Set up wolfSSL context. */
  60910. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  60911. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  60912. WOLFSSL_FILETYPE_PEM));
  60913. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  60914. WOLFSSL_FILETYPE_PEM));
  60915. /* Read from 'msg'. */
  60916. wolfSSL_SetIORecv(ctx, CsRecv);
  60917. /* No where to send to - dummy sender. */
  60918. wolfSSL_SetIOSend(ctx, CsSend);
  60919. /* Test cipher suite list with many copies of a cipher suite. */
  60920. ExpectNotNull(ssl = wolfSSL_new(ctx));
  60921. msg.buffer = clientHello;
  60922. msg.length = (unsigned int)sizeof(clientHello);
  60923. wolfSSL_SetIOReadCtx(ssl, &msg);
  60924. /* Force server to have as many occurrences of same cipher suite as
  60925. * possible. */
  60926. if (ssl != NULL) {
  60927. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  60928. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  60929. for (i = 0; i < suites->suiteSz; i += 2) {
  60930. suites->suites[i + 0] = TLS13_BYTE;
  60931. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  60932. }
  60933. }
  60934. /* Test multiple occurrences of same cipher suite. */
  60935. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  60936. wolfSSL_free(ssl);
  60937. ssl = NULL;
  60938. /* Set client order opposite to server order:
  60939. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  60940. clientHello[csOff + 0] = TLS13_BYTE;
  60941. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  60942. clientHello[csOff + 2] = TLS13_BYTE;
  60943. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  60944. /* Test server order negotiation. */
  60945. ExpectNotNull(ssl = wolfSSL_new(ctx));
  60946. msg.buffer = clientHello;
  60947. msg.length = (unsigned int)sizeof(clientHello);
  60948. wolfSSL_SetIOReadCtx(ssl, &msg);
  60949. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  60950. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  60951. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  60952. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  60953. /* Check refined order - server order. */
  60954. ExpectIntEQ(ssl->suites->suiteSz, 4);
  60955. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  60956. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  60957. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  60958. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  60959. wolfSSL_free(ssl);
  60960. ssl = NULL;
  60961. /* Test client order negotiation. */
  60962. ExpectNotNull(ssl = wolfSSL_new(ctx));
  60963. msg.buffer = clientHello;
  60964. msg.length = (unsigned int)sizeof(clientHello);
  60965. wolfSSL_SetIOReadCtx(ssl, &msg);
  60966. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  60967. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  60968. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  60969. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  60970. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  60971. /* Check refined order - client order. */
  60972. ExpectIntEQ(ssl->suites->suiteSz, 4);
  60973. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  60974. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  60975. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  60976. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  60977. wolfSSL_free(ssl);
  60978. ssl = NULL;
  60979. /* Check duplicate detection is working. */
  60980. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  60981. ExpectIntEQ(ctx->suites->suiteSz, 4);
  60982. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  60983. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  60984. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  60985. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  60986. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  60987. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  60988. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  60989. ExpectIntEQ(ctx->suites->suiteSz, 4);
  60990. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  60991. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  60992. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  60993. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  60994. #endif
  60995. wolfSSL_CTX_free(ctx);
  60996. #endif
  60997. return EXPECT_RESULT();
  60998. }
  60999. #endif
  61000. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  61001. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  61002. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  61003. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61004. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  61005. const unsigned char* priv, unsigned int privSz,
  61006. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  61007. unsigned char* out, unsigned int* outlen,
  61008. void* ctx)
  61009. {
  61010. int result;
  61011. /* Test fail when context associated with WOLFSSL is NULL */
  61012. if (ctx == NULL) {
  61013. return -1;
  61014. }
  61015. (void)ssl;
  61016. /* return 0 on success */
  61017. PRIVATE_KEY_UNLOCK();
  61018. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  61019. PRIVATE_KEY_LOCK();
  61020. return result;
  61021. }
  61022. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  61023. EXPECT_DECLS;
  61024. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  61025. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  61026. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  61027. WOLFSSL_SUCCESS);
  61028. #endif
  61029. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  61030. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  61031. WOLFSSL_SUCCESS);
  61032. #endif
  61033. return EXPECT_RESULT();
  61034. }
  61035. static int test_dh_ssl_setup(WOLFSSL* ssl)
  61036. {
  61037. EXPECT_DECLS;
  61038. static int dh_test_ctx = 1;
  61039. int ret;
  61040. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  61041. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  61042. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  61043. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  61044. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  61045. }
  61046. return EXPECT_RESULT();
  61047. }
  61048. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  61049. {
  61050. EXPECT_DECLS;
  61051. int ret;
  61052. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  61053. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  61054. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  61055. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  61056. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  61057. }
  61058. return EXPECT_RESULT();
  61059. }
  61060. #endif
  61061. static int test_DhCallbacks(void)
  61062. {
  61063. EXPECT_DECLS;
  61064. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  61065. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  61066. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61067. WOLFSSL_CTX *ctx = NULL;
  61068. WOLFSSL *ssl = NULL;
  61069. int test;
  61070. test_ssl_cbf func_cb_client;
  61071. test_ssl_cbf func_cb_server;
  61072. /* Test that DH callback APIs work. */
  61073. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  61074. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  61075. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  61076. /* load client ca cert */
  61077. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  61078. WOLFSSL_SUCCESS);
  61079. /* test with NULL arguments */
  61080. wolfSSL_SetDhAgreeCtx(NULL, &test);
  61081. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  61082. /* test success case */
  61083. test = 1;
  61084. ExpectNotNull(ssl = wolfSSL_new(ctx));
  61085. wolfSSL_SetDhAgreeCtx(ssl, &test);
  61086. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  61087. wolfSSL_free(ssl);
  61088. wolfSSL_CTX_free(ctx);
  61089. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61090. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61091. /* set callbacks to use DH functions */
  61092. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  61093. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  61094. func_cb_client.method = wolfTLSv1_2_client_method;
  61095. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  61096. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  61097. func_cb_server.method = wolfTLSv1_2_server_method;
  61098. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  61099. &func_cb_server, NULL), TEST_SUCCESS);
  61100. /* Test fail */
  61101. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61102. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61103. /* set callbacks to use DH functions */
  61104. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  61105. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  61106. func_cb_client.method = wolfTLSv1_2_client_method;
  61107. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  61108. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  61109. func_cb_server.method = wolfTLSv1_2_server_method;
  61110. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  61111. &func_cb_server, NULL), TEST_FAIL);
  61112. #endif
  61113. return EXPECT_RESULT();
  61114. }
  61115. #endif /* HAVE_PK_CALLBACKS */
  61116. #ifdef HAVE_HASHDRBG
  61117. #ifdef TEST_RESEED_INTERVAL
  61118. static int test_wc_RNG_GenerateBlock_Reseed(void)
  61119. {
  61120. EXPECT_DECLS;
  61121. int i;
  61122. WC_RNG rng;
  61123. byte key[32];
  61124. XMEMSET(&rng, 0, sizeof(WC_RNG));
  61125. ExpectIntEQ(wc_InitRng(&rng), 0);
  61126. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  61127. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  61128. }
  61129. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  61130. return EXPECT_RESULT();
  61131. }
  61132. #endif /* TEST_RESEED_INTERVAL */
  61133. static int test_wc_RNG_GenerateBlock(void)
  61134. {
  61135. EXPECT_DECLS;
  61136. int i;
  61137. WC_RNG rng;
  61138. byte key[32];
  61139. XMEMSET(&rng, 0, sizeof(WC_RNG));
  61140. ExpectIntEQ(wc_InitRng(&rng), 0);
  61141. for (i = 0; i < 10; i++) {
  61142. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  61143. }
  61144. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  61145. return EXPECT_RESULT();
  61146. }
  61147. #endif /* HAVE_HASHDRBG */
  61148. /*
  61149. * Testing get_rand_digit
  61150. */
  61151. static int test_get_rand_digit(void)
  61152. {
  61153. EXPECT_DECLS;
  61154. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  61155. WC_RNG rng;
  61156. mp_digit d;
  61157. XMEMSET(&rng, 0, sizeof(WC_RNG));
  61158. ExpectIntEQ(wc_InitRng(&rng), 0);
  61159. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  61160. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  61161. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  61162. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  61163. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  61164. #endif
  61165. return EXPECT_RESULT();
  61166. } /* End test_get_rand_digit*/
  61167. /*
  61168. * Testing get_digit_count
  61169. */
  61170. static int test_get_digit_count(void)
  61171. {
  61172. EXPECT_DECLS;
  61173. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  61174. mp_int a;
  61175. XMEMSET(&a, 0, sizeof(mp_int));
  61176. ExpectIntEQ(mp_init(&a), 0);
  61177. ExpectIntEQ(get_digit_count(NULL), 0);
  61178. ExpectIntEQ(get_digit_count(&a), 0);
  61179. mp_clear(&a);
  61180. #endif
  61181. return EXPECT_RESULT();
  61182. } /* End test_get_digit_count*/
  61183. /*
  61184. * Testing mp_cond_copy
  61185. */
  61186. static int test_mp_cond_copy(void)
  61187. {
  61188. EXPECT_DECLS;
  61189. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  61190. defined(WOLFSSL_PUBLIC_MP)
  61191. mp_int a;
  61192. mp_int b;
  61193. int copy = 0;
  61194. XMEMSET(&a, 0, sizeof(mp_int));
  61195. XMEMSET(&b, 0, sizeof(mp_int));
  61196. ExpectIntEQ(mp_init(&a), MP_OKAY);
  61197. ExpectIntEQ(mp_init(&b), MP_OKAY);
  61198. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  61199. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  61200. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  61201. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  61202. mp_clear(&a);
  61203. mp_clear(&b);
  61204. #endif
  61205. return EXPECT_RESULT();
  61206. } /* End test_mp_cond_copy*/
  61207. /*
  61208. * Testing mp_rand
  61209. */
  61210. static int test_mp_rand(void)
  61211. {
  61212. EXPECT_DECLS;
  61213. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  61214. mp_int a;
  61215. WC_RNG rng;
  61216. int digits = 1;
  61217. XMEMSET(&a, 0, sizeof(mp_int));
  61218. XMEMSET(&rng, 0, sizeof(WC_RNG));
  61219. ExpectIntEQ(mp_init(&a), MP_OKAY);
  61220. ExpectIntEQ(wc_InitRng(&rng), 0);
  61221. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  61222. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  61223. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  61224. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  61225. mp_clear(&a);
  61226. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  61227. #endif
  61228. return EXPECT_RESULT();
  61229. } /* End test_mp_rand*/
  61230. /*
  61231. * Testing get_digit
  61232. */
  61233. static int test_get_digit(void)
  61234. {
  61235. EXPECT_DECLS;
  61236. #if defined(WOLFSSL_PUBLIC_MP)
  61237. mp_int a;
  61238. int n = 0;
  61239. XMEMSET(&a, 0, sizeof(mp_int));
  61240. ExpectIntEQ(mp_init(&a), MP_OKAY);
  61241. ExpectIntEQ(get_digit(NULL, n), 0);
  61242. ExpectIntEQ(get_digit(&a, n), 0);
  61243. mp_clear(&a);
  61244. #endif
  61245. return EXPECT_RESULT();
  61246. } /* End test_get_digit*/
  61247. /*
  61248. * Testing wc_export_int
  61249. */
  61250. static int test_wc_export_int(void)
  61251. {
  61252. EXPECT_DECLS;
  61253. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  61254. defined(WOLFSSL_PUBLIC_MP)
  61255. mp_int mp;
  61256. byte buf[32];
  61257. word32 keySz = (word32)sizeof(buf);
  61258. word32 len = (word32)sizeof(buf);
  61259. XMEMSET(&mp, 0, sizeof(mp_int));
  61260. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  61261. ExpectIntEQ(mp_set(&mp, 1234), 0);
  61262. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  61263. BAD_FUNC_ARG);
  61264. len = sizeof(buf)-1;
  61265. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  61266. BUFFER_E);
  61267. len = sizeof(buf);
  61268. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  61269. len = 4; /* test input too small */
  61270. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  61271. len = sizeof(buf);
  61272. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  61273. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  61274. ExpectIntEQ(len, 5);
  61275. mp_clear(&mp);
  61276. #endif
  61277. return EXPECT_RESULT();
  61278. } /* End test_wc_export_int*/
  61279. static int test_wc_InitRngNonce(void)
  61280. {
  61281. EXPECT_DECLS;
  61282. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  61283. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  61284. HAVE_FIPS_VERSION >= 2))
  61285. WC_RNG rng;
  61286. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  61287. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  61288. word32 nonceSz = sizeof(nonce);
  61289. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  61290. ExpectIntEQ(wc_FreeRng(&rng), 0);
  61291. #endif
  61292. return EXPECT_RESULT();
  61293. } /* End test_wc_InitRngNonce*/
  61294. /*
  61295. * Testing wc_InitRngNonce_ex
  61296. */
  61297. static int test_wc_InitRngNonce_ex(void)
  61298. {
  61299. EXPECT_DECLS;
  61300. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  61301. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  61302. HAVE_FIPS_VERSION >= 2))
  61303. WC_RNG rng;
  61304. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  61305. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  61306. word32 nonceSz = sizeof(nonce);
  61307. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  61308. 0);
  61309. ExpectIntEQ(wc_FreeRng(&rng), 0);
  61310. #endif
  61311. return EXPECT_RESULT();
  61312. } /* End test_wc_InitRngNonce_ex */
  61313. static int test_wolfSSL_X509_CRL(void)
  61314. {
  61315. EXPECT_DECLS;
  61316. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  61317. X509_CRL *crl = NULL;
  61318. char pem[][100] = {
  61319. "./certs/crl/crl.pem",
  61320. "./certs/crl/crl2.pem",
  61321. "./certs/crl/caEccCrl.pem",
  61322. "./certs/crl/eccCliCRL.pem",
  61323. "./certs/crl/eccSrvCRL.pem",
  61324. ""
  61325. };
  61326. #ifndef NO_BIO
  61327. BIO *bio = NULL;
  61328. #endif
  61329. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  61330. char der[][100] = {
  61331. "./certs/crl/crl.der",
  61332. "./certs/crl/crl2.der",
  61333. ""};
  61334. #endif
  61335. XFILE fp = XBADFILE;
  61336. int i;
  61337. for (i = 0; pem[i][0] != '\0'; i++)
  61338. {
  61339. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  61340. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  61341. NULL, NULL));
  61342. ExpectNotNull(crl);
  61343. X509_CRL_free(crl);
  61344. if (fp != XBADFILE) {
  61345. XFCLOSE(fp);
  61346. fp = XBADFILE;
  61347. }
  61348. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  61349. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  61350. NULL));
  61351. if (EXPECT_FAIL()) {
  61352. crl = NULL;
  61353. }
  61354. ExpectNotNull(crl);
  61355. X509_CRL_free(crl);
  61356. crl = NULL;
  61357. if (fp != XBADFILE) {
  61358. XFCLOSE(fp);
  61359. fp = XBADFILE;
  61360. }
  61361. }
  61362. #ifndef NO_BIO
  61363. for (i = 0; pem[i][0] != '\0'; i++)
  61364. {
  61365. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  61366. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  61367. X509_CRL_free(crl);
  61368. crl = NULL;
  61369. BIO_free(bio);
  61370. bio = NULL;
  61371. }
  61372. #endif
  61373. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  61374. for (i = 0; der[i][0] != '\0'; i++) {
  61375. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  61376. ExpectTrue((fp != XBADFILE));
  61377. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  61378. ExpectNotNull(crl);
  61379. X509_CRL_free(crl);
  61380. if (fp != XBADFILE) {
  61381. XFCLOSE(fp);
  61382. fp = XBADFILE;
  61383. }
  61384. fp = XFOPEN(der[i], "rb");
  61385. ExpectTrue((fp != XBADFILE));
  61386. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  61387. if (EXPECT_FAIL()) {
  61388. crl = NULL;
  61389. }
  61390. ExpectNotNull(crl);
  61391. X509_CRL_free(crl);
  61392. crl = NULL;
  61393. if (fp != XBADFILE) {
  61394. XFCLOSE(fp);
  61395. fp = XBADFILE;
  61396. }
  61397. }
  61398. #endif
  61399. #endif
  61400. return EXPECT_RESULT();
  61401. }
  61402. static int test_wolfSSL_X509_load_crl_file(void)
  61403. {
  61404. EXPECT_DECLS;
  61405. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  61406. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO) && \
  61407. !defined(WOLFSSL_CRL_ALLOW_MISSING_CDP)
  61408. int i;
  61409. char pem[][100] = {
  61410. "./certs/crl/crl.pem",
  61411. "./certs/crl/crl2.pem",
  61412. "./certs/crl/caEccCrl.pem",
  61413. "./certs/crl/eccCliCRL.pem",
  61414. "./certs/crl/eccSrvCRL.pem",
  61415. #ifdef WC_RSA_PSS
  61416. "./certs/crl/crl_rsapss.pem",
  61417. #endif
  61418. ""
  61419. };
  61420. char der[][100] = {
  61421. "./certs/crl/crl.der",
  61422. "./certs/crl/crl2.der",
  61423. ""
  61424. };
  61425. WOLFSSL_X509_STORE* store = NULL;
  61426. WOLFSSL_X509_LOOKUP* lookup = NULL;
  61427. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  61428. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  61429. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  61430. X509_FILETYPE_PEM), 1);
  61431. #ifdef WC_RSA_PSS
  61432. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  61433. X509_FILETYPE_PEM), 1);
  61434. #endif
  61435. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  61436. X509_FILETYPE_PEM), 1);
  61437. if (store) {
  61438. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  61439. WOLFSSL_FILETYPE_PEM), 1);
  61440. /* since store hasn't yet known the revoked cert*/
  61441. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  61442. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  61443. }
  61444. for (i = 0; pem[i][0] != '\0'; i++) {
  61445. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  61446. 1);
  61447. }
  61448. if (store) {
  61449. /* since store knows crl list */
  61450. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  61451. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  61452. CRL_CERT_REVOKED);
  61453. #ifdef WC_RSA_PSS
  61454. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  61455. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  61456. CRL_CERT_REVOKED);
  61457. #endif
  61458. }
  61459. /* once feeing store */
  61460. X509_STORE_free(store);
  61461. store = NULL;
  61462. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  61463. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  61464. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  61465. X509_FILETYPE_PEM), 1);
  61466. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  61467. X509_FILETYPE_PEM), 1);
  61468. if (store) {
  61469. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  61470. WOLFSSL_FILETYPE_PEM), 1);
  61471. /* since store hasn't yet known the revoked cert*/
  61472. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  61473. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  61474. }
  61475. for (i = 0; der[i][0] != '\0'; i++) {
  61476. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  61477. 1);
  61478. }
  61479. if (store) {
  61480. /* since store knows crl list */
  61481. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  61482. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  61483. CRL_CERT_REVOKED);
  61484. }
  61485. /* test for incorrect parameter */
  61486. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  61487. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  61488. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  61489. X509_STORE_free(store);
  61490. store = NULL;
  61491. #endif
  61492. return EXPECT_RESULT();
  61493. }
  61494. static int test_wolfSSL_i2d_X509(void)
  61495. {
  61496. EXPECT_DECLS;
  61497. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  61498. const unsigned char* cert_buf = server_cert_der_2048;
  61499. unsigned char* out = NULL;
  61500. unsigned char* tmp = NULL;
  61501. X509* cert = NULL;
  61502. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  61503. /* Pointer should be advanced */
  61504. ExpectPtrGT(cert_buf, server_cert_der_2048);
  61505. ExpectIntGT(i2d_X509(cert, &out), 0);
  61506. ExpectNotNull(out);
  61507. tmp = out;
  61508. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  61509. ExpectPtrGT(tmp, out);
  61510. if (out != NULL)
  61511. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  61512. X509_free(cert);
  61513. #endif
  61514. return EXPECT_RESULT();
  61515. }
  61516. static int test_wolfSSL_d2i_X509_REQ(void)
  61517. {
  61518. EXPECT_DECLS;
  61519. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  61520. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  61521. !defined(WOLFSSL_SP_MATH)
  61522. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  61523. * were generated by libest
  61524. * ./certs/csr.attr.der contains sample attributes
  61525. * ./certs/csr.ext.der contains sample extensions */
  61526. const char* csrFile = "./certs/csr.signed.der";
  61527. const char* csrPopFile = "./certs/csr.attr.der";
  61528. const char* csrExtFile = "./certs/csr.ext.der";
  61529. /* ./certs/csr.dsa.pem is generated using
  61530. * openssl req -newkey dsa:certs/dsaparams.pem \
  61531. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  61532. * -outform PEM
  61533. * with the passphrase "wolfSSL"
  61534. */
  61535. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  61536. const char* csrDsaFile = "./certs/csr.dsa.pem";
  61537. XFILE f = XBADFILE;
  61538. #endif
  61539. BIO* bio = NULL;
  61540. X509* req = NULL;
  61541. EVP_PKEY *pub_key = NULL;
  61542. {
  61543. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  61544. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  61545. /*
  61546. * Extract the public key from the CSR
  61547. */
  61548. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  61549. /*
  61550. * Verify the signature in the CSR
  61551. */
  61552. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  61553. X509_free(req);
  61554. req = NULL;
  61555. BIO_free(bio);
  61556. bio = NULL;
  61557. EVP_PKEY_free(pub_key);
  61558. pub_key = NULL;
  61559. }
  61560. {
  61561. #ifdef OPENSSL_ALL
  61562. X509_ATTRIBUTE* attr = NULL;
  61563. ASN1_TYPE *at = NULL;
  61564. #endif
  61565. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  61566. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  61567. /*
  61568. * Extract the public key from the CSR
  61569. */
  61570. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  61571. /*
  61572. * Verify the signature in the CSR
  61573. */
  61574. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  61575. #ifdef OPENSSL_ALL
  61576. /*
  61577. * Obtain the challenge password from the CSR
  61578. */
  61579. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  61580. -1), 1);
  61581. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  61582. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  61583. ExpectNotNull(at->value.asn1_string);
  61584. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  61585. "2xIE+qqp/rhyTXP+");
  61586. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  61587. #endif
  61588. X509_free(req);
  61589. req = NULL;
  61590. BIO_free(bio);
  61591. bio = NULL;
  61592. EVP_PKEY_free(pub_key);
  61593. pub_key = NULL;
  61594. }
  61595. {
  61596. #ifdef OPENSSL_ALL
  61597. X509_ATTRIBUTE* attr = NULL;
  61598. ASN1_TYPE *at = NULL;
  61599. STACK_OF(X509_EXTENSION) *exts = NULL;
  61600. #endif
  61601. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  61602. /* This CSR contains an Extension Request attribute so
  61603. * we test extension parsing in a CSR attribute here. */
  61604. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  61605. /*
  61606. * Extract the public key from the CSR
  61607. */
  61608. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  61609. /*
  61610. * Verify the signature in the CSR
  61611. */
  61612. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  61613. #ifdef OPENSSL_ALL
  61614. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  61615. req));
  61616. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  61617. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  61618. /*
  61619. * Obtain the challenge password from the CSR
  61620. */
  61621. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  61622. -1), 0);
  61623. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  61624. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  61625. ExpectNotNull(at->value.asn1_string);
  61626. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  61627. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  61628. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  61629. #endif
  61630. X509_free(req);
  61631. req = NULL;
  61632. BIO_free(bio);
  61633. bio = NULL;
  61634. EVP_PKEY_free(pub_key);
  61635. pub_key = NULL;
  61636. }
  61637. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  61638. {
  61639. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  61640. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  61641. /*
  61642. * Extract the public key from the CSR
  61643. */
  61644. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  61645. /*
  61646. * Verify the signature in the CSR
  61647. */
  61648. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  61649. X509_free(req);
  61650. req = NULL;
  61651. BIO_free(bio);
  61652. /* Run the same test, but with a file pointer instead of a BIO.
  61653. * (PEM_read_X509_REQ)*/
  61654. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  61655. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  61656. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  61657. X509_free(req);
  61658. EVP_PKEY_free(pub_key);
  61659. }
  61660. #endif /* !NO_DSA && !HAVE_SELFTEST */
  61661. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  61662. return EXPECT_RESULT();
  61663. }
  61664. static int test_wolfSSL_PEM_read_X509(void)
  61665. {
  61666. EXPECT_DECLS;
  61667. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  61668. !defined(NO_RSA)
  61669. X509 *x509 = NULL;
  61670. XFILE fp = XBADFILE;
  61671. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  61672. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  61673. X509_free(x509);
  61674. if (fp != XBADFILE)
  61675. XFCLOSE(fp);
  61676. #endif
  61677. return EXPECT_RESULT();
  61678. }
  61679. static int test_wolfSSL_PEM_read(void)
  61680. {
  61681. EXPECT_DECLS;
  61682. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  61683. const char* filename = "./certs/server-keyEnc.pem";
  61684. XFILE fp = XBADFILE;
  61685. char* name = NULL;
  61686. char* header = NULL;
  61687. byte* data = NULL;
  61688. long len;
  61689. EVP_CIPHER_INFO cipher;
  61690. WOLFSSL_BIO* bio = NULL;
  61691. byte* fileData = NULL;
  61692. size_t fileDataSz = 0;
  61693. byte* out;
  61694. ExpectNotNull(bio = BIO_new_file(filename, "rb"));
  61695. ExpectIntEQ(PEM_read_bio(bio, NULL, &header, &data, &len), 0);
  61696. ExpectIntEQ(PEM_read_bio(bio, &name, NULL, &data, &len), 0);
  61697. ExpectIntEQ(PEM_read_bio(bio, &name, &header, NULL, &len), 0);
  61698. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, NULL), 0);
  61699. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  61700. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  61701. ExpectIntGT(XSTRLEN(header), 0);
  61702. ExpectIntGT(len, 0);
  61703. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61704. name = NULL;
  61705. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61706. header = NULL;
  61707. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61708. data = NULL;
  61709. BIO_free(bio);
  61710. bio = NULL;
  61711. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  61712. /* Fail cases. */
  61713. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  61714. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  61715. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  61716. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  61717. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  61718. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  61719. ExpectIntGT(XSTRLEN(header), 0);
  61720. ExpectIntGT(len, 0);
  61721. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  61722. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  61723. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  61724. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  61725. DYNAMIC_TYPE_TMP_BUFFER));
  61726. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  61727. if (fp != XBADFILE) {
  61728. XFCLOSE(fp);
  61729. fp = XBADFILE;
  61730. }
  61731. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  61732. /* Fail cases. */
  61733. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  61734. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  61735. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  61736. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  61737. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  61738. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  61739. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  61740. /* Fail cases. */
  61741. ExpectIntEQ(PEM_write(XBADFILE, name, header, data, len), 0);
  61742. ExpectIntEQ(PEM_write(stderr, NULL, header, data, len), 0);
  61743. ExpectIntEQ(PEM_write(stderr, name, NULL, data, len), 0);
  61744. ExpectIntEQ(PEM_write(stderr, name, header, NULL, len), 0);
  61745. /* Pass case */
  61746. ExpectIntEQ(PEM_write(stderr, name, header, data, len), fileDataSz);
  61747. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61748. name = NULL;
  61749. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61750. header = NULL;
  61751. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61752. data = NULL;
  61753. /* Read out of a fixed buffer BIO - forces malloc in PEM_read_bio. */
  61754. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  61755. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  61756. ExpectIntGT(XSTRLEN(header), 0);
  61757. ExpectIntGT(len, 0);
  61758. /* Fail cases. */
  61759. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  61760. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  61761. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  61762. #ifndef NO_DES3
  61763. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  61764. #endif
  61765. /* Fail cases. */
  61766. ExpectIntEQ(PEM_do_header(NULL, data, &len, PasswordCallBack,
  61767. (void*)"yassl123"), WOLFSSL_FAILURE);
  61768. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  61769. (void*)"yassl123"), WOLFSSL_FAILURE);
  61770. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  61771. (void*)"yassl123"), WOLFSSL_FAILURE);
  61772. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  61773. (void*)"yassl123"), WOLFSSL_FAILURE);
  61774. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NoPasswordCallBack,
  61775. (void*)"yassl123"), WOLFSSL_FAILURE);
  61776. #if !defined(NO_DES3) && !defined(NO_MD5)
  61777. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  61778. (void*)"yassl123"), WOLFSSL_SUCCESS);
  61779. #else
  61780. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  61781. (void*)"yassl123"), WOLFSSL_FAILURE);
  61782. #endif
  61783. BIO_free(bio);
  61784. bio = NULL;
  61785. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61786. fileData = NULL;
  61787. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61788. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61789. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61790. name = NULL;
  61791. header = NULL;
  61792. data = NULL;
  61793. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  61794. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  61795. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  61796. ExpectIntEQ(XSTRLEN(header), 0);
  61797. ExpectIntGT(len, 0);
  61798. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  61799. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  61800. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  61801. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  61802. DYNAMIC_TYPE_TMP_BUFFER));
  61803. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  61804. if (fp != XBADFILE)
  61805. XFCLOSE(fp);
  61806. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  61807. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  61808. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  61809. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  61810. BIO_free(bio);
  61811. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61812. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61813. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61814. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  61815. #endif
  61816. return EXPECT_RESULT();
  61817. }
  61818. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  61819. {
  61820. EXPECT_DECLS;
  61821. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  61822. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  61823. const byte iv[12] = { 0 };
  61824. const byte key[16] = { 0 };
  61825. const byte cleartext[16] = { 0 };
  61826. const byte aad[] = {
  61827. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  61828. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  61829. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  61830. 0x4f
  61831. };
  61832. byte out1Part[16];
  61833. byte outTag1Part[16];
  61834. byte out2Part[16];
  61835. byte outTag2Part[16];
  61836. byte decryptBuf[16];
  61837. int len = 0;
  61838. int tlen;
  61839. EVP_CIPHER_CTX* ctx = NULL;
  61840. /* ENCRYPT */
  61841. /* Send AAD and data in 1 part */
  61842. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  61843. tlen = 0;
  61844. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  61845. 1);
  61846. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  61847. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  61848. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  61849. sizeof(cleartext)), 1);
  61850. tlen += len;
  61851. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  61852. tlen += len;
  61853. ExpectIntEQ(tlen, sizeof(cleartext));
  61854. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  61855. outTag1Part), 1);
  61856. EVP_CIPHER_CTX_free(ctx);
  61857. ctx = NULL;
  61858. /* DECRYPT */
  61859. /* Send AAD and data in 1 part */
  61860. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  61861. tlen = 0;
  61862. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  61863. 1);
  61864. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  61865. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  61866. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  61867. sizeof(cleartext)), 1);
  61868. tlen += len;
  61869. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  61870. outTag1Part), 1);
  61871. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  61872. tlen += len;
  61873. ExpectIntEQ(tlen, sizeof(cleartext));
  61874. EVP_CIPHER_CTX_free(ctx);
  61875. ctx = NULL;
  61876. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  61877. /* ENCRYPT */
  61878. /* Send AAD and data in 2 parts */
  61879. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  61880. tlen = 0;
  61881. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  61882. 1);
  61883. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  61884. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  61885. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  61886. 1);
  61887. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  61888. tlen += len;
  61889. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  61890. sizeof(cleartext) - 1), 1);
  61891. tlen += len;
  61892. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  61893. tlen += len;
  61894. ExpectIntEQ(tlen, sizeof(cleartext));
  61895. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  61896. outTag2Part), 1);
  61897. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  61898. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  61899. EVP_CIPHER_CTX_free(ctx);
  61900. ctx = NULL;
  61901. /* DECRYPT */
  61902. /* Send AAD and data in 2 parts */
  61903. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  61904. tlen = 0;
  61905. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  61906. 1);
  61907. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  61908. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  61909. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  61910. 1);
  61911. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  61912. tlen += len;
  61913. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  61914. sizeof(cleartext) - 1), 1);
  61915. tlen += len;
  61916. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  61917. outTag1Part), 1);
  61918. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  61919. tlen += len;
  61920. ExpectIntEQ(tlen, sizeof(cleartext));
  61921. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  61922. /* Test AAD reuse */
  61923. EVP_CIPHER_CTX_free(ctx);
  61924. #endif
  61925. return EXPECT_RESULT();
  61926. }
  61927. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  61928. {
  61929. EXPECT_DECLS;
  61930. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  61931. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  61932. /* Zero length plain text */
  61933. byte key[] = {
  61934. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  61935. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  61936. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  61937. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  61938. }; /* align */
  61939. byte iv[] = {
  61940. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  61941. }; /* align */
  61942. byte plaintxt[1];
  61943. int ivSz = 12;
  61944. int plaintxtSz = 0;
  61945. unsigned char tag[16];
  61946. unsigned char tag_kat[] = {
  61947. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  61948. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  61949. };
  61950. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  61951. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  61952. int ciphertxtSz = 0;
  61953. int decryptedtxtSz = 0;
  61954. int len = 0;
  61955. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  61956. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  61957. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  61958. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  61959. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  61960. plaintxtSz));
  61961. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  61962. ciphertxtSz += len;
  61963. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  61964. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  61965. ExpectIntEQ(0, ciphertxtSz);
  61966. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  61967. EVP_CIPHER_CTX_init(de);
  61968. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  61969. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  61970. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  61971. decryptedtxtSz = len;
  61972. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  61973. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  61974. decryptedtxtSz += len;
  61975. ExpectIntEQ(0, decryptedtxtSz);
  61976. EVP_CIPHER_CTX_free(en);
  61977. EVP_CIPHER_CTX_free(de);
  61978. #endif
  61979. return EXPECT_RESULT();
  61980. }
  61981. static int test_wolfssl_EVP_aes_gcm(void)
  61982. {
  61983. EXPECT_DECLS;
  61984. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  61985. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  61986. /* A 256 bit key, AES_128 will use the first 128 bit*/
  61987. byte *key = (byte*)"01234567890123456789012345678901";
  61988. /* A 128 bit IV */
  61989. byte *iv = (byte*)"0123456789012345";
  61990. int ivSz = AES_BLOCK_SIZE;
  61991. /* Message to be encrypted */
  61992. byte *plaintxt = (byte*)"for things to change you have to change";
  61993. /* Additional non-confidential data */
  61994. byte *aad = (byte*)"Don't spend major time on minor things.";
  61995. unsigned char tag[AES_BLOCK_SIZE] = {0};
  61996. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  61997. int aadSz = (int)XSTRLEN((char*)aad);
  61998. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  61999. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  62000. int ciphertxtSz = 0;
  62001. int decryptedtxtSz = 0;
  62002. int len = 0;
  62003. int i = 0;
  62004. EVP_CIPHER_CTX en[2];
  62005. EVP_CIPHER_CTX de[2];
  62006. for (i = 0; i < 2; i++) {
  62007. EVP_CIPHER_CTX_init(&en[i]);
  62008. if (i == 0) {
  62009. /* Default uses 96-bits IV length */
  62010. #ifdef WOLFSSL_AES_128
  62011. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  62012. key, iv));
  62013. #elif defined(WOLFSSL_AES_192)
  62014. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  62015. key, iv));
  62016. #elif defined(WOLFSSL_AES_256)
  62017. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  62018. key, iv));
  62019. #endif
  62020. }
  62021. else {
  62022. #ifdef WOLFSSL_AES_128
  62023. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  62024. NULL, NULL));
  62025. #elif defined(WOLFSSL_AES_192)
  62026. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  62027. NULL, NULL));
  62028. #elif defined(WOLFSSL_AES_256)
  62029. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  62030. NULL, NULL));
  62031. #endif
  62032. /* non-default must to set the IV length first */
  62033. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  62034. ivSz, NULL));
  62035. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  62036. }
  62037. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  62038. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  62039. plaintxtSz));
  62040. ciphertxtSz = len;
  62041. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  62042. ciphertxtSz += len;
  62043. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  62044. AES_BLOCK_SIZE, tag));
  62045. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  62046. EVP_CIPHER_CTX_init(&de[i]);
  62047. if (i == 0) {
  62048. /* Default uses 96-bits IV length */
  62049. #ifdef WOLFSSL_AES_128
  62050. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  62051. key, iv));
  62052. #elif defined(WOLFSSL_AES_192)
  62053. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  62054. key, iv));
  62055. #elif defined(WOLFSSL_AES_256)
  62056. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  62057. key, iv));
  62058. #endif
  62059. }
  62060. else {
  62061. #ifdef WOLFSSL_AES_128
  62062. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  62063. NULL, NULL));
  62064. #elif defined(WOLFSSL_AES_192)
  62065. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  62066. NULL, NULL));
  62067. #elif defined(WOLFSSL_AES_256)
  62068. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  62069. NULL, NULL));
  62070. #endif
  62071. /* non-default must to set the IV length first */
  62072. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  62073. ivSz, NULL));
  62074. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  62075. }
  62076. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  62077. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  62078. ciphertxtSz));
  62079. decryptedtxtSz = len;
  62080. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  62081. AES_BLOCK_SIZE, tag));
  62082. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  62083. decryptedtxtSz += len;
  62084. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  62085. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  62086. /* modify tag*/
  62087. if (i == 0) {
  62088. /* Default uses 96-bits IV length */
  62089. #ifdef WOLFSSL_AES_128
  62090. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  62091. key, iv));
  62092. #elif defined(WOLFSSL_AES_192)
  62093. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  62094. key, iv));
  62095. #elif defined(WOLFSSL_AES_256)
  62096. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  62097. key, iv));
  62098. #endif
  62099. }
  62100. else {
  62101. #ifdef WOLFSSL_AES_128
  62102. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  62103. NULL, NULL));
  62104. #elif defined(WOLFSSL_AES_192)
  62105. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  62106. NULL, NULL));
  62107. #elif defined(WOLFSSL_AES_256)
  62108. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  62109. NULL, NULL));
  62110. #endif
  62111. /* non-default must to set the IV length first */
  62112. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  62113. ivSz, NULL));
  62114. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  62115. }
  62116. tag[AES_BLOCK_SIZE-1]+=0xBB;
  62117. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  62118. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  62119. AES_BLOCK_SIZE, tag));
  62120. /* fail due to wrong tag */
  62121. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  62122. ciphertxtSz));
  62123. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  62124. ExpectIntEQ(0, len);
  62125. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  62126. }
  62127. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  62128. return EXPECT_RESULT();
  62129. }
  62130. static int test_wolfssl_EVP_aria_gcm(void)
  62131. {
  62132. int res = TEST_SKIPPED;
  62133. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  62134. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  62135. /* A 256 bit key, AES_128 will use the first 128 bit*/
  62136. byte *key = (byte*)"01234567890123456789012345678901";
  62137. /* A 128 bit IV */
  62138. byte *iv = (byte*)"0123456789012345";
  62139. int ivSz = ARIA_BLOCK_SIZE;
  62140. /* Message to be encrypted */
  62141. const int plaintxtSz = 40;
  62142. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  62143. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  62144. /* Additional non-confidential data */
  62145. byte *aad = (byte*)"Don't spend major time on minor things.";
  62146. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  62147. int aadSz = (int)XSTRLEN((char*)aad);
  62148. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  62149. byte decryptedtxt[plaintxtSz];
  62150. int ciphertxtSz = 0;
  62151. int decryptedtxtSz = 0;
  62152. int len = 0;
  62153. int i = 0;
  62154. #define TEST_ARIA_GCM_COUNT 6
  62155. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  62156. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  62157. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  62158. EVP_CIPHER_CTX_init(&en[i]);
  62159. switch (i) {
  62160. case 0:
  62161. /* Default uses 96-bits IV length */
  62162. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  62163. break;
  62164. case 1:
  62165. /* Default uses 96-bits IV length */
  62166. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  62167. break;
  62168. case 2:
  62169. /* Default uses 96-bits IV length */
  62170. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  62171. break;
  62172. case 3:
  62173. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  62174. /* non-default must to set the IV length first */
  62175. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  62176. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  62177. break;
  62178. case 4:
  62179. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  62180. /* non-default must to set the IV length first */
  62181. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  62182. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  62183. break;
  62184. case 5:
  62185. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  62186. /* non-default must to set the IV length first */
  62187. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  62188. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  62189. break;
  62190. }
  62191. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  62192. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  62193. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  62194. ciphertxtSz = len;
  62195. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  62196. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  62197. ciphertxtSz += len;
  62198. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  62199. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  62200. EVP_CIPHER_CTX_init(&de[i]);
  62201. switch (i) {
  62202. case 0:
  62203. /* Default uses 96-bits IV length */
  62204. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  62205. break;
  62206. case 1:
  62207. /* Default uses 96-bits IV length */
  62208. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  62209. break;
  62210. case 2:
  62211. /* Default uses 96-bits IV length */
  62212. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  62213. break;
  62214. case 3:
  62215. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  62216. /* non-default must to set the IV length first */
  62217. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  62218. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  62219. break;
  62220. case 4:
  62221. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  62222. /* non-default must to set the IV length first */
  62223. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  62224. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  62225. break;
  62226. case 5:
  62227. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  62228. /* non-default must to set the IV length first */
  62229. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  62230. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  62231. break;
  62232. }
  62233. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  62234. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  62235. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  62236. decryptedtxtSz = len;
  62237. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  62238. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  62239. decryptedtxtSz += len;
  62240. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  62241. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  62242. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  62243. /* modify tag*/
  62244. tag[AES_BLOCK_SIZE-1]+=0xBB;
  62245. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  62246. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  62247. /* fail due to wrong tag */
  62248. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  62249. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  62250. AssertIntEQ(0, len);
  62251. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  62252. }
  62253. res = TEST_RES_CHECK(1);
  62254. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  62255. return res;
  62256. }
  62257. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  62258. {
  62259. EXPECT_DECLS;
  62260. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  62261. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  62262. /* Zero length plain text */
  62263. byte key[] = {
  62264. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62265. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62266. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62267. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  62268. }; /* align */
  62269. byte iv[] = {
  62270. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  62271. }; /* align */
  62272. byte plaintxt[1];
  62273. int ivSz = 12;
  62274. int plaintxtSz = 0;
  62275. unsigned char tag[16];
  62276. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  62277. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  62278. int ciphertxtSz = 0;
  62279. int decryptedtxtSz = 0;
  62280. int len = 0;
  62281. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  62282. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  62283. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  62284. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  62285. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  62286. plaintxtSz));
  62287. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  62288. ciphertxtSz += len;
  62289. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  62290. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  62291. ExpectIntEQ(0, ciphertxtSz);
  62292. EVP_CIPHER_CTX_init(de);
  62293. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  62294. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  62295. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  62296. decryptedtxtSz = len;
  62297. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  62298. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  62299. decryptedtxtSz += len;
  62300. ExpectIntEQ(0, decryptedtxtSz);
  62301. EVP_CIPHER_CTX_free(en);
  62302. EVP_CIPHER_CTX_free(de);
  62303. #endif
  62304. return EXPECT_RESULT();
  62305. }
  62306. static int test_wolfssl_EVP_aes_ccm(void)
  62307. {
  62308. EXPECT_DECLS;
  62309. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  62310. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  62311. /* A 256 bit key, AES_128 will use the first 128 bit*/
  62312. byte *key = (byte*)"01234567890123456789012345678901";
  62313. /* A 128 bit IV */
  62314. byte *iv = (byte*)"0123456789012";
  62315. int ivSz = (int)XSTRLEN((char*)iv);
  62316. /* Message to be encrypted */
  62317. byte *plaintxt = (byte*)"for things to change you have to change";
  62318. /* Additional non-confidential data */
  62319. byte *aad = (byte*)"Don't spend major time on minor things.";
  62320. unsigned char tag[AES_BLOCK_SIZE] = {0};
  62321. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  62322. int aadSz = (int)XSTRLEN((char*)aad);
  62323. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  62324. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  62325. int ciphertxtSz = 0;
  62326. int decryptedtxtSz = 0;
  62327. int len = 0;
  62328. int i = 0;
  62329. int ret;
  62330. EVP_CIPHER_CTX en[2];
  62331. EVP_CIPHER_CTX de[2];
  62332. for (i = 0; i < 2; i++) {
  62333. EVP_CIPHER_CTX_init(&en[i]);
  62334. if (i == 0) {
  62335. /* Default uses 96-bits IV length */
  62336. #ifdef WOLFSSL_AES_128
  62337. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  62338. key, iv));
  62339. #elif defined(WOLFSSL_AES_192)
  62340. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  62341. key, iv));
  62342. #elif defined(WOLFSSL_AES_256)
  62343. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  62344. key, iv));
  62345. #endif
  62346. }
  62347. else {
  62348. #ifdef WOLFSSL_AES_128
  62349. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  62350. NULL, NULL));
  62351. #elif defined(WOLFSSL_AES_192)
  62352. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  62353. NULL, NULL));
  62354. #elif defined(WOLFSSL_AES_256)
  62355. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  62356. NULL, NULL));
  62357. #endif
  62358. /* non-default must to set the IV length first */
  62359. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  62360. ivSz, NULL));
  62361. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  62362. }
  62363. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  62364. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  62365. plaintxtSz));
  62366. ciphertxtSz = len;
  62367. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  62368. ciphertxtSz += len;
  62369. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  62370. AES_BLOCK_SIZE, tag));
  62371. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  62372. ExpectIntEQ(ret, 1);
  62373. EVP_CIPHER_CTX_init(&de[i]);
  62374. if (i == 0) {
  62375. /* Default uses 96-bits IV length */
  62376. #ifdef WOLFSSL_AES_128
  62377. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  62378. key, iv));
  62379. #elif defined(WOLFSSL_AES_192)
  62380. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  62381. key, iv));
  62382. #elif defined(WOLFSSL_AES_256)
  62383. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  62384. key, iv));
  62385. #endif
  62386. }
  62387. else {
  62388. #ifdef WOLFSSL_AES_128
  62389. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  62390. NULL, NULL));
  62391. #elif defined(WOLFSSL_AES_192)
  62392. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  62393. NULL, NULL));
  62394. #elif defined(WOLFSSL_AES_256)
  62395. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  62396. NULL, NULL));
  62397. #endif
  62398. /* non-default must to set the IV length first */
  62399. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  62400. ivSz, NULL));
  62401. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  62402. }
  62403. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  62404. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  62405. ciphertxtSz));
  62406. decryptedtxtSz = len;
  62407. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  62408. AES_BLOCK_SIZE, tag));
  62409. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  62410. decryptedtxtSz += len;
  62411. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  62412. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  62413. /* modify tag*/
  62414. tag[AES_BLOCK_SIZE-1]+=0xBB;
  62415. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  62416. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  62417. AES_BLOCK_SIZE, tag));
  62418. /* fail due to wrong tag */
  62419. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  62420. ciphertxtSz));
  62421. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  62422. ExpectIntEQ(0, len);
  62423. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  62424. ExpectIntEQ(ret, 1);
  62425. }
  62426. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  62427. return EXPECT_RESULT();
  62428. }
  62429. static int test_wolfssl_EVP_chacha20_poly1305(void)
  62430. {
  62431. EXPECT_DECLS;
  62432. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  62433. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  62434. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  62435. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  62436. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  62437. byte cipherText[sizeof(plainText)];
  62438. byte decryptedText[sizeof(plainText)];
  62439. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  62440. EVP_CIPHER_CTX* ctx = NULL;
  62441. int outSz;
  62442. /* Encrypt. */
  62443. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62444. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  62445. NULL), WOLFSSL_SUCCESS);
  62446. /* Invalid IV length. */
  62447. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  62448. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  62449. /* Valid IV length. */
  62450. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  62451. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  62452. /* Invalid tag length. */
  62453. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  62454. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  62455. /* Valid tag length. */
  62456. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  62457. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  62458. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  62459. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  62460. WOLFSSL_SUCCESS);
  62461. ExpectIntEQ(outSz, sizeof(aad));
  62462. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  62463. sizeof(plainText)), WOLFSSL_SUCCESS);
  62464. ExpectIntEQ(outSz, sizeof(plainText));
  62465. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  62466. ExpectIntEQ(outSz, 0);
  62467. /* Invalid tag length. */
  62468. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  62469. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  62470. /* Valid tag length. */
  62471. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  62472. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  62473. EVP_CIPHER_CTX_free(ctx);
  62474. ctx = NULL;
  62475. /* Decrypt. */
  62476. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62477. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  62478. NULL), WOLFSSL_SUCCESS);
  62479. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  62480. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  62481. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  62482. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  62483. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  62484. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  62485. WOLFSSL_SUCCESS);
  62486. ExpectIntEQ(outSz, sizeof(aad));
  62487. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  62488. sizeof(cipherText)), WOLFSSL_SUCCESS);
  62489. ExpectIntEQ(outSz, sizeof(cipherText));
  62490. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  62491. WOLFSSL_SUCCESS);
  62492. ExpectIntEQ(outSz, 0);
  62493. EVP_CIPHER_CTX_free(ctx);
  62494. ctx = NULL;
  62495. /* Test partial Inits. CipherInit() allow setting of key and iv
  62496. * in separate calls. */
  62497. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62498. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  62499. key, NULL, 1), WOLFSSL_SUCCESS);
  62500. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  62501. WOLFSSL_SUCCESS);
  62502. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  62503. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  62504. ExpectIntEQ(outSz, sizeof(aad));
  62505. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  62506. sizeof(cipherText)), WOLFSSL_SUCCESS);
  62507. ExpectIntEQ(outSz, sizeof(cipherText));
  62508. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  62509. WOLFSSL_SUCCESS);
  62510. ExpectIntEQ(outSz, 0);
  62511. EVP_CIPHER_CTX_free(ctx);
  62512. #endif
  62513. return EXPECT_RESULT();
  62514. }
  62515. static int test_wolfssl_EVP_chacha20(void)
  62516. {
  62517. EXPECT_DECLS;
  62518. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  62519. byte key[CHACHA_MAX_KEY_SZ];
  62520. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  62521. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  62522. byte cipherText[sizeof(plainText)];
  62523. byte decryptedText[sizeof(plainText)];
  62524. EVP_CIPHER_CTX* ctx = NULL;
  62525. int outSz;
  62526. XMEMSET(key, 0, sizeof(key));
  62527. XMEMSET(iv, 0, sizeof(iv));
  62528. /* Encrypt. */
  62529. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62530. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  62531. NULL), WOLFSSL_SUCCESS);
  62532. /* Any tag length must fail - not an AEAD cipher. */
  62533. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  62534. 16, NULL), WOLFSSL_FAILURE);
  62535. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  62536. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  62537. sizeof(plainText)), WOLFSSL_SUCCESS);
  62538. ExpectIntEQ(outSz, sizeof(plainText));
  62539. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  62540. ExpectIntEQ(outSz, 0);
  62541. EVP_CIPHER_CTX_free(ctx);
  62542. ctx = NULL;
  62543. /* Decrypt. */
  62544. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62545. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  62546. NULL), WOLFSSL_SUCCESS);
  62547. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  62548. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  62549. sizeof(cipherText)), WOLFSSL_SUCCESS);
  62550. ExpectIntEQ(outSz, sizeof(cipherText));
  62551. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  62552. WOLFSSL_SUCCESS);
  62553. ExpectIntEQ(outSz, 0);
  62554. EVP_CIPHER_CTX_free(ctx);
  62555. ctx = NULL;
  62556. /* Test partial Inits. CipherInit() allow setting of key and iv
  62557. * in separate calls. */
  62558. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62559. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  62560. key, NULL, 1), WOLFSSL_SUCCESS);
  62561. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  62562. WOLFSSL_SUCCESS);
  62563. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  62564. sizeof(cipherText)), WOLFSSL_SUCCESS);
  62565. ExpectIntEQ(outSz, sizeof(cipherText));
  62566. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  62567. WOLFSSL_SUCCESS);
  62568. ExpectIntEQ(outSz, 0);
  62569. EVP_CIPHER_CTX_free(ctx);
  62570. #endif
  62571. return EXPECT_RESULT();
  62572. }
  62573. static int test_wolfssl_EVP_sm4_ecb(void)
  62574. {
  62575. int res = TEST_SKIPPED;
  62576. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  62577. EXPECT_DECLS;
  62578. byte key[SM4_KEY_SIZE];
  62579. byte plainText[SM4_BLOCK_SIZE] = {
  62580. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  62581. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  62582. };
  62583. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  62584. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  62585. EVP_CIPHER_CTX* ctx;
  62586. int outSz;
  62587. XMEMSET(key, 0, sizeof(key));
  62588. /* Encrypt. */
  62589. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62590. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  62591. WOLFSSL_SUCCESS);
  62592. /* Any tag length must fail - not an AEAD cipher. */
  62593. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  62594. WOLFSSL_FAILURE);
  62595. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  62596. WOLFSSL_SUCCESS);
  62597. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  62598. sizeof(plainText)), WOLFSSL_SUCCESS);
  62599. ExpectIntEQ(outSz, sizeof(plainText));
  62600. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  62601. WOLFSSL_SUCCESS);
  62602. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  62603. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  62604. EVP_CIPHER_CTX_free(ctx);
  62605. /* Decrypt. */
  62606. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62607. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  62608. WOLFSSL_SUCCESS);
  62609. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  62610. WOLFSSL_SUCCESS);
  62611. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  62612. sizeof(cipherText)), WOLFSSL_SUCCESS);
  62613. ExpectIntEQ(outSz, sizeof(plainText));
  62614. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  62615. WOLFSSL_SUCCESS);
  62616. ExpectIntEQ(outSz, 0);
  62617. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  62618. EVP_CIPHER_CTX_free(ctx);
  62619. res = EXPECT_RESULT();
  62620. #endif
  62621. return res;
  62622. }
  62623. static int test_wolfssl_EVP_sm4_cbc(void)
  62624. {
  62625. int res = TEST_SKIPPED;
  62626. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  62627. EXPECT_DECLS;
  62628. byte key[SM4_KEY_SIZE];
  62629. byte iv[SM4_BLOCK_SIZE];
  62630. byte plainText[SM4_BLOCK_SIZE] = {
  62631. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  62632. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  62633. };
  62634. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  62635. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  62636. EVP_CIPHER_CTX* ctx;
  62637. int outSz;
  62638. XMEMSET(key, 0, sizeof(key));
  62639. XMEMSET(iv, 0, sizeof(iv));
  62640. /* Encrypt. */
  62641. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62642. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  62643. WOLFSSL_SUCCESS);
  62644. /* Any tag length must fail - not an AEAD cipher. */
  62645. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  62646. WOLFSSL_FAILURE);
  62647. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  62648. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  62649. sizeof(plainText)), WOLFSSL_SUCCESS);
  62650. ExpectIntEQ(outSz, sizeof(plainText));
  62651. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  62652. WOLFSSL_SUCCESS);
  62653. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  62654. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  62655. EVP_CIPHER_CTX_free(ctx);
  62656. /* Decrypt. */
  62657. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62658. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  62659. WOLFSSL_SUCCESS);
  62660. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  62661. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  62662. sizeof(cipherText)), WOLFSSL_SUCCESS);
  62663. ExpectIntEQ(outSz, sizeof(plainText));
  62664. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  62665. WOLFSSL_SUCCESS);
  62666. ExpectIntEQ(outSz, 0);
  62667. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  62668. EVP_CIPHER_CTX_free(ctx);
  62669. /* Test partial Inits. CipherInit() allow setting of key and iv
  62670. * in separate calls. */
  62671. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62672. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  62673. WOLFSSL_SUCCESS);
  62674. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  62675. WOLFSSL_SUCCESS);
  62676. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  62677. sizeof(cipherText)), WOLFSSL_SUCCESS);
  62678. ExpectIntEQ(outSz, sizeof(plainText));
  62679. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  62680. WOLFSSL_SUCCESS);
  62681. ExpectIntEQ(outSz, 0);
  62682. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  62683. EVP_CIPHER_CTX_free(ctx);
  62684. res = EXPECT_RESULT();
  62685. #endif
  62686. return res;
  62687. }
  62688. static int test_wolfssl_EVP_sm4_ctr(void)
  62689. {
  62690. int res = TEST_SKIPPED;
  62691. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  62692. EXPECT_DECLS;
  62693. byte key[SM4_KEY_SIZE];
  62694. byte iv[SM4_BLOCK_SIZE];
  62695. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  62696. byte cipherText[sizeof(plainText)];
  62697. byte decryptedText[sizeof(plainText)];
  62698. EVP_CIPHER_CTX* ctx;
  62699. int outSz;
  62700. XMEMSET(key, 0, sizeof(key));
  62701. XMEMSET(iv, 0, sizeof(iv));
  62702. /* Encrypt. */
  62703. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62704. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  62705. WOLFSSL_SUCCESS);
  62706. /* Any tag length must fail - not an AEAD cipher. */
  62707. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  62708. WOLFSSL_FAILURE);
  62709. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  62710. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  62711. sizeof(plainText)), WOLFSSL_SUCCESS);
  62712. ExpectIntEQ(outSz, sizeof(plainText));
  62713. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  62714. ExpectIntEQ(outSz, 0);
  62715. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  62716. EVP_CIPHER_CTX_free(ctx);
  62717. /* Decrypt. */
  62718. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62719. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  62720. WOLFSSL_SUCCESS);
  62721. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  62722. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  62723. sizeof(cipherText)), WOLFSSL_SUCCESS);
  62724. ExpectIntEQ(outSz, sizeof(cipherText));
  62725. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  62726. WOLFSSL_SUCCESS);
  62727. ExpectIntEQ(outSz, 0);
  62728. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  62729. EVP_CIPHER_CTX_free(ctx);
  62730. /* Test partial Inits. CipherInit() allow setting of key and iv
  62731. * in separate calls. */
  62732. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  62733. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  62734. WOLFSSL_SUCCESS);
  62735. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  62736. WOLFSSL_SUCCESS);
  62737. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  62738. sizeof(cipherText)), WOLFSSL_SUCCESS);
  62739. ExpectIntEQ(outSz, sizeof(cipherText));
  62740. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  62741. WOLFSSL_SUCCESS);
  62742. ExpectIntEQ(outSz, 0);
  62743. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  62744. EVP_CIPHER_CTX_free(ctx);
  62745. res = EXPECT_RESULT();
  62746. #endif
  62747. return res;
  62748. }
  62749. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  62750. {
  62751. int res = TEST_SKIPPED;
  62752. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  62753. /* Zero length plain text */
  62754. EXPECT_DECLS;
  62755. byte key[] = {
  62756. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62757. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62758. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62759. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  62760. }; /* align */
  62761. byte iv[] = {
  62762. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  62763. }; /* align */
  62764. byte plaintxt[1];
  62765. int ivSz = 12;
  62766. int plaintxtSz = 0;
  62767. unsigned char tag[16];
  62768. unsigned char tag_kat[16] = {
  62769. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  62770. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  62771. };
  62772. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  62773. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  62774. int ciphertxtSz = 0;
  62775. int decryptedtxtSz = 0;
  62776. int len = 0;
  62777. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  62778. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  62779. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  62780. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  62781. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  62782. plaintxtSz));
  62783. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  62784. ciphertxtSz += len;
  62785. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  62786. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  62787. ExpectIntEQ(0, ciphertxtSz);
  62788. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  62789. EVP_CIPHER_CTX_init(de);
  62790. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  62791. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  62792. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  62793. decryptedtxtSz = len;
  62794. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  62795. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  62796. decryptedtxtSz += len;
  62797. ExpectIntEQ(0, decryptedtxtSz);
  62798. EVP_CIPHER_CTX_free(en);
  62799. EVP_CIPHER_CTX_free(de);
  62800. res = EXPECT_RESULT();
  62801. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  62802. return res;
  62803. }
  62804. static int test_wolfssl_EVP_sm4_gcm(void)
  62805. {
  62806. int res = TEST_SKIPPED;
  62807. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  62808. EXPECT_DECLS;
  62809. byte *key = (byte*)"0123456789012345";
  62810. /* A 128 bit IV */
  62811. byte *iv = (byte*)"0123456789012345";
  62812. int ivSz = SM4_BLOCK_SIZE;
  62813. /* Message to be encrypted */
  62814. byte *plaintxt = (byte*)"for things to change you have to change";
  62815. /* Additional non-confidential data */
  62816. byte *aad = (byte*)"Don't spend major time on minor things.";
  62817. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  62818. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  62819. int aadSz = (int)XSTRLEN((char*)aad);
  62820. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  62821. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  62822. int ciphertxtSz = 0;
  62823. int decryptedtxtSz = 0;
  62824. int len = 0;
  62825. int i = 0;
  62826. EVP_CIPHER_CTX en[2];
  62827. EVP_CIPHER_CTX de[2];
  62828. for (i = 0; i < 2; i++) {
  62829. EVP_CIPHER_CTX_init(&en[i]);
  62830. if (i == 0) {
  62831. /* Default uses 96-bits IV length */
  62832. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  62833. iv));
  62834. }
  62835. else {
  62836. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  62837. NULL));
  62838. /* non-default must to set the IV length first */
  62839. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  62840. ivSz, NULL));
  62841. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  62842. }
  62843. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  62844. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  62845. plaintxtSz));
  62846. ciphertxtSz = len;
  62847. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  62848. ciphertxtSz += len;
  62849. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  62850. SM4_BLOCK_SIZE, tag));
  62851. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  62852. EVP_CIPHER_CTX_init(&de[i]);
  62853. if (i == 0) {
  62854. /* Default uses 96-bits IV length */
  62855. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  62856. iv));
  62857. }
  62858. else {
  62859. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  62860. NULL));
  62861. /* non-default must to set the IV length first */
  62862. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  62863. ivSz, NULL));
  62864. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  62865. }
  62866. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  62867. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  62868. ciphertxtSz));
  62869. decryptedtxtSz = len;
  62870. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  62871. SM4_BLOCK_SIZE, tag));
  62872. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  62873. decryptedtxtSz += len;
  62874. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  62875. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  62876. /* modify tag*/
  62877. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  62878. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  62879. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  62880. SM4_BLOCK_SIZE, tag));
  62881. /* fail due to wrong tag */
  62882. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  62883. ciphertxtSz));
  62884. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  62885. ExpectIntEQ(0, len);
  62886. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  62887. }
  62888. res = EXPECT_RESULT();
  62889. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  62890. return res;
  62891. }
  62892. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  62893. {
  62894. int res = TEST_SKIPPED;
  62895. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  62896. /* Zero length plain text */
  62897. EXPECT_DECLS;
  62898. byte key[] = {
  62899. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62900. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62901. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62902. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  62903. }; /* align */
  62904. byte iv[] = {
  62905. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  62906. }; /* align */
  62907. byte plaintxt[1];
  62908. int ivSz = 12;
  62909. int plaintxtSz = 0;
  62910. unsigned char tag[16];
  62911. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  62912. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  62913. int ciphertxtSz = 0;
  62914. int decryptedtxtSz = 0;
  62915. int len = 0;
  62916. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  62917. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  62918. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  62919. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  62920. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  62921. plaintxtSz));
  62922. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  62923. ciphertxtSz += len;
  62924. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  62925. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  62926. ExpectIntEQ(0, ciphertxtSz);
  62927. EVP_CIPHER_CTX_init(de);
  62928. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  62929. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  62930. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  62931. decryptedtxtSz = len;
  62932. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  62933. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  62934. decryptedtxtSz += len;
  62935. ExpectIntEQ(0, decryptedtxtSz);
  62936. EVP_CIPHER_CTX_free(en);
  62937. EVP_CIPHER_CTX_free(de);
  62938. res = EXPECT_RESULT();
  62939. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  62940. return res;
  62941. }
  62942. static int test_wolfssl_EVP_sm4_ccm(void)
  62943. {
  62944. int res = TEST_SKIPPED;
  62945. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  62946. EXPECT_DECLS;
  62947. byte *key = (byte*)"0123456789012345";
  62948. byte *iv = (byte*)"0123456789012";
  62949. int ivSz = (int)XSTRLEN((char*)iv);
  62950. /* Message to be encrypted */
  62951. byte *plaintxt = (byte*)"for things to change you have to change";
  62952. /* Additional non-confidential data */
  62953. byte *aad = (byte*)"Don't spend major time on minor things.";
  62954. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  62955. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  62956. int aadSz = (int)XSTRLEN((char*)aad);
  62957. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  62958. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  62959. int ciphertxtSz = 0;
  62960. int decryptedtxtSz = 0;
  62961. int len = 0;
  62962. int i = 0;
  62963. EVP_CIPHER_CTX en[2];
  62964. EVP_CIPHER_CTX de[2];
  62965. for (i = 0; i < 2; i++) {
  62966. EVP_CIPHER_CTX_init(&en[i]);
  62967. if (i == 0) {
  62968. /* Default uses 96-bits IV length */
  62969. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  62970. iv));
  62971. }
  62972. else {
  62973. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  62974. NULL));
  62975. /* non-default must to set the IV length first */
  62976. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  62977. ivSz, NULL));
  62978. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  62979. }
  62980. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  62981. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  62982. plaintxtSz));
  62983. ciphertxtSz = len;
  62984. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  62985. ciphertxtSz += len;
  62986. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  62987. SM4_BLOCK_SIZE, tag));
  62988. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  62989. EVP_CIPHER_CTX_init(&de[i]);
  62990. if (i == 0) {
  62991. /* Default uses 96-bits IV length */
  62992. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  62993. iv));
  62994. }
  62995. else {
  62996. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  62997. NULL));
  62998. /* non-default must to set the IV length first */
  62999. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  63000. ivSz, NULL));
  63001. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  63002. }
  63003. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  63004. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  63005. ciphertxtSz));
  63006. decryptedtxtSz = len;
  63007. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  63008. SM4_BLOCK_SIZE, tag));
  63009. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  63010. decryptedtxtSz += len;
  63011. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  63012. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  63013. /* modify tag*/
  63014. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  63015. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  63016. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  63017. SM4_BLOCK_SIZE, tag));
  63018. /* fail due to wrong tag */
  63019. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  63020. ciphertxtSz));
  63021. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  63022. ExpectIntEQ(0, len);
  63023. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  63024. }
  63025. res = EXPECT_RESULT();
  63026. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  63027. return res;
  63028. }
  63029. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  63030. {
  63031. EXPECT_DECLS;
  63032. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  63033. EVP_PKEY_CTX* ctx = NULL;
  63034. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  63035. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  63036. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  63037. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  63038. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  63039. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  63040. byte outKey[34];
  63041. size_t outKeySz = sizeof(outKey);
  63042. /* These expected outputs were gathered by running the same test below using
  63043. * OpenSSL. */
  63044. const byte extractAndExpand[] = {
  63045. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  63046. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  63047. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  63048. };
  63049. const byte extractOnly[] = {
  63050. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  63051. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  63052. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  63053. };
  63054. const byte expandOnly[] = {
  63055. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  63056. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  63057. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  63058. };
  63059. const byte extractAndExpandAddInfo[] = {
  63060. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  63061. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  63062. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  63063. };
  63064. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  63065. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  63066. /* NULL ctx. */
  63067. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  63068. /* NULL md. */
  63069. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  63070. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  63071. /* NULL ctx. */
  63072. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  63073. WOLFSSL_FAILURE);
  63074. /* NULL salt is ok. */
  63075. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  63076. WOLFSSL_SUCCESS);
  63077. /* Salt length <= 0. */
  63078. /* Length 0 salt is ok. */
  63079. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  63080. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  63081. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  63082. WOLFSSL_SUCCESS);
  63083. /* NULL ctx. */
  63084. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  63085. WOLFSSL_FAILURE);
  63086. /* NULL key. */
  63087. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  63088. WOLFSSL_FAILURE);
  63089. /* Key length <= 0 */
  63090. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  63091. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  63092. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  63093. WOLFSSL_SUCCESS);
  63094. /* NULL ctx. */
  63095. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  63096. WOLFSSL_FAILURE);
  63097. /* NULL info is ok. */
  63098. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  63099. WOLFSSL_SUCCESS);
  63100. /* Info length <= 0 */
  63101. /* Length 0 info is ok. */
  63102. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  63103. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  63104. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  63105. WOLFSSL_SUCCESS);
  63106. /* NULL ctx. */
  63107. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  63108. WOLFSSL_FAILURE);
  63109. /* Extract and expand (default). */
  63110. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  63111. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  63112. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  63113. /* Extract only. */
  63114. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  63115. WOLFSSL_SUCCESS);
  63116. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  63117. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  63118. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  63119. outKeySz = sizeof(outKey);
  63120. /* Expand only. */
  63121. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  63122. WOLFSSL_SUCCESS);
  63123. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  63124. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  63125. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  63126. outKeySz = sizeof(outKey);
  63127. /* Extract and expand with appended additional info. */
  63128. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  63129. WOLFSSL_SUCCESS);
  63130. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  63131. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  63132. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  63133. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  63134. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  63135. EVP_PKEY_CTX_free(ctx);
  63136. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  63137. return EXPECT_RESULT();
  63138. }
  63139. #ifndef NO_BIO
  63140. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  63141. {
  63142. EXPECT_DECLS;
  63143. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  63144. BIO* bio = NULL;
  63145. X509_INFO* info = NULL;
  63146. STACK_OF(X509_INFO)* sk = NULL;
  63147. char* subject = NULL;
  63148. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  63149. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  63150. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  63151. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  63152. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  63153. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  63154. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  63155. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  63156. /* using dereference to maintain testing for Apache port*/
  63157. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  63158. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  63159. 0, 0));
  63160. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  63161. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  63162. X509_INFO_free(info);
  63163. info = NULL;
  63164. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  63165. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  63166. 0, 0));
  63167. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  63168. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  63169. X509_INFO_free(info);
  63170. ExpectNull(info = sk_X509_INFO_pop(sk));
  63171. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  63172. BIO_free(bio);
  63173. #endif
  63174. return EXPECT_RESULT();
  63175. }
  63176. #endif /* !NO_BIO */
  63177. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  63178. {
  63179. EXPECT_DECLS;
  63180. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  63181. X509 *x509 = NULL;
  63182. X509_NAME* name = NULL;
  63183. int idx = 0;
  63184. X509_NAME_ENTRY *ne = NULL;
  63185. ASN1_OBJECT *object = NULL;
  63186. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  63187. WOLFSSL_FILETYPE_PEM));
  63188. ExpectNotNull(name = X509_get_subject_name(x509));
  63189. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  63190. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  63191. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  63192. X509_free(x509);
  63193. #endif
  63194. return EXPECT_RESULT();
  63195. }
  63196. static int test_wolfSSL_X509_STORE_get1_certs(void)
  63197. {
  63198. EXPECT_DECLS;
  63199. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  63200. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  63201. X509_STORE_CTX *storeCtx = NULL;
  63202. X509_STORE *store = NULL;
  63203. X509 *caX509 = NULL;
  63204. X509 *svrX509 = NULL;
  63205. X509_NAME *subject = NULL;
  63206. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  63207. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  63208. SSL_FILETYPE_PEM));
  63209. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  63210. SSL_FILETYPE_PEM)));
  63211. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  63212. ExpectNotNull(store = X509_STORE_new());
  63213. ExpectNotNull(subject = X509_get_subject_name(caX509));
  63214. /* Errors */
  63215. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  63216. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  63217. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  63218. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  63219. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  63220. SSL_SUCCESS);
  63221. /* Should find the cert */
  63222. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  63223. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  63224. sk_X509_pop_free(certs, NULL);
  63225. certs = NULL;
  63226. /* Should not find the cert */
  63227. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  63228. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  63229. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  63230. sk_X509_pop_free(certs, NULL);
  63231. certs = NULL;
  63232. X509_STORE_free(store);
  63233. X509_STORE_CTX_free(storeCtx);
  63234. X509_free(svrX509);
  63235. X509_free(caX509);
  63236. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  63237. return EXPECT_RESULT();
  63238. }
  63239. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  63240. defined(WOLFSSL_LOCAL_X509_STORE) && \
  63241. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  63242. static int test_wolfSSL_X509_STORE_set_get_crl_provider(X509_STORE_CTX* ctx,
  63243. X509_CRL** crl_out, X509* cert) {
  63244. X509_CRL *crl = NULL;
  63245. XFILE fp = XBADFILE;
  63246. char* cert_issuer = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
  63247. int ret = 0;
  63248. (void)ctx;
  63249. if (cert_issuer == NULL)
  63250. return 0;
  63251. if ((fp = XFOPEN("certs/crl/crl.pem", "rb")) != XBADFILE) {
  63252. PEM_read_X509_CRL(fp, &crl, NULL, NULL);
  63253. XFCLOSE(fp);
  63254. if (crl != NULL) {
  63255. char* crl_issuer = X509_NAME_oneline(
  63256. X509_CRL_get_issuer(crl), NULL, 0);
  63257. if ((crl_issuer != NULL) &&
  63258. (XSTRCMP(cert_issuer, crl_issuer) == 0)) {
  63259. *crl_out = X509_CRL_dup(crl);
  63260. if (*crl_out != NULL)
  63261. ret = 1;
  63262. }
  63263. OPENSSL_free(crl_issuer);
  63264. }
  63265. }
  63266. X509_CRL_free(crl);
  63267. OPENSSL_free(cert_issuer);
  63268. return ret;
  63269. }
  63270. static int test_wolfSSL_X509_STORE_set_get_crl_provider2(X509_STORE_CTX* ctx,
  63271. X509_CRL** crl_out, X509* cert) {
  63272. (void)ctx;
  63273. (void)cert;
  63274. *crl_out = NULL;
  63275. return 1;
  63276. }
  63277. #ifndef NO_WOLFSSL_STUB
  63278. static int test_wolfSSL_X509_STORE_set_get_crl_check(X509_STORE_CTX* ctx,
  63279. X509_CRL* crl) {
  63280. (void)ctx;
  63281. (void)crl;
  63282. return 1;
  63283. }
  63284. #endif
  63285. static int test_wolfSSL_X509_STORE_set_get_crl_verify(int ok,
  63286. X509_STORE_CTX* ctx) {
  63287. int cert_error = X509_STORE_CTX_get_error(ctx);
  63288. X509_VERIFY_PARAM* param = X509_STORE_CTX_get0_param(ctx);
  63289. int flags = X509_VERIFY_PARAM_get_flags(param);
  63290. if ((flags & (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) !=
  63291. (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) {
  63292. /* Make sure the flags are set */
  63293. return 0;
  63294. }
  63295. /* Ignore CRL missing error */
  63296. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  63297. if (cert_error == CRL_MISSING)
  63298. #else
  63299. if (cert_error == X509_V_ERR_UNABLE_TO_GET_CRL)
  63300. #endif
  63301. return 1;
  63302. return ok;
  63303. }
  63304. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready(WOLFSSL_CTX* ctx)
  63305. {
  63306. EXPECT_DECLS;
  63307. X509_STORE* cert_store = NULL;
  63308. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  63309. WOLFSSL_SUCCESS);
  63310. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  63311. X509_STORE_set_get_crl(cert_store,
  63312. test_wolfSSL_X509_STORE_set_get_crl_provider);
  63313. #ifndef NO_WOLFSSL_STUB
  63314. X509_STORE_set_check_crl(cert_store,
  63315. test_wolfSSL_X509_STORE_set_get_crl_check);
  63316. #endif
  63317. return EXPECT_RESULT();
  63318. }
  63319. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2(WOLFSSL_CTX* ctx)
  63320. {
  63321. EXPECT_DECLS;
  63322. X509_STORE* cert_store = NULL;
  63323. X509_VERIFY_PARAM* param = NULL;
  63324. SSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  63325. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  63326. WOLFSSL_SUCCESS);
  63327. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  63328. X509_STORE_set_get_crl(cert_store,
  63329. test_wolfSSL_X509_STORE_set_get_crl_provider2);
  63330. #ifndef NO_WOLFSSL_STUB
  63331. X509_STORE_set_check_crl(cert_store,
  63332. test_wolfSSL_X509_STORE_set_get_crl_check);
  63333. #endif
  63334. X509_STORE_set_verify_cb(cert_store,
  63335. test_wolfSSL_X509_STORE_set_get_crl_verify);
  63336. ExpectNotNull(param = X509_STORE_get0_param(cert_store));
  63337. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(
  63338. param, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  63339. ExpectIntEQ(X509_STORE_set_flags(cert_store,
  63340. X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  63341. return EXPECT_RESULT();
  63342. }
  63343. #endif
  63344. /* This test mimics the usage of the CRL provider in gRPC */
  63345. static int test_wolfSSL_X509_STORE_set_get_crl(void)
  63346. {
  63347. EXPECT_DECLS;
  63348. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  63349. defined(WOLFSSL_LOCAL_X509_STORE) && \
  63350. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  63351. test_ssl_cbf func_cb_client;
  63352. test_ssl_cbf func_cb_server;
  63353. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  63354. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  63355. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready;
  63356. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  63357. &func_cb_server, NULL), TEST_SUCCESS);
  63358. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  63359. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  63360. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2;
  63361. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  63362. &func_cb_server, NULL), TEST_SUCCESS);
  63363. #endif
  63364. return EXPECT_RESULT();
  63365. }
  63366. static int test_wolfSSL_dup_CA_list(void)
  63367. {
  63368. int res = TEST_SKIPPED;
  63369. #if defined(OPENSSL_ALL)
  63370. EXPECT_DECLS;
  63371. STACK_OF(X509_NAME) *originalStack = NULL;
  63372. STACK_OF(X509_NAME) *copyStack = NULL;
  63373. int originalCount = 0;
  63374. int copyCount = 0;
  63375. X509_NAME *name = NULL;
  63376. int i;
  63377. originalStack = sk_X509_NAME_new_null();
  63378. ExpectNotNull(originalStack);
  63379. for (i = 0; i < 3; i++) {
  63380. name = X509_NAME_new();
  63381. ExpectNotNull(name);
  63382. ExpectIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  63383. if (EXPECT_FAIL()) {
  63384. X509_NAME_free(name);
  63385. }
  63386. }
  63387. copyStack = SSL_dup_CA_list(originalStack);
  63388. ExpectNotNull(copyStack);
  63389. originalCount = sk_X509_NAME_num(originalStack);
  63390. copyCount = sk_X509_NAME_num(copyStack);
  63391. ExpectIntEQ(originalCount, copyCount);
  63392. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  63393. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  63394. originalStack = NULL;
  63395. copyStack = NULL;
  63396. res = EXPECT_RESULT();
  63397. #endif /* OPENSSL_ALL */
  63398. return res;
  63399. }
  63400. static int test_ForceZero(void)
  63401. {
  63402. EXPECT_DECLS;
  63403. unsigned char data[32];
  63404. unsigned int i, j, len;
  63405. /* Test case with 0 length */
  63406. ForceZero(data, 0);
  63407. /* Test ForceZero */
  63408. for (i = 0; i < sizeof(data); i++) {
  63409. for (len = 1; len < sizeof(data) - i; len++) {
  63410. for (j = 0; j < sizeof(data); j++)
  63411. data[j] = j + 1;
  63412. ForceZero(data + i, len);
  63413. for (j = 0; j < sizeof(data); j++) {
  63414. if (j < i || j >= i + len) {
  63415. ExpectIntNE(data[j], 0x00);
  63416. }
  63417. else {
  63418. ExpectIntEQ(data[j], 0x00);
  63419. }
  63420. }
  63421. }
  63422. }
  63423. return EXPECT_RESULT();
  63424. }
  63425. #ifndef NO_BIO
  63426. static int test_wolfSSL_X509_print(void)
  63427. {
  63428. EXPECT_DECLS;
  63429. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  63430. !defined(NO_RSA) && defined(XSNPRINTF)
  63431. X509 *x509 = NULL;
  63432. BIO *bio = NULL;
  63433. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  63434. const X509_ALGOR *cert_sig_alg = NULL;
  63435. #endif
  63436. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  63437. WOLFSSL_FILETYPE_PEM));
  63438. /* print to memory */
  63439. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  63440. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  63441. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  63442. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  63443. /* Will print IP address subject alt name. */
  63444. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  63445. #elif defined(NO_ASN_TIME)
  63446. /* Will print IP address subject alt name but not Validity. */
  63447. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  63448. #else
  63449. /* Will print IP address subject alt name. */
  63450. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  63451. #endif
  63452. #elif defined(NO_ASN_TIME)
  63453. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  63454. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  63455. #else
  63456. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  63457. #endif
  63458. BIO_free(bio);
  63459. bio = NULL;
  63460. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  63461. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  63462. /* Print signature */
  63463. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  63464. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  63465. #endif
  63466. /* print to stderr */
  63467. #if !defined(NO_WOLFSSL_DIR)
  63468. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  63469. #endif
  63470. /* print again */
  63471. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  63472. X509_free(x509);
  63473. BIO_free(bio);
  63474. #endif
  63475. return EXPECT_RESULT();
  63476. }
  63477. static int test_wolfSSL_X509_CRL_print(void)
  63478. {
  63479. EXPECT_DECLS;
  63480. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  63481. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  63482. X509_CRL* crl = NULL;
  63483. BIO *bio = NULL;
  63484. XFILE fp = XBADFILE;
  63485. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  63486. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  63487. NULL, NULL));
  63488. if (fp != XBADFILE)
  63489. XFCLOSE(fp);
  63490. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  63491. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  63492. X509_CRL_free(crl);
  63493. BIO_free(bio);
  63494. #endif
  63495. return EXPECT_RESULT();
  63496. }
  63497. static int test_wolfSSL_BIO_get_len(void)
  63498. {
  63499. EXPECT_DECLS;
  63500. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  63501. BIO *bio = NULL;
  63502. const char txt[] = "Some example text to push to the BIO.";
  63503. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  63504. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  63505. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  63506. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  63507. BIO_free(bio);
  63508. bio = NULL;
  63509. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  63510. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  63511. BIO_free(bio);
  63512. #endif
  63513. return EXPECT_RESULT();
  63514. }
  63515. #endif /* !NO_BIO */
  63516. static int test_wolfSSL_RSA(void)
  63517. {
  63518. EXPECT_DECLS;
  63519. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  63520. RSA* rsa = NULL;
  63521. const BIGNUM *n = NULL;
  63522. const BIGNUM *e = NULL;
  63523. const BIGNUM *d = NULL;
  63524. const BIGNUM *p = NULL;
  63525. const BIGNUM *q = NULL;
  63526. const BIGNUM *dmp1 = NULL;
  63527. const BIGNUM *dmq1 = NULL;
  63528. const BIGNUM *iqmp = NULL;
  63529. ExpectNotNull(rsa = RSA_new());
  63530. ExpectIntEQ(RSA_size(NULL), 0);
  63531. ExpectIntEQ(RSA_size(rsa), 0);
  63532. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  63533. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  63534. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  63535. #ifdef WOLFSSL_RSA_KEY_CHECK
  63536. ExpectIntEQ(RSA_check_key(rsa), 0);
  63537. #endif
  63538. RSA_free(rsa);
  63539. rsa = NULL;
  63540. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  63541. ExpectIntEQ(RSA_size(rsa), 256);
  63542. #ifdef WOLFSSL_RSA_KEY_CHECK
  63543. ExpectIntEQ(RSA_check_key(NULL), 0);
  63544. ExpectIntEQ(RSA_check_key(rsa), 1);
  63545. #endif
  63546. /* sanity check */
  63547. ExpectIntEQ(RSA_bits(NULL), 0);
  63548. /* key */
  63549. ExpectIntEQ(RSA_bits(rsa), 2048);
  63550. RSA_get0_key(rsa, &n, &e, &d);
  63551. ExpectPtrEq(rsa->n, n);
  63552. ExpectPtrEq(rsa->e, e);
  63553. ExpectPtrEq(rsa->d, d);
  63554. n = NULL;
  63555. e = NULL;
  63556. d = NULL;
  63557. ExpectNotNull(n = BN_new());
  63558. ExpectNotNull(e = BN_new());
  63559. ExpectNotNull(d = BN_new());
  63560. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  63561. if (EXPECT_FAIL()) {
  63562. BN_free((BIGNUM*)n);
  63563. BN_free((BIGNUM*)e);
  63564. BN_free((BIGNUM*)d);
  63565. }
  63566. ExpectPtrEq(rsa->n, n);
  63567. ExpectPtrEq(rsa->e, e);
  63568. ExpectPtrEq(rsa->d, d);
  63569. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  63570. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  63571. /* crt_params */
  63572. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  63573. ExpectPtrEq(rsa->dmp1, dmp1);
  63574. ExpectPtrEq(rsa->dmq1, dmq1);
  63575. ExpectPtrEq(rsa->iqmp, iqmp);
  63576. dmp1 = NULL;
  63577. dmq1 = NULL;
  63578. iqmp = NULL;
  63579. ExpectNotNull(dmp1 = BN_new());
  63580. ExpectNotNull(dmq1 = BN_new());
  63581. ExpectNotNull(iqmp = BN_new());
  63582. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  63583. (BIGNUM*)iqmp), 1);
  63584. if (EXPECT_FAIL()) {
  63585. BN_free((BIGNUM*)dmp1);
  63586. BN_free((BIGNUM*)dmq1);
  63587. BN_free((BIGNUM*)iqmp);
  63588. }
  63589. ExpectPtrEq(rsa->dmp1, dmp1);
  63590. ExpectPtrEq(rsa->dmq1, dmq1);
  63591. ExpectPtrEq(rsa->iqmp, iqmp);
  63592. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  63593. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  63594. (BIGNUM*)iqmp), 0);
  63595. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  63596. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  63597. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  63598. ExpectNull(dmp1);
  63599. ExpectNull(dmq1);
  63600. ExpectNull(iqmp);
  63601. /* factors */
  63602. RSA_get0_factors(rsa, NULL, NULL);
  63603. RSA_get0_factors(rsa, &p, &q);
  63604. ExpectPtrEq(rsa->p, p);
  63605. ExpectPtrEq(rsa->q, q);
  63606. p = NULL;
  63607. q = NULL;
  63608. ExpectNotNull(p = BN_new());
  63609. ExpectNotNull(q = BN_new());
  63610. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  63611. if (EXPECT_FAIL()) {
  63612. BN_free((BIGNUM*)p);
  63613. BN_free((BIGNUM*)q);
  63614. }
  63615. ExpectPtrEq(rsa->p, p);
  63616. ExpectPtrEq(rsa->q, q);
  63617. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  63618. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  63619. RSA_get0_factors(NULL, NULL, NULL);
  63620. RSA_get0_factors(NULL, &p, &q);
  63621. ExpectNull(p);
  63622. ExpectNull(q);
  63623. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  63624. ExpectIntEQ(RSA_bits(rsa), 21);
  63625. RSA_free(rsa);
  63626. rsa = NULL;
  63627. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  63628. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  63629. ExpectIntEQ(RSA_size(rsa), 384);
  63630. ExpectIntEQ(RSA_bits(rsa), 3072);
  63631. RSA_free(rsa);
  63632. rsa = NULL;
  63633. #endif
  63634. /* remove for now with odd key size until adjusting rsa key size check with
  63635. wc_MakeRsaKey()
  63636. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  63637. RSA_free(rsa);
  63638. rsa = NULL;
  63639. */
  63640. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  63641. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  63642. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  63643. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  63644. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  63645. {
  63646. byte buff[FOURK_BUF];
  63647. byte der[FOURK_BUF];
  63648. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  63649. XFILE f = XBADFILE;
  63650. int bytes = 0;
  63651. /* test loading encrypted RSA private pem w/o password */
  63652. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  63653. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  63654. if (f != XBADFILE)
  63655. XFCLOSE(f);
  63656. XMEMSET(der, 0, sizeof(der));
  63657. /* test that error value is returned with no password */
  63658. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  63659. 0);
  63660. }
  63661. #endif
  63662. #endif
  63663. return EXPECT_RESULT();
  63664. }
  63665. static int test_wolfSSL_RSA_DER(void)
  63666. {
  63667. EXPECT_DECLS;
  63668. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  63669. RSA *rsa = NULL;
  63670. int i;
  63671. const unsigned char *buff = NULL;
  63672. unsigned char *newBuff = NULL;
  63673. struct tbl_s
  63674. {
  63675. const unsigned char *der;
  63676. int sz;
  63677. } tbl[] = {
  63678. #ifdef USE_CERT_BUFFERS_1024
  63679. {client_key_der_1024, sizeof_client_key_der_1024},
  63680. {server_key_der_1024, sizeof_server_key_der_1024},
  63681. #endif
  63682. #ifdef USE_CERT_BUFFERS_2048
  63683. {client_key_der_2048, sizeof_client_key_der_2048},
  63684. {server_key_der_2048, sizeof_server_key_der_2048},
  63685. #endif
  63686. {NULL, 0}
  63687. };
  63688. /* Public Key DER */
  63689. struct tbl_s pub[] = {
  63690. #ifdef USE_CERT_BUFFERS_1024
  63691. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  63692. #endif
  63693. #ifdef USE_CERT_BUFFERS_2048
  63694. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  63695. #endif
  63696. {NULL, 0}
  63697. };
  63698. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  63699. buff = pub[0].der;
  63700. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  63701. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  63702. buff = tbl[0].der;
  63703. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  63704. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  63705. rsa = RSA_new();
  63706. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  63707. RSA_free(rsa);
  63708. rsa = NULL;
  63709. for (i = 0; tbl[i].der != NULL; i++)
  63710. {
  63711. /* Passing in pointer results in pointer moving. */
  63712. buff = tbl[i].der;
  63713. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  63714. ExpectNotNull(rsa);
  63715. RSA_free(rsa);
  63716. rsa = NULL;
  63717. }
  63718. for (i = 0; tbl[i].der != NULL; i++)
  63719. {
  63720. /* Passing in pointer results in pointer moving. */
  63721. buff = tbl[i].der;
  63722. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  63723. ExpectNotNull(rsa);
  63724. RSA_free(rsa);
  63725. rsa = NULL;
  63726. }
  63727. for (i = 0; pub[i].der != NULL; i++)
  63728. {
  63729. buff = pub[i].der;
  63730. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  63731. ExpectNotNull(rsa);
  63732. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  63733. newBuff = NULL;
  63734. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  63735. ExpectNotNull(newBuff);
  63736. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  63737. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  63738. RSA_free(rsa);
  63739. rsa = NULL;
  63740. }
  63741. #endif
  63742. return EXPECT_RESULT();
  63743. }
  63744. static int test_wolfSSL_RSA_print(void)
  63745. {
  63746. EXPECT_DECLS;
  63747. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  63748. !defined(NO_STDIO_FILESYSTEM) && \
  63749. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  63750. !defined(NO_BIO) && defined(XFPRINTF)
  63751. BIO *bio = NULL;
  63752. WOLFSSL_RSA* rsa = NULL;
  63753. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  63754. ExpectNotNull(rsa = RSA_new());
  63755. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  63756. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  63757. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  63758. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  63759. /* Some very large number of indent spaces. */
  63760. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  63761. /* RSA is empty. */
  63762. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  63763. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  63764. RSA_free(rsa);
  63765. rsa = NULL;
  63766. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  63767. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  63768. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  63769. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  63770. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  63771. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  63772. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  63773. BIO_free(bio);
  63774. RSA_free(rsa);
  63775. #endif
  63776. return EXPECT_RESULT();
  63777. }
  63778. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  63779. {
  63780. EXPECT_DECLS;
  63781. #ifndef NO_RSA
  63782. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  63783. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  63784. RSA *rsa = NULL;
  63785. const unsigned char *derBuf = client_key_der_2048;
  63786. unsigned char em[256] = {0}; /* len = 2048/8 */
  63787. /* Random data simulating a hash */
  63788. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  63789. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  63790. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  63791. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  63792. };
  63793. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  63794. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  63795. RSA_PSS_SALTLEN_DIGEST), 0);
  63796. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  63797. RSA_PSS_SALTLEN_DIGEST), 0);
  63798. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  63799. RSA_PSS_SALTLEN_DIGEST), 0);
  63800. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  63801. RSA_PSS_SALTLEN_DIGEST), 0);
  63802. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  63803. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  63804. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  63805. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  63806. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  63807. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  63808. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  63809. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  63810. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  63811. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  63812. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  63813. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  63814. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  63815. RSA_PSS_SALTLEN_DIGEST), 1);
  63816. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  63817. RSA_PSS_SALTLEN_DIGEST), 1);
  63818. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  63819. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  63820. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  63821. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  63822. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  63823. RSA_PSS_SALTLEN_MAX), 1);
  63824. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  63825. RSA_PSS_SALTLEN_MAX), 1);
  63826. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  63827. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  63828. RSA_free(rsa);
  63829. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  63830. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  63831. #endif
  63832. return EXPECT_RESULT();
  63833. }
  63834. static int test_wolfSSL_RSA_sign_sha3(void)
  63835. {
  63836. EXPECT_DECLS;
  63837. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  63838. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  63839. RSA* rsa = NULL;
  63840. const unsigned char *derBuf = client_key_der_2048;
  63841. unsigned char sigRet[256] = {0};
  63842. unsigned int sigLen = sizeof(sigRet);
  63843. /* Random data simulating a hash */
  63844. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  63845. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  63846. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  63847. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  63848. };
  63849. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  63850. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  63851. rsa), 1);
  63852. RSA_free(rsa);
  63853. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  63854. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  63855. return EXPECT_RESULT();
  63856. }
  63857. static int test_wolfSSL_RSA_get0_key(void)
  63858. {
  63859. EXPECT_DECLS;
  63860. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  63861. RSA *rsa = NULL;
  63862. const BIGNUM* n = NULL;
  63863. const BIGNUM* e = NULL;
  63864. const BIGNUM* d = NULL;
  63865. const unsigned char* der;
  63866. int derSz;
  63867. #ifdef USE_CERT_BUFFERS_1024
  63868. der = client_key_der_1024;
  63869. derSz = sizeof_client_key_der_1024;
  63870. #elif defined(USE_CERT_BUFFERS_2048)
  63871. der = client_key_der_2048;
  63872. derSz = sizeof_client_key_der_2048;
  63873. #else
  63874. der = NULL;
  63875. derSz = 0;
  63876. #endif
  63877. if (der != NULL) {
  63878. RSA_get0_key(NULL, NULL, NULL, NULL);
  63879. RSA_get0_key(rsa, NULL, NULL, NULL);
  63880. RSA_get0_key(NULL, &n, &e, &d);
  63881. ExpectNull(n);
  63882. ExpectNull(e);
  63883. ExpectNull(d);
  63884. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  63885. ExpectNotNull(rsa);
  63886. RSA_get0_key(rsa, NULL, NULL, NULL);
  63887. RSA_get0_key(rsa, &n, NULL, NULL);
  63888. ExpectNotNull(n);
  63889. RSA_get0_key(rsa, NULL, &e, NULL);
  63890. ExpectNotNull(e);
  63891. RSA_get0_key(rsa, NULL, NULL, &d);
  63892. ExpectNotNull(d);
  63893. RSA_get0_key(rsa, &n, &e, &d);
  63894. ExpectNotNull(n);
  63895. ExpectNotNull(e);
  63896. ExpectNotNull(d);
  63897. RSA_free(rsa);
  63898. }
  63899. #endif
  63900. return EXPECT_RESULT();
  63901. }
  63902. static int test_wolfSSL_RSA_meth(void)
  63903. {
  63904. EXPECT_DECLS;
  63905. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  63906. RSA *rsa = NULL;
  63907. RSA_METHOD *rsa_meth = NULL;
  63908. #ifdef WOLFSSL_KEY_GEN
  63909. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  63910. RSA_free(rsa);
  63911. rsa = NULL;
  63912. #else
  63913. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  63914. #endif
  63915. ExpectNotNull(RSA_get_default_method());
  63916. wolfSSL_RSA_meth_free(NULL);
  63917. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  63918. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  63919. RSA_METHOD_FLAG_NO_CHECK));
  63920. #ifndef NO_WOLFSSL_STUB
  63921. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  63922. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  63923. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  63924. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  63925. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  63926. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  63927. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  63928. #endif
  63929. ExpectIntEQ(RSA_flags(NULL), 0);
  63930. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  63931. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  63932. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  63933. ExpectNotNull(rsa = RSA_new());
  63934. /* No method set. */
  63935. ExpectIntEQ(RSA_flags(rsa), 0);
  63936. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  63937. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  63938. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  63939. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  63940. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  63941. if (EXPECT_FAIL()) {
  63942. wolfSSL_RSA_meth_free(rsa_meth);
  63943. }
  63944. ExpectNull(RSA_get_method(NULL));
  63945. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  63946. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  63947. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  63948. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  63949. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  63950. RSA_METHOD_FLAG_NO_CHECK);
  63951. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  63952. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  63953. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  63954. /* rsa_meth is freed here */
  63955. RSA_free(rsa);
  63956. #endif
  63957. return EXPECT_RESULT();
  63958. }
  63959. static int test_wolfSSL_RSA_verify(void)
  63960. {
  63961. EXPECT_DECLS;
  63962. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  63963. #ifndef NO_BIO
  63964. XFILE fp = XBADFILE;
  63965. RSA *pKey = NULL;
  63966. RSA *pubKey = NULL;
  63967. X509 *cert = NULL;
  63968. const char *text = "Hello wolfSSL !";
  63969. unsigned char hash[SHA256_DIGEST_LENGTH];
  63970. unsigned char signature[2048/8];
  63971. unsigned int signatureLength;
  63972. byte *buf = NULL;
  63973. BIO *bio = NULL;
  63974. SHA256_CTX c;
  63975. EVP_PKEY *evpPkey = NULL;
  63976. EVP_PKEY *evpPubkey = NULL;
  63977. size_t sz;
  63978. /* generate hash */
  63979. SHA256_Init(&c);
  63980. SHA256_Update(&c, text, strlen(text));
  63981. SHA256_Final(hash, &c);
  63982. #ifdef WOLFSSL_SMALL_STACK_CACHE
  63983. /* workaround for small stack cache case */
  63984. wc_Sha256Free((wc_Sha256*)&c);
  63985. #endif
  63986. /* read privete key file */
  63987. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  63988. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  63989. ExpectTrue((sz = XFTELL(fp)) > 0);
  63990. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  63991. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  63992. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  63993. if (fp != XBADFILE) {
  63994. XFCLOSE(fp);
  63995. fp = XBADFILE;
  63996. }
  63997. /* read private key and sign hash data */
  63998. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  63999. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  64000. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  64001. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  64002. signature, &signatureLength, pKey), SSL_SUCCESS);
  64003. /* read public key and verify signed data */
  64004. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  64005. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  64006. if (fp != XBADFILE)
  64007. XFCLOSE(fp);
  64008. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  64009. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  64010. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  64011. signatureLength, pubKey), SSL_SUCCESS);
  64012. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  64013. signatureLength, NULL), SSL_FAILURE);
  64014. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  64015. signatureLength, pubKey), SSL_FAILURE);
  64016. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  64017. signatureLength, pubKey), SSL_FAILURE);
  64018. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  64019. signatureLength, NULL), SSL_FAILURE);
  64020. RSA_free(pKey);
  64021. EVP_PKEY_free(evpPkey);
  64022. RSA_free(pubKey);
  64023. EVP_PKEY_free(evpPubkey);
  64024. X509_free(cert);
  64025. BIO_free(bio);
  64026. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  64027. #endif
  64028. #endif
  64029. return EXPECT_RESULT();
  64030. }
  64031. static int test_wolfSSL_RSA_sign(void)
  64032. {
  64033. EXPECT_DECLS;
  64034. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  64035. RSA *rsa;
  64036. unsigned char hash[SHA256_DIGEST_LENGTH];
  64037. #ifdef USE_CERT_BUFFERS_1024
  64038. const unsigned char* privDer = client_key_der_1024;
  64039. size_t privDerSz = sizeof_client_key_der_1024;
  64040. const unsigned char* pubDer = client_keypub_der_1024;
  64041. size_t pubDerSz = sizeof_client_keypub_der_1024;
  64042. unsigned char signature[1024/8];
  64043. #else
  64044. const unsigned char* privDer = client_key_der_2048;
  64045. size_t privDerSz = sizeof_client_key_der_2048;
  64046. const unsigned char* pubDer = client_keypub_der_2048;
  64047. size_t pubDerSz = sizeof_client_keypub_der_2048;
  64048. unsigned char signature[2048/8];
  64049. #endif
  64050. unsigned int signatureLen;
  64051. const unsigned char* der;
  64052. XMEMSET(hash, 0, sizeof(hash));
  64053. der = privDer;
  64054. rsa = NULL;
  64055. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  64056. /* Invalid parameters. */
  64057. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  64058. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  64059. &signatureLen, rsa), 0);
  64060. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  64061. &signatureLen, rsa), 0);
  64062. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  64063. &signatureLen, rsa), 0);
  64064. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  64065. NULL, rsa), 0);
  64066. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  64067. &signatureLen, NULL), 0);
  64068. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  64069. &signatureLen, rsa), 1);
  64070. RSA_free(rsa);
  64071. der = pubDer;
  64072. rsa = NULL;
  64073. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  64074. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  64075. signatureLen, rsa), 1);
  64076. RSA_free(rsa);
  64077. #endif
  64078. return EXPECT_RESULT();
  64079. }
  64080. static int test_wolfSSL_RSA_sign_ex(void)
  64081. {
  64082. EXPECT_DECLS;
  64083. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  64084. RSA *rsa = NULL;
  64085. unsigned char hash[SHA256_DIGEST_LENGTH];
  64086. #ifdef USE_CERT_BUFFERS_1024
  64087. const unsigned char* privDer = client_key_der_1024;
  64088. size_t privDerSz = sizeof_client_key_der_1024;
  64089. const unsigned char* pubDer = client_keypub_der_1024;
  64090. size_t pubDerSz = sizeof_client_keypub_der_1024;
  64091. unsigned char signature[1024/8];
  64092. #else
  64093. const unsigned char* privDer = client_key_der_2048;
  64094. size_t privDerSz = sizeof_client_key_der_2048;
  64095. const unsigned char* pubDer = client_keypub_der_2048;
  64096. size_t pubDerSz = sizeof_client_keypub_der_2048;
  64097. unsigned char signature[2048/8];
  64098. #endif
  64099. unsigned int signatureLen;
  64100. const unsigned char* der;
  64101. unsigned char encodedHash[51];
  64102. unsigned int encodedHashLen;
  64103. const unsigned char expEncHash[] = {
  64104. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  64105. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  64106. 0x00, 0x04, 0x20,
  64107. /* Hash data */
  64108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64111. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64112. };
  64113. XMEMSET(hash, 0, sizeof(hash));
  64114. ExpectNotNull(rsa = wolfSSL_RSA_new());
  64115. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  64116. &signatureLen, rsa, 1), 0);
  64117. wolfSSL_RSA_free(rsa);
  64118. der = privDer;
  64119. rsa = NULL;
  64120. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  64121. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  64122. -1), 0);
  64123. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  64124. signature, &signatureLen, rsa, 1), 0);
  64125. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  64126. &signatureLen, rsa, 1), 0);
  64127. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  64128. &signatureLen, rsa, 1), 0);
  64129. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  64130. NULL, rsa, 1), 0);
  64131. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  64132. &signatureLen, NULL, 1), 0);
  64133. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  64134. &signatureLen, rsa, -1), 0);
  64135. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  64136. &signatureLen, rsa, 0), 0);
  64137. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  64138. &signatureLen, rsa, 0), 0);
  64139. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  64140. NULL, rsa, 0), 0);
  64141. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  64142. &signatureLen, rsa, 1), 1);
  64143. /* Test returning encoded hash. */
  64144. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  64145. &encodedHashLen, rsa, 0), 1);
  64146. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  64147. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  64148. RSA_free(rsa);
  64149. der = pubDer;
  64150. rsa = NULL;
  64151. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  64152. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  64153. signatureLen, rsa), 1);
  64154. RSA_free(rsa);
  64155. #endif
  64156. return EXPECT_RESULT();
  64157. }
  64158. static int test_wolfSSL_RSA_public_decrypt(void)
  64159. {
  64160. EXPECT_DECLS;
  64161. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  64162. RSA *rsa;
  64163. unsigned char msg[SHA256_DIGEST_LENGTH];
  64164. #ifdef USE_CERT_BUFFERS_1024
  64165. const unsigned char* pubDer = client_keypub_der_1024;
  64166. size_t pubDerSz = sizeof_client_keypub_der_1024;
  64167. unsigned char decMsg[1024/8];
  64168. const unsigned char encMsg[] = {
  64169. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  64170. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  64171. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  64172. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  64173. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  64174. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  64175. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  64176. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  64177. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  64178. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  64179. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  64180. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  64181. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  64182. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  64183. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  64184. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  64185. };
  64186. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  64187. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  64188. defined(WC_RSA_NO_PADDING)
  64189. const unsigned char encMsgNoPad[] = {
  64190. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  64191. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  64192. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  64193. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  64194. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  64195. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  64196. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  64197. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  64198. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  64199. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  64200. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  64201. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  64202. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  64203. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  64204. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  64205. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  64206. };
  64207. #endif
  64208. #else
  64209. const unsigned char* pubDer = client_keypub_der_2048;
  64210. size_t pubDerSz = sizeof_client_keypub_der_2048;
  64211. unsigned char decMsg[2048/8];
  64212. const unsigned char encMsg[] = {
  64213. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  64214. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  64215. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  64216. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  64217. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  64218. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  64219. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  64220. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  64221. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  64222. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  64223. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  64224. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  64225. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  64226. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  64227. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  64228. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  64229. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  64230. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  64231. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  64232. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  64233. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  64234. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  64235. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  64236. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  64237. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  64238. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  64239. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  64240. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  64241. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  64242. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  64243. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  64244. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  64245. };
  64246. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  64247. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  64248. defined(WC_RSA_NO_PADDING)
  64249. const unsigned char encMsgNoPad[] = {
  64250. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  64251. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  64252. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  64253. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  64254. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  64255. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  64256. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  64257. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  64258. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  64259. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  64260. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  64261. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  64262. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  64263. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  64264. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  64265. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  64266. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  64267. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  64268. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  64269. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  64270. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  64271. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  64272. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  64273. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  64274. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  64275. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  64276. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  64277. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  64278. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  64279. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  64280. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  64281. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  64282. };
  64283. #endif
  64284. #endif
  64285. const unsigned char* der;
  64286. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  64287. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  64288. defined(WC_RSA_NO_PADDING)
  64289. int i;
  64290. #endif
  64291. XMEMSET(msg, 0, sizeof(msg));
  64292. der = pubDer;
  64293. rsa = NULL;
  64294. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  64295. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  64296. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  64297. RSA_PKCS1_PADDING), -1);
  64298. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  64299. RSA_PKCS1_PADDING), -1);
  64300. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  64301. RSA_PKCS1_PADDING), -1);
  64302. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  64303. RSA_PKCS1_PADDING), -1);
  64304. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  64305. RSA_PKCS1_PSS_PADDING), -1);
  64306. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  64307. RSA_PKCS1_PADDING), 32);
  64308. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  64309. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  64310. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  64311. defined(WC_RSA_NO_PADDING)
  64312. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  64313. rsa, RSA_NO_PADDING), sizeof(decMsg));
  64314. /* Zeros before actual data. */
  64315. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  64316. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  64317. }
  64318. /* Check actual data. */
  64319. XMEMSET(msg, 0x01, sizeof(msg));
  64320. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  64321. #endif
  64322. RSA_free(rsa);
  64323. #endif
  64324. return EXPECT_RESULT();
  64325. }
  64326. static int test_wolfSSL_RSA_private_encrypt(void)
  64327. {
  64328. EXPECT_DECLS;
  64329. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  64330. RSA *rsa;
  64331. unsigned char msg[SHA256_DIGEST_LENGTH];
  64332. #ifdef USE_CERT_BUFFERS_1024
  64333. const unsigned char* privDer = client_key_der_1024;
  64334. size_t privDerSz = sizeof_client_key_der_1024;
  64335. unsigned char encMsg[1024/8];
  64336. const unsigned char expEncMsg[] = {
  64337. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  64338. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  64339. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  64340. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  64341. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  64342. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  64343. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  64344. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  64345. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  64346. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  64347. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  64348. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  64349. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  64350. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  64351. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  64352. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  64353. };
  64354. #ifdef WC_RSA_NO_PADDING
  64355. const unsigned char expEncMsgNoPad[] = {
  64356. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  64357. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  64358. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  64359. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  64360. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  64361. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  64362. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  64363. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  64364. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  64365. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  64366. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  64367. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  64368. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  64369. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  64370. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  64371. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  64372. };
  64373. #endif
  64374. #else
  64375. const unsigned char* privDer = client_key_der_2048;
  64376. size_t privDerSz = sizeof_client_key_der_2048;
  64377. unsigned char encMsg[2048/8];
  64378. const unsigned char expEncMsg[] = {
  64379. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  64380. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  64381. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  64382. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  64383. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  64384. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  64385. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  64386. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  64387. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  64388. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  64389. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  64390. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  64391. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  64392. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  64393. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  64394. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  64395. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  64396. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  64397. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  64398. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  64399. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  64400. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  64401. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  64402. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  64403. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  64404. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  64405. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  64406. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  64407. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  64408. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  64409. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  64410. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  64411. };
  64412. #ifdef WC_RSA_NO_PADDING
  64413. const unsigned char expEncMsgNoPad[] = {
  64414. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  64415. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  64416. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  64417. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  64418. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  64419. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  64420. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  64421. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  64422. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  64423. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  64424. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  64425. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  64426. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  64427. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  64428. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  64429. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  64430. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  64431. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  64432. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  64433. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  64434. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  64435. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  64436. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  64437. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  64438. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  64439. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  64440. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  64441. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  64442. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  64443. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  64444. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  64445. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  64446. };
  64447. #endif
  64448. #endif
  64449. const unsigned char* der;
  64450. XMEMSET(msg, 0x00, sizeof(msg));
  64451. der = privDer;
  64452. rsa = NULL;
  64453. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  64454. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  64455. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  64456. -1);
  64457. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  64458. RSA_PKCS1_PADDING), -1);
  64459. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  64460. RSA_PKCS1_PADDING), -1);
  64461. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  64462. RSA_PKCS1_PADDING), -1);
  64463. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  64464. RSA_PKCS1_PSS_PADDING), -1);
  64465. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  64466. RSA_PKCS1_PADDING), sizeof(encMsg));
  64467. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  64468. #ifdef WC_RSA_NO_PADDING
  64469. /* Non-zero message. */
  64470. XMEMSET(msg, 0x01, sizeof(msg));
  64471. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  64472. RSA_NO_PADDING), sizeof(encMsg));
  64473. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  64474. #endif
  64475. RSA_free(rsa);
  64476. #endif
  64477. return EXPECT_RESULT();
  64478. }
  64479. static int test_wolfSSL_RSA_public_encrypt(void)
  64480. {
  64481. EXPECT_DECLS;
  64482. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  64483. RSA* rsa = NULL;
  64484. const unsigned char msg[2048/8] = { 0 };
  64485. unsigned char encMsg[2048/8];
  64486. ExpectNotNull(rsa = RSA_new());
  64487. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  64488. RSA_PKCS1_PADDING), -1);
  64489. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  64490. RSA_PKCS1_PADDING), -1);
  64491. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  64492. RSA_PKCS1_PADDING), -1);
  64493. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  64494. RSA_PKCS1_PADDING), -1);
  64495. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  64496. RSA_PKCS1_PSS_PADDING), -1);
  64497. /* Empty RSA key. */
  64498. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  64499. RSA_PKCS1_PADDING), -1);
  64500. RSA_free(rsa);
  64501. #endif
  64502. return EXPECT_RESULT();
  64503. }
  64504. static int test_wolfSSL_RSA_private_decrypt(void)
  64505. {
  64506. EXPECT_DECLS;
  64507. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  64508. RSA* rsa = NULL;
  64509. unsigned char msg[2048/8];
  64510. const unsigned char encMsg[2048/8] = { 0 };
  64511. ExpectNotNull(rsa = RSA_new());
  64512. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  64513. RSA_PKCS1_PADDING), -1);
  64514. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  64515. RSA_PKCS1_PADDING), -1);
  64516. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  64517. RSA_PKCS1_PADDING), -1);
  64518. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  64519. RSA_PKCS1_PADDING), -1);
  64520. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  64521. RSA_PKCS1_PSS_PADDING), -1);
  64522. /* Empty RSA key. */
  64523. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  64524. RSA_PKCS1_PADDING), -1);
  64525. RSA_free(rsa);
  64526. #endif
  64527. return EXPECT_RESULT();
  64528. }
  64529. static int test_wolfSSL_RSA_GenAdd(void)
  64530. {
  64531. EXPECT_DECLS;
  64532. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  64533. RSA *rsa;
  64534. #ifdef USE_CERT_BUFFERS_1024
  64535. const unsigned char* privDer = client_key_der_1024;
  64536. size_t privDerSz = sizeof_client_key_der_1024;
  64537. const unsigned char* pubDer = client_keypub_der_1024;
  64538. size_t pubDerSz = sizeof_client_keypub_der_1024;
  64539. #else
  64540. const unsigned char* privDer = client_key_der_2048;
  64541. size_t privDerSz = sizeof_client_key_der_2048;
  64542. const unsigned char* pubDer = client_keypub_der_2048;
  64543. size_t pubDerSz = sizeof_client_keypub_der_2048;
  64544. #endif
  64545. const unsigned char* der;
  64546. der = privDer;
  64547. rsa = NULL;
  64548. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  64549. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  64550. #ifndef RSA_LOW_MEM
  64551. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  64552. #else
  64553. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  64554. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  64555. #endif
  64556. RSA_free(rsa);
  64557. der = pubDer;
  64558. rsa = NULL;
  64559. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  64560. /* Need private values. */
  64561. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  64562. RSA_free(rsa);
  64563. #endif
  64564. return EXPECT_RESULT();
  64565. }
  64566. static int test_wolfSSL_RSA_blinding_on(void)
  64567. {
  64568. EXPECT_DECLS;
  64569. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  64570. RSA *rsa;
  64571. WOLFSSL_BN_CTX *bnCtx = NULL;
  64572. #ifdef USE_CERT_BUFFERS_1024
  64573. const unsigned char* privDer = client_key_der_1024;
  64574. size_t privDerSz = sizeof_client_key_der_1024;
  64575. #else
  64576. const unsigned char* privDer = client_key_der_2048;
  64577. size_t privDerSz = sizeof_client_key_der_2048;
  64578. #endif
  64579. const unsigned char* der;
  64580. der = privDer;
  64581. rsa = NULL;
  64582. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  64583. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  64584. /* Does nothing so all parameters are valid. */
  64585. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  64586. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  64587. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  64588. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  64589. wolfSSL_BN_CTX_free(bnCtx);
  64590. RSA_free(rsa);
  64591. #endif
  64592. return EXPECT_RESULT();
  64593. }
  64594. static int test_wolfSSL_RSA_ex_data(void)
  64595. {
  64596. EXPECT_DECLS;
  64597. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  64598. RSA* rsa = NULL;
  64599. unsigned char data[1];
  64600. ExpectNotNull(rsa = RSA_new());
  64601. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  64602. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  64603. #ifdef MAX_EX_DATA
  64604. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  64605. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  64606. #endif
  64607. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  64608. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  64609. #ifdef HAVE_EX_DATA
  64610. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  64611. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  64612. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  64613. #else
  64614. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  64615. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  64616. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  64617. #endif
  64618. RSA_free(rsa);
  64619. #endif /* !NO_RSA && OPENSSL_EXTRA */
  64620. return EXPECT_RESULT();
  64621. }
  64622. static int test_wolfSSL_RSA_LoadDer(void)
  64623. {
  64624. EXPECT_DECLS;
  64625. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  64626. defined(OPENSSL_EXTRA_X509_SMALL))
  64627. RSA *rsa = NULL;
  64628. #ifdef USE_CERT_BUFFERS_1024
  64629. const unsigned char* privDer = client_key_der_1024;
  64630. size_t privDerSz = sizeof_client_key_der_1024;
  64631. #else
  64632. const unsigned char* privDer = client_key_der_2048;
  64633. size_t privDerSz = sizeof_client_key_der_2048;
  64634. #endif
  64635. ExpectNotNull(rsa = RSA_new());
  64636. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  64637. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  64638. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  64639. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  64640. RSA_free(rsa);
  64641. #endif /* !NO_RSA && OPENSSL_EXTRA */
  64642. return EXPECT_RESULT();
  64643. }
  64644. /* Local API. */
  64645. static int test_wolfSSL_RSA_To_Der(void)
  64646. {
  64647. EXPECT_DECLS;
  64648. #ifdef WOLFSSL_TEST_STATIC_BUILD
  64649. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  64650. RSA* rsa;
  64651. #ifdef USE_CERT_BUFFERS_1024
  64652. const unsigned char* privDer = client_key_der_1024;
  64653. size_t privDerSz = sizeof_client_key_der_1024;
  64654. const unsigned char* pubDer = client_keypub_der_1024;
  64655. size_t pubDerSz = sizeof_client_keypub_der_1024;
  64656. unsigned char out[sizeof(client_key_der_1024)];
  64657. #else
  64658. const unsigned char* privDer = client_key_der_2048;
  64659. size_t privDerSz = sizeof_client_key_der_2048;
  64660. const unsigned char* pubDer = client_keypub_der_2048;
  64661. size_t pubDerSz = sizeof_client_keypub_der_2048;
  64662. unsigned char out[sizeof(client_key_der_2048)];
  64663. #endif
  64664. const unsigned char* der;
  64665. unsigned char* outDer = NULL;
  64666. der = privDer;
  64667. rsa = NULL;
  64668. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  64669. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  64670. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  64671. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  64672. outDer = out;
  64673. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  64674. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  64675. outDer = NULL;
  64676. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  64677. ExpectNotNull(outDer);
  64678. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  64679. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  64680. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  64681. outDer = out;
  64682. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  64683. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  64684. RSA_free(rsa);
  64685. ExpectNotNull(rsa = RSA_new());
  64686. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  64687. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  64688. RSA_free(rsa);
  64689. der = pubDer;
  64690. rsa = NULL;
  64691. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  64692. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  64693. RSA_free(rsa);
  64694. #endif
  64695. #endif
  64696. return EXPECT_RESULT();
  64697. }
  64698. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  64699. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  64700. {
  64701. EXPECT_DECLS;
  64702. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  64703. XFILE file = XBADFILE;
  64704. const char* fname = "./certs/server-keyPub.pem";
  64705. RSA *rsa = NULL;
  64706. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  64707. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  64708. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  64709. ExpectIntEQ(RSA_size(rsa), 256);
  64710. RSA_free(rsa);
  64711. if (file != XBADFILE)
  64712. XFCLOSE(file);
  64713. #endif
  64714. return EXPECT_RESULT();
  64715. }
  64716. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  64717. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  64718. {
  64719. EXPECT_DECLS;
  64720. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  64721. defined(WOLFSSL_KEY_GEN)
  64722. RSA* rsa = NULL;
  64723. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  64724. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  64725. /* Valid but stub so returns 0. */
  64726. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  64727. #endif
  64728. return EXPECT_RESULT();
  64729. }
  64730. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  64731. {
  64732. EXPECT_DECLS;
  64733. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  64734. (defined(WOLFSSL_PEM_TO_DER) || \
  64735. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  64736. RSA* rsa = NULL;
  64737. #ifdef USE_CERT_BUFFERS_1024
  64738. const unsigned char* privDer = client_key_der_1024;
  64739. size_t privDerSz = sizeof_client_key_der_1024;
  64740. #else
  64741. const unsigned char* privDer = client_key_der_2048;
  64742. size_t privDerSz = sizeof_client_key_der_2048;
  64743. #endif
  64744. const unsigned char* der;
  64745. #ifndef NO_AES
  64746. unsigned char passwd[] = "password";
  64747. #endif
  64748. ExpectNotNull(rsa = RSA_new());
  64749. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  64750. NULL, NULL), 0);
  64751. RSA_free(rsa);
  64752. der = privDer;
  64753. rsa = NULL;
  64754. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  64755. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  64756. NULL, NULL), 0);
  64757. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  64758. NULL, NULL), 0);
  64759. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  64760. NULL, NULL), 1);
  64761. #ifndef NO_AES
  64762. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  64763. NULL, 0, NULL, NULL), 1);
  64764. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  64765. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  64766. #endif
  64767. RSA_free(rsa);
  64768. #endif
  64769. return EXPECT_RESULT();
  64770. }
  64771. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  64772. {
  64773. EXPECT_DECLS;
  64774. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  64775. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  64776. RSA* rsa = NULL;
  64777. #ifdef USE_CERT_BUFFERS_1024
  64778. const unsigned char* privDer = client_key_der_1024;
  64779. size_t privDerSz = sizeof_client_key_der_1024;
  64780. #else
  64781. const unsigned char* privDer = client_key_der_2048;
  64782. size_t privDerSz = sizeof_client_key_der_2048;
  64783. #endif
  64784. const unsigned char* der;
  64785. #ifndef NO_AES
  64786. unsigned char passwd[] = "password";
  64787. #endif
  64788. unsigned char* pem = NULL;
  64789. int plen;
  64790. ExpectNotNull(rsa = RSA_new());
  64791. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  64792. &plen), 0);
  64793. RSA_free(rsa);
  64794. der = privDer;
  64795. rsa = NULL;
  64796. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  64797. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  64798. &plen), 0);
  64799. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  64800. &plen), 0);
  64801. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  64802. NULL), 0);
  64803. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  64804. &plen), 1);
  64805. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  64806. pem = NULL;
  64807. #ifndef NO_AES
  64808. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  64809. NULL, 0, &pem, &plen), 1);
  64810. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  64811. pem = NULL;
  64812. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  64813. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  64814. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  64815. #endif
  64816. RSA_free(rsa);
  64817. #endif
  64818. return EXPECT_RESULT();
  64819. }
  64820. static int test_wolfSSL_DH(void)
  64821. {
  64822. EXPECT_DECLS;
  64823. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  64824. DH *dh = NULL;
  64825. BIGNUM* p;
  64826. BIGNUM* q;
  64827. BIGNUM* g;
  64828. BIGNUM* pub = NULL;
  64829. BIGNUM* priv = NULL;
  64830. #if defined(OPENSSL_ALL)
  64831. #if !defined(HAVE_FIPS) || \
  64832. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  64833. FILE* f = NULL;
  64834. unsigned char buf[268];
  64835. const unsigned char* pt = buf;
  64836. long len = 0;
  64837. dh = NULL;
  64838. XMEMSET(buf, 0, sizeof(buf));
  64839. /* Test 2048 bit parameters */
  64840. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  64841. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  64842. if (f != XBADFILE)
  64843. XFCLOSE(f);
  64844. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  64845. ExpectNotNull(dh->p);
  64846. ExpectNotNull(dh->g);
  64847. ExpectTrue(pt == buf);
  64848. ExpectIntEQ(DH_generate_key(dh), 1);
  64849. ExpectIntEQ(DH_generate_key(dh), 1);
  64850. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  64851. ExpectNotNull(pub = BN_new());
  64852. ExpectIntEQ(BN_set_word(pub, 1), 1);
  64853. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  64854. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  64855. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  64856. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  64857. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  64858. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  64859. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  64860. BN_free(pub);
  64861. pub = NULL;
  64862. DH_get0_pqg(dh, (const BIGNUM**)&p,
  64863. (const BIGNUM**)&q,
  64864. (const BIGNUM**)&g);
  64865. ExpectPtrEq(p, dh->p);
  64866. ExpectPtrEq(q, dh->q);
  64867. ExpectPtrEq(g, dh->g);
  64868. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  64869. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  64870. ExpectPtrEq(pub, dh->pub_key);
  64871. ExpectPtrEq(priv, dh->priv_key);
  64872. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  64873. ExpectPtrEq(pub, dh->pub_key);
  64874. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  64875. ExpectPtrEq(priv, dh->priv_key);
  64876. pub = NULL;
  64877. priv = NULL;
  64878. ExpectNotNull(pub = BN_new());
  64879. ExpectNotNull(priv = BN_new());
  64880. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  64881. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  64882. if (EXPECT_FAIL()) {
  64883. BN_free(pub);
  64884. BN_free(priv);
  64885. }
  64886. pub = NULL;
  64887. priv = NULL;
  64888. ExpectNotNull(pub = BN_new());
  64889. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  64890. if (EXPECT_FAIL()) {
  64891. BN_free(pub);
  64892. }
  64893. ExpectNotNull(priv = BN_new());
  64894. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  64895. if (EXPECT_FAIL()) {
  64896. BN_free(priv);
  64897. }
  64898. ExpectPtrEq(pub, dh->pub_key);
  64899. ExpectPtrEq(priv, dh->priv_key);
  64900. pub = NULL;
  64901. priv = NULL;
  64902. DH_free(dh);
  64903. dh = NULL;
  64904. ExpectNotNull(dh = DH_new());
  64905. p = NULL;
  64906. ExpectNotNull(p = BN_new());
  64907. ExpectIntEQ(BN_set_word(p, 1), 1);
  64908. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  64909. ExpectNotNull(pub = BN_new());
  64910. ExpectNotNull(priv = BN_new());
  64911. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  64912. if (EXPECT_FAIL()) {
  64913. BN_free(pub);
  64914. BN_free(priv);
  64915. }
  64916. pub = NULL;
  64917. priv = NULL;
  64918. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  64919. BN_free(p);
  64920. p = NULL;
  64921. DH_free(dh);
  64922. dh = NULL;
  64923. #ifdef WOLFSSL_KEY_GEN
  64924. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  64925. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  64926. DH_free(dh);
  64927. dh = NULL;
  64928. #endif
  64929. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  64930. #endif /* OPENSSL_ALL */
  64931. (void)dh;
  64932. (void)p;
  64933. (void)q;
  64934. (void)g;
  64935. (void)pub;
  64936. (void)priv;
  64937. ExpectNotNull(dh = wolfSSL_DH_new());
  64938. /* invalid parameters test */
  64939. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  64940. (const BIGNUM**)&q,
  64941. (const BIGNUM**)&g);
  64942. DH_get0_pqg(dh, NULL,
  64943. (const BIGNUM**)&q,
  64944. (const BIGNUM**)&g);
  64945. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  64946. DH_get0_pqg(dh, NULL, NULL, NULL);
  64947. DH_get0_pqg(dh, (const BIGNUM**)&p,
  64948. (const BIGNUM**)&q,
  64949. (const BIGNUM**)&g);
  64950. ExpectPtrEq(p, NULL);
  64951. ExpectPtrEq(q, NULL);
  64952. ExpectPtrEq(g, NULL);
  64953. DH_free(dh);
  64954. dh = NULL;
  64955. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  64956. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  64957. #if defined(OPENSSL_ALL) || \
  64958. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  64959. dh = wolfSSL_DH_new();
  64960. ExpectNotNull(dh);
  64961. p = wolfSSL_BN_new();
  64962. ExpectNotNull(p);
  64963. ExpectIntEQ(BN_set_word(p, 11), 1);
  64964. g = wolfSSL_BN_new();
  64965. ExpectNotNull(g);
  64966. ExpectIntEQ(BN_set_word(g, 2), 1);
  64967. q = wolfSSL_BN_new();
  64968. ExpectNotNull(q);
  64969. ExpectIntEQ(BN_set_word(q, 5), 1);
  64970. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  64971. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  64972. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  64973. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  64974. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  64975. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  64976. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  64977. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  64978. /* Don't need q. */
  64979. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  64980. if (EXPECT_FAIL()) {
  64981. BN_free(p);
  64982. BN_free(g);
  64983. }
  64984. p = NULL;
  64985. g = NULL;
  64986. /* Setting again will free the p and g. */
  64987. wolfSSL_BN_free(q);
  64988. q = NULL;
  64989. DH_free(dh);
  64990. dh = NULL;
  64991. dh = wolfSSL_DH_new();
  64992. ExpectNotNull(dh);
  64993. p = wolfSSL_BN_new();
  64994. ExpectNotNull(p);
  64995. ExpectIntEQ(BN_set_word(p, 11), 1);
  64996. g = wolfSSL_BN_new();
  64997. ExpectNotNull(g);
  64998. ExpectIntEQ(BN_set_word(g, 2), 1);
  64999. q = wolfSSL_BN_new();
  65000. ExpectNotNull(q);
  65001. ExpectIntEQ(BN_set_word(q, 5), 1);
  65002. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  65003. /* p, q and g are now owned by dh - don't free. */
  65004. if (EXPECT_FAIL()) {
  65005. BN_free(p);
  65006. BN_free(q);
  65007. BN_free(g);
  65008. }
  65009. p = NULL;
  65010. q = NULL;
  65011. g = NULL;
  65012. p = wolfSSL_BN_new();
  65013. ExpectNotNull(p);
  65014. ExpectIntEQ(BN_set_word(p, 11), 1);
  65015. g = wolfSSL_BN_new();
  65016. ExpectNotNull(g);
  65017. ExpectIntEQ(BN_set_word(g, 2), 1);
  65018. q = wolfSSL_BN_new();
  65019. ExpectNotNull(q);
  65020. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  65021. if (EXPECT_FAIL()) {
  65022. BN_free(p);
  65023. }
  65024. p = NULL;
  65025. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  65026. if (EXPECT_FAIL()) {
  65027. BN_free(q);
  65028. }
  65029. q = NULL;
  65030. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  65031. if (EXPECT_FAIL()) {
  65032. BN_free(g);
  65033. }
  65034. g = NULL;
  65035. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  65036. /* p, q and g are now owned by dh - don't free. */
  65037. DH_free(dh);
  65038. dh = NULL;
  65039. ExpectIntEQ(DH_generate_key(NULL), 0);
  65040. ExpectNotNull(dh = DH_new());
  65041. ExpectIntEQ(DH_generate_key(dh), 0);
  65042. p = wolfSSL_BN_new();
  65043. ExpectNotNull(p);
  65044. ExpectIntEQ(BN_set_word(p, 0), 1);
  65045. g = wolfSSL_BN_new();
  65046. ExpectNotNull(g);
  65047. ExpectIntEQ(BN_set_word(g, 2), 1);
  65048. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  65049. if (EXPECT_FAIL()) {
  65050. BN_free(p);
  65051. BN_free(g);
  65052. }
  65053. p = NULL;
  65054. g = NULL;
  65055. ExpectIntEQ(DH_generate_key(dh), 0);
  65056. DH_free(dh);
  65057. dh = NULL;
  65058. #endif
  65059. #endif
  65060. /* Test DH_up_ref() */
  65061. dh = wolfSSL_DH_new();
  65062. ExpectNotNull(dh);
  65063. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  65064. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  65065. DH_free(dh); /* decrease ref count */
  65066. DH_free(dh); /* free WOLFSSL_DH */
  65067. dh = NULL;
  65068. q = NULL;
  65069. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  65070. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  65071. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  65072. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  65073. #ifdef HAVE_FFDHE_2048
  65074. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  65075. DH_free(dh);
  65076. dh = NULL;
  65077. q = NULL;
  65078. #endif
  65079. #ifdef HAVE_FFDHE_3072
  65080. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  65081. DH_free(dh);
  65082. dh = NULL;
  65083. q = NULL;
  65084. #endif
  65085. #ifdef HAVE_FFDHE_4096
  65086. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  65087. DH_free(dh);
  65088. dh = NULL;
  65089. q = NULL;
  65090. #endif
  65091. #else
  65092. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  65093. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  65094. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  65095. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  65096. #endif /* OPENSSL_EXTRA && !NO_DH */
  65097. return EXPECT_RESULT();
  65098. }
  65099. static int test_wolfSSL_DH_dup(void)
  65100. {
  65101. EXPECT_DECLS;
  65102. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  65103. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  65104. defined(OPENSSL_EXTRA)
  65105. DH *dh = NULL;
  65106. DH *dhDup = NULL;
  65107. ExpectNotNull(dh = wolfSSL_DH_new());
  65108. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  65109. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  65110. #if defined(OPENSSL_ALL) || \
  65111. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  65112. {
  65113. WOLFSSL_BIGNUM* p = NULL;
  65114. WOLFSSL_BIGNUM* g = NULL;
  65115. ExpectNotNull(p = wolfSSL_BN_new());
  65116. ExpectNotNull(g = wolfSSL_BN_new());
  65117. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  65118. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  65119. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  65120. if (EXPECT_FAIL()) {
  65121. wolfSSL_BN_free(p);
  65122. wolfSSL_BN_free(g);
  65123. }
  65124. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  65125. wolfSSL_DH_free(dhDup);
  65126. }
  65127. #endif
  65128. wolfSSL_DH_free(dh);
  65129. #endif
  65130. #endif
  65131. return EXPECT_RESULT();
  65132. }
  65133. static int test_wolfSSL_DH_check(void)
  65134. {
  65135. EXPECT_DECLS;
  65136. #ifdef OPENSSL_ALL
  65137. #ifndef NO_DH
  65138. #ifndef NO_BIO
  65139. #ifndef NO_DSA
  65140. byte buf[6000];
  65141. char file[] = "./certs/dsaparams.pem";
  65142. XFILE f = XBADFILE;
  65143. int bytes;
  65144. BIO* bio = NULL;
  65145. DSA* dsa = NULL;
  65146. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  65147. static const byte dh2048[] = {
  65148. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  65149. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  65150. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  65151. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  65152. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  65153. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  65154. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  65155. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  65156. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  65157. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  65158. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  65159. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  65160. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  65161. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  65162. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  65163. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  65164. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  65165. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  65166. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  65167. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  65168. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  65169. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  65170. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  65171. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  65172. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  65173. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  65174. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  65175. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  65176. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  65177. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  65178. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  65179. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  65180. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  65181. 0x93, 0x02, 0x01, 0x02
  65182. };
  65183. const byte* params;
  65184. #endif
  65185. DH* dh = NULL;
  65186. WOLFSSL_BIGNUM* p = NULL;
  65187. WOLFSSL_BIGNUM* g = NULL;
  65188. WOLFSSL_BIGNUM* pTmp = NULL;
  65189. WOLFSSL_BIGNUM* gTmp = NULL;
  65190. int codes = -1;
  65191. #ifndef NO_DSA
  65192. /* Initialize DH */
  65193. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  65194. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  65195. if (f != XBADFILE)
  65196. XFCLOSE(f);
  65197. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  65198. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  65199. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  65200. ExpectNotNull(dh);
  65201. BIO_free(bio);
  65202. DSA_free(dsa);
  65203. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  65204. params = dh2048;
  65205. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  65206. (long)sizeof(dh2048)));
  65207. #else
  65208. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  65209. #endif
  65210. /* Test assumed to be valid dh.
  65211. * Should return WOLFSSL_SUCCESS
  65212. * codes should be 0
  65213. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  65214. */
  65215. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  65216. ExpectIntEQ(codes, 0);
  65217. /* Test NULL dh: expected BAD_FUNC_ARG */
  65218. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  65219. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  65220. if (dh != NULL) {
  65221. pTmp = dh->p;
  65222. dh->p = NULL;
  65223. }
  65224. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  65225. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  65226. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  65227. /* set dh->p back to normal so it won't fail on next tests */
  65228. if (dh != NULL) {
  65229. dh->p = pTmp;
  65230. pTmp = NULL;
  65231. }
  65232. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  65233. if (dh != NULL) {
  65234. gTmp = dh->g;
  65235. dh->g = NULL;
  65236. }
  65237. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  65238. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  65239. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  65240. if (dh != NULL) {
  65241. dh->g = gTmp;
  65242. gTmp = NULL;
  65243. }
  65244. /* Cleanup */
  65245. DH_free(dh);
  65246. dh = NULL;
  65247. dh = DH_new();
  65248. ExpectNotNull(dh);
  65249. /* Check empty DH. */
  65250. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  65251. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  65252. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  65253. /* Check non-prime valued p. */
  65254. ExpectNotNull(p = BN_new());
  65255. ExpectIntEQ(BN_set_word(p, 4), 1);
  65256. ExpectNotNull(g = BN_new());
  65257. ExpectIntEQ(BN_set_word(g, 2), 1);
  65258. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  65259. if (EXPECT_FAIL()) {
  65260. wolfSSL_BN_free(p);
  65261. wolfSSL_BN_free(g);
  65262. }
  65263. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  65264. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  65265. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  65266. DH_free(dh);
  65267. dh = NULL;
  65268. #endif
  65269. #endif /* !NO_DH && !NO_DSA */
  65270. #endif
  65271. return EXPECT_RESULT();
  65272. }
  65273. static int test_wolfSSL_DH_prime(void)
  65274. {
  65275. EXPECT_DECLS;
  65276. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  65277. WOLFSSL_BIGNUM* bn = NULL;
  65278. #if WOLFSSL_MAX_BN_BITS >= 768
  65279. WOLFSSL_BIGNUM* bn2 = NULL;
  65280. #endif
  65281. bn = wolfSSL_DH_768_prime(NULL);
  65282. #if WOLFSSL_MAX_BN_BITS >= 768
  65283. ExpectNotNull(bn);
  65284. bn2 = wolfSSL_DH_768_prime(bn);
  65285. ExpectNotNull(bn2);
  65286. ExpectTrue(bn == bn2);
  65287. wolfSSL_BN_free(bn);
  65288. bn = NULL;
  65289. #else
  65290. ExpectNull(bn);
  65291. #endif
  65292. bn = wolfSSL_DH_1024_prime(NULL);
  65293. #if WOLFSSL_MAX_BN_BITS >= 1024
  65294. ExpectNotNull(bn);
  65295. wolfSSL_BN_free(bn);
  65296. bn = NULL;
  65297. #else
  65298. ExpectNull(bn);
  65299. #endif
  65300. bn = wolfSSL_DH_2048_prime(NULL);
  65301. #if WOLFSSL_MAX_BN_BITS >= 2048
  65302. ExpectNotNull(bn);
  65303. wolfSSL_BN_free(bn);
  65304. bn = NULL;
  65305. #else
  65306. ExpectNull(bn);
  65307. #endif
  65308. bn = wolfSSL_DH_3072_prime(NULL);
  65309. #if WOLFSSL_MAX_BN_BITS >= 3072
  65310. ExpectNotNull(bn);
  65311. wolfSSL_BN_free(bn);
  65312. bn = NULL;
  65313. #else
  65314. ExpectNull(bn);
  65315. #endif
  65316. bn = wolfSSL_DH_4096_prime(NULL);
  65317. #if WOLFSSL_MAX_BN_BITS >= 4096
  65318. ExpectNotNull(bn);
  65319. wolfSSL_BN_free(bn);
  65320. bn = NULL;
  65321. #else
  65322. ExpectNull(bn);
  65323. #endif
  65324. bn = wolfSSL_DH_6144_prime(NULL);
  65325. #if WOLFSSL_MAX_BN_BITS >= 6144
  65326. ExpectNotNull(bn);
  65327. wolfSSL_BN_free(bn);
  65328. bn = NULL;
  65329. #else
  65330. ExpectNull(bn);
  65331. #endif
  65332. bn = wolfSSL_DH_8192_prime(NULL);
  65333. #if WOLFSSL_MAX_BN_BITS >= 8192
  65334. ExpectNotNull(bn);
  65335. wolfSSL_BN_free(bn);
  65336. bn = NULL;
  65337. #else
  65338. ExpectNull(bn);
  65339. #endif
  65340. #endif
  65341. return EXPECT_RESULT();
  65342. }
  65343. static int test_wolfSSL_DH_1536_prime(void)
  65344. {
  65345. EXPECT_DECLS;
  65346. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  65347. BIGNUM* bn = NULL;
  65348. unsigned char bits[200];
  65349. int sz = 192; /* known binary size */
  65350. const byte expected[] = {
  65351. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  65352. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  65353. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  65354. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  65355. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  65356. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  65357. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  65358. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  65359. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  65360. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  65361. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  65362. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  65363. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  65364. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  65365. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  65366. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  65367. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  65368. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  65369. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  65370. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  65371. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  65372. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  65373. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  65374. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  65375. };
  65376. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  65377. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  65378. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  65379. BN_free(bn);
  65380. #endif
  65381. return EXPECT_RESULT();
  65382. }
  65383. static int test_wolfSSL_DH_get_2048_256(void)
  65384. {
  65385. EXPECT_DECLS;
  65386. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  65387. WOLFSSL_DH* dh = NULL;
  65388. const WOLFSSL_BIGNUM* pBn;
  65389. const WOLFSSL_BIGNUM* gBn;
  65390. const WOLFSSL_BIGNUM* qBn;
  65391. const byte pExpected[] = {
  65392. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  65393. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  65394. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  65395. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  65396. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  65397. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  65398. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  65399. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  65400. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  65401. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  65402. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  65403. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  65404. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  65405. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  65406. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  65407. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  65408. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  65409. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  65410. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  65411. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  65412. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  65413. 0x1E, 0x1A, 0x15, 0x97
  65414. };
  65415. const byte gExpected[] = {
  65416. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  65417. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  65418. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  65419. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  65420. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  65421. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  65422. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  65423. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  65424. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  65425. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  65426. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  65427. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  65428. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  65429. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  65430. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  65431. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  65432. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  65433. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  65434. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  65435. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  65436. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  65437. 0x6C, 0xC4, 0x16, 0x59
  65438. };
  65439. const byte qExpected[] = {
  65440. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  65441. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  65442. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  65443. };
  65444. int pSz = 0;
  65445. int qSz = 0;
  65446. int gSz = 0;
  65447. byte* pReturned = NULL;
  65448. byte* qReturned = NULL;
  65449. byte* gReturned = NULL;
  65450. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  65451. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  65452. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  65453. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  65454. DYNAMIC_TYPE_TMP_BUFFER));
  65455. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  65456. ExpectIntEQ(pSz, sizeof(pExpected));
  65457. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  65458. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  65459. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  65460. DYNAMIC_TYPE_TMP_BUFFER));
  65461. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  65462. ExpectIntEQ(qSz, sizeof(qExpected));
  65463. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  65464. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  65465. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  65466. DYNAMIC_TYPE_TMP_BUFFER));
  65467. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  65468. ExpectIntEQ(gSz, sizeof(gExpected));
  65469. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  65470. wolfSSL_DH_free(dh);
  65471. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  65472. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  65473. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  65474. #endif
  65475. return EXPECT_RESULT();
  65476. }
  65477. static int test_wolfSSL_PEM_write_DHparams(void)
  65478. {
  65479. EXPECT_DECLS;
  65480. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  65481. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  65482. DH* dh = NULL;
  65483. BIO* bio = NULL;
  65484. XFILE fp = XBADFILE;
  65485. byte pem[2048];
  65486. int pemSz = 0;
  65487. const char expected[] =
  65488. "-----BEGIN DH PARAMETERS-----\n"
  65489. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  65490. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  65491. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  65492. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  65493. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  65494. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  65495. "-----END DH PARAMETERS-----\n";
  65496. const char badPem[] =
  65497. "-----BEGIN DH PARAMETERS-----\n"
  65498. "-----END DH PARAMETERS-----\n";
  65499. const char emptySeqPem[] =
  65500. "-----BEGIN DH PARAMETERS-----\n"
  65501. "MAA=\n"
  65502. "-----END DH PARAMETERS-----\n";
  65503. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  65504. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  65505. if (fp != XBADFILE) {
  65506. XFCLOSE(fp);
  65507. fp = XBADFILE;
  65508. }
  65509. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  65510. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  65511. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  65512. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  65513. (int)sizeof(badPem));
  65514. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  65515. BIO_free(bio);
  65516. bio = NULL;
  65517. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  65518. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  65519. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  65520. (int)sizeof(emptySeqPem));
  65521. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  65522. BIO_free(bio);
  65523. bio = NULL;
  65524. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  65525. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  65526. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  65527. BIO_free(bio);
  65528. bio = NULL;
  65529. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  65530. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  65531. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  65532. DH_free(dh);
  65533. dh = NULL;
  65534. dh = wolfSSL_DH_new();
  65535. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  65536. if (fp != XBADFILE) {
  65537. XFCLOSE(fp);
  65538. fp = XBADFILE;
  65539. }
  65540. wolfSSL_DH_free(dh);
  65541. dh = NULL;
  65542. /* check results */
  65543. XMEMSET(pem, 0, sizeof(pem));
  65544. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  65545. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  65546. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  65547. if (fp != XBADFILE)
  65548. XFCLOSE(fp);
  65549. #endif
  65550. return EXPECT_RESULT();
  65551. }
  65552. static int test_wolfSSL_d2i_DHparams(void)
  65553. {
  65554. EXPECT_DECLS;
  65555. #ifdef OPENSSL_ALL
  65556. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  65557. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  65558. XFILE f = XBADFILE;
  65559. unsigned char buf[4096];
  65560. const unsigned char* pt = buf;
  65561. #ifdef HAVE_FFDHE_2048
  65562. const char* params1 = "./certs/dh2048.der";
  65563. #endif
  65564. #ifdef HAVE_FFDHE_3072
  65565. const char* params2 = "./certs/dh3072.der";
  65566. #endif
  65567. long len = 0;
  65568. WOLFSSL_DH* dh = NULL;
  65569. XMEMSET(buf, 0, sizeof(buf));
  65570. /* Test 2048 bit parameters */
  65571. #ifdef HAVE_FFDHE_2048
  65572. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  65573. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  65574. if (f != XBADFILE) {
  65575. XFCLOSE(f);
  65576. f = XBADFILE;
  65577. }
  65578. /* Valid case */
  65579. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  65580. ExpectNotNull(dh->p);
  65581. ExpectNotNull(dh->g);
  65582. ExpectTrue(pt == buf);
  65583. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  65584. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  65585. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  65586. /* Invalid cases */
  65587. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  65588. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  65589. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  65590. DH_free(dh);
  65591. dh = NULL;
  65592. *buf = 0;
  65593. pt = buf;
  65594. #endif /* HAVE_FFDHE_2048 */
  65595. /* Test 3072 bit parameters */
  65596. #ifdef HAVE_FFDHE_3072
  65597. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  65598. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  65599. if (f != XBADFILE) {
  65600. XFCLOSE(f);
  65601. f = XBADFILE;
  65602. }
  65603. /* Valid case */
  65604. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  65605. ExpectNotNull(dh->p);
  65606. ExpectNotNull(dh->g);
  65607. ExpectTrue(pt != buf);
  65608. ExpectIntEQ(DH_generate_key(dh), 1);
  65609. /* Invalid cases */
  65610. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  65611. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  65612. DH_free(dh);
  65613. dh = NULL;
  65614. #endif /* HAVE_FFDHE_3072 */
  65615. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  65616. #endif /* !NO_DH */
  65617. #endif
  65618. return EXPECT_RESULT();
  65619. }
  65620. static int test_wolfSSL_DH_LoadDer(void)
  65621. {
  65622. EXPECT_DECLS;
  65623. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  65624. defined(OPENSSL_EXTRA)
  65625. static const byte dh2048[] = {
  65626. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  65627. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  65628. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  65629. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  65630. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  65631. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  65632. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  65633. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  65634. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  65635. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  65636. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  65637. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  65638. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  65639. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  65640. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  65641. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  65642. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  65643. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  65644. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  65645. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  65646. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  65647. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  65648. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  65649. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  65650. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  65651. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  65652. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  65653. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  65654. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  65655. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  65656. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  65657. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  65658. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  65659. 0x93, 0x02, 0x01, 0x02
  65660. };
  65661. WOLFSSL_DH* dh = NULL;
  65662. ExpectNotNull(dh = wolfSSL_DH_new());
  65663. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  65664. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  65665. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  65666. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  65667. wolfSSL_DH_free(dh);
  65668. #endif
  65669. return EXPECT_RESULT();
  65670. }
  65671. static int test_wolfSSL_i2d_DHparams(void)
  65672. {
  65673. EXPECT_DECLS;
  65674. #ifdef OPENSSL_ALL
  65675. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  65676. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  65677. XFILE f = XBADFILE;
  65678. unsigned char buf[4096];
  65679. const unsigned char* pt;
  65680. unsigned char* pt2;
  65681. #ifdef HAVE_FFDHE_2048
  65682. const char* params1 = "./certs/dh2048.der";
  65683. #endif
  65684. #ifdef HAVE_FFDHE_3072
  65685. const char* params2 = "./certs/dh3072.der";
  65686. #endif
  65687. long len = 0;
  65688. WOLFSSL_DH* dh = NULL;
  65689. /* Test 2048 bit parameters */
  65690. #ifdef HAVE_FFDHE_2048
  65691. pt = buf;
  65692. pt2 = buf;
  65693. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  65694. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  65695. if (f != XBADFILE) {
  65696. XFCLOSE(f);
  65697. f = XBADFILE;
  65698. }
  65699. /* Valid case */
  65700. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  65701. ExpectTrue(pt == buf);
  65702. ExpectIntEQ(DH_generate_key(dh), 1);
  65703. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  65704. /* Invalid case */
  65705. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  65706. /* Return length only */
  65707. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  65708. DH_free(dh);
  65709. dh = NULL;
  65710. *buf = 0;
  65711. #endif
  65712. /* Test 3072 bit parameters */
  65713. #ifdef HAVE_FFDHE_3072
  65714. pt = buf;
  65715. pt2 = buf;
  65716. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  65717. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  65718. if (f != XBADFILE) {
  65719. XFCLOSE(f);
  65720. f = XBADFILE;
  65721. }
  65722. /* Valid case */
  65723. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  65724. ExpectTrue(pt == buf);
  65725. ExpectIntEQ(DH_generate_key(dh), 1);
  65726. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  65727. /* Invalid case */
  65728. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  65729. /* Return length only */
  65730. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  65731. DH_free(dh);
  65732. dh = NULL;
  65733. #endif
  65734. dh = DH_new();
  65735. ExpectNotNull(dh);
  65736. pt2 = buf;
  65737. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  65738. DH_free(dh);
  65739. dh = NULL;
  65740. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  65741. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  65742. #endif
  65743. return EXPECT_RESULT();
  65744. }
  65745. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  65746. /*----------------------------------------------------------------------------*
  65747. | EC
  65748. *----------------------------------------------------------------------------*/
  65749. static int test_wolfSSL_EC_GROUP(void)
  65750. {
  65751. EXPECT_DECLS;
  65752. #ifdef OPENSSL_EXTRA
  65753. EC_GROUP *group = NULL;
  65754. EC_GROUP *group2 = NULL;
  65755. EC_GROUP *group3 = NULL;
  65756. #ifndef HAVE_ECC_BRAINPOOL
  65757. EC_GROUP *group4 = NULL;
  65758. #endif
  65759. WOLFSSL_BIGNUM* order = NULL;
  65760. int group_bits;
  65761. int i;
  65762. static const int knownEccNids[] = {
  65763. NID_X9_62_prime192v1,
  65764. NID_X9_62_prime192v2,
  65765. NID_X9_62_prime192v3,
  65766. NID_X9_62_prime239v1,
  65767. NID_X9_62_prime239v2,
  65768. NID_X9_62_prime239v3,
  65769. NID_X9_62_prime256v1,
  65770. NID_secp112r1,
  65771. NID_secp112r2,
  65772. NID_secp128r1,
  65773. NID_secp128r2,
  65774. NID_secp160r1,
  65775. NID_secp160r2,
  65776. NID_secp224r1,
  65777. NID_secp384r1,
  65778. NID_secp521r1,
  65779. NID_secp160k1,
  65780. NID_secp192k1,
  65781. NID_secp224k1,
  65782. NID_secp256k1,
  65783. NID_brainpoolP160r1,
  65784. NID_brainpoolP192r1,
  65785. NID_brainpoolP224r1,
  65786. NID_brainpoolP256r1,
  65787. NID_brainpoolP320r1,
  65788. NID_brainpoolP384r1,
  65789. NID_brainpoolP512r1,
  65790. };
  65791. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  65792. static const int knownEccEnums[] = {
  65793. ECC_SECP192R1,
  65794. ECC_PRIME192V2,
  65795. ECC_PRIME192V3,
  65796. ECC_PRIME239V1,
  65797. ECC_PRIME239V2,
  65798. ECC_PRIME239V3,
  65799. ECC_SECP256R1,
  65800. ECC_SECP112R1,
  65801. ECC_SECP112R2,
  65802. ECC_SECP128R1,
  65803. ECC_SECP128R2,
  65804. ECC_SECP160R1,
  65805. ECC_SECP160R2,
  65806. ECC_SECP224R1,
  65807. ECC_SECP384R1,
  65808. ECC_SECP521R1,
  65809. ECC_SECP160K1,
  65810. ECC_SECP192K1,
  65811. ECC_SECP224K1,
  65812. ECC_SECP256K1,
  65813. ECC_BRAINPOOLP160R1,
  65814. ECC_BRAINPOOLP192R1,
  65815. ECC_BRAINPOOLP224R1,
  65816. ECC_BRAINPOOLP256R1,
  65817. ECC_BRAINPOOLP320R1,
  65818. ECC_BRAINPOOLP384R1,
  65819. ECC_BRAINPOOLP512R1,
  65820. };
  65821. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  65822. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  65823. ExpectNotNull(group2 = EC_GROUP_dup(group));
  65824. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  65825. #ifndef HAVE_ECC_BRAINPOOL
  65826. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  65827. NID_brainpoolP256r1));
  65828. #endif
  65829. ExpectNull(EC_GROUP_dup(NULL));
  65830. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  65831. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  65832. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  65833. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  65834. #ifndef HAVE_ECC_BRAINPOOL
  65835. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  65836. #endif
  65837. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  65838. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  65839. ExpectNotNull(order = BN_new());
  65840. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  65841. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  65842. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  65843. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  65844. wolfSSL_BN_free(order);
  65845. ExpectNotNull(EC_GROUP_method_of(group));
  65846. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  65847. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  65848. NID_X9_62_prime_field);
  65849. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  65850. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  65851. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  65852. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  65853. #ifndef NO_WOLFSSL_STUB
  65854. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  65855. #endif
  65856. #ifndef HAVE_ECC_BRAINPOOL
  65857. EC_GROUP_free(group4);
  65858. #endif
  65859. EC_GROUP_free(group3);
  65860. EC_GROUP_free(group2);
  65861. EC_GROUP_free(group);
  65862. for (i = 0; i < knowEccNidsLen; i++) {
  65863. group = NULL;
  65864. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  65865. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  65866. EC_GROUP_free(group);
  65867. }
  65868. for (i = 0; i < knowEccEnumsLen; i++) {
  65869. group = NULL;
  65870. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  65871. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  65872. EC_GROUP_free(group);
  65873. }
  65874. #endif
  65875. return EXPECT_RESULT();
  65876. }
  65877. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  65878. {
  65879. EXPECT_DECLS;
  65880. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  65881. EC_GROUP *group = NULL;
  65882. BIO* bio = NULL;
  65883. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  65884. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  65885. EC_GROUP *ret = NULL;
  65886. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  65887. "BgUrgQQAIg==\n"
  65888. "-----END EC PARAMETERS-----";
  65889. #endif
  65890. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  65891. "MAA=\n"
  65892. "-----END EC PARAMETERS-----";
  65893. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  65894. "BgA=\n"
  65895. "-----END EC PARAMETERS-----";
  65896. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  65897. "BgE=\n"
  65898. "-----END EC PARAMETERS-----";
  65899. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  65900. "BgE*\n"
  65901. "-----END EC PARAMETERS-----";
  65902. /* Test that first parameter, bio, being NULL fails. */
  65903. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  65904. /* Test that reading named parameters works. */
  65905. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  65906. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  65907. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  65908. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  65909. BIO_free(bio);
  65910. bio = NULL;
  65911. EC_GROUP_free(group);
  65912. group = NULL;
  65913. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  65914. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  65915. /* Test that reusing group works. */
  65916. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  65917. sizeof(ec_nc_p384)));
  65918. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  65919. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  65920. BIO_free(bio);
  65921. bio = NULL;
  65922. EC_GROUP_free(group);
  65923. group = NULL;
  65924. /* Test that returning through group works. */
  65925. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  65926. sizeof(ec_nc_p384)));
  65927. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  65928. ExpectIntEQ(group == ret, 1);
  65929. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  65930. BIO_free(bio);
  65931. bio = NULL;
  65932. EC_GROUP_free(group);
  65933. group = NULL;
  65934. #endif
  65935. /* Test 0x30, 0x00 (not and object id) fails. */
  65936. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  65937. sizeof(ec_nc_bad_1)));
  65938. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  65939. BIO_free(bio);
  65940. bio = NULL;
  65941. /* Test 0x06, 0x00 (empty object id) fails. */
  65942. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  65943. sizeof(ec_nc_bad_2)));
  65944. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  65945. BIO_free(bio);
  65946. bio = NULL;
  65947. /* Test 0x06, 0x01 (badly formed object id) fails. */
  65948. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  65949. sizeof(ec_nc_bad_3)));
  65950. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  65951. BIO_free(bio);
  65952. bio = NULL;
  65953. /* Test invalid PEM encoding - invalid character. */
  65954. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  65955. sizeof(ec_nc_bad_4)));
  65956. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  65957. BIO_free(bio);
  65958. #endif
  65959. return EXPECT_RESULT();
  65960. }
  65961. static int test_wolfSSL_EC_POINT(void)
  65962. {
  65963. EXPECT_DECLS;
  65964. #if !defined(WOLFSSL_SP_MATH) && \
  65965. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  65966. #ifdef OPENSSL_EXTRA
  65967. BN_CTX* ctx = NULL;
  65968. EC_GROUP* group = NULL;
  65969. #ifndef HAVE_ECC_BRAINPOOL
  65970. EC_GROUP* group2 = NULL;
  65971. #endif
  65972. EC_POINT* Gxy = NULL;
  65973. EC_POINT* new_point = NULL;
  65974. EC_POINT* set_point = NULL;
  65975. EC_POINT* get_point = NULL;
  65976. EC_POINT* infinity = NULL;
  65977. BIGNUM* k = NULL;
  65978. BIGNUM* Gx = NULL;
  65979. BIGNUM* Gy = NULL;
  65980. BIGNUM* Gz = NULL;
  65981. BIGNUM* X = NULL;
  65982. BIGNUM* Y = NULL;
  65983. BIGNUM* set_point_bn = NULL;
  65984. char* hexStr = NULL;
  65985. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  65986. "17AF381913FF7A96314EA47055EA0FD0";
  65987. /* NISTP256R1 Gx/Gy */
  65988. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  65989. "77037D812DEB33A0F4A13945D898C296";
  65990. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  65991. "2BCE33576B315ECECBB6406837BF51F5";
  65992. const char* uncompG
  65993. = "046B17D1F2E12C4247F8BCE6E563A440F2"
  65994. "77037D812DEB33A0F4A13945D898C296"
  65995. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  65996. "2BCE33576B315ECECBB6406837BF51F5";
  65997. const char* compG
  65998. = "036B17D1F2E12C4247F8BCE6E563A440F2"
  65999. "77037D812DEB33A0F4A13945D898C296";
  66000. #ifndef HAVE_SELFTEST
  66001. EC_POINT *tmp = NULL;
  66002. size_t bin_len;
  66003. unsigned int blen = 0;
  66004. unsigned char* buf = NULL;
  66005. unsigned char bufInf[1] = { 0x00 };
  66006. const unsigned char binUncompG[] = {
  66007. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  66008. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  66009. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  66010. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  66011. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  66012. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  66013. };
  66014. const unsigned char binUncompGBad[] = {
  66015. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  66016. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  66017. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  66018. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  66019. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  66020. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  66021. };
  66022. #ifdef HAVE_COMP_KEY
  66023. const unsigned char binCompG[] = {
  66024. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  66025. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  66026. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  66027. };
  66028. #endif
  66029. #endif
  66030. ExpectNotNull(ctx = BN_CTX_new());
  66031. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  66032. #ifndef HAVE_ECC_BRAINPOOL
  66033. /* Used to make groups curve_idx == -1. */
  66034. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  66035. #endif
  66036. ExpectNull(EC_POINT_new(NULL));
  66037. ExpectNotNull(Gxy = EC_POINT_new(group));
  66038. ExpectNotNull(new_point = EC_POINT_new(group));
  66039. ExpectNotNull(set_point = EC_POINT_new(group));
  66040. ExpectNotNull(X = BN_new());
  66041. ExpectNotNull(Y = BN_new());
  66042. ExpectNotNull(set_point_bn = BN_new());
  66043. ExpectNotNull(infinity = EC_POINT_new(group));
  66044. /* load test values */
  66045. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  66046. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  66047. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  66048. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  66049. /* populate coordinates for input point */
  66050. if (Gxy != NULL) {
  66051. Gxy->X = Gx;
  66052. Gxy->Y = Gy;
  66053. Gxy->Z = Gz;
  66054. }
  66055. /* Test handling of NULL point. */
  66056. EC_POINT_clear_free(NULL);
  66057. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  66058. NULL, NULL, ctx), 0);
  66059. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  66060. NULL, NULL, ctx), 0);
  66061. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  66062. NULL, NULL, ctx), 0);
  66063. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  66064. X, NULL, ctx), 0);
  66065. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  66066. NULL, Y, ctx), 0);
  66067. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  66068. X, Y, ctx), 0);
  66069. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  66070. X, Y, ctx), 0);
  66071. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  66072. NULL, Y, ctx), 0);
  66073. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  66074. X, NULL, ctx), 0);
  66075. /* Getting point at infinity returns an error. */
  66076. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  66077. X, Y, ctx), 0);
  66078. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  66079. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  66080. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  66081. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  66082. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  66083. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  66084. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  66085. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  66086. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  66087. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  66088. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  66089. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  66090. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  66091. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  66092. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  66093. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  66094. /* perform point multiplication */
  66095. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  66096. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  66097. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  66098. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  66099. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  66100. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  66101. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  66102. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  66103. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  66104. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  66105. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  66106. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  66107. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  66108. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  66109. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  66110. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  66111. /* Set point to something. */
  66112. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  66113. #else
  66114. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  66115. ctx), 1);
  66116. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  66117. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  66118. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  66119. #endif
  66120. /* check if point X coordinate is zero */
  66121. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  66122. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  66123. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  66124. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  66125. #endif
  66126. /* extract the coordinates from point */
  66127. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  66128. ctx), WOLFSSL_SUCCESS);
  66129. /* check if point X coordinate is zero */
  66130. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  66131. /* set the same X and Y points in another object */
  66132. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  66133. ctx), WOLFSSL_SUCCESS);
  66134. /* compare points as they should be the same */
  66135. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  66136. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  66137. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  66138. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  66139. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  66140. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  66141. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  66142. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  66143. /* Test copying */
  66144. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  66145. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  66146. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  66147. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  66148. /* Test inverting */
  66149. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  66150. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  66151. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  66152. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  66153. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  66154. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  66155. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  66156. {
  66157. EC_POINT* orig_point = NULL;
  66158. ExpectNotNull(orig_point = EC_POINT_new(group));
  66159. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  66160. 1);
  66161. /* new_point should be set_point inverted so adding it will revert
  66162. * the point back to set_point */
  66163. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  66164. NULL), 1);
  66165. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  66166. EC_POINT_free(orig_point);
  66167. }
  66168. #endif
  66169. /* Test getting affine converts from projective. */
  66170. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  66171. /* Force non-affine coordinates */
  66172. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  66173. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  66174. if (new_point != NULL) {
  66175. new_point->inSet = 0;
  66176. }
  66177. /* extract the coordinates from point */
  66178. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  66179. ctx), WOLFSSL_SUCCESS);
  66180. /* check if point ordinates have changed. */
  66181. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  66182. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  66183. /* Test check for infinity */
  66184. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  66185. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  66186. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  66187. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  66188. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  66189. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  66190. #else
  66191. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  66192. #endif
  66193. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  66194. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  66195. /* check bn2hex */
  66196. hexStr = BN_bn2hex(k);
  66197. ExpectStrEQ(hexStr, kTest);
  66198. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  66199. defined(XFPRINTF)
  66200. BN_print_fp(stderr, k);
  66201. fprintf(stderr, "\n");
  66202. #endif
  66203. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  66204. hexStr = BN_bn2hex(Gx);
  66205. ExpectStrEQ(hexStr, kGx);
  66206. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  66207. defined(XFPRINTF)
  66208. BN_print_fp(stderr, Gx);
  66209. fprintf(stderr, "\n");
  66210. #endif
  66211. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  66212. hexStr = BN_bn2hex(Gy);
  66213. ExpectStrEQ(hexStr, kGy);
  66214. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  66215. defined(XFPRINTF)
  66216. BN_print_fp(stderr, Gy);
  66217. fprintf(stderr, "\n");
  66218. #endif
  66219. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  66220. /* Test point to hex */
  66221. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  66222. ctx));
  66223. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  66224. ctx));
  66225. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  66226. ctx));
  66227. #ifndef HAVE_ECC_BRAINPOOL
  66228. /* Group not supported in wolfCrypt. */
  66229. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  66230. ctx));
  66231. #endif
  66232. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  66233. ExpectNotNull(hexStr);
  66234. ExpectStrEQ(hexStr, uncompG);
  66235. ExpectNotNull(get_point = EC_POINT_hex2point(group, hexStr, NULL, ctx));
  66236. ExpectIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
  66237. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  66238. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  66239. ExpectNotNull(hexStr);
  66240. ExpectStrEQ(hexStr, compG);
  66241. #ifdef HAVE_COMP_KEY
  66242. ExpectNotNull(get_point = EC_POINT_hex2point
  66243. (group, hexStr, get_point, ctx));
  66244. ExpectIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
  66245. #endif
  66246. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  66247. EC_POINT_free(get_point);
  66248. #ifndef HAVE_SELFTEST
  66249. /* Test point to oct */
  66250. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  66251. NULL, 0, ctx), 0);
  66252. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  66253. NULL, 0, ctx), 0);
  66254. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  66255. NULL, 0, ctx), 0);
  66256. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  66257. NULL, 0, ctx);
  66258. ExpectIntEQ(bin_len, sizeof(binUncompG));
  66259. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  66260. DYNAMIC_TYPE_ECC));
  66261. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  66262. buf, bin_len, ctx), bin_len);
  66263. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  66264. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  66265. /* Infinity (x=0, y=0) encodes as '0x00'. */
  66266. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  66267. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  66268. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  66269. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  66270. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  66271. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  66272. ExpectIntEQ(bufInf[0], 0);
  66273. wolfSSL_EC_POINT_dump(NULL, NULL);
  66274. /* Test point i2d */
  66275. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  66276. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  66277. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  66278. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  66279. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  66280. ExpectIntEQ(blen, sizeof(binUncompG));
  66281. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  66282. blen -= 1;
  66283. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  66284. blen += 1;
  66285. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  66286. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  66287. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  66288. #ifdef HAVE_COMP_KEY
  66289. /* Test point to oct compressed */
  66290. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  66291. 0, ctx);
  66292. ExpectIntEQ(bin_len, sizeof(binCompG));
  66293. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  66294. DYNAMIC_TYPE_ECC));
  66295. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  66296. bin_len, ctx), bin_len);
  66297. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  66298. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  66299. #endif
  66300. /* Test point BN */
  66301. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  66302. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  66303. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  66304. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  66305. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  66306. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  66307. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  66308. /* Test oct to point */
  66309. ExpectNotNull(tmp = EC_POINT_new(group));
  66310. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  66311. ctx), 0);
  66312. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  66313. ctx), 0);
  66314. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  66315. ctx), 0);
  66316. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  66317. sizeof(binUncompGBad), ctx), 0);
  66318. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  66319. ctx), 1);
  66320. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  66321. EC_POINT_free(tmp);
  66322. tmp = NULL;
  66323. /* Test setting BN ordinates. */
  66324. ExpectNotNull(tmp = EC_POINT_new(group));
  66325. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  66326. NULL, ctx), 0);
  66327. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  66328. NULL, ctx), 0);
  66329. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  66330. NULL, ctx), 0);
  66331. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  66332. NULL, ctx), 0);
  66333. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  66334. Gy, ctx), 0);
  66335. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  66336. ctx), 0);
  66337. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  66338. ctx), 0);
  66339. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  66340. Gy, ctx), 0);
  66341. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  66342. NULL, ctx), 0);
  66343. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  66344. ctx), 1);
  66345. EC_POINT_free(tmp);
  66346. tmp = NULL;
  66347. /* Test point d2i */
  66348. ExpectNotNull(tmp = EC_POINT_new(group));
  66349. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  66350. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  66351. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  66352. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  66353. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  66354. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  66355. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  66356. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  66357. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  66358. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  66359. EC_POINT_free(tmp);
  66360. tmp = NULL;
  66361. #ifdef HAVE_COMP_KEY
  66362. /* Test oct compressed to point */
  66363. ExpectNotNull(tmp = EC_POINT_new(group));
  66364. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  66365. 1);
  66366. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  66367. EC_POINT_free(tmp);
  66368. tmp = NULL;
  66369. /* Test point d2i - compressed */
  66370. ExpectNotNull(tmp = EC_POINT_new(group));
  66371. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  66372. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  66373. EC_POINT_free(tmp);
  66374. tmp = NULL;
  66375. #endif
  66376. #endif
  66377. /* test BN_mod_add */
  66378. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  66379. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  66380. 1);
  66381. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  66382. /* cleanup */
  66383. BN_free(X);
  66384. BN_free(Y);
  66385. BN_free(k);
  66386. BN_free(set_point_bn);
  66387. EC_POINT_free(infinity);
  66388. EC_POINT_free(new_point);
  66389. EC_POINT_free(set_point);
  66390. EC_POINT_clear_free(Gxy);
  66391. #ifndef HAVE_ECC_BRAINPOOL
  66392. EC_GROUP_free(group2);
  66393. #endif
  66394. EC_GROUP_free(group);
  66395. BN_CTX_free(ctx);
  66396. #endif
  66397. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  66398. return EXPECT_RESULT();
  66399. }
  66400. static int test_wolfSSL_SPAKE(void)
  66401. {
  66402. EXPECT_DECLS;
  66403. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  66404. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  66405. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  66406. BIGNUM* x = NULL; /* kdc priv */
  66407. BIGNUM* y = NULL; /* client priv */
  66408. BIGNUM* w = NULL; /* shared value */
  66409. byte M_bytes[] = {
  66410. /* uncompressed */
  66411. 0x04,
  66412. /* x */
  66413. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  66414. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  66415. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  66416. /* y */
  66417. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  66418. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  66419. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  66420. };
  66421. EC_POINT* M = NULL; /* shared value */
  66422. byte N_bytes[] = {
  66423. /* uncompressed */
  66424. 0x04,
  66425. /* x */
  66426. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  66427. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  66428. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  66429. /* y */
  66430. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  66431. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  66432. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  66433. };
  66434. EC_POINT* N = NULL; /* shared value */
  66435. EC_POINT* T = NULL; /* kdc pub */
  66436. EC_POINT* tmp1 = NULL; /* kdc pub */
  66437. EC_POINT* tmp2 = NULL; /* kdc pub */
  66438. EC_POINT* S = NULL; /* client pub */
  66439. EC_POINT* client_secret = NULL;
  66440. EC_POINT* kdc_secret = NULL;
  66441. EC_GROUP* group = NULL;
  66442. BN_CTX* bn_ctx = NULL;
  66443. /* Values taken from a test run of Kerberos 5 */
  66444. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  66445. ExpectNotNull(bn_ctx = BN_CTX_new());
  66446. ExpectNotNull(M = EC_POINT_new(group));
  66447. ExpectNotNull(N = EC_POINT_new(group));
  66448. ExpectNotNull(T = EC_POINT_new(group));
  66449. ExpectNotNull(tmp1 = EC_POINT_new(group));
  66450. ExpectNotNull(tmp2 = EC_POINT_new(group));
  66451. ExpectNotNull(S = EC_POINT_new(group));
  66452. ExpectNotNull(client_secret = EC_POINT_new(group));
  66453. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  66454. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  66455. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  66456. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  66457. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  66458. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  66459. "506AB395478F0AAB647752CF117B36250"), 1);
  66460. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  66461. 1);
  66462. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  66463. 1);
  66464. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  66465. /* kdc */
  66466. /* T=x*P+w*M */
  66467. /* All in one function call */
  66468. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  66469. /* Spread into separate calls */
  66470. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  66471. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  66472. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  66473. 1);
  66474. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  66475. /* client */
  66476. /* S=y*P+w*N */
  66477. /* All in one function call */
  66478. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  66479. /* Spread into separate calls */
  66480. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  66481. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  66482. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  66483. 1);
  66484. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  66485. /* K=y*(T-w*M) */
  66486. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  66487. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  66488. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  66489. 1);
  66490. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  66491. bn_ctx), 1);
  66492. /* kdc */
  66493. /* K=x*(S-w*N) */
  66494. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  66495. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  66496. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  66497. 1);
  66498. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  66499. 1);
  66500. /* kdc_secret == client_secret */
  66501. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  66502. BN_free(x);
  66503. BN_free(y);
  66504. BN_free(w);
  66505. EC_POINT_free(M);
  66506. EC_POINT_free(N);
  66507. EC_POINT_free(T);
  66508. EC_POINT_free(tmp1);
  66509. EC_POINT_free(tmp2);
  66510. EC_POINT_free(S);
  66511. EC_POINT_free(client_secret);
  66512. EC_POINT_free(kdc_secret);
  66513. EC_GROUP_free(group);
  66514. BN_CTX_free(bn_ctx);
  66515. #endif
  66516. return EXPECT_RESULT();
  66517. }
  66518. static int test_wolfSSL_EC_KEY_generate(void)
  66519. {
  66520. EXPECT_DECLS;
  66521. #ifdef OPENSSL_EXTRA
  66522. WOLFSSL_EC_KEY* key = NULL;
  66523. #ifndef HAVE_ECC_BRAINPOOL
  66524. WOLFSSL_EC_GROUP* group = NULL;
  66525. #endif
  66526. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  66527. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  66528. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  66529. wolfSSL_EC_KEY_free(key);
  66530. key = NULL;
  66531. #ifndef HAVE_ECC_BRAINPOOL
  66532. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  66533. NID_brainpoolP256r1));
  66534. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  66535. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  66536. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  66537. wolfSSL_EC_KEY_free(key);
  66538. wolfSSL_EC_GROUP_free(group);
  66539. #endif
  66540. #endif
  66541. return EXPECT_RESULT();
  66542. }
  66543. static int test_EC_i2d(void)
  66544. {
  66545. EXPECT_DECLS;
  66546. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  66547. EC_KEY *key = NULL;
  66548. EC_KEY *copy = NULL;
  66549. int len = 0;
  66550. unsigned char *buf = NULL;
  66551. unsigned char *p = NULL;
  66552. const unsigned char *tmp = NULL;
  66553. const unsigned char octBad[] = {
  66554. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  66555. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  66556. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  66557. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  66558. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  66559. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  66560. };
  66561. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  66562. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  66563. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  66564. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  66565. DYNAMIC_TYPE_TMP_BUFFER));
  66566. p = buf;
  66567. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  66568. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  66569. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  66570. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  66571. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  66572. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  66573. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  66574. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  66575. tmp = buf;
  66576. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  66577. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  66578. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  66579. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  66580. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  66581. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  66582. tmp = buf;
  66583. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  66584. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  66585. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  66586. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  66587. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  66588. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  66589. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  66590. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  66591. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  66592. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  66593. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  66594. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  66595. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  66596. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  66597. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  66598. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  66599. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  66600. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  66601. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  66602. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  66603. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  66604. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  66605. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  66606. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  66607. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  66608. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  66609. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  66610. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  66611. buf = NULL;
  66612. buf = NULL;
  66613. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  66614. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  66615. DYNAMIC_TYPE_TMP_BUFFER));
  66616. p = buf;
  66617. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  66618. p = NULL;
  66619. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  66620. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  66621. p = NULL;
  66622. /* Bad point is also an invalid private key. */
  66623. tmp = octBad;
  66624. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  66625. tmp = buf;
  66626. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  66627. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  66628. buf = NULL;
  66629. buf = NULL;
  66630. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  66631. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  66632. DYNAMIC_TYPE_TMP_BUFFER));
  66633. p = buf;
  66634. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  66635. p = NULL;
  66636. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  66637. tmp = buf;
  66638. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  66639. tmp = octBad;
  66640. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  66641. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  66642. ExpectIntEQ(EC_KEY_check_key(key), 1);
  66643. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  66644. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  66645. EC_KEY_free(key);
  66646. EC_KEY_free(copy);
  66647. #endif
  66648. return EXPECT_RESULT();
  66649. }
  66650. static int test_wolfSSL_EC_curve(void)
  66651. {
  66652. EXPECT_DECLS;
  66653. #if defined(OPENSSL_EXTRA)
  66654. int nid = NID_secp160k1;
  66655. const char* nid_name = NULL;
  66656. ExpectNull(EC_curve_nid2nist(NID_sha256));
  66657. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  66658. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  66659. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  66660. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  66661. #endif
  66662. return EXPECT_RESULT();
  66663. }
  66664. static int test_wolfSSL_EC_KEY_dup(void)
  66665. {
  66666. EXPECT_DECLS;
  66667. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  66668. WOLFSSL_EC_KEY* ecKey = NULL;
  66669. WOLFSSL_EC_KEY* dupKey = NULL;
  66670. ecc_key* srcKey = NULL;
  66671. ecc_key* destKey = NULL;
  66672. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  66673. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  66674. /* Valid cases */
  66675. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  66676. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  66677. /* Compare pubkey */
  66678. if (ecKey != NULL) {
  66679. srcKey = (ecc_key*)ecKey->internal;
  66680. }
  66681. if (dupKey != NULL) {
  66682. destKey = (ecc_key*)dupKey->internal;
  66683. }
  66684. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  66685. /* compare EC_GROUP */
  66686. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  66687. /* compare EC_POINT */
  66688. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  66689. dupKey->pub_key, NULL), MP_EQ);
  66690. /* compare BIGNUM */
  66691. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  66692. wolfSSL_EC_KEY_free(dupKey);
  66693. dupKey = NULL;
  66694. /* Invalid cases */
  66695. /* NULL key */
  66696. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  66697. /* NULL ecc_key */
  66698. if (ecKey != NULL) {
  66699. wc_ecc_free((ecc_key*)ecKey->internal);
  66700. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  66701. ecKey->internal = NULL; /* Set ecc_key to NULL */
  66702. }
  66703. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  66704. wolfSSL_EC_KEY_free(ecKey);
  66705. ecKey = NULL;
  66706. wolfSSL_EC_KEY_free(dupKey);
  66707. dupKey = NULL;
  66708. /* NULL Group */
  66709. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  66710. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  66711. if (ecKey != NULL) {
  66712. wolfSSL_EC_GROUP_free(ecKey->group);
  66713. ecKey->group = NULL; /* Set group to NULL */
  66714. }
  66715. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  66716. wolfSSL_EC_KEY_free(ecKey);
  66717. ecKey = NULL;
  66718. wolfSSL_EC_KEY_free(dupKey);
  66719. dupKey = NULL;
  66720. /* NULL public key */
  66721. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  66722. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  66723. if (ecKey != NULL) {
  66724. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  66725. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  66726. }
  66727. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  66728. if (ecKey != NULL) {
  66729. wolfSSL_EC_POINT_free(ecKey->pub_key);
  66730. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  66731. }
  66732. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  66733. wolfSSL_EC_KEY_free(ecKey);
  66734. ecKey = NULL;
  66735. wolfSSL_EC_KEY_free(dupKey);
  66736. dupKey = NULL;
  66737. /* NULL private key */
  66738. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  66739. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  66740. if (ecKey != NULL) {
  66741. wolfSSL_BN_free(ecKey->priv_key);
  66742. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  66743. }
  66744. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  66745. wolfSSL_EC_KEY_free(ecKey);
  66746. ecKey = NULL;
  66747. wolfSSL_EC_KEY_free(dupKey);
  66748. dupKey = NULL;
  66749. /* Test EC_KEY_up_ref */
  66750. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  66751. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  66752. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  66753. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  66754. /* reference count doesn't follow duplicate */
  66755. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  66756. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  66757. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  66758. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  66759. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  66760. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  66761. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  66762. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  66763. #endif
  66764. return EXPECT_RESULT();
  66765. }
  66766. static int test_wolfSSL_EC_KEY_set_group(void)
  66767. {
  66768. EXPECT_DECLS;
  66769. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  66770. defined(OPENSSL_EXTRA)
  66771. EC_KEY *key = NULL;
  66772. EC_GROUP *group = NULL;
  66773. const EC_GROUP *group2 = NULL;
  66774. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  66775. ExpectNotNull(key = EC_KEY_new());
  66776. ExpectNull(EC_KEY_get0_group(NULL));
  66777. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  66778. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  66779. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  66780. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  66781. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  66782. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  66783. EC_GROUP_free(group);
  66784. EC_KEY_free(key);
  66785. #endif
  66786. return EXPECT_RESULT();
  66787. }
  66788. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  66789. {
  66790. EXPECT_DECLS;
  66791. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  66792. BIO* bio = NULL;
  66793. EC_KEY* key = NULL;
  66794. /* Error condition: NULL key. */
  66795. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  66796. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  66797. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  66798. /* Conversion form defaults to uncompressed. */
  66799. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  66800. #ifdef HAVE_COMP_KEY
  66801. /* Explicitly set to compressed. */
  66802. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  66803. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  66804. #else
  66805. /* Will still work just won't change anything. */
  66806. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  66807. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  66808. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  66809. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  66810. #endif
  66811. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  66812. BIO_free(bio);
  66813. EC_KEY_free(key);
  66814. #endif
  66815. return EXPECT_RESULT();
  66816. }
  66817. static int test_wolfSSL_EC_KEY_private_key(void)
  66818. {
  66819. EXPECT_DECLS;
  66820. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  66821. WOLFSSL_EC_KEY* key = NULL;
  66822. WOLFSSL_BIGNUM* priv = NULL;
  66823. WOLFSSL_BIGNUM* priv2 = NULL;
  66824. WOLFSSL_BIGNUM* bn;
  66825. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  66826. ExpectNotNull(priv = wolfSSL_BN_new());
  66827. ExpectNotNull(priv2 = wolfSSL_BN_new());
  66828. ExpectIntNE(BN_set_word(priv, 2), 0);
  66829. ExpectIntNE(BN_set_word(priv2, 2), 0);
  66830. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  66831. /* No private key set. */
  66832. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  66833. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  66834. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  66835. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  66836. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  66837. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  66838. ExpectPtrNE(bn, priv);
  66839. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  66840. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  66841. ExpectPtrNE(bn, priv2);
  66842. wolfSSL_BN_free(priv2);
  66843. wolfSSL_BN_free(priv);
  66844. wolfSSL_EC_KEY_free(key);
  66845. #endif
  66846. return EXPECT_RESULT();
  66847. }
  66848. static int test_wolfSSL_EC_KEY_public_key(void)
  66849. {
  66850. EXPECT_DECLS;
  66851. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  66852. WOLFSSL_EC_KEY* key = NULL;
  66853. WOLFSSL_EC_POINT* pub = NULL;
  66854. WOLFSSL_EC_POINT* point = NULL;
  66855. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  66856. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  66857. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  66858. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  66859. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  66860. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  66861. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  66862. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  66863. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  66864. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  66865. ExpectPtrEq(point, pub);
  66866. wolfSSL_EC_KEY_free(key);
  66867. #endif
  66868. return EXPECT_RESULT();
  66869. }
  66870. static int test_wolfSSL_EC_KEY_print_fp(void)
  66871. {
  66872. EXPECT_DECLS;
  66873. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  66874. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  66875. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  66876. !defined(NO_STDIO_FILESYSTEM)
  66877. EC_KEY* key = NULL;
  66878. /* Bad file pointer. */
  66879. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  66880. /* NULL key. */
  66881. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  66882. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  66883. /* Negative indent. */
  66884. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  66885. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  66886. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  66887. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  66888. wolfSSL_EC_KEY_free(key);
  66889. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  66890. NID_X9_62_prime256v1)));
  66891. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  66892. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  66893. wolfSSL_EC_KEY_free(key);
  66894. #endif
  66895. return EXPECT_RESULT();
  66896. }
  66897. static int test_wolfSSL_EC_get_builtin_curves(void)
  66898. {
  66899. EXPECT_DECLS;
  66900. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  66901. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  66902. EC_builtin_curve* curves = NULL;
  66903. size_t crv_len = 0;
  66904. size_t i = 0;
  66905. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  66906. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  66907. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  66908. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  66909. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  66910. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  66911. if (curves[i].comment != NULL) {
  66912. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  66913. }
  66914. }
  66915. if (crv_len > 1) {
  66916. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  66917. }
  66918. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  66919. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  66920. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  66921. return EXPECT_RESULT();
  66922. }
  66923. static int test_wolfSSL_ECDSA_SIG(void)
  66924. {
  66925. EXPECT_DECLS;
  66926. #ifdef OPENSSL_EXTRA
  66927. WOLFSSL_ECDSA_SIG* sig = NULL;
  66928. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  66929. WOLFSSL_BIGNUM* r = NULL;
  66930. WOLFSSL_BIGNUM* s = NULL;
  66931. const WOLFSSL_BIGNUM* r2 = NULL;
  66932. const WOLFSSL_BIGNUM* s2 = NULL;
  66933. const unsigned char* cp = NULL;
  66934. unsigned char* p = NULL;
  66935. unsigned char outSig[8];
  66936. unsigned char sigData[8] =
  66937. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  66938. unsigned char sigDataBad[8] =
  66939. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  66940. wolfSSL_ECDSA_SIG_free(NULL);
  66941. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  66942. ExpectNotNull(r = wolfSSL_BN_new());
  66943. ExpectNotNull(s = wolfSSL_BN_new());
  66944. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  66945. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  66946. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  66947. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  66948. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  66949. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  66950. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  66951. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  66952. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  66953. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  66954. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  66955. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  66956. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  66957. r2 = NULL;
  66958. s2 = NULL;
  66959. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  66960. ExpectNull(r2);
  66961. ExpectNull(s2);
  66962. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  66963. if (EXPECT_FAIL()) {
  66964. wolfSSL_BN_free(r);
  66965. wolfSSL_BN_free(s);
  66966. }
  66967. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  66968. ExpectPtrEq(r2, r);
  66969. ExpectPtrEq(s2, s);
  66970. r2 = NULL;
  66971. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  66972. ExpectPtrEq(r2, r);
  66973. s2 = NULL;
  66974. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  66975. ExpectPtrEq(s2, s);
  66976. /* r and s are freed when sig is freed. */
  66977. wolfSSL_ECDSA_SIG_free(sig);
  66978. sig = NULL;
  66979. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  66980. cp = sigDataBad;
  66981. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  66982. cp = sigData;
  66983. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  66984. ExpectIntEQ((cp == sigData + 8), 1);
  66985. cp = sigData;
  66986. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  66987. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  66988. ExpectIntEQ((sig == sig2), 1);
  66989. cp = outSig;
  66990. p = outSig;
  66991. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  66992. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  66993. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  66994. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  66995. ExpectIntEQ((p == outSig + 8), 1);
  66996. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  66997. p = NULL;
  66998. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), 8);
  66999. #ifndef WOLFSSL_I2D_ECDSA_SIG_ALLOC
  67000. ExpectNull(p);
  67001. #else
  67002. ExpectNotNull(p);
  67003. ExpectIntEQ(XMEMCMP(p, outSig, 8), 0);
  67004. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  67005. #endif
  67006. wolfSSL_ECDSA_SIG_free(sig);
  67007. #endif
  67008. return EXPECT_RESULT();
  67009. }
  67010. static int test_ECDSA_size_sign(void)
  67011. {
  67012. EXPECT_DECLS;
  67013. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  67014. EC_KEY* key = NULL;
  67015. ECDSA_SIG* ecdsaSig = NULL;
  67016. int id;
  67017. byte hash[WC_MAX_DIGEST_SIZE];
  67018. byte hash2[WC_MAX_DIGEST_SIZE];
  67019. byte sig[ECC_MAX_SIG_SIZE];
  67020. unsigned int sigSz = sizeof(sig);
  67021. XMEMSET(hash, 123, sizeof(hash));
  67022. XMEMSET(hash2, 234, sizeof(hash2));
  67023. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  67024. ExpectIntEQ(id, ECC_SECP256R1);
  67025. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  67026. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  67027. ExpectIntGE(ECDSA_size(NULL), 0);
  67028. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  67029. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  67030. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  67031. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, NULL), 0);
  67032. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, (int)sigSz, key), 0);
  67033. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, (int)sigSz, key), 0);
  67034. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  67035. ExpectIntGE(ECDSA_size(key), sigSz);
  67036. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, key), 1);
  67037. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, (int)sigSz, key), 0);
  67038. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  67039. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  67040. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  67041. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  67042. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  67043. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  67044. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  67045. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  67046. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  67047. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  67048. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  67049. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  67050. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  67051. ECDSA_SIG_free(ecdsaSig);
  67052. EC_KEY_free(key);
  67053. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  67054. return EXPECT_RESULT();
  67055. }
  67056. static int test_ECDH_compute_key(void)
  67057. {
  67058. EXPECT_DECLS;
  67059. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  67060. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  67061. EC_KEY* key1 = NULL;
  67062. EC_KEY* key2 = NULL;
  67063. EC_POINT* pub1 = NULL;
  67064. EC_POINT* pub2 = NULL;
  67065. byte secret1[32];
  67066. byte secret2[32];
  67067. int i;
  67068. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  67069. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  67070. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  67071. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  67072. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  67073. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  67074. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  67075. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  67076. 0);
  67077. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  67078. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  67079. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  67080. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  67081. 0);
  67082. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  67083. 0);
  67084. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  67085. NULL), 0);
  67086. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  67087. sizeof(secret1));
  67088. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  67089. sizeof(secret2));
  67090. for (i = 0; i < (int)sizeof(secret1); i++) {
  67091. ExpectIntEQ(secret1[i], secret2[i]);
  67092. }
  67093. EC_KEY_free(key2);
  67094. EC_KEY_free(key1);
  67095. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  67096. * !WOLF_CRYPTO_CB_ONLY_ECC */
  67097. return EXPECT_RESULT();
  67098. }
  67099. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  67100. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  67101. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  67102. !defined(NO_ASN_TIME)
  67103. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  67104. int expectedDerSz)
  67105. {
  67106. EXPECT_DECLS;
  67107. X509* x509 = NULL;
  67108. BIGNUM* serial_number = NULL;
  67109. X509_NAME* name = NULL;
  67110. time_t epoch_off = 0;
  67111. ASN1_INTEGER* asn1_serial_number;
  67112. long not_before, not_after;
  67113. int derSz;
  67114. ExpectNotNull(x509 = X509_new());
  67115. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  67116. ExpectNotNull(serial_number = BN_new());
  67117. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  67118. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  67119. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  67120. /* version 3 */
  67121. ExpectIntNE(X509_set_version(x509, 2L), 0);
  67122. ExpectNotNull(name = X509_NAME_new());
  67123. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  67124. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  67125. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  67126. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  67127. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  67128. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  67129. not_before = (long)wc_Time(NULL);
  67130. not_after = not_before + (365 * 24 * 60 * 60);
  67131. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  67132. &epoch_off));
  67133. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  67134. &epoch_off));
  67135. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  67136. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  67137. ExpectIntGE(derSz, expectedDerSz);
  67138. BN_free(serial_number);
  67139. X509_NAME_free(name);
  67140. X509_free(x509);
  67141. return EXPECT_RESULT();
  67142. }
  67143. #endif
  67144. static int test_openssl_generate_key_and_cert(void)
  67145. {
  67146. EXPECT_DECLS;
  67147. #if defined(OPENSSL_EXTRA)
  67148. int expectedDerSz;
  67149. EVP_PKEY* pkey = NULL;
  67150. #ifdef HAVE_ECC
  67151. EC_KEY* ec_key = NULL;
  67152. #endif
  67153. #if !defined(NO_RSA)
  67154. int key_length = 2048;
  67155. BIGNUM* exponent = NULL;
  67156. RSA* rsa = NULL;
  67157. ExpectNotNull(pkey = EVP_PKEY_new());
  67158. ExpectNotNull(exponent = BN_new());
  67159. ExpectNotNull(rsa = RSA_new());
  67160. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  67161. #ifndef WOLFSSL_KEY_GEN
  67162. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  67163. #if defined(USE_CERT_BUFFERS_1024)
  67164. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  67165. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  67166. key_length = 1024;
  67167. #elif defined(USE_CERT_BUFFERS_2048)
  67168. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  67169. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  67170. #else
  67171. RSA_free(rsa);
  67172. rsa = NULL;
  67173. #endif
  67174. #else
  67175. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  67176. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  67177. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  67178. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  67179. #endif
  67180. if (rsa) {
  67181. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  67182. if (EXPECT_FAIL()) {
  67183. RSA_free(rsa);
  67184. }
  67185. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  67186. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  67187. expectedDerSz = 743;
  67188. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  67189. expectedDerSz), TEST_SUCCESS);
  67190. #endif
  67191. }
  67192. EVP_PKEY_free(pkey);
  67193. pkey = NULL;
  67194. BN_free(exponent);
  67195. #endif /* !NO_RSA */
  67196. #ifdef HAVE_ECC
  67197. ExpectNotNull(pkey = EVP_PKEY_new());
  67198. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  67199. #ifndef NO_WOLFSSL_STUB
  67200. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  67201. #endif
  67202. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  67203. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  67204. if (EXPECT_FAIL()) {
  67205. EC_KEY_free(ec_key);
  67206. }
  67207. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  67208. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  67209. expectedDerSz = 344;
  67210. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  67211. TEST_SUCCESS);
  67212. #endif
  67213. EVP_PKEY_free(pkey);
  67214. #endif /* HAVE_ECC */
  67215. (void)pkey;
  67216. (void)expectedDerSz;
  67217. #endif /* OPENSSL_EXTRA */
  67218. return EXPECT_RESULT();
  67219. }
  67220. static int test_stubs_are_stubs(void)
  67221. {
  67222. EXPECT_DECLS;
  67223. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  67224. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  67225. WOLFSSL_CTX* ctx = NULL;
  67226. WOLFSSL_CTX* ctxN = NULL;
  67227. #ifndef NO_WOLFSSL_CLIENT
  67228. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  67229. #elif !defined(NO_WOLFSSL_SERVER)
  67230. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  67231. #endif
  67232. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  67233. ExpectIntEQ((int) x(z), 0)
  67234. /* test logic, all stubs return same result regardless of ctx being NULL
  67235. * as there are no sanity checks, it's just a stub! If at some
  67236. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  67237. * if invalid inputs are supplied. Test calling both
  67238. * with and without valid inputs, if a stub functionality remains unchanged.
  67239. */
  67240. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  67241. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  67242. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  67243. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  67244. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  67245. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  67246. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  67247. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  67248. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  67249. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  67250. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  67251. /* when implemented this should take WOLFSSL object instead, right now
  67252. * always returns 0 */
  67253. ExpectPtrEq(SSL_get_current_expansion(NULL), NULL);
  67254. wolfSSL_CTX_free(ctx);
  67255. ctx = NULL;
  67256. ExpectStrEQ(SSL_COMP_get_name(NULL), "not supported");
  67257. ExpectPtrEq(SSL_get_current_expansion(NULL), NULL);
  67258. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  67259. * !NO_WOLFSSL_SERVER) */
  67260. return EXPECT_RESULT();
  67261. }
  67262. static int test_CONF_modules_xxx(void)
  67263. {
  67264. int res = TEST_SKIPPED;
  67265. #if defined(OPENSSL_EXTRA)
  67266. CONF_modules_free();
  67267. CONF_modules_unload(0);
  67268. CONF_modules_unload(1);
  67269. CONF_modules_unload(-1);
  67270. res = TEST_SUCCESS;
  67271. #endif /* OPENSSL_EXTRA */
  67272. return res;
  67273. }
  67274. static int test_CRYPTO_set_dynlock_xxx(void)
  67275. {
  67276. int res = TEST_SKIPPED;
  67277. #if defined(OPENSSL_EXTRA)
  67278. CRYPTO_set_dynlock_create_callback(
  67279. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  67280. CRYPTO_set_dynlock_create_callback(
  67281. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  67282. CRYPTO_set_dynlock_destroy_callback(
  67283. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  67284. CRYPTO_set_dynlock_destroy_callback(
  67285. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  67286. CRYPTO_set_dynlock_lock_callback(
  67287. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  67288. CRYPTO_set_dynlock_lock_callback(
  67289. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  67290. res = TEST_SUCCESS;
  67291. #endif /* OPENSSL_EXTRA */
  67292. return res;
  67293. }
  67294. static int test_CRYPTO_THREADID_xxx(void)
  67295. {
  67296. EXPECT_DECLS;
  67297. #if defined(OPENSSL_EXTRA)
  67298. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  67299. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  67300. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  67301. #endif /* OPENSSL_EXTRA */
  67302. return EXPECT_RESULT();
  67303. }
  67304. static int test_ENGINE_cleanup(void)
  67305. {
  67306. int res = TEST_SKIPPED;
  67307. #if defined(OPENSSL_EXTRA)
  67308. ENGINE_cleanup();
  67309. res = TEST_SUCCESS;
  67310. #endif /* OPENSSL_EXTRA */
  67311. return res;
  67312. }
  67313. static int test_wolfSSL_CTX_LoadCRL(void)
  67314. {
  67315. EXPECT_DECLS;
  67316. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  67317. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  67318. WOLFSSL_CTX* ctx = NULL;
  67319. WOLFSSL* ssl = NULL;
  67320. const char* badPath = "dummypath";
  67321. const char* validPath = "./certs/crl";
  67322. const char* validFilePath = "./certs/crl/cliCrl.pem";
  67323. const char* issuerCert = "./certs/client-cert.pem";
  67324. int derType = WOLFSSL_FILETYPE_ASN1;
  67325. int pemType = WOLFSSL_FILETYPE_PEM;
  67326. #ifdef HAVE_CRL_MONITOR
  67327. int monitor = WOLFSSL_CRL_MONITOR;
  67328. #else
  67329. int monitor = 0;
  67330. #endif
  67331. WOLFSSL_CERT_MANAGER* cm = NULL;
  67332. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  67333. BAD_FUNC_ARG)
  67334. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  67335. NOT_COMPILED_IN)
  67336. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  67337. WOLFSSL_SUCCESS)
  67338. #ifndef NO_WOLFSSL_CLIENT
  67339. #define NEW_CTX(ctx) ExpectNotNull( \
  67340. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  67341. #elif !defined(NO_WOLFSSL_SERVER)
  67342. #define NEW_CTX(ctx) ExpectNotNull( \
  67343. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  67344. #else
  67345. #define NEW_CTX(ctx) return
  67346. #endif
  67347. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  67348. NEW_CTX(ctx);
  67349. #ifndef HAVE_CRL_MONITOR
  67350. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  67351. wolfSSL_CTX_free(ctx);
  67352. NEW_CTX(ctx);
  67353. #endif
  67354. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  67355. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  67356. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  67357. wolfSSL_CTX_free(ctx);
  67358. ctx = NULL;
  67359. NEW_CTX(ctx);
  67360. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  67361. WOLFSSL_SUCCESS);
  67362. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  67363. wolfSSL_CTX_free(ctx);
  67364. ctx = NULL;
  67365. NEW_CTX(ctx);
  67366. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  67367. WOLFSSL_SUCCESS);
  67368. ExpectNotNull(ssl = wolfSSL_new(ctx));
  67369. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  67370. wolfSSL_free(ssl);
  67371. ssl = NULL;
  67372. wolfSSL_CTX_free(ctx);
  67373. ctx = NULL;
  67374. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  67375. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  67376. WOLFSSL_SUCCESS);
  67377. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  67378. WOLFSSL_SUCCESS);
  67379. wolfSSL_CertManagerFree(cm);
  67380. #endif
  67381. return EXPECT_RESULT();
  67382. }
  67383. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL) && \
  67384. !defined(WOLFSSL_CRL_ALLOW_MISSING_CDP)
  67385. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  67386. {
  67387. EXPECT_DECLS;
  67388. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  67389. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  67390. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  67391. return EXPECT_RESULT();
  67392. }
  67393. #endif
  67394. static int test_multiple_crls_same_issuer(void)
  67395. {
  67396. EXPECT_DECLS;
  67397. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL) && \
  67398. !defined(WOLFSSL_CRL_ALLOW_MISSING_CDP)
  67399. test_ssl_cbf client_cbs, server_cbs;
  67400. struct {
  67401. const char* server_cert;
  67402. const char* server_key;
  67403. } test_params[] = {
  67404. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  67405. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  67406. };
  67407. size_t i;
  67408. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  67409. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  67410. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  67411. server_cbs.certPemFile = test_params[i].server_cert;
  67412. server_cbs.keyPemFile = test_params[i].server_key;
  67413. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  67414. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  67415. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  67416. &server_cbs, NULL), TEST_FAIL);
  67417. }
  67418. #endif
  67419. return EXPECT_RESULT();
  67420. }
  67421. static int test_SetTmpEC_DHE_Sz(void)
  67422. {
  67423. EXPECT_DECLS;
  67424. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  67425. WOLFSSL_CTX *ctx = NULL;
  67426. WOLFSSL *ssl = NULL;
  67427. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  67428. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  67429. ExpectNotNull(ssl = wolfSSL_new(ctx));
  67430. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  67431. wolfSSL_free(ssl);
  67432. wolfSSL_CTX_free(ctx);
  67433. #endif
  67434. return EXPECT_RESULT();
  67435. }
  67436. static int test_wolfSSL_CTX_get0_privatekey(void)
  67437. {
  67438. EXPECT_DECLS;
  67439. #ifdef OPENSSL_ALL
  67440. WOLFSSL_CTX* ctx = NULL;
  67441. (void)ctx;
  67442. #ifndef NO_RSA
  67443. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  67444. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  67445. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  67446. WOLFSSL_FILETYPE_PEM));
  67447. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  67448. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  67449. WOLFSSL_FILETYPE_PEM));
  67450. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  67451. wolfSSL_CTX_free(ctx);
  67452. ctx = NULL;
  67453. #endif
  67454. #ifdef HAVE_ECC
  67455. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  67456. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  67457. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  67458. WOLFSSL_FILETYPE_PEM));
  67459. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  67460. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  67461. WOLFSSL_FILETYPE_PEM));
  67462. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  67463. wolfSSL_CTX_free(ctx);
  67464. #endif
  67465. #endif
  67466. return EXPECT_RESULT();
  67467. }
  67468. static int test_wolfSSL_dtls_set_mtu(void)
  67469. {
  67470. EXPECT_DECLS;
  67471. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  67472. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  67473. !defined(WOLFSSL_NO_TLS12)
  67474. WOLFSSL_CTX* ctx = NULL;
  67475. WOLFSSL* ssl = NULL;
  67476. const char* testCertFile;
  67477. const char* testKeyFile;
  67478. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  67479. #ifndef NO_RSA
  67480. testCertFile = svrCertFile;
  67481. testKeyFile = svrKeyFile;
  67482. #elif defined(HAVE_ECC)
  67483. testCertFile = eccCertFile;
  67484. testKeyFile = eccKeyFile;
  67485. #endif
  67486. if (testCertFile != NULL && testKeyFile != NULL) {
  67487. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  67488. WOLFSSL_FILETYPE_PEM));
  67489. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  67490. WOLFSSL_FILETYPE_PEM));
  67491. }
  67492. ExpectNotNull(ssl = wolfSSL_new(ctx));
  67493. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  67494. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  67495. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  67496. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  67497. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  67498. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  67499. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  67500. wolfSSL_free(ssl);
  67501. wolfSSL_CTX_free(ctx);
  67502. #endif
  67503. return EXPECT_RESULT();
  67504. }
  67505. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  67506. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  67507. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  67508. enum HandShakeType hsType, word16 length)
  67509. {
  67510. size_t idx = 0;
  67511. byte* l;
  67512. /* record layer */
  67513. /* handshake type */
  67514. out[idx++] = handshake;
  67515. /* protocol version */
  67516. out[idx++] = 0xfe;
  67517. out[idx++] = 0xfd; /* DTLS 1.2 */
  67518. /* epoch 0 */
  67519. XMEMSET(out + idx, 0, 2);
  67520. idx += 2;
  67521. /* sequence number */
  67522. XMEMSET(out + idx, 0, 6);
  67523. c32toa(seq, out + idx + 2);
  67524. idx += 6;
  67525. /* length in BE */
  67526. if (length)
  67527. c16toa(length, out + idx);
  67528. else
  67529. c16toa(outSz - idx - 2, out + idx);
  67530. idx += 2;
  67531. /* handshake layer */
  67532. /* handshake type */
  67533. out[idx++] = (byte)hsType;
  67534. /* length */
  67535. l = out + idx;
  67536. idx += 3;
  67537. /* message seq */
  67538. c16toa(0, out + idx);
  67539. idx += 2;
  67540. /* frag offset */
  67541. c32to24(0, out + idx);
  67542. idx += 3;
  67543. /* frag length */
  67544. c32to24((word32)outSz - (word32)idx - 3, l);
  67545. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  67546. idx += 3;
  67547. XMEMSET(out + idx, 0, outSz - idx);
  67548. }
  67549. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  67550. {
  67551. byte msg[] = "This is a msg for the client";
  67552. byte reply[40];
  67553. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  67554. reply[sizeof(reply) - 1] = '\0';
  67555. fprintf(stderr, "Client message: %s\n", reply);
  67556. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  67557. }
  67558. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  67559. {
  67560. byte ch[50];
  67561. int fd = wolfSSL_get_fd(ssl);
  67562. byte msg[] = "This is a msg for the server";
  67563. byte reply[40];
  67564. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  67565. /* Server should ignore this datagram */
  67566. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  67567. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  67568. /* Server should ignore this datagram */
  67569. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  67570. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  67571. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  67572. reply[sizeof(reply) - 1] = '\0';
  67573. fprintf(stderr, "Server response: %s\n", reply);
  67574. }
  67575. static int test_wolfSSL_dtls_plaintext(void)
  67576. {
  67577. callback_functions func_cb_client;
  67578. callback_functions func_cb_server;
  67579. size_t i;
  67580. struct test_params {
  67581. method_provider client_meth;
  67582. method_provider server_meth;
  67583. ssl_callback on_result_server;
  67584. ssl_callback on_result_client;
  67585. } params[] = {
  67586. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  67587. test_wolfSSL_dtls_plaintext_server,
  67588. test_wolfSSL_dtls_plaintext_client},
  67589. };
  67590. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  67591. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  67592. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  67593. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  67594. func_cb_server.method = params[i].server_meth;
  67595. func_cb_client.method = params[i].client_meth;
  67596. func_cb_client.on_result = params[i].on_result_client;
  67597. func_cb_server.on_result = params[i].on_result_server;
  67598. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  67599. if (!func_cb_client.return_code)
  67600. return TEST_FAIL;
  67601. if (!func_cb_server.return_code)
  67602. return TEST_FAIL;
  67603. }
  67604. return TEST_RES_CHECK(1);
  67605. }
  67606. #else
  67607. static int test_wolfSSL_dtls_plaintext(void) {
  67608. return TEST_SKIPPED;
  67609. }
  67610. #endif
  67611. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  67612. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  67613. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  67614. {
  67615. byte b[1100]; /* buffer for the messages to send */
  67616. size_t idx = 0;
  67617. size_t seq_offset = 0;
  67618. size_t msg_offset = 0;
  67619. int i;
  67620. int fd = wolfSSL_get_fd(ssl);
  67621. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  67622. word32 seq_number = 100; /* start high so server definitely reads this */
  67623. word16 msg_number = 50; /* start high so server has to buffer this */
  67624. AssertIntEQ(ret, 1);
  67625. /* Now let's start spamming the peer with fragments it needs to store */
  67626. XMEMSET(b, -1, sizeof(b));
  67627. /* record layer */
  67628. /* handshake type */
  67629. b[idx++] = 22;
  67630. /* protocol version */
  67631. b[idx++] = 0xfe;
  67632. b[idx++] = 0xfd; /* DTLS 1.2 */
  67633. /* epoch 0 */
  67634. XMEMSET(b + idx, 0, 2);
  67635. idx += 2;
  67636. /* sequence number */
  67637. XMEMSET(b + idx, 0, 6);
  67638. seq_offset = idx + 2; /* increment only the low 32 bits */
  67639. idx += 6;
  67640. /* static length in BE */
  67641. c16toa(42, b + idx);
  67642. idx += 2;
  67643. /* handshake layer */
  67644. /* cert type */
  67645. b[idx++] = 11;
  67646. /* length */
  67647. c32to24(1000, b + idx);
  67648. idx += 3;
  67649. /* message seq */
  67650. c16toa(0, b + idx);
  67651. msg_offset = idx;
  67652. idx += 2;
  67653. /* frag offset */
  67654. c32to24(500, b + idx);
  67655. idx += 3;
  67656. /* frag length */
  67657. c32to24(30, b + idx);
  67658. idx += 3;
  67659. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  67660. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  67661. seq_number++, msg_number++, i++) {
  67662. struct timespec delay;
  67663. XMEMSET(&delay, 0, sizeof(delay));
  67664. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  67665. c32toa(seq_number, b + seq_offset);
  67666. c16toa(msg_number, b + msg_offset);
  67667. ret = (int)send(fd, b, 55, 0);
  67668. nanosleep(&delay, NULL);
  67669. }
  67670. }
  67671. #ifdef WOLFSSL_DTLS13
  67672. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  67673. {
  67674. const word16 sendCountMax = 100;
  67675. byte b[150]; /* buffer for the messages to send */
  67676. size_t idx = 0;
  67677. size_t msg_offset = 0;
  67678. int fd = wolfSSL_get_fd(ssl);
  67679. word16 msg_number = 10; /* start high so server has to buffer this */
  67680. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  67681. AssertIntEQ(ret, 1);
  67682. /* Now let's start spamming the peer with fragments it needs to store */
  67683. XMEMSET(b, -1, sizeof(b));
  67684. /* handshake type */
  67685. b[idx++] = 11;
  67686. /* length */
  67687. c32to24(10000, b + idx);
  67688. idx += 3;
  67689. /* message_seq */
  67690. msg_offset = idx;
  67691. idx += 2;
  67692. /* fragment_offset */
  67693. c32to24(5000, b + idx);
  67694. idx += 3;
  67695. /* fragment_length */
  67696. c32to24(100, b + idx);
  67697. idx += 3;
  67698. /* fragment contents */
  67699. idx += 100;
  67700. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  67701. byte sendBuf[150];
  67702. int sendSz = sizeof(sendBuf);
  67703. struct timespec delay;
  67704. XMEMSET(&delay, 0, sizeof(delay));
  67705. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  67706. c16toa(msg_number, b + msg_offset);
  67707. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  67708. (int)idx, handshake, 0, 0, 0);
  67709. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  67710. nanosleep(&delay, NULL);
  67711. }
  67712. }
  67713. #endif
  67714. static int test_wolfSSL_dtls_fragments(void)
  67715. {
  67716. EXPECT_DECLS;
  67717. callback_functions func_cb_client;
  67718. callback_functions func_cb_server;
  67719. size_t i;
  67720. struct test_params {
  67721. method_provider client_meth;
  67722. method_provider server_meth;
  67723. ssl_callback spammer;
  67724. } params[] = {
  67725. #if !defined(WOLFSSL_NO_TLS12)
  67726. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  67727. test_wolfSSL_dtls12_fragments_spammer},
  67728. #endif
  67729. #ifdef WOLFSSL_DTLS13
  67730. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  67731. test_wolfSSL_dtls13_fragments_spammer},
  67732. #endif
  67733. };
  67734. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  67735. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  67736. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  67737. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  67738. func_cb_server.method = params[i].server_meth;
  67739. func_cb_client.method = params[i].client_meth;
  67740. func_cb_client.ssl_ready = params[i].spammer;
  67741. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  67742. ExpectFalse(func_cb_client.return_code);
  67743. ExpectFalse(func_cb_server.return_code);
  67744. /* The socket should be closed by the server resulting in a
  67745. * socket error, fatal error or reading a close notify alert */
  67746. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  67747. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  67748. func_cb_client.last_err != FATAL_ERROR) {
  67749. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  67750. }
  67751. /* Check the server returned an error indicating the msg buffer
  67752. * was full */
  67753. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  67754. if (EXPECT_FAIL())
  67755. break;
  67756. }
  67757. return EXPECT_RESULT();
  67758. }
  67759. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  67760. {
  67761. int fd, ret;
  67762. byte alert_msg[] = {
  67763. 0x15, /* alert type */
  67764. 0xfe, 0xfd, /* version */
  67765. 0x00, 0x00, /* epoch */
  67766. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  67767. 0x00, 0x02, /* length */
  67768. 0x02, /* level: fatal */
  67769. 0x46 /* protocol version */
  67770. };
  67771. fd = wolfSSL_get_fd(ssl);
  67772. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  67773. AssertIntGT(ret, 0);
  67774. }
  67775. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  67776. {
  67777. callback_functions client_cbs, server_cbs;
  67778. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  67779. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  67780. client_cbs.doUdp = server_cbs.doUdp = 1;
  67781. if (version12) {
  67782. #if !defined(WOLFSSL_NO_TLS12)
  67783. client_cbs.method = wolfDTLSv1_2_client_method;
  67784. server_cbs.method = wolfDTLSv1_2_server_method;
  67785. #else
  67786. return TEST_SKIPPED;
  67787. #endif
  67788. }
  67789. else
  67790. {
  67791. #ifdef WOLFSSL_DTLS13
  67792. client_cbs.method = wolfDTLSv1_3_client_method;
  67793. server_cbs.method = wolfDTLSv1_3_server_method;
  67794. #else
  67795. return TEST_SKIPPED;
  67796. #endif /* WOLFSSL_DTLS13 */
  67797. }
  67798. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  67799. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  67800. if (!client_cbs.return_code)
  67801. return TEST_FAIL;
  67802. if (!server_cbs.return_code)
  67803. return TEST_FAIL;
  67804. return TEST_SUCCESS;
  67805. }
  67806. static int test_wolfSSL_ignore_alert_before_cookie(void)
  67807. {
  67808. int ret;
  67809. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  67810. if (ret != 0)
  67811. return ret;
  67812. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  67813. if (ret != 0)
  67814. return ret;
  67815. return 0;
  67816. }
  67817. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  67818. {
  67819. int ret;
  67820. int fd;
  67821. byte bad_msg[] = {
  67822. 0x17, /* app data */
  67823. 0xaa, 0xfd, /* bad version */
  67824. 0x00, 0x01, /* epoch 1 */
  67825. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  67826. 0x00, 0x26, /* length: 38 bytes */
  67827. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  67828. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  67829. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  67830. 0x03, 0x18, 0x72
  67831. };
  67832. fd = wolfSSL_get_fd(ssl);
  67833. AssertIntGE(fd, 0);
  67834. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  67835. AssertIntEQ(ret, sizeof(bad_msg));
  67836. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  67837. AssertIntEQ(ret, sizeof("badrecordtest"));
  67838. }
  67839. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  67840. {
  67841. byte buf[100];
  67842. int ret;
  67843. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  67844. AssertIntGT(ret, 0);
  67845. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  67846. }
  67847. static int _test_wolfSSL_dtls_bad_record(
  67848. method_provider client_method, method_provider server_method)
  67849. {
  67850. callback_functions client_cbs, server_cbs;
  67851. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  67852. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  67853. client_cbs.doUdp = server_cbs.doUdp = 1;
  67854. client_cbs.method = client_method;
  67855. server_cbs.method = server_method;
  67856. client_cbs.on_result = test_wolfSSL_send_bad_record;
  67857. server_cbs.on_result = test_wolfSSL_read_string;
  67858. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  67859. if (!client_cbs.return_code)
  67860. return TEST_FAIL;
  67861. if (!server_cbs.return_code)
  67862. return TEST_FAIL;
  67863. return TEST_SUCCESS;
  67864. }
  67865. static int test_wolfSSL_dtls_bad_record(void)
  67866. {
  67867. int ret = TEST_SUCCESS;
  67868. #if !defined(WOLFSSL_NO_TLS12)
  67869. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  67870. wolfDTLSv1_2_server_method);
  67871. #endif
  67872. #ifdef WOLFSSL_DTLS13
  67873. if (ret == TEST_SUCCESS) {
  67874. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  67875. wolfDTLSv1_3_server_method);
  67876. }
  67877. #endif /* WOLFSSL_DTLS13 */
  67878. return ret;
  67879. }
  67880. #else
  67881. static int test_wolfSSL_dtls_fragments(void) {
  67882. return TEST_SKIPPED;
  67883. }
  67884. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  67885. return TEST_SKIPPED;
  67886. }
  67887. static int test_wolfSSL_dtls_bad_record(void) {
  67888. return TEST_SKIPPED;
  67889. }
  67890. #endif
  67891. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  67892. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  67893. defined(HAVE_IO_TESTS_DEPENDENCIES)
  67894. static byte test_AEAD_fail_decryption = 0;
  67895. static byte test_AEAD_seq_num = 0;
  67896. static byte test_AEAD_done = 0;
  67897. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  67898. {
  67899. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  67900. if (ret > 0) {
  67901. if (test_AEAD_fail_decryption) {
  67902. /* Modify the packet to trigger a decryption failure */
  67903. buf[ret/2] ^= 0xFF;
  67904. if (test_AEAD_fail_decryption == 1)
  67905. test_AEAD_fail_decryption = 0;
  67906. }
  67907. }
  67908. (void)ctx;
  67909. return ret;
  67910. }
  67911. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  67912. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  67913. {
  67914. if (sendLimit)
  67915. w64Zero(sendLimit);
  67916. switch (ssl->specs.bulk_cipher_algorithm) {
  67917. case wolfssl_aes_gcm:
  67918. if (sendLimit)
  67919. *sendLimit = AEAD_AES_LIMIT;
  67920. FALL_THROUGH;
  67921. case wolfssl_chacha:
  67922. if (hardLimit)
  67923. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  67924. if (keyUpdateLimit)
  67925. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  67926. break;
  67927. case wolfssl_aes_ccm:
  67928. if (sendLimit)
  67929. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  67930. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  67931. if (hardLimit)
  67932. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  67933. if (keyUpdateLimit)
  67934. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  67935. }
  67936. else {
  67937. if (hardLimit)
  67938. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  67939. if (keyUpdateLimit)
  67940. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  67941. }
  67942. break;
  67943. default:
  67944. fprintf(stderr, "Unrecognized bulk cipher");
  67945. AssertFalse(1);
  67946. break;
  67947. }
  67948. }
  67949. static void test_AEAD_limit_client(WOLFSSL* ssl)
  67950. {
  67951. int ret;
  67952. int i;
  67953. int didReKey = 0;
  67954. char msgBuf[20];
  67955. w64wrapper hardLimit;
  67956. w64wrapper keyUpdateLimit;
  67957. w64wrapper counter;
  67958. w64wrapper sendLimit;
  67959. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  67960. w64Zero(&counter);
  67961. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  67962. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  67963. for (i = 0; i < 10; i++) {
  67964. /* Test some failed decryptions */
  67965. test_AEAD_fail_decryption = 1;
  67966. w64Increment(&counter);
  67967. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  67968. /* Should succeed since decryption failures are dropped */
  67969. AssertIntGT(ret, 0);
  67970. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  67971. }
  67972. test_AEAD_fail_decryption = 1;
  67973. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  67974. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  67975. /* 100 read calls should be enough to complete the key update */
  67976. w64Zero(&counter);
  67977. for (i = 0; i < 100; i++) {
  67978. /* Key update should be sent and negotiated */
  67979. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  67980. AssertIntGT(ret, 0);
  67981. /* Epoch after one key update is 4 */
  67982. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  67983. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  67984. didReKey = 1;
  67985. break;
  67986. }
  67987. }
  67988. AssertTrue(didReKey);
  67989. if (!w64IsZero(sendLimit)) {
  67990. /* Test the sending limit for AEAD ciphers */
  67991. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  67992. test_AEAD_seq_num = 1;
  67993. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  67994. AssertIntGT(ret, 0);
  67995. didReKey = 0;
  67996. w64Zero(&counter);
  67997. /* 100 read calls should be enough to complete the key update */
  67998. for (i = 0; i < 100; i++) {
  67999. /* Key update should be sent and negotiated */
  68000. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  68001. AssertIntGT(ret, 0);
  68002. /* Epoch after another key update is 5 */
  68003. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  68004. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  68005. didReKey = 1;
  68006. break;
  68007. }
  68008. }
  68009. AssertTrue(didReKey);
  68010. }
  68011. test_AEAD_fail_decryption = 2;
  68012. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  68013. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  68014. /* Connection should fail with a DECRYPT_ERROR */
  68015. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  68016. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  68017. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  68018. test_AEAD_done = 1;
  68019. }
  68020. int counter = 0;
  68021. static void test_AEAD_limit_server(WOLFSSL* ssl)
  68022. {
  68023. char msgBuf[] = "Sending data";
  68024. int ret = WOLFSSL_SUCCESS;
  68025. w64wrapper sendLimit;
  68026. SOCKET_T fd = wolfSSL_get_fd(ssl);
  68027. struct timespec delay;
  68028. XMEMSET(&delay, 0, sizeof(delay));
  68029. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  68030. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  68031. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  68032. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  68033. while (!test_AEAD_done && ret > 0) {
  68034. counter++;
  68035. if (test_AEAD_seq_num) {
  68036. /* We need to update the seq number so that we can understand the
  68037. * peer. Otherwise we will incorrectly interpret the seq number. */
  68038. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  68039. AssertNotNull(e);
  68040. e->nextPeerSeqNumber = sendLimit;
  68041. test_AEAD_seq_num = 0;
  68042. }
  68043. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  68044. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  68045. nanosleep(&delay, NULL);
  68046. }
  68047. }
  68048. static int test_wolfSSL_dtls_AEAD_limit(void)
  68049. {
  68050. callback_functions func_cb_client;
  68051. callback_functions func_cb_server;
  68052. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  68053. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  68054. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  68055. func_cb_server.method = wolfDTLSv1_3_server_method;
  68056. func_cb_client.method = wolfDTLSv1_3_client_method;
  68057. func_cb_server.on_result = test_AEAD_limit_server;
  68058. func_cb_client.on_result = test_AEAD_limit_client;
  68059. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  68060. if (!func_cb_client.return_code)
  68061. return TEST_FAIL;
  68062. if (!func_cb_server.return_code)
  68063. return TEST_FAIL;
  68064. return TEST_SUCCESS;
  68065. }
  68066. #else
  68067. static int test_wolfSSL_dtls_AEAD_limit(void)
  68068. {
  68069. return TEST_SKIPPED;
  68070. }
  68071. #endif
  68072. #if defined(WOLFSSL_DTLS) && \
  68073. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  68074. !defined(DEBUG_VECTOR_REGISTER_ACCESS_FUZZING)
  68075. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  68076. {
  68077. int fd, ret;
  68078. byte ch_msg[] = {
  68079. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  68080. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  68081. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  68082. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  68083. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  68084. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  68085. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  68086. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  68087. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  68088. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  68089. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  68090. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  68091. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  68092. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  68093. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  68094. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  68095. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  68096. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  68097. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  68098. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  68099. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  68100. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  68101. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  68102. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  68103. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  68104. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  68105. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  68106. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  68107. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  68108. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  68109. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  68110. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  68111. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  68112. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  68113. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  68114. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  68115. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  68116. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  68117. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  68118. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  68119. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  68120. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  68121. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  68122. 0x31, 0x68, 0x4c
  68123. };
  68124. fd = wolfSSL_get_fd(ssl);
  68125. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  68126. AssertIntGT(ret, 0);
  68127. /* consume the HRR otherwise handshake will fail */
  68128. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  68129. AssertIntGT(ret, 0);
  68130. }
  68131. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  68132. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  68133. {
  68134. int fd, ret;
  68135. byte ch_msh_invalid_cookie[] = {
  68136. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  68137. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  68138. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  68139. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  68140. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  68141. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  68142. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  68143. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  68144. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  68145. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  68146. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  68147. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  68148. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  68149. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  68150. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  68151. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  68152. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  68153. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  68154. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  68155. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  68156. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  68157. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  68158. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  68159. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  68160. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  68161. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  68162. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  68163. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  68164. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  68165. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  68166. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  68167. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  68168. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  68169. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  68170. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  68171. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  68172. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  68173. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  68174. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  68175. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  68176. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  68177. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  68178. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  68179. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  68180. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  68181. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  68182. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  68183. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  68184. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  68185. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  68186. 0x23, 0x00, 0x00
  68187. };
  68188. byte alert_reply[50];
  68189. byte expected_alert_reply[] = {
  68190. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  68191. 0x02, 0x02, 0x2f
  68192. };
  68193. fd = wolfSSL_get_fd(ssl);
  68194. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  68195. AssertIntGT(ret, 0);
  68196. /* should reply with an illegal_parameter reply */
  68197. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  68198. AssertIntEQ(ret, sizeof(expected_alert_reply));
  68199. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  68200. }
  68201. #endif
  68202. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  68203. {
  68204. #ifndef NO_MD5
  68205. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  68206. #elif !defined(NO_SHA)
  68207. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  68208. #elif !defined(NO_SHA256)
  68209. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  68210. #else
  68211. #error "We need a digest to hash the WOLFSSL object"
  68212. #endif
  68213. byte hashBuf[WC_MAX_DIGEST_SIZE];
  68214. wc_HashAlg hash;
  68215. const TLSX* exts = ssl->extensions;
  68216. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  68217. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  68218. * InitHandshakeHashes */
  68219. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  68220. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  68221. /* Following fields are not important to compare */
  68222. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  68223. sslCopy.buffers.inputBuffer.buffer = NULL;
  68224. sslCopy.buffers.inputBuffer.bufferSize = 0;
  68225. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  68226. sslCopy.buffers.inputBuffer.offset = 0;
  68227. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  68228. sslCopy.buffers.outputBuffer.buffer = NULL;
  68229. sslCopy.buffers.outputBuffer.bufferSize = 0;
  68230. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  68231. sslCopy.buffers.outputBuffer.offset = 0;
  68232. sslCopy.error = 0;
  68233. sslCopy.curSize = 0;
  68234. sslCopy.curStartIdx = 0;
  68235. sslCopy.keys.curSeq_lo = 0;
  68236. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  68237. #ifdef WOLFSSL_DTLS13
  68238. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  68239. sslCopy.dtls13FastTimeout = 0;
  68240. #endif
  68241. sslCopy.keys.dtls_peer_handshake_number = 0;
  68242. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  68243. sslCopy.hsHashes = NULL;
  68244. #ifdef WOLFSSL_ASYNC_IO
  68245. #ifdef WOLFSSL_ASYNC_CRYPT
  68246. sslCopy.asyncDev = NULL;
  68247. #endif
  68248. sslCopy.async = NULL;
  68249. #endif
  68250. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  68251. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  68252. /* hash extension list */
  68253. while (exts != NULL) {
  68254. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  68255. exts = exts->next;
  68256. }
  68257. /* Hash suites */
  68258. if (sslCopy.suites != NULL) {
  68259. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  68260. sizeof(struct Suites)), 0);
  68261. }
  68262. /* Hash hsHashes */
  68263. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  68264. sizeof(*hsHashes)), 0);
  68265. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  68266. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  68267. return MakeWordFromHash(hashBuf);
  68268. }
  68269. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  68270. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  68271. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  68272. char *buf, int sz, void *ctx)
  68273. {
  68274. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  68275. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  68276. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  68277. }
  68278. else {
  68279. return WOLFSSL_CBIO_ERR_WANT_READ;
  68280. }
  68281. }
  68282. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  68283. {
  68284. /* Compare the ssl object before and after one ClientHello msg */
  68285. SOCKET_T fd = wolfSSL_get_fd(ssl);
  68286. int res;
  68287. int err;
  68288. word32 initHash;
  68289. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  68290. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  68291. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  68292. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  68293. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  68294. (void)initHash;
  68295. res = tcp_select(fd, 5);
  68296. /* We are expecting a msg. A timeout indicates failure. */
  68297. AssertIntEQ(res, TEST_RECV_READY);
  68298. res = wolfSSL_accept(ssl);
  68299. err = wolfSSL_get_error(ssl, res);
  68300. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  68301. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  68302. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  68303. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  68304. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  68305. }
  68306. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  68307. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  68308. {
  68309. int ret;
  68310. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  68311. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  68312. test_wolfSSL_dtls_compare_stateless(ssl);
  68313. }
  68314. #endif
  68315. static int test_wolfSSL_dtls_stateless(void)
  68316. {
  68317. callback_functions client_cbs, server_cbs;
  68318. size_t i;
  68319. struct {
  68320. method_provider client_meth;
  68321. method_provider server_meth;
  68322. ssl_callback client_ssl_ready;
  68323. ssl_callback server_ssl_ready;
  68324. } test_params[] = {
  68325. #if !defined(WOLFSSL_NO_TLS12)
  68326. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  68327. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  68328. #endif
  68329. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  68330. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  68331. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  68332. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  68333. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  68334. #endif
  68335. };
  68336. if (0 == sizeof(test_params)){
  68337. return TEST_SKIPPED;
  68338. }
  68339. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  68340. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  68341. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  68342. client_cbs.doUdp = server_cbs.doUdp = 1;
  68343. client_cbs.method = test_params[i].client_meth;
  68344. server_cbs.method = test_params[i].server_meth;
  68345. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  68346. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  68347. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  68348. if (!client_cbs.return_code)
  68349. return TEST_FAIL;
  68350. if (!server_cbs.return_code)
  68351. return TEST_FAIL;
  68352. }
  68353. return TEST_SUCCESS;
  68354. }
  68355. #else
  68356. static int test_wolfSSL_dtls_stateless(void)
  68357. {
  68358. return TEST_SKIPPED;
  68359. }
  68360. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  68361. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  68362. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  68363. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  68364. !defined(WOLFSSL_NO_CLIENT_AUTH))
  68365. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  68366. {
  68367. int ret;
  68368. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  68369. fprintf(stderr, "loading cert %s failed\n", certA);
  68370. fprintf(stderr, "Error: (%d): %s\n", ret,
  68371. wolfSSL_ERR_reason_error_string(ret));
  68372. return -1;
  68373. }
  68374. return 0;
  68375. }
  68376. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  68377. {
  68378. int ret;
  68379. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  68380. != WOLFSSL_SUCCESS) {
  68381. fprintf(stderr, "could not verify the cert: %s\n", certA);
  68382. fprintf(stderr, "Error: (%d): %s\n", ret,
  68383. wolfSSL_ERR_reason_error_string(ret));
  68384. return -1;
  68385. }
  68386. else {
  68387. fprintf(stderr, "successfully verified: %s\n", certA);
  68388. }
  68389. return 0;
  68390. }
  68391. #define LOAD_ONE_CA(a, b, c, d) \
  68392. do { \
  68393. (a) = load_ca_into_cm(c, d); \
  68394. if ((a) != 0) \
  68395. return (b); \
  68396. else \
  68397. (b)--; \
  68398. } while(0)
  68399. #define VERIFY_ONE_CERT(a, b, c, d) \
  68400. do { \
  68401. (a) = verify_cert_with_cm(c, d);\
  68402. if ((a) != 0) \
  68403. return (b); \
  68404. else \
  68405. (b)--; \
  68406. } while(0)
  68407. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  68408. {
  68409. int ret;
  68410. int i = -1;
  68411. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  68412. char chainGArr[9][50] = {"certs/ca-cert.pem",
  68413. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  68414. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  68415. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  68416. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  68417. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  68418. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  68419. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  68420. "certs/test-pathlen/chainG-entity.pem"};
  68421. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  68422. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  68423. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  68424. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  68425. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  68426. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  68427. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  68428. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  68429. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  68430. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  68431. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  68432. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  68433. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  68434. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  68435. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  68436. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  68437. /* test validating the entity twice, should have no effect on pathLen since
  68438. * entity/leaf cert */
  68439. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  68440. return ret;
  68441. }
  68442. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  68443. {
  68444. int ret;
  68445. int i = -1;
  68446. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  68447. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  68448. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  68449. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  68450. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  68451. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  68452. */
  68453. char chainHArr[6][50] = {"certs/ca-cert.pem",
  68454. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  68455. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  68456. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  68457. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  68458. "certs/test-pathlen/chainH-entity.pem"};
  68459. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  68460. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  68461. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  68462. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  68463. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  68464. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  68465. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  68466. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  68467. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  68468. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  68469. return ret;
  68470. }
  68471. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  68472. {
  68473. int ret;
  68474. int i = -1;
  68475. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  68476. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  68477. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  68478. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  68479. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  68480. */
  68481. char chainIArr[5][50] = {"certs/ca-cert.pem",
  68482. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  68483. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  68484. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  68485. "certs/test-pathlen/chainI-entity.pem"};
  68486. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  68487. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  68488. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  68489. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  68490. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  68491. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  68492. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  68493. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  68494. return ret;
  68495. }
  68496. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  68497. {
  68498. int ret;
  68499. int i = -1;
  68500. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  68501. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  68502. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  68503. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  68504. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  68505. */
  68506. char chainJArr[6][50] = {"certs/ca-cert.pem",
  68507. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  68508. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  68509. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  68510. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  68511. "certs/test-pathlen/chainJ-entity.pem"};
  68512. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  68513. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  68514. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  68515. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  68516. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  68517. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  68518. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  68519. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  68520. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  68521. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  68522. return ret;
  68523. }
  68524. static int test_various_pathlen_chains(void)
  68525. {
  68526. EXPECT_DECLS;
  68527. WOLFSSL_CERT_MANAGER* cm = NULL;
  68528. /* Test chain G (large chain with varying pathLens) */
  68529. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  68530. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  68531. ExpectIntEQ(test_chainG(cm), -1);
  68532. #else
  68533. ExpectIntEQ(test_chainG(cm), 0);
  68534. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  68535. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  68536. wolfSSL_CertManagerFree(cm);
  68537. /* end test chain G */
  68538. /* Test chain H (5 chain with same pathLens) */
  68539. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  68540. ExpectIntLT(test_chainH(cm), 0);
  68541. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  68542. wolfSSL_CertManagerFree(cm);
  68543. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  68544. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  68545. wolfSSL_CertManagerFree(cm);
  68546. /* end test chain H */
  68547. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  68548. * followed by 2 ICA's, should pass) */
  68549. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  68550. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  68551. ExpectIntEQ(test_chainI(cm), -1);
  68552. #else
  68553. ExpectIntEQ(test_chainI(cm), 0);
  68554. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  68555. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  68556. wolfSSL_CertManagerFree(cm);
  68557. cm = NULL;
  68558. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  68559. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  68560. wolfSSL_CertManagerFree(cm);
  68561. cm = NULL;
  68562. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  68563. * this time followed by 3 ICA's, should fail */
  68564. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  68565. ExpectIntLT(test_chainJ(cm), 0);
  68566. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  68567. wolfSSL_CertManagerFree(cm);
  68568. cm = NULL;
  68569. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  68570. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  68571. wolfSSL_CertManagerFree(cm);
  68572. return EXPECT_RESULT();
  68573. }
  68574. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  68575. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  68576. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  68577. {
  68578. EXPECT_DECLS;
  68579. byte ekm[100] = {0};
  68580. (void)ctx;
  68581. /* Success Cases */
  68582. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  68583. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  68584. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  68585. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  68586. /* Use some random context */
  68587. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  68588. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  68589. /* Failure cases */
  68590. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  68591. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  68592. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  68593. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  68594. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  68595. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  68596. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  68597. "extended master secret", XSTR_SIZEOF("extended master secret"),
  68598. NULL, 0, 0), 0);
  68599. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  68600. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  68601. return EXPECT_RESULT();
  68602. }
  68603. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  68604. {
  68605. wolfSSL_KeepArrays(ssl);
  68606. return TEST_SUCCESS;
  68607. }
  68608. static int test_export_keying_material(void)
  68609. {
  68610. EXPECT_DECLS;
  68611. test_ssl_cbf serverCb;
  68612. test_ssl_cbf clientCb;
  68613. XMEMSET(&serverCb, 0, sizeof(serverCb));
  68614. XMEMSET(&clientCb, 0, sizeof(clientCb));
  68615. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  68616. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  68617. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  68618. return EXPECT_RESULT();
  68619. }
  68620. #endif /* HAVE_KEYING_MATERIAL */
  68621. static int test_wolfSSL_THREADID_hash(void)
  68622. {
  68623. EXPECT_DECLS;
  68624. #if defined(OPENSSL_EXTRA)
  68625. CRYPTO_THREADID id;
  68626. CRYPTO_THREADID_current(NULL);
  68627. /* Hash result is unsigned long. */
  68628. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  68629. XMEMSET(&id, 0, sizeof(id));
  68630. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  68631. #endif /* OPENSSL_EXTRA */
  68632. return EXPECT_RESULT();
  68633. }
  68634. static int test_wolfSSL_set_ecdh_auto(void)
  68635. {
  68636. EXPECT_DECLS;
  68637. #if defined(OPENSSL_EXTRA)
  68638. WOLFSSL* ssl = NULL;
  68639. ExpectIntEQ(SSL_set_ecdh_auto(NULL,0), 1);
  68640. ExpectIntEQ(SSL_set_ecdh_auto(NULL,1), 1);
  68641. ExpectIntEQ(SSL_set_ecdh_auto(ssl,0), 1);
  68642. ExpectIntEQ(SSL_set_ecdh_auto(ssl,1), 1);
  68643. #endif /* OPENSSL_EXTRA */
  68644. return EXPECT_RESULT();
  68645. }
  68646. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  68647. {
  68648. EXPECT_DECLS;
  68649. #if defined(OPENSSL_EXTRA)
  68650. WOLFSSL_CTX* ctx = NULL;
  68651. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  68652. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  68653. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  68654. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  68655. #endif /* OPENSSL_EXTRA */
  68656. return EXPECT_RESULT();
  68657. }
  68658. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  68659. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  68660. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  68661. {
  68662. EXPECT_DECLS;
  68663. callback_functions* callbacks = NULL;
  68664. WOLFSSL_CTX* ctx = NULL;
  68665. WOLFSSL* ssl = NULL;
  68666. SOCKET_T sfd = 0;
  68667. SOCKET_T cfd = 0;
  68668. word16 port;
  68669. char msg[] = "I hear you fa shizzle!";
  68670. int len = (int) XSTRLEN(msg);
  68671. char input[1024];
  68672. int ret = 0;
  68673. int err = 0;
  68674. if (!args)
  68675. WOLFSSL_RETURN_FROM_THREAD(0);
  68676. ((func_args*)args)->return_code = TEST_FAIL;
  68677. callbacks = ((func_args*)args)->callbacks;
  68678. ctx = wolfSSL_CTX_new(callbacks->method());
  68679. #if defined(USE_WINDOWS_API)
  68680. port = ((func_args*)args)->signal->port;
  68681. #else
  68682. /* Let tcp_listen assign port */
  68683. port = 0;
  68684. #endif
  68685. #ifdef WOLFSSL_TIRTOS
  68686. fdOpenSession(Task_self());
  68687. #endif
  68688. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  68689. caCertFile, 0));
  68690. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  68691. svrCertFile, WOLFSSL_FILETYPE_PEM));
  68692. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  68693. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  68694. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  68695. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  68696. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  68697. #elif !defined(NO_DH)
  68698. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  68699. #endif
  68700. if (callbacks->ctx_ready)
  68701. callbacks->ctx_ready(ctx);
  68702. ssl = wolfSSL_new(ctx);
  68703. ExpectNotNull(ssl);
  68704. /* listen and accept */
  68705. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  68706. CloseSocket(sfd);
  68707. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  68708. if (callbacks->ssl_ready)
  68709. callbacks->ssl_ready(ssl);
  68710. if (EXPECT_SUCCESS()) {
  68711. do {
  68712. err = 0; /* Reset error */
  68713. ret = wolfSSL_accept(ssl);
  68714. if (ret != WOLFSSL_SUCCESS) {
  68715. err = wolfSSL_get_error(ssl, 0);
  68716. }
  68717. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  68718. }
  68719. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  68720. /* read and write data */
  68721. XMEMSET(input, 0, sizeof(input));
  68722. while (EXPECT_SUCCESS()) {
  68723. ret = wolfSSL_read(ssl, input, sizeof(input));
  68724. if (ret > 0) {
  68725. break;
  68726. }
  68727. else {
  68728. err = wolfSSL_get_error(ssl,ret);
  68729. if (err == WOLFSSL_ERROR_WANT_READ) {
  68730. continue;
  68731. }
  68732. break;
  68733. }
  68734. }
  68735. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  68736. do {
  68737. ret = wolfSSL_write(ssl, msg, len);
  68738. if (ret > 0) {
  68739. break;
  68740. }
  68741. } while (ret < 0);
  68742. }
  68743. /* bidirectional shutdown */
  68744. while (EXPECT_SUCCESS()) {
  68745. ret = wolfSSL_shutdown(ssl);
  68746. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  68747. if (ret == WOLFSSL_SUCCESS) {
  68748. break;
  68749. }
  68750. }
  68751. if (EXPECT_SUCCESS()) {
  68752. /* wait for the peer to disconnect the tcp connection */
  68753. do {
  68754. ret = wolfSSL_read(ssl, input, sizeof(input));
  68755. err = wolfSSL_get_error(ssl, ret);
  68756. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  68757. }
  68758. /* detect TCP disconnect */
  68759. ExpectIntLE(ret,WOLFSSL_FAILURE);
  68760. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  68761. ((func_args*)args)->return_code = EXPECT_RESULT();
  68762. wolfSSL_free(ssl);
  68763. wolfSSL_CTX_free(ctx);
  68764. CloseSocket(cfd);
  68765. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  68766. wc_ecc_fp_free(); /* free per thread cache */
  68767. #endif
  68768. WOLFSSL_RETURN_FROM_THREAD(0);
  68769. }
  68770. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  68771. {
  68772. EXPECT_DECLS;
  68773. callback_functions* callbacks = NULL;
  68774. WOLFSSL_CTX* ctx = NULL;
  68775. WOLFSSL* ssl = NULL;
  68776. SOCKET_T sfd = 0;
  68777. char msg[] = "hello wolfssl server!";
  68778. int len = (int) XSTRLEN(msg);
  68779. char input[1024];
  68780. int idx;
  68781. int ret, err;
  68782. if (!args)
  68783. WOLFSSL_RETURN_FROM_THREAD(0);
  68784. ((func_args*)args)->return_code = TEST_FAIL;
  68785. callbacks = ((func_args*)args)->callbacks;
  68786. ctx = wolfSSL_CTX_new(callbacks->method());
  68787. #ifdef WOLFSSL_TIRTOS
  68788. fdOpenSession(Task_self());
  68789. #endif
  68790. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  68791. caCertFile, 0));
  68792. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  68793. cliCertFile, WOLFSSL_FILETYPE_PEM));
  68794. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  68795. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  68796. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  68797. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  68798. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  68799. if (EXPECT_SUCCESS()) {
  68800. do {
  68801. err = 0; /* Reset error */
  68802. ret = wolfSSL_connect(ssl);
  68803. if (ret != WOLFSSL_SUCCESS) {
  68804. err = wolfSSL_get_error(ssl, 0);
  68805. }
  68806. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  68807. }
  68808. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  68809. if (EXPECT_SUCCESS()) {
  68810. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  68811. input[idx] = 0;
  68812. }
  68813. }
  68814. if (EXPECT_SUCCESS()) {
  68815. ret = wolfSSL_shutdown(ssl);
  68816. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  68817. ret = wolfSSL_shutdown(ssl);
  68818. }
  68819. }
  68820. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  68821. ((func_args*)args)->return_code = EXPECT_RESULT();
  68822. wolfSSL_free(ssl);
  68823. wolfSSL_CTX_free(ctx);
  68824. CloseSocket(sfd);
  68825. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  68826. wc_ecc_fp_free(); /* free per thread cache */
  68827. #endif
  68828. WOLFSSL_RETURN_FROM_THREAD(0);
  68829. }
  68830. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  68831. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  68832. /* This test is to check wolfSSL_read behaves as same as
  68833. * openSSL when it is called after SSL_shutdown completes.
  68834. */
  68835. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  68836. {
  68837. EXPECT_DECLS;
  68838. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  68839. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  68840. tcp_ready ready;
  68841. func_args client_args;
  68842. func_args server_args;
  68843. THREAD_TYPE serverThread;
  68844. THREAD_TYPE clientThread;
  68845. callback_functions server_cbf;
  68846. callback_functions client_cbf;
  68847. #ifdef WOLFSSL_TIRTOS
  68848. fdOpenSession(Task_self());
  68849. #endif
  68850. StartTCP();
  68851. InitTcpReady(&ready);
  68852. #if defined(USE_WINDOWS_API)
  68853. /* use RNG to get random port if using windows */
  68854. ready.port = GetRandomPort();
  68855. #endif
  68856. XMEMSET(&client_args, 0, sizeof(func_args));
  68857. XMEMSET(&server_args, 0, sizeof(func_args));
  68858. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  68859. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  68860. server_cbf.method = wolfTLSv1_2_server_method;
  68861. client_cbf.method = wolfTLSv1_2_client_method;
  68862. server_args.callbacks = &server_cbf;
  68863. client_args.callbacks = &client_cbf;
  68864. server_args.signal = &ready;
  68865. client_args.signal = &ready;
  68866. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  68867. wait_tcp_ready(&server_args);
  68868. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  68869. join_thread(clientThread);
  68870. join_thread(serverThread);
  68871. ExpectTrue(client_args.return_code);
  68872. ExpectTrue(server_args.return_code);
  68873. FreeTcpReady(&ready);
  68874. #endif
  68875. return EXPECT_RESULT();
  68876. }
  68877. static int test_wolfSSL_CTX_get_min_proto_version(void)
  68878. {
  68879. EXPECT_DECLS;
  68880. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  68881. WOLFSSL_CTX *ctx = NULL;
  68882. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  68883. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  68884. WOLFSSL_SUCCESS);
  68885. #ifdef WOLFSSL_ALLOW_SSLV3
  68886. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  68887. #else
  68888. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  68889. #endif
  68890. wolfSSL_CTX_free(ctx);
  68891. ctx = NULL;
  68892. #ifndef NO_OLD_TLS
  68893. #ifdef WOLFSSL_ALLOW_TLSV10
  68894. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  68895. #else
  68896. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  68897. #endif
  68898. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  68899. WOLFSSL_SUCCESS);
  68900. #ifdef WOLFSSL_ALLOW_TLSV10
  68901. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  68902. #else
  68903. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  68904. #endif
  68905. wolfSSL_CTX_free(ctx);
  68906. ctx = NULL;
  68907. #endif
  68908. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  68909. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  68910. WOLFSSL_SUCCESS);
  68911. #ifndef NO_OLD_TLS
  68912. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  68913. #else
  68914. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  68915. #endif
  68916. wolfSSL_CTX_free(ctx);
  68917. ctx = NULL;
  68918. #ifndef WOLFSSL_NO_TLS12
  68919. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  68920. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  68921. WOLFSSL_SUCCESS);
  68922. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  68923. wolfSSL_CTX_free(ctx);
  68924. ctx = NULL;
  68925. #endif
  68926. #ifdef WOLFSSL_TLS13
  68927. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  68928. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  68929. WOLFSSL_SUCCESS);
  68930. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  68931. wolfSSL_CTX_free(ctx);
  68932. ctx = NULL;
  68933. #endif
  68934. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  68935. return EXPECT_RESULT();
  68936. }
  68937. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  68938. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  68939. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  68940. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  68941. static int test_wolfSSL_set_SSL_CTX(void)
  68942. {
  68943. EXPECT_DECLS;
  68944. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  68945. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  68946. !defined(NO_RSA)
  68947. WOLFSSL_CTX *ctx1 = NULL;
  68948. WOLFSSL_CTX *ctx2 = NULL;
  68949. WOLFSSL *ssl = NULL;
  68950. const byte *session_id1 = (const byte *)"CTX1";
  68951. const byte *session_id2 = (const byte *)"CTX2";
  68952. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  68953. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  68954. WOLFSSL_FILETYPE_PEM));
  68955. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  68956. WOLFSSL_FILETYPE_PEM));
  68957. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  68958. WOLFSSL_SUCCESS);
  68959. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  68960. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  68961. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  68962. WOLFSSL_SUCCESS);
  68963. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  68964. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  68965. WOLFSSL_FILETYPE_PEM));
  68966. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  68967. WOLFSSL_FILETYPE_PEM));
  68968. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  68969. WOLFSSL_SUCCESS);
  68970. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  68971. WOLFSSL_SUCCESS);
  68972. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  68973. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  68974. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  68975. WOLFSSL_SUCCESS);
  68976. #ifdef HAVE_SESSION_TICKET
  68977. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  68978. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  68979. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  68980. #endif
  68981. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  68982. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  68983. #ifdef WOLFSSL_INT_H
  68984. #ifdef WOLFSSL_SESSION_ID_CTX
  68985. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  68986. #endif
  68987. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  68988. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  68989. #endif
  68990. #ifdef HAVE_SESSION_TICKET
  68991. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  68992. #endif
  68993. /* Set the ctx1 that has TLSv1.3 as max proto version */
  68994. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  68995. /* MUST not change proto versions of ssl */
  68996. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  68997. #ifdef HAVE_SESSION_TICKET
  68998. /* MUST not change */
  68999. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  69000. #endif
  69001. /* MUST change */
  69002. #ifdef WOLFSSL_INT_H
  69003. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  69004. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  69005. #ifdef WOLFSSL_SESSION_ID_CTX
  69006. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  69007. #endif
  69008. #endif
  69009. wolfSSL_free(ssl);
  69010. wolfSSL_CTX_free(ctx1);
  69011. wolfSSL_CTX_free(ctx2);
  69012. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  69013. return EXPECT_RESULT();
  69014. }
  69015. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  69016. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  69017. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  69018. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  69019. static int test_wolfSSL_security_level(void)
  69020. {
  69021. EXPECT_DECLS;
  69022. #if defined(OPENSSL_EXTRA)
  69023. SSL_CTX *ctx = NULL;
  69024. #ifdef WOLFSSL_TLS13
  69025. #ifdef NO_WOLFSSL_SERVER
  69026. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  69027. #else
  69028. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  69029. #endif
  69030. SSL_CTX_set_security_level(NULL, 1);
  69031. SSL_CTX_set_security_level(ctx, 1);
  69032. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  69033. /* Stub so nothing happens. */
  69034. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  69035. SSL_CTX_free(ctx);
  69036. #else
  69037. (void)ctx;
  69038. #endif
  69039. #endif
  69040. return EXPECT_RESULT();
  69041. }
  69042. static int test_wolfSSL_SSL_in_init(void)
  69043. {
  69044. EXPECT_DECLS;
  69045. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  69046. SSL_CTX* ctx = NULL;
  69047. SSL* ssl = NULL;
  69048. const char* testCertFile;
  69049. const char* testKeyFile;
  69050. #ifdef WOLFSSL_TLS13
  69051. #ifdef NO_WOLFSSL_SERVER
  69052. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  69053. #else
  69054. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  69055. #endif
  69056. #else
  69057. #ifdef NO_WOLFSSL_SERVER
  69058. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  69059. #else
  69060. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  69061. #endif
  69062. #endif
  69063. #ifndef NO_RSA
  69064. testCertFile = svrCertFile;
  69065. testKeyFile = svrKeyFile;
  69066. #elif defined(HAVE_ECC)
  69067. testCertFile = eccCertFile;
  69068. testKeyFile = eccKeyFile;
  69069. #else
  69070. testCertFile = NULL;
  69071. testKeyFile = NULL;
  69072. #endif
  69073. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  69074. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  69075. SSL_FILETYPE_PEM));
  69076. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  69077. SSL_FILETYPE_PEM));
  69078. }
  69079. ExpectNotNull(ssl = SSL_new(ctx));
  69080. ExpectIntEQ(SSL_in_init(ssl), 1);
  69081. SSL_CTX_free(ctx);
  69082. SSL_free(ssl);
  69083. #endif
  69084. return EXPECT_RESULT();
  69085. }
  69086. static int test_wolfSSL_CTX_set_timeout(void)
  69087. {
  69088. EXPECT_DECLS;
  69089. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  69090. int timeout;
  69091. WOLFSSL_CTX* ctx = NULL;
  69092. (void)timeout;
  69093. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  69094. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  69095. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  69096. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  69097. */
  69098. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  69099. /* giving 0 as timeout value sets default timeout */
  69100. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  69101. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  69102. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  69103. #else
  69104. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  69105. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  69106. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  69107. #endif
  69108. wolfSSL_CTX_free(ctx);
  69109. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  69110. return EXPECT_RESULT();
  69111. }
  69112. static int test_wolfSSL_OpenSSL_version(void)
  69113. {
  69114. EXPECT_DECLS;
  69115. #if defined(OPENSSL_EXTRA)
  69116. const char* ver;
  69117. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  69118. ExpectNotNull(ver = OpenSSL_version(0));
  69119. #else
  69120. ExpectNotNull(ver = OpenSSL_version());
  69121. #endif
  69122. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  69123. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  69124. #endif
  69125. return EXPECT_RESULT();
  69126. }
  69127. static int test_CONF_CTX_CMDLINE(void)
  69128. {
  69129. EXPECT_DECLS;
  69130. #if defined(OPENSSL_ALL)
  69131. SSL_CTX* ctx = NULL;
  69132. SSL_CONF_CTX* cctx = NULL;
  69133. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  69134. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  69135. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  69136. /* set flags */
  69137. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  69138. WOLFSSL_CONF_FLAG_CMDLINE);
  69139. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  69140. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  69141. /* cmd invalid command */
  69142. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  69143. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  69144. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  69145. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  69146. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  69147. /* cmd Certificate and Private Key*/
  69148. {
  69149. #if !defined(NO_CERTS) && !defined(NO_RSA)
  69150. const char* ourCert = svrCertFile;
  69151. const char* ourKey = svrKeyFile;
  69152. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  69153. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  69154. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  69155. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  69156. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  69157. #endif
  69158. }
  69159. /* cmd curves */
  69160. {
  69161. #if defined(HAVE_ECC)
  69162. const char* curve = "secp256r1";
  69163. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  69164. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  69165. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  69166. #endif
  69167. }
  69168. /* cmd CipherString */
  69169. {
  69170. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  69171. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  69172. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  69173. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  69174. }
  69175. /* cmd DH parameter */
  69176. {
  69177. #if !defined(NO_DH) && !defined(NO_BIO)
  69178. const char* ourdhcert = "./certs/dh2048.pem";
  69179. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  69180. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  69181. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  69182. #endif
  69183. }
  69184. SSL_CTX_free(ctx);
  69185. SSL_CONF_CTX_free(cctx);
  69186. #endif /* OPENSSL_EXTRA */
  69187. return EXPECT_RESULT();
  69188. }
  69189. static int test_CONF_CTX_FILE(void)
  69190. {
  69191. EXPECT_DECLS;
  69192. #if defined(OPENSSL_ALL)
  69193. SSL_CTX* ctx = NULL;
  69194. SSL_CONF_CTX* cctx = NULL;
  69195. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  69196. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  69197. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  69198. /* set flags */
  69199. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  69200. WOLFSSL_CONF_FLAG_FILE);
  69201. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  69202. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  69203. /* sanity check */
  69204. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  69205. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  69206. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  69207. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  69208. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  69209. /* cmd Certificate and Private Key*/
  69210. {
  69211. #if !defined(NO_CERTS) && !defined(NO_RSA)
  69212. const char* ourCert = svrCertFile;
  69213. const char* ourKey = svrKeyFile;
  69214. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  69215. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  69216. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  69217. WOLFSSL_SUCCESS);
  69218. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  69219. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  69220. #endif
  69221. }
  69222. /* cmd curves */
  69223. {
  69224. #if defined(HAVE_ECC)
  69225. const char* curve = "secp256r1";
  69226. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  69227. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  69228. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  69229. #endif
  69230. }
  69231. /* cmd CipherString */
  69232. {
  69233. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  69234. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  69235. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  69236. WOLFSSL_SUCCESS);
  69237. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  69238. }
  69239. /* cmd DH parameter */
  69240. {
  69241. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  69242. const char* ourdhcert = "./certs/dh3072.pem";
  69243. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  69244. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  69245. WOLFSSL_SUCCESS);
  69246. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  69247. #endif
  69248. }
  69249. SSL_CTX_free(ctx);
  69250. SSL_CONF_CTX_free(cctx);
  69251. #endif /* OPENSSL_EXTRA */
  69252. return EXPECT_RESULT();
  69253. }
  69254. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  69255. {
  69256. EXPECT_DECLS;
  69257. #ifdef HAVE_EX_DATA
  69258. int idx1, idx2;
  69259. /* test for unsupported class index */
  69260. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  69261. 0,NULL, NULL, NULL, NULL ), -1);
  69262. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  69263. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  69264. 0,NULL, NULL, NULL, NULL ), -1);
  69265. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  69266. 0,NULL, NULL, NULL, NULL ), -1);
  69267. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  69268. 0,NULL, NULL, NULL, NULL ), -1);
  69269. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  69270. 0,NULL, NULL, NULL, NULL ), -1);
  69271. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  69272. 0,NULL, NULL, NULL, NULL ), -1);
  69273. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  69274. 0,NULL, NULL, NULL, NULL ), -1);
  69275. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  69276. 0,NULL, NULL, NULL, NULL ), -1);
  69277. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  69278. 0,NULL, NULL, NULL, NULL ), -1);
  69279. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  69280. 0,NULL, NULL, NULL, NULL ), -1);
  69281. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  69282. 0,NULL, NULL, NULL, NULL ), -1);
  69283. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  69284. 0,NULL, NULL, NULL, NULL ), -1);
  69285. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  69286. 0,NULL, NULL, NULL, NULL ), -1);
  69287. /* test for supported class index */
  69288. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  69289. 0,NULL, NULL, NULL, NULL );
  69290. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  69291. 0,NULL, NULL, NULL, NULL );
  69292. ExpectIntNE(idx1, -1);
  69293. ExpectIntNE(idx2, -1);
  69294. ExpectIntNE(idx1, idx2);
  69295. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  69296. 0,NULL, NULL, NULL, NULL );
  69297. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  69298. 0,NULL, NULL, NULL, NULL );
  69299. ExpectIntNE(idx1, -1);
  69300. ExpectIntNE(idx2, -1);
  69301. ExpectIntNE(idx1, idx2);
  69302. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  69303. 0,NULL, NULL, NULL, NULL );
  69304. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  69305. 0,NULL, NULL, NULL, NULL );
  69306. ExpectIntNE(idx1, -1);
  69307. ExpectIntNE(idx2, -1);
  69308. ExpectIntNE(idx1, idx2);
  69309. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  69310. 0,NULL, NULL, NULL, NULL );
  69311. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  69312. 0,NULL, NULL, NULL, NULL );
  69313. ExpectIntNE(idx1, -1);
  69314. ExpectIntNE(idx2, -1);
  69315. ExpectIntNE(idx1, idx2);
  69316. #endif /* HAVE_EX_DATA */
  69317. return EXPECT_RESULT();
  69318. }
  69319. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  69320. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  69321. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  69322. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  69323. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  69324. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  69325. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  69326. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  69327. #define SESSION_NEW_IDX_PTR "Testing"
  69328. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  69329. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  69330. {
  69331. AssertNotNull(p);
  69332. AssertNull(ptr);
  69333. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  69334. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  69335. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  69336. }
  69337. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  69338. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  69339. void* arg)
  69340. {
  69341. EXPECT_DECLS;
  69342. ExpectNotNull(out);
  69343. ExpectNotNull(in);
  69344. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  69345. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  69346. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  69347. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  69348. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  69349. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  69350. if (EXPECT_SUCCESS()) {
  69351. return SSL_SUCCESS;
  69352. }
  69353. else {
  69354. return SSL_FAILURE;
  69355. }
  69356. }
  69357. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  69358. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  69359. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  69360. {
  69361. EXPECT_DECLS;
  69362. ExpectNotNull(p);
  69363. ExpectNull(ptr);
  69364. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  69365. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  69366. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  69367. if (EXPECT_SUCCESS()) {
  69368. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  69369. }
  69370. }
  69371. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  69372. {
  69373. EXPECT_DECLS;
  69374. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  69375. (void*)SESSION_NEW_IDX_PTR,
  69376. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  69377. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  69378. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  69379. SSL_SESSION* s = SSL_SESSION_new();
  69380. SSL_SESSION* d = NULL;
  69381. ExpectNotNull(s);
  69382. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  69383. ExpectNotNull(d = SSL_SESSION_dup(s));
  69384. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  69385. SSL_SESSION_free(s);
  69386. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  69387. SSL_SESSION_free(d);
  69388. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  69389. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  69390. crypto_ex_cb_ctx_session = NULL;
  69391. return EXPECT_RESULT();
  69392. }
  69393. #else
  69394. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  69395. {
  69396. return TEST_SKIPPED;
  69397. }
  69398. #endif
  69399. static int test_wolfSSL_set_psk_use_session_callback(void)
  69400. {
  69401. EXPECT_DECLS;
  69402. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  69403. SSL_CTX* ctx = NULL;
  69404. SSL* ssl = NULL;
  69405. const char* testCertFile;
  69406. const char* testKeyFile;
  69407. #ifdef WOLFSSL_TLS13
  69408. #ifdef NO_WOLFSSL_SERVER
  69409. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  69410. #else
  69411. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  69412. #endif
  69413. #else
  69414. #ifdef NO_WOLFSSL_SERVER
  69415. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  69416. #else
  69417. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  69418. #endif
  69419. #endif
  69420. #ifndef NO_RSA
  69421. testCertFile = svrCertFile;
  69422. testKeyFile = svrKeyFile;
  69423. #elif defined(HAVE_ECC)
  69424. testCertFile = eccCertFile;
  69425. testKeyFile = eccKeyFile;
  69426. #else
  69427. testCertFile = NULL;
  69428. testKeyFile = NULL;
  69429. #endif
  69430. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  69431. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  69432. SSL_FILETYPE_PEM));
  69433. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  69434. SSL_FILETYPE_PEM));
  69435. }
  69436. ExpectNotNull(ssl = SSL_new(ctx));
  69437. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  69438. SSL_CTX_free(ctx);
  69439. SSL_free(ssl);
  69440. #endif
  69441. return EXPECT_RESULT();
  69442. }
  69443. static int test_wolfSSL_ERR_strings(void)
  69444. {
  69445. EXPECT_DECLS;
  69446. #if !defined(NO_ERROR_STRINGS)
  69447. const char* err1 = "unsupported cipher suite";
  69448. const char* err2 = "wolfSSL PEM routines";
  69449. const char* err = NULL;
  69450. (void)err;
  69451. (void)err1;
  69452. (void)err2;
  69453. #if defined(OPENSSL_EXTRA)
  69454. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  69455. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  69456. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  69457. ExpectIntEQ((*err == '\0'), 1);
  69458. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  69459. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  69460. #else
  69461. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  69462. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  69463. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  69464. ExpectIntEQ((*err == '\0'), 1);
  69465. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  69466. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  69467. ExpectIntEQ((*err == '\0'), 1);
  69468. #endif
  69469. #endif
  69470. return EXPECT_RESULT();
  69471. }
  69472. static int test_wolfSSL_EVP_shake128(void)
  69473. {
  69474. EXPECT_DECLS;
  69475. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  69476. defined(WOLFSSL_SHAKE128)
  69477. const EVP_MD* md = NULL;
  69478. ExpectNotNull(md = EVP_shake128());
  69479. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  69480. #endif
  69481. return EXPECT_RESULT();
  69482. }
  69483. static int test_wolfSSL_EVP_shake256(void)
  69484. {
  69485. EXPECT_DECLS;
  69486. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  69487. defined(WOLFSSL_SHAKE256)
  69488. const EVP_MD* md = NULL;
  69489. ExpectNotNull(md = EVP_shake256());
  69490. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  69491. #endif
  69492. return EXPECT_RESULT();
  69493. }
  69494. /*
  69495. * Testing EVP digest API with SM3
  69496. */
  69497. static int test_wolfSSL_EVP_sm3(void)
  69498. {
  69499. int res = TEST_SKIPPED;
  69500. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  69501. EXPECT_DECLS;
  69502. const EVP_MD* md = NULL;
  69503. EVP_MD_CTX* mdCtx = NULL;
  69504. byte data[WC_SM3_BLOCK_SIZE * 4];
  69505. byte hash[WC_SM3_DIGEST_SIZE];
  69506. byte calcHash[WC_SM3_DIGEST_SIZE];
  69507. byte expHash[WC_SM3_DIGEST_SIZE] = {
  69508. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  69509. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  69510. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  69511. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  69512. };
  69513. word32 chunk;
  69514. word32 i;
  69515. unsigned int sz;
  69516. int ret;
  69517. XMEMSET(data, 0, sizeof(data));
  69518. md = EVP_sm3();
  69519. ExpectTrue(md != NULL);
  69520. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  69521. mdCtx = EVP_MD_CTX_new();
  69522. ExpectTrue(mdCtx != NULL);
  69523. /* Invalid Parameters */
  69524. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  69525. /* Valid Parameters */
  69526. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  69527. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  69528. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  69529. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  69530. /* Valid Parameters */
  69531. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  69532. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  69533. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  69534. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  69535. WOLFSSL_SUCCESS);
  69536. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  69537. WOLFSSL_SUCCESS);
  69538. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  69539. WOLFSSL_SUCCESS);
  69540. /* Ensure too many bytes for lengths. */
  69541. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  69542. WOLFSSL_SUCCESS);
  69543. /* Invalid Parameters */
  69544. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  69545. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  69546. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  69547. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  69548. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  69549. /* Valid Parameters */
  69550. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  69551. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  69552. /* Chunk tests. */
  69553. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  69554. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  69555. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  69556. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  69557. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  69558. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  69559. WOLFSSL_SUCCESS);
  69560. }
  69561. if (i < (word32)sizeof(data)) {
  69562. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  69563. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  69564. }
  69565. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  69566. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  69567. }
  69568. /* Not testing when the low 32-bit length overflows. */
  69569. ret = EVP_MD_CTX_cleanup(mdCtx);
  69570. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  69571. wolfSSL_EVP_MD_CTX_free(mdCtx);
  69572. res = EXPECT_RESULT();
  69573. #endif
  69574. return res;
  69575. } /* END test_EVP_sm3 */
  69576. static int test_EVP_blake2(void)
  69577. {
  69578. EXPECT_DECLS;
  69579. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  69580. const EVP_MD* md = NULL;
  69581. (void)md;
  69582. #if defined(HAVE_BLAKE2)
  69583. ExpectNotNull(md = EVP_blake2b512());
  69584. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  69585. #endif
  69586. #if defined(HAVE_BLAKE2S)
  69587. ExpectNotNull(md = EVP_blake2s256());
  69588. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  69589. #endif
  69590. #endif
  69591. return EXPECT_RESULT();
  69592. }
  69593. #if defined(OPENSSL_EXTRA)
  69594. static void list_md_fn(const EVP_MD* m, const char* from,
  69595. const char* to, void* arg)
  69596. {
  69597. const char* mn;
  69598. BIO *bio;
  69599. (void) from;
  69600. (void) to;
  69601. (void) arg;
  69602. (void) mn;
  69603. (void) bio;
  69604. if (!m) {
  69605. /* alias */
  69606. AssertNull(m);
  69607. AssertNotNull(to);
  69608. }
  69609. else {
  69610. AssertNotNull(m);
  69611. AssertNull(to);
  69612. }
  69613. AssertNotNull(from);
  69614. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  69615. mn = EVP_get_digestbyname(from);
  69616. /* print to stderr */
  69617. AssertNotNull(arg);
  69618. bio = BIO_new(BIO_s_file());
  69619. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  69620. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  69621. BIO_free(bio);
  69622. #endif
  69623. }
  69624. #endif
  69625. static int test_EVP_MD_do_all(void)
  69626. {
  69627. int res = TEST_SKIPPED;
  69628. #if defined(OPENSSL_EXTRA)
  69629. EVP_MD_do_all(NULL, stderr);
  69630. EVP_MD_do_all(list_md_fn, stderr);
  69631. res = TEST_SUCCESS;
  69632. #endif
  69633. return res;
  69634. }
  69635. #if defined(OPENSSL_EXTRA)
  69636. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  69637. {
  69638. (void)arg;
  69639. (void)nm;
  69640. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  69641. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  69642. /* print to stderr */
  69643. AssertNotNull(arg);
  69644. BIO *bio = BIO_new(BIO_s_file());
  69645. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  69646. BIO_printf(bio, "%s\n", nm);
  69647. BIO_free(bio);
  69648. #endif
  69649. }
  69650. #endif
  69651. static int test_OBJ_NAME_do_all(void)
  69652. {
  69653. int res = TEST_SKIPPED;
  69654. #if defined(OPENSSL_EXTRA)
  69655. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  69656. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  69657. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  69658. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  69659. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  69660. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  69661. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  69662. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  69663. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  69664. res = TEST_SUCCESS;
  69665. #endif
  69666. return res;
  69667. }
  69668. static int test_SSL_CIPHER_get_xxx(void)
  69669. {
  69670. EXPECT_DECLS;
  69671. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  69672. !defined(NO_FILESYSTEM)
  69673. const SSL_CIPHER* cipher = NULL;
  69674. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  69675. int i, numCiphers = 0;
  69676. SSL_CTX* ctx = NULL;
  69677. SSL* ssl = NULL;
  69678. const char* testCertFile;
  69679. const char* testKeyFile;
  69680. char buf[256] = {0};
  69681. const char* cipher_id = NULL;
  69682. int expect_nid1 = NID_undef;
  69683. int expect_nid2 = NID_undef;
  69684. int expect_nid3 = NID_undef;
  69685. int expect_nid4 = NID_undef;
  69686. int expect_nid5 = 0;
  69687. const char* cipher_id2 = NULL;
  69688. int expect_nid21 = NID_undef;
  69689. int expect_nid22 = NID_undef;
  69690. int expect_nid23 = NID_undef;
  69691. int expect_nid24 = NID_undef;
  69692. int expect_nid25 = 0;
  69693. (void)cipher;
  69694. (void)supportedCiphers;
  69695. (void)i;
  69696. (void)numCiphers;
  69697. (void)ctx;
  69698. (void)ssl;
  69699. (void)testCertFile;
  69700. (void)testKeyFile;
  69701. #if defined(WOLFSSL_TLS13)
  69702. cipher_id = "TLS13-AES128-GCM-SHA256";
  69703. expect_nid1 = NID_auth_rsa;
  69704. expect_nid2 = NID_aes_128_gcm;
  69705. expect_nid3 = NID_sha256;
  69706. expect_nid4 = NID_kx_any;
  69707. expect_nid5 = 1;
  69708. #if !defined(WOLFSSL_NO_TLS12)
  69709. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  69710. expect_nid21 = NID_auth_rsa;
  69711. expect_nid22 = NID_aes_256_gcm;
  69712. expect_nid23 = NID_sha384;
  69713. expect_nid24 = NID_kx_ecdhe;
  69714. expect_nid25 = 1;
  69715. #endif
  69716. #endif
  69717. #ifdef NO_WOLFSSL_SERVER
  69718. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  69719. #else
  69720. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  69721. #endif
  69722. if (cipher_id) {
  69723. #ifndef NO_RSA
  69724. testCertFile = svrCertFile;
  69725. testKeyFile = svrKeyFile;
  69726. #elif defined(HAVE_ECC)
  69727. testCertFile = eccCertFile;
  69728. testKeyFile = eccKeyFile;
  69729. #else
  69730. testCertFile = NULL;
  69731. testKeyFile = NULL;
  69732. #endif
  69733. if (testCertFile != NULL && testKeyFile != NULL) {
  69734. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  69735. SSL_FILETYPE_PEM));
  69736. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  69737. SSL_FILETYPE_PEM));
  69738. }
  69739. ExpectNotNull(ssl = SSL_new(ctx));
  69740. ExpectIntEQ(SSL_in_init(ssl), 1);
  69741. supportedCiphers = SSL_get_ciphers(ssl);
  69742. numCiphers = sk_num(supportedCiphers);
  69743. for (i = 0; i < numCiphers; ++i) {
  69744. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  69745. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  69746. }
  69747. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  69748. break;
  69749. }
  69750. }
  69751. /* test case for */
  69752. if (i != numCiphers) {
  69753. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  69754. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  69755. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  69756. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  69757. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  69758. }
  69759. if (cipher_id2) {
  69760. for (i = 0; i < numCiphers; ++i) {
  69761. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  69762. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  69763. }
  69764. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  69765. break;
  69766. }
  69767. }
  69768. /* test case for */
  69769. if (i != numCiphers) {
  69770. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  69771. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  69772. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  69773. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  69774. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  69775. }
  69776. }
  69777. }
  69778. SSL_CTX_free(ctx);
  69779. SSL_free(ssl);
  69780. #endif
  69781. return EXPECT_RESULT();
  69782. }
  69783. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  69784. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  69785. int* keyFormat)
  69786. {
  69787. int ret;
  69788. byte* key_buf = NULL;
  69789. size_t key_sz = 0;
  69790. EncryptedInfo encInfo;
  69791. XMEMSET(&encInfo, 0, sizeof(encInfo));
  69792. ret = load_file(privKeyFile, &key_buf, &key_sz);
  69793. if (ret == 0) {
  69794. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  69795. NULL, &encInfo, keyFormat);
  69796. }
  69797. if (key_buf != NULL) {
  69798. free(key_buf); key_buf = NULL;
  69799. }
  69800. (void)encInfo; /* not used in this test */
  69801. #ifdef DEBUG_WOLFSSL
  69802. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  69803. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  69804. (*pDer)->length);
  69805. #endif
  69806. return ret;
  69807. }
  69808. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  69809. {
  69810. int ret = CRYPTOCB_UNAVAILABLE;
  69811. const char* privKeyFile = (const char*)ctx;
  69812. DerBuffer* pDer = NULL;
  69813. int keyFormat = 0;
  69814. if (info->algo_type == WC_ALGO_TYPE_PK) {
  69815. #ifdef DEBUG_WOLFSSL
  69816. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  69817. #endif
  69818. #ifndef NO_RSA
  69819. if (info->pk.type == WC_PK_TYPE_RSA) {
  69820. switch (info->pk.rsa.type) {
  69821. case RSA_PUBLIC_ENCRYPT:
  69822. case RSA_PUBLIC_DECRYPT:
  69823. /* perform software based RSA public op */
  69824. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  69825. break;
  69826. case RSA_PRIVATE_ENCRYPT:
  69827. case RSA_PRIVATE_DECRYPT:
  69828. {
  69829. RsaKey key;
  69830. /* perform software based RSA private op */
  69831. #ifdef DEBUG_WOLFSSL
  69832. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  69833. #endif
  69834. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  69835. &keyFormat);
  69836. if (ret != 0) {
  69837. return ret;
  69838. }
  69839. ret = wc_InitRsaKey(&key, HEAP_HINT);
  69840. if (ret == 0) {
  69841. word32 keyIdx = 0;
  69842. /* load RSA private key and perform private transform */
  69843. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  69844. &key, pDer->length);
  69845. if (ret == 0) {
  69846. ret = wc_RsaFunction(
  69847. info->pk.rsa.in, info->pk.rsa.inLen,
  69848. info->pk.rsa.out, info->pk.rsa.outLen,
  69849. info->pk.rsa.type, &key, info->pk.rsa.rng);
  69850. }
  69851. else {
  69852. /* if decode fails, then fall-back to software based crypto */
  69853. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  69854. "key decode failed %d, falling back to "
  69855. "software\n", ret);
  69856. ret = CRYPTOCB_UNAVAILABLE;
  69857. }
  69858. wc_FreeRsaKey(&key);
  69859. }
  69860. wc_FreeDer(&pDer); pDer = NULL;
  69861. break;
  69862. }
  69863. }
  69864. #ifdef DEBUG_WOLFSSL
  69865. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  69866. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  69867. #endif
  69868. }
  69869. #endif /* !NO_RSA */
  69870. #ifdef HAVE_ECC
  69871. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  69872. /* mark this key as ephemeral */
  69873. if (info->pk.eckg.key != NULL) {
  69874. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  69875. sizeof(info->pk.eckg.key->label));
  69876. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  69877. }
  69878. }
  69879. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  69880. ecc_key key;
  69881. /* perform software based ECC sign */
  69882. #ifdef DEBUG_WOLFSSL
  69883. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  69884. #endif
  69885. if (info->pk.eccsign.key != NULL &&
  69886. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  69887. /* this is an empheral key */
  69888. #ifdef DEBUG_WOLFSSL
  69889. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  69890. "ephemeral key\n");
  69891. #endif
  69892. return CRYPTOCB_UNAVAILABLE;
  69893. }
  69894. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  69895. if (ret != 0) {
  69896. return ret;
  69897. }
  69898. ret = wc_ecc_init(&key);
  69899. if (ret == 0) {
  69900. word32 keyIdx = 0;
  69901. /* load ECC private key and perform private transform */
  69902. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  69903. &key, pDer->length);
  69904. if (ret == 0) {
  69905. ret = wc_ecc_sign_hash(
  69906. info->pk.eccsign.in, info->pk.eccsign.inlen,
  69907. info->pk.eccsign.out, info->pk.eccsign.outlen,
  69908. info->pk.eccsign.rng, &key);
  69909. }
  69910. else {
  69911. /* if decode fails, then fall-back to software based crypto */
  69912. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  69913. "decode failed %d, falling back to software\n", ret);
  69914. ret = CRYPTOCB_UNAVAILABLE;
  69915. }
  69916. wc_ecc_free(&key);
  69917. }
  69918. wc_FreeDer(&pDer); pDer = NULL;
  69919. #ifdef DEBUG_WOLFSSL
  69920. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  69921. ret, *info->pk.eccsign.outlen);
  69922. #endif
  69923. }
  69924. #endif /* HAVE_ECC */
  69925. #ifdef HAVE_ED25519
  69926. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  69927. ed25519_key key;
  69928. /* perform software based ED25519 sign */
  69929. #ifdef DEBUG_WOLFSSL
  69930. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  69931. #endif
  69932. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  69933. if (ret != 0) {
  69934. return ret;
  69935. }
  69936. ret = wc_ed25519_init(&key);
  69937. if (ret == 0) {
  69938. word32 keyIdx = 0;
  69939. /* load ED25519 private key and perform private transform */
  69940. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  69941. &key, pDer->length);
  69942. if (ret == 0) {
  69943. /* calculate public key */
  69944. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  69945. if (ret == 0) {
  69946. key.pubKeySet = 1;
  69947. ret = wc_ed25519_sign_msg_ex(
  69948. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  69949. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  69950. &key, info->pk.ed25519sign.type,
  69951. info->pk.ed25519sign.context,
  69952. info->pk.ed25519sign.contextLen);
  69953. }
  69954. }
  69955. else {
  69956. /* if decode fails, then fall-back to software based crypto */
  69957. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  69958. "decode failed %d, falling back to software\n", ret);
  69959. ret = CRYPTOCB_UNAVAILABLE;
  69960. }
  69961. wc_ed25519_free(&key);
  69962. }
  69963. wc_FreeDer(&pDer); pDer = NULL;
  69964. #ifdef DEBUG_WOLFSSL
  69965. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  69966. ret, *info->pk.ed25519sign.outLen);
  69967. #endif
  69968. }
  69969. #endif /* HAVE_ED25519 */
  69970. }
  69971. (void)thisDevId;
  69972. (void)keyFormat;
  69973. return ret;
  69974. }
  69975. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  69976. static int test_wc_CryptoCb_TLS(int tlsVer,
  69977. const char* cliCaPemFile, const char* cliCertPemFile,
  69978. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  69979. const char* svrCaPemFile, const char* svrCertPemFile,
  69980. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  69981. {
  69982. EXPECT_DECLS;
  69983. callback_functions client_cbf;
  69984. callback_functions server_cbf;
  69985. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  69986. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  69987. if (tlsVer == WOLFSSL_TLSV1_3) {
  69988. #ifdef WOLFSSL_TLS13
  69989. server_cbf.method = wolfTLSv1_3_server_method;
  69990. client_cbf.method = wolfTLSv1_3_client_method;
  69991. #endif
  69992. }
  69993. else if (tlsVer == WOLFSSL_TLSV1_2) {
  69994. #ifndef WOLFSSL_NO_TLS12
  69995. server_cbf.method = wolfTLSv1_2_server_method;
  69996. client_cbf.method = wolfTLSv1_2_client_method;
  69997. #endif
  69998. }
  69999. else if (tlsVer == WOLFSSL_TLSV1_1) {
  70000. #ifndef NO_OLD_TLS
  70001. server_cbf.method = wolfTLSv1_1_server_method;
  70002. client_cbf.method = wolfTLSv1_1_client_method;
  70003. #endif
  70004. }
  70005. else if (tlsVer == WOLFSSL_TLSV1) {
  70006. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  70007. server_cbf.method = wolfTLSv1_server_method;
  70008. client_cbf.method = wolfTLSv1_client_method;
  70009. #endif
  70010. }
  70011. else if (tlsVer == WOLFSSL_SSLV3) {
  70012. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  70013. defined(WOLFSSL_STATIC_RSA)
  70014. server_cbf.method = wolfSSLv3_server_method;
  70015. client_cbf.method = wolfSSLv3_client_method;
  70016. #endif
  70017. }
  70018. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  70019. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  70020. server_cbf.method = wolfDTLSv1_2_server_method;
  70021. client_cbf.method = wolfDTLSv1_2_client_method;
  70022. #endif
  70023. }
  70024. else if (tlsVer == WOLFSSL_DTLSV1) {
  70025. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  70026. server_cbf.method = wolfDTLSv1_server_method;
  70027. client_cbf.method = wolfDTLSv1_client_method;
  70028. #endif
  70029. }
  70030. if (server_cbf.method == NULL) {
  70031. /* not enabled */
  70032. return TEST_SUCCESS;
  70033. }
  70034. /* Setup the keys for the TLS test */
  70035. client_cbf.certPemFile = cliCertPemFile;
  70036. client_cbf.keyPemFile = cliPubKeyPemFile;
  70037. client_cbf.caPemFile = cliCaPemFile;
  70038. server_cbf.certPemFile = svrCertPemFile;
  70039. server_cbf.keyPemFile = svrPubKeyPemFile;
  70040. server_cbf.caPemFile = svrCaPemFile;
  70041. /* Setup a crypto callback with pointer to private key file for testing */
  70042. client_cbf.devId = 1;
  70043. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  70044. (void*)cliPrivKeyPemFile);
  70045. server_cbf.devId = 2;
  70046. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  70047. (void*)svrPrivKeyPemFile);
  70048. /* Perform TLS server and client test */
  70049. /* First test is at WOLFSSL_CTX level */
  70050. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  70051. /* Check for success */
  70052. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  70053. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  70054. if (EXPECT_SUCCESS()) {
  70055. /* Second test is a WOLFSSL object level */
  70056. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  70057. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  70058. }
  70059. /* Check for success */
  70060. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  70061. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  70062. /* Un register the devId's */
  70063. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  70064. client_cbf.devId = INVALID_DEVID;
  70065. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  70066. server_cbf.devId = INVALID_DEVID;
  70067. return EXPECT_RESULT();
  70068. }
  70069. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  70070. static int test_wc_CryptoCb(void)
  70071. {
  70072. EXPECT_DECLS;
  70073. #ifdef WOLF_CRYPTO_CB
  70074. /* TODO: Add crypto callback API tests */
  70075. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  70076. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  70077. int tlsVer;
  70078. #endif
  70079. #ifndef NO_RSA
  70080. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  70081. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  70082. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  70083. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  70084. TEST_SUCCESS);
  70085. }
  70086. #endif
  70087. #ifdef HAVE_ECC
  70088. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  70089. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  70090. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  70091. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  70092. TEST_SUCCESS);
  70093. }
  70094. #endif
  70095. #ifdef HAVE_ED25519
  70096. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  70097. if (tlsVer == WOLFSSL_DTLSV1) continue;
  70098. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  70099. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  70100. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  70101. TEST_SUCCESS);
  70102. }
  70103. #endif
  70104. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  70105. #endif /* WOLF_CRYPTO_CB */
  70106. return EXPECT_RESULT();
  70107. }
  70108. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  70109. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  70110. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  70111. const char* cliCaPemFile, const char* cliCertPemFile,
  70112. const char* cliPrivKeyPemFile,
  70113. const char* svrCaPemFile, const char* svrCertPemFile,
  70114. const char* svrPrivKeyPemFile,
  70115. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  70116. {
  70117. EXPECT_DECLS;
  70118. callback_functions client_cbf;
  70119. callback_functions server_cbf;
  70120. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  70121. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  70122. if (tlsVer == WOLFSSL_TLSV1_3) {
  70123. #ifdef WOLFSSL_TLS13
  70124. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  70125. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  70126. #endif
  70127. }
  70128. else if (tlsVer == WOLFSSL_TLSV1_2) {
  70129. #ifndef WOLFSSL_NO_TLS12
  70130. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  70131. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  70132. #endif
  70133. }
  70134. else if (tlsVer == WOLFSSL_TLSV1_1) {
  70135. #ifndef NO_OLD_TLS
  70136. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  70137. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  70138. #endif
  70139. }
  70140. else if (tlsVer == WOLFSSL_TLSV1) {
  70141. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  70142. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  70143. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  70144. #endif
  70145. }
  70146. else if (tlsVer == WOLFSSL_SSLV3) {
  70147. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  70148. defined(WOLFSSL_STATIC_RSA)
  70149. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  70150. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  70151. #endif
  70152. }
  70153. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  70154. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  70155. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  70156. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  70157. #endif
  70158. }
  70159. else if (tlsVer == WOLFSSL_DTLSV1) {
  70160. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  70161. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  70162. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  70163. #endif
  70164. }
  70165. if (server_cbf.method_ex == NULL) {
  70166. /* not enabled */
  70167. return TEST_SUCCESS;
  70168. }
  70169. /* Setup the keys for the TLS test */
  70170. client_cbf.certPemFile = cliCertPemFile;
  70171. client_cbf.keyPemFile = cliPrivKeyPemFile;
  70172. client_cbf.caPemFile = cliCaPemFile;
  70173. server_cbf.certPemFile = svrCertPemFile;
  70174. server_cbf.keyPemFile = svrPrivKeyPemFile;
  70175. server_cbf.caPemFile = svrCaPemFile;
  70176. client_cbf.mem = cliMem;
  70177. client_cbf.memSz = cliMemSz;
  70178. server_cbf.mem = svrMem;
  70179. server_cbf.memSz = svrMemSz;
  70180. client_cbf.devId = INVALID_DEVID;
  70181. server_cbf.devId = INVALID_DEVID;
  70182. /* Perform TLS server and client test */
  70183. /* First test is at WOLFSSL_CTX level */
  70184. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  70185. /* Check for success */
  70186. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  70187. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  70188. if (EXPECT_SUCCESS()) {
  70189. /* Second test is a WOLFSSL object level */
  70190. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  70191. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  70192. }
  70193. /* Check for success */
  70194. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  70195. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  70196. return EXPECT_RESULT();
  70197. }
  70198. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  70199. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  70200. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  70201. {
  70202. EXPECT_DECLS;
  70203. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  70204. WOLFSSL_MEM_STATS mem_stats;
  70205. WOLFSSL_MEM_CONN_STATS ssl_stats;
  70206. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  70207. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  70208. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  70209. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  70210. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  70211. #endif
  70212. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  70213. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  70214. #ifndef WOLFSSL_STATIC_MEMORY_LEAN
  70215. /* this should fail because kMaxCtxClients == 2 */
  70216. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  70217. #else
  70218. (void)ssl3;
  70219. #endif
  70220. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  70221. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  70222. wolfSSL_PrintStatsConn(&ssl_stats);
  70223. #endif
  70224. (void)ssl_stats;
  70225. }
  70226. /* display collected statistics */
  70227. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  70228. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  70229. wolfSSL_PrintStats(&mem_stats);
  70230. #endif
  70231. (void)mem_stats;
  70232. }
  70233. wolfSSL_free(ssl1);
  70234. wolfSSL_free(ssl2);
  70235. return EXPECT_RESULT();
  70236. }
  70237. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  70238. static int test_wolfSSL_CTX_StaticMemory(void)
  70239. {
  70240. EXPECT_DECLS;
  70241. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  70242. wolfSSL_method_func method_func;
  70243. WOLFSSL_CTX* ctx;
  70244. const int kMaxCtxClients = 2;
  70245. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  70246. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  70247. int tlsVer;
  70248. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  70249. #endif
  70250. #endif
  70251. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  70252. #ifndef NO_WOLFSSL_SERVER
  70253. #ifndef WOLFSSL_NO_TLS12
  70254. method_func = wolfTLSv1_2_server_method_ex;
  70255. #else
  70256. method_func = wolfTLSv1_3_server_method_ex;
  70257. #endif
  70258. #else
  70259. #ifndef WOLFSSL_NO_TLS12
  70260. method_func = wolfTLSv1_2_client_method_ex;
  70261. #else
  70262. method_func = wolfTLSv1_3_client_method_ex;
  70263. #endif
  70264. #endif
  70265. /* Test creating CTX directly from static memory pool */
  70266. ctx = NULL;
  70267. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  70268. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  70269. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  70270. wolfSSL_CTX_free(ctx);
  70271. ctx = NULL;
  70272. /* Test for heap allocated CTX, then assigning static pool to it */
  70273. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  70274. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  70275. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  70276. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  70277. wolfSSL_CTX_free(ctx);
  70278. /* TLS Level Tests using static memory */
  70279. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  70280. #ifndef NO_RSA
  70281. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  70282. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  70283. svrCertFile, cliCertFile, cliKeyFile,
  70284. cliCertFile, svrCertFile, svrKeyFile,
  70285. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  70286. TEST_SUCCESS);
  70287. }
  70288. #endif
  70289. #ifdef HAVE_ECC
  70290. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  70291. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  70292. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  70293. cliEccCertFile, eccCertFile, eccKeyFile,
  70294. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  70295. TEST_SUCCESS);
  70296. }
  70297. #endif
  70298. #ifdef HAVE_ED25519
  70299. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  70300. if (tlsVer == WOLFSSL_DTLSV1) continue;
  70301. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  70302. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  70303. cliEdCertFile, edCertFile, edKeyFile,
  70304. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  70305. TEST_SUCCESS);
  70306. }
  70307. #endif
  70308. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  70309. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  70310. return EXPECT_RESULT();
  70311. }
  70312. static int test_openssl_FIPS_drbg(void)
  70313. {
  70314. EXPECT_DECLS;
  70315. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  70316. DRBG_CTX* dctx = NULL;
  70317. byte data1[32], data2[32], zeroData[32];
  70318. byte testSeed[16];
  70319. size_t dlen = sizeof(data1);
  70320. int i;
  70321. XMEMSET(data1, 0, dlen);
  70322. XMEMSET(data2, 0, dlen);
  70323. XMEMSET(zeroData, 0, sizeof(zeroData));
  70324. for (i = 0; i < (int)sizeof(testSeed); i++) {
  70325. testSeed[i] = (byte)i;
  70326. }
  70327. ExpectNotNull(dctx = FIPS_get_default_drbg());
  70328. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  70329. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  70330. WOLFSSL_SUCCESS);
  70331. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  70332. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  70333. WOLFSSL_SUCCESS);
  70334. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  70335. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  70336. WOLFSSL_SUCCESS);
  70337. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  70338. WOLFSSL_SUCCESS);
  70339. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  70340. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  70341. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  70342. #ifndef HAVE_GLOBAL_RNG
  70343. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  70344. wolfSSL_FIPS_drbg_free(dctx);
  70345. #endif
  70346. #endif
  70347. return EXPECT_RESULT();
  70348. }
  70349. static int test_wolfSSL_FIPS_mode(void)
  70350. {
  70351. EXPECT_DECLS;
  70352. #if defined(OPENSSL_ALL)
  70353. #ifdef HAVE_FIPS
  70354. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  70355. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  70356. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  70357. #else
  70358. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  70359. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  70360. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  70361. #endif
  70362. #endif
  70363. return EXPECT_RESULT();
  70364. }
  70365. #ifdef WOLFSSL_DTLS
  70366. /* Prints out the current window */
  70367. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  70368. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  70369. int i;
  70370. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  70371. word32 b = w[i];
  70372. int j;
  70373. /* Prints out a 32 bit binary number in big endian order */
  70374. for (j = 0; j < 32; j++, b <<= 1) {
  70375. if (b & (((word32)1) << 31))
  70376. fprintf(stderr, "1");
  70377. else
  70378. fprintf(stderr, "0");
  70379. }
  70380. fprintf(stderr, " ");
  70381. }
  70382. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  70383. #else
  70384. (void)h;
  70385. (void)l;
  70386. (void)w;
  70387. #endif
  70388. }
  70389. /* a - cur_hi
  70390. * b - cur_lo
  70391. * c - next_hi
  70392. * d - next_lo
  70393. * e - window
  70394. * f - expected next_hi
  70395. * g - expected next_lo
  70396. * h - expected window[1]
  70397. * i - expected window[0]
  70398. */
  70399. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  70400. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  70401. DUW_TEST_print_window_binary((a), (b), (e)); \
  70402. ExpectIntEQ((c), (f)); \
  70403. ExpectIntEQ((d), (g)); \
  70404. ExpectIntEQ((e)[1], (h)); \
  70405. ExpectIntEQ((e)[0], (i)); \
  70406. } while (0)
  70407. static int test_wolfSSL_DtlsUpdateWindow(void)
  70408. {
  70409. EXPECT_DECLS;
  70410. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  70411. word32 next_lo = 0;
  70412. word16 next_hi = 0;
  70413. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  70414. fprintf(stderr, "\n");
  70415. #endif
  70416. XMEMSET(window, 0, sizeof window);
  70417. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  70418. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  70419. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  70420. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  70421. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  70422. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  70423. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  70424. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  70425. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  70426. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  70427. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  70428. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  70429. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  70430. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  70431. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  70432. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  70433. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  70434. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  70435. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  70436. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  70437. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  70438. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  70439. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  70440. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  70441. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  70442. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  70443. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  70444. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  70445. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  70446. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  70447. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  70448. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  70449. return EXPECT_RESULT();
  70450. }
  70451. #endif /* WOLFSSL_DTLS */
  70452. #ifdef WOLFSSL_DTLS
  70453. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  70454. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  70455. {
  70456. DtlsMsg* cur;
  70457. static byte msg[100];
  70458. static byte msgInit = 0;
  70459. if (!msgInit) {
  70460. int i;
  70461. for (i = 0; i < 100; i++)
  70462. msg[i] = i + 1;
  70463. msgInit = 1;
  70464. }
  70465. /* Sanitize test parameters */
  70466. if (len > sizeof(msg))
  70467. return -1;
  70468. if (f_offset + f_len > sizeof(msg))
  70469. return -1;
  70470. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  70471. if (ssl->dtls_rx_msg_list == NULL)
  70472. return -100;
  70473. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  70474. return -200;
  70475. if (cur->fragBucketListCount != f_count)
  70476. return -300;
  70477. if (cur->ready != ready)
  70478. return -400;
  70479. if (cur->bytesReceived != bytesReceived)
  70480. return -500;
  70481. if (ready) {
  70482. if (cur->fragBucketList != NULL)
  70483. return -600;
  70484. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  70485. return -700;
  70486. }
  70487. else {
  70488. DtlsFragBucket* fb;
  70489. if (cur->fragBucketList == NULL)
  70490. return -800;
  70491. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  70492. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  70493. return -900;
  70494. }
  70495. }
  70496. return 0;
  70497. }
  70498. static int test_wolfSSL_DTLS_fragment_buckets(void)
  70499. {
  70500. EXPECT_DECLS;
  70501. WOLFSSL ssl[1];
  70502. XMEMSET(ssl, 0, sizeof(*ssl));
  70503. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  70504. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  70505. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  70506. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  70507. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  70508. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  70509. /* Test all permutations of 3 regions */
  70510. /* 1 2 3 */
  70511. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  70512. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  70513. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  70514. /* 1 3 2 */
  70515. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  70516. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  70517. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  70518. /* 2 1 3 */
  70519. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  70520. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  70521. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  70522. /* 2 3 1 */
  70523. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  70524. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  70525. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  70526. /* 3 1 2 */
  70527. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  70528. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  70529. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  70530. /* 3 2 1 */
  70531. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  70532. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  70533. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  70534. /* Test overlapping regions */
  70535. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  70536. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  70537. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  70538. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  70539. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  70540. /* Test overlapping multiple regions */
  70541. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  70542. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  70543. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  70544. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  70545. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  70546. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  70547. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  70548. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  70549. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  70550. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  70551. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  70552. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  70553. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  70554. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  70555. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  70556. ssl->dtls_rx_msg_list = NULL;
  70557. ssl->dtls_rx_msg_list_sz = 0;
  70558. return EXPECT_RESULT();
  70559. }
  70560. #endif
  70561. #if !defined(NO_FILESYSTEM) && \
  70562. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  70563. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  70564. static int test_wolfSSL_dtls_stateless2(void)
  70565. {
  70566. EXPECT_DECLS;
  70567. WOLFSSL *ssl_c = NULL;
  70568. WOLFSSL *ssl_c2 = NULL;
  70569. WOLFSSL *ssl_s = NULL;
  70570. struct test_memio_ctx test_ctx;
  70571. WOLFSSL_CTX *ctx_c = NULL;
  70572. WOLFSSL_CTX *ctx_s = NULL;
  70573. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  70574. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  70575. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  70576. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  70577. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  70578. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  70579. /* send CH */
  70580. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  70581. (ssl_c2->error == WANT_READ));
  70582. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  70583. (ssl_s->error == WANT_READ));
  70584. ExpectIntNE(test_ctx.c_len, 0);
  70585. /* consume HRR */
  70586. test_ctx.c_len = 0;
  70587. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  70588. wolfSSL_free(ssl_c2);
  70589. wolfSSL_free(ssl_c);
  70590. wolfSSL_free(ssl_s);
  70591. wolfSSL_CTX_free(ctx_c);
  70592. wolfSSL_CTX_free(ctx_s);
  70593. return EXPECT_RESULT();
  70594. }
  70595. #ifdef HAVE_MAX_FRAGMENT
  70596. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  70597. {
  70598. EXPECT_DECLS;
  70599. WOLFSSL *ssl_c = NULL;
  70600. WOLFSSL *ssl_c2 = NULL;
  70601. WOLFSSL *ssl_s = NULL;
  70602. struct test_memio_ctx test_ctx;
  70603. WOLFSSL_CTX *ctx_c = NULL;
  70604. WOLFSSL_CTX *ctx_s = NULL;
  70605. word16 max_fragment = 0;
  70606. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  70607. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  70608. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  70609. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  70610. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  70611. WOLFSSL_SUCCESS);
  70612. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  70613. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  70614. if (ssl_s != NULL) {
  70615. max_fragment = ssl_s->max_fragment;
  70616. }
  70617. /* send CH */
  70618. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  70619. (ssl_c2->error == WANT_READ));
  70620. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  70621. (ssl_s->error == WANT_READ));
  70622. /* CH without cookie shouldn't change state */
  70623. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  70624. ExpectIntNE(test_ctx.c_len, 0);
  70625. /* consume HRR from buffer */
  70626. test_ctx.c_len = 0;
  70627. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  70628. wolfSSL_free(ssl_c2);
  70629. wolfSSL_free(ssl_c);
  70630. wolfSSL_free(ssl_s);
  70631. wolfSSL_CTX_free(ctx_c);
  70632. wolfSSL_CTX_free(ctx_s);
  70633. return EXPECT_RESULT();
  70634. }
  70635. #endif /* HAVE_MAX_FRAGMENT */
  70636. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  70637. #define ROUNDS_WITH_HVR 4
  70638. #define ROUNDS_WITHOUT_HVR 2
  70639. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  70640. static int buf_is_hvr(const byte *data, int len)
  70641. {
  70642. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  70643. return 0;
  70644. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  70645. }
  70646. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  70647. {
  70648. EXPECT_DECLS;
  70649. struct test_memio_ctx test_ctx;
  70650. WOLFSSL_CTX *ctx_c = NULL;
  70651. WOLFSSL_CTX *ctx_s = NULL;
  70652. WOLFSSL *ssl_c = NULL;
  70653. WOLFSSL *ssl_s = NULL;
  70654. WOLFSSL_SESSION *sess = NULL;
  70655. int round_trips;
  70656. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  70657. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  70658. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  70659. #ifdef HAVE_SESSION_TICKET
  70660. if (useticket) {
  70661. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  70662. }
  70663. #endif
  70664. round_trips = ROUNDS_WITH_HVR;
  70665. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  70666. &round_trips), 0);
  70667. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  70668. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  70669. wolfSSL_shutdown(ssl_c);
  70670. wolfSSL_shutdown(ssl_s);
  70671. wolfSSL_free(ssl_c);
  70672. ssl_c = NULL;
  70673. wolfSSL_free(ssl_s);
  70674. ssl_s = NULL;
  70675. test_ctx.c_len = test_ctx.s_len = 0;
  70676. /* make resumption invalid */
  70677. if (bad && (sess != NULL)) {
  70678. if (useticket) {
  70679. #ifdef HAVE_SESSION_TICKET
  70680. if (sess->ticket != NULL) {
  70681. sess->ticket[0] = !sess->ticket[0];
  70682. }
  70683. #endif /* HAVE_SESSION_TICKET */
  70684. }
  70685. else {
  70686. sess->sessionID[0] = !sess->sessionID[0];
  70687. }
  70688. }
  70689. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  70690. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  70691. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  70692. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  70693. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  70694. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  70695. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  70696. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  70697. (ssl_c->error == WANT_READ));
  70698. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  70699. (ssl_s->error == WANT_READ));
  70700. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  70701. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  70702. if (!useticket) {
  70703. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  70704. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  70705. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  70706. }
  70707. wolfSSL_SESSION_free(sess);
  70708. wolfSSL_free(ssl_c);
  70709. wolfSSL_free(ssl_s);
  70710. wolfSSL_CTX_free(ctx_c);
  70711. wolfSSL_CTX_free(ctx_s);
  70712. return EXPECT_RESULT();
  70713. }
  70714. static int test_wolfSSL_dtls_stateless_resume(void)
  70715. {
  70716. EXPECT_DECLS;
  70717. #ifdef HAVE_SESSION_TICKET
  70718. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  70719. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  70720. #endif /* HAVE_SESION_TICKET */
  70721. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  70722. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  70723. return EXPECT_RESULT();
  70724. }
  70725. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  70726. #if !defined(NO_OLD_TLS)
  70727. static int test_wolfSSL_dtls_stateless_downgrade(void)
  70728. {
  70729. EXPECT_DECLS;
  70730. WOLFSSL_CTX *ctx_c = NULL;
  70731. WOLFSSL_CTX *ctx_c2 = NULL;
  70732. WOLFSSL_CTX *ctx_s = NULL;
  70733. WOLFSSL *ssl_c = NULL;
  70734. WOLFSSL *ssl_c2 = NULL;
  70735. WOLFSSL *ssl_s = NULL;
  70736. struct test_memio_ctx test_ctx;
  70737. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  70738. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  70739. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  70740. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  70741. WOLFSSL_SUCCESS);
  70742. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  70743. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  70744. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  70745. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  70746. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  70747. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  70748. /* send CH */
  70749. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  70750. (ssl_c2->error == WANT_READ));
  70751. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  70752. (ssl_s->error == WANT_READ));
  70753. ExpectIntNE(test_ctx.c_len, 0);
  70754. /* consume HRR */
  70755. test_ctx.c_len = 0;
  70756. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  70757. wolfSSL_free(ssl_c2);
  70758. wolfSSL_free(ssl_c);
  70759. wolfSSL_free(ssl_s);
  70760. wolfSSL_CTX_free(ctx_c);
  70761. wolfSSL_CTX_free(ctx_c2);
  70762. wolfSSL_CTX_free(ctx_s);
  70763. return EXPECT_RESULT();
  70764. }
  70765. #endif /* !defined(NO_OLD_TLS) */
  70766. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  70767. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  70768. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  70769. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  70770. static int test_WOLFSSL_dtls_version_alert(void)
  70771. {
  70772. EXPECT_DECLS;
  70773. struct test_memio_ctx test_ctx;
  70774. WOLFSSL_CTX *ctx_c = NULL;
  70775. WOLFSSL_CTX *ctx_s = NULL;
  70776. WOLFSSL *ssl_c = NULL;
  70777. WOLFSSL *ssl_s = NULL;
  70778. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  70779. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  70780. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  70781. /* client hello */
  70782. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  70783. (ssl_c->error == WANT_READ));
  70784. /* hrr */
  70785. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  70786. (ssl_s->error == WANT_READ));
  70787. /* client hello 1 */
  70788. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  70789. (ssl_c->error == WANT_READ));
  70790. /* server hello */
  70791. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  70792. (ssl_s->error == WANT_READ));
  70793. /* should fail */
  70794. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  70795. (ssl_c->error == VERSION_ERROR));
  70796. /* shuould fail */
  70797. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  70798. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  70799. wolfSSL_free(ssl_c);
  70800. wolfSSL_free(ssl_s);
  70801. wolfSSL_CTX_free(ctx_c);
  70802. wolfSSL_CTX_free(ctx_s);
  70803. return EXPECT_RESULT();
  70804. }
  70805. #else
  70806. static int test_WOLFSSL_dtls_version_alert(void)
  70807. {
  70808. return TEST_SKIPPED;
  70809. }
  70810. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  70811. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  70812. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  70813. */
  70814. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  70815. && defined(WOLFSSL_TLS13) && \
  70816. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  70817. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  70818. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  70819. {
  70820. struct test_memio_ctx *test_ctx;
  70821. byte buf[2048];
  70822. int idx, sz;
  70823. word32 tmp;
  70824. int ret;
  70825. idx = 5; /* space for record header */
  70826. buf[idx] = session_ticket; /* type */
  70827. idx++;
  70828. tmp = OPAQUE32_LEN +
  70829. OPAQUE32_LEN +
  70830. OPAQUE8_LEN + nonceLength +
  70831. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  70832. c32to24(tmp, buf + idx);
  70833. idx += OPAQUE24_LEN;
  70834. c32toa((word32)12345, buf+idx); /* lifetime */
  70835. idx += OPAQUE32_LEN;
  70836. c32toa((word32)12345, buf+idx); /* add */
  70837. idx += OPAQUE32_LEN;
  70838. buf[idx] = nonceLength; /* nonce length */
  70839. idx++;
  70840. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  70841. idx += nonceLength;
  70842. tmp = 1; /* ticket len */
  70843. c16toa((word16)tmp, buf+idx);
  70844. idx += 2;
  70845. buf[idx] = 0xFF; /* ticket */
  70846. idx++;
  70847. tmp = 0; /* ext len */
  70848. c16toa((word16)tmp, buf+idx);
  70849. idx += 2;
  70850. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  70851. handshake, 0, 0, 0);
  70852. AssertIntGT(sz, 0);
  70853. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  70854. AssertNotNull(test_ctx);
  70855. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  70856. return !(ret == sz);
  70857. }
  70858. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  70859. {
  70860. int ret = 0;
  70861. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  70862. ret = -1;
  70863. }
  70864. else {
  70865. int i;
  70866. for (i = 0; i < len; i++) {
  70867. if (sess->ticketNonce.data[i] != len) {
  70868. ret = -1;
  70869. break;
  70870. }
  70871. }
  70872. }
  70873. return ret;
  70874. }
  70875. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  70876. {
  70877. EXPECT_DECLS;
  70878. char *buf[1024];
  70879. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  70880. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  70881. (ssl_c->error == WANT_READ));
  70882. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  70883. return EXPECT_RESULT();
  70884. }
  70885. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  70886. {
  70887. EXPECT_DECLS;
  70888. WOLFSSL_SESSION *sess = NULL;
  70889. WOLFSSL_SESSION *cached = NULL;
  70890. WOLFSSL_CTX *ctx = ssl_c->ctx;
  70891. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  70892. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  70893. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  70894. NULL, ssl_c->options.side, 1,NULL), 0);
  70895. ExpectNotNull(cached = wolfSSL_SESSION_new());
  70896. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  70897. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  70898. wolfSSL_SESSION_free(cached);
  70899. wolfSSL_SESSION_free(sess);
  70900. return EXPECT_RESULT();
  70901. }
  70902. static int test_ticket_nonce_malloc(void)
  70903. {
  70904. EXPECT_DECLS;
  70905. struct test_memio_ctx test_ctx;
  70906. WOLFSSL_CTX *ctx_c = NULL;
  70907. WOLFSSL_CTX *ctx_s = NULL;
  70908. WOLFSSL *ssl_c = NULL;
  70909. WOLFSSL *ssl_s = NULL;
  70910. byte small;
  70911. byte medium;
  70912. byte big;
  70913. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  70914. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  70915. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  70916. /* will send ticket manually */
  70917. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  70918. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  70919. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  70920. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  70921. (ssl_s->options.handShakeDone == 0)) {
  70922. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  70923. (ssl_c->error == WANT_READ));
  70924. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  70925. (ssl_s->error == WANT_READ));
  70926. }
  70927. small = TLS13_TICKET_NONCE_STATIC_SZ;
  70928. #if TLS13_TICKET_NONCE_STATIC_SZ + 20 <= 255
  70929. medium = small + 20;
  70930. #else
  70931. medium = 255;
  70932. #endif
  70933. #if TLS13_TICKET_NONCE_STATIC_SZ + 20 + 20 <= 255
  70934. big = small + 20;
  70935. #else
  70936. big = 255;
  70937. #endif
  70938. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  70939. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  70940. ssl_c->session->ticketNonce.dataStatic);
  70941. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  70942. TEST_SUCCESS);
  70943. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  70944. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  70945. TEST_SUCCESS);
  70946. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  70947. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  70948. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  70949. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  70950. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  70951. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  70952. wolfSSL_free(ssl_c);
  70953. wolfSSL_free(ssl_s);
  70954. wolfSSL_CTX_free(ctx_c);
  70955. wolfSSL_CTX_free(ctx_s);
  70956. return EXPECT_RESULT();
  70957. }
  70958. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  70959. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  70960. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  70961. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  70962. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  70963. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  70964. static int test_ticket_ret_create(void)
  70965. {
  70966. EXPECT_DECLS;
  70967. WOLFSSL_CTX *ctx_c = NULL;
  70968. WOLFSSL_CTX *ctx_s = NULL;
  70969. WOLFSSL *ssl_c = NULL;
  70970. WOLFSSL *ssl_s = NULL;
  70971. byte ticket[SESSION_TICKET_LEN];
  70972. struct test_memio_ctx test_ctx;
  70973. WOLFSSL_SESSION *sess = NULL;
  70974. word16 ticketLen = 0;
  70975. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  70976. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  70977. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  70978. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  70979. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  70980. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  70981. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  70982. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  70983. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  70984. if (sess != NULL) {
  70985. ticketLen = sess->ticketLen;
  70986. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  70987. }
  70988. wolfSSL_free(ssl_c);
  70989. ssl_c = NULL;
  70990. wolfSSL_free(ssl_s);
  70991. ssl_s = NULL;
  70992. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  70993. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  70994. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  70995. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  70996. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  70997. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  70998. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  70999. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  71000. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  71001. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  71002. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  71003. wolfSSL_SESSION_free(sess);
  71004. wolfSSL_free(ssl_c);
  71005. wolfSSL_free(ssl_s);
  71006. wolfSSL_CTX_free(ctx_c);
  71007. wolfSSL_CTX_free(ctx_s);
  71008. return EXPECT_RESULT();
  71009. }
  71010. #else
  71011. static int test_ticket_ret_create(void)
  71012. {
  71013. return TEST_SKIPPED;
  71014. }
  71015. #endif
  71016. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  71017. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  71018. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  71019. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  71020. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  71021. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  71022. {
  71023. int ret;
  71024. WOLFSSL_SESSION* session = NULL;
  71025. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  71026. if (!wolfSSL_is_server(ssl)) {
  71027. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  71028. AssertNotNull(session);
  71029. }
  71030. do {
  71031. ret = wolfSSL_shutdown(ssl);
  71032. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  71033. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  71034. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  71035. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  71036. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  71037. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  71038. #endif
  71039. if (!wolfSSL_is_server(ssl)) {
  71040. /* client */
  71041. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  71042. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  71043. wolfSSL_set_session(ssl, session);
  71044. wolfSSL_SESSION_free(session);
  71045. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  71046. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  71047. }
  71048. else {
  71049. /* server */
  71050. /* Different ciphersuite so that the ticket will be invalidated based on
  71051. * the ciphersuite */
  71052. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  71053. WOLFSSL_SUCCESS);
  71054. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  71055. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  71056. }
  71057. }
  71058. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  71059. {
  71060. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  71061. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  71062. WOLFSSL_SUCCESS);
  71063. }
  71064. static int test_ticket_and_psk_mixing(void)
  71065. {
  71066. EXPECT_DECLS;
  71067. /* Test mixing tickets and regular PSK */
  71068. callback_functions client_cbs, server_cbs;
  71069. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  71070. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  71071. client_cbs.method = wolfTLSv1_3_client_method;
  71072. server_cbs.method = wolfTLSv1_3_server_method;
  71073. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  71074. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  71075. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  71076. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  71077. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  71078. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  71079. return EXPECT_RESULT();
  71080. }
  71081. #else
  71082. static int test_ticket_and_psk_mixing(void)
  71083. {
  71084. return TEST_SKIPPED;
  71085. }
  71086. #endif
  71087. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  71088. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  71089. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  71090. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  71091. static int test_prioritize_psk_cb_called = FALSE;
  71092. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  71093. const char* identity, unsigned char* key, unsigned int key_max_len,
  71094. const char** ciphersuite)
  71095. {
  71096. test_prioritize_psk_cb_called = TRUE;
  71097. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  71098. }
  71099. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  71100. {
  71101. int ret;
  71102. WOLFSSL_SESSION* session = NULL;
  71103. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  71104. if (!wolfSSL_is_server(ssl)) {
  71105. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  71106. AssertNotNull(session);
  71107. }
  71108. do {
  71109. ret = wolfSSL_shutdown(ssl);
  71110. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  71111. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  71112. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  71113. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  71114. * important. */
  71115. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  71116. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  71117. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  71118. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  71119. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  71120. #endif
  71121. if (!wolfSSL_is_server(ssl)) {
  71122. /* client */
  71123. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  71124. wolfSSL_set_session(ssl, session);
  71125. wolfSSL_SESSION_free(session);
  71126. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  71127. }
  71128. else {
  71129. /* server */
  71130. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  71131. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  71132. #ifdef WOLFSSL_PRIORITIZE_PSK
  71133. /* The ticket should be first tried with all ciphersuites and chosen */
  71134. AssertFalse(test_prioritize_psk_cb_called);
  71135. #else
  71136. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  71137. * callback that sets this var. */
  71138. AssertTrue(test_prioritize_psk_cb_called);
  71139. #endif
  71140. }
  71141. }
  71142. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  71143. {
  71144. if (!wolfSSL_is_server(ssl))
  71145. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  71146. WOLFSSL_SUCCESS);
  71147. else
  71148. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  71149. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  71150. }
  71151. static int test_prioritize_psk(void)
  71152. {
  71153. EXPECT_DECLS;
  71154. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  71155. * of the PSK's will be followed instead of the ciphersuite. */
  71156. callback_functions client_cbs, server_cbs;
  71157. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  71158. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  71159. client_cbs.method = wolfTLSv1_3_client_method;
  71160. server_cbs.method = wolfTLSv1_3_server_method;
  71161. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  71162. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  71163. client_cbs.on_result = test_prioritize_psk_on_result;
  71164. server_cbs.on_result = test_prioritize_psk_on_result;
  71165. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  71166. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  71167. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  71168. return EXPECT_RESULT();
  71169. }
  71170. #else
  71171. static int test_prioritize_psk(void)
  71172. {
  71173. return TEST_SKIPPED;
  71174. }
  71175. #endif
  71176. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  71177. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  71178. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  71179. !defined(WOLFSSL_NO_TLS12)
  71180. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  71181. {
  71182. EXPECT_DECLS;
  71183. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  71184. /* Set TLS 1.3 specific suite */
  71185. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  71186. return EXPECT_RESULT();
  71187. }
  71188. static int test_wolfSSL_CTX_set_ciphersuites(void)
  71189. {
  71190. EXPECT_DECLS;
  71191. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  71192. * do a 1.2 connection. */
  71193. test_ssl_cbf client_cbs, server_cbs;
  71194. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  71195. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  71196. client_cbs.method = wolfTLSv1_2_client_method;
  71197. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  71198. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  71199. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  71200. &server_cbs, NULL), TEST_SUCCESS);
  71201. return EXPECT_RESULT();
  71202. }
  71203. #else
  71204. static int test_wolfSSL_CTX_set_ciphersuites(void)
  71205. {
  71206. return TEST_SKIPPED;
  71207. }
  71208. #endif
  71209. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  71210. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  71211. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  71212. {
  71213. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  71214. return TEST_SUCCESS;
  71215. }
  71216. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  71217. {
  71218. EXPECT_DECLS;
  71219. WOLFSSL_ALERT_HISTORY h;
  71220. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  71221. ExpectIntEQ(h.last_rx.level, alert_fatal);
  71222. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  71223. return EXPECT_RESULT();
  71224. }
  71225. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  71226. {
  71227. EXPECT_DECLS;
  71228. test_ssl_cbf client_cbs, server_cbs;
  71229. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  71230. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  71231. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  71232. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  71233. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  71234. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  71235. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  71236. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  71237. &server_cbs, NULL), TEST_FAIL);
  71238. return EXPECT_RESULT();
  71239. }
  71240. #else
  71241. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  71242. {
  71243. return TEST_SKIPPED;
  71244. }
  71245. #endif
  71246. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  71247. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  71248. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  71249. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  71250. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  71251. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  71252. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  71253. static int test_TLS_13_ticket_different_ciphers_run = 0;
  71254. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  71255. {
  71256. EXPECT_DECLS;
  71257. switch (test_TLS_13_ticket_different_ciphers_run) {
  71258. case 0:
  71259. /* First run */
  71260. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  71261. WOLFSSL_SUCCESS);
  71262. if (wolfSSL_is_server(ssl)) {
  71263. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  71264. wolfSSL_get_SSL_CTX(ssl));
  71265. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  71266. test_TLS_13_ticket_different_ciphers_ctx));
  71267. }
  71268. break;
  71269. case 1:
  71270. /* Second run */
  71271. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  71272. "TLS13-AES128-GCM-SHA256"),
  71273. WOLFSSL_SUCCESS);
  71274. if (!wolfSSL_is_server(ssl)) {
  71275. ExpectIntEQ(wolfSSL_set_session(ssl,
  71276. test_TLS_13_ticket_different_ciphers_session),
  71277. WOLFSSL_SUCCESS);
  71278. }
  71279. break;
  71280. default:
  71281. /* Bad state? */
  71282. Fail(("Should not enter here"), ("Should not enter here"));
  71283. }
  71284. return EXPECT_RESULT();
  71285. }
  71286. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  71287. {
  71288. EXPECT_DECLS;
  71289. switch (test_TLS_13_ticket_different_ciphers_run) {
  71290. case 0:
  71291. /* First run */
  71292. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  71293. wolfSSL_get1_session(ssl));
  71294. break;
  71295. case 1:
  71296. /* Second run */
  71297. ExpectTrue(wolfSSL_session_reused(ssl));
  71298. break;
  71299. default:
  71300. /* Bad state? */
  71301. Fail(("Should not enter here"), ("Should not enter here"));
  71302. }
  71303. return EXPECT_RESULT();
  71304. }
  71305. static int test_TLS_13_ticket_different_ciphers(void)
  71306. {
  71307. EXPECT_DECLS;
  71308. /* Check that we handle the connection when the ticket doesn't match
  71309. * the first ciphersuite. */
  71310. test_ssl_cbf client_cbs, server_cbs;
  71311. struct test_params {
  71312. method_provider client_meth;
  71313. method_provider server_meth;
  71314. int doUdp;
  71315. } params[] = {
  71316. #ifdef WOLFSSL_DTLS13
  71317. /* Test that the stateless code handles sessions correctly */
  71318. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  71319. #endif
  71320. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  71321. };
  71322. size_t i;
  71323. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  71324. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  71325. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  71326. test_TLS_13_ticket_different_ciphers_run = 0;
  71327. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  71328. client_cbs.method = params[i].client_meth;
  71329. server_cbs.method = params[i].server_meth;
  71330. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  71331. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  71332. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  71333. server_cbs.ticNoInit = 1;
  71334. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  71335. &server_cbs, NULL), TEST_SUCCESS);
  71336. test_TLS_13_ticket_different_ciphers_run++;
  71337. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  71338. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  71339. &server_cbs, NULL), TEST_SUCCESS);
  71340. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  71341. test_TLS_13_ticket_different_ciphers_session = NULL;
  71342. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  71343. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  71344. }
  71345. return EXPECT_RESULT();
  71346. }
  71347. #else
  71348. static int test_TLS_13_ticket_different_ciphers(void)
  71349. {
  71350. return TEST_SKIPPED;
  71351. }
  71352. #endif
  71353. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  71354. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  71355. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  71356. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  71357. byte test_extra_alerts_wrong_cs_sh[] = {
  71358. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  71359. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  71360. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  71361. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  71362. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  71363. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  71364. 0xbb, 0x07, 0x54, 0x1c,
  71365. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  71366. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  71367. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  71368. };
  71369. static int test_extra_alerts_wrong_cs(void)
  71370. {
  71371. EXPECT_DECLS;
  71372. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  71373. struct test_memio_ctx test_ctx;
  71374. WOLFSSL_CTX *ctx_c = NULL;
  71375. WOLFSSL_ALERT_HISTORY h;
  71376. WOLFSSL *ssl_c = NULL;
  71377. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  71378. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  71379. wolfTLSv1_2_client_method, NULL), 0);
  71380. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  71381. WOLFSSL_SUCCESS);
  71382. /* CH */
  71383. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71384. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71385. WOLFSSL_ERROR_WANT_READ);
  71386. /* consume CH */
  71387. test_ctx.s_len = 0;
  71388. /* inject SH */
  71389. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  71390. sizeof(test_extra_alerts_wrong_cs_sh));
  71391. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  71392. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71393. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71394. WOLFSSL_ERROR_WANT_READ);
  71395. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  71396. ExpectIntEQ(h.last_tx.code, handshake_failure);
  71397. ExpectIntEQ(h.last_tx.level, alert_fatal);
  71398. wolfSSL_free(ssl_c);
  71399. wolfSSL_CTX_free(ctx_c);
  71400. #endif
  71401. return EXPECT_RESULT();
  71402. }
  71403. #else
  71404. static int test_extra_alerts_wrong_cs(void)
  71405. {
  71406. return TEST_SKIPPED;
  71407. }
  71408. #endif
  71409. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  71410. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  71411. #define TEST_CS_DOWNGRADE_CLIENT "ECDHE-RSA-AES256-GCM-SHA384"
  71412. byte test_wrong_cs_downgrade_sh[] = {
  71413. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0x10,
  71414. 0x2c, 0x88, 0xd9, 0x7a, 0x23, 0xc9, 0xbd, 0x11, 0x3b, 0x64, 0x24, 0xab,
  71415. 0x5b, 0x45, 0x33, 0xf6, 0x2c, 0x34, 0xe4, 0xcf, 0xf4, 0x78, 0xc8, 0x62,
  71416. 0x06, 0xc7, 0xe5, 0x30, 0x39, 0xbf, 0xa1, 0x20, 0xa3, 0x06, 0x74, 0xc3,
  71417. 0xa9, 0x74, 0x52, 0x8a, 0xfb, 0xae, 0xf0, 0xd8, 0x6f, 0xb2, 0x9d, 0xfe,
  71418. 0x78, 0xf0, 0x3f, 0x51, 0x8f, 0x9c, 0xcf, 0xbe, 0x61, 0x43, 0x9d, 0xf8,
  71419. 0x85, 0xe5, 0x2f, 0x54,
  71420. 0xc0, 0x2f, /* ECDHE-RSA-AES128-GCM-SHA256 */
  71421. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  71422. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  71423. };
  71424. static int test_wrong_cs_downgrade(void)
  71425. {
  71426. EXPECT_DECLS;
  71427. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  71428. struct test_memio_ctx test_ctx;
  71429. WOLFSSL_CTX *ctx_c = NULL;
  71430. WOLFSSL *ssl_c = NULL;
  71431. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  71432. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  71433. wolfSSLv23_client_method, NULL), 0);
  71434. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_CS_DOWNGRADE_CLIENT),
  71435. WOLFSSL_SUCCESS);
  71436. /* CH */
  71437. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71438. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71439. WOLFSSL_ERROR_WANT_READ);
  71440. /* consume CH */
  71441. test_ctx.s_len = 0;
  71442. /* inject SH */
  71443. XMEMCPY(test_ctx.c_buff, test_wrong_cs_downgrade_sh,
  71444. sizeof(test_wrong_cs_downgrade_sh));
  71445. test_ctx.c_len = sizeof(test_wrong_cs_downgrade_sh);
  71446. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71447. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71448. MATCH_SUITE_ERROR);
  71449. wolfSSL_free(ssl_c);
  71450. wolfSSL_CTX_free(ctx_c);
  71451. #endif
  71452. return EXPECT_RESULT();
  71453. }
  71454. #else
  71455. static int test_wrong_cs_downgrade(void)
  71456. {
  71457. return TEST_SKIPPED;
  71458. }
  71459. #endif
  71460. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  71461. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  71462. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  71463. {
  71464. tail_len -= msg_length;
  71465. XMEMMOVE(msg, msg + msg_length, tail_len);
  71466. *len = *len - msg_length;
  71467. }
  71468. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  71469. byte *found)
  71470. {
  71471. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  71472. const unsigned int _RECORD_HEADER_SZ = 5;
  71473. const int _change_cipher_hs = 55;
  71474. const int _change_cipher = 20;
  71475. const int _handshake = 22;
  71476. unsigned int tail_len;
  71477. byte *idx, *curr;
  71478. word8 currType;
  71479. word16 rLength;
  71480. word32 hLength;
  71481. idx = buf;
  71482. tail_len = (unsigned int)*len;
  71483. *found = 0;
  71484. while (tail_len > _RECORD_HEADER_SZ) {
  71485. curr = idx;
  71486. currType = *idx;
  71487. ato16(idx + 3, &rLength);
  71488. idx += _RECORD_HEADER_SZ;
  71489. tail_len -= _RECORD_HEADER_SZ;
  71490. if (tail_len < rLength)
  71491. return -1;
  71492. if (type == _change_cipher_hs && currType == _change_cipher) {
  71493. if (rLength != 1)
  71494. return -1;
  71495. /* match */
  71496. test_remove_msg(curr, *len - (int)(curr - buf),
  71497. len, _RECORD_HEADER_SZ + 1);
  71498. *found = 1;
  71499. return 0;
  71500. }
  71501. if (currType != _handshake) {
  71502. idx += rLength;
  71503. tail_len -= rLength;
  71504. continue;
  71505. }
  71506. if (rLength < _HANDSHAKE_HEADER_SZ)
  71507. return -1;
  71508. currType = *idx;
  71509. ato24(idx+1, &hLength);
  71510. hLength += _HANDSHAKE_HEADER_SZ;
  71511. if (tail_len < hLength)
  71512. return -1;
  71513. if (currType != type) {
  71514. idx += hLength;
  71515. tail_len -= hLength;
  71516. continue;
  71517. }
  71518. /* match */
  71519. test_remove_msg(curr, *len - (int)(curr - buf), len,
  71520. hLength + _RECORD_HEADER_SZ);
  71521. *found = 1;
  71522. return 0;
  71523. }
  71524. /* not found */
  71525. return 0;
  71526. }
  71527. static int test_remove_hs_message(byte hs_message_type,
  71528. int extra_round, byte alert_type)
  71529. {
  71530. EXPECT_DECLS;
  71531. WOLFSSL_CTX *ctx_c = NULL;
  71532. WOLFSSL_CTX *ctx_s = NULL;
  71533. WOLFSSL *ssl_c = NULL;
  71534. WOLFSSL *ssl_s = NULL;
  71535. struct test_memio_ctx test_ctx;
  71536. WOLFSSL_ALERT_HISTORY h;
  71537. byte found = 0;
  71538. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  71539. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  71540. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  71541. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71542. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71543. WOLFSSL_ERROR_WANT_READ);
  71544. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  71545. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71546. WOLFSSL_ERROR_WANT_READ);
  71547. if (extra_round) {
  71548. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71549. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71550. WOLFSSL_ERROR_WANT_READ);
  71551. /* this will complete handshake from server side */
  71552. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  71553. }
  71554. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  71555. &test_ctx.c_len, hs_message_type, &found), 0);
  71556. if (!found) {
  71557. wolfSSL_free(ssl_c);
  71558. wolfSSL_CTX_free(ctx_c);
  71559. wolfSSL_free(ssl_s);
  71560. wolfSSL_CTX_free(ctx_s);
  71561. return TEST_SKIPPED;
  71562. }
  71563. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71564. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71565. WOLFSSL_ERROR_WANT_READ);
  71566. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  71567. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  71568. ExpectIntEQ(h.last_tx.level, alert_fatal);
  71569. wolfSSL_free(ssl_c);
  71570. wolfSSL_CTX_free(ctx_c);
  71571. wolfSSL_free(ssl_s);
  71572. wolfSSL_CTX_free(ctx_s);
  71573. return EXPECT_RESULT();
  71574. }
  71575. static int test_extra_alerts_skip_hs(void)
  71576. {
  71577. EXPECT_DECLS;
  71578. const byte _server_key_exchange = 12;
  71579. const byte _server_hello = 2;
  71580. const byte _certificate = 11;
  71581. /* server_hello */
  71582. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  71583. unexpected_message), TEST_FAIL);
  71584. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  71585. 0xff), TEST_FAIL);
  71586. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  71587. unexpected_message), TEST_FAIL);
  71588. return EXPECT_RESULT();
  71589. }
  71590. #else
  71591. static int test_extra_alerts_skip_hs(void)
  71592. {
  71593. return TEST_SKIPPED;
  71594. }
  71595. #endif
  71596. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  71597. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  71598. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  71599. unsigned char* key, unsigned int key_max_len)
  71600. {
  71601. (void)ssl;
  71602. (void)id;
  71603. (void)key_max_len;
  71604. /* zero means error */
  71605. key[0] = 0x10;
  71606. return 1;
  71607. }
  71608. static int test_extra_alerts_bad_psk(void)
  71609. {
  71610. EXPECT_DECLS;
  71611. WOLFSSL_CTX *ctx_c = NULL;
  71612. WOLFSSL_CTX *ctx_s = NULL;
  71613. WOLFSSL *ssl_c = NULL;
  71614. WOLFSSL *ssl_s = NULL;
  71615. struct test_memio_ctx test_ctx;
  71616. WOLFSSL_ALERT_HISTORY h;
  71617. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  71618. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  71619. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  71620. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  71621. WOLFSSL_SUCCESS);
  71622. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  71623. WOLFSSL_SUCCESS);
  71624. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  71625. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71626. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71627. WOLFSSL_ERROR_WANT_READ);
  71628. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  71629. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  71630. WOLFSSL_ERROR_WANT_READ);
  71631. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71632. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71633. WOLFSSL_ERROR_WANT_READ);
  71634. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  71635. ExpectIntEQ(h.last_tx.code, handshake_failure);
  71636. ExpectIntEQ(h.last_tx.level, alert_fatal);
  71637. wolfSSL_free(ssl_c);
  71638. wolfSSL_CTX_free(ctx_c);
  71639. wolfSSL_free(ssl_s);
  71640. wolfSSL_CTX_free(ctx_s);
  71641. return EXPECT_RESULT();
  71642. }
  71643. #else
  71644. static int test_extra_alerts_bad_psk(void)
  71645. {
  71646. return TEST_SKIPPED;
  71647. }
  71648. #endif
  71649. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  71650. && !defined(NO_PSK)
  71651. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  71652. const char* hint, char* identity, unsigned int id_max_len,
  71653. unsigned char* key, unsigned int key_max_len)
  71654. {
  71655. (void)ssl;
  71656. (void)hint;
  71657. (void)key_max_len;
  71658. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  71659. XSTRNCPY(identity, "Client_identity", id_max_len);
  71660. key[0] = 0x20;
  71661. return 1;
  71662. }
  71663. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  71664. const char* id, unsigned char* key, unsigned int key_max_len)
  71665. {
  71666. (void)ssl;
  71667. (void)id;
  71668. (void)key_max_len;
  71669. /* zero means error */
  71670. key[0] = 0x10;
  71671. return 1;
  71672. }
  71673. #endif
  71674. static int test_tls13_bad_psk_binder(void)
  71675. {
  71676. EXPECT_DECLS;
  71677. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  71678. && !defined(NO_PSK)
  71679. WOLFSSL_CTX *ctx_c = NULL;
  71680. WOLFSSL_CTX *ctx_s = NULL;
  71681. WOLFSSL *ssl_c = NULL;
  71682. WOLFSSL *ssl_s = NULL;
  71683. struct test_memio_ctx test_ctx;
  71684. WOLFSSL_ALERT_HISTORY h;
  71685. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  71686. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  71687. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  71688. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  71689. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  71690. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71691. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71692. WOLFSSL_ERROR_WANT_READ);
  71693. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  71694. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  71695. BAD_BINDER);
  71696. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  71697. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  71698. FATAL_ERROR);
  71699. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  71700. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  71701. ExpectIntEQ(h.last_rx.level, alert_fatal);
  71702. wolfSSL_free(ssl_c);
  71703. wolfSSL_CTX_free(ctx_c);
  71704. wolfSSL_free(ssl_s);
  71705. wolfSSL_CTX_free(ctx_s);
  71706. #endif
  71707. return EXPECT_RESULT();
  71708. }
  71709. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  71710. defined(HAVE_IO_TESTS_DEPENDENCIES)
  71711. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  71712. int sz, void *ctx)
  71713. {
  71714. static int sentServerHello = FALSE;
  71715. if (!sentServerHello) {
  71716. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  71717. size_t i;
  71718. if (sz < (int)sizeof(renegExt))
  71719. return WOLFSSL_CBIO_ERR_GENERAL;
  71720. /* Remove SCR from ServerHello */
  71721. for (i = 0; i < sz - sizeof(renegExt); i++) {
  71722. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  71723. /* Found the extension. Change it to something unrecognized. */
  71724. buf[i+1] = 0x11;
  71725. break;
  71726. }
  71727. }
  71728. sentServerHello = TRUE;
  71729. }
  71730. return EmbedSend(ssl, buf, sz, ctx);
  71731. }
  71732. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  71733. {
  71734. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  71735. }
  71736. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  71737. {
  71738. WOLFSSL_ALERT_HISTORY h;
  71739. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  71740. AssertIntEQ(h.last_rx.code, handshake_failure);
  71741. AssertIntEQ(h.last_rx.level, alert_fatal);
  71742. }
  71743. static int test_harden_no_secure_renegotiation(void)
  71744. {
  71745. EXPECT_DECLS;
  71746. callback_functions client_cbs, server_cbs;
  71747. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  71748. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  71749. client_cbs.method = wolfTLSv1_2_client_method;
  71750. server_cbs.method = wolfTLSv1_2_server_method;
  71751. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  71752. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  71753. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  71754. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  71755. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  71756. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  71757. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  71758. server_cbs.last_err == FATAL_ERROR);
  71759. return EXPECT_RESULT();
  71760. }
  71761. #else
  71762. static int test_harden_no_secure_renegotiation(void)
  71763. {
  71764. return TEST_SKIPPED;
  71765. }
  71766. #endif
  71767. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  71768. static int test_override_alt_cert_chain_cert_cb(int preverify,
  71769. WOLFSSL_X509_STORE_CTX* store)
  71770. {
  71771. fprintf(stderr, "preverify: %d\n", preverify);
  71772. fprintf(stderr, "store->error: %d\n", store->error);
  71773. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  71774. if (store->error == OCSP_INVALID_STATUS) {
  71775. fprintf(stderr, "Overriding OCSP error\n");
  71776. return 1;
  71777. }
  71778. #ifndef WOLFSSL_ALT_CERT_CHAINS
  71779. else if ((store->error == ASN_NO_SIGNER_E ||
  71780. store->error == ASN_SELF_SIGNED_E
  71781. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  71782. defined(HAVE_WEBSERVER)
  71783. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  71784. #endif
  71785. ) && store->error_depth == store->totalCerts - 1) {
  71786. fprintf(stderr, "Overriding no signer error only for root cert\n");
  71787. return 1;
  71788. }
  71789. #endif
  71790. else
  71791. return preverify;
  71792. }
  71793. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  71794. int urlSz, unsigned char* request, int requestSz,
  71795. unsigned char** response)
  71796. {
  71797. (void)ioCtx;
  71798. (void)url;
  71799. (void)urlSz;
  71800. (void)request;
  71801. (void)requestSz;
  71802. (void)response;
  71803. return -1;
  71804. }
  71805. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  71806. {
  71807. EXPECT_DECLS;
  71808. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  71809. test_override_alt_cert_chain_cert_cb);
  71810. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  71811. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  71812. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  71813. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  71814. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  71815. WOLFSSL_SUCCESS);
  71816. return EXPECT_RESULT();
  71817. }
  71818. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  71819. {
  71820. EXPECT_DECLS;
  71821. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  71822. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  71823. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  71824. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  71825. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  71826. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  71827. WOLFSSL_SUCCESS);
  71828. return EXPECT_RESULT();
  71829. }
  71830. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  71831. {
  71832. EXPECT_DECLS;
  71833. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  71834. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  71835. return EXPECT_RESULT();
  71836. }
  71837. static int test_override_alt_cert_chain(void)
  71838. {
  71839. EXPECT_DECLS;
  71840. size_t i;
  71841. struct test_params {
  71842. ctx_cb client_ctx_cb;
  71843. ctx_cb server_ctx_cb;
  71844. int result;
  71845. } params[] = {
  71846. {test_override_alt_cert_chain_client_ctx_ready,
  71847. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  71848. {test_override_alt_cert_chain_client_ctx_ready2,
  71849. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  71850. };
  71851. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  71852. test_ssl_cbf client_cbs, server_cbs;
  71853. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  71854. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  71855. fprintf(stderr, "test config: %d\n", (int)i);
  71856. client_cbs.ctx_ready = params[i].client_ctx_cb;
  71857. server_cbs.ctx_ready = params[i].server_ctx_cb;
  71858. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  71859. &server_cbs, NULL), params[i].result);
  71860. ExpectIntEQ(client_cbs.return_code, params[i].result);
  71861. ExpectIntEQ(server_cbs.return_code, params[i].result);
  71862. }
  71863. return EXPECT_RESULT();
  71864. }
  71865. #else
  71866. static int test_override_alt_cert_chain(void)
  71867. {
  71868. return TEST_SKIPPED;
  71869. }
  71870. #endif
  71871. #if defined(HAVE_RPK)
  71872. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  71873. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  71874. #if defined(WOLFSSL_ALWAYS_VERIFY_CB) && defined(WOLFSSL_TLS13)
  71875. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  71876. {
  71877. int ret = WOLFSSL_SUCCESS;
  71878. (void)mode;
  71879. (void)strctx;
  71880. WOLFSSL_ENTER("MyRpkVerifyCb");
  71881. return ret;
  71882. }
  71883. #endif /* WOLFSSL_ALWAYS_VERIFY_CB && WOLFSSL_TLS13 */
  71884. static WC_INLINE int test_rpk_memio_setup(
  71885. struct test_memio_ctx *ctx,
  71886. WOLFSSL_CTX **ctx_c,
  71887. WOLFSSL_CTX **ctx_s,
  71888. WOLFSSL **ssl_c,
  71889. WOLFSSL **ssl_s,
  71890. method_provider method_c,
  71891. method_provider method_s,
  71892. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  71893. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  71894. const char* pkey_c, int fmt_kc, /* client private key and format */
  71895. const char* pkey_s, int fmt_ks /* server private key and format */
  71896. )
  71897. {
  71898. int ret;
  71899. if (ctx_c != NULL && *ctx_c == NULL) {
  71900. *ctx_c = wolfSSL_CTX_new(method_c());
  71901. if (*ctx_c == NULL) {
  71902. return -1;
  71903. }
  71904. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  71905. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  71906. if (ret != WOLFSSL_SUCCESS) {
  71907. return -1;
  71908. }
  71909. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  71910. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  71911. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  71912. if (ret != WOLFSSL_SUCCESS) {
  71913. return -1;
  71914. }
  71915. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  71916. if (ret != WOLFSSL_SUCCESS) {
  71917. return -1;
  71918. }
  71919. }
  71920. if (ctx_s != NULL && *ctx_s == NULL) {
  71921. *ctx_s = wolfSSL_CTX_new(method_s());
  71922. if (*ctx_s == NULL) {
  71923. return -1;
  71924. }
  71925. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  71926. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  71927. if (ret != WOLFSSL_SUCCESS) {
  71928. return -1;
  71929. }
  71930. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  71931. if (ret != WOLFSSL_SUCCESS) {
  71932. return -1;
  71933. }
  71934. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  71935. if (ret != WOLFSSL_SUCCESS) {
  71936. return -1;
  71937. }
  71938. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  71939. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  71940. if (ctx->s_ciphers != NULL) {
  71941. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  71942. if (ret != WOLFSSL_SUCCESS) {
  71943. return -1;
  71944. }
  71945. }
  71946. }
  71947. if (ctx_c != NULL && ssl_c != NULL) {
  71948. *ssl_c = wolfSSL_new(*ctx_c);
  71949. if (*ssl_c == NULL) {
  71950. return -1;
  71951. }
  71952. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  71953. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  71954. }
  71955. if (ctx_s != NULL && ssl_s != NULL) {
  71956. *ssl_s = wolfSSL_new(*ctx_s);
  71957. if (*ssl_s == NULL) {
  71958. return -1;
  71959. }
  71960. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  71961. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  71962. #if !defined(NO_DH)
  71963. SetDH(*ssl_s);
  71964. #endif
  71965. }
  71966. return 0;
  71967. }
  71968. #endif /* HAVE_RPK */
  71969. static int test_rpk_set_xxx_cert_type(void)
  71970. {
  71971. EXPECT_DECLS;
  71972. #if defined(HAVE_RPK)
  71973. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  71974. WOLFSSL_CTX* ctx = NULL;
  71975. WOLFSSL* ssl = NULL;
  71976. int tp;
  71977. ctx = wolfSSL_CTX_new(wolfTLS_client_method());
  71978. ExpectNotNull(ctx);
  71979. ssl = wolfSSL_new(ctx);
  71980. ExpectNotNull(ssl);
  71981. /*--------------------------------------------*/
  71982. /* tests for wolfSSL_CTX_set_client_cert_type */
  71983. /*--------------------------------------------*/
  71984. /* illegal parameter test caces */
  71985. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  71986. MAX_CLIENT_CERT_TYPE_CNT),
  71987. BAD_FUNC_ARG);
  71988. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  71989. sizeof(ctype)),
  71990. BAD_FUNC_ARG);
  71991. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  71992. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  71993. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  71994. MAX_CLIENT_CERT_TYPE_CNT),
  71995. BAD_FUNC_ARG);
  71996. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  71997. ctype[1] = 10; /* set unknown cert type */
  71998. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  71999. MAX_CLIENT_CERT_TYPE_CNT),
  72000. BAD_FUNC_ARG);
  72001. /* pass larger type count */
  72002. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  72003. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  72004. ctype[2] = 1; /* pass unacceptable type count */
  72005. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  72006. MAX_CLIENT_CERT_TYPE_CNT + 1),
  72007. BAD_FUNC_ARG);
  72008. /* should accept NULL for type buffer */
  72009. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  72010. MAX_CLIENT_CERT_TYPE_CNT),
  72011. WOLFSSL_SUCCESS);
  72012. /* should accept zero for type count */
  72013. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  72014. 0),
  72015. WOLFSSL_SUCCESS);
  72016. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  72017. MAX_CLIENT_CERT_TYPE_CNT),
  72018. WOLFSSL_SUCCESS);
  72019. /*--------------------------------------------*/
  72020. /* tests for wolfSSL_CTX_set_server_cert_type */
  72021. /*--------------------------------------------*/
  72022. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  72023. MAX_SERVER_CERT_TYPE_CNT),
  72024. BAD_FUNC_ARG);
  72025. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  72026. sizeof(ctype)),
  72027. BAD_FUNC_ARG);
  72028. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  72029. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  72030. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  72031. MAX_SERVER_CERT_TYPE_CNT),
  72032. BAD_FUNC_ARG);
  72033. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  72034. ctype[1] = 10; /* set unknown cert type */
  72035. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  72036. MAX_SERVER_CERT_TYPE_CNT),
  72037. BAD_FUNC_ARG);
  72038. /* pass larger type count */
  72039. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  72040. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  72041. ctype[2] = 1; /* pass unacceptable type count */
  72042. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  72043. MAX_SERVER_CERT_TYPE_CNT + 1),
  72044. BAD_FUNC_ARG);
  72045. /* should accept NULL for type buffer */
  72046. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  72047. MAX_SERVER_CERT_TYPE_CNT),
  72048. WOLFSSL_SUCCESS);
  72049. /* should accept zero for type count */
  72050. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  72051. 0),
  72052. WOLFSSL_SUCCESS);
  72053. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  72054. MAX_CLIENT_CERT_TYPE_CNT),
  72055. WOLFSSL_SUCCESS);
  72056. /*--------------------------------------------*/
  72057. /* tests for wolfSSL_set_client_cert_type */
  72058. /*--------------------------------------------*/
  72059. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  72060. MAX_CLIENT_CERT_TYPE_CNT),
  72061. BAD_FUNC_ARG);
  72062. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  72063. sizeof(ctype)),
  72064. BAD_FUNC_ARG);
  72065. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  72066. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  72067. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  72068. MAX_CLIENT_CERT_TYPE_CNT),
  72069. BAD_FUNC_ARG);
  72070. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  72071. ctype[1] = 10; /* set unknown cert type */
  72072. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  72073. MAX_CLIENT_CERT_TYPE_CNT),
  72074. BAD_FUNC_ARG);
  72075. /* pass larger type count */
  72076. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  72077. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  72078. ctype[2] = 1; /* pass unacceptable type count */
  72079. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  72080. MAX_CLIENT_CERT_TYPE_CNT + 1),
  72081. BAD_FUNC_ARG);
  72082. /* should accept NULL for type buffer */
  72083. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  72084. MAX_CLIENT_CERT_TYPE_CNT),
  72085. WOLFSSL_SUCCESS);
  72086. /* should accept zero for type count */
  72087. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  72088. 0),
  72089. WOLFSSL_SUCCESS);
  72090. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  72091. MAX_CLIENT_CERT_TYPE_CNT),
  72092. WOLFSSL_SUCCESS);
  72093. /*--------------------------------------------*/
  72094. /* tests for wolfSSL_CTX_set_server_cert_type */
  72095. /*--------------------------------------------*/
  72096. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  72097. MAX_SERVER_CERT_TYPE_CNT),
  72098. BAD_FUNC_ARG);
  72099. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  72100. sizeof(ctype)),
  72101. BAD_FUNC_ARG);
  72102. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  72103. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  72104. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  72105. MAX_SERVER_CERT_TYPE_CNT),
  72106. BAD_FUNC_ARG);
  72107. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  72108. ctype[1] = 10; /* set unknown cert type */
  72109. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  72110. MAX_SERVER_CERT_TYPE_CNT),
  72111. BAD_FUNC_ARG);
  72112. /* pass larger type count */
  72113. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  72114. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  72115. ctype[2] = 1; /* pass unacceptable type count */
  72116. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  72117. MAX_SERVER_CERT_TYPE_CNT + 1),
  72118. BAD_FUNC_ARG);
  72119. /* should accept NULL for type buffer */
  72120. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  72121. MAX_SERVER_CERT_TYPE_CNT),
  72122. WOLFSSL_SUCCESS);
  72123. /* should accept zero for type count */
  72124. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  72125. 0),
  72126. WOLFSSL_SUCCESS);
  72127. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  72128. MAX_SERVER_CERT_TYPE_CNT),
  72129. WOLFSSL_SUCCESS);
  72130. /*------------------------------------------------*/
  72131. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  72132. /*------------------------------------------------*/
  72133. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  72134. BAD_FUNC_ARG);
  72135. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  72136. BAD_FUNC_ARG);
  72137. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  72138. BAD_FUNC_ARG);
  72139. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  72140. BAD_FUNC_ARG);
  72141. /* clean up */
  72142. wolfSSL_free(ssl);
  72143. wolfSSL_CTX_free(ctx);
  72144. #endif
  72145. return EXPECT_RESULT();
  72146. }
  72147. static int test_tls13_rpk_handshake(void)
  72148. {
  72149. EXPECT_DECLS;
  72150. #if defined(HAVE_RPK) && (!defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13))
  72151. #ifdef WOLFSSL_TLS13
  72152. int ret = 0;
  72153. #endif
  72154. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  72155. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  72156. struct test_memio_ctx test_ctx;
  72157. int err;
  72158. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  72159. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  72160. int typeCnt_c;
  72161. int typeCnt_s;
  72162. int tp = 0;
  72163. #if defined(WOLFSSL_ALWAYS_VERIFY_CB) && defined(WOLFSSL_TLS13)
  72164. int isServer;
  72165. #endif
  72166. (void)err;
  72167. (void)typeCnt_c;
  72168. (void)typeCnt_s;
  72169. (void)certType_c;
  72170. (void)certType_s;
  72171. #ifndef WOLFSSL_NO_TLS12
  72172. /* TLS1.2
  72173. * Both client and server load x509 cert and start handshaking.
  72174. * Check no negotiation occurred.
  72175. */
  72176. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72177. ExpectIntEQ(
  72178. test_rpk_memio_setup(
  72179. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72180. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  72181. cliCertFile, WOLFSSL_FILETYPE_PEM,
  72182. svrCertFile, WOLFSSL_FILETYPE_PEM,
  72183. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  72184. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  72185. , 0);
  72186. /* set client certificate type in client end */
  72187. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  72188. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  72189. typeCnt_c = 2;
  72190. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  72191. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  72192. typeCnt_s = 2;
  72193. /* both client and server do not call client/server_cert_type APIs,
  72194. * expecting default settings works and no negotiation performed.
  72195. */
  72196. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  72197. return TEST_FAIL;
  72198. /* confirm no negotiation occurred */
  72199. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  72200. WOLFSSL_SUCCESS);
  72201. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72202. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  72203. WOLFSSL_SUCCESS);
  72204. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72205. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  72206. WOLFSSL_SUCCESS);
  72207. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72208. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  72209. WOLFSSL_SUCCESS);
  72210. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72211. (void)typeCnt_c;
  72212. (void)typeCnt_s;
  72213. wolfSSL_free(ssl_c);
  72214. wolfSSL_CTX_free(ctx_c);
  72215. wolfSSL_free(ssl_s);
  72216. wolfSSL_CTX_free(ctx_s);
  72217. ssl_c = ssl_s = NULL;
  72218. ctx_c = ctx_s = NULL;
  72219. #endif
  72220. #ifdef WOLFSSL_TLS13
  72221. /* Both client and server load x509 cert and start handshaking.
  72222. * Check no negotiation occurred.
  72223. */
  72224. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72225. ExpectIntEQ(
  72226. test_rpk_memio_setup(
  72227. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72228. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  72229. cliCertFile, WOLFSSL_FILETYPE_PEM,
  72230. svrCertFile, WOLFSSL_FILETYPE_PEM,
  72231. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  72232. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  72233. , 0);
  72234. /* set client certificate type in client end */
  72235. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  72236. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  72237. typeCnt_c = 2;
  72238. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  72239. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  72240. typeCnt_s = 2;
  72241. /* both client and server do not call client/server_cert_type APIs,
  72242. * expecting default settings works and no negotiation performed.
  72243. */
  72244. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  72245. return TEST_FAIL;
  72246. /* confirm no negotiation occurred */
  72247. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  72248. WOLFSSL_SUCCESS);
  72249. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72250. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  72251. WOLFSSL_SUCCESS);
  72252. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72253. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  72254. WOLFSSL_SUCCESS);
  72255. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72256. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  72257. WOLFSSL_SUCCESS);
  72258. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72259. (void)typeCnt_c;
  72260. (void)typeCnt_s;
  72261. wolfSSL_free(ssl_c);
  72262. wolfSSL_CTX_free(ctx_c);
  72263. wolfSSL_free(ssl_s);
  72264. wolfSSL_CTX_free(ctx_s);
  72265. ssl_c = ssl_s = NULL;
  72266. ctx_c = ctx_s = NULL;
  72267. /* Both client and server load RPK cert and start handshaking.
  72268. * Confirm negotiated cert types match as expected.
  72269. */
  72270. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72271. ExpectIntEQ(
  72272. test_rpk_memio_setup(
  72273. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72274. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  72275. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  72276. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  72277. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  72278. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  72279. , 0);
  72280. /* set client certificate type in client end */
  72281. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  72282. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  72283. typeCnt_c = 2;
  72284. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  72285. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  72286. typeCnt_s = 2;
  72287. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  72288. WOLFSSL_SUCCESS);
  72289. /* set server certificate type in client end */
  72290. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  72291. WOLFSSL_SUCCESS);
  72292. /* set client certificate type in server end */
  72293. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  72294. WOLFSSL_SUCCESS);
  72295. /* set server certificate type in server end */
  72296. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  72297. WOLFSSL_SUCCESS);
  72298. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  72299. return TEST_FAIL;
  72300. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  72301. WOLFSSL_SUCCESS);
  72302. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72303. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  72304. WOLFSSL_SUCCESS);
  72305. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72306. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  72307. WOLFSSL_SUCCESS);
  72308. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72309. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  72310. WOLFSSL_SUCCESS);
  72311. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72312. wolfSSL_free(ssl_c);
  72313. wolfSSL_CTX_free(ctx_c);
  72314. wolfSSL_free(ssl_s);
  72315. wolfSSL_CTX_free(ctx_s);
  72316. ssl_c = ssl_s = NULL;
  72317. ctx_c = ctx_s = NULL;
  72318. #endif
  72319. #ifndef WOLFSSL_NO_TLS12
  72320. /* TLS1.2
  72321. * Both client and server load RPK cert and start handshaking.
  72322. * Confirm negotiated cert types match as expected.
  72323. */
  72324. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72325. ExpectIntEQ(
  72326. test_rpk_memio_setup(
  72327. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72328. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  72329. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  72330. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  72331. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  72332. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  72333. , 0);
  72334. /* set client certificate type in client end */
  72335. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  72336. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  72337. typeCnt_c = 2;
  72338. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  72339. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  72340. typeCnt_s = 2;
  72341. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  72342. WOLFSSL_SUCCESS);
  72343. /* set server certificate type in client end */
  72344. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  72345. WOLFSSL_SUCCESS);
  72346. /* set client certificate type in server end */
  72347. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  72348. WOLFSSL_SUCCESS);
  72349. /* set server certificate type in server end */
  72350. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  72351. WOLFSSL_SUCCESS);
  72352. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  72353. return TEST_FAIL;
  72354. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  72355. WOLFSSL_SUCCESS);
  72356. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72357. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  72358. WOLFSSL_SUCCESS);
  72359. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72360. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  72361. WOLFSSL_SUCCESS);
  72362. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72363. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  72364. WOLFSSL_SUCCESS);
  72365. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72366. wolfSSL_free(ssl_c);
  72367. wolfSSL_CTX_free(ctx_c);
  72368. wolfSSL_free(ssl_s);
  72369. wolfSSL_CTX_free(ctx_s);
  72370. ssl_c = ssl_s = NULL;
  72371. ctx_c = ctx_s = NULL;
  72372. #endif
  72373. #ifdef WOLFSSL_TLS13
  72374. /* Both client and server load x509 cert.
  72375. * Have client call set_client_cert_type with both RPK and x509.
  72376. * This doesn't makes client add client cert type extension to ClientHello,
  72377. * since it does not load RPK cert actually.
  72378. */
  72379. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72380. ExpectIntEQ(
  72381. test_rpk_memio_setup(
  72382. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72383. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  72384. cliCertFile, WOLFSSL_FILETYPE_PEM,
  72385. svrCertFile, WOLFSSL_FILETYPE_PEM,
  72386. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  72387. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  72388. , 0);
  72389. /* set client certificate type in client end
  72390. *
  72391. * client indicates both RPK and x509 certs are available but loaded RPK
  72392. * cert only. It does not have client add client-cert-type extension in CH.
  72393. */
  72394. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  72395. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  72396. typeCnt_c = 2;
  72397. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  72398. WOLFSSL_SUCCESS);
  72399. /* client indicates both RPK and x509 certs are acceptable */
  72400. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  72401. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  72402. typeCnt_s = 2;
  72403. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  72404. WOLFSSL_SUCCESS);
  72405. /* server indicates both RPK and x509 certs are acceptable */
  72406. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  72407. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  72408. typeCnt_c = 2;
  72409. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  72410. WOLFSSL_SUCCESS);
  72411. /* server should indicate only RPK cert is available */
  72412. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  72413. certType_s[1] = -1;
  72414. typeCnt_s = 1;
  72415. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  72416. WOLFSSL_SUCCESS);
  72417. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  72418. return TEST_FAIL;
  72419. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  72420. * be returned as cert type.
  72421. */
  72422. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  72423. WOLFSSL_SUCCESS);
  72424. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72425. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  72426. WOLFSSL_SUCCESS);
  72427. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  72428. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  72429. WOLFSSL_SUCCESS);
  72430. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72431. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  72432. WOLFSSL_SUCCESS);
  72433. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  72434. wolfSSL_free(ssl_c);
  72435. wolfSSL_CTX_free(ctx_c);
  72436. wolfSSL_free(ssl_s);
  72437. wolfSSL_CTX_free(ctx_s);
  72438. ssl_c = ssl_s = NULL;
  72439. ctx_c = ctx_s = NULL;
  72440. /* Have client load RPK cert and have server load x509 cert.
  72441. * Check the negotiation result from both ends.
  72442. */
  72443. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72444. ExpectIntEQ(
  72445. test_rpk_memio_setup(
  72446. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72447. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  72448. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  72449. svrCertFile, WOLFSSL_FILETYPE_PEM,
  72450. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  72451. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  72452. , 0);
  72453. /* have client tell to use RPK cert */
  72454. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  72455. certType_c[1] = -1;
  72456. typeCnt_c = 1;
  72457. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  72458. WOLFSSL_SUCCESS);
  72459. /* have client tell to accept both RPK and x509 cert */
  72460. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  72461. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  72462. typeCnt_s = 2;
  72463. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  72464. WOLFSSL_SUCCESS);
  72465. /* have server accept to both RPK and x509 cert */
  72466. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  72467. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  72468. typeCnt_c = 2;
  72469. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  72470. WOLFSSL_SUCCESS);
  72471. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  72472. * end, expecting the default setting works.
  72473. */
  72474. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  72475. return TEST_FAIL;
  72476. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  72477. WOLFSSL_SUCCESS);
  72478. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72479. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  72480. WOLFSSL_SUCCESS);
  72481. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  72482. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  72483. WOLFSSL_SUCCESS);
  72484. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72485. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  72486. WOLFSSL_SUCCESS);
  72487. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  72488. wolfSSL_free(ssl_c);
  72489. wolfSSL_CTX_free(ctx_c);
  72490. wolfSSL_free(ssl_s);
  72491. wolfSSL_CTX_free(ctx_s);
  72492. ssl_c = ssl_s = NULL;
  72493. ctx_c = ctx_s = NULL;
  72494. /* Have both client and server load RPK cert, however, have server
  72495. * indicate its cert type x509.
  72496. * Client is expected to detect the cert type mismatch then to send alert
  72497. * with "unsupported_certificate".
  72498. */
  72499. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72500. ExpectIntEQ(
  72501. test_rpk_memio_setup(
  72502. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72503. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  72504. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  72505. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  72506. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  72507. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  72508. , 0);
  72509. /* have client tell to use RPK cert */
  72510. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  72511. certType_c[1] = -1;
  72512. typeCnt_c = 1;
  72513. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  72514. WOLFSSL_SUCCESS);
  72515. /* have client tell to accept both RPK and x509 cert */
  72516. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  72517. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  72518. typeCnt_s = 2;
  72519. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  72520. WOLFSSL_SUCCESS);
  72521. /* have server accept to both RPK and x509 cert */
  72522. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  72523. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  72524. typeCnt_c = 2;
  72525. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  72526. WOLFSSL_SUCCESS);
  72527. /* have server tell to use x509 cert intentionally. This will bring
  72528. * certificate type mismatch in client side.
  72529. */
  72530. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  72531. certType_s[1] = -1;
  72532. typeCnt_s = 1;
  72533. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  72534. WOLFSSL_SUCCESS);
  72535. /* expect client detect cert type mismatch then send Alert */
  72536. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  72537. if (ret != -1)
  72538. return TEST_FAIL;
  72539. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  72540. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  72541. WOLFSSL_SUCCESS);
  72542. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72543. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  72544. WOLFSSL_SUCCESS);
  72545. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  72546. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  72547. WOLFSSL_SUCCESS);
  72548. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72549. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  72550. WOLFSSL_SUCCESS);
  72551. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  72552. wolfSSL_free(ssl_c);
  72553. wolfSSL_CTX_free(ctx_c);
  72554. wolfSSL_free(ssl_s);
  72555. wolfSSL_CTX_free(ctx_s);
  72556. ssl_c = ssl_s = NULL;
  72557. ctx_c = ctx_s = NULL;
  72558. /* Have client load x509 cert and server load RPK cert,
  72559. * however, have client indicate its cert type RPK.
  72560. * Server is expected to detect the cert type mismatch then to send alert
  72561. * with "unsupported_certificate".
  72562. */
  72563. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72564. ExpectIntEQ(
  72565. test_rpk_memio_setup(
  72566. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72567. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  72568. cliCertFile, WOLFSSL_FILETYPE_PEM,
  72569. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  72570. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  72571. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  72572. , 0);
  72573. /* have client tell to use RPK cert intentionally */
  72574. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  72575. certType_c[1] = -1;
  72576. typeCnt_c = 1;
  72577. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  72578. WOLFSSL_SUCCESS);
  72579. /* have client tell to accept both RPK and x509 cert */
  72580. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  72581. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  72582. typeCnt_s = 2;
  72583. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  72584. WOLFSSL_SUCCESS);
  72585. /* have server accept to both RPK and x509 cert */
  72586. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  72587. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  72588. typeCnt_c = 2;
  72589. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  72590. WOLFSSL_SUCCESS);
  72591. /* have server tell to use x509 cert intentionally. This will bring
  72592. * certificate type mismatch in client side.
  72593. */
  72594. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  72595. certType_s[1] = -1;
  72596. typeCnt_s = 1;
  72597. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  72598. WOLFSSL_SUCCESS);
  72599. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  72600. /* expect server detect cert type mismatch then send Alert */
  72601. ExpectIntNE(ret, 0);
  72602. err = wolfSSL_get_error(ssl_c, ret);
  72603. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  72604. /* client did not load RPK cert actually, so negotiation did not happen */
  72605. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  72606. WOLFSSL_SUCCESS);
  72607. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72608. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  72609. WOLFSSL_SUCCESS);
  72610. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  72611. /* client did not load RPK cert actually, so negotiation did not happen */
  72612. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  72613. WOLFSSL_SUCCESS);
  72614. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  72615. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  72616. WOLFSSL_SUCCESS);
  72617. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  72618. wolfSSL_free(ssl_c);
  72619. wolfSSL_CTX_free(ctx_c);
  72620. wolfSSL_free(ssl_s);
  72621. wolfSSL_CTX_free(ctx_s);
  72622. ssl_c = ssl_s = NULL;
  72623. ctx_c = ctx_s = NULL;
  72624. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  72625. /* Both client and server load RPK cert and set certificate verify
  72626. * callbacks then start handshaking.
  72627. * Confirm both side can refer the peer's cert.
  72628. */
  72629. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72630. ExpectIntEQ(
  72631. test_rpk_memio_setup(
  72632. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72633. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  72634. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  72635. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  72636. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  72637. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  72638. , 0);
  72639. /* set client certificate type in client end */
  72640. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  72641. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  72642. typeCnt_c = 2;
  72643. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  72644. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  72645. typeCnt_s = 2;
  72646. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  72647. WOLFSSL_SUCCESS);
  72648. /* set server certificate type in client end */
  72649. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  72650. WOLFSSL_SUCCESS);
  72651. /* set client certificate type in server end */
  72652. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  72653. WOLFSSL_SUCCESS);
  72654. /* set server certificate type in server end */
  72655. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  72656. WOLFSSL_SUCCESS);
  72657. /* set certificate verify callback to both client and server */
  72658. isServer = 0;
  72659. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  72660. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  72661. isServer = 1;
  72662. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  72663. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  72664. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  72665. if (ret != 0)
  72666. return TEST_FAIL;
  72667. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  72668. WOLFSSL_SUCCESS);
  72669. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72670. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  72671. WOLFSSL_SUCCESS);
  72672. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72673. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  72674. WOLFSSL_SUCCESS);
  72675. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72676. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  72677. WOLFSSL_SUCCESS);
  72678. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  72679. wolfSSL_free(ssl_c);
  72680. wolfSSL_CTX_free(ctx_c);
  72681. wolfSSL_free(ssl_s);
  72682. wolfSSL_CTX_free(ctx_s);
  72683. ssl_c = ssl_s = NULL;
  72684. ctx_c = ctx_s = NULL;
  72685. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  72686. #endif /* WOLFSSL_TLS13 */
  72687. #endif /* HAVE_RPK && (!WOLFSSL_NO_TLS12 || WOLFSSL_TLS13) */
  72688. return EXPECT_RESULT();
  72689. }
  72690. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  72691. static int test_dtls13_bad_epoch_ch(void)
  72692. {
  72693. EXPECT_DECLS;
  72694. WOLFSSL_CTX *ctx_c = NULL;
  72695. WOLFSSL_CTX *ctx_s = NULL;
  72696. WOLFSSL *ssl_c = NULL;
  72697. WOLFSSL *ssl_s = NULL;
  72698. struct test_memio_ctx test_ctx;
  72699. const int EPOCH_OFF = 3;
  72700. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72701. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72702. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  72703. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  72704. * with just one message */
  72705. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  72706. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  72707. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  72708. WOLFSSL_ERROR_WANT_READ);
  72709. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  72710. /* first CH should use epoch 0x0 */
  72711. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  72712. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  72713. /* change epoch to 2 */
  72714. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  72715. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  72716. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  72717. WOLFSSL_ERROR_WANT_READ);
  72718. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  72719. /* resend the CH */
  72720. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  72721. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  72722. wolfSSL_free(ssl_c);
  72723. wolfSSL_CTX_free(ctx_c);
  72724. wolfSSL_free(ssl_s);
  72725. wolfSSL_CTX_free(ctx_s);
  72726. return EXPECT_RESULT();
  72727. }
  72728. #else
  72729. static int test_dtls13_bad_epoch_ch(void)
  72730. {
  72731. return TEST_SKIPPED;
  72732. }
  72733. #endif
  72734. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  72735. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  72736. {
  72737. EXPECT_DECLS;
  72738. WOLFSSL_SESSION *sess = NULL;
  72739. /* Setup the session to avoid errors */
  72740. ssl->session->timeout = -1;
  72741. ssl->session->side = WOLFSSL_CLIENT_END;
  72742. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  72743. defined(HAVE_SESSION_TICKET))
  72744. ssl->session->version = ssl->version;
  72745. #endif
  72746. /* Force a short session ID to be sent */
  72747. ssl->session->sessionIDSz = 4;
  72748. #ifndef NO_SESSION_CACHE_REF
  72749. /* Allow the client cache to be used */
  72750. ssl->session->idLen = 4;
  72751. #endif
  72752. ssl->session->isSetup = 1;
  72753. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  72754. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  72755. return EXPECT_RESULT();
  72756. }
  72757. static int test_short_session_id(void)
  72758. {
  72759. EXPECT_DECLS;
  72760. test_ssl_cbf client_cbf;
  72761. test_ssl_cbf server_cbf;
  72762. size_t i;
  72763. struct {
  72764. method_provider client_meth;
  72765. method_provider server_meth;
  72766. const char* tls_version;
  72767. } params[] = {
  72768. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  72769. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  72770. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  72771. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  72772. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  72773. #ifdef WOLFSSL_DTLS13
  72774. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  72775. #endif
  72776. #endif
  72777. #ifndef WOLFSSL_NO_TLS12
  72778. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  72779. #ifdef WOLFSSL_DTLS
  72780. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  72781. #endif
  72782. #endif
  72783. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  72784. !defined(NO_DES3))
  72785. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  72786. #ifdef WOLFSSL_DTLS
  72787. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  72788. #endif
  72789. #endif
  72790. };
  72791. fprintf(stderr, "\n");
  72792. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  72793. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  72794. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  72795. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  72796. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  72797. client_cbf.method = params[i].client_meth;
  72798. server_cbf.method = params[i].server_meth;
  72799. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  72800. &server_cbf, NULL), TEST_SUCCESS);
  72801. }
  72802. return EXPECT_RESULT();
  72803. }
  72804. #else
  72805. static int test_short_session_id(void)
  72806. {
  72807. return TEST_SKIPPED;
  72808. }
  72809. #endif
  72810. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  72811. && defined(WOLFSSL_DTLS13)
  72812. static byte* test_find_string(const char *string,
  72813. byte *buf, int buf_size)
  72814. {
  72815. int string_size, i;
  72816. string_size = (int)XSTRLEN(string);
  72817. for (i = 0; i < buf_size - string_size - 1; i++) {
  72818. if (XSTRCMP((char*)&buf[i], string) == 0)
  72819. return &buf[i];
  72820. }
  72821. return NULL;
  72822. }
  72823. static int test_wolfSSL_dtls13_null_cipher(void)
  72824. {
  72825. EXPECT_DECLS;
  72826. WOLFSSL_CTX *ctx_c = NULL;
  72827. WOLFSSL_CTX *ctx_s = NULL;
  72828. WOLFSSL *ssl_c = NULL;
  72829. WOLFSSL *ssl_s = NULL;
  72830. struct test_memio_ctx test_ctx;
  72831. const char *test_str = "test";
  72832. int test_str_size;
  72833. byte buf[255], *ptr = NULL;
  72834. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  72835. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  72836. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  72837. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  72838. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  72839. test_str_size = XSTRLEN("test") + 1;
  72840. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  72841. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  72842. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  72843. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  72844. /* check that the packet was sent cleartext */
  72845. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  72846. test_ctx.s_len));
  72847. if (ptr != NULL) {
  72848. /* modify the message */
  72849. *ptr = 'H';
  72850. /* bad messages should be ignored in DTLS */
  72851. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  72852. ExpectIntEQ(ssl_s->error, WANT_READ);
  72853. }
  72854. wolfSSL_free(ssl_c);
  72855. wolfSSL_free(ssl_s);
  72856. wolfSSL_CTX_free(ctx_c);
  72857. wolfSSL_CTX_free(ctx_s);
  72858. return TEST_SUCCESS;
  72859. }
  72860. #else
  72861. static int test_wolfSSL_dtls13_null_cipher(void)
  72862. {
  72863. return TEST_SKIPPED;
  72864. }
  72865. #endif
  72866. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  72867. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  72868. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  72869. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  72870. {
  72871. SOCKADDR_S peer;
  72872. XSOCKLENT len;
  72873. int ret;
  72874. XMEMSET((byte*)&peer, 0, sizeof(peer));
  72875. len = sizeof(peer);
  72876. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  72877. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  72878. return -1;
  72879. switch (peer.ss_family) {
  72880. #ifdef WOLFSSL_IPV6
  72881. case WOLFSSL_IP6: {
  72882. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  72883. break;
  72884. }
  72885. #endif /* WOLFSSL_IPV6 */
  72886. case WOLFSSL_IP4:
  72887. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  72888. break;
  72889. default:
  72890. return -1;
  72891. }
  72892. return 0;
  72893. }
  72894. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  72895. {
  72896. char buf[1] = {'t'};
  72897. SOCKADDR_IN_T addr;
  72898. int sock_fd;
  72899. word16 port;
  72900. int err;
  72901. (void)ssl;
  72902. (void)ctx;
  72903. if (ssl == NULL)
  72904. return -1;
  72905. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  72906. if (err != 0)
  72907. return -1;
  72908. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  72909. if (sock_fd == -1)
  72910. return -1;
  72911. build_addr(&addr, wolfSSLIP, port, 1, 0);
  72912. /* send a packet to the server. Being another socket, the kernel will ensure
  72913. * the source port will be different. */
  72914. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  72915. sizeof(addr));
  72916. close(sock_fd);
  72917. if (err == -1)
  72918. return -1;
  72919. return 0;
  72920. }
  72921. /* setup a SSL session but just after the handshake send a packet to the server
  72922. * with a source address different than the one of the connected client. The I/O
  72923. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  72924. * is done in test_dtls_msg_from_other_peer_cb */
  72925. static int test_dtls_msg_from_other_peer(void)
  72926. {
  72927. EXPECT_DECLS;
  72928. callback_functions client_cbs;
  72929. callback_functions server_cbs;
  72930. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  72931. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  72932. client_cbs.method = wolfDTLSv1_2_client_method;
  72933. server_cbs.method = wolfDTLSv1_2_server_method;
  72934. client_cbs.doUdp = 1;
  72935. server_cbs.doUdp = 1;
  72936. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  72937. test_dtls_msg_from_other_peer_cb);
  72938. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  72939. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  72940. return EXPECT_RESULT();
  72941. }
  72942. #else
  72943. static int test_dtls_msg_from_other_peer(void)
  72944. {
  72945. return TEST_SKIPPED;
  72946. }
  72947. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  72948. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  72949. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  72950. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  72951. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  72952. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  72953. static int test_dtls_ipv6_check(void)
  72954. {
  72955. EXPECT_DECLS;
  72956. WOLFSSL_CTX *ctx_c = NULL;
  72957. WOLFSSL_CTX *ctx_s = NULL;
  72958. WOLFSSL *ssl_c = NULL;
  72959. WOLFSSL *ssl_s = NULL;
  72960. SOCKADDR_IN fake_addr6;
  72961. int sockfd = -1;
  72962. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  72963. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  72964. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  72965. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  72966. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  72967. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  72968. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  72969. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  72970. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  72971. /* mimic a sockaddr_in6 struct, this way we can't test without
  72972. * WOLFSSL_IPV6 */
  72973. fake_addr6.sin_family = WOLFSSL_IP6;
  72974. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  72975. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  72976. /* can't return error here, as the peer is opaque for wolfssl library at
  72977. * this point */
  72978. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  72979. WOLFSSL_SUCCESS);
  72980. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  72981. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  72982. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  72983. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  72984. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  72985. WOLFSSL_SUCCESS);
  72986. /* reuse the socket */
  72987. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  72988. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  72989. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  72990. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  72991. if (sockfd != -1)
  72992. close(sockfd);
  72993. wolfSSL_free(ssl_c);
  72994. wolfSSL_CTX_free(ctx_c);
  72995. wolfSSL_free(ssl_s);
  72996. wolfSSL_CTX_free(ctx_s);
  72997. return EXPECT_RESULT();
  72998. }
  72999. #else
  73000. static int test_dtls_ipv6_check(void)
  73001. {
  73002. return TEST_SKIPPED;
  73003. }
  73004. #endif
  73005. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  73006. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  73007. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  73008. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  73009. {
  73010. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  73011. }
  73012. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  73013. {
  73014. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  73015. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  73016. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  73017. }
  73018. else {
  73019. char testMsg[] = "Message after SCR";
  73020. char msgBuf[sizeof(testMsg)];
  73021. int ret;
  73022. if (!wolfSSL_is_server(ssl)) {
  73023. AssertIntEQ(WOLFSSL_SUCCESS,
  73024. wolfSSL_set_session(ssl,
  73025. test_wolfSSL_SCR_after_resumption_session));
  73026. }
  73027. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  73028. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  73029. sizeof(testMsg));
  73030. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  73031. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  73032. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  73033. AssertIntEQ(ret, sizeof(msgBuf));
  73034. }
  73035. }
  73036. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  73037. {
  73038. AssertIntEQ(WOLFSSL_SUCCESS,
  73039. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  73040. }
  73041. static int test_wolfSSL_SCR_after_resumption(void)
  73042. {
  73043. EXPECT_DECLS;
  73044. callback_functions func_cb_client;
  73045. callback_functions func_cb_server;
  73046. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  73047. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  73048. func_cb_client.method = wolfTLSv1_2_client_method;
  73049. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  73050. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  73051. func_cb_server.method = wolfTLSv1_2_server_method;
  73052. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  73053. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  73054. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  73055. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  73056. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  73057. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  73058. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  73059. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  73060. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  73061. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  73062. return EXPECT_RESULT();
  73063. }
  73064. #else
  73065. static int test_wolfSSL_SCR_after_resumption(void)
  73066. {
  73067. return TEST_SKIPPED;
  73068. }
  73069. #endif
  73070. static int test_wolfSSL_configure_args(void)
  73071. {
  73072. EXPECT_DECLS;
  73073. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  73074. ExpectNotNull(wolfSSL_configure_args());
  73075. #endif
  73076. return EXPECT_RESULT();
  73077. }
  73078. static int test_dtls_no_extensions(void)
  73079. {
  73080. EXPECT_DECLS;
  73081. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  73082. !defined(WOLFSSL_NO_TLS12)
  73083. WOLFSSL *ssl_s = NULL;
  73084. WOLFSSL_CTX *ctx_s = NULL;
  73085. struct test_memio_ctx test_ctx;
  73086. const byte chNoExtensions[] = {
  73087. /* Handshake type */
  73088. 0x16,
  73089. /* Version */
  73090. 0xfe, 0xff,
  73091. /* Epoch */
  73092. 0x00, 0x00,
  73093. /* Seq number */
  73094. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73095. /* Length */
  73096. 0x00, 0x40,
  73097. /* CH type */
  73098. 0x01,
  73099. /* Length */
  73100. 0x00, 0x00, 0x34,
  73101. /* Msg Seq */
  73102. 0x00, 0x00,
  73103. /* Frag offset */
  73104. 0x00, 0x00, 0x00,
  73105. /* Frag length */
  73106. 0x00, 0x00, 0x34,
  73107. /* Version */
  73108. 0xfe, 0xff,
  73109. /* Random */
  73110. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  73111. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  73112. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  73113. /* Session Length */
  73114. 0x00,
  73115. /* Cookie Length */
  73116. 0x00,
  73117. /* CS Length */
  73118. 0x00, 0x0c,
  73119. /* CS */
  73120. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  73121. /* Comp Meths Length */
  73122. 0x01,
  73123. /* Comp Meths */
  73124. 0x00
  73125. /* And finally... no extensions */
  73126. };
  73127. int i;
  73128. #ifdef OPENSSL_EXTRA
  73129. int repeats = 2;
  73130. #else
  73131. int repeats = 1;
  73132. #endif
  73133. for (i = 0; i < repeats; i++) {
  73134. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  73135. ssl_s = NULL;
  73136. ctx_s = NULL;
  73137. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  73138. NULL, wolfDTLS_server_method), 0);
  73139. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  73140. test_ctx.s_len = sizeof(chNoExtensions);
  73141. #ifdef OPENSSL_EXTRA
  73142. if (i > 0) {
  73143. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  73144. WOLFSSL_SUCCESS);
  73145. }
  73146. #endif
  73147. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  73148. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  73149. /* Expecting a handshake msg. Either HVR or SH. */
  73150. ExpectIntGT(test_ctx.c_len, 0);
  73151. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  73152. wolfSSL_free(ssl_s);
  73153. wolfSSL_CTX_free(ctx_s);
  73154. }
  73155. #endif
  73156. return EXPECT_RESULT();
  73157. }
  73158. static int test_tls_alert_no_server_hello(void)
  73159. {
  73160. EXPECT_DECLS;
  73161. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  73162. WOLFSSL *ssl_c = NULL;
  73163. WOLFSSL_CTX *ctx_c = NULL;
  73164. struct test_memio_ctx test_ctx;
  73165. unsigned char alert_msg[] = { 0x15, 0x03, 0x01, 0x00, 0x02, 0x02, 0x28 };
  73166. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  73167. ssl_c = NULL;
  73168. ctx_c = NULL;
  73169. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  73170. wolfTLSv1_2_client_method, NULL), 0);
  73171. XMEMCPY(test_ctx.c_buff, alert_msg, sizeof(alert_msg));
  73172. test_ctx.c_len = sizeof(alert_msg);
  73173. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  73174. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), FATAL_ERROR);
  73175. wolfSSL_free(ssl_c);
  73176. wolfSSL_CTX_free(ctx_c);
  73177. #endif
  73178. return EXPECT_RESULT();
  73179. }
  73180. static int test_TLSX_CA_NAMES_bad_extension(void)
  73181. {
  73182. EXPECT_DECLS;
  73183. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  73184. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  73185. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  73186. defined(HAVE_NULL_CIPHER)
  73187. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  73188. * parse the CA Names extension. Otherwise it will return other non-related
  73189. * errors. If CA Names will be parsed in more configurations, that should
  73190. * be reflected in the macro guard above. */
  73191. WOLFSSL *ssl_c = NULL;
  73192. WOLFSSL_CTX *ctx_c = NULL;
  73193. struct test_memio_ctx test_ctx;
  73194. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  73195. const byte shBadCaNamesExt[] = {
  73196. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  73197. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  73198. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  73199. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  73200. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  73201. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  73202. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  73203. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  73204. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  73205. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  73206. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  73207. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  73208. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  73209. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  73210. };
  73211. const byte shBadCaNamesExt2[] = {
  73212. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  73213. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  73214. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  73215. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  73216. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  73217. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  73218. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  73219. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  73220. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  73221. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  73222. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  73223. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  73224. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  73225. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  73226. };
  73227. int i = 0;
  73228. for (i = 0; i < 2; i++) {
  73229. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  73230. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  73231. wolfTLSv1_3_client_method, NULL), 0);
  73232. switch (i) {
  73233. case 0:
  73234. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  73235. sizeof(shBadCaNamesExt));
  73236. test_ctx.c_len = sizeof(shBadCaNamesExt);
  73237. break;
  73238. case 1:
  73239. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  73240. sizeof(shBadCaNamesExt2));
  73241. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  73242. break;
  73243. }
  73244. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  73245. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  73246. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  73247. #else
  73248. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  73249. #endif
  73250. wolfSSL_free(ssl_c);
  73251. ssl_c = NULL;
  73252. wolfSSL_CTX_free(ctx_c);
  73253. ctx_c = NULL;
  73254. }
  73255. #endif
  73256. return EXPECT_RESULT();
  73257. }
  73258. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  73259. defined(HAVE_IO_TESTS_DEPENDENCIES)
  73260. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  73261. {
  73262. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  73263. WOLFSSL_SUCCESS);
  73264. }
  73265. static int test_dtls_1_0_hvr_downgrade(void)
  73266. {
  73267. EXPECT_DECLS;
  73268. callback_functions func_cb_client;
  73269. callback_functions func_cb_server;
  73270. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  73271. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  73272. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  73273. func_cb_client.method = wolfDTLS_client_method;
  73274. func_cb_server.method = wolfDTLSv1_2_server_method;
  73275. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  73276. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  73277. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  73278. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  73279. return EXPECT_RESULT();
  73280. }
  73281. #else
  73282. static int test_dtls_1_0_hvr_downgrade(void)
  73283. {
  73284. EXPECT_DECLS;
  73285. return EXPECT_RESULT();
  73286. }
  73287. #endif
  73288. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  73289. defined(HAVE_SESSION_TICKET)
  73290. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  73291. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  73292. {
  73293. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  73294. AssertNotNull(test_session_ticket_no_id_session);
  73295. }
  73296. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  73297. {
  73298. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  73299. }
  73300. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  73301. {
  73302. test_session_ticket_no_id_session->sessionIDSz = 0;
  73303. AssertIntEQ(WOLFSSL_SUCCESS,
  73304. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  73305. }
  73306. static int test_session_ticket_no_id(void)
  73307. {
  73308. /* We are testing an expired (invalid crypto context in out case since the
  73309. * ctx changes) session ticket being sent with the session ID being 0
  73310. * length. */
  73311. EXPECT_DECLS;
  73312. callback_functions func_cb_client;
  73313. callback_functions func_cb_server;
  73314. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  73315. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  73316. func_cb_client.method = wolfTLSv1_2_client_method;
  73317. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  73318. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  73319. func_cb_server.method = wolfTLSv1_2_server_method;
  73320. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  73321. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  73322. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  73323. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  73324. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  73325. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  73326. func_cb_client.method = wolfTLSv1_2_client_method;
  73327. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  73328. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  73329. func_cb_server.method = wolfTLSv1_2_server_method;
  73330. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  73331. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  73332. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  73333. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  73334. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  73335. return EXPECT_RESULT();
  73336. }
  73337. #else
  73338. static int test_session_ticket_no_id(void)
  73339. {
  73340. EXPECT_DECLS;
  73341. return EXPECT_RESULT();
  73342. }
  73343. #endif
  73344. static int test_session_ticket_hs_update(void)
  73345. {
  73346. EXPECT_DECLS;
  73347. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  73348. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  73349. struct test_memio_ctx test_ctx;
  73350. struct test_memio_ctx test_ctx2;
  73351. struct test_memio_ctx test_ctx3;
  73352. WOLFSSL_CTX *ctx_c = NULL;
  73353. WOLFSSL_CTX *ctx_s = NULL;
  73354. WOLFSSL *ssl_c = NULL;
  73355. WOLFSSL *ssl_c2 = NULL;
  73356. WOLFSSL *ssl_c3 = NULL;
  73357. WOLFSSL *ssl_s = NULL;
  73358. WOLFSSL *ssl_s2 = NULL;
  73359. WOLFSSL *ssl_s3 = NULL;
  73360. WOLFSSL_SESSION *sess = NULL;
  73361. byte read_data[1];
  73362. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  73363. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  73364. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  73365. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  73366. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  73367. /* Generate tickets */
  73368. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  73369. wolfSSL_SetLoggingPrefix("client");
  73370. /* Read the ticket msg */
  73371. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  73372. WOLFSSL_FATAL_ERROR);
  73373. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  73374. WOLFSSL_ERROR_WANT_READ);
  73375. wolfSSL_SetLoggingPrefix(NULL);
  73376. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  73377. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  73378. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  73379. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  73380. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  73381. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  73382. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  73383. wolfSSL_SetLoggingPrefix("client");
  73384. /* Exchange initial flights for the second connection */
  73385. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  73386. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  73387. WOLFSSL_ERROR_WANT_READ);
  73388. wolfSSL_SetLoggingPrefix(NULL);
  73389. wolfSSL_SetLoggingPrefix("server");
  73390. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  73391. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  73392. WOLFSSL_ERROR_WANT_READ);
  73393. wolfSSL_SetLoggingPrefix(NULL);
  73394. /* Complete third connection so that new tickets are exchanged */
  73395. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  73396. /* Read the ticket msg */
  73397. wolfSSL_SetLoggingPrefix("client");
  73398. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  73399. WOLFSSL_FATAL_ERROR);
  73400. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  73401. WOLFSSL_ERROR_WANT_READ);
  73402. wolfSSL_SetLoggingPrefix(NULL);
  73403. /* Complete second connection */
  73404. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  73405. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  73406. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  73407. wolfSSL_free(ssl_c);
  73408. wolfSSL_free(ssl_c2);
  73409. wolfSSL_free(ssl_c3);
  73410. wolfSSL_free(ssl_s);
  73411. wolfSSL_free(ssl_s2);
  73412. wolfSSL_free(ssl_s3);
  73413. wolfSSL_CTX_free(ctx_c);
  73414. wolfSSL_CTX_free(ctx_s);
  73415. wolfSSL_SESSION_free(sess);
  73416. #endif
  73417. return EXPECT_RESULT();
  73418. }
  73419. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  73420. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  73421. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  73422. {
  73423. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  73424. WOLFSSL_SUCCESS);
  73425. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  73426. }
  73427. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  73428. {
  73429. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  73430. }
  73431. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  73432. {
  73433. char testMsg[] = "Message after SCR";
  73434. char msgBuf[sizeof(testMsg)];
  73435. if (wolfSSL_is_server(ssl)) {
  73436. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  73437. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  73438. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  73439. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  73440. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  73441. sizeof(testMsg));
  73442. }
  73443. else {
  73444. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  73445. sizeof(testMsg));
  73446. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  73447. }
  73448. }
  73449. static int test_dtls_downgrade_scr_server(void)
  73450. {
  73451. EXPECT_DECLS;
  73452. callback_functions func_cb_client;
  73453. callback_functions func_cb_server;
  73454. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  73455. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  73456. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  73457. func_cb_client.method = wolfDTLSv1_2_client_method;
  73458. func_cb_server.method = wolfDTLS_server_method;
  73459. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  73460. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  73461. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  73462. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  73463. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  73464. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  73465. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  73466. return EXPECT_RESULT();
  73467. }
  73468. #else
  73469. static int test_dtls_downgrade_scr_server(void)
  73470. {
  73471. EXPECT_DECLS;
  73472. return EXPECT_RESULT();
  73473. }
  73474. #endif
  73475. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  73476. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  73477. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  73478. {
  73479. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  73480. WOLFSSL_SUCCESS);
  73481. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  73482. }
  73483. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  73484. {
  73485. char testMsg[] = "Message after SCR";
  73486. char msgBuf[sizeof(testMsg)];
  73487. if (wolfSSL_is_server(ssl)) {
  73488. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  73489. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  73490. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  73491. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  73492. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  73493. sizeof(testMsg));
  73494. }
  73495. else {
  73496. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  73497. sizeof(testMsg));
  73498. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  73499. }
  73500. }
  73501. static int test_dtls_downgrade_scr(void)
  73502. {
  73503. EXPECT_DECLS;
  73504. callback_functions func_cb_client;
  73505. callback_functions func_cb_server;
  73506. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  73507. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  73508. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  73509. func_cb_client.method = wolfDTLS_client_method;
  73510. func_cb_server.method = wolfDTLSv1_2_server_method;
  73511. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  73512. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  73513. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  73514. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  73515. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  73516. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  73517. return EXPECT_RESULT();
  73518. }
  73519. #else
  73520. static int test_dtls_downgrade_scr(void)
  73521. {
  73522. EXPECT_DECLS;
  73523. return EXPECT_RESULT();
  73524. }
  73525. #endif
  73526. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  73527. && !defined(WOLFSSL_NO_TLS12)
  73528. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  73529. char *data, int sz, void *ctx)
  73530. {
  73531. static int call_counter = 0;
  73532. call_counter++;
  73533. (void)ssl;
  73534. (void)data;
  73535. (void)sz;
  73536. (void)ctx;
  73537. switch (call_counter) {
  73538. case 1:
  73539. case 2:
  73540. return WOLFSSL_CBIO_ERR_TIMEOUT;
  73541. case 3:
  73542. return WOLFSSL_CBIO_ERR_WANT_READ;
  73543. default:
  73544. AssertIntLE(call_counter, 3);
  73545. return -1;
  73546. }
  73547. }
  73548. #endif
  73549. /* Make sure we don't send acks before getting a server hello */
  73550. static int test_dtls_client_hello_timeout_downgrade(void)
  73551. {
  73552. EXPECT_DECLS;
  73553. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  73554. && !defined(WOLFSSL_NO_TLS12)
  73555. WOLFSSL_CTX *ctx_c = NULL;
  73556. WOLFSSL_CTX *ctx_s = NULL;
  73557. WOLFSSL *ssl_c = NULL;
  73558. WOLFSSL *ssl_s = NULL;
  73559. struct test_memio_ctx test_ctx;
  73560. DtlsRecordLayerHeader* dtlsRH;
  73561. size_t len;
  73562. byte sequence_number[8];
  73563. int i;
  73564. for (i = 0; i < 2; i++) {
  73565. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  73566. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  73567. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  73568. if (i == 0) {
  73569. /* First time simulate timeout in IO layer */
  73570. /* CH1 */
  73571. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73572. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73573. /* HVR */
  73574. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  73575. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  73576. /* CH2 */
  73577. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73578. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73579. /* SH flight */
  73580. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  73581. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  73582. /* Drop the SH */
  73583. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  73584. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  73585. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  73586. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  73587. (sizeof(DtlsRecordLayerHeader) + len));
  73588. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  73589. /* Read the remainder of the flight */
  73590. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73591. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73592. wolfSSL_SSLSetIORecv(ssl_c,
  73593. test_dtls_client_hello_timeout_downgrade_read_cb);
  73594. /* CH3 */
  73595. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73596. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73597. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  73598. }
  73599. else {
  73600. /* Second time call wolfSSL_dtls_got_timeout */
  73601. /* CH1 */
  73602. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73603. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73604. /* HVR */
  73605. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  73606. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  73607. /* CH2 */
  73608. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73609. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73610. /* SH flight */
  73611. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  73612. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  73613. /* Drop the SH */
  73614. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  73615. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  73616. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  73617. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  73618. (sizeof(DtlsRecordLayerHeader) + len));
  73619. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  73620. /* Read the remainder of the flight */
  73621. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73622. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73623. /* Quick timeout should be set as we received at least one msg */
  73624. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  73625. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  73626. /* Quick timeout should be cleared after a quick timeout */
  73627. /* CH3 */
  73628. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  73629. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  73630. }
  73631. /* Parse out to make sure we got exactly one ClientHello message */
  73632. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  73633. /* Second ClientHello after HVR */
  73634. sequence_number[7] = 2;
  73635. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  73636. ExpectIntEQ(dtlsRH->type, handshake);
  73637. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  73638. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  73639. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  73640. sizeof(sequence_number)), 0);
  73641. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  73642. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  73643. /* Connection should be able to continue */
  73644. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  73645. wolfSSL_free(ssl_c);
  73646. wolfSSL_free(ssl_s);
  73647. wolfSSL_CTX_free(ctx_c);
  73648. wolfSSL_CTX_free(ctx_s);
  73649. ssl_c = NULL;
  73650. ssl_s = NULL;
  73651. ctx_c = NULL;
  73652. ctx_s = NULL;
  73653. if (!EXPECT_SUCCESS())
  73654. break;
  73655. }
  73656. #endif
  73657. return EXPECT_RESULT();
  73658. }
  73659. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  73660. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  73661. int sz, void *ctx)
  73662. {
  73663. static int call_counter = 0;
  73664. call_counter++;
  73665. (void)ssl;
  73666. (void)data;
  73667. (void)sz;
  73668. (void)ctx;
  73669. switch (call_counter) {
  73670. case 1:
  73671. return WOLFSSL_CBIO_ERR_TIMEOUT;
  73672. case 2:
  73673. return WOLFSSL_CBIO_ERR_WANT_READ;
  73674. default:
  73675. AssertIntLE(call_counter, 2);
  73676. return -1;
  73677. }
  73678. }
  73679. #endif
  73680. /* Make sure we don't send acks before getting a server hello */
  73681. static int test_dtls_client_hello_timeout(void)
  73682. {
  73683. EXPECT_DECLS;
  73684. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  73685. WOLFSSL *ssl_c = NULL;
  73686. WOLFSSL_CTX *ctx_c = NULL;
  73687. struct test_memio_ctx test_ctx;
  73688. DtlsRecordLayerHeader* dtlsRH;
  73689. size_t idx;
  73690. size_t len;
  73691. byte sequence_number[8];
  73692. int i;
  73693. for (i = 0; i < 2; i++) {
  73694. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  73695. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  73696. wolfDTLSv1_3_client_method, NULL), 0);
  73697. if (i == 0) {
  73698. /* First time simulate timeout in IO layer */
  73699. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  73700. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  73701. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73702. }
  73703. else {
  73704. /* Second time call wolfSSL_dtls_got_timeout */
  73705. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  73706. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73707. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  73708. }
  73709. /* Parse out to make sure we got exactly two ClientHello messages */
  73710. idx = 0;
  73711. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  73712. /* First ClientHello */
  73713. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  73714. ExpectIntEQ(dtlsRH->type, handshake);
  73715. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  73716. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  73717. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  73718. sizeof(sequence_number)), 0);
  73719. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  73720. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  73721. idx += sizeof(DtlsRecordLayerHeader) + len;
  73722. /* Second ClientHello */
  73723. sequence_number[7] = 1;
  73724. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  73725. ExpectIntEQ(dtlsRH->type, handshake);
  73726. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  73727. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  73728. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  73729. sizeof(sequence_number)), 0);
  73730. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  73731. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  73732. wolfSSL_free(ssl_c);
  73733. wolfSSL_CTX_free(ctx_c);
  73734. ssl_c = NULL;
  73735. ctx_c = NULL;
  73736. if (!EXPECT_SUCCESS())
  73737. break;
  73738. }
  73739. #endif
  73740. return EXPECT_RESULT();
  73741. }
  73742. /* DTLS test when dropping the changed cipher spec message */
  73743. static int test_dtls_dropped_ccs(void)
  73744. {
  73745. EXPECT_DECLS;
  73746. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  73747. && !defined(WOLFSSL_NO_TLS12)
  73748. WOLFSSL_CTX *ctx_c = NULL;
  73749. WOLFSSL_CTX *ctx_s = NULL;
  73750. WOLFSSL *ssl_c = NULL;
  73751. WOLFSSL *ssl_s = NULL;
  73752. struct test_memio_ctx test_ctx;
  73753. DtlsRecordLayerHeader* dtlsRH;
  73754. size_t len;
  73755. byte data[1];
  73756. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  73757. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  73758. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  73759. /* CH1 */
  73760. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73761. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73762. /* HVR */
  73763. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  73764. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  73765. /* CH2 */
  73766. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73767. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73768. /* Server first flight */
  73769. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  73770. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  73771. /* Client flight */
  73772. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73773. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73774. /* Server ccs + finished */
  73775. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  73776. /* Drop the ccs */
  73777. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  73778. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  73779. ExpectIntEQ(len, 1);
  73780. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  73781. if (EXPECT_SUCCESS()) {
  73782. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  73783. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  73784. (sizeof(DtlsRecordLayerHeader) + len));
  73785. }
  73786. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  73787. /* Client rtx flight */
  73788. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73789. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73790. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  73791. /* Server ccs + finished rtx */
  73792. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  73793. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  73794. /* Client processes finished */
  73795. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  73796. wolfSSL_free(ssl_c);
  73797. wolfSSL_free(ssl_s);
  73798. wolfSSL_CTX_free(ctx_c);
  73799. wolfSSL_CTX_free(ctx_s);
  73800. #endif
  73801. return EXPECT_RESULT();
  73802. }
  73803. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  73804. && !defined(WOLFSSL_NO_TLS12)
  73805. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  73806. byte seq_num)
  73807. {
  73808. EXPECT_DECLS;
  73809. DtlsRecordLayerHeader* dtlsRH;
  73810. byte sequence_number[8];
  73811. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  73812. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  73813. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  73814. ExpectIntEQ(dtlsRH->type, handshake);
  73815. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  73816. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  73817. sequence_number[7] = seq_num;
  73818. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  73819. sizeof(sequence_number)), 0);
  73820. return EXPECT_RESULT();
  73821. }
  73822. #endif
  73823. /*
  73824. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  73825. * and after a HelloRetryRequest. This is testing the server side as it is
  73826. * operating statelessly and should copy the sequence number of the ClientHello.
  73827. */
  73828. static int test_dtls_seq_num_downgrade(void)
  73829. {
  73830. EXPECT_DECLS;
  73831. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  73832. && !defined(WOLFSSL_NO_TLS12)
  73833. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  73834. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  73835. struct test_memio_ctx test_ctx;
  73836. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  73837. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  73838. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  73839. /* CH1 */
  73840. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73841. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73842. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  73843. test_ctx.s_len, 0), TEST_SUCCESS);
  73844. /* HVR */
  73845. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  73846. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  73847. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  73848. test_ctx.c_len, 0), TEST_SUCCESS);
  73849. /* CH2 */
  73850. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  73851. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  73852. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  73853. test_ctx.s_len, 1), TEST_SUCCESS);
  73854. /* Server first flight */
  73855. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  73856. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  73857. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  73858. test_ctx.c_len, 1), TEST_SUCCESS);
  73859. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  73860. wolfSSL_free(ssl_c);
  73861. wolfSSL_CTX_free(ctx_c);
  73862. wolfSSL_free(ssl_s);
  73863. wolfSSL_CTX_free(ctx_s);
  73864. #endif
  73865. return EXPECT_RESULT();
  73866. }
  73867. /**
  73868. * Make sure we don't send RSA Signature Hash Algorithms in the
  73869. * CertificateRequest when we don't have any such ciphers set.
  73870. * @return EXPECT_RESULT()
  73871. */
  73872. static int test_certreq_sighash_algos(void)
  73873. {
  73874. EXPECT_DECLS;
  73875. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  73876. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  73877. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  73878. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  73879. WOLFSSL_CTX *ctx_c = NULL;
  73880. WOLFSSL_CTX *ctx_s = NULL;
  73881. WOLFSSL *ssl_c = NULL;
  73882. WOLFSSL *ssl_s = NULL;
  73883. struct test_memio_ctx test_ctx;
  73884. int idx = 0;
  73885. int maxIdx = 0;
  73886. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  73887. test_ctx.c_ciphers = test_ctx.s_ciphers =
  73888. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  73889. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  73890. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  73891. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  73892. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  73893. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  73894. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  73895. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  73896. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  73897. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  73898. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  73899. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  73900. WOLFSSL_ERROR_WANT_READ);
  73901. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  73902. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  73903. WOLFSSL_ERROR_WANT_READ);
  73904. /* Find the CertificateRequest message */
  73905. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  73906. word16 len;
  73907. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  73908. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  73909. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  73910. ato16(test_ctx.c_buff + idx, &len);
  73911. idx += OPAQUE16_LEN;
  73912. if (test_ctx.c_buff[idx] == certificate_request) {
  73913. idx++;
  73914. /* length */
  73915. idx += OPAQUE24_LEN;
  73916. /* cert types */
  73917. idx += 1 + test_ctx.c_buff[idx];
  73918. /* Sig algos */
  73919. ato16(test_ctx.c_buff + idx, &len);
  73920. idx += OPAQUE16_LEN;
  73921. maxIdx = idx + (int)len;
  73922. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  73923. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  73924. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  73925. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  73926. else
  73927. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  73928. }
  73929. break;
  73930. }
  73931. else {
  73932. idx += (int)len;
  73933. }
  73934. }
  73935. ExpectIntLT(idx, test_ctx.c_len);
  73936. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  73937. wolfSSL_free(ssl_c);
  73938. wolfSSL_free(ssl_s);
  73939. wolfSSL_CTX_free(ctx_c);
  73940. wolfSSL_CTX_free(ctx_s);
  73941. #endif
  73942. return EXPECT_RESULT();
  73943. }
  73944. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  73945. !defined(WOLFSSL_CRL_ALLOW_MISSING_CDP)
  73946. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  73947. {
  73948. EXPECT_DECLS;
  73949. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  73950. myVerifyAction = VERIFY_USE_PREVERFIY;
  73951. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  73952. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  73953. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  73954. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  73955. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  73956. WOLFSSL_SUCCESS);
  73957. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  73958. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  73959. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  73960. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  73961. "./certs/crl/ca-int.pem",
  73962. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  73963. return EXPECT_RESULT();
  73964. }
  73965. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  73966. {
  73967. EXPECT_DECLS;
  73968. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  73969. myVerifyAction = VERIFY_USE_PREVERFIY;
  73970. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  73971. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  73972. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  73973. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  73974. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  73975. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  73976. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  73977. WOLFSSL_SUCCESS);
  73978. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  73979. "./certs/crl/ca-int2.pem",
  73980. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  73981. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  73982. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  73983. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  73984. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  73985. "./certs/crl/ca-int.pem",
  73986. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  73987. return EXPECT_RESULT();
  73988. }
  73989. #endif
  73990. static int test_revoked_loaded_int_cert(void)
  73991. {
  73992. EXPECT_DECLS;
  73993. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  73994. !defined(WOLFSSL_CRL_ALLOW_MISSING_CDP)
  73995. test_ssl_cbf client_cbf;
  73996. test_ssl_cbf server_cbf;
  73997. struct {
  73998. const char* certPemFile;
  73999. const char* keyPemFile;
  74000. ctx_cb client_ctx_ready;
  74001. } test_params[] = {
  74002. {"./certs/intermediate/ca-int2-cert.pem",
  74003. "./certs/intermediate/ca-int2-key.pem",
  74004. test_revoked_loaded_int_cert_ctx_ready1},
  74005. {"./certs/intermediate/server-chain.pem",
  74006. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  74007. {"./certs/intermediate/server-chain-short.pem",
  74008. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  74009. };
  74010. size_t i;
  74011. printf("\n");
  74012. for (i = 0; i < XELEM_CNT(test_params); i++) {
  74013. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  74014. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  74015. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  74016. server_cbf.certPemFile = test_params[i].certPemFile;
  74017. server_cbf.keyPemFile = test_params[i].keyPemFile;
  74018. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  74019. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  74020. &server_cbf, NULL), TEST_FAIL);
  74021. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  74022. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  74023. if (!EXPECT_SUCCESS())
  74024. break;
  74025. printf("\t%s passed\n", test_params[i].certPemFile);
  74026. }
  74027. #endif
  74028. return EXPECT_RESULT();
  74029. }
  74030. static int test_dtls13_frag_ch_pq(void)
  74031. {
  74032. EXPECT_DECLS;
  74033. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  74034. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  74035. WOLFSSL_CTX *ctx_c = NULL;
  74036. WOLFSSL_CTX *ctx_s = NULL;
  74037. WOLFSSL *ssl_c = NULL;
  74038. WOLFSSL *ssl_s = NULL;
  74039. struct test_memio_ctx test_ctx;
  74040. const char *test_str = "test";
  74041. int test_str_size;
  74042. byte buf[255];
  74043. int group = WOLFSSL_KYBER_LEVEL5;
  74044. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  74045. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  74046. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  74047. /* Add in a large post-quantum key share to make the CH long. */
  74048. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  74049. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  74050. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  74051. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  74052. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  74053. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  74054. test_str_size = XSTRLEN("test") + 1;
  74055. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  74056. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  74057. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  74058. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  74059. wolfSSL_free(ssl_c);
  74060. wolfSSL_free(ssl_s);
  74061. wolfSSL_CTX_free(ctx_c);
  74062. wolfSSL_CTX_free(ctx_s);
  74063. #endif
  74064. return EXPECT_RESULT();
  74065. }
  74066. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  74067. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  74068. static int test_dtls_frag_ch_count_records(byte* b, int len)
  74069. {
  74070. DtlsRecordLayerHeader* dtlsRH;
  74071. int records = 0;
  74072. size_t recordLen;
  74073. while (len > 0) {
  74074. records++;
  74075. dtlsRH = (DtlsRecordLayerHeader*)b;
  74076. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  74077. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  74078. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  74079. }
  74080. return records;
  74081. }
  74082. #endif
  74083. static int test_dtls_frag_ch(void)
  74084. {
  74085. EXPECT_DECLS;
  74086. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  74087. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  74088. WOLFSSL_CTX *ctx_c = NULL;
  74089. WOLFSSL_CTX *ctx_s = NULL;
  74090. WOLFSSL *ssl_c = NULL;
  74091. WOLFSSL *ssl_s = NULL;
  74092. struct test_memio_ctx test_ctx;
  74093. static unsigned int DUMMY_MTU = 256;
  74094. unsigned char four_frag_CH[] = {
  74095. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74096. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74097. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  74098. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  74099. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  74100. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  74101. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  74102. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  74103. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  74104. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  74105. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  74106. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  74107. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  74108. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  74109. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  74110. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  74111. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  74112. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  74113. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  74114. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74115. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  74116. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  74117. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  74118. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  74119. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  74120. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  74121. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  74122. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  74123. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  74124. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  74125. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  74126. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  74127. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  74128. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  74129. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  74130. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  74131. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  74132. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  74133. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  74134. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  74135. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  74136. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  74137. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  74138. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  74139. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  74140. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  74141. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  74142. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  74143. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  74144. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  74145. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  74146. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  74147. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  74148. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  74149. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  74150. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  74151. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  74152. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  74153. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  74154. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  74155. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  74156. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  74157. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  74158. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  74159. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  74160. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  74161. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  74162. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  74163. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  74164. 0xa5, 0xcb, 0x68, 0xf3
  74165. };
  74166. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  74167. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  74168. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  74169. /* Fragment msgs */
  74170. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  74171. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  74172. /* Add in some key shares to make the CH long */
  74173. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  74174. WOLFSSL_SUCCESS);
  74175. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  74176. WOLFSSL_SUCCESS);
  74177. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  74178. WOLFSSL_SUCCESS);
  74179. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  74180. WOLFSSL_SUCCESS);
  74181. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  74182. /* Reject fragmented first CH */
  74183. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  74184. sizeof(four_frag_CH)), 4);
  74185. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  74186. test_ctx.s_len = sizeof(four_frag_CH);
  74187. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  74188. int s_len = test_ctx.s_len;
  74189. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  74190. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  74191. /* Fail if we didn't advance the buffer to avoid infinite loops */
  74192. ExpectIntLT(test_ctx.s_len, s_len);
  74193. }
  74194. /* Expect all fragments read */
  74195. ExpectIntEQ(test_ctx.s_len, 0);
  74196. /* Expect quietly dropping fragmented first CH */
  74197. ExpectIntEQ(test_ctx.c_len, 0);
  74198. /* CH1 */
  74199. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  74200. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74201. /* Count records. Expect 1 unfragmented CH */
  74202. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  74203. test_ctx.s_len), 1);
  74204. /* HRR */
  74205. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  74206. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  74207. /* CH2 */
  74208. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  74209. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74210. /* Count records. Expect fragmented CH */
  74211. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  74212. test_ctx.s_len), 1);
  74213. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  74214. wolfSSL_free(ssl_c);
  74215. wolfSSL_free(ssl_s);
  74216. wolfSSL_CTX_free(ctx_c);
  74217. wolfSSL_CTX_free(ctx_s);
  74218. ssl_c = ssl_s = NULL;
  74219. ctx_c = ctx_s = NULL;
  74220. #endif
  74221. return EXPECT_RESULT();
  74222. }
  74223. static int test_dtls_empty_keyshare_with_cookie(void)
  74224. {
  74225. EXPECT_DECLS;
  74226. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  74227. WOLFSSL_CTX *ctx_s = NULL;
  74228. WOLFSSL *ssl_s = NULL;
  74229. struct test_memio_ctx test_ctx;
  74230. unsigned char ch_empty_keyshare_with_cookie[] = {
  74231. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  74232. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  74233. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  74234. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  74235. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  74236. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  74237. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  74238. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  74239. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  74240. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  74241. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  74242. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  74243. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  74244. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  74245. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  74246. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  74247. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  74248. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  74249. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  74250. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  74251. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  74252. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  74253. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  74254. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  74255. };
  74256. DtlsRecordLayerHeader* dtlsRH;
  74257. byte sequence_number[8];
  74258. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  74259. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  74260. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  74261. sizeof(ch_empty_keyshare_with_cookie));
  74262. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  74263. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  74264. NULL, wolfDTLSv1_3_server_method), 0);
  74265. /* CH1 */
  74266. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  74267. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  74268. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  74269. ExpectIntEQ(test_ctx.c_len, 15);
  74270. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  74271. ExpectIntEQ(dtlsRH->type, alert);
  74272. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  74273. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  74274. sequence_number[7] = 1;
  74275. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  74276. sizeof(sequence_number)), 0);
  74277. ExpectIntEQ(dtlsRH->length[0], 0);
  74278. ExpectIntEQ(dtlsRH->length[1], 2);
  74279. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  74280. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  74281. wolfSSL_free(ssl_s);
  74282. wolfSSL_CTX_free(ctx_s);
  74283. #endif
  74284. return EXPECT_RESULT();
  74285. }
  74286. static int test_dtls_old_seq_number(void)
  74287. {
  74288. EXPECT_DECLS;
  74289. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  74290. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  74291. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  74292. struct test_memio_ctx test_ctx;
  74293. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  74294. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  74295. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  74296. /* CH1 */
  74297. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  74298. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74299. /* HVR */
  74300. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  74301. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  74302. /* CH2 */
  74303. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  74304. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74305. /* Server first flight */
  74306. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  74307. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  74308. /* Client second flight */
  74309. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  74310. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74311. /* Modify the sequence number */
  74312. {
  74313. DtlsRecordLayerHeader* dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  74314. XMEMSET(dtlsRH->sequence_number, 0, sizeof(dtlsRH->sequence_number));
  74315. }
  74316. /* Server second flight */
  74317. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  74318. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  74319. /* Server should not do anything as a pkt was dropped */
  74320. ExpectIntEQ(test_ctx.c_len, 0);
  74321. ExpectIntEQ(test_ctx.s_len, 0);
  74322. /* Trigger rtx */
  74323. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  74324. /* Complete connection */
  74325. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  74326. wolfSSL_free(ssl_c);
  74327. wolfSSL_CTX_free(ctx_c);
  74328. wolfSSL_free(ssl_s);
  74329. wolfSSL_CTX_free(ctx_s);
  74330. #endif
  74331. return EXPECT_RESULT();
  74332. }
  74333. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  74334. defined(HAVE_LIBOQS)
  74335. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  74336. {
  74337. int group = WOLFSSL_KYBER_LEVEL5;
  74338. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  74339. }
  74340. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  74341. {
  74342. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  74343. }
  74344. #endif
  74345. static int test_tls13_pq_groups(void)
  74346. {
  74347. EXPECT_DECLS;
  74348. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  74349. defined(HAVE_LIBOQS)
  74350. callback_functions func_cb_client;
  74351. callback_functions func_cb_server;
  74352. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  74353. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  74354. func_cb_client.method = wolfTLSv1_3_client_method;
  74355. func_cb_server.method = wolfTLSv1_3_server_method;
  74356. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  74357. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  74358. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  74359. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  74360. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  74361. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  74362. #endif
  74363. return EXPECT_RESULT();
  74364. }
  74365. static int test_tls13_early_data(void)
  74366. {
  74367. EXPECT_DECLS;
  74368. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  74369. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  74370. int written = 0;
  74371. int read = 0;
  74372. size_t i;
  74373. int splitEarlyData;
  74374. char msg[] = "This is early data";
  74375. char msg2[] = "This is client data";
  74376. char msg3[] = "This is server data";
  74377. char msg4[] = "This is server immediate data";
  74378. char msgBuf[50];
  74379. struct {
  74380. method_provider client_meth;
  74381. method_provider server_meth;
  74382. const char* tls_version;
  74383. int isUdp;
  74384. } params[] = {
  74385. #ifdef WOLFSSL_TLS13
  74386. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  74387. "TLS 1.3", 0 },
  74388. #endif
  74389. #ifdef WOLFSSL_DTLS13
  74390. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  74391. "DTLS 1.3", 1 },
  74392. #endif
  74393. };
  74394. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  74395. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  74396. struct test_memio_ctx test_ctx;
  74397. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  74398. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  74399. WOLFSSL_SESSION *sess = NULL;
  74400. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  74401. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  74402. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  74403. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  74404. /* Get a ticket so that we can do 0-RTT on the next connection */
  74405. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  74406. /* Make sure we read the ticket */
  74407. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  74408. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74409. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  74410. wolfSSL_free(ssl_c);
  74411. ssl_c = NULL;
  74412. wolfSSL_free(ssl_s);
  74413. ssl_s = NULL;
  74414. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  74415. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  74416. params[i].client_meth, params[i].server_meth), 0);
  74417. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  74418. #ifdef WOLFSSL_DTLS13
  74419. if (params[i].isUdp) {
  74420. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  74421. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  74422. #else
  74423. /* Let's test this but we generally don't recommend turning off the
  74424. * cookie exchange */
  74425. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  74426. #endif
  74427. }
  74428. #endif
  74429. /* Test 0-RTT data */
  74430. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  74431. &written), sizeof(msg));
  74432. ExpectIntEQ(written, sizeof(msg));
  74433. if (splitEarlyData) {
  74434. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  74435. &written), sizeof(msg));
  74436. ExpectIntEQ(written, sizeof(msg));
  74437. }
  74438. /* Read first 0-RTT data (if split otherwise entire data) */
  74439. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  74440. &read), sizeof(msg));
  74441. ExpectIntEQ(read, sizeof(msg));
  74442. ExpectStrEQ(msg, msgBuf);
  74443. /* Test 0.5-RTT data */
  74444. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  74445. if (splitEarlyData) {
  74446. /* Read second 0-RTT data */
  74447. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  74448. &read), sizeof(msg));
  74449. ExpectIntEQ(read, sizeof(msg));
  74450. ExpectStrEQ(msg, msgBuf);
  74451. }
  74452. if (params[i].isUdp) {
  74453. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  74454. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  74455. /* Read server 0.5-RTT data */
  74456. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  74457. ExpectStrEQ(msg4, msgBuf);
  74458. /* Complete handshake */
  74459. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  74460. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74461. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  74462. * a user would loop until it is true but here we control both sides so we
  74463. * just assert the expected value. wolfSSL_read_early_data does not provide
  74464. * handshake status to us with non-blocking IO and we can't use
  74465. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  74466. * parsing logic. */
  74467. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  74468. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  74469. &read), 0);
  74470. ExpectIntEQ(read, 0);
  74471. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  74472. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  74473. }
  74474. else {
  74475. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  74476. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  74477. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  74478. &read), 0);
  74479. ExpectIntEQ(read, 0);
  74480. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  74481. /* Read server 0.5-RTT data */
  74482. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  74483. ExpectStrEQ(msg4, msgBuf);
  74484. }
  74485. /* Test bi-directional write */
  74486. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  74487. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  74488. ExpectStrEQ(msg2, msgBuf);
  74489. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  74490. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  74491. ExpectStrEQ(msg3, msgBuf);
  74492. ExpectTrue(wolfSSL_session_reused(ssl_c));
  74493. ExpectTrue(wolfSSL_session_reused(ssl_s));
  74494. wolfSSL_SESSION_free(sess);
  74495. wolfSSL_free(ssl_c);
  74496. wolfSSL_free(ssl_s);
  74497. wolfSSL_CTX_free(ctx_c);
  74498. wolfSSL_CTX_free(ctx_s);
  74499. }
  74500. }
  74501. #endif
  74502. return EXPECT_RESULT();
  74503. }
  74504. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  74505. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  74506. {
  74507. EXPECT_DECLS;
  74508. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  74509. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  74510. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  74511. return EXPECT_RESULT();
  74512. }
  74513. #endif
  74514. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  74515. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  74516. {
  74517. EXPECT_DECLS;
  74518. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  74519. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  74520. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  74521. return EXPECT_RESULT();
  74522. }
  74523. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  74524. {
  74525. EXPECT_DECLS;
  74526. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  74527. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  74528. 0), 1);
  74529. return EXPECT_RESULT();
  74530. }
  74531. #endif
  74532. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  74533. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  74534. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  74535. {
  74536. EXPECT_DECLS;
  74537. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  74538. return EXPECT_RESULT();
  74539. }
  74540. #endif
  74541. static int test_self_signed_stapling(void)
  74542. {
  74543. EXPECT_DECLS;
  74544. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  74545. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  74546. test_ssl_cbf client_cbf;
  74547. test_ssl_cbf server_cbf;
  74548. size_t i;
  74549. struct {
  74550. method_provider client_meth;
  74551. method_provider server_meth;
  74552. ctx_cb client_ctx;
  74553. const char* tls_version;
  74554. } params[] = {
  74555. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  74556. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  74557. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  74558. #endif
  74559. #ifndef WOLFSSL_NO_TLS12
  74560. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  74561. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  74562. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  74563. #endif
  74564. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  74565. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  74566. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  74567. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  74568. test_self_signed_stapling_client_v2_multi_ctx_ready,
  74569. "TLSv1_2 v2 multi" },
  74570. #endif
  74571. #endif
  74572. };
  74573. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  74574. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  74575. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  74576. printf("\nTesting self-signed cert with status request: %s\n",
  74577. params[i].tls_version);
  74578. client_cbf.method = params[i].client_meth;
  74579. client_cbf.ctx_ready = params[i].client_ctx;
  74580. server_cbf.method = params[i].server_meth;
  74581. server_cbf.certPemFile = "certs/ca-cert.pem";
  74582. server_cbf.keyPemFile = "certs/ca-key.pem";
  74583. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  74584. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  74585. &server_cbf, NULL), TEST_SUCCESS);
  74586. }
  74587. #endif
  74588. return EXPECT_RESULT();
  74589. }
  74590. static int test_tls_multi_handshakes_one_record(void)
  74591. {
  74592. EXPECT_DECLS;
  74593. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  74594. struct test_memio_ctx test_ctx;
  74595. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  74596. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  74597. RecordLayerHeader* rh = NULL;
  74598. byte *len ;
  74599. int newRecIdx = RECORD_HEADER_SZ;
  74600. int idx = 0;
  74601. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  74602. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  74603. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  74604. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  74605. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74606. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  74607. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  74608. /* Combine server handshake msgs into one record */
  74609. while (idx < test_ctx.c_len) {
  74610. word16 recLen;
  74611. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  74612. len = &rh->length[0];
  74613. ato16((const byte*)len, &recLen);
  74614. idx += RECORD_HEADER_SZ;
  74615. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  74616. (size_t)recLen);
  74617. newRecIdx += recLen;
  74618. idx += recLen;
  74619. }
  74620. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  74621. len = &rh->length[0];
  74622. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  74623. test_ctx.c_len = newRecIdx;
  74624. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  74625. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74626. wolfSSL_free(ssl_c);
  74627. wolfSSL_free(ssl_s);
  74628. wolfSSL_CTX_free(ctx_c);
  74629. wolfSSL_CTX_free(ctx_s);
  74630. #endif
  74631. return EXPECT_RESULT();
  74632. }
  74633. static int test_write_dup(void)
  74634. {
  74635. EXPECT_DECLS;
  74636. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  74637. size_t i, j;
  74638. char hiWorld[] = "dup message";
  74639. char readData[sizeof(hiWorld) + 5];
  74640. struct {
  74641. method_provider client_meth;
  74642. method_provider server_meth;
  74643. const char* version_name;
  74644. int version;
  74645. } methods[] = {
  74646. #ifndef WOLFSSL_NO_TLS12
  74647. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  74648. #endif
  74649. #ifdef WOLFSSL_TLS13
  74650. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  74651. #endif
  74652. };
  74653. struct {
  74654. const char* cipher;
  74655. int version;
  74656. } ciphers[] = {
  74657. /* For simplicity the macros are copied from internal.h */
  74658. /* TLS 1.2 */
  74659. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  74660. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  74661. #ifndef NO_RSA
  74662. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  74663. #endif
  74664. #endif
  74665. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  74666. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  74667. #endif
  74668. #endif
  74669. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  74670. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  74671. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  74672. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  74673. #endif
  74674. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  74675. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  74676. #endif
  74677. #endif
  74678. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  74679. && !defined(NO_TLS) && !defined(NO_AES)
  74680. #ifdef HAVE_AESGCM
  74681. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  74682. #ifndef NO_RSA
  74683. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  74684. #endif
  74685. #endif
  74686. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  74687. #ifndef NO_RSA
  74688. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  74689. #endif
  74690. #endif
  74691. #endif
  74692. #endif
  74693. /* TLS 1.3 */
  74694. #ifdef WOLFSSL_TLS13
  74695. #ifdef HAVE_AESGCM
  74696. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  74697. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  74698. #endif
  74699. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  74700. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  74701. #endif
  74702. #endif
  74703. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  74704. #ifndef NO_SHA256
  74705. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  74706. #endif
  74707. #endif
  74708. #ifdef HAVE_AESCCM
  74709. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  74710. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  74711. #endif
  74712. #endif
  74713. #endif
  74714. };
  74715. for (i = 0; i < XELEM_CNT(methods); i++) {
  74716. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  74717. struct test_memio_ctx test_ctx;
  74718. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  74719. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  74720. WOLFSSL *ssl_c2 = NULL;
  74721. if (methods[i].version != ciphers[j].version)
  74722. continue;
  74723. if (i == 0 && j == 0)
  74724. printf("\n");
  74725. printf("Testing %s with %s... ", methods[i].version_name,
  74726. ciphers[j].cipher);
  74727. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  74728. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  74729. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  74730. methods[i].client_meth, methods[i].server_meth), 0);
  74731. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  74732. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  74733. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  74734. WRITE_DUP_WRITE_E);
  74735. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  74736. sizeof(hiWorld));
  74737. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  74738. sizeof(hiWorld));
  74739. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  74740. sizeof(hiWorld));
  74741. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  74742. WRITE_DUP_READ_E);
  74743. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  74744. sizeof(hiWorld));
  74745. if (EXPECT_SUCCESS())
  74746. printf("ok\n");
  74747. else
  74748. printf("failed\n");
  74749. wolfSSL_free(ssl_c);
  74750. wolfSSL_free(ssl_c2);
  74751. wolfSSL_free(ssl_s);
  74752. wolfSSL_CTX_free(ctx_c);
  74753. wolfSSL_CTX_free(ctx_s);
  74754. }
  74755. }
  74756. #endif
  74757. return EXPECT_RESULT();
  74758. }
  74759. static int test_read_write_hs(void)
  74760. {
  74761. EXPECT_DECLS;
  74762. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  74763. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  74764. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  74765. struct test_memio_ctx test_ctx;
  74766. byte test_buffer[16];
  74767. unsigned int test;
  74768. /* test == 0 : client writes, server reads */
  74769. /* test == 1 : server writes, client reads */
  74770. for (test = 0; test < 2; test++) {
  74771. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  74772. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  74773. wolfTLSv1_2_client_method,
  74774. wolfTLSv1_2_server_method), 0);
  74775. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  74776. /* CH -> */
  74777. if (test == 0) {
  74778. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  74779. } else {
  74780. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  74781. sizeof(test_buffer)), -1);
  74782. }
  74783. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74784. /* <- SH + SKE + SHD */
  74785. if (test == 0) {
  74786. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  74787. sizeof(test_buffer)), -1);
  74788. } else {
  74789. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  74790. }
  74791. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  74792. /* -> CKE + CLIENT FINISHED */
  74793. if (test == 0) {
  74794. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  74795. } else {
  74796. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  74797. sizeof(test_buffer)), -1);
  74798. }
  74799. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74800. /* abide clang static analyzer */
  74801. if (ssl_s != NULL) {
  74802. /* disable group message to separate sending of ChangeCipherspec
  74803. * from Finished */
  74804. ssl_s->options.groupMessages = 0;
  74805. }
  74806. /* allow writing of CS, but not FINISHED */
  74807. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  74808. /* <- CS */
  74809. if (test == 0) {
  74810. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  74811. sizeof(test_buffer)), -1);
  74812. } else {
  74813. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  74814. }
  74815. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  74816. /* move CS message where the client can read it */
  74817. memmove(test_ctx.c_buff,
  74818. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  74819. test_ctx.c_len = 6;
  74820. /* read CS */
  74821. if (test == 0) {
  74822. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  74823. } else {
  74824. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  74825. sizeof(test_buffer)), -1);
  74826. }
  74827. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  74828. ExpectIntEQ(test_ctx.c_len, 0);
  74829. if (test == 0) {
  74830. /* send SERVER FINISHED */
  74831. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  74832. sizeof(test_buffer)), -1);
  74833. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  74834. WOLFSSL_ERROR_WANT_READ);
  74835. } else {
  74836. /* send SERVER FINISHED + App Data */
  74837. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  74838. }
  74839. ExpectIntGT(test_ctx.c_len, 0);
  74840. /* Send and receive the data */
  74841. if (test == 0) {
  74842. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  74843. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  74844. sizeof(test_buffer)), 5);
  74845. } else {
  74846. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  74847. sizeof(test_buffer)), 5);
  74848. }
  74849. ExpectBufEQ(test_buffer, "hello", 5);
  74850. wolfSSL_free(ssl_c);
  74851. wolfSSL_free(ssl_s);
  74852. wolfSSL_CTX_free(ctx_c);
  74853. wolfSSL_CTX_free(ctx_s);
  74854. ssl_c = ssl_s = NULL;
  74855. ctx_c = ctx_s = NULL;
  74856. }
  74857. #endif
  74858. return EXPECT_RESULT();
  74859. }
  74860. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  74861. static const char* test_get_signature_nid_siglag;
  74862. static int test_get_signature_nid_sig;
  74863. static int test_get_signature_nid_hash;
  74864. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  74865. {
  74866. EXPECT_DECLS;
  74867. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  74868. if (!wolfSSL_is_server(ssl)) {
  74869. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  74870. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  74871. }
  74872. return EXPECT_RESULT();
  74873. }
  74874. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  74875. {
  74876. EXPECT_DECLS;
  74877. int nid = 0;
  74878. (void)ctx;
  74879. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  74880. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  74881. ExpectIntEQ(nid, test_get_signature_nid_sig);
  74882. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  74883. ExpectIntEQ(nid, test_get_signature_nid_hash);
  74884. }
  74885. else /* No sigalg info on static ciphersuite */
  74886. return TEST_SUCCESS;
  74887. return EXPECT_RESULT();
  74888. }
  74889. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  74890. {
  74891. EXPECT_DECLS;
  74892. int nid = 0;
  74893. (void)ctx;
  74894. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  74895. ExpectIntEQ(nid, test_get_signature_nid_sig);
  74896. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  74897. ExpectIntEQ(nid, test_get_signature_nid_hash);
  74898. return EXPECT_RESULT();
  74899. }
  74900. #endif
  74901. static int test_get_signature_nid(void)
  74902. {
  74903. EXPECT_DECLS;
  74904. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  74905. test_ssl_cbf client_cbf;
  74906. test_ssl_cbf server_cbf;
  74907. size_t i;
  74908. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  74909. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  74910. caCertFile }
  74911. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  74912. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  74913. caEccCertFile }
  74914. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  74915. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  74916. caCertFile }
  74917. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  74918. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  74919. caEccCertFile }
  74920. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  74921. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  74922. caEdCertFile }
  74923. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  74924. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  74925. caEd448CertFile }
  74926. struct {
  74927. const char* siglag;
  74928. int sig_nid;
  74929. int hash_nid;
  74930. int tls_ver;
  74931. const char* server_cert;
  74932. const char* server_key;
  74933. const char* client_ca;
  74934. } params[] = {
  74935. #ifndef NO_RSA
  74936. #ifndef NO_SHA256
  74937. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  74938. #ifdef WC_RSA_PSS
  74939. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  74940. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  74941. #endif
  74942. #endif
  74943. #ifdef WOLFSSL_SHA512
  74944. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  74945. #ifdef WC_RSA_PSS
  74946. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  74947. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  74948. #endif
  74949. #endif
  74950. #endif
  74951. #ifdef HAVE_ECC
  74952. #ifndef NO_SHA256
  74953. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  74954. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  74955. #endif
  74956. #endif
  74957. #ifdef HAVE_ED25519
  74958. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  74959. #endif
  74960. #ifdef HAVE_ED448
  74961. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  74962. #endif
  74963. };
  74964. printf("\n");
  74965. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  74966. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  74967. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  74968. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  74969. params[i].siglag);
  74970. switch (params[i].tls_ver) {
  74971. #ifndef WOLFSSL_NO_TLS12
  74972. case WOLFSSL_TLSV1_2:
  74973. client_cbf.method = wolfTLSv1_2_client_method;
  74974. server_cbf.method = wolfTLSv1_2_server_method;
  74975. break;
  74976. #endif
  74977. #ifdef WOLFSSL_TLS13
  74978. case WOLFSSL_TLSV1_3:
  74979. client_cbf.method = wolfTLSv1_3_client_method;
  74980. server_cbf.method = wolfTLSv1_3_server_method;
  74981. break;
  74982. #endif
  74983. default:
  74984. printf("skipping\n");
  74985. continue;
  74986. }
  74987. test_get_signature_nid_siglag = params[i].siglag;
  74988. test_get_signature_nid_sig = params[i].sig_nid;
  74989. test_get_signature_nid_hash = params[i].hash_nid;
  74990. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  74991. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  74992. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  74993. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  74994. server_cbf.certPemFile = params[i].server_cert;
  74995. server_cbf.keyPemFile = params[i].server_key;
  74996. client_cbf.caPemFile = params[i].client_ca;
  74997. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  74998. &server_cbf, NULL), TEST_SUCCESS);
  74999. if (EXPECT_SUCCESS())
  75000. printf("passed\n");
  75001. }
  75002. #endif
  75003. return EXPECT_RESULT();
  75004. }
  75005. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  75006. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  75007. {
  75008. #ifndef NO_MD5
  75009. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  75010. #elif !defined(NO_SHA)
  75011. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  75012. #elif !defined(NO_SHA256)
  75013. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  75014. #else
  75015. #error "We need a digest to hash the Signer object"
  75016. #endif
  75017. byte hashBuf[WC_MAX_DIGEST_SIZE];
  75018. wc_HashAlg hash;
  75019. size_t i;
  75020. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  75021. for (i = 0; i < CA_TABLE_SIZE; i++) {
  75022. Signer* cur;
  75023. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  75024. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  75025. sizeof(*cur)), 0);
  75026. }
  75027. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  75028. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  75029. return MakeWordFromHash(hashBuf);
  75030. }
  75031. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  75032. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  75033. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  75034. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  75035. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  75036. {
  75037. EXPECT_DECLS;
  75038. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  75039. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  75040. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  75041. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  75042. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  75043. return EXPECT_RESULT();
  75044. }
  75045. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  75046. {
  75047. EXPECT_DECLS;
  75048. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  75049. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  75050. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  75051. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  75052. return EXPECT_RESULT();
  75053. }
  75054. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  75055. {
  75056. EXPECT_DECLS;
  75057. WOLFSSL_CERT_MANAGER* cm;
  75058. (void)ssl;
  75059. /* WARNING: this approach bypasses the reference counter check in
  75060. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  75061. * cause unexpected behaviour when other active connections try accessing
  75062. * the caTable. */
  75063. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  75064. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  75065. WOLFSSL_SUCCESS);
  75066. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  75067. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  75068. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  75069. return EXPECT_RESULT();
  75070. }
  75071. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  75072. {
  75073. EXPECT_DECLS;
  75074. WOLFSSL_CTX* ctx;
  75075. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  75076. return EXPECT_RESULT();
  75077. }
  75078. #endif
  75079. static int test_tls_cert_store_unchanged(void)
  75080. {
  75081. EXPECT_DECLS;
  75082. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  75083. test_ssl_cbf client_cbf;
  75084. test_ssl_cbf server_cbf;
  75085. int i;
  75086. for (i = 0; i < 2; i++) {
  75087. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  75088. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  75089. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  75090. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  75091. sizeof(test_tls_cert_store_unchanged_before_hashes));
  75092. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  75093. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  75094. sizeof(test_tls_cert_store_unchanged_after_hashes));
  75095. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  75096. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  75097. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  75098. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  75099. switch (i) {
  75100. case 0:
  75101. client_cbf.on_ctx_cleanup =
  75102. test_tls_cert_store_unchanged_ctx_cleanup;
  75103. server_cbf.on_ctx_cleanup =
  75104. test_tls_cert_store_unchanged_ctx_cleanup;
  75105. break;
  75106. case 1:
  75107. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  75108. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  75109. break;
  75110. default:
  75111. Fail(("Should not enter here"), ("Entered here"));
  75112. }
  75113. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  75114. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  75115. server_cbf.caPemFile = caCertFile;
  75116. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  75117. &server_cbf, NULL), TEST_SUCCESS);
  75118. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  75119. test_tls_cert_store_unchanged_after_hashes,
  75120. sizeof(test_tls_cert_store_unchanged_after_hashes));
  75121. }
  75122. #endif
  75123. return EXPECT_RESULT();
  75124. }
  75125. static int test_wolfSSL_SendUserCanceled(void)
  75126. {
  75127. EXPECT_DECLS;
  75128. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  75129. size_t i;
  75130. struct {
  75131. method_provider client_meth;
  75132. method_provider server_meth;
  75133. const char* tls_version;
  75134. } params[] = {
  75135. #if defined(WOLFSSL_TLS13)
  75136. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  75137. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  75138. #ifdef WOLFSSL_DTLS13
  75139. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  75140. #endif
  75141. #endif
  75142. #ifndef WOLFSSL_NO_TLS12
  75143. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  75144. #ifdef WOLFSSL_DTLS
  75145. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  75146. #endif
  75147. #endif
  75148. #if !defined(NO_OLD_TLS)
  75149. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  75150. #ifdef WOLFSSL_DTLS
  75151. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  75152. #endif
  75153. #endif
  75154. };
  75155. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  75156. WOLFSSL_CTX *ctx_c = NULL;
  75157. WOLFSSL_CTX *ctx_s = NULL;
  75158. WOLFSSL *ssl_c = NULL;
  75159. WOLFSSL *ssl_s = NULL;
  75160. struct test_memio_ctx test_ctx;
  75161. WOLFSSL_ALERT_HISTORY h;
  75162. printf("Testing %s\n", params[i].tls_version);
  75163. XMEMSET(&h, 0, sizeof(h));
  75164. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  75165. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  75166. params[i].client_meth, params[i].server_meth), 0);
  75167. /* CH1 */
  75168. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  75169. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  75170. ExpectIntEQ(wolfSSL_SendUserCanceled(ssl_s), WOLFSSL_SHUTDOWN_NOT_DONE);
  75171. /* Alert closed connection */
  75172. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  75173. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_ZERO_RETURN);
  75174. /* Last alert will be close notify because user_canceled should be
  75175. * followed by a close_notify */
  75176. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  75177. ExpectIntEQ(h.last_rx.code, close_notify);
  75178. ExpectIntEQ(h.last_rx.level, alert_warning);
  75179. wolfSSL_free(ssl_c);
  75180. wolfSSL_free(ssl_s);
  75181. wolfSSL_CTX_free(ctx_c);
  75182. wolfSSL_CTX_free(ctx_s);
  75183. }
  75184. #endif
  75185. return EXPECT_RESULT();
  75186. }
  75187. /*----------------------------------------------------------------------------*
  75188. | Main
  75189. *----------------------------------------------------------------------------*/
  75190. typedef int (*TEST_FUNC)(void);
  75191. typedef struct {
  75192. const char *name;
  75193. TEST_FUNC func;
  75194. byte run:1;
  75195. byte fail:1;
  75196. } TEST_CASE;
  75197. #define TEST_DECL(func) { #func, func, 0, 0 }
  75198. int testAll = 1;
  75199. TEST_CASE testCases[] = {
  75200. TEST_DECL(test_fileAccess),
  75201. /*********************************
  75202. * wolfcrypt
  75203. *********************************/
  75204. TEST_DECL(test_ForceZero),
  75205. TEST_DECL(test_wolfCrypt_Init),
  75206. TEST_DECL(test_wc_LoadStaticMemory_ex),
  75207. /* Locking with Compat Mutex */
  75208. TEST_DECL(test_wc_SetMutexCb),
  75209. TEST_DECL(test_wc_LockMutex_ex),
  75210. /* Digests */
  75211. TEST_DECL(test_wc_InitMd5),
  75212. TEST_DECL(test_wc_Md5Update),
  75213. TEST_DECL(test_wc_Md5Final),
  75214. TEST_DECL(test_wc_InitSha),
  75215. TEST_DECL(test_wc_ShaUpdate),
  75216. TEST_DECL(test_wc_ShaFinal),
  75217. TEST_DECL(test_wc_InitSha256),
  75218. TEST_DECL(test_wc_Sha256Update),
  75219. TEST_DECL(test_wc_Sha256Final),
  75220. TEST_DECL(test_wc_Sha256FinalRaw),
  75221. TEST_DECL(test_wc_Sha256GetFlags),
  75222. TEST_DECL(test_wc_Sha256Free),
  75223. TEST_DECL(test_wc_Sha256GetHash),
  75224. TEST_DECL(test_wc_Sha256Copy),
  75225. TEST_DECL(test_wc_InitSha224),
  75226. TEST_DECL(test_wc_Sha224Update),
  75227. TEST_DECL(test_wc_Sha224Final),
  75228. TEST_DECL(test_wc_Sha224SetFlags),
  75229. TEST_DECL(test_wc_Sha224GetFlags),
  75230. TEST_DECL(test_wc_Sha224Free),
  75231. TEST_DECL(test_wc_Sha224GetHash),
  75232. TEST_DECL(test_wc_Sha224Copy),
  75233. TEST_DECL(test_wc_InitSha512),
  75234. TEST_DECL(test_wc_Sha512Update),
  75235. TEST_DECL(test_wc_Sha512Final),
  75236. TEST_DECL(test_wc_Sha512GetFlags),
  75237. TEST_DECL(test_wc_Sha512FinalRaw),
  75238. TEST_DECL(test_wc_Sha512Free),
  75239. TEST_DECL(test_wc_Sha512GetHash),
  75240. TEST_DECL(test_wc_Sha512Copy),
  75241. TEST_DECL(test_wc_InitSha512_224),
  75242. TEST_DECL(test_wc_Sha512_224Update),
  75243. TEST_DECL(test_wc_Sha512_224Final),
  75244. TEST_DECL(test_wc_Sha512_224GetFlags),
  75245. TEST_DECL(test_wc_Sha512_224FinalRaw),
  75246. TEST_DECL(test_wc_Sha512_224Free),
  75247. TEST_DECL(test_wc_Sha512_224GetHash),
  75248. TEST_DECL(test_wc_Sha512_224Copy),
  75249. TEST_DECL(test_wc_InitSha512_256),
  75250. TEST_DECL(test_wc_Sha512_256Update),
  75251. TEST_DECL(test_wc_Sha512_256Final),
  75252. TEST_DECL(test_wc_Sha512_256GetFlags),
  75253. TEST_DECL(test_wc_Sha512_256FinalRaw),
  75254. TEST_DECL(test_wc_Sha512_256Free),
  75255. TEST_DECL(test_wc_Sha512_256GetHash),
  75256. TEST_DECL(test_wc_Sha512_256Copy),
  75257. TEST_DECL(test_wc_InitSha384),
  75258. TEST_DECL(test_wc_Sha384Update),
  75259. TEST_DECL(test_wc_Sha384Final),
  75260. TEST_DECL(test_wc_Sha384GetFlags),
  75261. TEST_DECL(test_wc_Sha384FinalRaw),
  75262. TEST_DECL(test_wc_Sha384Free),
  75263. TEST_DECL(test_wc_Sha384GetHash),
  75264. TEST_DECL(test_wc_Sha384Copy),
  75265. TEST_DECL(test_wc_InitBlake2b),
  75266. TEST_DECL(test_wc_InitBlake2b_WithKey),
  75267. TEST_DECL(test_wc_InitBlake2s_WithKey),
  75268. TEST_DECL(test_wc_InitRipeMd),
  75269. TEST_DECL(test_wc_RipeMdUpdate),
  75270. TEST_DECL(test_wc_RipeMdFinal),
  75271. TEST_DECL(test_wc_InitSha3),
  75272. TEST_DECL(testing_wc_Sha3_Update),
  75273. TEST_DECL(test_wc_Sha3_224_Final),
  75274. TEST_DECL(test_wc_Sha3_256_Final),
  75275. TEST_DECL(test_wc_Sha3_384_Final),
  75276. TEST_DECL(test_wc_Sha3_512_Final),
  75277. TEST_DECL(test_wc_Sha3_224_Copy),
  75278. TEST_DECL(test_wc_Sha3_256_Copy),
  75279. TEST_DECL(test_wc_Sha3_384_Copy),
  75280. TEST_DECL(test_wc_Sha3_512_Copy),
  75281. TEST_DECL(test_wc_Sha3_GetFlags),
  75282. TEST_DECL(test_wc_InitShake256),
  75283. TEST_DECL(testing_wc_Shake256_Update),
  75284. TEST_DECL(test_wc_Shake256_Final),
  75285. TEST_DECL(test_wc_Shake256_Copy),
  75286. TEST_DECL(test_wc_Shake256Hash),
  75287. /* SM3 Digest */
  75288. TEST_DECL(test_wc_InitSm3Free),
  75289. TEST_DECL(test_wc_Sm3UpdateFinal),
  75290. TEST_DECL(test_wc_Sm3GetHash),
  75291. TEST_DECL(test_wc_Sm3Copy),
  75292. TEST_DECL(test_wc_Sm3FinalRaw),
  75293. TEST_DECL(test_wc_Sm3GetSetFlags),
  75294. TEST_DECL(test_wc_Sm3Hash),
  75295. TEST_DECL(test_wc_HashInit),
  75296. TEST_DECL(test_wc_HashSetFlags),
  75297. TEST_DECL(test_wc_HashGetFlags),
  75298. /* HMAC */
  75299. TEST_DECL(test_wc_Md5HmacSetKey),
  75300. TEST_DECL(test_wc_Md5HmacUpdate),
  75301. TEST_DECL(test_wc_Md5HmacFinal),
  75302. TEST_DECL(test_wc_ShaHmacSetKey),
  75303. TEST_DECL(test_wc_ShaHmacUpdate),
  75304. TEST_DECL(test_wc_ShaHmacFinal),
  75305. TEST_DECL(test_wc_Sha224HmacSetKey),
  75306. TEST_DECL(test_wc_Sha224HmacUpdate),
  75307. TEST_DECL(test_wc_Sha224HmacFinal),
  75308. TEST_DECL(test_wc_Sha256HmacSetKey),
  75309. TEST_DECL(test_wc_Sha256HmacUpdate),
  75310. TEST_DECL(test_wc_Sha256HmacFinal),
  75311. TEST_DECL(test_wc_Sha384HmacSetKey),
  75312. TEST_DECL(test_wc_Sha384HmacUpdate),
  75313. TEST_DECL(test_wc_Sha384HmacFinal),
  75314. /* CMAC */
  75315. TEST_DECL(test_wc_InitCmac),
  75316. TEST_DECL(test_wc_CmacUpdate),
  75317. TEST_DECL(test_wc_CmacFinal),
  75318. TEST_DECL(test_wc_AesCmacGenerate),
  75319. /* Cipher */
  75320. TEST_DECL(test_wc_AesGcmStream),
  75321. TEST_DECL(test_wc_Des3_SetIV),
  75322. TEST_DECL(test_wc_Des3_SetKey),
  75323. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  75324. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  75325. TEST_DECL(test_wc_Des3_EcbEncrypt),
  75326. TEST_DECL(test_wc_Chacha_SetKey),
  75327. TEST_DECL(test_wc_Chacha_Process),
  75328. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  75329. TEST_DECL(test_wc_Poly1305SetKey),
  75330. TEST_DECL(test_wc_CamelliaSetKey),
  75331. TEST_DECL(test_wc_CamelliaSetIV),
  75332. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  75333. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  75334. TEST_DECL(test_wc_Arc4SetKey),
  75335. TEST_DECL(test_wc_Arc4Process),
  75336. TEST_DECL(test_wc_Rc2SetKey),
  75337. TEST_DECL(test_wc_Rc2SetIV),
  75338. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  75339. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  75340. /* AES cipher and GMAC. */
  75341. TEST_DECL(test_wc_AesSetKey),
  75342. TEST_DECL(test_wc_AesSetIV),
  75343. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  75344. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  75345. TEST_DECL(test_wc_AesGcmSetKey),
  75346. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  75347. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  75348. TEST_DECL(test_wc_GmacSetKey),
  75349. TEST_DECL(test_wc_GmacUpdate),
  75350. TEST_DECL(test_wc_AesCcmSetKey),
  75351. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  75352. #if defined(WOLFSSL_AES_EAX) && \
  75353. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  75354. TEST_DECL(test_wc_AesEaxVectors),
  75355. TEST_DECL(test_wc_AesEaxEncryptAuth),
  75356. TEST_DECL(test_wc_AesEaxDecryptAuth),
  75357. #endif /* WOLFSSL_AES_EAX */
  75358. /* SM4 cipher */
  75359. TEST_DECL(test_wc_Sm4),
  75360. TEST_DECL(test_wc_Sm4Ecb),
  75361. TEST_DECL(test_wc_Sm4Cbc),
  75362. TEST_DECL(test_wc_Sm4Ctr),
  75363. TEST_DECL(test_wc_Sm4Gcm),
  75364. TEST_DECL(test_wc_Sm4Ccm),
  75365. /* RNG tests */
  75366. #ifdef HAVE_HASHDRBG
  75367. #ifdef TEST_RESEED_INTERVAL
  75368. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  75369. #endif
  75370. TEST_DECL(test_wc_RNG_GenerateBlock),
  75371. #endif
  75372. TEST_DECL(test_get_rand_digit),
  75373. TEST_DECL(test_wc_InitRngNonce),
  75374. TEST_DECL(test_wc_InitRngNonce_ex),
  75375. /* MP API tests */
  75376. TEST_DECL(test_get_digit_count),
  75377. TEST_DECL(test_mp_cond_copy),
  75378. TEST_DECL(test_mp_rand),
  75379. TEST_DECL(test_get_digit),
  75380. TEST_DECL(test_wc_export_int),
  75381. /* RSA */
  75382. TEST_DECL(test_wc_InitRsaKey),
  75383. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  75384. TEST_DECL(test_wc_RsaPublicKeyDecode),
  75385. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  75386. TEST_DECL(test_wc_RsaPrivateKeyDecodeRaw),
  75387. TEST_DECL(test_wc_MakeRsaKey),
  75388. TEST_DECL(test_wc_CheckProbablePrime),
  75389. TEST_DECL(test_wc_RsaPSS_Verify),
  75390. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  75391. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  75392. TEST_DECL(test_wc_RsaKeyToDer),
  75393. TEST_DECL(test_wc_RsaKeyToPublicDer),
  75394. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  75395. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  75396. TEST_DECL(test_wc_RsaEncryptSize),
  75397. TEST_DECL(test_wc_RsaSSL_SignVerify),
  75398. TEST_DECL(test_wc_RsaFlattenPublicKey),
  75399. TEST_DECL(test_RsaDecryptBoundsCheck),
  75400. /* DSA */
  75401. TEST_DECL(test_wc_InitDsaKey),
  75402. TEST_DECL(test_wc_DsaSignVerify),
  75403. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  75404. TEST_DECL(test_wc_MakeDsaKey),
  75405. TEST_DECL(test_wc_DsaKeyToDer),
  75406. TEST_DECL(test_wc_DsaKeyToPublicDer),
  75407. TEST_DECL(test_wc_DsaImportParamsRaw),
  75408. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  75409. TEST_DECL(test_wc_DsaExportParamsRaw),
  75410. TEST_DECL(test_wc_DsaExportKeyRaw),
  75411. /* DH */
  75412. TEST_DECL(test_wc_DhPublicKeyDecode),
  75413. /* wolfCrypt ECC tests */
  75414. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  75415. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  75416. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  75417. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  75418. !defined(HAVE_SELFTEST) && \
  75419. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  75420. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  75421. #endif
  75422. TEST_DECL(test_wc_ecc_make_key),
  75423. TEST_DECL(test_wc_ecc_init),
  75424. TEST_DECL(test_wc_ecc_check_key),
  75425. TEST_DECL(test_wc_ecc_get_generator),
  75426. TEST_DECL(test_wc_ecc_size),
  75427. TEST_DECL(test_wc_ecc_params),
  75428. TEST_DECL(test_wc_ecc_signVerify_hash),
  75429. TEST_DECL(test_wc_ecc_shared_secret),
  75430. TEST_DECL(test_wc_ecc_export_x963),
  75431. TEST_DECL(test_wc_ecc_export_x963_ex),
  75432. TEST_DECL(test_wc_ecc_import_x963),
  75433. TEST_DECL(test_wc_ecc_import_private_key),
  75434. TEST_DECL(test_wc_ecc_export_private_only),
  75435. TEST_DECL(test_wc_ecc_rs_to_sig),
  75436. TEST_DECL(test_wc_ecc_import_raw),
  75437. TEST_DECL(test_wc_ecc_import_unsigned),
  75438. TEST_DECL(test_wc_ecc_sig_size),
  75439. TEST_DECL(test_wc_ecc_ctx_new),
  75440. TEST_DECL(test_wc_ecc_ctx_reset),
  75441. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  75442. TEST_DECL(test_wc_ecc_ctx_set_info),
  75443. TEST_DECL(test_wc_ecc_encryptDecrypt),
  75444. TEST_DECL(test_wc_ecc_del_point),
  75445. TEST_DECL(test_wc_ecc_pointFns),
  75446. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  75447. TEST_DECL(test_wc_ecc_verify_hash_ex),
  75448. TEST_DECL(test_wc_ecc_mulmod),
  75449. TEST_DECL(test_wc_ecc_is_valid_idx),
  75450. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  75451. TEST_DECL(test_wc_ecc_sig_size_calc),
  75452. TEST_DECL(test_wc_EccPrivateKeyToDer),
  75453. /* SM2 elliptic curve */
  75454. TEST_DECL(test_wc_ecc_sm2_make_key),
  75455. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  75456. TEST_DECL(test_wc_ecc_sm2_create_digest),
  75457. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  75458. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  75459. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  75460. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  75461. /* Curve25519 */
  75462. TEST_DECL(test_wc_curve25519_init),
  75463. TEST_DECL(test_wc_curve25519_size),
  75464. TEST_DECL(test_wc_curve25519_export_key_raw),
  75465. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  75466. TEST_DECL(test_wc_curve25519_make_key),
  75467. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  75468. TEST_DECL(test_wc_curve25519_make_pub),
  75469. TEST_DECL(test_wc_curve25519_export_public_ex),
  75470. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  75471. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  75472. TEST_DECL(test_wc_curve25519_import_private),
  75473. /* ED25519 */
  75474. TEST_DECL(test_wc_ed25519_make_key),
  75475. TEST_DECL(test_wc_ed25519_init),
  75476. TEST_DECL(test_wc_ed25519_sign_msg),
  75477. TEST_DECL(test_wc_ed25519_import_public),
  75478. TEST_DECL(test_wc_ed25519_import_private_key),
  75479. TEST_DECL(test_wc_ed25519_export),
  75480. TEST_DECL(test_wc_ed25519_size),
  75481. TEST_DECL(test_wc_ed25519_exportKey),
  75482. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  75483. TEST_DECL(test_wc_Ed25519KeyToDer),
  75484. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  75485. /* Curve448 */
  75486. TEST_DECL(test_wc_curve448_make_key),
  75487. TEST_DECL(test_wc_curve448_shared_secret_ex),
  75488. TEST_DECL(test_wc_curve448_export_public_ex),
  75489. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  75490. TEST_DECL(test_wc_curve448_export_key_raw),
  75491. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  75492. TEST_DECL(test_wc_curve448_import_private),
  75493. TEST_DECL(test_wc_curve448_init),
  75494. TEST_DECL(test_wc_curve448_size),
  75495. /* Ed448 */
  75496. TEST_DECL(test_wc_ed448_make_key),
  75497. TEST_DECL(test_wc_ed448_init),
  75498. TEST_DECL(test_wc_ed448_sign_msg),
  75499. TEST_DECL(test_wc_ed448_import_public),
  75500. TEST_DECL(test_wc_ed448_import_private_key),
  75501. TEST_DECL(test_wc_ed448_export),
  75502. TEST_DECL(test_wc_ed448_size),
  75503. TEST_DECL(test_wc_ed448_exportKey),
  75504. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  75505. TEST_DECL(test_wc_Ed448KeyToDer),
  75506. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  75507. TEST_DECL(test_wc_Curve448PrivateKeyToDer),
  75508. /* Kyber */
  75509. TEST_DECL(test_wc_kyber_make_key_kats),
  75510. TEST_DECL(test_wc_kyber_encapsulate_kats),
  75511. TEST_DECL(test_wc_kyber_decapsulate_kats),
  75512. /* Dilithium */
  75513. TEST_DECL(test_wc_dilithium),
  75514. TEST_DECL(test_wc_dilithium_make_key),
  75515. TEST_DECL(test_wc_dilithium_sign),
  75516. TEST_DECL(test_wc_dilithium_verify),
  75517. TEST_DECL(test_wc_dilithium_check_key),
  75518. TEST_DECL(test_wc_dilithium_public_der_decode),
  75519. TEST_DECL(test_wc_dilithium_der),
  75520. TEST_DECL(test_wc_dilithium_make_key_from_seed),
  75521. TEST_DECL(test_wc_dilithium_verify_kats),
  75522. /* Signature API */
  75523. TEST_DECL(test_wc_SignatureGetSize_ecc),
  75524. TEST_DECL(test_wc_SignatureGetSize_rsa),
  75525. /* PEM and DER APIs. */
  75526. TEST_DECL(test_wc_PemToDer),
  75527. TEST_DECL(test_wc_AllocDer),
  75528. TEST_DECL(test_wc_CertPemToDer),
  75529. TEST_DECL(test_wc_KeyPemToDer),
  75530. TEST_DECL(test_wc_PubKeyPemToDer),
  75531. TEST_DECL(test_wc_PemPubKeyToDer),
  75532. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  75533. TEST_DECL(test_wc_CheckCertSigPubKey),
  75534. /* wolfCrypt ASN tests */
  75535. TEST_DECL(test_ToTraditional),
  75536. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  75537. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  75538. /* Certificate */
  75539. TEST_DECL(test_wc_SetSubjectRaw),
  75540. TEST_DECL(test_wc_GetSubjectRaw),
  75541. TEST_DECL(test_wc_SetIssuerRaw),
  75542. TEST_DECL(test_wc_SetIssueBuffer),
  75543. TEST_DECL(test_wc_SetSubjectKeyId),
  75544. TEST_DECL(test_wc_SetSubject),
  75545. TEST_DECL(test_CheckCertSignature),
  75546. TEST_DECL(test_wc_ParseCert),
  75547. TEST_DECL(test_wc_ParseCert_Error),
  75548. TEST_DECL(test_MakeCertWithPathLen),
  75549. TEST_DECL(test_MakeCertWith0Ser),
  75550. TEST_DECL(test_MakeCertWithCaFalse),
  75551. TEST_DECL(test_wc_SetKeyUsage),
  75552. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  75553. TEST_DECL(test_wc_SetSubjectBuffer),
  75554. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  75555. /* wolfcrypt PKCS#7 */
  75556. TEST_DECL(test_wc_PKCS7_New),
  75557. TEST_DECL(test_wc_PKCS7_Init),
  75558. TEST_DECL(test_wc_PKCS7_InitWithCert),
  75559. TEST_DECL(test_wc_PKCS7_EncodeData),
  75560. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  75561. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  75562. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  75563. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  75564. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  75565. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  75566. TEST_DECL(test_wc_PKCS7_Degenerate),
  75567. TEST_DECL(test_wc_PKCS7_BER),
  75568. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  75569. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  75570. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  75571. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  75572. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  75573. /* wolfCrypt PKCS#12 */
  75574. TEST_DECL(test_wc_i2d_PKCS12),
  75575. /*
  75576. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  75577. * avoid memory leaks.
  75578. */
  75579. TEST_DECL(test_wolfCrypt_Cleanup),
  75580. TEST_DECL(test_wolfSSL_Init),
  75581. TEST_DECL(test_dual_alg_support),
  75582. /*********************************
  75583. * OpenSSL compatibility API tests
  75584. *********************************/
  75585. /* If at some point a stub get implemented this test should fail indicating
  75586. * a need to implement a new test case
  75587. */
  75588. TEST_DECL(test_stubs_are_stubs),
  75589. /* ASN.1 compatibility API tests */
  75590. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  75591. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  75592. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  75593. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  75594. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  75595. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  75596. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  75597. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  75598. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  75599. TEST_DECL(test_wolfSSL_ASN1_get_object),
  75600. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  75601. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  75602. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  75603. TEST_DECL(test_wolfSSL_ASN1_STRING),
  75604. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  75605. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  75606. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  75607. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  75608. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  75609. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  75610. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  75611. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  75612. TEST_DECL(test_wolfSSL_ASN1_TIME),
  75613. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  75614. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  75615. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  75616. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  75617. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  75618. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  75619. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  75620. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  75621. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  75622. TEST_DECL(test_wolfSSL_lhash),
  75623. TEST_DECL(test_wolfSSL_certs),
  75624. TEST_DECL(test_wolfSSL_private_keys),
  75625. TEST_DECL(test_wolfSSL_PEM_def_callback),
  75626. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  75627. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  75628. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  75629. TEST_DECL(test_wolfSSL_PEM_PrivateKey_rsa),
  75630. TEST_DECL(test_wolfSSL_PEM_PrivateKey_ecc),
  75631. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dsa),
  75632. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dh),
  75633. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  75634. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  75635. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  75636. #ifndef NO_BIO
  75637. TEST_DECL(test_wolfSSL_BIO),
  75638. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  75639. TEST_DECL(test_wolfSSL_PEM_read_bio),
  75640. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  75641. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  75642. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  75643. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  75644. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  75645. #endif
  75646. /* EVP API testing */
  75647. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  75648. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  75649. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  75650. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  75651. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  75652. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  75653. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  75654. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  75655. TEST_DECL(test_wolfSSL_EVP_shake128),
  75656. TEST_DECL(test_wolfSSL_EVP_shake256),
  75657. TEST_DECL(test_wolfSSL_EVP_sm3),
  75658. TEST_DECL(test_EVP_blake2),
  75659. #ifdef OPENSSL_ALL
  75660. TEST_DECL(test_wolfSSL_EVP_md4),
  75661. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  75662. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  75663. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  75664. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  75665. #endif
  75666. TEST_DECL(test_EVP_MD_do_all),
  75667. TEST_DECL(test_wolfSSL_EVP_MD_size),
  75668. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  75669. TEST_DECL(test_wolfSSL_EVP_Digest),
  75670. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  75671. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  75672. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  75673. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  75674. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  75675. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  75676. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  75677. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  75678. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  75679. TEST_DECL(test_wolfssl_EVP_chacha20),
  75680. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  75681. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  75682. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  75683. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  75684. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  75685. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  75686. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  75687. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  75688. #ifdef OPENSSL_ALL
  75689. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  75690. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  75691. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  75692. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  75693. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  75694. TEST_DECL(test_wolfSSL_EVP_rc4),
  75695. TEST_DECL(test_wolfSSL_EVP_enc_null),
  75696. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  75697. TEST_DECL(test_wolfSSL_EVP_mdc2),
  75698. TEST_DECL(test_evp_cipher_aes_gcm),
  75699. #endif
  75700. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  75701. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  75702. #ifdef OPENSSL_EXTRA
  75703. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  75704. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  75705. #endif
  75706. #ifdef OPENSSL_ALL
  75707. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  75708. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  75709. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  75710. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  75711. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  75712. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  75713. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  75714. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  75715. #endif
  75716. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  75717. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  75718. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  75719. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  75720. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  75721. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  75722. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  75723. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  75724. #ifndef NO_BIO
  75725. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  75726. #endif
  75727. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  75728. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  75729. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  75730. #ifndef NO_BIO
  75731. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  75732. #endif /* !NO_BIO */
  75733. #endif
  75734. #ifdef OPENSSL_ALL
  75735. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  75736. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  75737. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  75738. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  75739. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  75740. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  75741. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  75742. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  75743. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  75744. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  75745. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  75746. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  75747. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  75748. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  75749. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  75750. #endif
  75751. TEST_DECL(test_EVP_PKEY_rsa),
  75752. TEST_DECL(test_EVP_PKEY_ec),
  75753. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  75754. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  75755. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  75756. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  75757. TEST_DECL(test_EVP_PKEY_cmp),
  75758. #ifdef OPENSSL_ALL
  75759. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  75760. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  75761. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  75762. #endif
  75763. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  75764. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  75765. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  75766. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  75767. #endif
  75768. #ifndef NO_BIO
  75769. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  75770. TEST_DECL(test_wolfSSL_GetLoggingCb),
  75771. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  75772. TEST_DECL(test_wc_ERR_remove_state),
  75773. TEST_DECL(test_wc_ERR_print_errors_fp),
  75774. #endif
  75775. TEST_DECL(test_wolfSSL_configure_args),
  75776. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  75777. TEST_DECL(test_wolfSSL_set1_curves_list),
  75778. TEST_DECL(test_wolfSSL_curves_mismatch),
  75779. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  75780. TEST_DECL(test_wolfSSL_OtherName),
  75781. TEST_DECL(test_wolfSSL_FPKI),
  75782. TEST_DECL(test_wolfSSL_URI),
  75783. TEST_DECL(test_wolfSSL_TBS),
  75784. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  75785. TEST_DECL(test_X509_STORE_untrusted),
  75786. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  75787. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  75788. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  75789. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  75790. TEST_DECL(test_wolfSSL_X509_Name_canon),
  75791. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  75792. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  75793. TEST_DECL(test_wolfSSL_X509_NID),
  75794. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  75795. TEST_DECL(test_wolfSSL_get0_param),
  75796. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  75797. TEST_DECL(test_wolfSSL_set1_host),
  75798. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  75799. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  75800. TEST_DECL(test_wolfSSL_X509_STORE),
  75801. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  75802. TEST_DECL(test_X509_STORE_get0_objects),
  75803. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  75804. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  75805. TEST_DECL(test_wolfSSL_X509_STORE_set_get_crl),
  75806. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  75807. TEST_DECL(test_wolfSSL_X509_cmp_time),
  75808. TEST_DECL(test_wolfSSL_X509_time_adj),
  75809. /* X509 tests */
  75810. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  75811. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  75812. TEST_DECL(test_wolfSSL_X509_check_host),
  75813. TEST_DECL(test_wolfSSL_X509_check_email),
  75814. TEST_DECL(test_wolfSSL_X509_check_private_key),
  75815. TEST_DECL(test_wolfSSL_X509),
  75816. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  75817. TEST_DECL(test_wolfSSL_X509_sign),
  75818. TEST_DECL(test_wolfSSL_X509_sign2),
  75819. TEST_DECL(test_wolfSSL_X509_verify),
  75820. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  75821. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  75822. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  75823. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  75824. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  75825. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  75826. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  75827. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  75828. TEST_DECL(test_wolfSSL_X509_NAME),
  75829. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  75830. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  75831. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  75832. TEST_DECL(test_wolfSSL_X509_set_name),
  75833. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  75834. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  75835. TEST_DECL(test_wolfSSL_X509_set_version),
  75836. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  75837. TEST_DECL(test_wolfSSL_X509_CRL),
  75838. TEST_DECL(test_wolfSSL_i2d_X509),
  75839. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  75840. TEST_DECL(test_wolfSSL_PEM_read_X509),
  75841. TEST_DECL(test_wolfSSL_X509_check_ca),
  75842. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  75843. TEST_DECL(test_wolfSSL_X509_bad_altname),
  75844. TEST_DECL(test_wolfSSL_X509_name_match),
  75845. TEST_DECL(test_wolfSSL_X509_name_match2),
  75846. TEST_DECL(test_wolfSSL_X509_name_match3),
  75847. TEST_DECL(test_wolfSSL_X509_max_altnames),
  75848. TEST_DECL(test_wolfSSL_X509_max_name_constraints),
  75849. TEST_DECL(test_wolfSSL_make_cert),
  75850. #ifndef NO_BIO
  75851. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  75852. TEST_DECL(test_wolfSSL_X509_INFO),
  75853. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  75854. #endif
  75855. #ifdef OPENSSL_ALL
  75856. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  75857. #endif
  75858. TEST_DECL(test_wolfSSL_X509_CA_num),
  75859. TEST_DECL(test_wolfSSL_X509_get_version),
  75860. #ifndef NO_BIO
  75861. TEST_DECL(test_wolfSSL_X509_print),
  75862. TEST_DECL(test_wolfSSL_X509_CRL_print),
  75863. #endif
  75864. TEST_DECL(test_X509_get_signature_nid),
  75865. /* X509 extension testing. */
  75866. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  75867. TEST_DECL(test_wolfSSL_X509_get_ext),
  75868. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  75869. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  75870. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  75871. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  75872. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  75873. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  75874. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  75875. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  75876. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  75877. TEST_DECL(test_wolfSSL_X509V3_EXT),
  75878. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  75879. TEST_DECL(test_wolfSSL_X509_cmp),
  75880. TEST_DECL(test_GENERAL_NAME_set0_othername),
  75881. TEST_DECL(test_othername_and_SID_ext),
  75882. TEST_DECL(test_wolfSSL_dup_CA_list),
  75883. /* OpenSSL sk_X509 API test */
  75884. TEST_DECL(test_sk_X509),
  75885. /* OpenSSL sk_X509_CRL API test */
  75886. TEST_DECL(test_sk_X509_CRL),
  75887. /* OpenSSL X509 REQ API test */
  75888. TEST_DECL(test_X509_REQ),
  75889. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  75890. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  75891. TEST_DECL(test_X509_LOOKUP_add_dir),
  75892. /* RAND compatibility API */
  75893. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  75894. TEST_DECL(test_wolfSSL_RAND_bytes),
  75895. TEST_DECL(test_wolfSSL_RAND),
  75896. /* BN compatibility API */
  75897. TEST_DECL(test_wolfSSL_BN_CTX),
  75898. TEST_DECL(test_wolfSSL_BN),
  75899. TEST_DECL(test_wolfSSL_BN_init),
  75900. TEST_DECL(test_wolfSSL_BN_enc_dec),
  75901. TEST_DECL(test_wolfSSL_BN_word),
  75902. TEST_DECL(test_wolfSSL_BN_bits),
  75903. TEST_DECL(test_wolfSSL_BN_shift),
  75904. TEST_DECL(test_wolfSSL_BN_math),
  75905. TEST_DECL(test_wolfSSL_BN_math_mod),
  75906. TEST_DECL(test_wolfSSL_BN_math_other),
  75907. TEST_DECL(test_wolfSSL_BN_rand),
  75908. TEST_DECL(test_wolfSSL_BN_prime),
  75909. /* OpenSSL PKCS5 API test */
  75910. TEST_DECL(test_wolfSSL_PKCS5),
  75911. /* OpenSSL PKCS8 API test */
  75912. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  75913. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  75914. /* OpenSSL PKCS7 API test */
  75915. TEST_DECL(test_wolfssl_PKCS7),
  75916. TEST_DECL(test_wolfSSL_PKCS7_certs),
  75917. TEST_DECL(test_wolfSSL_PKCS7_sign),
  75918. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  75919. #ifndef NO_BIO
  75920. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  75921. #ifdef HAVE_SMIME
  75922. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  75923. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  75924. #endif /* HAVE_SMIME */
  75925. #endif /* !NO_BIO */
  75926. /* OpenSSL PKCS12 API test */
  75927. TEST_DECL(test_wolfSSL_PKCS12),
  75928. /* Can't memory test as callbacks use Assert. */
  75929. TEST_DECL(test_error_queue_per_thread),
  75930. TEST_DECL(test_wolfSSL_ERR_put_error),
  75931. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  75932. #ifndef NO_BIO
  75933. TEST_DECL(test_wolfSSL_ERR_print_errors),
  75934. #endif
  75935. TEST_DECL(test_OBJ_NAME_do_all),
  75936. TEST_DECL(test_wolfSSL_OBJ),
  75937. TEST_DECL(test_wolfSSL_OBJ_cmp),
  75938. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  75939. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  75940. #ifdef OPENSSL_ALL
  75941. TEST_DECL(test_wolfSSL_OBJ_ln),
  75942. TEST_DECL(test_wolfSSL_OBJ_sn),
  75943. #endif
  75944. #ifndef NO_BIO
  75945. TEST_DECL(test_wolfSSL_BIO_gets),
  75946. TEST_DECL(test_wolfSSL_BIO_puts),
  75947. TEST_DECL(test_wolfSSL_BIO_dump),
  75948. /* Can't memory test as server hangs. */
  75949. TEST_DECL(test_wolfSSL_BIO_should_retry),
  75950. TEST_DECL(test_wolfSSL_BIO_write),
  75951. TEST_DECL(test_wolfSSL_BIO_printf),
  75952. TEST_DECL(test_wolfSSL_BIO_f_md),
  75953. TEST_DECL(test_wolfSSL_BIO_up_ref),
  75954. TEST_DECL(test_wolfSSL_BIO_reset),
  75955. TEST_DECL(test_wolfSSL_BIO_get_len),
  75956. #endif
  75957. TEST_DECL(test_wolfSSL_check_domain),
  75958. TEST_DECL(test_wolfSSL_cert_cb),
  75959. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  75960. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  75961. TEST_DECL(test_wolfSSL_sigalg_info),
  75962. /* Can't memory test as tcp_connect aborts. */
  75963. TEST_DECL(test_wolfSSL_SESSION),
  75964. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  75965. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  75966. TEST_DECL(test_wolfSSL_ticket_keys),
  75967. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  75968. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  75969. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  75970. TEST_DECL(test_wolfSSL_verify_mode),
  75971. TEST_DECL(test_wolfSSL_verify_depth),
  75972. TEST_DECL(test_wolfSSL_verify_result),
  75973. TEST_DECL(test_wolfSSL_msg_callback),
  75974. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  75975. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  75976. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  75977. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  75978. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  75979. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  75980. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  75981. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  75982. TEST_DECL(test_wolfSSL_PEM_read),
  75983. TEST_DECL(test_wolfSSL_OpenSSL_version),
  75984. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  75985. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  75986. TEST_DECL(test_CONF_modules_xxx),
  75987. #ifdef OPENSSL_ALL
  75988. TEST_DECL(test_wolfSSL_TXT_DB),
  75989. TEST_DECL(test_wolfSSL_NCONF),
  75990. #endif
  75991. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  75992. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  75993. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  75994. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  75995. TEST_DECL(test_CRYPTO_THREADID_xxx),
  75996. TEST_DECL(test_ENGINE_cleanup),
  75997. /* test the no op functions for compatibility */
  75998. TEST_DECL(test_no_op_functions),
  75999. /* OpenSSL error API tests */
  76000. TEST_DECL(test_ERR_load_crypto_strings),
  76001. #ifdef OPENSSL_ALL
  76002. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  76003. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  76004. TEST_DECL(test_wolfSSL_CTX_ctrl),
  76005. #endif /* OPENSSL_ALL */
  76006. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  76007. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  76008. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  76009. /*********************************
  76010. * Crypto API tests
  76011. *********************************/
  76012. TEST_DECL(test_wolfSSL_MD4),
  76013. TEST_DECL(test_wolfSSL_MD5),
  76014. TEST_DECL(test_wolfSSL_MD5_Transform),
  76015. TEST_DECL(test_wolfSSL_SHA),
  76016. TEST_DECL(test_wolfSSL_SHA_Transform),
  76017. TEST_DECL(test_wolfSSL_SHA224),
  76018. TEST_DECL(test_wolfSSL_SHA256),
  76019. TEST_DECL(test_wolfSSL_SHA256_Transform),
  76020. TEST_DECL(test_wolfSSL_SHA512_Transform),
  76021. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  76022. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  76023. TEST_DECL(test_wolfSSL_HMAC_CTX),
  76024. TEST_DECL(test_wolfSSL_HMAC),
  76025. TEST_DECL(test_wolfSSL_CMAC),
  76026. TEST_DECL(test_wolfSSL_DES),
  76027. TEST_DECL(test_wolfSSL_DES_ncbc),
  76028. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  76029. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  76030. TEST_DECL(test_wolfSSL_AES_encrypt),
  76031. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  76032. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  76033. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  76034. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  76035. TEST_DECL(test_wolfSSL_RC4),
  76036. TEST_DECL(test_wolfSSL_RSA),
  76037. TEST_DECL(test_wolfSSL_RSA_DER),
  76038. TEST_DECL(test_wolfSSL_RSA_print),
  76039. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  76040. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  76041. TEST_DECL(test_wolfSSL_RSA_get0_key),
  76042. TEST_DECL(test_wolfSSL_RSA_meth),
  76043. TEST_DECL(test_wolfSSL_RSA_verify),
  76044. TEST_DECL(test_wolfSSL_RSA_sign),
  76045. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  76046. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  76047. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  76048. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  76049. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  76050. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  76051. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  76052. TEST_DECL(test_wolfSSL_RSA_ex_data),
  76053. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  76054. TEST_DECL(test_wolfSSL_RSA_To_Der),
  76055. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  76056. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  76057. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  76058. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  76059. TEST_DECL(test_wolfSSL_DH),
  76060. TEST_DECL(test_wolfSSL_DH_dup),
  76061. TEST_DECL(test_wolfSSL_DH_check),
  76062. TEST_DECL(test_wolfSSL_DH_prime),
  76063. TEST_DECL(test_wolfSSL_DH_1536_prime),
  76064. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  76065. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  76066. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  76067. TEST_DECL(test_wolfSSL_d2i_DHparams),
  76068. TEST_DECL(test_wolfSSL_DH_LoadDer),
  76069. TEST_DECL(test_wolfSSL_i2d_DHparams),
  76070. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  76071. TEST_DECL(test_wolfSSL_EC_GROUP),
  76072. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  76073. TEST_DECL(test_wolfSSL_EC_POINT),
  76074. TEST_DECL(test_wolfSSL_SPAKE),
  76075. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  76076. TEST_DECL(test_EC_i2d),
  76077. TEST_DECL(test_wolfSSL_EC_curve),
  76078. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  76079. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  76080. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  76081. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  76082. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  76083. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  76084. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  76085. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  76086. TEST_DECL(test_ECDSA_size_sign),
  76087. TEST_DECL(test_ECDH_compute_key),
  76088. #endif
  76089. #ifdef OPENSSL_EXTRA
  76090. TEST_DECL(test_EC25519),
  76091. TEST_DECL(test_ED25519),
  76092. TEST_DECL(test_EC448),
  76093. TEST_DECL(test_ED448),
  76094. #endif
  76095. TEST_DECL(test_DSA_do_sign_verify),
  76096. #ifdef OPENSSL_ALL
  76097. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  76098. TEST_DECL(test_wolfSSL_DSA_SIG),
  76099. #endif
  76100. TEST_DECL(test_openssl_generate_key_and_cert),
  76101. TEST_DECL(test_wolfSSL_FIPS_mode),
  76102. TEST_DECL(test_openssl_FIPS_drbg),
  76103. /*********************************
  76104. * CertManager API tests
  76105. *********************************/
  76106. TEST_DECL(test_wolfSSL_CertManagerAPI),
  76107. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  76108. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  76109. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  76110. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  76111. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  76112. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  76113. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  76114. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  76115. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  76116. TEST_DECL(test_wolfSSL_CertManagerCRL),
  76117. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  76118. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  76119. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  76120. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  76121. !defined(WOLFSSL_NO_CLIENT_AUTH))
  76122. TEST_DECL(test_various_pathlen_chains),
  76123. #endif
  76124. /*********************************
  76125. * SSL/TLS API tests
  76126. *********************************/
  76127. TEST_DECL(test_wolfSSL_Method_Allocators),
  76128. #ifndef NO_WOLFSSL_SERVER
  76129. TEST_DECL(test_wolfSSL_CTX_new),
  76130. #endif
  76131. TEST_DECL(test_server_wolfSSL_new),
  76132. TEST_DECL(test_client_wolfSSL_new),
  76133. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  76134. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  76135. TEST_DECL(test_for_double_Free),
  76136. #endif
  76137. TEST_DECL(test_wolfSSL_set_options),
  76138. #ifdef WOLFSSL_TLS13
  76139. /* TLS v1.3 API tests */
  76140. TEST_DECL(test_tls13_apis),
  76141. TEST_DECL(test_tls13_cipher_suites),
  76142. #endif
  76143. TEST_DECL(test_wolfSSL_tmp_dh),
  76144. TEST_DECL(test_wolfSSL_ctrl),
  76145. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  76146. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  76147. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  76148. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  76149. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  76150. #endif
  76151. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  76152. TEST_DECL(test_wolfSSL_security_level),
  76153. TEST_DECL(test_wolfSSL_SSL_in_init),
  76154. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  76155. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  76156. TEST_DECL(test_CONF_CTX_FILE),
  76157. TEST_DECL(test_CONF_CTX_CMDLINE),
  76158. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  76159. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  76160. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  76161. /* Bad certificate signature tests */
  76162. TEST_DECL(test_EccSigFailure_cm),
  76163. TEST_DECL(test_RsaSigFailure_cm),
  76164. #endif /* NO_CERTS */
  76165. /* PKCS8 testing */
  76166. TEST_DECL(test_wolfSSL_no_password_cb),
  76167. TEST_DECL(test_wolfSSL_PKCS8),
  76168. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  76169. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  76170. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  76171. TEST_DECL(test_wolfSSL_get_finished),
  76172. /* Uses Assert in handshake callback. */
  76173. TEST_DECL(test_wolfSSL_CTX_add_session),
  76174. /* Large number of memory allocations. */
  76175. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  76176. /* Large number of memory allocations. */
  76177. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  76178. /* Large number of memory allocations. */
  76179. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  76180. /* Large number of memory allocations. */
  76181. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  76182. /* Large number of memory allocations. */
  76183. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  76184. /* Large number of memory allocations. */
  76185. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  76186. #endif
  76187. TEST_DECL(test_SSL_CIPHER_get_xxx),
  76188. TEST_DECL(test_wolfSSL_ERR_strings),
  76189. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  76190. TEST_DECL(test_wolfSSL_CTX_use_certificate),
  76191. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  76192. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  76193. TEST_DECL(test_wolfSSL_use_certificate_buffer),
  76194. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  76195. TEST_DECL(test_wolfSSL_CTX_use_RSAPrivateKey_file),
  76196. TEST_DECL(test_wolfSSL_use_RSAPrivateKey_file),
  76197. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey),
  76198. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  76199. /* Large number of memory allocations. */
  76200. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  76201. TEST_DECL(test_wolfSSL_CertRsaPss),
  76202. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  76203. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  76204. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  76205. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  76206. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_buffer_format),
  76207. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  76208. TEST_DECL(test_wolfSSL_use_certificate_chain_file),
  76209. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  76210. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  76211. TEST_DECL(test_multiple_crls_same_issuer),
  76212. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  76213. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  76214. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  76215. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  76216. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  76217. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  76218. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  76219. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  76220. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  76221. TEST_DECL(test_SetTmpEC_DHE_Sz),
  76222. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  76223. #ifdef WOLFSSL_DTLS
  76224. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  76225. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  76226. #endif
  76227. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  76228. /* Uses Assert in handshake callback. */
  76229. TEST_DECL(test_wolfSSL_dtls_plaintext),
  76230. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  76231. defined(HAVE_IO_TESTS_DEPENDENCIES)
  76232. TEST_DECL(test_wolfSSL_read_write),
  76233. /* Can't memory test as server hangs if client fails before second connect.
  76234. */
  76235. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  76236. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  76237. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  76238. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  76239. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  76240. /* Can't memory test as server hangs. */
  76241. TEST_DECL(test_wolfSSL_dtls_export),
  76242. /* Uses Assert in handshake callback. */
  76243. TEST_DECL(test_wolfSSL_tls_export),
  76244. #endif
  76245. TEST_DECL(test_wolfSSL_dtls_export_peers),
  76246. TEST_DECL(test_wolfSSL_SetMinVersion),
  76247. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  76248. /* wolfSSL handshake APIs. */
  76249. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  76250. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  76251. TEST_DECL(test_wolfSSL_BUF),
  76252. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  76253. /* Can't memory test as server hangs. */
  76254. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  76255. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  76256. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  76257. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  76258. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  76259. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  76260. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  76261. /* Can't memory test as server hangs. */
  76262. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  76263. TEST_DECL(test_wolfSSL_Tls13_postauth),
  76264. TEST_DECL(test_wolfSSL_set_ecdh_auto),
  76265. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  76266. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  76267. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  76268. TEST_DECL(test_wolfSSL_THREADID_hash),
  76269. /* TLS extensions tests */
  76270. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  76271. #ifdef HAVE_SNI
  76272. TEST_DECL(test_wolfSSL_UseSNI_params),
  76273. /* Uses Assert in handshake callback. */
  76274. TEST_DECL(test_wolfSSL_UseSNI_connection),
  76275. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  76276. #endif /* HAVE_SNI */
  76277. #endif
  76278. TEST_DECL(test_wolfSSL_UseTrustedCA),
  76279. TEST_DECL(test_wolfSSL_UseMaxFragment),
  76280. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  76281. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  76282. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  76283. /* Uses Assert in handshake callback. */
  76284. TEST_DECL(test_wolfSSL_UseALPN_connection),
  76285. TEST_DECL(test_wolfSSL_UseALPN_params),
  76286. #endif
  76287. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  76288. /* Uses Assert in handshake callback. */
  76289. TEST_DECL(test_wolfSSL_set_alpn_protos),
  76290. #endif
  76291. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  76292. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  76293. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  76294. TEST_DECL(test_tls_ext_duplicate),
  76295. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  76296. defined(HAVE_IO_TESTS_DEPENDENCIES)
  76297. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  76298. /* Uses Assert in handshake callback. */
  76299. TEST_DECL(test_wolfSSL_Tls13_ECH),
  76300. #endif
  76301. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  76302. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  76303. /* OCSP Stapling */
  76304. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  76305. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  76306. TEST_DECL(test_self_signed_stapling),
  76307. /* Multicast */
  76308. TEST_DECL(test_wolfSSL_mcast),
  76309. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  76310. TEST_DECL(test_wolfSSL_msgCb),
  76311. TEST_DECL(test_wolfSSL_either_side),
  76312. TEST_DECL(test_wolfSSL_DTLS_either_side),
  76313. /* Uses Assert in handshake callback. */
  76314. TEST_DECL(test_wolfSSL_dtls_fragments),
  76315. /* Uses Assert in handshake callback. */
  76316. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  76317. /* Uses Assert in handshake callback. */
  76318. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  76319. /* Uses Assert in handshake callback. */
  76320. TEST_DECL(test_wolfSSL_dtls_bad_record),
  76321. /* Uses Assert in handshake callback. */
  76322. TEST_DECL(test_wolfSSL_dtls_stateless),
  76323. TEST_DECL(test_generate_cookie),
  76324. #ifndef NO_BIO
  76325. /* Can't memory test as server hangs. */
  76326. TEST_DECL(test_wolfSSL_BIO_connect),
  76327. /* Can't memory test as server Asserts in thread. */
  76328. TEST_DECL(test_wolfSSL_BIO_accept),
  76329. TEST_DECL(test_wolfSSL_BIO_tls),
  76330. TEST_DECL(test_wolfSSL_BIO_datagram),
  76331. #endif
  76332. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  76333. TEST_DECL(test_DhCallbacks),
  76334. #endif
  76335. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  76336. TEST_DECL(test_export_keying_material),
  76337. #endif
  76338. /* Can't memory test as client/server Asserts in thread. */
  76339. TEST_DECL(test_ticket_and_psk_mixing),
  76340. /* Can't memory test as client/server Asserts in thread. */
  76341. TEST_DECL(test_prioritize_psk),
  76342. /* Can't memory test as client/server hangs. */
  76343. TEST_DECL(test_wc_CryptoCb),
  76344. /* Can't memory test as client/server hangs. */
  76345. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  76346. #if !defined(NO_FILESYSTEM) && \
  76347. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  76348. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  76349. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  76350. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  76351. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  76352. #ifdef HAVE_MAX_FRAGMENT
  76353. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  76354. #endif /* HAVE_MAX_FRAGMENT */
  76355. #ifndef NO_RSA
  76356. TEST_DECL(test_wolfSSL_dtls_stateless2),
  76357. #if !defined(NO_OLD_TLS)
  76358. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  76359. #endif /* !defined(NO_OLD_TLS) */
  76360. #endif /* ! NO_RSA */
  76361. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  76362. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  76363. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  76364. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  76365. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  76366. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  76367. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  76368. && defined(WOLFSSL_TLS13) && \
  76369. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  76370. TEST_DECL(test_ticket_nonce_malloc),
  76371. #endif
  76372. TEST_DECL(test_ticket_ret_create),
  76373. TEST_DECL(test_wrong_cs_downgrade),
  76374. TEST_DECL(test_extra_alerts_wrong_cs),
  76375. TEST_DECL(test_extra_alerts_skip_hs),
  76376. TEST_DECL(test_extra_alerts_bad_psk),
  76377. TEST_DECL(test_tls13_bad_psk_binder),
  76378. /* Can't memory test as client/server Asserts. */
  76379. TEST_DECL(test_harden_no_secure_renegotiation),
  76380. TEST_DECL(test_override_alt_cert_chain),
  76381. TEST_DECL(test_rpk_set_xxx_cert_type),
  76382. TEST_DECL(test_tls13_rpk_handshake),
  76383. TEST_DECL(test_dtls13_bad_epoch_ch),
  76384. TEST_DECL(test_short_session_id),
  76385. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  76386. /* Can't memory test as client/server hangs. */
  76387. TEST_DECL(test_dtls_msg_from_other_peer),
  76388. TEST_DECL(test_dtls_ipv6_check),
  76389. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  76390. TEST_DECL(test_dtls_no_extensions),
  76391. TEST_DECL(test_tls_alert_no_server_hello),
  76392. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  76393. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  76394. TEST_DECL(test_session_ticket_no_id),
  76395. TEST_DECL(test_session_ticket_hs_update),
  76396. TEST_DECL(test_dtls_downgrade_scr_server),
  76397. TEST_DECL(test_dtls_downgrade_scr),
  76398. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  76399. TEST_DECL(test_dtls_client_hello_timeout),
  76400. TEST_DECL(test_dtls_dropped_ccs),
  76401. TEST_DECL(test_dtls_seq_num_downgrade),
  76402. TEST_DECL(test_certreq_sighash_algos),
  76403. TEST_DECL(test_revoked_loaded_int_cert),
  76404. TEST_DECL(test_dtls_frag_ch),
  76405. TEST_DECL(test_dtls13_frag_ch_pq),
  76406. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  76407. TEST_DECL(test_dtls_old_seq_number),
  76408. TEST_DECL(test_tls13_pq_groups),
  76409. TEST_DECL(test_tls13_early_data),
  76410. TEST_DECL(test_tls_multi_handshakes_one_record),
  76411. TEST_DECL(test_write_dup),
  76412. TEST_DECL(test_read_write_hs),
  76413. TEST_DECL(test_get_signature_nid),
  76414. TEST_DECL(test_tls_cert_store_unchanged),
  76415. TEST_DECL(test_wolfSSL_SendUserCanceled),
  76416. /* This test needs to stay at the end to clean up any caches allocated. */
  76417. TEST_DECL(test_wolfSSL_Cleanup)
  76418. };
  76419. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  76420. static void TestSetup(void)
  76421. {
  76422. /* Stub, for now. Add common test setup code here. */
  76423. }
  76424. static void TestCleanup(void)
  76425. {
  76426. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  76427. /* Clear any errors added to the error queue during the test run. */
  76428. wolfSSL_ERR_clear_error();
  76429. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  76430. }
  76431. /* Print out all API test cases with numeric identifier.
  76432. */
  76433. void ApiTest_PrintTestCases(void)
  76434. {
  76435. int i;
  76436. printf("All Test Cases:\n");
  76437. for (i = 0; i < TEST_CASE_CNT; i++) {
  76438. printf("%3d: %s\n", i + 1, testCases[i].name);
  76439. }
  76440. }
  76441. /* Add test case with index to the list to run.
  76442. *
  76443. * @param [in] idx Index of test case to run starting at 1.
  76444. * @return 0 on success.
  76445. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  76446. */
  76447. int ApiTest_RunIdx(int idx)
  76448. {
  76449. if (idx < 1 || idx > TEST_CASE_CNT) {
  76450. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  76451. return BAD_FUNC_ARG;
  76452. }
  76453. testAll = 0;
  76454. testCases[idx-1].run = 1;
  76455. return 0;
  76456. }
  76457. /* Add test case with name to the list to run.
  76458. *
  76459. * @param [in] name Name of test case to run.
  76460. * @return 0 on success.
  76461. * @return BAD_FUNC_ARG when name is not a known test case name.
  76462. */
  76463. int ApiTest_RunName(char* name)
  76464. {
  76465. int i;
  76466. for (i = 0; i < TEST_CASE_CNT; i++) {
  76467. if (XSTRCMP(testCases[i].name, name) == 0) {
  76468. testAll = 0;
  76469. testCases[i].run = 1;
  76470. return 0;
  76471. }
  76472. }
  76473. printf("Test case name not found: %s\n", name);
  76474. printf("Use --list to see all test case names.\n");
  76475. return BAD_FUNC_ARG;
  76476. }
  76477. /* Converts the result code to a string.
  76478. *
  76479. * @param [in] res Test result code.
  76480. * @return String describing test result.
  76481. */
  76482. static const char* apitest_res_string(int res)
  76483. {
  76484. const char* str = "invalid result";
  76485. switch (res) {
  76486. case TEST_SUCCESS:
  76487. str = "passed";
  76488. break;
  76489. case TEST_FAIL:
  76490. str = "failed";
  76491. break;
  76492. case TEST_SKIPPED:
  76493. str = "skipped";
  76494. break;
  76495. }
  76496. return str;
  76497. }
  76498. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  76499. static double gettime_secs(void)
  76500. #if defined(_MSC_VER) && defined(_WIN32)
  76501. {
  76502. /* there's no gettimeofday for Windows, so we'll use system time */
  76503. #define EPOCH_DIFF 11644473600LL
  76504. FILETIME currentFileTime;
  76505. GetSystemTimePreciseAsFileTime(&currentFileTime);
  76506. ULARGE_INTEGER uli = { 0, 0 };
  76507. uli.LowPart = currentFileTime.dwLowDateTime;
  76508. uli.HighPart = currentFileTime.dwHighDateTime;
  76509. /* Convert to seconds since Unix epoch */
  76510. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  76511. }
  76512. #else
  76513. {
  76514. struct timeval tv;
  76515. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  76516. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  76517. }
  76518. #endif
  76519. #endif
  76520. int ApiTest(void)
  76521. {
  76522. int i;
  76523. int ret;
  76524. int res = 0;
  76525. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  76526. double timeDiff;
  76527. #endif
  76528. printf(" Begin API Tests\n");
  76529. fflush(stdout);
  76530. /* we must perform init and cleanup if not all tests are running */
  76531. if (!testAll) {
  76532. #ifdef WOLFCRYPT_ONLY
  76533. if (wolfCrypt_Init() != 0) {
  76534. printf("wolfCrypt Initialization failed\n");
  76535. res = 1;
  76536. }
  76537. #else
  76538. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  76539. printf("wolfSSL Initialization failed\n");
  76540. res = 1;
  76541. }
  76542. #endif
  76543. }
  76544. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  76545. if (res == 0) {
  76546. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  76547. printf("failed to create tmp dir\n");
  76548. res = 1;
  76549. }
  76550. else {
  76551. tmpDirNameSet = 1;
  76552. }
  76553. }
  76554. #endif
  76555. if (res == 0) {
  76556. for (i = 0; i < TEST_CASE_CNT; ++i) {
  76557. EXPECT_DECLS;
  76558. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  76559. currentTestName = testCases[i].name;
  76560. #endif
  76561. /* When not testing all cases then skip if not marked for running.
  76562. */
  76563. if (!testAll && !testCases[i].run) {
  76564. continue;
  76565. }
  76566. TestSetup();
  76567. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  76568. fflush(stdout);
  76569. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  76570. timeDiff = gettime_secs();
  76571. #endif
  76572. ret = testCases[i].func();
  76573. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  76574. timeDiff = gettime_secs() - timeDiff;
  76575. #endif
  76576. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  76577. if (ret != TEST_SKIPPED) {
  76578. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  76579. }
  76580. else
  76581. #endif
  76582. {
  76583. printf(" %s\n", apitest_res_string(ret));
  76584. }
  76585. fflush(stdout);
  76586. /* if return code is < 0 and not skipped then assert error */
  76587. Expect((ret > 0 || ret == TEST_SKIPPED),
  76588. ("Test failed\n"),
  76589. ("ret %d", ret));
  76590. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  76591. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  76592. TestCleanup();
  76593. }
  76594. }
  76595. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  76596. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  76597. wc_ecc_fp_free(); /* free per thread cache */
  76598. #endif
  76599. if (!testAll) {
  76600. #ifdef WOLFCRYPT_ONLY
  76601. wolfCrypt_Cleanup();
  76602. #else
  76603. wolfSSL_Cleanup();
  76604. #endif
  76605. }
  76606. (void)testDevId;
  76607. if (res != 0) {
  76608. printf("\nFAILURES:\n");
  76609. for (i = 0; i < TEST_CASE_CNT; ++i) {
  76610. if (testCases[i].fail) {
  76611. printf(" %3d: %s\n", i + 1, testCases[i].name);
  76612. }
  76613. }
  76614. printf("\n");
  76615. fflush(stdout);
  76616. }
  76617. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  76618. if (tmpDirNameSet) {
  76619. printf("\nBinary dumps of the memio streams can be found in the\n"
  76620. "%s directory. This can be imported into\n"
  76621. "Wireshark by transforming the file with\n"
  76622. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  76623. "And then loading test_output.dump.hex into Wireshark using\n"
  76624. "the \"Import from Hex Dump...\" option and selecting the\n"
  76625. "TCP encapsulation option.\n", tmpDirName);
  76626. }
  76627. #endif
  76628. printf(" End API Tests\n");
  76629. fflush(stdout);
  76630. return res;
  76631. }